cobra.c 5.5 KB

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