blob: 275428b31509177583cea0cbfd55fcbb91de9d52 [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 Herrmann23a5a4a2011-08-17 11:43:22 +020017#include <linux/leds.h>
David Herrmannfb51b442011-07-05 13:45:08 +020018#include <linux/module.h>
David Herrmann29d28062011-09-06 13:50:34 +020019#include <linux/mutex.h>
David Herrmann6591d752011-09-06 13:50:39 +020020#include <linux/power_supply.h>
David Herrmann23c063c2011-07-05 13:45:14 +020021#include <linux/spinlock.h>
David Herrmann02fb72a2011-07-05 13:45:09 +020022#include "hid-ids.h"
David Herrmann7e274402011-11-17 14:11:59 +010023#include "hid-wiimote.h"
David Herrmannfb51b442011-07-05 13:45:08 +020024
David Herrmann1abb9ad2011-07-05 13:45:16 +020025enum wiiproto_keys {
26 WIIPROTO_KEY_LEFT,
27 WIIPROTO_KEY_RIGHT,
28 WIIPROTO_KEY_UP,
29 WIIPROTO_KEY_DOWN,
30 WIIPROTO_KEY_PLUS,
31 WIIPROTO_KEY_MINUS,
32 WIIPROTO_KEY_ONE,
33 WIIPROTO_KEY_TWO,
34 WIIPROTO_KEY_A,
35 WIIPROTO_KEY_B,
36 WIIPROTO_KEY_HOME,
37 WIIPROTO_KEY_COUNT
38};
39
40static __u16 wiiproto_keymap[] = {
41 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
42 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
43 KEY_UP, /* WIIPROTO_KEY_UP */
44 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
45 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
46 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
47 BTN_1, /* WIIPROTO_KEY_ONE */
48 BTN_2, /* WIIPROTO_KEY_TWO */
49 BTN_A, /* WIIPROTO_KEY_A */
50 BTN_B, /* WIIPROTO_KEY_B */
51 BTN_MODE, /* WIIPROTO_KEY_HOME */
52};
53
David Herrmann6591d752011-09-06 13:50:39 +020054static enum power_supply_property wiimote_battery_props[] = {
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -080055 POWER_SUPPLY_PROP_CAPACITY,
56 POWER_SUPPLY_PROP_SCOPE,
David Herrmann6591d752011-09-06 13:50:39 +020057};
58
David Herrmann13938532013-05-05 23:12:46 +020059/* output queue handling */
60
David Herrmannd758b1f02013-05-05 23:12:50 +020061static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
62 size_t count)
David Herrmann0c218f12011-07-05 13:45:13 +020063{
64 __u8 *buf;
David Herrmannd758b1f02013-05-05 23:12:50 +020065 int ret;
David Herrmann0c218f12011-07-05 13:45:13 +020066
67 if (!hdev->hid_output_raw_report)
68 return -ENODEV;
69
70 buf = kmemdup(buffer, count, GFP_KERNEL);
71 if (!buf)
72 return -ENOMEM;
73
74 ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
75
76 kfree(buf);
77 return ret;
78}
79
David Herrmann13938532013-05-05 23:12:46 +020080static void wiimote_queue_worker(struct work_struct *work)
David Herrmann23c063c2011-07-05 13:45:14 +020081{
David Herrmann13938532013-05-05 23:12:46 +020082 struct wiimote_queue *queue = container_of(work, struct wiimote_queue,
83 worker);
84 struct wiimote_data *wdata = container_of(queue, struct wiimote_data,
85 queue);
David Herrmann23c063c2011-07-05 13:45:14 +020086 unsigned long flags;
David Herrmannd758b1f02013-05-05 23:12:50 +020087 int ret;
David Herrmann23c063c2011-07-05 13:45:14 +020088
David Herrmann13938532013-05-05 23:12:46 +020089 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020090
David Herrmann13938532013-05-05 23:12:46 +020091 while (wdata->queue.head != wdata->queue.tail) {
92 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmannd758b1f02013-05-05 23:12:50 +020093 ret = wiimote_hid_send(wdata->hdev,
David Herrmann13938532013-05-05 23:12:46 +020094 wdata->queue.outq[wdata->queue.tail].data,
95 wdata->queue.outq[wdata->queue.tail].size);
David Herrmannd758b1f02013-05-05 23:12:50 +020096 if (ret < 0) {
97 spin_lock_irqsave(&wdata->state.lock, flags);
98 wiimote_cmd_abort(wdata);
99 spin_unlock_irqrestore(&wdata->state.lock, flags);
100 }
David Herrmann13938532013-05-05 23:12:46 +0200101 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200102
David Herrmann13938532013-05-05 23:12:46 +0200103 wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
David Herrmann23c063c2011-07-05 13:45:14 +0200104 }
105
David Herrmann13938532013-05-05 23:12:46 +0200106 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200107}
108
109static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
110 size_t count)
111{
112 unsigned long flags;
113 __u8 newhead;
114
115 if (count > HID_MAX_BUFFER_SIZE) {
116 hid_warn(wdata->hdev, "Sending too large output report\n");
David Herrmannd758b1f02013-05-05 23:12:50 +0200117
118 spin_lock_irqsave(&wdata->queue.lock, flags);
119 goto out_error;
David Herrmann23c063c2011-07-05 13:45:14 +0200120 }
121
122 /*
123 * Copy new request into our output queue and check whether the
124 * queue is full. If it is full, discard this request.
125 * If it is empty we need to start a new worker that will
126 * send out the buffer to the hid device.
127 * If the queue is not empty, then there must be a worker
128 * that is currently sending out our buffer and this worker
129 * will reschedule itself until the queue is empty.
130 */
131
David Herrmann13938532013-05-05 23:12:46 +0200132 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200133
David Herrmann13938532013-05-05 23:12:46 +0200134 memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
135 wdata->queue.outq[wdata->queue.head].size = count;
136 newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
David Herrmann23c063c2011-07-05 13:45:14 +0200137
David Herrmann13938532013-05-05 23:12:46 +0200138 if (wdata->queue.head == wdata->queue.tail) {
139 wdata->queue.head = newhead;
140 schedule_work(&wdata->queue.worker);
141 } else if (newhead != wdata->queue.tail) {
142 wdata->queue.head = newhead;
David Herrmann23c063c2011-07-05 13:45:14 +0200143 } else {
144 hid_warn(wdata->hdev, "Output queue is full");
David Herrmannd758b1f02013-05-05 23:12:50 +0200145 goto out_error;
David Herrmann23c063c2011-07-05 13:45:14 +0200146 }
147
David Herrmannd758b1f02013-05-05 23:12:50 +0200148 goto out_unlock;
149
150out_error:
151 wiimote_cmd_abort(wdata);
152out_unlock:
David Herrmann13938532013-05-05 23:12:46 +0200153 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200154}
155
David Herrmannc003ec22011-09-06 13:50:26 +0200156/*
157 * This sets the rumble bit on the given output report if rumble is
158 * currently enabled.
159 * \cmd1 must point to the second byte in the output report => &cmd[1]
160 * This must be called on nearly every output report before passing it
161 * into the output queue!
162 */
163static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
164{
165 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
166 *cmd1 |= 0x01;
167}
168
169static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
170{
171 __u8 cmd[2];
172
173 rumble = !!rumble;
174 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
175 return;
176
177 if (rumble)
178 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
179 else
180 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
181
182 cmd[0] = WIIPROTO_REQ_RUMBLE;
183 cmd[1] = 0;
184
185 wiiproto_keep_rumble(wdata, &cmd[1]);
186 wiimote_queue(wdata, cmd, sizeof(cmd));
187}
188
David Herrmanndb308342011-07-05 13:45:17 +0200189static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
190{
191 __u8 cmd[2];
192
David Herrmann32a0d9a2011-07-05 13:45:18 +0200193 leds &= WIIPROTO_FLAGS_LEDS;
194 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
195 return;
196 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
197
David Herrmanndb308342011-07-05 13:45:17 +0200198 cmd[0] = WIIPROTO_REQ_LED;
199 cmd[1] = 0;
200
201 if (leds & WIIPROTO_FLAG_LED1)
202 cmd[1] |= 0x10;
203 if (leds & WIIPROTO_FLAG_LED2)
204 cmd[1] |= 0x20;
205 if (leds & WIIPROTO_FLAG_LED3)
206 cmd[1] |= 0x40;
207 if (leds & WIIPROTO_FLAG_LED4)
208 cmd[1] |= 0x80;
209
David Herrmannc003ec22011-09-06 13:50:26 +0200210 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200211 wiimote_queue(wdata, cmd, sizeof(cmd));
212}
213
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200214/*
215 * Check what peripherals of the wiimote are currently
216 * active and select a proper DRM that supports all of
217 * the requested data inputs.
218 */
219static __u8 select_drm(struct wiimote_data *wdata)
220{
David Herrmannf363e4f2011-09-06 13:50:30 +0200221 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
David Herrmanncb992212011-11-17 14:12:01 +0100222 bool ext = wiiext_active(wdata);
David Herrmannf363e4f2011-09-06 13:50:30 +0200223
224 if (ir == WIIPROTO_FLAG_IR_BASIC) {
225 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
226 return WIIPROTO_REQ_DRM_KAIE;
227 else
228 return WIIPROTO_REQ_DRM_KIE;
229 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
230 return WIIPROTO_REQ_DRM_KAI;
231 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
232 return WIIPROTO_REQ_DRM_SKAI1;
233 } else {
David Herrmanncb992212011-11-17 14:12:01 +0100234 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
235 if (ext)
236 return WIIPROTO_REQ_DRM_KAE;
237 else
238 return WIIPROTO_REQ_DRM_KA;
239 } else {
240 if (ext)
241 return WIIPROTO_REQ_DRM_KE;
242 else
243 return WIIPROTO_REQ_DRM_K;
244 }
David Herrmannf363e4f2011-09-06 13:50:30 +0200245 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200246}
247
David Herrmann7e274402011-11-17 14:11:59 +0100248void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200249{
250 __u8 cmd[3];
251
252 if (drm == WIIPROTO_REQ_NULL)
253 drm = select_drm(wdata);
254
255 cmd[0] = WIIPROTO_REQ_DRM;
256 cmd[1] = 0;
257 cmd[2] = drm;
258
David Herrmann43d782a2011-11-17 14:12:12 +0100259 wdata->state.drm = drm;
David Herrmannc003ec22011-09-06 13:50:26 +0200260 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200261 wiimote_queue(wdata, cmd, sizeof(cmd));
262}
263
David Herrmanne3979a92011-09-06 13:50:38 +0200264static void wiiproto_req_status(struct wiimote_data *wdata)
265{
266 __u8 cmd[2];
267
268 cmd[0] = WIIPROTO_REQ_SREQ;
269 cmd[1] = 0;
270
271 wiiproto_keep_rumble(wdata, &cmd[1]);
272 wiimote_queue(wdata, cmd, sizeof(cmd));
273}
274
David Herrmann98a558a2011-09-06 13:50:28 +0200275static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
276{
277 accel = !!accel;
278 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
279 return;
280
281 if (accel)
282 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
283 else
284 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
285
286 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
287}
288
David Herrmannfc221cd2011-09-06 13:50:36 +0200289static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
290{
291 __u8 cmd[2];
292
293 cmd[0] = WIIPROTO_REQ_IR1;
294 cmd[1] = flags;
295
296 wiiproto_keep_rumble(wdata, &cmd[1]);
297 wiimote_queue(wdata, cmd, sizeof(cmd));
298}
299
300static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
301{
302 __u8 cmd[2];
303
304 cmd[0] = WIIPROTO_REQ_IR2;
305 cmd[1] = flags;
306
307 wiiproto_keep_rumble(wdata, &cmd[1]);
308 wiimote_queue(wdata, cmd, sizeof(cmd));
309}
310
David Herrmannbe1ecd62011-09-06 13:50:33 +0200311#define wiiproto_req_wreg(wdata, os, buf, sz) \
312 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
313
314#define wiiproto_req_weeprom(wdata, os, buf, sz) \
315 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
316
317static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
318 __u32 offset, const __u8 *buf, __u8 size)
319{
320 __u8 cmd[22];
321
322 if (size > 16 || size == 0) {
323 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
324 return;
325 }
326
327 memset(cmd, 0, sizeof(cmd));
328 cmd[0] = WIIPROTO_REQ_WMEM;
329 cmd[2] = (offset >> 16) & 0xff;
330 cmd[3] = (offset >> 8) & 0xff;
331 cmd[4] = offset & 0xff;
332 cmd[5] = size;
333 memcpy(&cmd[6], buf, size);
334
335 if (!eeprom)
336 cmd[1] |= 0x04;
337
338 wiiproto_keep_rumble(wdata, &cmd[1]);
339 wiimote_queue(wdata, cmd, sizeof(cmd));
340}
341
David Herrmann1d3452c2011-11-17 14:12:11 +0100342void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
343 __u16 size)
David Herrmannfad8c0e2011-11-17 14:12:00 +0100344{
345 __u8 cmd[7];
346
347 if (size == 0) {
348 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
349 return;
350 }
351
352 cmd[0] = WIIPROTO_REQ_RMEM;
353 cmd[1] = 0;
354 cmd[2] = (offset >> 16) & 0xff;
355 cmd[3] = (offset >> 8) & 0xff;
356 cmd[4] = offset & 0xff;
357 cmd[5] = (size >> 8) & 0xff;
358 cmd[6] = size & 0xff;
359
360 if (!eeprom)
361 cmd[1] |= 0x04;
362
363 wiiproto_keep_rumble(wdata, &cmd[1]);
364 wiimote_queue(wdata, cmd, sizeof(cmd));
365}
366
David Herrmann33e84012011-09-06 13:50:35 +0200367/* requries the cmd-mutex to be held */
David Herrmann7e274402011-11-17 14:11:59 +0100368int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
David Herrmann33e84012011-09-06 13:50:35 +0200369 const __u8 *wmem, __u8 size)
370{
371 unsigned long flags;
372 int ret;
373
374 spin_lock_irqsave(&wdata->state.lock, flags);
375 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
376 wiiproto_req_wreg(wdata, offset, wmem, size);
377 spin_unlock_irqrestore(&wdata->state.lock, flags);
378
379 ret = wiimote_cmd_wait(wdata);
380 if (!ret && wdata->state.cmd_err)
381 ret = -EIO;
382
383 return ret;
384}
385
David Herrmannfad8c0e2011-11-17 14:12:00 +0100386/* requries the cmd-mutex to be held */
387ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
388 __u8 size)
389{
390 unsigned long flags;
391 ssize_t ret;
392
393 spin_lock_irqsave(&wdata->state.lock, flags);
394 wdata->state.cmd_read_size = size;
395 wdata->state.cmd_read_buf = rmem;
396 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
397 wiiproto_req_rreg(wdata, offset, size);
398 spin_unlock_irqrestore(&wdata->state.lock, flags);
399
400 ret = wiimote_cmd_wait(wdata);
401
402 spin_lock_irqsave(&wdata->state.lock, flags);
403 wdata->state.cmd_read_buf = NULL;
404 spin_unlock_irqrestore(&wdata->state.lock, flags);
405
406 if (!ret) {
407 if (wdata->state.cmd_read_size == 0)
408 ret = -EIO;
409 else
410 ret = wdata->state.cmd_read_size;
411 }
412
413 return ret;
414}
415
David Herrmannc57ff762013-05-05 23:12:48 +0200416/* requires the cmd-mutex to be held */
417static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
418{
419 __u8 wmem;
420 int ret;
421
422 /* initialize extension */
423 wmem = 0x55;
424 ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
425 if (ret)
426 return ret;
427
428 /* disable default encryption */
429 wmem = 0x0;
430 ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
431 if (ret)
432 return ret;
433
434 return 0;
435}
436
437/* requires the cmd-mutex to be held */
438static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata)
439{
440 __u8 rmem[6];
441 int ret;
442
443 /* read extension ID */
444 ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
445 if (ret != 6)
446 return WIIMOTE_EXT_NONE;
447
448 if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
449 rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
450 return WIIMOTE_EXT_NONE;
451
452 return WIIMOTE_EXT_UNKNOWN;
453}
454
David Herrmann6591d752011-09-06 13:50:39 +0200455static int wiimote_battery_get_property(struct power_supply *psy,
456 enum power_supply_property psp,
457 union power_supply_propval *val)
458{
459 struct wiimote_data *wdata = container_of(psy,
460 struct wiimote_data, battery);
461 int ret = 0, state;
462 unsigned long flags;
463
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800464 if (psp == POWER_SUPPLY_PROP_SCOPE) {
465 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
466 return 0;
467 }
468
David Herrmann6591d752011-09-06 13:50:39 +0200469 ret = wiimote_cmd_acquire(wdata);
470 if (ret)
471 return ret;
472
473 spin_lock_irqsave(&wdata->state.lock, flags);
474 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
475 wiiproto_req_status(wdata);
476 spin_unlock_irqrestore(&wdata->state.lock, flags);
477
David Herrmann6b80bb92013-05-05 23:12:49 +0200478 wiimote_cmd_wait(wdata);
David Herrmann6591d752011-09-06 13:50:39 +0200479 wiimote_cmd_release(wdata);
480
David Herrmann6b80bb92013-05-05 23:12:49 +0200481 spin_lock_irqsave(&wdata->state.lock, flags);
482 state = wdata->state.cmd_battery;
483 spin_unlock_irqrestore(&wdata->state.lock, flags);
David Herrmann6591d752011-09-06 13:50:39 +0200484
485 switch (psp) {
486 case POWER_SUPPLY_PROP_CAPACITY:
487 val->intval = state * 100 / 255;
488 break;
489 default:
490 ret = -EINVAL;
491 break;
492 }
493
494 return ret;
495}
496
David Herrmannfc221cd2011-09-06 13:50:36 +0200497static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
498{
499 int ret;
500 unsigned long flags;
501 __u8 format = 0;
502 static const __u8 data_enable[] = { 0x01 };
503 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
504 0x00, 0xaa, 0x00, 0x64 };
505 static const __u8 data_sens2[] = { 0x63, 0x03 };
506 static const __u8 data_fin[] = { 0x08 };
507
508 spin_lock_irqsave(&wdata->state.lock, flags);
509
510 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
511 spin_unlock_irqrestore(&wdata->state.lock, flags);
512 return 0;
513 }
514
515 if (mode == 0) {
516 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
517 wiiproto_req_ir1(wdata, 0);
518 wiiproto_req_ir2(wdata, 0);
519 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
520 spin_unlock_irqrestore(&wdata->state.lock, flags);
521 return 0;
522 }
523
524 spin_unlock_irqrestore(&wdata->state.lock, flags);
525
526 ret = wiimote_cmd_acquire(wdata);
527 if (ret)
528 return ret;
529
530 /* send PIXEL CLOCK ENABLE cmd first */
531 spin_lock_irqsave(&wdata->state.lock, flags);
532 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
533 wiiproto_req_ir1(wdata, 0x06);
534 spin_unlock_irqrestore(&wdata->state.lock, flags);
535
536 ret = wiimote_cmd_wait(wdata);
537 if (ret)
538 goto unlock;
539 if (wdata->state.cmd_err) {
540 ret = -EIO;
541 goto unlock;
542 }
543
544 /* enable IR LOGIC */
545 spin_lock_irqsave(&wdata->state.lock, flags);
546 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
547 wiiproto_req_ir2(wdata, 0x06);
548 spin_unlock_irqrestore(&wdata->state.lock, flags);
549
550 ret = wiimote_cmd_wait(wdata);
551 if (ret)
552 goto unlock;
553 if (wdata->state.cmd_err) {
554 ret = -EIO;
555 goto unlock;
556 }
557
558 /* enable IR cam but do not make it send data, yet */
559 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
560 sizeof(data_enable));
561 if (ret)
562 goto unlock;
563
564 /* write first sensitivity block */
565 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
566 sizeof(data_sens1));
567 if (ret)
568 goto unlock;
569
570 /* write second sensitivity block */
571 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
572 sizeof(data_sens2));
573 if (ret)
574 goto unlock;
575
576 /* put IR cam into desired state */
577 switch (mode) {
578 case WIIPROTO_FLAG_IR_FULL:
579 format = 5;
580 break;
581 case WIIPROTO_FLAG_IR_EXT:
582 format = 3;
583 break;
584 case WIIPROTO_FLAG_IR_BASIC:
585 format = 1;
586 break;
587 }
588 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
589 if (ret)
590 goto unlock;
591
592 /* make IR cam send data */
593 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
594 if (ret)
595 goto unlock;
596
597 /* request new DRM mode compatible to IR mode */
598 spin_lock_irqsave(&wdata->state.lock, flags);
599 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
600 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
601 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
602 spin_unlock_irqrestore(&wdata->state.lock, flags);
603
604unlock:
605 wiimote_cmd_release(wdata);
606 return ret;
607}
608
David Herrmann23a5a4a2011-08-17 11:43:22 +0200609static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
610{
611 struct wiimote_data *wdata;
612 struct device *dev = led_dev->dev->parent;
613 int i;
614 unsigned long flags;
615 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200616
David Herrmann23a5a4a2011-08-17 11:43:22 +0200617 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
618
619 for (i = 0; i < 4; ++i) {
620 if (wdata->leds[i] == led_dev) {
621 spin_lock_irqsave(&wdata->state.lock, flags);
622 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
623 spin_unlock_irqrestore(&wdata->state.lock, flags);
624 break;
625 }
626 }
627
628 return value ? LED_FULL : LED_OFF;
629}
630
631static void wiimote_leds_set(struct led_classdev *led_dev,
632 enum led_brightness value)
633{
634 struct wiimote_data *wdata;
635 struct device *dev = led_dev->dev->parent;
636 int i;
637 unsigned long flags;
638 __u8 state, flag;
639
640 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
641
642 for (i = 0; i < 4; ++i) {
643 if (wdata->leds[i] == led_dev) {
644 flag = WIIPROTO_FLAG_LED(i + 1);
645 spin_lock_irqsave(&wdata->state.lock, flags);
646 state = wdata->state.flags;
647 if (value == LED_OFF)
648 wiiproto_req_leds(wdata, state & ~flag);
649 else
650 wiiproto_req_leds(wdata, state | flag);
651 spin_unlock_irqrestore(&wdata->state.lock, flags);
652 break;
653 }
654 }
655}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200656
David Herrmannd020be92011-09-06 13:50:27 +0200657static int wiimote_ff_play(struct input_dev *dev, void *data,
658 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200659{
David Herrmannd020be92011-09-06 13:50:27 +0200660 struct wiimote_data *wdata = input_get_drvdata(dev);
661 __u8 value;
662 unsigned long flags;
663
664 /*
665 * The wiimote supports only a single rumble motor so if any magnitude
666 * is set to non-zero then we start the rumble motor. If both are set to
667 * zero, we stop the rumble motor.
668 */
669
670 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
671 value = 1;
672 else
673 value = 0;
674
675 spin_lock_irqsave(&wdata->state.lock, flags);
676 wiiproto_req_rumble(wdata, value);
677 spin_unlock_irqrestore(&wdata->state.lock, flags);
678
David Herrmann672bc4e2011-07-05 13:45:11 +0200679 return 0;
680}
681
David Herrmann98a558a2011-09-06 13:50:28 +0200682static int wiimote_accel_open(struct input_dev *dev)
683{
684 struct wiimote_data *wdata = input_get_drvdata(dev);
David Herrmann98a558a2011-09-06 13:50:28 +0200685 unsigned long flags;
686
David Herrmann98a558a2011-09-06 13:50:28 +0200687 spin_lock_irqsave(&wdata->state.lock, flags);
688 wiiproto_req_accel(wdata, true);
689 spin_unlock_irqrestore(&wdata->state.lock, flags);
690
691 return 0;
692}
693
694static void wiimote_accel_close(struct input_dev *dev)
695{
696 struct wiimote_data *wdata = input_get_drvdata(dev);
697 unsigned long flags;
698
699 spin_lock_irqsave(&wdata->state.lock, flags);
700 wiiproto_req_accel(wdata, false);
701 spin_unlock_irqrestore(&wdata->state.lock, flags);
David Herrmann98a558a2011-09-06 13:50:28 +0200702}
703
David Herrmann0370d7c2011-09-06 13:50:37 +0200704static int wiimote_ir_open(struct input_dev *dev)
705{
706 struct wiimote_data *wdata = input_get_drvdata(dev);
David Herrmann0370d7c2011-09-06 13:50:37 +0200707
David Herrmann5682b1a2013-05-05 23:12:47 +0200708 return wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
David Herrmann0370d7c2011-09-06 13:50:37 +0200709}
710
711static void wiimote_ir_close(struct input_dev *dev)
712{
713 struct wiimote_data *wdata = input_get_drvdata(dev);
714
715 wiimote_init_ir(wdata, 0);
David Herrmann0370d7c2011-09-06 13:50:37 +0200716}
717
David Herrmann27f06942013-05-05 23:12:51 +0200718/* device module handling */
719
720static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
721 [WIIMOTE_DEV_PENDING] = (const __u8[]){
722 WIIMOD_NULL,
723 },
724 [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
725 WIIMOD_NULL,
726 },
727 [WIIMOTE_DEV_GENERIC] = (const __u8[]){
728 WIIMOD_NULL,
729 },
730 [WIIMOTE_DEV_GEN10] = (const __u8[]){
731 WIIMOD_NULL,
732 },
733 [WIIMOTE_DEV_GEN20] = (const __u8[]){
734 WIIMOD_NULL,
735 },
736};
737
738static void wiimote_modules_load(struct wiimote_data *wdata,
739 unsigned int devtype)
740{
741 bool need_input = false;
742 const __u8 *mods, *iter;
743 const struct wiimod_ops *ops;
744 int ret;
745
746 mods = wiimote_devtype_mods[devtype];
747
748 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
749 if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
750 need_input = true;
751 break;
752 }
753 }
754
755 if (need_input) {
756 wdata->input = input_allocate_device();
757 if (!wdata->input)
758 return;
759
760 input_set_drvdata(wdata->input, wdata);
761 wdata->input->dev.parent = &wdata->hdev->dev;
762 wdata->input->id.bustype = wdata->hdev->bus;
763 wdata->input->id.vendor = wdata->hdev->vendor;
764 wdata->input->id.product = wdata->hdev->product;
765 wdata->input->id.version = wdata->hdev->version;
766 wdata->input->name = WIIMOTE_NAME;
767 }
768
769 for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
770 ops = wiimod_table[*iter];
771 if (!ops->probe)
772 continue;
773
774 ret = ops->probe(ops, wdata);
775 if (ret)
776 goto error;
777 }
778
779 if (wdata->input) {
780 ret = input_register_device(wdata->input);
781 if (ret)
782 goto error;
783 }
784
785 spin_lock_irq(&wdata->state.lock);
786 wdata->state.devtype = devtype;
787 spin_unlock_irq(&wdata->state.lock);
788 return;
789
790error:
791 for ( ; iter-- != mods; ) {
792 ops = wiimod_table[*iter];
793 if (ops->remove)
794 ops->remove(ops, wdata);
795 }
796
797 if (wdata->input) {
798 input_free_device(wdata->input);
799 wdata->input = NULL;
800 }
801}
802
803static void wiimote_modules_unload(struct wiimote_data *wdata)
804{
805 const __u8 *mods, *iter;
806 const struct wiimod_ops *ops;
807 unsigned long flags;
808
809 mods = wiimote_devtype_mods[wdata->state.devtype];
810
811 spin_lock_irqsave(&wdata->state.lock, flags);
812 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
813 spin_unlock_irqrestore(&wdata->state.lock, flags);
814
815 /* find end of list */
816 for (iter = mods; *iter != WIIMOD_NULL; ++iter)
817 /* empty */ ;
818
819 if (wdata->input) {
820 input_get_device(wdata->input);
821 input_unregister_device(wdata->input);
822 }
823
824 for ( ; iter-- != mods; ) {
825 ops = wiimod_table[*iter];
826 if (ops->remove)
827 ops->remove(ops, wdata);
828 }
829
830 if (wdata->input) {
831 input_put_device(wdata->input);
832 wdata->input = NULL;
833 }
834}
835
David Herrmannc57ff762013-05-05 23:12:48 +0200836/* device (re-)initialization and detection */
837
838static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
839 [WIIMOTE_DEV_PENDING] = "Pending",
840 [WIIMOTE_DEV_UNKNOWN] = "Unknown",
841 [WIIMOTE_DEV_GENERIC] = "Generic",
842 [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
843 [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
844};
845
846/* Try to guess the device type based on all collected information. We
847 * first try to detect by static extension types, then VID/PID and the
848 * device name. If we cannot detect the device, we use
849 * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
850static void wiimote_init_set_type(struct wiimote_data *wdata,
851 __u8 exttype)
852{
853 __u8 devtype = WIIMOTE_DEV_GENERIC;
854 __u16 vendor, product;
855 const char *name;
856
857 vendor = wdata->hdev->vendor;
858 product = wdata->hdev->product;
859 name = wdata->hdev->name;
860
861 if (!strcmp(name, "Nintendo RVL-CNT-01")) {
862 devtype = WIIMOTE_DEV_GEN10;
863 goto done;
864 } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
865 devtype = WIIMOTE_DEV_GEN20;
866 goto done;
867 }
868
869 if (vendor == USB_VENDOR_ID_NINTENDO) {
870 if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
871 devtype = WIIMOTE_DEV_GEN10;
872 goto done;
873 } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
874 devtype = WIIMOTE_DEV_GEN20;
875 goto done;
876 }
877 }
878
879done:
880 if (devtype == WIIMOTE_DEV_GENERIC)
881 hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
882 name, vendor, product, exttype);
883 else
884 hid_info(wdata->hdev, "detected device: %s\n",
885 wiimote_devtype_names[devtype]);
886
David Herrmann27f06942013-05-05 23:12:51 +0200887 wiimote_modules_load(wdata, devtype);
David Herrmannc57ff762013-05-05 23:12:48 +0200888}
889
890static void wiimote_init_detect(struct wiimote_data *wdata)
891{
892 __u8 exttype = WIIMOTE_EXT_NONE;
893 bool ext;
894 int ret;
895
896 wiimote_cmd_acquire_noint(wdata);
897
898 spin_lock_irq(&wdata->state.lock);
David Herrmann27f06942013-05-05 23:12:51 +0200899 wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
David Herrmannc57ff762013-05-05 23:12:48 +0200900 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
901 wiiproto_req_status(wdata);
902 spin_unlock_irq(&wdata->state.lock);
903
904 ret = wiimote_cmd_wait_noint(wdata);
905 if (ret)
906 goto out_release;
907
908 spin_lock_irq(&wdata->state.lock);
909 ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
910 spin_unlock_irq(&wdata->state.lock);
911
912 if (!ext)
913 goto out_release;
914
915 wiimote_cmd_init_ext(wdata);
916 exttype = wiimote_cmd_read_ext(wdata);
917
918out_release:
919 wiimote_cmd_release(wdata);
920 wiimote_init_set_type(wdata, exttype);
921}
922
923static void wiimote_init_worker(struct work_struct *work)
924{
925 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
926 init_worker);
927
928 if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
929 wiimote_init_detect(wdata);
930}
931
932/* protocol handlers */
933
David Herrmann1abb9ad2011-07-05 13:45:16 +0200934static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
935{
936 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
937 !!(payload[0] & 0x01));
938 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
939 !!(payload[0] & 0x02));
940 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
941 !!(payload[0] & 0x04));
942 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
943 !!(payload[0] & 0x08));
944 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
945 !!(payload[0] & 0x10));
946 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
947 !!(payload[1] & 0x01));
948 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
949 !!(payload[1] & 0x02));
950 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
951 !!(payload[1] & 0x04));
952 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
953 !!(payload[1] & 0x08));
954 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
955 !!(payload[1] & 0x10));
956 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
957 !!(payload[1] & 0x80));
958 input_sync(wdata->input);
959}
960
David Herrmannefcf9182011-09-06 13:50:29 +0200961static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
962{
963 __u16 x, y, z;
964
965 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
966 return;
967
968 /*
969 * payload is: BB BB XX YY ZZ
970 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
971 * contain the upper 8 bits of each value. The lower 2 bits are
972 * contained in the buttons data BB BB.
973 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
974 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
975 * accel value and bit 6 is the second bit of the Z value.
976 * The first bit of Y and Z values is not available and always set to 0.
977 * 0x200 is returned on no movement.
978 */
979
980 x = payload[2] << 2;
981 y = payload[3] << 2;
982 z = payload[4] << 2;
983
984 x |= (payload[0] >> 5) & 0x3;
985 y |= (payload[1] >> 4) & 0x2;
986 z |= (payload[1] >> 5) & 0x2;
987
988 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
989 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
990 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
991 input_sync(wdata->accel);
992}
993
David Herrmanneac39e72011-09-06 13:50:31 +0200994#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
995 ABS_HAT0X, ABS_HAT0Y)
996#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
997 ABS_HAT1X, ABS_HAT1Y)
998#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
999 ABS_HAT2X, ABS_HAT2Y)
1000#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
1001 ABS_HAT3X, ABS_HAT3Y)
1002
1003static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
1004 bool packed, __u8 xid, __u8 yid)
1005{
1006 __u16 x, y;
1007
1008 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
1009 return;
1010
1011 /*
1012 * Basic IR data is encoded into 3 bytes. The first two bytes are the
David Herrmann74b89e82012-05-08 16:52:31 +02001013 * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
David Herrmanneac39e72011-09-06 13:50:31 +02001014 * of both.
1015 * If data is packed, then the 3rd byte is put first and slightly
1016 * reordered. This allows to interleave packed and non-packed data to
1017 * have two IR sets in 5 bytes instead of 6.
1018 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
1019 */
1020
1021 if (packed) {
David Herrmann74b89e82012-05-08 16:52:31 +02001022 x = ir[1] | ((ir[0] & 0x03) << 8);
1023 y = ir[2] | ((ir[0] & 0x0c) << 6);
David Herrmanneac39e72011-09-06 13:50:31 +02001024 } else {
David Herrmann74b89e82012-05-08 16:52:31 +02001025 x = ir[0] | ((ir[2] & 0x30) << 4);
1026 y = ir[1] | ((ir[2] & 0xc0) << 2);
David Herrmanneac39e72011-09-06 13:50:31 +02001027 }
1028
1029 input_report_abs(wdata->ir, xid, x);
1030 input_report_abs(wdata->ir, yid, y);
1031}
David Herrmannefcf9182011-09-06 13:50:29 +02001032
David Herrmann2d44e3d2013-04-02 19:58:36 +02001033/* reduced status report with "BB BB" key data only */
1034static void handler_status_K(struct wiimote_data *wdata,
1035 const __u8 *payload)
David Herrmannc87019e2011-08-17 11:43:24 +02001036{
1037 handler_keys(wdata, payload);
1038
1039 /* on status reports the drm is reset so we need to resend the drm */
1040 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001041}
1042
1043/* extended status report with "BB BB LF 00 00 VV" data */
1044static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
1045{
1046 handler_status_K(wdata, payload);
David Herrmanne3979a92011-09-06 13:50:38 +02001047
David Herrmannc57ff762013-05-05 23:12:48 +02001048 /* update extension status */
1049 if (payload[2] & 0x02) {
1050 wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
1051 wiiext_event(wdata, true);
1052 } else {
1053 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
1054 wiiext_event(wdata, false);
1055 }
David Herrmanncb992212011-11-17 14:12:01 +01001056
David Herrmann6b80bb92013-05-05 23:12:49 +02001057 wdata->state.cmd_battery = payload[5];
1058 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
David Herrmanne3979a92011-09-06 13:50:38 +02001059 wiimote_cmd_complete(wdata);
David Herrmannc87019e2011-08-17 11:43:24 +02001060}
1061
David Herrmann2d44e3d2013-04-02 19:58:36 +02001062/* reduced generic report with "BB BB" key data only */
1063static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
1064{
1065 handler_keys(wdata, payload);
1066}
1067
David Herrmannbe1ecd62011-09-06 13:50:33 +02001068static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
1069{
David Herrmannfad8c0e2011-11-17 14:12:00 +01001070 __u16 offset = payload[3] << 8 | payload[4];
1071 __u8 size = (payload[2] >> 4) + 1;
1072 __u8 err = payload[2] & 0x0f;
1073
David Herrmannbe1ecd62011-09-06 13:50:33 +02001074 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +01001075
1076 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
1077 if (err)
1078 size = 0;
1079 else if (size > wdata->state.cmd_read_size)
1080 size = wdata->state.cmd_read_size;
1081
1082 wdata->state.cmd_read_size = size;
1083 if (wdata->state.cmd_read_buf)
1084 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
1085 wiimote_cmd_complete(wdata);
1086 }
David Herrmannbe1ecd62011-09-06 13:50:33 +02001087}
1088
David Herrmannc87019e2011-08-17 11:43:24 +02001089static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
1090{
1091 __u8 err = payload[3];
1092 __u8 cmd = payload[2];
1093
1094 handler_keys(wdata, payload);
1095
David Herrmann33e84012011-09-06 13:50:35 +02001096 if (wiimote_cmd_pending(wdata, cmd, 0)) {
1097 wdata->state.cmd_err = err;
1098 wiimote_cmd_complete(wdata);
1099 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +02001100 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
1101 cmd);
David Herrmann33e84012011-09-06 13:50:35 +02001102 }
David Herrmannc87019e2011-08-17 11:43:24 +02001103}
1104
David Herrmannefcf9182011-09-06 13:50:29 +02001105static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
1106{
1107 handler_keys(wdata, payload);
1108 handler_accel(wdata, payload);
1109}
1110
David Herrmann7336b9f2011-09-06 13:50:32 +02001111static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
1112{
1113 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +01001114 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +02001115}
1116
David Herrmannefcf9182011-09-06 13:50:29 +02001117static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
1118{
1119 handler_keys(wdata, payload);
1120 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +02001121 ir_to_input0(wdata, &payload[5], false);
1122 ir_to_input1(wdata, &payload[8], false);
1123 ir_to_input2(wdata, &payload[11], false);
1124 ir_to_input3(wdata, &payload[14], false);
1125 input_sync(wdata->ir);
1126}
1127
David Herrmann7336b9f2011-09-06 13:50:32 +02001128static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
1129{
1130 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +01001131 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +02001132}
1133
David Herrmanneac39e72011-09-06 13:50:31 +02001134static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
1135{
1136 handler_keys(wdata, payload);
1137 ir_to_input0(wdata, &payload[2], false);
1138 ir_to_input1(wdata, &payload[4], true);
1139 ir_to_input2(wdata, &payload[7], false);
1140 ir_to_input3(wdata, &payload[9], true);
1141 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +01001142 wiiext_handle(wdata, &payload[12]);
David Herrmannefcf9182011-09-06 13:50:29 +02001143}
1144
1145static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
1146{
1147 handler_keys(wdata, payload);
1148 handler_accel(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +01001149 wiiext_handle(wdata, &payload[5]);
David Herrmannefcf9182011-09-06 13:50:29 +02001150}
1151
1152static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
1153{
1154 handler_keys(wdata, payload);
1155 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +02001156 ir_to_input0(wdata, &payload[5], false);
1157 ir_to_input1(wdata, &payload[7], true);
1158 ir_to_input2(wdata, &payload[10], false);
1159 ir_to_input3(wdata, &payload[12], true);
1160 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +01001161 wiiext_handle(wdata, &payload[15]);
David Herrmannefcf9182011-09-06 13:50:29 +02001162}
1163
David Herrmann7336b9f2011-09-06 13:50:32 +02001164static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
1165{
David Herrmann0b6815d2011-11-17 14:12:06 +01001166 wiiext_handle(wdata, payload);
David Herrmann7336b9f2011-09-06 13:50:32 +02001167}
1168
David Herrmannefcf9182011-09-06 13:50:29 +02001169static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
1170{
1171 handler_keys(wdata, payload);
1172
1173 wdata->state.accel_split[0] = payload[2];
1174 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
1175 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +02001176
1177 ir_to_input0(wdata, &payload[3], false);
1178 ir_to_input1(wdata, &payload[12], false);
1179 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +02001180}
1181
1182static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
1183{
1184 __u8 buf[5];
1185
1186 handler_keys(wdata, payload);
1187
1188 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
1189 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
1190
1191 buf[0] = 0;
1192 buf[1] = 0;
1193 buf[2] = wdata->state.accel_split[0];
1194 buf[3] = payload[2];
1195 buf[4] = wdata->state.accel_split[1];
1196 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +02001197
1198 ir_to_input2(wdata, &payload[3], false);
1199 ir_to_input3(wdata, &payload[12], false);
1200 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +02001201}
1202
David Herrmanna4d19192011-07-05 13:45:15 +02001203struct wiiproto_handler {
1204 __u8 id;
1205 size_t size;
1206 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
1207};
1208
1209static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +02001210 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001211 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
David Herrmannbe1ecd62011-09-06 13:50:33 +02001212 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001213 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
David Herrmannc87019e2011-08-17 11:43:24 +02001214 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001215 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
David Herrmann1abb9ad2011-07-05 13:45:16 +02001216 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +02001217 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001218 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001219 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001220 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001221 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001222 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001223 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001224 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001225 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001226 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
David Herrmanneac39e72011-09-06 13:50:31 +02001227 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001228 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +02001229 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +02001230 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +02001231 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +02001232 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
1233 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +02001234 { .id = 0 }
1235};
1236
David Herrmann02fb72a2011-07-05 13:45:09 +02001237static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
1238 u8 *raw_data, int size)
1239{
David Herrmann4d36e972011-07-05 13:45:12 +02001240 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +02001241 struct wiiproto_handler *h;
1242 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001243 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +02001244
David Herrmann02fb72a2011-07-05 13:45:09 +02001245 if (size < 1)
1246 return -EINVAL;
1247
David Herrmann32a0d9a2011-07-05 13:45:18 +02001248 spin_lock_irqsave(&wdata->state.lock, flags);
1249
David Herrmanna4d19192011-07-05 13:45:15 +02001250 for (i = 0; handlers[i].id; ++i) {
1251 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +02001252 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +02001253 h->func(wdata, &raw_data[1]);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001254 break;
David Herrmann7336b9f2011-09-06 13:50:32 +02001255 }
David Herrmanna4d19192011-07-05 13:45:15 +02001256 }
1257
David Herrmann2d44e3d2013-04-02 19:58:36 +02001258 if (!handlers[i].id)
David Herrmann7336b9f2011-09-06 13:50:32 +02001259 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1260 size);
1261
David Herrmann32a0d9a2011-07-05 13:45:18 +02001262 spin_unlock_irqrestore(&wdata->state.lock, flags);
1263
David Herrmann02fb72a2011-07-05 13:45:09 +02001264 return 0;
1265}
1266
David Herrmann23a5a4a2011-08-17 11:43:22 +02001267static void wiimote_leds_destroy(struct wiimote_data *wdata)
1268{
1269 int i;
1270 struct led_classdev *led;
1271
1272 for (i = 0; i < 4; ++i) {
1273 if (wdata->leds[i]) {
1274 led = wdata->leds[i];
1275 wdata->leds[i] = NULL;
1276 led_classdev_unregister(led);
1277 kfree(led);
1278 }
1279 }
1280}
1281
1282static int wiimote_leds_create(struct wiimote_data *wdata)
1283{
1284 int i, ret;
1285 struct device *dev = &wdata->hdev->dev;
1286 size_t namesz = strlen(dev_name(dev)) + 9;
1287 struct led_classdev *led;
1288 char *name;
1289
1290 for (i = 0; i < 4; ++i) {
1291 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1292 if (!led) {
1293 ret = -ENOMEM;
1294 goto err;
1295 }
1296 name = (void*)&led[1];
1297 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1298 led->name = name;
1299 led->brightness = 0;
1300 led->max_brightness = 1;
1301 led->brightness_get = wiimote_leds_get;
1302 led->brightness_set = wiimote_leds_set;
1303
1304 ret = led_classdev_register(dev, led);
1305 if (ret) {
1306 kfree(led);
1307 goto err;
1308 }
1309 wdata->leds[i] = led;
1310 }
1311
1312 return 0;
1313
1314err:
1315 wiimote_leds_destroy(wdata);
1316 return ret;
1317}
1318
David Herrmanne894d0e2011-07-05 13:45:10 +02001319static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1320{
1321 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001322 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001323
1324 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1325 if (!wdata)
1326 return NULL;
1327
David Herrmann672bc4e2011-07-05 13:45:11 +02001328 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001329 if (!wdata->input)
1330 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001331
David Herrmanne894d0e2011-07-05 13:45:10 +02001332 wdata->hdev = hdev;
1333 hid_set_drvdata(hdev, wdata);
1334
David Herrmann672bc4e2011-07-05 13:45:11 +02001335 input_set_drvdata(wdata->input, wdata);
David Herrmann672bc4e2011-07-05 13:45:11 +02001336 wdata->input->dev.parent = &wdata->hdev->dev;
1337 wdata->input->id.bustype = wdata->hdev->bus;
1338 wdata->input->id.vendor = wdata->hdev->vendor;
1339 wdata->input->id.product = wdata->hdev->product;
1340 wdata->input->id.version = wdata->hdev->version;
1341 wdata->input->name = WIIMOTE_NAME;
1342
David Herrmann1abb9ad2011-07-05 13:45:16 +02001343 set_bit(EV_KEY, wdata->input->evbit);
1344 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1345 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1346
David Herrmannd020be92011-09-06 13:50:27 +02001347 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001348 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1349 goto err_input;
1350
1351 wdata->accel = input_allocate_device();
1352 if (!wdata->accel)
1353 goto err_input;
1354
1355 input_set_drvdata(wdata->accel, wdata);
1356 wdata->accel->open = wiimote_accel_open;
1357 wdata->accel->close = wiimote_accel_close;
1358 wdata->accel->dev.parent = &wdata->hdev->dev;
1359 wdata->accel->id.bustype = wdata->hdev->bus;
1360 wdata->accel->id.vendor = wdata->hdev->vendor;
1361 wdata->accel->id.product = wdata->hdev->product;
1362 wdata->accel->id.version = wdata->hdev->version;
1363 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1364
1365 set_bit(EV_ABS, wdata->accel->evbit);
1366 set_bit(ABS_RX, wdata->accel->absbit);
1367 set_bit(ABS_RY, wdata->accel->absbit);
1368 set_bit(ABS_RZ, wdata->accel->absbit);
1369 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1370 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1371 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001372
David Herrmannf363e4f2011-09-06 13:50:30 +02001373 wdata->ir = input_allocate_device();
1374 if (!wdata->ir)
1375 goto err_ir;
1376
1377 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001378 wdata->ir->open = wiimote_ir_open;
1379 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001380 wdata->ir->dev.parent = &wdata->hdev->dev;
1381 wdata->ir->id.bustype = wdata->hdev->bus;
1382 wdata->ir->id.vendor = wdata->hdev->vendor;
1383 wdata->ir->id.product = wdata->hdev->product;
1384 wdata->ir->id.version = wdata->hdev->version;
1385 wdata->ir->name = WIIMOTE_NAME " IR";
1386
1387 set_bit(EV_ABS, wdata->ir->evbit);
1388 set_bit(ABS_HAT0X, wdata->ir->absbit);
1389 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1390 set_bit(ABS_HAT1X, wdata->ir->absbit);
1391 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1392 set_bit(ABS_HAT2X, wdata->ir->absbit);
1393 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1394 set_bit(ABS_HAT3X, wdata->ir->absbit);
1395 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1396 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1397 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1398 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1399 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1400 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1401 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1402 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1403 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1404
David Herrmann13938532013-05-05 23:12:46 +02001405 spin_lock_init(&wdata->queue.lock);
1406 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
David Herrmann23c063c2011-07-05 13:45:14 +02001407
David Herrmann32a0d9a2011-07-05 13:45:18 +02001408 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001409 init_completion(&wdata->state.ready);
1410 mutex_init(&wdata->state.sync);
David Herrmann43d782a2011-11-17 14:12:12 +01001411 wdata->state.drm = WIIPROTO_REQ_DRM_K;
David Herrmann6b80bb92013-05-05 23:12:49 +02001412 wdata->state.cmd_battery = 0xff;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001413
David Herrmannc57ff762013-05-05 23:12:48 +02001414 INIT_WORK(&wdata->init_worker, wiimote_init_worker);
1415
David Herrmanne894d0e2011-07-05 13:45:10 +02001416 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001417
David Herrmannf363e4f2011-09-06 13:50:30 +02001418err_ir:
1419 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001420err_input:
1421 input_free_device(wdata->input);
1422err:
1423 kfree(wdata);
1424 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001425}
1426
1427static void wiimote_destroy(struct wiimote_data *wdata)
1428{
David Herrmann43e5e7c2011-11-17 14:12:10 +01001429 wiidebug_deinit(wdata);
David Herrmanncb992212011-11-17 14:12:01 +01001430 wiiext_deinit(wdata);
David Herrmann23a5a4a2011-08-17 11:43:22 +02001431 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001432
David Herrmann27f06942013-05-05 23:12:51 +02001433 wiimote_modules_unload(wdata);
David Herrmann6591d752011-09-06 13:50:39 +02001434 power_supply_unregister(&wdata->battery);
David Herrmann001dac82013-04-02 19:58:34 +02001435 kfree(wdata->battery.name);
David Herrmann98a558a2011-09-06 13:50:28 +02001436 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001437 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001438 input_unregister_device(wdata->input);
David Herrmannc57ff762013-05-05 23:12:48 +02001439 cancel_work_sync(&wdata->init_worker);
David Herrmann13938532013-05-05 23:12:46 +02001440 cancel_work_sync(&wdata->queue.worker);
David Herrmann5682b1a2013-05-05 23:12:47 +02001441 hid_hw_close(wdata->hdev);
David Herrmann3989ef62011-08-17 11:43:20 +02001442 hid_hw_stop(wdata->hdev);
1443
David Herrmanne894d0e2011-07-05 13:45:10 +02001444 kfree(wdata);
1445}
1446
David Herrmann02fb72a2011-07-05 13:45:09 +02001447static int wiimote_hid_probe(struct hid_device *hdev,
1448 const struct hid_device_id *id)
1449{
David Herrmanne894d0e2011-07-05 13:45:10 +02001450 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001451 int ret;
1452
David Herrmann90120d62011-11-17 14:12:14 +01001453 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1454
David Herrmanne894d0e2011-07-05 13:45:10 +02001455 wdata = wiimote_create(hdev);
1456 if (!wdata) {
1457 hid_err(hdev, "Can't alloc device\n");
1458 return -ENOMEM;
1459 }
1460
David Herrmann02fb72a2011-07-05 13:45:09 +02001461 ret = hid_parse(hdev);
1462 if (ret) {
1463 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001464 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001465 }
1466
1467 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1468 if (ret) {
1469 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001470 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001471 }
1472
David Herrmann5682b1a2013-05-05 23:12:47 +02001473 ret = hid_hw_open(hdev);
1474 if (ret) {
1475 hid_err(hdev, "cannot start hardware I/O\n");
1476 goto err_stop;
1477 }
1478
David Herrmann98a558a2011-09-06 13:50:28 +02001479 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001480 if (ret) {
1481 hid_err(hdev, "Cannot register input device\n");
David Herrmann5682b1a2013-05-05 23:12:47 +02001482 goto err_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001483 }
1484
David Herrmannf363e4f2011-09-06 13:50:30 +02001485 ret = input_register_device(wdata->ir);
1486 if (ret) {
1487 hid_err(hdev, "Cannot register input device\n");
1488 goto err_ir;
1489 }
1490
David Herrmann98a558a2011-09-06 13:50:28 +02001491 ret = input_register_device(wdata->input);
1492 if (ret) {
1493 hid_err(hdev, "Cannot register input device\n");
1494 goto err_input;
1495 }
1496
David Herrmann6591d752011-09-06 13:50:39 +02001497 wdata->battery.properties = wiimote_battery_props;
1498 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1499 wdata->battery.get_property = wiimote_battery_get_property;
David Herrmann6591d752011-09-06 13:50:39 +02001500 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1501 wdata->battery.use_for_apm = 0;
David Herrmann001dac82013-04-02 19:58:34 +02001502 wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
1503 wdata->hdev->uniq);
1504 if (!wdata->battery.name) {
1505 ret = -ENOMEM;
1506 goto err_battery_name;
1507 }
David Herrmann6591d752011-09-06 13:50:39 +02001508
1509 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1510 if (ret) {
1511 hid_err(hdev, "Cannot register battery device\n");
1512 goto err_battery;
1513 }
1514
Jiri Kosina217c8b22012-02-07 13:40:56 +01001515 power_supply_powers(&wdata->battery, &hdev->dev);
1516
David Herrmann23a5a4a2011-08-17 11:43:22 +02001517 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001518 if (ret)
1519 goto err_free;
1520
David Herrmanncb992212011-11-17 14:12:01 +01001521 ret = wiiext_init(wdata);
1522 if (ret)
1523 goto err_free;
1524
David Herrmann43e5e7c2011-11-17 14:12:10 +01001525 ret = wiidebug_init(wdata);
1526 if (ret)
1527 goto err_free;
1528
David Herrmann02fb72a2011-07-05 13:45:09 +02001529 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001530
1531 /* by default set led1 after device initialization */
1532 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001533 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001534 spin_unlock_irq(&wdata->state.lock);
1535
David Herrmannc57ff762013-05-05 23:12:48 +02001536 /* schedule device detection */
1537 schedule_work(&wdata->init_worker);
1538
David Herrmann02fb72a2011-07-05 13:45:09 +02001539 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001540
David Herrmann3989ef62011-08-17 11:43:20 +02001541err_free:
1542 wiimote_destroy(wdata);
1543 return ret;
1544
David Herrmann6591d752011-09-06 13:50:39 +02001545err_battery:
David Herrmann001dac82013-04-02 19:58:34 +02001546 kfree(wdata->battery.name);
1547err_battery_name:
David Herrmann6591d752011-09-06 13:50:39 +02001548 input_unregister_device(wdata->input);
1549 wdata->input = NULL;
David Herrmann98a558a2011-09-06 13:50:28 +02001550err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001551 input_unregister_device(wdata->ir);
1552 wdata->ir = NULL;
1553err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001554 input_unregister_device(wdata->accel);
1555 wdata->accel = NULL;
David Herrmann5682b1a2013-05-05 23:12:47 +02001556err_close:
1557 hid_hw_close(hdev);
David Herrmann672bc4e2011-07-05 13:45:11 +02001558err_stop:
1559 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001560err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001561 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001562 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001563 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001564 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001565 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001566}
1567
1568static void wiimote_hid_remove(struct hid_device *hdev)
1569{
David Herrmanne894d0e2011-07-05 13:45:10 +02001570 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1571
David Herrmann02fb72a2011-07-05 13:45:09 +02001572 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001573 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001574}
1575
1576static const struct hid_device_id wiimote_hid_devices[] = {
1577 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1578 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
David Herrmanna33042f2013-04-02 19:58:35 +02001579 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1580 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
David Herrmann02fb72a2011-07-05 13:45:09 +02001581 { }
1582};
1583MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1584
1585static struct hid_driver wiimote_hid_driver = {
1586 .name = "wiimote",
1587 .id_table = wiimote_hid_devices,
1588 .probe = wiimote_hid_probe,
1589 .remove = wiimote_hid_remove,
1590 .raw_event = wiimote_hid_event,
1591};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001592module_hid_driver(wiimote_hid_driver);
David Herrmann02fb72a2011-07-05 13:45:09 +02001593
David Herrmannfb51b442011-07-05 13:45:08 +02001594MODULE_LICENSE("GPL");
1595MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
David Herrmann92eda7e2013-05-05 23:12:45 +02001596MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");