walkera0701.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Parallel port to Walkera WK-0701 TX joystick
  3. *
  4. * Copyright (c) 2008 Peter Popovec
  5. *
  6. * More about driver: <file:Documentation/input/walkera0701.txt>
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. */
  13. /* #define WK0701_DEBUG */
  14. #define RESERVE 20000
  15. #define SYNC_PULSE 1306000
  16. #define BIN0_PULSE 288000
  17. #define BIN1_PULSE 438000
  18. #define ANALOG_MIN_PULSE 318000
  19. #define ANALOG_MAX_PULSE 878000
  20. #define ANALOG_DELTA 80000
  21. #define BIN_SAMPLE ((BIN0_PULSE + BIN1_PULSE) / 2)
  22. #define NO_SYNC 25
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/parport.h>
  26. #include <linux/input.h>
  27. #include <linux/hrtimer.h>
  28. MODULE_AUTHOR("Peter Popovec <popovec@fei.tuke.sk>");
  29. MODULE_DESCRIPTION("Walkera WK-0701 TX as joystick");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int walkera0701_pp_no;
  32. module_param_named(port, walkera0701_pp_no, int, 0);
  33. MODULE_PARM_DESC(port,
  34. "Parallel port adapter for Walkera WK-0701 TX (default is 0)");
  35. /*
  36. * For now, only one device is supported, if somebody need more devices, code
  37. * can be expanded, one struct walkera_dev per device must be allocated and
  38. * set up by walkera0701_connect (release of device by walkera0701_disconnect)
  39. */
  40. struct walkera_dev {
  41. unsigned char buf[25];
  42. u64 irq_time, irq_lasttime;
  43. int counter;
  44. int ack;
  45. struct input_dev *input_dev;
  46. struct hrtimer timer;
  47. struct parport *parport;
  48. struct pardevice *pardevice;
  49. };
  50. static struct walkera_dev w_dev;
  51. static inline void walkera0701_parse_frame(struct walkera_dev *w)
  52. {
  53. int i;
  54. int val1, val2, val3, val4, val5, val6, val7, val8;
  55. int crc1, crc2;
  56. for (crc1 = crc2 = i = 0; i < 10; i++) {
  57. crc1 += w->buf[i] & 7;
  58. crc2 += (w->buf[i] & 8) >> 3;
  59. }
  60. if ((w->buf[10] & 7) != (crc1 & 7))
  61. return;
  62. if (((w->buf[10] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
  63. return;
  64. for (crc1 = crc2 = 0, i = 11; i < 23; i++) {
  65. crc1 += w->buf[i] & 7;
  66. crc2 += (w->buf[i] & 8) >> 3;
  67. }
  68. if ((w->buf[23] & 7) != (crc1 & 7))
  69. return;
  70. if (((w->buf[23] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
  71. return;
  72. val1 = ((w->buf[0] & 7) * 256 + w->buf[1] * 16 + w->buf[2]) >> 2;
  73. val1 *= ((w->buf[0] >> 2) & 2) - 1; /* sign */
  74. val2 = (w->buf[2] & 1) << 8 | (w->buf[3] << 4) | w->buf[4];
  75. val2 *= (w->buf[2] & 2) - 1; /* sign */
  76. val3 = ((w->buf[5] & 7) * 256 + w->buf[6] * 16 + w->buf[7]) >> 2;
  77. val3 *= ((w->buf[5] >> 2) & 2) - 1; /* sign */
  78. val4 = (w->buf[7] & 1) << 8 | (w->buf[8] << 4) | w->buf[9];
  79. val4 *= (w->buf[7] & 2) - 1; /* sign */
  80. val5 = ((w->buf[11] & 7) * 256 + w->buf[12] * 16 + w->buf[13]) >> 2;
  81. val5 *= ((w->buf[11] >> 2) & 2) - 1; /* sign */
  82. val6 = (w->buf[13] & 1) << 8 | (w->buf[14] << 4) | w->buf[15];
  83. val6 *= (w->buf[13] & 2) - 1; /* sign */
  84. val7 = ((w->buf[16] & 7) * 256 + w->buf[17] * 16 + w->buf[18]) >> 2;
  85. val7 *= ((w->buf[16] >> 2) & 2) - 1; /*sign */
  86. val8 = (w->buf[18] & 1) << 8 | (w->buf[19] << 4) | w->buf[20];
  87. val8 *= (w->buf[18] & 2) - 1; /*sign */
  88. #ifdef WK0701_DEBUG
  89. {
  90. int magic, magic_bit;
  91. magic = (w->buf[21] << 4) | w->buf[22];
  92. magic_bit = (w->buf[24] & 8) >> 3;
  93. printk(KERN_DEBUG
  94. "walkera0701: %4d %4d %4d %4d %4d %4d %4d %4d (magic %2x %d)\n",
  95. val1, val2, val3, val4, val5, val6, val7, val8, magic,
  96. magic_bit);
  97. }
  98. #endif
  99. input_report_abs(w->input_dev, ABS_X, val2);
  100. input_report_abs(w->input_dev, ABS_Y, val1);
  101. input_report_abs(w->input_dev, ABS_Z, val6);
  102. input_report_abs(w->input_dev, ABS_THROTTLE, val3);
  103. input_report_abs(w->input_dev, ABS_RUDDER, val4);
  104. input_report_abs(w->input_dev, ABS_MISC, val7);
  105. input_report_key(w->input_dev, BTN_GEAR_DOWN, val5 > 0);
  106. }
  107. static inline int read_ack(struct pardevice *p)
  108. {
  109. return parport_read_status(p->port) & 0x40;
  110. }
  111. /* falling edge, prepare to BIN value calculation */
  112. static void walkera0701_irq_handler(void *handler_data)
  113. {
  114. u64 pulse_time;
  115. struct walkera_dev *w = handler_data;
  116. w->irq_time = ktime_to_ns(ktime_get());
  117. pulse_time = w->irq_time - w->irq_lasttime;
  118. w->irq_lasttime = w->irq_time;
  119. /* cancel timer, if in handler or active do resync */
  120. if (unlikely(0 != hrtimer_try_to_cancel(&w->timer))) {
  121. w->counter = NO_SYNC;
  122. return;
  123. }
  124. if (w->counter < NO_SYNC) {
  125. if (w->ack) {
  126. pulse_time -= BIN1_PULSE;
  127. w->buf[w->counter] = 8;
  128. } else {
  129. pulse_time -= BIN0_PULSE;
  130. w->buf[w->counter] = 0;
  131. }
  132. if (w->counter == 24) { /* full frame */
  133. walkera0701_parse_frame(w);
  134. w->counter = NO_SYNC;
  135. if (abs(pulse_time - SYNC_PULSE) < RESERVE) /* new frame sync */
  136. w->counter = 0;
  137. } else {
  138. if ((pulse_time > (ANALOG_MIN_PULSE - RESERVE)
  139. && (pulse_time < (ANALOG_MAX_PULSE + RESERVE)))) {
  140. pulse_time -= (ANALOG_MIN_PULSE - RESERVE);
  141. pulse_time = (u32) pulse_time / ANALOG_DELTA; /* overtiping is safe, pulsetime < s32.. */
  142. w->buf[w->counter++] |= (pulse_time & 7);
  143. } else
  144. w->counter = NO_SYNC;
  145. }
  146. } else if (abs(pulse_time - SYNC_PULSE - BIN0_PULSE) <
  147. RESERVE + BIN1_PULSE - BIN0_PULSE) /* frame sync .. */
  148. w->counter = 0;
  149. hrtimer_start(&w->timer, ktime_set(0, BIN_SAMPLE), HRTIMER_MODE_REL);
  150. }
  151. static enum hrtimer_restart timer_handler(struct hrtimer
  152. *handle)
  153. {
  154. struct walkera_dev *w;
  155. w = container_of(handle, struct walkera_dev, timer);
  156. w->ack = read_ack(w->pardevice);
  157. return HRTIMER_NORESTART;
  158. }
  159. static int walkera0701_open(struct input_dev *dev)
  160. {
  161. struct walkera_dev *w = input_get_drvdata(dev);
  162. parport_enable_irq(w->parport);
  163. return 0;
  164. }
  165. static void walkera0701_close(struct input_dev *dev)
  166. {
  167. struct walkera_dev *w = input_get_drvdata(dev);
  168. parport_disable_irq(w->parport);
  169. hrtimer_cancel(&w->timer);
  170. }
  171. static int walkera0701_connect(struct walkera_dev *w, int parport)
  172. {
  173. int err = -ENODEV;
  174. w->parport = parport_find_number(parport);
  175. if (w->parport == NULL)
  176. return -ENODEV;
  177. if (w->parport->irq == -1) {
  178. printk(KERN_ERR "walkera0701: parport without interrupt\n");
  179. goto init_err;
  180. }
  181. err = -EBUSY;
  182. w->pardevice = parport_register_device(w->parport, "walkera0701",
  183. NULL, NULL, walkera0701_irq_handler,
  184. PARPORT_DEV_EXCL, w);
  185. if (!w->pardevice)
  186. goto init_err;
  187. if (parport_negotiate(w->pardevice->port, IEEE1284_MODE_COMPAT))
  188. goto init_err1;
  189. if (parport_claim(w->pardevice))
  190. goto init_err1;
  191. hrtimer_init(&w->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  192. w->timer.function = timer_handler;
  193. w->input_dev = input_allocate_device();
  194. if (!w->input_dev)
  195. goto init_err2;
  196. input_set_drvdata(w->input_dev, w);
  197. w->input_dev->name = "Walkera WK-0701 TX";
  198. w->input_dev->phys = w->parport->name;
  199. w->input_dev->id.bustype = BUS_PARPORT;
  200. /* TODO what id vendor/product/version ? */
  201. w->input_dev->id.vendor = 0x0001;
  202. w->input_dev->id.product = 0x0001;
  203. w->input_dev->id.version = 0x0100;
  204. w->input_dev->open = walkera0701_open;
  205. w->input_dev->close = walkera0701_close;
  206. w->input_dev->evbit[0] = BIT(EV_ABS) | BIT_MASK(EV_KEY);
  207. w->input_dev->keybit[BIT_WORD(BTN_GEAR_DOWN)] = BIT_MASK(BTN_GEAR_DOWN);
  208. input_set_abs_params(w->input_dev, ABS_X, -512, 512, 0, 0);
  209. input_set_abs_params(w->input_dev, ABS_Y, -512, 512, 0, 0);
  210. input_set_abs_params(w->input_dev, ABS_Z, -512, 512, 0, 0);
  211. input_set_abs_params(w->input_dev, ABS_THROTTLE, -512, 512, 0, 0);
  212. input_set_abs_params(w->input_dev, ABS_RUDDER, -512, 512, 0, 0);
  213. input_set_abs_params(w->input_dev, ABS_MISC, -512, 512, 0, 0);
  214. err = input_register_device(w->input_dev);
  215. if (err)
  216. goto init_err3;
  217. return 0;
  218. init_err3:
  219. input_free_device(w->input_dev);
  220. init_err2:
  221. parport_release(w->pardevice);
  222. init_err1:
  223. parport_unregister_device(w->pardevice);
  224. init_err:
  225. parport_put_port(w->parport);
  226. return err;
  227. }
  228. static void walkera0701_disconnect(struct walkera_dev *w)
  229. {
  230. input_unregister_device(w->input_dev);
  231. parport_release(w->pardevice);
  232. parport_unregister_device(w->pardevice);
  233. parport_put_port(w->parport);
  234. }
  235. static int __init walkera0701_init(void)
  236. {
  237. return walkera0701_connect(&w_dev, walkera0701_pp_no);
  238. }
  239. static void __exit walkera0701_exit(void)
  240. {
  241. walkera0701_disconnect(&w_dev);
  242. }
  243. module_init(walkera0701_init);
  244. module_exit(walkera0701_exit);