xref: /linux/drivers/input/joystick/guillemot.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2001 Vojtech Pavlik
4  */
5 
6 /*
7  * Guillemot Digital Interface Protocol driver for Linux
8  */
9 
10 /*
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/gameport.h>
18 #include <linux/input.h>
19 #include <linux/jiffies.h>
20 
21 #define DRIVER_DESC	"Guillemot Digital joystick driver"
22 
23 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
24 MODULE_DESCRIPTION(DRIVER_DESC);
25 MODULE_LICENSE("GPL");
26 
27 #define GUILLEMOT_MAX_START	600	/* 600 us */
28 #define GUILLEMOT_MAX_STROBE	60	/* 60 us */
29 #define GUILLEMOT_MAX_LENGTH	17	/* 17 bytes */
30 
31 static short guillemot_abs_pad[] =
32 	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
33 
34 static short guillemot_btn_pad[] =
35 	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
36 
37 static struct {
38         int x;
39         int y;
40 } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
41 
42 struct guillemot_type {
43 	unsigned char id;
44 	short *abs;
45 	short *btn;
46 	int hat;
47 	char *name;
48 };
49 
50 struct guillemot {
51 	struct gameport *gameport;
52 	struct input_dev *dev;
53 	int bads;
54 	int reads;
55 	struct guillemot_type *type;
56 	unsigned char length;
57 	char phys[32];
58 };
59 
60 static struct guillemot_type guillemot_type[] = {
61 	{ 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
62 	{ 0 }};
63 
64 /*
65  * guillemot_read_packet() reads Guillemot joystick data.
66  */
67 
68 static int guillemot_read_packet(struct gameport *gameport, u8 *data)
69 {
70 	unsigned long flags;
71 	unsigned char u, v;
72 	unsigned int t, s;
73 	int i;
74 
75 	for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
76 		data[i] = 0;
77 
78 	i = 0;
79 	t = gameport_time(gameport, GUILLEMOT_MAX_START);
80 	s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
81 
82 	local_irq_save(flags);
83 	gameport_trigger(gameport);
84 	v = gameport_read(gameport);
85 
86 	while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
87 		t--;
88 		u = v; v = gameport_read(gameport);
89 		if (v & ~u & 0x10) {
90 			data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
91 			i++;
92 			t = s;
93 		}
94 	}
95 
96 	local_irq_restore(flags);
97 
98 	return i;
99 }
100 
101 /*
102  * guillemot_poll() reads and analyzes Guillemot joystick data.
103  */
104 
105 static void guillemot_poll(struct gameport *gameport)
106 {
107 	struct guillemot *guillemot = gameport_get_drvdata(gameport);
108 	struct input_dev *dev = guillemot->dev;
109 	u8 data[GUILLEMOT_MAX_LENGTH];
110 	int i;
111 
112 	guillemot->reads++;
113 
114 	if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
115 		data[0] != 0x55 || data[16] != 0xaa) {
116 		guillemot->bads++;
117 	} else {
118 
119 		for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
120 			input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
121 
122 		if (guillemot->type->hat) {
123 			input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
124 			input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
125 		}
126 
127 		for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
128 			input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
129 	}
130 
131 	input_sync(dev);
132 }
133 
134 /*
135  * guillemot_open() is a callback from the input open routine.
136  */
137 
138 static int guillemot_open(struct input_dev *dev)
139 {
140 	struct guillemot *guillemot = input_get_drvdata(dev);
141 
142 	gameport_start_polling(guillemot->gameport);
143 	return 0;
144 }
145 
146 /*
147  * guillemot_close() is a callback from the input close routine.
148  */
149 
150 static void guillemot_close(struct input_dev *dev)
151 {
152 	struct guillemot *guillemot = input_get_drvdata(dev);
153 
154 	gameport_stop_polling(guillemot->gameport);
155 }
156 
157 /*
158  * guillemot_connect() probes for Guillemot joysticks.
159  */
160 
161 static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
162 {
163 	struct guillemot *guillemot;
164 	struct input_dev *input_dev;
165 	u8 data[GUILLEMOT_MAX_LENGTH];
166 	int i, t;
167 	int err;
168 
169 	guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
170 	input_dev = input_allocate_device();
171 	if (!guillemot || !input_dev) {
172 		err = -ENOMEM;
173 		goto fail1;
174 	}
175 
176 	guillemot->gameport = gameport;
177 	guillemot->dev = input_dev;
178 
179 	gameport_set_drvdata(gameport, guillemot);
180 
181 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
182 	if (err)
183 		goto fail1;
184 
185 	i = guillemot_read_packet(gameport, data);
186 
187 	if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
188 		err = -ENODEV;
189 		goto fail2;
190 	}
191 
192 	for (i = 0; guillemot_type[i].name; i++)
193 		if (guillemot_type[i].id == data[11])
194 			break;
195 
196 	if (!guillemot_type[i].name) {
197 		printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
198 			gameport->phys, data[12], data[13], data[11], data[14], data[15]);
199 		err = -ENODEV;
200 		goto fail2;
201 	}
202 
203 	gameport_set_poll_handler(gameport, guillemot_poll);
204 	gameport_set_poll_interval(gameport, 20);
205 
206 	snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
207 	guillemot->type = guillemot_type + i;
208 
209 	input_dev->name = guillemot_type[i].name;
210 	input_dev->phys = guillemot->phys;
211 	input_dev->id.bustype = BUS_GAMEPORT;
212 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
213 	input_dev->id.product = guillemot_type[i].id;
214 	input_dev->id.version = (int)data[14] << 8 | data[15];
215 	input_dev->dev.parent = &gameport->dev;
216 
217 	input_set_drvdata(input_dev, guillemot);
218 
219 	input_dev->open = guillemot_open;
220 	input_dev->close = guillemot_close;
221 
222 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
223 
224 	for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
225 		input_set_abs_params(input_dev, t, 0, 255, 0, 0);
226 
227 	if (guillemot->type->hat) {
228 		input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
229 		input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
230 	}
231 
232 	for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
233 		set_bit(t, input_dev->keybit);
234 
235 	err = input_register_device(guillemot->dev);
236 	if (err)
237 		goto fail2;
238 
239 	return 0;
240 
241 fail2:	gameport_close(gameport);
242 fail1:  gameport_set_drvdata(gameport, NULL);
243 	input_free_device(input_dev);
244 	kfree(guillemot);
245 	return err;
246 }
247 
248 static void guillemot_disconnect(struct gameport *gameport)
249 {
250 	struct guillemot *guillemot = gameport_get_drvdata(gameport);
251 
252 	printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
253 	input_unregister_device(guillemot->dev);
254 	gameport_close(gameport);
255 	kfree(guillemot);
256 }
257 
258 static struct gameport_driver guillemot_drv = {
259 	.driver		= {
260 		.name	= "guillemot",
261 	},
262 	.description	= DRIVER_DESC,
263 	.connect	= guillemot_connect,
264 	.disconnect	= guillemot_disconnect,
265 };
266 
267 module_gameport_driver(guillemot_drv);
268