spaceorb.c 6.1 KB

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