twidjoy.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2001 Arndt Schoenewald
  4. * Copyright (c) 2000-2001 Vojtech Pavlik
  5. * Copyright (c) 2000 Mark Fletcher
  6. *
  7. * Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
  8. */
  9. /*
  10. * Driver to use Handykey's Twiddler (the first edition, i.e. the one with
  11. * the RS232 interface) as a joystick under Linux
  12. *
  13. * The Twiddler is a one-handed chording keyboard featuring twelve buttons on
  14. * the front, six buttons on the top, and a built-in tilt sensor. The buttons
  15. * on the front, which are grouped as four rows of three buttons, are pressed
  16. * by the four fingers (this implies only one button per row can be held down
  17. * at the same time) and the buttons on the top are for the thumb. The tilt
  18. * sensor delivers X and Y axis data depending on how the Twiddler is held.
  19. * Additional information can be found at http://www.handykey.com.
  20. *
  21. * This driver does not use the Twiddler for its intended purpose, i.e. as
  22. * a chording keyboard, but as a joystick: pressing and releasing a button
  23. * immediately sends a corresponding button event, and tilting it generates
  24. * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game
  25. * controller with amazing 18 buttons :-)
  26. *
  27. * Note: The Twiddler2 (the successor of the Twiddler that connects directly
  28. * to the PS/2 keyboard and mouse ports) is NOT supported by this driver!
  29. *
  30. * For questions or feedback regarding this driver module please contact:
  31. * Arndt Schoenewald <[email protected]>
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/slab.h>
  36. #include <linux/input.h>
  37. #include <linux/serio.h>
  38. #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. /*
  42. * Constants.
  43. */
  44. #define TWIDJOY_MAX_LENGTH 5
  45. static struct twidjoy_button_spec {
  46. int bitshift;
  47. int bitmask;
  48. int buttons[3];
  49. }
  50. twidjoy_buttons[] = {
  51. { 0, 3, { BTN_A, BTN_B, BTN_C } },
  52. { 2, 3, { BTN_X, BTN_Y, BTN_Z } },
  53. { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },
  54. { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
  55. { 8, 1, { BTN_BASE5 } },
  56. { 9, 1, { BTN_BASE } },
  57. { 10, 1, { BTN_BASE3 } },
  58. { 11, 1, { BTN_BASE4 } },
  59. { 12, 1, { BTN_BASE2 } },
  60. { 13, 1, { BTN_BASE6 } },
  61. { 0, 0, { 0 } }
  62. };
  63. /*
  64. * Per-Twiddler data.
  65. */
  66. struct twidjoy {
  67. struct input_dev *dev;
  68. int idx;
  69. unsigned char data[TWIDJOY_MAX_LENGTH];
  70. char phys[32];
  71. };
  72. /*
  73. * twidjoy_process_packet() decodes packets the driver receives from the
  74. * Twiddler. It updates the data accordingly.
  75. */
  76. static void twidjoy_process_packet(struct twidjoy *twidjoy)
  77. {
  78. struct input_dev *dev = twidjoy->dev;
  79. unsigned char *data = twidjoy->data;
  80. struct twidjoy_button_spec *bp;
  81. int button_bits, abs_x, abs_y;
  82. button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
  83. for (bp = twidjoy_buttons; bp->bitmask; bp++) {
  84. int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
  85. int i;
  86. for (i = 0; i < bp->bitmask; i++)
  87. input_report_key(dev, bp->buttons[i], i+1 == value);
  88. }
  89. abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
  90. if (data[4] & 0x08) abs_x -= 256;
  91. abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
  92. if (data[3] & 0x02) abs_y -= 256;
  93. input_report_abs(dev, ABS_X, -abs_x);
  94. input_report_abs(dev, ABS_Y, +abs_y);
  95. input_sync(dev);
  96. }
  97. /*
  98. * twidjoy_interrupt() is called by the low level driver when characters
  99. * are ready for us. We then buffer them for further processing, or call the
  100. * packet processing routine.
  101. */
  102. static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
  103. {
  104. struct twidjoy *twidjoy = serio_get_drvdata(serio);
  105. /* All Twiddler packets are 5 bytes. The fact that the first byte
  106. * has a MSB of 0 and all other bytes have a MSB of 1 can be used
  107. * to check and regain sync. */
  108. if ((data & 0x80) == 0)
  109. twidjoy->idx = 0; /* this byte starts a new packet */
  110. else if (twidjoy->idx == 0)
  111. return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
  112. if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
  113. twidjoy->data[twidjoy->idx++] = data;
  114. if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
  115. twidjoy_process_packet(twidjoy);
  116. twidjoy->idx = 0;
  117. }
  118. return IRQ_HANDLED;
  119. }
  120. /*
  121. * twidjoy_disconnect() is the opposite of twidjoy_connect()
  122. */
  123. static void twidjoy_disconnect(struct serio *serio)
  124. {
  125. struct twidjoy *twidjoy = serio_get_drvdata(serio);
  126. serio_close(serio);
  127. serio_set_drvdata(serio, NULL);
  128. input_unregister_device(twidjoy->dev);
  129. kfree(twidjoy);
  130. }
  131. /*
  132. * twidjoy_connect() is the routine that is called when someone adds a
  133. * new serio device. It looks for the Twiddler, and if found, registers
  134. * it as an input device.
  135. */
  136. static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
  137. {
  138. struct twidjoy_button_spec *bp;
  139. struct twidjoy *twidjoy;
  140. struct input_dev *input_dev;
  141. int err = -ENOMEM;
  142. int i;
  143. twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
  144. input_dev = input_allocate_device();
  145. if (!twidjoy || !input_dev)
  146. goto fail1;
  147. twidjoy->dev = input_dev;
  148. snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
  149. input_dev->name = "Handykey Twiddler";
  150. input_dev->phys = twidjoy->phys;
  151. input_dev->id.bustype = BUS_RS232;
  152. input_dev->id.vendor = SERIO_TWIDJOY;
  153. input_dev->id.product = 0x0001;
  154. input_dev->id.version = 0x0100;
  155. input_dev->dev.parent = &serio->dev;
  156. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  157. input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
  158. input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
  159. for (bp = twidjoy_buttons; bp->bitmask; bp++)
  160. for (i = 0; i < bp->bitmask; i++)
  161. set_bit(bp->buttons[i], input_dev->keybit);
  162. serio_set_drvdata(serio, twidjoy);
  163. err = serio_open(serio, drv);
  164. if (err)
  165. goto fail2;
  166. err = input_register_device(twidjoy->dev);
  167. if (err)
  168. goto fail3;
  169. return 0;
  170. fail3: serio_close(serio);
  171. fail2: serio_set_drvdata(serio, NULL);
  172. fail1: input_free_device(input_dev);
  173. kfree(twidjoy);
  174. return err;
  175. }
  176. /*
  177. * The serio driver structure.
  178. */
  179. static const struct serio_device_id twidjoy_serio_ids[] = {
  180. {
  181. .type = SERIO_RS232,
  182. .proto = SERIO_TWIDJOY,
  183. .id = SERIO_ANY,
  184. .extra = SERIO_ANY,
  185. },
  186. { 0 }
  187. };
  188. MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
  189. static struct serio_driver twidjoy_drv = {
  190. .driver = {
  191. .name = "twidjoy",
  192. },
  193. .description = DRIVER_DESC,
  194. .id_table = twidjoy_serio_ids,
  195. .interrupt = twidjoy_interrupt,
  196. .connect = twidjoy_connect,
  197. .disconnect = twidjoy_disconnect,
  198. };
  199. module_serio_driver(twidjoy_drv);