zhenhua.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #define DRIVER_DESC "RC transmitter with 5-byte Zhen Hua protocol joystick driver"
  50. MODULE_DESCRIPTION(DRIVER_DESC);
  51. MODULE_LICENSE("GPL");
  52. /*
  53. * Constants.
  54. */
  55. #define ZHENHUA_MAX_LENGTH 5
  56. /*
  57. * Zhen Hua data.
  58. */
  59. struct zhenhua {
  60. struct input_dev *dev;
  61. int idx;
  62. unsigned char data[ZHENHUA_MAX_LENGTH];
  63. char phys[32];
  64. };
  65. /* bits in all incoming bytes needs to be "reversed" */
  66. static int zhenhua_bitreverse(int x)
  67. {
  68. x = ((x & 0xaa) >> 1) | ((x & 0x55) << 1);
  69. x = ((x & 0xcc) >> 2) | ((x & 0x33) << 2);
  70. x = ((x & 0xf0) >> 4) | ((x & 0x0f) << 4);
  71. return x;
  72. }
  73. /*
  74. * zhenhua_process_packet() decodes packets the driver receives from the
  75. * RC transmitter. It updates the data accordingly.
  76. */
  77. static void zhenhua_process_packet(struct zhenhua *zhenhua)
  78. {
  79. struct input_dev *dev = zhenhua->dev;
  80. unsigned char *data = zhenhua->data;
  81. input_report_abs(dev, ABS_Y, data[1]);
  82. input_report_abs(dev, ABS_X, data[2]);
  83. input_report_abs(dev, ABS_RZ, data[3]);
  84. input_report_abs(dev, ABS_Z, data[4]);
  85. input_sync(dev);
  86. }
  87. /*
  88. * zhenhua_interrupt() is called by the low level driver when characters
  89. * are ready for us. We then buffer them for further processing, or call the
  90. * packet processing routine.
  91. */
  92. static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
  93. {
  94. struct zhenhua *zhenhua = serio_get_drvdata(serio);
  95. /* All Zhen Hua packets are 5 bytes. The fact that the first byte
  96. * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
  97. * can be used to check and regain sync. */
  98. if (data == 0xef)
  99. zhenhua->idx = 0; /* this byte starts a new packet */
  100. else if (zhenhua->idx == 0)
  101. return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
  102. if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
  103. zhenhua->data[zhenhua->idx++] = zhenhua_bitreverse(data);
  104. if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
  105. zhenhua_process_packet(zhenhua);
  106. zhenhua->idx = 0;
  107. }
  108. return IRQ_HANDLED;
  109. }
  110. /*
  111. * zhenhua_disconnect() is the opposite of zhenhua_connect()
  112. */
  113. static void zhenhua_disconnect(struct serio *serio)
  114. {
  115. struct zhenhua *zhenhua = serio_get_drvdata(serio);
  116. serio_close(serio);
  117. serio_set_drvdata(serio, NULL);
  118. input_unregister_device(zhenhua->dev);
  119. kfree(zhenhua);
  120. }
  121. /*
  122. * zhenhua_connect() is the routine that is called when someone adds a
  123. * new serio device. It looks for the Twiddler, and if found, registers
  124. * it as an input device.
  125. */
  126. static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
  127. {
  128. struct zhenhua *zhenhua;
  129. struct input_dev *input_dev;
  130. int err = -ENOMEM;
  131. zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
  132. input_dev = input_allocate_device();
  133. if (!zhenhua || !input_dev)
  134. goto fail1;
  135. zhenhua->dev = input_dev;
  136. snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
  137. input_dev->name = "Zhen Hua 5-byte device";
  138. input_dev->phys = zhenhua->phys;
  139. input_dev->id.bustype = BUS_RS232;
  140. input_dev->id.vendor = SERIO_ZHENHUA;
  141. input_dev->id.product = 0x0001;
  142. input_dev->id.version = 0x0100;
  143. input_dev->dev.parent = &serio->dev;
  144. input_dev->evbit[0] = BIT(EV_ABS);
  145. input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
  146. input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
  147. input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
  148. input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
  149. serio_set_drvdata(serio, zhenhua);
  150. err = serio_open(serio, drv);
  151. if (err)
  152. goto fail2;
  153. err = input_register_device(zhenhua->dev);
  154. if (err)
  155. goto fail3;
  156. return 0;
  157. fail3: serio_close(serio);
  158. fail2: serio_set_drvdata(serio, NULL);
  159. fail1: input_free_device(input_dev);
  160. kfree(zhenhua);
  161. return err;
  162. }
  163. /*
  164. * The serio driver structure.
  165. */
  166. static struct serio_device_id zhenhua_serio_ids[] = {
  167. {
  168. .type = SERIO_RS232,
  169. .proto = SERIO_ZHENHUA,
  170. .id = SERIO_ANY,
  171. .extra = SERIO_ANY,
  172. },
  173. { 0 }
  174. };
  175. MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
  176. static struct serio_driver zhenhua_drv = {
  177. .driver = {
  178. .name = "zhenhua",
  179. },
  180. .description = DRIVER_DESC,
  181. .id_table = zhenhua_serio_ids,
  182. .interrupt = zhenhua_interrupt,
  183. .connect = zhenhua_connect,
  184. .disconnect = zhenhua_disconnect,
  185. };
  186. module_serio_driver(zhenhua_drv);