spaceorb.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 1999-2001 Vojtech Pavlik
  3. *
  4. * Based on the work of:
  5. * David Thompson
  6. */
  7. /*
  8. * SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Should you need to contact me, the author, you can do so either by
  26. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  27. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/input.h>
  34. #include <linux/serio.h>
  35. #define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"
  36. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  37. MODULE_DESCRIPTION(DRIVER_DESC);
  38. MODULE_LICENSE("GPL");
  39. /*
  40. * Constants.
  41. */
  42. #define SPACEORB_MAX_LENGTH 64
  43. static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
  44. static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
  45. /*
  46. * Per-Orb data.
  47. */
  48. struct spaceorb {
  49. struct input_dev *dev;
  50. int idx;
  51. unsigned char data[SPACEORB_MAX_LENGTH];
  52. char phys[32];
  53. };
  54. static unsigned char spaceorb_xor[] = "SpaceWare";
  55. static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
  56. "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
  57. /*
  58. * spaceorb_process_packet() decodes packets the driver receives from the
  59. * SpaceOrb.
  60. */
  61. static void spaceorb_process_packet(struct spaceorb *spaceorb)
  62. {
  63. struct input_dev *dev = spaceorb->dev;
  64. unsigned char *data = spaceorb->data;
  65. unsigned char c = 0;
  66. int axes[6];
  67. int i;
  68. if (spaceorb->idx < 2) return;
  69. for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
  70. if (c) return;
  71. switch (data[0]) {
  72. case 'R': /* Reset packet */
  73. spaceorb->data[spaceorb->idx - 1] = 0;
  74. for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
  75. printk(KERN_INFO "input: %s [%s] is %s\n",
  76. dev->name, spaceorb->data + i, spaceorb->phys);
  77. break;
  78. case 'D': /* Ball + button data */
  79. if (spaceorb->idx != 12) return;
  80. for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
  81. axes[0] = ( data[2] << 3) | (data[ 3] >> 4);
  82. axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
  83. axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
  84. axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
  85. axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
  86. axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
  87. for (i = 0; i < 6; i++)
  88. input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
  89. for (i = 0; i < 6; i++)
  90. input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
  91. break;
  92. case 'K': /* Button data */
  93. if (spaceorb->idx != 5) return;
  94. for (i = 0; i < 6; i++)
  95. input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
  96. break;
  97. case 'E': /* Error packet */
  98. if (spaceorb->idx != 4) return;
  99. printk(KERN_ERR "spaceorb: Device error. [ ");
  100. for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
  101. printk("]\n");
  102. break;
  103. }
  104. input_sync(dev);
  105. }
  106. static irqreturn_t spaceorb_interrupt(struct serio *serio,
  107. unsigned char data, unsigned int flags)
  108. {
  109. struct spaceorb* spaceorb = serio_get_drvdata(serio);
  110. if (~data & 0x80) {
  111. if (spaceorb->idx) spaceorb_process_packet(spaceorb);
  112. spaceorb->idx = 0;
  113. }
  114. if (spaceorb->idx < SPACEORB_MAX_LENGTH)
  115. spaceorb->data[spaceorb->idx++] = data & 0x7f;
  116. return IRQ_HANDLED;
  117. }
  118. /*
  119. * spaceorb_disconnect() is the opposite of spaceorb_connect()
  120. */
  121. static void spaceorb_disconnect(struct serio *serio)
  122. {
  123. struct spaceorb* spaceorb = serio_get_drvdata(serio);
  124. serio_close(serio);
  125. serio_set_drvdata(serio, NULL);
  126. input_unregister_device(spaceorb->dev);
  127. kfree(spaceorb);
  128. }
  129. /*
  130. * spaceorb_connect() is the routine that is called when someone adds a
  131. * new serio device that supports SpaceOrb/Avenger protocol and registers
  132. * it as an input device.
  133. */
  134. static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)
  135. {
  136. struct spaceorb *spaceorb;
  137. struct input_dev *input_dev;
  138. int err = -ENOMEM;
  139. int i;
  140. spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL);
  141. input_dev = input_allocate_device();
  142. if (!spaceorb || !input_dev)
  143. goto fail1;
  144. spaceorb->dev = input_dev;
  145. snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys);
  146. input_dev->name = "SpaceTec SpaceOrb 360 / Avenger";
  147. input_dev->phys = spaceorb->phys;
  148. input_dev->id.bustype = BUS_RS232;
  149. input_dev->id.vendor = SERIO_SPACEORB;
  150. input_dev->id.product = 0x0001;
  151. input_dev->id.version = 0x0100;
  152. input_dev->dev.parent = &serio->dev;
  153. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  154. for (i = 0; i < 6; i++)
  155. set_bit(spaceorb_buttons[i], input_dev->keybit);
  156. for (i = 0; i < 6; i++)
  157. input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0);
  158. serio_set_drvdata(serio, spaceorb);
  159. err = serio_open(serio, drv);
  160. if (err)
  161. goto fail2;
  162. err = input_register_device(spaceorb->dev);
  163. if (err)
  164. goto fail3;
  165. return 0;
  166. fail3: serio_close(serio);
  167. fail2: serio_set_drvdata(serio, NULL);
  168. fail1: input_free_device(input_dev);
  169. kfree(spaceorb);
  170. return err;
  171. }
  172. /*
  173. * The serio driver structure.
  174. */
  175. static struct serio_device_id spaceorb_serio_ids[] = {
  176. {
  177. .type = SERIO_RS232,
  178. .proto = SERIO_SPACEORB,
  179. .id = SERIO_ANY,
  180. .extra = SERIO_ANY,
  181. },
  182. { 0 }
  183. };
  184. MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);
  185. static struct serio_driver spaceorb_drv = {
  186. .driver = {
  187. .name = "spaceorb",
  188. },
  189. .description = DRIVER_DESC,
  190. .id_table = spaceorb_serio_ids,
  191. .interrupt = spaceorb_interrupt,
  192. .connect = spaceorb_connect,
  193. .disconnect = spaceorb_disconnect,
  194. };
  195. /*
  196. * The functions for inserting/removing us as a module.
  197. */
  198. static int __init spaceorb_init(void)
  199. {
  200. return serio_register_driver(&spaceorb_drv);
  201. }
  202. static void __exit spaceorb_exit(void)
  203. {
  204. serio_unregister_driver(&spaceorb_drv);
  205. }
  206. module_init(spaceorb_init);
  207. module_exit(spaceorb_exit);