zhenhua.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * derived from "twidjoy.c"
  4. *
  5. * Copyright (c) 2008 Martin Kebert
  6. * Copyright (c) 2001 Arndt Schoenewald
  7. * Copyright (c) 2000-2001 Vojtech Pavlik
  8. * Copyright (c) 2000 Mark Fletcher
  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. */
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <linux/bitrev.h>
  35. #include <linux/input.h>
  36. #include <linux/serio.h>
  37. #define DRIVER_DESC "RC transmitter with 5-byte Zhen Hua protocol joystick driver"
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. /*
  41. * Constants.
  42. */
  43. #define ZHENHUA_MAX_LENGTH 5
  44. /*
  45. * Zhen Hua data.
  46. */
  47. struct zhenhua {
  48. struct input_dev *dev;
  49. int idx;
  50. unsigned char data[ZHENHUA_MAX_LENGTH];
  51. char phys[32];
  52. };
  53. /*
  54. * zhenhua_process_packet() decodes packets the driver receives from the
  55. * RC transmitter. It updates the data accordingly.
  56. */
  57. static void zhenhua_process_packet(struct zhenhua *zhenhua)
  58. {
  59. struct input_dev *dev = zhenhua->dev;
  60. unsigned char *data = zhenhua->data;
  61. input_report_abs(dev, ABS_Y, data[1]);
  62. input_report_abs(dev, ABS_X, data[2]);
  63. input_report_abs(dev, ABS_RZ, data[3]);
  64. input_report_abs(dev, ABS_Z, data[4]);
  65. input_sync(dev);
  66. }
  67. /*
  68. * zhenhua_interrupt() is called by the low level driver when characters
  69. * are ready for us. We then buffer them for further processing, or call the
  70. * packet processing routine.
  71. */
  72. static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
  73. {
  74. struct zhenhua *zhenhua = serio_get_drvdata(serio);
  75. /* All Zhen Hua packets are 5 bytes. The fact that the first byte
  76. * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
  77. * can be used to check and regain sync. */
  78. if (data == 0xef)
  79. zhenhua->idx = 0; /* this byte starts a new packet */
  80. else if (zhenhua->idx == 0)
  81. return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
  82. if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
  83. zhenhua->data[zhenhua->idx++] = bitrev8(data);
  84. if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
  85. zhenhua_process_packet(zhenhua);
  86. zhenhua->idx = 0;
  87. }
  88. return IRQ_HANDLED;
  89. }
  90. /*
  91. * zhenhua_disconnect() is the opposite of zhenhua_connect()
  92. */
  93. static void zhenhua_disconnect(struct serio *serio)
  94. {
  95. struct zhenhua *zhenhua = serio_get_drvdata(serio);
  96. serio_close(serio);
  97. serio_set_drvdata(serio, NULL);
  98. input_unregister_device(zhenhua->dev);
  99. kfree(zhenhua);
  100. }
  101. /*
  102. * zhenhua_connect() is the routine that is called when someone adds a
  103. * new serio device. It looks for the Twiddler, and if found, registers
  104. * it as an input device.
  105. */
  106. static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
  107. {
  108. struct zhenhua *zhenhua;
  109. struct input_dev *input_dev;
  110. int err = -ENOMEM;
  111. zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
  112. input_dev = input_allocate_device();
  113. if (!zhenhua || !input_dev)
  114. goto fail1;
  115. zhenhua->dev = input_dev;
  116. snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
  117. input_dev->name = "Zhen Hua 5-byte device";
  118. input_dev->phys = zhenhua->phys;
  119. input_dev->id.bustype = BUS_RS232;
  120. input_dev->id.vendor = SERIO_ZHENHUA;
  121. input_dev->id.product = 0x0001;
  122. input_dev->id.version = 0x0100;
  123. input_dev->dev.parent = &serio->dev;
  124. input_dev->evbit[0] = BIT(EV_ABS);
  125. input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
  126. input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
  127. input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
  128. input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
  129. serio_set_drvdata(serio, zhenhua);
  130. err = serio_open(serio, drv);
  131. if (err)
  132. goto fail2;
  133. err = input_register_device(zhenhua->dev);
  134. if (err)
  135. goto fail3;
  136. return 0;
  137. fail3: serio_close(serio);
  138. fail2: serio_set_drvdata(serio, NULL);
  139. fail1: input_free_device(input_dev);
  140. kfree(zhenhua);
  141. return err;
  142. }
  143. /*
  144. * The serio driver structure.
  145. */
  146. static const struct serio_device_id zhenhua_serio_ids[] = {
  147. {
  148. .type = SERIO_RS232,
  149. .proto = SERIO_ZHENHUA,
  150. .id = SERIO_ANY,
  151. .extra = SERIO_ANY,
  152. },
  153. { 0 }
  154. };
  155. MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
  156. static struct serio_driver zhenhua_drv = {
  157. .driver = {
  158. .name = "zhenhua",
  159. },
  160. .description = DRIVER_DESC,
  161. .id_table = zhenhua_serio_ids,
  162. .interrupt = zhenhua_interrupt,
  163. .connect = zhenhua_connect,
  164. .disconnect = zhenhua_disconnect,
  165. };
  166. module_serio_driver(zhenhua_drv);