1/*
2 * Copyright (c) 2001 Vojtech Pavlik
3 */
4
5/*
6 * Guillemot Digital Interface Protocol driver for Linux
7 */
8
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.
14 *
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.
19 *
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
23 */
24
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>
32
33#define DRIVER_DESC "Guillemot Digital joystick driver"
34
35MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
36MODULE_DESCRIPTION(DRIVER_DESC);
37MODULE_LICENSE("GPL");
38
39#define GUILLEMOT_MAX_START 600 /* 600 us */
40#define GUILLEMOT_MAX_STROBE 60 /* 60 us */
41#define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */
42
43static short guillemot_abs_pad[] =
44 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
45
46static 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 };
48
49static 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}};
53
54struct guillemot_type {
55 unsigned char id;
56 short *abs;
57 short *btn;
58 int hat;
59 char *name;
60};
61
62struct 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];
70};
71
72static struct guillemot_type guillemot_type[] = {
73 { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
74 { 0 }};
75
76/*
77 * guillemot_read_packet() reads Guillemot joystick data.
78 */
79
80static int guillemot_read_packet(struct gameport *gameport, u8 *data)
81{
82 unsigned long flags;
83 unsigned char u, v;
84 unsigned int t, s;
85 int i;
86
87 for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
88 data[i] = 0;
89
90 i = 0;
91 t = gameport_time(gameport, GUILLEMOT_MAX_START);
92 s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
93
94 local_irq_save(flags);
95 gameport_trigger(gameport);
96 v = gameport_read(gameport);
97
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;
105 }
106 }
107
108 local_irq_restore(flags);
109
110 return i;
111}
112
113/*
114 * guillemot_poll() reads and analyzes Guillemot joystick data.
115 */
116
117static void guillemot_poll(struct gameport *gameport)
118{
119 struct guillemot *guillemot = gameport_get_drvdata(gameport);
120 struct input_dev *dev = guillemot->dev;
121 u8 data[GUILLEMOT_MAX_LENGTH];
122 int i;
123
124 guillemot->reads++;
125
126 if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
127 data[0] != 0x55 || data[16] != 0xaa) {
128 guillemot->bads++;
129 } else {
130
131 for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
132 input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
133
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);
137 }
138
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);
141 }
142
143 input_sync(dev);
144}
145
146/*
147 * guillemot_open() is a callback from the input open routine.
148 */
149
150static int guillemot_open(struct input_dev *dev)
151{
152 struct guillemot *guillemot = input_get_drvdata(dev);
153
154 gameport_start_polling(guillemot->gameport);
155 return 0;
156}
157
158/*
159 * guillemot_close() is a callback from the input close routine.
160 */
161
162static void guillemot_close(struct input_dev *dev)
163{
164 struct guillemot *guillemot = input_get_drvdata(dev);
165
166 gameport_stop_polling(guillemot->gameport);
167}
168
169/*
170 * guillemot_connect() probes for Guillemot joysticks.
171 */
172
173static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
174{
175 struct guillemot *guillemot;
176 struct input_dev *input_dev;
177 u8 data[GUILLEMOT_MAX_LENGTH];
178 int i, t;
179 int err;
180
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;
186 }
187
188 guillemot->gameport = gameport;
189 guillemot->dev = input_dev;
190
191 gameport_set_drvdata(gameport, guillemot);
192
193 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
194 if (err)
195 goto fail1;
196
197 i = guillemot_read_packet(gameport, data);
198
199 if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
200 err = -ENODEV;
201 goto fail2;
202 }
203
204 for (i = 0; guillemot_type[i].name; i++)
205 if (guillemot_type[i].id == data[11])
206 break;
207
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;
213 }
214
215 gameport_set_poll_handler(gameport, guillemot_poll);
216 gameport_set_poll_interval(gameport, 20);
217
218 snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
219 guillemot->type = guillemot_type + i;
220
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;
228
229 input_set_drvdata(input_dev, guillemot);
230
231 input_dev->open = guillemot_open;
232 input_dev->close = guillemot_close;
233
234 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
235
236 for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
237 input_set_abs_params(input_dev, t, 0, 255, 0, 0);
238
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);
242 }
243
244 for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
245 set_bit(t, input_dev->keybit);
246
247 err = input_register_device(guillemot->dev);
248 if (err)
249 goto fail2;
250
251 return 0;
252
253fail2: gameport_close(gameport);
254fail1: gameport_set_drvdata(gameport, NULL);
255 input_free_device(input_dev);
256 kfree(guillemot);
257 return err;
258}
259
260static void guillemot_disconnect(struct gameport *gameport)
261{
262 struct guillemot *guillemot = gameport_get_drvdata(gameport);
263
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);
268}
269
270static struct gameport_driver guillemot_drv = {
271 .driver = {
272 .name = "guillemot",
273 },
274 .description = DRIVER_DESC,
275 .connect = guillemot_connect,
276 .disconnect = guillemot_disconnect,
277};
278
279module_gameport_driver(guillemot_drv);
280