HID: wiimote: Add sysfs support to wiimote driver
[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 #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
91                                                                         dev))
92
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
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
171 static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
172 {
173         __u8 cmd[2];
174
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
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
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
244 static int wiimote_input_event(struct input_dev *dev, unsigned int type,
245                                                 unsigned int code, int value)
246 {
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
254         return 0;
255 }
256
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
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[] = {
291         { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
292         { .id = 0 }
293 };
294
295 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
296                                                         u8 *raw_data, int size)
297 {
298         struct wiimote_data *wdata = hid_get_drvdata(hdev);
299         struct wiiproto_handler *h;
300         int i;
301         unsigned long flags;
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
308         if (size < 1)
309                 return -EINVAL;
310
311         spin_lock_irqsave(&wdata->state.lock, flags);
312
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
319         spin_unlock_irqrestore(&wdata->state.lock, flags);
320
321         return 0;
322 }
323
324 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
325 {
326         struct wiimote_data *wdata;
327         int i;
328
329         wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
330         if (!wdata)
331                 return NULL;
332
333         wdata->input = input_allocate_device();
334         if (!wdata->input) {
335                 kfree(wdata);
336                 return NULL;
337         }
338
339         wdata->hdev = hdev;
340         hid_set_drvdata(hdev, wdata);
341
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
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
355         spin_lock_init(&wdata->qlock);
356         INIT_WORK(&wdata->worker, wiimote_worker);
357
358         spin_lock_init(&wdata->state.lock);
359
360         return wdata;
361 }
362
363 static void wiimote_destroy(struct wiimote_data *wdata)
364 {
365         kfree(wdata);
366 }
367
368 static int wiimote_hid_probe(struct hid_device *hdev,
369                                 const struct hid_device_id *id)
370 {
371         struct wiimote_data *wdata;
372         int ret;
373
374         wdata = wiimote_create(hdev);
375         if (!wdata) {
376                 hid_err(hdev, "Can't alloc device\n");
377                 return -ENOMEM;
378         }
379
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
393         ret = hid_parse(hdev);
394         if (ret) {
395                 hid_err(hdev, "HID parse failed\n");
396                 goto err;
397         }
398
399         ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
400         if (ret) {
401                 hid_err(hdev, "HW start failed\n");
402                 goto err;
403         }
404
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
411         /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
412         smp_wmb();
413         atomic_set(&wdata->ready, 1);
414         hid_info(hdev, "New device registered\n");
415
416         /* by default set led1 after device initialization */
417         spin_lock_irq(&wdata->state.lock);
418         wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
419         spin_unlock_irq(&wdata->state.lock);
420
421         return 0;
422
423 err_stop:
424         hid_hw_stop(hdev);
425 err:
426         input_free_device(wdata->input);
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);
431         wiimote_destroy(wdata);
432         return ret;
433 }
434
435 static void wiimote_hid_remove(struct hid_device *hdev)
436 {
437         struct wiimote_data *wdata = hid_get_drvdata(hdev);
438
439         hid_info(hdev, "Device removed\n");
440
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
446         hid_hw_stop(hdev);
447         input_unregister_device(wdata->input);
448
449         cancel_work_sync(&wdata->worker);
450         wiimote_destroy(wdata);
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
479 static void __exit wiimote_exit(void)
480 {
481         hid_unregister_driver(&wiimote_hid_driver);
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);