cobra.c 6.2 KB

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