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