blob: 2d85d3a8b60a2b767162bb30f9952f5316afda29 [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 Herrmann0c218f12011-07-05 13:45:13 +020061static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
62 size_t count)
63{
64 __u8 *buf;
65 ssize_t ret;
66
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;
87
David Herrmann13938532013-05-05 23:12:46 +020088 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020089
David Herrmann13938532013-05-05 23:12:46 +020090 while (wdata->queue.head != wdata->queue.tail) {
91 spin_unlock_irqrestore(&wdata->queue.lock, flags);
92 wiimote_hid_send(wdata->hdev,
93 wdata->queue.outq[wdata->queue.tail].data,
94 wdata->queue.outq[wdata->queue.tail].size);
95 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +020096
David Herrmann13938532013-05-05 23:12:46 +020097 wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
David Herrmann23c063c2011-07-05 13:45:14 +020098 }
99
David Herrmann13938532013-05-05 23:12:46 +0200100 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200101}
102
103static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
104 size_t count)
105{
106 unsigned long flags;
107 __u8 newhead;
108
109 if (count > HID_MAX_BUFFER_SIZE) {
110 hid_warn(wdata->hdev, "Sending too large output report\n");
111 return;
112 }
113
114 /*
115 * Copy new request into our output queue and check whether the
116 * queue is full. If it is full, discard this request.
117 * If it is empty we need to start a new worker that will
118 * send out the buffer to the hid device.
119 * If the queue is not empty, then there must be a worker
120 * that is currently sending out our buffer and this worker
121 * will reschedule itself until the queue is empty.
122 */
123
David Herrmann13938532013-05-05 23:12:46 +0200124 spin_lock_irqsave(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200125
David Herrmann13938532013-05-05 23:12:46 +0200126 memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
127 wdata->queue.outq[wdata->queue.head].size = count;
128 newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
David Herrmann23c063c2011-07-05 13:45:14 +0200129
David Herrmann13938532013-05-05 23:12:46 +0200130 if (wdata->queue.head == wdata->queue.tail) {
131 wdata->queue.head = newhead;
132 schedule_work(&wdata->queue.worker);
133 } else if (newhead != wdata->queue.tail) {
134 wdata->queue.head = newhead;
David Herrmann23c063c2011-07-05 13:45:14 +0200135 } else {
136 hid_warn(wdata->hdev, "Output queue is full");
137 }
138
David Herrmann13938532013-05-05 23:12:46 +0200139 spin_unlock_irqrestore(&wdata->queue.lock, flags);
David Herrmann23c063c2011-07-05 13:45:14 +0200140}
141
David Herrmannc003ec22011-09-06 13:50:26 +0200142/*
143 * This sets the rumble bit on the given output report if rumble is
144 * currently enabled.
145 * \cmd1 must point to the second byte in the output report => &cmd[1]
146 * This must be called on nearly every output report before passing it
147 * into the output queue!
148 */
149static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
150{
151 if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
152 *cmd1 |= 0x01;
153}
154
155static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
156{
157 __u8 cmd[2];
158
159 rumble = !!rumble;
160 if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
161 return;
162
163 if (rumble)
164 wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
165 else
166 wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
167
168 cmd[0] = WIIPROTO_REQ_RUMBLE;
169 cmd[1] = 0;
170
171 wiiproto_keep_rumble(wdata, &cmd[1]);
172 wiimote_queue(wdata, cmd, sizeof(cmd));
173}
174
David Herrmanndb308342011-07-05 13:45:17 +0200175static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
176{
177 __u8 cmd[2];
178
David Herrmann32a0d9a2011-07-05 13:45:18 +0200179 leds &= WIIPROTO_FLAGS_LEDS;
180 if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
181 return;
182 wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
183
David Herrmanndb308342011-07-05 13:45:17 +0200184 cmd[0] = WIIPROTO_REQ_LED;
185 cmd[1] = 0;
186
187 if (leds & WIIPROTO_FLAG_LED1)
188 cmd[1] |= 0x10;
189 if (leds & WIIPROTO_FLAG_LED2)
190 cmd[1] |= 0x20;
191 if (leds & WIIPROTO_FLAG_LED3)
192 cmd[1] |= 0x40;
193 if (leds & WIIPROTO_FLAG_LED4)
194 cmd[1] |= 0x80;
195
David Herrmannc003ec22011-09-06 13:50:26 +0200196 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmanndb308342011-07-05 13:45:17 +0200197 wiimote_queue(wdata, cmd, sizeof(cmd));
198}
199
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200200/*
201 * Check what peripherals of the wiimote are currently
202 * active and select a proper DRM that supports all of
203 * the requested data inputs.
204 */
205static __u8 select_drm(struct wiimote_data *wdata)
206{
David Herrmannf363e4f2011-09-06 13:50:30 +0200207 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
David Herrmanncb992212011-11-17 14:12:01 +0100208 bool ext = wiiext_active(wdata);
David Herrmannf363e4f2011-09-06 13:50:30 +0200209
210 if (ir == WIIPROTO_FLAG_IR_BASIC) {
211 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
212 return WIIPROTO_REQ_DRM_KAIE;
213 else
214 return WIIPROTO_REQ_DRM_KIE;
215 } else if (ir == WIIPROTO_FLAG_IR_EXT) {
216 return WIIPROTO_REQ_DRM_KAI;
217 } else if (ir == WIIPROTO_FLAG_IR_FULL) {
218 return WIIPROTO_REQ_DRM_SKAI1;
219 } else {
David Herrmanncb992212011-11-17 14:12:01 +0100220 if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
221 if (ext)
222 return WIIPROTO_REQ_DRM_KAE;
223 else
224 return WIIPROTO_REQ_DRM_KA;
225 } else {
226 if (ext)
227 return WIIPROTO_REQ_DRM_KE;
228 else
229 return WIIPROTO_REQ_DRM_K;
230 }
David Herrmannf363e4f2011-09-06 13:50:30 +0200231 }
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200232}
233
David Herrmann7e274402011-11-17 14:11:59 +0100234void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200235{
236 __u8 cmd[3];
237
238 if (drm == WIIPROTO_REQ_NULL)
239 drm = select_drm(wdata);
240
241 cmd[0] = WIIPROTO_REQ_DRM;
242 cmd[1] = 0;
243 cmd[2] = drm;
244
David Herrmann43d782a2011-11-17 14:12:12 +0100245 wdata->state.drm = drm;
David Herrmannc003ec22011-09-06 13:50:26 +0200246 wiiproto_keep_rumble(wdata, &cmd[1]);
David Herrmann2cb5e4b2011-08-17 11:43:23 +0200247 wiimote_queue(wdata, cmd, sizeof(cmd));
248}
249
David Herrmanne3979a92011-09-06 13:50:38 +0200250static void wiiproto_req_status(struct wiimote_data *wdata)
251{
252 __u8 cmd[2];
253
254 cmd[0] = WIIPROTO_REQ_SREQ;
255 cmd[1] = 0;
256
257 wiiproto_keep_rumble(wdata, &cmd[1]);
258 wiimote_queue(wdata, cmd, sizeof(cmd));
259}
260
David Herrmann98a558a2011-09-06 13:50:28 +0200261static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
262{
263 accel = !!accel;
264 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
265 return;
266
267 if (accel)
268 wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
269 else
270 wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
271
272 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
273}
274
David Herrmannfc221cd2011-09-06 13:50:36 +0200275static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
276{
277 __u8 cmd[2];
278
279 cmd[0] = WIIPROTO_REQ_IR1;
280 cmd[1] = flags;
281
282 wiiproto_keep_rumble(wdata, &cmd[1]);
283 wiimote_queue(wdata, cmd, sizeof(cmd));
284}
285
286static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
287{
288 __u8 cmd[2];
289
290 cmd[0] = WIIPROTO_REQ_IR2;
291 cmd[1] = flags;
292
293 wiiproto_keep_rumble(wdata, &cmd[1]);
294 wiimote_queue(wdata, cmd, sizeof(cmd));
295}
296
David Herrmannbe1ecd62011-09-06 13:50:33 +0200297#define wiiproto_req_wreg(wdata, os, buf, sz) \
298 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
299
300#define wiiproto_req_weeprom(wdata, os, buf, sz) \
301 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
302
303static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
304 __u32 offset, const __u8 *buf, __u8 size)
305{
306 __u8 cmd[22];
307
308 if (size > 16 || size == 0) {
309 hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
310 return;
311 }
312
313 memset(cmd, 0, sizeof(cmd));
314 cmd[0] = WIIPROTO_REQ_WMEM;
315 cmd[2] = (offset >> 16) & 0xff;
316 cmd[3] = (offset >> 8) & 0xff;
317 cmd[4] = offset & 0xff;
318 cmd[5] = size;
319 memcpy(&cmd[6], buf, size);
320
321 if (!eeprom)
322 cmd[1] |= 0x04;
323
324 wiiproto_keep_rumble(wdata, &cmd[1]);
325 wiimote_queue(wdata, cmd, sizeof(cmd));
326}
327
David Herrmann1d3452c2011-11-17 14:12:11 +0100328void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
329 __u16 size)
David Herrmannfad8c0e2011-11-17 14:12:00 +0100330{
331 __u8 cmd[7];
332
333 if (size == 0) {
334 hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
335 return;
336 }
337
338 cmd[0] = WIIPROTO_REQ_RMEM;
339 cmd[1] = 0;
340 cmd[2] = (offset >> 16) & 0xff;
341 cmd[3] = (offset >> 8) & 0xff;
342 cmd[4] = offset & 0xff;
343 cmd[5] = (size >> 8) & 0xff;
344 cmd[6] = size & 0xff;
345
346 if (!eeprom)
347 cmd[1] |= 0x04;
348
349 wiiproto_keep_rumble(wdata, &cmd[1]);
350 wiimote_queue(wdata, cmd, sizeof(cmd));
351}
352
David Herrmann33e84012011-09-06 13:50:35 +0200353/* requries the cmd-mutex to be held */
David Herrmann7e274402011-11-17 14:11:59 +0100354int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
David Herrmann33e84012011-09-06 13:50:35 +0200355 const __u8 *wmem, __u8 size)
356{
357 unsigned long flags;
358 int ret;
359
360 spin_lock_irqsave(&wdata->state.lock, flags);
361 wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
362 wiiproto_req_wreg(wdata, offset, wmem, size);
363 spin_unlock_irqrestore(&wdata->state.lock, flags);
364
365 ret = wiimote_cmd_wait(wdata);
366 if (!ret && wdata->state.cmd_err)
367 ret = -EIO;
368
369 return ret;
370}
371
David Herrmannfad8c0e2011-11-17 14:12:00 +0100372/* requries the cmd-mutex to be held */
373ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
374 __u8 size)
375{
376 unsigned long flags;
377 ssize_t ret;
378
379 spin_lock_irqsave(&wdata->state.lock, flags);
380 wdata->state.cmd_read_size = size;
381 wdata->state.cmd_read_buf = rmem;
382 wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
383 wiiproto_req_rreg(wdata, offset, size);
384 spin_unlock_irqrestore(&wdata->state.lock, flags);
385
386 ret = wiimote_cmd_wait(wdata);
387
388 spin_lock_irqsave(&wdata->state.lock, flags);
389 wdata->state.cmd_read_buf = NULL;
390 spin_unlock_irqrestore(&wdata->state.lock, flags);
391
392 if (!ret) {
393 if (wdata->state.cmd_read_size == 0)
394 ret = -EIO;
395 else
396 ret = wdata->state.cmd_read_size;
397 }
398
399 return ret;
400}
401
David Herrmann6591d752011-09-06 13:50:39 +0200402static int wiimote_battery_get_property(struct power_supply *psy,
403 enum power_supply_property psp,
404 union power_supply_propval *val)
405{
406 struct wiimote_data *wdata = container_of(psy,
407 struct wiimote_data, battery);
408 int ret = 0, state;
409 unsigned long flags;
410
Jeremy Fitzhardinge73db8812011-12-07 11:29:46 -0800411 if (psp == POWER_SUPPLY_PROP_SCOPE) {
412 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
413 return 0;
414 }
415
David Herrmann6591d752011-09-06 13:50:39 +0200416 ret = wiimote_cmd_acquire(wdata);
417 if (ret)
418 return ret;
419
420 spin_lock_irqsave(&wdata->state.lock, flags);
421 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
422 wiiproto_req_status(wdata);
423 spin_unlock_irqrestore(&wdata->state.lock, flags);
424
425 ret = wiimote_cmd_wait(wdata);
426 state = wdata->state.cmd_battery;
427 wiimote_cmd_release(wdata);
428
429 if (ret)
430 return ret;
431
432 switch (psp) {
433 case POWER_SUPPLY_PROP_CAPACITY:
434 val->intval = state * 100 / 255;
435 break;
436 default:
437 ret = -EINVAL;
438 break;
439 }
440
441 return ret;
442}
443
David Herrmannfc221cd2011-09-06 13:50:36 +0200444static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
445{
446 int ret;
447 unsigned long flags;
448 __u8 format = 0;
449 static const __u8 data_enable[] = { 0x01 };
450 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
451 0x00, 0xaa, 0x00, 0x64 };
452 static const __u8 data_sens2[] = { 0x63, 0x03 };
453 static const __u8 data_fin[] = { 0x08 };
454
455 spin_lock_irqsave(&wdata->state.lock, flags);
456
457 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
458 spin_unlock_irqrestore(&wdata->state.lock, flags);
459 return 0;
460 }
461
462 if (mode == 0) {
463 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
464 wiiproto_req_ir1(wdata, 0);
465 wiiproto_req_ir2(wdata, 0);
466 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
467 spin_unlock_irqrestore(&wdata->state.lock, flags);
468 return 0;
469 }
470
471 spin_unlock_irqrestore(&wdata->state.lock, flags);
472
473 ret = wiimote_cmd_acquire(wdata);
474 if (ret)
475 return ret;
476
477 /* send PIXEL CLOCK ENABLE cmd first */
478 spin_lock_irqsave(&wdata->state.lock, flags);
479 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
480 wiiproto_req_ir1(wdata, 0x06);
481 spin_unlock_irqrestore(&wdata->state.lock, flags);
482
483 ret = wiimote_cmd_wait(wdata);
484 if (ret)
485 goto unlock;
486 if (wdata->state.cmd_err) {
487 ret = -EIO;
488 goto unlock;
489 }
490
491 /* enable IR LOGIC */
492 spin_lock_irqsave(&wdata->state.lock, flags);
493 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
494 wiiproto_req_ir2(wdata, 0x06);
495 spin_unlock_irqrestore(&wdata->state.lock, flags);
496
497 ret = wiimote_cmd_wait(wdata);
498 if (ret)
499 goto unlock;
500 if (wdata->state.cmd_err) {
501 ret = -EIO;
502 goto unlock;
503 }
504
505 /* enable IR cam but do not make it send data, yet */
506 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
507 sizeof(data_enable));
508 if (ret)
509 goto unlock;
510
511 /* write first sensitivity block */
512 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
513 sizeof(data_sens1));
514 if (ret)
515 goto unlock;
516
517 /* write second sensitivity block */
518 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
519 sizeof(data_sens2));
520 if (ret)
521 goto unlock;
522
523 /* put IR cam into desired state */
524 switch (mode) {
525 case WIIPROTO_FLAG_IR_FULL:
526 format = 5;
527 break;
528 case WIIPROTO_FLAG_IR_EXT:
529 format = 3;
530 break;
531 case WIIPROTO_FLAG_IR_BASIC:
532 format = 1;
533 break;
534 }
535 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
536 if (ret)
537 goto unlock;
538
539 /* make IR cam send data */
540 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
541 if (ret)
542 goto unlock;
543
544 /* request new DRM mode compatible to IR mode */
545 spin_lock_irqsave(&wdata->state.lock, flags);
546 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
547 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
548 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
549 spin_unlock_irqrestore(&wdata->state.lock, flags);
550
551unlock:
552 wiimote_cmd_release(wdata);
553 return ret;
554}
555
David Herrmann23a5a4a2011-08-17 11:43:22 +0200556static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
557{
558 struct wiimote_data *wdata;
559 struct device *dev = led_dev->dev->parent;
560 int i;
561 unsigned long flags;
562 bool value = false;
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200563
David Herrmann23a5a4a2011-08-17 11:43:22 +0200564 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
565
566 for (i = 0; i < 4; ++i) {
567 if (wdata->leds[i] == led_dev) {
568 spin_lock_irqsave(&wdata->state.lock, flags);
569 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
570 spin_unlock_irqrestore(&wdata->state.lock, flags);
571 break;
572 }
573 }
574
575 return value ? LED_FULL : LED_OFF;
576}
577
578static void wiimote_leds_set(struct led_classdev *led_dev,
579 enum led_brightness value)
580{
581 struct wiimote_data *wdata;
582 struct device *dev = led_dev->dev->parent;
583 int i;
584 unsigned long flags;
585 __u8 state, flag;
586
587 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
588
589 for (i = 0; i < 4; ++i) {
590 if (wdata->leds[i] == led_dev) {
591 flag = WIIPROTO_FLAG_LED(i + 1);
592 spin_lock_irqsave(&wdata->state.lock, flags);
593 state = wdata->state.flags;
594 if (value == LED_OFF)
595 wiiproto_req_leds(wdata, state & ~flag);
596 else
597 wiiproto_req_leds(wdata, state | flag);
598 spin_unlock_irqrestore(&wdata->state.lock, flags);
599 break;
600 }
601 }
602}
David Herrmann3c1c2fc2011-07-05 13:45:19 +0200603
David Herrmannd020be92011-09-06 13:50:27 +0200604static int wiimote_ff_play(struct input_dev *dev, void *data,
605 struct ff_effect *eff)
David Herrmann672bc4e2011-07-05 13:45:11 +0200606{
David Herrmannd020be92011-09-06 13:50:27 +0200607 struct wiimote_data *wdata = input_get_drvdata(dev);
608 __u8 value;
609 unsigned long flags;
610
611 /*
612 * The wiimote supports only a single rumble motor so if any magnitude
613 * is set to non-zero then we start the rumble motor. If both are set to
614 * zero, we stop the rumble motor.
615 */
616
617 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
618 value = 1;
619 else
620 value = 0;
621
622 spin_lock_irqsave(&wdata->state.lock, flags);
623 wiiproto_req_rumble(wdata, value);
624 spin_unlock_irqrestore(&wdata->state.lock, flags);
625
David Herrmann672bc4e2011-07-05 13:45:11 +0200626 return 0;
627}
628
David Herrmann26af1742011-08-17 11:43:21 +0200629static int wiimote_input_open(struct input_dev *dev)
630{
631 struct wiimote_data *wdata = input_get_drvdata(dev);
632
633 return hid_hw_open(wdata->hdev);
634}
635
636static void wiimote_input_close(struct input_dev *dev)
637{
638 struct wiimote_data *wdata = input_get_drvdata(dev);
639
640 hid_hw_close(wdata->hdev);
641}
642
David Herrmann98a558a2011-09-06 13:50:28 +0200643static int wiimote_accel_open(struct input_dev *dev)
644{
645 struct wiimote_data *wdata = input_get_drvdata(dev);
646 int ret;
647 unsigned long flags;
648
649 ret = hid_hw_open(wdata->hdev);
650 if (ret)
651 return ret;
652
653 spin_lock_irqsave(&wdata->state.lock, flags);
654 wiiproto_req_accel(wdata, true);
655 spin_unlock_irqrestore(&wdata->state.lock, flags);
656
657 return 0;
658}
659
660static void wiimote_accel_close(struct input_dev *dev)
661{
662 struct wiimote_data *wdata = input_get_drvdata(dev);
663 unsigned long flags;
664
665 spin_lock_irqsave(&wdata->state.lock, flags);
666 wiiproto_req_accel(wdata, false);
667 spin_unlock_irqrestore(&wdata->state.lock, flags);
668
669 hid_hw_close(wdata->hdev);
670}
671
David Herrmann0370d7c2011-09-06 13:50:37 +0200672static int wiimote_ir_open(struct input_dev *dev)
673{
674 struct wiimote_data *wdata = input_get_drvdata(dev);
675 int ret;
676
677 ret = hid_hw_open(wdata->hdev);
678 if (ret)
679 return ret;
680
681 ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
682 if (ret) {
683 hid_hw_close(wdata->hdev);
684 return ret;
685 }
686
687 return 0;
688}
689
690static void wiimote_ir_close(struct input_dev *dev)
691{
692 struct wiimote_data *wdata = input_get_drvdata(dev);
693
694 wiimote_init_ir(wdata, 0);
695 hid_hw_close(wdata->hdev);
696}
697
David Herrmann1abb9ad2011-07-05 13:45:16 +0200698static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
699{
700 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
701 !!(payload[0] & 0x01));
702 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT],
703 !!(payload[0] & 0x02));
704 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN],
705 !!(payload[0] & 0x04));
706 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP],
707 !!(payload[0] & 0x08));
708 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS],
709 !!(payload[0] & 0x10));
710 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO],
711 !!(payload[1] & 0x01));
712 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE],
713 !!(payload[1] & 0x02));
714 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B],
715 !!(payload[1] & 0x04));
716 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A],
717 !!(payload[1] & 0x08));
718 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS],
719 !!(payload[1] & 0x10));
720 input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME],
721 !!(payload[1] & 0x80));
722 input_sync(wdata->input);
723}
724
David Herrmannefcf9182011-09-06 13:50:29 +0200725static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
726{
727 __u16 x, y, z;
728
729 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
730 return;
731
732 /*
733 * payload is: BB BB XX YY ZZ
734 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
735 * contain the upper 8 bits of each value. The lower 2 bits are
736 * contained in the buttons data BB BB.
737 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
738 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
739 * accel value and bit 6 is the second bit of the Z value.
740 * The first bit of Y and Z values is not available and always set to 0.
741 * 0x200 is returned on no movement.
742 */
743
744 x = payload[2] << 2;
745 y = payload[3] << 2;
746 z = payload[4] << 2;
747
748 x |= (payload[0] >> 5) & 0x3;
749 y |= (payload[1] >> 4) & 0x2;
750 z |= (payload[1] >> 5) & 0x2;
751
752 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
753 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
754 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
755 input_sync(wdata->accel);
756}
757
David Herrmanneac39e72011-09-06 13:50:31 +0200758#define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
759 ABS_HAT0X, ABS_HAT0Y)
760#define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
761 ABS_HAT1X, ABS_HAT1Y)
762#define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
763 ABS_HAT2X, ABS_HAT2Y)
764#define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
765 ABS_HAT3X, ABS_HAT3Y)
766
767static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
768 bool packed, __u8 xid, __u8 yid)
769{
770 __u16 x, y;
771
772 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
773 return;
774
775 /*
776 * Basic IR data is encoded into 3 bytes. The first two bytes are the
David Herrmann74b89e82012-05-08 16:52:31 +0200777 * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
David Herrmanneac39e72011-09-06 13:50:31 +0200778 * of both.
779 * If data is packed, then the 3rd byte is put first and slightly
780 * reordered. This allows to interleave packed and non-packed data to
781 * have two IR sets in 5 bytes instead of 6.
782 * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
783 */
784
785 if (packed) {
David Herrmann74b89e82012-05-08 16:52:31 +0200786 x = ir[1] | ((ir[0] & 0x03) << 8);
787 y = ir[2] | ((ir[0] & 0x0c) << 6);
David Herrmanneac39e72011-09-06 13:50:31 +0200788 } else {
David Herrmann74b89e82012-05-08 16:52:31 +0200789 x = ir[0] | ((ir[2] & 0x30) << 4);
790 y = ir[1] | ((ir[2] & 0xc0) << 2);
David Herrmanneac39e72011-09-06 13:50:31 +0200791 }
792
793 input_report_abs(wdata->ir, xid, x);
794 input_report_abs(wdata->ir, yid, y);
795}
David Herrmannefcf9182011-09-06 13:50:29 +0200796
David Herrmann2d44e3d2013-04-02 19:58:36 +0200797/* reduced status report with "BB BB" key data only */
798static void handler_status_K(struct wiimote_data *wdata,
799 const __u8 *payload)
David Herrmannc87019e2011-08-17 11:43:24 +0200800{
801 handler_keys(wdata, payload);
802
803 /* on status reports the drm is reset so we need to resend the drm */
804 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
David Herrmann2d44e3d2013-04-02 19:58:36 +0200805}
806
807/* extended status report with "BB BB LF 00 00 VV" data */
808static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
809{
810 handler_status_K(wdata, payload);
David Herrmanne3979a92011-09-06 13:50:38 +0200811
David Herrmanncb992212011-11-17 14:12:01 +0100812 wiiext_event(wdata, payload[2] & 0x02);
813
David Herrmanne3979a92011-09-06 13:50:38 +0200814 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) {
815 wdata->state.cmd_battery = payload[5];
816 wiimote_cmd_complete(wdata);
817 }
David Herrmannc87019e2011-08-17 11:43:24 +0200818}
819
David Herrmann2d44e3d2013-04-02 19:58:36 +0200820/* reduced generic report with "BB BB" key data only */
821static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
822{
823 handler_keys(wdata, payload);
824}
825
David Herrmannbe1ecd62011-09-06 13:50:33 +0200826static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
827{
David Herrmannfad8c0e2011-11-17 14:12:00 +0100828 __u16 offset = payload[3] << 8 | payload[4];
829 __u8 size = (payload[2] >> 4) + 1;
830 __u8 err = payload[2] & 0x0f;
831
David Herrmannbe1ecd62011-09-06 13:50:33 +0200832 handler_keys(wdata, payload);
David Herrmannfad8c0e2011-11-17 14:12:00 +0100833
834 if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
835 if (err)
836 size = 0;
837 else if (size > wdata->state.cmd_read_size)
838 size = wdata->state.cmd_read_size;
839
840 wdata->state.cmd_read_size = size;
841 if (wdata->state.cmd_read_buf)
842 memcpy(wdata->state.cmd_read_buf, &payload[5], size);
843 wiimote_cmd_complete(wdata);
844 }
David Herrmannbe1ecd62011-09-06 13:50:33 +0200845}
846
David Herrmannc87019e2011-08-17 11:43:24 +0200847static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
848{
849 __u8 err = payload[3];
850 __u8 cmd = payload[2];
851
852 handler_keys(wdata, payload);
853
David Herrmann33e84012011-09-06 13:50:35 +0200854 if (wiimote_cmd_pending(wdata, cmd, 0)) {
855 wdata->state.cmd_err = err;
856 wiimote_cmd_complete(wdata);
857 } else if (err) {
David Herrmannc87019e2011-08-17 11:43:24 +0200858 hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
859 cmd);
David Herrmann33e84012011-09-06 13:50:35 +0200860 }
David Herrmannc87019e2011-08-17 11:43:24 +0200861}
862
David Herrmannefcf9182011-09-06 13:50:29 +0200863static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
864{
865 handler_keys(wdata, payload);
866 handler_accel(wdata, payload);
867}
868
David Herrmann7336b9f2011-09-06 13:50:32 +0200869static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
870{
871 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100872 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200873}
874
David Herrmannefcf9182011-09-06 13:50:29 +0200875static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
876{
877 handler_keys(wdata, payload);
878 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200879 ir_to_input0(wdata, &payload[5], false);
880 ir_to_input1(wdata, &payload[8], false);
881 ir_to_input2(wdata, &payload[11], false);
882 ir_to_input3(wdata, &payload[14], false);
883 input_sync(wdata->ir);
884}
885
David Herrmann7336b9f2011-09-06 13:50:32 +0200886static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
887{
888 handler_keys(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100889 wiiext_handle(wdata, &payload[2]);
David Herrmann7336b9f2011-09-06 13:50:32 +0200890}
891
David Herrmanneac39e72011-09-06 13:50:31 +0200892static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
893{
894 handler_keys(wdata, payload);
895 ir_to_input0(wdata, &payload[2], false);
896 ir_to_input1(wdata, &payload[4], true);
897 ir_to_input2(wdata, &payload[7], false);
898 ir_to_input3(wdata, &payload[9], true);
899 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +0100900 wiiext_handle(wdata, &payload[12]);
David Herrmannefcf9182011-09-06 13:50:29 +0200901}
902
903static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
904{
905 handler_keys(wdata, payload);
906 handler_accel(wdata, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100907 wiiext_handle(wdata, &payload[5]);
David Herrmannefcf9182011-09-06 13:50:29 +0200908}
909
910static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
911{
912 handler_keys(wdata, payload);
913 handler_accel(wdata, payload);
David Herrmanneac39e72011-09-06 13:50:31 +0200914 ir_to_input0(wdata, &payload[5], false);
915 ir_to_input1(wdata, &payload[7], true);
916 ir_to_input2(wdata, &payload[10], false);
917 ir_to_input3(wdata, &payload[12], true);
918 input_sync(wdata->ir);
David Herrmann0b6815d2011-11-17 14:12:06 +0100919 wiiext_handle(wdata, &payload[15]);
David Herrmannefcf9182011-09-06 13:50:29 +0200920}
921
David Herrmann7336b9f2011-09-06 13:50:32 +0200922static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
923{
David Herrmann0b6815d2011-11-17 14:12:06 +0100924 wiiext_handle(wdata, payload);
David Herrmann7336b9f2011-09-06 13:50:32 +0200925}
926
David Herrmannefcf9182011-09-06 13:50:29 +0200927static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
928{
929 handler_keys(wdata, payload);
930
931 wdata->state.accel_split[0] = payload[2];
932 wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
933 wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
David Herrmanneac39e72011-09-06 13:50:31 +0200934
935 ir_to_input0(wdata, &payload[3], false);
936 ir_to_input1(wdata, &payload[12], false);
937 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200938}
939
940static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
941{
942 __u8 buf[5];
943
944 handler_keys(wdata, payload);
945
946 wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
947 wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
948
949 buf[0] = 0;
950 buf[1] = 0;
951 buf[2] = wdata->state.accel_split[0];
952 buf[3] = payload[2];
953 buf[4] = wdata->state.accel_split[1];
954 handler_accel(wdata, buf);
David Herrmanneac39e72011-09-06 13:50:31 +0200955
956 ir_to_input2(wdata, &payload[3], false);
957 ir_to_input3(wdata, &payload[12], false);
958 input_sync(wdata->ir);
David Herrmannefcf9182011-09-06 13:50:29 +0200959}
960
David Herrmanna4d19192011-07-05 13:45:15 +0200961struct wiiproto_handler {
962 __u8 id;
963 size_t size;
964 void (*func)(struct wiimote_data *wdata, const __u8 *payload);
965};
966
967static struct wiiproto_handler handlers[] = {
David Herrmannc87019e2011-08-17 11:43:24 +0200968 { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200969 { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
David Herrmannbe1ecd62011-09-06 13:50:33 +0200970 { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200971 { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
David Herrmannc87019e2011-08-17 11:43:24 +0200972 { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200973 { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
David Herrmann1abb9ad2011-07-05 13:45:16 +0200974 { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
David Herrmannefcf9182011-09-06 13:50:29 +0200975 { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200976 { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +0200977 { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200978 { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +0200979 { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200980 { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +0200981 { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200982 { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +0200983 { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200984 { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
David Herrmanneac39e72011-09-06 13:50:31 +0200985 { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200986 { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
David Herrmannefcf9182011-09-06 13:50:29 +0200987 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
David Herrmann2d44e3d2013-04-02 19:58:36 +0200988 { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
David Herrmann7336b9f2011-09-06 13:50:32 +0200989 { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
David Herrmannefcf9182011-09-06 13:50:29 +0200990 { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
991 { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
David Herrmanna4d19192011-07-05 13:45:15 +0200992 { .id = 0 }
993};
994
David Herrmann02fb72a2011-07-05 13:45:09 +0200995static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
996 u8 *raw_data, int size)
997{
David Herrmann4d36e972011-07-05 13:45:12 +0200998 struct wiimote_data *wdata = hid_get_drvdata(hdev);
David Herrmanna4d19192011-07-05 13:45:15 +0200999 struct wiiproto_handler *h;
1000 int i;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001001 unsigned long flags;
David Herrmann4d36e972011-07-05 13:45:12 +02001002
David Herrmann02fb72a2011-07-05 13:45:09 +02001003 if (size < 1)
1004 return -EINVAL;
1005
David Herrmann32a0d9a2011-07-05 13:45:18 +02001006 spin_lock_irqsave(&wdata->state.lock, flags);
1007
David Herrmanna4d19192011-07-05 13:45:15 +02001008 for (i = 0; handlers[i].id; ++i) {
1009 h = &handlers[i];
David Herrmann7336b9f2011-09-06 13:50:32 +02001010 if (h->id == raw_data[0] && h->size < size) {
David Herrmanna4d19192011-07-05 13:45:15 +02001011 h->func(wdata, &raw_data[1]);
David Herrmann2d44e3d2013-04-02 19:58:36 +02001012 break;
David Herrmann7336b9f2011-09-06 13:50:32 +02001013 }
David Herrmanna4d19192011-07-05 13:45:15 +02001014 }
1015
David Herrmann2d44e3d2013-04-02 19:58:36 +02001016 if (!handlers[i].id)
David Herrmann7336b9f2011-09-06 13:50:32 +02001017 hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
1018 size);
1019
David Herrmann32a0d9a2011-07-05 13:45:18 +02001020 spin_unlock_irqrestore(&wdata->state.lock, flags);
1021
David Herrmann02fb72a2011-07-05 13:45:09 +02001022 return 0;
1023}
1024
David Herrmann23a5a4a2011-08-17 11:43:22 +02001025static void wiimote_leds_destroy(struct wiimote_data *wdata)
1026{
1027 int i;
1028 struct led_classdev *led;
1029
1030 for (i = 0; i < 4; ++i) {
1031 if (wdata->leds[i]) {
1032 led = wdata->leds[i];
1033 wdata->leds[i] = NULL;
1034 led_classdev_unregister(led);
1035 kfree(led);
1036 }
1037 }
1038}
1039
1040static int wiimote_leds_create(struct wiimote_data *wdata)
1041{
1042 int i, ret;
1043 struct device *dev = &wdata->hdev->dev;
1044 size_t namesz = strlen(dev_name(dev)) + 9;
1045 struct led_classdev *led;
1046 char *name;
1047
1048 for (i = 0; i < 4; ++i) {
1049 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
1050 if (!led) {
1051 ret = -ENOMEM;
1052 goto err;
1053 }
1054 name = (void*)&led[1];
1055 snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
1056 led->name = name;
1057 led->brightness = 0;
1058 led->max_brightness = 1;
1059 led->brightness_get = wiimote_leds_get;
1060 led->brightness_set = wiimote_leds_set;
1061
1062 ret = led_classdev_register(dev, led);
1063 if (ret) {
1064 kfree(led);
1065 goto err;
1066 }
1067 wdata->leds[i] = led;
1068 }
1069
1070 return 0;
1071
1072err:
1073 wiimote_leds_destroy(wdata);
1074 return ret;
1075}
1076
David Herrmanne894d0e2011-07-05 13:45:10 +02001077static struct wiimote_data *wiimote_create(struct hid_device *hdev)
1078{
1079 struct wiimote_data *wdata;
David Herrmann1abb9ad2011-07-05 13:45:16 +02001080 int i;
David Herrmanne894d0e2011-07-05 13:45:10 +02001081
1082 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
1083 if (!wdata)
1084 return NULL;
1085
David Herrmann672bc4e2011-07-05 13:45:11 +02001086 wdata->input = input_allocate_device();
David Herrmann98a558a2011-09-06 13:50:28 +02001087 if (!wdata->input)
1088 goto err;
David Herrmann672bc4e2011-07-05 13:45:11 +02001089
David Herrmanne894d0e2011-07-05 13:45:10 +02001090 wdata->hdev = hdev;
1091 hid_set_drvdata(hdev, wdata);
1092
David Herrmann672bc4e2011-07-05 13:45:11 +02001093 input_set_drvdata(wdata->input, wdata);
David Herrmann26af1742011-08-17 11:43:21 +02001094 wdata->input->open = wiimote_input_open;
1095 wdata->input->close = wiimote_input_close;
David Herrmann672bc4e2011-07-05 13:45:11 +02001096 wdata->input->dev.parent = &wdata->hdev->dev;
1097 wdata->input->id.bustype = wdata->hdev->bus;
1098 wdata->input->id.vendor = wdata->hdev->vendor;
1099 wdata->input->id.product = wdata->hdev->product;
1100 wdata->input->id.version = wdata->hdev->version;
1101 wdata->input->name = WIIMOTE_NAME;
1102
David Herrmann1abb9ad2011-07-05 13:45:16 +02001103 set_bit(EV_KEY, wdata->input->evbit);
1104 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
1105 set_bit(wiiproto_keymap[i], wdata->input->keybit);
1106
David Herrmannd020be92011-09-06 13:50:27 +02001107 set_bit(FF_RUMBLE, wdata->input->ffbit);
David Herrmann98a558a2011-09-06 13:50:28 +02001108 if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play))
1109 goto err_input;
1110
1111 wdata->accel = input_allocate_device();
1112 if (!wdata->accel)
1113 goto err_input;
1114
1115 input_set_drvdata(wdata->accel, wdata);
1116 wdata->accel->open = wiimote_accel_open;
1117 wdata->accel->close = wiimote_accel_close;
1118 wdata->accel->dev.parent = &wdata->hdev->dev;
1119 wdata->accel->id.bustype = wdata->hdev->bus;
1120 wdata->accel->id.vendor = wdata->hdev->vendor;
1121 wdata->accel->id.product = wdata->hdev->product;
1122 wdata->accel->id.version = wdata->hdev->version;
1123 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
1124
1125 set_bit(EV_ABS, wdata->accel->evbit);
1126 set_bit(ABS_RX, wdata->accel->absbit);
1127 set_bit(ABS_RY, wdata->accel->absbit);
1128 set_bit(ABS_RZ, wdata->accel->absbit);
1129 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
1130 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
1131 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
David Herrmannd020be92011-09-06 13:50:27 +02001132
David Herrmannf363e4f2011-09-06 13:50:30 +02001133 wdata->ir = input_allocate_device();
1134 if (!wdata->ir)
1135 goto err_ir;
1136
1137 input_set_drvdata(wdata->ir, wdata);
David Herrmann0370d7c2011-09-06 13:50:37 +02001138 wdata->ir->open = wiimote_ir_open;
1139 wdata->ir->close = wiimote_ir_close;
David Herrmannf363e4f2011-09-06 13:50:30 +02001140 wdata->ir->dev.parent = &wdata->hdev->dev;
1141 wdata->ir->id.bustype = wdata->hdev->bus;
1142 wdata->ir->id.vendor = wdata->hdev->vendor;
1143 wdata->ir->id.product = wdata->hdev->product;
1144 wdata->ir->id.version = wdata->hdev->version;
1145 wdata->ir->name = WIIMOTE_NAME " IR";
1146
1147 set_bit(EV_ABS, wdata->ir->evbit);
1148 set_bit(ABS_HAT0X, wdata->ir->absbit);
1149 set_bit(ABS_HAT0Y, wdata->ir->absbit);
1150 set_bit(ABS_HAT1X, wdata->ir->absbit);
1151 set_bit(ABS_HAT1Y, wdata->ir->absbit);
1152 set_bit(ABS_HAT2X, wdata->ir->absbit);
1153 set_bit(ABS_HAT2Y, wdata->ir->absbit);
1154 set_bit(ABS_HAT3X, wdata->ir->absbit);
1155 set_bit(ABS_HAT3Y, wdata->ir->absbit);
1156 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
1157 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
1158 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
1159 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
1160 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
1161 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
1162 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
1163 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
1164
David Herrmann13938532013-05-05 23:12:46 +02001165 spin_lock_init(&wdata->queue.lock);
1166 INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
David Herrmann23c063c2011-07-05 13:45:14 +02001167
David Herrmann32a0d9a2011-07-05 13:45:18 +02001168 spin_lock_init(&wdata->state.lock);
David Herrmann29d28062011-09-06 13:50:34 +02001169 init_completion(&wdata->state.ready);
1170 mutex_init(&wdata->state.sync);
David Herrmann43d782a2011-11-17 14:12:12 +01001171 wdata->state.drm = WIIPROTO_REQ_DRM_K;
David Herrmann32a0d9a2011-07-05 13:45:18 +02001172
David Herrmanne894d0e2011-07-05 13:45:10 +02001173 return wdata;
David Herrmann98a558a2011-09-06 13:50:28 +02001174
David Herrmannf363e4f2011-09-06 13:50:30 +02001175err_ir:
1176 input_free_device(wdata->accel);
David Herrmann98a558a2011-09-06 13:50:28 +02001177err_input:
1178 input_free_device(wdata->input);
1179err:
1180 kfree(wdata);
1181 return NULL;
David Herrmanne894d0e2011-07-05 13:45:10 +02001182}
1183
1184static void wiimote_destroy(struct wiimote_data *wdata)
1185{
David Herrmann43e5e7c2011-11-17 14:12:10 +01001186 wiidebug_deinit(wdata);
David Herrmanncb992212011-11-17 14:12:01 +01001187 wiiext_deinit(wdata);
David Herrmann23a5a4a2011-08-17 11:43:22 +02001188 wiimote_leds_destroy(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001189
David Herrmann6591d752011-09-06 13:50:39 +02001190 power_supply_unregister(&wdata->battery);
David Herrmann001dac82013-04-02 19:58:34 +02001191 kfree(wdata->battery.name);
David Herrmann98a558a2011-09-06 13:50:28 +02001192 input_unregister_device(wdata->accel);
David Herrmannf363e4f2011-09-06 13:50:30 +02001193 input_unregister_device(wdata->ir);
David Herrmann3989ef62011-08-17 11:43:20 +02001194 input_unregister_device(wdata->input);
David Herrmann13938532013-05-05 23:12:46 +02001195 cancel_work_sync(&wdata->queue.worker);
David Herrmann3989ef62011-08-17 11:43:20 +02001196 hid_hw_stop(wdata->hdev);
1197
David Herrmanne894d0e2011-07-05 13:45:10 +02001198 kfree(wdata);
1199}
1200
David Herrmann02fb72a2011-07-05 13:45:09 +02001201static int wiimote_hid_probe(struct hid_device *hdev,
1202 const struct hid_device_id *id)
1203{
David Herrmanne894d0e2011-07-05 13:45:10 +02001204 struct wiimote_data *wdata;
David Herrmann02fb72a2011-07-05 13:45:09 +02001205 int ret;
1206
David Herrmann90120d62011-11-17 14:12:14 +01001207 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1208
David Herrmanne894d0e2011-07-05 13:45:10 +02001209 wdata = wiimote_create(hdev);
1210 if (!wdata) {
1211 hid_err(hdev, "Can't alloc device\n");
1212 return -ENOMEM;
1213 }
1214
David Herrmann02fb72a2011-07-05 13:45:09 +02001215 ret = hid_parse(hdev);
1216 if (ret) {
1217 hid_err(hdev, "HID parse failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001218 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001219 }
1220
1221 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1222 if (ret) {
1223 hid_err(hdev, "HW start failed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001224 goto err;
David Herrmann02fb72a2011-07-05 13:45:09 +02001225 }
1226
David Herrmann98a558a2011-09-06 13:50:28 +02001227 ret = input_register_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001228 if (ret) {
1229 hid_err(hdev, "Cannot register input device\n");
1230 goto err_stop;
1231 }
1232
David Herrmannf363e4f2011-09-06 13:50:30 +02001233 ret = input_register_device(wdata->ir);
1234 if (ret) {
1235 hid_err(hdev, "Cannot register input device\n");
1236 goto err_ir;
1237 }
1238
David Herrmann98a558a2011-09-06 13:50:28 +02001239 ret = input_register_device(wdata->input);
1240 if (ret) {
1241 hid_err(hdev, "Cannot register input device\n");
1242 goto err_input;
1243 }
1244
David Herrmann6591d752011-09-06 13:50:39 +02001245 wdata->battery.properties = wiimote_battery_props;
1246 wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props);
1247 wdata->battery.get_property = wiimote_battery_get_property;
David Herrmann6591d752011-09-06 13:50:39 +02001248 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1249 wdata->battery.use_for_apm = 0;
David Herrmann001dac82013-04-02 19:58:34 +02001250 wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
1251 wdata->hdev->uniq);
1252 if (!wdata->battery.name) {
1253 ret = -ENOMEM;
1254 goto err_battery_name;
1255 }
David Herrmann6591d752011-09-06 13:50:39 +02001256
1257 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
1258 if (ret) {
1259 hid_err(hdev, "Cannot register battery device\n");
1260 goto err_battery;
1261 }
1262
Jiri Kosina217c8b22012-02-07 13:40:56 +01001263 power_supply_powers(&wdata->battery, &hdev->dev);
1264
David Herrmann23a5a4a2011-08-17 11:43:22 +02001265 ret = wiimote_leds_create(wdata);
David Herrmann3989ef62011-08-17 11:43:20 +02001266 if (ret)
1267 goto err_free;
1268
David Herrmanncb992212011-11-17 14:12:01 +01001269 ret = wiiext_init(wdata);
1270 if (ret)
1271 goto err_free;
1272
David Herrmann43e5e7c2011-11-17 14:12:10 +01001273 ret = wiidebug_init(wdata);
1274 if (ret)
1275 goto err_free;
1276
David Herrmann02fb72a2011-07-05 13:45:09 +02001277 hid_info(hdev, "New device registered\n");
David Herrmann32a0d9a2011-07-05 13:45:18 +02001278
1279 /* by default set led1 after device initialization */
1280 spin_lock_irq(&wdata->state.lock);
David Herrmanndb308342011-07-05 13:45:17 +02001281 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
David Herrmann32a0d9a2011-07-05 13:45:18 +02001282 spin_unlock_irq(&wdata->state.lock);
1283
David Herrmann02fb72a2011-07-05 13:45:09 +02001284 return 0;
David Herrmanne894d0e2011-07-05 13:45:10 +02001285
David Herrmann3989ef62011-08-17 11:43:20 +02001286err_free:
1287 wiimote_destroy(wdata);
1288 return ret;
1289
David Herrmann6591d752011-09-06 13:50:39 +02001290err_battery:
David Herrmann001dac82013-04-02 19:58:34 +02001291 kfree(wdata->battery.name);
1292err_battery_name:
David Herrmann6591d752011-09-06 13:50:39 +02001293 input_unregister_device(wdata->input);
1294 wdata->input = NULL;
David Herrmann98a558a2011-09-06 13:50:28 +02001295err_input:
David Herrmannf363e4f2011-09-06 13:50:30 +02001296 input_unregister_device(wdata->ir);
1297 wdata->ir = NULL;
1298err_ir:
David Herrmann98a558a2011-09-06 13:50:28 +02001299 input_unregister_device(wdata->accel);
1300 wdata->accel = NULL;
David Herrmann672bc4e2011-07-05 13:45:11 +02001301err_stop:
1302 hid_hw_stop(hdev);
David Herrmanne894d0e2011-07-05 13:45:10 +02001303err:
David Herrmannf363e4f2011-09-06 13:50:30 +02001304 input_free_device(wdata->ir);
David Herrmann98a558a2011-09-06 13:50:28 +02001305 input_free_device(wdata->accel);
David Herrmann672bc4e2011-07-05 13:45:11 +02001306 input_free_device(wdata->input);
David Herrmann3989ef62011-08-17 11:43:20 +02001307 kfree(wdata);
David Herrmanne894d0e2011-07-05 13:45:10 +02001308 return ret;
David Herrmann02fb72a2011-07-05 13:45:09 +02001309}
1310
1311static void wiimote_hid_remove(struct hid_device *hdev)
1312{
David Herrmanne894d0e2011-07-05 13:45:10 +02001313 struct wiimote_data *wdata = hid_get_drvdata(hdev);
1314
David Herrmann02fb72a2011-07-05 13:45:09 +02001315 hid_info(hdev, "Device removed\n");
David Herrmanne894d0e2011-07-05 13:45:10 +02001316 wiimote_destroy(wdata);
David Herrmann02fb72a2011-07-05 13:45:09 +02001317}
1318
1319static const struct hid_device_id wiimote_hid_devices[] = {
1320 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1321 USB_DEVICE_ID_NINTENDO_WIIMOTE) },
David Herrmanna33042f2013-04-02 19:58:35 +02001322 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
1323 USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
David Herrmann02fb72a2011-07-05 13:45:09 +02001324 { }
1325};
1326MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
1327
1328static struct hid_driver wiimote_hid_driver = {
1329 .name = "wiimote",
1330 .id_table = wiimote_hid_devices,
1331 .probe = wiimote_hid_probe,
1332 .remove = wiimote_hid_remove,
1333 .raw_event = wiimote_hid_event,
1334};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001335module_hid_driver(wiimote_hid_driver);
David Herrmann02fb72a2011-07-05 13:45:09 +02001336
David Herrmannfb51b442011-07-05 13:45:08 +02001337MODULE_LICENSE("GPL");
1338MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
David Herrmann92eda7e2013-05-05 23:12:45 +02001339MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");