HID: wiimote: Cache wiimote led state
[deliverable/linux.git] / drivers / hid / hid-wiimote.c
CommitLineData
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
25struct wiimote_buf {
26 __u8 data[HID_MAX_BUFFER_SIZE];
27 size_t size;
28};
fb51b443 29
32a0d9a5
DH
30struct wiimote_state {
31 spinlock_t lock;
32 __u8 flags;
33};
34
e894d0e3 35struct 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 56enum wiiproto_reqs {
db308346 57 WIIPROTO_REQ_LED = 0x11,
1abb9ad3
DH
58 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
0c218f14
DH
90static 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
23c063cb
DH
109static 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
129static 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
db308346
DH
168static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
169{
170 __u8 cmd[2];
171
32a0d9a5
DH
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
db308346
DH
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
672bc4e0
DH
192static int wiimote_input_event(struct input_dev *dev, unsigned int type,
193 unsigned int code, int value)
194{
4d36e975
DH
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
672bc4e0
DH
202 return 0;
203}
204
1abb9ad3
DH
205static 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
a4d19197
DH
232struct wiiproto_handler {
233 __u8 id;
234 size_t size;
235 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
236};
237
238static struct wiiproto_handler handlers[] = {
1abb9ad3 239 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
a4d19197
DH
240 { .id = 0 }
241};
242
02fb72a0
DH
243static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
244 u8 *raw_data, int size)
245{
4d36e975 246 struct wiimote_data *wdata = hid_get_drvdata(hdev);
a4d19197
DH
247 struct wiiproto_handler *h;
248 int i;
32a0d9a5 249 unsigned long flags;
4d36e975
DH
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
02fb72a0
DH
256 if (size < 1)
257 return -EINVAL;
258
32a0d9a5
DH
259 spin_lock_irqsave(&wdata->state.lock, flags);
260
a4d19197
DH
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
32a0d9a5
DH
267 spin_unlock_irqrestore(&wdata->state.lock, flags);
268
02fb72a0
DH
269 return 0;
270}
271
e894d0e3
DH
272static struct wiimote_data *wiimote_create(struct hid_device *hdev)
273{
274 struct wiimote_data *wdata;
1abb9ad3 275 int i;
e894d0e3
DH
276
277 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
278 if (!wdata)
279 return NULL;
280
672bc4e0
DH
281 wdata->input = input_allocate_device();
282 if (!wdata->input) {
283 kfree(wdata);
284 return NULL;
285 }
286
e894d0e3
DH
287 wdata->hdev = hdev;
288 hid_set_drvdata(hdev, wdata);
289
672bc4e0
DH
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
1abb9ad3
DH
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
23c063cb
DH
303 spin_lock_init(&wdata->qlock);
304 INIT_WORK(&wdata->worker, wiimote_worker);
305
32a0d9a5
DH
306 spin_lock_init(&wdata->state.lock);
307
e894d0e3
DH
308 return wdata;
309}
310
311static void wiimote_destroy(struct wiimote_data *wdata)
312{
313 kfree(wdata);
314}
315
02fb72a0
DH
316static int wiimote_hid_probe(struct hid_device *hdev,
317 const struct hid_device_id *id)
fb51b443 318{
e894d0e3 319 struct wiimote_data *wdata;
02fb72a0
DH
320 int ret;
321
e894d0e3
DH
322 wdata = wiimote_create(hdev);
323 if (!wdata) {
324 hid_err(hdev, "Can't alloc device\n");
325 return -ENOMEM;
326 }
327
02fb72a0
DH
328 ret = hid_parse(hdev);
329 if (ret) {
330 hid_err(hdev, "HID parse failed\n");
e894d0e3 331 goto err;
02fb72a0
DH
332 }
333
334 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
335 if (ret) {
336 hid_err(hdev, "HW start failed\n");
e894d0e3 337 goto err;
02fb72a0
DH
338 }
339
672bc4e0
DH
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
4d36e975
DH
346 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
347 smp_wmb();
348 atomic_set(&wdata->ready, 1);
02fb72a0 349 hid_info(hdev, "New device registered\n");
32a0d9a5
DH
350
351 /* by default set led1 after device initialization */
352 spin_lock_irq(&wdata->state.lock);
db308346 353 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
32a0d9a5
DH
354 spin_unlock_irq(&wdata->state.lock);
355
fb51b443 356 return 0;
e894d0e3 357
672bc4e0
DH
358err_stop:
359 hid_hw_stop(hdev);
e894d0e3 360err:
672bc4e0 361 input_free_device(wdata->input);
e894d0e3
DH
362 wiimote_destroy(wdata);
363 return ret;
fb51b443
DH
364}
365
02fb72a0
DH
366static void wiimote_hid_remove(struct hid_device *hdev)
367{
e894d0e3
DH
368 struct wiimote_data *wdata = hid_get_drvdata(hdev);
369
02fb72a0 370 hid_info(hdev, "Device removed\n");
23c063cb 371
02fb72a0 372 hid_hw_stop(hdev);
672bc4e0 373 input_unregister_device(wdata->input);
23c063cb
DH
374
375 cancel_work_sync(&wdata->worker);
e894d0e3 376 wiimote_destroy(wdata);
02fb72a0
DH
377}
378
379static const struct hid_device_id wiimote_hid_devices[] = {
380 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
381 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
382 { }
383};
384MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
385
386static 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
394static 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
fb51b443
DH
405static void __exit wiimote_exit(void)
406{
02fb72a0 407 hid_unregister_driver(&wiimote_hid_driver);
fb51b443
DH
408}
409
410module_init(wiimote_init);
411module_exit(wiimote_exit);
412MODULE_LICENSE("GPL");
413MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
414MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
415MODULE_VERSION(WIIMOTE_VERSION);
This page took 0.042239 seconds and 5 git commands to generate.