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-2001 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Based on the work of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *	Trystan Larey-Williams
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^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)  * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
^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)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/gameport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define DRIVER_DESC	"ThrustMaster DirectConnect joystick driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define TMDC_MAX_START		600	/* 600 us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define TMDC_MAX_STROBE		60	/* 60 us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define TMDC_MAX_LENGTH		13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define TMDC_MODE_M3DI		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define TMDC_MODE_3DRP		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define TMDC_MODE_AT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define TMDC_MODE_FM		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define TMDC_MODE_FGP		163
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define TMDC_BYTE_ID		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define TMDC_BYTE_REV		11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define TMDC_BYTE_DEF		12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define TMDC_ABS		7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define TMDC_ABS_HAT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define TMDC_BTN		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static const signed char tmdc_abs[TMDC_ABS] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	{ ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	{ ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static const signed char tmdc_abs_at[TMDC_ABS] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{ ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static const signed char tmdc_abs_fm[TMDC_ABS] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	{ ABS_RX, ABS_RY, ABS_X, ABS_Y };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static const short tmdc_btn_pad[TMDC_BTN] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static const short tmdc_btn_joy[TMDC_BTN] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	{ BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	  BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static const short tmdc_btn_fm[TMDC_BTN] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)         { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static const short tmdc_btn_at[TMDC_BTN] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)         { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)           BTN_BASE3, BTN_BASE2, BTN_BASE };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)         int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)         int y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) } tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static const struct tmdc_model {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	unsigned char id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	char abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	char hats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	char btnc[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	char btno[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	const signed char *axes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	const short *buttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) } tmdc_models[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	{   1, "ThrustMaster Millenium 3D Inceptor",	  6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{   3, "ThrustMaster Rage 3D Gamepad",		  2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{   4, "ThrustMaster Attack Throttle",		  5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{   8, "ThrustMaster FragMaster",		  4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{ 163, "Thrustmaster Fusion GamePad",		  2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	{   0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) };
^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) struct tmdc_port {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	char name[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	const signed char *abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	const short *btn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	unsigned char absc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	unsigned char btnc[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	unsigned char btno[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct tmdc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct gameport *gameport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct tmdc_port *port[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct input_dev *dev[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	char name[2][64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	char phys[2][32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int mode[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	signed char *abs[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	short *btn[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned char absc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned char btnc[2][4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	unsigned char btno[2][4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int reads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	int bads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	unsigned char exists;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * tmdc_read_packet() reads a ThrustMaster packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned char u, v, w, x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int i[2], j[2], t[2], p, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	p = gameport_time(gameport, TMDC_MAX_STROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	for (k = 0; k < 2; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		t[k] = gameport_time(gameport, TMDC_MAX_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		i[k] = j[k] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	gameport_trigger(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	w = gameport_read(gameport) >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		x = w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		w = gameport_read(gameport) >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			if (~v & u & 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				t[k] = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				if (j[k] == 0) {				 /* Start bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					if (~v & 1) t[k] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 					data[k][i[k]] = 0; j[k]++; continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				if (j[k] == 9) {				/* Stop bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 					if (v & 1) t[k] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 					j[k] = 0; i[k]++; continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				data[k][i[k]] |= (~v & 1) << (j[k]++ - 1);	/* Data bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			t[k]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	} while (t[0] > 0 || t[1] > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int i, k, l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (data[TMDC_BYTE_ID] != port->mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	for (i = 0; i < port->absc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (port->abs[i] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	switch (port->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		case TMDC_MODE_M3DI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			i = tmdc_byte_d[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i]       & 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		case TMDC_MODE_AT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			i = tmdc_byte_a[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	for (k = l = 0; k < 4; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		for (i = 0; i < port->btnc[k]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			input_report_key(port->dev, port->btn[i + l],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 				((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		l += port->btnc[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	input_sync(port->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * tmdc_poll() reads and analyzes ThrustMaster joystick data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static void tmdc_poll(struct gameport *gameport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	unsigned char data[2][TMDC_MAX_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	struct tmdc *tmdc = gameport_get_drvdata(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	unsigned char r, bad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	tmdc->reads++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		bad = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			if (r & (1 << i) & tmdc->exists) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				if (tmdc_parse_packet(tmdc->port[i], data[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 					bad = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			}
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	tmdc->bads += bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int tmdc_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct tmdc *tmdc = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	gameport_start_polling(tmdc->gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static void tmdc_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct tmdc *tmdc = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	gameport_stop_polling(tmdc->gameport);
^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 int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	const struct tmdc_model *model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct tmdc_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int i, j, b = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (!port || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	port->mode = data[TMDC_BYTE_ID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	for (model = tmdc_models; model->id && model->id != port->mode; model++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		/* empty */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	port->abs = model->axes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	port->btn = model->buttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!model->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		port->absc = data[TMDC_BYTE_DEF] >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		port->absc = model->abs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			port->btnc[i] = model->btnc[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		port->btno[i] = model->btno[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	snprintf(port->name, sizeof(port->name), model->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	port->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	input_dev->name = port->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	input_dev->phys = port->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	input_dev->id.bustype = BUS_GAMEPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	input_dev->id.product = model->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	input_dev->dev.parent = &tmdc->gameport->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	input_set_drvdata(input_dev, tmdc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	input_dev->open = tmdc_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	input_dev->close = tmdc_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	for (i = 0; i < port->absc && i < TMDC_ABS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (port->abs[i] >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			set_bit(port->btn[j + b], input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		b += port->btnc[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	err = input_register_device(port->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  fail:	input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	kfree(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * tmdc_probe() probes for ThrustMaster type joysticks.
^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) static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	unsigned char data[2][TMDC_MAX_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct tmdc *tmdc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	tmdc->gameport = gameport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	gameport_set_drvdata(gameport, tmdc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		goto fail2;
^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) 	gameport_set_poll_handler(gameport, tmdc_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	gameport_set_poll_interval(gameport, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		if (tmdc->exists & (1 << i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			err = tmdc_setup_port(tmdc, i, data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 				goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  fail3: while (--i >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		if (tmdc->port[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			input_unregister_device(tmdc->port[i]->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			kfree(tmdc->port[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  fail2:	gameport_close(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  fail1:	gameport_set_drvdata(gameport, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	kfree(tmdc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static void tmdc_disconnect(struct gameport *gameport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	struct tmdc *tmdc = gameport_get_drvdata(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (tmdc->port[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			input_unregister_device(tmdc->port[i]->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			kfree(tmdc->port[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	gameport_close(gameport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	gameport_set_drvdata(gameport, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	kfree(tmdc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static struct gameport_driver tmdc_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		.name	= "tmdc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.description	= DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	.connect	= tmdc_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	.disconnect	= tmdc_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) module_gameport_driver(tmdc_drv);