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/leds.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/power_supply.h>
21 #include <linux/spinlock.h>
23 #include "hid-wiimote.h"
40 static __u16 wiiproto_keymap[] = {
41 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
42 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
43 KEY_UP, /* WIIPROTO_KEY_UP */
44 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
45 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
46 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
47 BTN_1, /* WIIPROTO_KEY_ONE */
48 BTN_2, /* WIIPROTO_KEY_TWO */
49 BTN_A, /* WIIPROTO_KEY_A */
50 BTN_B, /* WIIPROTO_KEY_B */
51 BTN_MODE, /* WIIPROTO_KEY_HOME */
54 static enum power_supply_property wiimote_battery_props[] = {
55 POWER_SUPPLY_PROP_CAPACITY,
56 POWER_SUPPLY_PROP_SCOPE,
59 /* output queue handling */
61 static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
67 if (!hdev->hid_output_raw_report)
70 buf = kmemdup(buffer, count, GFP_KERNEL);
74 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
80 static void wiimote_queue_worker(struct work_struct *work)
82 struct wiimote_queue *queue = container_of(work, struct wiimote_queue,
84 struct wiimote_data *wdata = container_of(queue, struct wiimote_data,
88 spin_lock_irqsave(&wdata->queue.lock, flags);
90 while (wdata->queue.head != wdata->queue.tail) {
91 spin_unlock_irqrestore(&wdata->queue.lock, flags);
92 wiimote_hid_send(wdata->hdev,
93 wdata->queue.outq[wdata->queue.tail].data,
94 wdata->queue.outq[wdata->queue.tail].size);
95 spin_lock_irqsave(&wdata->queue.lock, flags);
97 wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
100 spin_unlock_irqrestore(&wdata->queue.lock, flags);
103 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
109 if (count > HID_MAX_BUFFER_SIZE) {
110 hid_warn(wdata->hdev, "Sending too large output report\n");
115 * Copy new request into our output queue and check whether the
116 * queue is full. If it is full, discard this request.
117 * If it is empty we need to start a new worker that will
118 * send out the buffer to the hid device.
119 * If the queue is not empty, then there must be a worker
120 * that is currently sending out our buffer and this worker
121 * will reschedule itself until the queue is empty.
124 spin_lock_irqsave(&wdata->queue.lock, flags);
126 memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
127 wdata->queue.outq[wdata->queue.head].size = count;
128 newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
130 if (wdata->queue.head == wdata->queue.tail) {
131 wdata->queue.head = newhead;
132 schedule_work(&wdata->queue.worker);
133 } else if (newhead != wdata->queue.tail) {
134 wdata->queue.head = newhead;
136 hid_warn(wdata->hdev, "Output queue is full");
139 spin_unlock_irqrestore(&wdata->queue.lock, flags);
143 * This sets the rumble bit on the given output report if rumble is
145 * \cmd1 must point to the second byte in the output report => &cmd[1]
146 * This must be called on nearly every output report before passing it
147 * into the output queue!
149 static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
151 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
155 static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
160 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
164 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
166 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
168 cmd[0] = WIIPROTO_REQ_RUMBLE;
171 wiiproto_keep_rumble(wdata, &cmd[1]);
172 wiimote_queue(wdata, cmd, sizeof(cmd));
175 static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
179 leds &= WIIPROTO_FLAGS_LEDS;
180 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
182 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
184 cmd[0] = WIIPROTO_REQ_LED;
187 if (leds & WIIPROTO_FLAG_LED1)
189 if (leds & WIIPROTO_FLAG_LED2)
191 if (leds & WIIPROTO_FLAG_LED3)
193 if (leds & WIIPROTO_FLAG_LED4)
196 wiiproto_keep_rumble(wdata, &cmd[1]);
197 wiimote_queue(wdata, cmd, sizeof(cmd));
201 * Check what peripherals of the wiimote are currently
202 * active and select a proper DRM that supports all of
203 * the requested data inputs.
205 static __u8 select_drm(struct wiimote_data *wdata)
207 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
208 bool ext = wiiext_active(wdata);
210 if (ir == WIIPROTO_FLAG_IR_BASIC) {
211 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
212 return WIIPROTO_REQ_DRM_KAIE;
214 return WIIPROTO_REQ_DRM_KIE;
215 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
216 return WIIPROTO_REQ_DRM_KAI;
217 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
218 return WIIPROTO_REQ_DRM_SKAI1;
220 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
222 return WIIPROTO_REQ_DRM_KAE;
224 return WIIPROTO_REQ_DRM_KA;
227 return WIIPROTO_REQ_DRM_KE;
229 return WIIPROTO_REQ_DRM_K;
234 void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
238 if (drm == WIIPROTO_REQ_NULL)
239 drm = select_drm(wdata);
241 cmd[0] = WIIPROTO_REQ_DRM;
245 wdata->state.drm = drm;
246 wiiproto_keep_rumble(wdata, &cmd[1]);
247 wiimote_queue(wdata, cmd, sizeof(cmd));
250 static void wiiproto_req_status(struct wiimote_data *wdata)
254 cmd[0] = WIIPROTO_REQ_SREQ;
257 wiiproto_keep_rumble(wdata, &cmd[1]);
258 wiimote_queue(wdata, cmd, sizeof(cmd));
261 static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
264 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
268 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
270 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
272 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
275 static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
279 cmd[0] = WIIPROTO_REQ_IR1;
282 wiiproto_keep_rumble(wdata, &cmd[1]);
283 wiimote_queue(wdata, cmd, sizeof(cmd));
286 static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
290 cmd[0] = WIIPROTO_REQ_IR2;
293 wiiproto_keep_rumble(wdata, &cmd[1]);
294 wiimote_queue(wdata, cmd, sizeof(cmd));
297 #define wiiproto_req_wreg(wdata, os, buf, sz) \
298 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
300 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
301 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
303 static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
304 __u32 offset, const __u8 *buf, __u8 size)
308 if (size > 16 || size == 0) {
309 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
313 memset(cmd, 0, sizeof(cmd));
314 cmd[0] = WIIPROTO_REQ_WMEM;
315 cmd[2] = (offset >> 16) & 0xff;
316 cmd[3] = (offset >> 8) & 0xff;
317 cmd[4] = offset & 0xff;
319 memcpy(&cmd[6], buf, size);
324 wiiproto_keep_rumble(wdata, &cmd[1]);
325 wiimote_queue(wdata, cmd, sizeof(cmd));
328 void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
334 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
338 cmd[0] = WIIPROTO_REQ_RMEM;
340 cmd[2] = (offset >> 16) & 0xff;
341 cmd[3] = (offset >> 8) & 0xff;
342 cmd[4] = offset & 0xff;
343 cmd[5] = (size >> 8) & 0xff;
344 cmd[6] = size & 0xff;
349 wiiproto_keep_rumble(wdata, &cmd[1]);
350 wiimote_queue(wdata, cmd, sizeof(cmd));
353 /* requries the cmd-mutex to be held */
354 int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
355 const __u8 *wmem, __u8 size)
360 spin_lock_irqsave(&wdata->state.lock, flags);
361 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
362 wiiproto_req_wreg(wdata, offset, wmem, size);
363 spin_unlock_irqrestore(&wdata->state.lock, flags);
365 ret = wiimote_cmd_wait(wdata);
366 if (!ret && wdata->state.cmd_err)
372 /* requries the cmd-mutex to be held */
373 ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
379 spin_lock_irqsave(&wdata->state.lock, flags);
380 wdata->state.cmd_read_size = size;
381 wdata->state.cmd_read_buf = rmem;
382 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
383 wiiproto_req_rreg(wdata, offset, size);
384 spin_unlock_irqrestore(&wdata->state.lock, flags);
386 ret = wiimote_cmd_wait(wdata);
388 spin_lock_irqsave(&wdata->state.lock, flags);
389 wdata->state.cmd_read_buf = NULL;
390 spin_unlock_irqrestore(&wdata->state.lock, flags);
393 if (wdata->state.cmd_read_size == 0)
396 ret = wdata->state.cmd_read_size;
402 static int wiimote_battery_get_property(struct power_supply *psy,
403 enum power_supply_property psp,
404 union power_supply_propval *val)
406 struct wiimote_data *wdata = container_of(psy,
407 struct wiimote_data, battery);
411 if (psp == POWER_SUPPLY_PROP_SCOPE) {
412 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
416 ret = wiimote_cmd_acquire(wdata);
420 spin_lock_irqsave(&wdata->state.lock, flags);
421 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
422 wiiproto_req_status(wdata);
423 spin_unlock_irqrestore(&wdata->state.lock, flags);
425 ret = wiimote_cmd_wait(wdata);
426 state = wdata->state.cmd_battery;
427 wiimote_cmd_release(wdata);
433 case POWER_SUPPLY_PROP_CAPACITY:
434 val->intval = state * 100 / 255;
444 static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
449 static const __u8 data_enable[] = { 0x01 };
450 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
451 0x00, 0xaa, 0x00, 0x64 };
452 static const __u8 data_sens2[] = { 0x63, 0x03 };
453 static const __u8 data_fin[] = { 0x08 };
455 spin_lock_irqsave(&wdata->state.lock, flags);
457 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
458 spin_unlock_irqrestore(&wdata->state.lock, flags);
463 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
464 wiiproto_req_ir1(wdata, 0);
465 wiiproto_req_ir2(wdata, 0);
466 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
467 spin_unlock_irqrestore(&wdata->state.lock, flags);
471 spin_unlock_irqrestore(&wdata->state.lock, flags);
473 ret = wiimote_cmd_acquire(wdata);
477 /* send PIXEL CLOCK ENABLE cmd first */
478 spin_lock_irqsave(&wdata->state.lock, flags);
479 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
480 wiiproto_req_ir1(wdata, 0x06);
481 spin_unlock_irqrestore(&wdata->state.lock, flags);
483 ret = wiimote_cmd_wait(wdata);
486 if (wdata->state.cmd_err) {
491 /* enable IR LOGIC */
492 spin_lock_irqsave(&wdata->state.lock, flags);
493 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
494 wiiproto_req_ir2(wdata, 0x06);
495 spin_unlock_irqrestore(&wdata->state.lock, flags);
497 ret = wiimote_cmd_wait(wdata);
500 if (wdata->state.cmd_err) {
505 /* enable IR cam but do not make it send data, yet */
506 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
507 sizeof(data_enable));
511 /* write first sensitivity block */
512 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
517 /* write second sensitivity block */
518 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
523 /* put IR cam into desired state */
525 case WIIPROTO_FLAG_IR_FULL:
528 case WIIPROTO_FLAG_IR_EXT:
531 case WIIPROTO_FLAG_IR_BASIC:
535 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
539 /* make IR cam send data */
540 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
544 /* request new DRM mode compatible to IR mode */
545 spin_lock_irqsave(&wdata->state.lock, flags);
546 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
547 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
548 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
549 spin_unlock_irqrestore(&wdata->state.lock, flags);
552 wiimote_cmd_release(wdata);
556 static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
558 struct wiimote_data *wdata;
559 struct device *dev = led_dev->dev->parent;
564 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
566 for (i = 0; i < 4; ++i) {
567 if (wdata->leds[i] == led_dev) {
568 spin_lock_irqsave(&wdata->state.lock, flags);
569 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
570 spin_unlock_irqrestore(&wdata->state.lock, flags);
575 return value ? LED_FULL : LED_OFF;
578 static void wiimote_leds_set(struct led_classdev *led_dev,
579 enum led_brightness value)
581 struct wiimote_data *wdata;
582 struct device *dev = led_dev->dev->parent;
587 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
589 for (i = 0; i < 4; ++i) {
590 if (wdata->leds[i] == led_dev) {
591 flag = WIIPROTO_FLAG_LED(i + 1);
592 spin_lock_irqsave(&wdata->state.lock, flags);
593 state = wdata->state.flags;
594 if (value == LED_OFF)
595 wiiproto_req_leds(wdata, state & ~flag);
597 wiiproto_req_leds(wdata, state | flag);
598 spin_unlock_irqrestore(&wdata->state.lock, flags);
604 static int wiimote_ff_play(struct input_dev *dev, void *data,
605 struct ff_effect *eff)
607 struct wiimote_data *wdata = input_get_drvdata(dev);
612 * The wiimote supports only a single rumble motor so if any magnitude
613 * is set to non-zero then we start the rumble motor. If both are set to
614 * zero, we stop the rumble motor.
617 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
622 spin_lock_irqsave(&wdata->state.lock, flags);
623 wiiproto_req_rumble(wdata, value);
624 spin_unlock_irqrestore(&wdata->state.lock, flags);
629 static int wiimote_input_open(struct input_dev *dev)
631 struct wiimote_data *wdata = input_get_drvdata(dev);
633 return hid_hw_open(wdata->hdev);
636 static void wiimote_input_close(struct input_dev *dev)
638 struct wiimote_data *wdata = input_get_drvdata(dev);
640 hid_hw_close(wdata->hdev);
643 static int wiimote_accel_open(struct input_dev *dev)
645 struct wiimote_data *wdata = input_get_drvdata(dev);
649 ret = hid_hw_open(wdata->hdev);
653 spin_lock_irqsave(&wdata->state.lock, flags);
654 wiiproto_req_accel(wdata, true);
655 spin_unlock_irqrestore(&wdata->state.lock, flags);
660 static void wiimote_accel_close(struct input_dev *dev)
662 struct wiimote_data *wdata = input_get_drvdata(dev);
665 spin_lock_irqsave(&wdata->state.lock, flags);
666 wiiproto_req_accel(wdata, false);
667 spin_unlock_irqrestore(&wdata->state.lock, flags);
669 hid_hw_close(wdata->hdev);
672 static int wiimote_ir_open(struct input_dev *dev)
674 struct wiimote_data *wdata = input_get_drvdata(dev);
677 ret = hid_hw_open(wdata->hdev);
681 ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
683 hid_hw_close(wdata->hdev);
690 static void wiimote_ir_close(struct input_dev *dev)
692 struct wiimote_data *wdata = input_get_drvdata(dev);
694 wiimote_init_ir(wdata, 0);
695 hid_hw_close(wdata->hdev);
698 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
700 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
701 !!(payload[0] & 0x01));
702 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
703 !!(payload[0] & 0x02));
704 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
705 !!(payload[0] & 0x04));
706 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
707 !!(payload[0] & 0x08));
708 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
709 !!(payload[0] & 0x10));
710 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
711 !!(payload[1] & 0x01));
712 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
713 !!(payload[1] & 0x02));
714 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
715 !!(payload[1] & 0x04));
716 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
717 !!(payload[1] & 0x08));
718 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
719 !!(payload[1] & 0x10));
720 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
721 !!(payload[1] & 0x80));
722 input_sync(wdata->input);
725 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
729 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
733 * payload is: BB BB XX YY ZZ
734 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
735 * contain the upper 8 bits of each value. The lower 2 bits are
736 * contained in the buttons data BB BB.
737 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
738 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
739 * accel value and bit 6 is the second bit of the Z value.
740 * The first bit of Y and Z values is not available and always set to 0.
741 * 0x200 is returned on no movement.
748 x |= (payload[0] >> 5) & 0x3;
749 y |= (payload[1] >> 4) & 0x2;
750 z |= (payload[1] >> 5) & 0x2;
752 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
753 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
754 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
755 input_sync(wdata->accel);
758 #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
759 ABS_HAT0X, ABS_HAT0Y)
760 #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
761 ABS_HAT1X, ABS_HAT1Y)
762 #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
763 ABS_HAT2X, ABS_HAT2Y)
764 #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
765 ABS_HAT3X, ABS_HAT3Y)
767 static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
768 bool packed, __u8 xid, __u8 yid)
772 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
776 * Basic IR data is encoded into 3 bytes. The first two bytes are the
777 * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
779 * If data is packed, then the 3rd byte is put first and slightly
780 * reordered. This allows to interleave packed and non-packed data to
781 * have two IR sets in 5 bytes instead of 6.
782 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
786 x = ir[1] | ((ir[0] & 0x03) << 8);
787 y = ir[2] | ((ir[0] & 0x0c) << 6);
789 x = ir[0] | ((ir[2] & 0x30) << 4);
790 y = ir[1] | ((ir[2] & 0xc0) << 2);
793 input_report_abs(wdata->ir, xid, x);
794 input_report_abs(wdata->ir, yid, y);
797 /* reduced status report with "BB BB" key data only */
798 static void handler_status_K(struct wiimote_data *wdata,
801 handler_keys(wdata, payload);
803 /* on status reports the drm is reset so we need to resend the drm */
804 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
807 /* extended status report with "BB BB LF 00 00 VV" data */
808 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
810 handler_status_K(wdata, payload);
812 wiiext_event(wdata, payload[2] & 0x02);
814 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) {
815 wdata->state.cmd_battery = payload[5];
816 wiimote_cmd_complete(wdata);
820 /* reduced generic report with "BB BB" key data only */
821 static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
823 handler_keys(wdata, payload);
826 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
828 __u16 offset = payload[3] << 8 | payload[4];
829 __u8 size = (payload[2] >> 4) + 1;
830 __u8 err = payload[2] & 0x0f;
832 handler_keys(wdata, payload);
834 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
837 else if (size > wdata->state.cmd_read_size)
838 size = wdata->state.cmd_read_size;
840 wdata->state.cmd_read_size = size;
841 if (wdata->state.cmd_read_buf)
842 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
843 wiimote_cmd_complete(wdata);
847 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
849 __u8 err = payload[3];
850 __u8 cmd = payload[2];
852 handler_keys(wdata, payload);
854 if (wiimote_cmd_pending(wdata, cmd, 0)) {
855 wdata->state.cmd_err = err;
856 wiimote_cmd_complete(wdata);
858 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
863 static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
865 handler_keys(wdata, payload);
866 handler_accel(wdata, payload);
869 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
871 handler_keys(wdata, payload);
872 wiiext_handle(wdata, &payload[2]);
875 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
877 handler_keys(wdata, payload);
878 handler_accel(wdata, payload);
879 ir_to_input0(wdata, &payload[5], false);
880 ir_to_input1(wdata, &payload[8], false);
881 ir_to_input2(wdata, &payload[11], false);
882 ir_to_input3(wdata, &payload[14], false);
883 input_sync(wdata->ir);
886 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
888 handler_keys(wdata, payload);
889 wiiext_handle(wdata, &payload[2]);
892 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
894 handler_keys(wdata, payload);
895 ir_to_input0(wdata, &payload[2], false);
896 ir_to_input1(wdata, &payload[4], true);
897 ir_to_input2(wdata, &payload[7], false);
898 ir_to_input3(wdata, &payload[9], true);
899 input_sync(wdata->ir);
900 wiiext_handle(wdata, &payload[12]);
903 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
905 handler_keys(wdata, payload);
906 handler_accel(wdata, payload);
907 wiiext_handle(wdata, &payload[5]);
910 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
912 handler_keys(wdata, payload);
913 handler_accel(wdata, payload);
914 ir_to_input0(wdata, &payload[5], false);
915 ir_to_input1(wdata, &payload[7], true);
916 ir_to_input2(wdata, &payload[10], false);
917 ir_to_input3(wdata, &payload[12], true);
918 input_sync(wdata->ir);
919 wiiext_handle(wdata, &payload[15]);
922 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
924 wiiext_handle(wdata, payload);
927 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
929 handler_keys(wdata, payload);
931 wdata->state.accel_split[0] = payload[2];
932 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
933 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
935 ir_to_input0(wdata, &payload[3], false);
936 ir_to_input1(wdata, &payload[12], false);
937 input_sync(wdata->ir);
940 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
944 handler_keys(wdata, payload);
946 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
947 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
951 buf[2] = wdata->state.accel_split[0];
953 buf[4] = wdata->state.accel_split[1];
954 handler_accel(wdata, buf);
956 ir_to_input2(wdata, &payload[3], false);
957 ir_to_input3(wdata, &payload[12], false);
958 input_sync(wdata->ir);
961 struct wiiproto_handler {
964 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
967 static struct wiiproto_handler handlers[] = {
968 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
969 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
970 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
971 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
972 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
973 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
974 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
975 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
976 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
977 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
978 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
979 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
980 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
981 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
982 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
983 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
984 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
985 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
986 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
987 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
988 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
989 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
990 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
991 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
995 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
996 u8 *raw_data, int size)
998 struct wiimote_data *wdata = hid_get_drvdata(hdev);
999 struct wiiproto_handler *h;
1001 unsigned long flags;
1006 spin_lock_irqsave(&wdata->state.lock, flags);
1008 for (i = 0; handlers[i].id; ++i) {
1010 if (h->id == raw_data[0] && h->size < size) {
1011 h->func(wdata, &raw_data[1]);
1016 if (!handlers[i].id)
1017 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1020 spin_unlock_irqrestore(&wdata->state.lock, flags);
1025 static void wiimote_leds_destroy(struct wiimote_data *wdata)
1028 struct led_classdev *led;
1030 for (i = 0; i < 4; ++i) {
1031 if (wdata->leds[i]) {
1032 led = wdata->leds[i];
1033 wdata->leds[i] = NULL;
1034 led_classdev_unregister(led);
1040 static int wiimote_leds_create(struct wiimote_data *wdata)
1043 struct device *dev = &wdata->hdev->dev;
1044 size_t namesz = strlen(dev_name(dev)) + 9;
1045 struct led_classdev *led;
1048 for (i = 0; i < 4; ++i) {
1049 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1054 name = (void*)&led[1];
1055 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1057 led->brightness = 0;
1058 led->max_brightness = 1;
1059 led->brightness_get = wiimote_leds_get;
1060 led->brightness_set = wiimote_leds_set;
1062 ret = led_classdev_register(dev, led);
1067 wdata->leds[i] = led;
1073 wiimote_leds_destroy(wdata);
1077 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1079 struct wiimote_data *wdata;
1082 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1086 wdata->input = input_allocate_device();
1091 hid_set_drvdata(hdev, wdata);
1093 input_set_drvdata(wdata->input, wdata);
1094 wdata->input->open = wiimote_input_open;
1095 wdata->input->close = wiimote_input_close;
1096 wdata->input->dev.parent = &wdata->hdev->dev;
1097 wdata->input->id.bustype = wdata->hdev->bus;
1098 wdata->input->id.vendor = wdata->hdev->vendor;
1099 wdata->input->id.product = wdata->hdev->product;
1100 wdata->input->id.version = wdata->hdev->version;
1101 wdata->input->name = WIIMOTE_NAME;
1103 set_bit(EV_KEY, wdata->input->evbit);
1104 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1105 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1107 set_bit(FF_RUMBLE, wdata->input->ffbit);
1108 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1111 wdata->accel = input_allocate_device();
1115 input_set_drvdata(wdata->accel, wdata);
1116 wdata->accel->open = wiimote_accel_open;
1117 wdata->accel->close = wiimote_accel_close;
1118 wdata->accel->dev.parent = &wdata->hdev->dev;
1119 wdata->accel->id.bustype = wdata->hdev->bus;
1120 wdata->accel->id.vendor = wdata->hdev->vendor;
1121 wdata->accel->id.product = wdata->hdev->product;
1122 wdata->accel->id.version = wdata->hdev->version;
1123 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1125 set_bit(EV_ABS, wdata->accel->evbit);
1126 set_bit(ABS_RX, wdata->accel->absbit);
1127 set_bit(ABS_RY, wdata->accel->absbit);
1128 set_bit(ABS_RZ, wdata->accel->absbit);
1129 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1130 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1131 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
1133 wdata->ir = input_allocate_device();
1137 input_set_drvdata(wdata->ir, wdata);
1138 wdata->ir->open = wiimote_ir_open;
1139 wdata->ir->close = wiimote_ir_close;
1140 wdata->ir->dev.parent = &wdata->hdev->dev;
1141 wdata->ir->id.bustype = wdata->hdev->bus;
1142 wdata->ir->id.vendor = wdata->hdev->vendor;
1143 wdata->ir->id.product = wdata->hdev->product;
1144 wdata->ir->id.version = wdata->hdev->version;
1145 wdata->ir->name = WIIMOTE_NAME " IR";
1147 set_bit(EV_ABS, wdata->ir->evbit);
1148 set_bit(ABS_HAT0X, wdata->ir->absbit);
1149 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1150 set_bit(ABS_HAT1X, wdata->ir->absbit);
1151 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1152 set_bit(ABS_HAT2X, wdata->ir->absbit);
1153 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1154 set_bit(ABS_HAT3X, wdata->ir->absbit);
1155 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1156 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1157 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1158 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1159 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1160 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1161 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1162 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1163 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1165 spin_lock_init(&wdata->queue.lock);
1166 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
1168 spin_lock_init(&wdata->state.lock);
1169 init_completion(&wdata->state.ready);
1170 mutex_init(&wdata->state.sync);
1171 wdata->state.drm = WIIPROTO_REQ_DRM_K;
1176 input_free_device(wdata->accel);
1178 input_free_device(wdata->input);
1184 static void wiimote_destroy(struct wiimote_data *wdata)
1186 wiidebug_deinit(wdata);
1187 wiiext_deinit(wdata);
1188 wiimote_leds_destroy(wdata);
1190 power_supply_unregister(&wdata->battery);
1191 kfree(wdata->battery.name);
1192 input_unregister_device(wdata->accel);
1193 input_unregister_device(wdata->ir);
1194 input_unregister_device(wdata->input);
1195 cancel_work_sync(&wdata->queue.worker);
1196 hid_hw_stop(wdata->hdev);
1201 static int wiimote_hid_probe(struct hid_device *hdev,
1202 const struct hid_device_id *id)
1204 struct wiimote_data *wdata;
1207 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1209 wdata = wiimote_create(hdev);
1211 hid_err(hdev, "Can't alloc device\n");
1215 ret = hid_parse(hdev);
1217 hid_err(hdev, "HID parse failed\n");
1221 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1223 hid_err(hdev, "HW start failed\n");
1227 ret = input_register_device(wdata->accel);
1229 hid_err(hdev, "Cannot register input device\n");
1233 ret = input_register_device(wdata->ir);
1235 hid_err(hdev, "Cannot register input device\n");
1239 ret = input_register_device(wdata->input);
1241 hid_err(hdev, "Cannot register input device\n");
1245 wdata->battery.properties = wiimote_battery_props;
1246 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1247 wdata->battery.get_property = wiimote_battery_get_property;
1248 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1249 wdata->battery.use_for_apm = 0;
1250 wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
1252 if (!wdata->battery.name) {
1254 goto err_battery_name;
1257 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1259 hid_err(hdev, "Cannot register battery device\n");
1263 power_supply_powers(&wdata->battery, &hdev->dev);
1265 ret = wiimote_leds_create(wdata);
1269 ret = wiiext_init(wdata);
1273 ret = wiidebug_init(wdata);
1277 hid_info(hdev, "New device registered\n");
1279 /* by default set led1 after device initialization */
1280 spin_lock_irq(&wdata->state.lock);
1281 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
1282 spin_unlock_irq(&wdata->state.lock);
1287 wiimote_destroy(wdata);
1291 kfree(wdata->battery.name);
1293 input_unregister_device(wdata->input);
1294 wdata->input = NULL;
1296 input_unregister_device(wdata->ir);
1299 input_unregister_device(wdata->accel);
1300 wdata->accel = NULL;
1304 input_free_device(wdata->ir);
1305 input_free_device(wdata->accel);
1306 input_free_device(wdata->input);
1311 static void wiimote_hid_remove(struct hid_device *hdev)
1313 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1315 hid_info(hdev, "Device removed\n");
1316 wiimote_destroy(wdata);
1319 static const struct hid_device_id wiimote_hid_devices[] = {
1320 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1321 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1322 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1323 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
1326 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1328 static struct hid_driver wiimote_hid_driver = {
1330 .id_table = wiimote_hid_devices,
1331 .probe = wiimote_hid_probe,
1332 .remove = wiimote_hid_remove,
1333 .raw_event = wiimote_hid_event,
1335 module_hid_driver(wiimote_hid_driver);
1337 MODULE_LICENSE("GPL");
1338 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1339 MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");