11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Copyright (c) 1998-2005 Vojtech Pavlik
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds
61da177e4SLinus Torvalds /*
71da177e4SLinus Torvalds * Logitech ADI joystick family driver for Linux
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include <linux/delay.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/string.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/input.h>
161da177e4SLinus Torvalds #include <linux/gameport.h>
174e57b681STim Schmielau #include <linux/jiffies.h>
181da177e4SLinus Torvalds
191da177e4SLinus Torvalds #define DRIVER_DESC "Logitech ADI joystick family driver"
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
221da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
231da177e4SLinus Torvalds MODULE_LICENSE("GPL");
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds * Times, array sizes, flags, ids.
271da177e4SLinus Torvalds */
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #define ADI_MAX_START 200 /* Trigger to packet timeout [200us] */
301da177e4SLinus Torvalds #define ADI_MAX_STROBE 40 /* Single bit timeout [40us] */
311da177e4SLinus Torvalds #define ADI_INIT_DELAY 10 /* Delay after init packet [10ms] */
321da177e4SLinus Torvalds #define ADI_DATA_DELAY 4 /* Delay after data packet [4ms] */
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds #define ADI_MAX_LENGTH 256
351da177e4SLinus Torvalds #define ADI_MIN_LENGTH 8
361da177e4SLinus Torvalds #define ADI_MIN_LEN_LENGTH 10
371da177e4SLinus Torvalds #define ADI_MIN_ID_LENGTH 66
3817dd3f0fSDmitry Torokhov #define ADI_MAX_NAME_LENGTH 64
391da177e4SLinus Torvalds #define ADI_MAX_CNAME_LENGTH 16
401da177e4SLinus Torvalds #define ADI_MAX_PHYS_LENGTH 64
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds #define ADI_FLAG_HAT 0x04
431da177e4SLinus Torvalds #define ADI_FLAG_10BIT 0x08
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds #define ADI_ID_TPD 0x01
461da177e4SLinus Torvalds #define ADI_ID_WGP 0x06
471da177e4SLinus Torvalds #define ADI_ID_WGPE 0x08
481da177e4SLinus Torvalds #define ADI_ID_MAX 0x0a
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds * Names, buttons, axes ...
521da177e4SLinus Torvalds */
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds static char *adi_names[] = { "WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
551da177e4SLinus Torvalds "WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
561da177e4SLinus Torvalds "WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
571da177e4SLinus Torvalds "WingMan GamePad USB", "Unknown Device %#x" };
581da177e4SLinus Torvalds
591da177e4SLinus Torvalds static char adi_wmgpe_abs[] = { ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
601da177e4SLinus Torvalds static char adi_wmi_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
611da177e4SLinus Torvalds static char adi_wmed3d_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
621da177e4SLinus Torvalds static char adi_cm2_abs[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
631da177e4SLinus Torvalds static char adi_wmf_abs[] = { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
641da177e4SLinus Torvalds
651da177e4SLinus Torvalds static short adi_wmgpe_key[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT };
661da177e4SLinus Torvalds static short adi_wmi_key[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
671da177e4SLinus Torvalds static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
681da177e4SLinus Torvalds static short adi_cm2_key[] = { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
711da177e4SLinus Torvalds adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
721da177e4SLinus Torvalds
731da177e4SLinus Torvalds static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
741da177e4SLinus Torvalds adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds * Hat to axis conversion arrays.
781da177e4SLinus Torvalds */
791da177e4SLinus Torvalds
801da177e4SLinus Torvalds static struct {
811da177e4SLinus Torvalds int x;
821da177e4SLinus Torvalds int y;
831da177e4SLinus Torvalds } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
841da177e4SLinus Torvalds
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds * Per-port information.
871da177e4SLinus Torvalds */
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds struct adi {
9017dd3f0fSDmitry Torokhov struct input_dev *dev;
911da177e4SLinus Torvalds int length;
921da177e4SLinus Torvalds int ret;
931da177e4SLinus Torvalds int idx;
941da177e4SLinus Torvalds unsigned char id;
951da177e4SLinus Torvalds char buttons;
961da177e4SLinus Torvalds char axes10;
971da177e4SLinus Torvalds char axes8;
981da177e4SLinus Torvalds signed char pad;
991da177e4SLinus Torvalds char hats;
1001da177e4SLinus Torvalds char *abs;
1011da177e4SLinus Torvalds short *key;
1021da177e4SLinus Torvalds char name[ADI_MAX_NAME_LENGTH];
1031da177e4SLinus Torvalds char cname[ADI_MAX_CNAME_LENGTH];
1041da177e4SLinus Torvalds char phys[ADI_MAX_PHYS_LENGTH];
1051da177e4SLinus Torvalds unsigned char data[ADI_MAX_LENGTH];
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds struct adi_port {
1091da177e4SLinus Torvalds struct gameport *gameport;
1101da177e4SLinus Torvalds struct adi adi[2];
1111da177e4SLinus Torvalds int bad;
1121da177e4SLinus Torvalds int reads;
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds * adi_read_packet() reads a Logitech ADI packet.
1171da177e4SLinus Torvalds */
1181da177e4SLinus Torvalds
adi_read_packet(struct adi_port * port)1191da177e4SLinus Torvalds static void adi_read_packet(struct adi_port *port)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds struct adi *adi = port->adi;
1221da177e4SLinus Torvalds struct gameport *gameport = port->gameport;
123*a949087cSColin Ian King unsigned char u, v, w, x;
1241da177e4SLinus Torvalds int t[2], s[2], i;
1251da177e4SLinus Torvalds unsigned long flags;
1261da177e4SLinus Torvalds
1271da177e4SLinus Torvalds for (i = 0; i < 2; i++) {
1281da177e4SLinus Torvalds adi[i].ret = -1;
1291da177e4SLinus Torvalds t[i] = gameport_time(gameport, ADI_MAX_START);
1301da177e4SLinus Torvalds s[i] = 0;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds
1331da177e4SLinus Torvalds local_irq_save(flags);
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds gameport_trigger(gameport);
136*a949087cSColin Ian King v = gameport_read(gameport);
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds do {
1391da177e4SLinus Torvalds u = v;
1401da177e4SLinus Torvalds w = u ^ (v = x = gameport_read(gameport));
1411da177e4SLinus Torvalds for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
1421da177e4SLinus Torvalds t[i]--;
1431da177e4SLinus Torvalds if ((w & 0x30) && s[i]) {
1441da177e4SLinus Torvalds if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) {
1451da177e4SLinus Torvalds adi[i].data[++adi[i].ret] = w;
1461da177e4SLinus Torvalds t[i] = gameport_time(gameport, ADI_MAX_STROBE);
1471da177e4SLinus Torvalds } else t[i] = 0;
1481da177e4SLinus Torvalds } else if (!(x & 0x30)) s[i] = 1;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds } while (t[0] > 0 || t[1] > 0);
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvalds local_irq_restore(flags);
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds return;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds * adi_move_bits() detects a possible 2-stream mode, and moves
1591da177e4SLinus Torvalds * the bits accordingly.
1601da177e4SLinus Torvalds */
1611da177e4SLinus Torvalds
adi_move_bits(struct adi_port * port,int length)1621da177e4SLinus Torvalds static void adi_move_bits(struct adi_port *port, int length)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds int i;
1651da177e4SLinus Torvalds struct adi *adi = port->adi;
1661da177e4SLinus Torvalds
1671da177e4SLinus Torvalds adi[0].idx = adi[1].idx = 0;
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
1701da177e4SLinus Torvalds if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds for (i = 1; i <= adi[1].ret; i++)
1731da177e4SLinus Torvalds adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
1741da177e4SLinus Torvalds
1751da177e4SLinus Torvalds adi[0].ret += adi[1].ret;
1761da177e4SLinus Torvalds adi[1].ret = -1;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds /*
1801da177e4SLinus Torvalds * adi_get_bits() gathers bits from the data packet.
1811da177e4SLinus Torvalds */
1821da177e4SLinus Torvalds
adi_get_bits(struct adi * adi,int count)1831da177e4SLinus Torvalds static inline int adi_get_bits(struct adi *adi, int count)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds int bits = 0;
1861da177e4SLinus Torvalds int i;
1871da177e4SLinus Torvalds if ((adi->idx += count) > adi->ret) return 0;
1881da177e4SLinus Torvalds for (i = 0; i < count; i++)
1891da177e4SLinus Torvalds bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
1901da177e4SLinus Torvalds return bits;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds
1931da177e4SLinus Torvalds /*
1941da177e4SLinus Torvalds * adi_decode() decodes Logitech joystick data into input events.
1951da177e4SLinus Torvalds */
1961da177e4SLinus Torvalds
adi_decode(struct adi * adi)1971da177e4SLinus Torvalds static int adi_decode(struct adi *adi)
1981da177e4SLinus Torvalds {
19917dd3f0fSDmitry Torokhov struct input_dev *dev = adi->dev;
2001da177e4SLinus Torvalds char *abs = adi->abs;
2011da177e4SLinus Torvalds short *key = adi->key;
2021da177e4SLinus Torvalds int i, t;
2031da177e4SLinus Torvalds
2041da177e4SLinus Torvalds if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
2051da177e4SLinus Torvalds return -1;
2061da177e4SLinus Torvalds
2071da177e4SLinus Torvalds for (i = 0; i < adi->axes10; i++)
2081da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvalds for (i = 0; i < adi->axes8; i++)
2111da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds for (i = 0; i < adi->buttons && i < 63; i++) {
2141da177e4SLinus Torvalds if (i == adi->pad) {
2151da177e4SLinus Torvalds t = adi_get_bits(adi, 4);
2161da177e4SLinus Torvalds input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t & 1));
2171da177e4SLinus Torvalds input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds input_report_key(dev, *key++, adi_get_bits(adi, 1));
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds
2221da177e4SLinus Torvalds for (i = 0; i < adi->hats; i++) {
2231da177e4SLinus Torvalds if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
2241da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
2251da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_hat_to_axis[t].y);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvalds for (i = 63; i < adi->buttons; i++)
2291da177e4SLinus Torvalds input_report_key(dev, *key++, adi_get_bits(adi, 1));
2301da177e4SLinus Torvalds
2311da177e4SLinus Torvalds input_sync(dev);
2321da177e4SLinus Torvalds
2331da177e4SLinus Torvalds return 0;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds /*
2371da177e4SLinus Torvalds * adi_read() reads the data packet and decodes it.
2381da177e4SLinus Torvalds */
2391da177e4SLinus Torvalds
adi_read(struct adi_port * port)2401da177e4SLinus Torvalds static int adi_read(struct adi_port *port)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds int i;
2431da177e4SLinus Torvalds int result = 0;
2441da177e4SLinus Torvalds
2451da177e4SLinus Torvalds adi_read_packet(port);
2461da177e4SLinus Torvalds adi_move_bits(port, port->adi[0].length);
2471da177e4SLinus Torvalds
2481da177e4SLinus Torvalds for (i = 0; i < 2; i++)
2491da177e4SLinus Torvalds if (port->adi[i].length)
2501da177e4SLinus Torvalds result |= adi_decode(port->adi + i);
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds return result;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds
2551da177e4SLinus Torvalds /*
2561da177e4SLinus Torvalds * adi_poll() repeatedly polls the Logitech joysticks.
2571da177e4SLinus Torvalds */
2581da177e4SLinus Torvalds
adi_poll(struct gameport * gameport)2591da177e4SLinus Torvalds static void adi_poll(struct gameport *gameport)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds struct adi_port *port = gameport_get_drvdata(gameport);
2621da177e4SLinus Torvalds
2631da177e4SLinus Torvalds port->bad -= adi_read(port);
2641da177e4SLinus Torvalds port->reads++;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds
2671da177e4SLinus Torvalds /*
2681da177e4SLinus Torvalds * adi_open() is a callback from the input open routine.
2691da177e4SLinus Torvalds */
2701da177e4SLinus Torvalds
adi_open(struct input_dev * dev)2711da177e4SLinus Torvalds static int adi_open(struct input_dev *dev)
2721da177e4SLinus Torvalds {
2738715c1cfSDmitry Torokhov struct adi_port *port = input_get_drvdata(dev);
2741da177e4SLinus Torvalds
2751da177e4SLinus Torvalds gameport_start_polling(port->gameport);
2761da177e4SLinus Torvalds return 0;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds
2791da177e4SLinus Torvalds /*
2801da177e4SLinus Torvalds * adi_close() is a callback from the input close routine.
2811da177e4SLinus Torvalds */
2821da177e4SLinus Torvalds
adi_close(struct input_dev * dev)2831da177e4SLinus Torvalds static void adi_close(struct input_dev *dev)
2841da177e4SLinus Torvalds {
2858715c1cfSDmitry Torokhov struct adi_port *port = input_get_drvdata(dev);
2861da177e4SLinus Torvalds
2871da177e4SLinus Torvalds gameport_stop_polling(port->gameport);
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds
2901da177e4SLinus Torvalds /*
2911da177e4SLinus Torvalds * adi_init_digital() sends a trigger & delay sequence
2921da177e4SLinus Torvalds * to reset and initialize a Logitech joystick into digital mode.
2931da177e4SLinus Torvalds */
2941da177e4SLinus Torvalds
adi_init_digital(struct gameport * gameport)2951da177e4SLinus Torvalds static void adi_init_digital(struct gameport *gameport)
2961da177e4SLinus Torvalds {
2971bf21b33SColin Ian King static const int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
2981da177e4SLinus Torvalds int i;
2991da177e4SLinus Torvalds
3001da177e4SLinus Torvalds for (i = 0; seq[i]; i++) {
3011da177e4SLinus Torvalds gameport_trigger(gameport);
30217dd3f0fSDmitry Torokhov if (seq[i] > 0)
30317dd3f0fSDmitry Torokhov msleep(seq[i]);
3041da177e4SLinus Torvalds if (seq[i] < 0) {
3051da177e4SLinus Torvalds mdelay(-seq[i]);
3061da177e4SLinus Torvalds udelay(-seq[i]*14); /* It looks like mdelay() is off by approx 1.4% */
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds
adi_id_decode(struct adi * adi,struct adi_port * port)3111da177e4SLinus Torvalds static void adi_id_decode(struct adi *adi, struct adi_port *port)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds int i, t;
3141da177e4SLinus Torvalds
3151da177e4SLinus Torvalds if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
3161da177e4SLinus Torvalds return;
3171da177e4SLinus Torvalds
3181da177e4SLinus Torvalds if (adi->ret < (t = adi_get_bits(adi, 10))) {
3191da177e4SLinus Torvalds printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
3201da177e4SLinus Torvalds return;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds
3231da177e4SLinus Torvalds adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
3241da177e4SLinus Torvalds
3251da177e4SLinus Torvalds if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
3261da177e4SLinus Torvalds
3271da177e4SLinus Torvalds adi->length = adi_get_bits(adi, 10);
3281da177e4SLinus Torvalds
3291da177e4SLinus Torvalds if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
3301da177e4SLinus Torvalds printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
3311da177e4SLinus Torvalds adi->length = 0;
3321da177e4SLinus Torvalds return;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds
3351da177e4SLinus Torvalds adi->axes8 = adi_get_bits(adi, 4);
3361da177e4SLinus Torvalds adi->buttons = adi_get_bits(adi, 6);
3371da177e4SLinus Torvalds
3381da177e4SLinus Torvalds if (adi_get_bits(adi, 6) != 8 && adi->hats) {
3391da177e4SLinus Torvalds printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
3401da177e4SLinus Torvalds adi->length = 0;
3411da177e4SLinus Torvalds return;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds
3441da177e4SLinus Torvalds adi->buttons += adi_get_bits(adi, 6);
3451da177e4SLinus Torvalds adi->hats += adi_get_bits(adi, 4);
3461da177e4SLinus Torvalds
3471da177e4SLinus Torvalds i = adi_get_bits(adi, 4);
3481da177e4SLinus Torvalds
3491da177e4SLinus Torvalds if (t & ADI_FLAG_10BIT) {
3501da177e4SLinus Torvalds adi->axes10 = adi->axes8 - i;
3511da177e4SLinus Torvalds adi->axes8 = i;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds
3541da177e4SLinus Torvalds t = adi_get_bits(adi, 4);
3551da177e4SLinus Torvalds
3561da177e4SLinus Torvalds for (i = 0; i < t; i++)
3571da177e4SLinus Torvalds adi->cname[i] = adi_get_bits(adi, 8);
3581da177e4SLinus Torvalds adi->cname[i] = 0;
3591da177e4SLinus Torvalds
3601da177e4SLinus Torvalds t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
3611da177e4SLinus Torvalds if (adi->length != t && adi->length != t + (t & 1)) {
3621da177e4SLinus Torvalds printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
3631da177e4SLinus Torvalds adi->length = 0;
3641da177e4SLinus Torvalds return;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds
3671da177e4SLinus Torvalds switch (adi->id) {
3681da177e4SLinus Torvalds case ADI_ID_TPD:
3691da177e4SLinus Torvalds adi->pad = 4;
3701da177e4SLinus Torvalds adi->buttons -= 4;
3711da177e4SLinus Torvalds break;
3721da177e4SLinus Torvalds case ADI_ID_WGP:
3731da177e4SLinus Torvalds adi->pad = 0;
3741da177e4SLinus Torvalds adi->buttons -= 4;
3751da177e4SLinus Torvalds break;
3761da177e4SLinus Torvalds default:
3771da177e4SLinus Torvalds adi->pad = -1;
3781da177e4SLinus Torvalds break;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds
adi_init_input(struct adi * adi,struct adi_port * port,int half)38217dd3f0fSDmitry Torokhov static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
3831da177e4SLinus Torvalds {
38417dd3f0fSDmitry Torokhov struct input_dev *input_dev;
3851da177e4SLinus Torvalds char buf[ADI_MAX_NAME_LENGTH];
38617dd3f0fSDmitry Torokhov int i, t;
3871da177e4SLinus Torvalds
38817dd3f0fSDmitry Torokhov adi->dev = input_dev = input_allocate_device();
38917dd3f0fSDmitry Torokhov if (!input_dev)
39017dd3f0fSDmitry Torokhov return -ENOMEM;
3911da177e4SLinus Torvalds
3921da177e4SLinus Torvalds t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
3931da177e4SLinus Torvalds
3941da177e4SLinus Torvalds snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
39517dd3f0fSDmitry Torokhov snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname);
3961da177e4SLinus Torvalds snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half);
3971da177e4SLinus Torvalds
3981da177e4SLinus Torvalds adi->abs = adi_abs[t];
3991da177e4SLinus Torvalds adi->key = adi_key[t];
4001da177e4SLinus Torvalds
40117dd3f0fSDmitry Torokhov input_dev->name = adi->name;
40217dd3f0fSDmitry Torokhov input_dev->phys = adi->phys;
40317dd3f0fSDmitry Torokhov input_dev->id.bustype = BUS_GAMEPORT;
40417dd3f0fSDmitry Torokhov input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH;
40517dd3f0fSDmitry Torokhov input_dev->id.product = adi->id;
40617dd3f0fSDmitry Torokhov input_dev->id.version = 0x0100;
407935e658eSDmitry Torokhov input_dev->dev.parent = &port->gameport->dev;
4088715c1cfSDmitry Torokhov
4098715c1cfSDmitry Torokhov input_set_drvdata(input_dev, port);
4101da177e4SLinus Torvalds
41117dd3f0fSDmitry Torokhov input_dev->open = adi_open;
41217dd3f0fSDmitry Torokhov input_dev->close = adi_close;
4131da177e4SLinus Torvalds
4147b19ada2SJiri Slaby input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
4151da177e4SLinus Torvalds
4161da177e4SLinus Torvalds for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
41717dd3f0fSDmitry Torokhov set_bit(adi->abs[i], input_dev->absbit);
4181da177e4SLinus Torvalds
4191da177e4SLinus Torvalds for (i = 0; i < adi->buttons; i++)
42017dd3f0fSDmitry Torokhov set_bit(adi->key[i], input_dev->keybit);
42117dd3f0fSDmitry Torokhov
42217dd3f0fSDmitry Torokhov return 0;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds
adi_init_center(struct adi * adi)4251da177e4SLinus Torvalds static void adi_init_center(struct adi *adi)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds int i, t, x;
4281da177e4SLinus Torvalds
4291da177e4SLinus Torvalds if (!adi->length)
4301da177e4SLinus Torvalds return;
4311da177e4SLinus Torvalds
4321da177e4SLinus Torvalds for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
4331da177e4SLinus Torvalds
4341da177e4SLinus Torvalds t = adi->abs[i];
435987a6c02SDaniel Mack x = input_abs_get_val(adi->dev, t);
4361da177e4SLinus Torvalds
4371da177e4SLinus Torvalds if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
4381da177e4SLinus Torvalds x = i < adi->axes10 ? 512 : 128;
4391da177e4SLinus Torvalds
4401da177e4SLinus Torvalds if (i < adi->axes10)
44117dd3f0fSDmitry Torokhov input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16);
4421da177e4SLinus Torvalds else if (i < adi->axes10 + adi->axes8)
44317dd3f0fSDmitry Torokhov input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16);
4441da177e4SLinus Torvalds else
44517dd3f0fSDmitry Torokhov input_set_abs_params(adi->dev, t, -1, 1, 0, 0);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds /*
4501da177e4SLinus Torvalds * adi_connect() probes for Logitech ADI joysticks.
4511da177e4SLinus Torvalds */
4521da177e4SLinus Torvalds
adi_connect(struct gameport * gameport,struct gameport_driver * drv)4531da177e4SLinus Torvalds static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds struct adi_port *port;
4561da177e4SLinus Torvalds int i;
4571da177e4SLinus Torvalds int err;
4581da177e4SLinus Torvalds
45917dd3f0fSDmitry Torokhov port = kzalloc(sizeof(struct adi_port), GFP_KERNEL);
46017dd3f0fSDmitry Torokhov if (!port)
4611da177e4SLinus Torvalds return -ENOMEM;
4621da177e4SLinus Torvalds
4631da177e4SLinus Torvalds port->gameport = gameport;
4641da177e4SLinus Torvalds
4651da177e4SLinus Torvalds gameport_set_drvdata(gameport, port);
4661da177e4SLinus Torvalds
4671da177e4SLinus Torvalds err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
46817dd3f0fSDmitry Torokhov if (err)
46917dd3f0fSDmitry Torokhov goto fail1;
4701da177e4SLinus Torvalds
4711da177e4SLinus Torvalds adi_init_digital(gameport);
4721da177e4SLinus Torvalds adi_read_packet(port);
4731da177e4SLinus Torvalds
4741da177e4SLinus Torvalds if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
4751da177e4SLinus Torvalds adi_move_bits(port, adi_get_bits(port->adi, 10));
4761da177e4SLinus Torvalds
4771da177e4SLinus Torvalds for (i = 0; i < 2; i++) {
4781da177e4SLinus Torvalds adi_id_decode(port->adi + i, port);
47917dd3f0fSDmitry Torokhov
48017dd3f0fSDmitry Torokhov if (!port->adi[i].length)
48117dd3f0fSDmitry Torokhov continue;
48217dd3f0fSDmitry Torokhov
48317dd3f0fSDmitry Torokhov err = adi_init_input(port->adi + i, port, i);
48417dd3f0fSDmitry Torokhov if (err)
48517dd3f0fSDmitry Torokhov goto fail2;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds
4881da177e4SLinus Torvalds if (!port->adi[0].length && !port->adi[1].length) {
48917dd3f0fSDmitry Torokhov err = -ENODEV;
49017dd3f0fSDmitry Torokhov goto fail2;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds
4931da177e4SLinus Torvalds gameport_set_poll_handler(gameport, adi_poll);
4941da177e4SLinus Torvalds gameport_set_poll_interval(gameport, 20);
4951da177e4SLinus Torvalds
4961da177e4SLinus Torvalds msleep(ADI_INIT_DELAY);
4971da177e4SLinus Torvalds if (adi_read(port)) {
4981da177e4SLinus Torvalds msleep(ADI_DATA_DELAY);
4991da177e4SLinus Torvalds adi_read(port);
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds
5021da177e4SLinus Torvalds for (i = 0; i < 2; i++)
5031da177e4SLinus Torvalds if (port->adi[i].length > 0) {
5041da177e4SLinus Torvalds adi_init_center(port->adi + i);
505127278ceSDmitry Torokhov err = input_register_device(port->adi[i].dev);
506127278ceSDmitry Torokhov if (err)
507127278ceSDmitry Torokhov goto fail3;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds
5101da177e4SLinus Torvalds return 0;
51117dd3f0fSDmitry Torokhov
512127278ceSDmitry Torokhov fail3: while (--i >= 0) {
513127278ceSDmitry Torokhov if (port->adi[i].length > 0) {
514127278ceSDmitry Torokhov input_unregister_device(port->adi[i].dev);
515127278ceSDmitry Torokhov port->adi[i].dev = NULL;
516127278ceSDmitry Torokhov }
517127278ceSDmitry Torokhov }
51817dd3f0fSDmitry Torokhov fail2: for (i = 0; i < 2; i++)
51917dd3f0fSDmitry Torokhov input_free_device(port->adi[i].dev);
52017dd3f0fSDmitry Torokhov gameport_close(gameport);
52117dd3f0fSDmitry Torokhov fail1: gameport_set_drvdata(gameport, NULL);
52217dd3f0fSDmitry Torokhov kfree(port);
52317dd3f0fSDmitry Torokhov return err;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds
adi_disconnect(struct gameport * gameport)5261da177e4SLinus Torvalds static void adi_disconnect(struct gameport *gameport)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds int i;
5291da177e4SLinus Torvalds struct adi_port *port = gameport_get_drvdata(gameport);
5301da177e4SLinus Torvalds
5311da177e4SLinus Torvalds for (i = 0; i < 2; i++)
5321da177e4SLinus Torvalds if (port->adi[i].length > 0)
53317dd3f0fSDmitry Torokhov input_unregister_device(port->adi[i].dev);
5341da177e4SLinus Torvalds gameport_close(gameport);
5351da177e4SLinus Torvalds gameport_set_drvdata(gameport, NULL);
5361da177e4SLinus Torvalds kfree(port);
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds
5391da177e4SLinus Torvalds static struct gameport_driver adi_drv = {
5401da177e4SLinus Torvalds .driver = {
5411da177e4SLinus Torvalds .name = "adi",
5421da177e4SLinus Torvalds },
5431da177e4SLinus Torvalds .description = DRIVER_DESC,
5441da177e4SLinus Torvalds .connect = adi_connect,
5451da177e4SLinus Torvalds .disconnect = adi_disconnect,
5461da177e4SLinus Torvalds };
5471da177e4SLinus Torvalds
54898a84131SAxel Lin module_gameport_driver(adi_drv);
549