iforce.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:29k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * $Id: iforce.c,v 1.56 2001/05/27 14:41:26 jdeneux Exp $
  3.  *
  4.  *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
  5.  *  Copyright (c) 2001 Johann Deneux <deneux@ifrance.com>
  6.  *
  7.  *  USB/RS232 I-Force joysticks and wheels.
  8.  *
  9.  *  Sponsored by SuSE
  10.  */
  11. /*
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25.  *
  26.  * Should you need to contact me, the author, you can do so either by
  27.  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  28.  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  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. /* FF: This module provides arbitrary resource management routines.
  40.  * I use it to manage the device's memory.
  41.  * Despite the name of this module, I am *not* going to access the ioports.
  42.  */
  43. #include <linux/ioport.h>
  44. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>, Johann Deneux <deneux@ifrance.com>");
  45. MODULE_DESCRIPTION("USB/RS232 I-Force joysticks and wheels driver");
  46. MODULE_LICENSE("GPL");
  47. #define IFORCE_MAX_LENGTH 16
  48. #if defined(CONFIG_INPUT_IFORCE_232) || defined(CONFIG_INPUT_IFORCE_232_MODULE)
  49. #define IFORCE_232 1
  50. #endif
  51. #if defined(CONFIG_INPUT_IFORCE_USB) || defined(CONFIG_INPUT_IFORCE_USB_MODULE)
  52. #define IFORCE_USB 2
  53. #endif
  54. #define FF_EFFECTS_MAX 32
  55. /* Each force feedback effect is made of one core effect, which can be
  56.  * associated to at most to effect modifiers
  57.  */
  58. #define FF_MOD1_IS_USED 0
  59. #define FF_MOD2_IS_USED 1
  60. #define FF_CORE_IS_USED 2
  61. #define FF_CORE_IS_PLAYED 3
  62. #define FF_MODCORE_MAX 3
  63. struct iforce_core_effect {
  64. /* Information about where modifiers are stored in the device's memory */
  65. struct resource mod1_chunk;
  66. struct resource mod2_chunk;
  67. unsigned long flags[NBITS(FF_MODCORE_MAX)];
  68. };
  69. #define FF_CMD_EFFECT 0x010e
  70. #define FF_CMD_SHAPE 0x0208
  71. #define FF_CMD_MAGNITUDE 0x0303
  72. #define FF_CMD_PERIOD 0x0407
  73. #define FF_CMD_INTERACT 0x050a
  74. #define FF_CMD_AUTOCENTER 0x4002
  75. #define FF_CMD_PLAY 0x4103
  76. #define FF_CMD_ENABLE 0x4201
  77. #define FF_CMD_GAIN 0x4301
  78. #define FF_CMD_QUERY 0xff01
  79. static signed short btn_joystick[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
  80. BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A, BTN_B, BTN_C, BTN_DEAD, -1 };
  81. static signed short btn_wheel[] =    { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
  82. BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A, BTN_B, BTN_C, -1 };
  83. static signed short abs_joystick[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
  84. static signed short abs_wheel[] =    { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, -1 };
  85. static signed short ff_iforce[] =    { FF_PERIODIC, FF_CONSTANT, FF_SPRING, FF_FRICTION,
  86. FF_SQUARE, FF_TRIANGLE, FF_SINE, FF_SAW_UP, FF_SAW_DOWN, FF_GAIN, FF_AUTOCENTER, -1 };
  87. static struct iforce_device {
  88. u16 idvendor;
  89. u16 idproduct;
  90. char *name;
  91. signed short *btn;
  92. signed short *abs;
  93. signed short *ff;
  94. } iforce_device[] = {
  95. { 0x046d, 0xc281, "Logitech WingMan Force", btn_joystick, abs_joystick, ff_iforce },
  96. { 0x046d, 0xc291, "Logitech WingMan Formula Force", btn_wheel, abs_wheel, ff_iforce },
  97. { 0x05ef, 0x020a, "AVB Top Shot Pegasus", btn_joystick, abs_joystick, ff_iforce },
  98. { 0x05ef, 0x8884, "AVB Mag Turbo Force", btn_wheel, abs_wheel, ff_iforce },
  99. { 0x06f8, 0x0001, "Guillemot Race Leader Force Feedback", btn_wheel, abs_wheel, ff_iforce },
  100. { 0x0000, 0x0000, "Unknown I-Force Device [%04x:%04x]", btn_joystick, abs_joystick, ff_iforce }
  101. };
  102. struct iforce {
  103. struct input_dev dev; /* Input device interface */
  104. struct iforce_device *type;
  105. char name[64];
  106. int open;
  107. int bus;
  108. unsigned char data[IFORCE_MAX_LENGTH];
  109. unsigned char edata[IFORCE_MAX_LENGTH];
  110. u16 ecmd;
  111. u16 expect_packet;
  112. #ifdef IFORCE_232
  113. struct serio *serio; /* RS232 transfer */
  114. int idx, pkt, len, id;
  115. unsigned char csum;
  116. #endif
  117. #ifdef IFORCE_USB
  118. struct usb_device *usbdev; /* USB transfer */
  119. struct urb irq, out, ctrl;
  120. struct usb_ctrlrequest dr;
  121. #endif
  122. /* Force Feedback */
  123. wait_queue_head_t wait;
  124. struct resource device_memory;
  125. struct iforce_core_effect core_effects[FF_EFFECTS_MAX];
  126. };
  127. static struct {
  128. __s32 x;
  129. __s32 y;
  130. } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  131. /* Get hi and low bytes of a 16-bits int */
  132. #define HI(a) ((unsigned char)((a) >> 8))
  133. #define LO(a) ((unsigned char)((a) & 0xff))
  134. /* Encode a time value */
  135. #define TIME_SCALE(a) ((a) == 0xffff ? 0xffff : (a) * 1000 / 256)
  136. static void dump_packet(char *msg, u16 cmd, unsigned char *data)
  137. {
  138. int i;
  139. printk(KERN_DEBUG "iforce.c: %s ( cmd = %04x, data = ", msg, cmd);
  140. for (i = 0; i < LO(cmd); i++)
  141. printk("%02x ", data[i]);
  142. printk(")n");
  143. }
  144. /*
  145.  * Send a packet of bytes to the device
  146.  */
  147. static void send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
  148. {
  149. switch (iforce->bus) {
  150. #ifdef IFORCE_232
  151. case IFORCE_232: {
  152. int i;
  153. unsigned char csum = 0x2b ^ HI(cmd) ^ LO(cmd);
  154. serio_write(iforce->serio, 0x2b);
  155. serio_write(iforce->serio, HI(cmd));
  156. serio_write(iforce->serio, LO(cmd));
  157. for (i = 0; i < LO(cmd); i++) {
  158. serio_write(iforce->serio, data[i]);
  159. csum = csum ^ data[i];
  160. }
  161. serio_write(iforce->serio, csum);
  162. return;
  163. }
  164. #endif
  165. #ifdef IFORCE_USB
  166. case IFORCE_USB: {
  167. DECLARE_WAITQUEUE(wait, current);
  168. int timeout = HZ; /* 1 second */
  169. memcpy(iforce->out.transfer_buffer + 1, data, LO(cmd));
  170. ((char*)iforce->out.transfer_buffer)[0] = HI(cmd);
  171. iforce->out.transfer_buffer_length = LO(cmd) + 2;
  172. iforce->out.dev = iforce->usbdev;
  173. set_current_state(TASK_INTERRUPTIBLE);
  174. add_wait_queue(&iforce->wait, &wait);
  175. if (usb_submit_urb(&iforce->out)) {
  176. set_current_state(TASK_RUNNING);
  177. remove_wait_queue(&iforce->wait, &wait);
  178. return;
  179. }
  180. while (timeout && iforce->out.status == -EINPROGRESS)
  181. timeout = schedule_timeout(timeout);
  182. set_current_state(TASK_RUNNING);
  183. remove_wait_queue(&iforce->wait, &wait);
  184. if (!timeout)
  185. usb_unlink_urb(&iforce->out);
  186. return;
  187. }
  188. #endif
  189. }
  190. }
  191. static void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
  192. {
  193. struct input_dev *dev = &iforce->dev;
  194. int i;
  195. #ifdef IFORCE_232
  196. if (HI(iforce->expect_packet) == HI(cmd)) {
  197. iforce->expect_packet = 0;
  198. iforce->ecmd = cmd;
  199. memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
  200. if (waitqueue_active(&iforce->wait))
  201. wake_up(&iforce->wait);
  202. }
  203. #endif
  204. if (!iforce->type)
  205. return;
  206. switch (HI(cmd)) {
  207. case 0x01: /* joystick position data */
  208. case 0x03: /* wheel position data */
  209. if (HI(cmd) == 1) {
  210. input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
  211. input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
  212. input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
  213. } else {
  214. input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
  215. input_report_abs(dev, ABS_GAS,   255 - data[2]);
  216. input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
  217. }
  218. input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
  219. input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
  220. for (i = 0; iforce->type->btn[i] >= 0; i++)
  221. input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
  222. break;
  223. case 0x02: /* status report */
  224. input_report_key(dev, BTN_DEAD, data[0] & 0x02);
  225. break;
  226. }
  227. }
  228. static int get_id_packet(struct iforce *iforce, char *packet)
  229. {
  230. DECLARE_WAITQUEUE(wait, current);
  231. int timeout = HZ; /* 1 second */
  232. switch (iforce->bus) {
  233. #ifdef IFORCE_USB
  234. case IFORCE_USB:
  235. iforce->dr.bRequest = packet[0];
  236. iforce->ctrl.dev = iforce->usbdev;
  237. set_current_state(TASK_INTERRUPTIBLE);
  238. add_wait_queue(&iforce->wait, &wait);
  239. if (usb_submit_urb(&iforce->ctrl)) {
  240. set_current_state(TASK_RUNNING);
  241. remove_wait_queue(&iforce->wait, &wait);
  242. return -1;
  243. }
  244. while (timeout && iforce->ctrl.status == -EINPROGRESS)
  245. timeout = schedule_timeout(timeout);
  246. set_current_state(TASK_RUNNING);
  247. remove_wait_queue(&iforce->wait, &wait);
  248. if (!timeout) {
  249. usb_unlink_urb(&iforce->ctrl);
  250. return -1;
  251. }
  252. break;
  253. #endif
  254. #ifdef IFORCE_232
  255. case IFORCE_232:
  256. iforce->expect_packet = FF_CMD_QUERY;
  257. send_packet(iforce, FF_CMD_QUERY, packet);
  258. set_current_state(TASK_INTERRUPTIBLE);
  259. add_wait_queue(&iforce->wait, &wait);
  260. while (timeout && iforce->expect_packet)
  261. timeout = schedule_timeout(timeout);
  262. set_current_state(TASK_RUNNING);
  263. remove_wait_queue(&iforce->wait, &wait);
  264. if (!timeout) {
  265. iforce->expect_packet = 0;
  266. return -1;
  267. }
  268. break;
  269. #endif
  270. }
  271. return -(iforce->edata[0] != packet[0]);
  272. }
  273. static int iforce_open(struct input_dev *dev)
  274. {
  275. struct iforce *iforce = dev->private;
  276. switch (iforce->bus) {
  277. #ifdef IFORCE_USB
  278. case IFORCE_USB:
  279. if (iforce->open++)
  280. break;
  281. iforce->irq.dev = iforce->usbdev;
  282. if (usb_submit_urb(&iforce->irq))
  283. return -EIO;
  284. break;
  285. #endif
  286. }
  287. return 0;
  288. }
  289. static void iforce_close(struct input_dev *dev)
  290. {
  291. struct iforce *iforce = dev->private;
  292. switch (iforce->bus) {
  293. #ifdef IFORCE_USB
  294. case IFORCE_USB:
  295. if (!--iforce->open)
  296. usb_unlink_urb(&iforce->irq);
  297. break;
  298. #endif
  299. }
  300. }
  301. /*
  302.  * Start or stop playing an effect
  303.  */
  304. static int iforce_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  305. {
  306. struct iforce* iforce = (struct iforce*)(dev->private);
  307. unsigned char data[3];
  308. printk(KERN_DEBUG "iforce.c: input_event(type = %d, code = %d, value = %d)n", type, code, value);
  309. if (type != EV_FF)
  310. return -1;
  311. switch (code) {
  312. case FF_GAIN:
  313. data[0] = value >> 9;
  314. send_packet(iforce, FF_CMD_GAIN, data);
  315. return 0;
  316. case FF_AUTOCENTER:
  317. data[0] = 0x03;
  318. data[1] = value >> 9;
  319. send_packet(iforce, FF_CMD_AUTOCENTER, data);
  320. data[0] = 0x04;
  321. data[1] = 0x01;
  322. send_packet(iforce, FF_CMD_AUTOCENTER, data);
  323. return 0;
  324. default: /* Play an effect */
  325. if (code >= iforce->dev.ff_effects_max)
  326. return -1;
  327. data[0] = LO(code);
  328. data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
  329. data[2] = LO(value);
  330. send_packet(iforce, FF_CMD_PLAY, data);
  331. return 0;
  332. }
  333. return -1;
  334. }
  335. /*
  336.  * Set the magnitude of a constant force effect
  337.  * Return error code
  338.  *
  339.  * Note: caller must ensure exclusive access to device
  340.  */
  341. static int make_magnitude_modifier(struct iforce* iforce,
  342. struct resource* mod_chunk, __s16 level)
  343. {
  344. unsigned char data[3];
  345. if (allocate_resource(&(iforce->device_memory), mod_chunk, 2,
  346. iforce->device_memory.start, iforce->device_memory.end, 2L,
  347. NULL, NULL)) {
  348. return -ENOMEM;
  349. }
  350. data[0] = LO(mod_chunk->start);
  351. data[1] = HI(mod_chunk->start);
  352. data[2] = HI(level);
  353. send_packet(iforce, FF_CMD_MAGNITUDE, data);
  354. return 0;
  355. }
  356. /*
  357.  * Upload the component of an effect dealing with the period, phase and magnitude
  358.  */
  359. static int make_period_modifier(struct iforce* iforce, struct resource* mod_chunk,
  360. __s16 magnitude, __s16 offset, u16 period, u16 phase)
  361. {
  362. unsigned char data[7];
  363. period = TIME_SCALE(period);
  364. if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c,
  365. iforce->device_memory.start, iforce->device_memory.end, 2L,
  366. NULL, NULL)) {
  367. return -ENOMEM;
  368. }
  369. data[0] = LO(mod_chunk->start);
  370. data[1] = HI(mod_chunk->start);
  371. data[2] = HI(magnitude);
  372. data[3] = HI(offset);
  373. data[4] = HI(phase);
  374. data[5] = LO(period);
  375. data[6] = HI(period);
  376. send_packet(iforce, FF_CMD_PERIOD, data);
  377. return 0;
  378. }
  379. /*
  380.  * Uploads the part of an effect setting the shape of the force
  381.  */
  382. static int make_shape_modifier(struct iforce* iforce, struct resource* mod_chunk,
  383. u16 attack_duration, __s16 initial_level,
  384. u16 fade_duration, __s16 final_level)
  385. {
  386. unsigned char data[8];
  387. attack_duration = TIME_SCALE(attack_duration);
  388. fade_duration = TIME_SCALE(fade_duration);
  389. if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e,
  390. iforce->device_memory.start, iforce->device_memory.end, 2L,
  391. NULL, NULL)) {
  392. return -ENOMEM;
  393. }
  394. data[0] = LO(mod_chunk->start);
  395. data[1] = HI(mod_chunk->start);
  396. data[2] = LO(attack_duration);
  397. data[3] = HI(attack_duration);
  398. data[4] = HI(initial_level);
  399. data[5] = LO(fade_duration);
  400. data[6] = HI(fade_duration);
  401. data[7] = HI(final_level);
  402. send_packet(iforce, FF_CMD_SHAPE, data);
  403. return 0;
  404. }
  405. /*
  406.  * Component of spring, friction, inertia... effects
  407.  */
  408. static int make_interactive_modifier(struct iforce* iforce,
  409. struct resource* mod_chunk,
  410. __s16 rsat, __s16 lsat, __s16 rk, __s16 lk, u16 db, __s16 center)
  411. {
  412. unsigned char data[10];
  413. if (allocate_resource(&(iforce->device_memory), mod_chunk, 8,
  414. iforce->device_memory.start, iforce->device_memory.end, 2L,
  415. NULL, NULL)) {
  416. return -ENOMEM;
  417. }
  418. data[0] = LO(mod_chunk->start);
  419. data[1] = HI(mod_chunk->start);
  420. data[2] = HI(rk);
  421. data[3] = HI(lk);
  422. data[4] = LO(center);
  423. data[5] = HI(center);
  424. data[6] = LO(db);
  425. data[7] = HI(db);
  426. data[8] = HI(rsat);
  427. data[9] = HI(lsat);
  428. send_packet(iforce, FF_CMD_INTERACT, data);
  429. return 0;
  430. }
  431. static unsigned char find_button(struct iforce *iforce, signed short button)
  432. {
  433. int i;
  434. for (i = 1; iforce->type->btn[i] >= 0; i++)
  435. if (iforce->type->btn[i] == button)
  436. return i + 1;
  437. return 0;
  438. }
  439. /*
  440.  * Send the part common to all effects to the device
  441.  */
  442. static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
  443. u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
  444. u16 interval, u16 direction)
  445. {
  446. unsigned char data[14];
  447. duration = TIME_SCALE(duration);
  448. delay    = TIME_SCALE(delay);
  449. interval = TIME_SCALE(interval);
  450. data[0]  = LO(id);
  451. data[1]  = effect_type;
  452. data[2]  = LO(axes) | find_button(iforce, button);
  453. data[3]  = LO(duration);
  454. data[4]  = HI(duration);
  455. data[5]  = HI(direction);
  456. data[6]  = LO(interval);
  457. data[7]  = HI(interval);
  458. data[8]  = LO(mod_id1);
  459. data[9]  = HI(mod_id1);
  460. data[10] = LO(mod_id2);
  461. data[11] = HI(mod_id2);
  462. data[12] = LO(delay);
  463. data[13] = HI(delay);
  464. send_packet(iforce, FF_CMD_EFFECT, data);
  465. return 0;
  466. }
  467. /*
  468.  * Upload a periodic effect to the device
  469.  */
  470. static int iforce_upload_periodic(struct iforce* iforce, struct ff_effect* effect)
  471. {
  472. u8 wave_code;
  473. int core_id = effect->id;
  474. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  475. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  476. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  477. int err = 0;
  478. err = make_period_modifier(iforce, mod1_chunk,
  479. effect->u.periodic.magnitude, effect->u.periodic.offset,
  480. effect->u.periodic.period, effect->u.periodic.phase);
  481. if (err) return err;
  482. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  483. err = make_shape_modifier(iforce, mod2_chunk,
  484. effect->u.periodic.shape.attack_length,
  485. effect->u.periodic.shape.attack_level,
  486. effect->u.periodic.shape.fade_length,
  487. effect->u.periodic.shape.fade_level);
  488. if (err) return err;
  489. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  490. switch (effect->u.periodic.waveform) {
  491. case FF_SQUARE: wave_code = 0x20; break;
  492. case FF_TRIANGLE: wave_code = 0x21; break;
  493. case FF_SINE: wave_code = 0x22; break;
  494. case FF_SAW_UP: wave_code = 0x23; break;
  495. case FF_SAW_DOWN: wave_code = 0x24; break;
  496. default: wave_code = 0x20; break;
  497. }
  498. err = make_core(iforce, effect->id,
  499. mod1_chunk->start,
  500. mod2_chunk->start,
  501. wave_code,
  502. 0x20,
  503. effect->replay.length,
  504. effect->replay.delay,
  505. effect->trigger.button,
  506. effect->trigger.interval,
  507. effect->u.periodic.direction);
  508. return err;
  509. }
  510. /*
  511.  * Upload a constant force effect
  512.  */
  513. static int iforce_upload_constant(struct iforce* iforce, struct ff_effect* effect)
  514. {
  515. int core_id = effect->id;
  516. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  517. struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
  518. struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
  519. int err = 0;
  520. printk(KERN_DEBUG "iforce.c: make constant effectn");
  521. err = make_magnitude_modifier(iforce, mod1_chunk, effect->u.constant.level);
  522. if (err) return err;
  523. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  524. err = make_shape_modifier(iforce, mod2_chunk,
  525. effect->u.constant.shape.attack_length,
  526. effect->u.constant.shape.attack_level,
  527. effect->u.constant.shape.fade_length,
  528. effect->u.constant.shape.fade_level);
  529. if (err) return err;
  530. set_bit(FF_MOD2_IS_USED, core_effect->flags);
  531. err = make_core(iforce, effect->id,
  532. mod1_chunk->start,
  533. mod2_chunk->start,
  534. 0x00,
  535. 0x20,
  536. effect->replay.length,
  537. effect->replay.delay,
  538. effect->trigger.button,
  539. effect->trigger.interval,
  540. effect->u.constant.direction);
  541. return err;
  542. }
  543. /*
  544.  * Upload an interactive effect. Those are for example friction, inertia, springs...
  545.  */
  546. static int iforce_upload_interactive(struct iforce* iforce, struct ff_effect* effect)
  547. {
  548. int core_id = effect->id;
  549. struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
  550. struct resource* mod_chunk = &(core_effect->mod1_chunk);
  551. u8 type, axes;
  552. u16 mod1, mod2, direction;
  553. int err = 0;
  554. printk(KERN_DEBUG "iforce.c: make interactive effectn");
  555. switch (effect->type) {
  556. case FF_SPRING: type = 0x40; break;
  557. case FF_FRICTION: type = 0x41; break;
  558. default: return -1;
  559. }
  560. err = make_interactive_modifier(iforce, mod_chunk,
  561. effect->u.interactive.right_saturation,
  562. effect->u.interactive.left_saturation,
  563. effect->u.interactive.right_coeff,
  564. effect->u.interactive.left_coeff,
  565. effect->u.interactive.deadband,
  566. effect->u.interactive.center);
  567. if (err) return err;
  568. set_bit(FF_MOD1_IS_USED, core_effect->flags);
  569. switch ((test_bit(ABS_X, &effect->u.interactive.axis) ||
  570. test_bit(ABS_WHEEL, &effect->u.interactive.axis)) |
  571. (!!test_bit(ABS_Y, &effect->u.interactive.axis) << 1)) {
  572. case 0: /* Only one axis, choose orientation */
  573. mod1 = mod_chunk->start;
  574. mod2 = 0xffff;
  575. direction = effect->u.interactive.direction;
  576. axes = 0x20;
  577. break;
  578. case 1: /* Only X axis */
  579. mod1 = mod_chunk->start;
  580. mod2 = 0xffff;
  581. direction = 0x5a00;
  582. axes = 0x40;
  583. break;
  584. case 2: /* Only Y axis */
  585. mod1 = 0xffff;
  586. mod2 = mod_chunk->start;
  587. direction = 0xb400;
  588. axes = 0x80;
  589. break;
  590. case 3: /* Both X and Y axes */
  591. /* TODO: same setting for both axes is not mandatory */
  592. mod1 = mod_chunk->start;
  593. mod2 = mod_chunk->start;
  594. direction = 0x6000;
  595. axes = 0xc0;
  596. break;
  597. default:
  598. return -1;
  599. }
  600. err = make_core(iforce, effect->id,
  601. mod1, mod2,
  602. type, axes,
  603. effect->replay.length, effect->replay.delay,
  604. effect->trigger.button, effect->trigger.interval,
  605. direction);
  606. return err;
  607. }
  608. /*
  609.  * Function called when an ioctl is performed on the event dev entry.
  610.  * It uploads an effect to the device
  611.  */
  612. static int iforce_upload_effect(struct input_dev *dev, struct ff_effect *effect)
  613. {
  614. struct iforce* iforce = (struct iforce*)(dev->private);
  615. int id;
  616. printk(KERN_DEBUG "iforce.c: upload effectn");
  617. /*
  618.  * Get a free id
  619.  */
  620. for (id=0; id < FF_EFFECTS_MAX; ++id)
  621. if (!test_bit(FF_CORE_IS_USED, iforce->core_effects[id].flags)) break;
  622. if ( id == FF_EFFECTS_MAX || id >= iforce->dev.ff_effects_max)
  623. return -ENOMEM;
  624. effect->id = id;
  625. set_bit(FF_CORE_IS_USED, iforce->core_effects[id].flags);
  626. /*
  627.  * Upload the effect
  628.  */
  629. switch (effect->type) {
  630. case FF_PERIODIC:
  631. return iforce_upload_periodic(iforce, effect);
  632. case FF_CONSTANT:
  633. return iforce_upload_constant(iforce, effect);
  634. case FF_SPRING:
  635. case FF_FRICTION:
  636. return iforce_upload_interactive(iforce, effect);
  637. default:
  638. return -1;
  639. }
  640. }
  641. /*
  642.  * Erases an effect: it frees the effect id and mark as unused the memory
  643.  * allocated for the parameters
  644.  */
  645. static int iforce_erase_effect(struct input_dev *dev, int effect_id)
  646. {
  647. struct iforce* iforce = (struct iforce*)(dev->private);
  648. int err = 0;
  649. struct iforce_core_effect* core_effect;
  650. printk(KERN_DEBUG "iforce.c: erase effect %dn", effect_id);
  651. if (effect_id < 0 || effect_id >= FF_EFFECTS_MAX)
  652. return -EINVAL;
  653. core_effect = iforce->core_effects + effect_id;
  654. if (test_bit(FF_MOD1_IS_USED, core_effect->flags))
  655. err = release_resource(&(iforce->core_effects[effect_id].mod1_chunk));
  656. if (!err && test_bit(FF_MOD2_IS_USED, core_effect->flags))
  657. err = release_resource(&(iforce->core_effects[effect_id].mod2_chunk));
  658. /*TODO: remember to change that if more FF_MOD* bits are added */
  659. core_effect->flags[0] = 0;
  660. return err;
  661. }
  662. static int iforce_init_device(struct iforce *iforce)
  663. {
  664. unsigned char c[] = "CEOV";
  665. int i;
  666. init_waitqueue_head(&iforce->wait);
  667. iforce->dev.ff_effects_max = 10;
  668. /*
  669.  * Input device fields.
  670.  */
  671. iforce->dev.idbus = BUS_USB;
  672. iforce->dev.private = iforce;
  673. iforce->dev.name = iforce->name;
  674. iforce->dev.open = iforce_open;
  675. iforce->dev.close = iforce_close;
  676. iforce->dev.event = iforce_input_event;
  677. iforce->dev.upload_effect = iforce_upload_effect;
  678. iforce->dev.erase_effect = iforce_erase_effect;
  679. /*
  680.  * On-device memory allocation.
  681.  */
  682. iforce->device_memory.name = "I-Force device effect memory";
  683. iforce->device_memory.start = 0;
  684. iforce->device_memory.end = 200;
  685. iforce->device_memory.flags = IORESOURCE_MEM;
  686. iforce->device_memory.parent = NULL;
  687. iforce->device_memory.child = NULL;
  688. iforce->device_memory.sibling = NULL;
  689. /*
  690.  * Wait until device ready - until it sends its first response.
  691.  */
  692. for (i = 0; i < 20; i++)
  693. if (!get_id_packet(iforce, "O"))
  694. break;
  695. if (i == 20) { /* 5 seconds */
  696. printk(KERN_ERR "iforce.c: Timeout waiting for response from device.n");
  697. iforce_close(&iforce->dev);
  698. return -1;
  699. }
  700. /*
  701.  * Get device info.
  702.  */
  703. if (!get_id_packet(iforce, "M"))
  704. iforce->dev.idvendor = (iforce->edata[2] << 8) | iforce->edata[1];
  705. if (!get_id_packet(iforce, "P"))
  706. iforce->dev.idproduct = (iforce->edata[2] << 8) | iforce->edata[1];
  707. if (!get_id_packet(iforce, "B"))
  708. iforce->device_memory.end = (iforce->edata[2] << 8) | iforce->edata[1];
  709. if (!get_id_packet(iforce, "N"))
  710. iforce->dev.ff_effects_max = iforce->edata[1];
  711. /*
  712.  * Display additional info.
  713.  */
  714. for (i = 0; c[i]; i++)
  715. if (!get_id_packet(iforce, c + i))
  716. dump_packet("info", iforce->ecmd, iforce->edata);
  717. /*
  718.  * Disable spring, enable force feedback.
  719.  * FIXME: We should use iforce_set_autocenter() et al here.
  720.  */
  721. send_packet(iforce, FF_CMD_AUTOCENTER, "0400");
  722. send_packet(iforce, FF_CMD_ENABLE, "04");
  723. /*
  724.  * Find appropriate device entry
  725.  */
  726. for (i = 0; iforce_device[i].idvendor; i++)
  727. if (iforce_device[i].idvendor == iforce->dev.idvendor &&
  728.     iforce_device[i].idproduct == iforce->dev.idproduct)
  729. break;
  730. iforce->type = iforce_device + i;
  731. sprintf(iforce->name, iforce->type->name,
  732. iforce->dev.idproduct, iforce->dev.idvendor);
  733. /*
  734.  * Set input device bitfields and ranges.
  735.  */
  736. iforce->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_FF);
  737. for (i = 0; iforce->type->btn[i] >= 0; i++) {
  738. signed short t = iforce->type->btn[i];
  739. set_bit(t, iforce->dev.keybit);
  740. if (t != BTN_DEAD)
  741. set_bit(FF_BTN(t), iforce->dev.ffbit);
  742. }
  743. for (i = 0; iforce->type->abs[i] >= 0; i++) {
  744. signed short t = iforce->type->abs[i];
  745. set_bit(t, iforce->dev.absbit);
  746. switch (t) {
  747. case ABS_X:
  748. case ABS_Y:
  749. case ABS_WHEEL:
  750. iforce->dev.absmax[t] =  1920;
  751. iforce->dev.absmin[t] = -1920;
  752. iforce->dev.absflat[t] = 128;
  753. iforce->dev.absfuzz[t] = 16;
  754. set_bit(FF_ABS(t), iforce->dev.ffbit);
  755. break;
  756. case ABS_THROTTLE:
  757. case ABS_GAS:
  758. case ABS_BRAKE:
  759. iforce->dev.absmax[t] = 255;
  760. iforce->dev.absmin[t] = 0;
  761. break;
  762. case ABS_HAT0X:
  763. case ABS_HAT0Y:
  764. iforce->dev.absmax[t] =  1;
  765. iforce->dev.absmin[t] = -1;
  766. break;
  767. }
  768. }
  769. for (i = 0; iforce->type->ff[i] >= 0; i++)
  770. set_bit(iforce->type->ff[i], iforce->dev.ffbit);
  771. /*
  772.  * Register input device.
  773.  */
  774. input_register_device(&iforce->dev);
  775. return 0;
  776. }
  777. #ifdef IFORCE_USB
  778. static void iforce_usb_irq(struct urb *urb)
  779. {
  780. struct iforce *iforce = urb->context;
  781. if (urb->status) return;
  782. iforce_process_packet(iforce,
  783. (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
  784. }
  785. static void iforce_usb_out(struct urb *urb)
  786. {
  787. struct iforce *iforce = urb->context;
  788. if (urb->status) return;
  789. if (waitqueue_active(&iforce->wait))
  790. wake_up(&iforce->wait);
  791. }
  792. static void iforce_usb_ctrl(struct urb *urb)
  793. {
  794. struct iforce *iforce = urb->context;
  795. if (urb->status) return;
  796. iforce->ecmd = 0xff00 | urb->actual_length;
  797. if (waitqueue_active(&iforce->wait))
  798. wake_up(&iforce->wait);
  799. }
  800. static void *iforce_usb_probe(struct usb_device *dev, unsigned int ifnum,
  801. const struct usb_device_id *id)
  802. {
  803. struct usb_endpoint_descriptor *epirq, *epout;
  804. struct iforce *iforce;
  805. epirq = dev->config[0].interface[ifnum].altsetting[0].endpoint + 0;
  806. epout = dev->config[0].interface[ifnum].altsetting[0].endpoint + 1;
  807. if (!(iforce = kmalloc(sizeof(struct iforce) + 32, GFP_KERNEL))) return NULL;
  808. memset(iforce, 0, sizeof(struct iforce));
  809. iforce->bus = IFORCE_USB;
  810. iforce->usbdev = dev;
  811. iforce->dr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
  812. iforce->dr.wIndex = 0;
  813. iforce->dr.wLength = 16;
  814. FILL_INT_URB(&iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
  815. iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
  816. FILL_BULK_URB(&iforce->out, dev, usb_sndbulkpipe(dev, epout->bEndpointAddress),
  817. iforce + 1, 32, iforce_usb_out, iforce);
  818. FILL_CONTROL_URB(&iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
  819. (void*) &iforce->dr, iforce->edata, 16, iforce_usb_ctrl, iforce);
  820. if (iforce_init_device(iforce)) {
  821. kfree(iforce);
  822. return NULL;
  823. }
  824. printk(KERN_INFO "input%d: %s [%d effects, %ld bytes memory] on usb%d:%d.%dn",
  825. iforce->dev.number, iforce->dev.name, iforce->dev.ff_effects_max,
  826. iforce->device_memory.end, dev->bus->busnum, dev->devnum, ifnum);
  827. return iforce;
  828. }
  829. static void iforce_usb_disconnect(struct usb_device *dev, void *ptr)
  830. {
  831. struct iforce *iforce = ptr;
  832. usb_unlink_urb(&iforce->irq);
  833. input_unregister_device(&iforce->dev);
  834. kfree(iforce);
  835. }
  836. static struct usb_device_id iforce_usb_ids [] = {
  837. { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */
  838. { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */
  839. { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */
  840. { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */
  841. { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */
  842. { } /* Terminating entry */
  843. };
  844. MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
  845. static struct usb_driver iforce_usb_driver = {
  846. name: "iforce",
  847. probe: iforce_usb_probe,
  848. disconnect: iforce_usb_disconnect,
  849. id_table: iforce_usb_ids,
  850. };
  851. #endif
  852. #ifdef IFORCE_232
  853. static void iforce_serio_irq(struct serio *serio, unsigned char data, unsigned int flags)
  854. {
  855. struct iforce* iforce = serio->private;
  856. if (!iforce->pkt) {
  857. if (data != 0x2b) {
  858. return;
  859. }
  860. iforce->pkt = 1;
  861. return;
  862. }
  863. if (!iforce->id) {
  864. if (data > 3 && data != 0xff) {
  865. iforce->pkt = 0;
  866. return;
  867. }
  868. iforce->id = data;
  869. return;
  870. }
  871. if (!iforce->len) {
  872. if (data > IFORCE_MAX_LENGTH) {
  873. iforce->pkt = 0;
  874. iforce->id = 0;
  875. return;
  876. }
  877. iforce->len = data;
  878. return;
  879. }
  880. if (iforce->idx < iforce->len) {
  881. iforce->csum += iforce->data[iforce->idx++] = data;
  882. return;
  883. }
  884. if (iforce->idx == iforce->len) {
  885. iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data);
  886. iforce->pkt = 0;
  887. iforce->id  = 0;
  888. iforce->len = 0;
  889. iforce->idx = 0;
  890. iforce->csum = 0;
  891. }
  892. }
  893. static void iforce_serio_connect(struct serio *serio, struct serio_dev *dev)
  894. {
  895. struct iforce *iforce;
  896. if (serio->type != (SERIO_RS232 | SERIO_IFORCE))
  897. return;
  898. if (!(iforce = kmalloc(sizeof(struct iforce), GFP_KERNEL))) return;
  899. memset(iforce, 0, sizeof(struct iforce));
  900. iforce->bus = IFORCE_232;
  901. iforce->serio = serio;
  902. serio->private = iforce;
  903. if (serio_open(serio, dev)) {
  904. kfree(iforce);
  905. return;
  906. }
  907. if (iforce_init_device(iforce)) {
  908. serio_close(serio);
  909. kfree(iforce);
  910. return;
  911. }
  912. printk(KERN_INFO "input%d: %s [%d effects, %ld bytes memory] on serio%dn",
  913. iforce->dev.number, iforce->dev.name, iforce->dev.ff_effects_max,
  914. iforce->device_memory.end, serio->number);
  915. }
  916. static void iforce_serio_disconnect(struct serio *serio)
  917. {
  918. struct iforce* iforce = serio->private;
  919. input_unregister_device(&iforce->dev);
  920. serio_close(serio);
  921. kfree(iforce);
  922. }
  923. static struct serio_dev iforce_serio_dev = {
  924. interrupt: iforce_serio_irq,
  925. connect: iforce_serio_connect,
  926. disconnect: iforce_serio_disconnect,
  927. };
  928. #endif
  929. static int __init iforce_init(void)
  930. {
  931. #ifdef IFORCE_USB
  932. usb_register(&iforce_usb_driver);
  933. #endif
  934. #ifdef IFORCE_232
  935. serio_register_device(&iforce_serio_dev);
  936. #endif
  937. return 0;
  938. }
  939. static void __exit iforce_exit(void)
  940. {
  941. #ifdef IFORCE_USB
  942. usb_deregister(&iforce_usb_driver);
  943. #endif
  944. #ifdef IFORCE_232
  945. serio_unregister_device(&iforce_serio_dev);
  946. #endif
  947. }
  948. module_init(iforce_init);
  949. module_exit(iforce_exit);