spaceball.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/init.h>
  34. #include <linux/input.h>
  35. #include <linux/serio.h>
  36. #define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver"
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. /*
  41. * Constants.
  42. */
  43. #define SPACEBALL_MAX_LENGTH 128
  44. #define SPACEBALL_MAX_ID 9
  45. #define SPACEBALL_1003 1
  46. #define SPACEBALL_2003B 3
  47. #define SPACEBALL_2003C 4
  48. #define SPACEBALL_3003C 7
  49. #define SPACEBALL_4000FLX 8
  50. #define SPACEBALL_4000FLX_L 9
  51. static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
  52. static char *spaceball_names[] = {
  53. "?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B",
  54. "SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController",
  55. "SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" };
  56. /*
  57. * Per-Ball data.
  58. */
  59. struct spaceball {
  60. struct input_dev *dev;
  61. int idx;
  62. int escape;
  63. unsigned char data[SPACEBALL_MAX_LENGTH];
  64. char phys[32];
  65. };
  66. /*
  67. * spaceball_process_packet() decodes packets the driver receives from the
  68. * SpaceBall.
  69. */
  70. static void spaceball_process_packet(struct spaceball* spaceball)
  71. {
  72. struct input_dev *dev = spaceball->dev;
  73. unsigned char *data = spaceball->data;
  74. int i;
  75. if (spaceball->idx < 2) return;
  76. switch (spaceball->data[0]) {
  77. case 'D': /* Ball data */
  78. if (spaceball->idx != 15) return;
  79. for (i = 0; i < 6; i++)
  80. input_report_abs(dev, spaceball_axes[i],
  81. (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2]));
  82. break;
  83. case 'K': /* Button data */
  84. if (spaceball->idx != 3) return;
  85. input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
  86. input_report_key(dev, BTN_2, data[2] & 0x02);
  87. input_report_key(dev, BTN_3, data[2] & 0x04);
  88. input_report_key(dev, BTN_4, data[2] & 0x08);
  89. input_report_key(dev, BTN_5, data[1] & 0x01);
  90. input_report_key(dev, BTN_6, data[1] & 0x02);
  91. input_report_key(dev, BTN_7, data[1] & 0x04);
  92. input_report_key(dev, BTN_8, data[1] & 0x10);
  93. break;
  94. case '.': /* Advanced button data */
  95. if (spaceball->idx != 3) return;
  96. input_report_key(dev, BTN_1, data[2] & 0x01);
  97. input_report_key(dev, BTN_2, data[2] & 0x02);
  98. input_report_key(dev, BTN_3, data[2] & 0x04);
  99. input_report_key(dev, BTN_4, data[2] & 0x08);
  100. input_report_key(dev, BTN_5, data[2] & 0x10);
  101. input_report_key(dev, BTN_6, data[2] & 0x20);
  102. input_report_key(dev, BTN_7, data[2] & 0x80);
  103. input_report_key(dev, BTN_8, data[1] & 0x01);
  104. input_report_key(dev, BTN_9, data[1] & 0x02);
  105. input_report_key(dev, BTN_A, data[1] & 0x04);
  106. input_report_key(dev, BTN_B, data[1] & 0x08);
  107. input_report_key(dev, BTN_C, data[1] & 0x10);
  108. input_report_key(dev, BTN_MODE, data[1] & 0x20);
  109. break;
  110. case 'E': /* Device error */
  111. spaceball->data[spaceball->idx - 1] = 0;
  112. printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
  113. break;
  114. case '?': /* Bad command packet */
  115. spaceball->data[spaceball->idx - 1] = 0;
  116. printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
  117. break;
  118. }
  119. input_sync(dev);
  120. }
  121. /*
  122. * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
  123. * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
  124. * can occur in the axis values.
  125. */
  126. static irqreturn_t spaceball_interrupt(struct serio *serio,
  127. unsigned char data, unsigned int flags)
  128. {
  129. struct spaceball *spaceball = serio_get_drvdata(serio);
  130. switch (data) {
  131. case 0xd:
  132. spaceball_process_packet(spaceball);
  133. spaceball->idx = 0;
  134. spaceball->escape = 0;
  135. break;
  136. case '^':
  137. if (!spaceball->escape) {
  138. spaceball->escape = 1;
  139. break;
  140. }
  141. spaceball->escape = 0;
  142. case 'M':
  143. case 'Q':
  144. case 'S':
  145. if (spaceball->escape) {
  146. spaceball->escape = 0;
  147. data &= 0x1f;
  148. }
  149. default:
  150. if (spaceball->escape)
  151. spaceball->escape = 0;
  152. if (spaceball->idx < SPACEBALL_MAX_LENGTH)
  153. spaceball->data[spaceball->idx++] = data;
  154. break;
  155. }
  156. return IRQ_HANDLED;
  157. }
  158. /*
  159. * spaceball_disconnect() is the opposite of spaceball_connect()
  160. */
  161. static void spaceball_disconnect(struct serio *serio)
  162. {
  163. struct spaceball* spaceball = serio_get_drvdata(serio);
  164. serio_close(serio);
  165. serio_set_drvdata(serio, NULL);
  166. input_unregister_device(spaceball->dev);
  167. kfree(spaceball);
  168. }
  169. /*
  170. * spaceball_connect() is the routine that is called when someone adds a
  171. * new serio device that supports Spaceball protocol and registers it as
  172. * an input device.
  173. */
  174. static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
  175. {
  176. struct spaceball *spaceball;
  177. struct input_dev *input_dev;
  178. int err = -ENOMEM;
  179. int i, id;
  180. if ((id = serio->id.id) > SPACEBALL_MAX_ID)
  181. return -ENODEV;
  182. spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL);
  183. input_dev = input_allocate_device();
  184. if (!spaceball || !input_dev)
  185. goto fail1;
  186. spaceball->dev = input_dev;
  187. snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys);
  188. input_dev->name = spaceball_names[id];
  189. input_dev->phys = spaceball->phys;
  190. input_dev->id.bustype = BUS_RS232;
  191. input_dev->id.vendor = SERIO_SPACEBALL;
  192. input_dev->id.product = id;
  193. input_dev->id.version = 0x0100;
  194. input_dev->dev.parent = &serio->dev;
  195. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  196. switch (id) {
  197. case SPACEBALL_4000FLX:
  198. case SPACEBALL_4000FLX_L:
  199. input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_9);
  200. input_dev->keybit[BIT_WORD(BTN_A)] |= BIT_MASK(BTN_A) |
  201. BIT_MASK(BTN_B) | BIT_MASK(BTN_C) |
  202. BIT_MASK(BTN_MODE);
  203. default:
  204. input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_2) |
  205. BIT_MASK(BTN_3) | BIT_MASK(BTN_4) |
  206. BIT_MASK(BTN_5) | BIT_MASK(BTN_6) |
  207. BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
  208. case SPACEBALL_3003C:
  209. input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1) |
  210. BIT_MASK(BTN_8);
  211. }
  212. for (i = 0; i < 3; i++) {
  213. input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40);
  214. input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8);
  215. }
  216. serio_set_drvdata(serio, spaceball);
  217. err = serio_open(serio, drv);
  218. if (err)
  219. goto fail2;
  220. err = input_register_device(spaceball->dev);
  221. if (err)
  222. goto fail3;
  223. return 0;
  224. fail3: serio_close(serio);
  225. fail2: serio_set_drvdata(serio, NULL);
  226. fail1: input_free_device(input_dev);
  227. kfree(spaceball);
  228. return err;
  229. }
  230. /*
  231. * The serio driver structure.
  232. */
  233. static struct serio_device_id spaceball_serio_ids[] = {
  234. {
  235. .type = SERIO_RS232,
  236. .proto = SERIO_SPACEBALL,
  237. .id = SERIO_ANY,
  238. .extra = SERIO_ANY,
  239. },
  240. { 0 }
  241. };
  242. MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
  243. static struct serio_driver spaceball_drv = {
  244. .driver = {
  245. .name = "spaceball",
  246. },
  247. .description = DRIVER_DESC,
  248. .id_table = spaceball_serio_ids,
  249. .interrupt = spaceball_interrupt,
  250. .connect = spaceball_connect,
  251. .disconnect = spaceball_disconnect,
  252. };
  253. /*
  254. * The functions for inserting/removing us as a module.
  255. */
  256. static int __init spaceball_init(void)
  257. {
  258. return serio_register_driver(&spaceball_drv);
  259. }
  260. static void __exit spaceball_exit(void)
  261. {
  262. serio_unregister_driver(&spaceball_drv);
  263. }
  264. module_init(spaceball_init);
  265. module_exit(spaceball_exit);