spaceorb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/input.h>
  33. #include <linux/serio.h>
  34. #define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"
  35. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  36. MODULE_DESCRIPTION(DRIVER_DESC);
  37. MODULE_LICENSE("GPL");
  38. /*
  39. * Constants.
  40. */
  41. #define SPACEORB_MAX_LENGTH 64
  42. static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
  43. static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
  44. /*
  45. * Per-Orb data.
  46. */
  47. struct spaceorb {
  48. struct input_dev *dev;
  49. int idx;
  50. unsigned char data[SPACEORB_MAX_LENGTH];
  51. char phys[32];
  52. };
  53. static unsigned char spaceorb_xor[] = "SpaceWare";
  54. static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
  55. "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
  56. /*
  57. * spaceorb_process_packet() decodes packets the driver receives from the
  58. * SpaceOrb.
  59. */
  60. static void spaceorb_process_packet(struct spaceorb *spaceorb)
  61. {
  62. struct input_dev *dev = spaceorb->dev;
  63. unsigned char *data = spaceorb->data;
  64. unsigned char c = 0;
  65. int axes[6];
  66. int i;
  67. if (spaceorb->idx < 2) return;
  68. for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
  69. if (c) return;
  70. switch (data[0]) {
  71. case 'R': /* Reset packet */
  72. spaceorb->data[spaceorb->idx - 1] = 0;
  73. for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
  74. printk(KERN_INFO "input: %s [%s] is %s\n",
  75. dev->name, spaceorb->data + i, spaceorb->phys);
  76. break;
  77. case 'D': /* Ball + button data */
  78. if (spaceorb->idx != 12) return;
  79. for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
  80. axes[0] = ( data[2] << 3) | (data[ 3] >> 4);
  81. axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
  82. axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
  83. axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
  84. axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
  85. axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
  86. for (i = 0; i < 6; i++)
  87. input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
  88. for (i = 0; i < 6; i++)
  89. input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
  90. break;
  91. case 'K': /* Button data */
  92. if (spaceorb->idx != 5) return;
  93. for (i = 0; i < 6; i++)
  94. input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
  95. break;
  96. case 'E': /* Error packet */
  97. if (spaceorb->idx != 4) return;
  98. printk(KERN_ERR "spaceorb: Device error. [ ");
  99. for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
  100. printk("]\n");
  101. break;
  102. }
  103. input_sync(dev);
  104. }
  105. static irqreturn_t spaceorb_interrupt(struct serio *serio,
  106. unsigned char data, unsigned int flags)
  107. {
  108. struct spaceorb* spaceorb = serio_get_drvdata(serio);
  109. if (~data & 0x80) {
  110. if (spaceorb->idx) spaceorb_process_packet(spaceorb);
  111. spaceorb->idx = 0;
  112. }
  113. if (spaceorb->idx < SPACEORB_MAX_LENGTH)
  114. spaceorb->data[spaceorb->idx++] = data & 0x7f;
  115. return IRQ_HANDLED;
  116. }
  117. /*
  118. * spaceorb_disconnect() is the opposite of spaceorb_connect()
  119. */
  120. static void spaceorb_disconnect(struct serio *serio)
  121. {
  122. struct spaceorb* spaceorb = serio_get_drvdata(serio);
  123. serio_close(serio);
  124. serio_set_drvdata(serio, NULL);
  125. input_unregister_device(spaceorb->dev);
  126. kfree(spaceorb);
  127. }
  128. /*
  129. * spaceorb_connect() is the routine that is called when someone adds a
  130. * new serio device that supports SpaceOrb/Avenger protocol and registers
  131. * it as an input device.
  132. */
  133. static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)
  134. {
  135. struct spaceorb *spaceorb;
  136. struct input_dev *input_dev;
  137. int err = -ENOMEM;
  138. int i;
  139. spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL);
  140. input_dev = input_allocate_device();
  141. if (!spaceorb || !input_dev)
  142. goto fail1;
  143. spaceorb->dev = input_dev;
  144. snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys);
  145. input_dev->name = "SpaceTec SpaceOrb 360 / Avenger";
  146. input_dev->phys = spaceorb->phys;
  147. input_dev->id.bustype = BUS_RS232;
  148. input_dev->id.vendor = SERIO_SPACEORB;
  149. input_dev->id.product = 0x0001;
  150. input_dev->id.version = 0x0100;
  151. input_dev->dev.parent = &serio->dev;
  152. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  153. for (i = 0; i < 6; i++)
  154. set_bit(spaceorb_buttons[i], input_dev->keybit);
  155. for (i = 0; i < 6; i++)
  156. input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0);
  157. serio_set_drvdata(serio, spaceorb);
  158. err = serio_open(serio, drv);
  159. if (err)
  160. goto fail2;
  161. err = input_register_device(spaceorb->dev);
  162. if (err)
  163. goto fail3;
  164. return 0;
  165. fail3: serio_close(serio);
  166. fail2: serio_set_drvdata(serio, NULL);
  167. fail1: input_free_device(input_dev);
  168. kfree(spaceorb);
  169. return err;
  170. }
  171. /*
  172. * The serio driver structure.
  173. */
  174. static struct serio_device_id spaceorb_serio_ids[] = {
  175. {
  176. .type = SERIO_RS232,
  177. .proto = SERIO_SPACEORB,
  178. .id = SERIO_ANY,
  179. .extra = SERIO_ANY,
  180. },
  181. { 0 }
  182. };
  183. MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);
  184. static struct serio_driver spaceorb_drv = {
  185. .driver = {
  186. .name = "spaceorb",
  187. },
  188. .description = DRIVER_DESC,
  189. .id_table = spaceorb_serio_ids,
  190. .interrupt = spaceorb_interrupt,
  191. .connect = spaceorb_connect,
  192. .disconnect = spaceorb_disconnect,
  193. };
  194. module_serio_driver(spaceorb_drv);