twidjoy.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 char *twidjoy_name = "Handykey Twiddler";
  65. static struct twidjoy_button_spec {
  66. int bitshift;
  67. int bitmask;
  68. int buttons[3];
  69. }
  70. twidjoy_buttons[] = {
  71. { 0, 3, { BTN_A, BTN_B, BTN_C } },
  72. { 2, 3, { BTN_X, BTN_Y, BTN_Z } },
  73. { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },
  74. { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
  75. { 8, 1, { BTN_BASE5 } },
  76. { 9, 1, { BTN_BASE } },
  77. { 10, 1, { BTN_BASE3 } },
  78. { 11, 1, { BTN_BASE4 } },
  79. { 12, 1, { BTN_BASE2 } },
  80. { 13, 1, { BTN_BASE6 } },
  81. { 0, 0, { 0 } }
  82. };
  83. /*
  84. * Per-Twiddler data.
  85. */
  86. struct twidjoy {
  87. struct input_dev dev;
  88. int idx;
  89. unsigned char data[TWIDJOY_MAX_LENGTH];
  90. char phys[32];
  91. };
  92. /*
  93. * twidjoy_process_packet() decodes packets the driver receives from the
  94. * Twiddler. It updates the data accordingly.
  95. */
  96. static void twidjoy_process_packet(struct twidjoy *twidjoy, struct pt_regs *regs)
  97. {
  98. if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
  99. struct input_dev *dev = &twidjoy->dev;
  100. unsigned char *data = twidjoy->data;
  101. struct twidjoy_button_spec *bp;
  102. int button_bits, abs_x, abs_y;
  103. button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
  104. input_regs(dev, regs);
  105. for (bp = twidjoy_buttons; bp->bitmask; bp++) {
  106. int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
  107. int i;
  108. for (i = 0; i < bp->bitmask; i++)
  109. input_report_key(dev, bp->buttons[i], i+1 == value);
  110. }
  111. abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
  112. if (data[4] & 0x08) abs_x -= 256;
  113. abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
  114. if (data[3] & 0x02) abs_y -= 256;
  115. input_report_abs(dev, ABS_X, -abs_x);
  116. input_report_abs(dev, ABS_Y, +abs_y);
  117. input_sync(dev);
  118. }
  119. return;
  120. }
  121. /*
  122. * twidjoy_interrupt() is called by the low level driver when characters
  123. * are ready for us. We then buffer them for further processing, or call the
  124. * packet processing routine.
  125. */
  126. static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags, struct pt_regs *regs)
  127. {
  128. struct twidjoy *twidjoy = serio_get_drvdata(serio);
  129. /* All Twiddler packets are 5 bytes. The fact that the first byte
  130. * has a MSB of 0 and all other bytes have a MSB of 1 can be used
  131. * to check and regain sync. */
  132. if ((data & 0x80) == 0)
  133. twidjoy->idx = 0; /* this byte starts a new packet */
  134. else if (twidjoy->idx == 0)
  135. return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
  136. if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
  137. twidjoy->data[twidjoy->idx++] = data;
  138. if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
  139. twidjoy_process_packet(twidjoy, regs);
  140. twidjoy->idx = 0;
  141. }
  142. return IRQ_HANDLED;
  143. }
  144. /*
  145. * twidjoy_disconnect() is the opposite of twidjoy_connect()
  146. */
  147. static void twidjoy_disconnect(struct serio *serio)
  148. {
  149. struct twidjoy *twidjoy = serio_get_drvdata(serio);
  150. input_unregister_device(&twidjoy->dev);
  151. serio_close(serio);
  152. serio_set_drvdata(serio, NULL);
  153. kfree(twidjoy);
  154. }
  155. /*
  156. * twidjoy_connect() is the routine that is called when someone adds a
  157. * new serio device. It looks for the Twiddler, and if found, registers
  158. * it as an input device.
  159. */
  160. static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
  161. {
  162. struct twidjoy_button_spec *bp;
  163. struct twidjoy *twidjoy;
  164. int i;
  165. int err;
  166. if (!(twidjoy = kmalloc(sizeof(struct twidjoy), GFP_KERNEL)))
  167. return -ENOMEM;
  168. memset(twidjoy, 0, sizeof(struct twidjoy));
  169. sprintf(twidjoy->phys, "%s/input0", serio->phys);
  170. init_input_dev(&twidjoy->dev);
  171. twidjoy->dev.name = twidjoy_name;
  172. twidjoy->dev.phys = twidjoy->phys;
  173. twidjoy->dev.id.bustype = BUS_RS232;
  174. twidjoy->dev.id.vendor = SERIO_TWIDJOY;
  175. twidjoy->dev.id.product = 0x0001;
  176. twidjoy->dev.id.version = 0x0100;
  177. twidjoy->dev.dev = &serio->dev;
  178. twidjoy->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  179. for (bp = twidjoy_buttons; bp->bitmask; bp++) {
  180. for (i = 0; i < bp->bitmask; i++)
  181. set_bit(bp->buttons[i], twidjoy->dev.keybit);
  182. }
  183. twidjoy->dev.absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
  184. for (i = 0; i < 2; i++) {
  185. twidjoy->dev.absmax[ABS_X+i] = 50;
  186. twidjoy->dev.absmin[ABS_X+i] = -50;
  187. /* TODO: arndt 20010708: Are these values appropriate? */
  188. twidjoy->dev.absfuzz[ABS_X+i] = 4;
  189. twidjoy->dev.absflat[ABS_X+i] = 4;
  190. }
  191. twidjoy->dev.private = twidjoy;
  192. serio_set_drvdata(serio, twidjoy);
  193. err = serio_open(serio, drv);
  194. if (err) {
  195. serio_set_drvdata(serio, NULL);
  196. kfree(twidjoy);
  197. return err;
  198. }
  199. input_register_device(&twidjoy->dev);
  200. printk(KERN_INFO "input: %s on %s\n", twidjoy_name, serio->phys);
  201. return 0;
  202. }
  203. /*
  204. * The serio driver structure.
  205. */
  206. static struct serio_device_id twidjoy_serio_ids[] = {
  207. {
  208. .type = SERIO_RS232,
  209. .proto = SERIO_TWIDJOY,
  210. .id = SERIO_ANY,
  211. .extra = SERIO_ANY,
  212. },
  213. { 0 }
  214. };
  215. MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
  216. static struct serio_driver twidjoy_drv = {
  217. .driver = {
  218. .name = "twidjoy",
  219. },
  220. .description = DRIVER_DESC,
  221. .id_table = twidjoy_serio_ids,
  222. .interrupt = twidjoy_interrupt,
  223. .connect = twidjoy_connect,
  224. .disconnect = twidjoy_disconnect,
  225. };
  226. /*
  227. * The functions for inserting/removing us as a module.
  228. */
  229. int __init twidjoy_init(void)
  230. {
  231. serio_register_driver(&twidjoy_drv);
  232. return 0;
  233. }
  234. void __exit twidjoy_exit(void)
  235. {
  236. serio_unregister_driver(&twidjoy_drv);
  237. }
  238. module_init(twidjoy_init);
  239. module_exit(twidjoy_exit);