cobra.c 5.5 KB

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