spaceball.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 1999-2001 Vojtech Pavlik
  3. *
  4. * Based on the work of:
  5. * David Thompson
  6. * Joseph Krahn
  7. */
  8. /*
  9. * SpaceTec SpaceBall 2003/3003/4000 FLX driver for Linux
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * Should you need to contact me, the author, you can do so either by
  27. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  28. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/input.h>
  34. #include <linux/serio.h>
  35. #define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver"
  36. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  37. MODULE_DESCRIPTION(DRIVER_DESC);
  38. MODULE_LICENSE("GPL");
  39. /*
  40. * Constants.
  41. */
  42. #define SPACEBALL_MAX_LENGTH 128
  43. #define SPACEBALL_MAX_ID 9
  44. #define SPACEBALL_1003 1
  45. #define SPACEBALL_2003B 3
  46. #define SPACEBALL_2003C 4
  47. #define SPACEBALL_3003C 7
  48. #define SPACEBALL_4000FLX 8
  49. #define SPACEBALL_4000FLX_L 9
  50. static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
  51. static char *spaceball_names[] = {
  52. "?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B",
  53. "SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController",
  54. "SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" };
  55. /*
  56. * Per-Ball data.
  57. */
  58. struct spaceball {
  59. struct input_dev *dev;
  60. int idx;
  61. int escape;
  62. unsigned char data[SPACEBALL_MAX_LENGTH];
  63. char phys[32];
  64. };
  65. /*
  66. * spaceball_process_packet() decodes packets the driver receives from the
  67. * SpaceBall.
  68. */
  69. static void spaceball_process_packet(struct spaceball* spaceball)
  70. {
  71. struct input_dev *dev = spaceball->dev;
  72. unsigned char *data = spaceball->data;
  73. int i;
  74. if (spaceball->idx < 2) return;
  75. switch (spaceball->data[0]) {
  76. case 'D': /* Ball data */
  77. if (spaceball->idx != 15) return;
  78. for (i = 0; i < 6; i++)
  79. input_report_abs(dev, spaceball_axes[i],
  80. (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2]));
  81. break;
  82. case 'K': /* Button data */
  83. if (spaceball->idx != 3) return;
  84. input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
  85. input_report_key(dev, BTN_2, data[2] & 0x02);
  86. input_report_key(dev, BTN_3, data[2] & 0x04);
  87. input_report_key(dev, BTN_4, data[2] & 0x08);
  88. input_report_key(dev, BTN_5, data[1] & 0x01);
  89. input_report_key(dev, BTN_6, data[1] & 0x02);
  90. input_report_key(dev, BTN_7, data[1] & 0x04);
  91. input_report_key(dev, BTN_8, data[1] & 0x10);
  92. break;
  93. case '.': /* Advanced button data */
  94. if (spaceball->idx != 3) return;
  95. input_report_key(dev, BTN_1, data[2] & 0x01);
  96. input_report_key(dev, BTN_2, data[2] & 0x02);
  97. input_report_key(dev, BTN_3, data[2] & 0x04);
  98. input_report_key(dev, BTN_4, data[2] & 0x08);
  99. input_report_key(dev, BTN_5, data[2] & 0x10);
  100. input_report_key(dev, BTN_6, data[2] & 0x20);
  101. input_report_key(dev, BTN_7, data[2] & 0x80);
  102. input_report_key(dev, BTN_8, data[1] & 0x01);
  103. input_report_key(dev, BTN_9, data[1] & 0x02);
  104. input_report_key(dev, BTN_A, data[1] & 0x04);
  105. input_report_key(dev, BTN_B, data[1] & 0x08);
  106. input_report_key(dev, BTN_C, data[1] & 0x10);
  107. input_report_key(dev, BTN_MODE, data[1] & 0x20);
  108. break;
  109. case 'E': /* Device error */
  110. spaceball->data[spaceball->idx - 1] = 0;
  111. printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
  112. break;
  113. case '?': /* Bad command packet */
  114. spaceball->data[spaceball->idx - 1] = 0;
  115. printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
  116. break;
  117. }
  118. input_sync(dev);
  119. }
  120. /*
  121. * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
  122. * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
  123. * can occur in the axis values.
  124. */
  125. static irqreturn_t spaceball_interrupt(struct serio *serio,
  126. unsigned char data, unsigned int flags)
  127. {
  128. struct spaceball *spaceball = serio_get_drvdata(serio);
  129. switch (data) {
  130. case 0xd:
  131. spaceball_process_packet(spaceball);
  132. spaceball->idx = 0;
  133. spaceball->escape = 0;
  134. break;
  135. case '^':
  136. if (!spaceball->escape) {
  137. spaceball->escape = 1;
  138. break;
  139. }
  140. spaceball->escape = 0;
  141. case 'M':
  142. case 'Q':
  143. case 'S':
  144. if (spaceball->escape) {
  145. spaceball->escape = 0;
  146. data &= 0x1f;
  147. }
  148. default:
  149. if (spaceball->escape)
  150. spaceball->escape = 0;
  151. if (spaceball->idx < SPACEBALL_MAX_LENGTH)
  152. spaceball->data[spaceball->idx++] = data;
  153. break;
  154. }
  155. return IRQ_HANDLED;
  156. }
  157. /*
  158. * spaceball_disconnect() is the opposite of spaceball_connect()
  159. */
  160. static void spaceball_disconnect(struct serio *serio)
  161. {
  162. struct spaceball* spaceball = serio_get_drvdata(serio);
  163. serio_close(serio);
  164. serio_set_drvdata(serio, NULL);
  165. input_unregister_device(spaceball->dev);
  166. kfree(spaceball);
  167. }
  168. /*
  169. * spaceball_connect() is the routine that is called when someone adds a
  170. * new serio device that supports Spaceball protocol and registers it as
  171. * an input device.
  172. */
  173. static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
  174. {
  175. struct spaceball *spaceball;
  176. struct input_dev *input_dev;
  177. int err = -ENOMEM;
  178. int i, id;
  179. if ((id = serio->id.id) > SPACEBALL_MAX_ID)
  180. return -ENODEV;
  181. spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL);
  182. input_dev = input_allocate_device();
  183. if (!spaceball || !input_dev)
  184. goto fail1;
  185. spaceball->dev = input_dev;
  186. snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys);
  187. input_dev->name = spaceball_names[id];
  188. input_dev->phys = spaceball->phys;
  189. input_dev->id.bustype = BUS_RS232;
  190. input_dev->id.vendor = SERIO_SPACEBALL;
  191. input_dev->id.product = id;
  192. input_dev->id.version = 0x0100;
  193. input_dev->dev.parent = &serio->dev;
  194. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  195. switch (id) {
  196. case SPACEBALL_4000FLX:
  197. case SPACEBALL_4000FLX_L:
  198. input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_9);
  199. input_dev->keybit[BIT_WORD(BTN_A)] |= BIT_MASK(BTN_A) |
  200. BIT_MASK(BTN_B) | BIT_MASK(BTN_C) |
  201. BIT_MASK(BTN_MODE);
  202. default:
  203. input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_2) |
  204. BIT_MASK(BTN_3) | BIT_MASK(BTN_4) |
  205. BIT_MASK(BTN_5) | BIT_MASK(BTN_6) |
  206. BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
  207. case SPACEBALL_3003C:
  208. input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1) |
  209. BIT_MASK(BTN_8);
  210. }
  211. for (i = 0; i < 3; i++) {
  212. input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40);
  213. input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8);
  214. }
  215. serio_set_drvdata(serio, spaceball);
  216. err = serio_open(serio, drv);
  217. if (err)
  218. goto fail2;
  219. err = input_register_device(spaceball->dev);
  220. if (err)
  221. goto fail3;
  222. return 0;
  223. fail3: serio_close(serio);
  224. fail2: serio_set_drvdata(serio, NULL);
  225. fail1: input_free_device(input_dev);
  226. kfree(spaceball);
  227. return err;
  228. }
  229. /*
  230. * The serio driver structure.
  231. */
  232. static struct serio_device_id spaceball_serio_ids[] = {
  233. {
  234. .type = SERIO_RS232,
  235. .proto = SERIO_SPACEBALL,
  236. .id = SERIO_ANY,
  237. .extra = SERIO_ANY,
  238. },
  239. { 0 }
  240. };
  241. MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
  242. static struct serio_driver spaceball_drv = {
  243. .driver = {
  244. .name = "spaceball",
  245. },
  246. .description = DRIVER_DESC,
  247. .id_table = spaceball_serio_ids,
  248. .interrupt = spaceball_interrupt,
  249. .connect = spaceball_connect,
  250. .disconnect = spaceball_disconnect,
  251. };
  252. module_serio_driver(spaceball_drv);