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