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