cobra.c 6.5 KB

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