zhenhua.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * derived from "twidjoy.c"
  3. *
  4. * Copyright (c) 2008 Martin Kebert
  5. * Copyright (c) 2001 Arndt Schoenewald
  6. * Copyright (c) 2000-2001 Vojtech Pavlik
  7. * Copyright (c) 2000 Mark Fletcher
  8. *
  9. */
  10. /*
  11. * Driver to use 4CH RC transmitter using Zhen Hua 5-byte protocol (Walkera Lama,
  12. * EasyCopter etc.) as a joystick under Linux.
  13. *
  14. * RC transmitters using Zhen Hua 5-byte protocol are cheap four channels
  15. * transmitters for control a RC planes or RC helicopters with possibility to
  16. * connect on a serial port.
  17. * Data coming from transmitter is in this order:
  18. * 1. byte = synchronisation byte
  19. * 2. byte = X axis
  20. * 3. byte = Y axis
  21. * 4. byte = RZ axis
  22. * 5. byte = Z axis
  23. * (and this is repeated)
  24. *
  25. * For questions or feedback regarding this driver module please contact:
  26. * Martin Kebert <gkmarty@gmail.com> - but I am not a C-programmer nor kernel
  27. * coder :-(
  28. */
  29. /*
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/slab.h>
  47. #include <linux/input.h>
  48. #include <linux/serio.h>
  49. #include <linux/init.h>
  50. #define DRIVER_DESC "RC transmitter with 5-byte Zhen Hua protocol joystick driver"
  51. MODULE_DESCRIPTION(DRIVER_DESC);
  52. MODULE_LICENSE("GPL");
  53. /*
  54. * Constants.
  55. */
  56. #define ZHENHUA_MAX_LENGTH 5
  57. /*
  58. * Zhen Hua data.
  59. */
  60. struct zhenhua {
  61. struct input_dev *dev;
  62. int idx;
  63. unsigned char data[ZHENHUA_MAX_LENGTH];
  64. char phys[32];
  65. };
  66. /* bits in all incoming bytes needs to be "reversed" */
  67. static int zhenhua_bitreverse(int x)
  68. {
  69. x = ((x & 0xaa) >> 1) | ((x & 0x55) << 1);
  70. x = ((x & 0xcc) >> 2) | ((x & 0x33) << 2);
  71. x = ((x & 0xf0) >> 4) | ((x & 0x0f) << 4);
  72. return x;
  73. }
  74. /*
  75. * zhenhua_process_packet() decodes packets the driver receives from the
  76. * RC transmitter. It updates the data accordingly.
  77. */
  78. static void zhenhua_process_packet(struct zhenhua *zhenhua)
  79. {
  80. struct input_dev *dev = zhenhua->dev;
  81. unsigned char *data = zhenhua->data;
  82. input_report_abs(dev, ABS_Y, data[1]);
  83. input_report_abs(dev, ABS_X, data[2]);
  84. input_report_abs(dev, ABS_RZ, data[3]);
  85. input_report_abs(dev, ABS_Z, data[4]);
  86. input_sync(dev);
  87. }
  88. /*
  89. * zhenhua_interrupt() is called by the low level driver when characters
  90. * are ready for us. We then buffer them for further processing, or call the
  91. * packet processing routine.
  92. */
  93. static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
  94. {
  95. struct zhenhua *zhenhua = serio_get_drvdata(serio);
  96. /* All Zhen Hua packets are 5 bytes. The fact that the first byte
  97. * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
  98. * can be used to check and regain sync. */
  99. if (data == 0xef)
  100. zhenhua->idx = 0; /* this byte starts a new packet */
  101. else if (zhenhua->idx == 0)
  102. return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
  103. if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
  104. zhenhua->data[zhenhua->idx++] = zhenhua_bitreverse(data);
  105. if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
  106. zhenhua_process_packet(zhenhua);
  107. zhenhua->idx = 0;
  108. }
  109. return IRQ_HANDLED;
  110. }
  111. /*
  112. * zhenhua_disconnect() is the opposite of zhenhua_connect()
  113. */
  114. static void zhenhua_disconnect(struct serio *serio)
  115. {
  116. struct zhenhua *zhenhua = serio_get_drvdata(serio);
  117. serio_close(serio);
  118. serio_set_drvdata(serio, NULL);
  119. input_unregister_device(zhenhua->dev);
  120. kfree(zhenhua);
  121. }
  122. /*
  123. * zhenhua_connect() is the routine that is called when someone adds a
  124. * new serio device. It looks for the Twiddler, and if found, registers
  125. * it as an input device.
  126. */
  127. static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
  128. {
  129. struct zhenhua *zhenhua;
  130. struct input_dev *input_dev;
  131. int err = -ENOMEM;
  132. zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
  133. input_dev = input_allocate_device();
  134. if (!zhenhua || !input_dev)
  135. goto fail1;
  136. zhenhua->dev = input_dev;
  137. snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
  138. input_dev->name = "Zhen Hua 5-byte device";
  139. input_dev->phys = zhenhua->phys;
  140. input_dev->id.bustype = BUS_RS232;
  141. input_dev->id.vendor = SERIO_ZHENHUA;
  142. input_dev->id.product = 0x0001;
  143. input_dev->id.version = 0x0100;
  144. input_dev->dev.parent = &serio->dev;
  145. input_dev->evbit[0] = BIT(EV_ABS);
  146. input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
  147. input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
  148. input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
  149. input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
  150. serio_set_drvdata(serio, zhenhua);
  151. err = serio_open(serio, drv);
  152. if (err)
  153. goto fail2;
  154. err = input_register_device(zhenhua->dev);
  155. if (err)
  156. goto fail3;
  157. return 0;
  158. fail3: serio_close(serio);
  159. fail2: serio_set_drvdata(serio, NULL);
  160. fail1: input_free_device(input_dev);
  161. kfree(zhenhua);
  162. return err;
  163. }
  164. /*
  165. * The serio driver structure.
  166. */
  167. static struct serio_device_id zhenhua_serio_ids[] = {
  168. {
  169. .type = SERIO_RS232,
  170. .proto = SERIO_ZHENHUA,
  171. .id = SERIO_ANY,
  172. .extra = SERIO_ANY,
  173. },
  174. { 0 }
  175. };
  176. MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
  177. static struct serio_driver zhenhua_drv = {
  178. .driver = {
  179. .name = "zhenhua",
  180. },
  181. .description = DRIVER_DESC,
  182. .id_table = zhenhua_serio_ids,
  183. .interrupt = zhenhua_interrupt,
  184. .connect = zhenhua_connect,
  185. .disconnect = zhenhua_disconnect,
  186. };
  187. /*
  188. * The functions for inserting/removing us as a module.
  189. */
  190. static int __init zhenhua_init(void)
  191. {
  192. return serio_register_driver(&zhenhua_drv);
  193. }
  194. static void __exit zhenhua_exit(void)
  195. {
  196. serio_unregister_driver(&zhenhua_drv);
  197. }
  198. module_init(zhenhua_init);
  199. module_exit(zhenhua_exit);