blob: 85a02e5f9fe873e5097a55861ec55c77f50ee05f [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 Herrmanndb308342011-07-05 13:45:17 +020049#define WIIPROTO_FLAG_LED1 0x01
50#define WIIPROTO_FLAG_LED2 0x02
51#define WIIPROTO_FLAG_LED3 0x04
52#define WIIPROTO_FLAG_LED4 0x08
David Herrmann32a0d9a2011-07-05 13:45:18 +020053#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
54 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
David Herrmanndb308342011-07-05 13:45:17 +020055
David Herrmann23a5a4a2011-08-17 11:43:22 +020056/* return flag for led \num */
57#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
58
David Herrmann1abb9ad2011-07-05 13:45:16 +020059enum wiiproto_reqs {
David Herrmann2cb5e4b2011-08-17 11:43:23 +020060 WIIPROTO_REQ_NULL = 0x0,
David Herrmanndb308342011-07-05 13:45:17 +020061 WIIPROTO_REQ_LED = 0x11,
David Herrmann2cb5e4b2011-08-17 11:43:23 +020062 WIIPROTO_REQ_DRM = 0x12,
David Herrmannc87019e2011-08-17 11:43:24 +020063 WIIPROTO_REQ_STATUS = 0x20,
64 WIIPROTO_REQ_RETURN = 0x22,
David Herrmann1abb9ad2011-07-05 13:45:16 +020065 WIIPROTO_REQ_DRM_K = 0x30,
66};
67
68enum wiiproto_keys {
69 WIIPROTO_KEY_LEFT,
70 WIIPROTO_KEY_RIGHT,
71 WIIPROTO_KEY_UP,
72 WIIPROTO_KEY_DOWN,
73 WIIPROTO_KEY_PLUS,
74 WIIPROTO_KEY_MINUS,
75 WIIPROTO_KEY_ONE,
76 WIIPROTO_KEY_TWO,
77 WIIPROTO_KEY_A,
78 WIIPROTO_KEY_B,
79 WIIPROTO_KEY_HOME,
80 WIIPROTO_KEY_COUNT
81};
82
83static __u16 wiiproto_keymap[] = {
84 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
85 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
86 KEY_UP, /* WIIPROTO_KEY_UP */
87 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
88 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
89 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
90 BTN_1, /* WIIPROTO_KEY_ONE */
91 BTN_2, /* WIIPROTO_KEY_TWO */
92 BTN_A, /* WIIPROTO_KEY_A */
93 BTN_B, /* WIIPROTO_KEY_B */
94 BTN_MODE, /* WIIPROTO_KEY_HOME */
95};
96
David Herrmann0c218f12011-07-05 13:45:13 +020097static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
98 size_t count)
99{
100 __u8 *buf;
101 ssize_t ret;
102
103 if (!hdev->hid_output_raw_report)
104 return -ENODEV;
105
106 buf = kmemdup(buffer, count, GFP_KERNEL);
107 if (!buf)
108 return -ENOMEM;
109
110 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
111
112 kfree(buf);
113 return ret;
114}
115
David Herrmann23c063c2011-07-05 13:45:14 +0200116static void wiimote_worker(struct work_struct *work)
117{
118 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
119 worker);
120 unsigned long flags;
121
122 spin_lock_irqsave(&wdata->qlock, flags);
123
124 while (wdata->head != wdata->tail) {
125 spin_unlock_irqrestore(&wdata->qlock, flags);
126 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
127 wdata->outq[wdata->tail].size);
128 spin_lock_irqsave(&wdata->qlock, flags);
129
130 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
131 }
132
133 spin_unlock_irqrestore(&wdata->qlock, flags);
134}
135
136static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
137 size_t count)
138{
139 unsigned long flags;
140 __u8 newhead;
141
142 if (count > HID_MAX_BUFFER_SIZE) {
143 hid_warn(wdata->hdev, "Sending too large output report\n");
144 return;
145 }
146
147 /*
148 * Copy new request into our output queue and check whether the
149 * queue is full. If it is full, discard this request.
150 * If it is empty we need to start a new worker that will
151 * send out the buffer to the hid device.
152 * If the queue is not empty, then there must be a worker
153 * that is currently sending out our buffer and this worker
154 * will reschedule itself until the queue is empty.
155 */
156
157 spin_lock_irqsave(&wdata->qlock, flags);
158
159 memcpy(wdata->outq[wdata->head].data, buffer, count);
160 wdata->outq[wdata->head].size = count;
161 newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
162
163 if (wdata->head == wdata->tail) {
164 wdata->head = newhead;
165 schedule_work(&wdata->worker);
166 } else if (newhead != wdata->tail) {
167 wdata->head = newhead;
168 } else {
169 hid_warn(wdata->hdev, "Output queue is full");
170 }
171
172 spin_unlock_irqrestore(&wdata->qlock, flags);
173}
174
David Herrmanndb308342011-07-05 13:45:17 +0200175static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
176{
177 __u8 cmd[2];
178
David Herrmann32a0d9a2011-07-05 13:45:18 +0200179 leds &= WIIPROTO_FLAGS_LEDS;
180 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
181 return;
182 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
183
David Herrmanndb308342011-07-05 13:45:17 +0200184 cmd[0] = WIIPROTO_REQ_LED;
185 cmd[1] = 0;
186
187 if (leds & WIIPROTO_FLAG_LED1)
188 cmd[1] |= 0x10;
189 if (leds & WIIPROTO_FLAG_LED2)
190 cmd[1] |= 0x20;
191 if (leds & WIIPROTO_FLAG_LED3)
192 cmd[1] |= 0x40;
193 if (leds & WIIPROTO_FLAG_LED4)
194 cmd[1] |= 0x80;
195
196 wiimote_queue(wdata, cmd, sizeof(cmd));
197}
198
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200199/*
200 * Check what peripherals of the wiimote are currently
201 * active and select a proper DRM that supports all of
202 * the requested data inputs.
203 */
204static __u8 select_drm(struct wiimote_data *wdata)
205{
206 return WIIPROTO_REQ_DRM_K;
207}
208
209static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
210{
211 __u8 cmd[3];
212
213 if (drm == WIIPROTO_REQ_NULL)
214 drm = select_drm(wdata);
215
216 cmd[0] = WIIPROTO_REQ_DRM;
217 cmd[1] = 0;
218 cmd[2] = drm;
219
220 wiimote_queue(wdata, cmd, sizeof(cmd));
221}
222
David Herrmann23a5a4a2011-08-17 11:43:22 +0200223static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
224{
225 struct wiimote_data *wdata;
226 struct device *dev = led_dev->dev->parent;
227 int i;
228 unsigned long flags;
229 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200230
David Herrmann23a5a4a2011-08-17 11:43:22 +0200231 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
232
233 for (i = 0; i < 4; ++i) {
234 if (wdata->leds[i] == led_dev) {
235 spin_lock_irqsave(&wdata->state.lock, flags);
236 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
237 spin_unlock_irqrestore(&wdata->state.lock, flags);
238 break;
239 }
240 }
241
242 return value ? LED_FULL : LED_OFF;
243}
244
245static void wiimote_leds_set(struct led_classdev *led_dev,
246 enum led_brightness value)
247{
248 struct wiimote_data *wdata;
249 struct device *dev = led_dev->dev->parent;
250 int i;
251 unsigned long flags;
252 __u8 state, flag;
253
254 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
255
256 for (i = 0; i < 4; ++i) {
257 if (wdata->leds[i] == led_dev) {
258 flag = WIIPROTO_FLAG_LED(i + 1);
259 spin_lock_irqsave(&wdata->state.lock, flags);
260 state = wdata->state.flags;
261 if (value == LED_OFF)
262 wiiproto_req_leds(wdata, state & ~flag);
263 else
264 wiiproto_req_leds(wdata, state | flag);
265 spin_unlock_irqrestore(&wdata->state.lock, flags);
266 break;
267 }
268 }
269}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200270
David Herrmann672bc4e2011-07-05 13:45:11 +0200271static int wiimote_input_event(struct input_dev *dev, unsigned int type,
272 unsigned int code, int value)
273{
274 return 0;
275}
276
David Herrmann26af1742011-08-17 11:43:21 +0200277static int wiimote_input_open(struct input_dev *dev)
278{
279 struct wiimote_data *wdata = input_get_drvdata(dev);
280
281 return hid_hw_open(wdata->hdev);
282}
283
284static void wiimote_input_close(struct input_dev *dev)
285{
286 struct wiimote_data *wdata = input_get_drvdata(dev);
287
288 hid_hw_close(wdata->hdev);
289}
290
David Herrmann1abb9ad2011-07-05 13:45:16 +0200291static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
292{
293 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
294 !!(payload[0] & 0x01));
295 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
296 !!(payload[0] & 0x02));
297 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
298 !!(payload[0] & 0x04));
299 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
300 !!(payload[0] & 0x08));
301 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
302 !!(payload[0] & 0x10));
303 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
304 !!(payload[1] & 0x01));
305 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
306 !!(payload[1] & 0x02));
307 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
308 !!(payload[1] & 0x04));
309 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
310 !!(payload[1] & 0x08));
311 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
312 !!(payload[1] & 0x10));
313 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
314 !!(payload[1] & 0x80));
315 input_sync(wdata->input);
316}
317
David Herrmannc87019e2011-08-17 11:43:24 +0200318static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
319{
320 handler_keys(wdata, payload);
321
322 /* on status reports the drm is reset so we need to resend the drm */
323 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
324}
325
326static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
327{
328 __u8 err = payload[3];
329 __u8 cmd = payload[2];
330
331 handler_keys(wdata, payload);
332
333 if (err)
334 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
335 cmd);
336}
337
David Herrmanna4d19192011-07-05 13:45:15 +0200338struct wiiproto_handler {
339 __u8 id;
340 size_t size;
341 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
342};
343
344static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200345 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
346 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200347 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmanna4d19192011-07-05 13:45:15 +0200348 { .id = 0 }
349};
350
David Herrmann02fb72a2011-07-05 13:45:09 +0200351static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
352 u8 *raw_data, int size)
353{
David Herrmann4d36e972011-07-05 13:45:12 +0200354 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200355 struct wiiproto_handler *h;
356 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +0200357 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +0200358
David Herrmann02fb72a2011-07-05 13:45:09 +0200359 if (size < 1)
360 return -EINVAL;
361
David Herrmann32a0d9a2011-07-05 13:45:18 +0200362 spin_lock_irqsave(&wdata->state.lock, flags);
363
David Herrmanna4d19192011-07-05 13:45:15 +0200364 for (i = 0; handlers[i].id; ++i) {
365 h = &handlers[i];
366 if (h->id == raw_data[0] && h->size < size)
367 h->func(wdata, &raw_data[1]);
368 }
369
David Herrmann32a0d9a2011-07-05 13:45:18 +0200370 spin_unlock_irqrestore(&wdata->state.lock, flags);
371
David Herrmann02fb72a2011-07-05 13:45:09 +0200372 return 0;
373}
374
David Herrmann23a5a4a2011-08-17 11:43:22 +0200375static void wiimote_leds_destroy(struct wiimote_data *wdata)
376{
377 int i;
378 struct led_classdev *led;
379
380 for (i = 0; i < 4; ++i) {
381 if (wdata->leds[i]) {
382 led = wdata->leds[i];
383 wdata->leds[i] = NULL;
384 led_classdev_unregister(led);
385 kfree(led);
386 }
387 }
388}
389
390static int wiimote_leds_create(struct wiimote_data *wdata)
391{
392 int i, ret;
393 struct device *dev = &wdata->hdev->dev;
394 size_t namesz = strlen(dev_name(dev)) + 9;
395 struct led_classdev *led;
396 char *name;
397
398 for (i = 0; i < 4; ++i) {
399 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
400 if (!led) {
401 ret = -ENOMEM;
402 goto err;
403 }
404 name = (void*)&led[1];
405 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
406 led->name = name;
407 led->brightness = 0;
408 led->max_brightness = 1;
409 led->brightness_get = wiimote_leds_get;
410 led->brightness_set = wiimote_leds_set;
411
412 ret = led_classdev_register(dev, led);
413 if (ret) {
414 kfree(led);
415 goto err;
416 }
417 wdata->leds[i] = led;
418 }
419
420 return 0;
421
422err:
423 wiimote_leds_destroy(wdata);
424 return ret;
425}
426
David Herrmanne894d0e2011-07-05 13:45:10 +0200427static struct wiimote_data *wiimote_create(struct hid_device *hdev)
428{
429 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +0200430 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +0200431
432 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
433 if (!wdata)
434 return NULL;
435
David Herrmann672bc4e2011-07-05 13:45:11 +0200436 wdata->input = input_allocate_device();
437 if (!wdata->input) {
438 kfree(wdata);
439 return NULL;
440 }
441
David Herrmanne894d0e2011-07-05 13:45:10 +0200442 wdata->hdev = hdev;
443 hid_set_drvdata(hdev, wdata);
444
David Herrmann672bc4e2011-07-05 13:45:11 +0200445 input_set_drvdata(wdata->input, wdata);
446 wdata->input->event = wiimote_input_event;
David Herrmann26af1742011-08-17 11:43:21 +0200447 wdata->input->open = wiimote_input_open;
448 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +0200449 wdata->input->dev.parent = &wdata->hdev->dev;
450 wdata->input->id.bustype = wdata->hdev->bus;
451 wdata->input->id.vendor = wdata->hdev->vendor;
452 wdata->input->id.product = wdata->hdev->product;
453 wdata->input->id.version = wdata->hdev->version;
454 wdata->input->name = WIIMOTE_NAME;
455
David Herrmann1abb9ad2011-07-05 13:45:16 +0200456 set_bit(EV_KEY, wdata->input->evbit);
457 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
458 set_bit(wiiproto_keymap[i], wdata->input->keybit);
459
David Herrmann23c063c2011-07-05 13:45:14 +0200460 spin_lock_init(&wdata->qlock);
461 INIT_WORK(&wdata->worker, wiimote_worker);
462
David Herrmann32a0d9a2011-07-05 13:45:18 +0200463 spin_lock_init(&wdata->state.lock);
464
David Herrmanne894d0e2011-07-05 13:45:10 +0200465 return wdata;
466}
467
468static void wiimote_destroy(struct wiimote_data *wdata)
469{
David Herrmann23a5a4a2011-08-17 11:43:22 +0200470 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200471
472 input_unregister_device(wdata->input);
473 cancel_work_sync(&wdata->worker);
474 hid_hw_stop(wdata->hdev);
475
David Herrmanne894d0e2011-07-05 13:45:10 +0200476 kfree(wdata);
477}
478
David Herrmann02fb72a2011-07-05 13:45:09 +0200479static int wiimote_hid_probe(struct hid_device *hdev,
480 const struct hid_device_id *id)
481{
David Herrmanne894d0e2011-07-05 13:45:10 +0200482 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +0200483 int ret;
484
David Herrmanne894d0e2011-07-05 13:45:10 +0200485 wdata = wiimote_create(hdev);
486 if (!wdata) {
487 hid_err(hdev, "Can't alloc device\n");
488 return -ENOMEM;
489 }
490
David Herrmann02fb72a2011-07-05 13:45:09 +0200491 ret = hid_parse(hdev);
492 if (ret) {
493 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200494 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200495 }
496
497 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
498 if (ret) {
499 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200500 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +0200501 }
502
David Herrmann672bc4e2011-07-05 13:45:11 +0200503 ret = input_register_device(wdata->input);
504 if (ret) {
505 hid_err(hdev, "Cannot register input device\n");
506 goto err_stop;
507 }
508
David Herrmann23a5a4a2011-08-17 11:43:22 +0200509 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +0200510 if (ret)
511 goto err_free;
512
David Herrmann02fb72a2011-07-05 13:45:09 +0200513 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +0200514
515 /* by default set led1 after device initialization */
516 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +0200517 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +0200518 spin_unlock_irq(&wdata->state.lock);
519
David Herrmann02fb72a2011-07-05 13:45:09 +0200520 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +0200521
David Herrmann3989ef62011-08-17 11:43:20 +0200522err_free:
523 wiimote_destroy(wdata);
524 return ret;
525
David Herrmann672bc4e2011-07-05 13:45:11 +0200526err_stop:
527 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +0200528err:
David Herrmann672bc4e2011-07-05 13:45:11 +0200529 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +0200530 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +0200531 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +0200532}
533
534static void wiimote_hid_remove(struct hid_device *hdev)
535{
David Herrmanne894d0e2011-07-05 13:45:10 +0200536 struct wiimote_data *wdata = hid_get_drvdata(hdev);
537
David Herrmann02fb72a2011-07-05 13:45:09 +0200538 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +0200539 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +0200540}
541
542static const struct hid_device_id wiimote_hid_devices[] = {
543 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
544 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
545 { }
546};
547MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
548
549static struct hid_driver wiimote_hid_driver = {
550 .name = "wiimote",
551 .id_table = wiimote_hid_devices,
552 .probe = wiimote_hid_probe,
553 .remove = wiimote_hid_remove,
554 .raw_event = wiimote_hid_event,
555};
556
David Herrmannfb51b442011-07-05 13:45:08 +0200557static int __init wiimote_init(void)
558{
David Herrmann02fb72a2011-07-05 13:45:09 +0200559 int ret;
560
561 ret = hid_register_driver(&wiimote_hid_driver);
562 if (ret)
563 pr_err("Can't register wiimote hid driver\n");
564
565 return ret;
David Herrmannfb51b442011-07-05 13:45:08 +0200566}
567
568static void __exit wiimote_exit(void)
569{
David Herrmann02fb72a2011-07-05 13:45:09 +0200570 hid_unregister_driver(&wiimote_hid_driver);
David Herrmannfb51b442011-07-05 13:45:08 +0200571}
572
573module_init(wiimote_init);
574module_exit(wiimote_exit);
575MODULE_LICENSE("GPL");
576MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
577MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
578MODULE_VERSION(WIIMOTE_VERSION);