cobra.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #include <linux/jiffies.h>
  35. #define DRIVER_DESC "Creative Labs Blaster GamePad Cobra driver"
  36. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  37. MODULE_DESCRIPTION(DRIVER_DESC);
  38. MODULE_LICENSE("GPL");
  39. #define COBRA_MAX_STROBE 45 /* 45 us max wait for first strobe */
  40. #define COBRA_LENGTH 36
  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. struct input_dev *input_dev;
  125. unsigned int data[2];
  126. int i, j;
  127. int err;
  128. cobra = kzalloc(sizeof(struct cobra), GFP_KERNEL);
  129. if (!cobra)
  130. return -ENOMEM;
  131. cobra->gameport = gameport;
  132. gameport_set_drvdata(gameport, cobra);
  133. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  134. if (err)
  135. goto fail1;
  136. cobra->exists = cobra_read_packet(gameport, data);
  137. for (i = 0; i < 2; i++)
  138. if ((cobra->exists >> i) & data[i] & 1) {
  139. printk(KERN_WARNING "cobra.c: Device %d on %s has the Ext bit set. ID is: %d"
  140. " Contact vojtech@ucw.cz\n", i, gameport->phys, (data[i] >> 2) & 7);
  141. cobra->exists &= ~(1 << i);
  142. }
  143. if (!cobra->exists) {
  144. err = -ENODEV;
  145. goto fail2;
  146. }
  147. gameport_set_poll_handler(gameport, cobra_poll);
  148. gameport_set_poll_interval(gameport, 20);
  149. for (i = 0; i < 2; i++) {
  150. if (~(cobra->exists >> i) & 1)
  151. continue;
  152. cobra->dev[i] = input_dev = input_allocate_device();
  153. if (!input_dev) {
  154. err = -ENOMEM;
  155. goto fail3;
  156. }
  157. sprintf(cobra->phys[i], "%s/input%d", gameport->phys, i);
  158. input_dev->name = "Creative Labs Blaster GamePad Cobra";
  159. input_dev->phys = cobra->phys[i];
  160. input_dev->id.bustype = BUS_GAMEPORT;
  161. input_dev->id.vendor = GAMEPORT_ID_VENDOR_CREATIVE;
  162. input_dev->id.product = 0x0008;
  163. input_dev->id.version = 0x0100;
  164. input_dev->cdev.dev = &gameport->dev;
  165. input_dev->private = cobra;
  166. input_dev->open = cobra_open;
  167. input_dev->close = cobra_close;
  168. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  169. input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
  170. input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
  171. for (j = 0; cobra_btn[j]; j++)
  172. set_bit(cobra_btn[j], input_dev->keybit);
  173. input_register_device(cobra->dev[i]);
  174. }
  175. return 0;
  176. fail3: for (i = 0; i < 2; i++)
  177. if (cobra->dev[i])
  178. input_unregister_device(cobra->dev[i]);
  179. fail2: gameport_close(gameport);
  180. fail1: gameport_set_drvdata(gameport, NULL);
  181. kfree(cobra);
  182. return err;
  183. }
  184. static void cobra_disconnect(struct gameport *gameport)
  185. {
  186. struct cobra *cobra = gameport_get_drvdata(gameport);
  187. int i;
  188. for (i = 0; i < 2; i++)
  189. if ((cobra->exists >> i) & 1)
  190. input_unregister_device(cobra->dev[i]);
  191. gameport_close(gameport);
  192. gameport_set_drvdata(gameport, NULL);
  193. kfree(cobra);
  194. }
  195. static struct gameport_driver cobra_drv = {
  196. .driver = {
  197. .name = "cobra",
  198. },
  199. .description = DRIVER_DESC,
  200. .connect = cobra_connect,
  201. .disconnect = cobra_disconnect,
  202. };
  203. static int __init cobra_init(void)
  204. {
  205. gameport_register_driver(&cobra_drv);
  206. return 0;
  207. }
  208. static void __exit cobra_exit(void)
  209. {
  210. gameport_unregister_driver(&cobra_drv);
  211. }
  212. module_init(cobra_init);
  213. module_exit(cobra_exit);