zhenhua.c 5.5 KB

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