patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / joystick / guillemot.c
1 /*
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 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Guillemot Digital joystick driver");
41 MODULE_LICENSE("GPL");
42
43 #define GUILLEMOT_MAX_START     600     /* 600 us */
44 #define GUILLEMOT_MAX_STROBE    60      /* 60 us */
45 #define GUILLEMOT_MAX_LENGTH    17      /* 17 bytes */
46 #define GUILLEMOT_REFRESH_TIME  HZ/50   /* 20 ms */
47
48 static short guillemot_abs_pad[] =
49         { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
50
51 static short guillemot_btn_pad[] =
52         { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
53
54 static struct {
55         int x;
56         int y;
57 } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
58
59 struct guillemot_type {
60         unsigned char id;
61         short *abs;
62         short *btn;
63         int hat;
64         char *name;
65 };
66
67 struct guillemot {
68         struct gameport *gameport;
69         struct input_dev dev;
70         struct timer_list timer;
71         int used;
72         int bads;
73         int reads;
74         struct guillemot_type *type;
75         unsigned char length;
76         char phys[32];
77 };
78
79 static struct guillemot_type guillemot_type[] = {
80         { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
81         { 0 }};
82
83 /*
84  * guillemot_read_packet() reads Guillemot joystick data.
85  */
86
87 static int guillemot_read_packet(struct gameport *gameport, u8 *data)
88 {
89         unsigned long flags;
90         unsigned char u, v;
91         unsigned int t, s;
92         int i;
93
94         for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
95                 data[i] = 0;
96
97         i = 0;
98         t = gameport_time(gameport, GUILLEMOT_MAX_START);
99         s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
100
101         local_irq_save(flags);
102         gameport_trigger(gameport);
103         v = gameport_read(gameport);
104
105         while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
106                 t--;
107                 u = v; v = gameport_read(gameport);
108                 if (v & ~u & 0x10) {
109                         data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
110                         i++;
111                         t = s;
112                 }
113         }
114
115         local_irq_restore(flags);
116
117         return i;
118 }
119
120 /*
121  * guillemot_timer() reads and analyzes Guillemot joystick data.
122  */
123
124 static void guillemot_timer(unsigned long private)
125 {
126         struct guillemot *guillemot = (struct guillemot *) private;
127         struct input_dev *dev = &guillemot->dev;
128         u8 data[GUILLEMOT_MAX_LENGTH];
129         int i;
130
131         guillemot->reads++;
132
133         if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
134                 data[0] != 0x55 || data[16] != 0xaa) {
135                 guillemot->bads++;
136         } else {
137
138                 for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
139                         input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
140
141                 if (guillemot->type->hat) {
142                         input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
143                         input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
144                 }
145
146                 for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
147                         input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
148         }
149
150         input_sync(dev);
151
152         mod_timer(&guillemot->timer, jiffies + GUILLEMOT_REFRESH_TIME);
153 }
154
155 /*
156  * guillemot_open() is a callback from the input open routine.
157  */
158
159 static int guillemot_open(struct input_dev *dev)
160 {
161         struct guillemot *guillemot = dev->private;
162         if (!guillemot->used++)
163                 mod_timer(&guillemot->timer, jiffies + GUILLEMOT_REFRESH_TIME);
164         return 0;
165 }
166
167 /*
168  * guillemot_close() is a callback from the input close routine.
169  */
170
171 static void guillemot_close(struct input_dev *dev)
172 {
173         struct guillemot *guillemot = dev->private;
174         if (!--guillemot->used)
175                 del_timer(&guillemot->timer);
176 }
177
178 /*
179  * guillemot_connect() probes for Guillemot joysticks.
180  */
181
182 static void guillemot_connect(struct gameport *gameport, struct gameport_dev *dev)
183 {
184         struct guillemot *guillemot;
185         u8 data[GUILLEMOT_MAX_LENGTH];
186         int i, t;
187
188         if (!(guillemot = kmalloc(sizeof(struct guillemot), GFP_KERNEL)))
189                 return;
190         memset(guillemot, 0, sizeof(struct guillemot));
191
192         gameport->private = guillemot;
193
194         guillemot->gameport = gameport;
195         init_timer(&guillemot->timer);
196         guillemot->timer.data = (long) guillemot;
197         guillemot->timer.function = guillemot_timer;
198
199         if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
200                 goto fail1;
201
202         i = guillemot_read_packet(gameport, data);
203
204         if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa)
205                 goto fail2;
206
207         for (i = 0; guillemot_type[i].name; i++)
208                 if (guillemot_type[i].id == data[11])
209                         break;
210
211         if (!guillemot_type[i].name) {
212                 printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
213                         gameport->phys, data[12], data[13], data[11], data[14], data[15]);
214                 goto fail2;
215         }
216
217         sprintf(guillemot->phys, "%s/input0", gameport->phys);
218
219         guillemot->type = guillemot_type + i;
220
221         guillemot->dev.private = guillemot;
222         guillemot->dev.open = guillemot_open;
223         guillemot->dev.close = guillemot_close;
224
225         guillemot->dev.name = guillemot_type[i].name;
226         guillemot->dev.phys = guillemot->phys;
227         guillemot->dev.id.bustype = BUS_GAMEPORT;
228         guillemot->dev.id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
229         guillemot->dev.id.product = guillemot_type[i].id;
230         guillemot->dev.id.version = (int)data[14] << 8 | data[15];
231
232         guillemot->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
233
234         for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++) {
235                 set_bit(t, guillemot->dev.absbit);
236                 guillemot->dev.absmin[t] = 0;
237                 guillemot->dev.absmax[t] = 255;
238         }
239
240         if (guillemot->type->hat)
241                 for (i = 0; i < 2; i++) {
242                         t = ABS_HAT0X + i;
243                         set_bit(t, guillemot->dev.absbit);
244                         guillemot->dev.absmin[t] = -1;
245                         guillemot->dev.absmax[t] = 1;
246                 }
247
248         for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
249                 set_bit(t, guillemot->dev.keybit);
250
251         input_register_device(&guillemot->dev);
252         printk(KERN_INFO "input: %s ver %d.%02d on %s\n",
253                 guillemot->type->name, data[14], data[15], gameport->phys);
254
255         return;
256 fail2:  gameport_close(gameport);
257 fail1:  kfree(guillemot);
258 }
259
260 static void guillemot_disconnect(struct gameport *gameport)
261 {
262         struct guillemot *guillemot = gameport->private;
263         printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
264         input_unregister_device(&guillemot->dev);
265         gameport_close(gameport);
266         kfree(guillemot);
267 }
268
269 static struct gameport_dev guillemot_dev = {
270         .connect =      guillemot_connect,
271         .disconnect =   guillemot_disconnect,
272 };
273
274 int __init guillemot_init(void)
275 {
276         gameport_register_device(&guillemot_dev);
277         return 0;
278 }
279
280 void __exit guillemot_exit(void)
281 {
282         gameport_unregister_device(&guillemot_dev);
283 }
284
285 module_init(guillemot_init);
286 module_exit(guillemot_exit);