Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / hid / hid-wiimote-modules.c
1 /*
2  * Device Modules for Nintendo Wii / Wii U HID Driver
3  * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
4  */
5
6 /*
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  */
12
13 /*
14  * Wiimote Modules
15  * Nintendo devices provide different peripherals and many new devices lack
16  * initial features like the IR camera. Therefore, each peripheral device is
17  * implemented as an independent module and we probe on each device only the
18  * modules for the hardware that really is available.
19  *
20  * Module registration is sequential. Unregistration is done in reverse order.
21  * After device detection, the needed modules are loaded. Users can trigger
22  * re-detection which causes all modules to be unloaded and then reload the
23  * modules for the new detected device.
24  *
25  * wdata->input is a shared input device. It is always initialized prior to
26  * module registration. If at least one registered module is marked as
27  * WIIMOD_FLAG_INPUT, then the input device will get registered after all
28  * modules were registered.
29  * Please note that it is unregistered _before_ the "remove" callbacks are
30  * called. This guarantees that no input interaction is done, anymore. However,
31  * the wiimote core keeps a reference to the input device so it is freed only
32  * after all modules were removed. It is safe to send events to unregistered
33  * input devices.
34  */
35
36 #include <linux/device.h>
37 #include <linux/hid.h>
38 #include <linux/input.h>
39 #include <linux/spinlock.h>
40 #include "hid-wiimote.h"
41
42 /*
43  * Keys
44  * The initial Wii Remote provided a bunch of buttons that are reported as
45  * part of the core protocol. Many later devices dropped these and report
46  * invalid data in the core button reports. Load this only on devices which
47  * correctly send button reports.
48  * It uses the shared input device.
49  */
50
51 static const __u16 wiimod_keys_map[] = {
52         KEY_LEFT,       /* WIIPROTO_KEY_LEFT */
53         KEY_RIGHT,      /* WIIPROTO_KEY_RIGHT */
54         KEY_UP,         /* WIIPROTO_KEY_UP */
55         KEY_DOWN,       /* WIIPROTO_KEY_DOWN */
56         KEY_NEXT,       /* WIIPROTO_KEY_PLUS */
57         KEY_PREVIOUS,   /* WIIPROTO_KEY_MINUS */
58         BTN_1,          /* WIIPROTO_KEY_ONE */
59         BTN_2,          /* WIIPROTO_KEY_TWO */
60         BTN_A,          /* WIIPROTO_KEY_A */
61         BTN_B,          /* WIIPROTO_KEY_B */
62         BTN_MODE,       /* WIIPROTO_KEY_HOME */
63 };
64
65 static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
66 {
67         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
68                                                         !!(keys[0] & 0x01));
69         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
70                                                         !!(keys[0] & 0x02));
71         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
72                                                         !!(keys[0] & 0x04));
73         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
74                                                         !!(keys[0] & 0x08));
75         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
76                                                         !!(keys[0] & 0x10));
77         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
78                                                         !!(keys[1] & 0x01));
79         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
80                                                         !!(keys[1] & 0x02));
81         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
82                                                         !!(keys[1] & 0x04));
83         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
84                                                         !!(keys[1] & 0x08));
85         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
86                                                         !!(keys[1] & 0x10));
87         input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
88                                                         !!(keys[1] & 0x80));
89         input_sync(wdata->input);
90 }
91
92 static int wiimod_keys_probe(const struct wiimod_ops *ops,
93                              struct wiimote_data *wdata)
94 {
95         unsigned int i;
96
97         set_bit(EV_KEY, wdata->input->evbit);
98         for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
99                 set_bit(wiimod_keys_map[i], wdata->input->keybit);
100
101         return 0;
102 }
103
104 static const struct wiimod_ops wiimod_keys = {
105         .flags = WIIMOD_FLAG_INPUT,
106         .arg = 0,
107         .probe = wiimod_keys_probe,
108         .remove = NULL,
109         .in_keys = wiimod_keys_in_keys,
110 };
111
112 /*
113  * Rumble
114  * Nearly all devices provide a rumble feature. A small motor for
115  * force-feedback effects. We provide an FF_RUMBLE memless ff device on the
116  * shared input device if this module is loaded.
117  * The rumble motor is controlled via a flag on almost every output report so
118  * the wiimote core handles the rumble flag. But if a device doesn't provide
119  * the rumble motor, this flag shouldn't be set.
120  */
121
122 /* used by wiimod_rumble and wiipro_rumble */
123 static void wiimod_rumble_worker(struct work_struct *work)
124 {
125         struct wiimote_data *wdata = container_of(work, struct wiimote_data,
126                                                   rumble_worker);
127
128         spin_lock_irq(&wdata->state.lock);
129         wiiproto_req_rumble(wdata, wdata->state.cache_rumble);
130         spin_unlock_irq(&wdata->state.lock);
131 }
132
133 static int wiimod_rumble_play(struct input_dev *dev, void *data,
134                               struct ff_effect *eff)
135 {
136         struct wiimote_data *wdata = input_get_drvdata(dev);
137         __u8 value;
138
139         /*
140          * The wiimote supports only a single rumble motor so if any magnitude
141          * is set to non-zero then we start the rumble motor. If both are set to
142          * zero, we stop the rumble motor.
143          */
144
145         if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
146                 value = 1;
147         else
148                 value = 0;
149
150         /* Locking state.lock here might deadlock with input_event() calls.
151          * schedule_work acts as barrier. Merging multiple changes is fine. */
152         wdata->state.cache_rumble = value;
153         schedule_work(&wdata->rumble_worker);
154
155         return 0;
156 }
157
158 static int wiimod_rumble_probe(const struct wiimod_ops *ops,
159                                struct wiimote_data *wdata)
160 {
161         INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
162
163         set_bit(FF_RUMBLE, wdata->input->ffbit);
164         if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
165                 return -ENOMEM;
166
167         return 0;
168 }
169
170 static void wiimod_rumble_remove(const struct wiimod_ops *ops,
171                                  struct wiimote_data *wdata)
172 {
173         unsigned long flags;
174
175         cancel_work_sync(&wdata->rumble_worker);
176
177         spin_lock_irqsave(&wdata->state.lock, flags);
178         wiiproto_req_rumble(wdata, 0);
179         spin_unlock_irqrestore(&wdata->state.lock, flags);
180 }
181
182 static const struct wiimod_ops wiimod_rumble = {
183         .flags = WIIMOD_FLAG_INPUT,
184         .arg = 0,
185         .probe = wiimod_rumble_probe,
186         .remove = wiimod_rumble_remove,
187 };
188
189 /*
190  * Battery
191  * 1 byte of battery capacity information is sent along every protocol status
192  * report. The wiimote core caches it but we try to update it on every
193  * user-space request.
194  * This is supported by nearly every device so it's almost always enabled.
195  */
196
197 static enum power_supply_property wiimod_battery_props[] = {
198         POWER_SUPPLY_PROP_CAPACITY,
199         POWER_SUPPLY_PROP_SCOPE,
200 };
201
202 static int wiimod_battery_get_property(struct power_supply *psy,
203                                        enum power_supply_property psp,
204                                        union power_supply_propval *val)
205 {
206         struct wiimote_data *wdata = power_supply_get_drvdata(psy);
207         int ret = 0, state;
208         unsigned long flags;
209
210         if (psp == POWER_SUPPLY_PROP_SCOPE) {
211                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
212                 return 0;
213         } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
214                 return -EINVAL;
215         }
216
217         ret = wiimote_cmd_acquire(wdata);
218         if (ret)
219                 return ret;
220
221         spin_lock_irqsave(&wdata->state.lock, flags);
222         wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
223         wiiproto_req_status(wdata);
224         spin_unlock_irqrestore(&wdata->state.lock, flags);
225
226         wiimote_cmd_wait(wdata);
227         wiimote_cmd_release(wdata);
228
229         spin_lock_irqsave(&wdata->state.lock, flags);
230         state = wdata->state.cmd_battery;
231         spin_unlock_irqrestore(&wdata->state.lock, flags);
232
233         val->intval = state * 100 / 255;
234         return ret;
235 }
236
237 static int wiimod_battery_probe(const struct wiimod_ops *ops,
238                                 struct wiimote_data *wdata)
239 {
240         struct power_supply_config psy_cfg = { .drv_data = wdata, };
241         int ret;
242
243         wdata->battery_desc.properties = wiimod_battery_props;
244         wdata->battery_desc.num_properties = ARRAY_SIZE(wiimod_battery_props);
245         wdata->battery_desc.get_property = wiimod_battery_get_property;
246         wdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
247         wdata->battery_desc.use_for_apm = 0;
248         wdata->battery_desc.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
249                                              wdata->hdev->uniq);
250         if (!wdata->battery_desc.name)
251                 return -ENOMEM;
252
253         wdata->battery = power_supply_register(&wdata->hdev->dev,
254                                                &wdata->battery_desc,
255                                                &psy_cfg);
256         if (IS_ERR(wdata->battery)) {
257                 hid_err(wdata->hdev, "cannot register battery device\n");
258                 ret = PTR_ERR(wdata->battery);
259                 goto err_free;
260         }
261
262         power_supply_powers(wdata->battery, &wdata->hdev->dev);
263         return 0;
264
265 err_free:
266         kfree(wdata->battery_desc.name);
267         wdata->battery_desc.name = NULL;
268         return ret;
269 }
270
271 static void wiimod_battery_remove(const struct wiimod_ops *ops,
272                                   struct wiimote_data *wdata)
273 {
274         if (!wdata->battery_desc.name)
275                 return;
276
277         power_supply_unregister(wdata->battery);
278         kfree(wdata->battery_desc.name);
279         wdata->battery_desc.name = NULL;
280 }
281
282 static const struct wiimod_ops wiimod_battery = {
283         .flags = 0,
284         .arg = 0,
285         .probe = wiimod_battery_probe,
286         .remove = wiimod_battery_remove,
287 };
288
289 /*
290  * LED
291  * 0 to 4 player LEDs are supported by devices. The "arg" field of the
292  * wiimod_ops structure specifies which LED this module controls. This allows
293  * to register a limited number of LEDs.
294  * State is managed by wiimote core.
295  */
296
297 static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
298 {
299         struct wiimote_data *wdata;
300         struct device *dev = led_dev->dev->parent;
301         int i;
302         unsigned long flags;
303         bool value = false;
304
305         wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
306
307         for (i = 0; i < 4; ++i) {
308                 if (wdata->leds[i] == led_dev) {
309                         spin_lock_irqsave(&wdata->state.lock, flags);
310                         value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
311                         spin_unlock_irqrestore(&wdata->state.lock, flags);
312                         break;
313                 }
314         }
315
316         return value ? LED_FULL : LED_OFF;
317 }
318
319 static void wiimod_led_set(struct led_classdev *led_dev,
320                            enum led_brightness value)
321 {
322         struct wiimote_data *wdata;
323         struct device *dev = led_dev->dev->parent;
324         int i;
325         unsigned long flags;
326         __u8 state, flag;
327
328         wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
329
330         for (i = 0; i < 4; ++i) {
331                 if (wdata->leds[i] == led_dev) {
332                         flag = WIIPROTO_FLAG_LED(i + 1);
333                         spin_lock_irqsave(&wdata->state.lock, flags);
334                         state = wdata->state.flags;
335                         if (value == LED_OFF)
336                                 wiiproto_req_leds(wdata, state & ~flag);
337                         else
338                                 wiiproto_req_leds(wdata, state | flag);
339                         spin_unlock_irqrestore(&wdata->state.lock, flags);
340                         break;
341                 }
342         }
343 }
344
345 static int wiimod_led_probe(const struct wiimod_ops *ops,
346                             struct wiimote_data *wdata)
347 {
348         struct device *dev = &wdata->hdev->dev;
349         size_t namesz = strlen(dev_name(dev)) + 9;
350         struct led_classdev *led;
351         unsigned long flags;
352         char *name;
353         int ret;
354
355         led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
356         if (!led)
357                 return -ENOMEM;
358
359         name = (void*)&led[1];
360         snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
361         led->name = name;
362         led->brightness = 0;
363         led->max_brightness = 1;
364         led->brightness_get = wiimod_led_get;
365         led->brightness_set = wiimod_led_set;
366
367         wdata->leds[ops->arg] = led;
368         ret = led_classdev_register(dev, led);
369         if (ret)
370                 goto err_free;
371
372         /* enable LED1 to stop initial LED-blinking */
373         if (ops->arg == 0) {
374                 spin_lock_irqsave(&wdata->state.lock, flags);
375                 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
376                 spin_unlock_irqrestore(&wdata->state.lock, flags);
377         }
378
379         return 0;
380
381 err_free:
382         wdata->leds[ops->arg] = NULL;
383         kfree(led);
384         return ret;
385 }
386
387 static void wiimod_led_remove(const struct wiimod_ops *ops,
388                               struct wiimote_data *wdata)
389 {
390         if (!wdata->leds[ops->arg])
391                 return;
392
393         led_classdev_unregister(wdata->leds[ops->arg]);
394         kfree(wdata->leds[ops->arg]);
395         wdata->leds[ops->arg] = NULL;
396 }
397
398 static const struct wiimod_ops wiimod_leds[4] = {
399         {
400                 .flags = 0,
401                 .arg = 0,
402                 .probe = wiimod_led_probe,
403                 .remove = wiimod_led_remove,
404         },
405         {
406                 .flags = 0,
407                 .arg = 1,
408                 .probe = wiimod_led_probe,
409                 .remove = wiimod_led_remove,
410         },
411         {
412                 .flags = 0,
413                 .arg = 2,
414                 .probe = wiimod_led_probe,
415                 .remove = wiimod_led_remove,
416         },
417         {
418                 .flags = 0,
419                 .arg = 3,
420                 .probe = wiimod_led_probe,
421                 .remove = wiimod_led_remove,
422         },
423 };
424
425 /*
426  * Accelerometer
427  * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
428  * device, it's mostly cleared to 0. This module parses this data and provides
429  * it via a separate input device.
430  */
431
432 static void wiimod_accel_in_accel(struct wiimote_data *wdata,
433                                   const __u8 *accel)
434 {
435         __u16 x, y, z;
436
437         if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
438                 return;
439
440         /*
441          * payload is: BB BB XX YY ZZ
442          * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
443          * contain the upper 8 bits of each value. The lower 2 bits are
444          * contained in the buttons data BB BB.
445          * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
446          * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
447          * accel value and bit 6 is the second bit of the Z value.
448          * The first bit of Y and Z values is not available and always set to 0.
449          * 0x200 is returned on no movement.
450          */
451
452         x = accel[2] << 2;
453         y = accel[3] << 2;
454         z = accel[4] << 2;
455
456         x |= (accel[0] >> 5) & 0x3;
457         y |= (accel[1] >> 4) & 0x2;
458         z |= (accel[1] >> 5) & 0x2;
459
460         input_report_abs(wdata->accel, ABS_RX, x - 0x200);
461         input_report_abs(wdata->accel, ABS_RY, y - 0x200);
462         input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
463         input_sync(wdata->accel);
464 }
465
466 static int wiimod_accel_open(struct input_dev *dev)
467 {
468         struct wiimote_data *wdata = input_get_drvdata(dev);
469         unsigned long flags;
470
471         spin_lock_irqsave(&wdata->state.lock, flags);
472         wiiproto_req_accel(wdata, true);
473         spin_unlock_irqrestore(&wdata->state.lock, flags);
474
475         return 0;
476 }
477
478 static void wiimod_accel_close(struct input_dev *dev)
479 {
480         struct wiimote_data *wdata = input_get_drvdata(dev);
481         unsigned long flags;
482
483         spin_lock_irqsave(&wdata->state.lock, flags);
484         wiiproto_req_accel(wdata, false);
485         spin_unlock_irqrestore(&wdata->state.lock, flags);
486 }
487
488 static int wiimod_accel_probe(const struct wiimod_ops *ops,
489                               struct wiimote_data *wdata)
490 {
491         int ret;
492
493         wdata->accel = input_allocate_device();
494         if (!wdata->accel)
495                 return -ENOMEM;
496
497         input_set_drvdata(wdata->accel, wdata);
498         wdata->accel->open = wiimod_accel_open;
499         wdata->accel->close = wiimod_accel_close;
500         wdata->accel->dev.parent = &wdata->hdev->dev;
501         wdata->accel->id.bustype = wdata->hdev->bus;
502         wdata->accel->id.vendor = wdata->hdev->vendor;
503         wdata->accel->id.product = wdata->hdev->product;
504         wdata->accel->id.version = wdata->hdev->version;
505         wdata->accel->name = WIIMOTE_NAME " Accelerometer";
506
507         set_bit(EV_ABS, wdata->accel->evbit);
508         set_bit(ABS_RX, wdata->accel->absbit);
509         set_bit(ABS_RY, wdata->accel->absbit);
510         set_bit(ABS_RZ, wdata->accel->absbit);
511         input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
512         input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
513         input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
514
515         ret = input_register_device(wdata->accel);
516         if (ret) {
517                 hid_err(wdata->hdev, "cannot register input device\n");
518                 goto err_free;
519         }
520
521         return 0;
522
523 err_free:
524         input_free_device(wdata->accel);
525         wdata->accel = NULL;
526         return ret;
527 }
528
529 static void wiimod_accel_remove(const struct wiimod_ops *ops,
530                                 struct wiimote_data *wdata)
531 {
532         if (!wdata->accel)
533                 return;
534
535         input_unregister_device(wdata->accel);
536         wdata->accel = NULL;
537 }
538
539 static const struct wiimod_ops wiimod_accel = {
540         .flags = 0,
541         .arg = 0,
542         .probe = wiimod_accel_probe,
543         .remove = wiimod_accel_remove,
544         .in_accel = wiimod_accel_in_accel,
545 };
546
547 /*
548  * IR Cam
549  * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs
550  * to be initialized with a fairly complex procedure and consumes a lot of
551  * power. Therefore, as long as no application uses the IR input device, it is
552  * kept offline.
553  * Nearly no other device than the normal Wii Remotes supports the IR cam so
554  * you can disable this module for these devices.
555  */
556
557 static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
558                             bool packed, unsigned int id)
559 {
560         __u16 x, y;
561         __u8 xid, yid;
562         bool sync = false;
563
564         if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
565                 return;
566
567         switch (id) {
568         case 0:
569                 xid = ABS_HAT0X;
570                 yid = ABS_HAT0Y;
571                 break;
572         case 1:
573                 xid = ABS_HAT1X;
574                 yid = ABS_HAT1Y;
575                 break;
576         case 2:
577                 xid = ABS_HAT2X;
578                 yid = ABS_HAT2Y;
579                 break;
580         case 3:
581                 xid = ABS_HAT3X;
582                 yid = ABS_HAT3Y;
583                 sync = true;
584                 break;
585         default:
586                 return;
587         }
588
589         /*
590          * Basic IR data is encoded into 3 bytes. The first two bytes are the
591          * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
592          * of both.
593          * If data is packed, then the 3rd byte is put first and slightly
594          * reordered. This allows to interleave packed and non-packed data to
595          * have two IR sets in 5 bytes instead of 6.
596          * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev.
597          */
598
599         if (packed) {
600                 x = ir[1] | ((ir[0] & 0x03) << 8);
601                 y = ir[2] | ((ir[0] & 0x0c) << 6);
602         } else {
603                 x = ir[0] | ((ir[2] & 0x30) << 4);
604                 y = ir[1] | ((ir[2] & 0xc0) << 2);
605         }
606
607         input_report_abs(wdata->ir, xid, x);
608         input_report_abs(wdata->ir, yid, y);
609
610         if (sync)
611                 input_sync(wdata->ir);
612 }
613
614 static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
615 {
616         int ret;
617         unsigned long flags;
618         __u8 format = 0;
619         static const __u8 data_enable[] = { 0x01 };
620         static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
621                                                 0x00, 0xaa, 0x00, 0x64 };
622         static const __u8 data_sens2[] = { 0x63, 0x03 };
623         static const __u8 data_fin[] = { 0x08 };
624
625         spin_lock_irqsave(&wdata->state.lock, flags);
626
627         if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
628                 spin_unlock_irqrestore(&wdata->state.lock, flags);
629                 return 0;
630         }
631
632         if (mode == 0) {
633                 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
634                 wiiproto_req_ir1(wdata, 0);
635                 wiiproto_req_ir2(wdata, 0);
636                 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
637                 spin_unlock_irqrestore(&wdata->state.lock, flags);
638                 return 0;
639         }
640
641         spin_unlock_irqrestore(&wdata->state.lock, flags);
642
643         ret = wiimote_cmd_acquire(wdata);
644         if (ret)
645                 return ret;
646
647         /* send PIXEL CLOCK ENABLE cmd first */
648         spin_lock_irqsave(&wdata->state.lock, flags);
649         wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
650         wiiproto_req_ir1(wdata, 0x06);
651         spin_unlock_irqrestore(&wdata->state.lock, flags);
652
653         ret = wiimote_cmd_wait(wdata);
654         if (ret)
655                 goto unlock;
656         if (wdata->state.cmd_err) {
657                 ret = -EIO;
658                 goto unlock;
659         }
660
661         /* enable IR LOGIC */
662         spin_lock_irqsave(&wdata->state.lock, flags);
663         wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
664         wiiproto_req_ir2(wdata, 0x06);
665         spin_unlock_irqrestore(&wdata->state.lock, flags);
666
667         ret = wiimote_cmd_wait(wdata);
668         if (ret)
669                 goto unlock;
670         if (wdata->state.cmd_err) {
671                 ret = -EIO;
672                 goto unlock;
673         }
674
675         /* enable IR cam but do not make it send data, yet */
676         ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
677                                                         sizeof(data_enable));
678         if (ret)
679                 goto unlock;
680
681         /* write first sensitivity block */
682         ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
683                                                         sizeof(data_sens1));
684         if (ret)
685                 goto unlock;
686
687         /* write second sensitivity block */
688         ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
689                                                         sizeof(data_sens2));
690         if (ret)
691                 goto unlock;
692
693         /* put IR cam into desired state */
694         switch (mode) {
695                 case WIIPROTO_FLAG_IR_FULL:
696                         format = 5;
697                         break;
698                 case WIIPROTO_FLAG_IR_EXT:
699                         format = 3;
700                         break;
701                 case WIIPROTO_FLAG_IR_BASIC:
702                         format = 1;
703                         break;
704         }
705         ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
706         if (ret)
707                 goto unlock;
708
709         /* make IR cam send data */
710         ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
711         if (ret)
712                 goto unlock;
713
714         /* request new DRM mode compatible to IR mode */
715         spin_lock_irqsave(&wdata->state.lock, flags);
716         wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
717         wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
718         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
719         spin_unlock_irqrestore(&wdata->state.lock, flags);
720
721 unlock:
722         wiimote_cmd_release(wdata);
723         return ret;
724 }
725
726 static int wiimod_ir_open(struct input_dev *dev)
727 {
728         struct wiimote_data *wdata = input_get_drvdata(dev);
729
730         return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
731 }
732
733 static void wiimod_ir_close(struct input_dev *dev)
734 {
735         struct wiimote_data *wdata = input_get_drvdata(dev);
736
737         wiimod_ir_change(wdata, 0);
738 }
739
740 static int wiimod_ir_probe(const struct wiimod_ops *ops,
741                            struct wiimote_data *wdata)
742 {
743         int ret;
744
745         wdata->ir = input_allocate_device();
746         if (!wdata->ir)
747                 return -ENOMEM;
748
749         input_set_drvdata(wdata->ir, wdata);
750         wdata->ir->open = wiimod_ir_open;
751         wdata->ir->close = wiimod_ir_close;
752         wdata->ir->dev.parent = &wdata->hdev->dev;
753         wdata->ir->id.bustype = wdata->hdev->bus;
754         wdata->ir->id.vendor = wdata->hdev->vendor;
755         wdata->ir->id.product = wdata->hdev->product;
756         wdata->ir->id.version = wdata->hdev->version;
757         wdata->ir->name = WIIMOTE_NAME " IR";
758
759         set_bit(EV_ABS, wdata->ir->evbit);
760         set_bit(ABS_HAT0X, wdata->ir->absbit);
761         set_bit(ABS_HAT0Y, wdata->ir->absbit);
762         set_bit(ABS_HAT1X, wdata->ir->absbit);
763         set_bit(ABS_HAT1Y, wdata->ir->absbit);
764         set_bit(ABS_HAT2X, wdata->ir->absbit);
765         set_bit(ABS_HAT2Y, wdata->ir->absbit);
766         set_bit(ABS_HAT3X, wdata->ir->absbit);
767         set_bit(ABS_HAT3Y, wdata->ir->absbit);
768         input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
769         input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
770         input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
771         input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
772         input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
773         input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
774         input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
775         input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
776
777         ret = input_register_device(wdata->ir);
778         if (ret) {
779                 hid_err(wdata->hdev, "cannot register input device\n");
780                 goto err_free;
781         }
782
783         return 0;
784
785 err_free:
786         input_free_device(wdata->ir);
787         wdata->ir = NULL;
788         return ret;
789 }
790
791 static void wiimod_ir_remove(const struct wiimod_ops *ops,
792                              struct wiimote_data *wdata)
793 {
794         if (!wdata->ir)
795                 return;
796
797         input_unregister_device(wdata->ir);
798         wdata->ir = NULL;
799 }
800
801 static const struct wiimod_ops wiimod_ir = {
802         .flags = 0,
803         .arg = 0,
804         .probe = wiimod_ir_probe,
805         .remove = wiimod_ir_remove,
806         .in_ir = wiimod_ir_in_ir,
807 };
808
809 /*
810  * Nunchuk Extension
811  * The Nintendo Wii Nunchuk was the first official extension published by
812  * Nintendo. It provides two additional keys and a separate accelerometer. It
813  * can be hotplugged to standard Wii Remotes.
814  */
815
816 enum wiimod_nunchuk_keys {
817         WIIMOD_NUNCHUK_KEY_C,
818         WIIMOD_NUNCHUK_KEY_Z,
819         WIIMOD_NUNCHUK_KEY_NUM,
820 };
821
822 static const __u16 wiimod_nunchuk_map[] = {
823         BTN_C,          /* WIIMOD_NUNCHUK_KEY_C */
824         BTN_Z,          /* WIIMOD_NUNCHUK_KEY_Z */
825 };
826
827 static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
828 {
829         __s16 x, y, z, bx, by;
830
831         /*   Byte |   8    7 |  6    5 |  4    3 |  2 |  1  |
832          *   -----+----------+---------+---------+----+-----+
833          *    1   |              Button X <7:0>             |
834          *    2   |              Button Y <7:0>             |
835          *   -----+----------+---------+---------+----+-----+
836          *    3   |               Speed X <9:2>             |
837          *    4   |               Speed Y <9:2>             |
838          *    5   |               Speed Z <9:2>             |
839          *   -----+----------+---------+---------+----+-----+
840          *    6   | Z <1:0>  | Y <1:0> | X <1:0> | BC | BZ  |
841          *   -----+----------+---------+---------+----+-----+
842          * Button X/Y is the analog stick. Speed X, Y and Z are the
843          * accelerometer data in the same format as the wiimote's accelerometer.
844          * The 6th byte contains the LSBs of the accelerometer data.
845          * BC and BZ are the C and Z buttons: 0 means pressed
846          *
847          * If reported interleaved with motionp, then the layout changes. The
848          * 5th and 6th byte changes to:
849          *   -----+-----------------------------------+-----+
850          *    5   |            Speed Z <9:3>          | EXT |
851          *   -----+--------+-----+-----+----+----+----+-----+
852          *    6   |Z <2:1> |Y <1>|X <1>| BC | BZ | 0  |  0  |
853          *   -----+--------+-----+-----+----+----+----+-----+
854          * All three accelerometer values lose their LSB. The other data is
855          * still available but slightly moved.
856          *
857          * Center data for button values is 128. Center value for accelerometer
858          * values it 512 / 0x200
859          */
860
861         bx = ext[0];
862         by = ext[1];
863         bx -= 128;
864         by -= 128;
865
866         x = ext[2] << 2;
867         y = ext[3] << 2;
868         z = ext[4] << 2;
869
870         if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
871                 x |= (ext[5] >> 3) & 0x02;
872                 y |= (ext[5] >> 4) & 0x02;
873                 z &= ~0x4;
874                 z |= (ext[5] >> 5) & 0x06;
875         } else {
876                 x |= (ext[5] >> 2) & 0x03;
877                 y |= (ext[5] >> 4) & 0x03;
878                 z |= (ext[5] >> 6) & 0x03;
879         }
880
881         x -= 0x200;
882         y -= 0x200;
883         z -= 0x200;
884
885         input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
886         input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
887
888         input_report_abs(wdata->extension.input, ABS_RX, x);
889         input_report_abs(wdata->extension.input, ABS_RY, y);
890         input_report_abs(wdata->extension.input, ABS_RZ, z);
891
892         if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
893                 input_report_key(wdata->extension.input,
894                         wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
895                         !(ext[5] & 0x04));
896                 input_report_key(wdata->extension.input,
897                         wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
898                         !(ext[5] & 0x08));
899         } else {
900                 input_report_key(wdata->extension.input,
901                         wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
902                         !(ext[5] & 0x01));
903                 input_report_key(wdata->extension.input,
904                         wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
905                         !(ext[5] & 0x02));
906         }
907
908         input_sync(wdata->extension.input);
909 }
910
911 static int wiimod_nunchuk_open(struct input_dev *dev)
912 {
913         struct wiimote_data *wdata = input_get_drvdata(dev);
914         unsigned long flags;
915
916         spin_lock_irqsave(&wdata->state.lock, flags);
917         wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
918         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
919         spin_unlock_irqrestore(&wdata->state.lock, flags);
920
921         return 0;
922 }
923
924 static void wiimod_nunchuk_close(struct input_dev *dev)
925 {
926         struct wiimote_data *wdata = input_get_drvdata(dev);
927         unsigned long flags;
928
929         spin_lock_irqsave(&wdata->state.lock, flags);
930         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
931         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
932         spin_unlock_irqrestore(&wdata->state.lock, flags);
933 }
934
935 static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
936                                 struct wiimote_data *wdata)
937 {
938         int ret, i;
939
940         wdata->extension.input = input_allocate_device();
941         if (!wdata->extension.input)
942                 return -ENOMEM;
943
944         input_set_drvdata(wdata->extension.input, wdata);
945         wdata->extension.input->open = wiimod_nunchuk_open;
946         wdata->extension.input->close = wiimod_nunchuk_close;
947         wdata->extension.input->dev.parent = &wdata->hdev->dev;
948         wdata->extension.input->id.bustype = wdata->hdev->bus;
949         wdata->extension.input->id.vendor = wdata->hdev->vendor;
950         wdata->extension.input->id.product = wdata->hdev->product;
951         wdata->extension.input->id.version = wdata->hdev->version;
952         wdata->extension.input->name = WIIMOTE_NAME " Nunchuk";
953
954         set_bit(EV_KEY, wdata->extension.input->evbit);
955         for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i)
956                 set_bit(wiimod_nunchuk_map[i],
957                         wdata->extension.input->keybit);
958
959         set_bit(EV_ABS, wdata->extension.input->evbit);
960         set_bit(ABS_HAT0X, wdata->extension.input->absbit);
961         set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
962         input_set_abs_params(wdata->extension.input,
963                              ABS_HAT0X, -120, 120, 2, 4);
964         input_set_abs_params(wdata->extension.input,
965                              ABS_HAT0Y, -120, 120, 2, 4);
966         set_bit(ABS_RX, wdata->extension.input->absbit);
967         set_bit(ABS_RY, wdata->extension.input->absbit);
968         set_bit(ABS_RZ, wdata->extension.input->absbit);
969         input_set_abs_params(wdata->extension.input,
970                              ABS_RX, -500, 500, 2, 4);
971         input_set_abs_params(wdata->extension.input,
972                              ABS_RY, -500, 500, 2, 4);
973         input_set_abs_params(wdata->extension.input,
974                              ABS_RZ, -500, 500, 2, 4);
975
976         ret = input_register_device(wdata->extension.input);
977         if (ret)
978                 goto err_free;
979
980         return 0;
981
982 err_free:
983         input_free_device(wdata->extension.input);
984         wdata->extension.input = NULL;
985         return ret;
986 }
987
988 static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
989                                   struct wiimote_data *wdata)
990 {
991         if (!wdata->extension.input)
992                 return;
993
994         input_unregister_device(wdata->extension.input);
995         wdata->extension.input = NULL;
996 }
997
998 static const struct wiimod_ops wiimod_nunchuk = {
999         .flags = 0,
1000         .arg = 0,
1001         .probe = wiimod_nunchuk_probe,
1002         .remove = wiimod_nunchuk_remove,
1003         .in_ext = wiimod_nunchuk_in_ext,
1004 };
1005
1006 /*
1007  * Classic Controller
1008  * Another official extension from Nintendo. It provides a classic
1009  * gamecube-like controller that can be hotplugged on the Wii Remote.
1010  * It has several hardware buttons and switches that are all reported via
1011  * a normal extension device.
1012  */
1013
1014 enum wiimod_classic_keys {
1015         WIIMOD_CLASSIC_KEY_A,
1016         WIIMOD_CLASSIC_KEY_B,
1017         WIIMOD_CLASSIC_KEY_X,
1018         WIIMOD_CLASSIC_KEY_Y,
1019         WIIMOD_CLASSIC_KEY_ZL,
1020         WIIMOD_CLASSIC_KEY_ZR,
1021         WIIMOD_CLASSIC_KEY_PLUS,
1022         WIIMOD_CLASSIC_KEY_MINUS,
1023         WIIMOD_CLASSIC_KEY_HOME,
1024         WIIMOD_CLASSIC_KEY_LEFT,
1025         WIIMOD_CLASSIC_KEY_RIGHT,
1026         WIIMOD_CLASSIC_KEY_UP,
1027         WIIMOD_CLASSIC_KEY_DOWN,
1028         WIIMOD_CLASSIC_KEY_LT,
1029         WIIMOD_CLASSIC_KEY_RT,
1030         WIIMOD_CLASSIC_KEY_NUM,
1031 };
1032
1033 static const __u16 wiimod_classic_map[] = {
1034         BTN_A,          /* WIIMOD_CLASSIC_KEY_A */
1035         BTN_B,          /* WIIMOD_CLASSIC_KEY_B */
1036         BTN_X,          /* WIIMOD_CLASSIC_KEY_X */
1037         BTN_Y,          /* WIIMOD_CLASSIC_KEY_Y */
1038         BTN_TL2,        /* WIIMOD_CLASSIC_KEY_ZL */
1039         BTN_TR2,        /* WIIMOD_CLASSIC_KEY_ZR */
1040         KEY_NEXT,       /* WIIMOD_CLASSIC_KEY_PLUS */
1041         KEY_PREVIOUS,   /* WIIMOD_CLASSIC_KEY_MINUS */
1042         BTN_MODE,       /* WIIMOD_CLASSIC_KEY_HOME */
1043         KEY_LEFT,       /* WIIMOD_CLASSIC_KEY_LEFT */
1044         KEY_RIGHT,      /* WIIMOD_CLASSIC_KEY_RIGHT */
1045         KEY_UP,         /* WIIMOD_CLASSIC_KEY_UP */
1046         KEY_DOWN,       /* WIIMOD_CLASSIC_KEY_DOWN */
1047         BTN_TL,         /* WIIMOD_CLASSIC_KEY_LT */
1048         BTN_TR,         /* WIIMOD_CLASSIC_KEY_RT */
1049 };
1050
1051 static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1052 {
1053         __s8 rx, ry, lx, ly, lt, rt;
1054
1055         /*   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
1056          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1057          *    1   | RX <5:4>  |              LX <5:0>             |
1058          *    2   | RX <3:2>  |              LY <5:0>             |
1059          *   -----+-----+-----+-----+-----------------------------+
1060          *    3   |RX<1>| LT <5:4>  |         RY <5:1>            |
1061          *   -----+-----+-----------+-----------------------------+
1062          *    4   |     LT <3:1>    |         RT <5:1>            |
1063          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1064          *    5   | BDR | BDD | BLT | B-  | BH  | B+  | BRT |  1  |
1065          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1066          *    6   | BZL | BB  | BY  | BA  | BX  | BZR | BDL | BDU |
1067          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1068          * All buttons are 0 if pressed
1069          * RX and RY are right analog stick
1070          * LX and LY are left analog stick
1071          * LT is left trigger, RT is right trigger
1072          * BLT is 0 if left trigger is fully pressed
1073          * BRT is 0 if right trigger is fully pressed
1074          * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1075          * BZL is left Z button and BZR is right Z button
1076          * B-, BH, B+ are +, HOME and - buttons
1077          * BB, BY, BA, BX are A, B, X, Y buttons
1078          * LSB of RX, RY, LT, and RT are not transmitted and always 0.
1079          *
1080          * With motionp enabled it changes slightly to this:
1081          *   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
1082          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1083          *    1   | RX <5:4>  |          LX <5:1>           | BDU |
1084          *    2   | RX <3:2>  |          LY <5:1>           | BDL |
1085          *   -----+-----+-----+-----+-----------------------+-----+
1086          *    3   |RX<1>| LT <5:4>  |         RY <5:1>            |
1087          *   -----+-----+-----------+-----------------------------+
1088          *    4   |     LT <3:1>    |         RT <5:1>            |
1089          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1090          *    5   | BDR | BDD | BLT | B-  | BH  | B+  | BRT | EXT |
1091          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1092          *    6   | BZL | BB  | BY  | BA  | BX  | BZR |  0  |  0  |
1093          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1094          * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
1095          * is the same as before.
1096          */
1097
1098         if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1099                 lx = ext[0] & 0x3e;
1100                 ly = ext[1] & 0x3e;
1101         } else {
1102                 lx = ext[0] & 0x3f;
1103                 ly = ext[1] & 0x3f;
1104         }
1105
1106         rx = (ext[0] >> 3) & 0x18;
1107         rx |= (ext[1] >> 5) & 0x06;
1108         rx |= (ext[2] >> 7) & 0x01;
1109         ry = ext[2] & 0x1f;
1110
1111         rt = ext[3] & 0x1f;
1112         lt = (ext[2] >> 2) & 0x18;
1113         lt |= (ext[3] >> 5) & 0x07;
1114
1115         rx <<= 1;
1116         ry <<= 1;
1117         rt <<= 1;
1118         lt <<= 1;
1119
1120         input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
1121         input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
1122         input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
1123         input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
1124         input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
1125         input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
1126
1127         input_report_key(wdata->extension.input,
1128                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
1129                          !(ext[4] & 0x80));
1130         input_report_key(wdata->extension.input,
1131                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
1132                          !(ext[4] & 0x40));
1133         input_report_key(wdata->extension.input,
1134                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
1135                          !(ext[4] & 0x20));
1136         input_report_key(wdata->extension.input,
1137                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS],
1138                          !(ext[4] & 0x10));
1139         input_report_key(wdata->extension.input,
1140                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME],
1141                          !(ext[4] & 0x08));
1142         input_report_key(wdata->extension.input,
1143                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS],
1144                          !(ext[4] & 0x04));
1145         input_report_key(wdata->extension.input,
1146                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT],
1147                          !(ext[4] & 0x02));
1148         input_report_key(wdata->extension.input,
1149                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL],
1150                          !(ext[5] & 0x80));
1151         input_report_key(wdata->extension.input,
1152                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_B],
1153                          !(ext[5] & 0x40));
1154         input_report_key(wdata->extension.input,
1155                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y],
1156                          !(ext[5] & 0x20));
1157         input_report_key(wdata->extension.input,
1158                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_A],
1159                          !(ext[5] & 0x10));
1160         input_report_key(wdata->extension.input,
1161                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_X],
1162                          !(ext[5] & 0x08));
1163         input_report_key(wdata->extension.input,
1164                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
1165                          !(ext[5] & 0x04));
1166
1167         if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1168                 input_report_key(wdata->extension.input,
1169                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1170                          !(ext[1] & 0x01));
1171                 input_report_key(wdata->extension.input,
1172                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1173                          !(ext[0] & 0x01));
1174         } else {
1175                 input_report_key(wdata->extension.input,
1176                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1177                          !(ext[5] & 0x02));
1178                 input_report_key(wdata->extension.input,
1179                          wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1180                          !(ext[5] & 0x01));
1181         }
1182
1183         input_sync(wdata->extension.input);
1184 }
1185
1186 static int wiimod_classic_open(struct input_dev *dev)
1187 {
1188         struct wiimote_data *wdata = input_get_drvdata(dev);
1189         unsigned long flags;
1190
1191         spin_lock_irqsave(&wdata->state.lock, flags);
1192         wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1193         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1194         spin_unlock_irqrestore(&wdata->state.lock, flags);
1195
1196         return 0;
1197 }
1198
1199 static void wiimod_classic_close(struct input_dev *dev)
1200 {
1201         struct wiimote_data *wdata = input_get_drvdata(dev);
1202         unsigned long flags;
1203
1204         spin_lock_irqsave(&wdata->state.lock, flags);
1205         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1206         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1207         spin_unlock_irqrestore(&wdata->state.lock, flags);
1208 }
1209
1210 static int wiimod_classic_probe(const struct wiimod_ops *ops,
1211                                 struct wiimote_data *wdata)
1212 {
1213         int ret, i;
1214
1215         wdata->extension.input = input_allocate_device();
1216         if (!wdata->extension.input)
1217                 return -ENOMEM;
1218
1219         input_set_drvdata(wdata->extension.input, wdata);
1220         wdata->extension.input->open = wiimod_classic_open;
1221         wdata->extension.input->close = wiimod_classic_close;
1222         wdata->extension.input->dev.parent = &wdata->hdev->dev;
1223         wdata->extension.input->id.bustype = wdata->hdev->bus;
1224         wdata->extension.input->id.vendor = wdata->hdev->vendor;
1225         wdata->extension.input->id.product = wdata->hdev->product;
1226         wdata->extension.input->id.version = wdata->hdev->version;
1227         wdata->extension.input->name = WIIMOTE_NAME " Classic Controller";
1228
1229         set_bit(EV_KEY, wdata->extension.input->evbit);
1230         for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i)
1231                 set_bit(wiimod_classic_map[i],
1232                         wdata->extension.input->keybit);
1233
1234         set_bit(EV_ABS, wdata->extension.input->evbit);
1235         set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1236         set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1237         set_bit(ABS_HAT2X, wdata->extension.input->absbit);
1238         set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
1239         set_bit(ABS_HAT3X, wdata->extension.input->absbit);
1240         set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
1241         input_set_abs_params(wdata->extension.input,
1242                              ABS_HAT1X, -30, 30, 1, 1);
1243         input_set_abs_params(wdata->extension.input,
1244                              ABS_HAT1Y, -30, 30, 1, 1);
1245         input_set_abs_params(wdata->extension.input,
1246                              ABS_HAT2X, -30, 30, 1, 1);
1247         input_set_abs_params(wdata->extension.input,
1248                              ABS_HAT2Y, -30, 30, 1, 1);
1249         input_set_abs_params(wdata->extension.input,
1250                              ABS_HAT3X, -30, 30, 1, 1);
1251         input_set_abs_params(wdata->extension.input,
1252                              ABS_HAT3Y, -30, 30, 1, 1);
1253
1254         ret = input_register_device(wdata->extension.input);
1255         if (ret)
1256                 goto err_free;
1257
1258         return 0;
1259
1260 err_free:
1261         input_free_device(wdata->extension.input);
1262         wdata->extension.input = NULL;
1263         return ret;
1264 }
1265
1266 static void wiimod_classic_remove(const struct wiimod_ops *ops,
1267                                   struct wiimote_data *wdata)
1268 {
1269         if (!wdata->extension.input)
1270                 return;
1271
1272         input_unregister_device(wdata->extension.input);
1273         wdata->extension.input = NULL;
1274 }
1275
1276 static const struct wiimod_ops wiimod_classic = {
1277         .flags = 0,
1278         .arg = 0,
1279         .probe = wiimod_classic_probe,
1280         .remove = wiimod_classic_remove,
1281         .in_ext = wiimod_classic_in_ext,
1282 };
1283
1284 /*
1285  * Balance Board Extension
1286  * The Nintendo Wii Balance Board provides four hardware weight sensor plus a
1287  * single push button. No other peripherals are available. However, the
1288  * balance-board data is sent via a standard Wii Remote extension. All other
1289  * data for non-present hardware is zeroed out.
1290  * Some 3rd party devices react allergic if we try to access normal Wii Remote
1291  * hardware, so this extension module should be the only module that is loaded
1292  * on balance boards.
1293  * The balance board needs 8 bytes extension data instead of basic 6 bytes so
1294  * it needs the WIIMOD_FLAG_EXT8 flag.
1295  */
1296
1297 static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
1298 {
1299         input_report_key(wdata->extension.input, BTN_A,
1300                          !!(keys[1] & 0x08));
1301         input_sync(wdata->extension.input);
1302 }
1303
1304 static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
1305                                  const __u8 *ext)
1306 {
1307         __s32 val[4], tmp, div;
1308         unsigned int i;
1309         struct wiimote_state *s = &wdata->state;
1310
1311         /*
1312          * Balance board data layout:
1313          *
1314          *   Byte |  8  7  6  5  4  3  2  1  |
1315          *   -----+--------------------------+
1316          *    1   |    Top Right <15:8>      |
1317          *    2   |    Top Right  <7:0>      |
1318          *   -----+--------------------------+
1319          *    3   | Bottom Right <15:8>      |
1320          *    4   | Bottom Right  <7:0>      |
1321          *   -----+--------------------------+
1322          *    5   |     Top Left <15:8>      |
1323          *    6   |     Top Left  <7:0>      |
1324          *   -----+--------------------------+
1325          *    7   |  Bottom Left <15:8>      |
1326          *    8   |  Bottom Left  <7:0>      |
1327          *   -----+--------------------------+
1328          *
1329          * These values represent the weight-measurements of the Wii-balance
1330          * board with 16bit precision.
1331          *
1332          * The balance-board is never reported interleaved with motionp.
1333          */
1334
1335         val[0] = ext[0];
1336         val[0] <<= 8;
1337         val[0] |= ext[1];
1338
1339         val[1] = ext[2];
1340         val[1] <<= 8;
1341         val[1] |= ext[3];
1342
1343         val[2] = ext[4];
1344         val[2] <<= 8;
1345         val[2] |= ext[5];
1346
1347         val[3] = ext[6];
1348         val[3] <<= 8;
1349         val[3] |= ext[7];
1350
1351         /* apply calibration data */
1352         for (i = 0; i < 4; i++) {
1353                 if (val[i] <= s->calib_bboard[i][0]) {
1354                         tmp = 0;
1355                 } else if (val[i] < s->calib_bboard[i][1]) {
1356                         tmp = val[i] - s->calib_bboard[i][0];
1357                         tmp *= 1700;
1358                         div = s->calib_bboard[i][1] - s->calib_bboard[i][0];
1359                         tmp /= div ? div : 1;
1360                 } else {
1361                         tmp = val[i] - s->calib_bboard[i][1];
1362                         tmp *= 1700;
1363                         div = s->calib_bboard[i][2] - s->calib_bboard[i][1];
1364                         tmp /= div ? div : 1;
1365                         tmp += 1700;
1366                 }
1367                 val[i] = tmp;
1368         }
1369
1370         input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]);
1371         input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]);
1372         input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]);
1373         input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]);
1374         input_sync(wdata->extension.input);
1375 }
1376
1377 static int wiimod_bboard_open(struct input_dev *dev)
1378 {
1379         struct wiimote_data *wdata = input_get_drvdata(dev);
1380         unsigned long flags;
1381
1382         spin_lock_irqsave(&wdata->state.lock, flags);
1383         wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1384         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1385         spin_unlock_irqrestore(&wdata->state.lock, flags);
1386
1387         return 0;
1388 }
1389
1390 static void wiimod_bboard_close(struct input_dev *dev)
1391 {
1392         struct wiimote_data *wdata = input_get_drvdata(dev);
1393         unsigned long flags;
1394
1395         spin_lock_irqsave(&wdata->state.lock, flags);
1396         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1397         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1398         spin_unlock_irqrestore(&wdata->state.lock, flags);
1399 }
1400
1401 static ssize_t wiimod_bboard_calib_show(struct device *dev,
1402                                         struct device_attribute *attr,
1403                                         char *out)
1404 {
1405         struct wiimote_data *wdata = dev_to_wii(dev);
1406         int i, j, ret;
1407         __u16 val;
1408         __u8 buf[24], offs;
1409
1410         ret = wiimote_cmd_acquire(wdata);
1411         if (ret)
1412                 return ret;
1413
1414         ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1415         if (ret != 12) {
1416                 wiimote_cmd_release(wdata);
1417                 return ret < 0 ? ret : -EIO;
1418         }
1419         ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1420         if (ret != 12) {
1421                 wiimote_cmd_release(wdata);
1422                 return ret < 0 ? ret : -EIO;
1423         }
1424
1425         wiimote_cmd_release(wdata);
1426
1427         spin_lock_irq(&wdata->state.lock);
1428         offs = 0;
1429         for (i = 0; i < 3; ++i) {
1430                 for (j = 0; j < 4; ++j) {
1431                         wdata->state.calib_bboard[j][i] = buf[offs];
1432                         wdata->state.calib_bboard[j][i] <<= 8;
1433                         wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1434                         offs += 2;
1435                 }
1436         }
1437         spin_unlock_irq(&wdata->state.lock);
1438
1439         ret = 0;
1440         for (i = 0; i < 3; ++i) {
1441                 for (j = 0; j < 4; ++j) {
1442                         val = wdata->state.calib_bboard[j][i];
1443                         if (i == 2 && j == 3)
1444                                 ret += sprintf(&out[ret], "%04x\n", val);
1445                         else
1446                                 ret += sprintf(&out[ret], "%04x:", val);
1447                 }
1448         }
1449
1450         return ret;
1451 }
1452
1453 static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
1454
1455 static int wiimod_bboard_probe(const struct wiimod_ops *ops,
1456                                struct wiimote_data *wdata)
1457 {
1458         int ret, i, j;
1459         __u8 buf[24], offs;
1460
1461         wiimote_cmd_acquire_noint(wdata);
1462
1463         ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1464         if (ret != 12) {
1465                 wiimote_cmd_release(wdata);
1466                 return ret < 0 ? ret : -EIO;
1467         }
1468         ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1469         if (ret != 12) {
1470                 wiimote_cmd_release(wdata);
1471                 return ret < 0 ? ret : -EIO;
1472         }
1473
1474         wiimote_cmd_release(wdata);
1475
1476         offs = 0;
1477         for (i = 0; i < 3; ++i) {
1478                 for (j = 0; j < 4; ++j) {
1479                         wdata->state.calib_bboard[j][i] = buf[offs];
1480                         wdata->state.calib_bboard[j][i] <<= 8;
1481                         wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1482                         offs += 2;
1483                 }
1484         }
1485
1486         wdata->extension.input = input_allocate_device();
1487         if (!wdata->extension.input)
1488                 return -ENOMEM;
1489
1490         ret = device_create_file(&wdata->hdev->dev,
1491                                  &dev_attr_bboard_calib);
1492         if (ret) {
1493                 hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1494                 goto err_free;
1495         }
1496
1497         input_set_drvdata(wdata->extension.input, wdata);
1498         wdata->extension.input->open = wiimod_bboard_open;
1499         wdata->extension.input->close = wiimod_bboard_close;
1500         wdata->extension.input->dev.parent = &wdata->hdev->dev;
1501         wdata->extension.input->id.bustype = wdata->hdev->bus;
1502         wdata->extension.input->id.vendor = wdata->hdev->vendor;
1503         wdata->extension.input->id.product = wdata->hdev->product;
1504         wdata->extension.input->id.version = wdata->hdev->version;
1505         wdata->extension.input->name = WIIMOTE_NAME " Balance Board";
1506
1507         set_bit(EV_KEY, wdata->extension.input->evbit);
1508         set_bit(BTN_A, wdata->extension.input->keybit);
1509
1510         set_bit(EV_ABS, wdata->extension.input->evbit);
1511         set_bit(ABS_HAT0X, wdata->extension.input->absbit);
1512         set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
1513         set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1514         set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1515         input_set_abs_params(wdata->extension.input,
1516                              ABS_HAT0X, 0, 65535, 2, 4);
1517         input_set_abs_params(wdata->extension.input,
1518                              ABS_HAT0Y, 0, 65535, 2, 4);
1519         input_set_abs_params(wdata->extension.input,
1520                              ABS_HAT1X, 0, 65535, 2, 4);
1521         input_set_abs_params(wdata->extension.input,
1522                              ABS_HAT1Y, 0, 65535, 2, 4);
1523
1524         ret = input_register_device(wdata->extension.input);
1525         if (ret)
1526                 goto err_file;
1527
1528         return 0;
1529
1530 err_file:
1531         device_remove_file(&wdata->hdev->dev,
1532                            &dev_attr_bboard_calib);
1533 err_free:
1534         input_free_device(wdata->extension.input);
1535         wdata->extension.input = NULL;
1536         return ret;
1537 }
1538
1539 static void wiimod_bboard_remove(const struct wiimod_ops *ops,
1540                                  struct wiimote_data *wdata)
1541 {
1542         if (!wdata->extension.input)
1543                 return;
1544
1545         input_unregister_device(wdata->extension.input);
1546         wdata->extension.input = NULL;
1547         device_remove_file(&wdata->hdev->dev,
1548                            &dev_attr_bboard_calib);
1549 }
1550
1551 static const struct wiimod_ops wiimod_bboard = {
1552         .flags = WIIMOD_FLAG_EXT8,
1553         .arg = 0,
1554         .probe = wiimod_bboard_probe,
1555         .remove = wiimod_bboard_remove,
1556         .in_keys = wiimod_bboard_in_keys,
1557         .in_ext = wiimod_bboard_in_ext,
1558 };
1559
1560 /*
1561  * Pro Controller
1562  * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not
1563  * work together with the classic Wii, but only with the new Wii U. However, it
1564  * uses the same protocol and provides a builtin "classic controller pro"
1565  * extension, few standard buttons, a rumble motor, 4 LEDs and a battery.
1566  * We provide all these via a standard extension device as the device doesn't
1567  * feature an extension port.
1568  */
1569
1570 enum wiimod_pro_keys {
1571         WIIMOD_PRO_KEY_A,
1572         WIIMOD_PRO_KEY_B,
1573         WIIMOD_PRO_KEY_X,
1574         WIIMOD_PRO_KEY_Y,
1575         WIIMOD_PRO_KEY_PLUS,
1576         WIIMOD_PRO_KEY_MINUS,
1577         WIIMOD_PRO_KEY_HOME,
1578         WIIMOD_PRO_KEY_LEFT,
1579         WIIMOD_PRO_KEY_RIGHT,
1580         WIIMOD_PRO_KEY_UP,
1581         WIIMOD_PRO_KEY_DOWN,
1582         WIIMOD_PRO_KEY_TL,
1583         WIIMOD_PRO_KEY_TR,
1584         WIIMOD_PRO_KEY_ZL,
1585         WIIMOD_PRO_KEY_ZR,
1586         WIIMOD_PRO_KEY_THUMBL,
1587         WIIMOD_PRO_KEY_THUMBR,
1588         WIIMOD_PRO_KEY_NUM,
1589 };
1590
1591 static const __u16 wiimod_pro_map[] = {
1592         BTN_EAST,       /* WIIMOD_PRO_KEY_A */
1593         BTN_SOUTH,      /* WIIMOD_PRO_KEY_B */
1594         BTN_NORTH,      /* WIIMOD_PRO_KEY_X */
1595         BTN_WEST,       /* WIIMOD_PRO_KEY_Y */
1596         BTN_START,      /* WIIMOD_PRO_KEY_PLUS */
1597         BTN_SELECT,     /* WIIMOD_PRO_KEY_MINUS */
1598         BTN_MODE,       /* WIIMOD_PRO_KEY_HOME */
1599         BTN_DPAD_LEFT,  /* WIIMOD_PRO_KEY_LEFT */
1600         BTN_DPAD_RIGHT, /* WIIMOD_PRO_KEY_RIGHT */
1601         BTN_DPAD_UP,    /* WIIMOD_PRO_KEY_UP */
1602         BTN_DPAD_DOWN,  /* WIIMOD_PRO_KEY_DOWN */
1603         BTN_TL,         /* WIIMOD_PRO_KEY_TL */
1604         BTN_TR,         /* WIIMOD_PRO_KEY_TR */
1605         BTN_TL2,        /* WIIMOD_PRO_KEY_ZL */
1606         BTN_TR2,        /* WIIMOD_PRO_KEY_ZR */
1607         BTN_THUMBL,     /* WIIMOD_PRO_KEY_THUMBL */
1608         BTN_THUMBR,     /* WIIMOD_PRO_KEY_THUMBR */
1609 };
1610
1611 static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1612 {
1613         __s16 rx, ry, lx, ly;
1614
1615         /*   Byte |  8  |  7  |  6  |  5  |  4  |  3  |  2  |  1  |
1616          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1617          *    1   |                   LX <7:0>                    |
1618          *   -----+-----------------------+-----------------------+
1619          *    2   |  0     0     0     0  |       LX <11:8>       |
1620          *   -----+-----------------------+-----------------------+
1621          *    3   |                   RX <7:0>                    |
1622          *   -----+-----------------------+-----------------------+
1623          *    4   |  0     0     0     0  |       RX <11:8>       |
1624          *   -----+-----------------------+-----------------------+
1625          *    5   |                   LY <7:0>                    |
1626          *   -----+-----------------------+-----------------------+
1627          *    6   |  0     0     0     0  |       LY <11:8>       |
1628          *   -----+-----------------------+-----------------------+
1629          *    7   |                   RY <7:0>                    |
1630          *   -----+-----------------------+-----------------------+
1631          *    8   |  0     0     0     0  |       RY <11:8>       |
1632          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1633          *    9   | BDR | BDD | BLT | B-  | BH  | B+  | BRT |  1  |
1634          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1635          *   10   | BZL | BB  | BY  | BA  | BX  | BZR | BDL | BDU |
1636          *   -----+-----+-----+-----+-----+-----+-----+-----+-----+
1637          *   11   |  1  |     BATTERY     | USB |CHARG|LTHUM|RTHUM|
1638          *   -----+-----+-----------------+-----------+-----+-----+
1639          * All buttons are low-active (0 if pressed)
1640          * RX and RY are right analog stick
1641          * LX and LY are left analog stick
1642          * BLT is left trigger, BRT is right trigger.
1643          * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1644          * BZL is left Z button and BZR is right Z button
1645          * B-, BH, B+ are +, HOME and - buttons
1646          * BB, BY, BA, BX are A, B, X, Y buttons
1647          *
1648          * Bits marked as 0/1 are unknown and never changed during tests.
1649          *
1650          * Not entirely verified:
1651          *   CHARG: 1 if uncharging, 0 if charging
1652          *   USB: 1 if not connected, 0 if connected
1653          *   BATTERY: battery capacity from 000 (empty) to 100 (full)
1654          */
1655
1656         lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8);
1657         rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8);
1658         ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8);
1659         ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8);
1660
1661         /* zero-point offsets */
1662         lx -= 0x800;
1663         ly = 0x800 - ly;
1664         rx -= 0x800;
1665         ry = 0x800 - ry;
1666
1667         /* Trivial automatic calibration. We don't know any calibration data
1668          * in the EEPROM so we must use the first report to calibrate the
1669          * null-position of the analog sticks. Users can retrigger calibration
1670          * via sysfs, or set it explicitly. If data is off more than abs(500),
1671          * we skip calibration as the sticks are likely to be moved already. */
1672         if (!(wdata->state.flags & WIIPROTO_FLAG_PRO_CALIB_DONE)) {
1673                 wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1674                 if (abs(lx) < 500)
1675                         wdata->state.calib_pro_sticks[0] = -lx;
1676                 if (abs(ly) < 500)
1677                         wdata->state.calib_pro_sticks[1] = -ly;
1678                 if (abs(rx) < 500)
1679                         wdata->state.calib_pro_sticks[2] = -rx;
1680                 if (abs(ry) < 500)
1681                         wdata->state.calib_pro_sticks[3] = -ry;
1682         }
1683
1684         /* apply calibration data */
1685         lx += wdata->state.calib_pro_sticks[0];
1686         ly += wdata->state.calib_pro_sticks[1];
1687         rx += wdata->state.calib_pro_sticks[2];
1688         ry += wdata->state.calib_pro_sticks[3];
1689
1690         input_report_abs(wdata->extension.input, ABS_X, lx);
1691         input_report_abs(wdata->extension.input, ABS_Y, ly);
1692         input_report_abs(wdata->extension.input, ABS_RX, rx);
1693         input_report_abs(wdata->extension.input, ABS_RY, ry);
1694
1695         input_report_key(wdata->extension.input,
1696                          wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
1697                          !(ext[8] & 0x80));
1698         input_report_key(wdata->extension.input,
1699                          wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
1700                          !(ext[8] & 0x40));
1701         input_report_key(wdata->extension.input,
1702                          wiimod_pro_map[WIIMOD_PRO_KEY_TL],
1703                          !(ext[8] & 0x20));
1704         input_report_key(wdata->extension.input,
1705                          wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
1706                          !(ext[8] & 0x10));
1707         input_report_key(wdata->extension.input,
1708                          wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
1709                          !(ext[8] & 0x08));
1710         input_report_key(wdata->extension.input,
1711                          wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
1712                          !(ext[8] & 0x04));
1713         input_report_key(wdata->extension.input,
1714                          wiimod_pro_map[WIIMOD_PRO_KEY_TR],
1715                          !(ext[8] & 0x02));
1716
1717         input_report_key(wdata->extension.input,
1718                          wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
1719                          !(ext[9] & 0x80));
1720         input_report_key(wdata->extension.input,
1721                          wiimod_pro_map[WIIMOD_PRO_KEY_B],
1722                          !(ext[9] & 0x40));
1723         input_report_key(wdata->extension.input,
1724                          wiimod_pro_map[WIIMOD_PRO_KEY_Y],
1725                          !(ext[9] & 0x20));
1726         input_report_key(wdata->extension.input,
1727                          wiimod_pro_map[WIIMOD_PRO_KEY_A],
1728                          !(ext[9] & 0x10));
1729         input_report_key(wdata->extension.input,
1730                          wiimod_pro_map[WIIMOD_PRO_KEY_X],
1731                          !(ext[9] & 0x08));
1732         input_report_key(wdata->extension.input,
1733                          wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
1734                          !(ext[9] & 0x04));
1735         input_report_key(wdata->extension.input,
1736                          wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
1737                          !(ext[9] & 0x02));
1738         input_report_key(wdata->extension.input,
1739                          wiimod_pro_map[WIIMOD_PRO_KEY_UP],
1740                          !(ext[9] & 0x01));
1741
1742         input_report_key(wdata->extension.input,
1743                          wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
1744                          !(ext[10] & 0x02));
1745         input_report_key(wdata->extension.input,
1746                          wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
1747                          !(ext[10] & 0x01));
1748
1749         input_sync(wdata->extension.input);
1750 }
1751
1752 static int wiimod_pro_open(struct input_dev *dev)
1753 {
1754         struct wiimote_data *wdata = input_get_drvdata(dev);
1755         unsigned long flags;
1756
1757         spin_lock_irqsave(&wdata->state.lock, flags);
1758         wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1759         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1760         spin_unlock_irqrestore(&wdata->state.lock, flags);
1761
1762         return 0;
1763 }
1764
1765 static void wiimod_pro_close(struct input_dev *dev)
1766 {
1767         struct wiimote_data *wdata = input_get_drvdata(dev);
1768         unsigned long flags;
1769
1770         spin_lock_irqsave(&wdata->state.lock, flags);
1771         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1772         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1773         spin_unlock_irqrestore(&wdata->state.lock, flags);
1774 }
1775
1776 static int wiimod_pro_play(struct input_dev *dev, void *data,
1777                            struct ff_effect *eff)
1778 {
1779         struct wiimote_data *wdata = input_get_drvdata(dev);
1780         __u8 value;
1781
1782         /*
1783          * The wiimote supports only a single rumble motor so if any magnitude
1784          * is set to non-zero then we start the rumble motor. If both are set to
1785          * zero, we stop the rumble motor.
1786          */
1787
1788         if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
1789                 value = 1;
1790         else
1791                 value = 0;
1792
1793         /* Locking state.lock here might deadlock with input_event() calls.
1794          * schedule_work acts as barrier. Merging multiple changes is fine. */
1795         wdata->state.cache_rumble = value;
1796         schedule_work(&wdata->rumble_worker);
1797
1798         return 0;
1799 }
1800
1801 static ssize_t wiimod_pro_calib_show(struct device *dev,
1802                                      struct device_attribute *attr,
1803                                      char *out)
1804 {
1805         struct wiimote_data *wdata = dev_to_wii(dev);
1806         int r;
1807
1808         r = 0;
1809         r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[0]);
1810         r += sprintf(&out[r], "%+06hd ", wdata->state.calib_pro_sticks[1]);
1811         r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[2]);
1812         r += sprintf(&out[r], "%+06hd\n", wdata->state.calib_pro_sticks[3]);
1813
1814         return r;
1815 }
1816
1817 static ssize_t wiimod_pro_calib_store(struct device *dev,
1818                                       struct device_attribute *attr,
1819                                       const char *buf, size_t count)
1820 {
1821         struct wiimote_data *wdata = dev_to_wii(dev);
1822         int r;
1823         s16 x1, y1, x2, y2;
1824
1825         if (!strncmp(buf, "scan\n", 5)) {
1826                 spin_lock_irq(&wdata->state.lock);
1827                 wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1828                 spin_unlock_irq(&wdata->state.lock);
1829         } else {
1830                 r = sscanf(buf, "%hd:%hd %hd:%hd", &x1, &y1, &x2, &y2);
1831                 if (r != 4)
1832                         return -EINVAL;
1833
1834                 spin_lock_irq(&wdata->state.lock);
1835                 wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1836                 spin_unlock_irq(&wdata->state.lock);
1837
1838                 wdata->state.calib_pro_sticks[0] = x1;
1839                 wdata->state.calib_pro_sticks[1] = y1;
1840                 wdata->state.calib_pro_sticks[2] = x2;
1841                 wdata->state.calib_pro_sticks[3] = y2;
1842         }
1843
1844         return strnlen(buf, PAGE_SIZE);
1845 }
1846
1847 static DEVICE_ATTR(pro_calib, S_IRUGO|S_IWUSR|S_IWGRP, wiimod_pro_calib_show,
1848                    wiimod_pro_calib_store);
1849
1850 static int wiimod_pro_probe(const struct wiimod_ops *ops,
1851                             struct wiimote_data *wdata)
1852 {
1853         int ret, i;
1854         unsigned long flags;
1855
1856         INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
1857         wdata->state.calib_pro_sticks[0] = 0;
1858         wdata->state.calib_pro_sticks[1] = 0;
1859         wdata->state.calib_pro_sticks[2] = 0;
1860         wdata->state.calib_pro_sticks[3] = 0;
1861
1862         spin_lock_irqsave(&wdata->state.lock, flags);
1863         wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1864         spin_unlock_irqrestore(&wdata->state.lock, flags);
1865
1866         wdata->extension.input = input_allocate_device();
1867         if (!wdata->extension.input)
1868                 return -ENOMEM;
1869
1870         set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
1871         input_set_drvdata(wdata->extension.input, wdata);
1872
1873         if (input_ff_create_memless(wdata->extension.input, NULL,
1874                                     wiimod_pro_play)) {
1875                 ret = -ENOMEM;
1876                 goto err_free;
1877         }
1878
1879         ret = device_create_file(&wdata->hdev->dev,
1880                                  &dev_attr_pro_calib);
1881         if (ret) {
1882                 hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1883                 goto err_free;
1884         }
1885
1886         wdata->extension.input->open = wiimod_pro_open;
1887         wdata->extension.input->close = wiimod_pro_close;
1888         wdata->extension.input->dev.parent = &wdata->hdev->dev;
1889         wdata->extension.input->id.bustype = wdata->hdev->bus;
1890         wdata->extension.input->id.vendor = wdata->hdev->vendor;
1891         wdata->extension.input->id.product = wdata->hdev->product;
1892         wdata->extension.input->id.version = wdata->hdev->version;
1893         wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
1894
1895         set_bit(EV_KEY, wdata->extension.input->evbit);
1896         for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
1897                 set_bit(wiimod_pro_map[i],
1898                         wdata->extension.input->keybit);
1899
1900         set_bit(EV_ABS, wdata->extension.input->evbit);
1901         set_bit(ABS_X, wdata->extension.input->absbit);
1902         set_bit(ABS_Y, wdata->extension.input->absbit);
1903         set_bit(ABS_RX, wdata->extension.input->absbit);
1904         set_bit(ABS_RY, wdata->extension.input->absbit);
1905         input_set_abs_params(wdata->extension.input,
1906                              ABS_X, -0x400, 0x400, 4, 100);
1907         input_set_abs_params(wdata->extension.input,
1908                              ABS_Y, -0x400, 0x400, 4, 100);
1909         input_set_abs_params(wdata->extension.input,
1910                              ABS_RX, -0x400, 0x400, 4, 100);
1911         input_set_abs_params(wdata->extension.input,
1912                              ABS_RY, -0x400, 0x400, 4, 100);
1913
1914         ret = input_register_device(wdata->extension.input);
1915         if (ret)
1916                 goto err_file;
1917
1918         return 0;
1919
1920 err_file:
1921         device_remove_file(&wdata->hdev->dev,
1922                            &dev_attr_pro_calib);
1923 err_free:
1924         input_free_device(wdata->extension.input);
1925         wdata->extension.input = NULL;
1926         return ret;
1927 }
1928
1929 static void wiimod_pro_remove(const struct wiimod_ops *ops,
1930                               struct wiimote_data *wdata)
1931 {
1932         unsigned long flags;
1933
1934         if (!wdata->extension.input)
1935                 return;
1936
1937         input_unregister_device(wdata->extension.input);
1938         wdata->extension.input = NULL;
1939         cancel_work_sync(&wdata->rumble_worker);
1940         device_remove_file(&wdata->hdev->dev,
1941                            &dev_attr_pro_calib);
1942
1943         spin_lock_irqsave(&wdata->state.lock, flags);
1944         wiiproto_req_rumble(wdata, 0);
1945         spin_unlock_irqrestore(&wdata->state.lock, flags);
1946 }
1947
1948 static const struct wiimod_ops wiimod_pro = {
1949         .flags = WIIMOD_FLAG_EXT16,
1950         .arg = 0,
1951         .probe = wiimod_pro_probe,
1952         .remove = wiimod_pro_remove,
1953         .in_ext = wiimod_pro_in_ext,
1954 };
1955
1956 /*
1957  * Builtin Motion Plus
1958  * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
1959  * disables polling for Motion-Plus. This should be set only for devices which
1960  * don't allow MP hotplugging.
1961  */
1962
1963 static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
1964                                    struct wiimote_data *wdata)
1965 {
1966         unsigned long flags;
1967
1968         spin_lock_irqsave(&wdata->state.lock, flags);
1969         wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
1970         spin_unlock_irqrestore(&wdata->state.lock, flags);
1971
1972         return 0;
1973 }
1974
1975 static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
1976                                      struct wiimote_data *wdata)
1977 {
1978         unsigned long flags;
1979
1980         spin_lock_irqsave(&wdata->state.lock, flags);
1981         wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
1982         spin_unlock_irqrestore(&wdata->state.lock, flags);
1983 }
1984
1985 static const struct wiimod_ops wiimod_builtin_mp = {
1986         .flags = 0,
1987         .arg = 0,
1988         .probe = wiimod_builtin_mp_probe,
1989         .remove = wiimod_builtin_mp_remove,
1990 };
1991
1992 /*
1993  * No Motion Plus
1994  * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
1995  * disables motion-plus. This is needed for devices that advertise this but we
1996  * don't know how to use it (or whether it is actually present).
1997  */
1998
1999 static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
2000                               struct wiimote_data *wdata)
2001 {
2002         unsigned long flags;
2003
2004         spin_lock_irqsave(&wdata->state.lock, flags);
2005         wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2006         spin_unlock_irqrestore(&wdata->state.lock, flags);
2007
2008         return 0;
2009 }
2010
2011 static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
2012                                 struct wiimote_data *wdata)
2013 {
2014         unsigned long flags;
2015
2016         spin_lock_irqsave(&wdata->state.lock, flags);
2017         wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2018         spin_unlock_irqrestore(&wdata->state.lock, flags);
2019 }
2020
2021 static const struct wiimod_ops wiimod_no_mp = {
2022         .flags = 0,
2023         .arg = 0,
2024         .probe = wiimod_no_mp_probe,
2025         .remove = wiimod_no_mp_remove,
2026 };
2027
2028 /*
2029  * Motion Plus
2030  * The Motion Plus extension provides rotation sensors (gyro) as a small
2031  * extension device for Wii Remotes. Many devices have them built-in so
2032  * you cannot see them from the outside.
2033  * Motion Plus extensions are special because they are on a separate extension
2034  * port and allow other extensions to be used simultaneously. This is all
2035  * handled by the Wiimote Core so we don't have to deal with it.
2036  */
2037
2038 static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
2039 {
2040         __s32 x, y, z;
2041
2042         /*        |   8    7    6    5    4    3 |  2  |  1  |
2043          *   -----+------------------------------+-----+-----+
2044          *    1   |               Yaw Speed <7:0>            |
2045          *    2   |              Roll Speed <7:0>            |
2046          *    3   |             Pitch Speed <7:0>            |
2047          *   -----+------------------------------+-----+-----+
2048          *    4   |       Yaw Speed <13:8>       | Yaw |Pitch|
2049          *   -----+------------------------------+-----+-----+
2050          *    5   |      Roll Speed <13:8>       |Roll | Ext |
2051          *   -----+------------------------------+-----+-----+
2052          *    6   |     Pitch Speed <13:8>       |  1  |  0  |
2053          *   -----+------------------------------+-----+-----+
2054          * The single bits Yaw, Roll, Pitch in the lower right corner specify
2055          * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
2056          * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
2057          * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
2058          * and 9 for slow.
2059          * If the wiimote is not rotating the sensor reports 2^13 = 8192.
2060          * Ext specifies whether an extension is connected to the motionp.
2061          * which is parsed by wiimote-core.
2062          */
2063
2064         x = ext[0];
2065         y = ext[1];
2066         z = ext[2];
2067
2068         x |= (((__u16)ext[3]) << 6) & 0xff00;
2069         y |= (((__u16)ext[4]) << 6) & 0xff00;
2070         z |= (((__u16)ext[5]) << 6) & 0xff00;
2071
2072         x -= 8192;
2073         y -= 8192;
2074         z -= 8192;
2075
2076         if (!(ext[3] & 0x02))
2077                 x *= 18;
2078         else
2079                 x *= 9;
2080         if (!(ext[4] & 0x02))
2081                 y *= 18;
2082         else
2083                 y *= 9;
2084         if (!(ext[3] & 0x01))
2085                 z *= 18;
2086         else
2087                 z *= 9;
2088
2089         input_report_abs(wdata->mp, ABS_RX, x);
2090         input_report_abs(wdata->mp, ABS_RY, y);
2091         input_report_abs(wdata->mp, ABS_RZ, z);
2092         input_sync(wdata->mp);
2093 }
2094
2095 static int wiimod_mp_open(struct input_dev *dev)
2096 {
2097         struct wiimote_data *wdata = input_get_drvdata(dev);
2098         unsigned long flags;
2099
2100         spin_lock_irqsave(&wdata->state.lock, flags);
2101         wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
2102         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2103         __wiimote_schedule(wdata);
2104         spin_unlock_irqrestore(&wdata->state.lock, flags);
2105
2106         return 0;
2107 }
2108
2109 static void wiimod_mp_close(struct input_dev *dev)
2110 {
2111         struct wiimote_data *wdata = input_get_drvdata(dev);
2112         unsigned long flags;
2113
2114         spin_lock_irqsave(&wdata->state.lock, flags);
2115         wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
2116         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2117         __wiimote_schedule(wdata);
2118         spin_unlock_irqrestore(&wdata->state.lock, flags);
2119 }
2120
2121 static int wiimod_mp_probe(const struct wiimod_ops *ops,
2122                            struct wiimote_data *wdata)
2123 {
2124         int ret;
2125
2126         wdata->mp = input_allocate_device();
2127         if (!wdata->mp)
2128                 return -ENOMEM;
2129
2130         input_set_drvdata(wdata->mp, wdata);
2131         wdata->mp->open = wiimod_mp_open;
2132         wdata->mp->close = wiimod_mp_close;
2133         wdata->mp->dev.parent = &wdata->hdev->dev;
2134         wdata->mp->id.bustype = wdata->hdev->bus;
2135         wdata->mp->id.vendor = wdata->hdev->vendor;
2136         wdata->mp->id.product = wdata->hdev->product;
2137         wdata->mp->id.version = wdata->hdev->version;
2138         wdata->mp->name = WIIMOTE_NAME " Motion Plus";
2139
2140         set_bit(EV_ABS, wdata->mp->evbit);
2141         set_bit(ABS_RX, wdata->mp->absbit);
2142         set_bit(ABS_RY, wdata->mp->absbit);
2143         set_bit(ABS_RZ, wdata->mp->absbit);
2144         input_set_abs_params(wdata->mp,
2145                              ABS_RX, -16000, 16000, 4, 8);
2146         input_set_abs_params(wdata->mp,
2147                              ABS_RY, -16000, 16000, 4, 8);
2148         input_set_abs_params(wdata->mp,
2149                              ABS_RZ, -16000, 16000, 4, 8);
2150
2151         ret = input_register_device(wdata->mp);
2152         if (ret)
2153                 goto err_free;
2154
2155         return 0;
2156
2157 err_free:
2158         input_free_device(wdata->mp);
2159         wdata->mp = NULL;
2160         return ret;
2161 }
2162
2163 static void wiimod_mp_remove(const struct wiimod_ops *ops,
2164                              struct wiimote_data *wdata)
2165 {
2166         if (!wdata->mp)
2167                 return;
2168
2169         input_unregister_device(wdata->mp);
2170         wdata->mp = NULL;
2171 }
2172
2173 const struct wiimod_ops wiimod_mp = {
2174         .flags = 0,
2175         .arg = 0,
2176         .probe = wiimod_mp_probe,
2177         .remove = wiimod_mp_remove,
2178         .in_mp = wiimod_mp_in_mp,
2179 };
2180
2181 /* module table */
2182
2183 static const struct wiimod_ops wiimod_dummy;
2184
2185 const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
2186         [WIIMOD_KEYS] = &wiimod_keys,
2187         [WIIMOD_RUMBLE] = &wiimod_rumble,
2188         [WIIMOD_BATTERY] = &wiimod_battery,
2189         [WIIMOD_LED1] = &wiimod_leds[0],
2190         [WIIMOD_LED2] = &wiimod_leds[1],
2191         [WIIMOD_LED3] = &wiimod_leds[2],
2192         [WIIMOD_LED4] = &wiimod_leds[3],
2193         [WIIMOD_ACCEL] = &wiimod_accel,
2194         [WIIMOD_IR] = &wiimod_ir,
2195         [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
2196         [WIIMOD_NO_MP] = &wiimod_no_mp,
2197 };
2198
2199 const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
2200         [WIIMOTE_EXT_NONE] = &wiimod_dummy,
2201         [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
2202         [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
2203         [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
2204         [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
2205         [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,
2206 };