warrior.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 1999-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * Logitech WingMan Warrior joystick driver for Linux
  6. */
  7. /*
  8. * This program is free warftware; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/input.h>
  30. #include <linux/serio.h>
  31. #include <linux/init.h>
  32. #define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
  33. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  34. MODULE_DESCRIPTION(DRIVER_DESC);
  35. MODULE_LICENSE("GPL");
  36. /*
  37. * Constants.
  38. */
  39. #define WARRIOR_MAX_LENGTH 16
  40. static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
  41. /*
  42. * Per-Warrior data.
  43. */
  44. struct warrior {
  45. struct input_dev *dev;
  46. int idx, len;
  47. unsigned char data[WARRIOR_MAX_LENGTH];
  48. char phys[32];
  49. };
  50. /*
  51. * warrior_process_packet() decodes packets the driver receives from the
  52. * Warrior. It updates the data accordingly.
  53. */
  54. static void warrior_process_packet(struct warrior *warrior)
  55. {
  56. struct input_dev *dev = warrior->dev;
  57. unsigned char *data = warrior->data;
  58. if (!warrior->idx) return;
  59. switch ((data[0] >> 4) & 7) {
  60. case 1: /* Button data */
  61. input_report_key(dev, BTN_TRIGGER, data[3] & 1);
  62. input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
  63. input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
  64. input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
  65. break;
  66. case 3: /* XY-axis info->data */
  67. input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
  68. input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  69. break;
  70. case 5: /* Throttle, spinner, hat info->data */
  71. input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  72. input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
  73. input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
  74. input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
  75. break;
  76. }
  77. input_sync(dev);
  78. }
  79. /*
  80. * warrior_interrupt() is called by the low level driver when characters
  81. * are ready for us. We then buffer them for further processing, or call the
  82. * packet processing routine.
  83. */
  84. static irqreturn_t warrior_interrupt(struct serio *serio,
  85. unsigned char data, unsigned int flags)
  86. {
  87. struct warrior *warrior = serio_get_drvdata(serio);
  88. if (data & 0x80) {
  89. if (warrior->idx) warrior_process_packet(warrior);
  90. warrior->idx = 0;
  91. warrior->len = warrior_lengths[(data >> 4) & 7];
  92. }
  93. if (warrior->idx < warrior->len)
  94. warrior->data[warrior->idx++] = data;
  95. if (warrior->idx == warrior->len) {
  96. if (warrior->idx) warrior_process_packet(warrior);
  97. warrior->idx = 0;
  98. warrior->len = 0;
  99. }
  100. return IRQ_HANDLED;
  101. }
  102. /*
  103. * warrior_disconnect() is the opposite of warrior_connect()
  104. */
  105. static void warrior_disconnect(struct serio *serio)
  106. {
  107. struct warrior *warrior = serio_get_drvdata(serio);
  108. serio_close(serio);
  109. serio_set_drvdata(serio, NULL);
  110. input_unregister_device(warrior->dev);
  111. kfree(warrior);
  112. }
  113. /*
  114. * warrior_connect() is the routine that is called when someone adds a
  115. * new serio device. It looks for the Warrior, and if found, registers
  116. * it as an input device.
  117. */
  118. static int warrior_connect(struct serio *serio, struct serio_driver *drv)
  119. {
  120. struct warrior *warrior;
  121. struct input_dev *input_dev;
  122. int err = -ENOMEM;
  123. warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
  124. input_dev = input_allocate_device();
  125. if (!warrior || !input_dev)
  126. goto fail1;
  127. warrior->dev = input_dev;
  128. snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
  129. input_dev->name = "Logitech WingMan Warrior";
  130. input_dev->phys = warrior->phys;
  131. input_dev->id.bustype = BUS_RS232;
  132. input_dev->id.vendor = SERIO_WARRIOR;
  133. input_dev->id.product = 0x0001;
  134. input_dev->id.version = 0x0100;
  135. input_dev->dev.parent = &serio->dev;
  136. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
  137. BIT_MASK(EV_ABS);
  138. input_dev->keybit[BIT_WORD(BTN_TRIGGER)] = BIT_MASK(BTN_TRIGGER) |
  139. BIT_MASK(BTN_THUMB) | BIT_MASK(BTN_TOP) | BIT_MASK(BTN_TOP2);
  140. input_dev->relbit[0] = BIT_MASK(REL_DIAL);
  141. input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
  142. input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
  143. input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
  144. input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
  145. input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
  146. serio_set_drvdata(serio, warrior);
  147. err = serio_open(serio, drv);
  148. if (err)
  149. goto fail2;
  150. err = input_register_device(warrior->dev);
  151. if (err)
  152. goto fail3;
  153. return 0;
  154. fail3: serio_close(serio);
  155. fail2: serio_set_drvdata(serio, NULL);
  156. fail1: input_free_device(input_dev);
  157. kfree(warrior);
  158. return err;
  159. }
  160. /*
  161. * The serio driver structure.
  162. */
  163. static struct serio_device_id warrior_serio_ids[] = {
  164. {
  165. .type = SERIO_RS232,
  166. .proto = SERIO_WARRIOR,
  167. .id = SERIO_ANY,
  168. .extra = SERIO_ANY,
  169. },
  170. { 0 }
  171. };
  172. MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
  173. static struct serio_driver warrior_drv = {
  174. .driver = {
  175. .name = "warrior",
  176. },
  177. .description = DRIVER_DESC,
  178. .id_table = warrior_serio_ids,
  179. .interrupt = warrior_interrupt,
  180. .connect = warrior_connect,
  181. .disconnect = warrior_disconnect,
  182. };
  183. /*
  184. * The functions for inserting/removing us as a module.
  185. */
  186. static int __init warrior_init(void)
  187. {
  188. return serio_register_driver(&warrior_drv);
  189. }
  190. static void __exit warrior_exit(void)
  191. {
  192. serio_unregister_driver(&warrior_drv);
  193. }
  194. module_init(warrior_init);
  195. module_exit(warrior_exit);