Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Device Modules for Nintendo Wii / Wii U HID Driver
0004  * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
0005  */
0006 
0007 /*
0008  */
0009 
0010 /*
0011  * Wiimote Modules
0012  * Nintendo devices provide different peripherals and many new devices lack
0013  * initial features like the IR camera. Therefore, each peripheral device is
0014  * implemented as an independent module and we probe on each device only the
0015  * modules for the hardware that really is available.
0016  *
0017  * Module registration is sequential. Unregistration is done in reverse order.
0018  * After device detection, the needed modules are loaded. Users can trigger
0019  * re-detection which causes all modules to be unloaded and then reload the
0020  * modules for the new detected device.
0021  *
0022  * wdata->input is a shared input device. It is always initialized prior to
0023  * module registration. If at least one registered module is marked as
0024  * WIIMOD_FLAG_INPUT, then the input device will get registered after all
0025  * modules were registered.
0026  * Please note that it is unregistered _before_ the "remove" callbacks are
0027  * called. This guarantees that no input interaction is done, anymore. However,
0028  * the wiimote core keeps a reference to the input device so it is freed only
0029  * after all modules were removed. It is safe to send events to unregistered
0030  * input devices.
0031  */
0032 
0033 #include <linux/device.h>
0034 #include <linux/hid.h>
0035 #include <linux/input.h>
0036 #include <linux/spinlock.h>
0037 #include "hid-wiimote.h"
0038 
0039 /*
0040  * Keys
0041  * The initial Wii Remote provided a bunch of buttons that are reported as
0042  * part of the core protocol. Many later devices dropped these and report
0043  * invalid data in the core button reports. Load this only on devices which
0044  * correctly send button reports.
0045  * It uses the shared input device.
0046  */
0047 
0048 static const __u16 wiimod_keys_map[] = {
0049     KEY_LEFT,   /* WIIPROTO_KEY_LEFT */
0050     KEY_RIGHT,  /* WIIPROTO_KEY_RIGHT */
0051     KEY_UP,     /* WIIPROTO_KEY_UP */
0052     KEY_DOWN,   /* WIIPROTO_KEY_DOWN */
0053     KEY_NEXT,   /* WIIPROTO_KEY_PLUS */
0054     KEY_PREVIOUS,   /* WIIPROTO_KEY_MINUS */
0055     BTN_1,      /* WIIPROTO_KEY_ONE */
0056     BTN_2,      /* WIIPROTO_KEY_TWO */
0057     BTN_A,      /* WIIPROTO_KEY_A */
0058     BTN_B,      /* WIIPROTO_KEY_B */
0059     BTN_MODE,   /* WIIPROTO_KEY_HOME */
0060 };
0061 
0062 static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
0063 {
0064     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
0065                             !!(keys[0] & 0x01));
0066     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
0067                             !!(keys[0] & 0x02));
0068     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
0069                             !!(keys[0] & 0x04));
0070     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
0071                             !!(keys[0] & 0x08));
0072     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
0073                             !!(keys[0] & 0x10));
0074     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
0075                             !!(keys[1] & 0x01));
0076     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
0077                             !!(keys[1] & 0x02));
0078     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
0079                             !!(keys[1] & 0x04));
0080     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
0081                             !!(keys[1] & 0x08));
0082     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
0083                             !!(keys[1] & 0x10));
0084     input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
0085                             !!(keys[1] & 0x80));
0086     input_sync(wdata->input);
0087 }
0088 
0089 static int wiimod_keys_probe(const struct wiimod_ops *ops,
0090                  struct wiimote_data *wdata)
0091 {
0092     unsigned int i;
0093 
0094     set_bit(EV_KEY, wdata->input->evbit);
0095     for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
0096         set_bit(wiimod_keys_map[i], wdata->input->keybit);
0097 
0098     return 0;
0099 }
0100 
0101 static const struct wiimod_ops wiimod_keys = {
0102     .flags = WIIMOD_FLAG_INPUT,
0103     .arg = 0,
0104     .probe = wiimod_keys_probe,
0105     .remove = NULL,
0106     .in_keys = wiimod_keys_in_keys,
0107 };
0108 
0109 /*
0110  * Rumble
0111  * Nearly all devices provide a rumble feature. A small motor for
0112  * force-feedback effects. We provide an FF_RUMBLE memless ff device on the
0113  * shared input device if this module is loaded.
0114  * The rumble motor is controlled via a flag on almost every output report so
0115  * the wiimote core handles the rumble flag. But if a device doesn't provide
0116  * the rumble motor, this flag shouldn't be set.
0117  */
0118 
0119 /* used by wiimod_rumble and wiipro_rumble */
0120 static void wiimod_rumble_worker(struct work_struct *work)
0121 {
0122     struct wiimote_data *wdata = container_of(work, struct wiimote_data,
0123                           rumble_worker);
0124 
0125     spin_lock_irq(&wdata->state.lock);
0126     wiiproto_req_rumble(wdata, wdata->state.cache_rumble);
0127     spin_unlock_irq(&wdata->state.lock);
0128 }
0129 
0130 static int wiimod_rumble_play(struct input_dev *dev, void *data,
0131                   struct ff_effect *eff)
0132 {
0133     struct wiimote_data *wdata = input_get_drvdata(dev);
0134     __u8 value;
0135 
0136     /*
0137      * The wiimote supports only a single rumble motor so if any magnitude
0138      * is set to non-zero then we start the rumble motor. If both are set to
0139      * zero, we stop the rumble motor.
0140      */
0141 
0142     if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
0143         value = 1;
0144     else
0145         value = 0;
0146 
0147     /* Locking state.lock here might deadlock with input_event() calls.
0148      * schedule_work acts as barrier. Merging multiple changes is fine. */
0149     wdata->state.cache_rumble = value;
0150     schedule_work(&wdata->rumble_worker);
0151 
0152     return 0;
0153 }
0154 
0155 static int wiimod_rumble_probe(const struct wiimod_ops *ops,
0156                    struct wiimote_data *wdata)
0157 {
0158     INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
0159 
0160     set_bit(FF_RUMBLE, wdata->input->ffbit);
0161     if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
0162         return -ENOMEM;
0163 
0164     return 0;
0165 }
0166 
0167 static void wiimod_rumble_remove(const struct wiimod_ops *ops,
0168                  struct wiimote_data *wdata)
0169 {
0170     unsigned long flags;
0171 
0172     cancel_work_sync(&wdata->rumble_worker);
0173 
0174     spin_lock_irqsave(&wdata->state.lock, flags);
0175     wiiproto_req_rumble(wdata, 0);
0176     spin_unlock_irqrestore(&wdata->state.lock, flags);
0177 }
0178 
0179 static const struct wiimod_ops wiimod_rumble = {
0180     .flags = WIIMOD_FLAG_INPUT,
0181     .arg = 0,
0182     .probe = wiimod_rumble_probe,
0183     .remove = wiimod_rumble_remove,
0184 };
0185 
0186 /*
0187  * Battery
0188  * 1 byte of battery capacity information is sent along every protocol status
0189  * report. The wiimote core caches it but we try to update it on every
0190  * user-space request.
0191  * This is supported by nearly every device so it's almost always enabled.
0192  */
0193 
0194 static enum power_supply_property wiimod_battery_props[] = {
0195     POWER_SUPPLY_PROP_CAPACITY,
0196     POWER_SUPPLY_PROP_SCOPE,
0197 };
0198 
0199 static int wiimod_battery_get_property(struct power_supply *psy,
0200                        enum power_supply_property psp,
0201                        union power_supply_propval *val)
0202 {
0203     struct wiimote_data *wdata = power_supply_get_drvdata(psy);
0204     int ret = 0, state;
0205     unsigned long flags;
0206 
0207     if (psp == POWER_SUPPLY_PROP_SCOPE) {
0208         val->intval = POWER_SUPPLY_SCOPE_DEVICE;
0209         return 0;
0210     } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
0211         return -EINVAL;
0212     }
0213 
0214     ret = wiimote_cmd_acquire(wdata);
0215     if (ret)
0216         return ret;
0217 
0218     spin_lock_irqsave(&wdata->state.lock, flags);
0219     wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
0220     wiiproto_req_status(wdata);
0221     spin_unlock_irqrestore(&wdata->state.lock, flags);
0222 
0223     wiimote_cmd_wait(wdata);
0224     wiimote_cmd_release(wdata);
0225 
0226     spin_lock_irqsave(&wdata->state.lock, flags);
0227     state = wdata->state.cmd_battery;
0228     spin_unlock_irqrestore(&wdata->state.lock, flags);
0229 
0230     val->intval = state * 100 / 255;
0231     return ret;
0232 }
0233 
0234 static int wiimod_battery_probe(const struct wiimod_ops *ops,
0235                 struct wiimote_data *wdata)
0236 {
0237     struct power_supply_config psy_cfg = { .drv_data = wdata, };
0238     int ret;
0239 
0240     wdata->battery_desc.properties = wiimod_battery_props;
0241     wdata->battery_desc.num_properties = ARRAY_SIZE(wiimod_battery_props);
0242     wdata->battery_desc.get_property = wiimod_battery_get_property;
0243     wdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
0244     wdata->battery_desc.use_for_apm = 0;
0245     wdata->battery_desc.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
0246                          wdata->hdev->uniq);
0247     if (!wdata->battery_desc.name)
0248         return -ENOMEM;
0249 
0250     wdata->battery = power_supply_register(&wdata->hdev->dev,
0251                            &wdata->battery_desc,
0252                            &psy_cfg);
0253     if (IS_ERR(wdata->battery)) {
0254         hid_err(wdata->hdev, "cannot register battery device\n");
0255         ret = PTR_ERR(wdata->battery);
0256         goto err_free;
0257     }
0258 
0259     power_supply_powers(wdata->battery, &wdata->hdev->dev);
0260     return 0;
0261 
0262 err_free:
0263     kfree(wdata->battery_desc.name);
0264     wdata->battery_desc.name = NULL;
0265     return ret;
0266 }
0267 
0268 static void wiimod_battery_remove(const struct wiimod_ops *ops,
0269                   struct wiimote_data *wdata)
0270 {
0271     if (!wdata->battery_desc.name)
0272         return;
0273 
0274     power_supply_unregister(wdata->battery);
0275     kfree(wdata->battery_desc.name);
0276     wdata->battery_desc.name = NULL;
0277 }
0278 
0279 static const struct wiimod_ops wiimod_battery = {
0280     .flags = 0,
0281     .arg = 0,
0282     .probe = wiimod_battery_probe,
0283     .remove = wiimod_battery_remove,
0284 };
0285 
0286 /*
0287  * LED
0288  * 0 to 4 player LEDs are supported by devices. The "arg" field of the
0289  * wiimod_ops structure specifies which LED this module controls. This allows
0290  * to register a limited number of LEDs.
0291  * State is managed by wiimote core.
0292  */
0293 
0294 static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
0295 {
0296     struct device *dev = led_dev->dev->parent;
0297     struct wiimote_data *wdata = dev_to_wii(dev);
0298     int i;
0299     unsigned long flags;
0300     bool value = false;
0301 
0302     for (i = 0; i < 4; ++i) {
0303         if (wdata->leds[i] == led_dev) {
0304             spin_lock_irqsave(&wdata->state.lock, flags);
0305             value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
0306             spin_unlock_irqrestore(&wdata->state.lock, flags);
0307             break;
0308         }
0309     }
0310 
0311     return value ? LED_FULL : LED_OFF;
0312 }
0313 
0314 static void wiimod_led_set(struct led_classdev *led_dev,
0315                enum led_brightness value)
0316 {
0317     struct device *dev = led_dev->dev->parent;
0318     struct wiimote_data *wdata = dev_to_wii(dev);
0319     int i;
0320     unsigned long flags;
0321     __u8 state, flag;
0322 
0323     for (i = 0; i < 4; ++i) {
0324         if (wdata->leds[i] == led_dev) {
0325             flag = WIIPROTO_FLAG_LED(i + 1);
0326             spin_lock_irqsave(&wdata->state.lock, flags);
0327             state = wdata->state.flags;
0328             if (value == LED_OFF)
0329                 wiiproto_req_leds(wdata, state & ~flag);
0330             else
0331                 wiiproto_req_leds(wdata, state | flag);
0332             spin_unlock_irqrestore(&wdata->state.lock, flags);
0333             break;
0334         }
0335     }
0336 }
0337 
0338 static int wiimod_led_probe(const struct wiimod_ops *ops,
0339                 struct wiimote_data *wdata)
0340 {
0341     struct device *dev = &wdata->hdev->dev;
0342     size_t namesz = strlen(dev_name(dev)) + 9;
0343     struct led_classdev *led;
0344     unsigned long flags;
0345     char *name;
0346     int ret;
0347 
0348     led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
0349     if (!led)
0350         return -ENOMEM;
0351 
0352     name = (void*)&led[1];
0353     snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
0354     led->name = name;
0355     led->brightness = 0;
0356     led->max_brightness = 1;
0357     led->brightness_get = wiimod_led_get;
0358     led->brightness_set = wiimod_led_set;
0359 
0360     wdata->leds[ops->arg] = led;
0361     ret = led_classdev_register(dev, led);
0362     if (ret)
0363         goto err_free;
0364 
0365     /* enable LED1 to stop initial LED-blinking */
0366     if (ops->arg == 0) {
0367         spin_lock_irqsave(&wdata->state.lock, flags);
0368         wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
0369         spin_unlock_irqrestore(&wdata->state.lock, flags);
0370     }
0371 
0372     return 0;
0373 
0374 err_free:
0375     wdata->leds[ops->arg] = NULL;
0376     kfree(led);
0377     return ret;
0378 }
0379 
0380 static void wiimod_led_remove(const struct wiimod_ops *ops,
0381                   struct wiimote_data *wdata)
0382 {
0383     if (!wdata->leds[ops->arg])
0384         return;
0385 
0386     led_classdev_unregister(wdata->leds[ops->arg]);
0387     kfree(wdata->leds[ops->arg]);
0388     wdata->leds[ops->arg] = NULL;
0389 }
0390 
0391 static const struct wiimod_ops wiimod_leds[4] = {
0392     {
0393         .flags = 0,
0394         .arg = 0,
0395         .probe = wiimod_led_probe,
0396         .remove = wiimod_led_remove,
0397     },
0398     {
0399         .flags = 0,
0400         .arg = 1,
0401         .probe = wiimod_led_probe,
0402         .remove = wiimod_led_remove,
0403     },
0404     {
0405         .flags = 0,
0406         .arg = 2,
0407         .probe = wiimod_led_probe,
0408         .remove = wiimod_led_remove,
0409     },
0410     {
0411         .flags = 0,
0412         .arg = 3,
0413         .probe = wiimod_led_probe,
0414         .remove = wiimod_led_remove,
0415     },
0416 };
0417 
0418 /*
0419  * Accelerometer
0420  * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
0421  * device, it's mostly cleared to 0. This module parses this data and provides
0422  * it via a separate input device.
0423  */
0424 
0425 static void wiimod_accel_in_accel(struct wiimote_data *wdata,
0426                   const __u8 *accel)
0427 {
0428     __u16 x, y, z;
0429 
0430     if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
0431         return;
0432 
0433     /*
0434      * payload is: BB BB XX YY ZZ
0435      * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
0436      * contain the upper 8 bits of each value. The lower 2 bits are
0437      * contained in the buttons data BB BB.
0438      * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
0439      * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
0440      * accel value and bit 6 is the second bit of the Z value.
0441      * The first bit of Y and Z values is not available and always set to 0.
0442      * 0x200 is returned on no movement.
0443      */
0444 
0445     x = accel[2] << 2;
0446     y = accel[3] << 2;
0447     z = accel[4] << 2;
0448 
0449     x |= (accel[0] >> 5) & 0x3;
0450     y |= (accel[1] >> 4) & 0x2;
0451     z |= (accel[1] >> 5) & 0x2;
0452 
0453     input_report_abs(wdata->accel, ABS_RX, x - 0x200);
0454     input_report_abs(wdata->accel, ABS_RY, y - 0x200);
0455     input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
0456     input_sync(wdata->accel);
0457 }
0458 
0459 static int wiimod_accel_open(struct input_dev *dev)
0460 {
0461     struct wiimote_data *wdata = input_get_drvdata(dev);
0462     unsigned long flags;
0463 
0464     spin_lock_irqsave(&wdata->state.lock, flags);
0465     wiiproto_req_accel(wdata, true);
0466     spin_unlock_irqrestore(&wdata->state.lock, flags);
0467 
0468     return 0;
0469 }
0470 
0471 static void wiimod_accel_close(struct input_dev *dev)
0472 {
0473     struct wiimote_data *wdata = input_get_drvdata(dev);
0474     unsigned long flags;
0475 
0476     spin_lock_irqsave(&wdata->state.lock, flags);
0477     wiiproto_req_accel(wdata, false);
0478     spin_unlock_irqrestore(&wdata->state.lock, flags);
0479 }
0480 
0481 static int wiimod_accel_probe(const struct wiimod_ops *ops,
0482                   struct wiimote_data *wdata)
0483 {
0484     int ret;
0485 
0486     wdata->accel = input_allocate_device();
0487     if (!wdata->accel)
0488         return -ENOMEM;
0489 
0490     input_set_drvdata(wdata->accel, wdata);
0491     wdata->accel->open = wiimod_accel_open;
0492     wdata->accel->close = wiimod_accel_close;
0493     wdata->accel->dev.parent = &wdata->hdev->dev;
0494     wdata->accel->id.bustype = wdata->hdev->bus;
0495     wdata->accel->id.vendor = wdata->hdev->vendor;
0496     wdata->accel->id.product = wdata->hdev->product;
0497     wdata->accel->id.version = wdata->hdev->version;
0498     wdata->accel->name = WIIMOTE_NAME " Accelerometer";
0499 
0500     set_bit(EV_ABS, wdata->accel->evbit);
0501     set_bit(ABS_RX, wdata->accel->absbit);
0502     set_bit(ABS_RY, wdata->accel->absbit);
0503     set_bit(ABS_RZ, wdata->accel->absbit);
0504     input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
0505     input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
0506     input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
0507 
0508     ret = input_register_device(wdata->accel);
0509     if (ret) {
0510         hid_err(wdata->hdev, "cannot register input device\n");
0511         goto err_free;
0512     }
0513 
0514     return 0;
0515 
0516 err_free:
0517     input_free_device(wdata->accel);
0518     wdata->accel = NULL;
0519     return ret;
0520 }
0521 
0522 static void wiimod_accel_remove(const struct wiimod_ops *ops,
0523                 struct wiimote_data *wdata)
0524 {
0525     if (!wdata->accel)
0526         return;
0527 
0528     input_unregister_device(wdata->accel);
0529     wdata->accel = NULL;
0530 }
0531 
0532 static const struct wiimod_ops wiimod_accel = {
0533     .flags = 0,
0534     .arg = 0,
0535     .probe = wiimod_accel_probe,
0536     .remove = wiimod_accel_remove,
0537     .in_accel = wiimod_accel_in_accel,
0538 };
0539 
0540 /*
0541  * IR Cam
0542  * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs
0543  * to be initialized with a fairly complex procedure and consumes a lot of
0544  * power. Therefore, as long as no application uses the IR input device, it is
0545  * kept offline.
0546  * Nearly no other device than the normal Wii Remotes supports the IR cam so
0547  * you can disable this module for these devices.
0548  */
0549 
0550 static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
0551                 bool packed, unsigned int id)
0552 {
0553     __u16 x, y;
0554     __u8 xid, yid;
0555     bool sync = false;
0556 
0557     if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
0558         return;
0559 
0560     switch (id) {
0561     case 0:
0562         xid = ABS_HAT0X;
0563         yid = ABS_HAT0Y;
0564         break;
0565     case 1:
0566         xid = ABS_HAT1X;
0567         yid = ABS_HAT1Y;
0568         break;
0569     case 2:
0570         xid = ABS_HAT2X;
0571         yid = ABS_HAT2Y;
0572         break;
0573     case 3:
0574         xid = ABS_HAT3X;
0575         yid = ABS_HAT3Y;
0576         sync = true;
0577         break;
0578     default:
0579         return;
0580     }
0581 
0582     /*
0583      * Basic IR data is encoded into 3 bytes. The first two bytes are the
0584      * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
0585      * of both.
0586      * If data is packed, then the 3rd byte is put first and slightly
0587      * reordered. This allows to interleave packed and non-packed data to
0588      * have two IR sets in 5 bytes instead of 6.
0589      * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev.
0590      */
0591 
0592     if (packed) {
0593         x = ir[1] | ((ir[0] & 0x03) << 8);
0594         y = ir[2] | ((ir[0] & 0x0c) << 6);
0595     } else {
0596         x = ir[0] | ((ir[2] & 0x30) << 4);
0597         y = ir[1] | ((ir[2] & 0xc0) << 2);
0598     }
0599 
0600     input_report_abs(wdata->ir, xid, x);
0601     input_report_abs(wdata->ir, yid, y);
0602 
0603     if (sync)
0604         input_sync(wdata->ir);
0605 }
0606 
0607 static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
0608 {
0609     int ret;
0610     unsigned long flags;
0611     __u8 format = 0;
0612     static const __u8 data_enable[] = { 0x01 };
0613     static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
0614                         0x00, 0xaa, 0x00, 0x64 };
0615     static const __u8 data_sens2[] = { 0x63, 0x03 };
0616     static const __u8 data_fin[] = { 0x08 };
0617 
0618     spin_lock_irqsave(&wdata->state.lock, flags);
0619 
0620     if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
0621         spin_unlock_irqrestore(&wdata->state.lock, flags);
0622         return 0;
0623     }
0624 
0625     if (mode == 0) {
0626         wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
0627         wiiproto_req_ir1(wdata, 0);
0628         wiiproto_req_ir2(wdata, 0);
0629         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
0630         spin_unlock_irqrestore(&wdata->state.lock, flags);
0631         return 0;
0632     }
0633 
0634     spin_unlock_irqrestore(&wdata->state.lock, flags);
0635 
0636     ret = wiimote_cmd_acquire(wdata);
0637     if (ret)
0638         return ret;
0639 
0640     /* send PIXEL CLOCK ENABLE cmd first */
0641     spin_lock_irqsave(&wdata->state.lock, flags);
0642     wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
0643     wiiproto_req_ir1(wdata, 0x06);
0644     spin_unlock_irqrestore(&wdata->state.lock, flags);
0645 
0646     ret = wiimote_cmd_wait(wdata);
0647     if (ret)
0648         goto unlock;
0649     if (wdata->state.cmd_err) {
0650         ret = -EIO;
0651         goto unlock;
0652     }
0653 
0654     /* enable IR LOGIC */
0655     spin_lock_irqsave(&wdata->state.lock, flags);
0656     wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
0657     wiiproto_req_ir2(wdata, 0x06);
0658     spin_unlock_irqrestore(&wdata->state.lock, flags);
0659 
0660     ret = wiimote_cmd_wait(wdata);
0661     if (ret)
0662         goto unlock;
0663     if (wdata->state.cmd_err) {
0664         ret = -EIO;
0665         goto unlock;
0666     }
0667 
0668     /* enable IR cam but do not make it send data, yet */
0669     ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
0670                             sizeof(data_enable));
0671     if (ret)
0672         goto unlock;
0673 
0674     /* write first sensitivity block */
0675     ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
0676                             sizeof(data_sens1));
0677     if (ret)
0678         goto unlock;
0679 
0680     /* write second sensitivity block */
0681     ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
0682                             sizeof(data_sens2));
0683     if (ret)
0684         goto unlock;
0685 
0686     /* put IR cam into desired state */
0687     switch (mode) {
0688         case WIIPROTO_FLAG_IR_FULL:
0689             format = 5;
0690             break;
0691         case WIIPROTO_FLAG_IR_EXT:
0692             format = 3;
0693             break;
0694         case WIIPROTO_FLAG_IR_BASIC:
0695             format = 1;
0696             break;
0697     }
0698     ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
0699     if (ret)
0700         goto unlock;
0701 
0702     /* make IR cam send data */
0703     ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
0704     if (ret)
0705         goto unlock;
0706 
0707     /* request new DRM mode compatible to IR mode */
0708     spin_lock_irqsave(&wdata->state.lock, flags);
0709     wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
0710     wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
0711     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
0712     spin_unlock_irqrestore(&wdata->state.lock, flags);
0713 
0714 unlock:
0715     wiimote_cmd_release(wdata);
0716     return ret;
0717 }
0718 
0719 static int wiimod_ir_open(struct input_dev *dev)
0720 {
0721     struct wiimote_data *wdata = input_get_drvdata(dev);
0722 
0723     return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
0724 }
0725 
0726 static void wiimod_ir_close(struct input_dev *dev)
0727 {
0728     struct wiimote_data *wdata = input_get_drvdata(dev);
0729 
0730     wiimod_ir_change(wdata, 0);
0731 }
0732 
0733 static int wiimod_ir_probe(const struct wiimod_ops *ops,
0734                struct wiimote_data *wdata)
0735 {
0736     int ret;
0737 
0738     wdata->ir = input_allocate_device();
0739     if (!wdata->ir)
0740         return -ENOMEM;
0741 
0742     input_set_drvdata(wdata->ir, wdata);
0743     wdata->ir->open = wiimod_ir_open;
0744     wdata->ir->close = wiimod_ir_close;
0745     wdata->ir->dev.parent = &wdata->hdev->dev;
0746     wdata->ir->id.bustype = wdata->hdev->bus;
0747     wdata->ir->id.vendor = wdata->hdev->vendor;
0748     wdata->ir->id.product = wdata->hdev->product;
0749     wdata->ir->id.version = wdata->hdev->version;
0750     wdata->ir->name = WIIMOTE_NAME " IR";
0751 
0752     set_bit(EV_ABS, wdata->ir->evbit);
0753     set_bit(ABS_HAT0X, wdata->ir->absbit);
0754     set_bit(ABS_HAT0Y, wdata->ir->absbit);
0755     set_bit(ABS_HAT1X, wdata->ir->absbit);
0756     set_bit(ABS_HAT1Y, wdata->ir->absbit);
0757     set_bit(ABS_HAT2X, wdata->ir->absbit);
0758     set_bit(ABS_HAT2Y, wdata->ir->absbit);
0759     set_bit(ABS_HAT3X, wdata->ir->absbit);
0760     set_bit(ABS_HAT3Y, wdata->ir->absbit);
0761     input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
0762     input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
0763     input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
0764     input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
0765     input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
0766     input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
0767     input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
0768     input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
0769 
0770     ret = input_register_device(wdata->ir);
0771     if (ret) {
0772         hid_err(wdata->hdev, "cannot register input device\n");
0773         goto err_free;
0774     }
0775 
0776     return 0;
0777 
0778 err_free:
0779     input_free_device(wdata->ir);
0780     wdata->ir = NULL;
0781     return ret;
0782 }
0783 
0784 static void wiimod_ir_remove(const struct wiimod_ops *ops,
0785                  struct wiimote_data *wdata)
0786 {
0787     if (!wdata->ir)
0788         return;
0789 
0790     input_unregister_device(wdata->ir);
0791     wdata->ir = NULL;
0792 }
0793 
0794 static const struct wiimod_ops wiimod_ir = {
0795     .flags = 0,
0796     .arg = 0,
0797     .probe = wiimod_ir_probe,
0798     .remove = wiimod_ir_remove,
0799     .in_ir = wiimod_ir_in_ir,
0800 };
0801 
0802 /*
0803  * Nunchuk Extension
0804  * The Nintendo Wii Nunchuk was the first official extension published by
0805  * Nintendo. It provides two additional keys and a separate accelerometer. It
0806  * can be hotplugged to standard Wii Remotes.
0807  */
0808 
0809 enum wiimod_nunchuk_keys {
0810     WIIMOD_NUNCHUK_KEY_C,
0811     WIIMOD_NUNCHUK_KEY_Z,
0812     WIIMOD_NUNCHUK_KEY_NUM,
0813 };
0814 
0815 static const __u16 wiimod_nunchuk_map[] = {
0816     BTN_C,      /* WIIMOD_NUNCHUK_KEY_C */
0817     BTN_Z,      /* WIIMOD_NUNCHUK_KEY_Z */
0818 };
0819 
0820 static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
0821 {
0822     __s16 x, y, z, bx, by;
0823 
0824     /*   Byte |   8    7 |  6    5 |  4    3 |  2 |  1  |
0825      *   -----+----------+---------+---------+----+-----+
0826      *    1   |              Button X <7:0>             |
0827      *    2   |              Button Y <7:0>             |
0828      *   -----+----------+---------+---------+----+-----+
0829      *    3   |               Speed X <9:2>             |
0830      *    4   |               Speed Y <9:2>             |
0831      *    5   |               Speed Z <9:2>             |
0832      *   -----+----------+---------+---------+----+-----+
0833      *    6   | Z <1:0>  | Y <1:0> | X <1:0> | BC | BZ  |
0834      *   -----+----------+---------+---------+----+-----+
0835      * Button X/Y is the analog stick. Speed X, Y and Z are the
0836      * accelerometer data in the same format as the wiimote's accelerometer.
0837      * The 6th byte contains the LSBs of the accelerometer data.
0838      * BC and BZ are the C and Z buttons: 0 means pressed
0839      *
0840      * If reported interleaved with motionp, then the layout changes. The
0841      * 5th and 6th byte changes to:
0842      *   -----+-----------------------------------+-----+
0843      *    5   |            Speed Z <9:3>          | EXT |
0844      *   -----+--------+-----+-----+----+----+----+-----+
0845      *    6   |Z <2:1> |Y <1>|X <1>| BC | BZ | 0  |  0  |
0846      *   -----+--------+-----+-----+----+----+----+-----+
0847      * All three accelerometer values lose their LSB. The other data is
0848      * still available but slightly moved.
0849      *
0850      * Center data for button values is 128. Center value for accelerometer
0851      * values it 512 / 0x200
0852      */
0853 
0854     bx = ext[0];
0855     by = ext[1];
0856     bx -= 128;
0857     by -= 128;
0858 
0859     x = ext[2] << 2;
0860     y = ext[3] << 2;
0861     z = ext[4] << 2;
0862 
0863     if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
0864         x |= (ext[5] >> 3) & 0x02;
0865         y |= (ext[5] >> 4) & 0x02;
0866         z &= ~0x4;
0867         z |= (ext[5] >> 5) & 0x06;
0868     } else {
0869         x |= (ext[5] >> 2) & 0x03;
0870         y |= (ext[5] >> 4) & 0x03;
0871         z |= (ext[5] >> 6) & 0x03;
0872     }
0873 
0874     x -= 0x200;
0875     y -= 0x200;
0876     z -= 0x200;
0877 
0878     input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
0879     input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
0880 
0881     input_report_abs(wdata->extension.input, ABS_RX, x);
0882     input_report_abs(wdata->extension.input, ABS_RY, y);
0883     input_report_abs(wdata->extension.input, ABS_RZ, z);
0884 
0885     if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
0886         input_report_key(wdata->extension.input,
0887             wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
0888             !(ext[5] & 0x04));
0889         input_report_key(wdata->extension.input,
0890             wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
0891             !(ext[5] & 0x08));
0892     } else {
0893         input_report_key(wdata->extension.input,
0894             wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
0895             !(ext[5] & 0x01));
0896         input_report_key(wdata->extension.input,
0897             wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
0898             !(ext[5] & 0x02));
0899     }
0900 
0901     input_sync(wdata->extension.input);
0902 }
0903 
0904 static int wiimod_nunchuk_open(struct input_dev *dev)
0905 {
0906     struct wiimote_data *wdata = input_get_drvdata(dev);
0907     unsigned long flags;
0908 
0909     spin_lock_irqsave(&wdata->state.lock, flags);
0910     wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
0911     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
0912     spin_unlock_irqrestore(&wdata->state.lock, flags);
0913 
0914     return 0;
0915 }
0916 
0917 static void wiimod_nunchuk_close(struct input_dev *dev)
0918 {
0919     struct wiimote_data *wdata = input_get_drvdata(dev);
0920     unsigned long flags;
0921 
0922     spin_lock_irqsave(&wdata->state.lock, flags);
0923     wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
0924     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
0925     spin_unlock_irqrestore(&wdata->state.lock, flags);
0926 }
0927 
0928 static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
0929                 struct wiimote_data *wdata)
0930 {
0931     int ret, i;
0932 
0933     wdata->extension.input = input_allocate_device();
0934     if (!wdata->extension.input)
0935         return -ENOMEM;
0936 
0937     input_set_drvdata(wdata->extension.input, wdata);
0938     wdata->extension.input->open = wiimod_nunchuk_open;
0939     wdata->extension.input->close = wiimod_nunchuk_close;
0940     wdata->extension.input->dev.parent = &wdata->hdev->dev;
0941     wdata->extension.input->id.bustype = wdata->hdev->bus;
0942     wdata->extension.input->id.vendor = wdata->hdev->vendor;
0943     wdata->extension.input->id.product = wdata->hdev->product;
0944     wdata->extension.input->id.version = wdata->hdev->version;
0945     wdata->extension.input->name = WIIMOTE_NAME " Nunchuk";
0946 
0947     set_bit(EV_KEY, wdata->extension.input->evbit);
0948     for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i)
0949         set_bit(wiimod_nunchuk_map[i],
0950             wdata->extension.input->keybit);
0951 
0952     set_bit(EV_ABS, wdata->extension.input->evbit);
0953     set_bit(ABS_HAT0X, wdata->extension.input->absbit);
0954     set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
0955     input_set_abs_params(wdata->extension.input,
0956                  ABS_HAT0X, -120, 120, 2, 4);
0957     input_set_abs_params(wdata->extension.input,
0958                  ABS_HAT0Y, -120, 120, 2, 4);
0959     set_bit(ABS_RX, wdata->extension.input->absbit);
0960     set_bit(ABS_RY, wdata->extension.input->absbit);
0961     set_bit(ABS_RZ, wdata->extension.input->absbit);
0962     input_set_abs_params(wdata->extension.input,
0963                  ABS_RX, -500, 500, 2, 4);
0964     input_set_abs_params(wdata->extension.input,
0965                  ABS_RY, -500, 500, 2, 4);
0966     input_set_abs_params(wdata->extension.input,
0967                  ABS_RZ, -500, 500, 2, 4);
0968 
0969     ret = input_register_device(wdata->extension.input);
0970     if (ret)
0971         goto err_free;
0972 
0973     return 0;
0974 
0975 err_free:
0976     input_free_device(wdata->extension.input);
0977     wdata->extension.input = NULL;
0978     return ret;
0979 }
0980 
0981 static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
0982                   struct wiimote_data *wdata)
0983 {
0984     if (!wdata->extension.input)
0985         return;
0986 
0987     input_unregister_device(wdata->extension.input);
0988     wdata->extension.input = NULL;
0989 }
0990 
0991 static const struct wiimod_ops wiimod_nunchuk = {
0992     .flags = 0,
0993     .arg = 0,
0994     .probe = wiimod_nunchuk_probe,
0995     .remove = wiimod_nunchuk_remove,
0996     .in_ext = wiimod_nunchuk_in_ext,
0997 };
0998 
0999 /*
1000  * Classic Controller
1001  * Another official extension from Nintendo. It provides a classic
1002  * gamecube-like controller that can be hotplugged on the Wii Remote.
1003  * It has several hardware buttons and switches that are all reported via
1004  * a normal extension device.
1005  */
1006 
1007 enum wiimod_classic_keys {
1008     WIIMOD_CLASSIC_KEY_A,
1009     WIIMOD_CLASSIC_KEY_B,
1010     WIIMOD_CLASSIC_KEY_X,
1011     WIIMOD_CLASSIC_KEY_Y,
1012     WIIMOD_CLASSIC_KEY_ZL,
1013     WIIMOD_CLASSIC_KEY_ZR,
1014     WIIMOD_CLASSIC_KEY_PLUS,
1015     WIIMOD_CLASSIC_KEY_MINUS,
1016     WIIMOD_CLASSIC_KEY_HOME,
1017     WIIMOD_CLASSIC_KEY_LEFT,
1018     WIIMOD_CLASSIC_KEY_RIGHT,
1019     WIIMOD_CLASSIC_KEY_UP,
1020     WIIMOD_CLASSIC_KEY_DOWN,
1021     WIIMOD_CLASSIC_KEY_LT,
1022     WIIMOD_CLASSIC_KEY_RT,
1023     WIIMOD_CLASSIC_KEY_NUM,
1024 };
1025 
1026 static const __u16 wiimod_classic_map[] = {
1027     BTN_A,      /* WIIMOD_CLASSIC_KEY_A */
1028     BTN_B,      /* WIIMOD_CLASSIC_KEY_B */
1029     BTN_X,      /* WIIMOD_CLASSIC_KEY_X */
1030     BTN_Y,      /* WIIMOD_CLASSIC_KEY_Y */
1031     BTN_TL2,    /* WIIMOD_CLASSIC_KEY_ZL */
1032     BTN_TR2,    /* WIIMOD_CLASSIC_KEY_ZR */
1033     KEY_NEXT,   /* WIIMOD_CLASSIC_KEY_PLUS */
1034     KEY_PREVIOUS,   /* WIIMOD_CLASSIC_KEY_MINUS */
1035     BTN_MODE,   /* WIIMOD_CLASSIC_KEY_HOME */
1036     KEY_LEFT,   /* WIIMOD_CLASSIC_KEY_LEFT */
1037     KEY_RIGHT,  /* WIIMOD_CLASSIC_KEY_RIGHT */
1038     KEY_UP,     /* WIIMOD_CLASSIC_KEY_UP */
1039     KEY_DOWN,   /* WIIMOD_CLASSIC_KEY_DOWN */
1040     BTN_TL,     /* WIIMOD_CLASSIC_KEY_LT */
1041     BTN_TR,     /* WIIMOD_CLASSIC_KEY_RT */
1042 };
1043 
1044 static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1045 {
1046     __s8 rx, ry, lx, ly, lt, rt;
1047 
1048     /*   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
1049      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1050      *    1   | RX <5:4>  |              LX <5:0>             |
1051      *    2   | RX <3:2>  |              LY <5:0>             |
1052      *   -----+-----+-----+-----+-----------------------------+
1053      *    3   |RX<1>| LT <5:4>  |         RY <5:1>            |
1054      *   -----+-----+-----------+-----------------------------+
1055      *    4   |     LT <3:1>    |         RT <5:1>            |
1056      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1057      *    5   | BDR | BDD | BLT | B-  | BH  | B+  | BRT |  1  |
1058      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1059      *    6   | BZL | BB  | BY  | BA  | BX  | BZR | BDL | BDU |
1060      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1061      * All buttons are 0 if pressed
1062      * RX and RY are right analog stick
1063      * LX and LY are left analog stick
1064      * LT is left trigger, RT is right trigger
1065      * BLT is 0 if left trigger is fully pressed
1066      * BRT is 0 if right trigger is fully pressed
1067      * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1068      * BZL is left Z button and BZR is right Z button
1069      * B-, BH, B+ are +, HOME and - buttons
1070      * BB, BY, BA, BX are A, B, X, Y buttons
1071      * LSB of RX, RY, LT, and RT are not transmitted and always 0.
1072      *
1073      * With motionp enabled it changes slightly to this:
1074      *   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
1075      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1076      *    1   | RX <5:4>  |          LX <5:1>           | BDU |
1077      *    2   | RX <3:2>  |          LY <5:1>           | BDL |
1078      *   -----+-----+-----+-----+-----------------------+-----+
1079      *    3   |RX<1>| LT <5:4>  |         RY <5:1>            |
1080      *   -----+-----+-----------+-----------------------------+
1081      *    4   |     LT <3:1>    |         RT <5:1>            |
1082      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1083      *    5   | BDR | BDD | BLT | B-  | BH  | B+  | BRT | EXT |
1084      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1085      *    6   | BZL | BB  | BY  | BA  | BX  | BZR |  0  |  0  |
1086      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1087      * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
1088      * is the same as before.
1089      */
1090 
1091     static const s8 digital_to_analog[3] = {0x20, 0, -0x20};
1092 
1093     if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1094         if (wiimote_dpad_as_analog) {
1095             lx = digital_to_analog[1 - !(ext[4] & 0x80)
1096                 + !(ext[1] & 0x01)];
1097             ly = digital_to_analog[1 - !(ext[4] & 0x40)
1098                 + !(ext[0] & 0x01)];
1099         } else {
1100             lx = (ext[0] & 0x3e) - 0x20;
1101             ly = (ext[1] & 0x3e) - 0x20;
1102         }
1103     } else {
1104         if (wiimote_dpad_as_analog) {
1105             lx = digital_to_analog[1 - !(ext[4] & 0x80)
1106                 + !(ext[5] & 0x02)];
1107             ly = digital_to_analog[1 - !(ext[4] & 0x40)
1108                 + !(ext[5] & 0x01)];
1109         } else {
1110             lx = (ext[0] & 0x3f) - 0x20;
1111             ly = (ext[1] & 0x3f) - 0x20;
1112         }
1113     }
1114 
1115     rx = (ext[0] >> 3) & 0x18;
1116     rx |= (ext[1] >> 5) & 0x06;
1117     rx |= (ext[2] >> 7) & 0x01;
1118     ry = ext[2] & 0x1f;
1119 
1120     rt = ext[3] & 0x1f;
1121     lt = (ext[2] >> 2) & 0x18;
1122     lt |= (ext[3] >> 5) & 0x07;
1123 
1124     rx <<= 1;
1125     ry <<= 1;
1126     rt <<= 1;
1127     lt <<= 1;
1128 
1129     input_report_abs(wdata->extension.input, ABS_HAT1X, lx);
1130     input_report_abs(wdata->extension.input, ABS_HAT1Y, ly);
1131     input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
1132     input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
1133     input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
1134     input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
1135 
1136     input_report_key(wdata->extension.input,
1137              wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
1138              !(ext[4] & 0x20));
1139     input_report_key(wdata->extension.input,
1140              wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS],
1141              !(ext[4] & 0x10));
1142     input_report_key(wdata->extension.input,
1143              wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME],
1144              !(ext[4] & 0x08));
1145     input_report_key(wdata->extension.input,
1146              wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS],
1147              !(ext[4] & 0x04));
1148     input_report_key(wdata->extension.input,
1149              wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT],
1150              !(ext[4] & 0x02));
1151     input_report_key(wdata->extension.input,
1152              wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL],
1153              !(ext[5] & 0x80));
1154     input_report_key(wdata->extension.input,
1155              wiimod_classic_map[WIIMOD_CLASSIC_KEY_B],
1156              !(ext[5] & 0x40));
1157     input_report_key(wdata->extension.input,
1158              wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y],
1159              !(ext[5] & 0x20));
1160     input_report_key(wdata->extension.input,
1161              wiimod_classic_map[WIIMOD_CLASSIC_KEY_A],
1162              !(ext[5] & 0x10));
1163     input_report_key(wdata->extension.input,
1164              wiimod_classic_map[WIIMOD_CLASSIC_KEY_X],
1165              !(ext[5] & 0x08));
1166     input_report_key(wdata->extension.input,
1167              wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
1168              !(ext[5] & 0x04));
1169 
1170     if (!wiimote_dpad_as_analog) {
1171         input_report_key(wdata->extension.input,
1172                  wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
1173                  !(ext[4] & 0x80));
1174         input_report_key(wdata->extension.input,
1175                  wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
1176                  !(ext[4] & 0x40));
1177 
1178         if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1179             input_report_key(wdata->extension.input,
1180                  wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1181                  !(ext[1] & 0x01));
1182             input_report_key(wdata->extension.input,
1183                  wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1184                  !(ext[0] & 0x01));
1185         } else {
1186             input_report_key(wdata->extension.input,
1187                  wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1188                  !(ext[5] & 0x02));
1189             input_report_key(wdata->extension.input,
1190                  wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1191                  !(ext[5] & 0x01));
1192         }
1193     }
1194 
1195     input_sync(wdata->extension.input);
1196 }
1197 
1198 static int wiimod_classic_open(struct input_dev *dev)
1199 {
1200     struct wiimote_data *wdata = input_get_drvdata(dev);
1201     unsigned long flags;
1202 
1203     spin_lock_irqsave(&wdata->state.lock, flags);
1204     wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1205     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1206     spin_unlock_irqrestore(&wdata->state.lock, flags);
1207 
1208     return 0;
1209 }
1210 
1211 static void wiimod_classic_close(struct input_dev *dev)
1212 {
1213     struct wiimote_data *wdata = input_get_drvdata(dev);
1214     unsigned long flags;
1215 
1216     spin_lock_irqsave(&wdata->state.lock, flags);
1217     wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1218     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1219     spin_unlock_irqrestore(&wdata->state.lock, flags);
1220 }
1221 
1222 static int wiimod_classic_probe(const struct wiimod_ops *ops,
1223                 struct wiimote_data *wdata)
1224 {
1225     int ret, i;
1226 
1227     wdata->extension.input = input_allocate_device();
1228     if (!wdata->extension.input)
1229         return -ENOMEM;
1230 
1231     input_set_drvdata(wdata->extension.input, wdata);
1232     wdata->extension.input->open = wiimod_classic_open;
1233     wdata->extension.input->close = wiimod_classic_close;
1234     wdata->extension.input->dev.parent = &wdata->hdev->dev;
1235     wdata->extension.input->id.bustype = wdata->hdev->bus;
1236     wdata->extension.input->id.vendor = wdata->hdev->vendor;
1237     wdata->extension.input->id.product = wdata->hdev->product;
1238     wdata->extension.input->id.version = wdata->hdev->version;
1239     wdata->extension.input->name = WIIMOTE_NAME " Classic Controller";
1240 
1241     set_bit(EV_KEY, wdata->extension.input->evbit);
1242     for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i)
1243         set_bit(wiimod_classic_map[i],
1244             wdata->extension.input->keybit);
1245 
1246     set_bit(EV_ABS, wdata->extension.input->evbit);
1247     set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1248     set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1249     set_bit(ABS_HAT2X, wdata->extension.input->absbit);
1250     set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
1251     set_bit(ABS_HAT3X, wdata->extension.input->absbit);
1252     set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
1253     input_set_abs_params(wdata->extension.input,
1254                  ABS_HAT1X, -30, 30, 1, 1);
1255     input_set_abs_params(wdata->extension.input,
1256                  ABS_HAT1Y, -30, 30, 1, 1);
1257     input_set_abs_params(wdata->extension.input,
1258                  ABS_HAT2X, -30, 30, 1, 1);
1259     input_set_abs_params(wdata->extension.input,
1260                  ABS_HAT2Y, -30, 30, 1, 1);
1261     input_set_abs_params(wdata->extension.input,
1262                  ABS_HAT3X, -30, 30, 1, 1);
1263     input_set_abs_params(wdata->extension.input,
1264                  ABS_HAT3Y, -30, 30, 1, 1);
1265 
1266     ret = input_register_device(wdata->extension.input);
1267     if (ret)
1268         goto err_free;
1269 
1270     return 0;
1271 
1272 err_free:
1273     input_free_device(wdata->extension.input);
1274     wdata->extension.input = NULL;
1275     return ret;
1276 }
1277 
1278 static void wiimod_classic_remove(const struct wiimod_ops *ops,
1279                   struct wiimote_data *wdata)
1280 {
1281     if (!wdata->extension.input)
1282         return;
1283 
1284     input_unregister_device(wdata->extension.input);
1285     wdata->extension.input = NULL;
1286 }
1287 
1288 static const struct wiimod_ops wiimod_classic = {
1289     .flags = 0,
1290     .arg = 0,
1291     .probe = wiimod_classic_probe,
1292     .remove = wiimod_classic_remove,
1293     .in_ext = wiimod_classic_in_ext,
1294 };
1295 
1296 /*
1297  * Balance Board Extension
1298  * The Nintendo Wii Balance Board provides four hardware weight sensor plus a
1299  * single push button. No other peripherals are available. However, the
1300  * balance-board data is sent via a standard Wii Remote extension. All other
1301  * data for non-present hardware is zeroed out.
1302  * Some 3rd party devices react allergic if we try to access normal Wii Remote
1303  * hardware, so this extension module should be the only module that is loaded
1304  * on balance boards.
1305  * The balance board needs 8 bytes extension data instead of basic 6 bytes so
1306  * it needs the WIIMOD_FLAG_EXT8 flag.
1307  */
1308 
1309 static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
1310 {
1311     input_report_key(wdata->extension.input, BTN_A,
1312              !!(keys[1] & 0x08));
1313     input_sync(wdata->extension.input);
1314 }
1315 
1316 static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
1317                  const __u8 *ext)
1318 {
1319     __s32 val[4], tmp, div;
1320     unsigned int i;
1321     struct wiimote_state *s = &wdata->state;
1322 
1323     /*
1324      * Balance board data layout:
1325      *
1326      *   Byte |  8  7  6  5  4  3  2  1  |
1327      *   -----+--------------------------+
1328      *    1   |    Top Right <15:8>      |
1329      *    2   |    Top Right  <7:0>      |
1330      *   -----+--------------------------+
1331      *    3   | Bottom Right <15:8>      |
1332      *    4   | Bottom Right  <7:0>      |
1333      *   -----+--------------------------+
1334      *    5   |     Top Left <15:8>      |
1335      *    6   |     Top Left  <7:0>      |
1336      *   -----+--------------------------+
1337      *    7   |  Bottom Left <15:8>      |
1338      *    8   |  Bottom Left  <7:0>      |
1339      *   -----+--------------------------+
1340      *
1341      * These values represent the weight-measurements of the Wii-balance
1342      * board with 16bit precision.
1343      *
1344      * The balance-board is never reported interleaved with motionp.
1345      */
1346 
1347     val[0] = ext[0];
1348     val[0] <<= 8;
1349     val[0] |= ext[1];
1350 
1351     val[1] = ext[2];
1352     val[1] <<= 8;
1353     val[1] |= ext[3];
1354 
1355     val[2] = ext[4];
1356     val[2] <<= 8;
1357     val[2] |= ext[5];
1358 
1359     val[3] = ext[6];
1360     val[3] <<= 8;
1361     val[3] |= ext[7];
1362 
1363     /* apply calibration data */
1364     for (i = 0; i < 4; i++) {
1365         if (val[i] <= s->calib_bboard[i][0]) {
1366             tmp = 0;
1367         } else if (val[i] < s->calib_bboard[i][1]) {
1368             tmp = val[i] - s->calib_bboard[i][0];
1369             tmp *= 1700;
1370             div = s->calib_bboard[i][1] - s->calib_bboard[i][0];
1371             tmp /= div ? div : 1;
1372         } else {
1373             tmp = val[i] - s->calib_bboard[i][1];
1374             tmp *= 1700;
1375             div = s->calib_bboard[i][2] - s->calib_bboard[i][1];
1376             tmp /= div ? div : 1;
1377             tmp += 1700;
1378         }
1379         val[i] = tmp;
1380     }
1381 
1382     input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]);
1383     input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]);
1384     input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]);
1385     input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]);
1386     input_sync(wdata->extension.input);
1387 }
1388 
1389 static int wiimod_bboard_open(struct input_dev *dev)
1390 {
1391     struct wiimote_data *wdata = input_get_drvdata(dev);
1392     unsigned long flags;
1393 
1394     spin_lock_irqsave(&wdata->state.lock, flags);
1395     wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1396     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1397     spin_unlock_irqrestore(&wdata->state.lock, flags);
1398 
1399     return 0;
1400 }
1401 
1402 static void wiimod_bboard_close(struct input_dev *dev)
1403 {
1404     struct wiimote_data *wdata = input_get_drvdata(dev);
1405     unsigned long flags;
1406 
1407     spin_lock_irqsave(&wdata->state.lock, flags);
1408     wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1409     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1410     spin_unlock_irqrestore(&wdata->state.lock, flags);
1411 }
1412 
1413 static ssize_t wiimod_bboard_calib_show(struct device *dev,
1414                     struct device_attribute *attr,
1415                     char *out)
1416 {
1417     struct wiimote_data *wdata = dev_to_wii(dev);
1418     int i, j, ret;
1419     __u16 val;
1420     __u8 buf[24], offs;
1421 
1422     ret = wiimote_cmd_acquire(wdata);
1423     if (ret)
1424         return ret;
1425 
1426     ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1427     if (ret != 12) {
1428         wiimote_cmd_release(wdata);
1429         return ret < 0 ? ret : -EIO;
1430     }
1431     ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1432     if (ret != 12) {
1433         wiimote_cmd_release(wdata);
1434         return ret < 0 ? ret : -EIO;
1435     }
1436 
1437     wiimote_cmd_release(wdata);
1438 
1439     spin_lock_irq(&wdata->state.lock);
1440     offs = 0;
1441     for (i = 0; i < 3; ++i) {
1442         for (j = 0; j < 4; ++j) {
1443             wdata->state.calib_bboard[j][i] = buf[offs];
1444             wdata->state.calib_bboard[j][i] <<= 8;
1445             wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1446             offs += 2;
1447         }
1448     }
1449     spin_unlock_irq(&wdata->state.lock);
1450 
1451     ret = 0;
1452     for (i = 0; i < 3; ++i) {
1453         for (j = 0; j < 4; ++j) {
1454             val = wdata->state.calib_bboard[j][i];
1455             if (i == 2 && j == 3)
1456                 ret += sprintf(&out[ret], "%04x\n", val);
1457             else
1458                 ret += sprintf(&out[ret], "%04x:", val);
1459         }
1460     }
1461 
1462     return ret;
1463 }
1464 
1465 static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
1466 
1467 static int wiimod_bboard_probe(const struct wiimod_ops *ops,
1468                    struct wiimote_data *wdata)
1469 {
1470     int ret, i, j;
1471     __u8 buf[24], offs;
1472 
1473     wiimote_cmd_acquire_noint(wdata);
1474 
1475     ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1476     if (ret != 12) {
1477         wiimote_cmd_release(wdata);
1478         return ret < 0 ? ret : -EIO;
1479     }
1480     ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1481     if (ret != 12) {
1482         wiimote_cmd_release(wdata);
1483         return ret < 0 ? ret : -EIO;
1484     }
1485 
1486     wiimote_cmd_release(wdata);
1487 
1488     offs = 0;
1489     for (i = 0; i < 3; ++i) {
1490         for (j = 0; j < 4; ++j) {
1491             wdata->state.calib_bboard[j][i] = buf[offs];
1492             wdata->state.calib_bboard[j][i] <<= 8;
1493             wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1494             offs += 2;
1495         }
1496     }
1497 
1498     wdata->extension.input = input_allocate_device();
1499     if (!wdata->extension.input)
1500         return -ENOMEM;
1501 
1502     ret = device_create_file(&wdata->hdev->dev,
1503                  &dev_attr_bboard_calib);
1504     if (ret) {
1505         hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1506         goto err_free;
1507     }
1508 
1509     input_set_drvdata(wdata->extension.input, wdata);
1510     wdata->extension.input->open = wiimod_bboard_open;
1511     wdata->extension.input->close = wiimod_bboard_close;
1512     wdata->extension.input->dev.parent = &wdata->hdev->dev;
1513     wdata->extension.input->id.bustype = wdata->hdev->bus;
1514     wdata->extension.input->id.vendor = wdata->hdev->vendor;
1515     wdata->extension.input->id.product = wdata->hdev->product;
1516     wdata->extension.input->id.version = wdata->hdev->version;
1517     wdata->extension.input->name = WIIMOTE_NAME " Balance Board";
1518 
1519     set_bit(EV_KEY, wdata->extension.input->evbit);
1520     set_bit(BTN_A, wdata->extension.input->keybit);
1521 
1522     set_bit(EV_ABS, wdata->extension.input->evbit);
1523     set_bit(ABS_HAT0X, wdata->extension.input->absbit);
1524     set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
1525     set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1526     set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1527     input_set_abs_params(wdata->extension.input,
1528                  ABS_HAT0X, 0, 65535, 2, 4);
1529     input_set_abs_params(wdata->extension.input,
1530                  ABS_HAT0Y, 0, 65535, 2, 4);
1531     input_set_abs_params(wdata->extension.input,
1532                  ABS_HAT1X, 0, 65535, 2, 4);
1533     input_set_abs_params(wdata->extension.input,
1534                  ABS_HAT1Y, 0, 65535, 2, 4);
1535 
1536     ret = input_register_device(wdata->extension.input);
1537     if (ret)
1538         goto err_file;
1539 
1540     return 0;
1541 
1542 err_file:
1543     device_remove_file(&wdata->hdev->dev,
1544                &dev_attr_bboard_calib);
1545 err_free:
1546     input_free_device(wdata->extension.input);
1547     wdata->extension.input = NULL;
1548     return ret;
1549 }
1550 
1551 static void wiimod_bboard_remove(const struct wiimod_ops *ops,
1552                  struct wiimote_data *wdata)
1553 {
1554     if (!wdata->extension.input)
1555         return;
1556 
1557     input_unregister_device(wdata->extension.input);
1558     wdata->extension.input = NULL;
1559     device_remove_file(&wdata->hdev->dev,
1560                &dev_attr_bboard_calib);
1561 }
1562 
1563 static const struct wiimod_ops wiimod_bboard = {
1564     .flags = WIIMOD_FLAG_EXT8,
1565     .arg = 0,
1566     .probe = wiimod_bboard_probe,
1567     .remove = wiimod_bboard_remove,
1568     .in_keys = wiimod_bboard_in_keys,
1569     .in_ext = wiimod_bboard_in_ext,
1570 };
1571 
1572 /*
1573  * Pro Controller
1574  * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not
1575  * work together with the classic Wii, but only with the new Wii U. However, it
1576  * uses the same protocol and provides a builtin "classic controller pro"
1577  * extension, few standard buttons, a rumble motor, 4 LEDs and a battery.
1578  * We provide all these via a standard extension device as the device doesn't
1579  * feature an extension port.
1580  */
1581 
1582 enum wiimod_pro_keys {
1583     WIIMOD_PRO_KEY_A,
1584     WIIMOD_PRO_KEY_B,
1585     WIIMOD_PRO_KEY_X,
1586     WIIMOD_PRO_KEY_Y,
1587     WIIMOD_PRO_KEY_PLUS,
1588     WIIMOD_PRO_KEY_MINUS,
1589     WIIMOD_PRO_KEY_HOME,
1590     WIIMOD_PRO_KEY_LEFT,
1591     WIIMOD_PRO_KEY_RIGHT,
1592     WIIMOD_PRO_KEY_UP,
1593     WIIMOD_PRO_KEY_DOWN,
1594     WIIMOD_PRO_KEY_TL,
1595     WIIMOD_PRO_KEY_TR,
1596     WIIMOD_PRO_KEY_ZL,
1597     WIIMOD_PRO_KEY_ZR,
1598     WIIMOD_PRO_KEY_THUMBL,
1599     WIIMOD_PRO_KEY_THUMBR,
1600     WIIMOD_PRO_KEY_NUM,
1601 };
1602 
1603 static const __u16 wiimod_pro_map[] = {
1604     BTN_EAST,   /* WIIMOD_PRO_KEY_A */
1605     BTN_SOUTH,  /* WIIMOD_PRO_KEY_B */
1606     BTN_NORTH,  /* WIIMOD_PRO_KEY_X */
1607     BTN_WEST,   /* WIIMOD_PRO_KEY_Y */
1608     BTN_START,  /* WIIMOD_PRO_KEY_PLUS */
1609     BTN_SELECT, /* WIIMOD_PRO_KEY_MINUS */
1610     BTN_MODE,   /* WIIMOD_PRO_KEY_HOME */
1611     BTN_DPAD_LEFT,  /* WIIMOD_PRO_KEY_LEFT */
1612     BTN_DPAD_RIGHT, /* WIIMOD_PRO_KEY_RIGHT */
1613     BTN_DPAD_UP,    /* WIIMOD_PRO_KEY_UP */
1614     BTN_DPAD_DOWN,  /* WIIMOD_PRO_KEY_DOWN */
1615     BTN_TL,     /* WIIMOD_PRO_KEY_TL */
1616     BTN_TR,     /* WIIMOD_PRO_KEY_TR */
1617     BTN_TL2,    /* WIIMOD_PRO_KEY_ZL */
1618     BTN_TR2,    /* WIIMOD_PRO_KEY_ZR */
1619     BTN_THUMBL, /* WIIMOD_PRO_KEY_THUMBL */
1620     BTN_THUMBR, /* WIIMOD_PRO_KEY_THUMBR */
1621 };
1622 
1623 static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1624 {
1625     __s16 rx, ry, lx, ly;
1626 
1627     /*   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
1628      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1629      *    1   |                   LX <7:0>                    |
1630      *   -----+-----------------------+-----------------------+
1631      *    2   |  0     0     0     0  |       LX <11:8>       |
1632      *   -----+-----------------------+-----------------------+
1633      *    3   |                   RX <7:0>                    |
1634      *   -----+-----------------------+-----------------------+
1635      *    4   |  0     0     0     0  |       RX <11:8>       |
1636      *   -----+-----------------------+-----------------------+
1637      *    5   |                   LY <7:0>                    |
1638      *   -----+-----------------------+-----------------------+
1639      *    6   |  0     0     0     0  |       LY <11:8>       |
1640      *   -----+-----------------------+-----------------------+
1641      *    7   |                   RY <7:0>                    |
1642      *   -----+-----------------------+-----------------------+
1643      *    8   |  0     0     0     0  |       RY <11:8>       |
1644      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1645      *    9   | BDR | BDD | BLT | B-  | BH  | B+  | BRT |  1  |
1646      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1647      *   10   | BZL | BB  | BY  | BA  | BX  | BZR | BDL | BDU |
1648      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1649      *   11   |  1  |     BATTERY     | USB |CHARG|LTHUM|RTHUM|
1650      *   -----+-----+-----------------+-----------+-----+-----+
1651      * All buttons are low-active (0 if pressed)
1652      * RX and RY are right analog stick
1653      * LX and LY are left analog stick
1654      * BLT is left trigger, BRT is right trigger.
1655      * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1656      * BZL is left Z button and BZR is right Z button
1657      * B-, BH, B+ are +, HOME and - buttons
1658      * BB, BY, BA, BX are A, B, X, Y buttons
1659      *
1660      * Bits marked as 0/1 are unknown and never changed during tests.
1661      *
1662      * Not entirely verified:
1663      *   CHARG: 1 if uncharging, 0 if charging
1664      *   USB: 1 if not connected, 0 if connected
1665      *   BATTERY: battery capacity from 000 (empty) to 100 (full)
1666      */
1667 
1668     lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8);
1669     rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8);
1670     ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8);
1671     ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8);
1672 
1673     /* zero-point offsets */
1674     lx -= 0x800;
1675     ly = 0x800 - ly;
1676     rx -= 0x800;
1677     ry = 0x800 - ry;
1678 
1679     /* Trivial automatic calibration. We don't know any calibration data
1680      * in the EEPROM so we must use the first report to calibrate the
1681      * null-position of the analog sticks. Users can retrigger calibration
1682      * via sysfs, or set it explicitly. If data is off more than abs(500),
1683      * we skip calibration as the sticks are likely to be moved already. */
1684     if (!(wdata->state.flags & WIIPROTO_FLAG_PRO_CALIB_DONE)) {
1685         wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1686         if (abs(lx) < 500)
1687             wdata->state.calib_pro_sticks[0] = -lx;
1688         if (abs(ly) < 500)
1689             wdata->state.calib_pro_sticks[1] = -ly;
1690         if (abs(rx) < 500)
1691             wdata->state.calib_pro_sticks[2] = -rx;
1692         if (abs(ry) < 500)
1693             wdata->state.calib_pro_sticks[3] = -ry;
1694     }
1695 
1696     /* apply calibration data */
1697     lx += wdata->state.calib_pro_sticks[0];
1698     ly += wdata->state.calib_pro_sticks[1];
1699     rx += wdata->state.calib_pro_sticks[2];
1700     ry += wdata->state.calib_pro_sticks[3];
1701 
1702     input_report_abs(wdata->extension.input, ABS_X, lx);
1703     input_report_abs(wdata->extension.input, ABS_Y, ly);
1704     input_report_abs(wdata->extension.input, ABS_RX, rx);
1705     input_report_abs(wdata->extension.input, ABS_RY, ry);
1706 
1707     input_report_key(wdata->extension.input,
1708              wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
1709              !(ext[8] & 0x80));
1710     input_report_key(wdata->extension.input,
1711              wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
1712              !(ext[8] & 0x40));
1713     input_report_key(wdata->extension.input,
1714              wiimod_pro_map[WIIMOD_PRO_KEY_TL],
1715              !(ext[8] & 0x20));
1716     input_report_key(wdata->extension.input,
1717              wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
1718              !(ext[8] & 0x10));
1719     input_report_key(wdata->extension.input,
1720              wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
1721              !(ext[8] & 0x08));
1722     input_report_key(wdata->extension.input,
1723              wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
1724              !(ext[8] & 0x04));
1725     input_report_key(wdata->extension.input,
1726              wiimod_pro_map[WIIMOD_PRO_KEY_TR],
1727              !(ext[8] & 0x02));
1728 
1729     input_report_key(wdata->extension.input,
1730              wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
1731              !(ext[9] & 0x80));
1732     input_report_key(wdata->extension.input,
1733              wiimod_pro_map[WIIMOD_PRO_KEY_B],
1734              !(ext[9] & 0x40));
1735     input_report_key(wdata->extension.input,
1736              wiimod_pro_map[WIIMOD_PRO_KEY_Y],
1737              !(ext[9] & 0x20));
1738     input_report_key(wdata->extension.input,
1739              wiimod_pro_map[WIIMOD_PRO_KEY_A],
1740              !(ext[9] & 0x10));
1741     input_report_key(wdata->extension.input,
1742              wiimod_pro_map[WIIMOD_PRO_KEY_X],
1743              !(ext[9] & 0x08));
1744     input_report_key(wdata->extension.input,
1745              wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
1746              !(ext[9] & 0x04));
1747     input_report_key(wdata->extension.input,
1748              wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
1749              !(ext[9] & 0x02));
1750     input_report_key(wdata->extension.input,
1751              wiimod_pro_map[WIIMOD_PRO_KEY_UP],
1752              !(ext[9] & 0x01));
1753 
1754     input_report_key(wdata->extension.input,
1755              wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
1756              !(ext[10] & 0x02));
1757     input_report_key(wdata->extension.input,
1758              wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
1759              !(ext[10] & 0x01));
1760 
1761     input_sync(wdata->extension.input);
1762 }
1763 
1764 static int wiimod_pro_open(struct input_dev *dev)
1765 {
1766     struct wiimote_data *wdata = input_get_drvdata(dev);
1767     unsigned long flags;
1768 
1769     spin_lock_irqsave(&wdata->state.lock, flags);
1770     wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1771     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1772     spin_unlock_irqrestore(&wdata->state.lock, flags);
1773 
1774     return 0;
1775 }
1776 
1777 static void wiimod_pro_close(struct input_dev *dev)
1778 {
1779     struct wiimote_data *wdata = input_get_drvdata(dev);
1780     unsigned long flags;
1781 
1782     spin_lock_irqsave(&wdata->state.lock, flags);
1783     wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1784     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1785     spin_unlock_irqrestore(&wdata->state.lock, flags);
1786 }
1787 
1788 static int wiimod_pro_play(struct input_dev *dev, void *data,
1789                struct ff_effect *eff)
1790 {
1791     struct wiimote_data *wdata = input_get_drvdata(dev);
1792     __u8 value;
1793 
1794     /*
1795      * The wiimote supports only a single rumble motor so if any magnitude
1796      * is set to non-zero then we start the rumble motor. If both are set to
1797      * zero, we stop the rumble motor.
1798      */
1799 
1800     if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
1801         value = 1;
1802     else
1803         value = 0;
1804 
1805     /* Locking state.lock here might deadlock with input_event() calls.
1806      * schedule_work acts as barrier. Merging multiple changes is fine. */
1807     wdata->state.cache_rumble = value;
1808     schedule_work(&wdata->rumble_worker);
1809 
1810     return 0;
1811 }
1812 
1813 static ssize_t wiimod_pro_calib_show(struct device *dev,
1814                      struct device_attribute *attr,
1815                      char *out)
1816 {
1817     struct wiimote_data *wdata = dev_to_wii(dev);
1818     int r;
1819 
1820     r = 0;
1821     r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[0]);
1822     r += sprintf(&out[r], "%+06hd ", wdata->state.calib_pro_sticks[1]);
1823     r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[2]);
1824     r += sprintf(&out[r], "%+06hd\n", wdata->state.calib_pro_sticks[3]);
1825 
1826     return r;
1827 }
1828 
1829 static ssize_t wiimod_pro_calib_store(struct device *dev,
1830                       struct device_attribute *attr,
1831                       const char *buf, size_t count)
1832 {
1833     struct wiimote_data *wdata = dev_to_wii(dev);
1834     int r;
1835     s16 x1, y1, x2, y2;
1836 
1837     if (!strncmp(buf, "scan\n", 5)) {
1838         spin_lock_irq(&wdata->state.lock);
1839         wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1840         spin_unlock_irq(&wdata->state.lock);
1841     } else {
1842         r = sscanf(buf, "%hd:%hd %hd:%hd", &x1, &y1, &x2, &y2);
1843         if (r != 4)
1844             return -EINVAL;
1845 
1846         spin_lock_irq(&wdata->state.lock);
1847         wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1848         spin_unlock_irq(&wdata->state.lock);
1849 
1850         wdata->state.calib_pro_sticks[0] = x1;
1851         wdata->state.calib_pro_sticks[1] = y1;
1852         wdata->state.calib_pro_sticks[2] = x2;
1853         wdata->state.calib_pro_sticks[3] = y2;
1854     }
1855 
1856     return strnlen(buf, PAGE_SIZE);
1857 }
1858 
1859 static DEVICE_ATTR(pro_calib, S_IRUGO|S_IWUSR|S_IWGRP, wiimod_pro_calib_show,
1860            wiimod_pro_calib_store);
1861 
1862 static int wiimod_pro_probe(const struct wiimod_ops *ops,
1863                 struct wiimote_data *wdata)
1864 {
1865     int ret, i;
1866     unsigned long flags;
1867 
1868     INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
1869     wdata->state.calib_pro_sticks[0] = 0;
1870     wdata->state.calib_pro_sticks[1] = 0;
1871     wdata->state.calib_pro_sticks[2] = 0;
1872     wdata->state.calib_pro_sticks[3] = 0;
1873 
1874     spin_lock_irqsave(&wdata->state.lock, flags);
1875     wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1876     spin_unlock_irqrestore(&wdata->state.lock, flags);
1877 
1878     wdata->extension.input = input_allocate_device();
1879     if (!wdata->extension.input)
1880         return -ENOMEM;
1881 
1882     set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
1883     input_set_drvdata(wdata->extension.input, wdata);
1884 
1885     if (input_ff_create_memless(wdata->extension.input, NULL,
1886                     wiimod_pro_play)) {
1887         ret = -ENOMEM;
1888         goto err_free;
1889     }
1890 
1891     ret = device_create_file(&wdata->hdev->dev,
1892                  &dev_attr_pro_calib);
1893     if (ret) {
1894         hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1895         goto err_free;
1896     }
1897 
1898     wdata->extension.input->open = wiimod_pro_open;
1899     wdata->extension.input->close = wiimod_pro_close;
1900     wdata->extension.input->dev.parent = &wdata->hdev->dev;
1901     wdata->extension.input->id.bustype = wdata->hdev->bus;
1902     wdata->extension.input->id.vendor = wdata->hdev->vendor;
1903     wdata->extension.input->id.product = wdata->hdev->product;
1904     wdata->extension.input->id.version = wdata->hdev->version;
1905     wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
1906 
1907     set_bit(EV_KEY, wdata->extension.input->evbit);
1908     for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
1909         set_bit(wiimod_pro_map[i],
1910             wdata->extension.input->keybit);
1911 
1912     set_bit(EV_ABS, wdata->extension.input->evbit);
1913     set_bit(ABS_X, wdata->extension.input->absbit);
1914     set_bit(ABS_Y, wdata->extension.input->absbit);
1915     set_bit(ABS_RX, wdata->extension.input->absbit);
1916     set_bit(ABS_RY, wdata->extension.input->absbit);
1917     input_set_abs_params(wdata->extension.input,
1918                  ABS_X, -0x400, 0x400, 4, 100);
1919     input_set_abs_params(wdata->extension.input,
1920                  ABS_Y, -0x400, 0x400, 4, 100);
1921     input_set_abs_params(wdata->extension.input,
1922                  ABS_RX, -0x400, 0x400, 4, 100);
1923     input_set_abs_params(wdata->extension.input,
1924                  ABS_RY, -0x400, 0x400, 4, 100);
1925 
1926     ret = input_register_device(wdata->extension.input);
1927     if (ret)
1928         goto err_file;
1929 
1930     return 0;
1931 
1932 err_file:
1933     device_remove_file(&wdata->hdev->dev,
1934                &dev_attr_pro_calib);
1935 err_free:
1936     input_free_device(wdata->extension.input);
1937     wdata->extension.input = NULL;
1938     return ret;
1939 }
1940 
1941 static void wiimod_pro_remove(const struct wiimod_ops *ops,
1942                   struct wiimote_data *wdata)
1943 {
1944     unsigned long flags;
1945 
1946     if (!wdata->extension.input)
1947         return;
1948 
1949     input_unregister_device(wdata->extension.input);
1950     wdata->extension.input = NULL;
1951     cancel_work_sync(&wdata->rumble_worker);
1952     device_remove_file(&wdata->hdev->dev,
1953                &dev_attr_pro_calib);
1954 
1955     spin_lock_irqsave(&wdata->state.lock, flags);
1956     wiiproto_req_rumble(wdata, 0);
1957     spin_unlock_irqrestore(&wdata->state.lock, flags);
1958 }
1959 
1960 static const struct wiimod_ops wiimod_pro = {
1961     .flags = WIIMOD_FLAG_EXT16,
1962     .arg = 0,
1963     .probe = wiimod_pro_probe,
1964     .remove = wiimod_pro_remove,
1965     .in_ext = wiimod_pro_in_ext,
1966 };
1967 
1968 /*
1969  * Drums
1970  * Guitar-Hero, Rock-Band and other games came bundled with drums which can
1971  * be plugged as extension to a Wiimote. Drum-reports are still not entirely
1972  * figured out, but the most important information is known.
1973  * We create a separate device for drums and report all information via this
1974  * input device.
1975  */
1976 
1977 static inline void wiimod_drums_report_pressure(struct wiimote_data *wdata,
1978                         __u8 none, __u8 which,
1979                         __u8 pressure, __u8 onoff,
1980                         __u8 *store, __u16 code,
1981                         __u8 which_code)
1982 {
1983     static const __u8 default_pressure = 3;
1984 
1985     if (!none && which == which_code) {
1986         *store = pressure;
1987         input_report_abs(wdata->extension.input, code, *store);
1988     } else if (onoff != !!*store) {
1989         *store = onoff ? default_pressure : 0;
1990         input_report_abs(wdata->extension.input, code, *store);
1991     }
1992 }
1993 
1994 static void wiimod_drums_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1995 {
1996     __u8 pressure, which, none, hhp, sx, sy;
1997     __u8 o, r, y, g, b, bass, bm, bp;
1998 
1999     /*   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
2000      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2001      *    1   |  0  |  0  |              SX <5:0>             |
2002      *    2   |  0  |  0  |              SY <5:0>             |
2003      *   -----+-----+-----+-----------------------------+-----+
2004      *    3   | HPP | NON |         WHICH <5:1>         |  ?  |
2005      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2006      *    4   |   SOFT <7:5>    |  0  |  1  |  1  |  0  |  ?  |
2007      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2008      *    5   |  ?  |  1  |  1  | B-  |  1  | B+  |  1  |  ?  |
2009      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2010      *    6   |  O  |  R  |  Y  |  G  |  B  | BSS |  1  |  1  |
2011      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2012      * All buttons are 0 if pressed
2013      *
2014      * With Motion+ enabled, the following bits will get invalid:
2015      *   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
2016      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2017      *    1   |  0  |  0  |              SX <5:1>       |XXXXX|
2018      *    2   |  0  |  0  |              SY <5:1>       |XXXXX|
2019      *   -----+-----+-----+-----------------------------+-----+
2020      *    3   | HPP | NON |         WHICH <5:1>         |  ?  |
2021      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2022      *    4   |   SOFT <7:5>    |  0  |  1  |  1  |  0  |  ?  |
2023      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2024      *    5   |  ?  |  1  |  1  | B-  |  1  | B+  |  1  |XXXXX|
2025      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2026      *    6   |  O  |  R  |  Y  |  G  |  B  | BSS |XXXXX|XXXXX|
2027      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2028      */
2029 
2030     pressure = 7 - (ext[3] >> 5);
2031     which = (ext[2] >> 1) & 0x1f;
2032     none = !!(ext[2] & 0x40);
2033     hhp = !(ext[2] & 0x80);
2034     sx = ext[0] & 0x3f;
2035     sy = ext[1] & 0x3f;
2036     o = !(ext[5] & 0x80);
2037     r = !(ext[5] & 0x40);
2038     y = !(ext[5] & 0x20);
2039     g = !(ext[5] & 0x10);
2040     b = !(ext[5] & 0x08);
2041     bass = !(ext[5] & 0x04);
2042     bm = !(ext[4] & 0x10);
2043     bp = !(ext[4] & 0x04);
2044 
2045     if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
2046         sx &= 0x3e;
2047         sy &= 0x3e;
2048     }
2049 
2050     wiimod_drums_report_pressure(wdata, none, which, pressure,
2051                      o, &wdata->state.pressure_drums[0],
2052                      ABS_HAT2Y, 0x0e);
2053     wiimod_drums_report_pressure(wdata, none, which, pressure,
2054                      r, &wdata->state.pressure_drums[1],
2055                      ABS_HAT0X, 0x19);
2056     wiimod_drums_report_pressure(wdata, none, which, pressure,
2057                      y, &wdata->state.pressure_drums[2],
2058                      ABS_HAT2X, 0x11);
2059     wiimod_drums_report_pressure(wdata, none, which, pressure,
2060                      g, &wdata->state.pressure_drums[3],
2061                      ABS_HAT1X, 0x12);
2062     wiimod_drums_report_pressure(wdata, none, which, pressure,
2063                      b, &wdata->state.pressure_drums[4],
2064                      ABS_HAT0Y, 0x0f);
2065 
2066     /* Bass shares pressure with hi-hat (set via hhp) */
2067     wiimod_drums_report_pressure(wdata, none, hhp ? 0xff : which, pressure,
2068                      bass, &wdata->state.pressure_drums[5],
2069                      ABS_HAT3X, 0x1b);
2070     /* Hi-hat has no on/off values, just pressure. Force to off/0. */
2071     wiimod_drums_report_pressure(wdata, none, hhp ? which : 0xff, pressure,
2072                      0, &wdata->state.pressure_drums[6],
2073                      ABS_HAT3Y, 0x0e);
2074 
2075     input_report_abs(wdata->extension.input, ABS_X, sx - 0x20);
2076     input_report_abs(wdata->extension.input, ABS_Y, sy - 0x20);
2077 
2078     input_report_key(wdata->extension.input, BTN_START, bp);
2079     input_report_key(wdata->extension.input, BTN_SELECT, bm);
2080 
2081     input_sync(wdata->extension.input);
2082 }
2083 
2084 static int wiimod_drums_open(struct input_dev *dev)
2085 {
2086     struct wiimote_data *wdata = input_get_drvdata(dev);
2087     unsigned long flags;
2088 
2089     spin_lock_irqsave(&wdata->state.lock, flags);
2090     wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
2091     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2092     spin_unlock_irqrestore(&wdata->state.lock, flags);
2093 
2094     return 0;
2095 }
2096 
2097 static void wiimod_drums_close(struct input_dev *dev)
2098 {
2099     struct wiimote_data *wdata = input_get_drvdata(dev);
2100     unsigned long flags;
2101 
2102     spin_lock_irqsave(&wdata->state.lock, flags);
2103     wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
2104     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2105     spin_unlock_irqrestore(&wdata->state.lock, flags);
2106 }
2107 
2108 static int wiimod_drums_probe(const struct wiimod_ops *ops,
2109                   struct wiimote_data *wdata)
2110 {
2111     int ret;
2112 
2113     wdata->extension.input = input_allocate_device();
2114     if (!wdata->extension.input)
2115         return -ENOMEM;
2116 
2117     input_set_drvdata(wdata->extension.input, wdata);
2118     wdata->extension.input->open = wiimod_drums_open;
2119     wdata->extension.input->close = wiimod_drums_close;
2120     wdata->extension.input->dev.parent = &wdata->hdev->dev;
2121     wdata->extension.input->id.bustype = wdata->hdev->bus;
2122     wdata->extension.input->id.vendor = wdata->hdev->vendor;
2123     wdata->extension.input->id.product = wdata->hdev->product;
2124     wdata->extension.input->id.version = wdata->hdev->version;
2125     wdata->extension.input->name = WIIMOTE_NAME " Drums";
2126 
2127     set_bit(EV_KEY, wdata->extension.input->evbit);
2128     set_bit(BTN_START, wdata->extension.input->keybit);
2129     set_bit(BTN_SELECT, wdata->extension.input->keybit);
2130 
2131     set_bit(EV_ABS, wdata->extension.input->evbit);
2132     set_bit(ABS_X, wdata->extension.input->absbit);
2133     set_bit(ABS_Y, wdata->extension.input->absbit);
2134     set_bit(ABS_HAT0X, wdata->extension.input->absbit);
2135     set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
2136     set_bit(ABS_HAT1X, wdata->extension.input->absbit);
2137     set_bit(ABS_HAT2X, wdata->extension.input->absbit);
2138     set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
2139     set_bit(ABS_HAT3X, wdata->extension.input->absbit);
2140     set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
2141     input_set_abs_params(wdata->extension.input,
2142                  ABS_X, -32, 31, 1, 1);
2143     input_set_abs_params(wdata->extension.input,
2144                  ABS_Y, -32, 31, 1, 1);
2145     input_set_abs_params(wdata->extension.input,
2146                  ABS_HAT0X, 0, 7, 0, 0);
2147     input_set_abs_params(wdata->extension.input,
2148                  ABS_HAT0Y, 0, 7, 0, 0);
2149     input_set_abs_params(wdata->extension.input,
2150                  ABS_HAT1X, 0, 7, 0, 0);
2151     input_set_abs_params(wdata->extension.input,
2152                  ABS_HAT2X, 0, 7, 0, 0);
2153     input_set_abs_params(wdata->extension.input,
2154                  ABS_HAT2Y, 0, 7, 0, 0);
2155     input_set_abs_params(wdata->extension.input,
2156                  ABS_HAT3X, 0, 7, 0, 0);
2157     input_set_abs_params(wdata->extension.input,
2158                  ABS_HAT3Y, 0, 7, 0, 0);
2159 
2160     ret = input_register_device(wdata->extension.input);
2161     if (ret)
2162         goto err_free;
2163 
2164     return 0;
2165 
2166 err_free:
2167     input_free_device(wdata->extension.input);
2168     wdata->extension.input = NULL;
2169     return ret;
2170 }
2171 
2172 static void wiimod_drums_remove(const struct wiimod_ops *ops,
2173                 struct wiimote_data *wdata)
2174 {
2175     if (!wdata->extension.input)
2176         return;
2177 
2178     input_unregister_device(wdata->extension.input);
2179     wdata->extension.input = NULL;
2180 }
2181 
2182 static const struct wiimod_ops wiimod_drums = {
2183     .flags = 0,
2184     .arg = 0,
2185     .probe = wiimod_drums_probe,
2186     .remove = wiimod_drums_remove,
2187     .in_ext = wiimod_drums_in_ext,
2188 };
2189 
2190 /*
2191  * Guitar
2192  * Guitar-Hero, Rock-Band and other games came bundled with guitars which can
2193  * be plugged as extension to a Wiimote.
2194  * We create a separate device for guitars and report all information via this
2195  * input device.
2196  */
2197 
2198 enum wiimod_guitar_keys {
2199     WIIMOD_GUITAR_KEY_G,
2200     WIIMOD_GUITAR_KEY_R,
2201     WIIMOD_GUITAR_KEY_Y,
2202     WIIMOD_GUITAR_KEY_B,
2203     WIIMOD_GUITAR_KEY_O,
2204     WIIMOD_GUITAR_KEY_UP,
2205     WIIMOD_GUITAR_KEY_DOWN,
2206     WIIMOD_GUITAR_KEY_PLUS,
2207     WIIMOD_GUITAR_KEY_MINUS,
2208     WIIMOD_GUITAR_KEY_NUM,
2209 };
2210 
2211 static const __u16 wiimod_guitar_map[] = {
2212     BTN_1,          /* WIIMOD_GUITAR_KEY_G */
2213     BTN_2,          /* WIIMOD_GUITAR_KEY_R */
2214     BTN_3,          /* WIIMOD_GUITAR_KEY_Y */
2215     BTN_4,          /* WIIMOD_GUITAR_KEY_B */
2216     BTN_5,          /* WIIMOD_GUITAR_KEY_O */
2217     BTN_DPAD_UP,        /* WIIMOD_GUITAR_KEY_UP */
2218     BTN_DPAD_DOWN,      /* WIIMOD_GUITAR_KEY_DOWN */
2219     BTN_START,      /* WIIMOD_GUITAR_KEY_PLUS */
2220     BTN_SELECT,     /* WIIMOD_GUITAR_KEY_MINUS */
2221 };
2222 
2223 static void wiimod_guitar_in_ext(struct wiimote_data *wdata, const __u8 *ext)
2224 {
2225     __u8 sx, sy, tb, wb, bd, bm, bp, bo, br, bb, bg, by, bu;
2226 
2227     /*   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
2228      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2229      *    1   |  0  |  0  |              SX <5:0>             |
2230      *    2   |  0  |  0  |              SY <5:0>             |
2231      *   -----+-----+-----+-----+-----------------------------+
2232      *    3   |  0  |  0  |  0  |      TB <4:0>               |
2233      *   -----+-----+-----+-----+-----------------------------+
2234      *    4   |  0  |  0  |  0  |      WB <4:0>               |
2235      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2236      *    5   |  1  | BD  |  1  | B-  |  1  | B+  |  1  |  1  |
2237      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2238      *    6   | BO  | BR  | BB  | BG  | BY  |  1  |  1  | BU  |
2239      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2240      * All buttons are 0 if pressed
2241      *
2242      * With Motion+ enabled, it will look like this:
2243      *   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
2244      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2245      *    1   |  0  |  0  |              SX <5:1>       | BU  |
2246      *    2   |  0  |  0  |              SY <5:1>       |  1  |
2247      *   -----+-----+-----+-----+-----------------------+-----+
2248      *    3   |  0  |  0  |  0  |      TB <4:0>               |
2249      *   -----+-----+-----+-----+-----------------------------+
2250      *    4   |  0  |  0  |  0  |      WB <4:0>               |
2251      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2252      *    5   |  1  | BD  |  1  | B-  |  1  | B+  |  1  |XXXXX|
2253      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2254      *    6   | BO  | BR  | BB  | BG  | BY  |  1  |XXXXX|XXXXX|
2255      *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
2256      */
2257 
2258     sx = ext[0] & 0x3f;
2259     sy = ext[1] & 0x3f;
2260     tb = ext[2] & 0x1f;
2261     wb = ext[3] & 0x1f;
2262     bd = !(ext[4] & 0x40);
2263     bm = !(ext[4] & 0x10);
2264     bp = !(ext[4] & 0x04);
2265     bo = !(ext[5] & 0x80);
2266     br = !(ext[5] & 0x40);
2267     bb = !(ext[5] & 0x20);
2268     bg = !(ext[5] & 0x10);
2269     by = !(ext[5] & 0x08);
2270     bu = !(ext[5] & 0x01);
2271 
2272     if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
2273         bu = !(ext[0] & 0x01);
2274         sx &= 0x3e;
2275         sy &= 0x3e;
2276     }
2277 
2278     input_report_abs(wdata->extension.input, ABS_X, sx - 0x20);
2279     input_report_abs(wdata->extension.input, ABS_Y, sy - 0x20);
2280     input_report_abs(wdata->extension.input, ABS_HAT0X, tb);
2281     input_report_abs(wdata->extension.input, ABS_HAT1X, wb - 0x10);
2282 
2283     input_report_key(wdata->extension.input,
2284              wiimod_guitar_map[WIIMOD_GUITAR_KEY_G],
2285              bg);
2286     input_report_key(wdata->extension.input,
2287              wiimod_guitar_map[WIIMOD_GUITAR_KEY_R],
2288              br);
2289     input_report_key(wdata->extension.input,
2290              wiimod_guitar_map[WIIMOD_GUITAR_KEY_Y],
2291              by);
2292     input_report_key(wdata->extension.input,
2293              wiimod_guitar_map[WIIMOD_GUITAR_KEY_B],
2294              bb);
2295     input_report_key(wdata->extension.input,
2296              wiimod_guitar_map[WIIMOD_GUITAR_KEY_O],
2297              bo);
2298     input_report_key(wdata->extension.input,
2299              wiimod_guitar_map[WIIMOD_GUITAR_KEY_UP],
2300              bu);
2301     input_report_key(wdata->extension.input,
2302              wiimod_guitar_map[WIIMOD_GUITAR_KEY_DOWN],
2303              bd);
2304     input_report_key(wdata->extension.input,
2305              wiimod_guitar_map[WIIMOD_GUITAR_KEY_PLUS],
2306              bp);
2307     input_report_key(wdata->extension.input,
2308              wiimod_guitar_map[WIIMOD_GUITAR_KEY_MINUS],
2309              bm);
2310 
2311     input_sync(wdata->extension.input);
2312 }
2313 
2314 static int wiimod_guitar_open(struct input_dev *dev)
2315 {
2316     struct wiimote_data *wdata = input_get_drvdata(dev);
2317     unsigned long flags;
2318 
2319     spin_lock_irqsave(&wdata->state.lock, flags);
2320     wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
2321     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2322     spin_unlock_irqrestore(&wdata->state.lock, flags);
2323 
2324     return 0;
2325 }
2326 
2327 static void wiimod_guitar_close(struct input_dev *dev)
2328 {
2329     struct wiimote_data *wdata = input_get_drvdata(dev);
2330     unsigned long flags;
2331 
2332     spin_lock_irqsave(&wdata->state.lock, flags);
2333     wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
2334     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2335     spin_unlock_irqrestore(&wdata->state.lock, flags);
2336 }
2337 
2338 static int wiimod_guitar_probe(const struct wiimod_ops *ops,
2339                    struct wiimote_data *wdata)
2340 {
2341     int ret, i;
2342 
2343     wdata->extension.input = input_allocate_device();
2344     if (!wdata->extension.input)
2345         return -ENOMEM;
2346 
2347     input_set_drvdata(wdata->extension.input, wdata);
2348     wdata->extension.input->open = wiimod_guitar_open;
2349     wdata->extension.input->close = wiimod_guitar_close;
2350     wdata->extension.input->dev.parent = &wdata->hdev->dev;
2351     wdata->extension.input->id.bustype = wdata->hdev->bus;
2352     wdata->extension.input->id.vendor = wdata->hdev->vendor;
2353     wdata->extension.input->id.product = wdata->hdev->product;
2354     wdata->extension.input->id.version = wdata->hdev->version;
2355     wdata->extension.input->name = WIIMOTE_NAME " Guitar";
2356 
2357     set_bit(EV_KEY, wdata->extension.input->evbit);
2358     for (i = 0; i < WIIMOD_GUITAR_KEY_NUM; ++i)
2359         set_bit(wiimod_guitar_map[i],
2360             wdata->extension.input->keybit);
2361 
2362     set_bit(EV_ABS, wdata->extension.input->evbit);
2363     set_bit(ABS_X, wdata->extension.input->absbit);
2364     set_bit(ABS_Y, wdata->extension.input->absbit);
2365     set_bit(ABS_HAT0X, wdata->extension.input->absbit);
2366     set_bit(ABS_HAT1X, wdata->extension.input->absbit);
2367     input_set_abs_params(wdata->extension.input,
2368                  ABS_X, -32, 31, 1, 1);
2369     input_set_abs_params(wdata->extension.input,
2370                  ABS_Y, -32, 31, 1, 1);
2371     input_set_abs_params(wdata->extension.input,
2372                  ABS_HAT0X, 0, 0x1f, 1, 1);
2373     input_set_abs_params(wdata->extension.input,
2374                  ABS_HAT1X, 0, 0x0f, 1, 1);
2375 
2376     ret = input_register_device(wdata->extension.input);
2377     if (ret)
2378         goto err_free;
2379 
2380     return 0;
2381 
2382 err_free:
2383     input_free_device(wdata->extension.input);
2384     wdata->extension.input = NULL;
2385     return ret;
2386 }
2387 
2388 static void wiimod_guitar_remove(const struct wiimod_ops *ops,
2389                  struct wiimote_data *wdata)
2390 {
2391     if (!wdata->extension.input)
2392         return;
2393 
2394     input_unregister_device(wdata->extension.input);
2395     wdata->extension.input = NULL;
2396 }
2397 
2398 static const struct wiimod_ops wiimod_guitar = {
2399     .flags = 0,
2400     .arg = 0,
2401     .probe = wiimod_guitar_probe,
2402     .remove = wiimod_guitar_remove,
2403     .in_ext = wiimod_guitar_in_ext,
2404 };
2405 
2406 /*
2407  * Builtin Motion Plus
2408  * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
2409  * disables polling for Motion-Plus. This should be set only for devices which
2410  * don't allow MP hotplugging.
2411  */
2412 
2413 static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
2414                    struct wiimote_data *wdata)
2415 {
2416     unsigned long flags;
2417 
2418     spin_lock_irqsave(&wdata->state.lock, flags);
2419     wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
2420     spin_unlock_irqrestore(&wdata->state.lock, flags);
2421 
2422     return 0;
2423 }
2424 
2425 static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
2426                      struct wiimote_data *wdata)
2427 {
2428     unsigned long flags;
2429 
2430     spin_lock_irqsave(&wdata->state.lock, flags);
2431     wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
2432     spin_unlock_irqrestore(&wdata->state.lock, flags);
2433 }
2434 
2435 static const struct wiimod_ops wiimod_builtin_mp = {
2436     .flags = 0,
2437     .arg = 0,
2438     .probe = wiimod_builtin_mp_probe,
2439     .remove = wiimod_builtin_mp_remove,
2440 };
2441 
2442 /*
2443  * No Motion Plus
2444  * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
2445  * disables motion-plus. This is needed for devices that advertise this but we
2446  * don't know how to use it (or whether it is actually present).
2447  */
2448 
2449 static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
2450                   struct wiimote_data *wdata)
2451 {
2452     unsigned long flags;
2453 
2454     spin_lock_irqsave(&wdata->state.lock, flags);
2455     wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2456     spin_unlock_irqrestore(&wdata->state.lock, flags);
2457 
2458     return 0;
2459 }
2460 
2461 static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
2462                 struct wiimote_data *wdata)
2463 {
2464     unsigned long flags;
2465 
2466     spin_lock_irqsave(&wdata->state.lock, flags);
2467     wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2468     spin_unlock_irqrestore(&wdata->state.lock, flags);
2469 }
2470 
2471 static const struct wiimod_ops wiimod_no_mp = {
2472     .flags = 0,
2473     .arg = 0,
2474     .probe = wiimod_no_mp_probe,
2475     .remove = wiimod_no_mp_remove,
2476 };
2477 
2478 /*
2479  * Motion Plus
2480  * The Motion Plus extension provides rotation sensors (gyro) as a small
2481  * extension device for Wii Remotes. Many devices have them built-in so
2482  * you cannot see them from the outside.
2483  * Motion Plus extensions are special because they are on a separate extension
2484  * port and allow other extensions to be used simultaneously. This is all
2485  * handled by the Wiimote Core so we don't have to deal with it.
2486  */
2487 
2488 static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
2489 {
2490     __s32 x, y, z;
2491 
2492     /*        |   8    7    6    5    4    3 |  2  |  1  |
2493      *   -----+------------------------------+-----+-----+
2494      *    1   |               Yaw Speed <7:0>            |
2495      *    2   |              Roll Speed <7:0>            |
2496      *    3   |             Pitch Speed <7:0>            |
2497      *   -----+------------------------------+-----+-----+
2498      *    4   |       Yaw Speed <13:8>       | Yaw |Pitch|
2499      *   -----+------------------------------+-----+-----+
2500      *    5   |      Roll Speed <13:8>       |Roll | Ext |
2501      *   -----+------------------------------+-----+-----+
2502      *    6   |     Pitch Speed <13:8>       |  1  |  0  |
2503      *   -----+------------------------------+-----+-----+
2504      * The single bits Yaw, Roll, Pitch in the lower right corner specify
2505      * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
2506      * roation is 8192/440 units / deg/s and for fast rotation 8192/2000
2507      * units / deg/s. To get a linear scale for fast rotation we multiply
2508      * by 2000/440 = ~4.5454 and scale both fast and slow by 9 to match the
2509      * previous scale reported by this driver.
2510      * This leaves a linear scale with 8192*9/440 (~167.564) units / deg/s.
2511      * If the wiimote is not rotating the sensor reports 2^13 = 8192.
2512      * Ext specifies whether an extension is connected to the motionp.
2513      * which is parsed by wiimote-core.
2514      */
2515 
2516     x = ext[0];
2517     y = ext[1];
2518     z = ext[2];
2519 
2520     x |= (((__u16)ext[3]) << 6) & 0xff00;
2521     y |= (((__u16)ext[4]) << 6) & 0xff00;
2522     z |= (((__u16)ext[5]) << 6) & 0xff00;
2523 
2524     x -= 8192;
2525     y -= 8192;
2526     z -= 8192;
2527 
2528     if (!(ext[3] & 0x02))
2529         x = (x * 2000 * 9) / 440;
2530     else
2531         x *= 9;
2532     if (!(ext[4] & 0x02))
2533         y = (y * 2000 * 9) / 440;
2534     else
2535         y *= 9;
2536     if (!(ext[3] & 0x01))
2537         z = (z * 2000 * 9) / 440;
2538     else
2539         z *= 9;
2540 
2541     input_report_abs(wdata->mp, ABS_RX, x);
2542     input_report_abs(wdata->mp, ABS_RY, y);
2543     input_report_abs(wdata->mp, ABS_RZ, z);
2544     input_sync(wdata->mp);
2545 }
2546 
2547 static int wiimod_mp_open(struct input_dev *dev)
2548 {
2549     struct wiimote_data *wdata = input_get_drvdata(dev);
2550     unsigned long flags;
2551 
2552     spin_lock_irqsave(&wdata->state.lock, flags);
2553     wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
2554     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2555     __wiimote_schedule(wdata);
2556     spin_unlock_irqrestore(&wdata->state.lock, flags);
2557 
2558     return 0;
2559 }
2560 
2561 static void wiimod_mp_close(struct input_dev *dev)
2562 {
2563     struct wiimote_data *wdata = input_get_drvdata(dev);
2564     unsigned long flags;
2565 
2566     spin_lock_irqsave(&wdata->state.lock, flags);
2567     wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
2568     wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2569     __wiimote_schedule(wdata);
2570     spin_unlock_irqrestore(&wdata->state.lock, flags);
2571 }
2572 
2573 static int wiimod_mp_probe(const struct wiimod_ops *ops,
2574                struct wiimote_data *wdata)
2575 {
2576     int ret;
2577 
2578     wdata->mp = input_allocate_device();
2579     if (!wdata->mp)
2580         return -ENOMEM;
2581 
2582     input_set_drvdata(wdata->mp, wdata);
2583     wdata->mp->open = wiimod_mp_open;
2584     wdata->mp->close = wiimod_mp_close;
2585     wdata->mp->dev.parent = &wdata->hdev->dev;
2586     wdata->mp->id.bustype = wdata->hdev->bus;
2587     wdata->mp->id.vendor = wdata->hdev->vendor;
2588     wdata->mp->id.product = wdata->hdev->product;
2589     wdata->mp->id.version = wdata->hdev->version;
2590     wdata->mp->name = WIIMOTE_NAME " Motion Plus";
2591 
2592     set_bit(EV_ABS, wdata->mp->evbit);
2593     set_bit(ABS_RX, wdata->mp->absbit);
2594     set_bit(ABS_RY, wdata->mp->absbit);
2595     set_bit(ABS_RZ, wdata->mp->absbit);
2596     input_set_abs_params(wdata->mp,
2597                  ABS_RX, -16000, 16000, 4, 8);
2598     input_set_abs_params(wdata->mp,
2599                  ABS_RY, -16000, 16000, 4, 8);
2600     input_set_abs_params(wdata->mp,
2601                  ABS_RZ, -16000, 16000, 4, 8);
2602 
2603     ret = input_register_device(wdata->mp);
2604     if (ret)
2605         goto err_free;
2606 
2607     return 0;
2608 
2609 err_free:
2610     input_free_device(wdata->mp);
2611     wdata->mp = NULL;
2612     return ret;
2613 }
2614 
2615 static void wiimod_mp_remove(const struct wiimod_ops *ops,
2616                  struct wiimote_data *wdata)
2617 {
2618     if (!wdata->mp)
2619         return;
2620 
2621     input_unregister_device(wdata->mp);
2622     wdata->mp = NULL;
2623 }
2624 
2625 const struct wiimod_ops wiimod_mp = {
2626     .flags = 0,
2627     .arg = 0,
2628     .probe = wiimod_mp_probe,
2629     .remove = wiimod_mp_remove,
2630     .in_mp = wiimod_mp_in_mp,
2631 };
2632 
2633 /* module table */
2634 
2635 static const struct wiimod_ops wiimod_dummy;
2636 
2637 const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
2638     [WIIMOD_KEYS] = &wiimod_keys,
2639     [WIIMOD_RUMBLE] = &wiimod_rumble,
2640     [WIIMOD_BATTERY] = &wiimod_battery,
2641     [WIIMOD_LED1] = &wiimod_leds[0],
2642     [WIIMOD_LED2] = &wiimod_leds[1],
2643     [WIIMOD_LED3] = &wiimod_leds[2],
2644     [WIIMOD_LED4] = &wiimod_leds[3],
2645     [WIIMOD_ACCEL] = &wiimod_accel,
2646     [WIIMOD_IR] = &wiimod_ir,
2647     [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
2648     [WIIMOD_NO_MP] = &wiimod_no_mp,
2649 };
2650 
2651 const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
2652     [WIIMOTE_EXT_NONE] = &wiimod_dummy,
2653     [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
2654     [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
2655     [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
2656     [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
2657     [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,
2658     [WIIMOTE_EXT_DRUMS] = &wiimod_drums,
2659     [WIIMOTE_EXT_GUITAR] = &wiimod_guitar,
2660 };