cobra.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/init.h>
  30. #include <linux/gameport.h>
  31. #include <linux/input.h>
  32. #include <linux/jiffies.h>
  33. #define DRIVER_DESC "Creative Labs Blaster GamePad Cobra driver"
  34. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  35. MODULE_DESCRIPTION(DRIVER_DESC);
  36. MODULE_LICENSE("GPL");
  37. #define COBRA_MAX_STROBE 45 /* 45 us max wait for first strobe */
  38. #define COBRA_LENGTH 36
  39. 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 };
  40. struct cobra {
  41. struct gameport *gameport;
  42. struct input_dev *dev[2];
  43. int reads;
  44. int bads;
  45. unsigned char exists;
  46. char phys[2][32];
  47. };
  48. static unsigned char cobra_read_packet(struct gameport *gameport, unsigned int *data)
  49. {
  50. unsigned long flags;
  51. unsigned char u, v, w;
  52. __u64 buf[2];
  53. int r[2], t[2];
  54. int i, j, ret;
  55. int strobe = gameport_time(gameport, COBRA_MAX_STROBE);
  56. for (i = 0; i < 2; i++) {
  57. r[i] = buf[i] = 0;
  58. t[i] = COBRA_MAX_STROBE;
  59. }
  60. local_irq_save(flags);
  61. u = gameport_read(gameport);
  62. do {
  63. t[0]--; t[1]--;
  64. v = gameport_read(gameport);
  65. for (i = 0, w = u ^ v; i < 2 && w; i++, w >>= 2)
  66. if (w & 0x30) {
  67. if ((w & 0x30) < 0x30 && r[i] < COBRA_LENGTH && t[i] > 0) {
  68. buf[i] |= (__u64)((w >> 5) & 1) << r[i]++;
  69. t[i] = strobe;
  70. u = v;
  71. } else t[i] = 0;
  72. }
  73. } while (t[0] > 0 || t[1] > 0);
  74. local_irq_restore(flags);
  75. ret = 0;
  76. for (i = 0; i < 2; i++) {
  77. if (r[i] != COBRA_LENGTH) continue;
  78. for (j = 0; j < COBRA_LENGTH && (buf[i] & 0x04104107f) ^ 0x041041040; j++)
  79. buf[i] = (buf[i] >> 1) | ((__u64)(buf[i] & 1) << (COBRA_LENGTH - 1));
  80. if (j < COBRA_LENGTH) ret |= (1 << i);
  81. data[i] = ((buf[i] >> 7) & 0x000001f) | ((buf[i] >> 8) & 0x00003e0)
  82. | ((buf[i] >> 9) & 0x0007c00) | ((buf[i] >> 10) & 0x00f8000)
  83. | ((buf[i] >> 11) & 0x1f00000);
  84. }
  85. return ret;
  86. }
  87. static void cobra_poll(struct gameport *gameport)
  88. {
  89. struct cobra *cobra = gameport_get_drvdata(gameport);
  90. struct input_dev *dev;
  91. unsigned int data[2];
  92. int i, j, r;
  93. cobra->reads++;
  94. if ((r = cobra_read_packet(gameport, data)) != cobra->exists) {
  95. cobra->bads++;
  96. return;
  97. }
  98. for (i = 0; i < 2; i++)
  99. if (cobra->exists & r & (1 << i)) {
  100. dev = cobra->dev[i];
  101. input_report_abs(dev, ABS_X, ((data[i] >> 4) & 1) - ((data[i] >> 3) & 1));
  102. input_report_abs(dev, ABS_Y, ((data[i] >> 2) & 1) - ((data[i] >> 1) & 1));
  103. for (j = 0; cobra_btn[j]; j++)
  104. input_report_key(dev, cobra_btn[j], data[i] & (0x20 << j));
  105. input_sync(dev);
  106. }
  107. }
  108. static int cobra_open(struct input_dev *dev)
  109. {
  110. struct cobra *cobra = input_get_drvdata(dev);
  111. gameport_start_polling(cobra->gameport);
  112. return 0;
  113. }
  114. static void cobra_close(struct input_dev *dev)
  115. {
  116. struct cobra *cobra = input_get_drvdata(dev);
  117. gameport_stop_polling(cobra->gameport);
  118. }
  119. static int cobra_connect(struct gameport *gameport, struct gameport_driver *drv)
  120. {
  121. struct cobra *cobra;
  122. struct input_dev *input_dev;
  123. unsigned int data[2];
  124. int i, j;
  125. int err;
  126. cobra = kzalloc(sizeof(struct cobra), GFP_KERNEL);
  127. if (!cobra)
  128. return -ENOMEM;
  129. cobra->gameport = gameport;
  130. gameport_set_drvdata(gameport, cobra);
  131. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  132. if (err)
  133. goto fail1;
  134. cobra->exists = cobra_read_packet(gameport, data);
  135. for (i = 0; i < 2; i++)
  136. if ((cobra->exists >> i) & data[i] & 1) {
  137. printk(KERN_WARNING "cobra.c: Device %d on %s has the Ext bit set. ID is: %d"
  138. " Contact vojtech@ucw.cz\n", i, gameport->phys, (data[i] >> 2) & 7);
  139. cobra->exists &= ~(1 << i);
  140. }
  141. if (!cobra->exists) {
  142. err = -ENODEV;
  143. goto fail2;
  144. }
  145. gameport_set_poll_handler(gameport, cobra_poll);
  146. gameport_set_poll_interval(gameport, 20);
  147. for (i = 0; i < 2; i++) {
  148. if (~(cobra->exists >> i) & 1)
  149. continue;
  150. cobra->dev[i] = input_dev = input_allocate_device();
  151. if (!input_dev) {
  152. err = -ENOMEM;
  153. goto fail3;
  154. }
  155. snprintf(cobra->phys[i], sizeof(cobra->phys[i]),
  156. "%s/input%d", gameport->phys, i);
  157. input_dev->name = "Creative Labs Blaster GamePad Cobra";
  158. input_dev->phys = cobra->phys[i];
  159. input_dev->id.bustype = BUS_GAMEPORT;
  160. input_dev->id.vendor = GAMEPORT_ID_VENDOR_CREATIVE;
  161. input_dev->id.product = 0x0008;
  162. input_dev->id.version = 0x0100;
  163. input_dev->dev.parent = &gameport->dev;
  164. input_set_drvdata(input_dev, cobra);
  165. input_dev->open = cobra_open;
  166. input_dev->close = cobra_close;
  167. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  168. input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
  169. input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
  170. for (j = 0; cobra_btn[j]; j++)
  171. set_bit(cobra_btn[j], input_dev->keybit);
  172. err = input_register_device(cobra->dev[i]);
  173. if (err)
  174. goto fail4;
  175. }
  176. return 0;
  177. fail4: input_free_device(cobra->dev[i]);
  178. fail3: while (--i >= 0)
  179. if (cobra->dev[i])
  180. input_unregister_device(cobra->dev[i]);
  181. fail2: gameport_close(gameport);
  182. fail1: gameport_set_drvdata(gameport, NULL);
  183. kfree(cobra);
  184. return err;
  185. }
  186. static void cobra_disconnect(struct gameport *gameport)
  187. {
  188. struct cobra *cobra = gameport_get_drvdata(gameport);
  189. int i;
  190. for (i = 0; i < 2; i++)
  191. if ((cobra->exists >> i) & 1)
  192. input_unregister_device(cobra->dev[i]);
  193. gameport_close(gameport);
  194. gameport_set_drvdata(gameport, NULL);
  195. kfree(cobra);
  196. }
  197. static struct gameport_driver cobra_drv = {
  198. .driver = {
  199. .name = "cobra",
  200. },
  201. .description = DRIVER_DESC,
  202. .connect = cobra_connect,
  203. .disconnect = cobra_disconnect,
  204. };
  205. static int __init cobra_init(void)
  206. {
  207. return gameport_register_driver(&cobra_drv);
  208. }
  209. static void __exit cobra_exit(void)
  210. {
  211. gameport_unregister_driver(&cobra_drv);
  212. }
  213. module_init(cobra_init);
  214. module_exit(cobra_exit);