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)  *  Copyright (c) 2000-2001 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Copyright (c) 2000 Mark Fletcher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Gravis Stinger gamepad driver for Linux
^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)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define DRIVER_DESC	"Gravis Stinger gamepad driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Constants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define STINGER_MAX_LENGTH 8
^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)  * Per-Stinger data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) struct stinger {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	unsigned char data[STINGER_MAX_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * stinger_process_packet() decodes packets the driver receives from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * Stinger. It updates the data accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static void stinger_process_packet(struct stinger *stinger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct input_dev *dev = stinger->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned char *data = stinger->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!stinger->idx) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	input_report_key(dev, BTN_A,	  ((data[0] & 0x20) >> 5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	input_report_key(dev, BTN_B,	  ((data[0] & 0x10) >> 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	input_report_key(dev, BTN_C,	  ((data[0] & 0x08) >> 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	input_report_key(dev, BTN_X,	  ((data[0] & 0x04) >> 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	input_report_key(dev, BTN_Y,	  ((data[3] & 0x20) >> 5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	input_report_key(dev, BTN_Z,	  ((data[3] & 0x10) >> 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	input_report_key(dev, BTN_TL,     ((data[3] & 0x08) >> 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	input_report_key(dev, BTN_TR,     ((data[3] & 0x04) >> 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	input_report_key(dev, BTN_START,   (data[3] & 0x01));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * stinger_interrupt() is called by the low level driver when characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * are ready for us. We then buffer them for further processing, or call the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * packet processing routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static irqreturn_t stinger_interrupt(struct serio *serio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	unsigned char data, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct stinger *stinger = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* All Stinger packets are 4 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (stinger->idx < STINGER_MAX_LENGTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		stinger->data[stinger->idx++] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (stinger->idx == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		stinger_process_packet(stinger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		stinger->idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * stinger_disconnect() is the opposite of stinger_connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void stinger_disconnect(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct stinger *stinger = serio_get_drvdata(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	input_unregister_device(stinger->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	kfree(stinger);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * stinger_connect() is the routine that is called when someone adds a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * new serio device that supports Stinger protocol and registers it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * an input device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int stinger_connect(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct stinger *stinger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!stinger || !input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	stinger->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	input_dev->name = "Gravis Stinger";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	input_dev->phys = stinger->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	input_dev->id.bustype = BUS_RS232;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	input_dev->id.vendor = SERIO_STINGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	input_dev->id.product = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	input_dev->dev.parent = &serio->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	serio_set_drvdata(serio, stinger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	err = serio_open(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	err = input_register_device(stinger->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  fail3:	serio_close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  fail2:	serio_set_drvdata(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  fail1:	input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	kfree(stinger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * The serio driver structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static const struct serio_device_id stinger_serio_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		.type	= SERIO_RS232,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		.proto	= SERIO_STINGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		.id	= SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		.extra	= SERIO_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	{ 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static struct serio_driver stinger_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		.name	= "stinger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	.description	= DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.id_table	= stinger_serio_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	.interrupt	= stinger_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	.connect	= stinger_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	.disconnect	= stinger_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) module_serio_driver(stinger_drv);