1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2001 Vojtech Pavlik
7 * Guillemot Digital Interface Protocol driver for Linux
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/gameport.h>
15 #include <linux/input.h>
16 #include <linux/jiffies.h>
18 #define DRIVER_DESC "Guillemot Digital joystick driver"
20 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
21 MODULE_DESCRIPTION(DRIVER_DESC);
22 MODULE_LICENSE("GPL");
24 #define GUILLEMOT_MAX_START 600 /* 600 us */
25 #define GUILLEMOT_MAX_STROBE 60 /* 60 us */
26 #define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */
28 static short guillemot_abs_pad[] =
29 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
31 static short guillemot_btn_pad[] =
32 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
37 } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
39 struct guillemot_type {
48 struct gameport *gameport;
49 struct input_dev *dev;
52 struct guillemot_type *type;
57 static struct guillemot_type guillemot_type[] = {
58 { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
62 * guillemot_read_packet() reads Guillemot joystick data.
65 static int guillemot_read_packet(struct gameport *gameport, u8 *data)
72 for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
76 t = gameport_time(gameport, GUILLEMOT_MAX_START);
77 s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
79 local_irq_save(flags);
80 gameport_trigger(gameport);
81 v = gameport_read(gameport);
83 while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
85 u = v; v = gameport_read(gameport);
87 data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
93 local_irq_restore(flags);
99 * guillemot_poll() reads and analyzes Guillemot joystick data.
102 static void guillemot_poll(struct gameport *gameport)
104 struct guillemot *guillemot = gameport_get_drvdata(gameport);
105 struct input_dev *dev = guillemot->dev;
106 u8 data[GUILLEMOT_MAX_LENGTH];
111 if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
112 data[0] != 0x55 || data[16] != 0xaa) {
116 for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
117 input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
119 if (guillemot->type->hat) {
120 input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
121 input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
124 for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
125 input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
132 * guillemot_open() is a callback from the input open routine.
135 static int guillemot_open(struct input_dev *dev)
137 struct guillemot *guillemot = input_get_drvdata(dev);
139 gameport_start_polling(guillemot->gameport);
144 * guillemot_close() is a callback from the input close routine.
147 static void guillemot_close(struct input_dev *dev)
149 struct guillemot *guillemot = input_get_drvdata(dev);
151 gameport_stop_polling(guillemot->gameport);
155 * guillemot_connect() probes for Guillemot joysticks.
158 static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
160 struct guillemot *guillemot;
161 struct input_dev *input_dev;
162 u8 data[GUILLEMOT_MAX_LENGTH];
166 guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
167 input_dev = input_allocate_device();
168 if (!guillemot || !input_dev) {
173 guillemot->gameport = gameport;
174 guillemot->dev = input_dev;
176 gameport_set_drvdata(gameport, guillemot);
178 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
182 i = guillemot_read_packet(gameport, data);
184 if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
189 for (i = 0; guillemot_type[i].name; i++)
190 if (guillemot_type[i].id == data[11])
193 if (!guillemot_type[i].name) {
194 printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
195 gameport->phys, data[12], data[13], data[11], data[14], data[15]);
200 gameport_set_poll_handler(gameport, guillemot_poll);
201 gameport_set_poll_interval(gameport, 20);
203 snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
204 guillemot->type = guillemot_type + i;
206 input_dev->name = guillemot_type[i].name;
207 input_dev->phys = guillemot->phys;
208 input_dev->id.bustype = BUS_GAMEPORT;
209 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
210 input_dev->id.product = guillemot_type[i].id;
211 input_dev->id.version = (int)data[14] << 8 | data[15];
212 input_dev->dev.parent = &gameport->dev;
214 input_set_drvdata(input_dev, guillemot);
216 input_dev->open = guillemot_open;
217 input_dev->close = guillemot_close;
219 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
221 for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
222 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
224 if (guillemot->type->hat) {
225 input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
226 input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
229 for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
230 set_bit(t, input_dev->keybit);
232 err = input_register_device(guillemot->dev);
238 fail2: gameport_close(gameport);
239 fail1: gameport_set_drvdata(gameport, NULL);
240 input_free_device(input_dev);
245 static void guillemot_disconnect(struct gameport *gameport)
247 struct guillemot *guillemot = gameport_get_drvdata(gameport);
249 printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
250 input_unregister_device(guillemot->dev);
251 gameport_close(gameport);
255 static struct gameport_driver guillemot_drv = {
259 .description = DRIVER_DESC,
260 .connect = guillemot_connect,
261 .disconnect = guillemot_disconnect,
264 module_gameport_driver(guillemot_drv);