cobra.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 1999-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * Creative Labs Blaster GamePad Cobra driver for Linux
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/gameport.h>
  30. #include <linux/input.h>
  31. #include <linux/jiffies.h>
  32. #define DRIVER_DESC "Creative Labs Blaster GamePad Cobra driver"
  33. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  34. MODULE_DESCRIPTION(DRIVER_DESC);
  35. MODULE_LICENSE("GPL");
  36. #define COBRA_MAX_STROBE 45 /* 45 us max wait for first strobe */
  37. #define COBRA_LENGTH 36
  38. static int cobra_btn[] = { BTN_START, BTN_SELECT, BTN_TL, BTN_TR, BTN_X, BTN_Y, BTN_Z, BTN_A, BTN_B, BTN_C, BTN_TL2, BTN_TR2, 0 };
  39. struct cobra {
  40. struct gameport *gameport;
  41. struct input_dev *dev[2];
  42. int reads;
  43. int bads;
  44. unsigned char exists;
  45. char phys[2][32];
  46. };
  47. static unsigned char cobra_read_packet(struct gameport *gameport, unsigned int *data)
  48. {
  49. unsigned long flags;
  50. unsigned char u, v, w;
  51. __u64 buf[2];
  52. int r[2], t[2];
  53. int i, j, ret;
  54. int strobe = gameport_time(gameport, COBRA_MAX_STROBE);
  55. for (i = 0; i < 2; i++) {
  56. r[i] = buf[i] = 0;
  57. t[i] = COBRA_MAX_STROBE;
  58. }
  59. local_irq_save(flags);
  60. u = gameport_read(gameport);
  61. do {
  62. t[0]--; t[1]--;
  63. v = gameport_read(gameport);
  64. for (i = 0, w = u ^ v; i < 2 && w; i++, w >>= 2)
  65. if (w & 0x30) {
  66. if ((w & 0x30) < 0x30 && r[i] < COBRA_LENGTH && t[i] > 0) {
  67. buf[i] |= (__u64)((w >> 5) & 1) << r[i]++;
  68. t[i] = strobe;
  69. u = v;
  70. } else t[i] = 0;
  71. }
  72. } while (t[0] > 0 || t[1] > 0);
  73. local_irq_restore(flags);
  74. ret = 0;
  75. for (i = 0; i < 2; i++) {
  76. if (r[i] != COBRA_LENGTH) continue;
  77. for (j = 0; j < COBRA_LENGTH && (buf[i] & 0x04104107f) ^ 0x041041040; j++)
  78. buf[i] = (buf[i] >> 1) | ((__u64)(buf[i] & 1) << (COBRA_LENGTH - 1));
  79. if (j < COBRA_LENGTH) ret |= (1 << i);
  80. data[i] = ((buf[i] >> 7) & 0x000001f) | ((buf[i] >> 8) & 0x00003e0)
  81. | ((buf[i] >> 9) & 0x0007c00) | ((buf[i] >> 10) & 0x00f8000)
  82. | ((buf[i] >> 11) & 0x1f00000);
  83. }
  84. return ret;
  85. }
  86. static void cobra_poll(struct gameport *gameport)
  87. {
  88. struct cobra *cobra = gameport_get_drvdata(gameport);
  89. struct input_dev *dev;
  90. unsigned int data[2];
  91. int i, j, r;
  92. cobra->reads++;
  93. if ((r = cobra_read_packet(gameport, data)) != cobra->exists) {
  94. cobra->bads++;
  95. return;
  96. }
  97. for (i = 0; i < 2; i++)
  98. if (cobra->exists & r & (1 << i)) {
  99. dev = cobra->dev[i];
  100. input_report_abs(dev, ABS_X, ((data[i] >> 4) & 1) - ((data[i] >> 3) & 1));
  101. input_report_abs(dev, ABS_Y, ((data[i] >> 2) & 1) - ((data[i] >> 1) & 1));
  102. for (j = 0; cobra_btn[j]; j++)
  103. input_report_key(dev, cobra_btn[j], data[i] & (0x20 << j));
  104. input_sync(dev);
  105. }
  106. }
  107. static int cobra_open(struct input_dev *dev)
  108. {
  109. struct cobra *cobra = input_get_drvdata(dev);
  110. gameport_start_polling(cobra->gameport);
  111. return 0;
  112. }
  113. static void cobra_close(struct input_dev *dev)
  114. {
  115. struct cobra *cobra = input_get_drvdata(dev);
  116. gameport_stop_polling(cobra->gameport);
  117. }
  118. static int cobra_connect(struct gameport *gameport, struct gameport_driver *drv)
  119. {
  120. struct cobra *cobra;
  121. struct input_dev *input_dev;
  122. unsigned int data[2];
  123. int i, j;
  124. int err;
  125. cobra = kzalloc(sizeof(struct cobra), GFP_KERNEL);
  126. if (!cobra)
  127. return -ENOMEM;
  128. cobra->gameport = gameport;
  129. gameport_set_drvdata(gameport, cobra);
  130. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  131. if (err)
  132. goto fail1;
  133. cobra->exists = cobra_read_packet(gameport, data);
  134. for (i = 0; i < 2; i++)
  135. if ((cobra->exists >> i) & data[i] & 1) {
  136. printk(KERN_WARNING "cobra.c: Device %d on %s has the Ext bit set. ID is: %d"
  137. " Contact vojtech@ucw.cz\n", i, gameport->phys, (data[i] >> 2) & 7);
  138. cobra->exists &= ~(1 << i);
  139. }
  140. if (!cobra->exists) {
  141. err = -ENODEV;
  142. goto fail2;
  143. }
  144. gameport_set_poll_handler(gameport, cobra_poll);
  145. gameport_set_poll_interval(gameport, 20);
  146. for (i = 0; i < 2; i++) {
  147. if (~(cobra->exists >> i) & 1)
  148. continue;
  149. cobra->dev[i] = input_dev = input_allocate_device();
  150. if (!input_dev) {
  151. err = -ENOMEM;
  152. goto fail3;
  153. }
  154. snprintf(cobra->phys[i], sizeof(cobra->phys[i]),
  155. "%s/input%d", gameport->phys, i);
  156. input_dev->name = "Creative Labs Blaster GamePad Cobra";
  157. input_dev->phys = cobra->phys[i];
  158. input_dev->id.bustype = BUS_GAMEPORT;
  159. input_dev->id.vendor = GAMEPORT_ID_VENDOR_CREATIVE;
  160. input_dev->id.product = 0x0008;
  161. input_dev->id.version = 0x0100;
  162. input_dev->dev.parent = &gameport->dev;
  163. input_set_drvdata(input_dev, cobra);
  164. input_dev->open = cobra_open;
  165. input_dev->close = cobra_close;
  166. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  167. input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
  168. input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
  169. for (j = 0; cobra_btn[j]; j++)
  170. set_bit(cobra_btn[j], input_dev->keybit);
  171. err = input_register_device(cobra->dev[i]);
  172. if (err)
  173. goto fail4;
  174. }
  175. return 0;
  176. fail4: input_free_device(cobra->dev[i]);
  177. fail3: while (--i >= 0)
  178. if (cobra->dev[i])
  179. input_unregister_device(cobra->dev[i]);
  180. fail2: gameport_close(gameport);
  181. fail1: gameport_set_drvdata(gameport, NULL);
  182. kfree(cobra);
  183. return err;
  184. }
  185. static void cobra_disconnect(struct gameport *gameport)
  186. {
  187. struct cobra *cobra = gameport_get_drvdata(gameport);
  188. int i;
  189. for (i = 0; i < 2; i++)
  190. if ((cobra->exists >> i) & 1)
  191. input_unregister_device(cobra->dev[i]);
  192. gameport_close(gameport);
  193. gameport_set_drvdata(gameport, NULL);
  194. kfree(cobra);
  195. }
  196. static struct gameport_driver cobra_drv = {
  197. .driver = {
  198. .name = "cobra",
  199. },
  200. .description = DRIVER_DESC,
  201. .connect = cobra_connect,
  202. .disconnect = cobra_disconnect,
  203. };
  204. module_gameport_driver(cobra_drv);