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