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/device.h>
14 #include <linux/hid.h>
15 #include <linux/input.h>
16 #include <linux/leds.h>
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
21 #define WIIMOTE_VERSION "0.1"
22 #define WIIMOTE_NAME "Nintendo Wii Remote"
23 #define WIIMOTE_BUFSIZE 32
26 __u8 data
[HID_MAX_BUFFER_SIZE
];
30 struct wiimote_state
{
36 struct hid_device
*hdev
;
37 struct input_dev
*input
;
38 struct led_classdev
*leds
[4];
43 struct wiimote_buf outq
[WIIMOTE_BUFSIZE
];
44 struct work_struct worker
;
46 struct wiimote_state state
;
49 #define WIIPROTO_FLAG_LED1 0x01
50 #define WIIPROTO_FLAG_LED2 0x02
51 #define WIIPROTO_FLAG_LED3 0x04
52 #define WIIPROTO_FLAG_LED4 0x08
53 #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
54 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
56 /* return flag for led \num */
57 #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
60 WIIPROTO_REQ_NULL
= 0x0,
61 WIIPROTO_REQ_LED
= 0x11,
62 WIIPROTO_REQ_DRM
= 0x12,
63 WIIPROTO_REQ_STATUS
= 0x20,
64 WIIPROTO_REQ_RETURN
= 0x22,
65 WIIPROTO_REQ_DRM_K
= 0x30,
83 static __u16 wiiproto_keymap
[] = {
84 KEY_LEFT
, /* WIIPROTO_KEY_LEFT */
85 KEY_RIGHT
, /* WIIPROTO_KEY_RIGHT */
86 KEY_UP
, /* WIIPROTO_KEY_UP */
87 KEY_DOWN
, /* WIIPROTO_KEY_DOWN */
88 KEY_NEXT
, /* WIIPROTO_KEY_PLUS */
89 KEY_PREVIOUS
, /* WIIPROTO_KEY_MINUS */
90 BTN_1
, /* WIIPROTO_KEY_ONE */
91 BTN_2
, /* WIIPROTO_KEY_TWO */
92 BTN_A
, /* WIIPROTO_KEY_A */
93 BTN_B
, /* WIIPROTO_KEY_B */
94 BTN_MODE
, /* WIIPROTO_KEY_HOME */
97 static ssize_t
wiimote_hid_send(struct hid_device
*hdev
, __u8
*buffer
,
103 if (!hdev
->hid_output_raw_report
)
106 buf
= kmemdup(buffer
, count
, GFP_KERNEL
);
110 ret
= hdev
->hid_output_raw_report(hdev
, buf
, count
, HID_OUTPUT_REPORT
);
116 static void wiimote_worker(struct work_struct
*work
)
118 struct wiimote_data
*wdata
= container_of(work
, struct wiimote_data
,
122 spin_lock_irqsave(&wdata
->qlock
, flags
);
124 while (wdata
->head
!= wdata
->tail
) {
125 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
126 wiimote_hid_send(wdata
->hdev
, wdata
->outq
[wdata
->tail
].data
,
127 wdata
->outq
[wdata
->tail
].size
);
128 spin_lock_irqsave(&wdata
->qlock
, flags
);
130 wdata
->tail
= (wdata
->tail
+ 1) % WIIMOTE_BUFSIZE
;
133 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
136 static void wiimote_queue(struct wiimote_data
*wdata
, const __u8
*buffer
,
142 if (count
> HID_MAX_BUFFER_SIZE
) {
143 hid_warn(wdata
->hdev
, "Sending too large output report\n");
148 * Copy new request into our output queue and check whether the
149 * queue is full. If it is full, discard this request.
150 * If it is empty we need to start a new worker that will
151 * send out the buffer to the hid device.
152 * If the queue is not empty, then there must be a worker
153 * that is currently sending out our buffer and this worker
154 * will reschedule itself until the queue is empty.
157 spin_lock_irqsave(&wdata
->qlock
, flags
);
159 memcpy(wdata
->outq
[wdata
->head
].data
, buffer
, count
);
160 wdata
->outq
[wdata
->head
].size
= count
;
161 newhead
= (wdata
->head
+ 1) % WIIMOTE_BUFSIZE
;
163 if (wdata
->head
== wdata
->tail
) {
164 wdata
->head
= newhead
;
165 schedule_work(&wdata
->worker
);
166 } else if (newhead
!= wdata
->tail
) {
167 wdata
->head
= newhead
;
169 hid_warn(wdata
->hdev
, "Output queue is full");
172 spin_unlock_irqrestore(&wdata
->qlock
, flags
);
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 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
200 * Check what peripherals of the wiimote are currently
201 * active and select a proper DRM that supports all of
202 * the requested data inputs.
204 static __u8
select_drm(struct wiimote_data
*wdata
)
206 return WIIPROTO_REQ_DRM_K
;
209 static void wiiproto_req_drm(struct wiimote_data
*wdata
, __u8 drm
)
213 if (drm
== WIIPROTO_REQ_NULL
)
214 drm
= select_drm(wdata
);
216 cmd
[0] = WIIPROTO_REQ_DRM
;
220 wiimote_queue(wdata
, cmd
, sizeof(cmd
));
223 static enum led_brightness
wiimote_leds_get(struct led_classdev
*led_dev
)
225 struct wiimote_data
*wdata
;
226 struct device
*dev
= led_dev
->dev
->parent
;
231 wdata
= hid_get_drvdata(container_of(dev
, struct hid_device
, dev
));
233 for (i
= 0; i
< 4; ++i
) {
234 if (wdata
->leds
[i
] == led_dev
) {
235 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
236 value
= wdata
->state
.flags
& WIIPROTO_FLAG_LED(i
+ 1);
237 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
242 return value
? LED_FULL
: LED_OFF
;
245 static void wiimote_leds_set(struct led_classdev
*led_dev
,
246 enum led_brightness value
)
248 struct wiimote_data
*wdata
;
249 struct device
*dev
= led_dev
->dev
->parent
;
254 wdata
= hid_get_drvdata(container_of(dev
, struct hid_device
, dev
));
256 for (i
= 0; i
< 4; ++i
) {
257 if (wdata
->leds
[i
] == led_dev
) {
258 flag
= WIIPROTO_FLAG_LED(i
+ 1);
259 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
260 state
= wdata
->state
.flags
;
261 if (value
== LED_OFF
)
262 wiiproto_req_leds(wdata
, state
& ~flag
);
264 wiiproto_req_leds(wdata
, state
| flag
);
265 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
271 static int wiimote_input_event(struct input_dev
*dev
, unsigned int type
,
272 unsigned int code
, int value
)
277 static int wiimote_input_open(struct input_dev
*dev
)
279 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
281 return hid_hw_open(wdata
->hdev
);
284 static void wiimote_input_close(struct input_dev
*dev
)
286 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
288 hid_hw_close(wdata
->hdev
);
291 static void handler_keys(struct wiimote_data
*wdata
, const __u8
*payload
)
293 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_LEFT
],
294 !!(payload
[0] & 0x01));
295 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_RIGHT
],
296 !!(payload
[0] & 0x02));
297 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_DOWN
],
298 !!(payload
[0] & 0x04));
299 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_UP
],
300 !!(payload
[0] & 0x08));
301 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_PLUS
],
302 !!(payload
[0] & 0x10));
303 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_TWO
],
304 !!(payload
[1] & 0x01));
305 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_ONE
],
306 !!(payload
[1] & 0x02));
307 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_B
],
308 !!(payload
[1] & 0x04));
309 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_A
],
310 !!(payload
[1] & 0x08));
311 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_MINUS
],
312 !!(payload
[1] & 0x10));
313 input_report_key(wdata
->input
, wiiproto_keymap
[WIIPROTO_KEY_HOME
],
314 !!(payload
[1] & 0x80));
315 input_sync(wdata
->input
);
318 static void handler_status(struct wiimote_data
*wdata
, const __u8
*payload
)
320 handler_keys(wdata
, payload
);
322 /* on status reports the drm is reset so we need to resend the drm */
323 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
326 static void handler_return(struct wiimote_data
*wdata
, const __u8
*payload
)
328 __u8 err
= payload
[3];
329 __u8 cmd
= payload
[2];
331 handler_keys(wdata
, payload
);
334 hid_warn(wdata
->hdev
, "Remote error %hhu on req %hhu\n", err
,
338 struct wiiproto_handler
{
341 void (*func
)(struct wiimote_data
*wdata
, const __u8
*payload
);
344 static struct wiiproto_handler handlers
[] = {
345 { .id
= WIIPROTO_REQ_STATUS
, .size
= 6, .func
= handler_status
},
346 { .id
= WIIPROTO_REQ_RETURN
, .size
= 4, .func
= handler_return
},
347 { .id
= WIIPROTO_REQ_DRM_K
, .size
= 2, .func
= handler_keys
},
351 static int wiimote_hid_event(struct hid_device
*hdev
, struct hid_report
*report
,
352 u8
*raw_data
, int size
)
354 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
355 struct wiiproto_handler
*h
;
362 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
364 for (i
= 0; handlers
[i
].id
; ++i
) {
366 if (h
->id
== raw_data
[0] && h
->size
< size
)
367 h
->func(wdata
, &raw_data
[1]);
370 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
375 static void wiimote_leds_destroy(struct wiimote_data
*wdata
)
378 struct led_classdev
*led
;
380 for (i
= 0; i
< 4; ++i
) {
381 if (wdata
->leds
[i
]) {
382 led
= wdata
->leds
[i
];
383 wdata
->leds
[i
] = NULL
;
384 led_classdev_unregister(led
);
390 static int wiimote_leds_create(struct wiimote_data
*wdata
)
393 struct device
*dev
= &wdata
->hdev
->dev
;
394 size_t namesz
= strlen(dev_name(dev
)) + 9;
395 struct led_classdev
*led
;
398 for (i
= 0; i
< 4; ++i
) {
399 led
= kzalloc(sizeof(struct led_classdev
) + namesz
, GFP_KERNEL
);
404 name
= (void*)&led
[1];
405 snprintf(name
, namesz
, "%s:blue:p%d", dev_name(dev
), i
);
408 led
->max_brightness
= 1;
409 led
->brightness_get
= wiimote_leds_get
;
410 led
->brightness_set
= wiimote_leds_set
;
412 ret
= led_classdev_register(dev
, led
);
417 wdata
->leds
[i
] = led
;
423 wiimote_leds_destroy(wdata
);
427 static struct wiimote_data
*wiimote_create(struct hid_device
*hdev
)
429 struct wiimote_data
*wdata
;
432 wdata
= kzalloc(sizeof(*wdata
), GFP_KERNEL
);
436 wdata
->input
= input_allocate_device();
443 hid_set_drvdata(hdev
, wdata
);
445 input_set_drvdata(wdata
->input
, wdata
);
446 wdata
->input
->event
= wiimote_input_event
;
447 wdata
->input
->open
= wiimote_input_open
;
448 wdata
->input
->close
= wiimote_input_close
;
449 wdata
->input
->dev
.parent
= &wdata
->hdev
->dev
;
450 wdata
->input
->id
.bustype
= wdata
->hdev
->bus
;
451 wdata
->input
->id
.vendor
= wdata
->hdev
->vendor
;
452 wdata
->input
->id
.product
= wdata
->hdev
->product
;
453 wdata
->input
->id
.version
= wdata
->hdev
->version
;
454 wdata
->input
->name
= WIIMOTE_NAME
;
456 set_bit(EV_KEY
, wdata
->input
->evbit
);
457 for (i
= 0; i
< WIIPROTO_KEY_COUNT
; ++i
)
458 set_bit(wiiproto_keymap
[i
], wdata
->input
->keybit
);
460 spin_lock_init(&wdata
->qlock
);
461 INIT_WORK(&wdata
->worker
, wiimote_worker
);
463 spin_lock_init(&wdata
->state
.lock
);
468 static void wiimote_destroy(struct wiimote_data
*wdata
)
470 wiimote_leds_destroy(wdata
);
472 input_unregister_device(wdata
->input
);
473 cancel_work_sync(&wdata
->worker
);
474 hid_hw_stop(wdata
->hdev
);
479 static int wiimote_hid_probe(struct hid_device
*hdev
,
480 const struct hid_device_id
*id
)
482 struct wiimote_data
*wdata
;
485 wdata
= wiimote_create(hdev
);
487 hid_err(hdev
, "Can't alloc device\n");
491 ret
= hid_parse(hdev
);
493 hid_err(hdev
, "HID parse failed\n");
497 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
499 hid_err(hdev
, "HW start failed\n");
503 ret
= input_register_device(wdata
->input
);
505 hid_err(hdev
, "Cannot register input device\n");
509 ret
= wiimote_leds_create(wdata
);
513 hid_info(hdev
, "New device registered\n");
515 /* by default set led1 after device initialization */
516 spin_lock_irq(&wdata
->state
.lock
);
517 wiiproto_req_leds(wdata
, WIIPROTO_FLAG_LED1
);
518 spin_unlock_irq(&wdata
->state
.lock
);
523 wiimote_destroy(wdata
);
529 input_free_device(wdata
->input
);
534 static void wiimote_hid_remove(struct hid_device
*hdev
)
536 struct wiimote_data
*wdata
= hid_get_drvdata(hdev
);
538 hid_info(hdev
, "Device removed\n");
539 wiimote_destroy(wdata
);
542 static const struct hid_device_id wiimote_hid_devices
[] = {
543 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO
,
544 USB_DEVICE_ID_NINTENDO_WIIMOTE
) },
547 MODULE_DEVICE_TABLE(hid
, wiimote_hid_devices
);
549 static struct hid_driver wiimote_hid_driver
= {
551 .id_table
= wiimote_hid_devices
,
552 .probe
= wiimote_hid_probe
,
553 .remove
= wiimote_hid_remove
,
554 .raw_event
= wiimote_hid_event
,
557 static int __init
wiimote_init(void)
561 ret
= hid_register_driver(&wiimote_hid_driver
);
563 pr_err("Can't register wiimote hid driver\n");
568 static void __exit
wiimote_exit(void)
570 hid_unregister_driver(&wiimote_hid_driver
);
573 module_init(wiimote_init
);
574 module_exit(wiimote_exit
);
575 MODULE_LICENSE("GPL");
576 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
577 MODULE_DESCRIPTION(WIIMOTE_NAME
" Device Driver");
578 MODULE_VERSION(WIIMOTE_VERSION
);