magellan.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * $Id: magellan.c,v 1.16 2002/01/22 20:28:39 vojtech Exp $
  3. *
  4. * Copyright (c) 1999-2001 Vojtech Pavlik
  5. */
  6. /*
  7. * Magellan and Space Mouse 6dof controller driver for Linux
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Should you need to contact me, the author, you can do so either by
  25. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  26. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/input.h>
  32. #include <linux/serio.h>
  33. #include <linux/init.h>
  34. #define DRIVER_DESC "Magellan and SpaceMouse 6dof controller driver"
  35. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  36. MODULE_DESCRIPTION(DRIVER_DESC);
  37. MODULE_LICENSE("GPL");
  38. /*
  39. * Definitions & global arrays.
  40. */
  41. #define MAGELLAN_MAX_LENGTH 32
  42. static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
  43. static int magellan_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
  44. static char *magellan_name = "LogiCad3D Magellan / SpaceMouse";
  45. /*
  46. * Per-Magellan data.
  47. */
  48. struct magellan {
  49. struct input_dev dev;
  50. int idx;
  51. unsigned char data[MAGELLAN_MAX_LENGTH];
  52. char phys[32];
  53. };
  54. /*
  55. * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan
  56. * have correct upper nibbles for the lower ones, if not, the packet will
  57. * be thrown away. It also strips these upper halves to simplify further
  58. * processing.
  59. */
  60. static int magellan_crunch_nibbles(unsigned char *data, int count)
  61. {
  62. static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";
  63. do {
  64. if (data[count] == nibbles[data[count] & 0xf])
  65. data[count] = data[count] & 0xf;
  66. else
  67. return -1;
  68. } while (--count);
  69. return 0;
  70. }
  71. static void magellan_process_packet(struct magellan* magellan, struct pt_regs *regs)
  72. {
  73. struct input_dev *dev = &magellan->dev;
  74. unsigned char *data = magellan->data;
  75. int i, t;
  76. if (!magellan->idx) return;
  77. input_regs(dev, regs);
  78. switch (magellan->data[0]) {
  79. case 'd': /* Axis data */
  80. if (magellan->idx != 25) return;
  81. if (magellan_crunch_nibbles(data, 24)) return;
  82. for (i = 0; i < 6; i++)
  83. input_report_abs(dev, magellan_axes[i],
  84. (data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 |
  85. data[(i << 2) + 3] << 4 | data[(i << 2) + 4]) - 32768);
  86. break;
  87. case 'k': /* Button data */
  88. if (magellan->idx != 4) return;
  89. if (magellan_crunch_nibbles(data, 3)) return;
  90. t = (data[1] << 1) | (data[2] << 5) | data[3];
  91. for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1);
  92. break;
  93. }
  94. input_sync(dev);
  95. }
  96. static irqreturn_t magellan_interrupt(struct serio *serio,
  97. unsigned char data, unsigned int flags, struct pt_regs *regs)
  98. {
  99. struct magellan* magellan = serio_get_drvdata(serio);
  100. if (data == '\r') {
  101. magellan_process_packet(magellan, regs);
  102. magellan->idx = 0;
  103. } else {
  104. if (magellan->idx < MAGELLAN_MAX_LENGTH)
  105. magellan->data[magellan->idx++] = data;
  106. }
  107. return IRQ_HANDLED;
  108. }
  109. /*
  110. * magellan_disconnect() is the opposite of magellan_connect()
  111. */
  112. static void magellan_disconnect(struct serio *serio)
  113. {
  114. struct magellan* magellan = serio_get_drvdata(serio);
  115. input_unregister_device(&magellan->dev);
  116. serio_close(serio);
  117. serio_set_drvdata(serio, NULL);
  118. kfree(magellan);
  119. }
  120. /*
  121. * magellan_connect() is the routine that is called when someone adds a
  122. * new serio device that supports Magellan protocol and registers it as
  123. * an input device.
  124. */
  125. static int magellan_connect(struct serio *serio, struct serio_driver *drv)
  126. {
  127. struct magellan *magellan;
  128. int i, t;
  129. int err;
  130. if (!(magellan = kmalloc(sizeof(struct magellan), GFP_KERNEL)))
  131. return -ENOMEM;
  132. memset(magellan, 0, sizeof(struct magellan));
  133. magellan->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  134. for (i = 0; i < 9; i++)
  135. set_bit(magellan_buttons[i], magellan->dev.keybit);
  136. for (i = 0; i < 6; i++) {
  137. t = magellan_axes[i];
  138. set_bit(t, magellan->dev.absbit);
  139. magellan->dev.absmin[t] = -360;
  140. magellan->dev.absmax[t] = 360;
  141. }
  142. sprintf(magellan->phys, "%s/input0", serio->phys);
  143. init_input_dev(&magellan->dev);
  144. magellan->dev.private = magellan;
  145. magellan->dev.name = magellan_name;
  146. magellan->dev.phys = magellan->phys;
  147. magellan->dev.id.bustype = BUS_RS232;
  148. magellan->dev.id.vendor = SERIO_MAGELLAN;
  149. magellan->dev.id.product = 0x0001;
  150. magellan->dev.id.version = 0x0100;
  151. magellan->dev.dev = &serio->dev;
  152. serio_set_drvdata(serio, magellan);
  153. err = serio_open(serio, drv);
  154. if (err) {
  155. serio_set_drvdata(serio, NULL);
  156. kfree(magellan);
  157. return err;
  158. }
  159. input_register_device(&magellan->dev);
  160. printk(KERN_INFO "input: %s on %s\n", magellan_name, serio->phys);
  161. return 0;
  162. }
  163. /*
  164. * The serio driver structure.
  165. */
  166. static struct serio_device_id magellan_serio_ids[] = {
  167. {
  168. .type = SERIO_RS232,
  169. .proto = SERIO_MAGELLAN,
  170. .id = SERIO_ANY,
  171. .extra = SERIO_ANY,
  172. },
  173. { 0 }
  174. };
  175. MODULE_DEVICE_TABLE(serio, magellan_serio_ids);
  176. static struct serio_driver magellan_drv = {
  177. .driver = {
  178. .name = "magellan",
  179. },
  180. .description = DRIVER_DESC,
  181. .id_table = magellan_serio_ids,
  182. .interrupt = magellan_interrupt,
  183. .connect = magellan_connect,
  184. .disconnect = magellan_disconnect,
  185. };
  186. /*
  187. * The functions for inserting/removing us as a module.
  188. */
  189. static int __init magellan_init(void)
  190. {
  191. serio_register_driver(&magellan_drv);
  192. return 0;
  193. }
  194. static void __exit magellan_exit(void)
  195. {
  196. serio_unregister_driver(&magellan_drv);
  197. }
  198. module_init(magellan_init);
  199. module_exit(magellan_exit);