stinger.c 5.7 KB

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