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) {
215 /* GEN10 and ealier devices bind IR formats to DRMs.
216 * Hence, we cannot use DRM_KAI here as it might be
217 * bound to IR_EXT. Use DRM_KAIE unconditionally so we
218 * work with all devices and our parsers can use the
219 * fixed formats, too. */
220 return WIIPROTO_REQ_DRM_KAIE;
222 return WIIPROTO_REQ_DRM_KIE;
224 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
225 return WIIPROTO_REQ_DRM_KAI;
226 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
227 return WIIPROTO_REQ_DRM_SKAI1;
229 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
231 return WIIPROTO_REQ_DRM_KAE;
233 return WIIPROTO_REQ_DRM_KA;
236 return WIIPROTO_REQ_DRM_KEE;
238 return WIIPROTO_REQ_DRM_K;
243 void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
247 if (wdata->state.flags & WIIPROTO_FLAG_DRM_LOCKED)
248 drm = wdata->state.drm;
249 else if (drm == WIIPROTO_REQ_NULL)
250 drm = select_drm(wdata);
252 cmd[0] = WIIPROTO_REQ_DRM;
256 wdata->state.drm = drm;
257 wiiproto_keep_rumble(wdata, &cmd[1]);
258 wiimote_queue(wdata, cmd, sizeof(cmd));
261 void wiiproto_req_status(struct wiimote_data *wdata)
265 cmd[0] = WIIPROTO_REQ_SREQ;
268 wiiproto_keep_rumble(wdata, &cmd[1]);
269 wiimote_queue(wdata, cmd, sizeof(cmd));
272 void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
275 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
279 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
281 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
283 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
286 void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
290 cmd[0] = WIIPROTO_REQ_IR1;
293 wiiproto_keep_rumble(wdata, &cmd[1]);
294 wiimote_queue(wdata, cmd, sizeof(cmd));
297 void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
301 cmd[0] = WIIPROTO_REQ_IR2;
304 wiiproto_keep_rumble(wdata, &cmd[1]);
305 wiimote_queue(wdata, cmd, sizeof(cmd));
308 #define wiiproto_req_wreg(wdata, os, buf, sz) \
309 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
311 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
312 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
314 static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
315 __u32 offset, const __u8 *buf, __u8 size)
319 if (size > 16 || size == 0) {
320 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
324 memset(cmd, 0, sizeof(cmd));
325 cmd[0] = WIIPROTO_REQ_WMEM;
326 cmd[2] = (offset >> 16) & 0xff;
327 cmd[3] = (offset >> 8) & 0xff;
328 cmd[4] = offset & 0xff;
330 memcpy(&cmd[6], buf, size);
335 wiiproto_keep_rumble(wdata, &cmd[1]);
336 wiimote_queue(wdata, cmd, sizeof(cmd));
339 void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
345 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
349 cmd[0] = WIIPROTO_REQ_RMEM;
351 cmd[2] = (offset >> 16) & 0xff;
352 cmd[3] = (offset >> 8) & 0xff;
353 cmd[4] = offset & 0xff;
354 cmd[5] = (size >> 8) & 0xff;
355 cmd[6] = size & 0xff;
360 wiiproto_keep_rumble(wdata, &cmd[1]);
361 wiimote_queue(wdata, cmd, sizeof(cmd));
364 /* requries the cmd-mutex to be held */
365 int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
366 const __u8 *wmem, __u8 size)
371 spin_lock_irqsave(&wdata->state.lock, flags);
372 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
373 wiiproto_req_wreg(wdata, offset, wmem, size);
374 spin_unlock_irqrestore(&wdata->state.lock, flags);
376 ret = wiimote_cmd_wait(wdata);
377 if (!ret && wdata->state.cmd_err)
383 /* requries the cmd-mutex to be held */
384 ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
390 spin_lock_irqsave(&wdata->state.lock, flags);
391 wdata->state.cmd_read_size = size;
392 wdata->state.cmd_read_buf = rmem;
393 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
394 wiiproto_req_rreg(wdata, offset, size);
395 spin_unlock_irqrestore(&wdata->state.lock, flags);
397 ret = wiimote_cmd_wait(wdata);
399 spin_lock_irqsave(&wdata->state.lock, flags);
400 wdata->state.cmd_read_buf = NULL;
401 spin_unlock_irqrestore(&wdata->state.lock, flags);
404 if (wdata->state.cmd_read_size == 0)
407 ret = wdata->state.cmd_read_size;
413 /* requires the cmd-mutex to be held */
414 static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
419 /* initialize extension */
421 ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
425 /* disable default encryption */
427 ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
434 /* requires the cmd-mutex to be held */
435 static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem)
439 /* read extension ID */
440 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
442 return WIIMOTE_EXT_NONE;
444 hid_dbg(wdata->hdev, "extension ID: %02x:%02x %02x:%02x %02x:%02x\n",
445 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
447 if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
448 rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
449 return WIIMOTE_EXT_NONE;
451 if (rmem[4] == 0x00 && rmem[5] == 0x00)
452 return WIIMOTE_EXT_NUNCHUK;
453 if (rmem[4] == 0x01 && rmem[5] == 0x01)
454 return WIIMOTE_EXT_CLASSIC_CONTROLLER;
455 if (rmem[4] == 0x04 && rmem[5] == 0x02)
456 return WIIMOTE_EXT_BALANCE_BOARD;
457 if (rmem[4] == 0x01 && rmem[5] == 0x20)
458 return WIIMOTE_EXT_PRO_CONTROLLER;
459 if (rmem[0] == 0x01 && rmem[1] == 0x00 &&
460 rmem[4] == 0x01 && rmem[5] == 0x03)
461 return WIIMOTE_EXT_GUITAR_HERO_DRUMS;
462 if (rmem[0] == 0x00 && rmem[1] == 0x00 &&
463 rmem[4] == 0x01 && rmem[5] == 0x03)
464 return WIIMOTE_EXT_GUITAR_HERO_GUITAR;
466 return WIIMOTE_EXT_UNKNOWN;
469 /* requires the cmd-mutex to be held */
470 static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
477 ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
481 /* disable default encryption */
483 ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
490 /* requires the cmd-mutex to be held */
491 static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
495 /* map MP with correct pass-through mode */
497 case WIIMOTE_EXT_CLASSIC_CONTROLLER:
498 case WIIMOTE_EXT_GUITAR_HERO_DRUMS:
499 case WIIMOTE_EXT_GUITAR_HERO_GUITAR:
502 case WIIMOTE_EXT_NUNCHUK:
510 return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
513 /* requires the cmd-mutex to be held */
514 static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
518 /* read motion plus ID */
519 ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
523 hid_dbg(wdata->hdev, "motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
524 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
529 hid_info(wdata->hdev, "unknown motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
530 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
535 /* requires the cmd-mutex to be held */
536 static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
541 /* read motion plus ID */
542 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
544 return WIIMOTE_MP_NONE;
546 hid_dbg(wdata->hdev, "mapped motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
547 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
549 if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
550 rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
551 return WIIMOTE_MP_NONE;
553 if (rmem[4] == 0x04 && rmem[5] == 0x05)
554 return WIIMOTE_MP_SINGLE;
555 else if (rmem[4] == 0x05 && rmem[5] == 0x05)
556 return WIIMOTE_MP_PASSTHROUGH_NUNCHUK;
557 else if (rmem[4] == 0x07 && rmem[5] == 0x05)
558 return WIIMOTE_MP_PASSTHROUGH_CLASSIC;
560 return WIIMOTE_MP_UNKNOWN;
563 /* device module handling */
565 static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
566 [WIIMOTE_DEV_PENDING] = (const __u8[]){
569 [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
573 [WIIMOTE_DEV_GENERIC] = (const __u8[]){
585 [WIIMOTE_DEV_GEN10] = (const __u8[]){
597 [WIIMOTE_DEV_GEN20] = (const __u8[]){
610 [WIIMOTE_DEV_BALANCE_BOARD] = (const __u8[]) {
616 [WIIMOTE_DEV_PRO_CONTROLLER] = (const __u8[]) {
627 static void wiimote_modules_load(struct wiimote_data *wdata,
628 unsigned int devtype)
630 bool need_input = false;
631 const __u8 *mods, *iter;
632 const struct wiimod_ops *ops;
635 mods = wiimote_devtype_mods[devtype];
637 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
638 if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
645 wdata->input = input_allocate_device();
649 input_set_drvdata(wdata->input, wdata);
650 wdata->input->dev.parent = &wdata->hdev->dev;
651 wdata->input->id.bustype = wdata->hdev->bus;
652 wdata->input->id.vendor = wdata->hdev->vendor;
653 wdata->input->id.product = wdata->hdev->product;
654 wdata->input->id.version = wdata->hdev->version;
655 wdata->input->name = WIIMOTE_NAME;
658 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
659 ops = wiimod_table[*iter];
663 ret = ops->probe(ops, wdata);
669 ret = input_register_device(wdata->input);
674 spin_lock_irq(&wdata->state.lock);
675 wdata->state.devtype = devtype;
676 spin_unlock_irq(&wdata->state.lock);
680 for ( ; iter-- != mods; ) {
681 ops = wiimod_table[*iter];
683 ops->remove(ops, wdata);
687 input_free_device(wdata->input);
692 static void wiimote_modules_unload(struct wiimote_data *wdata)
694 const __u8 *mods, *iter;
695 const struct wiimod_ops *ops;
698 mods = wiimote_devtype_mods[wdata->state.devtype];
700 spin_lock_irqsave(&wdata->state.lock, flags);
701 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
702 spin_unlock_irqrestore(&wdata->state.lock, flags);
704 /* find end of list */
705 for (iter = mods; *iter != WIIMOD_NULL; ++iter)
709 input_get_device(wdata->input);
710 input_unregister_device(wdata->input);
713 for ( ; iter-- != mods; ) {
714 ops = wiimod_table[*iter];
716 ops->remove(ops, wdata);
720 input_put_device(wdata->input);
725 /* device extension handling */
727 static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
730 const struct wiimod_ops *ops;
733 ops = wiimod_ext_table[ext];
736 ret = ops->probe(ops, wdata);
738 ext = WIIMOTE_EXT_UNKNOWN;
741 spin_lock_irqsave(&wdata->state.lock, flags);
742 wdata->state.exttype = ext;
743 spin_unlock_irqrestore(&wdata->state.lock, flags);
746 static void wiimote_ext_unload(struct wiimote_data *wdata)
749 const struct wiimod_ops *ops;
751 ops = wiimod_ext_table[wdata->state.exttype];
753 spin_lock_irqsave(&wdata->state.lock, flags);
754 wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
755 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
756 spin_unlock_irqrestore(&wdata->state.lock, flags);
759 ops->remove(ops, wdata);
762 static void wiimote_mp_load(struct wiimote_data *wdata)
765 const struct wiimod_ops *ops;
771 ret = ops->probe(ops, wdata);
776 spin_lock_irqsave(&wdata->state.lock, flags);
777 wdata->state.mp = mode;
778 spin_unlock_irqrestore(&wdata->state.lock, flags);
781 static void wiimote_mp_unload(struct wiimote_data *wdata)
784 const struct wiimod_ops *ops;
786 if (wdata->state.mp < 2)
791 spin_lock_irqsave(&wdata->state.lock, flags);
793 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
794 spin_unlock_irqrestore(&wdata->state.lock, flags);
797 ops->remove(ops, wdata);
800 /* device (re-)initialization and detection */
802 static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
803 [WIIMOTE_DEV_PENDING] = "Pending",
804 [WIIMOTE_DEV_UNKNOWN] = "Unknown",
805 [WIIMOTE_DEV_GENERIC] = "Generic",
806 [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
807 [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
808 [WIIMOTE_DEV_BALANCE_BOARD] = "Nintendo Wii Balance Board",
809 [WIIMOTE_DEV_PRO_CONTROLLER] = "Nintendo Wii U Pro Controller",
812 /* Try to guess the device type based on all collected information. We
813 * first try to detect by static extension types, then VID/PID and the
814 * device name. If we cannot detect the device, we use
815 * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
816 static void wiimote_init_set_type(struct wiimote_data *wdata,
819 __u8 devtype = WIIMOTE_DEV_GENERIC;
820 __u16 vendor, product;
823 vendor = wdata->hdev->vendor;
824 product = wdata->hdev->product;
825 name = wdata->hdev->name;
827 if (exttype == WIIMOTE_EXT_BALANCE_BOARD) {
828 devtype = WIIMOTE_DEV_BALANCE_BOARD;
830 } else if (exttype == WIIMOTE_EXT_PRO_CONTROLLER) {
831 devtype = WIIMOTE_DEV_PRO_CONTROLLER;
835 if (!strcmp(name, "Nintendo RVL-CNT-01")) {
836 devtype = WIIMOTE_DEV_GEN10;
838 } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
839 devtype = WIIMOTE_DEV_GEN20;
841 } else if (!strcmp(name, "Nintendo RVL-WBC-01")) {
842 devtype = WIIMOTE_DEV_BALANCE_BOARD;
844 } else if (!strcmp(name, "Nintendo RVL-CNT-01-UC")) {
845 devtype = WIIMOTE_DEV_PRO_CONTROLLER;
849 if (vendor == USB_VENDOR_ID_NINTENDO) {
850 if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
851 devtype = WIIMOTE_DEV_GEN10;
853 } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
854 devtype = WIIMOTE_DEV_GEN20;
860 if (devtype == WIIMOTE_DEV_GENERIC)
861 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
862 name, vendor, product, exttype);
864 hid_info(wdata->hdev, "detected device: %s\n",
865 wiimote_devtype_names[devtype]);
867 wiimote_modules_load(wdata, devtype);
870 static void wiimote_init_detect(struct wiimote_data *wdata)
872 __u8 exttype = WIIMOTE_EXT_NONE, extdata[6];
876 wiimote_cmd_acquire_noint(wdata);
878 spin_lock_irq(&wdata->state.lock);
879 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
880 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
881 wiiproto_req_status(wdata);
882 spin_unlock_irq(&wdata->state.lock);
884 ret = wiimote_cmd_wait_noint(wdata);
888 spin_lock_irq(&wdata->state.lock);
889 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
890 spin_unlock_irq(&wdata->state.lock);
895 wiimote_cmd_init_ext(wdata);
896 exttype = wiimote_cmd_read_ext(wdata, extdata);
899 wiimote_cmd_release(wdata);
900 wiimote_init_set_type(wdata, exttype);
902 /* schedule MP timer */
903 spin_lock_irq(&wdata->state.lock);
904 if (!(wdata->state.flags & WIIPROTO_FLAG_BUILTIN_MP) &&
905 !(wdata->state.flags & WIIPROTO_FLAG_NO_MP))
906 mod_timer(&wdata->timer, jiffies + HZ * 4);
907 spin_unlock_irq(&wdata->state.lock);
911 * MP hotplug events are not generated by the wiimote. Therefore, we need
912 * polling to detect it. We use a 4s interval for polling MP registers. This
913 * seems reasonable considering applications can trigger it manually via
916 static void wiimote_init_poll_mp(struct wiimote_data *wdata)
921 wiimote_cmd_acquire_noint(wdata);
922 wiimote_cmd_init_mp(wdata);
923 mp = wiimote_cmd_read_mp(wdata, mpdata);
924 wiimote_cmd_release(wdata);
926 /* load/unload MP module if it changed */
928 if (!wdata->state.mp) {
929 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
930 wiimote_mp_load(wdata);
932 } else if (wdata->state.mp) {
933 wiimote_mp_unload(wdata);
936 mod_timer(&wdata->timer, jiffies + HZ * 4);
940 * Check whether the wiimote is in the expected state. The extension registers
941 * may change during hotplug and initialization so we might get hotplug events
942 * that we caused by remapping some memory.
943 * We use some heuristics here to check known states. If the wiimote is in the
944 * expected state, we can ignore the hotplug event.
946 * Returns "true" if the device is in expected state, "false" if we should
947 * redo hotplug handling and extension initialization.
949 static bool wiimote_init_check(struct wiimote_data *wdata)
955 spin_lock_irq(&wdata->state.lock);
956 flags = wdata->state.flags;
957 spin_unlock_irq(&wdata->state.lock);
959 wiimote_cmd_acquire_noint(wdata);
961 /* If MP is used and active, but the extension is not, we expect:
962 * read_mp_mapped() == WIIMOTE_MP_SINGLE
963 * state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE
964 * We do not check EXT_PLUGGED because it might change during
965 * initialization of MP without extensions.
966 * - If MP is unplugged/replugged, read_mp_mapped() fails
967 * - If EXT is plugged, MP_PLUGGED will get set */
968 if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
969 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
970 type = wiimote_cmd_read_mp_mapped(wdata);
971 ret = type == WIIMOTE_MP_SINGLE;
973 spin_lock_irq(&wdata->state.lock);
974 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
975 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
976 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
977 spin_unlock_irq(&wdata->state.lock);
980 hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
982 /* while MP is mapped, we get EXT_PLUGGED events */
988 /* If MP is unused, but the extension port is used, we expect:
989 * read_ext == state.exttype
990 * state.flags == !MP_ACTIVE && EXT_ACTIVE
991 * - If MP is plugged/unplugged, our timer detects it
992 * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */
993 if (!(flags & WIIPROTO_FLAG_MP_USED) &&
994 wdata->state.exttype != WIIMOTE_EXT_NONE) {
995 type = wiimote_cmd_read_ext(wdata, data);
996 ret = type == wdata->state.exttype;
998 spin_lock_irq(&wdata->state.lock);
999 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1000 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1001 spin_unlock_irq(&wdata->state.lock);
1004 hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
1006 /* poll MP for hotplug events */
1012 /* If neither MP nor an extension are used, we expect:
1013 * read_ext() == WIIMOTE_EXT_NONE
1014 * state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED
1015 * No need to perform any action in this case as everything is
1017 * - If MP is plugged/unplugged, our timer detects it
1018 * - If EXT is plugged, EXT_PLUGGED will be set */
1019 if (!(flags & WIIPROTO_FLAG_MP_USED) &&
1020 wdata->state.exttype == WIIMOTE_EXT_NONE) {
1021 type = wiimote_cmd_read_ext(wdata, data);
1022 ret = type == wdata->state.exttype;
1024 spin_lock_irq(&wdata->state.lock);
1025 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1026 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1027 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1028 spin_unlock_irq(&wdata->state.lock);
1031 hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
1033 /* poll MP for hotplug events */
1039 /* The trickiest part is if both EXT and MP are active. We cannot read
1040 * the EXT ID, anymore, because MP is mapped over it. However, we use
1041 * a handy trick here:
1042 * - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent
1043 * MP_PLUGGED might be re-sent again before we are scheduled, but
1044 * EXT_ACTIVE will stay unset.
1045 * So it is enough to check for mp_mapped() and MP_ACTIVE and
1046 * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */
1047 if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
1048 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
1049 type = wiimote_cmd_read_mp_mapped(wdata);
1050 ret = type != WIIMOTE_MP_NONE;
1051 ret = ret && type != WIIMOTE_MP_UNKNOWN;
1052 ret = ret && type != WIIMOTE_MP_SINGLE;
1054 spin_lock_irq(&wdata->state.lock);
1055 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1056 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1057 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1058 spin_unlock_irq(&wdata->state.lock);
1061 hid_dbg(wdata->hdev, "state left: EXT && MP\n");
1063 /* while MP is mapped, we get EXT_PLUGGED events */
1073 wiimote_cmd_release(wdata);
1075 /* only poll for MP if requested and if state didn't change */
1076 if (ret && poll_mp && !(flags & WIIPROTO_FLAG_BUILTIN_MP) &&
1077 !(flags & WIIPROTO_FLAG_NO_MP))
1078 wiimote_init_poll_mp(wdata);
1083 static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = {
1084 [WIIMOTE_EXT_NONE] = "None",
1085 [WIIMOTE_EXT_UNKNOWN] = "Unknown",
1086 [WIIMOTE_EXT_NUNCHUK] = "Nintendo Wii Nunchuk",
1087 [WIIMOTE_EXT_CLASSIC_CONTROLLER] = "Nintendo Wii Classic Controller",
1088 [WIIMOTE_EXT_BALANCE_BOARD] = "Nintendo Wii Balance Board",
1089 [WIIMOTE_EXT_PRO_CONTROLLER] = "Nintendo Wii U Pro Controller",
1090 [WIIMOTE_EXT_GUITAR_HERO_DRUMS] = "Nintendo Wii Guitar Hero Drums",
1091 [WIIMOTE_EXT_GUITAR_HERO_GUITAR] = "Nintendo Wii Guitar Hero Guitar",
1095 * Handle hotplug events
1096 * If we receive an hotplug event and the device-check failed, we deinitialize
1097 * the extension ports, re-read all extension IDs and set the device into
1098 * the desired state. This involves mapping MP into the main extension
1099 * registers, setting up extension passthrough modes and initializing the
1100 * requested extensions.
1102 static void wiimote_init_hotplug(struct wiimote_data *wdata)
1104 __u8 exttype, extdata[6], mpdata[6];
1108 hid_dbg(wdata->hdev, "detect extensions..\n");
1110 wiimote_cmd_acquire_noint(wdata);
1112 spin_lock_irq(&wdata->state.lock);
1114 /* get state snapshot that we will then work on */
1115 flags = wdata->state.flags;
1117 /* disable event forwarding temporarily */
1118 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1119 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1121 spin_unlock_irq(&wdata->state.lock);
1123 /* init extension and MP (deactivates current extension or MP) */
1124 wiimote_cmd_init_ext(wdata);
1125 if (flags & WIIPROTO_FLAG_NO_MP) {
1128 wiimote_cmd_init_mp(wdata);
1129 mp = wiimote_cmd_read_mp(wdata, mpdata);
1131 exttype = wiimote_cmd_read_ext(wdata, extdata);
1133 wiimote_cmd_release(wdata);
1135 /* load/unload extension module if it changed */
1136 if (exttype != wdata->state.exttype) {
1137 /* unload previous extension */
1138 wiimote_ext_unload(wdata);
1140 if (exttype == WIIMOTE_EXT_UNKNOWN) {
1141 hid_info(wdata->hdev, "cannot detect extension; %02x:%02x %02x:%02x %02x:%02x\n",
1142 extdata[0], extdata[1], extdata[2],
1143 extdata[3], extdata[4], extdata[5]);
1144 } else if (exttype == WIIMOTE_EXT_NONE) {
1145 spin_lock_irq(&wdata->state.lock);
1146 wdata->state.exttype = WIIMOTE_EXT_NONE;
1147 spin_unlock_irq(&wdata->state.lock);
1149 hid_info(wdata->hdev, "detected extension: %s\n",
1150 wiimote_exttype_names[exttype]);
1151 /* try loading new extension */
1152 wiimote_ext_load(wdata, exttype);
1156 /* load/unload MP module if it changed */
1158 if (!wdata->state.mp) {
1159 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
1160 wiimote_mp_load(wdata);
1162 } else if (wdata->state.mp) {
1163 wiimote_mp_unload(wdata);
1166 /* if MP is not used, do not map or activate it */
1167 if (!(flags & WIIPROTO_FLAG_MP_USED))
1170 /* map MP into main extension registers if used */
1172 wiimote_cmd_acquire_noint(wdata);
1173 wiimote_cmd_map_mp(wdata, exttype);
1174 wiimote_cmd_release(wdata);
1176 /* delete MP hotplug timer */
1177 del_timer_sync(&wdata->timer);
1179 /* reschedule MP hotplug timer */
1180 if (!(flags & WIIPROTO_FLAG_BUILTIN_MP) &&
1181 !(flags & WIIPROTO_FLAG_NO_MP))
1182 mod_timer(&wdata->timer, jiffies + HZ * 4);
1185 spin_lock_irq(&wdata->state.lock);
1187 /* enable data forwarding again and set expected hotplug state */
1189 wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
1190 if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
1191 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1192 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1194 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1195 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1196 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1198 } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
1199 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1202 /* request status report for hotplug state updates */
1203 wiiproto_req_status(wdata);
1205 spin_unlock_irq(&wdata->state.lock);
1207 hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
1208 wdata->state.mp, wdata->state.exttype);
1211 static void wiimote_init_worker(struct work_struct *work)
1213 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
1215 bool changed = false;
1217 if (wdata->state.devtype == WIIMOTE_DEV_PENDING) {
1218 wiimote_init_detect(wdata);
1222 if (changed || !wiimote_init_check(wdata))
1223 wiimote_init_hotplug(wdata);
1226 kobject_uevent(&wdata->hdev->dev.kobj, KOBJ_CHANGE);
1229 void __wiimote_schedule(struct wiimote_data *wdata)
1231 if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
1232 schedule_work(&wdata->init_worker);
1235 static void wiimote_schedule(struct wiimote_data *wdata)
1237 unsigned long flags;
1239 spin_lock_irqsave(&wdata->state.lock, flags);
1240 __wiimote_schedule(wdata);
1241 spin_unlock_irqrestore(&wdata->state.lock, flags);
1244 static void wiimote_init_timeout(unsigned long arg)
1246 struct wiimote_data *wdata = (void*)arg;
1248 wiimote_schedule(wdata);
1251 /* protocol handlers */
1253 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
1255 const __u8 *iter, *mods;
1256 const struct wiimod_ops *ops;
1258 ops = wiimod_ext_table[wdata->state.exttype];
1260 ops->in_keys(wdata, payload);
1264 mods = wiimote_devtype_mods[wdata->state.devtype];
1265 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1266 ops = wiimod_table[*iter];
1268 ops->in_keys(wdata, payload);
1274 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
1276 const __u8 *iter, *mods;
1277 const struct wiimod_ops *ops;
1279 ops = wiimod_ext_table[wdata->state.exttype];
1280 if (ops->in_accel) {
1281 ops->in_accel(wdata, payload);
1285 mods = wiimote_devtype_mods[wdata->state.devtype];
1286 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1287 ops = wiimod_table[*iter];
1288 if (ops->in_accel) {
1289 ops->in_accel(wdata, payload);
1295 static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len)
1299 if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8)
1301 if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16)
1307 static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
1310 static const __u8 invalid[21] = { 0xff, 0xff, 0xff, 0xff,
1311 0xff, 0xff, 0xff, 0xff,
1312 0xff, 0xff, 0xff, 0xff,
1313 0xff, 0xff, 0xff, 0xff,
1314 0xff, 0xff, 0xff, 0xff,
1316 const __u8 *iter, *mods;
1317 const struct wiimod_ops *ops;
1322 if (len < 6 || !memcmp(payload, invalid, len))
1325 /* if MP is active, track MP slot hotplugging */
1326 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1327 /* this bit is set for invalid events (eg. during hotplug) */
1328 if (payload[5] & 0x01)
1331 if (payload[4] & 0x01) {
1332 if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
1333 hid_dbg(wdata->hdev, "MP hotplug: 1\n");
1334 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1335 __wiimote_schedule(wdata);
1338 if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
1339 hid_dbg(wdata->hdev, "MP hotplug: 0\n");
1340 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1341 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1342 __wiimote_schedule(wdata);
1346 /* detect MP data that is sent interleaved with EXT data */
1347 is_mp = payload[5] & 0x02;
1352 /* ignore EXT events if no extension is active */
1353 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
1356 /* try forwarding to extension handler, first */
1357 ops = wiimod_ext_table[wdata->state.exttype];
1358 if (is_mp && ops->in_mp) {
1359 ops->in_mp(wdata, payload);
1361 } else if (!is_mp && valid_ext_handler(ops, len)) {
1362 ops->in_ext(wdata, payload);
1366 /* try forwarding to MP handler */
1368 if (is_mp && ops->in_mp) {
1369 ops->in_mp(wdata, payload);
1371 } else if (!is_mp && valid_ext_handler(ops, len)) {
1372 ops->in_ext(wdata, payload);
1376 /* try forwarding to loaded modules */
1377 mods = wiimote_devtype_mods[wdata->state.devtype];
1378 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1379 ops = wiimod_table[*iter];
1380 if (is_mp && ops->in_mp) {
1381 ops->in_mp(wdata, payload);
1383 } else if (!is_mp && valid_ext_handler(ops, len)) {
1384 ops->in_ext(wdata, payload);
1390 #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1391 #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1392 #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1393 #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
1395 static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
1396 bool packed, unsigned int id)
1398 const __u8 *iter, *mods;
1399 const struct wiimod_ops *ops;
1401 ops = wiimod_ext_table[wdata->state.exttype];
1403 ops->in_ir(wdata, payload, packed, id);
1407 mods = wiimote_devtype_mods[wdata->state.devtype];
1408 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1409 ops = wiimod_table[*iter];
1411 ops->in_ir(wdata, payload, packed, id);
1417 /* reduced status report with "BB BB" key data only */
1418 static void handler_status_K(struct wiimote_data *wdata,
1419 const __u8 *payload)
1421 handler_keys(wdata, payload);
1423 /* on status reports the drm is reset so we need to resend the drm */
1424 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1427 /* extended status report with "BB BB LF 00 00 VV" data */
1428 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1430 handler_status_K(wdata, payload);
1432 /* update extension status */
1433 if (payload[2] & 0x02) {
1434 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
1435 hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
1436 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1437 __wiimote_schedule(wdata);
1440 if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
1441 hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
1442 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1443 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1444 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1445 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1446 __wiimote_schedule(wdata);
1450 wdata->state.cmd_battery = payload[5];
1451 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
1452 wiimote_cmd_complete(wdata);
1455 /* reduced generic report with "BB BB" key data only */
1456 static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1458 handler_keys(wdata, payload);
1461 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1463 __u16 offset = payload[3] << 8 | payload[4];
1464 __u8 size = (payload[2] >> 4) + 1;
1465 __u8 err = payload[2] & 0x0f;
1467 handler_keys(wdata, payload);
1469 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1472 else if (size > wdata->state.cmd_read_size)
1473 size = wdata->state.cmd_read_size;
1475 wdata->state.cmd_read_size = size;
1476 if (wdata->state.cmd_read_buf)
1477 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1478 wiimote_cmd_complete(wdata);
1482 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1484 __u8 err = payload[3];
1485 __u8 cmd = payload[2];
1487 handler_keys(wdata, payload);
1489 if (wiimote_cmd_pending(wdata, cmd, 0)) {
1490 wdata->state.cmd_err = err;
1491 wiimote_cmd_complete(wdata);
1493 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
1498 static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1500 handler_keys(wdata, payload);
1501 handler_accel(wdata, payload);
1504 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1506 handler_keys(wdata, payload);
1507 handler_ext(wdata, &payload[2], 8);
1510 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1512 handler_keys(wdata, payload);
1513 handler_accel(wdata, payload);
1514 ir_to_input0(wdata, &payload[5], false);
1515 ir_to_input1(wdata, &payload[8], false);
1516 ir_to_input2(wdata, &payload[11], false);
1517 ir_to_input3(wdata, &payload[14], false);
1520 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1522 handler_keys(wdata, payload);
1523 handler_ext(wdata, &payload[2], 19);
1526 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1528 handler_keys(wdata, payload);
1529 ir_to_input0(wdata, &payload[2], false);
1530 ir_to_input1(wdata, &payload[4], true);
1531 ir_to_input2(wdata, &payload[7], false);
1532 ir_to_input3(wdata, &payload[9], true);
1533 handler_ext(wdata, &payload[12], 9);
1536 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1538 handler_keys(wdata, payload);
1539 handler_accel(wdata, payload);
1540 handler_ext(wdata, &payload[5], 16);
1543 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1545 handler_keys(wdata, payload);
1546 handler_accel(wdata, payload);
1547 ir_to_input0(wdata, &payload[5], false);
1548 ir_to_input1(wdata, &payload[7], true);
1549 ir_to_input2(wdata, &payload[10], false);
1550 ir_to_input3(wdata, &payload[12], true);
1551 handler_ext(wdata, &payload[15], 6);
1554 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1556 handler_ext(wdata, payload, 21);
1559 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1561 handler_keys(wdata, payload);
1563 wdata->state.accel_split[0] = payload[2];
1564 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1565 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
1567 ir_to_input0(wdata, &payload[3], false);
1568 ir_to_input1(wdata, &payload[12], false);
1571 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1575 handler_keys(wdata, payload);
1577 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1578 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1582 buf[2] = wdata->state.accel_split[0];
1583 buf[3] = payload[2];
1584 buf[4] = wdata->state.accel_split[1];
1585 handler_accel(wdata, buf);
1587 ir_to_input2(wdata, &payload[3], false);
1588 ir_to_input3(wdata, &payload[12], false);
1591 struct wiiproto_handler {
1594 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1597 static struct wiiproto_handler handlers[] = {
1598 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
1599 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
1600 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
1601 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
1602 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
1603 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
1604 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
1605 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
1606 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
1607 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
1608 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
1609 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
1610 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
1611 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
1612 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
1613 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
1614 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
1615 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
1616 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
1617 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
1618 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
1619 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
1620 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1621 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
1625 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1626 u8 *raw_data, int size)
1628 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1629 struct wiiproto_handler *h;
1631 unsigned long flags;
1636 spin_lock_irqsave(&wdata->state.lock, flags);
1638 for (i = 0; handlers[i].id; ++i) {
1640 if (h->id == raw_data[0] && h->size < size) {
1641 h->func(wdata, &raw_data[1]);
1646 if (!handlers[i].id)
1647 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1650 spin_unlock_irqrestore(&wdata->state.lock, flags);
1655 static ssize_t wiimote_ext_show(struct device *dev,
1656 struct device_attribute *attr,
1659 struct wiimote_data *wdata = dev_to_wii(dev);
1661 unsigned long flags;
1663 spin_lock_irqsave(&wdata->state.lock, flags);
1664 type = wdata->state.exttype;
1665 spin_unlock_irqrestore(&wdata->state.lock, flags);
1668 case WIIMOTE_EXT_NONE:
1669 return sprintf(buf, "none\n");
1670 case WIIMOTE_EXT_NUNCHUK:
1671 return sprintf(buf, "nunchuk\n");
1672 case WIIMOTE_EXT_CLASSIC_CONTROLLER:
1673 return sprintf(buf, "classic\n");
1674 case WIIMOTE_EXT_BALANCE_BOARD:
1675 return sprintf(buf, "balanceboard\n");
1676 case WIIMOTE_EXT_PRO_CONTROLLER:
1677 return sprintf(buf, "procontroller\n");
1678 case WIIMOTE_EXT_GUITAR_HERO_DRUMS:
1679 return sprintf(buf, "drums\n");
1680 case WIIMOTE_EXT_GUITAR_HERO_GUITAR:
1681 return sprintf(buf, "guitar\n");
1682 case WIIMOTE_EXT_UNKNOWN:
1685 return sprintf(buf, "unknown\n");
1689 static ssize_t wiimote_ext_store(struct device *dev,
1690 struct device_attribute *attr,
1691 const char *buf, size_t count)
1693 struct wiimote_data *wdata = dev_to_wii(dev);
1695 if (!strcmp(buf, "scan")) {
1696 wiimote_schedule(wdata);
1701 return strnlen(buf, PAGE_SIZE);
1704 static DEVICE_ATTR(extension, S_IRUGO | S_IWUSR | S_IWGRP, wiimote_ext_show,
1707 static ssize_t wiimote_dev_show(struct device *dev,
1708 struct device_attribute *attr,
1711 struct wiimote_data *wdata = dev_to_wii(dev);
1713 unsigned long flags;
1715 spin_lock_irqsave(&wdata->state.lock, flags);
1716 type = wdata->state.devtype;
1717 spin_unlock_irqrestore(&wdata->state.lock, flags);
1720 case WIIMOTE_DEV_GENERIC:
1721 return sprintf(buf, "generic\n");
1722 case WIIMOTE_DEV_GEN10:
1723 return sprintf(buf, "gen10\n");
1724 case WIIMOTE_DEV_GEN20:
1725 return sprintf(buf, "gen20\n");
1726 case WIIMOTE_DEV_BALANCE_BOARD:
1727 return sprintf(buf, "balanceboard\n");
1728 case WIIMOTE_DEV_PRO_CONTROLLER:
1729 return sprintf(buf, "procontroller\n");
1730 case WIIMOTE_DEV_PENDING:
1731 return sprintf(buf, "pending\n");
1732 case WIIMOTE_DEV_UNKNOWN:
1735 return sprintf(buf, "unknown\n");
1739 static DEVICE_ATTR(devtype, S_IRUGO, wiimote_dev_show, NULL);
1741 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1743 struct wiimote_data *wdata;
1745 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1750 hid_set_drvdata(hdev, wdata);
1752 spin_lock_init(&wdata->queue.lock);
1753 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
1755 spin_lock_init(&wdata->state.lock);
1756 init_completion(&wdata->state.ready);
1757 mutex_init(&wdata->state.sync);
1758 wdata->state.drm = WIIPROTO_REQ_DRM_K;
1759 wdata->state.cmd_battery = 0xff;
1761 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
1762 setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata);
1767 static void wiimote_destroy(struct wiimote_data *wdata)
1769 unsigned long flags;
1771 wiidebug_deinit(wdata);
1773 /* prevent init_worker from being scheduled again */
1774 spin_lock_irqsave(&wdata->state.lock, flags);
1775 wdata->state.flags |= WIIPROTO_FLAG_EXITING;
1776 spin_unlock_irqrestore(&wdata->state.lock, flags);
1778 cancel_work_sync(&wdata->init_worker);
1779 del_timer_sync(&wdata->timer);
1781 device_remove_file(&wdata->hdev->dev, &dev_attr_devtype);
1782 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1784 wiimote_mp_unload(wdata);
1785 wiimote_ext_unload(wdata);
1786 wiimote_modules_unload(wdata);
1787 cancel_work_sync(&wdata->queue.worker);
1788 hid_hw_close(wdata->hdev);
1789 hid_hw_stop(wdata->hdev);
1794 static int wiimote_hid_probe(struct hid_device *hdev,
1795 const struct hid_device_id *id)
1797 struct wiimote_data *wdata;
1800 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1802 wdata = wiimote_create(hdev);
1804 hid_err(hdev, "Can't alloc device\n");
1808 ret = hid_parse(hdev);
1810 hid_err(hdev, "HID parse failed\n");
1814 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1816 hid_err(hdev, "HW start failed\n");
1820 ret = hid_hw_open(hdev);
1822 hid_err(hdev, "cannot start hardware I/O\n");
1826 ret = device_create_file(&hdev->dev, &dev_attr_extension);
1828 hid_err(hdev, "cannot create sysfs attribute\n");
1832 ret = device_create_file(&hdev->dev, &dev_attr_devtype);
1834 hid_err(hdev, "cannot create sysfs attribute\n");
1838 ret = wiidebug_init(wdata);
1842 hid_info(hdev, "New device registered\n");
1844 /* schedule device detection */
1845 wiimote_schedule(wdata);
1850 wiimote_destroy(wdata);
1854 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
1860 input_free_device(wdata->ir);
1861 input_free_device(wdata->accel);
1866 static void wiimote_hid_remove(struct hid_device *hdev)
1868 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1870 hid_info(hdev, "Device removed\n");
1871 wiimote_destroy(wdata);
1874 static const struct hid_device_id wiimote_hid_devices[] = {
1875 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1876 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1877 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1878 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
1881 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1883 static struct hid_driver wiimote_hid_driver = {
1885 .id_table = wiimote_hid_devices,
1886 .probe = wiimote_hid_probe,
1887 .remove = wiimote_hid_remove,
1888 .raw_event = wiimote_hid_event,
1890 module_hid_driver(wiimote_hid_driver);
1892 MODULE_LICENSE("GPL");
1893 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1894 MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");