blob: a594383ce03db0458ea13f34638ed7f0eafb8b19 [file] [log] [blame]
David Herrmannfb51b442011-07-05 13:45:08 +02001/*
2 * HID driver for Nintendo Wiimote devices
3 * Copyright (c) 2011 David Herrmann
4 */
5
6/*
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)
10 * any later version.
11 */
12
David Herrmann4d36e972011-07-05 13:45:12 +020013#include <linux/atomic.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020014#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020015#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020016#include <linux/input.h>
David Herrmannfb51b442011-07-05 13:45:08 +020017#include <linux/module.h>
David Herrmann23c063c2011-07-05 13:45:14 +020018#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020019#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020020
21#define WIIMOTE_VERSION "0.1"
22#define WIIMOTE_NAME "Nintendo Wii Remote"
David Herrmann23c063c2011-07-05 13:45:14 +020023#define WIIMOTE_BUFSIZE 32
24
25struct wiimote_buf {
26 __u8 data[HID_MAX_BUFFER_SIZE];
27 size_t size;
28};
David Herrmannfb51b442011-07-05 13:45:08 +020029
David Herrmann32a0d9a2011-07-05 13:45:18 +020030struct wiimote_state {
31 spinlock_t lock;
32 __u8 flags;
33};
34
David Herrmanne894d0e2011-07-05 13:45:10 +020035struct wiimote_data {
David Herrmann4d36e972011-07-05 13:45:12 +020036 atomic_t ready;
David Herrmanne894d0e2011-07-05 13:45:10 +020037 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020038 struct input_dev *input;
David Herrmann23c063c2011-07-05 13:45:14 +020039
40 spinlock_t qlock;
41 __u8 head;
42 __u8 tail;
43 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
44 struct work_struct worker;
David Herrmann32a0d9a2011-07-05 13:45:18 +020045
46 struct wiimote_state state;
David Herrmanne894d0e2011-07-05 13:45:10 +020047};
48
David Herrmanndb308342011-07-05 13:45:17 +020049#define WIIPROTO_FLAG_LED1 0x01
50#define WIIPROTO_FLAG_LED2 0x02
51#define WIIPROTO_FLAG_LED3 0x04
52#define WIIPROTO_FLAG_LED4 0x08
David Herrmann32a0d9a2011-07-05 13:45:18 +020053#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
54 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
David Herrmanndb308342011-07-05 13:45:17 +020055
David Herrmann1abb9ad2011-07-05 13:45:16 +020056enum wiiproto_reqs {
David Herrmanndb308342011-07-05 13:45:17 +020057 WIIPROTO_REQ_LED = 0x11,
David Herrmann1abb9ad2011-07-05 13:45:16 +020058 WIIPROTO_REQ_DRM_K = 0x30,
59};
60
61enum wiiproto_keys {
62 WIIPROTO_KEY_LEFT,
63 WIIPROTO_KEY_RIGHT,
64 WIIPROTO_KEY_UP,
65 WIIPROTO_KEY_DOWN,
66 WIIPROTO_KEY_PLUS,
67 WIIPROTO_KEY_MINUS,
68 WIIPROTO_KEY_ONE,
69 WIIPROTO_KEY_TWO,
70 WIIPROTO_KEY_A,
71 WIIPROTO_KEY_B,
72 WIIPROTO_KEY_HOME,
73 WIIPROTO_KEY_COUNT
74};
75
76static __u16 wiiproto_keymap[] = {
77 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
78 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
79 KEY_UP, /* WIIPROTO_KEY_UP */
80 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
81 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
82 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
83 BTN_1, /* WIIPROTO_KEY_ONE */
84 BTN_2, /* WIIPROTO_KEY_TWO */
85 BTN_A, /* WIIPROTO_KEY_A */
86 BTN_B, /* WIIPROTO_KEY_B */
87 BTN_MODE, /* WIIPROTO_KEY_HOME */
88};
89
David Herrmann3c1c2fc2011-07-05 13:45:19 +020090#define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
91 dev))
92
David Herrmann0c218f12011-07-05 13:45:13 +020093static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
94 size_t count)
95{
96 __u8 *buf;
97 ssize_t ret;
98
99 if (!hdev->hid_output_raw_report)
100 return -ENODEV;
101
102 buf = kmemdup(buffer, count, GFP_KERNEL);
103 if (!buf)
104 return -ENOMEM;
105
106 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
107
108 kfree(buf);
109 return ret;
110}
111
David Herrmann23c063c2011-07-05 13:45:14 +0200112static void wiimote_worker(struct work_struct *work)
113{
114 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
115 worker);
116 unsigned long flags;
117
118 spin_lock_irqsave(&wdata->qlock, flags);
119
120 while (wdata->head != wdata->tail) {
121 spin_unlock_irqrestore(&wdata->qlock, flags);
122 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
123 wdata->outq[wdata->tail].size);
124 spin_lock_irqsave(&wdata->qlock, flags);
125
126 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
127 }
128
129 spin_unlock_irqrestore(&wdata->qlock, flags);
130}
131
132static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
133 size_t count)
134{
135 unsigned long flags;
136 __u8 newhead;
137
138 if (count > HID_MAX_BUFFER_SIZE) {
139 hid_warn(wdata->hdev, "Sending too large output report\n");
140 return;
141 }
142
143 /*
144 * Copy new request into our output queue and check whether the
145 * queue is full. If it is full, discard this request.
146 * If it is empty we need to start a new worker that will
147 * send out the buffer to the hid device.
148 * If the queue is not empty, then there must be a worker
149 * that is currently sending out our buffer and this worker
150 * will reschedule itself until the queue is empty.
151 */
152
153 spin_lock_irqsave(&wdata->qlock, flags);
154
155 memcpy(wdata->outq[wdata->head].data, buffer, count);
156 wdata->outq[wdata->head].size = count;
157 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
158
159 if (wdata->head == wdata->tail) {
160 wdata->head = newhead;
161 schedule_work(&wdata->worker);
162 } else if (newhead != wdata->tail) {
163 wdata->head = newhead;
164 } else {
165 hid_warn(wdata->hdev, "Output queue is full");
166 }
167
168 spin_unlock_irqrestore(&wdata->qlock, flags);
169}
170
David Herrmanndb308342011-07-05 13:45:17 +0200171static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
172{
173 __u8 cmd[2];
174
David Herrmann32a0d9a2011-07-05 13:45:18 +0200175 leds &= WIIPROTO_FLAGS_LEDS;
176 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
177 return;
178 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
179
David Herrmanndb308342011-07-05 13:45:17 +0200180 cmd[0] = WIIPROTO_REQ_LED;
181 cmd[1] = 0;
182
183 if (leds & WIIPROTO_FLAG_LED1)
184 cmd[1] |= 0x10;
185 if (leds & WIIPROTO_FLAG_LED2)
186 cmd[1] |= 0x20;
187 if (leds & WIIPROTO_FLAG_LED3)
188 cmd[1] |= 0x40;
189 if (leds & WIIPROTO_FLAG_LED4)
190 cmd[1] |= 0x80;
191
192 wiimote_queue(wdata, cmd, sizeof(cmd));
193}
194
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200195#define wiifs_led_show_set(num) \
196static ssize_t wiifs_led_show_##num(struct device *dev, \
197 struct device_attribute *attr, char *buf) \
198{ \
199 struct wiimote_data *wdata = dev_to_wii(dev); \
200 unsigned long flags; \
201 int state; \
202 \
203 if (!atomic_read(&wdata->ready)) \
204 return -EBUSY; \
205 \
206 spin_lock_irqsave(&wdata->state.lock, flags); \
207 state = !!(wdata->state.flags & WIIPROTO_FLAG_LED##num); \
208 spin_unlock_irqrestore(&wdata->state.lock, flags); \
209 \
210 return sprintf(buf, "%d\n", state); \
211} \
212static ssize_t wiifs_led_set_##num(struct device *dev, \
213 struct device_attribute *attr, const char *buf, size_t count) \
214{ \
215 struct wiimote_data *wdata = dev_to_wii(dev); \
216 int tmp = simple_strtoul(buf, NULL, 10); \
217 unsigned long flags; \
218 __u8 state; \
219 \
220 if (!atomic_read(&wdata->ready)) \
221 return -EBUSY; \
222 \
223 spin_lock_irqsave(&wdata->state.lock, flags); \
224 \
225 state = wdata->state.flags; \
226 \
227 if (tmp) \
228 wiiproto_req_leds(wdata, state | WIIPROTO_FLAG_LED##num);\
229 else \
230 wiiproto_req_leds(wdata, state & ~WIIPROTO_FLAG_LED##num);\
231 \
232 spin_unlock_irqrestore(&wdata->state.lock, flags); \
233 \
234 return count; \
235} \
236static DEVICE_ATTR(led##num, S_IRUGO | S_IWUSR, wiifs_led_show_##num, \
237 wiifs_led_set_##num)
238
239wiifs_led_show_set(1);
240wiifs_led_show_set(2);
241wiifs_led_show_set(3);
242wiifs_led_show_set(4);
243
David Herrmann672bc4e2011-07-05 13:45:11 +0200244static int wiimote_input_event(struct input_dev *dev, unsigned int type,
245 unsigned int code, int value)
246{
David Herrmann4d36e972011-07-05 13:45:12 +0200247 struct wiimote_data *wdata = input_get_drvdata(dev);
248
249 if (!atomic_read(&wdata->ready))
250 return -EBUSY;
251 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
252 smp_rmb();
253
David Herrmann672bc4e2011-07-05 13:45:11 +0200254 return 0;
255}
256
David Herrmann1abb9ad2011-07-05 13:45:16 +0200257static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
258{
259 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
260 !!(payload[0] & 0x01));
261 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
262 !!(payload[0] & 0x02));
263 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
264 !!(payload[0] & 0x04));
265 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
266 !!(payload[0] & 0x08));
267 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
268 !!(payload[0] & 0x10));
269 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
270 !!(payload[1] & 0x01));
271 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
272 !!(payload[1] & 0x02));
273 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
274 !!(payload[1] & 0x04));
275 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
276 !!(payload[1] & 0x08));
277 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
278 !!(payload[1] & 0x10));
279 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
280 !!(payload[1] & 0x80));
281 input_sync(wdata->input);
282}
283
David Herrmanna4d19192011-07-05 13:45:15 +0200284struct wiiproto_handler {
285 __u8 id;
286 size_t size;
287 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
288};
289
290static struct wiiproto_handler handlers[] = {
David Herrmann1abb9ad2011-07-05 13:45:16 +0200291 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmanna4d19192011-07-05 13:45:15 +0200292 { .id = 0 }
293};
294
David Herrmann02fb72a2011-07-05 13:45:09 +0200295static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
296 u8 *raw_data, int size)
297{
David Herrmann4d36e972011-07-05 13:45:12 +0200298 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200299 struct wiiproto_handler *h;
300 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200301 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +0200302
303 if (!atomic_read(&wdata->ready))
304 return -EBUSY;
305 /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
306 smp_rmb();
307
David Herrmann02fb72a2011-07-05 13:45:09 +0200308 if (size < 1)
309 return -EINVAL;
310
David Herrmann32a0d9a2011-07-05 13:45:18 +0200311 spin_lock_irqsave(&wdata->state.lock, flags);
312
David Herrmanna4d19192011-07-05 13:45:15 +0200313 for (i = 0; handlers[i].id; ++i) {
314 h = &handlers[i];
315 if (h->id == raw_data[0] && h->size < size)
316 h->func(wdata, &raw_data[1]);
317 }
318
David Herrmann32a0d9a2011-07-05 13:45:18 +0200319 spin_unlock_irqrestore(&wdata->state.lock, flags);
320
David Herrmann02fb72a2011-07-05 13:45:09 +0200321 return 0;
322}
323
David Herrmanne894d0e2011-07-05 13:45:10 +0200324static struct wiimote_data *wiimote_create(struct hid_device *hdev)
325{
326 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +0200327 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +0200328
329 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
330 if (!wdata)
331 return NULL;
332
David Herrmann672bc4e2011-07-05 13:45:11 +0200333 wdata->input = input_allocate_device();
334 if (!wdata->input) {
335 kfree(wdata);
336 return NULL;
337 }
338
David Herrmanne894d0e2011-07-05 13:45:10 +0200339 wdata->hdev = hdev;
340 hid_set_drvdata(hdev, wdata);
341
David Herrmann672bc4e2011-07-05 13:45:11 +0200342 input_set_drvdata(wdata->input, wdata);
343 wdata->input->event = wiimote_input_event;
344 wdata->input->dev.parent = &wdata->hdev->dev;
345 wdata->input->id.bustype = wdata->hdev->bus;
346 wdata->input->id.vendor = wdata->hdev->vendor;
347 wdata->input->id.product = wdata->hdev->product;
348 wdata->input->id.version = wdata->hdev->version;
349 wdata->input->name = WIIMOTE_NAME;
350
David Herrmann1abb9ad2011-07-05 13:45:16 +0200351 set_bit(EV_KEY, wdata->input->evbit);
352 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
353 set_bit(wiiproto_keymap[i], wdata->input->keybit);
354
David Herrmann23c063c2011-07-05 13:45:14 +0200355 spin_lock_init(&wdata->qlock);
356 INIT_WORK(&wdata->worker, wiimote_worker);
357
David Herrmann32a0d9a2011-07-05 13:45:18 +0200358 spin_lock_init(&wdata->state.lock);
359
David Herrmanne894d0e2011-07-05 13:45:10 +0200360 return wdata;
361}
362
363static void wiimote_destroy(struct wiimote_data *wdata)
364{
365 kfree(wdata);
366}
367
David Herrmann02fb72a2011-07-05 13:45:09 +0200368static int wiimote_hid_probe(struct hid_device *hdev,
369 const struct hid_device_id *id)
370{
David Herrmanne894d0e2011-07-05 13:45:10 +0200371 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200372 int ret;
373
David Herrmanne894d0e2011-07-05 13:45:10 +0200374 wdata = wiimote_create(hdev);
375 if (!wdata) {
376 hid_err(hdev, "Can't alloc device\n");
377 return -ENOMEM;
378 }
379
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200380 ret = device_create_file(&hdev->dev, &dev_attr_led1);
381 if (ret)
382 goto err;
383 ret = device_create_file(&hdev->dev, &dev_attr_led2);
384 if (ret)
385 goto err;
386 ret = device_create_file(&hdev->dev, &dev_attr_led3);
387 if (ret)
388 goto err;
389 ret = device_create_file(&hdev->dev, &dev_attr_led4);
390 if (ret)
391 goto err;
392
David Herrmann02fb72a2011-07-05 13:45:09 +0200393 ret = hid_parse(hdev);
394 if (ret) {
395 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200396 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200397 }
398
399 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
400 if (ret) {
401 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200402 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200403 }
404
David Herrmann672bc4e2011-07-05 13:45:11 +0200405 ret = input_register_device(wdata->input);
406 if (ret) {
407 hid_err(hdev, "Cannot register input device\n");
408 goto err_stop;
409 }
410
David Herrmann4d36e972011-07-05 13:45:12 +0200411 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
412 smp_wmb();
413 atomic_set(&wdata->ready, 1);
David Herrmann02fb72a2011-07-05 13:45:09 +0200414 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +0200415
416 /* by default set led1 after device initialization */
417 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +0200418 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +0200419 spin_unlock_irq(&wdata->state.lock);
420
David Herrmann02fb72a2011-07-05 13:45:09 +0200421 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200422
David Herrmann672bc4e2011-07-05 13:45:11 +0200423err_stop:
424 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200425err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200426 input_free_device(wdata->input);
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200427 device_remove_file(&hdev->dev, &dev_attr_led1);
428 device_remove_file(&hdev->dev, &dev_attr_led2);
429 device_remove_file(&hdev->dev, &dev_attr_led3);
430 device_remove_file(&hdev->dev, &dev_attr_led4);
David Herrmanne894d0e2011-07-05 13:45:10 +0200431 wiimote_destroy(wdata);
432 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200433}
434
435static void wiimote_hid_remove(struct hid_device *hdev)
436{
David Herrmanne894d0e2011-07-05 13:45:10 +0200437 struct wiimote_data *wdata = hid_get_drvdata(hdev);
438
David Herrmann02fb72a2011-07-05 13:45:09 +0200439 hid_info(hdev, "Device removed\n");
David Herrmann23c063c2011-07-05 13:45:14 +0200440
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200441 device_remove_file(&hdev->dev, &dev_attr_led1);
442 device_remove_file(&hdev->dev, &dev_attr_led2);
443 device_remove_file(&hdev->dev, &dev_attr_led3);
444 device_remove_file(&hdev->dev, &dev_attr_led4);
445
David Herrmann02fb72a2011-07-05 13:45:09 +0200446 hid_hw_stop(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +0200447 input_unregister_device(wdata->input);
David Herrmann23c063c2011-07-05 13:45:14 +0200448
449 cancel_work_sync(&wdata->worker);
David Herrmanne894d0e2011-07-05 13:45:10 +0200450 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200451}
452
453static const struct hid_device_id wiimote_hid_devices[] = {
454 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
455 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
456 { }
457};
458MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
459
460static struct hid_driver wiimote_hid_driver = {
461 .name = "wiimote",
462 .id_table = wiimote_hid_devices,
463 .probe = wiimote_hid_probe,
464 .remove = wiimote_hid_remove,
465 .raw_event = wiimote_hid_event,
466};
467
David Herrmannfb51b442011-07-05 13:45:08 +0200468static int __init wiimote_init(void)
469{
David Herrmann02fb72a2011-07-05 13:45:09 +0200470 int ret;
471
472 ret = hid_register_driver(&wiimote_hid_driver);
473 if (ret)
474 pr_err("Can't register wiimote hid driver\n");
475
476 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200477}
478
479static void __exit wiimote_exit(void)
480{
David Herrmann02fb72a2011-07-05 13:45:09 +0200481 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200482}
483
484module_init(wiimote_init);
485module_exit(wiimote_exit);
486MODULE_LICENSE("GPL");
487MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
488MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
489MODULE_VERSION(WIIMOTE_VERSION);