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