blob: 3fb18fbe080cb1ff426438977a95b1c78c480f14 [file] [log] [blame]
David Herrmannfb51b442011-07-05 13:45:08 +02001/*
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
David Herrmann4d36e972011-07-05 13:45:12 +020013#include <linux/atomic.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020014#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020015#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020016#include <linux/input.h>
David Herrmannfb51b442011-07-05 13:45:08 +020017#include <linux/module.h>
David Herrmann23c063c2011-07-05 13:45:14 +020018#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020019#include "hid-ids.h"
David Herrmannfb51b442011-07-05 13:45:08 +020020
21#define WIIMOTE_VERSION "0.1"
22#define WIIMOTE_NAME "Nintendo Wii Remote"
David Herrmann23c063c2011-07-05 13:45:14 +020023#define WIIMOTE_BUFSIZE 32
24
25struct wiimote_buf {
26 __u8 data[HID_MAX_BUFFER_SIZE];
27 size_t size;
28};
David Herrmannfb51b442011-07-05 13:45:08 +020029
David Herrmanne894d0e2011-07-05 13:45:10 +020030struct wiimote_data {
David Herrmann4d36e972011-07-05 13:45:12 +020031 atomic_t ready;
David Herrmanne894d0e2011-07-05 13:45:10 +020032 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020033 struct input_dev *input;
David Herrmann23c063c2011-07-05 13:45:14 +020034
35 spinlock_t qlock;
36 __u8 head;
37 __u8 tail;
38 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
39 struct work_struct worker;
David Herrmanne894d0e2011-07-05 13:45:10 +020040};
41
David Herrmanndb308342011-07-05 13:45:17 +020042#define WIIPROTO_FLAG_LED1 0x01
43#define WIIPROTO_FLAG_LED2 0x02
44#define WIIPROTO_FLAG_LED3 0x04
45#define WIIPROTO_FLAG_LED4 0x08
46
David Herrmann1abb9ad2011-07-05 13:45:16 +020047enum wiiproto_reqs {
David Herrmanndb308342011-07-05 13:45:17 +020048 WIIPROTO_REQ_LED = 0x11,
David Herrmann1abb9ad2011-07-05 13:45:16 +020049 WIIPROTO_REQ_DRM_K = 0x30,
50};
51
52enum 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
67static __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
David Herrmann0c218f12011-07-05 13:45:13 +020081static 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
David Herrmann23c063c2011-07-05 13:45:14 +0200100static 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
120static 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
David Herrmanndb308342011-07-05 13:45:17 +0200159static 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
David Herrmann672bc4e2011-07-05 13:45:11 +0200178static int wiimote_input_event(struct input_dev *dev, unsigned int type,
179 unsigned int code, int value)
180{
David Herrmann4d36e972011-07-05 13:45:12 +0200181 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
David Herrmann672bc4e2011-07-05 13:45:11 +0200188 return 0;
189}
190
David Herrmann1abb9ad2011-07-05 13:45:16 +0200191static 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
David Herrmanna4d19192011-07-05 13:45:15 +0200218struct wiiproto_handler {
219 __u8 id;
220 size_t size;
221 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
222};
223
224static struct wiiproto_handler handlers[] = {
David Herrmann1abb9ad2011-07-05 13:45:16 +0200225 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmanna4d19192011-07-05 13:45:15 +0200226 { .id = 0 }
227};
228
David Herrmann02fb72a2011-07-05 13:45:09 +0200229static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
230 u8 *raw_data, int size)
231{
David Herrmann4d36e972011-07-05 13:45:12 +0200232 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200233 struct wiiproto_handler *h;
234 int i;
David Herrmann4d36e972011-07-05 13:45:12 +0200235
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
David Herrmann02fb72a2011-07-05 13:45:09 +0200241 if (size < 1)
242 return -EINVAL;
243
David Herrmanna4d19192011-07-05 13:45:15 +0200244 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
David Herrmann02fb72a2011-07-05 13:45:09 +0200250 return 0;
251}
252
David Herrmanne894d0e2011-07-05 13:45:10 +0200253static struct wiimote_data *wiimote_create(struct hid_device *hdev)
254{
255 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +0200256 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +0200257
258 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
259 if (!wdata)
260 return NULL;
261
David Herrmann672bc4e2011-07-05 13:45:11 +0200262 wdata->input = input_allocate_device();
263 if (!wdata->input) {
264 kfree(wdata);
265 return NULL;
266 }
267
David Herrmanne894d0e2011-07-05 13:45:10 +0200268 wdata->hdev = hdev;
269 hid_set_drvdata(hdev, wdata);
270
David Herrmann672bc4e2011-07-05 13:45:11 +0200271 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
David Herrmann1abb9ad2011-07-05 13:45:16 +0200280 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
David Herrmann23c063c2011-07-05 13:45:14 +0200284 spin_lock_init(&wdata->qlock);
285 INIT_WORK(&wdata->worker, wiimote_worker);
286
David Herrmanne894d0e2011-07-05 13:45:10 +0200287 return wdata;
288}
289
290static void wiimote_destroy(struct wiimote_data *wdata)
291{
292 kfree(wdata);
293}
294
David Herrmann02fb72a2011-07-05 13:45:09 +0200295static int wiimote_hid_probe(struct hid_device *hdev,
296 const struct hid_device_id *id)
297{
David Herrmanne894d0e2011-07-05 13:45:10 +0200298 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200299 int ret;
300
David Herrmanne894d0e2011-07-05 13:45:10 +0200301 wdata = wiimote_create(hdev);
302 if (!wdata) {
303 hid_err(hdev, "Can't alloc device\n");
304 return -ENOMEM;
305 }
306
David Herrmann02fb72a2011-07-05 13:45:09 +0200307 ret = hid_parse(hdev);
308 if (ret) {
309 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200310 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200311 }
312
313 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
314 if (ret) {
315 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200316 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200317 }
318
David Herrmann672bc4e2011-07-05 13:45:11 +0200319 ret = input_register_device(wdata->input);
320 if (ret) {
321 hid_err(hdev, "Cannot register input device\n");
322 goto err_stop;
323 }
324
David Herrmann4d36e972011-07-05 13:45:12 +0200325 /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
326 smp_wmb();
327 atomic_set(&wdata->ready, 1);
David Herrmann02fb72a2011-07-05 13:45:09 +0200328 hid_info(hdev, "New device registered\n");
David Herrmanndb308342011-07-05 13:45:17 +0200329 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann02fb72a2011-07-05 13:45:09 +0200330 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200331
David Herrmann672bc4e2011-07-05 13:45:11 +0200332err_stop:
333 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200334err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200335 input_free_device(wdata->input);
David Herrmanne894d0e2011-07-05 13:45:10 +0200336 wiimote_destroy(wdata);
337 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200338}
339
340static void wiimote_hid_remove(struct hid_device *hdev)
341{
David Herrmanne894d0e2011-07-05 13:45:10 +0200342 struct wiimote_data *wdata = hid_get_drvdata(hdev);
343
David Herrmann02fb72a2011-07-05 13:45:09 +0200344 hid_info(hdev, "Device removed\n");
David Herrmann23c063c2011-07-05 13:45:14 +0200345
David Herrmann02fb72a2011-07-05 13:45:09 +0200346 hid_hw_stop(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +0200347 input_unregister_device(wdata->input);
David Herrmann23c063c2011-07-05 13:45:14 +0200348
349 cancel_work_sync(&wdata->worker);
David Herrmanne894d0e2011-07-05 13:45:10 +0200350 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200351}
352
353static const struct hid_device_id wiimote_hid_devices[] = {
354 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
355 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
356 { }
357};
358MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
359
360static 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
David Herrmannfb51b442011-07-05 13:45:08 +0200368static int __init wiimote_init(void)
369{
David Herrmann02fb72a2011-07-05 13:45:09 +0200370 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;
David Herrmannfb51b442011-07-05 13:45:08 +0200377}
378
379static void __exit wiimote_exit(void)
380{
David Herrmann02fb72a2011-07-05 13:45:09 +0200381 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200382}
383
384module_init(wiimote_init);
385module_exit(wiimote_exit);
386MODULE_LICENSE("GPL");
387MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
388MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
389MODULE_VERSION(WIIMOTE_VERSION);