1 /* 2 * $Id: iforce.h,v 1.13 2002/07/07 10:22:50 jdeneux Exp $ 3 * 4 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> 5 * Copyright (c) 2001-2002 Johann Deneux <deneux@ifrance.com> 6 * 7 * USB/RS232 I-Force joysticks and wheels. 8 */ 9 10 /* 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 * 25 * Should you need to contact me, the author, you can do so either by 26 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: 27 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic 28 */ 29 30 #include <linux/kernel.h> 31 #include <linux/slab.h> 32 #include <linux/input.h> 33 #include <linux/module.h> 34 #include <linux/init.h> 35 #include <linux/spinlock.h> 36 #include <linux/usb.h> 37 #include <linux/serio.h> 38 #include <linux/config.h> 39 #include <linux/circ_buf.h> 40 #include <linux/mutex.h> 41 42 /* This module provides arbitrary resource management routines. 43 * I use it to manage the device's memory. 44 * Despite the name of this module, I am *not* going to access the ioports. 45 */ 46 #include <linux/ioport.h> 47 48 49 #define IFORCE_MAX_LENGTH 16 50 51 /* iforce::bus */ 52 #define IFORCE_232 1 53 #define IFORCE_USB 2 54 55 #define FALSE 0 56 #define TRUE 1 57 58 #define FF_EFFECTS_MAX 32 59 60 /* Each force feedback effect is made of one core effect, which can be 61 * associated to at most to effect modifiers 62 */ 63 #define FF_MOD1_IS_USED 0 64 #define FF_MOD2_IS_USED 1 65 #define FF_CORE_IS_USED 2 66 #define FF_CORE_IS_PLAYED 3 /* Effect is currently being played */ 67 #define FF_CORE_SHOULD_PLAY 4 /* User wants the effect to be played */ 68 #define FF_CORE_UPDATE 5 /* Effect is being updated */ 69 #define FF_MODCORE_MAX 5 70 71 #define CHECK_OWNERSHIP(i, iforce) \ 72 ((i) < FF_EFFECTS_MAX && i >= 0 && \ 73 test_bit(FF_CORE_IS_USED, (iforce)->core_effects[(i)].flags) && \ 74 (current->pid == 0 || \ 75 (iforce)->core_effects[(i)].owner == current->pid)) 76 77 struct iforce_core_effect { 78 /* Information about where modifiers are stored in the device's memory */ 79 struct resource mod1_chunk; 80 struct resource mod2_chunk; 81 unsigned long flags[NBITS(FF_MODCORE_MAX)]; 82 pid_t owner; 83 /* Used to keep track of parameters of an effect. They are needed 84 * to know what parts of an effect changed in an update operation. 85 * We try to send only parameter packets if possible, as sending 86 * effect parameter requires the effect to be stoped and restarted 87 */ 88 struct ff_effect effect; 89 }; 90 91 #define FF_CMD_EFFECT 0x010e 92 #define FF_CMD_ENVELOPE 0x0208 93 #define FF_CMD_MAGNITUDE 0x0303 94 #define FF_CMD_PERIOD 0x0407 95 #define FF_CMD_CONDITION 0x050a 96 97 #define FF_CMD_AUTOCENTER 0x4002 98 #define FF_CMD_PLAY 0x4103 99 #define FF_CMD_ENABLE 0x4201 100 #define FF_CMD_GAIN 0x4301 101 102 #define FF_CMD_QUERY 0xff01 103 104 /* Buffer for async write */ 105 #define XMIT_SIZE 256 106 #define XMIT_INC(var, n) (var)+=n; (var)&= XMIT_SIZE -1 107 /* iforce::xmit_flags */ 108 #define IFORCE_XMIT_RUNNING 0 109 #define IFORCE_XMIT_AGAIN 1 110 111 struct iforce_device { 112 u16 idvendor; 113 u16 idproduct; 114 char *name; 115 signed short *btn; 116 signed short *abs; 117 signed short *ff; 118 }; 119 120 struct iforce { 121 struct input_dev *dev; /* Input device interface */ 122 struct iforce_device *type; 123 int bus; 124 125 unsigned char data[IFORCE_MAX_LENGTH]; 126 unsigned char edata[IFORCE_MAX_LENGTH]; 127 u16 ecmd; 128 u16 expect_packet; 129 130 #ifdef CONFIG_JOYSTICK_IFORCE_232 131 struct serio *serio; /* RS232 transfer */ 132 int idx, pkt, len, id; 133 unsigned char csum; 134 #endif 135 #ifdef CONFIG_JOYSTICK_IFORCE_USB 136 struct usb_device *usbdev; /* USB transfer */ 137 struct urb *irq, *out, *ctrl; 138 struct usb_ctrlrequest cr; 139 #endif 140 spinlock_t xmit_lock; 141 /* Buffer used for asynchronous sending of bytes to the device */ 142 struct circ_buf xmit; 143 unsigned char xmit_data[XMIT_SIZE]; 144 long xmit_flags[1]; 145 146 /* Force Feedback */ 147 wait_queue_head_t wait; 148 struct resource device_memory; 149 struct iforce_core_effect core_effects[FF_EFFECTS_MAX]; 150 struct mutex mem_mutex; 151 }; 152 153 /* Get hi and low bytes of a 16-bits int */ 154 #define HI(a) ((unsigned char)((a) >> 8)) 155 #define LO(a) ((unsigned char)((a) & 0xff)) 156 157 /* For many parameters, it seems that 0x80 is a special value that should 158 * be avoided. Instead, we replace this value by 0x7f 159 */ 160 #define HIFIX80(a) ((unsigned char)(((a)<0? (a)+255 : (a))>>8)) 161 162 /* Encode a time value */ 163 #define TIME_SCALE(a) (a) 164 165 166 /* Public functions */ 167 /* iforce-serio.c */ 168 void iforce_serial_xmit(struct iforce *iforce); 169 170 /* iforce-usb.c */ 171 void iforce_usb_xmit(struct iforce *iforce); 172 void iforce_usb_delete(struct iforce *iforce); 173 174 /* iforce-main.c */ 175 int iforce_init_device(struct iforce *iforce); 176 void iforce_delete_device(struct iforce *iforce); 177 178 /* iforce-packets.c */ 179 int iforce_control_playback(struct iforce*, u16 id, unsigned int); 180 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data, struct pt_regs *regs); 181 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data); 182 void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data) ; 183 int iforce_get_id_packet(struct iforce *iforce, char *packet); 184 185 /* iforce-ff.c */ 186 int iforce_upload_periodic(struct iforce*, struct ff_effect*, int is_update); 187 int iforce_upload_constant(struct iforce*, struct ff_effect*, int is_update); 188 int iforce_upload_condition(struct iforce*, struct ff_effect*, int is_update); 189 190 /* Public variables */ 191 extern struct serio_driver iforce_serio_drv; 192 extern struct usb_driver iforce_usb_driver; 193