twidjoy.c 7.5 KB

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