Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / input / joystick / zhenhua.c
blobcc0e2a77ac5e4d7fe9ceddd1db8c11b6260a1c28
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * derived from "twidjoy.c"
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 */
12 * Driver to use 4CH RC transmitter using Zhen Hua 5-byte protocol (Walkera Lama,
13 * EasyCopter etc.) as a joystick under Linux.
15 * RC transmitters using Zhen Hua 5-byte protocol are cheap four channels
16 * transmitters for control a RC planes or RC helicopters with possibility to
17 * connect on a serial port.
18 * Data coming from transmitter is in this order:
19 * 1. byte = synchronisation byte
20 * 2. byte = X axis
21 * 3. byte = Y axis
22 * 4. byte = RZ axis
23 * 5. byte = Z axis
24 * (and this is repeated)
26 * For questions or feedback regarding this driver module please contact:
27 * Martin Kebert <gkmarty@gmail.com> - but I am not a C-programmer nor kernel
28 * coder :-(
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>
38 #define DRIVER_DESC "RC transmitter with 5-byte Zhen Hua protocol joystick driver"
40 MODULE_DESCRIPTION(DRIVER_DESC);
41 MODULE_LICENSE("GPL");
44 * Constants.
47 #define ZHENHUA_MAX_LENGTH 5
50 * Zhen Hua data.
53 struct zhenhua {
54 struct input_dev *dev;
55 int idx;
56 unsigned char data[ZHENHUA_MAX_LENGTH];
57 char phys[32];
61 * zhenhua_process_packet() decodes packets the driver receives from the
62 * RC transmitter. It updates the data accordingly.
65 static void zhenhua_process_packet(struct zhenhua *zhenhua)
67 struct input_dev *dev = zhenhua->dev;
68 unsigned char *data = zhenhua->data;
70 input_report_abs(dev, ABS_Y, data[1]);
71 input_report_abs(dev, ABS_X, data[2]);
72 input_report_abs(dev, ABS_RZ, data[3]);
73 input_report_abs(dev, ABS_Z, data[4]);
75 input_sync(dev);
79 * zhenhua_interrupt() is called by the low level driver when characters
80 * are ready for us. We then buffer them for further processing, or call the
81 * packet processing routine.
84 static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
86 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. */
92 if (data == 0xef)
93 zhenhua->idx = 0; /* this byte starts a new packet */
94 else if (zhenhua->idx == 0)
95 return IRQ_HANDLED; /* wrong MSB -- ignore this byte */
97 if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
98 zhenhua->data[zhenhua->idx++] = bitrev8(data);
100 if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
101 zhenhua_process_packet(zhenhua);
102 zhenhua->idx = 0;
105 return IRQ_HANDLED;
109 * zhenhua_disconnect() is the opposite of zhenhua_connect()
112 static void zhenhua_disconnect(struct serio *serio)
114 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);
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.
128 static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
130 struct zhenhua *zhenhua;
131 struct input_dev *input_dev;
132 int err = -ENOMEM;
134 zhenhua = kzalloc(sizeof(*zhenhua), GFP_KERNEL);
135 input_dev = input_allocate_device();
136 if (!zhenhua || !input_dev)
137 goto fail1;
139 zhenhua->dev = input_dev;
140 snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
142 input_dev->name = "Zhen Hua 5-byte device";
143 input_dev->phys = zhenhua->phys;
144 input_dev->id.bustype = BUS_RS232;
145 input_dev->id.vendor = SERIO_ZHENHUA;
146 input_dev->id.product = 0x0001;
147 input_dev->id.version = 0x0100;
148 input_dev->dev.parent = &serio->dev;
150 input_dev->evbit[0] = BIT(EV_ABS);
151 input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
152 input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
153 input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
154 input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
156 serio_set_drvdata(serio, zhenhua);
158 err = serio_open(serio, drv);
159 if (err)
160 goto fail2;
162 err = input_register_device(zhenhua->dev);
163 if (err)
164 goto fail3;
166 return 0;
168 fail3: serio_close(serio);
169 fail2: serio_set_drvdata(serio, NULL);
170 fail1: input_free_device(input_dev);
171 kfree(zhenhua);
172 return err;
176 * The serio driver structure.
179 static const struct serio_device_id zhenhua_serio_ids[] = {
181 .type = SERIO_RS232,
182 .proto = SERIO_ZHENHUA,
183 .id = SERIO_ANY,
184 .extra = SERIO_ANY,
186 { 0 }
189 MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
191 static struct serio_driver zhenhua_drv = {
192 .driver = {
193 .name = "zhenhua",
195 .description = DRIVER_DESC,
196 .id_table = zhenhua_serio_ids,
197 .interrupt = zhenhua_interrupt,
198 .connect = zhenhua_connect,
199 .disconnect = zhenhua_disconnect,
202 module_serio_driver(zhenhua_drv);