Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / joystick / guillemot.c
blob43ff817d80aca09c183f42b7ee22cac74be106a0
1 /*
2 * Copyright (c) 2001 Vojtech Pavlik
3 */
5 /*
6 * Guillemot Digital Interface Protocol driver for Linux
7 */
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/gameport.h>
30 #include <linux/input.h>
31 #include <linux/jiffies.h>
33 #define DRIVER_DESC "Guillemot Digital joystick driver"
35 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
36 MODULE_DESCRIPTION(DRIVER_DESC);
37 MODULE_LICENSE("GPL");
39 #define GUILLEMOT_MAX_START 600 /* 600 us */
40 #define GUILLEMOT_MAX_STROBE 60 /* 60 us */
41 #define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */
43 static short guillemot_abs_pad[] =
44 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
46 static short guillemot_btn_pad[] =
47 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
49 static struct {
50 int x;
51 int y;
52 } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
54 struct guillemot_type {
55 unsigned char id;
56 short *abs;
57 short *btn;
58 int hat;
59 char *name;
62 struct guillemot {
63 struct gameport *gameport;
64 struct input_dev *dev;
65 int bads;
66 int reads;
67 struct guillemot_type *type;
68 unsigned char length;
69 char phys[32];
72 static struct guillemot_type guillemot_type[] = {
73 { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
74 { 0 }};
77 * guillemot_read_packet() reads Guillemot joystick data.
80 static int guillemot_read_packet(struct gameport *gameport, u8 *data)
82 unsigned long flags;
83 unsigned char u, v;
84 unsigned int t, s;
85 int i;
87 for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
88 data[i] = 0;
90 i = 0;
91 t = gameport_time(gameport, GUILLEMOT_MAX_START);
92 s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
94 local_irq_save(flags);
95 gameport_trigger(gameport);
96 v = gameport_read(gameport);
98 while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
99 t--;
100 u = v; v = gameport_read(gameport);
101 if (v & ~u & 0x10) {
102 data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
103 i++;
104 t = s;
108 local_irq_restore(flags);
110 return i;
114 * guillemot_poll() reads and analyzes Guillemot joystick data.
117 static void guillemot_poll(struct gameport *gameport)
119 struct guillemot *guillemot = gameport_get_drvdata(gameport);
120 struct input_dev *dev = guillemot->dev;
121 u8 data[GUILLEMOT_MAX_LENGTH];
122 int i;
124 guillemot->reads++;
126 if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
127 data[0] != 0x55 || data[16] != 0xaa) {
128 guillemot->bads++;
129 } else {
131 for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
132 input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
134 if (guillemot->type->hat) {
135 input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
136 input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
139 for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
140 input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
143 input_sync(dev);
147 * guillemot_open() is a callback from the input open routine.
150 static int guillemot_open(struct input_dev *dev)
152 struct guillemot *guillemot = input_get_drvdata(dev);
154 gameport_start_polling(guillemot->gameport);
155 return 0;
159 * guillemot_close() is a callback from the input close routine.
162 static void guillemot_close(struct input_dev *dev)
164 struct guillemot *guillemot = input_get_drvdata(dev);
166 gameport_stop_polling(guillemot->gameport);
170 * guillemot_connect() probes for Guillemot joysticks.
173 static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
175 struct guillemot *guillemot;
176 struct input_dev *input_dev;
177 u8 data[GUILLEMOT_MAX_LENGTH];
178 int i, t;
179 int err;
181 guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
182 input_dev = input_allocate_device();
183 if (!guillemot || !input_dev) {
184 err = -ENOMEM;
185 goto fail1;
188 guillemot->gameport = gameport;
189 guillemot->dev = input_dev;
191 gameport_set_drvdata(gameport, guillemot);
193 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
194 if (err)
195 goto fail1;
197 i = guillemot_read_packet(gameport, data);
199 if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
200 err = -ENODEV;
201 goto fail2;
204 for (i = 0; guillemot_type[i].name; i++)
205 if (guillemot_type[i].id == data[11])
206 break;
208 if (!guillemot_type[i].name) {
209 printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
210 gameport->phys, data[12], data[13], data[11], data[14], data[15]);
211 err = -ENODEV;
212 goto fail2;
215 gameport_set_poll_handler(gameport, guillemot_poll);
216 gameport_set_poll_interval(gameport, 20);
218 snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
219 guillemot->type = guillemot_type + i;
221 input_dev->name = guillemot_type[i].name;
222 input_dev->phys = guillemot->phys;
223 input_dev->id.bustype = BUS_GAMEPORT;
224 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
225 input_dev->id.product = guillemot_type[i].id;
226 input_dev->id.version = (int)data[14] << 8 | data[15];
227 input_dev->dev.parent = &gameport->dev;
229 input_set_drvdata(input_dev, guillemot);
231 input_dev->open = guillemot_open;
232 input_dev->close = guillemot_close;
234 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
236 for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
237 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
239 if (guillemot->type->hat) {
240 input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
241 input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
244 for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
245 set_bit(t, input_dev->keybit);
247 err = input_register_device(guillemot->dev);
248 if (err)
249 goto fail2;
251 return 0;
253 fail2: gameport_close(gameport);
254 fail1: gameport_set_drvdata(gameport, NULL);
255 input_free_device(input_dev);
256 kfree(guillemot);
257 return err;
260 static void guillemot_disconnect(struct gameport *gameport)
262 struct guillemot *guillemot = gameport_get_drvdata(gameport);
264 printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
265 input_unregister_device(guillemot->dev);
266 gameport_close(gameport);
267 kfree(guillemot);
270 static struct gameport_driver guillemot_drv = {
271 .driver = {
272 .name = "guillemot",
274 .description = DRIVER_DESC,
275 .connect = guillemot_connect,
276 .disconnect = guillemot_disconnect,
279 module_gameport_driver(guillemot_drv);