]>
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 | |
e894d0e3 | 30 | struct wiimote_data { |
4d36e975 | 31 | atomic_t ready; |
e894d0e3 | 32 | struct hid_device *hdev; |
672bc4e0 | 33 | struct input_dev *input; |
23c063cb DH |
34 | |
35 | spinlock_t qlock; | |
36 | __u8 head; | |
37 | __u8 tail; | |
38 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; | |
39 | struct work_struct worker; | |
e894d0e3 DH |
40 | }; |
41 | ||
db308346 DH |
42 | #define WIIPROTO_FLAG_LED1 0x01 |
43 | #define WIIPROTO_FLAG_LED2 0x02 | |
44 | #define WIIPROTO_FLAG_LED3 0x04 | |
45 | #define WIIPROTO_FLAG_LED4 0x08 | |
46 | ||
1abb9ad3 | 47 | enum wiiproto_reqs { |
db308346 | 48 | WIIPROTO_REQ_LED = 0x11, |
1abb9ad3 DH |
49 | WIIPROTO_REQ_DRM_K = 0x30, |
50 | }; | |
51 | ||
52 | enum wiiproto_keys { | |
53 | WIIPROTO_KEY_LEFT, | |
54 | WIIPROTO_KEY_RIGHT, | |
55 | WIIPROTO_KEY_UP, | |
56 | WIIPROTO_KEY_DOWN, | |
57 | WIIPROTO_KEY_PLUS, | |
58 | WIIPROTO_KEY_MINUS, | |
59 | WIIPROTO_KEY_ONE, | |
60 | WIIPROTO_KEY_TWO, | |
61 | WIIPROTO_KEY_A, | |
62 | WIIPROTO_KEY_B, | |
63 | WIIPROTO_KEY_HOME, | |
64 | WIIPROTO_KEY_COUNT | |
65 | }; | |
66 | ||
67 | static __u16 wiiproto_keymap[] = { | |
68 | KEY_LEFT, /* WIIPROTO_KEY_LEFT */ | |
69 | KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ | |
70 | KEY_UP, /* WIIPROTO_KEY_UP */ | |
71 | KEY_DOWN, /* WIIPROTO_KEY_DOWN */ | |
72 | KEY_NEXT, /* WIIPROTO_KEY_PLUS */ | |
73 | KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ | |
74 | BTN_1, /* WIIPROTO_KEY_ONE */ | |
75 | BTN_2, /* WIIPROTO_KEY_TWO */ | |
76 | BTN_A, /* WIIPROTO_KEY_A */ | |
77 | BTN_B, /* WIIPROTO_KEY_B */ | |
78 | BTN_MODE, /* WIIPROTO_KEY_HOME */ | |
79 | }; | |
80 | ||
0c218f14 DH |
81 | static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, |
82 | size_t count) | |
83 | { | |
84 | __u8 *buf; | |
85 | ssize_t ret; | |
86 | ||
87 | if (!hdev->hid_output_raw_report) | |
88 | return -ENODEV; | |
89 | ||
90 | buf = kmemdup(buffer, count, GFP_KERNEL); | |
91 | if (!buf) | |
92 | return -ENOMEM; | |
93 | ||
94 | ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); | |
95 | ||
96 | kfree(buf); | |
97 | return ret; | |
98 | } | |
99 | ||
23c063cb DH |
100 | static void wiimote_worker(struct work_struct *work) |
101 | { | |
102 | struct wiimote_data *wdata = container_of(work, struct wiimote_data, | |
103 | worker); | |
104 | unsigned long flags; | |
105 | ||
106 | spin_lock_irqsave(&wdata->qlock, flags); | |
107 | ||
108 | while (wdata->head != wdata->tail) { | |
109 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
110 | wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, | |
111 | wdata->outq[wdata->tail].size); | |
112 | spin_lock_irqsave(&wdata->qlock, flags); | |
113 | ||
114 | wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; | |
115 | } | |
116 | ||
117 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
118 | } | |
119 | ||
120 | static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, | |
121 | size_t count) | |
122 | { | |
123 | unsigned long flags; | |
124 | __u8 newhead; | |
125 | ||
126 | if (count > HID_MAX_BUFFER_SIZE) { | |
127 | hid_warn(wdata->hdev, "Sending too large output report\n"); | |
128 | return; | |
129 | } | |
130 | ||
131 | /* | |
132 | * Copy new request into our output queue and check whether the | |
133 | * queue is full. If it is full, discard this request. | |
134 | * If it is empty we need to start a new worker that will | |
135 | * send out the buffer to the hid device. | |
136 | * If the queue is not empty, then there must be a worker | |
137 | * that is currently sending out our buffer and this worker | |
138 | * will reschedule itself until the queue is empty. | |
139 | */ | |
140 | ||
141 | spin_lock_irqsave(&wdata->qlock, flags); | |
142 | ||
143 | memcpy(wdata->outq[wdata->head].data, buffer, count); | |
144 | wdata->outq[wdata->head].size = count; | |
145 | newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; | |
146 | ||
147 | if (wdata->head == wdata->tail) { | |
148 | wdata->head = newhead; | |
149 | schedule_work(&wdata->worker); | |
150 | } else if (newhead != wdata->tail) { | |
151 | wdata->head = newhead; | |
152 | } else { | |
153 | hid_warn(wdata->hdev, "Output queue is full"); | |
154 | } | |
155 | ||
156 | spin_unlock_irqrestore(&wdata->qlock, flags); | |
157 | } | |
158 | ||
db308346 DH |
159 | static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) |
160 | { | |
161 | __u8 cmd[2]; | |
162 | ||
163 | cmd[0] = WIIPROTO_REQ_LED; | |
164 | cmd[1] = 0; | |
165 | ||
166 | if (leds & WIIPROTO_FLAG_LED1) | |
167 | cmd[1] |= 0x10; | |
168 | if (leds & WIIPROTO_FLAG_LED2) | |
169 | cmd[1] |= 0x20; | |
170 | if (leds & WIIPROTO_FLAG_LED3) | |
171 | cmd[1] |= 0x40; | |
172 | if (leds & WIIPROTO_FLAG_LED4) | |
173 | cmd[1] |= 0x80; | |
174 | ||
175 | wiimote_queue(wdata, cmd, sizeof(cmd)); | |
176 | } | |
177 | ||
672bc4e0 DH |
178 | static int wiimote_input_event(struct input_dev *dev, unsigned int type, |
179 | unsigned int code, int value) | |
180 | { | |
4d36e975 DH |
181 | struct wiimote_data *wdata = input_get_drvdata(dev); |
182 | ||
183 | if (!atomic_read(&wdata->ready)) | |
184 | return -EBUSY; | |
185 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ | |
186 | smp_rmb(); | |
187 | ||
672bc4e0 DH |
188 | return 0; |
189 | } | |
190 | ||
1abb9ad3 DH |
191 | static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) |
192 | { | |
193 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], | |
194 | !!(payload[0] & 0x01)); | |
195 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], | |
196 | !!(payload[0] & 0x02)); | |
197 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], | |
198 | !!(payload[0] & 0x04)); | |
199 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], | |
200 | !!(payload[0] & 0x08)); | |
201 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], | |
202 | !!(payload[0] & 0x10)); | |
203 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], | |
204 | !!(payload[1] & 0x01)); | |
205 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], | |
206 | !!(payload[1] & 0x02)); | |
207 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], | |
208 | !!(payload[1] & 0x04)); | |
209 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], | |
210 | !!(payload[1] & 0x08)); | |
211 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], | |
212 | !!(payload[1] & 0x10)); | |
213 | input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], | |
214 | !!(payload[1] & 0x80)); | |
215 | input_sync(wdata->input); | |
216 | } | |
217 | ||
a4d19197 DH |
218 | struct wiiproto_handler { |
219 | __u8 id; | |
220 | size_t size; | |
221 | void (*func)(struct wiimote_data *wdata, const __u8 *payload); | |
222 | }; | |
223 | ||
224 | static struct wiiproto_handler handlers[] = { | |
1abb9ad3 | 225 | { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys }, |
a4d19197 DH |
226 | { .id = 0 } |
227 | }; | |
228 | ||
02fb72a0 DH |
229 | static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, |
230 | u8 *raw_data, int size) | |
231 | { | |
4d36e975 | 232 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
a4d19197 DH |
233 | struct wiiproto_handler *h; |
234 | int i; | |
4d36e975 DH |
235 | |
236 | if (!atomic_read(&wdata->ready)) | |
237 | return -EBUSY; | |
238 | /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ | |
239 | smp_rmb(); | |
240 | ||
02fb72a0 DH |
241 | if (size < 1) |
242 | return -EINVAL; | |
243 | ||
a4d19197 DH |
244 | for (i = 0; handlers[i].id; ++i) { |
245 | h = &handlers[i]; | |
246 | if (h->id == raw_data[0] && h->size < size) | |
247 | h->func(wdata, &raw_data[1]); | |
248 | } | |
249 | ||
02fb72a0 DH |
250 | return 0; |
251 | } | |
252 | ||
e894d0e3 DH |
253 | static struct wiimote_data *wiimote_create(struct hid_device *hdev) |
254 | { | |
255 | struct wiimote_data *wdata; | |
1abb9ad3 | 256 | int i; |
e894d0e3 DH |
257 | |
258 | wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); | |
259 | if (!wdata) | |
260 | return NULL; | |
261 | ||
672bc4e0 DH |
262 | wdata->input = input_allocate_device(); |
263 | if (!wdata->input) { | |
264 | kfree(wdata); | |
265 | return NULL; | |
266 | } | |
267 | ||
e894d0e3 DH |
268 | wdata->hdev = hdev; |
269 | hid_set_drvdata(hdev, wdata); | |
270 | ||
672bc4e0 DH |
271 | input_set_drvdata(wdata->input, wdata); |
272 | wdata->input->event = wiimote_input_event; | |
273 | wdata->input->dev.parent = &wdata->hdev->dev; | |
274 | wdata->input->id.bustype = wdata->hdev->bus; | |
275 | wdata->input->id.vendor = wdata->hdev->vendor; | |
276 | wdata->input->id.product = wdata->hdev->product; | |
277 | wdata->input->id.version = wdata->hdev->version; | |
278 | wdata->input->name = WIIMOTE_NAME; | |
279 | ||
1abb9ad3 DH |
280 | set_bit(EV_KEY, wdata->input->evbit); |
281 | for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) | |
282 | set_bit(wiiproto_keymap[i], wdata->input->keybit); | |
283 | ||
23c063cb DH |
284 | spin_lock_init(&wdata->qlock); |
285 | INIT_WORK(&wdata->worker, wiimote_worker); | |
286 | ||
e894d0e3 DH |
287 | return wdata; |
288 | } | |
289 | ||
290 | static void wiimote_destroy(struct wiimote_data *wdata) | |
291 | { | |
292 | kfree(wdata); | |
293 | } | |
294 | ||
02fb72a0 DH |
295 | static int wiimote_hid_probe(struct hid_device *hdev, |
296 | const struct hid_device_id *id) | |
fb51b443 | 297 | { |
e894d0e3 | 298 | struct wiimote_data *wdata; |
02fb72a0 DH |
299 | int ret; |
300 | ||
e894d0e3 DH |
301 | wdata = wiimote_create(hdev); |
302 | if (!wdata) { | |
303 | hid_err(hdev, "Can't alloc device\n"); | |
304 | return -ENOMEM; | |
305 | } | |
306 | ||
02fb72a0 DH |
307 | ret = hid_parse(hdev); |
308 | if (ret) { | |
309 | hid_err(hdev, "HID parse failed\n"); | |
e894d0e3 | 310 | goto err; |
02fb72a0 DH |
311 | } |
312 | ||
313 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
314 | if (ret) { | |
315 | hid_err(hdev, "HW start failed\n"); | |
e894d0e3 | 316 | goto err; |
02fb72a0 DH |
317 | } |
318 | ||
672bc4e0 DH |
319 | ret = input_register_device(wdata->input); |
320 | if (ret) { | |
321 | hid_err(hdev, "Cannot register input device\n"); | |
322 | goto err_stop; | |
323 | } | |
324 | ||
4d36e975 DH |
325 | /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ |
326 | smp_wmb(); | |
327 | atomic_set(&wdata->ready, 1); | |
02fb72a0 | 328 | hid_info(hdev, "New device registered\n"); |
db308346 | 329 | wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); |
fb51b443 | 330 | return 0; |
e894d0e3 | 331 | |
672bc4e0 DH |
332 | err_stop: |
333 | hid_hw_stop(hdev); | |
e894d0e3 | 334 | err: |
672bc4e0 | 335 | input_free_device(wdata->input); |
e894d0e3 DH |
336 | wiimote_destroy(wdata); |
337 | return ret; | |
fb51b443 DH |
338 | } |
339 | ||
02fb72a0 DH |
340 | static void wiimote_hid_remove(struct hid_device *hdev) |
341 | { | |
e894d0e3 DH |
342 | struct wiimote_data *wdata = hid_get_drvdata(hdev); |
343 | ||
02fb72a0 | 344 | hid_info(hdev, "Device removed\n"); |
23c063cb | 345 | |
02fb72a0 | 346 | hid_hw_stop(hdev); |
672bc4e0 | 347 | input_unregister_device(wdata->input); |
23c063cb DH |
348 | |
349 | cancel_work_sync(&wdata->worker); | |
e894d0e3 | 350 | wiimote_destroy(wdata); |
02fb72a0 DH |
351 | } |
352 | ||
353 | static const struct hid_device_id wiimote_hid_devices[] = { | |
354 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, | |
355 | USB_DEVICE_ID_NINTENDO_WIIMOTE) }, | |
356 | { } | |
357 | }; | |
358 | MODULE_DEVICE_TABLE(hid, wiimote_hid_devices); | |
359 | ||
360 | static struct hid_driver wiimote_hid_driver = { | |
361 | .name = "wiimote", | |
362 | .id_table = wiimote_hid_devices, | |
363 | .probe = wiimote_hid_probe, | |
364 | .remove = wiimote_hid_remove, | |
365 | .raw_event = wiimote_hid_event, | |
366 | }; | |
367 | ||
368 | static int __init wiimote_init(void) | |
369 | { | |
370 | int ret; | |
371 | ||
372 | ret = hid_register_driver(&wiimote_hid_driver); | |
373 | if (ret) | |
374 | pr_err("Can't register wiimote hid driver\n"); | |
375 | ||
376 | return ret; | |
377 | } | |
378 | ||
fb51b443 DH |
379 | static void __exit wiimote_exit(void) |
380 | { | |
02fb72a0 | 381 | hid_unregister_driver(&wiimote_hid_driver); |
fb51b443 DH |
382 | } |
383 | ||
384 | module_init(wiimote_init); | |
385 | module_exit(wiimote_exit); | |
386 | MODULE_LICENSE("GPL"); | |
387 | MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); | |
388 | MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); | |
389 | MODULE_VERSION(WIIMOTE_VERSION); |