Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  derived from "twidjoy.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (c) 2008 Martin Kebert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Copyright (c) 2001 Arndt Schoenewald
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Copyright (c) 2000-2001 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  Copyright (c) 2000 Mark Fletcher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Driver to use 4CH RC transmitter using Zhen Hua 5-byte protocol (Walkera Lama,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * EasyCopter etc.) as a joystick under Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * RC transmitters using Zhen Hua 5-byte protocol are cheap four channels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * transmitters for control a RC planes or RC helicopters with possibility to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * connect on a serial port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Data coming from transmitter is in this order:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * 1. byte = synchronisation byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * 2. byte = X axis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * 3. byte = Y axis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * 4. byte = RZ axis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * 5. byte = Z axis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * (and this is repeated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * For questions or feedback regarding this driver module please contact:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Martin Kebert <gkmarty@gmail.com> - but I am not a C-programmer nor kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * coder :-(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/bitrev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define DRIVER_DESC	"RC transmitter with 5-byte Zhen Hua protocol joystick driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * Constants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define ZHENHUA_MAX_LENGTH 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * Zhen Hua data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) struct zhenhua {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unsigned char data[ZHENHUA_MAX_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * zhenhua_process_packet() decodes packets the driver receives from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * RC transmitter. It updates the data accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void zhenhua_process_packet(struct zhenhua *zhenhua)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct input_dev *dev = zhenhua->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	unsigned char *data = zhenhua->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	input_report_abs(dev, ABS_Y, data[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	input_report_abs(dev, ABS_X, data[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	input_report_abs(dev, ABS_RZ, data[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	input_report_abs(dev, ABS_Z, data[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * zhenhua_interrupt() is called by the low level driver when characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * are ready for us. We then buffer them for further processing, or call the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * packet processing routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static irqreturn_t zhenhua_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct zhenhua *zhenhua = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/* All Zhen Hua packets are 5 bytes. The fact that the first byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 * is allways 0xf7 and all others are in range 0x32 - 0xc8 (50-200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * can be used to check and regain sync. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (data == 0xef)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		zhenhua->idx = 0;	/* this byte starts a new packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	else if (zhenhua->idx == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return IRQ_HANDLED;	/* wrong MSB -- ignore this byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (zhenhua->idx < ZHENHUA_MAX_LENGTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		zhenhua->data[zhenhua->idx++] = bitrev8(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (zhenhua->idx == ZHENHUA_MAX_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		zhenhua_process_packet(zhenhua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		zhenhua->idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * zhenhua_disconnect() is the opposite of zhenhua_connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static void zhenhua_disconnect(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct zhenhua *zhenhua = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	input_unregister_device(zhenhua->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	kfree(zhenhua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * zhenhua_connect() is the routine that is called when someone adds a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * new serio device. It looks for the Twiddler, and if found, registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * it as an input device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int zhenhua_connect(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct zhenhua *zhenhua;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	zhenhua = kzalloc(sizeof(struct zhenhua), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (!zhenhua || !input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	zhenhua->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	snprintf(zhenhua->phys, sizeof(zhenhua->phys), "%s/input0", serio->phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	input_dev->name = "Zhen Hua 5-byte device";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	input_dev->phys = zhenhua->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	input_dev->id.bustype = BUS_RS232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	input_dev->id.vendor = SERIO_ZHENHUA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	input_dev->id.product = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	input_dev->dev.parent = &serio->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	input_dev->evbit[0] = BIT(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	input_set_abs_params(input_dev, ABS_X, 50, 200, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	input_set_abs_params(input_dev, ABS_Y, 50, 200, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	input_set_abs_params(input_dev, ABS_Z, 50, 200, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	input_set_abs_params(input_dev, ABS_RZ, 50, 200, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	serio_set_drvdata(serio, zhenhua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	err = serio_open(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	err = input_register_device(zhenhua->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  fail3:	serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  fail2:	serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  fail1:	input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	kfree(zhenhua);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * The serio driver structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static const struct serio_device_id zhenhua_serio_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		.type	= SERIO_RS232,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		.proto	= SERIO_ZHENHUA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		.id	= SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		.extra	= SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	{ 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_DEVICE_TABLE(serio, zhenhua_serio_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static struct serio_driver zhenhua_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		.name	= "zhenhua",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.description	= DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.id_table	= zhenhua_serio_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.interrupt	= zhenhua_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.connect	= zhenhua_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	.disconnect	= zhenhua_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) module_serio_driver(zhenhua_drv);