spaceorb.c 6.6 KB

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