11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) 1998-2005 Vojtech Pavlik 31da177e4SLinus Torvalds */ 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds /* 61da177e4SLinus Torvalds * Logitech ADI joystick family driver for Linux 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds /* 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 111da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 121da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 131da177e4SLinus Torvalds * (at your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 161da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 171da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 181da177e4SLinus Torvalds * GNU General Public License for more details. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 211da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 221da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * Should you need to contact me, the author, you can do so either by 251da177e4SLinus Torvalds * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: 261da177e4SLinus Torvalds * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic 271da177e4SLinus Torvalds */ 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds #include <linux/delay.h> 301da177e4SLinus Torvalds #include <linux/kernel.h> 311da177e4SLinus Torvalds #include <linux/module.h> 321da177e4SLinus Torvalds #include <linux/string.h> 331da177e4SLinus Torvalds #include <linux/slab.h> 341da177e4SLinus Torvalds #include <linux/input.h> 351da177e4SLinus Torvalds #include <linux/gameport.h> 364e57b681STim Schmielau #include <linux/jiffies.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #define DRIVER_DESC "Logitech ADI joystick family driver" 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 411da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 421da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * Times, array sizes, flags, ids. 461da177e4SLinus Torvalds */ 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #define ADI_MAX_START 200 /* Trigger to packet timeout [200us] */ 491da177e4SLinus Torvalds #define ADI_MAX_STROBE 40 /* Single bit timeout [40us] */ 501da177e4SLinus Torvalds #define ADI_INIT_DELAY 10 /* Delay after init packet [10ms] */ 511da177e4SLinus Torvalds #define ADI_DATA_DELAY 4 /* Delay after data packet [4ms] */ 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #define ADI_MAX_LENGTH 256 541da177e4SLinus Torvalds #define ADI_MIN_LENGTH 8 551da177e4SLinus Torvalds #define ADI_MIN_LEN_LENGTH 10 561da177e4SLinus Torvalds #define ADI_MIN_ID_LENGTH 66 5717dd3f0fSDmitry Torokhov #define ADI_MAX_NAME_LENGTH 64 581da177e4SLinus Torvalds #define ADI_MAX_CNAME_LENGTH 16 591da177e4SLinus Torvalds #define ADI_MAX_PHYS_LENGTH 64 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #define ADI_FLAG_HAT 0x04 621da177e4SLinus Torvalds #define ADI_FLAG_10BIT 0x08 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #define ADI_ID_TPD 0x01 651da177e4SLinus Torvalds #define ADI_ID_WGP 0x06 661da177e4SLinus Torvalds #define ADI_ID_WGPE 0x08 671da177e4SLinus Torvalds #define ADI_ID_MAX 0x0a 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* 701da177e4SLinus Torvalds * Names, buttons, axes ... 711da177e4SLinus Torvalds */ 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds static char *adi_names[] = { "WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2", 741da177e4SLinus Torvalds "WingMan Interceptor", "WingMan Formula", "WingMan GamePad", 751da177e4SLinus Torvalds "WingMan Extreme Digital 3D", "WingMan GamePad Extreme", 761da177e4SLinus Torvalds "WingMan GamePad USB", "Unknown Device %#x" }; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds static char adi_wmgpe_abs[] = { ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y }; 791da177e4SLinus Torvalds static char adi_wmi_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y }; 801da177e4SLinus Torvalds static char adi_wmed3d_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y }; 811da177e4SLinus Torvalds static char adi_cm2_abs[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ }; 821da177e4SLinus Torvalds static char adi_wmf_abs[] = { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y }; 831da177e4SLinus Torvalds 841da177e4SLinus 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 }; 851da177e4SLinus Torvalds static short adi_wmi_key[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA }; 861da177e4SLinus Torvalds static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 }; 871da177e4SLinus Torvalds static short adi_cm2_key[] = { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 }; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs, 901da177e4SLinus Torvalds adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs }; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key, 931da177e4SLinus Torvalds adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key }; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /* 961da177e4SLinus Torvalds * Hat to axis conversion arrays. 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds static struct { 1001da177e4SLinus Torvalds int x; 1011da177e4SLinus Torvalds int y; 1021da177e4SLinus Torvalds } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * Per-port information. 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds struct adi { 10917dd3f0fSDmitry Torokhov struct input_dev *dev; 1101da177e4SLinus Torvalds int length; 1111da177e4SLinus Torvalds int ret; 1121da177e4SLinus Torvalds int idx; 1131da177e4SLinus Torvalds unsigned char id; 1141da177e4SLinus Torvalds char buttons; 1151da177e4SLinus Torvalds char axes10; 1161da177e4SLinus Torvalds char axes8; 1171da177e4SLinus Torvalds signed char pad; 1181da177e4SLinus Torvalds char hats; 1191da177e4SLinus Torvalds char *abs; 1201da177e4SLinus Torvalds short *key; 1211da177e4SLinus Torvalds char name[ADI_MAX_NAME_LENGTH]; 1221da177e4SLinus Torvalds char cname[ADI_MAX_CNAME_LENGTH]; 1231da177e4SLinus Torvalds char phys[ADI_MAX_PHYS_LENGTH]; 1241da177e4SLinus Torvalds unsigned char data[ADI_MAX_LENGTH]; 1251da177e4SLinus Torvalds }; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds struct adi_port { 1281da177e4SLinus Torvalds struct gameport *gameport; 1291da177e4SLinus Torvalds struct adi adi[2]; 1301da177e4SLinus Torvalds int bad; 1311da177e4SLinus Torvalds int reads; 1321da177e4SLinus Torvalds }; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * adi_read_packet() reads a Logitech ADI packet. 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds static void adi_read_packet(struct adi_port *port) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds struct adi *adi = port->adi; 1411da177e4SLinus Torvalds struct gameport *gameport = port->gameport; 1421da177e4SLinus Torvalds unsigned char u, v, w, x, z; 1431da177e4SLinus Torvalds int t[2], s[2], i; 1441da177e4SLinus Torvalds unsigned long flags; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 1471da177e4SLinus Torvalds adi[i].ret = -1; 1481da177e4SLinus Torvalds t[i] = gameport_time(gameport, ADI_MAX_START); 1491da177e4SLinus Torvalds s[i] = 0; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds local_irq_save(flags); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds gameport_trigger(gameport); 1551da177e4SLinus Torvalds v = z = gameport_read(gameport); 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds do { 1581da177e4SLinus Torvalds u = v; 1591da177e4SLinus Torvalds w = u ^ (v = x = gameport_read(gameport)); 1601da177e4SLinus Torvalds for (i = 0; i < 2; i++, w >>= 2, x >>= 2) { 1611da177e4SLinus Torvalds t[i]--; 1621da177e4SLinus Torvalds if ((w & 0x30) && s[i]) { 1631da177e4SLinus Torvalds if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) { 1641da177e4SLinus Torvalds adi[i].data[++adi[i].ret] = w; 1651da177e4SLinus Torvalds t[i] = gameport_time(gameport, ADI_MAX_STROBE); 1661da177e4SLinus Torvalds } else t[i] = 0; 1671da177e4SLinus Torvalds } else if (!(x & 0x30)) s[i] = 1; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds } while (t[0] > 0 || t[1] > 0); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds local_irq_restore(flags); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds return; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* 1771da177e4SLinus Torvalds * adi_move_bits() detects a possible 2-stream mode, and moves 1781da177e4SLinus Torvalds * the bits accordingly. 1791da177e4SLinus Torvalds */ 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static void adi_move_bits(struct adi_port *port, int length) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds int i; 1841da177e4SLinus Torvalds struct adi *adi = port->adi; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds adi[0].idx = adi[1].idx = 0; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds if (adi[0].ret <= 0 || adi[1].ret <= 0) return; 1891da177e4SLinus Torvalds if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds for (i = 1; i <= adi[1].ret; i++) 1921da177e4SLinus Torvalds adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds adi[0].ret += adi[1].ret; 1951da177e4SLinus Torvalds adi[1].ret = -1; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* 1991da177e4SLinus Torvalds * adi_get_bits() gathers bits from the data packet. 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static inline int adi_get_bits(struct adi *adi, int count) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds int bits = 0; 2051da177e4SLinus Torvalds int i; 2061da177e4SLinus Torvalds if ((adi->idx += count) > adi->ret) return 0; 2071da177e4SLinus Torvalds for (i = 0; i < count; i++) 2081da177e4SLinus Torvalds bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i; 2091da177e4SLinus Torvalds return bits; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* 2131da177e4SLinus Torvalds * adi_decode() decodes Logitech joystick data into input events. 2141da177e4SLinus Torvalds */ 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds static int adi_decode(struct adi *adi) 2171da177e4SLinus Torvalds { 21817dd3f0fSDmitry Torokhov struct input_dev *dev = adi->dev; 2191da177e4SLinus Torvalds char *abs = adi->abs; 2201da177e4SLinus Torvalds short *key = adi->key; 2211da177e4SLinus Torvalds int i, t; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4))) 2241da177e4SLinus Torvalds return -1; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds for (i = 0; i < adi->axes10; i++) 2271da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_get_bits(adi, 10)); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds for (i = 0; i < adi->axes8; i++) 2301da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_get_bits(adi, 8)); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds for (i = 0; i < adi->buttons && i < 63; i++) { 2331da177e4SLinus Torvalds if (i == adi->pad) { 2341da177e4SLinus Torvalds t = adi_get_bits(adi, 4); 2351da177e4SLinus Torvalds input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t & 1)); 2361da177e4SLinus Torvalds input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1)); 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds input_report_key(dev, *key++, adi_get_bits(adi, 1)); 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds for (i = 0; i < adi->hats; i++) { 2421da177e4SLinus Torvalds if ((t = adi_get_bits(adi, 4)) > 8) t = 0; 2431da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_hat_to_axis[t].x); 2441da177e4SLinus Torvalds input_report_abs(dev, *abs++, adi_hat_to_axis[t].y); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds for (i = 63; i < adi->buttons; i++) 2481da177e4SLinus Torvalds input_report_key(dev, *key++, adi_get_bits(adi, 1)); 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds input_sync(dev); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds return 0; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* 2561da177e4SLinus Torvalds * adi_read() reads the data packet and decodes it. 2571da177e4SLinus Torvalds */ 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static int adi_read(struct adi_port *port) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds int i; 2621da177e4SLinus Torvalds int result = 0; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds adi_read_packet(port); 2651da177e4SLinus Torvalds adi_move_bits(port, port->adi[0].length); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds for (i = 0; i < 2; i++) 2681da177e4SLinus Torvalds if (port->adi[i].length) 2691da177e4SLinus Torvalds result |= adi_decode(port->adi + i); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds return result; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* 2751da177e4SLinus Torvalds * adi_poll() repeatedly polls the Logitech joysticks. 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds static void adi_poll(struct gameport *gameport) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds struct adi_port *port = gameport_get_drvdata(gameport); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds port->bad -= adi_read(port); 2831da177e4SLinus Torvalds port->reads++; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* 2871da177e4SLinus Torvalds * adi_open() is a callback from the input open routine. 2881da177e4SLinus Torvalds */ 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static int adi_open(struct input_dev *dev) 2911da177e4SLinus Torvalds { 2928715c1cfSDmitry Torokhov struct adi_port *port = input_get_drvdata(dev); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds gameport_start_polling(port->gameport); 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* 2991da177e4SLinus Torvalds * adi_close() is a callback from the input close routine. 3001da177e4SLinus Torvalds */ 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds static void adi_close(struct input_dev *dev) 3031da177e4SLinus Torvalds { 3048715c1cfSDmitry Torokhov struct adi_port *port = input_get_drvdata(dev); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds gameport_stop_polling(port->gameport); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* 3101da177e4SLinus Torvalds * adi_init_digital() sends a trigger & delay sequence 3111da177e4SLinus Torvalds * to reset and initialize a Logitech joystick into digital mode. 3121da177e4SLinus Torvalds */ 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static void adi_init_digital(struct gameport *gameport) 3151da177e4SLinus Torvalds { 316*1bf21b33SColin Ian King static const int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 }; 3171da177e4SLinus Torvalds int i; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds for (i = 0; seq[i]; i++) { 3201da177e4SLinus Torvalds gameport_trigger(gameport); 32117dd3f0fSDmitry Torokhov if (seq[i] > 0) 32217dd3f0fSDmitry Torokhov msleep(seq[i]); 3231da177e4SLinus Torvalds if (seq[i] < 0) { 3241da177e4SLinus Torvalds mdelay(-seq[i]); 3251da177e4SLinus Torvalds udelay(-seq[i]*14); /* It looks like mdelay() is off by approx 1.4% */ 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds static void adi_id_decode(struct adi *adi, struct adi_port *port) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds int i, t; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */ 3351da177e4SLinus Torvalds return; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds if (adi->ret < (t = adi_get_bits(adi, 10))) { 3381da177e4SLinus Torvalds printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret); 3391da177e4SLinus Torvalds return; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds adi->length = adi_get_bits(adi, 10); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) { 3491da177e4SLinus Torvalds printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length); 3501da177e4SLinus Torvalds adi->length = 0; 3511da177e4SLinus Torvalds return; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds adi->axes8 = adi_get_bits(adi, 4); 3551da177e4SLinus Torvalds adi->buttons = adi_get_bits(adi, 6); 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds if (adi_get_bits(adi, 6) != 8 && adi->hats) { 3581da177e4SLinus Torvalds printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n"); 3591da177e4SLinus Torvalds adi->length = 0; 3601da177e4SLinus Torvalds return; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds adi->buttons += adi_get_bits(adi, 6); 3641da177e4SLinus Torvalds adi->hats += adi_get_bits(adi, 4); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds i = adi_get_bits(adi, 4); 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds if (t & ADI_FLAG_10BIT) { 3691da177e4SLinus Torvalds adi->axes10 = adi->axes8 - i; 3701da177e4SLinus Torvalds adi->axes8 = i; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds t = adi_get_bits(adi, 4); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds for (i = 0; i < t; i++) 3761da177e4SLinus Torvalds adi->cname[i] = adi_get_bits(adi, 8); 3771da177e4SLinus Torvalds adi->cname[i] = 0; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4; 3801da177e4SLinus Torvalds if (adi->length != t && adi->length != t + (t & 1)) { 3811da177e4SLinus Torvalds printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length); 3821da177e4SLinus Torvalds adi->length = 0; 3831da177e4SLinus Torvalds return; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds switch (adi->id) { 3871da177e4SLinus Torvalds case ADI_ID_TPD: 3881da177e4SLinus Torvalds adi->pad = 4; 3891da177e4SLinus Torvalds adi->buttons -= 4; 3901da177e4SLinus Torvalds break; 3911da177e4SLinus Torvalds case ADI_ID_WGP: 3921da177e4SLinus Torvalds adi->pad = 0; 3931da177e4SLinus Torvalds adi->buttons -= 4; 3941da177e4SLinus Torvalds break; 3951da177e4SLinus Torvalds default: 3961da177e4SLinus Torvalds adi->pad = -1; 3971da177e4SLinus Torvalds break; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 40117dd3f0fSDmitry Torokhov static int adi_init_input(struct adi *adi, struct adi_port *port, int half) 4021da177e4SLinus Torvalds { 40317dd3f0fSDmitry Torokhov struct input_dev *input_dev; 4041da177e4SLinus Torvalds char buf[ADI_MAX_NAME_LENGTH]; 40517dd3f0fSDmitry Torokhov int i, t; 4061da177e4SLinus Torvalds 40717dd3f0fSDmitry Torokhov adi->dev = input_dev = input_allocate_device(); 40817dd3f0fSDmitry Torokhov if (!input_dev) 40917dd3f0fSDmitry Torokhov return -ENOMEM; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id); 41417dd3f0fSDmitry Torokhov snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname); 4151da177e4SLinus Torvalds snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half); 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds adi->abs = adi_abs[t]; 4181da177e4SLinus Torvalds adi->key = adi_key[t]; 4191da177e4SLinus Torvalds 42017dd3f0fSDmitry Torokhov input_dev->name = adi->name; 42117dd3f0fSDmitry Torokhov input_dev->phys = adi->phys; 42217dd3f0fSDmitry Torokhov input_dev->id.bustype = BUS_GAMEPORT; 42317dd3f0fSDmitry Torokhov input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH; 42417dd3f0fSDmitry Torokhov input_dev->id.product = adi->id; 42517dd3f0fSDmitry Torokhov input_dev->id.version = 0x0100; 426935e658eSDmitry Torokhov input_dev->dev.parent = &port->gameport->dev; 4278715c1cfSDmitry Torokhov 4288715c1cfSDmitry Torokhov input_set_drvdata(input_dev, port); 4291da177e4SLinus Torvalds 43017dd3f0fSDmitry Torokhov input_dev->open = adi_open; 43117dd3f0fSDmitry Torokhov input_dev->close = adi_close; 4321da177e4SLinus Torvalds 4337b19ada2SJiri Slaby input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) 43617dd3f0fSDmitry Torokhov set_bit(adi->abs[i], input_dev->absbit); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds for (i = 0; i < adi->buttons; i++) 43917dd3f0fSDmitry Torokhov set_bit(adi->key[i], input_dev->keybit); 44017dd3f0fSDmitry Torokhov 44117dd3f0fSDmitry Torokhov return 0; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds static void adi_init_center(struct adi *adi) 4451da177e4SLinus Torvalds { 4461da177e4SLinus Torvalds int i, t, x; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (!adi->length) 4491da177e4SLinus Torvalds return; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) { 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds t = adi->abs[i]; 454987a6c02SDaniel Mack x = input_abs_get_val(adi->dev, t); 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE) 4571da177e4SLinus Torvalds x = i < adi->axes10 ? 512 : 128; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds if (i < adi->axes10) 46017dd3f0fSDmitry Torokhov input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16); 4611da177e4SLinus Torvalds else if (i < adi->axes10 + adi->axes8) 46217dd3f0fSDmitry Torokhov input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16); 4631da177e4SLinus Torvalds else 46417dd3f0fSDmitry Torokhov input_set_abs_params(adi->dev, t, -1, 1, 0, 0); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds /* 4691da177e4SLinus Torvalds * adi_connect() probes for Logitech ADI joysticks. 4701da177e4SLinus Torvalds */ 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds static int adi_connect(struct gameport *gameport, struct gameport_driver *drv) 4731da177e4SLinus Torvalds { 4741da177e4SLinus Torvalds struct adi_port *port; 4751da177e4SLinus Torvalds int i; 4761da177e4SLinus Torvalds int err; 4771da177e4SLinus Torvalds 47817dd3f0fSDmitry Torokhov port = kzalloc(sizeof(struct adi_port), GFP_KERNEL); 47917dd3f0fSDmitry Torokhov if (!port) 4801da177e4SLinus Torvalds return -ENOMEM; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds port->gameport = gameport; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds gameport_set_drvdata(gameport, port); 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); 48717dd3f0fSDmitry Torokhov if (err) 48817dd3f0fSDmitry Torokhov goto fail1; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds adi_init_digital(gameport); 4911da177e4SLinus Torvalds adi_read_packet(port); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH) 4941da177e4SLinus Torvalds adi_move_bits(port, adi_get_bits(port->adi, 10)); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 4971da177e4SLinus Torvalds adi_id_decode(port->adi + i, port); 49817dd3f0fSDmitry Torokhov 49917dd3f0fSDmitry Torokhov if (!port->adi[i].length) 50017dd3f0fSDmitry Torokhov continue; 50117dd3f0fSDmitry Torokhov 50217dd3f0fSDmitry Torokhov err = adi_init_input(port->adi + i, port, i); 50317dd3f0fSDmitry Torokhov if (err) 50417dd3f0fSDmitry Torokhov goto fail2; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (!port->adi[0].length && !port->adi[1].length) { 50817dd3f0fSDmitry Torokhov err = -ENODEV; 50917dd3f0fSDmitry Torokhov goto fail2; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds gameport_set_poll_handler(gameport, adi_poll); 5131da177e4SLinus Torvalds gameport_set_poll_interval(gameport, 20); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds msleep(ADI_INIT_DELAY); 5161da177e4SLinus Torvalds if (adi_read(port)) { 5171da177e4SLinus Torvalds msleep(ADI_DATA_DELAY); 5181da177e4SLinus Torvalds adi_read(port); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds for (i = 0; i < 2; i++) 5221da177e4SLinus Torvalds if (port->adi[i].length > 0) { 5231da177e4SLinus Torvalds adi_init_center(port->adi + i); 524127278ceSDmitry Torokhov err = input_register_device(port->adi[i].dev); 525127278ceSDmitry Torokhov if (err) 526127278ceSDmitry Torokhov goto fail3; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds return 0; 53017dd3f0fSDmitry Torokhov 531127278ceSDmitry Torokhov fail3: while (--i >= 0) { 532127278ceSDmitry Torokhov if (port->adi[i].length > 0) { 533127278ceSDmitry Torokhov input_unregister_device(port->adi[i].dev); 534127278ceSDmitry Torokhov port->adi[i].dev = NULL; 535127278ceSDmitry Torokhov } 536127278ceSDmitry Torokhov } 53717dd3f0fSDmitry Torokhov fail2: for (i = 0; i < 2; i++) 53817dd3f0fSDmitry Torokhov input_free_device(port->adi[i].dev); 53917dd3f0fSDmitry Torokhov gameport_close(gameport); 54017dd3f0fSDmitry Torokhov fail1: gameport_set_drvdata(gameport, NULL); 54117dd3f0fSDmitry Torokhov kfree(port); 54217dd3f0fSDmitry Torokhov return err; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds static void adi_disconnect(struct gameport *gameport) 5461da177e4SLinus Torvalds { 5471da177e4SLinus Torvalds int i; 5481da177e4SLinus Torvalds struct adi_port *port = gameport_get_drvdata(gameport); 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds for (i = 0; i < 2; i++) 5511da177e4SLinus Torvalds if (port->adi[i].length > 0) 55217dd3f0fSDmitry Torokhov input_unregister_device(port->adi[i].dev); 5531da177e4SLinus Torvalds gameport_close(gameport); 5541da177e4SLinus Torvalds gameport_set_drvdata(gameport, NULL); 5551da177e4SLinus Torvalds kfree(port); 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds static struct gameport_driver adi_drv = { 5591da177e4SLinus Torvalds .driver = { 5601da177e4SLinus Torvalds .name = "adi", 5611da177e4SLinus Torvalds }, 5621da177e4SLinus Torvalds .description = DRIVER_DESC, 5631da177e4SLinus Torvalds .connect = adi_connect, 5641da177e4SLinus Torvalds .disconnect = adi_disconnect, 5651da177e4SLinus Torvalds }; 5661da177e4SLinus Torvalds 56798a84131SAxel Lin module_gameport_driver(adi_drv); 568