2 * HID driver for Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
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 static ssize_t
wiimote_hid_send(struct hid_device
*hdev
, __u8
*buffer
,
65 if (!hdev
->hid_output_raw_report
)
68 buf
= kmemdup(buffer
, count
, GFP_KERNEL
);
72 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
78 static void wiimote_worker(struct work_struct
*work
)
80 struct wiimote_data
*wdata
= container_of(work
, struct wiimote_data
,
84 spin_lock_irqsave(&wdata
->qlock
, flags
);
86 while (wdata
->head
!= wdata
->tail
) {
87 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
88 wiimote_hid_send(wdata
->hdev
, wdata
->outq
[wdata
->tail
].data
,
89 wdata
->outq
[wdata
->tail
].size
);
90 spin_lock_irqsave(&wdata
->qlock
, flags
);
92 wdata
->tail
= (wdata
->tail
+ 1) % WIIMOTE_BUFSIZE
;
95 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
98 static void wiimote_queue(struct wiimote_data
*wdata
, const __u8
*buffer
,
104 if (count
> HID_MAX_BUFFER_SIZE
) {
105 hid_warn(wdata
->hdev
, "Sending too large output report\n");
110 * Copy new request into our output queue and check whether the
111 * queue is full. If it is full, discard this request.
112 * If it is empty we need to start a new worker that will
113 * send out the buffer to the hid device.
114 * If the queue is not empty, then there must be a worker
115 * that is currently sending out our buffer and this worker
116 * will reschedule itself until the queue is empty.
119 spin_lock_irqsave(&wdata
->qlock
, flags
);
121 memcpy(wdata
->outq
[wdata
->head
].data
, buffer
, count
);
122 wdata
->outq
[wdata
->head
].size
= count
;
123 newhead
= (wdata
->head
+ 1) % WIIMOTE_BUFSIZE
;
125 if (wdata
->head
== wdata
->tail
) {
126 wdata
->head
= newhead
;
127 schedule_work(&wdata
->worker
);
128 } else if (newhead
!= wdata
->tail
) {
129 wdata
->head
= newhead
;
131 hid_warn(wdata
->hdev
, "Output queue is full");
134 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
138 * This sets the rumble bit on the given output report if rumble is
140 * \cmd1 must point to the second byte in the output report => &cmd[1]
141 * This must be called on nearly every output report before passing it
142 * into the output queue!
144 static inline void wiiproto_keep_rumble(struct wiimote_data
*wdata
, __u8
*cmd1
)
146 if (wdata
->state
.flags
& WIIPROTO_FLAG_RUMBLE
)
150 static void wiiproto_req_rumble(struct wiimote_data
*wdata
, __u8 rumble
)
155 if (rumble
== !!(wdata
->state
.flags
& WIIPROTO_FLAG_RUMBLE
))
159 wdata
->state
.flags
|= WIIPROTO_FLAG_RUMBLE
;
161 wdata
->state
.flags
&= ~WIIPROTO_FLAG_RUMBLE
;
163 cmd
[0] = WIIPROTO_REQ_RUMBLE
;
166 wiiproto_keep_rumble(wdata
, &cmd
[1]);
167 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
170 static void wiiproto_req_leds(struct wiimote_data
*wdata
, int leds
)
174 leds
&= WIIPROTO_FLAGS_LEDS
;
175 if ((wdata
->state
.flags
& WIIPROTO_FLAGS_LEDS
) == leds
)
177 wdata
->state
.flags
= (wdata
->state
.flags
& ~WIIPROTO_FLAGS_LEDS
) | leds
;
179 cmd
[0] = WIIPROTO_REQ_LED
;
182 if (leds
& WIIPROTO_FLAG_LED1
)
184 if (leds
& WIIPROTO_FLAG_LED2
)
186 if (leds
& WIIPROTO_FLAG_LED3
)
188 if (leds
& WIIPROTO_FLAG_LED4
)
191 wiiproto_keep_rumble(wdata
, &cmd
[1]);
192 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
196 * Check what peripherals of the wiimote are currently
197 * active and select a proper DRM that supports all of
198 * the requested data inputs.
200 static __u8
select_drm(struct wiimote_data
*wdata
)
202 __u8 ir
= wdata
->state
.flags
& WIIPROTO_FLAGS_IR
;
203 bool ext
= wiiext_active(wdata
);
205 if (ir
== WIIPROTO_FLAG_IR_BASIC
) {
206 if (wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
)
207 return WIIPROTO_REQ_DRM_KAIE
;
209 return WIIPROTO_REQ_DRM_KIE
;
210 } else if (ir
== WIIPROTO_FLAG_IR_EXT
) {
211 return WIIPROTO_REQ_DRM_KAI
;
212 } else if (ir
== WIIPROTO_FLAG_IR_FULL
) {
213 return WIIPROTO_REQ_DRM_SKAI1
;
215 if (wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
) {
217 return WIIPROTO_REQ_DRM_KAE
;
219 return WIIPROTO_REQ_DRM_KA
;
222 return WIIPROTO_REQ_DRM_KE
;
224 return WIIPROTO_REQ_DRM_K
;
229 void wiiproto_req_drm(struct wiimote_data
*wdata
, __u8 drm
)
233 if (drm
== WIIPROTO_REQ_NULL
)
234 drm
= select_drm(wdata
);
236 cmd
[0] = WIIPROTO_REQ_DRM
;
240 wdata
->state
.drm
= drm
;
241 wiiproto_keep_rumble(wdata
, &cmd
[1]);
242 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
245 static void wiiproto_req_status(struct wiimote_data
*wdata
)
249 cmd
[0] = WIIPROTO_REQ_SREQ
;
252 wiiproto_keep_rumble(wdata
, &cmd
[1]);
253 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
256 static void wiiproto_req_accel(struct wiimote_data
*wdata
, __u8 accel
)
259 if (accel
== !!(wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
))
263 wdata
->state
.flags
|= WIIPROTO_FLAG_ACCEL
;
265 wdata
->state
.flags
&= ~WIIPROTO_FLAG_ACCEL
;
267 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
270 static void wiiproto_req_ir1(struct wiimote_data
*wdata
, __u8 flags
)
274 cmd
[0] = WIIPROTO_REQ_IR1
;
277 wiiproto_keep_rumble(wdata
, &cmd
[1]);
278 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
281 static void wiiproto_req_ir2(struct wiimote_data
*wdata
, __u8 flags
)
285 cmd
[0] = WIIPROTO_REQ_IR2
;
288 wiiproto_keep_rumble(wdata
, &cmd
[1]);
289 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
292 #define wiiproto_req_wreg(wdata, os, buf, sz) \
293 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
295 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
296 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
298 static void wiiproto_req_wmem(struct wiimote_data
*wdata
, bool eeprom
,
299 __u32 offset
, const __u8
*buf
, __u8 size
)
303 if (size
> 16 || size
== 0) {
304 hid_warn(wdata
->hdev
, "Invalid length %d wmem request\n", size
);
308 memset(cmd
, 0, sizeof(cmd
));
309 cmd
[0] = WIIPROTO_REQ_WMEM
;
310 cmd
[2] = (offset
>> 16) & 0xff;
311 cmd
[3] = (offset
>> 8) & 0xff;
312 cmd
[4] = offset
& 0xff;
314 memcpy(&cmd
[6], buf
, size
);
319 wiiproto_keep_rumble(wdata
, &cmd
[1]);
320 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
323 void wiiproto_req_rmem(struct wiimote_data
*wdata
, bool eeprom
, __u32 offset
,
329 hid_warn(wdata
->hdev
, "Invalid length %d rmem request\n", size
);
333 cmd
[0] = WIIPROTO_REQ_RMEM
;
335 cmd
[2] = (offset
>> 16) & 0xff;
336 cmd
[3] = (offset
>> 8) & 0xff;
337 cmd
[4] = offset
& 0xff;
338 cmd
[5] = (size
>> 8) & 0xff;
339 cmd
[6] = size
& 0xff;
344 wiiproto_keep_rumble(wdata
, &cmd
[1]);
345 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
348 /* requries the cmd-mutex to be held */
349 int wiimote_cmd_write(struct wiimote_data
*wdata
, __u32 offset
,
350 const __u8
*wmem
, __u8 size
)
355 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
356 wiimote_cmd_set(wdata
, WIIPROTO_REQ_WMEM
, 0);
357 wiiproto_req_wreg(wdata
, offset
, wmem
, size
);
358 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
360 ret
= wiimote_cmd_wait(wdata
);
361 if (!ret
&& wdata
->state
.cmd_err
)
367 /* requries the cmd-mutex to be held */
368 ssize_t
wiimote_cmd_read(struct wiimote_data
*wdata
, __u32 offset
, __u8
*rmem
,
374 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
375 wdata
->state
.cmd_read_size
= size
;
376 wdata
->state
.cmd_read_buf
= rmem
;
377 wiimote_cmd_set(wdata
, WIIPROTO_REQ_RMEM
, offset
& 0xffff);
378 wiiproto_req_rreg(wdata
, offset
, size
);
379 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
381 ret
= wiimote_cmd_wait(wdata
);
383 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
384 wdata
->state
.cmd_read_buf
= NULL
;
385 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
388 if (wdata
->state
.cmd_read_size
== 0)
391 ret
= wdata
->state
.cmd_read_size
;
397 static int wiimote_battery_get_property(struct power_supply
*psy
,
398 enum power_supply_property psp
,
399 union power_supply_propval
*val
)
401 struct wiimote_data
*wdata
= container_of(psy
,
402 struct wiimote_data
, battery
);
406 if (psp
== POWER_SUPPLY_PROP_SCOPE
) {
407 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
411 ret
= wiimote_cmd_acquire(wdata
);
415 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
416 wiimote_cmd_set(wdata
, WIIPROTO_REQ_SREQ
, 0);
417 wiiproto_req_status(wdata
);
418 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
420 ret
= wiimote_cmd_wait(wdata
);
421 state
= wdata
->state
.cmd_battery
;
422 wiimote_cmd_release(wdata
);
428 case POWER_SUPPLY_PROP_CAPACITY
:
429 val
->intval
= state
* 100 / 255;
439 static int wiimote_init_ir(struct wiimote_data
*wdata
, __u16 mode
)
444 static const __u8 data_enable
[] = { 0x01 };
445 static const __u8 data_sens1
[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
446 0x00, 0xaa, 0x00, 0x64 };
447 static const __u8 data_sens2
[] = { 0x63, 0x03 };
448 static const __u8 data_fin
[] = { 0x08 };
450 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
452 if (mode
== (wdata
->state
.flags
& WIIPROTO_FLAGS_IR
)) {
453 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
458 wdata
->state
.flags
&= ~WIIPROTO_FLAGS_IR
;
459 wiiproto_req_ir1(wdata
, 0);
460 wiiproto_req_ir2(wdata
, 0);
461 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
462 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
466 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
468 ret
= wiimote_cmd_acquire(wdata
);
472 /* send PIXEL CLOCK ENABLE cmd first */
473 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
474 wiimote_cmd_set(wdata
, WIIPROTO_REQ_IR1
, 0);
475 wiiproto_req_ir1(wdata
, 0x06);
476 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
478 ret
= wiimote_cmd_wait(wdata
);
481 if (wdata
->state
.cmd_err
) {
486 /* enable IR LOGIC */
487 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
488 wiimote_cmd_set(wdata
, WIIPROTO_REQ_IR2
, 0);
489 wiiproto_req_ir2(wdata
, 0x06);
490 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
492 ret
= wiimote_cmd_wait(wdata
);
495 if (wdata
->state
.cmd_err
) {
500 /* enable IR cam but do not make it send data, yet */
501 ret
= wiimote_cmd_write(wdata
, 0xb00030, data_enable
,
502 sizeof(data_enable
));
506 /* write first sensitivity block */
507 ret
= wiimote_cmd_write(wdata
, 0xb00000, data_sens1
,
512 /* write second sensitivity block */
513 ret
= wiimote_cmd_write(wdata
, 0xb0001a, data_sens2
,
518 /* put IR cam into desired state */
520 case WIIPROTO_FLAG_IR_FULL
:
523 case WIIPROTO_FLAG_IR_EXT
:
526 case WIIPROTO_FLAG_IR_BASIC
:
530 ret
= wiimote_cmd_write(wdata
, 0xb00033, &format
, sizeof(format
));
534 /* make IR cam send data */
535 ret
= wiimote_cmd_write(wdata
, 0xb00030, data_fin
, sizeof(data_fin
));
539 /* request new DRM mode compatible to IR mode */
540 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
541 wdata
->state
.flags
&= ~WIIPROTO_FLAGS_IR
;
542 wdata
->state
.flags
|= mode
& WIIPROTO_FLAGS_IR
;
543 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
544 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
547 wiimote_cmd_release(wdata
);
551 static enum led_brightness
wiimote_leds_get(struct led_classdev
*led_dev
)
553 struct wiimote_data
*wdata
;
554 struct device
*dev
= led_dev
->dev
->parent
;
559 wdata
= hid_get_drvdata(container_of(dev
, struct hid_device
, dev
));
561 for (i
= 0; i
< 4; ++i
) {
562 if (wdata
->leds
[i
] == led_dev
) {
563 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
564 value
= wdata
->state
.flags
& WIIPROTO_FLAG_LED(i
+ 1);
565 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
570 return value
? LED_FULL
: LED_OFF
;
573 static void wiimote_leds_set(struct led_classdev
*led_dev
,
574 enum led_brightness value
)
576 struct wiimote_data
*wdata
;
577 struct device
*dev
= led_dev
->dev
->parent
;
582 wdata
= hid_get_drvdata(container_of(dev
, struct hid_device
, dev
));
584 for (i
= 0; i
< 4; ++i
) {
585 if (wdata
->leds
[i
] == led_dev
) {
586 flag
= WIIPROTO_FLAG_LED(i
+ 1);
587 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
588 state
= wdata
->state
.flags
;
589 if (value
== LED_OFF
)
590 wiiproto_req_leds(wdata
, state
& ~flag
);
592 wiiproto_req_leds(wdata
, state
| flag
);
593 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
599 static int wiimote_ff_play(struct input_dev
*dev
, void *data
,
600 struct ff_effect
*eff
)
602 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
607 * The wiimote supports only a single rumble motor so if any magnitude
608 * is set to non-zero then we start the rumble motor. If both are set to
609 * zero, we stop the rumble motor.
612 if (eff
->u
.rumble
.strong_magnitude
|| eff
->u
.rumble
.weak_magnitude
)
617 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
618 wiiproto_req_rumble(wdata
, value
);
619 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
624 static int wiimote_input_open(struct input_dev
*dev
)
626 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
628 return hid_hw_open(wdata
->hdev
);
631 static void wiimote_input_close(struct input_dev
*dev
)
633 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
635 hid_hw_close(wdata
->hdev
);
638 static int wiimote_accel_open(struct input_dev
*dev
)
640 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
644 ret
= hid_hw_open(wdata
->hdev
);
648 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
649 wiiproto_req_accel(wdata
, true);
650 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
655 static void wiimote_accel_close(struct input_dev
*dev
)
657 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
660 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
661 wiiproto_req_accel(wdata
, false);
662 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
664 hid_hw_close(wdata
->hdev
);
667 static int wiimote_ir_open(struct input_dev
*dev
)
669 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
672 ret
= hid_hw_open(wdata
->hdev
);
676 ret
= wiimote_init_ir(wdata
, WIIPROTO_FLAG_IR_BASIC
);
678 hid_hw_close(wdata
->hdev
);
685 static void wiimote_ir_close(struct input_dev
*dev
)
687 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
689 wiimote_init_ir(wdata
, 0);
690 hid_hw_close(wdata
->hdev
);
693 static void handler_keys(struct wiimote_data
*wdata
, const __u8
*payload
)
695 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_LEFT
],
696 !!(payload
[0] & 0x01));
697 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_RIGHT
],
698 !!(payload
[0] & 0x02));
699 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_DOWN
],
700 !!(payload
[0] & 0x04));
701 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_UP
],
702 !!(payload
[0] & 0x08));
703 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_PLUS
],
704 !!(payload
[0] & 0x10));
705 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_TWO
],
706 !!(payload
[1] & 0x01));
707 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_ONE
],
708 !!(payload
[1] & 0x02));
709 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_B
],
710 !!(payload
[1] & 0x04));
711 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_A
],
712 !!(payload
[1] & 0x08));
713 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_MINUS
],
714 !!(payload
[1] & 0x10));
715 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_HOME
],
716 !!(payload
[1] & 0x80));
717 input_sync(wdata
->input
);
720 static void handler_accel(struct wiimote_data
*wdata
, const __u8
*payload
)
724 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
))
728 * payload is: BB BB XX YY ZZ
729 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
730 * contain the upper 8 bits of each value. The lower 2 bits are
731 * contained in the buttons data BB BB.
732 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
733 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
734 * accel value and bit 6 is the second bit of the Z value.
735 * The first bit of Y and Z values is not available and always set to 0.
736 * 0x200 is returned on no movement.
743 x
|= (payload
[0] >> 5) & 0x3;
744 y
|= (payload
[1] >> 4) & 0x2;
745 z
|= (payload
[1] >> 5) & 0x2;
747 input_report_abs(wdata
->accel
, ABS_RX
, x
- 0x200);
748 input_report_abs(wdata
->accel
, ABS_RY
, y
- 0x200);
749 input_report_abs(wdata
->accel
, ABS_RZ
, z
- 0x200);
750 input_sync(wdata
->accel
);
753 #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
754 ABS_HAT0X, ABS_HAT0Y)
755 #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
756 ABS_HAT1X, ABS_HAT1Y)
757 #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
758 ABS_HAT2X, ABS_HAT2Y)
759 #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
760 ABS_HAT3X, ABS_HAT3Y)
762 static void __ir_to_input(struct wiimote_data
*wdata
, const __u8
*ir
,
763 bool packed
, __u8 xid
, __u8 yid
)
767 if (!(wdata
->state
.flags
& WIIPROTO_FLAGS_IR
))
771 * Basic IR data is encoded into 3 bytes. The first two bytes are the
772 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
774 * If data is packed, then the 3rd byte is put first and slightly
775 * reordered. This allows to interleave packed and non-packed data to
776 * have two IR sets in 5 bytes instead of 6.
777 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
785 y
|= (ir
[0] >> 2) & 0x3;
790 x
|= (ir
[2] >> 4) & 0x3;
791 y
|= (ir
[2] >> 6) & 0x3;
794 input_report_abs(wdata
->ir
, xid
, x
);
795 input_report_abs(wdata
->ir
, yid
, y
);
798 static void handler_status(struct wiimote_data
*wdata
, const __u8
*payload
)
800 handler_keys(wdata
, payload
);
802 /* on status reports the drm is reset so we need to resend the drm */
803 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
805 wiiext_event(wdata
, payload
[2] & 0x02);
807 if (wiimote_cmd_pending(wdata
, WIIPROTO_REQ_SREQ
, 0)) {
808 wdata
->state
.cmd_battery
= payload
[5];
809 wiimote_cmd_complete(wdata
);
813 static void handler_data(struct wiimote_data
*wdata
, const __u8
*payload
)
815 __u16 offset
= payload
[3] << 8 | payload
[4];
816 __u8 size
= (payload
[2] >> 4) + 1;
817 __u8 err
= payload
[2] & 0x0f;
819 handler_keys(wdata
, payload
);
821 if (wiimote_cmd_pending(wdata
, WIIPROTO_REQ_RMEM
, offset
)) {
824 else if (size
> wdata
->state
.cmd_read_size
)
825 size
= wdata
->state
.cmd_read_size
;
827 wdata
->state
.cmd_read_size
= size
;
828 if (wdata
->state
.cmd_read_buf
)
829 memcpy(wdata
->state
.cmd_read_buf
, &payload
[5], size
);
830 wiimote_cmd_complete(wdata
);
834 static void handler_return(struct wiimote_data
*wdata
, const __u8
*payload
)
836 __u8 err
= payload
[3];
837 __u8 cmd
= payload
[2];
839 handler_keys(wdata
, payload
);
841 if (wiimote_cmd_pending(wdata
, cmd
, 0)) {
842 wdata
->state
.cmd_err
= err
;
843 wiimote_cmd_complete(wdata
);
845 hid_warn(wdata
->hdev
, "Remote error %hhu on req %hhu\n", err
,
850 static void handler_drm_KA(struct wiimote_data
*wdata
, const __u8
*payload
)
852 handler_keys(wdata
, payload
);
853 handler_accel(wdata
, payload
);
856 static void handler_drm_KE(struct wiimote_data
*wdata
, const __u8
*payload
)
858 handler_keys(wdata
, payload
);
859 wiiext_handle(wdata
, &payload
[2]);
862 static void handler_drm_KAI(struct wiimote_data
*wdata
, const __u8
*payload
)
864 handler_keys(wdata
, payload
);
865 handler_accel(wdata
, payload
);
866 ir_to_input0(wdata
, &payload
[5], false);
867 ir_to_input1(wdata
, &payload
[8], false);
868 ir_to_input2(wdata
, &payload
[11], false);
869 ir_to_input3(wdata
, &payload
[14], false);
870 input_sync(wdata
->ir
);
873 static void handler_drm_KEE(struct wiimote_data
*wdata
, const __u8
*payload
)
875 handler_keys(wdata
, payload
);
876 wiiext_handle(wdata
, &payload
[2]);
879 static void handler_drm_KIE(struct wiimote_data
*wdata
, const __u8
*payload
)
881 handler_keys(wdata
, payload
);
882 ir_to_input0(wdata
, &payload
[2], false);
883 ir_to_input1(wdata
, &payload
[4], true);
884 ir_to_input2(wdata
, &payload
[7], false);
885 ir_to_input3(wdata
, &payload
[9], true);
886 input_sync(wdata
->ir
);
887 wiiext_handle(wdata
, &payload
[12]);
890 static void handler_drm_KAE(struct wiimote_data
*wdata
, const __u8
*payload
)
892 handler_keys(wdata
, payload
);
893 handler_accel(wdata
, payload
);
894 wiiext_handle(wdata
, &payload
[5]);
897 static void handler_drm_KAIE(struct wiimote_data
*wdata
, const __u8
*payload
)
899 handler_keys(wdata
, payload
);
900 handler_accel(wdata
, payload
);
901 ir_to_input0(wdata
, &payload
[5], false);
902 ir_to_input1(wdata
, &payload
[7], true);
903 ir_to_input2(wdata
, &payload
[10], false);
904 ir_to_input3(wdata
, &payload
[12], true);
905 input_sync(wdata
->ir
);
906 wiiext_handle(wdata
, &payload
[15]);
909 static void handler_drm_E(struct wiimote_data
*wdata
, const __u8
*payload
)
911 wiiext_handle(wdata
, payload
);
914 static void handler_drm_SKAI1(struct wiimote_data
*wdata
, const __u8
*payload
)
916 handler_keys(wdata
, payload
);
918 wdata
->state
.accel_split
[0] = payload
[2];
919 wdata
->state
.accel_split
[1] = (payload
[0] >> 1) & (0x10 | 0x20);
920 wdata
->state
.accel_split
[1] |= (payload
[1] << 1) & (0x40 | 0x80);
922 ir_to_input0(wdata
, &payload
[3], false);
923 ir_to_input1(wdata
, &payload
[12], false);
924 input_sync(wdata
->ir
);
927 static void handler_drm_SKAI2(struct wiimote_data
*wdata
, const __u8
*payload
)
931 handler_keys(wdata
, payload
);
933 wdata
->state
.accel_split
[1] |= (payload
[0] >> 5) & (0x01 | 0x02);
934 wdata
->state
.accel_split
[1] |= (payload
[1] >> 3) & (0x04 | 0x08);
938 buf
[2] = wdata
->state
.accel_split
[0];
940 buf
[4] = wdata
->state
.accel_split
[1];
941 handler_accel(wdata
, buf
);
943 ir_to_input2(wdata
, &payload
[3], false);
944 ir_to_input3(wdata
, &payload
[12], false);
945 input_sync(wdata
->ir
);
948 struct wiiproto_handler
{
951 void (*func
)(struct wiimote_data
*wdata
, const __u8
*payload
);
954 static struct wiiproto_handler handlers
[] = {
955 { .id
= WIIPROTO_REQ_STATUS
, .size
= 6, .func
= handler_status
},
956 { .id
= WIIPROTO_REQ_DATA
, .size
= 21, .func
= handler_data
},
957 { .id
= WIIPROTO_REQ_RETURN
, .size
= 4, .func
= handler_return
},
958 { .id
= WIIPROTO_REQ_DRM_K
, .size
= 2, .func
= handler_keys
},
959 { .id
= WIIPROTO_REQ_DRM_KA
, .size
= 5, .func
= handler_drm_KA
},
960 { .id
= WIIPROTO_REQ_DRM_KE
, .size
= 10, .func
= handler_drm_KE
},
961 { .id
= WIIPROTO_REQ_DRM_KAI
, .size
= 17, .func
= handler_drm_KAI
},
962 { .id
= WIIPROTO_REQ_DRM_KEE
, .size
= 21, .func
= handler_drm_KEE
},
963 { .id
= WIIPROTO_REQ_DRM_KAE
, .size
= 21, .func
= handler_drm_KAE
},
964 { .id
= WIIPROTO_REQ_DRM_KIE
, .size
= 21, .func
= handler_drm_KIE
},
965 { .id
= WIIPROTO_REQ_DRM_KAIE
, .size
= 21, .func
= handler_drm_KAIE
},
966 { .id
= WIIPROTO_REQ_DRM_E
, .size
= 21, .func
= handler_drm_E
},
967 { .id
= WIIPROTO_REQ_DRM_SKAI1
, .size
= 21, .func
= handler_drm_SKAI1
},
968 { .id
= WIIPROTO_REQ_DRM_SKAI2
, .size
= 21, .func
= handler_drm_SKAI2
},
972 static int wiimote_hid_event(struct hid_device
*hdev
, struct hid_report
*report
,
973 u8
*raw_data
, int size
)
975 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
976 struct wiiproto_handler
*h
;
979 bool handled
= false;
984 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
986 for (i
= 0; handlers
[i
].id
; ++i
) {
988 if (h
->id
== raw_data
[0] && h
->size
< size
) {
989 h
->func(wdata
, &raw_data
[1]);
995 hid_warn(hdev
, "Unhandled report %hhu size %d\n", raw_data
[0],
998 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1003 static void wiimote_leds_destroy(struct wiimote_data
*wdata
)
1006 struct led_classdev
*led
;
1008 for (i
= 0; i
< 4; ++i
) {
1009 if (wdata
->leds
[i
]) {
1010 led
= wdata
->leds
[i
];
1011 wdata
->leds
[i
] = NULL
;
1012 led_classdev_unregister(led
);
1018 static int wiimote_leds_create(struct wiimote_data
*wdata
)
1021 struct device
*dev
= &wdata
->hdev
->dev
;
1022 size_t namesz
= strlen(dev_name(dev
)) + 9;
1023 struct led_classdev
*led
;
1026 for (i
= 0; i
< 4; ++i
) {
1027 led
= kzalloc(sizeof(struct led_classdev
) + namesz
, GFP_KERNEL
);
1032 name
= (void*)&led
[1];
1033 snprintf(name
, namesz
, "%s:blue:p%d", dev_name(dev
), i
);
1035 led
->brightness
= 0;
1036 led
->max_brightness
= 1;
1037 led
->brightness_get
= wiimote_leds_get
;
1038 led
->brightness_set
= wiimote_leds_set
;
1040 ret
= led_classdev_register(dev
, led
);
1045 wdata
->leds
[i
] = led
;
1051 wiimote_leds_destroy(wdata
);
1055 static struct wiimote_data
*wiimote_create(struct hid_device
*hdev
)
1057 struct wiimote_data
*wdata
;
1060 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
1064 wdata
->input
= input_allocate_device();
1069 hid_set_drvdata(hdev
, wdata
);
1071 input_set_drvdata(wdata
->input
, wdata
);
1072 wdata
->input
->open
= wiimote_input_open
;
1073 wdata
->input
->close
= wiimote_input_close
;
1074 wdata
->input
->dev
.parent
= &wdata
->hdev
->dev
;
1075 wdata
->input
->id
.bustype
= wdata
->hdev
->bus
;
1076 wdata
->input
->id
.vendor
= wdata
->hdev
->vendor
;
1077 wdata
->input
->id
.product
= wdata
->hdev
->product
;
1078 wdata
->input
->id
.version
= wdata
->hdev
->version
;
1079 wdata
->input
->name
= WIIMOTE_NAME
;
1081 set_bit(EV_KEY
, wdata
->input
->evbit
);
1082 for (i
= 0; i
< WIIPROTO_KEY_COUNT
; ++i
)
1083 set_bit(wiiproto_keymap
[i
], wdata
->input
->keybit
);
1085 set_bit(FF_RUMBLE
, wdata
->input
->ffbit
);
1086 if (input_ff_create_memless(wdata
->input
, NULL
, wiimote_ff_play
))
1089 wdata
->accel
= input_allocate_device();
1093 input_set_drvdata(wdata
->accel
, wdata
);
1094 wdata
->accel
->open
= wiimote_accel_open
;
1095 wdata
->accel
->close
= wiimote_accel_close
;
1096 wdata
->accel
->dev
.parent
= &wdata
->hdev
->dev
;
1097 wdata
->accel
->id
.bustype
= wdata
->hdev
->bus
;
1098 wdata
->accel
->id
.vendor
= wdata
->hdev
->vendor
;
1099 wdata
->accel
->id
.product
= wdata
->hdev
->product
;
1100 wdata
->accel
->id
.version
= wdata
->hdev
->version
;
1101 wdata
->accel
->name
= WIIMOTE_NAME
" Accelerometer";
1103 set_bit(EV_ABS
, wdata
->accel
->evbit
);
1104 set_bit(ABS_RX
, wdata
->accel
->absbit
);
1105 set_bit(ABS_RY
, wdata
->accel
->absbit
);
1106 set_bit(ABS_RZ
, wdata
->accel
->absbit
);
1107 input_set_abs_params(wdata
->accel
, ABS_RX
, -500, 500, 2, 4);
1108 input_set_abs_params(wdata
->accel
, ABS_RY
, -500, 500, 2, 4);
1109 input_set_abs_params(wdata
->accel
, ABS_RZ
, -500, 500, 2, 4);
1111 wdata
->ir
= input_allocate_device();
1115 input_set_drvdata(wdata
->ir
, wdata
);
1116 wdata
->ir
->open
= wiimote_ir_open
;
1117 wdata
->ir
->close
= wiimote_ir_close
;
1118 wdata
->ir
->dev
.parent
= &wdata
->hdev
->dev
;
1119 wdata
->ir
->id
.bustype
= wdata
->hdev
->bus
;
1120 wdata
->ir
->id
.vendor
= wdata
->hdev
->vendor
;
1121 wdata
->ir
->id
.product
= wdata
->hdev
->product
;
1122 wdata
->ir
->id
.version
= wdata
->hdev
->version
;
1123 wdata
->ir
->name
= WIIMOTE_NAME
" IR";
1125 set_bit(EV_ABS
, wdata
->ir
->evbit
);
1126 set_bit(ABS_HAT0X
, wdata
->ir
->absbit
);
1127 set_bit(ABS_HAT0Y
, wdata
->ir
->absbit
);
1128 set_bit(ABS_HAT1X
, wdata
->ir
->absbit
);
1129 set_bit(ABS_HAT1Y
, wdata
->ir
->absbit
);
1130 set_bit(ABS_HAT2X
, wdata
->ir
->absbit
);
1131 set_bit(ABS_HAT2Y
, wdata
->ir
->absbit
);
1132 set_bit(ABS_HAT3X
, wdata
->ir
->absbit
);
1133 set_bit(ABS_HAT3Y
, wdata
->ir
->absbit
);
1134 input_set_abs_params(wdata
->ir
, ABS_HAT0X
, 0, 1023, 2, 4);
1135 input_set_abs_params(wdata
->ir
, ABS_HAT0Y
, 0, 767, 2, 4);
1136 input_set_abs_params(wdata
->ir
, ABS_HAT1X
, 0, 1023, 2, 4);
1137 input_set_abs_params(wdata
->ir
, ABS_HAT1Y
, 0, 767, 2, 4);
1138 input_set_abs_params(wdata
->ir
, ABS_HAT2X
, 0, 1023, 2, 4);
1139 input_set_abs_params(wdata
->ir
, ABS_HAT2Y
, 0, 767, 2, 4);
1140 input_set_abs_params(wdata
->ir
, ABS_HAT3X
, 0, 1023, 2, 4);
1141 input_set_abs_params(wdata
->ir
, ABS_HAT3Y
, 0, 767, 2, 4);
1143 spin_lock_init(&wdata
->qlock
);
1144 INIT_WORK(&wdata
->worker
, wiimote_worker
);
1146 spin_lock_init(&wdata
->state
.lock
);
1147 init_completion(&wdata
->state
.ready
);
1148 mutex_init(&wdata
->state
.sync
);
1149 wdata
->state
.drm
= WIIPROTO_REQ_DRM_K
;
1154 input_free_device(wdata
->accel
);
1156 input_free_device(wdata
->input
);
1162 static void wiimote_destroy(struct wiimote_data
*wdata
)
1164 wiidebug_deinit(wdata
);
1165 wiiext_deinit(wdata
);
1166 wiimote_leds_destroy(wdata
);
1168 power_supply_unregister(&wdata
->battery
);
1169 input_unregister_device(wdata
->accel
);
1170 input_unregister_device(wdata
->ir
);
1171 input_unregister_device(wdata
->input
);
1172 cancel_work_sync(&wdata
->worker
);
1173 hid_hw_stop(wdata
->hdev
);
1178 static int wiimote_hid_probe(struct hid_device
*hdev
,
1179 const struct hid_device_id
*id
)
1181 struct wiimote_data
*wdata
;
1184 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
1186 wdata
= wiimote_create(hdev
);
1188 hid_err(hdev
, "Can't alloc device\n");
1192 ret
= hid_parse(hdev
);
1194 hid_err(hdev
, "HID parse failed\n");
1198 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1200 hid_err(hdev
, "HW start failed\n");
1204 ret
= input_register_device(wdata
->accel
);
1206 hid_err(hdev
, "Cannot register input device\n");
1210 ret
= input_register_device(wdata
->ir
);
1212 hid_err(hdev
, "Cannot register input device\n");
1216 ret
= input_register_device(wdata
->input
);
1218 hid_err(hdev
, "Cannot register input device\n");
1222 wdata
->battery
.properties
= wiimote_battery_props
;
1223 wdata
->battery
.num_properties
= ARRAY_SIZE(wiimote_battery_props
);
1224 wdata
->battery
.get_property
= wiimote_battery_get_property
;
1225 wdata
->battery
.name
= "wiimote_battery";
1226 wdata
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1227 wdata
->battery
.use_for_apm
= 0;
1229 ret
= power_supply_register(&wdata
->hdev
->dev
, &wdata
->battery
);
1231 hid_err(hdev
, "Cannot register battery device\n");
1235 power_supply_powers(&wdata
->battery
, &hdev
->dev
);
1237 ret
= wiimote_leds_create(wdata
);
1241 ret
= wiiext_init(wdata
);
1245 ret
= wiidebug_init(wdata
);
1249 hid_info(hdev
, "New device registered\n");
1251 /* by default set led1 after device initialization */
1252 spin_lock_irq(&wdata
->state
.lock
);
1253 wiiproto_req_leds(wdata
, WIIPROTO_FLAG_LED1
);
1254 spin_unlock_irq(&wdata
->state
.lock
);
1259 wiimote_destroy(wdata
);
1263 input_unregister_device(wdata
->input
);
1264 wdata
->input
= NULL
;
1266 input_unregister_device(wdata
->ir
);
1269 input_unregister_device(wdata
->accel
);
1270 wdata
->accel
= NULL
;
1274 input_free_device(wdata
->ir
);
1275 input_free_device(wdata
->accel
);
1276 input_free_device(wdata
->input
);
1281 static void wiimote_hid_remove(struct hid_device
*hdev
)
1283 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
1285 hid_info(hdev
, "Device removed\n");
1286 wiimote_destroy(wdata
);
1289 static const struct hid_device_id wiimote_hid_devices
[] = {
1290 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
1291 USB_DEVICE_ID_NINTENDO_WIIMOTE
) },
1294 MODULE_DEVICE_TABLE(hid
, wiimote_hid_devices
);
1296 static struct hid_driver wiimote_hid_driver
= {
1298 .id_table
= wiimote_hid_devices
,
1299 .probe
= wiimote_hid_probe
,
1300 .remove
= wiimote_hid_remove
,
1301 .raw_event
= wiimote_hid_event
,
1304 static int __init
wiimote_init(void)
1308 ret
= hid_register_driver(&wiimote_hid_driver
);
1310 pr_err("Can't register wiimote hid driver\n");
1315 static void __exit
wiimote_exit(void)
1317 hid_unregister_driver(&wiimote_hid_driver
);
1320 module_init(wiimote_init
);
1321 module_exit(wiimote_exit
);
1322 MODULE_LICENSE("GPL");
1323 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1324 MODULE_DESCRIPTION(WIIMOTE_NAME
" Device Driver");