blob: 57faac5272300990b898df824e426718b5de6851 [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 Herrmannd020be92011-09-06 13:50:27 +0200308static int wiimote_ff_play(struct input_dev *dev, void *data,
309 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200310{
David Herrmannd020be92011-09-06 13:50:27 +0200311 struct wiimote_data *wdata = input_get_drvdata(dev);
312 __u8 value;
313 unsigned long flags;
314
315 /*
316 * The wiimote supports only a single rumble motor so if any magnitude
317 * is set to non-zero then we start the rumble motor. If both are set to
318 * zero, we stop the rumble motor.
319 */
320
321 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
322 value = 1;
323 else
324 value = 0;
325
326 spin_lock_irqsave(&wdata->state.lock, flags);
327 wiiproto_req_rumble(wdata, value);
328 spin_unlock_irqrestore(&wdata->state.lock, flags);
329
David Herrmann672bc4e2011-07-05 13:45:11 +0200330 return 0;
331}
332
David Herrmann26af1742011-08-17 11:43:21 +0200333static int wiimote_input_open(struct input_dev *dev)
334{
335 struct wiimote_data *wdata = input_get_drvdata(dev);
336
337 return hid_hw_open(wdata->hdev);
338}
339
340static void wiimote_input_close(struct input_dev *dev)
341{
342 struct wiimote_data *wdata = input_get_drvdata(dev);
343
344 hid_hw_close(wdata->hdev);
345}
346
David Herrmann1abb9ad2011-07-05 13:45:16 +0200347static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
348{
349 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
350 !!(payload[0] & 0x01));
351 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
352 !!(payload[0] & 0x02));
353 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
354 !!(payload[0] & 0x04));
355 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
356 !!(payload[0] & 0x08));
357 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
358 !!(payload[0] & 0x10));
359 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
360 !!(payload[1] & 0x01));
361 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
362 !!(payload[1] & 0x02));
363 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
364 !!(payload[1] & 0x04));
365 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
366 !!(payload[1] & 0x08));
367 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
368 !!(payload[1] & 0x10));
369 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
370 !!(payload[1] & 0x80));
371 input_sync(wdata->input);
372}
373
David Herrmannc87019e2011-08-17 11:43:24 +0200374static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
375{
376 handler_keys(wdata, payload);
377
378 /* on status reports the drm is reset so we need to resend the drm */
379 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
380}
381
382static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
383{
384 __u8 err = payload[3];
385 __u8 cmd = payload[2];
386
387 handler_keys(wdata, payload);
388
389 if (err)
390 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
391 cmd);
392}
393
David Herrmanna4d19192011-07-05 13:45:15 +0200394struct wiiproto_handler {
395 __u8 id;
396 size_t size;
397 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
398};
399
400static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200401 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
402 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200403 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmanna4d19192011-07-05 13:45:15 +0200404 { .id = 0 }
405};
406
David Herrmann02fb72a2011-07-05 13:45:09 +0200407static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
408 u8 *raw_data, int size)
409{
David Herrmann4d36e972011-07-05 13:45:12 +0200410 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200411 struct wiiproto_handler *h;
412 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200413 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +0200414
David Herrmann02fb72a2011-07-05 13:45:09 +0200415 if (size < 1)
416 return -EINVAL;
417
David Herrmann32a0d9a2011-07-05 13:45:18 +0200418 spin_lock_irqsave(&wdata->state.lock, flags);
419
David Herrmanna4d19192011-07-05 13:45:15 +0200420 for (i = 0; handlers[i].id; ++i) {
421 h = &handlers[i];
422 if (h->id == raw_data[0] && h->size < size)
423 h->func(wdata, &raw_data[1]);
424 }
425
David Herrmann32a0d9a2011-07-05 13:45:18 +0200426 spin_unlock_irqrestore(&wdata->state.lock, flags);
427
David Herrmann02fb72a2011-07-05 13:45:09 +0200428 return 0;
429}
430
David Herrmann23a5a4a2011-08-17 11:43:22 +0200431static void wiimote_leds_destroy(struct wiimote_data *wdata)
432{
433 int i;
434 struct led_classdev *led;
435
436 for (i = 0; i < 4; ++i) {
437 if (wdata->leds[i]) {
438 led = wdata->leds[i];
439 wdata->leds[i] = NULL;
440 led_classdev_unregister(led);
441 kfree(led);
442 }
443 }
444}
445
446static int wiimote_leds_create(struct wiimote_data *wdata)
447{
448 int i, ret;
449 struct device *dev = &wdata->hdev->dev;
450 size_t namesz = strlen(dev_name(dev)) + 9;
451 struct led_classdev *led;
452 char *name;
453
454 for (i = 0; i < 4; ++i) {
455 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
456 if (!led) {
457 ret = -ENOMEM;
458 goto err;
459 }
460 name = (void*)&led[1];
461 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
462 led->name = name;
463 led->brightness = 0;
464 led->max_brightness = 1;
465 led->brightness_get = wiimote_leds_get;
466 led->brightness_set = wiimote_leds_set;
467
468 ret = led_classdev_register(dev, led);
469 if (ret) {
470 kfree(led);
471 goto err;
472 }
473 wdata->leds[i] = led;
474 }
475
476 return 0;
477
478err:
479 wiimote_leds_destroy(wdata);
480 return ret;
481}
482
David Herrmanne894d0e2011-07-05 13:45:10 +0200483static struct wiimote_data *wiimote_create(struct hid_device *hdev)
484{
485 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +0200486 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +0200487
488 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
489 if (!wdata)
490 return NULL;
491
David Herrmann672bc4e2011-07-05 13:45:11 +0200492 wdata->input = input_allocate_device();
493 if (!wdata->input) {
494 kfree(wdata);
495 return NULL;
496 }
497
David Herrmanne894d0e2011-07-05 13:45:10 +0200498 wdata->hdev = hdev;
499 hid_set_drvdata(hdev, wdata);
500
David Herrmann672bc4e2011-07-05 13:45:11 +0200501 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +0200502 wdata->input->open = wiimote_input_open;
503 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +0200504 wdata->input->dev.parent = &wdata->hdev->dev;
505 wdata->input->id.bustype = wdata->hdev->bus;
506 wdata->input->id.vendor = wdata->hdev->vendor;
507 wdata->input->id.product = wdata->hdev->product;
508 wdata->input->id.version = wdata->hdev->version;
509 wdata->input->name = WIIMOTE_NAME;
510
David Herrmann1abb9ad2011-07-05 13:45:16 +0200511 set_bit(EV_KEY, wdata->input->evbit);
512 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
513 set_bit(wiiproto_keymap[i], wdata->input->keybit);
514
David Herrmannd020be92011-09-06 13:50:27 +0200515 set_bit(FF_RUMBLE, wdata->input->ffbit);
516 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) {
517 input_free_device(wdata->input);
518 kfree(wdata);
519 return NULL;
520 }
521
David Herrmann23c063c2011-07-05 13:45:14 +0200522 spin_lock_init(&wdata->qlock);
523 INIT_WORK(&wdata->worker, wiimote_worker);
524
David Herrmann32a0d9a2011-07-05 13:45:18 +0200525 spin_lock_init(&wdata->state.lock);
526
David Herrmanne894d0e2011-07-05 13:45:10 +0200527 return wdata;
528}
529
530static void wiimote_destroy(struct wiimote_data *wdata)
531{
David Herrmann23a5a4a2011-08-17 11:43:22 +0200532 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200533
534 input_unregister_device(wdata->input);
535 cancel_work_sync(&wdata->worker);
536 hid_hw_stop(wdata->hdev);
537
David Herrmanne894d0e2011-07-05 13:45:10 +0200538 kfree(wdata);
539}
540
David Herrmann02fb72a2011-07-05 13:45:09 +0200541static int wiimote_hid_probe(struct hid_device *hdev,
542 const struct hid_device_id *id)
543{
David Herrmanne894d0e2011-07-05 13:45:10 +0200544 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200545 int ret;
546
David Herrmanne894d0e2011-07-05 13:45:10 +0200547 wdata = wiimote_create(hdev);
548 if (!wdata) {
549 hid_err(hdev, "Can't alloc device\n");
550 return -ENOMEM;
551 }
552
David Herrmann02fb72a2011-07-05 13:45:09 +0200553 ret = hid_parse(hdev);
554 if (ret) {
555 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200556 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200557 }
558
559 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
560 if (ret) {
561 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200562 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200563 }
564
David Herrmann672bc4e2011-07-05 13:45:11 +0200565 ret = input_register_device(wdata->input);
566 if (ret) {
567 hid_err(hdev, "Cannot register input device\n");
568 goto err_stop;
569 }
570
David Herrmann23a5a4a2011-08-17 11:43:22 +0200571 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200572 if (ret)
573 goto err_free;
574
David Herrmann02fb72a2011-07-05 13:45:09 +0200575 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +0200576
577 /* by default set led1 after device initialization */
578 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +0200579 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +0200580 spin_unlock_irq(&wdata->state.lock);
581
David Herrmann02fb72a2011-07-05 13:45:09 +0200582 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200583
David Herrmann3989ef62011-08-17 11:43:20 +0200584err_free:
585 wiimote_destroy(wdata);
586 return ret;
587
David Herrmann672bc4e2011-07-05 13:45:11 +0200588err_stop:
589 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200590err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200591 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +0200592 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +0200593 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200594}
595
596static void wiimote_hid_remove(struct hid_device *hdev)
597{
David Herrmanne894d0e2011-07-05 13:45:10 +0200598 struct wiimote_data *wdata = hid_get_drvdata(hdev);
599
David Herrmann02fb72a2011-07-05 13:45:09 +0200600 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200601 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200602}
603
604static const struct hid_device_id wiimote_hid_devices[] = {
605 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
606 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
607 { }
608};
609MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
610
611static struct hid_driver wiimote_hid_driver = {
612 .name = "wiimote",
613 .id_table = wiimote_hid_devices,
614 .probe = wiimote_hid_probe,
615 .remove = wiimote_hid_remove,
616 .raw_event = wiimote_hid_event,
617};
618
David Herrmannfb51b442011-07-05 13:45:08 +0200619static int __init wiimote_init(void)
620{
David Herrmann02fb72a2011-07-05 13:45:09 +0200621 int ret;
622
623 ret = hid_register_driver(&wiimote_hid_driver);
624 if (ret)
625 pr_err("Can't register wiimote hid driver\n");
626
627 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200628}
629
630static void __exit wiimote_exit(void)
631{
David Herrmann02fb72a2011-07-05 13:45:09 +0200632 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200633}
634
635module_init(wiimote_init);
636module_exit(wiimote_exit);
637MODULE_LICENSE("GPL");
638MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
639MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
640MODULE_VERSION(WIIMOTE_VERSION);