]>
Commit | Line | Data |
---|---|---|
fb51b443 DH |
1 | /* |
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 | ||
4d36e975 | 13 | #include <linux/atomic.h> |
672bc4e0 | 14 | #include <linux/device.h> |
02fb72a0 | 15 | #include <linux/hid.h> |
672bc4e0 | 16 | #include <linux/input.h> |
fb51b443 | 17 | #include <linux/module.h> |
23c063cb | 18 | #include <linux/spinlock.h> |
02fb72a0 | 19 | #include "hid-ids.h" |
fb51b443 DH |
20 | |
21 | #define WIIMOTE_VERSION "0.1" | |
22 | #define WIIMOTE_NAME "Nintendo Wii Remote" | |
23c063cb DH |
23 | #define WIIMOTE_BUFSIZE 32 |
24 | ||
25 | struct wiimote_buf { | |
26 | __u8 data[HID_MAX_BUFFER_SIZE]; | |
27 | size_t size; | |
28 | }; | |
fb51b443 | 29 | |
32a0d9a5 DH |
30 | struct wiimote_state { |
31 | spinlock_t lock; | |
32 | __u8 flags; | |
33 | }; | |
34 | ||
e894d0e3 | 35 | struct wiimote_data { |
4d36e975 | 36 | atomic_t ready; |
e894d0e3 | 37 | struct hid_device *hdev; |
672bc4e0 | 38 | struct input_dev *input; |
23c063cb DH |
39 | |
40 | spinlock_t qlock; | |
41 | __u8 head; | |
42 | __u8 tail; | |
43 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; | |
44 | struct work_struct worker; | |
32a0d9a5 DH |
45 | |
46 | struct wiimote_state state; | |
e894d0e3 DH |
47 | }; |
48 | ||
db308346 DH |
49 | #define WIIPROTO_FLAG_LED1 0x01 |
50 | #define WIIPROTO_FLAG_LED2 0x02 | |
51 | #define WIIPROTO_FLAG_LED3 0x04 | |
52 | #define WIIPROTO_FLAG_LED4 0x08 | |
32a0d9a5 DH |
53 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
54 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) | |
db308346 | 55 | |
1abb9ad3 | 56 | enum wiiproto_reqs { |
db308346 | 57 | WIIPROTO_REQ_LED = 0x11, |
1abb9ad3 DH |
58 | WIIPROTO_REQ_DRM_K = 0x30, |
59 | }; | |
60 | ||
61 | enum 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 | ||
76 | static __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 | ||
3c1c2fce DH |
90 | #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \ |
91 | dev)) | |
92 | ||
0c218f14 DH |
93 | static 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 | ||
23c063cb DH |
112 | static 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 | ||
132 | static 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 | ||
db308346 DH |
171 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
172 | { | |
173 | __u8 cmd[2]; | |
174 | ||
32a0d9a5 DH |
175 | 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 | ||
db308346 DH |
180 | 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 | ||
3c1c2fce DH |
195 | #define wiifs_led_show_set(num) \ |
196 | static 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 | } \ | |
212 | static 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 | } \ | |
236 | static DEVICE_ATTR(led##num, S_IRUGO | S_IWUSR, wiifs_led_show_##num, \ | |
237 | wiifs_led_set_##num) | |
238 | ||
239 | wiifs_led_show_set(1); | |
240 | wiifs_led_show_set(2); | |
241 | wiifs_led_show_set(3); | |
242 | wiifs_led_show_set(4); | |
243 | ||
672bc4e0 DH |
244 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
245 | unsigned int code, int value) | |
246 | { | |
4d36e975 DH |
247 | 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 | ||
672bc4e0 DH |
254 | return 0; |
255 | } | |
256 | ||
1abb9ad3 DH |
257 | static 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 | ||
a4d19197 DH |
284 | struct wiiproto_handler { |
285 | __u8 id; | |
286 | size_t size; | |
287 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); | |
288 | }; | |
289 | ||
290 | static struct wiiproto_handler handlers[] = { | |
1abb9ad3 | 291 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
a4d19197 DH |
292 | { .id = 0 } |
293 | }; | |
294 | ||
02fb72a0 DH |
295 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
296 | u8 *raw_data, int size) | |
297 | { | |
4d36e975 | 298 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
a4d19197 DH |
299 | struct wiiproto_handler *h; |
300 | int i; | |
32a0d9a5 | 301 | unsigned long flags; |
4d36e975 DH |
302 | |
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 | ||
02fb72a0 DH |
308 | if (size < 1) |
309 | return -EINVAL; | |
310 | ||
32a0d9a5 DH |
311 | spin_lock_irqsave(&wdata->state.lock, flags); |
312 | ||
a4d19197 DH |
313 | 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 | ||
32a0d9a5 DH |
319 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
320 | ||
02fb72a0 DH |
321 | return 0; |
322 | } | |
323 | ||
e894d0e3 DH |
324 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
325 | { | |
326 | struct wiimote_data *wdata; | |
1abb9ad3 | 327 | int i; |
e894d0e3 DH |
328 | |
329 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); | |
330 | if (!wdata) | |
331 | return NULL; | |
332 | ||
672bc4e0 DH |
333 | wdata->input = input_allocate_device(); |
334 | if (!wdata->input) { | |
335 | kfree(wdata); | |
336 | return NULL; | |
337 | } | |
338 | ||
e894d0e3 DH |
339 | wdata->hdev = hdev; |
340 | hid_set_drvdata(hdev, wdata); | |
341 | ||
672bc4e0 DH |
342 | 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 | ||
1abb9ad3 DH |
351 | 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 | ||
23c063cb DH |
355 | spin_lock_init(&wdata->qlock); |
356 | INIT_WORK(&wdata->worker, wiimote_worker); | |
357 | ||
32a0d9a5 DH |
358 | spin_lock_init(&wdata->state.lock); |
359 | ||
e894d0e3 DH |
360 | return wdata; |
361 | } | |
362 | ||
363 | static void wiimote_destroy(struct wiimote_data *wdata) | |
364 | { | |
365 | kfree(wdata); | |
366 | } | |
367 | ||
02fb72a0 DH |
368 | static int wiimote_hid_probe(struct hid_device *hdev, |
369 | const struct hid_device_id *id) | |
fb51b443 | 370 | { |
e894d0e3 | 371 | struct wiimote_data *wdata; |
02fb72a0 DH |
372 | int ret; |
373 | ||
e894d0e3 DH |
374 | wdata = wiimote_create(hdev); |
375 | if (!wdata) { | |
376 | hid_err(hdev, "Can't alloc device\n"); | |
377 | return -ENOMEM; | |
378 | } | |
379 | ||
3c1c2fce DH |
380 | 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 | ||
02fb72a0 DH |
393 | ret = hid_parse(hdev); |
394 | if (ret) { | |
395 | hid_err(hdev, "HID parse failed\n"); | |
e894d0e3 | 396 | goto err; |
02fb72a0 DH |
397 | } |
398 | ||
399 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
400 | if (ret) { | |
401 | hid_err(hdev, "HW start failed\n"); | |
e894d0e3 | 402 | goto err; |
02fb72a0 DH |
403 | } |
404 | ||
672bc4e0 DH |
405 | ret = input_register_device(wdata->input); |
406 | if (ret) { | |
407 | hid_err(hdev, "Cannot register input device\n"); | |
408 | goto err_stop; | |
409 | } | |
410 | ||
4d36e975 DH |
411 | /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ |
412 | smp_wmb(); | |
413 | atomic_set(&wdata->ready, 1); | |
02fb72a0 | 414 | hid_info(hdev, "New device registered\n"); |
32a0d9a5 DH |
415 | |
416 | /* by default set led1 after device initialization */ | |
417 | spin_lock_irq(&wdata->state.lock); | |
db308346 | 418 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
32a0d9a5 DH |
419 | spin_unlock_irq(&wdata->state.lock); |
420 | ||
fb51b443 | 421 | return 0; |
e894d0e3 | 422 | |
672bc4e0 DH |
423 | err_stop: |
424 | hid_hw_stop(hdev); | |
e894d0e3 | 425 | err: |
672bc4e0 | 426 | input_free_device(wdata->input); |
3c1c2fce DH |
427 | 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); | |
e894d0e3 DH |
431 | wiimote_destroy(wdata); |
432 | return ret; | |
fb51b443 DH |
433 | } |
434 | ||
02fb72a0 DH |
435 | static void wiimote_hid_remove(struct hid_device *hdev) |
436 | { | |
e894d0e3 DH |
437 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
438 | ||
02fb72a0 | 439 | hid_info(hdev, "Device removed\n"); |
23c063cb | 440 | |
3c1c2fce DH |
441 | 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 | ||
02fb72a0 | 446 | hid_hw_stop(hdev); |
672bc4e0 | 447 | input_unregister_device(wdata->input); |
23c063cb DH |
448 | |
449 | cancel_work_sync(&wdata->worker); | |
e894d0e3 | 450 | wiimote_destroy(wdata); |
02fb72a0 DH |
451 | } |
452 | ||
453 | static const struct hid_device_id wiimote_hid_devices[] = { | |
454 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, | |
455 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, | |
456 | { } | |
457 | }; | |
458 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); | |
459 | ||
460 | static 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 | ||
468 | static int __init wiimote_init(void) | |
469 | { | |
470 | 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; | |
477 | } | |
478 | ||
fb51b443 DH |
479 | static void __exit wiimote_exit(void) |
480 | { | |
02fb72a0 | 481 | hid_unregister_driver(&wiimote_hid_driver); |
fb51b443 DH |
482 | } |
483 | ||
484 | module_init(wiimote_init); | |
485 | module_exit(wiimote_exit); | |
486 | MODULE_LICENSE("GPL"); | |
487 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); | |
488 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); | |
489 | MODULE_VERSION(WIIMOTE_VERSION); |