warrior.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /*
  44. * Per-Warrior data.
  45. */
  46. struct warrior {
  47. struct input_dev *dev;
  48. int idx, len;
  49. unsigned char data[WARRIOR_MAX_LENGTH];
  50. char phys[32];
  51. };
  52. /*
  53. * warrior_process_packet() decodes packets the driver receives from the
  54. * Warrior. It updates the data accordingly.
  55. */
  56. static void warrior_process_packet(struct warrior *warrior, struct pt_regs *regs)
  57. {
  58. struct input_dev *dev = warrior->dev;
  59. unsigned char *data = warrior->data;
  60. if (!warrior->idx) return;
  61. input_regs(dev, regs);
  62. switch ((data[0] >> 4) & 7) {
  63. case 1: /* Button data */
  64. input_report_key(dev, BTN_TRIGGER, data[3] & 1);
  65. input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1);
  66. input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1);
  67. input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1);
  68. break;
  69. case 3: /* XY-axis info->data */
  70. input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5)));
  71. input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  72. break;
  73. case 5: /* Throttle, spinner, hat info->data */
  74. input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7));
  75. input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0));
  76. input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0));
  77. input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5));
  78. break;
  79. }
  80. input_sync(dev);
  81. }
  82. /*
  83. * warrior_interrupt() is called by the low level driver when characters
  84. * are ready for us. We then buffer them for further processing, or call the
  85. * packet processing routine.
  86. */
  87. static irqreturn_t warrior_interrupt(struct serio *serio,
  88. unsigned char data, unsigned int flags, struct pt_regs *regs)
  89. {
  90. struct warrior *warrior = serio_get_drvdata(serio);
  91. if (data & 0x80) {
  92. if (warrior->idx) warrior_process_packet(warrior, regs);
  93. warrior->idx = 0;
  94. warrior->len = warrior_lengths[(data >> 4) & 7];
  95. }
  96. if (warrior->idx < warrior->len)
  97. warrior->data[warrior->idx++] = data;
  98. if (warrior->idx == warrior->len) {
  99. if (warrior->idx) warrior_process_packet(warrior, regs);
  100. warrior->idx = 0;
  101. warrior->len = 0;
  102. }
  103. return IRQ_HANDLED;
  104. }
  105. /*
  106. * warrior_disconnect() is the opposite of warrior_connect()
  107. */
  108. static void warrior_disconnect(struct serio *serio)
  109. {
  110. struct warrior *warrior = serio_get_drvdata(serio);
  111. serio_close(serio);
  112. serio_set_drvdata(serio, NULL);
  113. input_unregister_device(warrior->dev);
  114. kfree(warrior);
  115. }
  116. /*
  117. * warrior_connect() is the routine that is called when someone adds a
  118. * new serio device. It looks for the Warrior, and if found, registers
  119. * it as an input device.
  120. */
  121. static int warrior_connect(struct serio *serio, struct serio_driver *drv)
  122. {
  123. struct warrior *warrior;
  124. struct input_dev *input_dev;
  125. int err = -ENOMEM;
  126. warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL);
  127. input_dev = input_allocate_device();
  128. if (!warrior || !input_dev)
  129. goto fail;
  130. warrior->dev = input_dev;
  131. sprintf(warrior->phys, "%s/input0", serio->phys);
  132. input_dev->name = "Logitech WingMan Warrior";
  133. input_dev->phys = warrior->phys;
  134. input_dev->id.bustype = BUS_RS232;
  135. input_dev->id.vendor = SERIO_WARRIOR;
  136. input_dev->id.product = 0x0001;
  137. input_dev->id.version = 0x0100;
  138. input_dev->cdev.dev = &serio->dev;
  139. input_dev->private = warrior;
  140. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
  141. input_dev->keybit[LONG(BTN_TRIGGER)] = BIT(BTN_TRIGGER) | BIT(BTN_THUMB) | BIT(BTN_TOP) | BIT(BTN_TOP2);
  142. input_dev->relbit[0] = BIT(REL_DIAL);
  143. input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8);
  144. input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8);
  145. input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0);
  146. input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
  147. input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
  148. serio_set_drvdata(serio, warrior);
  149. err = serio_open(serio, drv);
  150. if (err)
  151. goto fail;
  152. input_register_device(warrior->dev);
  153. return 0;
  154. fail: serio_set_drvdata(serio, NULL);
  155. 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. /*
  183. * The functions for inserting/removing us as a module.
  184. */
  185. static int __init warrior_init(void)
  186. {
  187. serio_register_driver(&warrior_drv);
  188. return 0;
  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);