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