stinger.c 5.2 KB

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