warrior.c 6.5 KB

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