warrior.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #define DRIVER_DESC "Logitech WingMan Warrior joystick driver"
  32. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  33. MODULE_DESCRIPTION(DRIVER_DESC);
  34. MODULE_LICENSE("GPL");
  35. /*
  36. * Constants.
  37. */
  38. #define WARRIOR_MAX_LENGTH 16
  39. static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
  40. /*
  41. * Per-Warrior data.
  42. */
  43. struct warrior {
  44. struct input_dev *dev;
  45. int idx, len;
  46. unsigned char data[WARRIOR_MAX_LENGTH];
  47. char phys[32];
  48. };
  49. /*
  50. * warrior_process_packet() decodes packets the driver receives from the
  51. * Warrior. It updates the data accordingly.
  52. */
  53. static void warrior_process_packet(struct warrior *warrior)
  54. {
  55. struct input_dev *dev = warrior->dev;
  56. unsigned char *data = warrior->data;
  57. if (!warrior->idx) return;
  58. switch ((data[0] >> 4) & 7) {
  59. case 1: /* Button data */
  60. input_report_key(dev, BTN_TRIGGER, data[3] & 1);
  61. input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
  62. input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
  63. input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
  64. break;
  65. case 3: /* XY-axis info->data */
  66. input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
  67. input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  68. break;
  69. case 5: /* Throttle, spinner, hat info->data */
  70. input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  71. input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
  72. input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
  73. input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
  74. break;
  75. }
  76. input_sync(dev);
  77. }
  78. /*
  79. * warrior_interrupt() is called by the low level driver when characters
  80. * are ready for us. We then buffer them for further processing, or call the
  81. * packet processing routine.
  82. */
  83. static irqreturn_t warrior_interrupt(struct serio *serio,
  84. unsigned char data, unsigned int flags)
  85. {
  86. struct warrior *warrior = serio_get_drvdata(serio);
  87. if (data & 0x80) {
  88. if (warrior->idx) warrior_process_packet(warrior);
  89. warrior->idx = 0;
  90. warrior->len = warrior_lengths[(data >> 4) & 7];
  91. }
  92. if (warrior->idx < warrior->len)
  93. warrior->data[warrior->idx++] = data;
  94. if (warrior->idx == warrior->len) {
  95. if (warrior->idx) warrior_process_packet(warrior);
  96. warrior->idx = 0;
  97. warrior->len = 0;
  98. }
  99. return IRQ_HANDLED;
  100. }
  101. /*
  102. * warrior_disconnect() is the opposite of warrior_connect()
  103. */
  104. static void warrior_disconnect(struct serio *serio)
  105. {
  106. struct warrior *warrior = serio_get_drvdata(serio);
  107. serio_close(serio);
  108. serio_set_drvdata(serio, NULL);
  109. input_unregister_device(warrior->dev);
  110. kfree(warrior);
  111. }
  112. /*
  113. * warrior_connect() is the routine that is called when someone adds a
  114. * new serio device. It looks for the Warrior, and if found, registers
  115. * it as an input device.
  116. */
  117. static int warrior_connect(struct serio *serio, struct serio_driver *drv)
  118. {
  119. struct warrior *warrior;
  120. struct input_dev *input_dev;
  121. int err = -ENOMEM;
  122. warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
  123. input_dev = input_allocate_device();
  124. if (!warrior || !input_dev)
  125. goto fail1;
  126. warrior->dev = input_dev;
  127. snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys);
  128. input_dev->name = "Logitech WingMan Warrior";
  129. input_dev->phys = warrior->phys;
  130. input_dev->id.bustype = BUS_RS232;
  131. input_dev->id.vendor = SERIO_WARRIOR;
  132. input_dev->id.product = 0x0001;
  133. input_dev->id.version = 0x0100;
  134. input_dev->dev.parent = &serio->dev;
  135. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
  136. BIT_MASK(EV_ABS);
  137. input_dev->keybit[BIT_WORD(BTN_TRIGGER)] = BIT_MASK(BTN_TRIGGER) |
  138. BIT_MASK(BTN_THUMB) | BIT_MASK(BTN_TOP) | BIT_MASK(BTN_TOP2);
  139. input_dev->relbit[0] = BIT_MASK(REL_DIAL);
  140. input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
  141. input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
  142. input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
  143. input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
  144. input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
  145. serio_set_drvdata(serio, warrior);
  146. err = serio_open(serio, drv);
  147. if (err)
  148. goto fail2;
  149. err = input_register_device(warrior->dev);
  150. if (err)
  151. goto fail3;
  152. return 0;
  153. fail3: serio_close(serio);
  154. fail2: serio_set_drvdata(serio, NULL);
  155. fail1: input_free_device(input_dev);
  156. kfree(warrior);
  157. return err;
  158. }
  159. /*
  160. * The serio driver structure.
  161. */
  162. static struct serio_device_id warrior_serio_ids[] = {
  163. {
  164. .type = SERIO_RS232,
  165. .proto = SERIO_WARRIOR,
  166. .id = SERIO_ANY,
  167. .extra = SERIO_ANY,
  168. },
  169. { 0 }
  170. };
  171. MODULE_DEVICE_TABLE(serio, warrior_serio_ids);
  172. static struct serio_driver warrior_drv = {
  173. .driver = {
  174. .name = "warrior",
  175. },
  176. .description = DRIVER_DESC,
  177. .id_table = warrior_serio_ids,
  178. .interrupt = warrior_interrupt,
  179. .connect = warrior_connect,
  180. .disconnect = warrior_disconnect,
  181. };
  182. module_serio_driver(warrior_drv);