dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / input / joystick / tmdc.c
blob6f4a01cfe79fac8ab8dfff229b1f03b94c331793
1 /*
2 * Copyright (c) 1998-2001 Vojtech Pavlik
4 * Based on the work of:
5 * Trystan Larey-Williams
6 */
8 /*
9 * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/delay.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/gameport.h>
33 #include <linux/input.h>
34 #include <linux/jiffies.h>
36 #define DRIVER_DESC "ThrustMaster DirectConnect joystick driver"
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39 MODULE_DESCRIPTION(DRIVER_DESC);
40 MODULE_LICENSE("GPL");
42 #define TMDC_MAX_START 600 /* 600 us */
43 #define TMDC_MAX_STROBE 60 /* 60 us */
44 #define TMDC_MAX_LENGTH 13
46 #define TMDC_MODE_M3DI 1
47 #define TMDC_MODE_3DRP 3
48 #define TMDC_MODE_AT 4
49 #define TMDC_MODE_FM 8
50 #define TMDC_MODE_FGP 163
52 #define TMDC_BYTE_ID 10
53 #define TMDC_BYTE_REV 11
54 #define TMDC_BYTE_DEF 12
56 #define TMDC_ABS 7
57 #define TMDC_ABS_HAT 4
58 #define TMDC_BTN 16
60 static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
61 static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
63 static const signed char tmdc_abs[TMDC_ABS] =
64 { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
65 static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
66 { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
67 static const signed char tmdc_abs_at[TMDC_ABS] =
68 { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
69 static const signed char tmdc_abs_fm[TMDC_ABS] =
70 { ABS_RX, ABS_RY, ABS_X, ABS_Y };
72 static const short tmdc_btn_pad[TMDC_BTN] =
73 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
74 static const short tmdc_btn_joy[TMDC_BTN] =
75 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
76 BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
77 static const short tmdc_btn_fm[TMDC_BTN] =
78 { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
79 static const short tmdc_btn_at[TMDC_BTN] =
80 { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
81 BTN_BASE3, BTN_BASE2, BTN_BASE };
83 static const struct {
84 int x;
85 int y;
86 } tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
88 static const struct tmdc_model {
89 unsigned char id;
90 const char *name;
91 char abs;
92 char hats;
93 char btnc[4];
94 char btno[4];
95 const signed char *axes;
96 const short *buttons;
97 } tmdc_models[] = {
98 { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
99 { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
100 { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
101 { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
102 { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
103 { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
107 struct tmdc_port {
108 struct input_dev *dev;
109 char name[64];
110 char phys[32];
111 int mode;
112 const signed char *abs;
113 const short *btn;
114 unsigned char absc;
115 unsigned char btnc[4];
116 unsigned char btno[4];
119 struct tmdc {
120 struct gameport *gameport;
121 struct tmdc_port *port[2];
122 #if 0
123 struct input_dev *dev[2];
124 char name[2][64];
125 char phys[2][32];
126 int mode[2];
127 signed char *abs[2];
128 short *btn[2];
129 unsigned char absc[2];
130 unsigned char btnc[2][4];
131 unsigned char btno[2][4];
132 #endif
133 int reads;
134 int bads;
135 unsigned char exists;
139 * tmdc_read_packet() reads a ThrustMaster packet.
142 static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
144 unsigned char u, v, w, x;
145 unsigned long flags;
146 int i[2], j[2], t[2], p, k;
148 p = gameport_time(gameport, TMDC_MAX_STROBE);
150 for (k = 0; k < 2; k++) {
151 t[k] = gameport_time(gameport, TMDC_MAX_START);
152 i[k] = j[k] = 0;
155 local_irq_save(flags);
156 gameport_trigger(gameport);
158 w = gameport_read(gameport) >> 4;
160 do {
161 x = w;
162 w = gameport_read(gameport) >> 4;
164 for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
165 if (~v & u & 2) {
166 if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
167 t[k] = p;
168 if (j[k] == 0) { /* Start bit */
169 if (~v & 1) t[k] = 0;
170 data[k][i[k]] = 0; j[k]++; continue;
172 if (j[k] == 9) { /* Stop bit */
173 if (v & 1) t[k] = 0;
174 j[k] = 0; i[k]++; continue;
176 data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */
178 t[k]--;
180 } while (t[0] > 0 || t[1] > 0);
182 local_irq_restore(flags);
184 return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
187 static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
189 int i, k, l;
191 if (data[TMDC_BYTE_ID] != port->mode)
192 return -1;
194 for (i = 0; i < port->absc; i++) {
195 if (port->abs[i] < 0)
196 return 0;
198 input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
201 switch (port->mode) {
203 case TMDC_MODE_M3DI:
205 i = tmdc_byte_d[0];
206 input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
207 input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i] & 1));
208 break;
210 case TMDC_MODE_AT:
212 i = tmdc_byte_a[3];
213 input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
214 input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
215 break;
219 for (k = l = 0; k < 4; k++) {
220 for (i = 0; i < port->btnc[k]; i++)
221 input_report_key(port->dev, port->btn[i + l],
222 ((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
223 l += port->btnc[k];
226 input_sync(port->dev);
228 return 0;
232 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
235 static void tmdc_poll(struct gameport *gameport)
237 unsigned char data[2][TMDC_MAX_LENGTH];
238 struct tmdc *tmdc = gameport_get_drvdata(gameport);
239 unsigned char r, bad = 0;
240 int i;
242 tmdc->reads++;
244 if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
245 bad = 1;
246 else {
247 for (i = 0; i < 2; i++) {
248 if (r & (1 << i) & tmdc->exists) {
250 if (tmdc_parse_packet(tmdc->port[i], data[i]))
251 bad = 1;
256 tmdc->bads += bad;
259 static int tmdc_open(struct input_dev *dev)
261 struct tmdc *tmdc = input_get_drvdata(dev);
263 gameport_start_polling(tmdc->gameport);
264 return 0;
267 static void tmdc_close(struct input_dev *dev)
269 struct tmdc *tmdc = input_get_drvdata(dev);
271 gameport_stop_polling(tmdc->gameport);
274 static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
276 const struct tmdc_model *model;
277 struct tmdc_port *port;
278 struct input_dev *input_dev;
279 int i, j, b = 0;
280 int err;
282 tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
283 input_dev = input_allocate_device();
284 if (!port || !input_dev) {
285 err = -ENOMEM;
286 goto fail;
289 port->mode = data[TMDC_BYTE_ID];
291 for (model = tmdc_models; model->id && model->id != port->mode; model++)
292 /* empty */;
294 port->abs = model->axes;
295 port->btn = model->buttons;
297 if (!model->id) {
298 port->absc = data[TMDC_BYTE_DEF] >> 4;
299 for (i = 0; i < 4; i++)
300 port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
301 } else {
302 port->absc = model->abs;
303 for (i = 0; i < 4; i++)
304 port->btnc[i] = model->btnc[i];
307 for (i = 0; i < 4; i++)
308 port->btno[i] = model->btno[i];
310 snprintf(port->name, sizeof(port->name), model->name,
311 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
312 snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
314 port->dev = input_dev;
316 input_dev->name = port->name;
317 input_dev->phys = port->phys;
318 input_dev->id.bustype = BUS_GAMEPORT;
319 input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
320 input_dev->id.product = model->id;
321 input_dev->id.version = 0x0100;
322 input_dev->dev.parent = &tmdc->gameport->dev;
324 input_set_drvdata(input_dev, tmdc);
326 input_dev->open = tmdc_open;
327 input_dev->close = tmdc_close;
329 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
331 for (i = 0; i < port->absc && i < TMDC_ABS; i++)
332 if (port->abs[i] >= 0)
333 input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
335 for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
336 input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
338 for (i = 0; i < 4; i++) {
339 for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
340 set_bit(port->btn[j + b], input_dev->keybit);
341 b += port->btnc[i];
344 err = input_register_device(port->dev);
345 if (err)
346 goto fail;
348 return 0;
350 fail: input_free_device(input_dev);
351 kfree(port);
352 return err;
356 * tmdc_probe() probes for ThrustMaster type joysticks.
359 static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
361 unsigned char data[2][TMDC_MAX_LENGTH];
362 struct tmdc *tmdc;
363 int i;
364 int err;
366 if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
367 return -ENOMEM;
369 tmdc->gameport = gameport;
371 gameport_set_drvdata(gameport, tmdc);
373 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
374 if (err)
375 goto fail1;
377 if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
378 err = -ENODEV;
379 goto fail2;
382 gameport_set_poll_handler(gameport, tmdc_poll);
383 gameport_set_poll_interval(gameport, 20);
385 for (i = 0; i < 2; i++) {
386 if (tmdc->exists & (1 << i)) {
388 err = tmdc_setup_port(tmdc, i, data[i]);
389 if (err)
390 goto fail3;
394 return 0;
396 fail3: while (--i >= 0) {
397 if (tmdc->port[i]) {
398 input_unregister_device(tmdc->port[i]->dev);
399 kfree(tmdc->port[i]);
402 fail2: gameport_close(gameport);
403 fail1: gameport_set_drvdata(gameport, NULL);
404 kfree(tmdc);
405 return err;
408 static void tmdc_disconnect(struct gameport *gameport)
410 struct tmdc *tmdc = gameport_get_drvdata(gameport);
411 int i;
413 for (i = 0; i < 2; i++) {
414 if (tmdc->port[i]) {
415 input_unregister_device(tmdc->port[i]->dev);
416 kfree(tmdc->port[i]);
419 gameport_close(gameport);
420 gameport_set_drvdata(gameport, NULL);
421 kfree(tmdc);
424 static struct gameport_driver tmdc_drv = {
425 .driver = {
426 .name = "tmdc",
427 .owner = THIS_MODULE,
429 .description = DRIVER_DESC,
430 .connect = tmdc_connect,
431 .disconnect = tmdc_disconnect,
434 module_gameport_driver(tmdc_drv);