blob: c528473c09d8945448791dad332c8a52b28793d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: guillemot.c,v 1.10 2002/01/22 20:28:12 vojtech Exp $
3 *
4 * Copyright (c) 2001 Vojtech Pavlik
5 */
6
7/*
8 * Guillemot Digital Interface Protocol driver for Linux
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 */
30
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/module.h>
34#include <linux/delay.h>
35#include <linux/init.h>
36#include <linux/gameport.h>
37#include <linux/input.h>
38
39#define DRIVER_DESC "Guillemot Digital joystick driver"
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44
45#define GUILLEMOT_MAX_START 600 /* 600 us */
46#define GUILLEMOT_MAX_STROBE 60 /* 60 us */
47#define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */
48
49static short guillemot_abs_pad[] =
50 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
51
52static short guillemot_btn_pad[] =
53 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
54
55static struct {
56 int x;
57 int y;
58} guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
59
60struct guillemot_type {
61 unsigned char id;
62 short *abs;
63 short *btn;
64 int hat;
65 char *name;
66};
67
68struct guillemot {
69 struct gameport *gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050070 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int bads;
72 int reads;
73 struct guillemot_type *type;
74 unsigned char length;
75 char phys[32];
76};
77
78static struct guillemot_type guillemot_type[] = {
79 { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
80 { 0 }};
81
82/*
83 * guillemot_read_packet() reads Guillemot joystick data.
84 */
85
86static int guillemot_read_packet(struct gameport *gameport, u8 *data)
87{
88 unsigned long flags;
89 unsigned char u, v;
90 unsigned int t, s;
91 int i;
92
93 for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
94 data[i] = 0;
95
96 i = 0;
97 t = gameport_time(gameport, GUILLEMOT_MAX_START);
98 s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
99
100 local_irq_save(flags);
101 gameport_trigger(gameport);
102 v = gameport_read(gameport);
103
104 while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
105 t--;
106 u = v; v = gameport_read(gameport);
107 if (v & ~u & 0x10) {
108 data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
109 i++;
110 t = s;
111 }
112 }
113
114 local_irq_restore(flags);
115
116 return i;
117}
118
119/*
120 * guillemot_poll() reads and analyzes Guillemot joystick data.
121 */
122
123static void guillemot_poll(struct gameport *gameport)
124{
125 struct guillemot *guillemot = gameport_get_drvdata(gameport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500126 struct input_dev *dev = guillemot->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 u8 data[GUILLEMOT_MAX_LENGTH];
128 int i;
129
130 guillemot->reads++;
131
132 if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
133 data[0] != 0x55 || data[16] != 0xaa) {
134 guillemot->bads++;
135 } else {
136
137 for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
138 input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
139
140 if (guillemot->type->hat) {
141 input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
142 input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
143 }
144
145 for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
146 input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
147 }
148
149 input_sync(dev);
150}
151
152/*
153 * guillemot_open() is a callback from the input open routine.
154 */
155
156static int guillemot_open(struct input_dev *dev)
157{
158 struct guillemot *guillemot = dev->private;
159
160 gameport_start_polling(guillemot->gameport);
161 return 0;
162}
163
164/*
165 * guillemot_close() is a callback from the input close routine.
166 */
167
168static void guillemot_close(struct input_dev *dev)
169{
170 struct guillemot *guillemot = dev->private;
171
172 gameport_stop_polling(guillemot->gameport);
173}
174
175/*
176 * guillemot_connect() probes for Guillemot joysticks.
177 */
178
179static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
180{
181 struct guillemot *guillemot;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500182 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 u8 data[GUILLEMOT_MAX_LENGTH];
184 int i, t;
185 int err;
186
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500187 guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
188 input_dev = input_allocate_device();
189 if (!guillemot || !input_dev) {
190 err = -ENOMEM;
191 goto fail1;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 guillemot->gameport = gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500195 guillemot->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 gameport_set_drvdata(gameport, guillemot);
198
199 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
200 if (err)
201 goto fail1;
202
203 i = guillemot_read_packet(gameport, data);
204
205 if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
206 err = -ENODEV;
207 goto fail2;
208 }
209
210 for (i = 0; guillemot_type[i].name; i++)
211 if (guillemot_type[i].id == data[11])
212 break;
213
214 if (!guillemot_type[i].name) {
215 printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
216 gameport->phys, data[12], data[13], data[11], data[14], data[15]);
217 err = -ENODEV;
218 goto fail2;
219 }
220
221 gameport_set_poll_handler(gameport, guillemot_poll);
222 gameport_set_poll_interval(gameport, 20);
223
224 sprintf(guillemot->phys, "%s/input0", gameport->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 guillemot->type = guillemot_type + i;
226
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500227 input_dev->name = guillemot_type[i].name;
228 input_dev->phys = guillemot->phys;
229 input_dev->id.bustype = BUS_GAMEPORT;
230 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
231 input_dev->id.product = guillemot_type[i].id;
232 input_dev->id.version = (int)data[14] << 8 | data[15];
233 input_dev->cdev.dev = &gameport->dev;
234 input_dev->private = guillemot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500236 input_dev->open = guillemot_open;
237 input_dev->close = guillemot_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500239 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500242 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (guillemot->type->hat) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500245 input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
246 input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500250 set_bit(t, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500252 input_register_device(guillemot->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return 0;
255
256fail2: gameport_close(gameport);
257fail1: gameport_set_drvdata(gameport, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500258 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kfree(guillemot);
260 return err;
261}
262
263static void guillemot_disconnect(struct gameport *gameport)
264{
265 struct guillemot *guillemot = gameport_get_drvdata(gameport);
266
267 printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500268 input_unregister_device(guillemot->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 gameport_close(gameport);
270 kfree(guillemot);
271}
272
273static struct gameport_driver guillemot_drv = {
274 .driver = {
275 .name = "guillemot",
276 },
277 .description = DRIVER_DESC,
278 .connect = guillemot_connect,
279 .disconnect = guillemot_disconnect,
280};
281
282static int __init guillemot_init(void)
283{
284 gameport_register_driver(&guillemot_drv);
285 return 0;
286}
287
288static void __exit guillemot_exit(void)
289{
290 gameport_unregister_driver(&guillemot_drv);
291}
292
293module_init(guillemot_init);
294module_exit(guillemot_exit);