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>
24 #define WIIMOTE_VERSION "0.2"
25 #define WIIMOTE_NAME "Nintendo Wii Remote"
26 #define WIIMOTE_BUFSIZE 32
29 __u8 data
[HID_MAX_BUFFER_SIZE
];
33 struct wiimote_state
{
38 /* synchronous cmd requests */
40 struct completion ready
;
44 /* results of synchronous requests */
50 struct hid_device
*hdev
;
51 struct input_dev
*input
;
52 struct led_classdev
*leds
[4];
53 struct input_dev
*accel
;
55 struct power_supply battery
;
60 struct wiimote_buf outq
[WIIMOTE_BUFSIZE
];
61 struct work_struct worker
;
63 struct wiimote_state state
;
66 #define WIIPROTO_FLAG_LED1 0x01
67 #define WIIPROTO_FLAG_LED2 0x02
68 #define WIIPROTO_FLAG_LED3 0x04
69 #define WIIPROTO_FLAG_LED4 0x08
70 #define WIIPROTO_FLAG_RUMBLE 0x10
71 #define WIIPROTO_FLAG_ACCEL 0x20
72 #define WIIPROTO_FLAG_IR_BASIC 0x40
73 #define WIIPROTO_FLAG_IR_EXT 0x80
74 #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */
75 #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
76 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
77 #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
78 WIIPROTO_FLAG_IR_FULL)
80 /* return flag for led \num */
81 #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
84 WIIPROTO_REQ_NULL
= 0x0,
85 WIIPROTO_REQ_RUMBLE
= 0x10,
86 WIIPROTO_REQ_LED
= 0x11,
87 WIIPROTO_REQ_DRM
= 0x12,
88 WIIPROTO_REQ_IR1
= 0x13,
89 WIIPROTO_REQ_SREQ
= 0x15,
90 WIIPROTO_REQ_WMEM
= 0x16,
91 WIIPROTO_REQ_RMEM
= 0x17,
92 WIIPROTO_REQ_IR2
= 0x1a,
93 WIIPROTO_REQ_STATUS
= 0x20,
94 WIIPROTO_REQ_DATA
= 0x21,
95 WIIPROTO_REQ_RETURN
= 0x22,
96 WIIPROTO_REQ_DRM_K
= 0x30,
97 WIIPROTO_REQ_DRM_KA
= 0x31,
98 WIIPROTO_REQ_DRM_KE
= 0x32,
99 WIIPROTO_REQ_DRM_KAI
= 0x33,
100 WIIPROTO_REQ_DRM_KEE
= 0x34,
101 WIIPROTO_REQ_DRM_KAE
= 0x35,
102 WIIPROTO_REQ_DRM_KIE
= 0x36,
103 WIIPROTO_REQ_DRM_KAIE
= 0x37,
104 WIIPROTO_REQ_DRM_E
= 0x3d,
105 WIIPROTO_REQ_DRM_SKAI1
= 0x3e,
106 WIIPROTO_REQ_DRM_SKAI2
= 0x3f,
124 static __u16 wiiproto_keymap
[] = {
125 KEY_LEFT
, /* WIIPROTO_KEY_LEFT */
126 KEY_RIGHT
, /* WIIPROTO_KEY_RIGHT */
127 KEY_UP
, /* WIIPROTO_KEY_UP */
128 KEY_DOWN
, /* WIIPROTO_KEY_DOWN */
129 KEY_NEXT
, /* WIIPROTO_KEY_PLUS */
130 KEY_PREVIOUS
, /* WIIPROTO_KEY_MINUS */
131 BTN_1
, /* WIIPROTO_KEY_ONE */
132 BTN_2
, /* WIIPROTO_KEY_TWO */
133 BTN_A
, /* WIIPROTO_KEY_A */
134 BTN_B
, /* WIIPROTO_KEY_B */
135 BTN_MODE
, /* WIIPROTO_KEY_HOME */
138 static enum power_supply_property wiimote_battery_props
[] = {
139 POWER_SUPPLY_PROP_CAPACITY
142 /* requires the state.lock spinlock to be held */
143 static inline bool wiimote_cmd_pending(struct wiimote_data
*wdata
, int cmd
,
146 return wdata
->state
.cmd
== cmd
&& wdata
->state
.opt
== opt
;
149 /* requires the state.lock spinlock to be held */
150 static inline void wiimote_cmd_complete(struct wiimote_data
*wdata
)
152 wdata
->state
.cmd
= WIIPROTO_REQ_NULL
;
153 complete(&wdata
->state
.ready
);
156 static inline int wiimote_cmd_acquire(struct wiimote_data
*wdata
)
158 return mutex_lock_interruptible(&wdata
->state
.sync
) ? -ERESTARTSYS
: 0;
161 /* requires the state.lock spinlock to be held */
162 static inline void wiimote_cmd_set(struct wiimote_data
*wdata
, int cmd
,
165 INIT_COMPLETION(wdata
->state
.ready
);
166 wdata
->state
.cmd
= cmd
;
167 wdata
->state
.opt
= opt
;
170 static inline void wiimote_cmd_release(struct wiimote_data
*wdata
)
172 mutex_unlock(&wdata
->state
.sync
);
175 static inline int wiimote_cmd_wait(struct wiimote_data
*wdata
)
179 ret
= wait_for_completion_interruptible_timeout(&wdata
->state
.ready
, HZ
);
188 static ssize_t
wiimote_hid_send(struct hid_device
*hdev
, __u8
*buffer
,
194 if (!hdev
->hid_output_raw_report
)
197 buf
= kmemdup(buffer
, count
, GFP_KERNEL
);
201 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
207 static void wiimote_worker(struct work_struct
*work
)
209 struct wiimote_data
*wdata
= container_of(work
, struct wiimote_data
,
213 spin_lock_irqsave(&wdata
->qlock
, flags
);
215 while (wdata
->head
!= wdata
->tail
) {
216 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
217 wiimote_hid_send(wdata
->hdev
, wdata
->outq
[wdata
->tail
].data
,
218 wdata
->outq
[wdata
->tail
].size
);
219 spin_lock_irqsave(&wdata
->qlock
, flags
);
221 wdata
->tail
= (wdata
->tail
+ 1) % WIIMOTE_BUFSIZE
;
224 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
227 static void wiimote_queue(struct wiimote_data
*wdata
, const __u8
*buffer
,
233 if (count
> HID_MAX_BUFFER_SIZE
) {
234 hid_warn(wdata
->hdev
, "Sending too large output report\n");
239 * Copy new request into our output queue and check whether the
240 * queue is full. If it is full, discard this request.
241 * If it is empty we need to start a new worker that will
242 * send out the buffer to the hid device.
243 * If the queue is not empty, then there must be a worker
244 * that is currently sending out our buffer and this worker
245 * will reschedule itself until the queue is empty.
248 spin_lock_irqsave(&wdata
->qlock
, flags
);
250 memcpy(wdata
->outq
[wdata
->head
].data
, buffer
, count
);
251 wdata
->outq
[wdata
->head
].size
= count
;
252 newhead
= (wdata
->head
+ 1) % WIIMOTE_BUFSIZE
;
254 if (wdata
->head
== wdata
->tail
) {
255 wdata
->head
= newhead
;
256 schedule_work(&wdata
->worker
);
257 } else if (newhead
!= wdata
->tail
) {
258 wdata
->head
= newhead
;
260 hid_warn(wdata
->hdev
, "Output queue is full");
263 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
267 * This sets the rumble bit on the given output report if rumble is
269 * \cmd1 must point to the second byte in the output report => &cmd[1]
270 * This must be called on nearly every output report before passing it
271 * into the output queue!
273 static inline void wiiproto_keep_rumble(struct wiimote_data
*wdata
, __u8
*cmd1
)
275 if (wdata
->state
.flags
& WIIPROTO_FLAG_RUMBLE
)
279 static void wiiproto_req_rumble(struct wiimote_data
*wdata
, __u8 rumble
)
284 if (rumble
== !!(wdata
->state
.flags
& WIIPROTO_FLAG_RUMBLE
))
288 wdata
->state
.flags
|= WIIPROTO_FLAG_RUMBLE
;
290 wdata
->state
.flags
&= ~WIIPROTO_FLAG_RUMBLE
;
292 cmd
[0] = WIIPROTO_REQ_RUMBLE
;
295 wiiproto_keep_rumble(wdata
, &cmd
[1]);
296 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
299 static void wiiproto_req_leds(struct wiimote_data
*wdata
, int leds
)
303 leds
&= WIIPROTO_FLAGS_LEDS
;
304 if ((wdata
->state
.flags
& WIIPROTO_FLAGS_LEDS
) == leds
)
306 wdata
->state
.flags
= (wdata
->state
.flags
& ~WIIPROTO_FLAGS_LEDS
) | leds
;
308 cmd
[0] = WIIPROTO_REQ_LED
;
311 if (leds
& WIIPROTO_FLAG_LED1
)
313 if (leds
& WIIPROTO_FLAG_LED2
)
315 if (leds
& WIIPROTO_FLAG_LED3
)
317 if (leds
& WIIPROTO_FLAG_LED4
)
320 wiiproto_keep_rumble(wdata
, &cmd
[1]);
321 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
325 * Check what peripherals of the wiimote are currently
326 * active and select a proper DRM that supports all of
327 * the requested data inputs.
329 static __u8
select_drm(struct wiimote_data
*wdata
)
331 __u8 ir
= wdata
->state
.flags
& WIIPROTO_FLAGS_IR
;
333 if (ir
== WIIPROTO_FLAG_IR_BASIC
) {
334 if (wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
)
335 return WIIPROTO_REQ_DRM_KAIE
;
337 return WIIPROTO_REQ_DRM_KIE
;
338 } else if (ir
== WIIPROTO_FLAG_IR_EXT
) {
339 return WIIPROTO_REQ_DRM_KAI
;
340 } else if (ir
== WIIPROTO_FLAG_IR_FULL
) {
341 return WIIPROTO_REQ_DRM_SKAI1
;
343 if (wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
)
344 return WIIPROTO_REQ_DRM_KA
;
346 return WIIPROTO_REQ_DRM_K
;
350 static void wiiproto_req_drm(struct wiimote_data
*wdata
, __u8 drm
)
354 if (drm
== WIIPROTO_REQ_NULL
)
355 drm
= select_drm(wdata
);
357 cmd
[0] = WIIPROTO_REQ_DRM
;
361 wiiproto_keep_rumble(wdata
, &cmd
[1]);
362 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
365 static void wiiproto_req_status(struct wiimote_data
*wdata
)
369 cmd
[0] = WIIPROTO_REQ_SREQ
;
372 wiiproto_keep_rumble(wdata
, &cmd
[1]);
373 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
376 static void wiiproto_req_accel(struct wiimote_data
*wdata
, __u8 accel
)
379 if (accel
== !!(wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
))
383 wdata
->state
.flags
|= WIIPROTO_FLAG_ACCEL
;
385 wdata
->state
.flags
&= ~WIIPROTO_FLAG_ACCEL
;
387 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
390 static void wiiproto_req_ir1(struct wiimote_data
*wdata
, __u8 flags
)
394 cmd
[0] = WIIPROTO_REQ_IR1
;
397 wiiproto_keep_rumble(wdata
, &cmd
[1]);
398 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
401 static void wiiproto_req_ir2(struct wiimote_data
*wdata
, __u8 flags
)
405 cmd
[0] = WIIPROTO_REQ_IR2
;
408 wiiproto_keep_rumble(wdata
, &cmd
[1]);
409 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
412 #define wiiproto_req_wreg(wdata, os, buf, sz) \
413 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
415 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
416 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
418 static void wiiproto_req_wmem(struct wiimote_data
*wdata
, bool eeprom
,
419 __u32 offset
, const __u8
*buf
, __u8 size
)
423 if (size
> 16 || size
== 0) {
424 hid_warn(wdata
->hdev
, "Invalid length %d wmem request\n", size
);
428 memset(cmd
, 0, sizeof(cmd
));
429 cmd
[0] = WIIPROTO_REQ_WMEM
;
430 cmd
[2] = (offset
>> 16) & 0xff;
431 cmd
[3] = (offset
>> 8) & 0xff;
432 cmd
[4] = offset
& 0xff;
434 memcpy(&cmd
[6], buf
, size
);
439 wiiproto_keep_rumble(wdata
, &cmd
[1]);
440 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
443 /* requries the cmd-mutex to be held */
444 static int wiimote_cmd_write(struct wiimote_data
*wdata
, __u32 offset
,
445 const __u8
*wmem
, __u8 size
)
450 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
451 wiimote_cmd_set(wdata
, WIIPROTO_REQ_WMEM
, 0);
452 wiiproto_req_wreg(wdata
, offset
, wmem
, size
);
453 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
455 ret
= wiimote_cmd_wait(wdata
);
456 if (!ret
&& wdata
->state
.cmd_err
)
462 static int wiimote_battery_get_property(struct power_supply
*psy
,
463 enum power_supply_property psp
,
464 union power_supply_propval
*val
)
466 struct wiimote_data
*wdata
= container_of(psy
,
467 struct wiimote_data
, battery
);
471 ret
= wiimote_cmd_acquire(wdata
);
475 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
476 wiimote_cmd_set(wdata
, WIIPROTO_REQ_SREQ
, 0);
477 wiiproto_req_status(wdata
);
478 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
480 ret
= wiimote_cmd_wait(wdata
);
481 state
= wdata
->state
.cmd_battery
;
482 wiimote_cmd_release(wdata
);
488 case POWER_SUPPLY_PROP_CAPACITY
:
489 val
->intval
= state
* 100 / 255;
499 static int wiimote_init_ir(struct wiimote_data
*wdata
, __u16 mode
)
504 static const __u8 data_enable
[] = { 0x01 };
505 static const __u8 data_sens1
[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
506 0x00, 0xaa, 0x00, 0x64 };
507 static const __u8 data_sens2
[] = { 0x63, 0x03 };
508 static const __u8 data_fin
[] = { 0x08 };
510 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
512 if (mode
== (wdata
->state
.flags
& WIIPROTO_FLAGS_IR
)) {
513 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
518 wdata
->state
.flags
&= ~WIIPROTO_FLAGS_IR
;
519 wiiproto_req_ir1(wdata
, 0);
520 wiiproto_req_ir2(wdata
, 0);
521 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
522 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
526 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
528 ret
= wiimote_cmd_acquire(wdata
);
532 /* send PIXEL CLOCK ENABLE cmd first */
533 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
534 wiimote_cmd_set(wdata
, WIIPROTO_REQ_IR1
, 0);
535 wiiproto_req_ir1(wdata
, 0x06);
536 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
538 ret
= wiimote_cmd_wait(wdata
);
541 if (wdata
->state
.cmd_err
) {
546 /* enable IR LOGIC */
547 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
548 wiimote_cmd_set(wdata
, WIIPROTO_REQ_IR2
, 0);
549 wiiproto_req_ir2(wdata
, 0x06);
550 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
552 ret
= wiimote_cmd_wait(wdata
);
555 if (wdata
->state
.cmd_err
) {
560 /* enable IR cam but do not make it send data, yet */
561 ret
= wiimote_cmd_write(wdata
, 0xb00030, data_enable
,
562 sizeof(data_enable
));
566 /* write first sensitivity block */
567 ret
= wiimote_cmd_write(wdata
, 0xb00000, data_sens1
,
572 /* write second sensitivity block */
573 ret
= wiimote_cmd_write(wdata
, 0xb0001a, data_sens2
,
578 /* put IR cam into desired state */
580 case WIIPROTO_FLAG_IR_FULL
:
583 case WIIPROTO_FLAG_IR_EXT
:
586 case WIIPROTO_FLAG_IR_BASIC
:
590 ret
= wiimote_cmd_write(wdata
, 0xb00033, &format
, sizeof(format
));
594 /* make IR cam send data */
595 ret
= wiimote_cmd_write(wdata
, 0xb00030, data_fin
, sizeof(data_fin
));
599 /* request new DRM mode compatible to IR mode */
600 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
601 wdata
->state
.flags
&= ~WIIPROTO_FLAGS_IR
;
602 wdata
->state
.flags
|= mode
& WIIPROTO_FLAGS_IR
;
603 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
604 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
607 wiimote_cmd_release(wdata
);
611 static enum led_brightness
wiimote_leds_get(struct led_classdev
*led_dev
)
613 struct wiimote_data
*wdata
;
614 struct device
*dev
= led_dev
->dev
->parent
;
619 wdata
= hid_get_drvdata(container_of(dev
, struct hid_device
, dev
));
621 for (i
= 0; i
< 4; ++i
) {
622 if (wdata
->leds
[i
] == led_dev
) {
623 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
624 value
= wdata
->state
.flags
& WIIPROTO_FLAG_LED(i
+ 1);
625 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
630 return value
? LED_FULL
: LED_OFF
;
633 static void wiimote_leds_set(struct led_classdev
*led_dev
,
634 enum led_brightness value
)
636 struct wiimote_data
*wdata
;
637 struct device
*dev
= led_dev
->dev
->parent
;
642 wdata
= hid_get_drvdata(container_of(dev
, struct hid_device
, dev
));
644 for (i
= 0; i
< 4; ++i
) {
645 if (wdata
->leds
[i
] == led_dev
) {
646 flag
= WIIPROTO_FLAG_LED(i
+ 1);
647 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
648 state
= wdata
->state
.flags
;
649 if (value
== LED_OFF
)
650 wiiproto_req_leds(wdata
, state
& ~flag
);
652 wiiproto_req_leds(wdata
, state
| flag
);
653 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
659 static int wiimote_ff_play(struct input_dev
*dev
, void *data
,
660 struct ff_effect
*eff
)
662 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
667 * The wiimote supports only a single rumble motor so if any magnitude
668 * is set to non-zero then we start the rumble motor. If both are set to
669 * zero, we stop the rumble motor.
672 if (eff
->u
.rumble
.strong_magnitude
|| eff
->u
.rumble
.weak_magnitude
)
677 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
678 wiiproto_req_rumble(wdata
, value
);
679 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
684 static int wiimote_input_open(struct input_dev
*dev
)
686 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
688 return hid_hw_open(wdata
->hdev
);
691 static void wiimote_input_close(struct input_dev
*dev
)
693 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
695 hid_hw_close(wdata
->hdev
);
698 static int wiimote_accel_open(struct input_dev
*dev
)
700 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
704 ret
= hid_hw_open(wdata
->hdev
);
708 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
709 wiiproto_req_accel(wdata
, true);
710 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
715 static void wiimote_accel_close(struct input_dev
*dev
)
717 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
720 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
721 wiiproto_req_accel(wdata
, false);
722 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
724 hid_hw_close(wdata
->hdev
);
727 static int wiimote_ir_open(struct input_dev
*dev
)
729 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
732 ret
= hid_hw_open(wdata
->hdev
);
736 ret
= wiimote_init_ir(wdata
, WIIPROTO_FLAG_IR_BASIC
);
738 hid_hw_close(wdata
->hdev
);
745 static void wiimote_ir_close(struct input_dev
*dev
)
747 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
749 wiimote_init_ir(wdata
, 0);
750 hid_hw_close(wdata
->hdev
);
753 static void handler_keys(struct wiimote_data
*wdata
, const __u8
*payload
)
755 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_LEFT
],
756 !!(payload
[0] & 0x01));
757 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_RIGHT
],
758 !!(payload
[0] & 0x02));
759 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_DOWN
],
760 !!(payload
[0] & 0x04));
761 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_UP
],
762 !!(payload
[0] & 0x08));
763 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_PLUS
],
764 !!(payload
[0] & 0x10));
765 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_TWO
],
766 !!(payload
[1] & 0x01));
767 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_ONE
],
768 !!(payload
[1] & 0x02));
769 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_B
],
770 !!(payload
[1] & 0x04));
771 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_A
],
772 !!(payload
[1] & 0x08));
773 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_MINUS
],
774 !!(payload
[1] & 0x10));
775 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_HOME
],
776 !!(payload
[1] & 0x80));
777 input_sync(wdata
->input
);
780 static void handler_accel(struct wiimote_data
*wdata
, const __u8
*payload
)
784 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
))
788 * payload is: BB BB XX YY ZZ
789 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
790 * contain the upper 8 bits of each value. The lower 2 bits are
791 * contained in the buttons data BB BB.
792 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
793 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
794 * accel value and bit 6 is the second bit of the Z value.
795 * The first bit of Y and Z values is not available and always set to 0.
796 * 0x200 is returned on no movement.
803 x
|= (payload
[0] >> 5) & 0x3;
804 y
|= (payload
[1] >> 4) & 0x2;
805 z
|= (payload
[1] >> 5) & 0x2;
807 input_report_abs(wdata
->accel
, ABS_RX
, x
- 0x200);
808 input_report_abs(wdata
->accel
, ABS_RY
, y
- 0x200);
809 input_report_abs(wdata
->accel
, ABS_RZ
, z
- 0x200);
810 input_sync(wdata
->accel
);
813 #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
814 ABS_HAT0X, ABS_HAT0Y)
815 #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
816 ABS_HAT1X, ABS_HAT1Y)
817 #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
818 ABS_HAT2X, ABS_HAT2Y)
819 #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
820 ABS_HAT3X, ABS_HAT3Y)
822 static void __ir_to_input(struct wiimote_data
*wdata
, const __u8
*ir
,
823 bool packed
, __u8 xid
, __u8 yid
)
827 if (!(wdata
->state
.flags
& WIIPROTO_FLAGS_IR
))
831 * Basic IR data is encoded into 3 bytes. The first two bytes are the
832 * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
834 * If data is packed, then the 3rd byte is put first and slightly
835 * reordered. This allows to interleave packed and non-packed data to
836 * have two IR sets in 5 bytes instead of 6.
837 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
845 y
|= (ir
[0] >> 2) & 0x3;
850 x
|= (ir
[2] >> 4) & 0x3;
851 y
|= (ir
[2] >> 6) & 0x3;
854 input_report_abs(wdata
->ir
, xid
, x
);
855 input_report_abs(wdata
->ir
, yid
, y
);
858 static void handler_status(struct wiimote_data
*wdata
, const __u8
*payload
)
860 handler_keys(wdata
, payload
);
862 /* on status reports the drm is reset so we need to resend the drm */
863 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
865 if (wiimote_cmd_pending(wdata
, WIIPROTO_REQ_SREQ
, 0)) {
866 wdata
->state
.cmd_battery
= payload
[5];
867 wiimote_cmd_complete(wdata
);
871 static void handler_data(struct wiimote_data
*wdata
, const __u8
*payload
)
873 handler_keys(wdata
, payload
);
876 static void handler_return(struct wiimote_data
*wdata
, const __u8
*payload
)
878 __u8 err
= payload
[3];
879 __u8 cmd
= payload
[2];
881 handler_keys(wdata
, payload
);
883 if (wiimote_cmd_pending(wdata
, cmd
, 0)) {
884 wdata
->state
.cmd_err
= err
;
885 wiimote_cmd_complete(wdata
);
887 hid_warn(wdata
->hdev
, "Remote error %hhu on req %hhu\n", err
,
892 static void handler_drm_KA(struct wiimote_data
*wdata
, const __u8
*payload
)
894 handler_keys(wdata
, payload
);
895 handler_accel(wdata
, payload
);
898 static void handler_drm_KE(struct wiimote_data
*wdata
, const __u8
*payload
)
900 handler_keys(wdata
, payload
);
903 static void handler_drm_KAI(struct wiimote_data
*wdata
, const __u8
*payload
)
905 handler_keys(wdata
, payload
);
906 handler_accel(wdata
, payload
);
907 ir_to_input0(wdata
, &payload
[5], false);
908 ir_to_input1(wdata
, &payload
[8], false);
909 ir_to_input2(wdata
, &payload
[11], false);
910 ir_to_input3(wdata
, &payload
[14], false);
911 input_sync(wdata
->ir
);
914 static void handler_drm_KEE(struct wiimote_data
*wdata
, const __u8
*payload
)
916 handler_keys(wdata
, payload
);
919 static void handler_drm_KIE(struct wiimote_data
*wdata
, const __u8
*payload
)
921 handler_keys(wdata
, payload
);
922 ir_to_input0(wdata
, &payload
[2], false);
923 ir_to_input1(wdata
, &payload
[4], true);
924 ir_to_input2(wdata
, &payload
[7], false);
925 ir_to_input3(wdata
, &payload
[9], true);
926 input_sync(wdata
->ir
);
929 static void handler_drm_KAE(struct wiimote_data
*wdata
, const __u8
*payload
)
931 handler_keys(wdata
, payload
);
932 handler_accel(wdata
, payload
);
935 static void handler_drm_KAIE(struct wiimote_data
*wdata
, const __u8
*payload
)
937 handler_keys(wdata
, payload
);
938 handler_accel(wdata
, payload
);
939 ir_to_input0(wdata
, &payload
[5], false);
940 ir_to_input1(wdata
, &payload
[7], true);
941 ir_to_input2(wdata
, &payload
[10], false);
942 ir_to_input3(wdata
, &payload
[12], true);
943 input_sync(wdata
->ir
);
946 static void handler_drm_E(struct wiimote_data
*wdata
, const __u8
*payload
)
950 static void handler_drm_SKAI1(struct wiimote_data
*wdata
, const __u8
*payload
)
952 handler_keys(wdata
, payload
);
954 wdata
->state
.accel_split
[0] = payload
[2];
955 wdata
->state
.accel_split
[1] = (payload
[0] >> 1) & (0x10 | 0x20);
956 wdata
->state
.accel_split
[1] |= (payload
[1] << 1) & (0x40 | 0x80);
958 ir_to_input0(wdata
, &payload
[3], false);
959 ir_to_input1(wdata
, &payload
[12], false);
960 input_sync(wdata
->ir
);
963 static void handler_drm_SKAI2(struct wiimote_data
*wdata
, const __u8
*payload
)
967 handler_keys(wdata
, payload
);
969 wdata
->state
.accel_split
[1] |= (payload
[0] >> 5) & (0x01 | 0x02);
970 wdata
->state
.accel_split
[1] |= (payload
[1] >> 3) & (0x04 | 0x08);
974 buf
[2] = wdata
->state
.accel_split
[0];
976 buf
[4] = wdata
->state
.accel_split
[1];
977 handler_accel(wdata
, buf
);
979 ir_to_input2(wdata
, &payload
[3], false);
980 ir_to_input3(wdata
, &payload
[12], false);
981 input_sync(wdata
->ir
);
984 struct wiiproto_handler
{
987 void (*func
)(struct wiimote_data
*wdata
, const __u8
*payload
);
990 static struct wiiproto_handler handlers
[] = {
991 { .id
= WIIPROTO_REQ_STATUS
, .size
= 6, .func
= handler_status
},
992 { .id
= WIIPROTO_REQ_DATA
, .size
= 21, .func
= handler_data
},
993 { .id
= WIIPROTO_REQ_RETURN
, .size
= 4, .func
= handler_return
},
994 { .id
= WIIPROTO_REQ_DRM_K
, .size
= 2, .func
= handler_keys
},
995 { .id
= WIIPROTO_REQ_DRM_KA
, .size
= 5, .func
= handler_drm_KA
},
996 { .id
= WIIPROTO_REQ_DRM_KE
, .size
= 10, .func
= handler_drm_KE
},
997 { .id
= WIIPROTO_REQ_DRM_KAI
, .size
= 17, .func
= handler_drm_KAI
},
998 { .id
= WIIPROTO_REQ_DRM_KEE
, .size
= 21, .func
= handler_drm_KEE
},
999 { .id
= WIIPROTO_REQ_DRM_KAE
, .size
= 21, .func
= handler_drm_KAE
},
1000 { .id
= WIIPROTO_REQ_DRM_KIE
, .size
= 21, .func
= handler_drm_KIE
},
1001 { .id
= WIIPROTO_REQ_DRM_KAIE
, .size
= 21, .func
= handler_drm_KAIE
},
1002 { .id
= WIIPROTO_REQ_DRM_E
, .size
= 21, .func
= handler_drm_E
},
1003 { .id
= WIIPROTO_REQ_DRM_SKAI1
, .size
= 21, .func
= handler_drm_SKAI1
},
1004 { .id
= WIIPROTO_REQ_DRM_SKAI2
, .size
= 21, .func
= handler_drm_SKAI2
},
1008 static int wiimote_hid_event(struct hid_device
*hdev
, struct hid_report
*report
,
1009 u8
*raw_data
, int size
)
1011 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
1012 struct wiiproto_handler
*h
;
1014 unsigned long flags
;
1015 bool handled
= false;
1020 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1022 for (i
= 0; handlers
[i
].id
; ++i
) {
1024 if (h
->id
== raw_data
[0] && h
->size
< size
) {
1025 h
->func(wdata
, &raw_data
[1]);
1031 hid_warn(hdev
, "Unhandled report %hhu size %d\n", raw_data
[0],
1034 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1039 static void wiimote_leds_destroy(struct wiimote_data
*wdata
)
1042 struct led_classdev
*led
;
1044 for (i
= 0; i
< 4; ++i
) {
1045 if (wdata
->leds
[i
]) {
1046 led
= wdata
->leds
[i
];
1047 wdata
->leds
[i
] = NULL
;
1048 led_classdev_unregister(led
);
1054 static int wiimote_leds_create(struct wiimote_data
*wdata
)
1057 struct device
*dev
= &wdata
->hdev
->dev
;
1058 size_t namesz
= strlen(dev_name(dev
)) + 9;
1059 struct led_classdev
*led
;
1062 for (i
= 0; i
< 4; ++i
) {
1063 led
= kzalloc(sizeof(struct led_classdev
) + namesz
, GFP_KERNEL
);
1068 name
= (void*)&led
[1];
1069 snprintf(name
, namesz
, "%s:blue:p%d", dev_name(dev
), i
);
1071 led
->brightness
= 0;
1072 led
->max_brightness
= 1;
1073 led
->brightness_get
= wiimote_leds_get
;
1074 led
->brightness_set
= wiimote_leds_set
;
1076 ret
= led_classdev_register(dev
, led
);
1081 wdata
->leds
[i
] = led
;
1087 wiimote_leds_destroy(wdata
);
1091 static struct wiimote_data
*wiimote_create(struct hid_device
*hdev
)
1093 struct wiimote_data
*wdata
;
1096 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
1100 wdata
->input
= input_allocate_device();
1105 hid_set_drvdata(hdev
, wdata
);
1107 input_set_drvdata(wdata
->input
, wdata
);
1108 wdata
->input
->open
= wiimote_input_open
;
1109 wdata
->input
->close
= wiimote_input_close
;
1110 wdata
->input
->dev
.parent
= &wdata
->hdev
->dev
;
1111 wdata
->input
->id
.bustype
= wdata
->hdev
->bus
;
1112 wdata
->input
->id
.vendor
= wdata
->hdev
->vendor
;
1113 wdata
->input
->id
.product
= wdata
->hdev
->product
;
1114 wdata
->input
->id
.version
= wdata
->hdev
->version
;
1115 wdata
->input
->name
= WIIMOTE_NAME
;
1117 set_bit(EV_KEY
, wdata
->input
->evbit
);
1118 for (i
= 0; i
< WIIPROTO_KEY_COUNT
; ++i
)
1119 set_bit(wiiproto_keymap
[i
], wdata
->input
->keybit
);
1121 set_bit(FF_RUMBLE
, wdata
->input
->ffbit
);
1122 if (input_ff_create_memless(wdata
->input
, NULL
, wiimote_ff_play
))
1125 wdata
->accel
= input_allocate_device();
1129 input_set_drvdata(wdata
->accel
, wdata
);
1130 wdata
->accel
->open
= wiimote_accel_open
;
1131 wdata
->accel
->close
= wiimote_accel_close
;
1132 wdata
->accel
->dev
.parent
= &wdata
->hdev
->dev
;
1133 wdata
->accel
->id
.bustype
= wdata
->hdev
->bus
;
1134 wdata
->accel
->id
.vendor
= wdata
->hdev
->vendor
;
1135 wdata
->accel
->id
.product
= wdata
->hdev
->product
;
1136 wdata
->accel
->id
.version
= wdata
->hdev
->version
;
1137 wdata
->accel
->name
= WIIMOTE_NAME
" Accelerometer";
1139 set_bit(EV_ABS
, wdata
->accel
->evbit
);
1140 set_bit(ABS_RX
, wdata
->accel
->absbit
);
1141 set_bit(ABS_RY
, wdata
->accel
->absbit
);
1142 set_bit(ABS_RZ
, wdata
->accel
->absbit
);
1143 input_set_abs_params(wdata
->accel
, ABS_RX
, -500, 500, 2, 4);
1144 input_set_abs_params(wdata
->accel
, ABS_RY
, -500, 500, 2, 4);
1145 input_set_abs_params(wdata
->accel
, ABS_RZ
, -500, 500, 2, 4);
1147 wdata
->ir
= input_allocate_device();
1151 input_set_drvdata(wdata
->ir
, wdata
);
1152 wdata
->ir
->open
= wiimote_ir_open
;
1153 wdata
->ir
->close
= wiimote_ir_close
;
1154 wdata
->ir
->dev
.parent
= &wdata
->hdev
->dev
;
1155 wdata
->ir
->id
.bustype
= wdata
->hdev
->bus
;
1156 wdata
->ir
->id
.vendor
= wdata
->hdev
->vendor
;
1157 wdata
->ir
->id
.product
= wdata
->hdev
->product
;
1158 wdata
->ir
->id
.version
= wdata
->hdev
->version
;
1159 wdata
->ir
->name
= WIIMOTE_NAME
" IR";
1161 set_bit(EV_ABS
, wdata
->ir
->evbit
);
1162 set_bit(ABS_HAT0X
, wdata
->ir
->absbit
);
1163 set_bit(ABS_HAT0Y
, wdata
->ir
->absbit
);
1164 set_bit(ABS_HAT1X
, wdata
->ir
->absbit
);
1165 set_bit(ABS_HAT1Y
, wdata
->ir
->absbit
);
1166 set_bit(ABS_HAT2X
, wdata
->ir
->absbit
);
1167 set_bit(ABS_HAT2Y
, wdata
->ir
->absbit
);
1168 set_bit(ABS_HAT3X
, wdata
->ir
->absbit
);
1169 set_bit(ABS_HAT3Y
, wdata
->ir
->absbit
);
1170 input_set_abs_params(wdata
->ir
, ABS_HAT0X
, 0, 1023, 2, 4);
1171 input_set_abs_params(wdata
->ir
, ABS_HAT0Y
, 0, 767, 2, 4);
1172 input_set_abs_params(wdata
->ir
, ABS_HAT1X
, 0, 1023, 2, 4);
1173 input_set_abs_params(wdata
->ir
, ABS_HAT1Y
, 0, 767, 2, 4);
1174 input_set_abs_params(wdata
->ir
, ABS_HAT2X
, 0, 1023, 2, 4);
1175 input_set_abs_params(wdata
->ir
, ABS_HAT2Y
, 0, 767, 2, 4);
1176 input_set_abs_params(wdata
->ir
, ABS_HAT3X
, 0, 1023, 2, 4);
1177 input_set_abs_params(wdata
->ir
, ABS_HAT3Y
, 0, 767, 2, 4);
1179 spin_lock_init(&wdata
->qlock
);
1180 INIT_WORK(&wdata
->worker
, wiimote_worker
);
1182 spin_lock_init(&wdata
->state
.lock
);
1183 init_completion(&wdata
->state
.ready
);
1184 mutex_init(&wdata
->state
.sync
);
1189 input_free_device(wdata
->accel
);
1191 input_free_device(wdata
->input
);
1197 static void wiimote_destroy(struct wiimote_data
*wdata
)
1199 wiimote_leds_destroy(wdata
);
1201 power_supply_unregister(&wdata
->battery
);
1202 input_unregister_device(wdata
->accel
);
1203 input_unregister_device(wdata
->ir
);
1204 input_unregister_device(wdata
->input
);
1205 cancel_work_sync(&wdata
->worker
);
1206 hid_hw_stop(wdata
->hdev
);
1211 static int wiimote_hid_probe(struct hid_device
*hdev
,
1212 const struct hid_device_id
*id
)
1214 struct wiimote_data
*wdata
;
1217 wdata
= wiimote_create(hdev
);
1219 hid_err(hdev
, "Can't alloc device\n");
1223 ret
= hid_parse(hdev
);
1225 hid_err(hdev
, "HID parse failed\n");
1229 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1231 hid_err(hdev
, "HW start failed\n");
1235 ret
= input_register_device(wdata
->accel
);
1237 hid_err(hdev
, "Cannot register input device\n");
1241 ret
= input_register_device(wdata
->ir
);
1243 hid_err(hdev
, "Cannot register input device\n");
1247 ret
= input_register_device(wdata
->input
);
1249 hid_err(hdev
, "Cannot register input device\n");
1253 wdata
->battery
.properties
= wiimote_battery_props
;
1254 wdata
->battery
.num_properties
= ARRAY_SIZE(wiimote_battery_props
);
1255 wdata
->battery
.get_property
= wiimote_battery_get_property
;
1256 wdata
->battery
.name
= "wiimote_battery";
1257 wdata
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1258 wdata
->battery
.use_for_apm
= 0;
1260 ret
= power_supply_register(&wdata
->hdev
->dev
, &wdata
->battery
);
1262 hid_err(hdev
, "Cannot register battery device\n");
1266 ret
= wiimote_leds_create(wdata
);
1270 hid_info(hdev
, "New device registered\n");
1272 /* by default set led1 after device initialization */
1273 spin_lock_irq(&wdata
->state
.lock
);
1274 wiiproto_req_leds(wdata
, WIIPROTO_FLAG_LED1
);
1275 spin_unlock_irq(&wdata
->state
.lock
);
1280 wiimote_destroy(wdata
);
1284 input_unregister_device(wdata
->input
);
1285 wdata
->input
= NULL
;
1287 input_unregister_device(wdata
->ir
);
1290 input_unregister_device(wdata
->accel
);
1291 wdata
->accel
= NULL
;
1295 input_free_device(wdata
->ir
);
1296 input_free_device(wdata
->accel
);
1297 input_free_device(wdata
->input
);
1302 static void wiimote_hid_remove(struct hid_device
*hdev
)
1304 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
1306 hid_info(hdev
, "Device removed\n");
1307 wiimote_destroy(wdata
);
1310 static const struct hid_device_id wiimote_hid_devices
[] = {
1311 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
1312 USB_DEVICE_ID_NINTENDO_WIIMOTE
) },
1315 MODULE_DEVICE_TABLE(hid
, wiimote_hid_devices
);
1317 static struct hid_driver wiimote_hid_driver
= {
1319 .id_table
= wiimote_hid_devices
,
1320 .probe
= wiimote_hid_probe
,
1321 .remove
= wiimote_hid_remove
,
1322 .raw_event
= wiimote_hid_event
,
1325 static int __init
wiimote_init(void)
1329 ret
= hid_register_driver(&wiimote_hid_driver
);
1331 pr_err("Can't register wiimote hid driver\n");
1336 static void __exit
wiimote_exit(void)
1338 hid_unregister_driver(&wiimote_hid_driver
);
1341 module_init(wiimote_init
);
1342 module_exit(wiimote_exit
);
1343 MODULE_LICENSE("GPL");
1344 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1345 MODULE_DESCRIPTION(WIIMOTE_NAME
" Device Driver");
1346 MODULE_VERSION(WIIMOTE_VERSION
);