twidjoy.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver"
  53. MODULE_DESCRIPTION(DRIVER_DESC);
  54. MODULE_LICENSE("GPL");
  55. /*
  56. * Constants.
  57. */
  58. #define TWIDJOY_MAX_LENGTH 5
  59. static struct twidjoy_button_spec {
  60. int bitshift;
  61. int bitmask;
  62. int buttons[3];
  63. }
  64. twidjoy_buttons[] = {
  65. { 0, 3, { BTN_A, BTN_B, BTN_C } },
  66. { 2, 3, { BTN_X, BTN_Y, BTN_Z } },
  67. { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } },
  68. { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } },
  69. { 8, 1, { BTN_BASE5 } },
  70. { 9, 1, { BTN_BASE } },
  71. { 10, 1, { BTN_BASE3 } },
  72. { 11, 1, { BTN_BASE4 } },
  73. { 12, 1, { BTN_BASE2 } },
  74. { 13, 1, { BTN_BASE6 } },
  75. { 0, 0, { 0 } }
  76. };
  77. /*
  78. * Per-Twiddler data.
  79. */
  80. struct twidjoy {
  81. struct input_dev *dev;
  82. int idx;
  83. unsigned char data[TWIDJOY_MAX_LENGTH];
  84. char phys[32];
  85. };
  86. /*
  87. * twidjoy_process_packet() decodes packets the driver receives from the
  88. * Twiddler. It updates the data accordingly.
  89. */
  90. static void twidjoy_process_packet(struct twidjoy *twidjoy)
  91. {
  92. struct input_dev *dev = twidjoy->dev;
  93. unsigned char *data = twidjoy->data;
  94. struct twidjoy_button_spec *bp;
  95. int button_bits, abs_x, abs_y;
  96. button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f);
  97. for (bp = twidjoy_buttons; bp->bitmask; bp++) {
  98. int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift;
  99. int i;
  100. for (i = 0; i < bp->bitmask; i++)
  101. input_report_key(dev, bp->buttons[i], i+1 == value);
  102. }
  103. abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2);
  104. if (data[4] & 0x08) abs_x -= 256;
  105. abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0);
  106. if (data[3] & 0x02) abs_y -= 256;
  107. input_report_abs(dev, ABS_X, -abs_x);
  108. input_report_abs(dev, ABS_Y, +abs_y);
  109. input_sync(dev);
  110. }
  111. /*
  112. * twidjoy_interrupt() is called by the low level driver when characters
  113. * are ready for us. We then buffer them for further processing, or call the
  114. * packet processing routine.
  115. */
  116. static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
  117. {
  118. struct twidjoy *twidjoy = serio_get_drvdata(serio);
  119. /* All Twiddler packets are 5 bytes. The fact that the first byte
  120. * has a MSB of 0 and all other bytes have a MSB of 1 can be used
  121. * to check and regain sync. */
  122. if ((data & 0x80) == 0)
  123. twidjoy->idx = 0; /* this byte starts a new packet */
  124. else if (twidjoy->idx == 0)
  125. return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
  126. if (twidjoy->idx < TWIDJOY_MAX_LENGTH)
  127. twidjoy->data[twidjoy->idx++] = data;
  128. if (twidjoy->idx == TWIDJOY_MAX_LENGTH) {
  129. twidjoy_process_packet(twidjoy);
  130. twidjoy->idx = 0;
  131. }
  132. return IRQ_HANDLED;
  133. }
  134. /*
  135. * twidjoy_disconnect() is the opposite of twidjoy_connect()
  136. */
  137. static void twidjoy_disconnect(struct serio *serio)
  138. {
  139. struct twidjoy *twidjoy = serio_get_drvdata(serio);
  140. serio_close(serio);
  141. serio_set_drvdata(serio, NULL);
  142. input_unregister_device(twidjoy->dev);
  143. kfree(twidjoy);
  144. }
  145. /*
  146. * twidjoy_connect() is the routine that is called when someone adds a
  147. * new serio device. It looks for the Twiddler, and if found, registers
  148. * it as an input device.
  149. */
  150. static int twidjoy_connect(struct serio *serio, struct serio_driver *drv)
  151. {
  152. struct twidjoy_button_spec *bp;
  153. struct twidjoy *twidjoy;
  154. struct input_dev *input_dev;
  155. int err = -ENOMEM;
  156. int i;
  157. twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL);
  158. input_dev = input_allocate_device();
  159. if (!twidjoy || !input_dev)
  160. goto fail1;
  161. twidjoy->dev = input_dev;
  162. snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys);
  163. input_dev->name = "Handykey Twiddler";
  164. input_dev->phys = twidjoy->phys;
  165. input_dev->id.bustype = BUS_RS232;
  166. input_dev->id.vendor = SERIO_TWIDJOY;
  167. input_dev->id.product = 0x0001;
  168. input_dev->id.version = 0x0100;
  169. input_dev->dev.parent = &serio->dev;
  170. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  171. input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4);
  172. input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4);
  173. for (bp = twidjoy_buttons; bp->bitmask; bp++)
  174. for (i = 0; i < bp->bitmask; i++)
  175. set_bit(bp->buttons[i], input_dev->keybit);
  176. serio_set_drvdata(serio, twidjoy);
  177. err = serio_open(serio, drv);
  178. if (err)
  179. goto fail2;
  180. err = input_register_device(twidjoy->dev);
  181. if (err)
  182. goto fail3;
  183. return 0;
  184. fail3: serio_close(serio);
  185. fail2: serio_set_drvdata(serio, NULL);
  186. fail1: input_free_device(input_dev);
  187. kfree(twidjoy);
  188. return err;
  189. }
  190. /*
  191. * The serio driver structure.
  192. */
  193. static struct serio_device_id twidjoy_serio_ids[] = {
  194. {
  195. .type = SERIO_RS232,
  196. .proto = SERIO_TWIDJOY,
  197. .id = SERIO_ANY,
  198. .extra = SERIO_ANY,
  199. },
  200. { 0 }
  201. };
  202. MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids);
  203. static struct serio_driver twidjoy_drv = {
  204. .driver = {
  205. .name = "twidjoy",
  206. },
  207. .description = DRIVER_DESC,
  208. .id_table = twidjoy_serio_ids,
  209. .interrupt = twidjoy_interrupt,
  210. .connect = twidjoy_connect,
  211. .disconnect = twidjoy_disconnect,
  212. };
  213. module_serio_driver(twidjoy_drv);