zhenhua.c 4.9 KB

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