blob: 7c703e1f90d87d0e7c22dd384a7e9a75d97edbc8 [file] [log] [blame]
David Herrmannfb51b442011-07-05 13:45:08 +02001/*
David Herrmann92eda7e2013-05-05 23:12:45 +02002 * HID driver for Nintendo Wii / Wii U peripherals
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
David Herrmannfb51b442011-07-05 13:45:08 +02004 */
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
David Herrmann29d28062011-09-06 13:50:34 +020013#include <linux/completion.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020014#include <linux/device.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020015#include <linux/hid.h>
David Herrmann672bc4e2011-07-05 13:45:11 +020016#include <linux/input.h>
David Herrmannfb51b442011-07-05 13:45:08 +020017#include <linux/module.h>
David Herrmann29d28062011-09-06 13:50:34 +020018#include <linux/mutex.h>
David Herrmann23c063c2011-07-05 13:45:14 +020019#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020020#include "hid-ids.h"
David Herrmann7e274402011-11-17 14:11:59 +010021#include "hid-wiimote.h"
David Herrmannfb51b442011-07-05 13:45:08 +020022
David Herrmann13938532013-05-05 23:12:46 +020023/* output queue handling */
24
David Herrmannd758b1f02013-05-05 23:12:50 +020025static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
26 size_t count)
David Herrmann0c218f142011-07-05 13:45:13 +020027{
28 __u8 *buf;
David Herrmannd758b1f02013-05-05 23:12:50 +020029 int ret;
David Herrmann0c218f142011-07-05 13:45:13 +020030
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
David Herrmann13938532013-05-05 23:12:46 +020044static void wiimote_queue_worker(struct work_struct *work)
David Herrmann23c063c2011-07-05 13:45:14 +020045{
David Herrmann13938532013-05-05 23:12:46 +020046 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);
David Herrmann23c063c2011-07-05 13:45:14 +020050 unsigned long flags;
David Herrmannd758b1f02013-05-05 23:12:50 +020051 int ret;
David Herrmann23c063c2011-07-05 13:45:14 +020052
David Herrmann13938532013-05-05 23:12:46 +020053 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020054
David Herrmann13938532013-05-05 23:12:46 +020055 while (wdata->queue.head != wdata->queue.tail) {
56 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmannd758b1f02013-05-05 23:12:50 +020057 ret = wiimote_hid_send(wdata->hdev,
David Herrmann13938532013-05-05 23:12:46 +020058 wdata->queue.outq[wdata->queue.tail].data,
59 wdata->queue.outq[wdata->queue.tail].size);
David Herrmannd758b1f02013-05-05 23:12:50 +020060 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 }
David Herrmann13938532013-05-05 23:12:46 +020065 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020066
David Herrmann13938532013-05-05 23:12:46 +020067 wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
David Herrmann23c063c2011-07-05 13:45:14 +020068 }
69
David Herrmann13938532013-05-05 23:12:46 +020070 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020071}
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");
David Herrmannd758b1f02013-05-05 23:12:50 +020081
82 spin_lock_irqsave(&wdata->queue.lock, flags);
83 goto out_error;
David Herrmann23c063c2011-07-05 13:45:14 +020084 }
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
David Herrmann13938532013-05-05 23:12:46 +020096 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020097
David Herrmann13938532013-05-05 23:12:46 +020098 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;
David Herrmann23c063c2011-07-05 13:45:14 +0200101
David Herrmann13938532013-05-05 23:12:46 +0200102 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;
David Herrmann23c063c2011-07-05 13:45:14 +0200107 } else {
108 hid_warn(wdata->hdev, "Output queue is full");
David Herrmannd758b1f02013-05-05 23:12:50 +0200109 goto out_error;
David Herrmann23c063c2011-07-05 13:45:14 +0200110 }
111
David Herrmannd758b1f02013-05-05 23:12:50 +0200112 goto out_unlock;
113
114out_error:
115 wiimote_cmd_abort(wdata);
116out_unlock:
David Herrmann13938532013-05-05 23:12:46 +0200117 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200118}
119
David Herrmannc003ec22011-09-06 13:50:26 +0200120/*
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
David Herrmann20cef812013-05-05 23:12:52 +0200133void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
David Herrmannc003ec22011-09-06 13:50:26 +0200134{
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
David Herrmann6c5ae012013-05-05 23:12:54 +0200153void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
David Herrmanndb308342011-07-05 13:45:17 +0200154{
155 __u8 cmd[2];
156
David Herrmann32a0d9a2011-07-05 13:45:18 +0200157 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
David Herrmanndb308342011-07-05 13:45:17 +0200162 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
David Herrmannc003ec22011-09-06 13:50:26 +0200174 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200175 wiimote_queue(wdata, cmd, sizeof(cmd));
176}
177
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200178/*
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{
David Herrmannf363e4f2011-09-06 13:50:30 +0200185 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
David Herrmanncb992212011-11-17 14:12:01 +0100186 bool ext = wiiext_active(wdata);
David Herrmannf363e4f2011-09-06 13:50:30 +0200187
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 {
David Herrmanncb992212011-11-17 14:12:01 +0100198 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 }
David Herrmannf363e4f2011-09-06 13:50:30 +0200209 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200210}
211
David Herrmann7e274402011-11-17 14:11:59 +0100212void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200213{
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
David Herrmann43d782a2011-11-17 14:12:12 +0100223 wdata->state.drm = drm;
David Herrmannc003ec22011-09-06 13:50:26 +0200224 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200225 wiimote_queue(wdata, cmd, sizeof(cmd));
226}
227
David Herrmanndcf39232013-05-05 23:12:53 +0200228void wiiproto_req_status(struct wiimote_data *wdata)
David Herrmanne3979a92011-09-06 13:50:38 +0200229{
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
David Herrmann0ea16752013-05-05 23:12:55 +0200239void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
David Herrmann98a558a2011-09-06 13:50:28 +0200240{
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
David Herrmannfc221cd2011-09-06 13:50:36 +0200253static 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
David Herrmannbe1ecd62011-09-06 13:50:33 +0200275#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
David Herrmann1d3452c2011-11-17 14:12:11 +0100306void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
307 __u16 size)
David Herrmannfad8c0e2011-11-17 14:12:00 +0100308{
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
David Herrmann33e84012011-09-06 13:50:35 +0200331/* requries the cmd-mutex to be held */
David Herrmann7e274402011-11-17 14:11:59 +0100332int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
David Herrmann33e84012011-09-06 13:50:35 +0200333 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
David Herrmannfad8c0e2011-11-17 14:12:00 +0100350/* 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
David Herrmannc57ff762013-05-05 23:12:48 +0200380/* 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
David Herrmannfc221cd2011-09-06 13:50:36 +0200419static 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
David Herrmann0370d7c2011-09-06 13:50:37 +0200531static int wiimote_ir_open(struct input_dev *dev)
532{
533 struct wiimote_data *wdata = input_get_drvdata(dev);
David Herrmann0370d7c2011-09-06 13:50:37 +0200534
David Herrmann5682b1a2013-05-05 23:12:47 +0200535 return wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
David Herrmann0370d7c2011-09-06 13:50:37 +0200536}
537
538static void wiimote_ir_close(struct input_dev *dev)
539{
540 struct wiimote_data *wdata = input_get_drvdata(dev);
541
542 wiimote_init_ir(wdata, 0);
David Herrmann0370d7c2011-09-06 13:50:37 +0200543}
544
David Herrmann27f06942013-05-05 23:12:51 +0200545/* device module handling */
546
547static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
548 [WIIMOTE_DEV_PENDING] = (const __u8[]){
549 WIIMOD_NULL,
550 },
551 [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
552 WIIMOD_NULL,
553 },
554 [WIIMOTE_DEV_GENERIC] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200555 WIIMOD_KEYS,
556 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200557 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200558 WIIMOD_LED1,
559 WIIMOD_LED2,
560 WIIMOD_LED3,
561 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200562 WIIMOD_ACCEL,
David Herrmann27f06942013-05-05 23:12:51 +0200563 WIIMOD_NULL,
564 },
565 [WIIMOTE_DEV_GEN10] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200566 WIIMOD_KEYS,
567 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200568 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200569 WIIMOD_LED1,
570 WIIMOD_LED2,
571 WIIMOD_LED3,
572 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200573 WIIMOD_ACCEL,
David Herrmann27f06942013-05-05 23:12:51 +0200574 WIIMOD_NULL,
575 },
576 [WIIMOTE_DEV_GEN20] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200577 WIIMOD_KEYS,
578 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200579 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200580 WIIMOD_LED1,
581 WIIMOD_LED2,
582 WIIMOD_LED3,
583 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200584 WIIMOD_ACCEL,
David Herrmann27f06942013-05-05 23:12:51 +0200585 WIIMOD_NULL,
586 },
587};
588
589static void wiimote_modules_load(struct wiimote_data *wdata,
590 unsigned int devtype)
591{
592 bool need_input = false;
593 const __u8 *mods, *iter;
594 const struct wiimod_ops *ops;
595 int ret;
596
597 mods = wiimote_devtype_mods[devtype];
598
599 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
600 if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
601 need_input = true;
602 break;
603 }
604 }
605
606 if (need_input) {
607 wdata->input = input_allocate_device();
608 if (!wdata->input)
609 return;
610
611 input_set_drvdata(wdata->input, wdata);
612 wdata->input->dev.parent = &wdata->hdev->dev;
613 wdata->input->id.bustype = wdata->hdev->bus;
614 wdata->input->id.vendor = wdata->hdev->vendor;
615 wdata->input->id.product = wdata->hdev->product;
616 wdata->input->id.version = wdata->hdev->version;
617 wdata->input->name = WIIMOTE_NAME;
618 }
619
620 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
621 ops = wiimod_table[*iter];
622 if (!ops->probe)
623 continue;
624
625 ret = ops->probe(ops, wdata);
626 if (ret)
627 goto error;
628 }
629
630 if (wdata->input) {
631 ret = input_register_device(wdata->input);
632 if (ret)
633 goto error;
634 }
635
636 spin_lock_irq(&wdata->state.lock);
637 wdata->state.devtype = devtype;
638 spin_unlock_irq(&wdata->state.lock);
639 return;
640
641error:
642 for ( ; iter-- != mods; ) {
643 ops = wiimod_table[*iter];
644 if (ops->remove)
645 ops->remove(ops, wdata);
646 }
647
648 if (wdata->input) {
649 input_free_device(wdata->input);
650 wdata->input = NULL;
651 }
652}
653
654static void wiimote_modules_unload(struct wiimote_data *wdata)
655{
656 const __u8 *mods, *iter;
657 const struct wiimod_ops *ops;
658 unsigned long flags;
659
660 mods = wiimote_devtype_mods[wdata->state.devtype];
661
662 spin_lock_irqsave(&wdata->state.lock, flags);
663 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
664 spin_unlock_irqrestore(&wdata->state.lock, flags);
665
666 /* find end of list */
667 for (iter = mods; *iter != WIIMOD_NULL; ++iter)
668 /* empty */ ;
669
670 if (wdata->input) {
671 input_get_device(wdata->input);
672 input_unregister_device(wdata->input);
673 }
674
675 for ( ; iter-- != mods; ) {
676 ops = wiimod_table[*iter];
677 if (ops->remove)
678 ops->remove(ops, wdata);
679 }
680
681 if (wdata->input) {
682 input_put_device(wdata->input);
683 wdata->input = NULL;
684 }
685}
686
David Herrmannc57ff762013-05-05 23:12:48 +0200687/* device (re-)initialization and detection */
688
689static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
690 [WIIMOTE_DEV_PENDING] = "Pending",
691 [WIIMOTE_DEV_UNKNOWN] = "Unknown",
692 [WIIMOTE_DEV_GENERIC] = "Generic",
693 [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
694 [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
695};
696
697/* Try to guess the device type based on all collected information. We
698 * first try to detect by static extension types, then VID/PID and the
699 * device name. If we cannot detect the device, we use
700 * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
701static void wiimote_init_set_type(struct wiimote_data *wdata,
702 __u8 exttype)
703{
704 __u8 devtype = WIIMOTE_DEV_GENERIC;
705 __u16 vendor, product;
706 const char *name;
707
708 vendor = wdata->hdev->vendor;
709 product = wdata->hdev->product;
710 name = wdata->hdev->name;
711
712 if (!strcmp(name, "Nintendo RVL-CNT-01")) {
713 devtype = WIIMOTE_DEV_GEN10;
714 goto done;
715 } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
716 devtype = WIIMOTE_DEV_GEN20;
717 goto done;
718 }
719
720 if (vendor == USB_VENDOR_ID_NINTENDO) {
721 if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
722 devtype = WIIMOTE_DEV_GEN10;
723 goto done;
724 } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
725 devtype = WIIMOTE_DEV_GEN20;
726 goto done;
727 }
728 }
729
730done:
731 if (devtype == WIIMOTE_DEV_GENERIC)
732 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
733 name, vendor, product, exttype);
734 else
735 hid_info(wdata->hdev, "detected device: %s\n",
736 wiimote_devtype_names[devtype]);
737
David Herrmann27f06942013-05-05 23:12:51 +0200738 wiimote_modules_load(wdata, devtype);
David Herrmannc57ff762013-05-05 23:12:48 +0200739}
740
741static void wiimote_init_detect(struct wiimote_data *wdata)
742{
743 __u8 exttype = WIIMOTE_EXT_NONE;
744 bool ext;
745 int ret;
746
747 wiimote_cmd_acquire_noint(wdata);
748
749 spin_lock_irq(&wdata->state.lock);
David Herrmann27f06942013-05-05 23:12:51 +0200750 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
David Herrmannc57ff762013-05-05 23:12:48 +0200751 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
752 wiiproto_req_status(wdata);
753 spin_unlock_irq(&wdata->state.lock);
754
755 ret = wiimote_cmd_wait_noint(wdata);
756 if (ret)
757 goto out_release;
758
759 spin_lock_irq(&wdata->state.lock);
760 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
761 spin_unlock_irq(&wdata->state.lock);
762
763 if (!ext)
764 goto out_release;
765
766 wiimote_cmd_init_ext(wdata);
767 exttype = wiimote_cmd_read_ext(wdata);
768
769out_release:
770 wiimote_cmd_release(wdata);
771 wiimote_init_set_type(wdata, exttype);
772}
773
774static void wiimote_init_worker(struct work_struct *work)
775{
776 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
777 init_worker);
778
779 if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
780 wiimote_init_detect(wdata);
781}
782
783/* protocol handlers */
784
David Herrmann1abb9ad2011-07-05 13:45:16 +0200785static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
786{
David Herrmann20cef812013-05-05 23:12:52 +0200787 const __u8 *iter, *mods;
788 const struct wiimod_ops *ops;
789
790 mods = wiimote_devtype_mods[wdata->state.devtype];
791 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
792 ops = wiimod_table[*iter];
793 if (ops->in_keys) {
794 ops->in_keys(wdata, payload);
795 break;
796 }
797 }
David Herrmann1abb9ad2011-07-05 13:45:16 +0200798}
799
David Herrmannefcf9182011-09-06 13:50:29 +0200800static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
801{
David Herrmann0ea16752013-05-05 23:12:55 +0200802 const __u8 *iter, *mods;
803 const struct wiimod_ops *ops;
David Herrmannefcf9182011-09-06 13:50:29 +0200804
David Herrmann0ea16752013-05-05 23:12:55 +0200805 mods = wiimote_devtype_mods[wdata->state.devtype];
806 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
807 ops = wiimod_table[*iter];
808 if (ops->in_accel) {
809 ops->in_accel(wdata, payload);
810 break;
811 }
812 }
David Herrmannefcf9182011-09-06 13:50:29 +0200813}
814
David Herrmanneac39e72011-09-06 13:50:31 +0200815#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
816 ABS_HAT0X, ABS_HAT0Y)
817#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
818 ABS_HAT1X, ABS_HAT1Y)
819#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
820 ABS_HAT2X, ABS_HAT2Y)
821#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
822 ABS_HAT3X, ABS_HAT3Y)
823
824static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
825 bool packed, __u8 xid, __u8 yid)
826{
827 __u16 x, y;
828
829 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
830 return;
831
832 /*
833 * Basic IR data is encoded into 3 bytes. The first two bytes are the
David Herrmann74b89e82012-05-08 16:52:31 +0200834 * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
David Herrmanneac39e72011-09-06 13:50:31 +0200835 * of both.
836 * If data is packed, then the 3rd byte is put first and slightly
837 * reordered. This allows to interleave packed and non-packed data to
838 * have two IR sets in 5 bytes instead of 6.
839 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
840 */
841
842 if (packed) {
David Herrmann74b89e82012-05-08 16:52:31 +0200843 x = ir[1] | ((ir[0] & 0x03) << 8);
844 y = ir[2] | ((ir[0] & 0x0c) << 6);
David Herrmanneac39e72011-09-06 13:50:31 +0200845 } else {
David Herrmann74b89e82012-05-08 16:52:31 +0200846 x = ir[0] | ((ir[2] & 0x30) << 4);
847 y = ir[1] | ((ir[2] & 0xc0) << 2);
David Herrmanneac39e72011-09-06 13:50:31 +0200848 }
849
850 input_report_abs(wdata->ir, xid, x);
851 input_report_abs(wdata->ir, yid, y);
852}
David Herrmannefcf9182011-09-06 13:50:29 +0200853
David Herrmann2d44e3d2013-04-02 19:58:36 +0200854/* reduced status report with "BB BB" key data only */
855static void handler_status_K(struct wiimote_data *wdata,
856 const __u8 *payload)
David Herrmannc87019e2011-08-17 11:43:24 +0200857{
858 handler_keys(wdata, payload);
859
860 /* on status reports the drm is reset so we need to resend the drm */
861 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmann2d44e3d2013-04-02 19:58:36 +0200862}
863
864/* extended status report with "BB BB LF 00 00 VV" data */
865static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
866{
867 handler_status_K(wdata, payload);
David Herrmanne3979a92011-09-06 13:50:38 +0200868
David Herrmannc57ff762013-05-05 23:12:48 +0200869 /* update extension status */
870 if (payload[2] & 0x02) {
871 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
872 wiiext_event(wdata, true);
873 } else {
874 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
875 wiiext_event(wdata, false);
876 }
David Herrmanncb992212011-11-17 14:12:01 +0100877
David Herrmann6b80bb92013-05-05 23:12:49 +0200878 wdata->state.cmd_battery = payload[5];
879 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
David Herrmanne3979a92011-09-06 13:50:38 +0200880 wiimote_cmd_complete(wdata);
David Herrmannc87019e2011-08-17 11:43:24 +0200881}
882
David Herrmann2d44e3d2013-04-02 19:58:36 +0200883/* reduced generic report with "BB BB" key data only */
884static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
885{
886 handler_keys(wdata, payload);
887}
888
David Herrmannbe1ecd62011-09-06 13:50:33 +0200889static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
890{
David Herrmannfad8c0e2011-11-17 14:12:00 +0100891 __u16 offset = payload[3] << 8 | payload[4];
892 __u8 size = (payload[2] >> 4) + 1;
893 __u8 err = payload[2] & 0x0f;
894
David Herrmannbe1ecd62011-09-06 13:50:33 +0200895 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +0100896
897 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
898 if (err)
899 size = 0;
900 else if (size > wdata->state.cmd_read_size)
901 size = wdata->state.cmd_read_size;
902
903 wdata->state.cmd_read_size = size;
904 if (wdata->state.cmd_read_buf)
905 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
906 wiimote_cmd_complete(wdata);
907 }
David Herrmannbe1ecd62011-09-06 13:50:33 +0200908}
909
David Herrmannc87019e2011-08-17 11:43:24 +0200910static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
911{
912 __u8 err = payload[3];
913 __u8 cmd = payload[2];
914
915 handler_keys(wdata, payload);
916
David Herrmann33e84012011-09-06 13:50:35 +0200917 if (wiimote_cmd_pending(wdata, cmd, 0)) {
918 wdata->state.cmd_err = err;
919 wiimote_cmd_complete(wdata);
920 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200921 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
922 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200923 }
David Herrmannc87019e2011-08-17 11:43:24 +0200924}
925
David Herrmannefcf9182011-09-06 13:50:29 +0200926static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
927{
928 handler_keys(wdata, payload);
929 handler_accel(wdata, payload);
930}
931
David Herrmann7336b9f2011-09-06 13:50:32 +0200932static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
933{
934 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100935 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200936}
937
David Herrmannefcf9182011-09-06 13:50:29 +0200938static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
939{
940 handler_keys(wdata, payload);
941 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200942 ir_to_input0(wdata, &payload[5], false);
943 ir_to_input1(wdata, &payload[8], false);
944 ir_to_input2(wdata, &payload[11], false);
945 ir_to_input3(wdata, &payload[14], false);
946 input_sync(wdata->ir);
947}
948
David Herrmann7336b9f2011-09-06 13:50:32 +0200949static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
950{
951 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100952 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200953}
954
David Herrmanneac39e72011-09-06 13:50:31 +0200955static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
956{
957 handler_keys(wdata, payload);
958 ir_to_input0(wdata, &payload[2], false);
959 ir_to_input1(wdata, &payload[4], true);
960 ir_to_input2(wdata, &payload[7], false);
961 ir_to_input3(wdata, &payload[9], true);
962 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +0100963 wiiext_handle(wdata, &payload[12]);
David Herrmannefcf9182011-09-06 13:50:29 +0200964}
965
966static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
967{
968 handler_keys(wdata, payload);
969 handler_accel(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100970 wiiext_handle(wdata, &payload[5]);
David Herrmannefcf9182011-09-06 13:50:29 +0200971}
972
973static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
974{
975 handler_keys(wdata, payload);
976 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200977 ir_to_input0(wdata, &payload[5], false);
978 ir_to_input1(wdata, &payload[7], true);
979 ir_to_input2(wdata, &payload[10], false);
980 ir_to_input3(wdata, &payload[12], true);
981 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +0100982 wiiext_handle(wdata, &payload[15]);
David Herrmannefcf9182011-09-06 13:50:29 +0200983}
984
David Herrmann7336b9f2011-09-06 13:50:32 +0200985static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
986{
David Herrmann0b6815d2011-11-17 14:12:06 +0100987 wiiext_handle(wdata, payload);
David Herrmann7336b9f2011-09-06 13:50:32 +0200988}
989
David Herrmannefcf9182011-09-06 13:50:29 +0200990static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
991{
992 handler_keys(wdata, payload);
993
994 wdata->state.accel_split[0] = payload[2];
995 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
996 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200997
998 ir_to_input0(wdata, &payload[3], false);
999 ir_to_input1(wdata, &payload[12], false);
1000 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +02001001}
1002
1003static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1004{
1005 __u8 buf[5];
1006
1007 handler_keys(wdata, payload);
1008
1009 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1010 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1011
1012 buf[0] = 0;
1013 buf[1] = 0;
1014 buf[2] = wdata->state.accel_split[0];
1015 buf[3] = payload[2];
1016 buf[4] = wdata->state.accel_split[1];
1017 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +02001018
1019 ir_to_input2(wdata, &payload[3], false);
1020 ir_to_input3(wdata, &payload[12], false);
1021 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +02001022}
1023
David Herrmanna4d19192011-07-05 13:45:15 +02001024struct wiiproto_handler {
1025 __u8 id;
1026 size_t size;
1027 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1028};
1029
1030static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +02001031 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001032 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
David Herrmannbe1ecd62011-09-06 13:50:33 +02001033 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001034 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
David Herrmannc87019e2011-08-17 11:43:24 +02001035 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001036 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
David Herrmann1abb9ad2011-07-05 13:45:16 +02001037 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +02001038 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001039 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001040 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001041 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001042 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001043 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001044 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001045 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001046 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001047 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
David Herrmanneac39e72011-09-06 13:50:31 +02001048 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001049 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001050 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001051 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001052 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +02001053 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1054 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +02001055 { .id = 0 }
1056};
1057
David Herrmann02fb72a2011-07-05 13:45:09 +02001058static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1059 u8 *raw_data, int size)
1060{
David Herrmann4d36e972011-07-05 13:45:12 +02001061 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +02001062 struct wiiproto_handler *h;
1063 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001064 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +02001065
David Herrmann02fb72a2011-07-05 13:45:09 +02001066 if (size < 1)
1067 return -EINVAL;
1068
David Herrmann32a0d9a2011-07-05 13:45:18 +02001069 spin_lock_irqsave(&wdata->state.lock, flags);
1070
David Herrmanna4d19192011-07-05 13:45:15 +02001071 for (i = 0; handlers[i].id; ++i) {
1072 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +02001073 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +02001074 h->func(wdata, &raw_data[1]);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001075 break;
David Herrmann7336b9f2011-09-06 13:50:32 +02001076 }
David Herrmanna4d19192011-07-05 13:45:15 +02001077 }
1078
David Herrmann2d44e3d2013-04-02 19:58:36 +02001079 if (!handlers[i].id)
David Herrmann7336b9f2011-09-06 13:50:32 +02001080 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1081 size);
1082
David Herrmann32a0d9a2011-07-05 13:45:18 +02001083 spin_unlock_irqrestore(&wdata->state.lock, flags);
1084
David Herrmann02fb72a2011-07-05 13:45:09 +02001085 return 0;
1086}
1087
David Herrmanne894d0e2011-07-05 13:45:10 +02001088static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1089{
1090 struct wiimote_data *wdata;
1091
1092 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1093 if (!wdata)
1094 return NULL;
1095
1096 wdata->hdev = hdev;
1097 hid_set_drvdata(hdev, wdata);
1098
David Herrmannf363e4f2011-09-06 13:50:30 +02001099 wdata->ir = input_allocate_device();
1100 if (!wdata->ir)
David Herrmann0ea16752013-05-05 23:12:55 +02001101 goto err;
David Herrmannf363e4f2011-09-06 13:50:30 +02001102
1103 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001104 wdata->ir->open = wiimote_ir_open;
1105 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001106 wdata->ir->dev.parent = &wdata->hdev->dev;
1107 wdata->ir->id.bustype = wdata->hdev->bus;
1108 wdata->ir->id.vendor = wdata->hdev->vendor;
1109 wdata->ir->id.product = wdata->hdev->product;
1110 wdata->ir->id.version = wdata->hdev->version;
1111 wdata->ir->name = WIIMOTE_NAME " IR";
1112
1113 set_bit(EV_ABS, wdata->ir->evbit);
1114 set_bit(ABS_HAT0X, wdata->ir->absbit);
1115 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1116 set_bit(ABS_HAT1X, wdata->ir->absbit);
1117 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1118 set_bit(ABS_HAT2X, wdata->ir->absbit);
1119 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1120 set_bit(ABS_HAT3X, wdata->ir->absbit);
1121 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1122 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1123 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1124 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1125 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1126 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1127 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1128 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1129 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1130
David Herrmann13938532013-05-05 23:12:46 +02001131 spin_lock_init(&wdata->queue.lock);
1132 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
David Herrmann23c063c2011-07-05 13:45:14 +02001133
David Herrmann32a0d9a2011-07-05 13:45:18 +02001134 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001135 init_completion(&wdata->state.ready);
1136 mutex_init(&wdata->state.sync);
David Herrmann43d782a2011-11-17 14:12:12 +01001137 wdata->state.drm = WIIPROTO_REQ_DRM_K;
David Herrmann6b80bb92013-05-05 23:12:49 +02001138 wdata->state.cmd_battery = 0xff;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001139
David Herrmannc57ff762013-05-05 23:12:48 +02001140 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
1141
David Herrmanne894d0e2011-07-05 13:45:10 +02001142 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001143
David Herrmann98a558a2011-09-06 13:50:28 +02001144err:
1145 kfree(wdata);
1146 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001147}
1148
1149static void wiimote_destroy(struct wiimote_data *wdata)
1150{
David Herrmann43e5e7c2011-11-17 14:12:10 +01001151 wiidebug_deinit(wdata);
David Herrmanncb992212011-11-17 14:12:01 +01001152 wiiext_deinit(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001153
David Herrmann20cef812013-05-05 23:12:52 +02001154 cancel_work_sync(&wdata->init_worker);
David Herrmann27f06942013-05-05 23:12:51 +02001155 wiimote_modules_unload(wdata);
David Herrmannf363e4f2011-09-06 13:50:30 +02001156 input_unregister_device(wdata->ir);
David Herrmann13938532013-05-05 23:12:46 +02001157 cancel_work_sync(&wdata->queue.worker);
David Herrmann5682b1a2013-05-05 23:12:47 +02001158 hid_hw_close(wdata->hdev);
David Herrmann3989ef62011-08-17 11:43:20 +02001159 hid_hw_stop(wdata->hdev);
1160
David Herrmanne894d0e2011-07-05 13:45:10 +02001161 kfree(wdata);
1162}
1163
David Herrmann02fb72a2011-07-05 13:45:09 +02001164static int wiimote_hid_probe(struct hid_device *hdev,
1165 const struct hid_device_id *id)
1166{
David Herrmanne894d0e2011-07-05 13:45:10 +02001167 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001168 int ret;
1169
David Herrmann90120d62011-11-17 14:12:14 +01001170 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1171
David Herrmanne894d0e2011-07-05 13:45:10 +02001172 wdata = wiimote_create(hdev);
1173 if (!wdata) {
1174 hid_err(hdev, "Can't alloc device\n");
1175 return -ENOMEM;
1176 }
1177
David Herrmann02fb72a2011-07-05 13:45:09 +02001178 ret = hid_parse(hdev);
1179 if (ret) {
1180 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001181 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001182 }
1183
1184 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1185 if (ret) {
1186 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001187 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001188 }
1189
David Herrmann5682b1a2013-05-05 23:12:47 +02001190 ret = hid_hw_open(hdev);
1191 if (ret) {
1192 hid_err(hdev, "cannot start hardware I/O\n");
1193 goto err_stop;
1194 }
1195
David Herrmannf363e4f2011-09-06 13:50:30 +02001196 ret = input_register_device(wdata->ir);
1197 if (ret) {
1198 hid_err(hdev, "Cannot register input device\n");
1199 goto err_ir;
1200 }
1201
David Herrmanncb992212011-11-17 14:12:01 +01001202 ret = wiiext_init(wdata);
1203 if (ret)
1204 goto err_free;
1205
David Herrmann43e5e7c2011-11-17 14:12:10 +01001206 ret = wiidebug_init(wdata);
1207 if (ret)
1208 goto err_free;
1209
David Herrmann02fb72a2011-07-05 13:45:09 +02001210 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001211
David Herrmannc57ff762013-05-05 23:12:48 +02001212 /* schedule device detection */
1213 schedule_work(&wdata->init_worker);
1214
David Herrmann02fb72a2011-07-05 13:45:09 +02001215 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001216
David Herrmann3989ef62011-08-17 11:43:20 +02001217err_free:
1218 wiimote_destroy(wdata);
1219 return ret;
1220
David Herrmannf363e4f2011-09-06 13:50:30 +02001221err_ir:
David Herrmann5682b1a2013-05-05 23:12:47 +02001222 hid_hw_close(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +02001223err_stop:
1224 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001225err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001226 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001227 input_free_device(wdata->accel);
David Herrmann3989ef62011-08-17 11:43:20 +02001228 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001229 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001230}
1231
1232static void wiimote_hid_remove(struct hid_device *hdev)
1233{
David Herrmanne894d0e2011-07-05 13:45:10 +02001234 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1235
David Herrmann02fb72a2011-07-05 13:45:09 +02001236 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001237 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001238}
1239
1240static const struct hid_device_id wiimote_hid_devices[] = {
1241 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1242 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
David Herrmanna33042f2013-04-02 19:58:35 +02001243 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1244 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
David Herrmann02fb72a2011-07-05 13:45:09 +02001245 { }
1246};
1247MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1248
1249static struct hid_driver wiimote_hid_driver = {
1250 .name = "wiimote",
1251 .id_table = wiimote_hid_devices,
1252 .probe = wiimote_hid_probe,
1253 .remove = wiimote_hid_remove,
1254 .raw_event = wiimote_hid_event,
1255};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001256module_hid_driver(wiimote_hid_driver);
David Herrmann02fb72a2011-07-05 13:45:09 +02001257
David Herrmannfb51b442011-07-05 13:45:08 +02001258MODULE_LICENSE("GPL");
1259MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
David Herrmann92eda7e2013-05-05 23:12:45 +02001260MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");