HID: wiimote: Add debugfs support stubs
[linux-2.6.git] / drivers / hid / hid-wiimote-core.c
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
13 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input.h>
17 #include <linux/leds.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/power_supply.h>
21 #include <linux/spinlock.h>
22 #include "hid-ids.h"
23 #include "hid-wiimote.h"
24
25 #define WIIMOTE_VERSION "0.2"
26
27 enum wiiproto_keys {
28         WIIPROTO_KEY_LEFT,
29         WIIPROTO_KEY_RIGHT,
30         WIIPROTO_KEY_UP,
31         WIIPROTO_KEY_DOWN,
32         WIIPROTO_KEY_PLUS,
33         WIIPROTO_KEY_MINUS,
34         WIIPROTO_KEY_ONE,
35         WIIPROTO_KEY_TWO,
36         WIIPROTO_KEY_A,
37         WIIPROTO_KEY_B,
38         WIIPROTO_KEY_HOME,
39         WIIPROTO_KEY_COUNT
40 };
41
42 static __u16 wiiproto_keymap[] = {
43         KEY_LEFT,       /* WIIPROTO_KEY_LEFT */
44         KEY_RIGHT,      /* WIIPROTO_KEY_RIGHT */
45         KEY_UP,         /* WIIPROTO_KEY_UP */
46         KEY_DOWN,       /* WIIPROTO_KEY_DOWN */
47         KEY_NEXT,       /* WIIPROTO_KEY_PLUS */
48         KEY_PREVIOUS,   /* WIIPROTO_KEY_MINUS */
49         BTN_1,          /* WIIPROTO_KEY_ONE */
50         BTN_2,          /* WIIPROTO_KEY_TWO */
51         BTN_A,          /* WIIPROTO_KEY_A */
52         BTN_B,          /* WIIPROTO_KEY_B */
53         BTN_MODE,       /* WIIPROTO_KEY_HOME */
54 };
55
56 static enum power_supply_property wiimote_battery_props[] = {
57         POWER_SUPPLY_PROP_CAPACITY
58 };
59
60 static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
61                                                                 size_t count)
62 {
63         __u8 *buf;
64         ssize_t ret;
65
66         if (!hdev->hid_output_raw_report)
67                 return -ENODEV;
68
69         buf = kmemdup(buffer, count, GFP_KERNEL);
70         if (!buf)
71                 return -ENOMEM;
72
73         ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
74
75         kfree(buf);
76         return ret;
77 }
78
79 static void wiimote_worker(struct work_struct *work)
80 {
81         struct wiimote_data *wdata = container_of(work, struct wiimote_data,
82                                                                         worker);
83         unsigned long flags;
84
85         spin_lock_irqsave(&wdata->qlock, flags);
86
87         while (wdata->head != wdata->tail) {
88                 spin_unlock_irqrestore(&wdata->qlock, flags);
89                 wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
90                                                 wdata->outq[wdata->tail].size);
91                 spin_lock_irqsave(&wdata->qlock, flags);
92
93                 wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
94         }
95
96         spin_unlock_irqrestore(&wdata->qlock, flags);
97 }
98
99 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
100                                                                 size_t count)
101 {
102         unsigned long flags;
103         __u8 newhead;
104
105         if (count > HID_MAX_BUFFER_SIZE) {
106                 hid_warn(wdata->hdev, "Sending too large output report\n");
107                 return;
108         }
109
110         /*
111          * Copy new request into our output queue and check whether the
112          * queue is full. If it is full, discard this request.
113          * If it is empty we need to start a new worker that will
114          * send out the buffer to the hid device.
115          * If the queue is not empty, then there must be a worker
116          * that is currently sending out our buffer and this worker
117          * will reschedule itself until the queue is empty.
118          */
119
120         spin_lock_irqsave(&wdata->qlock, flags);
121
122         memcpy(wdata->outq[wdata->head].data, buffer, count);
123         wdata->outq[wdata->head].size = count;
124         newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
125
126         if (wdata->head == wdata->tail) {
127                 wdata->head = newhead;
128                 schedule_work(&wdata->worker);
129         } else if (newhead != wdata->tail) {
130                 wdata->head = newhead;
131         } else {
132                 hid_warn(wdata->hdev, "Output queue is full");
133         }
134
135         spin_unlock_irqrestore(&wdata->qlock, flags);
136 }
137
138 /*
139  * This sets the rumble bit on the given output report if rumble is
140  * currently enabled.
141  * \cmd1 must point to the second byte in the output report => &cmd[1]
142  * This must be called on nearly every output report before passing it
143  * into the output queue!
144  */
145 static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
146 {
147         if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
148                 *cmd1 |= 0x01;
149 }
150
151 static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
152 {
153         __u8 cmd[2];
154
155         rumble = !!rumble;
156         if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
157                 return;
158
159         if (rumble)
160                 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
161         else
162                 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
163
164         cmd[0] = WIIPROTO_REQ_RUMBLE;
165         cmd[1] = 0;
166
167         wiiproto_keep_rumble(wdata, &cmd[1]);
168         wiimote_queue(wdata, cmd, sizeof(cmd));
169 }
170
171 static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
172 {
173         __u8 cmd[2];
174
175         leds &= WIIPROTO_FLAGS_LEDS;
176         if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
177                 return;
178         wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
179
180         cmd[0] = WIIPROTO_REQ_LED;
181         cmd[1] = 0;
182
183         if (leds & WIIPROTO_FLAG_LED1)
184                 cmd[1] |= 0x10;
185         if (leds & WIIPROTO_FLAG_LED2)
186                 cmd[1] |= 0x20;
187         if (leds & WIIPROTO_FLAG_LED3)
188                 cmd[1] |= 0x40;
189         if (leds & WIIPROTO_FLAG_LED4)
190                 cmd[1] |= 0x80;
191
192         wiiproto_keep_rumble(wdata, &cmd[1]);
193         wiimote_queue(wdata, cmd, sizeof(cmd));
194 }
195
196 /*
197  * Check what peripherals of the wiimote are currently
198  * active and select a proper DRM that supports all of
199  * the requested data inputs.
200  */
201 static __u8 select_drm(struct wiimote_data *wdata)
202 {
203         __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
204         bool ext = wiiext_active(wdata);
205
206         if (ir == WIIPROTO_FLAG_IR_BASIC) {
207                 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
208                         return WIIPROTO_REQ_DRM_KAIE;
209                 else
210                         return WIIPROTO_REQ_DRM_KIE;
211         } else if (ir == WIIPROTO_FLAG_IR_EXT) {
212                 return WIIPROTO_REQ_DRM_KAI;
213         } else if (ir == WIIPROTO_FLAG_IR_FULL) {
214                 return WIIPROTO_REQ_DRM_SKAI1;
215         } else {
216                 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
217                         if (ext)
218                                 return WIIPROTO_REQ_DRM_KAE;
219                         else
220                                 return WIIPROTO_REQ_DRM_KA;
221                 } else {
222                         if (ext)
223                                 return WIIPROTO_REQ_DRM_KE;
224                         else
225                                 return WIIPROTO_REQ_DRM_K;
226                 }
227         }
228 }
229
230 void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
231 {
232         __u8 cmd[3];
233
234         if (drm == WIIPROTO_REQ_NULL)
235                 drm = select_drm(wdata);
236
237         cmd[0] = WIIPROTO_REQ_DRM;
238         cmd[1] = 0;
239         cmd[2] = drm;
240
241         wiiproto_keep_rumble(wdata, &cmd[1]);
242         wiimote_queue(wdata, cmd, sizeof(cmd));
243 }
244
245 static void wiiproto_req_status(struct wiimote_data *wdata)
246 {
247         __u8 cmd[2];
248
249         cmd[0] = WIIPROTO_REQ_SREQ;
250         cmd[1] = 0;
251
252         wiiproto_keep_rumble(wdata, &cmd[1]);
253         wiimote_queue(wdata, cmd, sizeof(cmd));
254 }
255
256 static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
257 {
258         accel = !!accel;
259         if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
260                 return;
261
262         if (accel)
263                 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
264         else
265                 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
266
267         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
268 }
269
270 static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
271 {
272         __u8 cmd[2];
273
274         cmd[0] = WIIPROTO_REQ_IR1;
275         cmd[1] = flags;
276
277         wiiproto_keep_rumble(wdata, &cmd[1]);
278         wiimote_queue(wdata, cmd, sizeof(cmd));
279 }
280
281 static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
282 {
283         __u8 cmd[2];
284
285         cmd[0] = WIIPROTO_REQ_IR2;
286         cmd[1] = flags;
287
288         wiiproto_keep_rumble(wdata, &cmd[1]);
289         wiimote_queue(wdata, cmd, sizeof(cmd));
290 }
291
292 #define wiiproto_req_wreg(wdata, os, buf, sz) \
293                         wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
294
295 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
296                         wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
297
298 static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
299                                 __u32 offset, const __u8 *buf, __u8 size)
300 {
301         __u8 cmd[22];
302
303         if (size > 16 || size == 0) {
304                 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
305                 return;
306         }
307
308         memset(cmd, 0, sizeof(cmd));
309         cmd[0] = WIIPROTO_REQ_WMEM;
310         cmd[2] = (offset >> 16) & 0xff;
311         cmd[3] = (offset >> 8) & 0xff;
312         cmd[4] = offset & 0xff;
313         cmd[5] = size;
314         memcpy(&cmd[6], buf, size);
315
316         if (!eeprom)
317                 cmd[1] |= 0x04;
318
319         wiiproto_keep_rumble(wdata, &cmd[1]);
320         wiimote_queue(wdata, cmd, sizeof(cmd));
321 }
322
323 #define wiiproto_req_rreg(wdata, os, sz) \
324                                 wiiproto_req_rmem((wdata), false, (os), (sz))
325
326 #define wiiproto_req_reeprom(wdata, os, sz) \
327                                 wiiproto_req_rmem((wdata), true, (os), (sz))
328
329 static void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
330                                                 __u32 offset, __u16 size)
331 {
332         __u8 cmd[7];
333
334         if (size == 0) {
335                 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
336                 return;
337         }
338
339         cmd[0] = WIIPROTO_REQ_RMEM;
340         cmd[1] = 0;
341         cmd[2] = (offset >> 16) & 0xff;
342         cmd[3] = (offset >> 8) & 0xff;
343         cmd[4] = offset & 0xff;
344         cmd[5] = (size >> 8) & 0xff;
345         cmd[6] = size & 0xff;
346
347         if (!eeprom)
348                 cmd[1] |= 0x04;
349
350         wiiproto_keep_rumble(wdata, &cmd[1]);
351         wiimote_queue(wdata, cmd, sizeof(cmd));
352 }
353
354 /* requries the cmd-mutex to be held */
355 int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
356                                                 const __u8 *wmem, __u8 size)
357 {
358         unsigned long flags;
359         int ret;
360
361         spin_lock_irqsave(&wdata->state.lock, flags);
362         wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
363         wiiproto_req_wreg(wdata, offset, wmem, size);
364         spin_unlock_irqrestore(&wdata->state.lock, flags);
365
366         ret = wiimote_cmd_wait(wdata);
367         if (!ret && wdata->state.cmd_err)
368                 ret = -EIO;
369
370         return ret;
371 }
372
373 /* requries the cmd-mutex to be held */
374 ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
375                                                                 __u8 size)
376 {
377         unsigned long flags;
378         ssize_t ret;
379
380         spin_lock_irqsave(&wdata->state.lock, flags);
381         wdata->state.cmd_read_size = size;
382         wdata->state.cmd_read_buf = rmem;
383         wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
384         wiiproto_req_rreg(wdata, offset, size);
385         spin_unlock_irqrestore(&wdata->state.lock, flags);
386
387         ret = wiimote_cmd_wait(wdata);
388
389         spin_lock_irqsave(&wdata->state.lock, flags);
390         wdata->state.cmd_read_buf = NULL;
391         spin_unlock_irqrestore(&wdata->state.lock, flags);
392
393         if (!ret) {
394                 if (wdata->state.cmd_read_size == 0)
395                         ret = -EIO;
396                 else
397                         ret = wdata->state.cmd_read_size;
398         }
399
400         return ret;
401 }
402
403 static int wiimote_battery_get_property(struct power_supply *psy,
404                                                 enum power_supply_property psp,
405                                                 union power_supply_propval *val)
406 {
407         struct wiimote_data *wdata = container_of(psy,
408                                                 struct wiimote_data, battery);
409         int ret = 0, state;
410         unsigned long flags;
411
412         ret = wiimote_cmd_acquire(wdata);
413         if (ret)
414                 return ret;
415
416         spin_lock_irqsave(&wdata->state.lock, flags);
417         wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
418         wiiproto_req_status(wdata);
419         spin_unlock_irqrestore(&wdata->state.lock, flags);
420
421         ret = wiimote_cmd_wait(wdata);
422         state = wdata->state.cmd_battery;
423         wiimote_cmd_release(wdata);
424
425         if (ret)
426                 return ret;
427
428         switch (psp) {
429                 case POWER_SUPPLY_PROP_CAPACITY:
430                         val->intval = state * 100 / 255;
431                         break;
432                 default:
433                         ret = -EINVAL;
434                         break;
435         }
436
437         return ret;
438 }
439
440 static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
441 {
442         int ret;
443         unsigned long flags;
444         __u8 format = 0;
445         static const __u8 data_enable[] = { 0x01 };
446         static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
447                                                 0x00, 0xaa, 0x00, 0x64 };
448         static const __u8 data_sens2[] = { 0x63, 0x03 };
449         static const __u8 data_fin[] = { 0x08 };
450
451         spin_lock_irqsave(&wdata->state.lock, flags);
452
453         if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
454                 spin_unlock_irqrestore(&wdata->state.lock, flags);
455                 return 0;
456         }
457
458         if (mode == 0) {
459                 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
460                 wiiproto_req_ir1(wdata, 0);
461                 wiiproto_req_ir2(wdata, 0);
462                 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
463                 spin_unlock_irqrestore(&wdata->state.lock, flags);
464                 return 0;
465         }
466
467         spin_unlock_irqrestore(&wdata->state.lock, flags);
468
469         ret = wiimote_cmd_acquire(wdata);
470         if (ret)
471                 return ret;
472
473         /* send PIXEL CLOCK ENABLE cmd first */
474         spin_lock_irqsave(&wdata->state.lock, flags);
475         wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
476         wiiproto_req_ir1(wdata, 0x06);
477         spin_unlock_irqrestore(&wdata->state.lock, flags);
478
479         ret = wiimote_cmd_wait(wdata);
480         if (ret)
481                 goto unlock;
482         if (wdata->state.cmd_err) {
483                 ret = -EIO;
484                 goto unlock;
485         }
486
487         /* enable IR LOGIC */
488         spin_lock_irqsave(&wdata->state.lock, flags);
489         wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
490         wiiproto_req_ir2(wdata, 0x06);
491         spin_unlock_irqrestore(&wdata->state.lock, flags);
492
493         ret = wiimote_cmd_wait(wdata);
494         if (ret)
495                 goto unlock;
496         if (wdata->state.cmd_err) {
497                 ret = -EIO;
498                 goto unlock;
499         }
500
501         /* enable IR cam but do not make it send data, yet */
502         ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
503                                                         sizeof(data_enable));
504         if (ret)
505                 goto unlock;
506
507         /* write first sensitivity block */
508         ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
509                                                         sizeof(data_sens1));
510         if (ret)
511                 goto unlock;
512
513         /* write second sensitivity block */
514         ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
515                                                         sizeof(data_sens2));
516         if (ret)
517                 goto unlock;
518
519         /* put IR cam into desired state */
520         switch (mode) {
521                 case WIIPROTO_FLAG_IR_FULL:
522                         format = 5;
523                         break;
524                 case WIIPROTO_FLAG_IR_EXT:
525                         format = 3;
526                         break;
527                 case WIIPROTO_FLAG_IR_BASIC:
528                         format = 1;
529                         break;
530         }
531         ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
532         if (ret)
533                 goto unlock;
534
535         /* make IR cam send data */
536         ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
537         if (ret)
538                 goto unlock;
539
540         /* request new DRM mode compatible to IR mode */
541         spin_lock_irqsave(&wdata->state.lock, flags);
542         wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
543         wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
544         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
545         spin_unlock_irqrestore(&wdata->state.lock, flags);
546
547 unlock:
548         wiimote_cmd_release(wdata);
549         return ret;
550 }
551
552 static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
553 {
554         struct wiimote_data *wdata;
555         struct device *dev = led_dev->dev->parent;
556         int i;
557         unsigned long flags;
558         bool value = false;
559
560         wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
561
562         for (i = 0; i < 4; ++i) {
563                 if (wdata->leds[i] == led_dev) {
564                         spin_lock_irqsave(&wdata->state.lock, flags);
565                         value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
566                         spin_unlock_irqrestore(&wdata->state.lock, flags);
567                         break;
568                 }
569         }
570
571         return value ? LED_FULL : LED_OFF;
572 }
573
574 static void wiimote_leds_set(struct led_classdev *led_dev,
575                                                 enum led_brightness value)
576 {
577         struct wiimote_data *wdata;
578         struct device *dev = led_dev->dev->parent;
579         int i;
580         unsigned long flags;
581         __u8 state, flag;
582
583         wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
584
585         for (i = 0; i < 4; ++i) {
586                 if (wdata->leds[i] == led_dev) {
587                         flag = WIIPROTO_FLAG_LED(i + 1);
588                         spin_lock_irqsave(&wdata->state.lock, flags);
589                         state = wdata->state.flags;
590                         if (value == LED_OFF)
591                                 wiiproto_req_leds(wdata, state & ~flag);
592                         else
593                                 wiiproto_req_leds(wdata, state | flag);
594                         spin_unlock_irqrestore(&wdata->state.lock, flags);
595                         break;
596                 }
597         }
598 }
599
600 static int wiimote_ff_play(struct input_dev *dev, void *data,
601                                                         struct ff_effect *eff)
602 {
603         struct wiimote_data *wdata = input_get_drvdata(dev);
604         __u8 value;
605         unsigned long flags;
606
607         /*
608          * The wiimote supports only a single rumble motor so if any magnitude
609          * is set to non-zero then we start the rumble motor. If both are set to
610          * zero, we stop the rumble motor.
611          */
612
613         if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
614                 value = 1;
615         else
616                 value = 0;
617
618         spin_lock_irqsave(&wdata->state.lock, flags);
619         wiiproto_req_rumble(wdata, value);
620         spin_unlock_irqrestore(&wdata->state.lock, flags);
621
622         return 0;
623 }
624
625 static int wiimote_input_open(struct input_dev *dev)
626 {
627         struct wiimote_data *wdata = input_get_drvdata(dev);
628
629         return hid_hw_open(wdata->hdev);
630 }
631
632 static void wiimote_input_close(struct input_dev *dev)
633 {
634         struct wiimote_data *wdata = input_get_drvdata(dev);
635
636         hid_hw_close(wdata->hdev);
637 }
638
639 static int wiimote_accel_open(struct input_dev *dev)
640 {
641         struct wiimote_data *wdata = input_get_drvdata(dev);
642         int ret;
643         unsigned long flags;
644
645         ret = hid_hw_open(wdata->hdev);
646         if (ret)
647                 return ret;
648
649         spin_lock_irqsave(&wdata->state.lock, flags);
650         wiiproto_req_accel(wdata, true);
651         spin_unlock_irqrestore(&wdata->state.lock, flags);
652
653         return 0;
654 }
655
656 static void wiimote_accel_close(struct input_dev *dev)
657 {
658         struct wiimote_data *wdata = input_get_drvdata(dev);
659         unsigned long flags;
660
661         spin_lock_irqsave(&wdata->state.lock, flags);
662         wiiproto_req_accel(wdata, false);
663         spin_unlock_irqrestore(&wdata->state.lock, flags);
664
665         hid_hw_close(wdata->hdev);
666 }
667
668 static int wiimote_ir_open(struct input_dev *dev)
669 {
670         struct wiimote_data *wdata = input_get_drvdata(dev);
671         int ret;
672
673         ret = hid_hw_open(wdata->hdev);
674         if (ret)
675                 return ret;
676
677         ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
678         if (ret) {
679                 hid_hw_close(wdata->hdev);
680                 return ret;
681         }
682
683         return 0;
684 }
685
686 static void wiimote_ir_close(struct input_dev *dev)
687 {
688         struct wiimote_data *wdata = input_get_drvdata(dev);
689
690         wiimote_init_ir(wdata, 0);
691         hid_hw_close(wdata->hdev);
692 }
693
694 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
695 {
696         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
697                                                         !!(payload[0] & 0x01));
698         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
699                                                         !!(payload[0] & 0x02));
700         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
701                                                         !!(payload[0] & 0x04));
702         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
703                                                         !!(payload[0] & 0x08));
704         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
705                                                         !!(payload[0] & 0x10));
706         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
707                                                         !!(payload[1] & 0x01));
708         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
709                                                         !!(payload[1] & 0x02));
710         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
711                                                         !!(payload[1] & 0x04));
712         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
713                                                         !!(payload[1] & 0x08));
714         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
715                                                         !!(payload[1] & 0x10));
716         input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
717                                                         !!(payload[1] & 0x80));
718         input_sync(wdata->input);
719 }
720
721 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
722 {
723         __u16 x, y, z;
724
725         if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
726                 return;
727
728         /*
729          * payload is: BB BB XX YY ZZ
730          * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
731          * contain the upper 8 bits of each value. The lower 2 bits are
732          * contained in the buttons data BB BB.
733          * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
734          * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
735          * accel value and bit 6 is the second bit of the Z value.
736          * The first bit of Y and Z values is not available and always set to 0.
737          * 0x200 is returned on no movement.
738          */
739
740         x = payload[2] << 2;
741         y = payload[3] << 2;
742         z = payload[4] << 2;
743
744         x |= (payload[0] >> 5) & 0x3;
745         y |= (payload[1] >> 4) & 0x2;
746         z |= (payload[1] >> 5) & 0x2;
747
748         input_report_abs(wdata->accel, ABS_RX, x - 0x200);
749         input_report_abs(wdata->accel, ABS_RY, y - 0x200);
750         input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
751         input_sync(wdata->accel);
752 }
753
754 #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
755                                                         ABS_HAT0X, ABS_HAT0Y)
756 #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
757                                                         ABS_HAT1X, ABS_HAT1Y)
758 #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
759                                                         ABS_HAT2X, ABS_HAT2Y)
760 #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
761                                                         ABS_HAT3X, ABS_HAT3Y)
762
763 static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
764                                                 bool packed, __u8 xid, __u8 yid)
765 {
766         __u16 x, y;
767
768         if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
769                 return;
770
771         /*
772          * Basic IR data is encoded into 3 bytes. The first two bytes are the
773          * upper 8 bit of the X/Y data, the 3rd byte contains the lower 2 bits
774          * of both.
775          * If data is packed, then the 3rd byte is put first and slightly
776          * reordered. This allows to interleave packed and non-packed data to
777          * have two IR sets in 5 bytes instead of 6.
778          * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
779          */
780
781         if (packed) {
782                 x = ir[1] << 2;
783                 y = ir[2] << 2;
784
785                 x |= ir[0] & 0x3;
786                 y |= (ir[0] >> 2) & 0x3;
787         } else {
788                 x = ir[0] << 2;
789                 y = ir[1] << 2;
790
791                 x |= (ir[2] >> 4) & 0x3;
792                 y |= (ir[2] >> 6) & 0x3;
793         }
794
795         input_report_abs(wdata->ir, xid, x);
796         input_report_abs(wdata->ir, yid, y);
797 }
798
799 static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
800 {
801         handler_keys(wdata, payload);
802
803         /* on status reports the drm is reset so we need to resend the drm */
804         wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
805
806         wiiext_event(wdata, payload[2] & 0x02);
807
808         if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) {
809                 wdata->state.cmd_battery = payload[5];
810                 wiimote_cmd_complete(wdata);
811         }
812 }
813
814 static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
815 {
816         __u16 offset = payload[3] << 8 | payload[4];
817         __u8 size = (payload[2] >> 4) + 1;
818         __u8 err = payload[2] & 0x0f;
819
820         handler_keys(wdata, payload);
821
822         if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
823                 if (err)
824                         size = 0;
825                 else if (size > wdata->state.cmd_read_size)
826                         size = wdata->state.cmd_read_size;
827
828                 wdata->state.cmd_read_size = size;
829                 if (wdata->state.cmd_read_buf)
830                         memcpy(wdata->state.cmd_read_buf, &payload[5], size);
831                 wiimote_cmd_complete(wdata);
832         }
833 }
834
835 static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
836 {
837         __u8 err = payload[3];
838         __u8 cmd = payload[2];
839
840         handler_keys(wdata, payload);
841
842         if (wiimote_cmd_pending(wdata, cmd, 0)) {
843                 wdata->state.cmd_err = err;
844                 wiimote_cmd_complete(wdata);
845         } else if (err) {
846                 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
847                                                                         cmd);
848         }
849 }
850
851 static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
852 {
853         handler_keys(wdata, payload);
854         handler_accel(wdata, payload);
855 }
856
857 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
858 {
859         handler_keys(wdata, payload);
860         wiiext_handle(wdata, &payload[2]);
861 }
862
863 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
864 {
865         handler_keys(wdata, payload);
866         handler_accel(wdata, payload);
867         ir_to_input0(wdata, &payload[5], false);
868         ir_to_input1(wdata, &payload[8], false);
869         ir_to_input2(wdata, &payload[11], false);
870         ir_to_input3(wdata, &payload[14], false);
871         input_sync(wdata->ir);
872 }
873
874 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
875 {
876         handler_keys(wdata, payload);
877         wiiext_handle(wdata, &payload[2]);
878 }
879
880 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
881 {
882         handler_keys(wdata, payload);
883         ir_to_input0(wdata, &payload[2], false);
884         ir_to_input1(wdata, &payload[4], true);
885         ir_to_input2(wdata, &payload[7], false);
886         ir_to_input3(wdata, &payload[9], true);
887         input_sync(wdata->ir);
888         wiiext_handle(wdata, &payload[12]);
889 }
890
891 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
892 {
893         handler_keys(wdata, payload);
894         handler_accel(wdata, payload);
895         wiiext_handle(wdata, &payload[5]);
896 }
897
898 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
899 {
900         handler_keys(wdata, payload);
901         handler_accel(wdata, payload);
902         ir_to_input0(wdata, &payload[5], false);
903         ir_to_input1(wdata, &payload[7], true);
904         ir_to_input2(wdata, &payload[10], false);
905         ir_to_input3(wdata, &payload[12], true);
906         input_sync(wdata->ir);
907         wiiext_handle(wdata, &payload[15]);
908 }
909
910 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
911 {
912         wiiext_handle(wdata, payload);
913 }
914
915 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
916 {
917         handler_keys(wdata, payload);
918
919         wdata->state.accel_split[0] = payload[2];
920         wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
921         wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
922
923         ir_to_input0(wdata, &payload[3], false);
924         ir_to_input1(wdata, &payload[12], false);
925         input_sync(wdata->ir);
926 }
927
928 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
929 {
930         __u8 buf[5];
931
932         handler_keys(wdata, payload);
933
934         wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
935         wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
936
937         buf[0] = 0;
938         buf[1] = 0;
939         buf[2] = wdata->state.accel_split[0];
940         buf[3] = payload[2];
941         buf[4] = wdata->state.accel_split[1];
942         handler_accel(wdata, buf);
943
944         ir_to_input2(wdata, &payload[3], false);
945         ir_to_input3(wdata, &payload[12], false);
946         input_sync(wdata->ir);
947 }
948
949 struct wiiproto_handler {
950         __u8 id;
951         size_t size;
952         void (*func)(struct wiimote_data *wdata, const __u8 *payload);
953 };
954
955 static struct wiiproto_handler handlers[] = {
956         { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
957         { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
958         { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
959         { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
960         { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
961         { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
962         { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
963         { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
964         { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
965         { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
966         { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
967         { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
968         { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
969         { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
970         { .id = 0 }
971 };
972
973 static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
974                                                         u8 *raw_data, int size)
975 {
976         struct wiimote_data *wdata = hid_get_drvdata(hdev);
977         struct wiiproto_handler *h;
978         int i;
979         unsigned long flags;
980         bool handled = false;
981
982         if (size < 1)
983                 return -EINVAL;
984
985         spin_lock_irqsave(&wdata->state.lock, flags);
986
987         for (i = 0; handlers[i].id; ++i) {
988                 h = &handlers[i];
989                 if (h->id == raw_data[0] && h->size < size) {
990                         h->func(wdata, &raw_data[1]);
991                         handled = true;
992                 }
993         }
994
995         if (!handled)
996                 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
997                                                                         size);
998
999         spin_unlock_irqrestore(&wdata->state.lock, flags);
1000
1001         return 0;
1002 }
1003
1004 static void wiimote_leds_destroy(struct wiimote_data *wdata)
1005 {
1006         int i;
1007         struct led_classdev *led;
1008
1009         for (i = 0; i < 4; ++i) {
1010                 if (wdata->leds[i]) {
1011                         led = wdata->leds[i];
1012                         wdata->leds[i] = NULL;
1013                         led_classdev_unregister(led);
1014                         kfree(led);
1015                 }
1016         }
1017 }
1018
1019 static int wiimote_leds_create(struct wiimote_data *wdata)
1020 {
1021         int i, ret;
1022         struct device *dev = &wdata->hdev->dev;
1023         size_t namesz = strlen(dev_name(dev)) + 9;
1024         struct led_classdev *led;
1025         char *name;
1026
1027         for (i = 0; i < 4; ++i) {
1028                 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1029                 if (!led) {
1030                         ret = -ENOMEM;
1031                         goto err;
1032                 }
1033                 name = (void*)&led[1];
1034                 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1035                 led->name = name;
1036                 led->brightness = 0;
1037                 led->max_brightness = 1;
1038                 led->brightness_get = wiimote_leds_get;
1039                 led->brightness_set = wiimote_leds_set;
1040
1041                 ret = led_classdev_register(dev, led);
1042                 if (ret) {
1043                         kfree(led);
1044                         goto err;
1045                 }
1046                 wdata->leds[i] = led;
1047         }
1048
1049         return 0;
1050
1051 err:
1052         wiimote_leds_destroy(wdata);
1053         return ret;
1054 }
1055
1056 static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1057 {
1058         struct wiimote_data *wdata;
1059         int i;
1060
1061         wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1062         if (!wdata)
1063                 return NULL;
1064
1065         wdata->input = input_allocate_device();
1066         if (!wdata->input)
1067                 goto err;
1068
1069         wdata->hdev = hdev;
1070         hid_set_drvdata(hdev, wdata);
1071
1072         input_set_drvdata(wdata->input, wdata);
1073         wdata->input->open = wiimote_input_open;
1074         wdata->input->close = wiimote_input_close;
1075         wdata->input->dev.parent = &wdata->hdev->dev;
1076         wdata->input->id.bustype = wdata->hdev->bus;
1077         wdata->input->id.vendor = wdata->hdev->vendor;
1078         wdata->input->id.product = wdata->hdev->product;
1079         wdata->input->id.version = wdata->hdev->version;
1080         wdata->input->name = WIIMOTE_NAME;
1081
1082         set_bit(EV_KEY, wdata->input->evbit);
1083         for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1084                 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1085
1086         set_bit(FF_RUMBLE, wdata->input->ffbit);
1087         if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1088                 goto err_input;
1089
1090         wdata->accel = input_allocate_device();
1091         if (!wdata->accel)
1092                 goto err_input;
1093
1094         input_set_drvdata(wdata->accel, wdata);
1095         wdata->accel->open = wiimote_accel_open;
1096         wdata->accel->close = wiimote_accel_close;
1097         wdata->accel->dev.parent = &wdata->hdev->dev;
1098         wdata->accel->id.bustype = wdata->hdev->bus;
1099         wdata->accel->id.vendor = wdata->hdev->vendor;
1100         wdata->accel->id.product = wdata->hdev->product;
1101         wdata->accel->id.version = wdata->hdev->version;
1102         wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1103
1104         set_bit(EV_ABS, wdata->accel->evbit);
1105         set_bit(ABS_RX, wdata->accel->absbit);
1106         set_bit(ABS_RY, wdata->accel->absbit);
1107         set_bit(ABS_RZ, wdata->accel->absbit);
1108         input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1109         input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1110         input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
1111
1112         wdata->ir = input_allocate_device();
1113         if (!wdata->ir)
1114                 goto err_ir;
1115
1116         input_set_drvdata(wdata->ir, wdata);
1117         wdata->ir->open = wiimote_ir_open;
1118         wdata->ir->close = wiimote_ir_close;
1119         wdata->ir->dev.parent = &wdata->hdev->dev;
1120         wdata->ir->id.bustype = wdata->hdev->bus;
1121         wdata->ir->id.vendor = wdata->hdev->vendor;
1122         wdata->ir->id.product = wdata->hdev->product;
1123         wdata->ir->id.version = wdata->hdev->version;
1124         wdata->ir->name = WIIMOTE_NAME " IR";
1125
1126         set_bit(EV_ABS, wdata->ir->evbit);
1127         set_bit(ABS_HAT0X, wdata->ir->absbit);
1128         set_bit(ABS_HAT0Y, wdata->ir->absbit);
1129         set_bit(ABS_HAT1X, wdata->ir->absbit);
1130         set_bit(ABS_HAT1Y, wdata->ir->absbit);
1131         set_bit(ABS_HAT2X, wdata->ir->absbit);
1132         set_bit(ABS_HAT2Y, wdata->ir->absbit);
1133         set_bit(ABS_HAT3X, wdata->ir->absbit);
1134         set_bit(ABS_HAT3Y, wdata->ir->absbit);
1135         input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1136         input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1137         input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1138         input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1139         input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1140         input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1141         input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1142         input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1143
1144         spin_lock_init(&wdata->qlock);
1145         INIT_WORK(&wdata->worker, wiimote_worker);
1146
1147         spin_lock_init(&wdata->state.lock);
1148         init_completion(&wdata->state.ready);
1149         mutex_init(&wdata->state.sync);
1150
1151         return wdata;
1152
1153 err_ir:
1154         input_free_device(wdata->accel);
1155 err_input:
1156         input_free_device(wdata->input);
1157 err:
1158         kfree(wdata);
1159         return NULL;
1160 }
1161
1162 static void wiimote_destroy(struct wiimote_data *wdata)
1163 {
1164         wiidebug_deinit(wdata);
1165         wiiext_deinit(wdata);
1166         wiimote_leds_destroy(wdata);
1167
1168         power_supply_unregister(&wdata->battery);
1169         input_unregister_device(wdata->accel);
1170         input_unregister_device(wdata->ir);
1171         input_unregister_device(wdata->input);
1172         cancel_work_sync(&wdata->worker);
1173         hid_hw_stop(wdata->hdev);
1174
1175         kfree(wdata);
1176 }
1177
1178 static int wiimote_hid_probe(struct hid_device *hdev,
1179                                 const struct hid_device_id *id)
1180 {
1181         struct wiimote_data *wdata;
1182         int ret;
1183
1184         wdata = wiimote_create(hdev);
1185         if (!wdata) {
1186                 hid_err(hdev, "Can't alloc device\n");
1187                 return -ENOMEM;
1188         }
1189
1190         ret = hid_parse(hdev);
1191         if (ret) {
1192                 hid_err(hdev, "HID parse failed\n");
1193                 goto err;
1194         }
1195
1196         ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1197         if (ret) {
1198                 hid_err(hdev, "HW start failed\n");
1199                 goto err;
1200         }
1201
1202         ret = input_register_device(wdata->accel);
1203         if (ret) {
1204                 hid_err(hdev, "Cannot register input device\n");
1205                 goto err_stop;
1206         }
1207
1208         ret = input_register_device(wdata->ir);
1209         if (ret) {
1210                 hid_err(hdev, "Cannot register input device\n");
1211                 goto err_ir;
1212         }
1213
1214         ret = input_register_device(wdata->input);
1215         if (ret) {
1216                 hid_err(hdev, "Cannot register input device\n");
1217                 goto err_input;
1218         }
1219
1220         wdata->battery.properties = wiimote_battery_props;
1221         wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1222         wdata->battery.get_property = wiimote_battery_get_property;
1223         wdata->battery.name = "wiimote_battery";
1224         wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1225         wdata->battery.use_for_apm = 0;
1226
1227         ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1228         if (ret) {
1229                 hid_err(hdev, "Cannot register battery device\n");
1230                 goto err_battery;
1231         }
1232
1233         ret = wiimote_leds_create(wdata);
1234         if (ret)
1235                 goto err_free;
1236
1237         ret = wiiext_init(wdata);
1238         if (ret)
1239                 goto err_free;
1240
1241         ret = wiidebug_init(wdata);
1242         if (ret)
1243                 goto err_free;
1244
1245         hid_info(hdev, "New device registered\n");
1246
1247         /* by default set led1 after device initialization */
1248         spin_lock_irq(&wdata->state.lock);
1249         wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
1250         spin_unlock_irq(&wdata->state.lock);
1251
1252         return 0;
1253
1254 err_free:
1255         wiimote_destroy(wdata);
1256         return ret;
1257
1258 err_battery:
1259         input_unregister_device(wdata->input);
1260         wdata->input = NULL;
1261 err_input:
1262         input_unregister_device(wdata->ir);
1263         wdata->ir = NULL;
1264 err_ir:
1265         input_unregister_device(wdata->accel);
1266         wdata->accel = NULL;
1267 err_stop:
1268         hid_hw_stop(hdev);
1269 err:
1270         input_free_device(wdata->ir);
1271         input_free_device(wdata->accel);
1272         input_free_device(wdata->input);
1273         kfree(wdata);
1274         return ret;
1275 }
1276
1277 static void wiimote_hid_remove(struct hid_device *hdev)
1278 {
1279         struct wiimote_data *wdata = hid_get_drvdata(hdev);
1280
1281         hid_info(hdev, "Device removed\n");
1282         wiimote_destroy(wdata);
1283 }
1284
1285 static const struct hid_device_id wiimote_hid_devices[] = {
1286         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1287                                 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
1288         { }
1289 };
1290 MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1291
1292 static struct hid_driver wiimote_hid_driver = {
1293         .name = "wiimote",
1294         .id_table = wiimote_hid_devices,
1295         .probe = wiimote_hid_probe,
1296         .remove = wiimote_hid_remove,
1297         .raw_event = wiimote_hid_event,
1298 };
1299
1300 static int __init wiimote_init(void)
1301 {
1302         int ret;
1303
1304         ret = hid_register_driver(&wiimote_hid_driver);
1305         if (ret)
1306                 pr_err("Can't register wiimote hid driver\n");
1307
1308         return ret;
1309 }
1310
1311 static void __exit wiimote_exit(void)
1312 {
1313         hid_unregister_driver(&wiimote_hid_driver);
1314 }
1315
1316 module_init(wiimote_init);
1317 module_exit(wiimote_exit);
1318 MODULE_LICENSE("GPL");
1319 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
1320 MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
1321 MODULE_VERSION(WIIMOTE_VERSION);