blob: 680975436289482dae2d96ca9025d6da5e89ec8a [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 Herrmann672bc4e2011-07-05 13:45:11 +020013#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020014#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020015#include <linux/input.h>
David Herrmann23a5a4a2011-08-17 11:43:22 +020016#include <linux/leds.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 Herrmann32a0d9a2011-07-05 13:45:18 +020030struct wiimote_state {
31 spinlock_t lock;
32 __u8 flags;
33};
34
David Herrmanne894d0e2011-07-05 13:45:10 +020035struct wiimote_data {
36 struct hid_device *hdev;
David Herrmann672bc4e2011-07-05 13:45:11 +020037 struct input_dev *input;
David Herrmann23a5a4a2011-08-17 11:43:22 +020038 struct led_classdev *leds[4];
David Herrmann23c063c2011-07-05 13:45:14 +020039
40 spinlock_t qlock;
41 __u8 head;
42 __u8 tail;
43 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
44 struct work_struct worker;
David Herrmann32a0d9a2011-07-05 13:45:18 +020045
46 struct wiimote_state state;
David Herrmanne894d0e2011-07-05 13:45:10 +020047};
48
David Herrmannc003ec22011-09-06 13:50:26 +020049#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_FLAG_RUMBLE 0x10
David Herrmann32a0d9a2011-07-05 13:45:18 +020054#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
55 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
David Herrmanndb308342011-07-05 13:45:17 +020056
David Herrmann23a5a4a2011-08-17 11:43:22 +020057/* return flag for led \num */
58#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
59
David Herrmann1abb9ad2011-07-05 13:45:16 +020060enum wiiproto_reqs {
David Herrmann2cb5e4b2011-08-17 11:43:23 +020061 WIIPROTO_REQ_NULL = 0x0,
David Herrmannc003ec22011-09-06 13:50:26 +020062 WIIPROTO_REQ_RUMBLE = 0x10,
David Herrmanndb308342011-07-05 13:45:17 +020063 WIIPROTO_REQ_LED = 0x11,
David Herrmann2cb5e4b2011-08-17 11:43:23 +020064 WIIPROTO_REQ_DRM = 0x12,
David Herrmannc87019e2011-08-17 11:43:24 +020065 WIIPROTO_REQ_STATUS = 0x20,
66 WIIPROTO_REQ_RETURN = 0x22,
David Herrmann1abb9ad2011-07-05 13:45:16 +020067 WIIPROTO_REQ_DRM_K = 0x30,
68};
69
70enum wiiproto_keys {
71 WIIPROTO_KEY_LEFT,
72 WIIPROTO_KEY_RIGHT,
73 WIIPROTO_KEY_UP,
74 WIIPROTO_KEY_DOWN,
75 WIIPROTO_KEY_PLUS,
76 WIIPROTO_KEY_MINUS,
77 WIIPROTO_KEY_ONE,
78 WIIPROTO_KEY_TWO,
79 WIIPROTO_KEY_A,
80 WIIPROTO_KEY_B,
81 WIIPROTO_KEY_HOME,
82 WIIPROTO_KEY_COUNT
83};
84
85static __u16 wiiproto_keymap[] = {
86 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
87 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
88 KEY_UP, /* WIIPROTO_KEY_UP */
89 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
90 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
91 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
92 BTN_1, /* WIIPROTO_KEY_ONE */
93 BTN_2, /* WIIPROTO_KEY_TWO */
94 BTN_A, /* WIIPROTO_KEY_A */
95 BTN_B, /* WIIPROTO_KEY_B */
96 BTN_MODE, /* WIIPROTO_KEY_HOME */
97};
98
David Herrmann0c218f12011-07-05 13:45:13 +020099static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
100 size_t count)
101{
102 __u8 *buf;
103 ssize_t ret;
104
105 if (!hdev->hid_output_raw_report)
106 return -ENODEV;
107
108 buf = kmemdup(buffer, count, GFP_KERNEL);
109 if (!buf)
110 return -ENOMEM;
111
112 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
113
114 kfree(buf);
115 return ret;
116}
117
David Herrmann23c063c2011-07-05 13:45:14 +0200118static void wiimote_worker(struct work_struct *work)
119{
120 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
121 worker);
122 unsigned long flags;
123
124 spin_lock_irqsave(&wdata->qlock, flags);
125
126 while (wdata->head != wdata->tail) {
127 spin_unlock_irqrestore(&wdata->qlock, flags);
128 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
129 wdata->outq[wdata->tail].size);
130 spin_lock_irqsave(&wdata->qlock, flags);
131
132 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
133 }
134
135 spin_unlock_irqrestore(&wdata->qlock, flags);
136}
137
138static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
139 size_t count)
140{
141 unsigned long flags;
142 __u8 newhead;
143
144 if (count > HID_MAX_BUFFER_SIZE) {
145 hid_warn(wdata->hdev, "Sending too large output report\n");
146 return;
147 }
148
149 /*
150 * Copy new request into our output queue and check whether the
151 * queue is full. If it is full, discard this request.
152 * If it is empty we need to start a new worker that will
153 * send out the buffer to the hid device.
154 * If the queue is not empty, then there must be a worker
155 * that is currently sending out our buffer and this worker
156 * will reschedule itself until the queue is empty.
157 */
158
159 spin_lock_irqsave(&wdata->qlock, flags);
160
161 memcpy(wdata->outq[wdata->head].data, buffer, count);
162 wdata->outq[wdata->head].size = count;
163 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
164
165 if (wdata->head == wdata->tail) {
166 wdata->head = newhead;
167 schedule_work(&wdata->worker);
168 } else if (newhead != wdata->tail) {
169 wdata->head = newhead;
170 } else {
171 hid_warn(wdata->hdev, "Output queue is full");
172 }
173
174 spin_unlock_irqrestore(&wdata->qlock, flags);
175}
176
David Herrmannc003ec22011-09-06 13:50:26 +0200177/*
178 * This sets the rumble bit on the given output report if rumble is
179 * currently enabled.
180 * \cmd1 must point to the second byte in the output report => &cmd[1]
181 * This must be called on nearly every output report before passing it
182 * into the output queue!
183 */
184static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
185{
186 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
187 *cmd1 |= 0x01;
188}
189
190static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
191{
192 __u8 cmd[2];
193
194 rumble = !!rumble;
195 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
196 return;
197
198 if (rumble)
199 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
200 else
201 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
202
203 cmd[0] = WIIPROTO_REQ_RUMBLE;
204 cmd[1] = 0;
205
206 wiiproto_keep_rumble(wdata, &cmd[1]);
207 wiimote_queue(wdata, cmd, sizeof(cmd));
208}
209
David Herrmanndb308342011-07-05 13:45:17 +0200210static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
211{
212 __u8 cmd[2];
213
David Herrmann32a0d9a2011-07-05 13:45:18 +0200214 leds &= WIIPROTO_FLAGS_LEDS;
215 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
216 return;
217 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
218
David Herrmanndb308342011-07-05 13:45:17 +0200219 cmd[0] = WIIPROTO_REQ_LED;
220 cmd[1] = 0;
221
222 if (leds & WIIPROTO_FLAG_LED1)
223 cmd[1] |= 0x10;
224 if (leds & WIIPROTO_FLAG_LED2)
225 cmd[1] |= 0x20;
226 if (leds & WIIPROTO_FLAG_LED3)
227 cmd[1] |= 0x40;
228 if (leds & WIIPROTO_FLAG_LED4)
229 cmd[1] |= 0x80;
230
David Herrmannc003ec22011-09-06 13:50:26 +0200231 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200232 wiimote_queue(wdata, cmd, sizeof(cmd));
233}
234
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200235/*
236 * Check what peripherals of the wiimote are currently
237 * active and select a proper DRM that supports all of
238 * the requested data inputs.
239 */
240static __u8 select_drm(struct wiimote_data *wdata)
241{
242 return WIIPROTO_REQ_DRM_K;
243}
244
245static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
246{
247 __u8 cmd[3];
248
249 if (drm == WIIPROTO_REQ_NULL)
250 drm = select_drm(wdata);
251
252 cmd[0] = WIIPROTO_REQ_DRM;
253 cmd[1] = 0;
254 cmd[2] = drm;
255
David Herrmannc003ec22011-09-06 13:50:26 +0200256 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200257 wiimote_queue(wdata, cmd, sizeof(cmd));
258}
259
David Herrmann23a5a4a2011-08-17 11:43:22 +0200260static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
261{
262 struct wiimote_data *wdata;
263 struct device *dev = led_dev->dev->parent;
264 int i;
265 unsigned long flags;
266 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200267
David Herrmann23a5a4a2011-08-17 11:43:22 +0200268 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
269
270 for (i = 0; i < 4; ++i) {
271 if (wdata->leds[i] == led_dev) {
272 spin_lock_irqsave(&wdata->state.lock, flags);
273 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
274 spin_unlock_irqrestore(&wdata->state.lock, flags);
275 break;
276 }
277 }
278
279 return value ? LED_FULL : LED_OFF;
280}
281
282static void wiimote_leds_set(struct led_classdev *led_dev,
283 enum led_brightness value)
284{
285 struct wiimote_data *wdata;
286 struct device *dev = led_dev->dev->parent;
287 int i;
288 unsigned long flags;
289 __u8 state, flag;
290
291 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
292
293 for (i = 0; i < 4; ++i) {
294 if (wdata->leds[i] == led_dev) {
295 flag = WIIPROTO_FLAG_LED(i + 1);
296 spin_lock_irqsave(&wdata->state.lock, flags);
297 state = wdata->state.flags;
298 if (value == LED_OFF)
299 wiiproto_req_leds(wdata, state & ~flag);
300 else
301 wiiproto_req_leds(wdata, state | flag);
302 spin_unlock_irqrestore(&wdata->state.lock, flags);
303 break;
304 }
305 }
306}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200307
David Herrmann672bc4e2011-07-05 13:45:11 +0200308static int wiimote_input_event(struct input_dev *dev, unsigned int type,
309 unsigned int code, int value)
310{
311 return 0;
312}
313
David Herrmann26af1742011-08-17 11:43:21 +0200314static int wiimote_input_open(struct input_dev *dev)
315{
316 struct wiimote_data *wdata = input_get_drvdata(dev);
317
318 return hid_hw_open(wdata->hdev);
319}
320
321static void wiimote_input_close(struct input_dev *dev)
322{
323 struct wiimote_data *wdata = input_get_drvdata(dev);
324
325 hid_hw_close(wdata->hdev);
326}
327
David Herrmann1abb9ad2011-07-05 13:45:16 +0200328static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
329{
330 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
331 !!(payload[0] & 0x01));
332 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
333 !!(payload[0] & 0x02));
334 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
335 !!(payload[0] & 0x04));
336 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
337 !!(payload[0] & 0x08));
338 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
339 !!(payload[0] & 0x10));
340 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
341 !!(payload[1] & 0x01));
342 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
343 !!(payload[1] & 0x02));
344 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
345 !!(payload[1] & 0x04));
346 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
347 !!(payload[1] & 0x08));
348 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
349 !!(payload[1] & 0x10));
350 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
351 !!(payload[1] & 0x80));
352 input_sync(wdata->input);
353}
354
David Herrmannc87019e2011-08-17 11:43:24 +0200355static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
356{
357 handler_keys(wdata, payload);
358
359 /* on status reports the drm is reset so we need to resend the drm */
360 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
361}
362
363static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
364{
365 __u8 err = payload[3];
366 __u8 cmd = payload[2];
367
368 handler_keys(wdata, payload);
369
370 if (err)
371 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
372 cmd);
373}
374
David Herrmanna4d19192011-07-05 13:45:15 +0200375struct wiiproto_handler {
376 __u8 id;
377 size_t size;
378 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
379};
380
381static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200382 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
383 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200384 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmanna4d19192011-07-05 13:45:15 +0200385 { .id = 0 }
386};
387
David Herrmann02fb72a2011-07-05 13:45:09 +0200388static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
389 u8 *raw_data, int size)
390{
David Herrmann4d36e972011-07-05 13:45:12 +0200391 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200392 struct wiiproto_handler *h;
393 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200394 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +0200395
David Herrmann02fb72a2011-07-05 13:45:09 +0200396 if (size < 1)
397 return -EINVAL;
398
David Herrmann32a0d9a2011-07-05 13:45:18 +0200399 spin_lock_irqsave(&wdata->state.lock, flags);
400
David Herrmanna4d19192011-07-05 13:45:15 +0200401 for (i = 0; handlers[i].id; ++i) {
402 h = &handlers[i];
403 if (h->id == raw_data[0] && h->size < size)
404 h->func(wdata, &raw_data[1]);
405 }
406
David Herrmann32a0d9a2011-07-05 13:45:18 +0200407 spin_unlock_irqrestore(&wdata->state.lock, flags);
408
David Herrmann02fb72a2011-07-05 13:45:09 +0200409 return 0;
410}
411
David Herrmann23a5a4a2011-08-17 11:43:22 +0200412static void wiimote_leds_destroy(struct wiimote_data *wdata)
413{
414 int i;
415 struct led_classdev *led;
416
417 for (i = 0; i < 4; ++i) {
418 if (wdata->leds[i]) {
419 led = wdata->leds[i];
420 wdata->leds[i] = NULL;
421 led_classdev_unregister(led);
422 kfree(led);
423 }
424 }
425}
426
427static int wiimote_leds_create(struct wiimote_data *wdata)
428{
429 int i, ret;
430 struct device *dev = &wdata->hdev->dev;
431 size_t namesz = strlen(dev_name(dev)) + 9;
432 struct led_classdev *led;
433 char *name;
434
435 for (i = 0; i < 4; ++i) {
436 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
437 if (!led) {
438 ret = -ENOMEM;
439 goto err;
440 }
441 name = (void*)&led[1];
442 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
443 led->name = name;
444 led->brightness = 0;
445 led->max_brightness = 1;
446 led->brightness_get = wiimote_leds_get;
447 led->brightness_set = wiimote_leds_set;
448
449 ret = led_classdev_register(dev, led);
450 if (ret) {
451 kfree(led);
452 goto err;
453 }
454 wdata->leds[i] = led;
455 }
456
457 return 0;
458
459err:
460 wiimote_leds_destroy(wdata);
461 return ret;
462}
463
David Herrmanne894d0e2011-07-05 13:45:10 +0200464static struct wiimote_data *wiimote_create(struct hid_device *hdev)
465{
466 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +0200467 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +0200468
469 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
470 if (!wdata)
471 return NULL;
472
David Herrmann672bc4e2011-07-05 13:45:11 +0200473 wdata->input = input_allocate_device();
474 if (!wdata->input) {
475 kfree(wdata);
476 return NULL;
477 }
478
David Herrmanne894d0e2011-07-05 13:45:10 +0200479 wdata->hdev = hdev;
480 hid_set_drvdata(hdev, wdata);
481
David Herrmann672bc4e2011-07-05 13:45:11 +0200482 input_set_drvdata(wdata->input, wdata);
483 wdata->input->event = wiimote_input_event;
David Herrmann26af1742011-08-17 11:43:21 +0200484 wdata->input->open = wiimote_input_open;
485 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +0200486 wdata->input->dev.parent = &wdata->hdev->dev;
487 wdata->input->id.bustype = wdata->hdev->bus;
488 wdata->input->id.vendor = wdata->hdev->vendor;
489 wdata->input->id.product = wdata->hdev->product;
490 wdata->input->id.version = wdata->hdev->version;
491 wdata->input->name = WIIMOTE_NAME;
492
David Herrmann1abb9ad2011-07-05 13:45:16 +0200493 set_bit(EV_KEY, wdata->input->evbit);
494 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
495 set_bit(wiiproto_keymap[i], wdata->input->keybit);
496
David Herrmann23c063c2011-07-05 13:45:14 +0200497 spin_lock_init(&wdata->qlock);
498 INIT_WORK(&wdata->worker, wiimote_worker);
499
David Herrmann32a0d9a2011-07-05 13:45:18 +0200500 spin_lock_init(&wdata->state.lock);
501
David Herrmanne894d0e2011-07-05 13:45:10 +0200502 return wdata;
503}
504
505static void wiimote_destroy(struct wiimote_data *wdata)
506{
David Herrmann23a5a4a2011-08-17 11:43:22 +0200507 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200508
509 input_unregister_device(wdata->input);
510 cancel_work_sync(&wdata->worker);
511 hid_hw_stop(wdata->hdev);
512
David Herrmanne894d0e2011-07-05 13:45:10 +0200513 kfree(wdata);
514}
515
David Herrmann02fb72a2011-07-05 13:45:09 +0200516static int wiimote_hid_probe(struct hid_device *hdev,
517 const struct hid_device_id *id)
518{
David Herrmanne894d0e2011-07-05 13:45:10 +0200519 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200520 int ret;
521
David Herrmanne894d0e2011-07-05 13:45:10 +0200522 wdata = wiimote_create(hdev);
523 if (!wdata) {
524 hid_err(hdev, "Can't alloc device\n");
525 return -ENOMEM;
526 }
527
David Herrmann02fb72a2011-07-05 13:45:09 +0200528 ret = hid_parse(hdev);
529 if (ret) {
530 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200531 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200532 }
533
534 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
535 if (ret) {
536 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200537 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200538 }
539
David Herrmann672bc4e2011-07-05 13:45:11 +0200540 ret = input_register_device(wdata->input);
541 if (ret) {
542 hid_err(hdev, "Cannot register input device\n");
543 goto err_stop;
544 }
545
David Herrmann23a5a4a2011-08-17 11:43:22 +0200546 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200547 if (ret)
548 goto err_free;
549
David Herrmann02fb72a2011-07-05 13:45:09 +0200550 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +0200551
552 /* by default set led1 after device initialization */
553 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +0200554 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +0200555 spin_unlock_irq(&wdata->state.lock);
556
David Herrmann02fb72a2011-07-05 13:45:09 +0200557 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200558
David Herrmann3989ef62011-08-17 11:43:20 +0200559err_free:
560 wiimote_destroy(wdata);
561 return ret;
562
David Herrmann672bc4e2011-07-05 13:45:11 +0200563err_stop:
564 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200565err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200566 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +0200567 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +0200568 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200569}
570
571static void wiimote_hid_remove(struct hid_device *hdev)
572{
David Herrmanne894d0e2011-07-05 13:45:10 +0200573 struct wiimote_data *wdata = hid_get_drvdata(hdev);
574
David Herrmann02fb72a2011-07-05 13:45:09 +0200575 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200576 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200577}
578
579static const struct hid_device_id wiimote_hid_devices[] = {
580 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
581 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
582 { }
583};
584MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
585
586static struct hid_driver wiimote_hid_driver = {
587 .name = "wiimote",
588 .id_table = wiimote_hid_devices,
589 .probe = wiimote_hid_probe,
590 .remove = wiimote_hid_remove,
591 .raw_event = wiimote_hid_event,
592};
593
David Herrmannfb51b442011-07-05 13:45:08 +0200594static int __init wiimote_init(void)
595{
David Herrmann02fb72a2011-07-05 13:45:09 +0200596 int ret;
597
598 ret = hid_register_driver(&wiimote_hid_driver);
599 if (ret)
600 pr_err("Can't register wiimote hid driver\n");
601
602 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200603}
604
605static void __exit wiimote_exit(void)
606{
David Herrmann02fb72a2011-07-05 13:45:09 +0200607 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200608}
609
610module_init(wiimote_init);
611module_exit(wiimote_exit);
612MODULE_LICENSE("GPL");
613MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
614MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
615MODULE_VERSION(WIIMOTE_VERSION);