Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 1996-2001 Vojtech Pavlik
0004  */
0005 
0006 /*
0007  * Analog joystick and gamepad driver for Linux
0008  */
0009 
0010 /*
0011  */
0012 
0013 #include <linux/delay.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/slab.h>
0017 #include <linux/bitops.h>
0018 #include <linux/init.h>
0019 #include <linux/input.h>
0020 #include <linux/gameport.h>
0021 #include <linux/jiffies.h>
0022 #include <linux/seq_buf.h>
0023 #include <linux/timex.h>
0024 #include <linux/timekeeping.h>
0025 
0026 #define DRIVER_DESC "Analog joystick and gamepad driver"
0027 
0028 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0029 MODULE_DESCRIPTION(DRIVER_DESC);
0030 MODULE_LICENSE("GPL");
0031 
0032 /*
0033  * Option parsing.
0034  */
0035 
0036 #define ANALOG_PORTS        16
0037 
0038 static char *js[ANALOG_PORTS];
0039 static unsigned int js_nargs;
0040 static int analog_options[ANALOG_PORTS];
0041 module_param_array_named(map, js, charp, &js_nargs, 0);
0042 MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
0043 
0044 /*
0045  * Times, feature definitions.
0046  */
0047 
0048 #define ANALOG_RUDDER       0x00004
0049 #define ANALOG_THROTTLE     0x00008
0050 #define ANALOG_AXES_STD     0x0000f
0051 #define ANALOG_BTNS_STD     0x000f0
0052 
0053 #define ANALOG_BTNS_CHF     0x00100
0054 #define ANALOG_HAT1_CHF     0x00200
0055 #define ANALOG_HAT2_CHF     0x00400
0056 #define ANALOG_HAT_FCS      0x00800
0057 #define ANALOG_HATS_ALL     0x00e00
0058 #define ANALOG_BTN_TL       0x01000
0059 #define ANALOG_BTN_TR       0x02000
0060 #define ANALOG_BTN_TL2      0x04000
0061 #define ANALOG_BTN_TR2      0x08000
0062 #define ANALOG_BTNS_TLR     0x03000
0063 #define ANALOG_BTNS_TLR2    0x0c000
0064 #define ANALOG_BTNS_GAMEPAD 0x0f000
0065 
0066 #define ANALOG_HBTN_CHF     0x10000
0067 #define ANALOG_ANY_CHF      0x10700
0068 #define ANALOG_SAITEK       0x20000
0069 #define ANALOG_EXTENSIONS   0x7ff00
0070 #define ANALOG_GAMEPAD      0x80000
0071 
0072 #define ANALOG_MAX_TIME     3   /* 3 ms */
0073 #define ANALOG_LOOP_TIME    2000    /* 2 * loop */
0074 #define ANALOG_SAITEK_DELAY 200 /* 200 us */
0075 #define ANALOG_SAITEK_TIME  2000    /* 2000 us */
0076 #define ANALOG_AXIS_TIME    2   /* 2 * refresh */
0077 #define ANALOG_INIT_RETRIES 8   /* 8 times */
0078 #define ANALOG_FUZZ_BITS    2   /* 2 bit more */
0079 #define ANALOG_FUZZ_MAGIC   36  /* 36 u*ms/loop */
0080 
0081 #define ANALOG_MAX_NAME_LENGTH  128
0082 #define ANALOG_MAX_PHYS_LENGTH  32
0083 
0084 static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
0085 static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
0086 static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
0087 static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
0088 static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };
0089 static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
0090                   BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
0091 
0092 static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
0093 
0094 struct analog {
0095     struct input_dev *dev;
0096     int mask;
0097     short *buttons;
0098     char name[ANALOG_MAX_NAME_LENGTH];
0099     char phys[ANALOG_MAX_PHYS_LENGTH];
0100 };
0101 
0102 struct analog_port {
0103     struct gameport *gameport;
0104     struct analog analog[2];
0105     unsigned char mask;
0106     char saitek;
0107     char cooked;
0108     int bads;
0109     int reads;
0110     int loop;
0111     int fuzz;
0112     int axes[4];
0113     int buttons;
0114     int initial[4];
0115     int axtime;
0116 };
0117 
0118 /*
0119  * analog_decode() decodes analog joystick data and reports input events.
0120  */
0121 
0122 static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
0123 {
0124     struct input_dev *dev = analog->dev;
0125     int i, j;
0126 
0127     if (analog->mask & ANALOG_HAT_FCS)
0128         for (i = 0; i < 4; i++)
0129             if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
0130                 buttons |= 1 << (i + 14);
0131                 break;
0132             }
0133 
0134     for (i = j = 0; i < 6; i++)
0135         if (analog->mask & (0x10 << i))
0136             input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
0137 
0138     if (analog->mask & ANALOG_HBTN_CHF)
0139         for (i = 0; i < 4; i++)
0140             input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
0141 
0142     if (analog->mask & ANALOG_BTN_TL)
0143         input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
0144     if (analog->mask & ANALOG_BTN_TR)
0145         input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
0146     if (analog->mask & ANALOG_BTN_TL2)
0147         input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
0148     if (analog->mask & ANALOG_BTN_TR2)
0149         input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
0150 
0151     for (i = j = 0; i < 4; i++)
0152         if (analog->mask & (1 << i))
0153             input_report_abs(dev, analog_axes[j++], axes[i]);
0154 
0155     for (i = j = 0; i < 3; i++)
0156         if (analog->mask & analog_exts[i]) {
0157             input_report_abs(dev, analog_hats[j++],
0158                 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
0159             input_report_abs(dev, analog_hats[j++],
0160                 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
0161         }
0162 
0163     input_sync(dev);
0164 }
0165 
0166 /*
0167  * analog_cooked_read() reads analog joystick data.
0168  */
0169 
0170 static int analog_cooked_read(struct analog_port *port)
0171 {
0172     struct gameport *gameport = port->gameport;
0173     ktime_t time[4], start, loop, now;
0174     unsigned int loopout, timeout;
0175     unsigned char data[4], this, last;
0176     unsigned long flags;
0177     int i, j;
0178 
0179     loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
0180     timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
0181 
0182     local_irq_save(flags);
0183     gameport_trigger(gameport);
0184     now = ktime_get();
0185     local_irq_restore(flags);
0186 
0187     start = now;
0188     this = port->mask;
0189     i = 0;
0190 
0191     do {
0192         loop = now;
0193         last = this;
0194 
0195         local_irq_disable();
0196         this = gameport_read(gameport) & port->mask;
0197         now = ktime_get();
0198         local_irq_restore(flags);
0199 
0200         if ((last ^ this) && (ktime_sub(now, loop) < loopout)) {
0201             data[i] = last ^ this;
0202             time[i] = now;
0203             i++;
0204         }
0205 
0206     } while (this && (i < 4) && (ktime_sub(now, start) < timeout));
0207 
0208     this <<= 4;
0209 
0210     for (--i; i >= 0; i--) {
0211         this |= data[i];
0212         for (j = 0; j < 4; j++)
0213             if (data[i] & (1 << j))
0214                 port->axes[j] = ((u32)ktime_sub(time[i], start) << ANALOG_FUZZ_BITS) / port->loop;
0215     }
0216 
0217     return -(this != port->mask);
0218 }
0219 
0220 static int analog_button_read(struct analog_port *port, char saitek, char chf)
0221 {
0222     unsigned char u;
0223     int t = 1, i = 0;
0224     int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
0225 
0226     u = gameport_read(port->gameport);
0227 
0228     if (!chf) {
0229         port->buttons = (~u >> 4) & 0xf;
0230         return 0;
0231     }
0232 
0233     port->buttons = 0;
0234 
0235     while ((~u & 0xf0) && (i < 16) && t) {
0236         port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
0237         if (!saitek) return 0;
0238         udelay(ANALOG_SAITEK_DELAY);
0239         t = strobe;
0240         gameport_trigger(port->gameport);
0241         while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
0242         i++;
0243     }
0244 
0245     return -(!t || (i == 16));
0246 }
0247 
0248 /*
0249  * analog_poll() repeatedly polls the Analog joysticks.
0250  */
0251 
0252 static void analog_poll(struct gameport *gameport)
0253 {
0254     struct analog_port *port = gameport_get_drvdata(gameport);
0255     int i;
0256 
0257     char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
0258     char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
0259 
0260     if (port->cooked) {
0261         port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
0262         if (chf)
0263             port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
0264         port->reads++;
0265     } else {
0266         if (!port->axtime--) {
0267             port->bads -= analog_cooked_read(port);
0268             port->bads -= analog_button_read(port, saitek, chf);
0269             port->reads++;
0270             port->axtime = ANALOG_AXIS_TIME - 1;
0271         } else {
0272             if (!saitek)
0273                 analog_button_read(port, saitek, chf);
0274         }
0275     }
0276 
0277     for (i = 0; i < 2; i++)
0278         if (port->analog[i].mask)
0279             analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
0280 }
0281 
0282 /*
0283  * analog_open() is a callback from the input open routine.
0284  */
0285 
0286 static int analog_open(struct input_dev *dev)
0287 {
0288     struct analog_port *port = input_get_drvdata(dev);
0289 
0290     gameport_start_polling(port->gameport);
0291     return 0;
0292 }
0293 
0294 /*
0295  * analog_close() is a callback from the input close routine.
0296  */
0297 
0298 static void analog_close(struct input_dev *dev)
0299 {
0300     struct analog_port *port = input_get_drvdata(dev);
0301 
0302     gameport_stop_polling(port->gameport);
0303 }
0304 
0305 /*
0306  * analog_calibrate_timer() calibrates the timer and computes loop
0307  * and timeout values for a joystick port.
0308  */
0309 
0310 static void analog_calibrate_timer(struct analog_port *port)
0311 {
0312     struct gameport *gameport = port->gameport;
0313     unsigned int i, t, tx;
0314     ktime_t t1, t2, t3;
0315     unsigned long flags;
0316 
0317     tx = ~0;
0318 
0319     for (i = 0; i < 50; i++) {
0320         local_irq_save(flags);
0321         t1 = ktime_get();
0322         for (t = 0; t < 50; t++) {
0323             gameport_read(gameport);
0324             t2 = ktime_get();
0325         }
0326         t3 = ktime_get();
0327         local_irq_restore(flags);
0328         udelay(i);
0329         t = ktime_sub(t2, t1) - ktime_sub(t3, t2);
0330         if (t < tx) tx = t;
0331     }
0332 
0333         port->loop = tx / 50;
0334 }
0335 
0336 /*
0337  * analog_name() constructs a name for an analog joystick.
0338  */
0339 
0340 static void analog_name(struct analog *analog)
0341 {
0342     struct seq_buf s;
0343 
0344     seq_buf_init(&s, analog->name, sizeof(analog->name));
0345     seq_buf_printf(&s, "Analog %d-axis %d-button",
0346          hweight8(analog->mask & ANALOG_AXES_STD),
0347          hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
0348          hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
0349 
0350     if (analog->mask & ANALOG_HATS_ALL)
0351         seq_buf_printf(&s, " %d-hat",
0352                    hweight16(analog->mask & ANALOG_HATS_ALL));
0353 
0354     if (analog->mask & ANALOG_HAT_FCS)
0355         seq_buf_printf(&s, " FCS");
0356     if (analog->mask & ANALOG_ANY_CHF)
0357         seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
0358 
0359     seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
0360 }
0361 
0362 /*
0363  * analog_init_device()
0364  */
0365 
0366 static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
0367 {
0368     struct input_dev *input_dev;
0369     int i, j, t, v, w, x, y, z;
0370     int error;
0371 
0372     analog_name(analog);
0373     snprintf(analog->phys, sizeof(analog->phys),
0374          "%s/input%d", port->gameport->phys, index);
0375     analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
0376 
0377     analog->dev = input_dev = input_allocate_device();
0378     if (!input_dev)
0379         return -ENOMEM;
0380 
0381     input_dev->name = analog->name;
0382     input_dev->phys = analog->phys;
0383     input_dev->id.bustype = BUS_GAMEPORT;
0384     input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
0385     input_dev->id.product = analog->mask >> 4;
0386     input_dev->id.version = 0x0100;
0387     input_dev->dev.parent = &port->gameport->dev;
0388 
0389     input_set_drvdata(input_dev, port);
0390 
0391     input_dev->open = analog_open;
0392     input_dev->close = analog_close;
0393 
0394     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0395 
0396     for (i = j = 0; i < 4; i++)
0397         if (analog->mask & (1 << i)) {
0398 
0399             t = analog_axes[j];
0400             x = port->axes[i];
0401             y = (port->axes[0] + port->axes[1]) >> 1;
0402             z = y - port->axes[i];
0403             z = z > 0 ? z : -z;
0404             v = (x >> 3);
0405             w = (x >> 3);
0406 
0407             if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
0408                 x = y;
0409 
0410             if (analog->mask & ANALOG_SAITEK) {
0411                 if (i == 2) x = port->axes[i];
0412                 v = x - (x >> 2);
0413                 w = (x >> 4);
0414             }
0415 
0416             input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
0417             j++;
0418         }
0419 
0420     for (i = j = 0; i < 3; i++)
0421         if (analog->mask & analog_exts[i])
0422             for (x = 0; x < 2; x++) {
0423                 t = analog_hats[j++];
0424                 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
0425             }
0426 
0427     for (i = j = 0; i < 4; i++)
0428         if (analog->mask & (0x10 << i))
0429             set_bit(analog->buttons[j++], input_dev->keybit);
0430 
0431     if (analog->mask & ANALOG_BTNS_CHF)
0432         for (i = 0; i < 2; i++)
0433             set_bit(analog->buttons[j++], input_dev->keybit);
0434 
0435     if (analog->mask & ANALOG_HBTN_CHF)
0436         for (i = 0; i < 4; i++)
0437             set_bit(analog->buttons[j++], input_dev->keybit);
0438 
0439     for (i = 0; i < 4; i++)
0440         if (analog->mask & (ANALOG_BTN_TL << i))
0441             set_bit(analog_pads[i], input_dev->keybit);
0442 
0443     analog_decode(analog, port->axes, port->initial, port->buttons);
0444 
0445     error = input_register_device(analog->dev);
0446     if (error) {
0447         input_free_device(analog->dev);
0448         return error;
0449     }
0450 
0451     return 0;
0452 }
0453 
0454 /*
0455  * analog_init_devices() sets up device-specific values and registers the input devices.
0456  */
0457 
0458 static int analog_init_masks(struct analog_port *port)
0459 {
0460     int i;
0461     struct analog *analog = port->analog;
0462     int max[4];
0463 
0464     if (!port->mask)
0465         return -1;
0466 
0467     if ((port->mask & 3) != 3 && port->mask != 0xc) {
0468         printk(KERN_WARNING "analog.c: Unknown joystick device found  "
0469             "(data=%#x, %s), probably not analog joystick.\n",
0470             port->mask, port->gameport->phys);
0471         return -1;
0472     }
0473 
0474 
0475     i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
0476 
0477     analog[0].mask = i & 0xfffff;
0478 
0479     analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
0480             | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
0481             | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
0482 
0483     analog[0].mask &= ~(ANALOG_HAT2_CHF)
0484             | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
0485 
0486     analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
0487             | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
0488             | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
0489             | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
0490 
0491     analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
0492             | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
0493             &  ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
0494 
0495     analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
0496 
0497     analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
0498             : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
0499 
0500     if (port->cooked) {
0501 
0502         for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
0503 
0504         if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
0505         if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
0506         if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
0507         if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
0508         if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
0509 
0510         gameport_calibrate(port->gameport, port->axes, max);
0511     }
0512 
0513     for (i = 0; i < 4; i++)
0514         port->initial[i] = port->axes[i];
0515 
0516     return -!(analog[0].mask || analog[1].mask);
0517 }
0518 
0519 static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
0520 {
0521     int i, t, u, v;
0522 
0523     port->gameport = gameport;
0524 
0525     gameport_set_drvdata(gameport, port);
0526 
0527     if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
0528 
0529         analog_calibrate_timer(port);
0530 
0531         gameport_trigger(gameport);
0532         t = gameport_read(gameport);
0533         msleep(ANALOG_MAX_TIME);
0534         port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
0535         port->fuzz = (NSEC_PER_MSEC * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
0536 
0537         for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
0538             if (!analog_cooked_read(port))
0539                 break;
0540             msleep(ANALOG_MAX_TIME);
0541         }
0542 
0543         u = v = 0;
0544 
0545         msleep(ANALOG_MAX_TIME);
0546         t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
0547         gameport_trigger(gameport);
0548         while ((gameport_read(port->gameport) & port->mask) && (u < t))
0549             u++;
0550         udelay(ANALOG_SAITEK_DELAY);
0551         t = gameport_time(gameport, ANALOG_SAITEK_TIME);
0552         gameport_trigger(gameport);
0553         while ((gameport_read(port->gameport) & port->mask) && (v < t))
0554             v++;
0555 
0556         if (v < (u >> 1)) { /* FIXME - more than one port */
0557             analog_options[0] |= /* FIXME - more than one port */
0558                 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
0559             return 0;
0560         }
0561 
0562         gameport_close(gameport);
0563     }
0564 
0565     if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
0566 
0567         for (i = 0; i < ANALOG_INIT_RETRIES; i++)
0568             if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
0569                 break;
0570         for (i = 0; i < 4; i++)
0571             if (port->axes[i] != -1)
0572                 port->mask |= 1 << i;
0573 
0574         port->fuzz = gameport->fuzz;
0575         port->cooked = 1;
0576         return 0;
0577     }
0578 
0579     return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
0580 }
0581 
0582 static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
0583 {
0584     struct analog_port *port;
0585     int i;
0586     int err;
0587 
0588     if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
0589         return -ENOMEM;
0590 
0591     err = analog_init_port(gameport, drv, port);
0592     if (err)
0593         goto fail1;
0594 
0595     err = analog_init_masks(port);
0596     if (err)
0597         goto fail2;
0598 
0599     gameport_set_poll_handler(gameport, analog_poll);
0600     gameport_set_poll_interval(gameport, 10);
0601 
0602     for (i = 0; i < 2; i++)
0603         if (port->analog[i].mask) {
0604             err = analog_init_device(port, port->analog + i, i);
0605             if (err)
0606                 goto fail3;
0607         }
0608 
0609     return 0;
0610 
0611  fail3: while (--i >= 0)
0612         if (port->analog[i].mask)
0613             input_unregister_device(port->analog[i].dev);
0614  fail2: gameport_close(gameport);
0615  fail1: gameport_set_drvdata(gameport, NULL);
0616     kfree(port);
0617     return err;
0618 }
0619 
0620 static void analog_disconnect(struct gameport *gameport)
0621 {
0622     struct analog_port *port = gameport_get_drvdata(gameport);
0623     int i;
0624 
0625     for (i = 0; i < 2; i++)
0626         if (port->analog[i].mask)
0627             input_unregister_device(port->analog[i].dev);
0628     gameport_close(gameport);
0629     gameport_set_drvdata(gameport, NULL);
0630     printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
0631         port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
0632         port->gameport->phys);
0633     kfree(port);
0634 }
0635 
0636 struct analog_types {
0637     char *name;
0638     int value;
0639 };
0640 
0641 static struct analog_types analog_types[] = {
0642     { "none",   0x00000000 },
0643     { "auto",   0x000000ff },
0644     { "2btn",   0x0000003f },
0645     { "y-joy",  0x0cc00033 },
0646     { "y-pad",  0x8cc80033 },
0647     { "fcs",    0x000008f7 },
0648     { "chf",    0x000002ff },
0649     { "fullchf",    0x000007ff },
0650     { "gamepad",    0x000830f3 },
0651     { "gamepad8",   0x0008f0f3 },
0652     { NULL, 0 }
0653 };
0654 
0655 static void analog_parse_options(void)
0656 {
0657     int i, j;
0658     char *end;
0659 
0660     for (i = 0; i < js_nargs; i++) {
0661 
0662         for (j = 0; analog_types[j].name; j++)
0663             if (!strcmp(analog_types[j].name, js[i])) {
0664                 analog_options[i] = analog_types[j].value;
0665                 break;
0666             }
0667         if (analog_types[j].name) continue;
0668 
0669         analog_options[i] = simple_strtoul(js[i], &end, 0);
0670         if (end != js[i]) continue;
0671 
0672         analog_options[i] = 0xff;
0673         if (!strlen(js[i])) continue;
0674 
0675         printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
0676     }
0677 
0678     for (; i < ANALOG_PORTS; i++)
0679         analog_options[i] = 0xff;
0680 }
0681 
0682 /*
0683  * The gameport device structure.
0684  */
0685 
0686 static struct gameport_driver analog_drv = {
0687     .driver     = {
0688         .name   = "analog",
0689     },
0690     .description    = DRIVER_DESC,
0691     .connect    = analog_connect,
0692     .disconnect = analog_disconnect,
0693 };
0694 
0695 static int __init analog_init(void)
0696 {
0697     analog_parse_options();
0698     return gameport_register_driver(&analog_drv);
0699 }
0700 
0701 static void __exit analog_exit(void)
0702 {
0703     gameport_unregister_driver(&analog_drv);
0704 }
0705 
0706 module_init(analog_init);
0707 module_exit(analog_exit);