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) 1998-2005 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^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)  * Logitech ADI joystick family driver for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^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) #include <linux/delay.h>
^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/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/gameport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define DRIVER_DESC	"Logitech ADI joystick family driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Times, array sizes, flags, ids.
^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) #define ADI_MAX_START		200	/* Trigger to packet timeout [200us] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define ADI_MAX_STROBE		40	/* Single bit timeout [40us] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define ADI_INIT_DELAY		10	/* Delay after init packet [10ms] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define ADI_DATA_DELAY		4	/* Delay after data packet [4ms] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define ADI_MAX_LENGTH		256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define ADI_MIN_LENGTH		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define ADI_MIN_LEN_LENGTH	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define ADI_MIN_ID_LENGTH	66
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ADI_MAX_NAME_LENGTH	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define ADI_MAX_CNAME_LENGTH	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define ADI_MAX_PHYS_LENGTH	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define ADI_FLAG_HAT		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define ADI_FLAG_10BIT		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define ADI_ID_TPD		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define ADI_ID_WGP		0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define ADI_ID_WGPE		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define ADI_ID_MAX		0x0a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Names, buttons, axes ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static char *adi_names[] = {	"WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				"WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 				"WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 				"WingMan GamePad USB", "Unknown Device %#x" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static char adi_wmgpe_abs[] =	{ ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static char adi_wmi_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static char adi_wmed3d_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static char adi_cm2_abs[] =	{ ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static char adi_wmf_abs[] =	{ ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static short adi_wmgpe_key[] =	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,  BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static short adi_wmi_key[] =	{ BTN_TRIGGER,  BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static short adi_wmed3d_key[] =	{ BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static short adi_cm2_key[] =	{ BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			   adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			    adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
^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)  * Hat to axis conversion arrays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * Per-port information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) struct adi {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	unsigned char id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	char buttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	char axes10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	char axes8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	signed char pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	char hats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	char *abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	short *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	char name[ADI_MAX_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	char cname[ADI_MAX_CNAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	char phys[ADI_MAX_PHYS_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned char data[ADI_MAX_LENGTH];
^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) struct adi_port {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct gameport *gameport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct adi adi[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int reads;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * adi_read_packet() reads a Logitech ADI packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static void adi_read_packet(struct adi_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct adi *adi = port->adi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct gameport *gameport = port->gameport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	unsigned char u, v, w, x, z;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	int t[2], s[2], i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		adi[i].ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		t[i] = gameport_time(gameport, ADI_MAX_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		s[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	gameport_trigger(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	v = z = gameport_read(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		u = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		w = u ^ (v = x = gameport_read(gameport));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			t[i]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			if ((w & 0x30) && s[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 					adi[i].data[++adi[i].ret] = w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 					t[i] = gameport_time(gameport, ADI_MAX_STROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				} else t[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			} else if (!(x & 0x30)) s[i] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	} while (t[0] > 0 || t[1] > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * adi_move_bits() detects a possible 2-stream mode, and moves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * the bits accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void adi_move_bits(struct adi_port *port, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct adi *adi = port->adi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	adi[0].idx = adi[1].idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	for (i = 1; i <= adi[1].ret; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	adi[0].ret += adi[1].ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	adi[1].ret = -1;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * adi_get_bits() gathers bits from the data packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static inline int adi_get_bits(struct adi *adi, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if ((adi->idx += count) > adi->ret) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * adi_decode() decodes Logitech joystick data into input events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int adi_decode(struct adi *adi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct input_dev *dev = adi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	char *abs = adi->abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	short *key = adi->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int i, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	for (i = 0; i < adi->axes10; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	for (i = 0; i < adi->axes8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	for (i = 0; i < adi->buttons && i < 63; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (i == adi->pad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			t = adi_get_bits(adi, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t       & 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		input_report_key(dev, *key++, adi_get_bits(adi, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	for (i = 0; i < adi->hats; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		input_report_abs(dev, *abs++, adi_hat_to_axis[t].y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	for (i = 63; i < adi->buttons; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		input_report_key(dev, *key++, adi_get_bits(adi, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * adi_read() reads the data packet and decodes it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int adi_read(struct adi_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	adi_read_packet(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	adi_move_bits(port, port->adi[0].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (port->adi[i].length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			 result |= adi_decode(port->adi + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  * adi_poll() repeatedly polls the Logitech joysticks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void adi_poll(struct gameport *gameport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct adi_port *port = gameport_get_drvdata(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	port->bad -= adi_read(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	port->reads++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * adi_open() is a callback from the input open routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static int adi_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct adi_port *port = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	gameport_start_polling(port->gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * adi_close() is a callback from the input close routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static void adi_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct adi_port *port = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	gameport_stop_polling(port->gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * adi_init_digital() sends a trigger & delay sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * to reset and initialize a Logitech joystick into digital mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static void adi_init_digital(struct gameport *gameport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	static const int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	for (i = 0; seq[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		gameport_trigger(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (seq[i] > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			msleep(seq[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		if (seq[i] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			mdelay(-seq[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			udelay(-seq[i]*14);	/* It looks like mdelay() is off by approx 1.4% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static void adi_id_decode(struct adi *adi, struct adi_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	int i, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (adi->ret < (t = adi_get_bits(adi, 10))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	adi->length = adi_get_bits(adi, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		adi->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	adi->axes8 = adi_get_bits(adi, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	adi->buttons = adi_get_bits(adi, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (adi_get_bits(adi, 6) != 8 && adi->hats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		adi->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	adi->buttons += adi_get_bits(adi, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	adi->hats += adi_get_bits(adi, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	i = adi_get_bits(adi, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (t & ADI_FLAG_10BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		adi->axes10 = adi->axes8 - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		adi->axes8 = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	t = adi_get_bits(adi, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	for (i = 0; i < t; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		adi->cname[i] = adi_get_bits(adi, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	adi->cname[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (adi->length != t && adi->length != t + (t & 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		adi->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	switch (adi->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		case ADI_ID_TPD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			adi->pad = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			adi->buttons -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		case ADI_ID_WGP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			adi->pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			adi->buttons -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			adi->pad = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	char buf[ADI_MAX_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	int i, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	adi->dev = input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (!input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	adi->abs = adi_abs[t];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	adi->key = adi_key[t];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	input_dev->name = adi->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	input_dev->phys = adi->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	input_dev->id.bustype = BUS_GAMEPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	input_dev->id.product = adi->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	input_dev->dev.parent = &port->gameport->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	input_set_drvdata(input_dev, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	input_dev->open = adi_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	input_dev->close = adi_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		set_bit(adi->abs[i], input_dev->absbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	for (i = 0; i < adi->buttons; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		set_bit(adi->key[i], input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static void adi_init_center(struct adi *adi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	int i, t, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (!adi->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		t = adi->abs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		x = input_abs_get_val(adi->dev, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			x = i < adi->axes10 ? 512 : 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		if (i < adi->axes10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		else if (i < adi->axes10 + adi->axes8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			input_set_abs_params(adi->dev, t, -1, 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * adi_connect() probes for Logitech ADI joysticks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct adi_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	port = kzalloc(sizeof(struct adi_port), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	port->gameport = gameport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	gameport_set_drvdata(gameport, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	adi_init_digital(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	adi_read_packet(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		adi_move_bits(port, adi_get_bits(port->adi, 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		adi_id_decode(port->adi + i, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		if (!port->adi[i].length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		err = adi_init_input(port->adi + i, port, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	if (!port->adi[0].length && !port->adi[1].length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	gameport_set_poll_handler(gameport, adi_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	gameport_set_poll_interval(gameport, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	msleep(ADI_INIT_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (adi_read(port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		msleep(ADI_DATA_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		adi_read(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		if (port->adi[i].length > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			adi_init_center(port->adi + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			err = input_register_device(port->adi[i].dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 				goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  fail3: while (--i >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		if (port->adi[i].length > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			input_unregister_device(port->adi[i].dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			port->adi[i].dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)  fail2:	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		input_free_device(port->adi[i].dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	gameport_close(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)  fail1:	gameport_set_drvdata(gameport, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	kfree(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static void adi_disconnect(struct gameport *gameport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	struct adi_port *port = gameport_get_drvdata(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		if (port->adi[i].length > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			input_unregister_device(port->adi[i].dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	gameport_close(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	gameport_set_drvdata(gameport, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	kfree(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static struct gameport_driver adi_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		.name	= "adi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	.description	= DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	.connect	= adi_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	.disconnect	= adi_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) module_gameport_driver(adi_drv);