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_DRM_K = 0x30,
81 static __u16 wiiproto_keymap[] = {
82 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
83 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
84 KEY_UP, /* WIIPROTO_KEY_UP */
85 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
86 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
87 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
88 BTN_1, /* WIIPROTO_KEY_ONE */
89 BTN_2, /* WIIPROTO_KEY_TWO */
90 BTN_A, /* WIIPROTO_KEY_A */
91 BTN_B, /* WIIPROTO_KEY_B */
92 BTN_MODE, /* WIIPROTO_KEY_HOME */
95 static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
101 if (!hdev->hid_output_raw_report)
104 buf = kmemdup(buffer, count, GFP_KERNEL);
108 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
114 static void wiimote_worker(struct work_struct *work)
116 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
120 spin_lock_irqsave(&wdata->qlock, flags);
122 while (wdata->head != wdata->tail) {
123 spin_unlock_irqrestore(&wdata->qlock, flags);
124 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
125 wdata->outq[wdata->tail].size);
126 spin_lock_irqsave(&wdata->qlock, flags);
128 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
131 spin_unlock_irqrestore(&wdata->qlock, flags);
134 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
140 if (count > HID_MAX_BUFFER_SIZE) {
141 hid_warn(wdata->hdev, "Sending too large output report\n");
146 * Copy new request into our output queue and check whether the
147 * queue is full. If it is full, discard this request.
148 * If it is empty we need to start a new worker that will
149 * send out the buffer to the hid device.
150 * If the queue is not empty, then there must be a worker
151 * that is currently sending out our buffer and this worker
152 * will reschedule itself until the queue is empty.
155 spin_lock_irqsave(&wdata->qlock, flags);
157 memcpy(wdata->outq[wdata->head].data, buffer, count);
158 wdata->outq[wdata->head].size = count;
159 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
161 if (wdata->head == wdata->tail) {
162 wdata->head = newhead;
163 schedule_work(&wdata->worker);
164 } else if (newhead != wdata->tail) {
165 wdata->head = newhead;
167 hid_warn(wdata->hdev, "Output queue is full");
170 spin_unlock_irqrestore(&wdata->qlock, flags);
173 static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
177 leds &= WIIPROTO_FLAGS_LEDS;
178 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
180 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
182 cmd[0] = WIIPROTO_REQ_LED;
185 if (leds & WIIPROTO_FLAG_LED1)
187 if (leds & WIIPROTO_FLAG_LED2)
189 if (leds & WIIPROTO_FLAG_LED3)
191 if (leds & WIIPROTO_FLAG_LED4)
194 wiimote_queue(wdata, cmd, sizeof(cmd));
198 * Check what peripherals of the wiimote are currently
199 * active and select a proper DRM that supports all of
200 * the requested data inputs.
202 static __u8 select_drm(struct wiimote_data *wdata)
204 return WIIPROTO_REQ_DRM_K;
207 static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
211 if (drm == WIIPROTO_REQ_NULL)
212 drm = select_drm(wdata);
214 cmd[0] = WIIPROTO_REQ_DRM;
218 wiimote_queue(wdata, cmd, sizeof(cmd));
221 static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
223 struct wiimote_data *wdata;
224 struct device *dev = led_dev->dev->parent;
229 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
231 for (i = 0; i < 4; ++i) {
232 if (wdata->leds[i] == led_dev) {
233 spin_lock_irqsave(&wdata->state.lock, flags);
234 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
235 spin_unlock_irqrestore(&wdata->state.lock, flags);
240 return value ? LED_FULL : LED_OFF;
243 static void wiimote_leds_set(struct led_classdev *led_dev,
244 enum led_brightness value)
246 struct wiimote_data *wdata;
247 struct device *dev = led_dev->dev->parent;
252 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
254 for (i = 0; i < 4; ++i) {
255 if (wdata->leds[i] == led_dev) {
256 flag = WIIPROTO_FLAG_LED(i + 1);
257 spin_lock_irqsave(&wdata->state.lock, flags);
258 state = wdata->state.flags;
259 if (value == LED_OFF)
260 wiiproto_req_leds(wdata, state & ~flag);
262 wiiproto_req_leds(wdata, state | flag);
263 spin_unlock_irqrestore(&wdata->state.lock, flags);
269 static int wiimote_input_event(struct input_dev *dev, unsigned int type,
270 unsigned int code, int value)
275 static int wiimote_input_open(struct input_dev *dev)
277 struct wiimote_data *wdata = input_get_drvdata(dev);
279 return hid_hw_open(wdata->hdev);
282 static void wiimote_input_close(struct input_dev *dev)
284 struct wiimote_data *wdata = input_get_drvdata(dev);
286 hid_hw_close(wdata->hdev);
289 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
291 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
292 !!(payload[0] & 0x01));
293 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
294 !!(payload[0] & 0x02));
295 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
296 !!(payload[0] & 0x04));
297 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
298 !!(payload[0] & 0x08));
299 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
300 !!(payload[0] & 0x10));
301 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
302 !!(payload[1] & 0x01));
303 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
304 !!(payload[1] & 0x02));
305 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
306 !!(payload[1] & 0x04));
307 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
308 !!(payload[1] & 0x08));
309 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
310 !!(payload[1] & 0x10));
311 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
312 !!(payload[1] & 0x80));
313 input_sync(wdata->input);
316 struct wiiproto_handler {
319 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
322 static struct wiiproto_handler handlers[] = {
323 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
327 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
328 u8 *raw_data, int size)
330 struct wiimote_data *wdata = hid_get_drvdata(hdev);
331 struct wiiproto_handler *h;
338 spin_lock_irqsave(&wdata->state.lock, flags);
340 for (i = 0; handlers[i].id; ++i) {
342 if (h->id == raw_data[0] && h->size < size)
343 h->func(wdata, &raw_data[1]);
346 spin_unlock_irqrestore(&wdata->state.lock, flags);
351 static void wiimote_leds_destroy(struct wiimote_data *wdata)
354 struct led_classdev *led;
356 for (i = 0; i < 4; ++i) {
357 if (wdata->leds[i]) {
358 led = wdata->leds[i];
359 wdata->leds[i] = NULL;
360 led_classdev_unregister(led);
366 static int wiimote_leds_create(struct wiimote_data *wdata)
369 struct device *dev = &wdata->hdev->dev;
370 size_t namesz = strlen(dev_name(dev)) + 9;
371 struct led_classdev *led;
374 for (i = 0; i < 4; ++i) {
375 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
380 name = (void*)&led[1];
381 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
384 led->max_brightness = 1;
385 led->brightness_get = wiimote_leds_get;
386 led->brightness_set = wiimote_leds_set;
388 ret = led_classdev_register(dev, led);
393 wdata->leds[i] = led;
399 wiimote_leds_destroy(wdata);
403 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
405 struct wiimote_data *wdata;
408 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
412 wdata->input = input_allocate_device();
419 hid_set_drvdata(hdev, wdata);
421 input_set_drvdata(wdata->input, wdata);
422 wdata->input->event = wiimote_input_event;
423 wdata->input->open = wiimote_input_open;
424 wdata->input->close = wiimote_input_close;
425 wdata->input->dev.parent = &wdata->hdev->dev;
426 wdata->input->id.bustype = wdata->hdev->bus;
427 wdata->input->id.vendor = wdata->hdev->vendor;
428 wdata->input->id.product = wdata->hdev->product;
429 wdata->input->id.version = wdata->hdev->version;
430 wdata->input->name = WIIMOTE_NAME;
432 set_bit(EV_KEY, wdata->input->evbit);
433 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
434 set_bit(wiiproto_keymap[i], wdata->input->keybit);
436 spin_lock_init(&wdata->qlock);
437 INIT_WORK(&wdata->worker, wiimote_worker);
439 spin_lock_init(&wdata->state.lock);
444 static void wiimote_destroy(struct wiimote_data *wdata)
446 wiimote_leds_destroy(wdata);
448 input_unregister_device(wdata->input);
449 cancel_work_sync(&wdata->worker);
450 hid_hw_stop(wdata->hdev);
455 static int wiimote_hid_probe(struct hid_device *hdev,
456 const struct hid_device_id *id)
458 struct wiimote_data *wdata;
461 wdata = wiimote_create(hdev);
463 hid_err(hdev, "Can't alloc device\n");
467 ret = hid_parse(hdev);
469 hid_err(hdev, "HID parse failed\n");
473 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
475 hid_err(hdev, "HW start failed\n");
479 ret = input_register_device(wdata->input);
481 hid_err(hdev, "Cannot register input device\n");
485 ret = wiimote_leds_create(wdata);
489 hid_info(hdev, "New device registered\n");
491 /* by default set led1 after device initialization */
492 spin_lock_irq(&wdata->state.lock);
493 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
494 spin_unlock_irq(&wdata->state.lock);
499 wiimote_destroy(wdata);
505 input_free_device(wdata->input);
510 static void wiimote_hid_remove(struct hid_device *hdev)
512 struct wiimote_data *wdata = hid_get_drvdata(hdev);
514 hid_info(hdev, "Device removed\n");
515 wiimote_destroy(wdata);
518 static const struct hid_device_id wiimote_hid_devices[] = {
519 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
520 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
523 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
525 static struct hid_driver wiimote_hid_driver = {
527 .id_table = wiimote_hid_devices,
528 .probe = wiimote_hid_probe,
529 .remove = wiimote_hid_remove,
530 .raw_event = wiimote_hid_event,
533 static int __init wiimote_init(void)
537 ret = hid_register_driver(&wiimote_hid_driver);
539 pr_err("Can't register wiimote hid driver\n");
544 static void __exit wiimote_exit(void)
546 hid_unregister_driver(&wiimote_hid_driver);
549 module_init(wiimote_init);
550 module_exit(wiimote_exit);
551 MODULE_LICENSE("GPL");
552 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
553 MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
554 MODULE_VERSION(WIIMOTE_VERSION);