spaceorb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. static char *spaceorb_name = "SpaceTec SpaceOrb 360 / Avenger";
  48. /*
  49. * Per-Orb data.
  50. */
  51. struct spaceorb {
  52. struct input_dev dev;
  53. struct serio *serio;
  54. int idx;
  55. unsigned char data[SPACEORB_MAX_LENGTH];
  56. char phys[32];
  57. };
  58. static unsigned char spaceorb_xor[] = "SpaceWare";
  59. static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
  60. "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
  61. /*
  62. * spaceorb_process_packet() decodes packets the driver receives from the
  63. * SpaceOrb.
  64. */
  65. static void spaceorb_process_packet(struct spaceorb *spaceorb, struct pt_regs *regs)
  66. {
  67. struct input_dev *dev = &spaceorb->dev;
  68. unsigned char *data = spaceorb->data;
  69. unsigned char c = 0;
  70. int axes[6];
  71. int i;
  72. if (spaceorb->idx < 2) return;
  73. for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
  74. if (c) return;
  75. input_regs(dev, regs);
  76. switch (data[0]) {
  77. case 'R': /* Reset packet */
  78. spaceorb->data[spaceorb->idx - 1] = 0;
  79. for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
  80. printk(KERN_INFO "input: %s [%s] on %s\n",
  81. spaceorb_name, spaceorb->data + i, spaceorb->serio->phys);
  82. break;
  83. case 'D': /* Ball + button data */
  84. if (spaceorb->idx != 12) return;
  85. for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
  86. axes[0] = ( data[2] << 3) | (data[ 3] >> 4);
  87. axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
  88. axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
  89. axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
  90. axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
  91. axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
  92. for (i = 0; i < 6; i++)
  93. input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
  94. for (i = 0; i < 6; i++)
  95. input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
  96. break;
  97. case 'K': /* Button data */
  98. if (spaceorb->idx != 5) return;
  99. for (i = 0; i < 6; i++)
  100. input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
  101. break;
  102. case 'E': /* Error packet */
  103. if (spaceorb->idx != 4) return;
  104. printk(KERN_ERR "joy-spaceorb: Device error. [ ");
  105. for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
  106. printk("]\n");
  107. break;
  108. }
  109. input_sync(dev);
  110. }
  111. static irqreturn_t spaceorb_interrupt(struct serio *serio,
  112. unsigned char data, unsigned int flags, struct pt_regs *regs)
  113. {
  114. struct spaceorb* spaceorb = serio_get_drvdata(serio);
  115. if (~data & 0x80) {
  116. if (spaceorb->idx) spaceorb_process_packet(spaceorb, regs);
  117. spaceorb->idx = 0;
  118. }
  119. if (spaceorb->idx < SPACEORB_MAX_LENGTH)
  120. spaceorb->data[spaceorb->idx++] = data & 0x7f;
  121. return IRQ_HANDLED;
  122. }
  123. /*
  124. * spaceorb_disconnect() is the opposite of spaceorb_connect()
  125. */
  126. static void spaceorb_disconnect(struct serio *serio)
  127. {
  128. struct spaceorb* spaceorb = serio_get_drvdata(serio);
  129. input_unregister_device(&spaceorb->dev);
  130. serio_close(serio);
  131. serio_set_drvdata(serio, NULL);
  132. kfree(spaceorb);
  133. }
  134. /*
  135. * spaceorb_connect() is the routine that is called when someone adds a
  136. * new serio device that supports SpaceOrb/Avenger protocol and registers
  137. * it as an input device.
  138. */
  139. static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)
  140. {
  141. struct spaceorb *spaceorb;
  142. int i, t;
  143. int err;
  144. if (!(spaceorb = kmalloc(sizeof(struct spaceorb), GFP_KERNEL)))
  145. return -ENOMEM;
  146. memset(spaceorb, 0, sizeof(struct spaceorb));
  147. spaceorb->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  148. for (i = 0; i < 6; i++)
  149. set_bit(spaceorb_buttons[i], spaceorb->dev.keybit);
  150. for (i = 0; i < 6; i++) {
  151. t = spaceorb_axes[i];
  152. set_bit(t, spaceorb->dev.absbit);
  153. spaceorb->dev.absmin[t] = -508;
  154. spaceorb->dev.absmax[t] = 508;
  155. }
  156. spaceorb->serio = serio;
  157. spaceorb->dev.private = spaceorb;
  158. sprintf(spaceorb->phys, "%s/input0", serio->phys);
  159. init_input_dev(&spaceorb->dev);
  160. spaceorb->dev.name = spaceorb_name;
  161. spaceorb->dev.phys = spaceorb->phys;
  162. spaceorb->dev.id.bustype = BUS_RS232;
  163. spaceorb->dev.id.vendor = SERIO_SPACEORB;
  164. spaceorb->dev.id.product = 0x0001;
  165. spaceorb->dev.id.version = 0x0100;
  166. spaceorb->dev.dev = &serio->dev;
  167. serio_set_drvdata(serio, spaceorb);
  168. err = serio_open(serio, drv);
  169. if (err) {
  170. serio_set_drvdata(serio, NULL);
  171. kfree(spaceorb);
  172. return err;
  173. }
  174. input_register_device(&spaceorb->dev);
  175. return 0;
  176. }
  177. /*
  178. * The serio driver structure.
  179. */
  180. static struct serio_device_id spaceorb_serio_ids[] = {
  181. {
  182. .type = SERIO_RS232,
  183. .proto = SERIO_SPACEORB,
  184. .id = SERIO_ANY,
  185. .extra = SERIO_ANY,
  186. },
  187. { 0 }
  188. };
  189. MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);
  190. static struct serio_driver spaceorb_drv = {
  191. .driver = {
  192. .name = "spaceorb",
  193. },
  194. .description = DRIVER_DESC,
  195. .id_table = spaceorb_serio_ids,
  196. .interrupt = spaceorb_interrupt,
  197. .connect = spaceorb_connect,
  198. .disconnect = spaceorb_disconnect,
  199. };
  200. /*
  201. * The functions for inserting/removing us as a module.
  202. */
  203. static int __init spaceorb_init(void)
  204. {
  205. serio_register_driver(&spaceorb_drv);
  206. return 0;
  207. }
  208. static void __exit spaceorb_exit(void)
  209. {
  210. serio_unregister_driver(&spaceorb_drv);
  211. }
  212. module_init(spaceorb_init);
  213. module_exit(spaceorb_exit);