walkera0701.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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 magic, magic_bit;
  56. int crc1, crc2;
  57. for (crc1 = crc2 = i = 0; i < 10; i++) {
  58. crc1 += w->buf[i] & 7;
  59. crc2 += (w->buf[i] & 8) >> 3;
  60. }
  61. if ((w->buf[10] & 7) != (crc1 & 7))
  62. return;
  63. if (((w->buf[10] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
  64. return;
  65. for (crc1 = crc2 = 0, i = 11; i < 23; i++) {
  66. crc1 += w->buf[i] & 7;
  67. crc2 += (w->buf[i] & 8) >> 3;
  68. }
  69. if ((w->buf[23] & 7) != (crc1 & 7))
  70. return;
  71. if (((w->buf[23] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
  72. return;
  73. val1 = ((w->buf[0] & 7) * 256 + w->buf[1] * 16 + w->buf[2]) >> 2;
  74. val1 *= ((w->buf[0] >> 2) & 2) - 1; /* sign */
  75. val2 = (w->buf[2] & 1) << 8 | (w->buf[3] << 4) | w->buf[4];
  76. val2 *= (w->buf[2] & 2) - 1; /* sign */
  77. val3 = ((w->buf[5] & 7) * 256 + w->buf[6] * 16 + w->buf[7]) >> 2;
  78. val3 *= ((w->buf[5] >> 2) & 2) - 1; /* sign */
  79. val4 = (w->buf[7] & 1) << 8 | (w->buf[8] << 4) | w->buf[9];
  80. val4 *= (w->buf[7] & 2) - 1; /* sign */
  81. val5 = ((w->buf[11] & 7) * 256 + w->buf[12] * 16 + w->buf[13]) >> 2;
  82. val5 *= ((w->buf[11] >> 2) & 2) - 1; /* sign */
  83. val6 = (w->buf[13] & 1) << 8 | (w->buf[14] << 4) | w->buf[15];
  84. val6 *= (w->buf[13] & 2) - 1; /* sign */
  85. val7 = ((w->buf[16] & 7) * 256 + w->buf[17] * 16 + w->buf[18]) >> 2;
  86. val7 *= ((w->buf[16] >> 2) & 2) - 1; /*sign */
  87. val8 = (w->buf[18] & 1) << 8 | (w->buf[19] << 4) | w->buf[20];
  88. val8 *= (w->buf[18] & 2) - 1; /*sign */
  89. magic = (w->buf[21] << 4) | w->buf[22];
  90. magic_bit = (w->buf[24] & 8) >> 3;
  91. pr_debug("%4d %4d %4d %4d %4d %4d %4d %4d (magic %2x %d)\n",
  92. val1, val2, val3, val4, val5, val6, val7, val8,
  93. magic, magic_bit);
  94. input_report_abs(w->input_dev, ABS_X, val2);
  95. input_report_abs(w->input_dev, ABS_Y, val1);
  96. input_report_abs(w->input_dev, ABS_Z, val6);
  97. input_report_abs(w->input_dev, ABS_THROTTLE, val3);
  98. input_report_abs(w->input_dev, ABS_RUDDER, val4);
  99. input_report_abs(w->input_dev, ABS_MISC, val7);
  100. input_report_key(w->input_dev, BTN_GEAR_DOWN, val5 > 0);
  101. }
  102. static inline int read_ack(struct pardevice *p)
  103. {
  104. return parport_read_status(p->port) & 0x40;
  105. }
  106. /* falling edge, prepare to BIN value calculation */
  107. static void walkera0701_irq_handler(void *handler_data)
  108. {
  109. u64 pulse_time;
  110. struct walkera_dev *w = handler_data;
  111. w->irq_time = ktime_to_ns(ktime_get());
  112. pulse_time = w->irq_time - w->irq_lasttime;
  113. w->irq_lasttime = w->irq_time;
  114. /* cancel timer, if in handler or active do resync */
  115. if (unlikely(0 != hrtimer_try_to_cancel(&w->timer))) {
  116. w->counter = NO_SYNC;
  117. return;
  118. }
  119. if (w->counter < NO_SYNC) {
  120. if (w->ack) {
  121. pulse_time -= BIN1_PULSE;
  122. w->buf[w->counter] = 8;
  123. } else {
  124. pulse_time -= BIN0_PULSE;
  125. w->buf[w->counter] = 0;
  126. }
  127. if (w->counter == 24) { /* full frame */
  128. walkera0701_parse_frame(w);
  129. w->counter = NO_SYNC;
  130. if (abs(pulse_time - SYNC_PULSE) < RESERVE) /* new frame sync */
  131. w->counter = 0;
  132. } else {
  133. if ((pulse_time > (ANALOG_MIN_PULSE - RESERVE)
  134. && (pulse_time < (ANALOG_MAX_PULSE + RESERVE)))) {
  135. pulse_time -= (ANALOG_MIN_PULSE - RESERVE);
  136. pulse_time = (u32) pulse_time / ANALOG_DELTA; /* overtiping is safe, pulsetime < s32.. */
  137. w->buf[w->counter++] |= (pulse_time & 7);
  138. } else
  139. w->counter = NO_SYNC;
  140. }
  141. } else if (abs(pulse_time - SYNC_PULSE - BIN0_PULSE) <
  142. RESERVE + BIN1_PULSE - BIN0_PULSE) /* frame sync .. */
  143. w->counter = 0;
  144. hrtimer_start(&w->timer, ktime_set(0, BIN_SAMPLE), HRTIMER_MODE_REL);
  145. }
  146. static enum hrtimer_restart timer_handler(struct hrtimer
  147. *handle)
  148. {
  149. struct walkera_dev *w;
  150. w = container_of(handle, struct walkera_dev, timer);
  151. w->ack = read_ack(w->pardevice);
  152. return HRTIMER_NORESTART;
  153. }
  154. static int walkera0701_open(struct input_dev *dev)
  155. {
  156. struct walkera_dev *w = input_get_drvdata(dev);
  157. if (parport_claim(w->pardevice))
  158. return -EBUSY;
  159. parport_enable_irq(w->parport);
  160. return 0;
  161. }
  162. static void walkera0701_close(struct input_dev *dev)
  163. {
  164. struct walkera_dev *w = input_get_drvdata(dev);
  165. parport_disable_irq(w->parport);
  166. hrtimer_cancel(&w->timer);
  167. parport_release(w->pardevice);
  168. }
  169. static void walkera0701_attach(struct parport *pp)
  170. {
  171. struct pardev_cb walkera0701_parport_cb;
  172. struct walkera_dev *w = &w_dev;
  173. if (pp->number != walkera0701_pp_no) {
  174. pr_debug("Not using parport%d.\n", pp->number);
  175. return;
  176. }
  177. if (pp->irq == -1) {
  178. pr_err("parport %d does not have interrupt assigned\n",
  179. pp->number);
  180. return;
  181. }
  182. w->parport = pp;
  183. memset(&walkera0701_parport_cb, 0, sizeof(walkera0701_parport_cb));
  184. walkera0701_parport_cb.flags = PARPORT_FLAG_EXCL;
  185. walkera0701_parport_cb.irq_func = walkera0701_irq_handler;
  186. walkera0701_parport_cb.private = w;
  187. w->pardevice = parport_register_dev_model(pp, "walkera0701",
  188. &walkera0701_parport_cb, 0);
  189. if (!w->pardevice) {
  190. pr_err("failed to register parport device\n");
  191. return;
  192. }
  193. if (parport_negotiate(w->pardevice->port, IEEE1284_MODE_COMPAT)) {
  194. pr_err("failed to negotiate parport mode\n");
  195. goto err_unregister_device;
  196. }
  197. hrtimer_init(&w->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  198. w->timer.function = timer_handler;
  199. w->input_dev = input_allocate_device();
  200. if (!w->input_dev) {
  201. pr_err("failed to allocate input device\n");
  202. goto err_unregister_device;
  203. }
  204. input_set_drvdata(w->input_dev, w);
  205. w->input_dev->name = "Walkera WK-0701 TX";
  206. w->input_dev->phys = w->parport->name;
  207. w->input_dev->id.bustype = BUS_PARPORT;
  208. /* TODO what id vendor/product/version ? */
  209. w->input_dev->id.vendor = 0x0001;
  210. w->input_dev->id.product = 0x0001;
  211. w->input_dev->id.version = 0x0100;
  212. w->input_dev->dev.parent = w->parport->dev;
  213. w->input_dev->open = walkera0701_open;
  214. w->input_dev->close = walkera0701_close;
  215. w->input_dev->evbit[0] = BIT(EV_ABS) | BIT_MASK(EV_KEY);
  216. w->input_dev->keybit[BIT_WORD(BTN_GEAR_DOWN)] = BIT_MASK(BTN_GEAR_DOWN);
  217. input_set_abs_params(w->input_dev, ABS_X, -512, 512, 0, 0);
  218. input_set_abs_params(w->input_dev, ABS_Y, -512, 512, 0, 0);
  219. input_set_abs_params(w->input_dev, ABS_Z, -512, 512, 0, 0);
  220. input_set_abs_params(w->input_dev, ABS_THROTTLE, -512, 512, 0, 0);
  221. input_set_abs_params(w->input_dev, ABS_RUDDER, -512, 512, 0, 0);
  222. input_set_abs_params(w->input_dev, ABS_MISC, -512, 512, 0, 0);
  223. if (input_register_device(w->input_dev)) {
  224. pr_err("failed to register input device\n");
  225. goto err_free_input_dev;
  226. }
  227. return;
  228. err_free_input_dev:
  229. input_free_device(w->input_dev);
  230. err_unregister_device:
  231. parport_unregister_device(w->pardevice);
  232. }
  233. static void walkera0701_detach(struct parport *port)
  234. {
  235. struct walkera_dev *w = &w_dev;
  236. if (!w->pardevice || w->parport->number != port->number)
  237. return;
  238. input_unregister_device(w->input_dev);
  239. parport_unregister_device(w->pardevice);
  240. w->parport = NULL;
  241. }
  242. static struct parport_driver walkera0701_parport_driver = {
  243. .name = "walkera0701",
  244. .match_port = walkera0701_attach,
  245. .detach = walkera0701_detach,
  246. .devmodel = true,
  247. };
  248. static int __init walkera0701_init(void)
  249. {
  250. return parport_register_driver(&walkera0701_parport_driver);
  251. }
  252. static void __exit walkera0701_exit(void)
  253. {
  254. parport_unregister_driver(&walkera0701_parport_driver);
  255. }
  256. module_init(walkera0701_init);
  257. module_exit(walkera0701_exit);