blob: dedf3c84e243f59162adc5e4e79e257a962bf32e [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 Herrmann0c218f12011-07-05 13:45:13 +020027{
28 __u8 *buf;
David Herrmannd758b1f02013-05-05 23:12:50 +020029 int ret;
David Herrmann0c218f12011-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.
David Herrmann4148b6b2013-05-05 23:12:57 +0200182 *
183 * Not all combinations are actually supported. The following
184 * combinations work only with limitations:
185 * - IR cam in extended or full mode disables any data transmission
186 * of extension controllers. There is no DRM mode that supports
187 * extension bytes plus extended/full IR.
188 * - IR cam with accelerometer and extension *_EXT8 is not supported.
189 * However, all extensions that need *_EXT8 are devices that don't
190 * support IR cameras. Hence, this shouldn't happen under normal
191 * operation.
192 * - *_EXT16 is only supported in combination with buttons and
193 * accelerometer. No IR or similar can be active simultaneously. As
194 * above, all modules that require it are mutually exclusive with
195 * IR/etc. so this doesn't matter.
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200196 */
197static __u8 select_drm(struct wiimote_data *wdata)
198{
David Herrmannf363e4f2011-09-06 13:50:30 +0200199 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
David Herrmann4148b6b2013-05-05 23:12:57 +0200200 bool ext;
201
202 ext = (wdata->state.flags & WIIPROTO_FLAG_EXT_USED) ||
203 (wdata->state.flags & WIIPROTO_FLAG_MP_USED);
David Herrmannf363e4f2011-09-06 13:50:30 +0200204
David Herrmannf1d4bed2013-05-05 23:12:58 +0200205 /* some 3rd-party balance-boards are hard-coded to KEE, *sigh* */
206 if (wdata->state.devtype == WIIMOTE_DEV_BALANCE_BOARD) {
207 if (ext)
208 return WIIPROTO_REQ_DRM_KEE;
209 else
210 return WIIPROTO_REQ_DRM_K;
211 }
212
David Herrmannf363e4f2011-09-06 13:50:30 +0200213 if (ir == WIIPROTO_FLAG_IR_BASIC) {
David Herrmann4148b6b2013-05-05 23:12:57 +0200214 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
215 if (ext)
216 return WIIPROTO_REQ_DRM_KAIE;
217 else
218 return WIIPROTO_REQ_DRM_KAI;
219 } else {
David Herrmannf363e4f2011-09-06 13:50:30 +0200220 return WIIPROTO_REQ_DRM_KIE;
David Herrmann4148b6b2013-05-05 23:12:57 +0200221 }
David Herrmannf363e4f2011-09-06 13:50:30 +0200222 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
223 return WIIPROTO_REQ_DRM_KAI;
224 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
225 return WIIPROTO_REQ_DRM_SKAI1;
226 } else {
David Herrmanncb992212011-11-17 14:12:01 +0100227 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
228 if (ext)
229 return WIIPROTO_REQ_DRM_KAE;
230 else
231 return WIIPROTO_REQ_DRM_KA;
232 } else {
233 if (ext)
David Herrmann4148b6b2013-05-05 23:12:57 +0200234 return WIIPROTO_REQ_DRM_KEE;
David Herrmanncb992212011-11-17 14:12:01 +0100235 else
236 return WIIPROTO_REQ_DRM_K;
237 }
David Herrmannf363e4f2011-09-06 13:50:30 +0200238 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200239}
240
David Herrmann7e274402011-11-17 14:11:59 +0100241void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200242{
243 __u8 cmd[3];
244
245 if (drm == WIIPROTO_REQ_NULL)
246 drm = select_drm(wdata);
247
248 cmd[0] = WIIPROTO_REQ_DRM;
249 cmd[1] = 0;
250 cmd[2] = drm;
251
David Herrmann43d782a2011-11-17 14:12:12 +0100252 wdata->state.drm = drm;
David Herrmannc003ec22011-09-06 13:50:26 +0200253 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200254 wiimote_queue(wdata, cmd, sizeof(cmd));
255}
256
David Herrmanndcf39232013-05-05 23:12:53 +0200257void wiiproto_req_status(struct wiimote_data *wdata)
David Herrmanne3979a92011-09-06 13:50:38 +0200258{
259 __u8 cmd[2];
260
261 cmd[0] = WIIPROTO_REQ_SREQ;
262 cmd[1] = 0;
263
264 wiiproto_keep_rumble(wdata, &cmd[1]);
265 wiimote_queue(wdata, cmd, sizeof(cmd));
266}
267
David Herrmann0ea16752013-05-05 23:12:55 +0200268void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
David Herrmann98a558a2011-09-06 13:50:28 +0200269{
270 accel = !!accel;
271 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
272 return;
273
274 if (accel)
275 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
276 else
277 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
278
279 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
280}
281
David Herrmann3b5f03c2013-05-05 23:12:56 +0200282void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
David Herrmannfc221cd2011-09-06 13:50:36 +0200283{
284 __u8 cmd[2];
285
286 cmd[0] = WIIPROTO_REQ_IR1;
287 cmd[1] = flags;
288
289 wiiproto_keep_rumble(wdata, &cmd[1]);
290 wiimote_queue(wdata, cmd, sizeof(cmd));
291}
292
David Herrmann3b5f03c2013-05-05 23:12:56 +0200293void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
David Herrmannfc221cd2011-09-06 13:50:36 +0200294{
295 __u8 cmd[2];
296
297 cmd[0] = WIIPROTO_REQ_IR2;
298 cmd[1] = flags;
299
300 wiiproto_keep_rumble(wdata, &cmd[1]);
301 wiimote_queue(wdata, cmd, sizeof(cmd));
302}
303
David Herrmannbe1ecd62011-09-06 13:50:33 +0200304#define wiiproto_req_wreg(wdata, os, buf, sz) \
305 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
306
307#define wiiproto_req_weeprom(wdata, os, buf, sz) \
308 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
309
310static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
311 __u32 offset, const __u8 *buf, __u8 size)
312{
313 __u8 cmd[22];
314
315 if (size > 16 || size == 0) {
316 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
317 return;
318 }
319
320 memset(cmd, 0, sizeof(cmd));
321 cmd[0] = WIIPROTO_REQ_WMEM;
322 cmd[2] = (offset >> 16) & 0xff;
323 cmd[3] = (offset >> 8) & 0xff;
324 cmd[4] = offset & 0xff;
325 cmd[5] = size;
326 memcpy(&cmd[6], buf, size);
327
328 if (!eeprom)
329 cmd[1] |= 0x04;
330
331 wiiproto_keep_rumble(wdata, &cmd[1]);
332 wiimote_queue(wdata, cmd, sizeof(cmd));
333}
334
David Herrmann1d3452c2011-11-17 14:12:11 +0100335void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
336 __u16 size)
David Herrmannfad8c0e2011-11-17 14:12:00 +0100337{
338 __u8 cmd[7];
339
340 if (size == 0) {
341 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
342 return;
343 }
344
345 cmd[0] = WIIPROTO_REQ_RMEM;
346 cmd[1] = 0;
347 cmd[2] = (offset >> 16) & 0xff;
348 cmd[3] = (offset >> 8) & 0xff;
349 cmd[4] = offset & 0xff;
350 cmd[5] = (size >> 8) & 0xff;
351 cmd[6] = size & 0xff;
352
353 if (!eeprom)
354 cmd[1] |= 0x04;
355
356 wiiproto_keep_rumble(wdata, &cmd[1]);
357 wiimote_queue(wdata, cmd, sizeof(cmd));
358}
359
David Herrmann33e84012011-09-06 13:50:35 +0200360/* requries the cmd-mutex to be held */
David Herrmann7e274402011-11-17 14:11:59 +0100361int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
David Herrmann33e84012011-09-06 13:50:35 +0200362 const __u8 *wmem, __u8 size)
363{
364 unsigned long flags;
365 int ret;
366
367 spin_lock_irqsave(&wdata->state.lock, flags);
368 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
369 wiiproto_req_wreg(wdata, offset, wmem, size);
370 spin_unlock_irqrestore(&wdata->state.lock, flags);
371
372 ret = wiimote_cmd_wait(wdata);
373 if (!ret && wdata->state.cmd_err)
374 ret = -EIO;
375
376 return ret;
377}
378
David Herrmannfad8c0e2011-11-17 14:12:00 +0100379/* requries the cmd-mutex to be held */
380ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
381 __u8 size)
382{
383 unsigned long flags;
384 ssize_t ret;
385
386 spin_lock_irqsave(&wdata->state.lock, flags);
387 wdata->state.cmd_read_size = size;
388 wdata->state.cmd_read_buf = rmem;
389 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
390 wiiproto_req_rreg(wdata, offset, size);
391 spin_unlock_irqrestore(&wdata->state.lock, flags);
392
393 ret = wiimote_cmd_wait(wdata);
394
395 spin_lock_irqsave(&wdata->state.lock, flags);
396 wdata->state.cmd_read_buf = NULL;
397 spin_unlock_irqrestore(&wdata->state.lock, flags);
398
399 if (!ret) {
400 if (wdata->state.cmd_read_size == 0)
401 ret = -EIO;
402 else
403 ret = wdata->state.cmd_read_size;
404 }
405
406 return ret;
407}
408
David Herrmannc57ff762013-05-05 23:12:48 +0200409/* requires the cmd-mutex to be held */
410static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
411{
412 __u8 wmem;
413 int ret;
414
415 /* initialize extension */
416 wmem = 0x55;
417 ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
418 if (ret)
419 return ret;
420
421 /* disable default encryption */
422 wmem = 0x0;
423 ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
424 if (ret)
425 return ret;
426
427 return 0;
428}
429
430/* requires the cmd-mutex to be held */
David Herrmann4148b6b2013-05-05 23:12:57 +0200431static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem)
David Herrmannc57ff762013-05-05 23:12:48 +0200432{
David Herrmannc57ff762013-05-05 23:12:48 +0200433 int ret;
434
435 /* read extension ID */
436 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
437 if (ret != 6)
438 return WIIMOTE_EXT_NONE;
439
David Herrmann4148b6b2013-05-05 23:12:57 +0200440 hid_dbg(wdata->hdev, "extension ID: %02x:%02x %02x:%02x %02x:%02x\n",
441 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
442
David Herrmannc57ff762013-05-05 23:12:48 +0200443 if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
444 rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
445 return WIIMOTE_EXT_NONE;
446
David Herrmannb6ee67b2013-05-05 23:12:59 +0200447 if (rmem[4] == 0x00 && rmem[5] == 0x00)
448 return WIIMOTE_EXT_NUNCHUK;
David Herrmannf1d4bed2013-05-05 23:12:58 +0200449 if (rmem[4] == 0x04 && rmem[5] == 0x02)
450 return WIIMOTE_EXT_BALANCE_BOARD;
451
David Herrmannc57ff762013-05-05 23:12:48 +0200452 return WIIMOTE_EXT_UNKNOWN;
453}
454
David Herrmann4148b6b2013-05-05 23:12:57 +0200455/* requires the cmd-mutex to be held */
456static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
457{
458 __u8 wmem;
459 int ret;
460
461 /* initialize MP */
462 wmem = 0x55;
463 ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
464 if (ret)
465 return ret;
466
467 /* disable default encryption */
468 wmem = 0x0;
469 ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
470 if (ret)
471 return ret;
472
473 return 0;
474}
475
476/* requires the cmd-mutex to be held */
477static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
478{
479 __u8 wmem;
480
481 /* map MP with correct pass-through mode */
482 switch (exttype) {
David Herrmannb6ee67b2013-05-05 23:12:59 +0200483 case WIIMOTE_EXT_NUNCHUK:
484 wmem = 0x05;
485 break;
David Herrmann4148b6b2013-05-05 23:12:57 +0200486 default:
487 wmem = 0x04;
488 break;
489 }
490
491 return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
492}
493
494/* requires the cmd-mutex to be held */
495static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
496{
497 int ret;
498
499 /* read motion plus ID */
500 ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
501 if (ret != 6)
502 return false;
503
504 hid_dbg(wdata->hdev, "motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
505 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
506
507 if (rmem[5] == 0x05)
508 return true;
509
510 hid_info(wdata->hdev, "unknown motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
511 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
512
513 return false;
514}
515
516/* requires the cmd-mutex to be held */
517static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
518{
519 int ret;
520 __u8 rmem[6];
521
522 /* read motion plus ID */
523 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
524 if (ret != 6)
525 return WIIMOTE_MP_NONE;
526
527 hid_dbg(wdata->hdev, "mapped motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
528 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
529
530 if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
531 rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
532 return WIIMOTE_MP_NONE;
533
534 if (rmem[4] == 0x04 && rmem[5] == 0x05)
535 return WIIMOTE_MP_SINGLE;
536 else if (rmem[4] == 0x05 && rmem[5] == 0x05)
537 return WIIMOTE_MP_PASSTHROUGH_NUNCHUK;
538 else if (rmem[4] == 0x07 && rmem[5] == 0x05)
539 return WIIMOTE_MP_PASSTHROUGH_CLASSIC;
540
541 return WIIMOTE_MP_UNKNOWN;
542}
543
David Herrmann27f06942013-05-05 23:12:51 +0200544/* device module handling */
545
546static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
547 [WIIMOTE_DEV_PENDING] = (const __u8[]){
548 WIIMOD_NULL,
549 },
550 [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
551 WIIMOD_NULL,
552 },
553 [WIIMOTE_DEV_GENERIC] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200554 WIIMOD_KEYS,
555 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200556 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200557 WIIMOD_LED1,
558 WIIMOD_LED2,
559 WIIMOD_LED3,
560 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200561 WIIMOD_ACCEL,
David Herrmann3b5f03c2013-05-05 23:12:56 +0200562 WIIMOD_IR,
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 Herrmann3b5f03c2013-05-05 23:12:56 +0200574 WIIMOD_IR,
David Herrmann27f06942013-05-05 23:12:51 +0200575 WIIMOD_NULL,
576 },
577 [WIIMOTE_DEV_GEN20] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200578 WIIMOD_KEYS,
579 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200580 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200581 WIIMOD_LED1,
582 WIIMOD_LED2,
583 WIIMOD_LED3,
584 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200585 WIIMOD_ACCEL,
David Herrmann3b5f03c2013-05-05 23:12:56 +0200586 WIIMOD_IR,
David Herrmann27f06942013-05-05 23:12:51 +0200587 WIIMOD_NULL,
588 },
David Herrmannf1d4bed2013-05-05 23:12:58 +0200589 [WIIMOTE_DEV_BALANCE_BOARD] = (const __u8[]) {
590 WIIMOD_BATTERY,
591 WIIMOD_LED1,
592 WIIMOD_NULL,
593 },
David Herrmann27f06942013-05-05 23:12:51 +0200594};
595
596static void wiimote_modules_load(struct wiimote_data *wdata,
597 unsigned int devtype)
598{
599 bool need_input = false;
600 const __u8 *mods, *iter;
601 const struct wiimod_ops *ops;
602 int ret;
603
604 mods = wiimote_devtype_mods[devtype];
605
606 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
607 if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
608 need_input = true;
609 break;
610 }
611 }
612
613 if (need_input) {
614 wdata->input = input_allocate_device();
615 if (!wdata->input)
616 return;
617
618 input_set_drvdata(wdata->input, wdata);
619 wdata->input->dev.parent = &wdata->hdev->dev;
620 wdata->input->id.bustype = wdata->hdev->bus;
621 wdata->input->id.vendor = wdata->hdev->vendor;
622 wdata->input->id.product = wdata->hdev->product;
623 wdata->input->id.version = wdata->hdev->version;
624 wdata->input->name = WIIMOTE_NAME;
625 }
626
627 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
628 ops = wiimod_table[*iter];
629 if (!ops->probe)
630 continue;
631
632 ret = ops->probe(ops, wdata);
633 if (ret)
634 goto error;
635 }
636
637 if (wdata->input) {
638 ret = input_register_device(wdata->input);
639 if (ret)
640 goto error;
641 }
642
643 spin_lock_irq(&wdata->state.lock);
644 wdata->state.devtype = devtype;
645 spin_unlock_irq(&wdata->state.lock);
646 return;
647
648error:
649 for ( ; iter-- != mods; ) {
650 ops = wiimod_table[*iter];
651 if (ops->remove)
652 ops->remove(ops, wdata);
653 }
654
655 if (wdata->input) {
656 input_free_device(wdata->input);
657 wdata->input = NULL;
658 }
659}
660
661static void wiimote_modules_unload(struct wiimote_data *wdata)
662{
663 const __u8 *mods, *iter;
664 const struct wiimod_ops *ops;
665 unsigned long flags;
666
667 mods = wiimote_devtype_mods[wdata->state.devtype];
668
669 spin_lock_irqsave(&wdata->state.lock, flags);
670 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
671 spin_unlock_irqrestore(&wdata->state.lock, flags);
672
673 /* find end of list */
674 for (iter = mods; *iter != WIIMOD_NULL; ++iter)
675 /* empty */ ;
676
677 if (wdata->input) {
678 input_get_device(wdata->input);
679 input_unregister_device(wdata->input);
680 }
681
682 for ( ; iter-- != mods; ) {
683 ops = wiimod_table[*iter];
684 if (ops->remove)
685 ops->remove(ops, wdata);
686 }
687
688 if (wdata->input) {
689 input_put_device(wdata->input);
690 wdata->input = NULL;
691 }
692}
693
David Herrmann4148b6b2013-05-05 23:12:57 +0200694/* device extension handling */
695
696static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
697{
698 unsigned long flags;
699 const struct wiimod_ops *ops;
700 int ret;
701
702 ops = wiimod_ext_table[ext];
703
704 if (ops->probe) {
705 ret = ops->probe(ops, wdata);
706 if (ret)
707 ext = WIIMOTE_EXT_UNKNOWN;
708 }
709
710 spin_lock_irqsave(&wdata->state.lock, flags);
711 wdata->state.exttype = ext;
712 spin_unlock_irqrestore(&wdata->state.lock, flags);
713}
714
715static void wiimote_ext_unload(struct wiimote_data *wdata)
716{
717 unsigned long flags;
718 const struct wiimod_ops *ops;
719
720 ops = wiimod_ext_table[wdata->state.exttype];
721
722 spin_lock_irqsave(&wdata->state.lock, flags);
723 wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
724 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
725 spin_unlock_irqrestore(&wdata->state.lock, flags);
726
727 if (ops->remove)
728 ops->remove(ops, wdata);
729}
730
731static void wiimote_mp_load(struct wiimote_data *wdata)
732{
733 unsigned long flags;
734 const struct wiimod_ops *ops;
735 int ret;
736 __u8 mode = 2;
737
738 ops = &wiimod_mp;
739 if (ops->probe) {
740 ret = ops->probe(ops, wdata);
741 if (ret)
742 mode = 1;
743 }
744
745 spin_lock_irqsave(&wdata->state.lock, flags);
746 wdata->state.mp = mode;
747 spin_unlock_irqrestore(&wdata->state.lock, flags);
748}
749
750static void wiimote_mp_unload(struct wiimote_data *wdata)
751{
752 unsigned long flags;
753 const struct wiimod_ops *ops;
754
755 if (wdata->state.mp < 2)
756 return;
757
758 ops = &wiimod_mp;
759
760 spin_lock_irqsave(&wdata->state.lock, flags);
761 wdata->state.mp = 0;
762 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
763 spin_unlock_irqrestore(&wdata->state.lock, flags);
764
765 if (ops->remove)
766 ops->remove(ops, wdata);
767}
768
David Herrmannc57ff762013-05-05 23:12:48 +0200769/* device (re-)initialization and detection */
770
771static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
772 [WIIMOTE_DEV_PENDING] = "Pending",
773 [WIIMOTE_DEV_UNKNOWN] = "Unknown",
774 [WIIMOTE_DEV_GENERIC] = "Generic",
775 [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
776 [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
David Herrmannf1d4bed2013-05-05 23:12:58 +0200777 [WIIMOTE_DEV_BALANCE_BOARD] = "Nintendo Wii Balance Board",
David Herrmannc57ff762013-05-05 23:12:48 +0200778};
779
780/* Try to guess the device type based on all collected information. We
781 * first try to detect by static extension types, then VID/PID and the
782 * device name. If we cannot detect the device, we use
783 * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
784static void wiimote_init_set_type(struct wiimote_data *wdata,
785 __u8 exttype)
786{
787 __u8 devtype = WIIMOTE_DEV_GENERIC;
788 __u16 vendor, product;
789 const char *name;
790
791 vendor = wdata->hdev->vendor;
792 product = wdata->hdev->product;
793 name = wdata->hdev->name;
794
David Herrmannf1d4bed2013-05-05 23:12:58 +0200795 if (exttype == WIIMOTE_EXT_BALANCE_BOARD) {
796 devtype = WIIMOTE_DEV_BALANCE_BOARD;
797 goto done;
798 }
799
David Herrmannc57ff762013-05-05 23:12:48 +0200800 if (!strcmp(name, "Nintendo RVL-CNT-01")) {
801 devtype = WIIMOTE_DEV_GEN10;
802 goto done;
803 } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
804 devtype = WIIMOTE_DEV_GEN20;
805 goto done;
David Herrmannf1d4bed2013-05-05 23:12:58 +0200806 } else if (!strcmp(name, "Nintendo RVL-WBC-01")) {
807 devtype = WIIMOTE_DEV_BALANCE_BOARD;
808 goto done;
David Herrmannc57ff762013-05-05 23:12:48 +0200809 }
810
811 if (vendor == USB_VENDOR_ID_NINTENDO) {
812 if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
813 devtype = WIIMOTE_DEV_GEN10;
814 goto done;
815 } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
816 devtype = WIIMOTE_DEV_GEN20;
817 goto done;
818 }
819 }
820
821done:
822 if (devtype == WIIMOTE_DEV_GENERIC)
823 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
824 name, vendor, product, exttype);
825 else
826 hid_info(wdata->hdev, "detected device: %s\n",
827 wiimote_devtype_names[devtype]);
828
David Herrmann27f06942013-05-05 23:12:51 +0200829 wiimote_modules_load(wdata, devtype);
David Herrmannc57ff762013-05-05 23:12:48 +0200830}
831
832static void wiimote_init_detect(struct wiimote_data *wdata)
833{
David Herrmann4148b6b2013-05-05 23:12:57 +0200834 __u8 exttype = WIIMOTE_EXT_NONE, extdata[6];
David Herrmannc57ff762013-05-05 23:12:48 +0200835 bool ext;
836 int ret;
837
838 wiimote_cmd_acquire_noint(wdata);
839
840 spin_lock_irq(&wdata->state.lock);
David Herrmann27f06942013-05-05 23:12:51 +0200841 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
David Herrmannc57ff762013-05-05 23:12:48 +0200842 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
843 wiiproto_req_status(wdata);
844 spin_unlock_irq(&wdata->state.lock);
845
846 ret = wiimote_cmd_wait_noint(wdata);
847 if (ret)
848 goto out_release;
849
850 spin_lock_irq(&wdata->state.lock);
851 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
852 spin_unlock_irq(&wdata->state.lock);
853
854 if (!ext)
855 goto out_release;
856
857 wiimote_cmd_init_ext(wdata);
David Herrmann4148b6b2013-05-05 23:12:57 +0200858 exttype = wiimote_cmd_read_ext(wdata, extdata);
David Herrmannc57ff762013-05-05 23:12:48 +0200859
860out_release:
861 wiimote_cmd_release(wdata);
862 wiimote_init_set_type(wdata, exttype);
David Herrmann4148b6b2013-05-05 23:12:57 +0200863 /* schedule MP timer */
864 mod_timer(&wdata->timer, jiffies + HZ * 4);
865}
866
867/*
868 * MP hotplug events are not generated by the wiimote. Therefore, we need
869 * polling to detect it. We use a 4s interval for polling MP registers. This
870 * seems reasonable considering applications can trigger it manually via
871 * sysfs requests.
872 */
873static void wiimote_init_poll_mp(struct wiimote_data *wdata)
874{
875 bool mp;
876 __u8 mpdata[6];
877
878 wiimote_cmd_acquire_noint(wdata);
879 wiimote_cmd_init_mp(wdata);
880 mp = wiimote_cmd_read_mp(wdata, mpdata);
881 wiimote_cmd_release(wdata);
882
883 /* load/unload MP module if it changed */
884 if (mp) {
885 if (!wdata->state.mp) {
886 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
887 wiimote_mp_load(wdata);
888 }
889 } else if (wdata->state.mp) {
890 wiimote_mp_unload(wdata);
891 }
892
893 mod_timer(&wdata->timer, jiffies + HZ * 4);
894}
895
896/*
897 * Check whether the wiimote is in the expected state. The extension registers
898 * may change during hotplug and initialization so we might get hotplug events
899 * that we caused by remapping some memory.
900 * We use some heuristics here to check known states. If the wiimote is in the
901 * expected state, we can ignore the hotplug event.
902 *
903 * Returns "true" if the device is in expected state, "false" if we should
904 * redo hotplug handling and extension initialization.
905 */
906static bool wiimote_init_check(struct wiimote_data *wdata)
907{
908 __u32 flags;
909 __u8 type, data[6];
910 bool ret, poll_mp;
911
912 spin_lock_irq(&wdata->state.lock);
913 flags = wdata->state.flags;
914 spin_unlock_irq(&wdata->state.lock);
915
916 wiimote_cmd_acquire_noint(wdata);
917
918 /* If MP is used and active, but the extension is not, we expect:
919 * read_mp_mapped() == WIIMOTE_MP_SINGLE
920 * state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE
921 * We do not check EXT_PLUGGED because it might change during
922 * initialization of MP without extensions.
923 * - If MP is unplugged/replugged, read_mp_mapped() fails
924 * - If EXT is plugged, MP_PLUGGED will get set */
925 if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
926 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
927 type = wiimote_cmd_read_mp_mapped(wdata);
928 ret = type == WIIMOTE_MP_SINGLE;
929
930 spin_lock_irq(&wdata->state.lock);
931 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
932 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
933 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
934 spin_unlock_irq(&wdata->state.lock);
935
936 if (!ret)
937 hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
938
939 /* while MP is mapped, we get EXT_PLUGGED events */
940 poll_mp = false;
941
942 goto out_release;
943 }
944
945 /* If MP is unused, but the extension port is used, we expect:
946 * read_ext == state.exttype
947 * state.flags == !MP_ACTIVE && EXT_ACTIVE
948 * - If MP is plugged/unplugged, our timer detects it
949 * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */
950 if (!(flags & WIIPROTO_FLAG_MP_USED) &&
951 wdata->state.exttype != WIIMOTE_EXT_NONE) {
952 type = wiimote_cmd_read_ext(wdata, data);
953 ret = type == wdata->state.exttype;
954
955 spin_lock_irq(&wdata->state.lock);
956 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
957 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
958 spin_unlock_irq(&wdata->state.lock);
959
960 if (!ret)
961 hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
962
963 /* poll MP for hotplug events */
964 poll_mp = true;
965
966 goto out_release;
967 }
968
969 /* If neither MP nor an extension are used, we expect:
970 * read_ext() == WIIMOTE_EXT_NONE
971 * state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED
972 * No need to perform any action in this case as everything is
973 * disabled already.
974 * - If MP is plugged/unplugged, our timer detects it
975 * - If EXT is plugged, EXT_PLUGGED will be set */
976 if (!(flags & WIIPROTO_FLAG_MP_USED) &&
977 wdata->state.exttype == WIIMOTE_EXT_NONE) {
978 type = wiimote_cmd_read_ext(wdata, data);
979 ret = type == wdata->state.exttype;
980
981 spin_lock_irq(&wdata->state.lock);
982 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
983 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
984 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
985 spin_unlock_irq(&wdata->state.lock);
986
987 if (!ret)
988 hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
989
990 /* poll MP for hotplug events */
991 poll_mp = true;
992
993 goto out_release;
994 }
995
996 /* The trickiest part is if both EXT and MP are active. We cannot read
997 * the EXT ID, anymore, because MP is mapped over it. However, we use
998 * a handy trick here:
999 * - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent
1000 * MP_PLUGGED might be re-sent again before we are scheduled, but
1001 * EXT_ACTIVE will stay unset.
1002 * So it is enough to check for mp_mapped() and MP_ACTIVE and
1003 * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */
1004 if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
1005 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
1006 type = wiimote_cmd_read_mp_mapped(wdata);
1007 ret = type != WIIMOTE_MP_NONE;
1008 ret = ret && type != WIIMOTE_MP_UNKNOWN;
1009 ret = ret && type != WIIMOTE_MP_SINGLE;
1010
1011 spin_lock_irq(&wdata->state.lock);
1012 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1013 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1014 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1015 spin_unlock_irq(&wdata->state.lock);
1016
1017 if (!ret)
1018 hid_dbg(wdata->hdev, "state left: EXT && MP\n");
1019
1020 /* while MP is mapped, we get EXT_PLUGGED events */
1021 poll_mp = false;
1022
1023 goto out_release;
1024 }
1025
1026 /* unknown state */
1027 ret = false;
1028
1029out_release:
1030 wiimote_cmd_release(wdata);
1031
1032 /* only poll for MP if requested and if state didn't change */
1033 if (ret && poll_mp)
1034 wiimote_init_poll_mp(wdata);
1035
1036 return ret;
1037}
1038
1039static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = {
1040 [WIIMOTE_EXT_NONE] = "None",
1041 [WIIMOTE_EXT_UNKNOWN] = "Unknown",
David Herrmannb6ee67b2013-05-05 23:12:59 +02001042 [WIIMOTE_EXT_NUNCHUK] = "Nintendo Wii Nunchuk",
David Herrmannf1d4bed2013-05-05 23:12:58 +02001043 [WIIMOTE_EXT_BALANCE_BOARD] = "Nintendo Wii Balance Board",
David Herrmann4148b6b2013-05-05 23:12:57 +02001044};
1045
1046/*
1047 * Handle hotplug events
1048 * If we receive an hotplug event and the device-check failed, we deinitialize
1049 * the extension ports, re-read all extension IDs and set the device into
1050 * the desired state. This involves mapping MP into the main extension
1051 * registers, setting up extension passthrough modes and initializing the
1052 * requested extensions.
1053 */
1054static void wiimote_init_hotplug(struct wiimote_data *wdata)
1055{
1056 __u8 exttype, extdata[6], mpdata[6];
1057 __u32 flags;
1058 bool mp;
1059
1060 hid_dbg(wdata->hdev, "detect extensions..\n");
1061
1062 wiimote_cmd_acquire_noint(wdata);
1063
1064 spin_lock_irq(&wdata->state.lock);
1065
1066 /* get state snapshot that we will then work on */
1067 flags = wdata->state.flags;
1068
1069 /* disable event forwarding temporarily */
1070 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1071 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1072
1073 spin_unlock_irq(&wdata->state.lock);
1074
1075 /* init extension and MP (deactivates current extension or MP) */
1076 wiimote_cmd_init_ext(wdata);
1077 wiimote_cmd_init_mp(wdata);
1078 mp = wiimote_cmd_read_mp(wdata, mpdata);
1079 exttype = wiimote_cmd_read_ext(wdata, extdata);
1080
1081 wiimote_cmd_release(wdata);
1082
1083 /* load/unload extension module if it changed */
1084 if (exttype != wdata->state.exttype) {
1085 /* unload previous extension */
1086 wiimote_ext_unload(wdata);
1087
1088 if (exttype == WIIMOTE_EXT_UNKNOWN) {
1089 hid_info(wdata->hdev, "cannot detect extension; %02x:%02x %02x:%02x %02x:%02x\n",
1090 extdata[0], extdata[1], extdata[2],
1091 extdata[3], extdata[4], extdata[5]);
1092 } else if (exttype == WIIMOTE_EXT_NONE) {
1093 spin_lock_irq(&wdata->state.lock);
1094 wdata->state.exttype = WIIMOTE_EXT_NONE;
1095 spin_unlock_irq(&wdata->state.lock);
1096 } else {
1097 hid_info(wdata->hdev, "detected extension: %s\n",
1098 wiimote_exttype_names[exttype]);
1099 /* try loading new extension */
1100 wiimote_ext_load(wdata, exttype);
1101 }
1102 }
1103
1104 /* load/unload MP module if it changed */
1105 if (mp) {
1106 if (!wdata->state.mp) {
1107 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
1108 wiimote_mp_load(wdata);
1109 }
1110 } else if (wdata->state.mp) {
1111 wiimote_mp_unload(wdata);
1112 }
1113
1114 /* if MP is not used, do not map or activate it */
1115 if (!(flags & WIIPROTO_FLAG_MP_USED))
1116 mp = false;
1117
1118 /* map MP into main extension registers if used */
1119 if (mp) {
1120 wiimote_cmd_acquire_noint(wdata);
1121 wiimote_cmd_map_mp(wdata, exttype);
1122 wiimote_cmd_release(wdata);
1123
1124 /* delete MP hotplug timer */
1125 del_timer_sync(&wdata->timer);
1126 } else {
1127 /* reschedule MP hotplug timer */
1128 mod_timer(&wdata->timer, jiffies + HZ * 4);
1129 }
1130
1131 spin_lock_irq(&wdata->state.lock);
1132
1133 /* enable data forwarding again and set expected hotplug state */
1134 if (mp) {
1135 wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
1136 if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
1137 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1138 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1139 } else {
1140 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1141 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1142 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1143 }
1144 } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
1145 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1146 }
1147
1148 /* request status report for hotplug state updates */
1149 wiiproto_req_status(wdata);
1150
1151 spin_unlock_irq(&wdata->state.lock);
1152
1153 hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
1154 wdata->state.mp, wdata->state.exttype);
David Herrmannc57ff762013-05-05 23:12:48 +02001155}
1156
1157static void wiimote_init_worker(struct work_struct *work)
1158{
1159 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
1160 init_worker);
1161
1162 if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
1163 wiimote_init_detect(wdata);
David Herrmann4148b6b2013-05-05 23:12:57 +02001164 if (!wiimote_init_check(wdata))
1165 wiimote_init_hotplug(wdata);
1166}
1167
1168void __wiimote_schedule(struct wiimote_data *wdata)
1169{
1170 if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
1171 schedule_work(&wdata->init_worker);
1172}
1173
1174static void wiimote_schedule(struct wiimote_data *wdata)
1175{
1176 unsigned long flags;
1177
1178 spin_lock_irqsave(&wdata->state.lock, flags);
1179 __wiimote_schedule(wdata);
1180 spin_unlock_irqrestore(&wdata->state.lock, flags);
1181}
1182
1183static void wiimote_init_timeout(unsigned long arg)
1184{
1185 struct wiimote_data *wdata = (void*)arg;
1186
1187 wiimote_schedule(wdata);
David Herrmannc57ff762013-05-05 23:12:48 +02001188}
1189
1190/* protocol handlers */
1191
David Herrmann1abb9ad2011-07-05 13:45:16 +02001192static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
1193{
David Herrmann20cef812013-05-05 23:12:52 +02001194 const __u8 *iter, *mods;
1195 const struct wiimod_ops *ops;
1196
David Herrmann4148b6b2013-05-05 23:12:57 +02001197 ops = wiimod_ext_table[wdata->state.exttype];
1198 if (ops->in_keys) {
1199 ops->in_keys(wdata, payload);
1200 return;
1201 }
1202
David Herrmann20cef812013-05-05 23:12:52 +02001203 mods = wiimote_devtype_mods[wdata->state.devtype];
1204 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1205 ops = wiimod_table[*iter];
1206 if (ops->in_keys) {
1207 ops->in_keys(wdata, payload);
1208 break;
1209 }
1210 }
David Herrmann1abb9ad2011-07-05 13:45:16 +02001211}
1212
David Herrmannefcf9182011-09-06 13:50:29 +02001213static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
1214{
David Herrmann0ea16752013-05-05 23:12:55 +02001215 const __u8 *iter, *mods;
1216 const struct wiimod_ops *ops;
David Herrmannefcf9182011-09-06 13:50:29 +02001217
David Herrmann4148b6b2013-05-05 23:12:57 +02001218 ops = wiimod_ext_table[wdata->state.exttype];
1219 if (ops->in_accel) {
1220 ops->in_accel(wdata, payload);
1221 return;
1222 }
1223
David Herrmann0ea16752013-05-05 23:12:55 +02001224 mods = wiimote_devtype_mods[wdata->state.devtype];
1225 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1226 ops = wiimod_table[*iter];
1227 if (ops->in_accel) {
1228 ops->in_accel(wdata, payload);
1229 break;
1230 }
1231 }
David Herrmannefcf9182011-09-06 13:50:29 +02001232}
1233
David Herrmann4148b6b2013-05-05 23:12:57 +02001234static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len)
1235{
1236 if (!ops->in_ext)
1237 return false;
1238 if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8)
1239 return false;
1240 if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16)
1241 return false;
1242
1243 return true;
1244}
1245
1246static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
1247 size_t len)
1248{
1249 const __u8 *iter, *mods;
1250 const struct wiimod_ops *ops;
1251 bool is_mp;
1252
1253 if (len < 6)
1254 return;
1255
1256 /* if MP is active, track MP slot hotplugging */
1257 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1258 /* this bit is set for invalid events (eg. during hotplug) */
1259 if (payload[5] & 0x01)
1260 return;
1261
1262 if (payload[4] & 0x01) {
1263 if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
1264 hid_dbg(wdata->hdev, "MP hotplug: 1\n");
1265 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1266 __wiimote_schedule(wdata);
1267 }
1268 } else {
1269 if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
1270 hid_dbg(wdata->hdev, "MP hotplug: 0\n");
1271 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1272 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1273 __wiimote_schedule(wdata);
1274 }
1275 }
1276
1277 /* detect MP data that is sent interleaved with EXT data */
1278 is_mp = payload[5] & 0x02;
1279 } else {
1280 is_mp = false;
1281 }
1282
1283 /* ignore EXT events if no extension is active */
1284 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
1285 return;
1286
1287 /* try forwarding to extension handler, first */
1288 ops = wiimod_ext_table[wdata->state.exttype];
1289 if (is_mp && ops->in_mp) {
1290 ops->in_mp(wdata, payload);
1291 return;
1292 } else if (!is_mp && valid_ext_handler(ops, len)) {
1293 ops->in_ext(wdata, payload);
1294 return;
1295 }
1296
1297 /* try forwarding to MP handler */
1298 ops = &wiimod_mp;
1299 if (is_mp && ops->in_mp) {
1300 ops->in_mp(wdata, payload);
1301 return;
1302 } else if (!is_mp && valid_ext_handler(ops, len)) {
1303 ops->in_ext(wdata, payload);
1304 return;
1305 }
1306
1307 /* try forwarding to loaded modules */
1308 mods = wiimote_devtype_mods[wdata->state.devtype];
1309 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1310 ops = wiimod_table[*iter];
1311 if (is_mp && ops->in_mp) {
1312 ops->in_mp(wdata, payload);
1313 return;
1314 } else if (!is_mp && valid_ext_handler(ops, len)) {
1315 ops->in_ext(wdata, payload);
1316 return;
1317 }
1318 }
1319}
1320
David Herrmann3b5f03c2013-05-05 23:12:56 +02001321#define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1322#define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1323#define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1324#define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
David Herrmanneac39e72011-09-06 13:50:31 +02001325
David Herrmann3b5f03c2013-05-05 23:12:56 +02001326static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
1327 bool packed, unsigned int id)
David Herrmanneac39e72011-09-06 13:50:31 +02001328{
David Herrmann3b5f03c2013-05-05 23:12:56 +02001329 const __u8 *iter, *mods;
1330 const struct wiimod_ops *ops;
David Herrmanneac39e72011-09-06 13:50:31 +02001331
David Herrmann4148b6b2013-05-05 23:12:57 +02001332 ops = wiimod_ext_table[wdata->state.exttype];
1333 if (ops->in_ir) {
1334 ops->in_ir(wdata, payload, packed, id);
1335 return;
1336 }
1337
David Herrmann3b5f03c2013-05-05 23:12:56 +02001338 mods = wiimote_devtype_mods[wdata->state.devtype];
1339 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1340 ops = wiimod_table[*iter];
1341 if (ops->in_ir) {
1342 ops->in_ir(wdata, payload, packed, id);
1343 break;
1344 }
David Herrmanneac39e72011-09-06 13:50:31 +02001345 }
David Herrmanneac39e72011-09-06 13:50:31 +02001346}
David Herrmannefcf9182011-09-06 13:50:29 +02001347
David Herrmann2d44e3d2013-04-02 19:58:36 +02001348/* reduced status report with "BB BB" key data only */
1349static void handler_status_K(struct wiimote_data *wdata,
1350 const __u8 *payload)
David Herrmannc87019e2011-08-17 11:43:24 +02001351{
1352 handler_keys(wdata, payload);
1353
1354 /* on status reports the drm is reset so we need to resend the drm */
1355 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001356}
1357
1358/* extended status report with "BB BB LF 00 00 VV" data */
1359static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1360{
1361 handler_status_K(wdata, payload);
David Herrmanne3979a92011-09-06 13:50:38 +02001362
David Herrmannc57ff762013-05-05 23:12:48 +02001363 /* update extension status */
1364 if (payload[2] & 0x02) {
David Herrmann4148b6b2013-05-05 23:12:57 +02001365 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
1366 hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
1367 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1368 __wiimote_schedule(wdata);
1369 }
David Herrmannc57ff762013-05-05 23:12:48 +02001370 } else {
David Herrmann4148b6b2013-05-05 23:12:57 +02001371 if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
1372 hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
1373 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1374 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1375 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1376 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1377 __wiimote_schedule(wdata);
1378 }
David Herrmannc57ff762013-05-05 23:12:48 +02001379 }
David Herrmanncb992212011-11-17 14:12:01 +01001380
David Herrmann6b80bb92013-05-05 23:12:49 +02001381 wdata->state.cmd_battery = payload[5];
1382 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
David Herrmanne3979a92011-09-06 13:50:38 +02001383 wiimote_cmd_complete(wdata);
David Herrmannc87019e2011-08-17 11:43:24 +02001384}
1385
David Herrmann2d44e3d2013-04-02 19:58:36 +02001386/* reduced generic report with "BB BB" key data only */
1387static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1388{
1389 handler_keys(wdata, payload);
1390}
1391
David Herrmannbe1ecd62011-09-06 13:50:33 +02001392static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1393{
David Herrmannfad8c0e2011-11-17 14:12:00 +01001394 __u16 offset = payload[3] << 8 | payload[4];
1395 __u8 size = (payload[2] >> 4) + 1;
1396 __u8 err = payload[2] & 0x0f;
1397
David Herrmannbe1ecd62011-09-06 13:50:33 +02001398 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +01001399
1400 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1401 if (err)
1402 size = 0;
1403 else if (size > wdata->state.cmd_read_size)
1404 size = wdata->state.cmd_read_size;
1405
1406 wdata->state.cmd_read_size = size;
1407 if (wdata->state.cmd_read_buf)
1408 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1409 wiimote_cmd_complete(wdata);
1410 }
David Herrmannbe1ecd62011-09-06 13:50:33 +02001411}
1412
David Herrmannc87019e2011-08-17 11:43:24 +02001413static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1414{
1415 __u8 err = payload[3];
1416 __u8 cmd = payload[2];
1417
1418 handler_keys(wdata, payload);
1419
David Herrmann33e84012011-09-06 13:50:35 +02001420 if (wiimote_cmd_pending(wdata, cmd, 0)) {
1421 wdata->state.cmd_err = err;
1422 wiimote_cmd_complete(wdata);
1423 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +02001424 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
1425 cmd);
David Herrmann33e84012011-09-06 13:50:35 +02001426 }
David Herrmannc87019e2011-08-17 11:43:24 +02001427}
1428
David Herrmannefcf9182011-09-06 13:50:29 +02001429static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1430{
1431 handler_keys(wdata, payload);
1432 handler_accel(wdata, payload);
1433}
1434
David Herrmann7336b9f2011-09-06 13:50:32 +02001435static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1436{
1437 handler_keys(wdata, payload);
David Herrmann4148b6b2013-05-05 23:12:57 +02001438 handler_ext(wdata, &payload[2], 8);
David Herrmann7336b9f2011-09-06 13:50:32 +02001439}
1440
David Herrmannefcf9182011-09-06 13:50:29 +02001441static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1442{
1443 handler_keys(wdata, payload);
1444 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +02001445 ir_to_input0(wdata, &payload[5], false);
1446 ir_to_input1(wdata, &payload[8], false);
1447 ir_to_input2(wdata, &payload[11], false);
1448 ir_to_input3(wdata, &payload[14], false);
David Herrmanneac39e72011-09-06 13:50:31 +02001449}
1450
David Herrmann7336b9f2011-09-06 13:50:32 +02001451static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1452{
1453 handler_keys(wdata, payload);
David Herrmann4148b6b2013-05-05 23:12:57 +02001454 handler_ext(wdata, &payload[2], 19);
David Herrmann7336b9f2011-09-06 13:50:32 +02001455}
1456
David Herrmanneac39e72011-09-06 13:50:31 +02001457static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1458{
1459 handler_keys(wdata, payload);
1460 ir_to_input0(wdata, &payload[2], false);
1461 ir_to_input1(wdata, &payload[4], true);
1462 ir_to_input2(wdata, &payload[7], false);
1463 ir_to_input3(wdata, &payload[9], true);
David Herrmann4148b6b2013-05-05 23:12:57 +02001464 handler_ext(wdata, &payload[12], 9);
David Herrmannefcf9182011-09-06 13:50:29 +02001465}
1466
1467static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1468{
1469 handler_keys(wdata, payload);
1470 handler_accel(wdata, payload);
David Herrmann4148b6b2013-05-05 23:12:57 +02001471 handler_ext(wdata, &payload[5], 16);
David Herrmannefcf9182011-09-06 13:50:29 +02001472}
1473
1474static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1475{
1476 handler_keys(wdata, payload);
1477 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +02001478 ir_to_input0(wdata, &payload[5], false);
1479 ir_to_input1(wdata, &payload[7], true);
1480 ir_to_input2(wdata, &payload[10], false);
1481 ir_to_input3(wdata, &payload[12], true);
David Herrmann4148b6b2013-05-05 23:12:57 +02001482 handler_ext(wdata, &payload[15], 6);
David Herrmannefcf9182011-09-06 13:50:29 +02001483}
1484
David Herrmann7336b9f2011-09-06 13:50:32 +02001485static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1486{
David Herrmann4148b6b2013-05-05 23:12:57 +02001487 handler_ext(wdata, payload, 21);
David Herrmann7336b9f2011-09-06 13:50:32 +02001488}
1489
David Herrmannefcf9182011-09-06 13:50:29 +02001490static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1491{
1492 handler_keys(wdata, payload);
1493
1494 wdata->state.accel_split[0] = payload[2];
1495 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1496 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +02001497
1498 ir_to_input0(wdata, &payload[3], false);
1499 ir_to_input1(wdata, &payload[12], false);
David Herrmannefcf9182011-09-06 13:50:29 +02001500}
1501
1502static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1503{
1504 __u8 buf[5];
1505
1506 handler_keys(wdata, payload);
1507
1508 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1509 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1510
1511 buf[0] = 0;
1512 buf[1] = 0;
1513 buf[2] = wdata->state.accel_split[0];
1514 buf[3] = payload[2];
1515 buf[4] = wdata->state.accel_split[1];
1516 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +02001517
1518 ir_to_input2(wdata, &payload[3], false);
1519 ir_to_input3(wdata, &payload[12], false);
David Herrmannefcf9182011-09-06 13:50:29 +02001520}
1521
David Herrmanna4d19192011-07-05 13:45:15 +02001522struct wiiproto_handler {
1523 __u8 id;
1524 size_t size;
1525 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1526};
1527
1528static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +02001529 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001530 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
David Herrmannbe1ecd62011-09-06 13:50:33 +02001531 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001532 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
David Herrmannc87019e2011-08-17 11:43:24 +02001533 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001534 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
David Herrmann1abb9ad2011-07-05 13:45:16 +02001535 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +02001536 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001537 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001538 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001539 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001540 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001541 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001542 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001543 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001544 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001545 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
David Herrmanneac39e72011-09-06 13:50:31 +02001546 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001547 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001548 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001549 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001550 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +02001551 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1552 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +02001553 { .id = 0 }
1554};
1555
David Herrmann02fb72a2011-07-05 13:45:09 +02001556static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1557 u8 *raw_data, int size)
1558{
David Herrmann4d36e972011-07-05 13:45:12 +02001559 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +02001560 struct wiiproto_handler *h;
1561 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001562 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +02001563
David Herrmann02fb72a2011-07-05 13:45:09 +02001564 if (size < 1)
1565 return -EINVAL;
1566
David Herrmann32a0d9a2011-07-05 13:45:18 +02001567 spin_lock_irqsave(&wdata->state.lock, flags);
1568
David Herrmanna4d19192011-07-05 13:45:15 +02001569 for (i = 0; handlers[i].id; ++i) {
1570 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +02001571 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +02001572 h->func(wdata, &raw_data[1]);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001573 break;
David Herrmann7336b9f2011-09-06 13:50:32 +02001574 }
David Herrmanna4d19192011-07-05 13:45:15 +02001575 }
1576
David Herrmann2d44e3d2013-04-02 19:58:36 +02001577 if (!handlers[i].id)
David Herrmann7336b9f2011-09-06 13:50:32 +02001578 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1579 size);
1580
David Herrmann32a0d9a2011-07-05 13:45:18 +02001581 spin_unlock_irqrestore(&wdata->state.lock, flags);
1582
David Herrmann02fb72a2011-07-05 13:45:09 +02001583 return 0;
1584}
1585
David Herrmanne894d0e2011-07-05 13:45:10 +02001586static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1587{
1588 struct wiimote_data *wdata;
1589
1590 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1591 if (!wdata)
1592 return NULL;
1593
1594 wdata->hdev = hdev;
1595 hid_set_drvdata(hdev, wdata);
1596
David Herrmann13938532013-05-05 23:12:46 +02001597 spin_lock_init(&wdata->queue.lock);
1598 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
David Herrmann23c063c2011-07-05 13:45:14 +02001599
David Herrmann32a0d9a2011-07-05 13:45:18 +02001600 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001601 init_completion(&wdata->state.ready);
1602 mutex_init(&wdata->state.sync);
David Herrmann43d782a2011-11-17 14:12:12 +01001603 wdata->state.drm = WIIPROTO_REQ_DRM_K;
David Herrmann6b80bb92013-05-05 23:12:49 +02001604 wdata->state.cmd_battery = 0xff;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001605
David Herrmannc57ff762013-05-05 23:12:48 +02001606 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
David Herrmann4148b6b2013-05-05 23:12:57 +02001607 setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata);
David Herrmannc57ff762013-05-05 23:12:48 +02001608
David Herrmanne894d0e2011-07-05 13:45:10 +02001609 return wdata;
1610}
1611
1612static void wiimote_destroy(struct wiimote_data *wdata)
1613{
David Herrmann4148b6b2013-05-05 23:12:57 +02001614 unsigned long flags;
1615
David Herrmann43e5e7c2011-11-17 14:12:10 +01001616 wiidebug_deinit(wdata);
David Herrmann4148b6b2013-05-05 23:12:57 +02001617
1618 /* prevent init_worker from being scheduled again */
1619 spin_lock_irqsave(&wdata->state.lock, flags);
1620 wdata->state.flags |= WIIPROTO_FLAG_EXITING;
1621 spin_unlock_irqrestore(&wdata->state.lock, flags);
David Herrmann3989ef62011-08-17 11:43:20 +02001622
David Herrmann20cef812013-05-05 23:12:52 +02001623 cancel_work_sync(&wdata->init_worker);
David Herrmann4148b6b2013-05-05 23:12:57 +02001624 del_timer_sync(&wdata->timer);
1625
1626 wiimote_mp_unload(wdata);
1627 wiimote_ext_unload(wdata);
David Herrmann27f06942013-05-05 23:12:51 +02001628 wiimote_modules_unload(wdata);
David Herrmann13938532013-05-05 23:12:46 +02001629 cancel_work_sync(&wdata->queue.worker);
David Herrmann5682b1a2013-05-05 23:12:47 +02001630 hid_hw_close(wdata->hdev);
David Herrmann3989ef62011-08-17 11:43:20 +02001631 hid_hw_stop(wdata->hdev);
1632
David Herrmanne894d0e2011-07-05 13:45:10 +02001633 kfree(wdata);
1634}
1635
David Herrmann02fb72a2011-07-05 13:45:09 +02001636static int wiimote_hid_probe(struct hid_device *hdev,
1637 const struct hid_device_id *id)
1638{
David Herrmanne894d0e2011-07-05 13:45:10 +02001639 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001640 int ret;
1641
David Herrmann90120d62011-11-17 14:12:14 +01001642 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1643
David Herrmanne894d0e2011-07-05 13:45:10 +02001644 wdata = wiimote_create(hdev);
1645 if (!wdata) {
1646 hid_err(hdev, "Can't alloc device\n");
1647 return -ENOMEM;
1648 }
1649
David Herrmann02fb72a2011-07-05 13:45:09 +02001650 ret = hid_parse(hdev);
1651 if (ret) {
1652 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001653 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001654 }
1655
1656 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1657 if (ret) {
1658 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001659 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001660 }
1661
David Herrmann5682b1a2013-05-05 23:12:47 +02001662 ret = hid_hw_open(hdev);
1663 if (ret) {
1664 hid_err(hdev, "cannot start hardware I/O\n");
1665 goto err_stop;
1666 }
1667
David Herrmann43e5e7c2011-11-17 14:12:10 +01001668 ret = wiidebug_init(wdata);
1669 if (ret)
1670 goto err_free;
1671
David Herrmann02fb72a2011-07-05 13:45:09 +02001672 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001673
David Herrmannc57ff762013-05-05 23:12:48 +02001674 /* schedule device detection */
David Herrmann4148b6b2013-05-05 23:12:57 +02001675 wiimote_schedule(wdata);
David Herrmannc57ff762013-05-05 23:12:48 +02001676
David Herrmann02fb72a2011-07-05 13:45:09 +02001677 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001678
David Herrmann3989ef62011-08-17 11:43:20 +02001679err_free:
1680 wiimote_destroy(wdata);
1681 return ret;
1682
David Herrmann672bc4e2011-07-05 13:45:11 +02001683err_stop:
1684 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001685err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001686 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001687 input_free_device(wdata->accel);
David Herrmann3989ef62011-08-17 11:43:20 +02001688 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001689 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001690}
1691
1692static void wiimote_hid_remove(struct hid_device *hdev)
1693{
David Herrmanne894d0e2011-07-05 13:45:10 +02001694 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1695
David Herrmann02fb72a2011-07-05 13:45:09 +02001696 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001697 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001698}
1699
1700static const struct hid_device_id wiimote_hid_devices[] = {
1701 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1702 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
David Herrmanna33042f2013-04-02 19:58:35 +02001703 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1704 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
David Herrmann02fb72a2011-07-05 13:45:09 +02001705 { }
1706};
1707MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1708
1709static struct hid_driver wiimote_hid_driver = {
1710 .name = "wiimote",
1711 .id_table = wiimote_hid_devices,
1712 .probe = wiimote_hid_probe,
1713 .remove = wiimote_hid_remove,
1714 .raw_event = wiimote_hid_event,
1715};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001716module_hid_driver(wiimote_hid_driver);
David Herrmann02fb72a2011-07-05 13:45:09 +02001717
David Herrmannfb51b442011-07-05 13:45:08 +02001718MODULE_LICENSE("GPL");
1719MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
David Herrmann92eda7e2013-05-05 23:12:45 +02001720MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");