cobra.c 6.4 KB

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