magellan.c 5.6 KB

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