ASoC: wm8711: Use IS_ENABLED() macro
[linux.git] / drivers / hid / hid-wiimote-core.c
1 /*
2  * HID driver for Nintendo Wii / Wii U peripherals
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 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/spinlock.h>
20 #include "hid-ids.h"
21 #include "hid-wiimote.h"
22
23 /* output queue handling */
24
25 static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
26                             size_t count)
27 {
28         __u8 *buf;
29         int ret;
30
31         if (!hdev->hid_output_raw_report)
32                 return -ENODEV;
33
34         buf = kmemdup(buffer, count, GFP_KERNEL);
35         if (!buf)
36                 return -ENOMEM;
37
38         ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
39
40         kfree(buf);
41         return ret;
42 }
43
44 static void wiimote_queue_worker(struct work_struct *work)
45 {
46         struct wiimote_queue *queue = container_of(work, struct wiimote_queue,
47                                                    worker);
48         struct wiimote_data *wdata = container_of(queue, struct wiimote_data,
49                                                   queue);
50         unsigned long flags;
51         int ret;
52
53         spin_lock_irqsave(&wdata->queue.lock, flags);
54
55         while (wdata->queue.head != wdata->queue.tail) {
56                 spin_unlock_irqrestore(&wdata->queue.lock, flags);
57                 ret = wiimote_hid_send(wdata->hdev,
58                                  wdata->queue.outq[wdata->queue.tail].data,
59                                  wdata->queue.outq[wdata->queue.tail].size);
60                 if (ret < 0) {
61                         spin_lock_irqsave(&wdata->state.lock, flags);
62                         wiimote_cmd_abort(wdata);
63                         spin_unlock_irqrestore(&wdata->state.lock, flags);
64                 }
65                 spin_lock_irqsave(&wdata->queue.lock, flags);
66
67                 wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
68         }
69
70         spin_unlock_irqrestore(&wdata->queue.lock, flags);
71 }
72
73 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
74                                                                 size_t count)
75 {
76         unsigned long flags;
77         __u8 newhead;
78
79         if (count > HID_MAX_BUFFER_SIZE) {
80                 hid_warn(wdata->hdev, "Sending too large output report\n");
81
82                 spin_lock_irqsave(&wdata->queue.lock, flags);
83                 goto out_error;
84         }
85
86         /*
87          * Copy new request into our output queue and check whether the
88          * queue is full. If it is full, discard this request.
89          * If it is empty we need to start a new worker that will
90          * send out the buffer to the hid device.
91          * If the queue is not empty, then there must be a worker
92          * that is currently sending out our buffer and this worker
93          * will reschedule itself until the queue is empty.
94          */
95
96         spin_lock_irqsave(&wdata->queue.lock, flags);
97
98         memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
99         wdata->queue.outq[wdata->queue.head].size = count;
100         newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
101
102         if (wdata->queue.head == wdata->queue.tail) {
103                 wdata->queue.head = newhead;
104                 schedule_work(&wdata->queue.worker);
105         } else if (newhead != wdata->queue.tail) {
106                 wdata->queue.head = newhead;
107         } else {
108                 hid_warn(wdata->hdev, "Output queue is full");
109                 goto out_error;
110         }
111
112         goto out_unlock;
113
114 out_error:
115         wiimote_cmd_abort(wdata);
116 out_unlock:
117         spin_unlock_irqrestore(&wdata->queue.lock, flags);
118 }
119
120 /*
121  * This sets the rumble bit on the given output report if rumble is
122  * currently enabled.
123  * \cmd1 must point to the second byte in the output report => &cmd[1]
124  * This must be called on nearly every output report before passing it
125  * into the output queue!
126  */
127 static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
128 {
129         if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
130                 *cmd1 |= 0x01;
131 }
132
133 void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
134 {
135         __u8 cmd[2];
136
137         rumble = !!rumble;
138         if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
139                 return;
140
141         if (rumble)
142                 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
143         else
144                 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
145
146         cmd[0] = WIIPROTO_REQ_RUMBLE;
147         cmd[1] = 0;
148
149         wiiproto_keep_rumble(wdata, &cmd[1]);
150         wiimote_queue(wdata, cmd, sizeof(cmd));
151 }
152
153 void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
154 {
155         __u8 cmd[2];
156
157         leds &= WIIPROTO_FLAGS_LEDS;
158         if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
159                 return;
160         wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
161
162         cmd[0] = WIIPROTO_REQ_LED;
163         cmd[1] = 0;
164
165         if (leds & WIIPROTO_FLAG_LED1)
166                 cmd[1] |= 0x10;
167         if (leds & WIIPROTO_FLAG_LED2)
168                 cmd[1] |= 0x20;
169         if (leds & WIIPROTO_FLAG_LED3)
170                 cmd[1] |= 0x40;
171         if (leds & WIIPROTO_FLAG_LED4)
172                 cmd[1] |= 0x80;
173
174         wiiproto_keep_rumble(wdata, &cmd[1]);
175         wiimote_queue(wdata, cmd, sizeof(cmd));
176 }
177
178 /*
179  * Check what peripherals of the wiimote are currently
180  * active and select a proper DRM that supports all of
181  * the requested data inputs.
182  *
183  * Not all combinations are actually supported. The following
184  * combinations work only with limitations:
185  *  - IR cam in extended or full mode disables any data transmission
186  *    of extension controllers. There is no DRM mode that supports
187  *    extension bytes plus extended/full IR.
188  *  - IR cam with accelerometer and extension *_EXT8 is not supported.
189  *    However, all extensions that need *_EXT8 are devices that don't
190  *    support IR cameras. Hence, this shouldn't happen under normal
191  *    operation.
192  *  - *_EXT16 is only supported in combination with buttons and
193  *    accelerometer. No IR or similar can be active simultaneously. As
194  *    above, all modules that require it are mutually exclusive with
195  *    IR/etc. so this doesn't matter.
196  */
197 static __u8 select_drm(struct wiimote_data *wdata)
198 {
199         __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
200         bool ext;
201
202         ext = (wdata->state.flags & WIIPROTO_FLAG_EXT_USED) ||
203               (wdata->state.flags & WIIPROTO_FLAG_MP_USED);
204
205         /* some 3rd-party balance-boards are hard-coded to KEE, *sigh* */
206         if (wdata->state.devtype == WIIMOTE_DEV_BALANCE_BOARD) {
207                 if (ext)
208                         return WIIPROTO_REQ_DRM_KEE;
209                 else
210                         return WIIPROTO_REQ_DRM_K;
211         }
212
213         if (ir == WIIPROTO_FLAG_IR_BASIC) {
214                 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
215                         /* GEN10 and ealier devices bind IR formats to DRMs.
216                          * Hence, we cannot use DRM_KAI here as it might be
217                          * bound to IR_EXT. Use DRM_KAIE unconditionally so we
218                          * work with all devices and our parsers can use the
219                          * fixed formats, too. */
220                         return WIIPROTO_REQ_DRM_KAIE;
221                 } else {
222                         return WIIPROTO_REQ_DRM_KIE;
223                 }
224         } else if (ir == WIIPROTO_FLAG_IR_EXT) {
225                 return WIIPROTO_REQ_DRM_KAI;
226         } else if (ir == WIIPROTO_FLAG_IR_FULL) {
227                 return WIIPROTO_REQ_DRM_SKAI1;
228         } else {
229                 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
230                         if (ext)
231                                 return WIIPROTO_REQ_DRM_KAE;
232                         else
233                                 return WIIPROTO_REQ_DRM_KA;
234                 } else {
235                         if (ext)
236                                 return WIIPROTO_REQ_DRM_KEE;
237                         else
238                                 return WIIPROTO_REQ_DRM_K;
239                 }
240         }
241 }
242
243 void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
244 {
245         __u8 cmd[3];
246
247         if (wdata->state.flags & WIIPROTO_FLAG_DRM_LOCKED)
248                 drm = wdata->state.drm;
249         else if (drm == WIIPROTO_REQ_NULL)
250                 drm = select_drm(wdata);
251
252         cmd[0] = WIIPROTO_REQ_DRM;
253         cmd[1] = 0;
254         cmd[2] = drm;
255
256         wdata->state.drm = drm;
257         wiiproto_keep_rumble(wdata, &cmd[1]);
258         wiimote_queue(wdata, cmd, sizeof(cmd));
259 }
260
261 void wiiproto_req_status(struct wiimote_data *wdata)
262 {
263         __u8 cmd[2];
264
265         cmd[0] = WIIPROTO_REQ_SREQ;
266         cmd[1] = 0;
267
268         wiiproto_keep_rumble(wdata, &cmd[1]);
269         wiimote_queue(wdata, cmd, sizeof(cmd));
270 }
271
272 void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
273 {
274         accel = !!accel;
275         if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
276                 return;
277
278         if (accel)
279                 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
280         else
281                 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
282
283         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
284 }
285
286 void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
287 {
288         __u8 cmd[2];
289
290         cmd[0] = WIIPROTO_REQ_IR1;
291         cmd[1] = flags;
292
293         wiiproto_keep_rumble(wdata, &cmd[1]);
294         wiimote_queue(wdata, cmd, sizeof(cmd));
295 }
296
297 void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
298 {
299         __u8 cmd[2];
300
301         cmd[0] = WIIPROTO_REQ_IR2;
302         cmd[1] = flags;
303
304         wiiproto_keep_rumble(wdata, &cmd[1]);
305         wiimote_queue(wdata, cmd, sizeof(cmd));
306 }
307
308 #define wiiproto_req_wreg(wdata, os, buf, sz) \
309                         wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
310
311 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
312                         wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
313
314 static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
315                                 __u32 offset, const __u8 *buf, __u8 size)
316 {
317         __u8 cmd[22];
318
319         if (size > 16 || size == 0) {
320                 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
321                 return;
322         }
323
324         memset(cmd, 0, sizeof(cmd));
325         cmd[0] = WIIPROTO_REQ_WMEM;
326         cmd[2] = (offset >> 16) & 0xff;
327         cmd[3] = (offset >> 8) & 0xff;
328         cmd[4] = offset & 0xff;
329         cmd[5] = size;
330         memcpy(&cmd[6], buf, size);
331
332         if (!eeprom)
333                 cmd[1] |= 0x04;
334
335         wiiproto_keep_rumble(wdata, &cmd[1]);
336         wiimote_queue(wdata, cmd, sizeof(cmd));
337 }
338
339 void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
340                                                                 __u16 size)
341 {
342         __u8 cmd[7];
343
344         if (size == 0) {
345                 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
346                 return;
347         }
348
349         cmd[0] = WIIPROTO_REQ_RMEM;
350         cmd[1] = 0;
351         cmd[2] = (offset >> 16) & 0xff;
352         cmd[3] = (offset >> 8) & 0xff;
353         cmd[4] = offset & 0xff;
354         cmd[5] = (size >> 8) & 0xff;
355         cmd[6] = size & 0xff;
356
357         if (!eeprom)
358                 cmd[1] |= 0x04;
359
360         wiiproto_keep_rumble(wdata, &cmd[1]);
361         wiimote_queue(wdata, cmd, sizeof(cmd));
362 }
363
364 /* requries the cmd-mutex to be held */
365 int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
366                                                 const __u8 *wmem, __u8 size)
367 {
368         unsigned long flags;
369         int ret;
370
371         spin_lock_irqsave(&wdata->state.lock, flags);
372         wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
373         wiiproto_req_wreg(wdata, offset, wmem, size);
374         spin_unlock_irqrestore(&wdata->state.lock, flags);
375
376         ret = wiimote_cmd_wait(wdata);
377         if (!ret && wdata->state.cmd_err)
378                 ret = -EIO;
379
380         return ret;
381 }
382
383 /* requries the cmd-mutex to be held */
384 ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
385                                                                 __u8 size)
386 {
387         unsigned long flags;
388         ssize_t ret;
389
390         spin_lock_irqsave(&wdata->state.lock, flags);
391         wdata->state.cmd_read_size = size;
392         wdata->state.cmd_read_buf = rmem;
393         wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
394         wiiproto_req_rreg(wdata, offset, size);
395         spin_unlock_irqrestore(&wdata->state.lock, flags);
396
397         ret = wiimote_cmd_wait(wdata);
398
399         spin_lock_irqsave(&wdata->state.lock, flags);
400         wdata->state.cmd_read_buf = NULL;
401         spin_unlock_irqrestore(&wdata->state.lock, flags);
402
403         if (!ret) {
404                 if (wdata->state.cmd_read_size == 0)
405                         ret = -EIO;
406                 else
407                         ret = wdata->state.cmd_read_size;
408         }
409
410         return ret;
411 }
412
413 /* requires the cmd-mutex to be held */
414 static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
415 {
416         __u8 wmem;
417         int ret;
418
419         /* initialize extension */
420         wmem = 0x55;
421         ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
422         if (ret)
423                 return ret;
424
425         /* disable default encryption */
426         wmem = 0x0;
427         ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
428         if (ret)
429                 return ret;
430
431         return 0;
432 }
433
434 /* requires the cmd-mutex to be held */
435 static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem)
436 {
437         int ret;
438
439         /* read extension ID */
440         ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
441         if (ret != 6)
442                 return WIIMOTE_EXT_NONE;
443
444         hid_dbg(wdata->hdev, "extension ID: %6phC\n", rmem);
445
446         if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
447             rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
448                 return WIIMOTE_EXT_NONE;
449
450         if (rmem[4] == 0x00 && rmem[5] == 0x00)
451                 return WIIMOTE_EXT_NUNCHUK;
452         if (rmem[4] == 0x01 && rmem[5] == 0x01)
453                 return WIIMOTE_EXT_CLASSIC_CONTROLLER;
454         if (rmem[4] == 0x04 && rmem[5] == 0x02)
455                 return WIIMOTE_EXT_BALANCE_BOARD;
456         if (rmem[4] == 0x01 && rmem[5] == 0x20)
457                 return WIIMOTE_EXT_PRO_CONTROLLER;
458
459         return WIIMOTE_EXT_UNKNOWN;
460 }
461
462 /* requires the cmd-mutex to be held */
463 static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
464 {
465         __u8 wmem;
466         int ret;
467
468         /* initialize MP */
469         wmem = 0x55;
470         ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
471         if (ret)
472                 return ret;
473
474         /* disable default encryption */
475         wmem = 0x0;
476         ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
477         if (ret)
478                 return ret;
479
480         return 0;
481 }
482
483 /* requires the cmd-mutex to be held */
484 static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
485 {
486         __u8 wmem;
487
488         /* map MP with correct pass-through mode */
489         switch (exttype) {
490         case WIIMOTE_EXT_CLASSIC_CONTROLLER:
491                 wmem = 0x07;
492                 break;
493         case WIIMOTE_EXT_NUNCHUK:
494                 wmem = 0x05;
495                 break;
496         default:
497                 wmem = 0x04;
498                 break;
499         }
500
501         return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
502 }
503
504 /* requires the cmd-mutex to be held */
505 static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
506 {
507         int ret;
508
509         /* read motion plus ID */
510         ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
511         if (ret != 6)
512                 return false;
513
514         hid_dbg(wdata->hdev, "motion plus ID: %6phC\n", rmem);
515
516         if (rmem[5] == 0x05)
517                 return true;
518
519         hid_info(wdata->hdev, "unknown motion plus ID: %6phC\n", rmem);
520
521         return false;
522 }
523
524 /* requires the cmd-mutex to be held */
525 static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
526 {
527         int ret;
528         __u8 rmem[6];
529
530         /* read motion plus ID */
531         ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
532         if (ret != 6)
533                 return WIIMOTE_MP_NONE;
534
535         hid_dbg(wdata->hdev, "mapped motion plus ID: %6phC\n", rmem);
536
537         if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
538             rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
539                 return WIIMOTE_MP_NONE;
540
541         if (rmem[4] == 0x04 && rmem[5] == 0x05)
542                 return WIIMOTE_MP_SINGLE;
543         else if (rmem[4] == 0x05 && rmem[5] == 0x05)
544                 return WIIMOTE_MP_PASSTHROUGH_NUNCHUK;
545         else if (rmem[4] == 0x07 && rmem[5] == 0x05)
546                 return WIIMOTE_MP_PASSTHROUGH_CLASSIC;
547
548         return WIIMOTE_MP_UNKNOWN;
549 }
550
551 /* device module handling */
552
553 static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
554         [WIIMOTE_DEV_PENDING] = (const __u8[]){
555                 WIIMOD_NULL,
556         },
557         [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
558                 WIIMOD_NO_MP,
559                 WIIMOD_NULL,
560         },
561         [WIIMOTE_DEV_GENERIC] = (const __u8[]){
562                 WIIMOD_KEYS,
563                 WIIMOD_RUMBLE,
564                 WIIMOD_BATTERY,
565                 WIIMOD_LED1,
566                 WIIMOD_LED2,
567                 WIIMOD_LED3,
568                 WIIMOD_LED4,
569                 WIIMOD_ACCEL,
570                 WIIMOD_IR,
571                 WIIMOD_NULL,
572         },
573         [WIIMOTE_DEV_GEN10] = (const __u8[]){
574                 WIIMOD_KEYS,
575                 WIIMOD_RUMBLE,
576                 WIIMOD_BATTERY,
577                 WIIMOD_LED1,
578                 WIIMOD_LED2,
579                 WIIMOD_LED3,
580                 WIIMOD_LED4,
581                 WIIMOD_ACCEL,
582                 WIIMOD_IR,
583                 WIIMOD_NULL,
584         },
585         [WIIMOTE_DEV_GEN20] = (const __u8[]){
586                 WIIMOD_KEYS,
587                 WIIMOD_RUMBLE,
588                 WIIMOD_BATTERY,
589                 WIIMOD_LED1,
590                 WIIMOD_LED2,
591                 WIIMOD_LED3,
592                 WIIMOD_LED4,
593                 WIIMOD_ACCEL,
594                 WIIMOD_IR,
595                 WIIMOD_BUILTIN_MP,
596                 WIIMOD_NULL,
597         },
598         [WIIMOTE_DEV_BALANCE_BOARD] = (const __u8[]) {
599                 WIIMOD_BATTERY,
600                 WIIMOD_LED1,
601                 WIIMOD_NO_MP,
602                 WIIMOD_NULL,
603         },
604         [WIIMOTE_DEV_PRO_CONTROLLER] = (const __u8[]) {
605                 WIIMOD_BATTERY,
606                 WIIMOD_LED1,
607                 WIIMOD_LED2,
608                 WIIMOD_LED3,
609                 WIIMOD_LED4,
610                 WIIMOD_NO_MP,
611                 WIIMOD_NULL,
612         },
613 };
614
615 static void wiimote_modules_load(struct wiimote_data *wdata,
616                                  unsigned int devtype)
617 {
618         bool need_input = false;
619         const __u8 *mods, *iter;
620         const struct wiimod_ops *ops;
621         int ret;
622
623         mods = wiimote_devtype_mods[devtype];
624
625         for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
626                 if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
627                         need_input = true;
628                         break;
629                 }
630         }
631
632         if (need_input) {
633                 wdata->input = input_allocate_device();
634                 if (!wdata->input)
635                         return;
636
637                 input_set_drvdata(wdata->input, wdata);
638                 wdata->input->dev.parent = &wdata->hdev->dev;
639                 wdata->input->id.bustype = wdata->hdev->bus;
640                 wdata->input->id.vendor = wdata->hdev->vendor;
641                 wdata->input->id.product = wdata->hdev->product;
642                 wdata->input->id.version = wdata->hdev->version;
643                 wdata->input->name = WIIMOTE_NAME;
644         }
645
646         for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
647                 ops = wiimod_table[*iter];
648                 if (!ops->probe)
649                         continue;
650
651                 ret = ops->probe(ops, wdata);
652                 if (ret)
653                         goto error;
654         }
655
656         if (wdata->input) {
657                 ret = input_register_device(wdata->input);
658                 if (ret)
659                         goto error;
660         }
661
662         spin_lock_irq(&wdata->state.lock);
663         wdata->state.devtype = devtype;
664         spin_unlock_irq(&wdata->state.lock);
665         return;
666
667 error:
668         for ( ; iter-- != mods; ) {
669                 ops = wiimod_table[*iter];
670                 if (ops->remove)
671                         ops->remove(ops, wdata);
672         }
673
674         if (wdata->input) {
675                 input_free_device(wdata->input);
676                 wdata->input = NULL;
677         }
678 }
679
680 static void wiimote_modules_unload(struct wiimote_data *wdata)
681 {
682         const __u8 *mods, *iter;
683         const struct wiimod_ops *ops;
684         unsigned long flags;
685
686         mods = wiimote_devtype_mods[wdata->state.devtype];
687
688         spin_lock_irqsave(&wdata->state.lock, flags);
689         wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
690         spin_unlock_irqrestore(&wdata->state.lock, flags);
691
692         /* find end of list */
693         for (iter = mods; *iter != WIIMOD_NULL; ++iter)
694                 /* empty */ ;
695
696         if (wdata->input) {
697                 input_get_device(wdata->input);
698                 input_unregister_device(wdata->input);
699         }
700
701         for ( ; iter-- != mods; ) {
702                 ops = wiimod_table[*iter];
703                 if (ops->remove)
704                         ops->remove(ops, wdata);
705         }
706
707         if (wdata->input) {
708                 input_put_device(wdata->input);
709                 wdata->input = NULL;
710         }
711 }
712
713 /* device extension handling */
714
715 static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
716 {
717         unsigned long flags;
718         const struct wiimod_ops *ops;
719         int ret;
720
721         ops = wiimod_ext_table[ext];
722
723         if (ops->probe) {
724                 ret = ops->probe(ops, wdata);
725                 if (ret)
726                         ext = WIIMOTE_EXT_UNKNOWN;
727         }
728
729         spin_lock_irqsave(&wdata->state.lock, flags);
730         wdata->state.exttype = ext;
731         spin_unlock_irqrestore(&wdata->state.lock, flags);
732 }
733
734 static void wiimote_ext_unload(struct wiimote_data *wdata)
735 {
736         unsigned long flags;
737         const struct wiimod_ops *ops;
738
739         ops = wiimod_ext_table[wdata->state.exttype];
740
741         spin_lock_irqsave(&wdata->state.lock, flags);
742         wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
743         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
744         spin_unlock_irqrestore(&wdata->state.lock, flags);
745
746         if (ops->remove)
747                 ops->remove(ops, wdata);
748 }
749
750 static void wiimote_mp_load(struct wiimote_data *wdata)
751 {
752         unsigned long flags;
753         const struct wiimod_ops *ops;
754         int ret;
755         __u8 mode = 2;
756
757         ops = &wiimod_mp;
758         if (ops->probe) {
759                 ret = ops->probe(ops, wdata);
760                 if (ret)
761                         mode = 1;
762         }
763
764         spin_lock_irqsave(&wdata->state.lock, flags);
765         wdata->state.mp = mode;
766         spin_unlock_irqrestore(&wdata->state.lock, flags);
767 }
768
769 static void wiimote_mp_unload(struct wiimote_data *wdata)
770 {
771         unsigned long flags;
772         const struct wiimod_ops *ops;
773
774         if (wdata->state.mp < 2)
775                 return;
776
777         ops = &wiimod_mp;
778
779         spin_lock_irqsave(&wdata->state.lock, flags);
780         wdata->state.mp = 0;
781         wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
782         spin_unlock_irqrestore(&wdata->state.lock, flags);
783
784         if (ops->remove)
785                 ops->remove(ops, wdata);
786 }
787
788 /* device (re-)initialization and detection */
789
790 static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
791         [WIIMOTE_DEV_PENDING] = "Pending",
792         [WIIMOTE_DEV_UNKNOWN] = "Unknown",
793         [WIIMOTE_DEV_GENERIC] = "Generic",
794         [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
795         [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
796         [WIIMOTE_DEV_BALANCE_BOARD] = "Nintendo Wii Balance Board",
797         [WIIMOTE_DEV_PRO_CONTROLLER] = "Nintendo Wii U Pro Controller",
798 };
799
800 /* Try to guess the device type based on all collected information. We
801  * first try to detect by static extension types, then VID/PID and the
802  * device name. If we cannot detect the device, we use
803  * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
804 static void wiimote_init_set_type(struct wiimote_data *wdata,
805                                   __u8 exttype)
806 {
807         __u8 devtype = WIIMOTE_DEV_GENERIC;
808         __u16 vendor, product;
809         const char *name;
810
811         vendor = wdata->hdev->vendor;
812         product = wdata->hdev->product;
813         name = wdata->hdev->name;
814
815         if (exttype == WIIMOTE_EXT_BALANCE_BOARD) {
816                 devtype = WIIMOTE_DEV_BALANCE_BOARD;
817                 goto done;
818         } else if (exttype == WIIMOTE_EXT_PRO_CONTROLLER) {
819                 devtype = WIIMOTE_DEV_PRO_CONTROLLER;
820                 goto done;
821         }
822
823         if (!strcmp(name, "Nintendo RVL-CNT-01")) {
824                 devtype = WIIMOTE_DEV_GEN10;
825                 goto done;
826         } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
827                 devtype = WIIMOTE_DEV_GEN20;
828                 goto done;
829         } else if (!strcmp(name, "Nintendo RVL-WBC-01")) {
830                 devtype = WIIMOTE_DEV_BALANCE_BOARD;
831                 goto done;
832         } else if (!strcmp(name, "Nintendo RVL-CNT-01-UC")) {
833                 devtype = WIIMOTE_DEV_PRO_CONTROLLER;
834                 goto done;
835         }
836
837         if (vendor == USB_VENDOR_ID_NINTENDO ||
838             vendor == USB_VENDOR_ID_NINTENDO2) {
839                 if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
840                         devtype = WIIMOTE_DEV_GEN10;
841                         goto done;
842                 } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
843                         devtype = WIIMOTE_DEV_GEN20;
844                         goto done;
845                 }
846         }
847
848 done:
849         if (devtype == WIIMOTE_DEV_GENERIC)
850                 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
851                         name, vendor, product, exttype);
852         else
853                 hid_info(wdata->hdev, "detected device: %s\n",
854                          wiimote_devtype_names[devtype]);
855
856         wiimote_modules_load(wdata, devtype);
857 }
858
859 static void wiimote_init_detect(struct wiimote_data *wdata)
860 {
861         __u8 exttype = WIIMOTE_EXT_NONE, extdata[6];
862         bool ext;
863         int ret;
864
865         wiimote_cmd_acquire_noint(wdata);
866
867         spin_lock_irq(&wdata->state.lock);
868         wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
869         wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
870         wiiproto_req_status(wdata);
871         spin_unlock_irq(&wdata->state.lock);
872
873         ret = wiimote_cmd_wait_noint(wdata);
874         if (ret)
875                 goto out_release;
876
877         spin_lock_irq(&wdata->state.lock);
878         ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
879         spin_unlock_irq(&wdata->state.lock);
880
881         if (!ext)
882                 goto out_release;
883
884         wiimote_cmd_init_ext(wdata);
885         exttype = wiimote_cmd_read_ext(wdata, extdata);
886
887 out_release:
888         wiimote_cmd_release(wdata);
889         wiimote_init_set_type(wdata, exttype);
890
891         /* schedule MP timer */
892         spin_lock_irq(&wdata->state.lock);
893         if (!(wdata->state.flags & WIIPROTO_FLAG_BUILTIN_MP) &&
894             !(wdata->state.flags & WIIPROTO_FLAG_NO_MP))
895                 mod_timer(&wdata->timer, jiffies + HZ * 4);
896         spin_unlock_irq(&wdata->state.lock);
897 }
898
899 /*
900  * MP hotplug events are not generated by the wiimote. Therefore, we need
901  * polling to detect it. We use a 4s interval for polling MP registers. This
902  * seems reasonable considering applications can trigger it manually via
903  * sysfs requests.
904  */
905 static void wiimote_init_poll_mp(struct wiimote_data *wdata)
906 {
907         bool mp;
908         __u8 mpdata[6];
909
910         wiimote_cmd_acquire_noint(wdata);
911         wiimote_cmd_init_mp(wdata);
912         mp = wiimote_cmd_read_mp(wdata, mpdata);
913         wiimote_cmd_release(wdata);
914
915         /* load/unload MP module if it changed */
916         if (mp) {
917                 if (!wdata->state.mp) {
918                         hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
919                         wiimote_mp_load(wdata);
920                 }
921         } else if (wdata->state.mp) {
922                 wiimote_mp_unload(wdata);
923         }
924
925         mod_timer(&wdata->timer, jiffies + HZ * 4);
926 }
927
928 /*
929  * Check whether the wiimote is in the expected state. The extension registers
930  * may change during hotplug and initialization so we might get hotplug events
931  * that we caused by remapping some memory.
932  * We use some heuristics here to check known states. If the wiimote is in the
933  * expected state, we can ignore the hotplug event.
934  *
935  * Returns "true" if the device is in expected state, "false" if we should
936  * redo hotplug handling and extension initialization.
937  */
938 static bool wiimote_init_check(struct wiimote_data *wdata)
939 {
940         __u32 flags;
941         __u8 type, data[6];
942         bool ret, poll_mp;
943
944         spin_lock_irq(&wdata->state.lock);
945         flags = wdata->state.flags;
946         spin_unlock_irq(&wdata->state.lock);
947
948         wiimote_cmd_acquire_noint(wdata);
949
950         /* If MP is used and active, but the extension is not, we expect:
951          *   read_mp_mapped() == WIIMOTE_MP_SINGLE
952          *   state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE
953          * We do not check EXT_PLUGGED because it might change during
954          * initialization of MP without extensions.
955          *  - If MP is unplugged/replugged, read_mp_mapped() fails
956          *  - If EXT is plugged, MP_PLUGGED will get set */
957         if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
958             wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
959                 type = wiimote_cmd_read_mp_mapped(wdata);
960                 ret = type == WIIMOTE_MP_SINGLE;
961
962                 spin_lock_irq(&wdata->state.lock);
963                 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
964                 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
965                 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
966                 spin_unlock_irq(&wdata->state.lock);
967
968                 if (!ret)
969                         hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
970
971                 /* while MP is mapped, we get EXT_PLUGGED events */
972                 poll_mp = false;
973
974                 goto out_release;
975         }
976
977         /* If MP is unused, but the extension port is used, we expect:
978          *   read_ext == state.exttype
979          *   state.flags == !MP_ACTIVE && EXT_ACTIVE
980          * - If MP is plugged/unplugged, our timer detects it
981          * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */
982         if (!(flags & WIIPROTO_FLAG_MP_USED) &&
983             wdata->state.exttype != WIIMOTE_EXT_NONE) {
984                 type = wiimote_cmd_read_ext(wdata, data);
985                 ret = type == wdata->state.exttype;
986
987                 spin_lock_irq(&wdata->state.lock);
988                 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
989                 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
990                 spin_unlock_irq(&wdata->state.lock);
991
992                 if (!ret)
993                         hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
994
995                 /* poll MP for hotplug events */
996                 poll_mp = true;
997
998                 goto out_release;
999         }
1000
1001         /* If neither MP nor an extension are used, we expect:
1002          *   read_ext() == WIIMOTE_EXT_NONE
1003          *   state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED
1004          * No need to perform any action in this case as everything is
1005          * disabled already.
1006          * - If MP is plugged/unplugged, our timer detects it
1007          * - If EXT is plugged, EXT_PLUGGED will be set */
1008         if (!(flags & WIIPROTO_FLAG_MP_USED) &&
1009             wdata->state.exttype == WIIMOTE_EXT_NONE) {
1010                 type = wiimote_cmd_read_ext(wdata, data);
1011                 ret = type == wdata->state.exttype;
1012
1013                 spin_lock_irq(&wdata->state.lock);
1014                 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1015                 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1016                 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1017                 spin_unlock_irq(&wdata->state.lock);
1018
1019                 if (!ret)
1020                         hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
1021
1022                 /* poll MP for hotplug events */
1023                 poll_mp = true;
1024
1025                 goto out_release;
1026         }
1027
1028         /* The trickiest part is if both EXT and MP are active. We cannot read
1029          * the EXT ID, anymore, because MP is mapped over it. However, we use
1030          * a handy trick here:
1031          *   - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent
1032          * MP_PLUGGED might be re-sent again before we are scheduled, but
1033          * EXT_ACTIVE will stay unset.
1034          * So it is enough to check for mp_mapped() and MP_ACTIVE and
1035          * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */
1036         if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
1037             wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
1038                 type = wiimote_cmd_read_mp_mapped(wdata);
1039                 ret = type != WIIMOTE_MP_NONE;
1040                 ret = ret && type != WIIMOTE_MP_UNKNOWN;
1041                 ret = ret && type != WIIMOTE_MP_SINGLE;
1042
1043                 spin_lock_irq(&wdata->state.lock);
1044                 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1045                 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1046                 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1047                 spin_unlock_irq(&wdata->state.lock);
1048
1049                 if (!ret)
1050                         hid_dbg(wdata->hdev, "state left: EXT && MP\n");
1051
1052                 /* while MP is mapped, we get EXT_PLUGGED events */
1053                 poll_mp = false;
1054
1055                 goto out_release;
1056         }
1057
1058         /* unknown state */
1059         ret = false;
1060
1061 out_release:
1062         wiimote_cmd_release(wdata);
1063
1064         /* only poll for MP if requested and if state didn't change */
1065         if (ret && poll_mp && !(flags & WIIPROTO_FLAG_BUILTIN_MP) &&
1066             !(flags & WIIPROTO_FLAG_NO_MP))
1067                 wiimote_init_poll_mp(wdata);
1068
1069         return ret;
1070 }
1071
1072 static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = {
1073         [WIIMOTE_EXT_NONE] = "None",
1074         [WIIMOTE_EXT_UNKNOWN] = "Unknown",
1075         [WIIMOTE_EXT_NUNCHUK] = "Nintendo Wii Nunchuk",
1076         [WIIMOTE_EXT_CLASSIC_CONTROLLER] = "Nintendo Wii Classic Controller",
1077         [WIIMOTE_EXT_BALANCE_BOARD] = "Nintendo Wii Balance Board",
1078         [WIIMOTE_EXT_PRO_CONTROLLER] = "Nintendo Wii U Pro Controller",
1079 };
1080
1081 /*
1082  * Handle hotplug events
1083  * If we receive an hotplug event and the device-check failed, we deinitialize
1084  * the extension ports, re-read all extension IDs and set the device into
1085  * the desired state. This involves mapping MP into the main extension
1086  * registers, setting up extension passthrough modes and initializing the
1087  * requested extensions.
1088  */
1089 static void wiimote_init_hotplug(struct wiimote_data *wdata)
1090 {
1091         __u8 exttype, extdata[6], mpdata[6];
1092         __u32 flags;
1093         bool mp;
1094
1095         hid_dbg(wdata->hdev, "detect extensions..\n");
1096
1097         wiimote_cmd_acquire_noint(wdata);
1098
1099         spin_lock_irq(&wdata->state.lock);
1100
1101         /* get state snapshot that we will then work on */
1102         flags = wdata->state.flags;
1103
1104         /* disable event forwarding temporarily */
1105         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1106         wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1107
1108         spin_unlock_irq(&wdata->state.lock);
1109
1110         /* init extension and MP (deactivates current extension or MP) */
1111         wiimote_cmd_init_ext(wdata);
1112         if (flags & WIIPROTO_FLAG_NO_MP) {
1113                 mp = false;
1114         } else {
1115                 wiimote_cmd_init_mp(wdata);
1116                 mp = wiimote_cmd_read_mp(wdata, mpdata);
1117         }
1118         exttype = wiimote_cmd_read_ext(wdata, extdata);
1119
1120         wiimote_cmd_release(wdata);
1121
1122         /* load/unload extension module if it changed */
1123         if (exttype != wdata->state.exttype) {
1124                 /* unload previous extension */
1125                 wiimote_ext_unload(wdata);
1126
1127                 if (exttype == WIIMOTE_EXT_UNKNOWN) {
1128                         hid_info(wdata->hdev, "cannot detect extension; %6phC\n",
1129                                  extdata);
1130                 } else if (exttype == WIIMOTE_EXT_NONE) {
1131                         spin_lock_irq(&wdata->state.lock);
1132                         wdata->state.exttype = WIIMOTE_EXT_NONE;
1133                         spin_unlock_irq(&wdata->state.lock);
1134                 } else {
1135                         hid_info(wdata->hdev, "detected extension: %s\n",
1136                                  wiimote_exttype_names[exttype]);
1137                         /* try loading new extension */
1138                         wiimote_ext_load(wdata, exttype);
1139                 }
1140         }
1141
1142         /* load/unload MP module if it changed */
1143         if (mp) {
1144                 if (!wdata->state.mp) {
1145                         hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
1146                         wiimote_mp_load(wdata);
1147                 }
1148         } else if (wdata->state.mp) {
1149                 wiimote_mp_unload(wdata);
1150         }
1151
1152         /* if MP is not used, do not map or activate it */
1153         if (!(flags & WIIPROTO_FLAG_MP_USED))
1154                 mp = false;
1155
1156         /* map MP into main extension registers if used */
1157         if (mp) {
1158                 wiimote_cmd_acquire_noint(wdata);
1159                 wiimote_cmd_map_mp(wdata, exttype);
1160                 wiimote_cmd_release(wdata);
1161
1162                 /* delete MP hotplug timer */
1163                 del_timer_sync(&wdata->timer);
1164         } else {
1165                 /* reschedule MP hotplug timer */
1166                 if (!(flags & WIIPROTO_FLAG_BUILTIN_MP) &&
1167                     !(flags & WIIPROTO_FLAG_NO_MP))
1168                         mod_timer(&wdata->timer, jiffies + HZ * 4);
1169         }
1170
1171         spin_lock_irq(&wdata->state.lock);
1172
1173         /* enable data forwarding again and set expected hotplug state */
1174         if (mp) {
1175                 wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
1176                 if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
1177                         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1178                         wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1179                 } else {
1180                         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1181                         wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1182                         wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1183                 }
1184         } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
1185                 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1186         }
1187
1188         /* request status report for hotplug state updates */
1189         wiiproto_req_status(wdata);
1190
1191         spin_unlock_irq(&wdata->state.lock);
1192
1193         hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
1194                 wdata->state.mp, wdata->state.exttype);
1195 }
1196
1197 static void wiimote_init_worker(struct work_struct *work)
1198 {
1199         struct wiimote_data *wdata = container_of(work, struct wiimote_data,
1200                                                   init_worker);
1201         bool changed = false;
1202
1203         if (wdata->state.devtype == WIIMOTE_DEV_PENDING) {
1204                 wiimote_init_detect(wdata);
1205                 changed = true;
1206         }
1207
1208         if (changed || !wiimote_init_check(wdata))
1209                 wiimote_init_hotplug(wdata);
1210
1211         if (changed)
1212                 kobject_uevent(&wdata->hdev->dev.kobj, KOBJ_CHANGE);
1213 }
1214
1215 void __wiimote_schedule(struct wiimote_data *wdata)
1216 {
1217         if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
1218                 schedule_work(&wdata->init_worker);
1219 }
1220
1221 static void wiimote_schedule(struct wiimote_data *wdata)
1222 {
1223         unsigned long flags;
1224
1225         spin_lock_irqsave(&wdata->state.lock, flags);
1226         __wiimote_schedule(wdata);
1227         spin_unlock_irqrestore(&wdata->state.lock, flags);
1228 }
1229
1230 static void wiimote_init_timeout(unsigned long arg)
1231 {
1232         struct wiimote_data *wdata = (void*)arg;
1233
1234         wiimote_schedule(wdata);
1235 }
1236
1237 /* protocol handlers */
1238
1239 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
1240 {
1241         const __u8 *iter, *mods;
1242         const struct wiimod_ops *ops;
1243
1244         ops = wiimod_ext_table[wdata->state.exttype];
1245         if (ops->in_keys) {
1246                 ops->in_keys(wdata, payload);
1247                 return;
1248         }
1249
1250         mods = wiimote_devtype_mods[wdata->state.devtype];
1251         for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1252                 ops = wiimod_table[*iter];
1253                 if (ops->in_keys) {
1254                         ops->in_keys(wdata, payload);
1255                         break;
1256                 }
1257         }
1258 }
1259
1260 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
1261 {
1262         const __u8 *iter, *mods;
1263         const struct wiimod_ops *ops;
1264
1265         ops = wiimod_ext_table[wdata->state.exttype];
1266         if (ops->in_accel) {
1267                 ops->in_accel(wdata, payload);
1268                 return;
1269         }
1270
1271         mods = wiimote_devtype_mods[wdata->state.devtype];
1272         for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1273                 ops = wiimod_table[*iter];
1274                 if (ops->in_accel) {
1275                         ops->in_accel(wdata, payload);
1276                         break;
1277                 }
1278         }
1279 }
1280
1281 static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len)
1282 {
1283         if (!ops->in_ext)
1284                 return false;
1285         if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8)
1286                 return false;
1287         if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16)
1288                 return false;
1289
1290         return true;
1291 }
1292
1293 static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
1294                         size_t len)
1295 {
1296         static const __u8 invalid[21] = { 0xff, 0xff, 0xff, 0xff,
1297                                           0xff, 0xff, 0xff, 0xff,
1298                                           0xff, 0xff, 0xff, 0xff,
1299                                           0xff, 0xff, 0xff, 0xff,
1300                                           0xff, 0xff, 0xff, 0xff,
1301                                           0xff };
1302         const __u8 *iter, *mods;
1303         const struct wiimod_ops *ops;
1304         bool is_mp;
1305
1306         if (len > 21)
1307                 len = 21;
1308         if (len < 6 || !memcmp(payload, invalid, len))
1309                 return;
1310
1311         /* if MP is active, track MP slot hotplugging */
1312         if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1313                 /* this bit is set for invalid events (eg. during hotplug) */
1314                 if (payload[5] & 0x01)
1315                         return;
1316
1317                 if (payload[4] & 0x01) {
1318                         if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
1319                                 hid_dbg(wdata->hdev, "MP hotplug: 1\n");
1320                                 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1321                                 __wiimote_schedule(wdata);
1322                         }
1323                 } else {
1324                         if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
1325                                 hid_dbg(wdata->hdev, "MP hotplug: 0\n");
1326                                 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1327                                 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1328                                 __wiimote_schedule(wdata);
1329                         }
1330                 }
1331
1332                 /* detect MP data that is sent interleaved with EXT data */
1333                 is_mp = payload[5] & 0x02;
1334         } else {
1335                 is_mp = false;
1336         }
1337
1338         /* ignore EXT events if no extension is active */
1339         if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
1340                 return;
1341
1342         /* try forwarding to extension handler, first */
1343         ops = wiimod_ext_table[wdata->state.exttype];
1344         if (is_mp && ops->in_mp) {
1345                 ops->in_mp(wdata, payload);
1346                 return;
1347         } else if (!is_mp && valid_ext_handler(ops, len)) {
1348                 ops->in_ext(wdata, payload);
1349                 return;
1350         }
1351
1352         /* try forwarding to MP handler */
1353         ops = &wiimod_mp;
1354         if (is_mp && ops->in_mp) {
1355                 ops->in_mp(wdata, payload);
1356                 return;
1357         } else if (!is_mp && valid_ext_handler(ops, len)) {
1358                 ops->in_ext(wdata, payload);
1359                 return;
1360         }
1361
1362         /* try forwarding to loaded modules */
1363         mods = wiimote_devtype_mods[wdata->state.devtype];
1364         for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1365                 ops = wiimod_table[*iter];
1366                 if (is_mp && ops->in_mp) {
1367                         ops->in_mp(wdata, payload);
1368                         return;
1369                 } else if (!is_mp && valid_ext_handler(ops, len)) {
1370                         ops->in_ext(wdata, payload);
1371                         return;
1372                 }
1373         }
1374 }
1375
1376 #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1377 #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1378 #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1379 #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
1380
1381 static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
1382                        bool packed, unsigned int id)
1383 {
1384         const __u8 *iter, *mods;
1385         const struct wiimod_ops *ops;
1386
1387         ops = wiimod_ext_table[wdata->state.exttype];
1388         if (ops->in_ir) {
1389                 ops->in_ir(wdata, payload, packed, id);
1390                 return;
1391         }
1392
1393         mods = wiimote_devtype_mods[wdata->state.devtype];
1394         for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1395                 ops = wiimod_table[*iter];
1396                 if (ops->in_ir) {
1397                         ops->in_ir(wdata, payload, packed, id);
1398                         break;
1399                 }
1400         }
1401 }
1402
1403 /* reduced status report with "BB BB" key data only */
1404 static void handler_status_K(struct wiimote_data *wdata,
1405                              const __u8 *payload)
1406 {
1407         handler_keys(wdata, payload);
1408
1409         /* on status reports the drm is reset so we need to resend the drm */
1410         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1411 }
1412
1413 /* extended status report with "BB BB LF 00 00 VV" data */
1414 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1415 {
1416         handler_status_K(wdata, payload);
1417
1418         /* update extension status */
1419         if (payload[2] & 0x02) {
1420                 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
1421                         hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
1422                         wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1423                         __wiimote_schedule(wdata);
1424                 }
1425         } else {
1426                 if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
1427                         hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
1428                         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1429                         wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1430                         wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1431                         wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1432                         __wiimote_schedule(wdata);
1433                 }
1434         }
1435
1436         wdata->state.cmd_battery = payload[5];
1437         if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
1438                 wiimote_cmd_complete(wdata);
1439 }
1440
1441 /* reduced generic report with "BB BB" key data only */
1442 static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1443 {
1444         handler_keys(wdata, payload);
1445 }
1446
1447 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1448 {
1449         __u16 offset = payload[3] << 8 | payload[4];
1450         __u8 size = (payload[2] >> 4) + 1;
1451         __u8 err = payload[2] & 0x0f;
1452
1453         handler_keys(wdata, payload);
1454
1455         if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1456                 if (err)
1457                         size = 0;
1458                 else if (size > wdata->state.cmd_read_size)
1459                         size = wdata->state.cmd_read_size;
1460
1461                 wdata->state.cmd_read_size = size;
1462                 if (wdata->state.cmd_read_buf)
1463                         memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1464                 wiimote_cmd_complete(wdata);
1465         }
1466 }
1467
1468 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1469 {
1470         __u8 err = payload[3];
1471         __u8 cmd = payload[2];
1472
1473         handler_keys(wdata, payload);
1474
1475         if (wiimote_cmd_pending(wdata, cmd, 0)) {
1476                 wdata->state.cmd_err = err;
1477                 wiimote_cmd_complete(wdata);
1478         } else if (err) {
1479                 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
1480                                                                         cmd);
1481         }
1482 }
1483
1484 static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1485 {
1486         handler_keys(wdata, payload);
1487         handler_accel(wdata, payload);
1488 }
1489
1490 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1491 {
1492         handler_keys(wdata, payload);
1493         handler_ext(wdata, &payload[2], 8);
1494 }
1495
1496 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1497 {
1498         handler_keys(wdata, payload);
1499         handler_accel(wdata, payload);
1500         ir_to_input0(wdata, &payload[5], false);
1501         ir_to_input1(wdata, &payload[8], false);
1502         ir_to_input2(wdata, &payload[11], false);
1503         ir_to_input3(wdata, &payload[14], false);
1504 }
1505
1506 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1507 {
1508         handler_keys(wdata, payload);
1509         handler_ext(wdata, &payload[2], 19);
1510 }
1511
1512 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1513 {
1514         handler_keys(wdata, payload);
1515         ir_to_input0(wdata, &payload[2], false);
1516         ir_to_input1(wdata, &payload[4], true);
1517         ir_to_input2(wdata, &payload[7], false);
1518         ir_to_input3(wdata, &payload[9], true);
1519         handler_ext(wdata, &payload[12], 9);
1520 }
1521
1522 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1523 {
1524         handler_keys(wdata, payload);
1525         handler_accel(wdata, payload);
1526         handler_ext(wdata, &payload[5], 16);
1527 }
1528
1529 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1530 {
1531         handler_keys(wdata, payload);
1532         handler_accel(wdata, payload);
1533         ir_to_input0(wdata, &payload[5], false);
1534         ir_to_input1(wdata, &payload[7], true);
1535         ir_to_input2(wdata, &payload[10], false);
1536         ir_to_input3(wdata, &payload[12], true);
1537         handler_ext(wdata, &payload[15], 6);
1538 }
1539
1540 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1541 {
1542         handler_ext(wdata, payload, 21);
1543 }
1544
1545 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1546 {
1547         handler_keys(wdata, payload);
1548
1549         wdata->state.accel_split[0] = payload[2];
1550         wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1551         wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
1552
1553         ir_to_input0(wdata, &payload[3], false);
1554         ir_to_input1(wdata, &payload[12], false);
1555 }
1556
1557 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1558 {
1559         __u8 buf[5];
1560
1561         handler_keys(wdata, payload);
1562
1563         wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1564         wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1565
1566         buf[0] = 0;
1567         buf[1] = 0;
1568         buf[2] = wdata->state.accel_split[0];
1569         buf[3] = payload[2];
1570         buf[4] = wdata->state.accel_split[1];
1571         handler_accel(wdata, buf);
1572
1573         ir_to_input2(wdata, &payload[3], false);
1574         ir_to_input3(wdata, &payload[12], false);
1575 }
1576
1577 struct wiiproto_handler {
1578         __u8 id;
1579         size_t size;
1580         void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1581 };
1582
1583 static struct wiiproto_handler handlers[] = {
1584         { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
1585         { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
1586         { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
1587         { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
1588         { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
1589         { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
1590         { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
1591         { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
1592         { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
1593         { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
1594         { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
1595         { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
1596         { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
1597         { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
1598         { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
1599         { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
1600         { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
1601         { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
1602         { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
1603         { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
1604         { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
1605         { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
1606         { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1607         { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
1608         { .id = 0 }
1609 };
1610
1611 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1612                                                         u8 *raw_data, int size)
1613 {
1614         struct wiimote_data *wdata = hid_get_drvdata(hdev);
1615         struct wiiproto_handler *h;
1616         int i;
1617         unsigned long flags;
1618
1619         if (size < 1)
1620                 return -EINVAL;
1621
1622         spin_lock_irqsave(&wdata->state.lock, flags);
1623
1624         for (i = 0; handlers[i].id; ++i) {
1625                 h = &handlers[i];
1626                 if (h->id == raw_data[0] && h->size < size) {
1627                         h->func(wdata, &raw_data[1]);
1628                         break;
1629                 }
1630         }
1631
1632         if (!handlers[i].id)
1633                 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1634                                                                         size);
1635
1636         spin_unlock_irqrestore(&wdata->state.lock, flags);
1637
1638         return 0;
1639 }
1640
1641 static ssize_t wiimote_ext_show(struct device *dev,
1642                                 struct device_attribute *attr,
1643                                 char *buf)
1644 {
1645         struct wiimote_data *wdata = dev_to_wii(dev);
1646         __u8 type;
1647         unsigned long flags;
1648
1649         spin_lock_irqsave(&wdata->state.lock, flags);
1650         type = wdata->state.exttype;
1651         spin_unlock_irqrestore(&wdata->state.lock, flags);
1652
1653         switch (type) {
1654         case WIIMOTE_EXT_NONE:
1655                 return sprintf(buf, "none\n");
1656         case WIIMOTE_EXT_NUNCHUK:
1657                 return sprintf(buf, "nunchuk\n");
1658         case WIIMOTE_EXT_CLASSIC_CONTROLLER:
1659                 return sprintf(buf, "classic\n");
1660         case WIIMOTE_EXT_BALANCE_BOARD:
1661                 return sprintf(buf, "balanceboard\n");
1662         case WIIMOTE_EXT_PRO_CONTROLLER:
1663                 return sprintf(buf, "procontroller\n");
1664         case WIIMOTE_EXT_UNKNOWN:
1665                 /* fallthrough */
1666         default:
1667                 return sprintf(buf, "unknown\n");
1668         }
1669 }
1670
1671 static ssize_t wiimote_ext_store(struct device *dev,
1672                                  struct device_attribute *attr,
1673                                  const char *buf, size_t count)
1674 {
1675         struct wiimote_data *wdata = dev_to_wii(dev);
1676
1677         if (!strcmp(buf, "scan")) {
1678                 wiimote_schedule(wdata);
1679         } else {
1680                 return -EINVAL;
1681         }
1682
1683         return strnlen(buf, PAGE_SIZE);
1684 }
1685
1686 static DEVICE_ATTR(extension, S_IRUGO | S_IWUSR | S_IWGRP, wiimote_ext_show,
1687                    wiimote_ext_store);
1688
1689 static ssize_t wiimote_dev_show(struct device *dev,
1690                                 struct device_attribute *attr,
1691                                 char *buf)
1692 {
1693         struct wiimote_data *wdata = dev_to_wii(dev);
1694         __u8 type;
1695         unsigned long flags;
1696
1697         spin_lock_irqsave(&wdata->state.lock, flags);
1698         type = wdata->state.devtype;
1699         spin_unlock_irqrestore(&wdata->state.lock, flags);
1700
1701         switch (type) {
1702         case WIIMOTE_DEV_GENERIC:
1703                 return sprintf(buf, "generic\n");
1704         case WIIMOTE_DEV_GEN10:
1705                 return sprintf(buf, "gen10\n");
1706         case WIIMOTE_DEV_GEN20:
1707                 return sprintf(buf, "gen20\n");
1708         case WIIMOTE_DEV_BALANCE_BOARD:
1709                 return sprintf(buf, "balanceboard\n");
1710         case WIIMOTE_DEV_PRO_CONTROLLER:
1711                 return sprintf(buf, "procontroller\n");
1712         case WIIMOTE_DEV_PENDING:
1713                 return sprintf(buf, "pending\n");
1714         case WIIMOTE_DEV_UNKNOWN:
1715                 /* fallthrough */
1716         default:
1717                 return sprintf(buf, "unknown\n");
1718         }
1719 }
1720
1721 static DEVICE_ATTR(devtype, S_IRUGO, wiimote_dev_show, NULL);
1722
1723 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1724 {
1725         struct wiimote_data *wdata;
1726
1727         wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1728         if (!wdata)
1729                 return NULL;
1730
1731         wdata->hdev = hdev;
1732         hid_set_drvdata(hdev, wdata);
1733
1734         spin_lock_init(&wdata->queue.lock);
1735         INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
1736
1737         spin_lock_init(&wdata->state.lock);
1738         init_completion(&wdata->state.ready);
1739         mutex_init(&wdata->state.sync);
1740         wdata->state.drm = WIIPROTO_REQ_DRM_K;
1741         wdata->state.cmd_battery = 0xff;
1742
1743         INIT_WORK(&wdata->init_worker, wiimote_init_worker);
1744         setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata);
1745
1746         return wdata;
1747 }
1748
1749 static void wiimote_destroy(struct wiimote_data *wdata)
1750 {
1751         unsigned long flags;
1752
1753         wiidebug_deinit(wdata);
1754
1755         /* prevent init_worker from being scheduled again */
1756         spin_lock_irqsave(&wdata->state.lock, flags);
1757         wdata->state.flags |= WIIPROTO_FLAG_EXITING;
1758         spin_unlock_irqrestore(&wdata->state.lock, flags);
1759
1760         cancel_work_sync(&wdata->init_worker);
1761         del_timer_sync(&wdata->timer);
1762
1763         device_remove_file(&wdata->hdev->dev, &dev_attr_devtype);
1764         device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1765
1766         wiimote_mp_unload(wdata);
1767         wiimote_ext_unload(wdata);
1768         wiimote_modules_unload(wdata);
1769         cancel_work_sync(&wdata->queue.worker);
1770         hid_hw_close(wdata->hdev);
1771         hid_hw_stop(wdata->hdev);
1772
1773         kfree(wdata);
1774 }
1775
1776 static int wiimote_hid_probe(struct hid_device *hdev,
1777                                 const struct hid_device_id *id)
1778 {
1779         struct wiimote_data *wdata;
1780         int ret;
1781
1782         hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1783
1784         wdata = wiimote_create(hdev);
1785         if (!wdata) {
1786                 hid_err(hdev, "Can't alloc device\n");
1787                 return -ENOMEM;
1788         }
1789
1790         ret = hid_parse(hdev);
1791         if (ret) {
1792                 hid_err(hdev, "HID parse failed\n");
1793                 goto err;
1794         }
1795
1796         ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1797         if (ret) {
1798                 hid_err(hdev, "HW start failed\n");
1799                 goto err;
1800         }
1801
1802         ret = hid_hw_open(hdev);
1803         if (ret) {
1804                 hid_err(hdev, "cannot start hardware I/O\n");
1805                 goto err_stop;
1806         }
1807
1808         ret = device_create_file(&hdev->dev, &dev_attr_extension);
1809         if (ret) {
1810                 hid_err(hdev, "cannot create sysfs attribute\n");
1811                 goto err_close;
1812         }
1813
1814         ret = device_create_file(&hdev->dev, &dev_attr_devtype);
1815         if (ret) {
1816                 hid_err(hdev, "cannot create sysfs attribute\n");
1817                 goto err_ext;
1818         }
1819
1820         ret = wiidebug_init(wdata);
1821         if (ret)
1822                 goto err_free;
1823
1824         hid_info(hdev, "New device registered\n");
1825
1826         /* schedule device detection */
1827         wiimote_schedule(wdata);
1828
1829         return 0;
1830
1831 err_free:
1832         wiimote_destroy(wdata);
1833         return ret;
1834
1835 err_ext:
1836         device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1837 err_close:
1838         hid_hw_close(hdev);
1839 err_stop:
1840         hid_hw_stop(hdev);
1841 err:
1842         input_free_device(wdata->ir);
1843         input_free_device(wdata->accel);
1844         kfree(wdata);
1845         return ret;
1846 }
1847
1848 static void wiimote_hid_remove(struct hid_device *hdev)
1849 {
1850         struct wiimote_data *wdata = hid_get_drvdata(hdev);
1851
1852         hid_info(hdev, "Device removed\n");
1853         wiimote_destroy(wdata);
1854 }
1855
1856 static const struct hid_device_id wiimote_hid_devices[] = {
1857         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1858                                 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1859         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO2,
1860                                 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1861         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1862                                 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
1863         { }
1864 };
1865 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1866
1867 static struct hid_driver wiimote_hid_driver = {
1868         .name = "wiimote",
1869         .id_table = wiimote_hid_devices,
1870         .probe = wiimote_hid_probe,
1871         .remove = wiimote_hid_remove,
1872         .raw_event = wiimote_hid_event,
1873 };
1874 module_hid_driver(wiimote_hid_driver);
1875
1876 MODULE_LICENSE("GPL");
1877 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1878 MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");