2 * HID driver for Nintendo Wii / Wii U peripherals
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
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)
13 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/spinlock.h>
21 #include "hid-wiimote.h"
23 /* output queue handling */
25 static int wiimote_hid_send(struct hid_device
*hdev
, __u8
*buffer
,
31 if (!hdev
->hid_output_raw_report
)
34 buf
= kmemdup(buffer
, count
, GFP_KERNEL
);
38 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
44 static void wiimote_queue_worker(struct work_struct
*work
)
46 struct wiimote_queue
*queue
= container_of(work
, struct wiimote_queue
,
48 struct wiimote_data
*wdata
= container_of(queue
, struct wiimote_data
,
53 spin_lock_irqsave(&wdata
->queue
.lock
, flags
);
55 while (wdata
->queue
.head
!= wdata
->queue
.tail
) {
56 spin_unlock_irqrestore(&wdata
->queue
.lock
, flags
);
57 ret
= wiimote_hid_send(wdata
->hdev
,
58 wdata
->queue
.outq
[wdata
->queue
.tail
].data
,
59 wdata
->queue
.outq
[wdata
->queue
.tail
].size
);
61 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
62 wiimote_cmd_abort(wdata
);
63 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
65 spin_lock_irqsave(&wdata
->queue
.lock
, flags
);
67 wdata
->queue
.tail
= (wdata
->queue
.tail
+ 1) % WIIMOTE_BUFSIZE
;
70 spin_unlock_irqrestore(&wdata
->queue
.lock
, flags
);
73 static void wiimote_queue(struct wiimote_data
*wdata
, const __u8
*buffer
,
79 if (count
> HID_MAX_BUFFER_SIZE
) {
80 hid_warn(wdata
->hdev
, "Sending too large output report\n");
82 spin_lock_irqsave(&wdata
->queue
.lock
, flags
);
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.
96 spin_lock_irqsave(&wdata
->queue
.lock
, flags
);
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
;
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
;
108 hid_warn(wdata
->hdev
, "Output queue is full");
115 wiimote_cmd_abort(wdata
);
117 spin_unlock_irqrestore(&wdata
->queue
.lock
, flags
);
121 * This sets the rumble bit on the given output report if rumble is
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!
127 static inline void wiiproto_keep_rumble(struct wiimote_data
*wdata
, __u8
*cmd1
)
129 if (wdata
->state
.flags
& WIIPROTO_FLAG_RUMBLE
)
133 void wiiproto_req_rumble(struct wiimote_data
*wdata
, __u8 rumble
)
138 if (rumble
== !!(wdata
->state
.flags
& WIIPROTO_FLAG_RUMBLE
))
142 wdata
->state
.flags
|= WIIPROTO_FLAG_RUMBLE
;
144 wdata
->state
.flags
&= ~WIIPROTO_FLAG_RUMBLE
;
146 cmd
[0] = WIIPROTO_REQ_RUMBLE
;
149 wiiproto_keep_rumble(wdata
, &cmd
[1]);
150 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
153 void wiiproto_req_leds(struct wiimote_data
*wdata
, int leds
)
157 leds
&= WIIPROTO_FLAGS_LEDS
;
158 if ((wdata
->state
.flags
& WIIPROTO_FLAGS_LEDS
) == leds
)
160 wdata
->state
.flags
= (wdata
->state
.flags
& ~WIIPROTO_FLAGS_LEDS
) | leds
;
162 cmd
[0] = WIIPROTO_REQ_LED
;
165 if (leds
& WIIPROTO_FLAG_LED1
)
167 if (leds
& WIIPROTO_FLAG_LED2
)
169 if (leds
& WIIPROTO_FLAG_LED3
)
171 if (leds
& WIIPROTO_FLAG_LED4
)
174 wiiproto_keep_rumble(wdata
, &cmd
[1]);
175 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
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.
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
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.
197 static __u8
select_drm(struct wiimote_data
*wdata
)
199 __u8 ir
= wdata
->state
.flags
& WIIPROTO_FLAGS_IR
;
202 ext
= (wdata
->state
.flags
& WIIPROTO_FLAG_EXT_USED
) ||
203 (wdata
->state
.flags
& WIIPROTO_FLAG_MP_USED
);
205 /* some 3rd-party balance-boards are hard-coded to KEE, *sigh* */
206 if (wdata
->state
.devtype
== WIIMOTE_DEV_BALANCE_BOARD
) {
208 return WIIPROTO_REQ_DRM_KEE
;
210 return WIIPROTO_REQ_DRM_K
;
213 if (ir
== WIIPROTO_FLAG_IR_BASIC
) {
214 if (wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
) {
216 return WIIPROTO_REQ_DRM_KAIE
;
218 return WIIPROTO_REQ_DRM_KAI
;
220 return WIIPROTO_REQ_DRM_KIE
;
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
;
227 if (wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
) {
229 return WIIPROTO_REQ_DRM_KAE
;
231 return WIIPROTO_REQ_DRM_KA
;
234 return WIIPROTO_REQ_DRM_KEE
;
236 return WIIPROTO_REQ_DRM_K
;
241 void wiiproto_req_drm(struct wiimote_data
*wdata
, __u8 drm
)
245 if (wdata
->state
.flags
& WIIPROTO_FLAG_DRM_LOCKED
)
246 drm
= wdata
->state
.drm
;
247 else if (drm
== WIIPROTO_REQ_NULL
)
248 drm
= select_drm(wdata
);
250 cmd
[0] = WIIPROTO_REQ_DRM
;
254 wdata
->state
.drm
= drm
;
255 wiiproto_keep_rumble(wdata
, &cmd
[1]);
256 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
259 void wiiproto_req_status(struct wiimote_data
*wdata
)
263 cmd
[0] = WIIPROTO_REQ_SREQ
;
266 wiiproto_keep_rumble(wdata
, &cmd
[1]);
267 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
270 void wiiproto_req_accel(struct wiimote_data
*wdata
, __u8 accel
)
273 if (accel
== !!(wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
))
277 wdata
->state
.flags
|= WIIPROTO_FLAG_ACCEL
;
279 wdata
->state
.flags
&= ~WIIPROTO_FLAG_ACCEL
;
281 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
284 void wiiproto_req_ir1(struct wiimote_data
*wdata
, __u8 flags
)
288 cmd
[0] = WIIPROTO_REQ_IR1
;
291 wiiproto_keep_rumble(wdata
, &cmd
[1]);
292 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
295 void wiiproto_req_ir2(struct wiimote_data
*wdata
, __u8 flags
)
299 cmd
[0] = WIIPROTO_REQ_IR2
;
302 wiiproto_keep_rumble(wdata
, &cmd
[1]);
303 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
306 #define wiiproto_req_wreg(wdata, os, buf, sz) \
307 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
309 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
310 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
312 static void wiiproto_req_wmem(struct wiimote_data
*wdata
, bool eeprom
,
313 __u32 offset
, const __u8
*buf
, __u8 size
)
317 if (size
> 16 || size
== 0) {
318 hid_warn(wdata
->hdev
, "Invalid length %d wmem request\n", size
);
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;
328 memcpy(&cmd
[6], buf
, size
);
333 wiiproto_keep_rumble(wdata
, &cmd
[1]);
334 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
337 void wiiproto_req_rmem(struct wiimote_data
*wdata
, bool eeprom
, __u32 offset
,
343 hid_warn(wdata
->hdev
, "Invalid length %d rmem request\n", size
);
347 cmd
[0] = WIIPROTO_REQ_RMEM
;
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;
358 wiiproto_keep_rumble(wdata
, &cmd
[1]);
359 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
362 /* requries the cmd-mutex to be held */
363 int wiimote_cmd_write(struct wiimote_data
*wdata
, __u32 offset
,
364 const __u8
*wmem
, __u8 size
)
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
);
374 ret
= wiimote_cmd_wait(wdata
);
375 if (!ret
&& wdata
->state
.cmd_err
)
381 /* requries the cmd-mutex to be held */
382 ssize_t
wiimote_cmd_read(struct wiimote_data
*wdata
, __u32 offset
, __u8
*rmem
,
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
);
395 ret
= wiimote_cmd_wait(wdata
);
397 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
398 wdata
->state
.cmd_read_buf
= NULL
;
399 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
402 if (wdata
->state
.cmd_read_size
== 0)
405 ret
= wdata
->state
.cmd_read_size
;
411 /* requires the cmd-mutex to be held */
412 static int wiimote_cmd_init_ext(struct wiimote_data
*wdata
)
417 /* initialize extension */
419 ret
= wiimote_cmd_write(wdata
, 0xa400f0, &wmem
, sizeof(wmem
));
423 /* disable default encryption */
425 ret
= wiimote_cmd_write(wdata
, 0xa400fb, &wmem
, sizeof(wmem
));
432 /* requires the cmd-mutex to be held */
433 static __u8
wiimote_cmd_read_ext(struct wiimote_data
*wdata
, __u8
*rmem
)
437 /* read extension ID */
438 ret
= wiimote_cmd_read(wdata
, 0xa400fa, rmem
, 6);
440 return WIIMOTE_EXT_NONE
;
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]);
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
;
449 if (rmem
[4] == 0x00 && rmem
[5] == 0x00)
450 return WIIMOTE_EXT_NUNCHUK
;
451 if (rmem
[4] == 0x01 && rmem
[5] == 0x01)
452 return WIIMOTE_EXT_CLASSIC_CONTROLLER
;
453 if (rmem
[4] == 0x04 && rmem
[5] == 0x02)
454 return WIIMOTE_EXT_BALANCE_BOARD
;
456 return WIIMOTE_EXT_UNKNOWN
;
459 /* requires the cmd-mutex to be held */
460 static int wiimote_cmd_init_mp(struct wiimote_data
*wdata
)
467 ret
= wiimote_cmd_write(wdata
, 0xa600f0, &wmem
, sizeof(wmem
));
471 /* disable default encryption */
473 ret
= wiimote_cmd_write(wdata
, 0xa600fb, &wmem
, sizeof(wmem
));
480 /* requires the cmd-mutex to be held */
481 static bool wiimote_cmd_map_mp(struct wiimote_data
*wdata
, __u8 exttype
)
485 /* map MP with correct pass-through mode */
487 case WIIMOTE_EXT_CLASSIC_CONTROLLER
:
490 case WIIMOTE_EXT_NUNCHUK
:
498 return wiimote_cmd_write(wdata
, 0xa600fe, &wmem
, sizeof(wmem
));
501 /* requires the cmd-mutex to be held */
502 static bool wiimote_cmd_read_mp(struct wiimote_data
*wdata
, __u8
*rmem
)
506 /* read motion plus ID */
507 ret
= wiimote_cmd_read(wdata
, 0xa600fa, rmem
, 6);
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]);
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]);
523 /* requires the cmd-mutex to be held */
524 static __u8
wiimote_cmd_read_mp_mapped(struct wiimote_data
*wdata
)
529 /* read motion plus ID */
530 ret
= wiimote_cmd_read(wdata
, 0xa400fa, rmem
, 6);
532 return WIIMOTE_MP_NONE
;
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]);
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
;
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
;
548 return WIIMOTE_MP_UNKNOWN
;
551 /* device module handling */
553 static const __u8
* const wiimote_devtype_mods
[WIIMOTE_DEV_NUM
] = {
554 [WIIMOTE_DEV_PENDING
] = (const __u8
[]){
557 [WIIMOTE_DEV_UNKNOWN
] = (const __u8
[]){
560 [WIIMOTE_DEV_GENERIC
] = (const __u8
[]){
572 [WIIMOTE_DEV_GEN10
] = (const __u8
[]){
584 [WIIMOTE_DEV_GEN20
] = (const __u8
[]){
596 [WIIMOTE_DEV_BALANCE_BOARD
] = (const __u8
[]) {
603 static void wiimote_modules_load(struct wiimote_data
*wdata
,
604 unsigned int devtype
)
606 bool need_input
= false;
607 const __u8
*mods
, *iter
;
608 const struct wiimod_ops
*ops
;
611 mods
= wiimote_devtype_mods
[devtype
];
613 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
) {
614 if (wiimod_table
[*iter
]->flags
& WIIMOD_FLAG_INPUT
) {
621 wdata
->input
= input_allocate_device();
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
;
634 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
) {
635 ops
= wiimod_table
[*iter
];
639 ret
= ops
->probe(ops
, wdata
);
645 ret
= input_register_device(wdata
->input
);
650 spin_lock_irq(&wdata
->state
.lock
);
651 wdata
->state
.devtype
= devtype
;
652 spin_unlock_irq(&wdata
->state
.lock
);
656 for ( ; iter
-- != mods
; ) {
657 ops
= wiimod_table
[*iter
];
659 ops
->remove(ops
, wdata
);
663 input_free_device(wdata
->input
);
668 static void wiimote_modules_unload(struct wiimote_data
*wdata
)
670 const __u8
*mods
, *iter
;
671 const struct wiimod_ops
*ops
;
674 mods
= wiimote_devtype_mods
[wdata
->state
.devtype
];
676 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
677 wdata
->state
.devtype
= WIIMOTE_DEV_UNKNOWN
;
678 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
680 /* find end of list */
681 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
)
685 input_get_device(wdata
->input
);
686 input_unregister_device(wdata
->input
);
689 for ( ; iter
-- != mods
; ) {
690 ops
= wiimod_table
[*iter
];
692 ops
->remove(ops
, wdata
);
696 input_put_device(wdata
->input
);
701 /* device extension handling */
703 static void wiimote_ext_load(struct wiimote_data
*wdata
, unsigned int ext
)
706 const struct wiimod_ops
*ops
;
709 ops
= wiimod_ext_table
[ext
];
712 ret
= ops
->probe(ops
, wdata
);
714 ext
= WIIMOTE_EXT_UNKNOWN
;
717 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
718 wdata
->state
.exttype
= ext
;
719 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
722 static void wiimote_ext_unload(struct wiimote_data
*wdata
)
725 const struct wiimod_ops
*ops
;
727 ops
= wiimod_ext_table
[wdata
->state
.exttype
];
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
);
735 ops
->remove(ops
, wdata
);
738 static void wiimote_mp_load(struct wiimote_data
*wdata
)
741 const struct wiimod_ops
*ops
;
747 ret
= ops
->probe(ops
, wdata
);
752 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
753 wdata
->state
.mp
= mode
;
754 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
757 static void wiimote_mp_unload(struct wiimote_data
*wdata
)
760 const struct wiimod_ops
*ops
;
762 if (wdata
->state
.mp
< 2)
767 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
769 wdata
->state
.flags
&= ~WIIPROTO_FLAG_MP_USED
;
770 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
773 ops
->remove(ops
, wdata
);
776 /* device (re-)initialization and detection */
778 static 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)",
784 [WIIMOTE_DEV_BALANCE_BOARD
] = "Nintendo Wii Balance Board",
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. */
791 static void wiimote_init_set_type(struct wiimote_data
*wdata
,
794 __u8 devtype
= WIIMOTE_DEV_GENERIC
;
795 __u16 vendor
, product
;
798 vendor
= wdata
->hdev
->vendor
;
799 product
= wdata
->hdev
->product
;
800 name
= wdata
->hdev
->name
;
802 if (exttype
== WIIMOTE_EXT_BALANCE_BOARD
) {
803 devtype
= WIIMOTE_DEV_BALANCE_BOARD
;
807 if (!strcmp(name
, "Nintendo RVL-CNT-01")) {
808 devtype
= WIIMOTE_DEV_GEN10
;
810 } else if (!strcmp(name
, "Nintendo RVL-CNT-01-TR")) {
811 devtype
= WIIMOTE_DEV_GEN20
;
813 } else if (!strcmp(name
, "Nintendo RVL-WBC-01")) {
814 devtype
= WIIMOTE_DEV_BALANCE_BOARD
;
818 if (vendor
== USB_VENDOR_ID_NINTENDO
) {
819 if (product
== USB_DEVICE_ID_NINTENDO_WIIMOTE
) {
820 devtype
= WIIMOTE_DEV_GEN10
;
822 } else if (product
== USB_DEVICE_ID_NINTENDO_WIIMOTE2
) {
823 devtype
= WIIMOTE_DEV_GEN20
;
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
);
833 hid_info(wdata
->hdev
, "detected device: %s\n",
834 wiimote_devtype_names
[devtype
]);
836 wiimote_modules_load(wdata
, devtype
);
839 static void wiimote_init_detect(struct wiimote_data
*wdata
)
841 __u8 exttype
= WIIMOTE_EXT_NONE
, extdata
[6];
845 wiimote_cmd_acquire_noint(wdata
);
847 spin_lock_irq(&wdata
->state
.lock
);
848 wdata
->state
.devtype
= WIIMOTE_DEV_UNKNOWN
;
849 wiimote_cmd_set(wdata
, WIIPROTO_REQ_SREQ
, 0);
850 wiiproto_req_status(wdata
);
851 spin_unlock_irq(&wdata
->state
.lock
);
853 ret
= wiimote_cmd_wait_noint(wdata
);
857 spin_lock_irq(&wdata
->state
.lock
);
858 ext
= wdata
->state
.flags
& WIIPROTO_FLAG_EXT_PLUGGED
;
859 spin_unlock_irq(&wdata
->state
.lock
);
864 wiimote_cmd_init_ext(wdata
);
865 exttype
= wiimote_cmd_read_ext(wdata
, extdata
);
868 wiimote_cmd_release(wdata
);
869 wiimote_init_set_type(wdata
, exttype
);
870 /* schedule MP timer */
871 mod_timer(&wdata
->timer
, jiffies
+ HZ
* 4);
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
880 static void wiimote_init_poll_mp(struct wiimote_data
*wdata
)
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
);
890 /* load/unload MP module if it changed */
892 if (!wdata
->state
.mp
) {
893 hid_info(wdata
->hdev
, "detected extension: Nintendo Wii Motion Plus\n");
894 wiimote_mp_load(wdata
);
896 } else if (wdata
->state
.mp
) {
897 wiimote_mp_unload(wdata
);
900 mod_timer(&wdata
->timer
, jiffies
+ HZ
* 4);
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.
910 * Returns "true" if the device is in expected state, "false" if we should
911 * redo hotplug handling and extension initialization.
913 static bool wiimote_init_check(struct wiimote_data
*wdata
)
919 spin_lock_irq(&wdata
->state
.lock
);
920 flags
= wdata
->state
.flags
;
921 spin_unlock_irq(&wdata
->state
.lock
);
923 wiimote_cmd_acquire_noint(wdata
);
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
;
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
);
944 hid_dbg(wdata
->hdev
, "state left: !EXT && MP\n");
946 /* while MP is mapped, we get EXT_PLUGGED events */
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
;
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
);
968 hid_dbg(wdata
->hdev
, "state left: EXT && !MP\n");
970 /* poll MP for hotplug events */
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
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
;
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
);
995 hid_dbg(wdata
->hdev
, "state left: !EXT && !MP\n");
997 /* poll MP for hotplug events */
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
;
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
);
1025 hid_dbg(wdata
->hdev
, "state left: EXT && MP\n");
1027 /* while MP is mapped, we get EXT_PLUGGED events */
1037 wiimote_cmd_release(wdata
);
1039 /* only poll for MP if requested and if state didn't change */
1041 wiimote_init_poll_mp(wdata
);
1046 static const char *wiimote_exttype_names
[WIIMOTE_EXT_NUM
] = {
1047 [WIIMOTE_EXT_NONE
] = "None",
1048 [WIIMOTE_EXT_UNKNOWN
] = "Unknown",
1049 [WIIMOTE_EXT_NUNCHUK
] = "Nintendo Wii Nunchuk",
1050 [WIIMOTE_EXT_CLASSIC_CONTROLLER
] = "Nintendo Wii Classic Controller",
1051 [WIIMOTE_EXT_BALANCE_BOARD
] = "Nintendo Wii Balance Board",
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.
1062 static void wiimote_init_hotplug(struct wiimote_data
*wdata
)
1064 __u8 exttype
, extdata
[6], mpdata
[6];
1068 hid_dbg(wdata
->hdev
, "detect extensions..\n");
1070 wiimote_cmd_acquire_noint(wdata
);
1072 spin_lock_irq(&wdata
->state
.lock
);
1074 /* get state snapshot that we will then work on */
1075 flags
= wdata
->state
.flags
;
1077 /* disable event forwarding temporarily */
1078 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_ACTIVE
;
1079 wdata
->state
.flags
&= ~WIIPROTO_FLAG_MP_ACTIVE
;
1081 spin_unlock_irq(&wdata
->state
.lock
);
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
);
1089 wiimote_cmd_release(wdata
);
1091 /* load/unload extension module if it changed */
1092 if (exttype
!= wdata
->state
.exttype
) {
1093 /* unload previous extension */
1094 wiimote_ext_unload(wdata
);
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
);
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
);
1112 /* load/unload MP module if it changed */
1114 if (!wdata
->state
.mp
) {
1115 hid_info(wdata
->hdev
, "detected extension: Nintendo Wii Motion Plus\n");
1116 wiimote_mp_load(wdata
);
1118 } else if (wdata
->state
.mp
) {
1119 wiimote_mp_unload(wdata
);
1122 /* if MP is not used, do not map or activate it */
1123 if (!(flags
& WIIPROTO_FLAG_MP_USED
))
1126 /* map MP into main extension registers if used */
1128 wiimote_cmd_acquire_noint(wdata
);
1129 wiimote_cmd_map_mp(wdata
, exttype
);
1130 wiimote_cmd_release(wdata
);
1132 /* delete MP hotplug timer */
1133 del_timer_sync(&wdata
->timer
);
1135 /* reschedule MP hotplug timer */
1136 mod_timer(&wdata
->timer
, jiffies
+ HZ
* 4);
1139 spin_lock_irq(&wdata
->state
.lock
);
1141 /* enable data forwarding again and set expected hotplug state */
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
;
1148 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_PLUGGED
;
1149 wdata
->state
.flags
|= WIIPROTO_FLAG_MP_PLUGGED
;
1150 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_ACTIVE
;
1152 } else if (wdata
->state
.exttype
!= WIIMOTE_EXT_NONE
) {
1153 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_ACTIVE
;
1156 /* request status report for hotplug state updates */
1157 wiiproto_req_status(wdata
);
1159 spin_unlock_irq(&wdata
->state
.lock
);
1161 hid_dbg(wdata
->hdev
, "detected extensions: MP: %d EXT: %d\n",
1162 wdata
->state
.mp
, wdata
->state
.exttype
);
1165 static void wiimote_init_worker(struct work_struct
*work
)
1167 struct wiimote_data
*wdata
= container_of(work
, struct wiimote_data
,
1170 if (wdata
->state
.devtype
== WIIMOTE_DEV_PENDING
)
1171 wiimote_init_detect(wdata
);
1172 if (!wiimote_init_check(wdata
))
1173 wiimote_init_hotplug(wdata
);
1176 void __wiimote_schedule(struct wiimote_data
*wdata
)
1178 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_EXITING
))
1179 schedule_work(&wdata
->init_worker
);
1182 static void wiimote_schedule(struct wiimote_data
*wdata
)
1184 unsigned long flags
;
1186 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1187 __wiimote_schedule(wdata
);
1188 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1191 static void wiimote_init_timeout(unsigned long arg
)
1193 struct wiimote_data
*wdata
= (void*)arg
;
1195 wiimote_schedule(wdata
);
1198 /* protocol handlers */
1200 static void handler_keys(struct wiimote_data
*wdata
, const __u8
*payload
)
1202 const __u8
*iter
, *mods
;
1203 const struct wiimod_ops
*ops
;
1205 ops
= wiimod_ext_table
[wdata
->state
.exttype
];
1207 ops
->in_keys(wdata
, payload
);
1211 mods
= wiimote_devtype_mods
[wdata
->state
.devtype
];
1212 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
) {
1213 ops
= wiimod_table
[*iter
];
1215 ops
->in_keys(wdata
, payload
);
1221 static void handler_accel(struct wiimote_data
*wdata
, const __u8
*payload
)
1223 const __u8
*iter
, *mods
;
1224 const struct wiimod_ops
*ops
;
1226 ops
= wiimod_ext_table
[wdata
->state
.exttype
];
1227 if (ops
->in_accel
) {
1228 ops
->in_accel(wdata
, payload
);
1232 mods
= wiimote_devtype_mods
[wdata
->state
.devtype
];
1233 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
) {
1234 ops
= wiimod_table
[*iter
];
1235 if (ops
->in_accel
) {
1236 ops
->in_accel(wdata
, payload
);
1242 static bool valid_ext_handler(const struct wiimod_ops
*ops
, size_t len
)
1246 if ((ops
->flags
& WIIMOD_FLAG_EXT8
) && len
< 8)
1248 if ((ops
->flags
& WIIMOD_FLAG_EXT16
) && len
< 16)
1254 static void handler_ext(struct wiimote_data
*wdata
, const __u8
*payload
,
1257 const __u8
*iter
, *mods
;
1258 const struct wiimod_ops
*ops
;
1264 /* if MP is active, track MP slot hotplugging */
1265 if (wdata
->state
.flags
& WIIPROTO_FLAG_MP_ACTIVE
) {
1266 /* this bit is set for invalid events (eg. during hotplug) */
1267 if (payload
[5] & 0x01)
1270 if (payload
[4] & 0x01) {
1271 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_MP_PLUGGED
)) {
1272 hid_dbg(wdata
->hdev
, "MP hotplug: 1\n");
1273 wdata
->state
.flags
|= WIIPROTO_FLAG_MP_PLUGGED
;
1274 __wiimote_schedule(wdata
);
1277 if (wdata
->state
.flags
& WIIPROTO_FLAG_MP_PLUGGED
) {
1278 hid_dbg(wdata
->hdev
, "MP hotplug: 0\n");
1279 wdata
->state
.flags
&= ~WIIPROTO_FLAG_MP_PLUGGED
;
1280 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_ACTIVE
;
1281 __wiimote_schedule(wdata
);
1285 /* detect MP data that is sent interleaved with EXT data */
1286 is_mp
= payload
[5] & 0x02;
1291 /* ignore EXT events if no extension is active */
1292 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_EXT_ACTIVE
) && !is_mp
)
1295 /* try forwarding to extension handler, first */
1296 ops
= wiimod_ext_table
[wdata
->state
.exttype
];
1297 if (is_mp
&& ops
->in_mp
) {
1298 ops
->in_mp(wdata
, payload
);
1300 } else if (!is_mp
&& valid_ext_handler(ops
, len
)) {
1301 ops
->in_ext(wdata
, payload
);
1305 /* try forwarding to MP handler */
1307 if (is_mp
&& ops
->in_mp
) {
1308 ops
->in_mp(wdata
, payload
);
1310 } else if (!is_mp
&& valid_ext_handler(ops
, len
)) {
1311 ops
->in_ext(wdata
, payload
);
1315 /* try forwarding to loaded modules */
1316 mods
= wiimote_devtype_mods
[wdata
->state
.devtype
];
1317 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
) {
1318 ops
= wiimod_table
[*iter
];
1319 if (is_mp
&& ops
->in_mp
) {
1320 ops
->in_mp(wdata
, payload
);
1322 } else if (!is_mp
&& valid_ext_handler(ops
, len
)) {
1323 ops
->in_ext(wdata
, payload
);
1329 #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1330 #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1331 #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1332 #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
1334 static void handler_ir(struct wiimote_data
*wdata
, const __u8
*payload
,
1335 bool packed
, unsigned int id
)
1337 const __u8
*iter
, *mods
;
1338 const struct wiimod_ops
*ops
;
1340 ops
= wiimod_ext_table
[wdata
->state
.exttype
];
1342 ops
->in_ir(wdata
, payload
, packed
, id
);
1346 mods
= wiimote_devtype_mods
[wdata
->state
.devtype
];
1347 for (iter
= mods
; *iter
!= WIIMOD_NULL
; ++iter
) {
1348 ops
= wiimod_table
[*iter
];
1350 ops
->in_ir(wdata
, payload
, packed
, id
);
1356 /* reduced status report with "BB BB" key data only */
1357 static void handler_status_K(struct wiimote_data
*wdata
,
1358 const __u8
*payload
)
1360 handler_keys(wdata
, payload
);
1362 /* on status reports the drm is reset so we need to resend the drm */
1363 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1366 /* extended status report with "BB BB LF 00 00 VV" data */
1367 static void handler_status(struct wiimote_data
*wdata
, const __u8
*payload
)
1369 handler_status_K(wdata
, payload
);
1371 /* update extension status */
1372 if (payload
[2] & 0x02) {
1373 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_EXT_PLUGGED
)) {
1374 hid_dbg(wdata
->hdev
, "EXT hotplug: 1\n");
1375 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_PLUGGED
;
1376 __wiimote_schedule(wdata
);
1379 if (wdata
->state
.flags
& WIIPROTO_FLAG_EXT_PLUGGED
) {
1380 hid_dbg(wdata
->hdev
, "EXT hotplug: 0\n");
1381 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_PLUGGED
;
1382 wdata
->state
.flags
&= ~WIIPROTO_FLAG_MP_PLUGGED
;
1383 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_ACTIVE
;
1384 wdata
->state
.flags
&= ~WIIPROTO_FLAG_MP_ACTIVE
;
1385 __wiimote_schedule(wdata
);
1389 wdata
->state
.cmd_battery
= payload
[5];
1390 if (wiimote_cmd_pending(wdata
, WIIPROTO_REQ_SREQ
, 0))
1391 wiimote_cmd_complete(wdata
);
1394 /* reduced generic report with "BB BB" key data only */
1395 static void handler_generic_K(struct wiimote_data
*wdata
, const __u8
*payload
)
1397 handler_keys(wdata
, payload
);
1400 static void handler_data(struct wiimote_data
*wdata
, const __u8
*payload
)
1402 __u16 offset
= payload
[3] << 8 | payload
[4];
1403 __u8 size
= (payload
[2] >> 4) + 1;
1404 __u8 err
= payload
[2] & 0x0f;
1406 handler_keys(wdata
, payload
);
1408 if (wiimote_cmd_pending(wdata
, WIIPROTO_REQ_RMEM
, offset
)) {
1411 else if (size
> wdata
->state
.cmd_read_size
)
1412 size
= wdata
->state
.cmd_read_size
;
1414 wdata
->state
.cmd_read_size
= size
;
1415 if (wdata
->state
.cmd_read_buf
)
1416 memcpy(wdata
->state
.cmd_read_buf
, &payload
[5], size
);
1417 wiimote_cmd_complete(wdata
);
1421 static void handler_return(struct wiimote_data
*wdata
, const __u8
*payload
)
1423 __u8 err
= payload
[3];
1424 __u8 cmd
= payload
[2];
1426 handler_keys(wdata
, payload
);
1428 if (wiimote_cmd_pending(wdata
, cmd
, 0)) {
1429 wdata
->state
.cmd_err
= err
;
1430 wiimote_cmd_complete(wdata
);
1432 hid_warn(wdata
->hdev
, "Remote error %hhu on req %hhu\n", err
,
1437 static void handler_drm_KA(struct wiimote_data
*wdata
, const __u8
*payload
)
1439 handler_keys(wdata
, payload
);
1440 handler_accel(wdata
, payload
);
1443 static void handler_drm_KE(struct wiimote_data
*wdata
, const __u8
*payload
)
1445 handler_keys(wdata
, payload
);
1446 handler_ext(wdata
, &payload
[2], 8);
1449 static void handler_drm_KAI(struct wiimote_data
*wdata
, const __u8
*payload
)
1451 handler_keys(wdata
, payload
);
1452 handler_accel(wdata
, payload
);
1453 ir_to_input0(wdata
, &payload
[5], false);
1454 ir_to_input1(wdata
, &payload
[8], false);
1455 ir_to_input2(wdata
, &payload
[11], false);
1456 ir_to_input3(wdata
, &payload
[14], false);
1459 static void handler_drm_KEE(struct wiimote_data
*wdata
, const __u8
*payload
)
1461 handler_keys(wdata
, payload
);
1462 handler_ext(wdata
, &payload
[2], 19);
1465 static void handler_drm_KIE(struct wiimote_data
*wdata
, const __u8
*payload
)
1467 handler_keys(wdata
, payload
);
1468 ir_to_input0(wdata
, &payload
[2], false);
1469 ir_to_input1(wdata
, &payload
[4], true);
1470 ir_to_input2(wdata
, &payload
[7], false);
1471 ir_to_input3(wdata
, &payload
[9], true);
1472 handler_ext(wdata
, &payload
[12], 9);
1475 static void handler_drm_KAE(struct wiimote_data
*wdata
, const __u8
*payload
)
1477 handler_keys(wdata
, payload
);
1478 handler_accel(wdata
, payload
);
1479 handler_ext(wdata
, &payload
[5], 16);
1482 static void handler_drm_KAIE(struct wiimote_data
*wdata
, const __u8
*payload
)
1484 handler_keys(wdata
, payload
);
1485 handler_accel(wdata
, payload
);
1486 ir_to_input0(wdata
, &payload
[5], false);
1487 ir_to_input1(wdata
, &payload
[7], true);
1488 ir_to_input2(wdata
, &payload
[10], false);
1489 ir_to_input3(wdata
, &payload
[12], true);
1490 handler_ext(wdata
, &payload
[15], 6);
1493 static void handler_drm_E(struct wiimote_data
*wdata
, const __u8
*payload
)
1495 handler_ext(wdata
, payload
, 21);
1498 static void handler_drm_SKAI1(struct wiimote_data
*wdata
, const __u8
*payload
)
1500 handler_keys(wdata
, payload
);
1502 wdata
->state
.accel_split
[0] = payload
[2];
1503 wdata
->state
.accel_split
[1] = (payload
[0] >> 1) & (0x10 | 0x20);
1504 wdata
->state
.accel_split
[1] |= (payload
[1] << 1) & (0x40 | 0x80);
1506 ir_to_input0(wdata
, &payload
[3], false);
1507 ir_to_input1(wdata
, &payload
[12], false);
1510 static void handler_drm_SKAI2(struct wiimote_data
*wdata
, const __u8
*payload
)
1514 handler_keys(wdata
, payload
);
1516 wdata
->state
.accel_split
[1] |= (payload
[0] >> 5) & (0x01 | 0x02);
1517 wdata
->state
.accel_split
[1] |= (payload
[1] >> 3) & (0x04 | 0x08);
1521 buf
[2] = wdata
->state
.accel_split
[0];
1522 buf
[3] = payload
[2];
1523 buf
[4] = wdata
->state
.accel_split
[1];
1524 handler_accel(wdata
, buf
);
1526 ir_to_input2(wdata
, &payload
[3], false);
1527 ir_to_input3(wdata
, &payload
[12], false);
1530 struct wiiproto_handler
{
1533 void (*func
)(struct wiimote_data
*wdata
, const __u8
*payload
);
1536 static struct wiiproto_handler handlers
[] = {
1537 { .id
= WIIPROTO_REQ_STATUS
, .size
= 6, .func
= handler_status
},
1538 { .id
= WIIPROTO_REQ_STATUS
, .size
= 2, .func
= handler_status_K
},
1539 { .id
= WIIPROTO_REQ_DATA
, .size
= 21, .func
= handler_data
},
1540 { .id
= WIIPROTO_REQ_DATA
, .size
= 2, .func
= handler_generic_K
},
1541 { .id
= WIIPROTO_REQ_RETURN
, .size
= 4, .func
= handler_return
},
1542 { .id
= WIIPROTO_REQ_RETURN
, .size
= 2, .func
= handler_generic_K
},
1543 { .id
= WIIPROTO_REQ_DRM_K
, .size
= 2, .func
= handler_keys
},
1544 { .id
= WIIPROTO_REQ_DRM_KA
, .size
= 5, .func
= handler_drm_KA
},
1545 { .id
= WIIPROTO_REQ_DRM_KA
, .size
= 2, .func
= handler_generic_K
},
1546 { .id
= WIIPROTO_REQ_DRM_KE
, .size
= 10, .func
= handler_drm_KE
},
1547 { .id
= WIIPROTO_REQ_DRM_KE
, .size
= 2, .func
= handler_generic_K
},
1548 { .id
= WIIPROTO_REQ_DRM_KAI
, .size
= 17, .func
= handler_drm_KAI
},
1549 { .id
= WIIPROTO_REQ_DRM_KAI
, .size
= 2, .func
= handler_generic_K
},
1550 { .id
= WIIPROTO_REQ_DRM_KEE
, .size
= 21, .func
= handler_drm_KEE
},
1551 { .id
= WIIPROTO_REQ_DRM_KEE
, .size
= 2, .func
= handler_generic_K
},
1552 { .id
= WIIPROTO_REQ_DRM_KAE
, .size
= 21, .func
= handler_drm_KAE
},
1553 { .id
= WIIPROTO_REQ_DRM_KAE
, .size
= 2, .func
= handler_generic_K
},
1554 { .id
= WIIPROTO_REQ_DRM_KIE
, .size
= 21, .func
= handler_drm_KIE
},
1555 { .id
= WIIPROTO_REQ_DRM_KIE
, .size
= 2, .func
= handler_generic_K
},
1556 { .id
= WIIPROTO_REQ_DRM_KAIE
, .size
= 21, .func
= handler_drm_KAIE
},
1557 { .id
= WIIPROTO_REQ_DRM_KAIE
, .size
= 2, .func
= handler_generic_K
},
1558 { .id
= WIIPROTO_REQ_DRM_E
, .size
= 21, .func
= handler_drm_E
},
1559 { .id
= WIIPROTO_REQ_DRM_SKAI1
, .size
= 21, .func
= handler_drm_SKAI1
},
1560 { .id
= WIIPROTO_REQ_DRM_SKAI2
, .size
= 21, .func
= handler_drm_SKAI2
},
1564 static int wiimote_hid_event(struct hid_device
*hdev
, struct hid_report
*report
,
1565 u8
*raw_data
, int size
)
1567 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
1568 struct wiiproto_handler
*h
;
1570 unsigned long flags
;
1575 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1577 for (i
= 0; handlers
[i
].id
; ++i
) {
1579 if (h
->id
== raw_data
[0] && h
->size
< size
) {
1580 h
->func(wdata
, &raw_data
[1]);
1585 if (!handlers
[i
].id
)
1586 hid_warn(hdev
, "Unhandled report %hhu size %d\n", raw_data
[0],
1589 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1594 static struct wiimote_data
*wiimote_create(struct hid_device
*hdev
)
1596 struct wiimote_data
*wdata
;
1598 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
1603 hid_set_drvdata(hdev
, wdata
);
1605 spin_lock_init(&wdata
->queue
.lock
);
1606 INIT_WORK(&wdata
->queue
.worker
, wiimote_queue_worker
);
1608 spin_lock_init(&wdata
->state
.lock
);
1609 init_completion(&wdata
->state
.ready
);
1610 mutex_init(&wdata
->state
.sync
);
1611 wdata
->state
.drm
= WIIPROTO_REQ_DRM_K
;
1612 wdata
->state
.cmd_battery
= 0xff;
1614 INIT_WORK(&wdata
->init_worker
, wiimote_init_worker
);
1615 setup_timer(&wdata
->timer
, wiimote_init_timeout
, (long)wdata
);
1620 static void wiimote_destroy(struct wiimote_data
*wdata
)
1622 unsigned long flags
;
1624 wiidebug_deinit(wdata
);
1626 /* prevent init_worker from being scheduled again */
1627 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1628 wdata
->state
.flags
|= WIIPROTO_FLAG_EXITING
;
1629 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1631 cancel_work_sync(&wdata
->init_worker
);
1632 del_timer_sync(&wdata
->timer
);
1634 wiimote_mp_unload(wdata
);
1635 wiimote_ext_unload(wdata
);
1636 wiimote_modules_unload(wdata
);
1637 cancel_work_sync(&wdata
->queue
.worker
);
1638 hid_hw_close(wdata
->hdev
);
1639 hid_hw_stop(wdata
->hdev
);
1644 static int wiimote_hid_probe(struct hid_device
*hdev
,
1645 const struct hid_device_id
*id
)
1647 struct wiimote_data
*wdata
;
1650 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
1652 wdata
= wiimote_create(hdev
);
1654 hid_err(hdev
, "Can't alloc device\n");
1658 ret
= hid_parse(hdev
);
1660 hid_err(hdev
, "HID parse failed\n");
1664 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1666 hid_err(hdev
, "HW start failed\n");
1670 ret
= hid_hw_open(hdev
);
1672 hid_err(hdev
, "cannot start hardware I/O\n");
1676 ret
= wiidebug_init(wdata
);
1680 hid_info(hdev
, "New device registered\n");
1682 /* schedule device detection */
1683 wiimote_schedule(wdata
);
1688 wiimote_destroy(wdata
);
1694 input_free_device(wdata
->ir
);
1695 input_free_device(wdata
->accel
);
1700 static void wiimote_hid_remove(struct hid_device
*hdev
)
1702 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
1704 hid_info(hdev
, "Device removed\n");
1705 wiimote_destroy(wdata
);
1708 static const struct hid_device_id wiimote_hid_devices
[] = {
1709 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
1710 USB_DEVICE_ID_NINTENDO_WIIMOTE
) },
1711 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
1712 USB_DEVICE_ID_NINTENDO_WIIMOTE2
) },
1715 MODULE_DEVICE_TABLE(hid
, wiimote_hid_devices
);
1717 static struct hid_driver wiimote_hid_driver
= {
1719 .id_table
= wiimote_hid_devices
,
1720 .probe
= wiimote_hid_probe
,
1721 .remove
= wiimote_hid_remove
,
1722 .raw_event
= wiimote_hid_event
,
1724 module_hid_driver(wiimote_hid_driver
);
1726 MODULE_LICENSE("GPL");
1727 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1728 MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");