stinger.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /*
  44. * Per-Stinger data.
  45. */
  46. struct stinger {
  47. struct input_dev *dev;
  48. int idx;
  49. unsigned char data[STINGER_MAX_LENGTH];
  50. char phys[32];
  51. };
  52. /*
  53. * stinger_process_packet() decodes packets the driver receives from the
  54. * Stinger. It updates the data accordingly.
  55. */
  56. static void stinger_process_packet(struct stinger *stinger, struct pt_regs *regs)
  57. {
  58. struct input_dev *dev = stinger->dev;
  59. unsigned char *data = stinger->data;
  60. if (!stinger->idx) return;
  61. input_regs(dev, regs);
  62. input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));
  63. input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));
  64. input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));
  65. input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));
  66. input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));
  67. input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));
  68. input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));
  69. input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));
  70. input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
  71. input_report_key(dev, BTN_START, (data[3] & 0x01));
  72. input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
  73. input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
  74. input_sync(dev);
  75. return;
  76. }
  77. /*
  78. * stinger_interrupt() is called by the low level driver when characters
  79. * are ready for us. We then buffer them for further processing, or call the
  80. * packet processing routine.
  81. */
  82. static irqreturn_t stinger_interrupt(struct serio *serio,
  83. unsigned char data, unsigned int flags, struct pt_regs *regs)
  84. {
  85. struct stinger *stinger = serio_get_drvdata(serio);
  86. /* All Stinger packets are 4 bytes */
  87. if (stinger->idx < STINGER_MAX_LENGTH)
  88. stinger->data[stinger->idx++] = data;
  89. if (stinger->idx == 4) {
  90. stinger_process_packet(stinger, regs);
  91. stinger->idx = 0;
  92. }
  93. return IRQ_HANDLED;
  94. }
  95. /*
  96. * stinger_disconnect() is the opposite of stinger_connect()
  97. */
  98. static void stinger_disconnect(struct serio *serio)
  99. {
  100. struct stinger *stinger = serio_get_drvdata(serio);
  101. serio_close(serio);
  102. serio_set_drvdata(serio, NULL);
  103. input_unregister_device(stinger->dev);
  104. kfree(stinger);
  105. }
  106. /*
  107. * stinger_connect() is the routine that is called when someone adds a
  108. * new serio device that supports Stinger protocol and registers it as
  109. * an input device.
  110. */
  111. static int stinger_connect(struct serio *serio, struct serio_driver *drv)
  112. {
  113. struct stinger *stinger;
  114. struct input_dev *input_dev;
  115. int err = -ENOMEM;
  116. stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
  117. input_dev = input_allocate_device();
  118. if (!stinger || !input_dev)
  119. goto fail;
  120. stinger->dev = input_dev;
  121. sprintf(stinger->phys, "%s/serio0", serio->phys);
  122. input_dev->name = "Gravis Stinger";
  123. input_dev->phys = stinger->phys;
  124. input_dev->id.bustype = BUS_RS232;
  125. input_dev->id.vendor = SERIO_STINGER;
  126. input_dev->id.product = 0x0001;
  127. input_dev->id.version = 0x0100;
  128. input_dev->cdev.dev = &serio->dev;
  129. input_dev->private = stinger;
  130. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  131. input_dev->keybit[LONG(BTN_A)] = BIT(BTN_A) | BIT(BTN_B) | BIT(BTN_C) | BIT(BTN_X) |
  132. BIT(BTN_Y) | BIT(BTN_Z) | BIT(BTN_TL) | BIT(BTN_TR) |
  133. BIT(BTN_START) | BIT(BTN_SELECT);
  134. input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
  135. input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
  136. serio_set_drvdata(serio, stinger);
  137. err = serio_open(serio, drv);
  138. if (err)
  139. goto fail;
  140. input_register_device(stinger->dev);
  141. return 0;
  142. fail: serio_set_drvdata(serio, NULL);
  143. 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. serio_register_driver(&stinger_drv);
  176. return 0;
  177. }
  178. static void __exit stinger_exit(void)
  179. {
  180. serio_unregister_driver(&stinger_drv);
  181. }
  182. module_init(stinger_init);
  183. module_exit(stinger_exit);