Linux Audio

Check our new training course

Loading...
v6.2
  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/*
 12 * Driver to use 4CH RC transmitter using Zhen Hua 5-byte protocol (Walkera Lama,
 13 * EasyCopter etc.) as a joystick under Linux.
 14 *
 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)
 25 *
 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 :-(
 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
 38#define DRIVER_DESC	"RC transmitter with 5-byte Zhen Hua protocol joystick driver"
 39
 40MODULE_DESCRIPTION(DRIVER_DESC);
 41MODULE_LICENSE("GPL");
 42
 43/*
 44 * Constants.
 45 */
 46
 47#define ZHENHUA_MAX_LENGTH 5
 48
 49/*
 50 * Zhen Hua data.
 51 */
 52
 53struct zhenhua {
 54	struct input_dev *dev;
 55	int idx;
 56	unsigned char data[ZHENHUA_MAX_LENGTH];
 57	char phys[32];
 58};
 59
 
 
 
 
 
 
 
 
 
 
 60/*
 61 * zhenhua_process_packet() decodes packets the driver receives from the
 62 * RC transmitter. It updates the data accordingly.
 63 */
 64
 65static void zhenhua_process_packet(struct zhenhua *zhenhua)
 66{
 67	struct input_dev *dev = zhenhua->dev;
 68	unsigned char *data = zhenhua->data;
 69
 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]);
 74
 75	input_sync(dev);
 76}
 77
 78/*
 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.
 82 */
 83
 84static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
 85{
 86	struct zhenhua *zhenhua = serio_get_drvdata(serio);
 87
 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
 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 */
 96
 97	if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
 98		zhenhua->data[zhenhua->idx++] = bitrev8(data);
 99
100	if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
101		zhenhua_process_packet(zhenhua);
102		zhenhua->idx = 0;
103	}
104
105	return IRQ_HANDLED;
106}
107
108/*
109 * zhenhua_disconnect() is the opposite of zhenhua_connect()
110 */
111
112static void zhenhua_disconnect(struct serio *serio)
113{
114	struct zhenhua *zhenhua = serio_get_drvdata(serio);
115
116	serio_close(serio);
117	serio_set_drvdata(serio, NULL);
118	input_unregister_device(zhenhua->dev);
119	kfree(zhenhua);
120}
121
122/*
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.
126 */
127
128static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
129{
130	struct zhenhua *zhenhua;
131	struct input_dev *input_dev;
132	int err = -ENOMEM;
133
134	zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
135	input_dev = input_allocate_device();
136	if (!zhenhua || !input_dev)
137		goto fail1;
138
139	zhenhua->dev = input_dev;
140	snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
141
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;
149
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);
155
156	serio_set_drvdata(serio, zhenhua);
157
158	err = serio_open(serio, drv);
159	if (err)
160		goto fail2;
161
162	err = input_register_device(zhenhua->dev);
163	if (err)
164		goto fail3;
165
166	return 0;
167
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;
173}
174
175/*
176 * The serio driver structure.
177 */
178
179static const struct serio_device_id zhenhua_serio_ids[] = {
180	{
181		.type	= SERIO_RS232,
182		.proto	= SERIO_ZHENHUA,
183		.id	= SERIO_ANY,
184		.extra	= SERIO_ANY,
185	},
186	{ 0 }
187};
188
189MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
190
191static struct serio_driver zhenhua_drv = {
192	.driver		= {
193		.name	= "zhenhua",
194	},
195	.description	= DRIVER_DESC,
196	.id_table	= zhenhua_serio_ids,
197	.interrupt	= zhenhua_interrupt,
198	.connect	= zhenhua_connect,
199	.disconnect	= zhenhua_disconnect,
200};
201
202module_serio_driver(zhenhua_drv);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v3.1
 
  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/*
 12 * Driver to use 4CH RC transmitter using Zhen Hua 5-byte protocol (Walkera Lama,
 13 * EasyCopter etc.) as a joystick under Linux.
 14 *
 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)
 25 *
 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 :-(
 29 */
 30
 31/*
 32 * This program is free software; you can redistribute it and/or modify
 33 * it under the terms of the GNU General Public License as published by
 34 * the Free Software Foundation; either version 2 of the License, or
 35 * (at your option) any later version.
 36 *
 37 * This program is distributed in the hope that it will be useful,
 38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 40 * GNU General Public License for more details.
 41 *
 42 * You should have received a copy of the GNU General Public License
 43 * along with this program; if not, write to the Free Software
 44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 45 */
 46
 47#include <linux/kernel.h>
 48#include <linux/module.h>
 49#include <linux/slab.h>
 
 50#include <linux/input.h>
 51#include <linux/serio.h>
 52#include <linux/init.h>
 53
 54#define DRIVER_DESC	"RC transmitter with 5-byte Zhen Hua protocol joystick driver"
 55
 56MODULE_DESCRIPTION(DRIVER_DESC);
 57MODULE_LICENSE("GPL");
 58
 59/*
 60 * Constants.
 61 */
 62
 63#define ZHENHUA_MAX_LENGTH 5
 64
 65/*
 66 * Zhen Hua data.
 67 */
 68
 69struct zhenhua {
 70	struct input_dev *dev;
 71	int idx;
 72	unsigned char data[ZHENHUA_MAX_LENGTH];
 73	char phys[32];
 74};
 75
 76
 77/* bits in all incoming bytes needs to be "reversed" */
 78static int zhenhua_bitreverse(int x)
 79{
 80	x = ((x & 0xaa) >> 1) | ((x & 0x55) << 1);
 81	x = ((x & 0xcc) >> 2) | ((x & 0x33) << 2);
 82	x = ((x & 0xf0) >> 4) | ((x & 0x0f) << 4);
 83	return x;
 84}
 85
 86/*
 87 * zhenhua_process_packet() decodes packets the driver receives from the
 88 * RC transmitter. It updates the data accordingly.
 89 */
 90
 91static void zhenhua_process_packet(struct zhenhua *zhenhua)
 92{
 93	struct input_dev *dev = zhenhua->dev;
 94	unsigned char *data = zhenhua->data;
 95
 96	input_report_abs(dev, ABS_Y, data[1]);
 97	input_report_abs(dev, ABS_X, data[2]);
 98	input_report_abs(dev, ABS_RZ, data[3]);
 99	input_report_abs(dev, ABS_Z, data[4]);
100
101	input_sync(dev);
102}
103
104/*
105 * zhenhua_interrupt() is called by the low level driver when characters
106 * are ready for us. We then buffer them for further processing, or call the
107 * packet processing routine.
108 */
109
110static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
111{
112	struct zhenhua *zhenhua = serio_get_drvdata(serio);
113
114	/* All Zhen Hua packets are 5 bytes. The fact that the first byte
115	 * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
116	 * can be used to check and regain sync. */
117
118	if (data == 0xef)
119		zhenhua->idx = 0;	/* this byte starts a new packet */
120	else if (zhenhua->idx == 0)
121		return IRQ_HANDLED;	/* wrong MSB -- ignore this byte */
122
123	if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
124		zhenhua->data[zhenhua->idx++] = zhenhua_bitreverse(data);
125
126	if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
127		zhenhua_process_packet(zhenhua);
128		zhenhua->idx = 0;
129	}
130
131	return IRQ_HANDLED;
132}
133
134/*
135 * zhenhua_disconnect() is the opposite of zhenhua_connect()
136 */
137
138static void zhenhua_disconnect(struct serio *serio)
139{
140	struct zhenhua *zhenhua = serio_get_drvdata(serio);
141
142	serio_close(serio);
143	serio_set_drvdata(serio, NULL);
144	input_unregister_device(zhenhua->dev);
145	kfree(zhenhua);
146}
147
148/*
149 * zhenhua_connect() is the routine that is called when someone adds a
150 * new serio device. It looks for the Twiddler, and if found, registers
151 * it as an input device.
152 */
153
154static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
155{
156	struct zhenhua *zhenhua;
157	struct input_dev *input_dev;
158	int err = -ENOMEM;
159
160	zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
161	input_dev = input_allocate_device();
162	if (!zhenhua || !input_dev)
163		goto fail1;
164
165	zhenhua->dev = input_dev;
166	snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
167
168	input_dev->name = "Zhen Hua 5-byte device";
169	input_dev->phys = zhenhua->phys;
170	input_dev->id.bustype = BUS_RS232;
171	input_dev->id.vendor = SERIO_ZHENHUA;
172	input_dev->id.product = 0x0001;
173	input_dev->id.version = 0x0100;
174	input_dev->dev.parent = &serio->dev;
175
176	input_dev->evbit[0] = BIT(EV_ABS);
177	input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
178	input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
179	input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
180	input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
181
182	serio_set_drvdata(serio, zhenhua);
183
184	err = serio_open(serio, drv);
185	if (err)
186		goto fail2;
187
188	err = input_register_device(zhenhua->dev);
189	if (err)
190		goto fail3;
191
192	return 0;
193
194 fail3:	serio_close(serio);
195 fail2:	serio_set_drvdata(serio, NULL);
196 fail1:	input_free_device(input_dev);
197	kfree(zhenhua);
198	return err;
199}
200
201/*
202 * The serio driver structure.
203 */
204
205static struct serio_device_id zhenhua_serio_ids[] = {
206	{
207		.type	= SERIO_RS232,
208		.proto	= SERIO_ZHENHUA,
209		.id	= SERIO_ANY,
210		.extra	= SERIO_ANY,
211	},
212	{ 0 }
213};
214
215MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
216
217static struct serio_driver zhenhua_drv = {
218	.driver		= {
219		.name	= "zhenhua",
220	},
221	.description	= DRIVER_DESC,
222	.id_table	= zhenhua_serio_ids,
223	.interrupt	= zhenhua_interrupt,
224	.connect	= zhenhua_connect,
225	.disconnect	= zhenhua_disconnect,
226};
227
228/*
229 * The functions for inserting/removing us as a module.
230 */
231
232static int __init zhenhua_init(void)
233{
234	return serio_register_driver(&zhenhua_drv);
235}
236
237static void __exit zhenhua_exit(void)
238{
239	serio_unregister_driver(&zhenhua_drv);
240}
241
242module_init(zhenhua_init);
243module_exit(zhenhua_exit);