Linux Audio

Check our new training course

Loading...
v3.15
  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
 53#define DRIVER_DESC	"RC transmitter with 5-byte Zhen Hua protocol joystick driver"
 54
 55MODULE_DESCRIPTION(DRIVER_DESC);
 56MODULE_LICENSE("GPL");
 57
 58/*
 59 * Constants.
 60 */
 61
 62#define ZHENHUA_MAX_LENGTH 5
 63
 64/*
 65 * Zhen Hua data.
 66 */
 67
 68struct zhenhua {
 69	struct input_dev *dev;
 70	int idx;
 71	unsigned char data[ZHENHUA_MAX_LENGTH];
 72	char phys[32];
 73};
 74
 75
 76/* bits in all incoming bytes needs to be "reversed" */
 77static int zhenhua_bitreverse(int x)
 78{
 79	x = ((x & 0xaa) >> 1) | ((x & 0x55) << 1);
 80	x = ((x & 0xcc) >> 2) | ((x & 0x33) << 2);
 81	x = ((x & 0xf0) >> 4) | ((x & 0x0f) << 4);
 82	return x;
 83}
 84
 85/*
 86 * zhenhua_process_packet() decodes packets the driver receives from the
 87 * RC transmitter. It updates the data accordingly.
 88 */
 89
 90static void zhenhua_process_packet(struct zhenhua *zhenhua)
 91{
 92	struct input_dev *dev = zhenhua->dev;
 93	unsigned char *data = zhenhua->data;
 94
 95	input_report_abs(dev, ABS_Y, data[1]);
 96	input_report_abs(dev, ABS_X, data[2]);
 97	input_report_abs(dev, ABS_RZ, data[3]);
 98	input_report_abs(dev, ABS_Z, data[4]);
 99
100	input_sync(dev);
101}
102
103/*
104 * zhenhua_interrupt() is called by the low level driver when characters
105 * are ready for us. We then buffer them for further processing, or call the
106 * packet processing routine.
107 */
108
109static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
110{
111	struct zhenhua *zhenhua = serio_get_drvdata(serio);
112
113	/* All Zhen Hua packets are 5 bytes. The fact that the first byte
114	 * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
115	 * can be used to check and regain sync. */
116
117	if (data == 0xef)
118		zhenhua->idx = 0;	/* this byte starts a new packet */
119	else if (zhenhua->idx == 0)
120		return IRQ_HANDLED;	/* wrong MSB -- ignore this byte */
121
122	if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
123		zhenhua->data[zhenhua->idx++] = zhenhua_bitreverse(data);
124
125	if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
126		zhenhua_process_packet(zhenhua);
127		zhenhua->idx = 0;
128	}
129
130	return IRQ_HANDLED;
131}
132
133/*
134 * zhenhua_disconnect() is the opposite of zhenhua_connect()
135 */
136
137static void zhenhua_disconnect(struct serio *serio)
138{
139	struct zhenhua *zhenhua = serio_get_drvdata(serio);
140
141	serio_close(serio);
142	serio_set_drvdata(serio, NULL);
143	input_unregister_device(zhenhua->dev);
144	kfree(zhenhua);
145}
146
147/*
148 * zhenhua_connect() is the routine that is called when someone adds a
149 * new serio device. It looks for the Twiddler, and if found, registers
150 * it as an input device.
151 */
152
153static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
154{
155	struct zhenhua *zhenhua;
156	struct input_dev *input_dev;
157	int err = -ENOMEM;
158
159	zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
160	input_dev = input_allocate_device();
161	if (!zhenhua || !input_dev)
162		goto fail1;
163
164	zhenhua->dev = input_dev;
165	snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
166
167	input_dev->name = "Zhen Hua 5-byte device";
168	input_dev->phys = zhenhua->phys;
169	input_dev->id.bustype = BUS_RS232;
170	input_dev->id.vendor = SERIO_ZHENHUA;
171	input_dev->id.product = 0x0001;
172	input_dev->id.version = 0x0100;
173	input_dev->dev.parent = &serio->dev;
174
175	input_dev->evbit[0] = BIT(EV_ABS);
176	input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
177	input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
178	input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
179	input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
180
181	serio_set_drvdata(serio, zhenhua);
182
183	err = serio_open(serio, drv);
184	if (err)
185		goto fail2;
186
187	err = input_register_device(zhenhua->dev);
188	if (err)
189		goto fail3;
190
191	return 0;
192
193 fail3:	serio_close(serio);
194 fail2:	serio_set_drvdata(serio, NULL);
195 fail1:	input_free_device(input_dev);
196	kfree(zhenhua);
197	return err;
198}
199
200/*
201 * The serio driver structure.
202 */
203
204static struct serio_device_id zhenhua_serio_ids[] = {
205	{
206		.type	= SERIO_RS232,
207		.proto	= SERIO_ZHENHUA,
208		.id	= SERIO_ANY,
209		.extra	= SERIO_ANY,
210	},
211	{ 0 }
212};
213
214MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
215
216static struct serio_driver zhenhua_drv = {
217	.driver		= {
218		.name	= "zhenhua",
219	},
220	.description	= DRIVER_DESC,
221	.id_table	= zhenhua_serio_ids,
222	.interrupt	= zhenhua_interrupt,
223	.connect	= zhenhua_connect,
224	.disconnect	= zhenhua_disconnect,
225};
226
227module_serio_driver(zhenhua_drv);
v4.6
  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/bitrev.h>
 51#include <linux/input.h>
 52#include <linux/serio.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 * zhenhua_process_packet() decodes packets the driver receives from the
 78 * RC transmitter. It updates the data accordingly.
 79 */
 80
 81static void zhenhua_process_packet(struct zhenhua *zhenhua)
 82{
 83	struct input_dev *dev = zhenhua->dev;
 84	unsigned char *data = zhenhua->data;
 85
 86	input_report_abs(dev, ABS_Y, data[1]);
 87	input_report_abs(dev, ABS_X, data[2]);
 88	input_report_abs(dev, ABS_RZ, data[3]);
 89	input_report_abs(dev, ABS_Z, data[4]);
 90
 91	input_sync(dev);
 92}
 93
 94/*
 95 * zhenhua_interrupt() is called by the low level driver when characters
 96 * are ready for us. We then buffer them for further processing, or call the
 97 * packet processing routine.
 98 */
 99
100static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
101{
102	struct zhenhua *zhenhua = serio_get_drvdata(serio);
103
104	/* All Zhen Hua packets are 5 bytes. The fact that the first byte
105	 * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
106	 * can be used to check and regain sync. */
107
108	if (data == 0xef)
109		zhenhua->idx = 0;	/* this byte starts a new packet */
110	else if (zhenhua->idx == 0)
111		return IRQ_HANDLED;	/* wrong MSB -- ignore this byte */
112
113	if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
114		zhenhua->data[zhenhua->idx++] = bitrev8(data);
115
116	if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
117		zhenhua_process_packet(zhenhua);
118		zhenhua->idx = 0;
119	}
120
121	return IRQ_HANDLED;
122}
123
124/*
125 * zhenhua_disconnect() is the opposite of zhenhua_connect()
126 */
127
128static void zhenhua_disconnect(struct serio *serio)
129{
130	struct zhenhua *zhenhua = serio_get_drvdata(serio);
131
132	serio_close(serio);
133	serio_set_drvdata(serio, NULL);
134	input_unregister_device(zhenhua->dev);
135	kfree(zhenhua);
136}
137
138/*
139 * zhenhua_connect() is the routine that is called when someone adds a
140 * new serio device. It looks for the Twiddler, and if found, registers
141 * it as an input device.
142 */
143
144static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
145{
146	struct zhenhua *zhenhua;
147	struct input_dev *input_dev;
148	int err = -ENOMEM;
149
150	zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
151	input_dev = input_allocate_device();
152	if (!zhenhua || !input_dev)
153		goto fail1;
154
155	zhenhua->dev = input_dev;
156	snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
157
158	input_dev->name = "Zhen Hua 5-byte device";
159	input_dev->phys = zhenhua->phys;
160	input_dev->id.bustype = BUS_RS232;
161	input_dev->id.vendor = SERIO_ZHENHUA;
162	input_dev->id.product = 0x0001;
163	input_dev->id.version = 0x0100;
164	input_dev->dev.parent = &serio->dev;
165
166	input_dev->evbit[0] = BIT(EV_ABS);
167	input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
168	input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
169	input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
170	input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
171
172	serio_set_drvdata(serio, zhenhua);
173
174	err = serio_open(serio, drv);
175	if (err)
176		goto fail2;
177
178	err = input_register_device(zhenhua->dev);
179	if (err)
180		goto fail3;
181
182	return 0;
183
184 fail3:	serio_close(serio);
185 fail2:	serio_set_drvdata(serio, NULL);
186 fail1:	input_free_device(input_dev);
187	kfree(zhenhua);
188	return err;
189}
190
191/*
192 * The serio driver structure.
193 */
194
195static struct serio_device_id zhenhua_serio_ids[] = {
196	{
197		.type	= SERIO_RS232,
198		.proto	= SERIO_ZHENHUA,
199		.id	= SERIO_ANY,
200		.extra	= SERIO_ANY,
201	},
202	{ 0 }
203};
204
205MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
206
207static struct serio_driver zhenhua_drv = {
208	.driver		= {
209		.name	= "zhenhua",
210	},
211	.description	= DRIVER_DESC,
212	.id_table	= zhenhua_serio_ids,
213	.interrupt	= zhenhua_interrupt,
214	.connect	= zhenhua_connect,
215	.disconnect	= zhenhua_disconnect,
216};
217
218module_serio_driver(zhenhua_drv);