twidjoy.c 7.7 KB

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