[PATCH 1/2] driver for Zhen Hua PCM-4CH RC transmitter v2

2008-02-13 Thread Kmarty
(again, now as attachments)

Hi,
this is a driver for Zhen Hua PCM-4CH RC transmitter (commonly used in
cheap Ready To Fly RC helicopters) which using Zhen Hua 5-byte
protocol for using them as a four axis joystick via serial port.

Transmitter connected to serial port (19200 8N1) sending periodically
5 bytes where first byte is for synchronization and next four bytes
are values of axis.


linux-2.6.24-zhenhua.diffstat
Description: Binary data
Signed-off-by: Martin Kebert [EMAIL PROTECTED]

diff -uprN -X linux-2.6.24-vanilla/Documentation/dontdiff linux-2.6.24-vanilla/drivers/input/joystick/Kconfig linux-2.6.24-zhenhua/drivers/input/joystick/Kconfig
--- linux-2.6.24-vanilla/drivers/input/joystick/Kconfig	2008-01-24 23:58:37.0 +0100
+++ linux-2.6.24-zhenhua/drivers/input/joystick/Kconfig	2008-02-08 22:48:34.0 +0100
@@ -193,6 +193,18 @@ config JOYSTICK_TWIDJOY
 	  To compile this driver as a module, choose M here: the
 	  module will be called twidjoy.
 
+config JOYSTICK_ZHENHUA
+	tristate 5-byte Zhenhua RC transmitter
+	select SERIO
+	help
+	  Say Y here if you have a Zhen Hua PCM-4CH transmitter which is
+	  supplied with a ready to fly micro electric indoor helicopters
+	  such as EasyCopter, Lama, MiniCopter, DragonFly or Jabo and want
+	  to use it via serial cable as a joystick.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called zhenhua.
+
 config JOYSTICK_DB9
 	tristate Multisystem, Sega Genesis, Saturn joysticks and gamepads
 	depends on PARPORT
diff -uprN -X linux-2.6.24-vanilla/Documentation/dontdiff linux-2.6.24-vanilla/drivers/input/joystick/Makefile linux-2.6.24-zhenhua/drivers/input/joystick/Makefile
--- linux-2.6.24-vanilla/drivers/input/joystick/Makefile	2008-01-24 23:58:37.0 +0100
+++ linux-2.6.24-zhenhua/drivers/input/joystick/Makefile	2008-02-08 19:45:36.0 +0100
@@ -27,5 +27,6 @@ obj-$(CONFIG_JOYSTICK_TURBOGRAFX)	+= tur
 obj-$(CONFIG_JOYSTICK_TWIDJOY)		+= twidjoy.o
 obj-$(CONFIG_JOYSTICK_WARRIOR)		+= warrior.o
 obj-$(CONFIG_JOYSTICK_XPAD)		+= xpad.o
+obj-$(CONFIG_JOYSTICK_ZHENHUA)		+= zhenhua.o
 
 obj-$(CONFIG_JOYSTICK_IFORCE)		+= iforce/
diff -uprN -X linux-2.6.24-vanilla/Documentation/dontdiff linux-2.6.24-vanilla/drivers/input/joystick/zhenhua.c linux-2.6.24-zhenhua/drivers/input/joystick/zhenhua.c
--- linux-2.6.24-vanilla/drivers/input/joystick/zhenhua.c	1970-01-01 01:00:00.0 +0100
+++ linux-2.6.24-zhenhua/drivers/input/joystick/zhenhua.c	2008-02-08 19:27:40.0 +0100
@@ -0,0 +1,243 @@
+/*
+ *  derived from twidjoy.c
+ *
+ *  Copyright (c) 2008 Martin Kebert
+ *  Copyright (c) 2001 Arndt Schoenewald
+ *  Copyright (c) 2000-2001 Vojtech Pavlik
+ *  Copyright (c) 2000 Mark Fletcher
+ *
+ */
+
+/*
+ * Driver to use 4CH RC transmitter using Zhen Hua 5-byte protocol (Walkera Lama,
+ * EasyCopter etc.) as a joystick under Linux.
+ *
+ * RC transmitters using Zhen Hua 5-byte protocol are cheap four channels
+ * transmitters for control a RC planes or RC helicopters with possibility to
+ * connect on a serial port.
+ * Data coming from transmitter is in this order:
+ * 1. byte = synchronisation byte
+ * 2. byte = X axis
+ * 3. byte = Y axis
+ * 4. byte = RZ axis
+ * 5. byte = Z axis
+ * (and this is repeated)
+ *
+ * For questions or feedback regarding this driver module please contact:
+ * Martin Kebert [EMAIL PROTECTED] - but I am not a C-programmer nor kernel
+ * coder :-(
+ */
+
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include linux/kernel.h
+#include linux/module.h
+#include linux/slab.h
+#include linux/input.h
+#include linux/serio.h
+#include linux/init.h
+
+#define DRIVER_DESC	RC transmitter with 5-byte Zhen Hua protocol joystick driver
+
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE(GPL);
+
+/*
+ * Constants.
+ */
+
+#define ZHENHUA_MAX_LENGTH 5
+
+/*
+ * Zhen Hua data.
+ */
+
+struct zhenhua {
+	struct input_dev *dev;
+	int idx;
+	unsigned char data[ZHENHUA_MAX_LENGTH];
+	char phys[32];
+};
+
+
+/* bits in all incoming bytes needs to be reversed */
+static int zhenhua_bitreverse(int x)
+{
+	x = ((x  0xaa)  1) | ((x  0x55)  1);
+	x = ((x  0xcc)  2) | ((x  0x33)  2);
+	x = ((x  0xf0)  4) | ((x  0x0f)  4);
+	return x;
+}
+
+/*
+ * zhenhua_process_packet() decodes packets the driver receives from the
+ * RC transmitter. 

[PATCH 2/2] driver for Zhen Hua PCM-4CH RC transmitter v2

2008-02-13 Thread Kmarty
(again, now as attachments)

Patch for inputattach from joystick-utils.


inputattach.c.diffstat
Description: Binary data
--- inputattach.c.orig	2008-02-06 10:35:22.0 +0100
+++ inputattach.c	2008-02-13 15:49:48.0 +0100
@@ -1,5 +1,4 @@
 /*
- * $Id: inputattach.c 2352 2006-02-08 12:19:31Z vojtech $
  *
  *  Copyright (c) 1999-2000 Vojtech Pavlik
  *
@@ -315,6 +314,42 @@
 	return 0;
 }
 
+int zhenhua_init(int fd, long *id, long *extra)
+{
+	/* Zhen Hua 5 byte protokol: prvni (synchronizacni) byte obsahuje vzdy 0xF7,
+	 * nasledujici ctyri pak jednotlive osy ovladace v intervalu 50-200.
+	 * Pozor, nacitana data (jednotlive bajty) jsou bitove prevracene (nejnizsi
+	 * bit je nejvyssim bitem) - neco jako little-endian na bitove urovni.
+	 * Synchronizacni byte ma tedy bez prevraceni hodnotu 0xEF
+	 *
+	 * Inicializace je temer shodna s twiddlerem */
+
+	unsigned char c[10];
+	int count;
+
+	for (count=0 ; count  5 ; count++) {
+		if(readchar(fd, c+0, 500)) return -1;
+		if(c[0] == 0xef) break;
+	}
+
+	if (count == 5) {
+		/* Could not find header byte in data stream */
+		return -1;
+	}
+
+	/* Read remaining 4 bytes plus the full next data packet */
+	for (count = 1; count  10; count++) {
+		if (readchar(fd, c+count, 500)) return -1;
+	}
+
+	/* check if next sync byte exists */
+	if (c[5] != 0xef)
+		return -1;
+
+	return 0;
+		
+}
+
 int dump_init(int fd, long *id, long *extra)
 {
 	unsigned char c, o = 0;
@@ -376,6 +411,7 @@
 { --elo4002,		-elo6b,	B9600, CS8 | CRTSCTS,		SERIO_ELO,	1,	0,	0,	NULL },
 { --elo271-140,	-elo4b,	B9600, CS8 | CRTSCTS,		SERIO_ELO,	2,	0,	0,	NULL },
 { --elo261-280,	-elo3b,	B9600, CS8 | CRTSCTS,		SERIO_ELO,	3,	0,	0,	NULL },
+{ --zhen-hua,		-zhen,	B19200, CS8, 			SERIO_ZHENHUA,	0,	0,	0,	zhenhua_init },
 { --dump,		-dump,	B2400, CS8, 			0,		0,	0,	0,	dump_init },
 { , , 0, 0 }
 
@@ -416,6 +452,7 @@
 		puts(  --ps2serkbd -ps2ser PS/2 via serial keyboard);
 		puts(  --twiddler  -twid   Handykey Twiddler chording keyboard);
 		puts(  --twiddler-joy  -twidjoy  Handykey Twiddler used as a joystick);
+		puts(  --zhen-hua  -zhen  Zhen Hua 5-byte protocol);
 		puts();
 return 1;
 }