stinger.c 5.8 KB

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