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