stinger.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define DRIVER_DESC "Gravis Stinger gamepad driver"
  33. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  34. MODULE_DESCRIPTION(DRIVER_DESC);
  35. MODULE_LICENSE("GPL");
  36. /*
  37. * Constants.
  38. */
  39. #define STINGER_MAX_LENGTH 8
  40. /*
  41. * Per-Stinger data.
  42. */
  43. struct stinger {
  44. struct input_dev *dev;
  45. int idx;
  46. unsigned char data[STINGER_MAX_LENGTH];
  47. char phys[32];
  48. };
  49. /*
  50. * stinger_process_packet() decodes packets the driver receives from the
  51. * Stinger. It updates the data accordingly.
  52. */
  53. static void stinger_process_packet(struct stinger *stinger)
  54. {
  55. struct input_dev *dev = stinger->dev;
  56. unsigned char *data = stinger->data;
  57. if (!stinger->idx) return;
  58. input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));
  59. input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));
  60. input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));
  61. input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));
  62. input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));
  63. input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));
  64. input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));
  65. input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));
  66. input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
  67. input_report_key(dev, BTN_START, (data[3] & 0x01));
  68. input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
  69. input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
  70. input_sync(dev);
  71. return;
  72. }
  73. /*
  74. * stinger_interrupt() is called by the low level driver when characters
  75. * are ready for us. We then buffer them for further processing, or call the
  76. * packet processing routine.
  77. */
  78. static irqreturn_t stinger_interrupt(struct serio *serio,
  79. unsigned char data, unsigned int flags)
  80. {
  81. struct stinger *stinger = serio_get_drvdata(serio);
  82. /* All Stinger packets are 4 bytes */
  83. if (stinger->idx < STINGER_MAX_LENGTH)
  84. stinger->data[stinger->idx++] = data;
  85. if (stinger->idx == 4) {
  86. stinger_process_packet(stinger);
  87. stinger->idx = 0;
  88. }
  89. return IRQ_HANDLED;
  90. }
  91. /*
  92. * stinger_disconnect() is the opposite of stinger_connect()
  93. */
  94. static void stinger_disconnect(struct serio *serio)
  95. {
  96. struct stinger *stinger = serio_get_drvdata(serio);
  97. serio_close(serio);
  98. serio_set_drvdata(serio, NULL);
  99. input_unregister_device(stinger->dev);
  100. kfree(stinger);
  101. }
  102. /*
  103. * stinger_connect() is the routine that is called when someone adds a
  104. * new serio device that supports Stinger protocol and registers it as
  105. * an input device.
  106. */
  107. static int stinger_connect(struct serio *serio, struct serio_driver *drv)
  108. {
  109. struct stinger *stinger;
  110. struct input_dev *input_dev;
  111. int err = -ENOMEM;
  112. stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
  113. input_dev = input_allocate_device();
  114. if (!stinger || !input_dev)
  115. goto fail1;
  116. stinger->dev = input_dev;
  117. snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
  118. input_dev->name = "Gravis Stinger";
  119. input_dev->phys = stinger->phys;
  120. input_dev->id.bustype = BUS_RS232;
  121. input_dev->id.vendor = SERIO_STINGER;
  122. input_dev->id.product = 0x0001;
  123. input_dev->id.version = 0x0100;
  124. input_dev->dev.parent = &serio->dev;
  125. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  126. input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
  127. BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
  128. BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
  129. BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
  130. input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
  131. input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
  132. serio_set_drvdata(serio, stinger);
  133. err = serio_open(serio, drv);
  134. if (err)
  135. goto fail2;
  136. err = input_register_device(stinger->dev);
  137. if (err)
  138. goto fail3;
  139. return 0;
  140. fail3: serio_close(serio);
  141. fail2: serio_set_drvdata(serio, NULL);
  142. fail1: input_free_device(input_dev);
  143. kfree(stinger);
  144. return err;
  145. }
  146. /*
  147. * The serio driver structure.
  148. */
  149. static struct serio_device_id stinger_serio_ids[] = {
  150. {
  151. .type = SERIO_RS232,
  152. .proto = SERIO_STINGER,
  153. .id = SERIO_ANY,
  154. .extra = SERIO_ANY,
  155. },
  156. { 0 }
  157. };
  158. MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
  159. static struct serio_driver stinger_drv = {
  160. .driver = {
  161. .name = "stinger",
  162. },
  163. .description = DRIVER_DESC,
  164. .id_table = stinger_serio_ids,
  165. .interrupt = stinger_interrupt,
  166. .connect = stinger_connect,
  167. .disconnect = stinger_disconnect,
  168. };
  169. module_serio_driver(stinger_drv);