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