blob: f6c4773fac568a6602713e7075b57baf68bb45a0 [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 Herrmann9d6f9ec2013-05-05 23:13:00 +0200449 if (rmem[4] == 0x01 && rmem[5] == 0x01)
450 return WIIMOTE_EXT_CLASSIC_CONTROLLER;
David Herrmannf1d4bed2013-05-05 23:12:58 +0200451 if (rmem[4] == 0x04 && rmem[5] == 0x02)
452 return WIIMOTE_EXT_BALANCE_BOARD;
453
David Herrmannc57ff762013-05-05 23:12:48 +0200454 return WIIMOTE_EXT_UNKNOWN;
455}
456
David Herrmann4148b6b2013-05-05 23:12:57 +0200457/* requires the cmd-mutex to be held */
458static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
459{
460 __u8 wmem;
461 int ret;
462
463 /* initialize MP */
464 wmem = 0x55;
465 ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
466 if (ret)
467 return ret;
468
469 /* disable default encryption */
470 wmem = 0x0;
471 ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
472 if (ret)
473 return ret;
474
475 return 0;
476}
477
478/* requires the cmd-mutex to be held */
479static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
480{
481 __u8 wmem;
482
483 /* map MP with correct pass-through mode */
484 switch (exttype) {
David Herrmann9d6f9ec2013-05-05 23:13:00 +0200485 case WIIMOTE_EXT_CLASSIC_CONTROLLER:
486 wmem = 0x07;
487 break;
David Herrmannb6ee67b2013-05-05 23:12:59 +0200488 case WIIMOTE_EXT_NUNCHUK:
489 wmem = 0x05;
490 break;
David Herrmann4148b6b2013-05-05 23:12:57 +0200491 default:
492 wmem = 0x04;
493 break;
494 }
495
496 return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
497}
498
499/* requires the cmd-mutex to be held */
500static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
501{
502 int ret;
503
504 /* read motion plus ID */
505 ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
506 if (ret != 6)
507 return false;
508
509 hid_dbg(wdata->hdev, "motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
510 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
511
512 if (rmem[5] == 0x05)
513 return true;
514
515 hid_info(wdata->hdev, "unknown motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
516 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
517
518 return false;
519}
520
521/* requires the cmd-mutex to be held */
522static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
523{
524 int ret;
525 __u8 rmem[6];
526
527 /* read motion plus ID */
528 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
529 if (ret != 6)
530 return WIIMOTE_MP_NONE;
531
532 hid_dbg(wdata->hdev, "mapped motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
533 rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
534
535 if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
536 rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
537 return WIIMOTE_MP_NONE;
538
539 if (rmem[4] == 0x04 && rmem[5] == 0x05)
540 return WIIMOTE_MP_SINGLE;
541 else if (rmem[4] == 0x05 && rmem[5] == 0x05)
542 return WIIMOTE_MP_PASSTHROUGH_NUNCHUK;
543 else if (rmem[4] == 0x07 && rmem[5] == 0x05)
544 return WIIMOTE_MP_PASSTHROUGH_CLASSIC;
545
546 return WIIMOTE_MP_UNKNOWN;
547}
548
David Herrmann27f06942013-05-05 23:12:51 +0200549/* device module handling */
550
551static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
552 [WIIMOTE_DEV_PENDING] = (const __u8[]){
553 WIIMOD_NULL,
554 },
555 [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
556 WIIMOD_NULL,
557 },
558 [WIIMOTE_DEV_GENERIC] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200559 WIIMOD_KEYS,
560 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200561 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200562 WIIMOD_LED1,
563 WIIMOD_LED2,
564 WIIMOD_LED3,
565 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200566 WIIMOD_ACCEL,
David Herrmann3b5f03c2013-05-05 23:12:56 +0200567 WIIMOD_IR,
David Herrmann27f06942013-05-05 23:12:51 +0200568 WIIMOD_NULL,
569 },
570 [WIIMOTE_DEV_GEN10] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200571 WIIMOD_KEYS,
572 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200573 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200574 WIIMOD_LED1,
575 WIIMOD_LED2,
576 WIIMOD_LED3,
577 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200578 WIIMOD_ACCEL,
David Herrmann3b5f03c2013-05-05 23:12:56 +0200579 WIIMOD_IR,
David Herrmann27f06942013-05-05 23:12:51 +0200580 WIIMOD_NULL,
581 },
582 [WIIMOTE_DEV_GEN20] = (const __u8[]){
David Herrmann20cef812013-05-05 23:12:52 +0200583 WIIMOD_KEYS,
584 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200585 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200586 WIIMOD_LED1,
587 WIIMOD_LED2,
588 WIIMOD_LED3,
589 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200590 WIIMOD_ACCEL,
David Herrmann3b5f03c2013-05-05 23:12:56 +0200591 WIIMOD_IR,
David Herrmann27f06942013-05-05 23:12:51 +0200592 WIIMOD_NULL,
593 },
David Herrmannf1d4bed2013-05-05 23:12:58 +0200594 [WIIMOTE_DEV_BALANCE_BOARD] = (const __u8[]) {
595 WIIMOD_BATTERY,
596 WIIMOD_LED1,
597 WIIMOD_NULL,
598 },
David Herrmann27f06942013-05-05 23:12:51 +0200599};
600
601static void wiimote_modules_load(struct wiimote_data *wdata,
602 unsigned int devtype)
603{
604 bool need_input = false;
605 const __u8 *mods, *iter;
606 const struct wiimod_ops *ops;
607 int ret;
608
609 mods = wiimote_devtype_mods[devtype];
610
611 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
612 if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
613 need_input = true;
614 break;
615 }
616 }
617
618 if (need_input) {
619 wdata->input = input_allocate_device();
620 if (!wdata->input)
621 return;
622
623 input_set_drvdata(wdata->input, wdata);
624 wdata->input->dev.parent = &wdata->hdev->dev;
625 wdata->input->id.bustype = wdata->hdev->bus;
626 wdata->input->id.vendor = wdata->hdev->vendor;
627 wdata->input->id.product = wdata->hdev->product;
628 wdata->input->id.version = wdata->hdev->version;
629 wdata->input->name = WIIMOTE_NAME;
630 }
631
632 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
633 ops = wiimod_table[*iter];
634 if (!ops->probe)
635 continue;
636
637 ret = ops->probe(ops, wdata);
638 if (ret)
639 goto error;
640 }
641
642 if (wdata->input) {
643 ret = input_register_device(wdata->input);
644 if (ret)
645 goto error;
646 }
647
648 spin_lock_irq(&wdata->state.lock);
649 wdata->state.devtype = devtype;
650 spin_unlock_irq(&wdata->state.lock);
651 return;
652
653error:
654 for ( ; iter-- != mods; ) {
655 ops = wiimod_table[*iter];
656 if (ops->remove)
657 ops->remove(ops, wdata);
658 }
659
660 if (wdata->input) {
661 input_free_device(wdata->input);
662 wdata->input = NULL;
663 }
664}
665
666static void wiimote_modules_unload(struct wiimote_data *wdata)
667{
668 const __u8 *mods, *iter;
669 const struct wiimod_ops *ops;
670 unsigned long flags;
671
672 mods = wiimote_devtype_mods[wdata->state.devtype];
673
674 spin_lock_irqsave(&wdata->state.lock, flags);
675 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
676 spin_unlock_irqrestore(&wdata->state.lock, flags);
677
678 /* find end of list */
679 for (iter = mods; *iter != WIIMOD_NULL; ++iter)
680 /* empty */ ;
681
682 if (wdata->input) {
683 input_get_device(wdata->input);
684 input_unregister_device(wdata->input);
685 }
686
687 for ( ; iter-- != mods; ) {
688 ops = wiimod_table[*iter];
689 if (ops->remove)
690 ops->remove(ops, wdata);
691 }
692
693 if (wdata->input) {
694 input_put_device(wdata->input);
695 wdata->input = NULL;
696 }
697}
698
David Herrmann4148b6b2013-05-05 23:12:57 +0200699/* device extension handling */
700
701static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
702{
703 unsigned long flags;
704 const struct wiimod_ops *ops;
705 int ret;
706
707 ops = wiimod_ext_table[ext];
708
709 if (ops->probe) {
710 ret = ops->probe(ops, wdata);
711 if (ret)
712 ext = WIIMOTE_EXT_UNKNOWN;
713 }
714
715 spin_lock_irqsave(&wdata->state.lock, flags);
716 wdata->state.exttype = ext;
717 spin_unlock_irqrestore(&wdata->state.lock, flags);
718}
719
720static void wiimote_ext_unload(struct wiimote_data *wdata)
721{
722 unsigned long flags;
723 const struct wiimod_ops *ops;
724
725 ops = wiimod_ext_table[wdata->state.exttype];
726
727 spin_lock_irqsave(&wdata->state.lock, flags);
728 wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
729 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
730 spin_unlock_irqrestore(&wdata->state.lock, flags);
731
732 if (ops->remove)
733 ops->remove(ops, wdata);
734}
735
736static void wiimote_mp_load(struct wiimote_data *wdata)
737{
738 unsigned long flags;
739 const struct wiimod_ops *ops;
740 int ret;
741 __u8 mode = 2;
742
743 ops = &wiimod_mp;
744 if (ops->probe) {
745 ret = ops->probe(ops, wdata);
746 if (ret)
747 mode = 1;
748 }
749
750 spin_lock_irqsave(&wdata->state.lock, flags);
751 wdata->state.mp = mode;
752 spin_unlock_irqrestore(&wdata->state.lock, flags);
753}
754
755static void wiimote_mp_unload(struct wiimote_data *wdata)
756{
757 unsigned long flags;
758 const struct wiimod_ops *ops;
759
760 if (wdata->state.mp < 2)
761 return;
762
763 ops = &wiimod_mp;
764
765 spin_lock_irqsave(&wdata->state.lock, flags);
766 wdata->state.mp = 0;
767 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
768 spin_unlock_irqrestore(&wdata->state.lock, flags);
769
770 if (ops->remove)
771 ops->remove(ops, wdata);
772}
773
David Herrmannc57ff762013-05-05 23:12:48 +0200774/* device (re-)initialization and detection */
775
776static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
777 [WIIMOTE_DEV_PENDING] = "Pending",
778 [WIIMOTE_DEV_UNKNOWN] = "Unknown",
779 [WIIMOTE_DEV_GENERIC] = "Generic",
780 [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
781 [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
David Herrmannf1d4bed2013-05-05 23:12:58 +0200782 [WIIMOTE_DEV_BALANCE_BOARD] = "Nintendo Wii Balance Board",
David Herrmannc57ff762013-05-05 23:12:48 +0200783};
784
785/* Try to guess the device type based on all collected information. We
786 * first try to detect by static extension types, then VID/PID and the
787 * device name. If we cannot detect the device, we use
788 * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
789static void wiimote_init_set_type(struct wiimote_data *wdata,
790 __u8 exttype)
791{
792 __u8 devtype = WIIMOTE_DEV_GENERIC;
793 __u16 vendor, product;
794 const char *name;
795
796 vendor = wdata->hdev->vendor;
797 product = wdata->hdev->product;
798 name = wdata->hdev->name;
799
David Herrmannf1d4bed2013-05-05 23:12:58 +0200800 if (exttype == WIIMOTE_EXT_BALANCE_BOARD) {
801 devtype = WIIMOTE_DEV_BALANCE_BOARD;
802 goto done;
803 }
804
David Herrmannc57ff762013-05-05 23:12:48 +0200805 if (!strcmp(name, "Nintendo RVL-CNT-01")) {
806 devtype = WIIMOTE_DEV_GEN10;
807 goto done;
808 } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
809 devtype = WIIMOTE_DEV_GEN20;
810 goto done;
David Herrmannf1d4bed2013-05-05 23:12:58 +0200811 } else if (!strcmp(name, "Nintendo RVL-WBC-01")) {
812 devtype = WIIMOTE_DEV_BALANCE_BOARD;
813 goto done;
David Herrmannc57ff762013-05-05 23:12:48 +0200814 }
815
816 if (vendor == USB_VENDOR_ID_NINTENDO) {
817 if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
818 devtype = WIIMOTE_DEV_GEN10;
819 goto done;
820 } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
821 devtype = WIIMOTE_DEV_GEN20;
822 goto done;
823 }
824 }
825
826done:
827 if (devtype == WIIMOTE_DEV_GENERIC)
828 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
829 name, vendor, product, exttype);
830 else
831 hid_info(wdata->hdev, "detected device: %s\n",
832 wiimote_devtype_names[devtype]);
833
David Herrmann27f06942013-05-05 23:12:51 +0200834 wiimote_modules_load(wdata, devtype);
David Herrmannc57ff762013-05-05 23:12:48 +0200835}
836
837static void wiimote_init_detect(struct wiimote_data *wdata)
838{
David Herrmann4148b6b2013-05-05 23:12:57 +0200839 __u8 exttype = WIIMOTE_EXT_NONE, extdata[6];
David Herrmannc57ff762013-05-05 23:12:48 +0200840 bool ext;
841 int ret;
842
843 wiimote_cmd_acquire_noint(wdata);
844
845 spin_lock_irq(&wdata->state.lock);
David Herrmann27f06942013-05-05 23:12:51 +0200846 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
David Herrmannc57ff762013-05-05 23:12:48 +0200847 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
848 wiiproto_req_status(wdata);
849 spin_unlock_irq(&wdata->state.lock);
850
851 ret = wiimote_cmd_wait_noint(wdata);
852 if (ret)
853 goto out_release;
854
855 spin_lock_irq(&wdata->state.lock);
856 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
857 spin_unlock_irq(&wdata->state.lock);
858
859 if (!ext)
860 goto out_release;
861
862 wiimote_cmd_init_ext(wdata);
David Herrmann4148b6b2013-05-05 23:12:57 +0200863 exttype = wiimote_cmd_read_ext(wdata, extdata);
David Herrmannc57ff762013-05-05 23:12:48 +0200864
865out_release:
866 wiimote_cmd_release(wdata);
867 wiimote_init_set_type(wdata, exttype);
David Herrmann4148b6b2013-05-05 23:12:57 +0200868 /* schedule MP timer */
869 mod_timer(&wdata->timer, jiffies + HZ * 4);
870}
871
872/*
873 * MP hotplug events are not generated by the wiimote. Therefore, we need
874 * polling to detect it. We use a 4s interval for polling MP registers. This
875 * seems reasonable considering applications can trigger it manually via
876 * sysfs requests.
877 */
878static void wiimote_init_poll_mp(struct wiimote_data *wdata)
879{
880 bool mp;
881 __u8 mpdata[6];
882
883 wiimote_cmd_acquire_noint(wdata);
884 wiimote_cmd_init_mp(wdata);
885 mp = wiimote_cmd_read_mp(wdata, mpdata);
886 wiimote_cmd_release(wdata);
887
888 /* load/unload MP module if it changed */
889 if (mp) {
890 if (!wdata->state.mp) {
891 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
892 wiimote_mp_load(wdata);
893 }
894 } else if (wdata->state.mp) {
895 wiimote_mp_unload(wdata);
896 }
897
898 mod_timer(&wdata->timer, jiffies + HZ * 4);
899}
900
901/*
902 * Check whether the wiimote is in the expected state. The extension registers
903 * may change during hotplug and initialization so we might get hotplug events
904 * that we caused by remapping some memory.
905 * We use some heuristics here to check known states. If the wiimote is in the
906 * expected state, we can ignore the hotplug event.
907 *
908 * Returns "true" if the device is in expected state, "false" if we should
909 * redo hotplug handling and extension initialization.
910 */
911static bool wiimote_init_check(struct wiimote_data *wdata)
912{
913 __u32 flags;
914 __u8 type, data[6];
915 bool ret, poll_mp;
916
917 spin_lock_irq(&wdata->state.lock);
918 flags = wdata->state.flags;
919 spin_unlock_irq(&wdata->state.lock);
920
921 wiimote_cmd_acquire_noint(wdata);
922
923 /* If MP is used and active, but the extension is not, we expect:
924 * read_mp_mapped() == WIIMOTE_MP_SINGLE
925 * state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE
926 * We do not check EXT_PLUGGED because it might change during
927 * initialization of MP without extensions.
928 * - If MP is unplugged/replugged, read_mp_mapped() fails
929 * - If EXT is plugged, MP_PLUGGED will get set */
930 if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
931 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
932 type = wiimote_cmd_read_mp_mapped(wdata);
933 ret = type == WIIMOTE_MP_SINGLE;
934
935 spin_lock_irq(&wdata->state.lock);
936 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
937 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
938 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
939 spin_unlock_irq(&wdata->state.lock);
940
941 if (!ret)
942 hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
943
944 /* while MP is mapped, we get EXT_PLUGGED events */
945 poll_mp = false;
946
947 goto out_release;
948 }
949
950 /* If MP is unused, but the extension port is used, we expect:
951 * read_ext == state.exttype
952 * state.flags == !MP_ACTIVE && EXT_ACTIVE
953 * - If MP is plugged/unplugged, our timer detects it
954 * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */
955 if (!(flags & WIIPROTO_FLAG_MP_USED) &&
956 wdata->state.exttype != WIIMOTE_EXT_NONE) {
957 type = wiimote_cmd_read_ext(wdata, data);
958 ret = type == wdata->state.exttype;
959
960 spin_lock_irq(&wdata->state.lock);
961 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
962 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
963 spin_unlock_irq(&wdata->state.lock);
964
965 if (!ret)
966 hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
967
968 /* poll MP for hotplug events */
969 poll_mp = true;
970
971 goto out_release;
972 }
973
974 /* If neither MP nor an extension are used, we expect:
975 * read_ext() == WIIMOTE_EXT_NONE
976 * state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED
977 * No need to perform any action in this case as everything is
978 * disabled already.
979 * - If MP is plugged/unplugged, our timer detects it
980 * - If EXT is plugged, EXT_PLUGGED will be set */
981 if (!(flags & WIIPROTO_FLAG_MP_USED) &&
982 wdata->state.exttype == WIIMOTE_EXT_NONE) {
983 type = wiimote_cmd_read_ext(wdata, data);
984 ret = type == wdata->state.exttype;
985
986 spin_lock_irq(&wdata->state.lock);
987 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
988 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
989 ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
990 spin_unlock_irq(&wdata->state.lock);
991
992 if (!ret)
993 hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
994
995 /* poll MP for hotplug events */
996 poll_mp = true;
997
998 goto out_release;
999 }
1000
1001 /* The trickiest part is if both EXT and MP are active. We cannot read
1002 * the EXT ID, anymore, because MP is mapped over it. However, we use
1003 * a handy trick here:
1004 * - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent
1005 * MP_PLUGGED might be re-sent again before we are scheduled, but
1006 * EXT_ACTIVE will stay unset.
1007 * So it is enough to check for mp_mapped() and MP_ACTIVE and
1008 * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */
1009 if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
1010 wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
1011 type = wiimote_cmd_read_mp_mapped(wdata);
1012 ret = type != WIIMOTE_MP_NONE;
1013 ret = ret && type != WIIMOTE_MP_UNKNOWN;
1014 ret = ret && type != WIIMOTE_MP_SINGLE;
1015
1016 spin_lock_irq(&wdata->state.lock);
1017 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
1018 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
1019 ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
1020 spin_unlock_irq(&wdata->state.lock);
1021
1022 if (!ret)
1023 hid_dbg(wdata->hdev, "state left: EXT && MP\n");
1024
1025 /* while MP is mapped, we get EXT_PLUGGED events */
1026 poll_mp = false;
1027
1028 goto out_release;
1029 }
1030
1031 /* unknown state */
1032 ret = false;
1033
1034out_release:
1035 wiimote_cmd_release(wdata);
1036
1037 /* only poll for MP if requested and if state didn't change */
1038 if (ret && poll_mp)
1039 wiimote_init_poll_mp(wdata);
1040
1041 return ret;
1042}
1043
1044static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = {
1045 [WIIMOTE_EXT_NONE] = "None",
1046 [WIIMOTE_EXT_UNKNOWN] = "Unknown",
David Herrmannb6ee67b2013-05-05 23:12:59 +02001047 [WIIMOTE_EXT_NUNCHUK] = "Nintendo Wii Nunchuk",
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001048 [WIIMOTE_EXT_CLASSIC_CONTROLLER] = "Nintendo Wii Classic Controller",
David Herrmannf1d4bed2013-05-05 23:12:58 +02001049 [WIIMOTE_EXT_BALANCE_BOARD] = "Nintendo Wii Balance Board",
David Herrmann4148b6b2013-05-05 23:12:57 +02001050};
1051
1052/*
1053 * Handle hotplug events
1054 * If we receive an hotplug event and the device-check failed, we deinitialize
1055 * the extension ports, re-read all extension IDs and set the device into
1056 * the desired state. This involves mapping MP into the main extension
1057 * registers, setting up extension passthrough modes and initializing the
1058 * requested extensions.
1059 */
1060static void wiimote_init_hotplug(struct wiimote_data *wdata)
1061{
1062 __u8 exttype, extdata[6], mpdata[6];
1063 __u32 flags;
1064 bool mp;
1065
1066 hid_dbg(wdata->hdev, "detect extensions..\n");
1067
1068 wiimote_cmd_acquire_noint(wdata);
1069
1070 spin_lock_irq(&wdata->state.lock);
1071
1072 /* get state snapshot that we will then work on */
1073 flags = wdata->state.flags;
1074
1075 /* disable event forwarding temporarily */
1076 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1077 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1078
1079 spin_unlock_irq(&wdata->state.lock);
1080
1081 /* init extension and MP (deactivates current extension or MP) */
1082 wiimote_cmd_init_ext(wdata);
1083 wiimote_cmd_init_mp(wdata);
1084 mp = wiimote_cmd_read_mp(wdata, mpdata);
1085 exttype = wiimote_cmd_read_ext(wdata, extdata);
1086
1087 wiimote_cmd_release(wdata);
1088
1089 /* load/unload extension module if it changed */
1090 if (exttype != wdata->state.exttype) {
1091 /* unload previous extension */
1092 wiimote_ext_unload(wdata);
1093
1094 if (exttype == WIIMOTE_EXT_UNKNOWN) {
1095 hid_info(wdata->hdev, "cannot detect extension; %02x:%02x %02x:%02x %02x:%02x\n",
1096 extdata[0], extdata[1], extdata[2],
1097 extdata[3], extdata[4], extdata[5]);
1098 } else if (exttype == WIIMOTE_EXT_NONE) {
1099 spin_lock_irq(&wdata->state.lock);
1100 wdata->state.exttype = WIIMOTE_EXT_NONE;
1101 spin_unlock_irq(&wdata->state.lock);
1102 } else {
1103 hid_info(wdata->hdev, "detected extension: %s\n",
1104 wiimote_exttype_names[exttype]);
1105 /* try loading new extension */
1106 wiimote_ext_load(wdata, exttype);
1107 }
1108 }
1109
1110 /* load/unload MP module if it changed */
1111 if (mp) {
1112 if (!wdata->state.mp) {
1113 hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
1114 wiimote_mp_load(wdata);
1115 }
1116 } else if (wdata->state.mp) {
1117 wiimote_mp_unload(wdata);
1118 }
1119
1120 /* if MP is not used, do not map or activate it */
1121 if (!(flags & WIIPROTO_FLAG_MP_USED))
1122 mp = false;
1123
1124 /* map MP into main extension registers if used */
1125 if (mp) {
1126 wiimote_cmd_acquire_noint(wdata);
1127 wiimote_cmd_map_mp(wdata, exttype);
1128 wiimote_cmd_release(wdata);
1129
1130 /* delete MP hotplug timer */
1131 del_timer_sync(&wdata->timer);
1132 } else {
1133 /* reschedule MP hotplug timer */
1134 mod_timer(&wdata->timer, jiffies + HZ * 4);
1135 }
1136
1137 spin_lock_irq(&wdata->state.lock);
1138
1139 /* enable data forwarding again and set expected hotplug state */
1140 if (mp) {
1141 wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
1142 if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
1143 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1144 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1145 } else {
1146 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1147 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1148 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1149 }
1150 } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
1151 wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
1152 }
1153
1154 /* request status report for hotplug state updates */
1155 wiiproto_req_status(wdata);
1156
1157 spin_unlock_irq(&wdata->state.lock);
1158
1159 hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
1160 wdata->state.mp, wdata->state.exttype);
David Herrmannc57ff762013-05-05 23:12:48 +02001161}
1162
1163static void wiimote_init_worker(struct work_struct *work)
1164{
1165 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
1166 init_worker);
1167
1168 if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
1169 wiimote_init_detect(wdata);
David Herrmann4148b6b2013-05-05 23:12:57 +02001170 if (!wiimote_init_check(wdata))
1171 wiimote_init_hotplug(wdata);
1172}
1173
1174void __wiimote_schedule(struct wiimote_data *wdata)
1175{
1176 if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
1177 schedule_work(&wdata->init_worker);
1178}
1179
1180static void wiimote_schedule(struct wiimote_data *wdata)
1181{
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&wdata->state.lock, flags);
1185 __wiimote_schedule(wdata);
1186 spin_unlock_irqrestore(&wdata->state.lock, flags);
1187}
1188
1189static void wiimote_init_timeout(unsigned long arg)
1190{
1191 struct wiimote_data *wdata = (void*)arg;
1192
1193 wiimote_schedule(wdata);
David Herrmannc57ff762013-05-05 23:12:48 +02001194}
1195
1196/* protocol handlers */
1197
David Herrmann1abb9ad2011-07-05 13:45:16 +02001198static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
1199{
David Herrmann20cef812013-05-05 23:12:52 +02001200 const __u8 *iter, *mods;
1201 const struct wiimod_ops *ops;
1202
David Herrmann4148b6b2013-05-05 23:12:57 +02001203 ops = wiimod_ext_table[wdata->state.exttype];
1204 if (ops->in_keys) {
1205 ops->in_keys(wdata, payload);
1206 return;
1207 }
1208
David Herrmann20cef812013-05-05 23:12:52 +02001209 mods = wiimote_devtype_mods[wdata->state.devtype];
1210 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1211 ops = wiimod_table[*iter];
1212 if (ops->in_keys) {
1213 ops->in_keys(wdata, payload);
1214 break;
1215 }
1216 }
David Herrmann1abb9ad2011-07-05 13:45:16 +02001217}
1218
David Herrmannefcf9182011-09-06 13:50:29 +02001219static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
1220{
David Herrmann0ea16752013-05-05 23:12:55 +02001221 const __u8 *iter, *mods;
1222 const struct wiimod_ops *ops;
David Herrmannefcf9182011-09-06 13:50:29 +02001223
David Herrmann4148b6b2013-05-05 23:12:57 +02001224 ops = wiimod_ext_table[wdata->state.exttype];
1225 if (ops->in_accel) {
1226 ops->in_accel(wdata, payload);
1227 return;
1228 }
1229
David Herrmann0ea16752013-05-05 23:12:55 +02001230 mods = wiimote_devtype_mods[wdata->state.devtype];
1231 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1232 ops = wiimod_table[*iter];
1233 if (ops->in_accel) {
1234 ops->in_accel(wdata, payload);
1235 break;
1236 }
1237 }
David Herrmannefcf9182011-09-06 13:50:29 +02001238}
1239
David Herrmann4148b6b2013-05-05 23:12:57 +02001240static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len)
1241{
1242 if (!ops->in_ext)
1243 return false;
1244 if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8)
1245 return false;
1246 if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16)
1247 return false;
1248
1249 return true;
1250}
1251
1252static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
1253 size_t len)
1254{
1255 const __u8 *iter, *mods;
1256 const struct wiimod_ops *ops;
1257 bool is_mp;
1258
1259 if (len < 6)
1260 return;
1261
1262 /* if MP is active, track MP slot hotplugging */
1263 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1264 /* this bit is set for invalid events (eg. during hotplug) */
1265 if (payload[5] & 0x01)
1266 return;
1267
1268 if (payload[4] & 0x01) {
1269 if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
1270 hid_dbg(wdata->hdev, "MP hotplug: 1\n");
1271 wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
1272 __wiimote_schedule(wdata);
1273 }
1274 } else {
1275 if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
1276 hid_dbg(wdata->hdev, "MP hotplug: 0\n");
1277 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1278 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1279 __wiimote_schedule(wdata);
1280 }
1281 }
1282
1283 /* detect MP data that is sent interleaved with EXT data */
1284 is_mp = payload[5] & 0x02;
1285 } else {
1286 is_mp = false;
1287 }
1288
1289 /* ignore EXT events if no extension is active */
1290 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
1291 return;
1292
1293 /* try forwarding to extension handler, first */
1294 ops = wiimod_ext_table[wdata->state.exttype];
1295 if (is_mp && ops->in_mp) {
1296 ops->in_mp(wdata, payload);
1297 return;
1298 } else if (!is_mp && valid_ext_handler(ops, len)) {
1299 ops->in_ext(wdata, payload);
1300 return;
1301 }
1302
1303 /* try forwarding to MP handler */
1304 ops = &wiimod_mp;
1305 if (is_mp && ops->in_mp) {
1306 ops->in_mp(wdata, payload);
1307 return;
1308 } else if (!is_mp && valid_ext_handler(ops, len)) {
1309 ops->in_ext(wdata, payload);
1310 return;
1311 }
1312
1313 /* try forwarding to loaded modules */
1314 mods = wiimote_devtype_mods[wdata->state.devtype];
1315 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1316 ops = wiimod_table[*iter];
1317 if (is_mp && ops->in_mp) {
1318 ops->in_mp(wdata, payload);
1319 return;
1320 } else if (!is_mp && valid_ext_handler(ops, len)) {
1321 ops->in_ext(wdata, payload);
1322 return;
1323 }
1324 }
1325}
1326
David Herrmann3b5f03c2013-05-05 23:12:56 +02001327#define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
1328#define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
1329#define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
1330#define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
David Herrmanneac39e72011-09-06 13:50:31 +02001331
David Herrmann3b5f03c2013-05-05 23:12:56 +02001332static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
1333 bool packed, unsigned int id)
David Herrmanneac39e72011-09-06 13:50:31 +02001334{
David Herrmann3b5f03c2013-05-05 23:12:56 +02001335 const __u8 *iter, *mods;
1336 const struct wiimod_ops *ops;
David Herrmanneac39e72011-09-06 13:50:31 +02001337
David Herrmann4148b6b2013-05-05 23:12:57 +02001338 ops = wiimod_ext_table[wdata->state.exttype];
1339 if (ops->in_ir) {
1340 ops->in_ir(wdata, payload, packed, id);
1341 return;
1342 }
1343
David Herrmann3b5f03c2013-05-05 23:12:56 +02001344 mods = wiimote_devtype_mods[wdata->state.devtype];
1345 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
1346 ops = wiimod_table[*iter];
1347 if (ops->in_ir) {
1348 ops->in_ir(wdata, payload, packed, id);
1349 break;
1350 }
David Herrmanneac39e72011-09-06 13:50:31 +02001351 }
David Herrmanneac39e72011-09-06 13:50:31 +02001352}
David Herrmannefcf9182011-09-06 13:50:29 +02001353
David Herrmann2d44e3d2013-04-02 19:58:36 +02001354/* reduced status report with "BB BB" key data only */
1355static void handler_status_K(struct wiimote_data *wdata,
1356 const __u8 *payload)
David Herrmannc87019e2011-08-17 11:43:24 +02001357{
1358 handler_keys(wdata, payload);
1359
1360 /* on status reports the drm is reset so we need to resend the drm */
1361 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001362}
1363
1364/* extended status report with "BB BB LF 00 00 VV" data */
1365static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1366{
1367 handler_status_K(wdata, payload);
David Herrmanne3979a92011-09-06 13:50:38 +02001368
David Herrmannc57ff762013-05-05 23:12:48 +02001369 /* update extension status */
1370 if (payload[2] & 0x02) {
David Herrmann4148b6b2013-05-05 23:12:57 +02001371 if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
1372 hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
1373 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1374 __wiimote_schedule(wdata);
1375 }
David Herrmannc57ff762013-05-05 23:12:48 +02001376 } else {
David Herrmann4148b6b2013-05-05 23:12:57 +02001377 if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
1378 hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
1379 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1380 wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
1381 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
1382 wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
1383 __wiimote_schedule(wdata);
1384 }
David Herrmannc57ff762013-05-05 23:12:48 +02001385 }
David Herrmanncb992212011-11-17 14:12:01 +01001386
David Herrmann6b80bb92013-05-05 23:12:49 +02001387 wdata->state.cmd_battery = payload[5];
1388 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
David Herrmanne3979a92011-09-06 13:50:38 +02001389 wiimote_cmd_complete(wdata);
David Herrmannc87019e2011-08-17 11:43:24 +02001390}
1391
David Herrmann2d44e3d2013-04-02 19:58:36 +02001392/* reduced generic report with "BB BB" key data only */
1393static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1394{
1395 handler_keys(wdata, payload);
1396}
1397
David Herrmannbe1ecd62011-09-06 13:50:33 +02001398static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1399{
David Herrmannfad8c0e2011-11-17 14:12:00 +01001400 __u16 offset = payload[3] << 8 | payload[4];
1401 __u8 size = (payload[2] >> 4) + 1;
1402 __u8 err = payload[2] & 0x0f;
1403
David Herrmannbe1ecd62011-09-06 13:50:33 +02001404 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +01001405
1406 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1407 if (err)
1408 size = 0;
1409 else if (size > wdata->state.cmd_read_size)
1410 size = wdata->state.cmd_read_size;
1411
1412 wdata->state.cmd_read_size = size;
1413 if (wdata->state.cmd_read_buf)
1414 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1415 wiimote_cmd_complete(wdata);
1416 }
David Herrmannbe1ecd62011-09-06 13:50:33 +02001417}
1418
David Herrmannc87019e2011-08-17 11:43:24 +02001419static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1420{
1421 __u8 err = payload[3];
1422 __u8 cmd = payload[2];
1423
1424 handler_keys(wdata, payload);
1425
David Herrmann33e84012011-09-06 13:50:35 +02001426 if (wiimote_cmd_pending(wdata, cmd, 0)) {
1427 wdata->state.cmd_err = err;
1428 wiimote_cmd_complete(wdata);
1429 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +02001430 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
1431 cmd);
David Herrmann33e84012011-09-06 13:50:35 +02001432 }
David Herrmannc87019e2011-08-17 11:43:24 +02001433}
1434
David Herrmannefcf9182011-09-06 13:50:29 +02001435static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1436{
1437 handler_keys(wdata, payload);
1438 handler_accel(wdata, payload);
1439}
1440
David Herrmann7336b9f2011-09-06 13:50:32 +02001441static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1442{
1443 handler_keys(wdata, payload);
David Herrmann4148b6b2013-05-05 23:12:57 +02001444 handler_ext(wdata, &payload[2], 8);
David Herrmann7336b9f2011-09-06 13:50:32 +02001445}
1446
David Herrmannefcf9182011-09-06 13:50:29 +02001447static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1448{
1449 handler_keys(wdata, payload);
1450 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +02001451 ir_to_input0(wdata, &payload[5], false);
1452 ir_to_input1(wdata, &payload[8], false);
1453 ir_to_input2(wdata, &payload[11], false);
1454 ir_to_input3(wdata, &payload[14], false);
David Herrmanneac39e72011-09-06 13:50:31 +02001455}
1456
David Herrmann7336b9f2011-09-06 13:50:32 +02001457static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1458{
1459 handler_keys(wdata, payload);
David Herrmann4148b6b2013-05-05 23:12:57 +02001460 handler_ext(wdata, &payload[2], 19);
David Herrmann7336b9f2011-09-06 13:50:32 +02001461}
1462
David Herrmanneac39e72011-09-06 13:50:31 +02001463static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1464{
1465 handler_keys(wdata, payload);
1466 ir_to_input0(wdata, &payload[2], false);
1467 ir_to_input1(wdata, &payload[4], true);
1468 ir_to_input2(wdata, &payload[7], false);
1469 ir_to_input3(wdata, &payload[9], true);
David Herrmann4148b6b2013-05-05 23:12:57 +02001470 handler_ext(wdata, &payload[12], 9);
David Herrmannefcf9182011-09-06 13:50:29 +02001471}
1472
1473static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1474{
1475 handler_keys(wdata, payload);
1476 handler_accel(wdata, payload);
David Herrmann4148b6b2013-05-05 23:12:57 +02001477 handler_ext(wdata, &payload[5], 16);
David Herrmannefcf9182011-09-06 13:50:29 +02001478}
1479
1480static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1481{
1482 handler_keys(wdata, payload);
1483 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +02001484 ir_to_input0(wdata, &payload[5], false);
1485 ir_to_input1(wdata, &payload[7], true);
1486 ir_to_input2(wdata, &payload[10], false);
1487 ir_to_input3(wdata, &payload[12], true);
David Herrmann4148b6b2013-05-05 23:12:57 +02001488 handler_ext(wdata, &payload[15], 6);
David Herrmannefcf9182011-09-06 13:50:29 +02001489}
1490
David Herrmann7336b9f2011-09-06 13:50:32 +02001491static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1492{
David Herrmann4148b6b2013-05-05 23:12:57 +02001493 handler_ext(wdata, payload, 21);
David Herrmann7336b9f2011-09-06 13:50:32 +02001494}
1495
David Herrmannefcf9182011-09-06 13:50:29 +02001496static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1497{
1498 handler_keys(wdata, payload);
1499
1500 wdata->state.accel_split[0] = payload[2];
1501 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1502 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +02001503
1504 ir_to_input0(wdata, &payload[3], false);
1505 ir_to_input1(wdata, &payload[12], false);
David Herrmannefcf9182011-09-06 13:50:29 +02001506}
1507
1508static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1509{
1510 __u8 buf[5];
1511
1512 handler_keys(wdata, payload);
1513
1514 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1515 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1516
1517 buf[0] = 0;
1518 buf[1] = 0;
1519 buf[2] = wdata->state.accel_split[0];
1520 buf[3] = payload[2];
1521 buf[4] = wdata->state.accel_split[1];
1522 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +02001523
1524 ir_to_input2(wdata, &payload[3], false);
1525 ir_to_input3(wdata, &payload[12], false);
David Herrmannefcf9182011-09-06 13:50:29 +02001526}
1527
David Herrmanna4d19192011-07-05 13:45:15 +02001528struct wiiproto_handler {
1529 __u8 id;
1530 size_t size;
1531 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1532};
1533
1534static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +02001535 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001536 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
David Herrmannbe1ecd62011-09-06 13:50:33 +02001537 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001538 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
David Herrmannc87019e2011-08-17 11:43:24 +02001539 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001540 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
David Herrmann1abb9ad2011-07-05 13:45:16 +02001541 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +02001542 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001543 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001544 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001545 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001546 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001547 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001548 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001549 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001550 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001551 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
David Herrmanneac39e72011-09-06 13:50:31 +02001552 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001553 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001554 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001555 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001556 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +02001557 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1558 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +02001559 { .id = 0 }
1560};
1561
David Herrmann02fb72a2011-07-05 13:45:09 +02001562static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1563 u8 *raw_data, int size)
1564{
David Herrmann4d36e972011-07-05 13:45:12 +02001565 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +02001566 struct wiiproto_handler *h;
1567 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001568 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +02001569
David Herrmann02fb72a2011-07-05 13:45:09 +02001570 if (size < 1)
1571 return -EINVAL;
1572
David Herrmann32a0d9a2011-07-05 13:45:18 +02001573 spin_lock_irqsave(&wdata->state.lock, flags);
1574
David Herrmanna4d19192011-07-05 13:45:15 +02001575 for (i = 0; handlers[i].id; ++i) {
1576 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +02001577 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +02001578 h->func(wdata, &raw_data[1]);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001579 break;
David Herrmann7336b9f2011-09-06 13:50:32 +02001580 }
David Herrmanna4d19192011-07-05 13:45:15 +02001581 }
1582
David Herrmann2d44e3d2013-04-02 19:58:36 +02001583 if (!handlers[i].id)
David Herrmann7336b9f2011-09-06 13:50:32 +02001584 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1585 size);
1586
David Herrmann32a0d9a2011-07-05 13:45:18 +02001587 spin_unlock_irqrestore(&wdata->state.lock, flags);
1588
David Herrmann02fb72a2011-07-05 13:45:09 +02001589 return 0;
1590}
1591
David Herrmanne894d0e2011-07-05 13:45:10 +02001592static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1593{
1594 struct wiimote_data *wdata;
1595
1596 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1597 if (!wdata)
1598 return NULL;
1599
1600 wdata->hdev = hdev;
1601 hid_set_drvdata(hdev, wdata);
1602
David Herrmann13938532013-05-05 23:12:46 +02001603 spin_lock_init(&wdata->queue.lock);
1604 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
David Herrmann23c063c2011-07-05 13:45:14 +02001605
David Herrmann32a0d9a2011-07-05 13:45:18 +02001606 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001607 init_completion(&wdata->state.ready);
1608 mutex_init(&wdata->state.sync);
David Herrmann43d782a2011-11-17 14:12:12 +01001609 wdata->state.drm = WIIPROTO_REQ_DRM_K;
David Herrmann6b80bb92013-05-05 23:12:49 +02001610 wdata->state.cmd_battery = 0xff;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001611
David Herrmannc57ff762013-05-05 23:12:48 +02001612 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
David Herrmann4148b6b2013-05-05 23:12:57 +02001613 setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata);
David Herrmannc57ff762013-05-05 23:12:48 +02001614
David Herrmanne894d0e2011-07-05 13:45:10 +02001615 return wdata;
1616}
1617
1618static void wiimote_destroy(struct wiimote_data *wdata)
1619{
David Herrmann4148b6b2013-05-05 23:12:57 +02001620 unsigned long flags;
1621
David Herrmann43e5e7c2011-11-17 14:12:10 +01001622 wiidebug_deinit(wdata);
David Herrmann4148b6b2013-05-05 23:12:57 +02001623
1624 /* prevent init_worker from being scheduled again */
1625 spin_lock_irqsave(&wdata->state.lock, flags);
1626 wdata->state.flags |= WIIPROTO_FLAG_EXITING;
1627 spin_unlock_irqrestore(&wdata->state.lock, flags);
David Herrmann3989ef62011-08-17 11:43:20 +02001628
David Herrmann20cef812013-05-05 23:12:52 +02001629 cancel_work_sync(&wdata->init_worker);
David Herrmann4148b6b2013-05-05 23:12:57 +02001630 del_timer_sync(&wdata->timer);
1631
1632 wiimote_mp_unload(wdata);
1633 wiimote_ext_unload(wdata);
David Herrmann27f06942013-05-05 23:12:51 +02001634 wiimote_modules_unload(wdata);
David Herrmann13938532013-05-05 23:12:46 +02001635 cancel_work_sync(&wdata->queue.worker);
David Herrmann5682b1a2013-05-05 23:12:47 +02001636 hid_hw_close(wdata->hdev);
David Herrmann3989ef62011-08-17 11:43:20 +02001637 hid_hw_stop(wdata->hdev);
1638
David Herrmanne894d0e2011-07-05 13:45:10 +02001639 kfree(wdata);
1640}
1641
David Herrmann02fb72a2011-07-05 13:45:09 +02001642static int wiimote_hid_probe(struct hid_device *hdev,
1643 const struct hid_device_id *id)
1644{
David Herrmanne894d0e2011-07-05 13:45:10 +02001645 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001646 int ret;
1647
David Herrmann90120d62011-11-17 14:12:14 +01001648 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1649
David Herrmanne894d0e2011-07-05 13:45:10 +02001650 wdata = wiimote_create(hdev);
1651 if (!wdata) {
1652 hid_err(hdev, "Can't alloc device\n");
1653 return -ENOMEM;
1654 }
1655
David Herrmann02fb72a2011-07-05 13:45:09 +02001656 ret = hid_parse(hdev);
1657 if (ret) {
1658 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001659 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001660 }
1661
1662 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1663 if (ret) {
1664 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001665 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001666 }
1667
David Herrmann5682b1a2013-05-05 23:12:47 +02001668 ret = hid_hw_open(hdev);
1669 if (ret) {
1670 hid_err(hdev, "cannot start hardware I/O\n");
1671 goto err_stop;
1672 }
1673
David Herrmann43e5e7c2011-11-17 14:12:10 +01001674 ret = wiidebug_init(wdata);
1675 if (ret)
1676 goto err_free;
1677
David Herrmann02fb72a2011-07-05 13:45:09 +02001678 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001679
David Herrmannc57ff762013-05-05 23:12:48 +02001680 /* schedule device detection */
David Herrmann4148b6b2013-05-05 23:12:57 +02001681 wiimote_schedule(wdata);
David Herrmannc57ff762013-05-05 23:12:48 +02001682
David Herrmann02fb72a2011-07-05 13:45:09 +02001683 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001684
David Herrmann3989ef62011-08-17 11:43:20 +02001685err_free:
1686 wiimote_destroy(wdata);
1687 return ret;
1688
David Herrmann672bc4e2011-07-05 13:45:11 +02001689err_stop:
1690 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001691err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001692 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001693 input_free_device(wdata->accel);
David Herrmann3989ef62011-08-17 11:43:20 +02001694 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001695 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001696}
1697
1698static void wiimote_hid_remove(struct hid_device *hdev)
1699{
David Herrmanne894d0e2011-07-05 13:45:10 +02001700 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1701
David Herrmann02fb72a2011-07-05 13:45:09 +02001702 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001703 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001704}
1705
1706static const struct hid_device_id wiimote_hid_devices[] = {
1707 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1708 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
David Herrmanna33042f2013-04-02 19:58:35 +02001709 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1710 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
David Herrmann02fb72a2011-07-05 13:45:09 +02001711 { }
1712};
1713MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1714
1715static struct hid_driver wiimote_hid_driver = {
1716 .name = "wiimote",
1717 .id_table = wiimote_hid_devices,
1718 .probe = wiimote_hid_probe,
1719 .remove = wiimote_hid_remove,
1720 .raw_event = wiimote_hid_event,
1721};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001722module_hid_driver(wiimote_hid_driver);
David Herrmann02fb72a2011-07-05 13:45:09 +02001723
David Herrmannfb51b442011-07-05 13:45:08 +02001724MODULE_LICENSE("GPL");
1725MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
David Herrmann92eda7e2013-05-05 23:12:45 +02001726MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");