blob: ae022b863593bdbcc8587cfc002b890c3ba50573 [file] [log] [blame]
David Herrmanncb992212011-11-17 14:12:01 +01001/*
2 * HID driver for Nintendo Wiimote extension devices
3 * Copyright (c) 2011 David Herrmann
4 */
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
13#include <linux/atomic.h>
14#include <linux/module.h>
15#include <linux/spinlock.h>
16#include <linux/workqueue.h>
17#include "hid-wiimote.h"
18
19struct wiimote_ext {
20 struct wiimote_data *wdata;
21 struct work_struct worker;
David Herrmann479901b2011-11-17 14:12:05 +010022 struct input_dev *input;
23 struct input_dev *mp_input;
David Herrmanncb992212011-11-17 14:12:01 +010024
25 atomic_t opened;
26 atomic_t mp_opened;
27 bool plugged;
David Herrmannb17b57a2011-11-17 14:12:07 +010028 bool mp_plugged;
David Herrmanncb992212011-11-17 14:12:01 +010029 bool motionp;
30 __u8 ext_type;
31};
32
33enum wiiext_type {
34 WIIEXT_NONE, /* placeholder */
35 WIIEXT_CLASSIC, /* Nintendo classic controller */
36 WIIEXT_NUNCHUCK, /* Nintendo nunchuck controller */
David Herrmann5ad67fb2012-09-17 12:31:35 +020037 WIIEXT_BALANCE_BOARD, /* Nintendo balance board controller */
David Herrmanncb992212011-11-17 14:12:01 +010038};
39
David Herrmanna5353502011-11-17 14:12:08 +010040enum wiiext_keys {
41 WIIEXT_KEY_C,
42 WIIEXT_KEY_Z,
David Herrmann59062152011-11-17 14:12:09 +010043 WIIEXT_KEY_A,
44 WIIEXT_KEY_B,
45 WIIEXT_KEY_X,
46 WIIEXT_KEY_Y,
47 WIIEXT_KEY_ZL,
48 WIIEXT_KEY_ZR,
49 WIIEXT_KEY_PLUS,
50 WIIEXT_KEY_MINUS,
51 WIIEXT_KEY_HOME,
52 WIIEXT_KEY_LEFT,
53 WIIEXT_KEY_RIGHT,
54 WIIEXT_KEY_UP,
55 WIIEXT_KEY_DOWN,
56 WIIEXT_KEY_LT,
57 WIIEXT_KEY_RT,
David Herrmanna5353502011-11-17 14:12:08 +010058 WIIEXT_KEY_COUNT
59};
60
61static __u16 wiiext_keymap[] = {
62 BTN_C, /* WIIEXT_KEY_C */
63 BTN_Z, /* WIIEXT_KEY_Z */
David Herrmann59062152011-11-17 14:12:09 +010064 BTN_A, /* WIIEXT_KEY_A */
65 BTN_B, /* WIIEXT_KEY_B */
66 BTN_X, /* WIIEXT_KEY_X */
67 BTN_Y, /* WIIEXT_KEY_Y */
68 BTN_TL2, /* WIIEXT_KEY_ZL */
69 BTN_TR2, /* WIIEXT_KEY_ZR */
70 KEY_NEXT, /* WIIEXT_KEY_PLUS */
71 KEY_PREVIOUS, /* WIIEXT_KEY_MINUS */
72 BTN_MODE, /* WIIEXT_KEY_HOME */
73 KEY_LEFT, /* WIIEXT_KEY_LEFT */
74 KEY_RIGHT, /* WIIEXT_KEY_RIGHT */
75 KEY_UP, /* WIIEXT_KEY_UP */
76 KEY_DOWN, /* WIIEXT_KEY_DOWN */
77 BTN_TL, /* WIIEXT_KEY_LT */
78 BTN_TR, /* WIIEXT_KEY_RT */
David Herrmanna5353502011-11-17 14:12:08 +010079};
80
Giuseppe Bilottae39fe252012-06-04 20:45:40 +020081/* disable all extensions */
David Herrmann82fb1b32011-11-17 14:12:02 +010082static void ext_disable(struct wiimote_ext *ext)
83{
84 unsigned long flags;
David Herrmann492ba952011-11-17 14:12:03 +010085 __u8 wmem = 0x55;
86
87 if (!wiimote_cmd_acquire(ext->wdata)) {
88 wiimote_cmd_write(ext->wdata, 0xa400f0, &wmem, sizeof(wmem));
89 wiimote_cmd_release(ext->wdata);
90 }
David Herrmann82fb1b32011-11-17 14:12:02 +010091
92 spin_lock_irqsave(&ext->wdata->state.lock, flags);
93 ext->motionp = false;
94 ext->ext_type = WIIEXT_NONE;
David Herrmann492ba952011-11-17 14:12:03 +010095 wiiproto_req_drm(ext->wdata, WIIPROTO_REQ_NULL);
David Herrmann82fb1b32011-11-17 14:12:02 +010096 spin_unlock_irqrestore(&ext->wdata->state.lock, flags);
97}
98
99static bool motionp_read(struct wiimote_ext *ext)
100{
David Herrmann492ba952011-11-17 14:12:03 +0100101 __u8 rmem[2], wmem;
102 ssize_t ret;
103 bool avail = false;
104
David Herrmann479901b2011-11-17 14:12:05 +0100105 if (!atomic_read(&ext->mp_opened))
106 return false;
107
David Herrmann492ba952011-11-17 14:12:03 +0100108 if (wiimote_cmd_acquire(ext->wdata))
109 return false;
110
111 /* initialize motion plus */
112 wmem = 0x55;
113 ret = wiimote_cmd_write(ext->wdata, 0xa600f0, &wmem, sizeof(wmem));
114 if (ret)
115 goto error;
116
117 /* read motion plus ID */
118 ret = wiimote_cmd_read(ext->wdata, 0xa600fe, rmem, 2);
119 if (ret == 2 || rmem[1] == 0x5)
120 avail = true;
121
122error:
123 wiimote_cmd_release(ext->wdata);
124 return avail;
David Herrmann82fb1b32011-11-17 14:12:02 +0100125}
126
127static __u8 ext_read(struct wiimote_ext *ext)
128{
David Herrmann492ba952011-11-17 14:12:03 +0100129 ssize_t ret;
130 __u8 rmem[2], wmem;
131 __u8 type = WIIEXT_NONE;
132
David Herrmann479901b2011-11-17 14:12:05 +0100133 if (!ext->plugged || !atomic_read(&ext->opened))
David Herrmann492ba952011-11-17 14:12:03 +0100134 return WIIEXT_NONE;
135
136 if (wiimote_cmd_acquire(ext->wdata))
137 return WIIEXT_NONE;
138
139 /* initialize extension */
140 wmem = 0x55;
141 ret = wiimote_cmd_write(ext->wdata, 0xa400f0, &wmem, sizeof(wmem));
142 if (!ret) {
143 /* disable encryption */
144 wmem = 0x0;
145 wiimote_cmd_write(ext->wdata, 0xa400fb, &wmem, sizeof(wmem));
146 }
147
148 /* read extension ID */
149 ret = wiimote_cmd_read(ext->wdata, 0xa400fe, rmem, 2);
150 if (ret == 2) {
151 if (rmem[0] == 0 && rmem[1] == 0)
152 type = WIIEXT_NUNCHUCK;
153 else if (rmem[0] == 0x01 && rmem[1] == 0x01)
154 type = WIIEXT_CLASSIC;
David Herrmann5ad67fb2012-09-17 12:31:35 +0200155 else if (rmem[0] == 0x04 && rmem[1] == 0x02)
156 type = WIIEXT_BALANCE_BOARD;
David Herrmann492ba952011-11-17 14:12:03 +0100157 }
158
159 wiimote_cmd_release(ext->wdata);
160
161 return type;
David Herrmann82fb1b32011-11-17 14:12:02 +0100162}
163
164static void ext_enable(struct wiimote_ext *ext, bool motionp, __u8 ext_type)
165{
166 unsigned long flags;
David Herrmann492ba952011-11-17 14:12:03 +0100167 __u8 wmem;
168 int ret;
169
170 if (motionp) {
171 if (wiimote_cmd_acquire(ext->wdata))
172 return;
173
174 if (ext_type == WIIEXT_CLASSIC)
175 wmem = 0x07;
176 else if (ext_type == WIIEXT_NUNCHUCK)
177 wmem = 0x05;
178 else
179 wmem = 0x04;
180
181 ret = wiimote_cmd_write(ext->wdata, 0xa600fe, &wmem, sizeof(wmem));
182 wiimote_cmd_release(ext->wdata);
183 if (ret)
184 return;
185 }
David Herrmann82fb1b32011-11-17 14:12:02 +0100186
187 spin_lock_irqsave(&ext->wdata->state.lock, flags);
188 ext->motionp = motionp;
189 ext->ext_type = ext_type;
David Herrmann492ba952011-11-17 14:12:03 +0100190 wiiproto_req_drm(ext->wdata, WIIPROTO_REQ_NULL);
David Herrmann82fb1b32011-11-17 14:12:02 +0100191 spin_unlock_irqrestore(&ext->wdata->state.lock, flags);
192}
193
David Herrmanncb992212011-11-17 14:12:01 +0100194static void wiiext_worker(struct work_struct *work)
195{
196 struct wiimote_ext *ext = container_of(work, struct wiimote_ext,
197 worker);
David Herrmann82fb1b32011-11-17 14:12:02 +0100198 bool motionp;
199 __u8 ext_type;
200
201 ext_disable(ext);
202 motionp = motionp_read(ext);
203 ext_type = ext_read(ext);
204 ext_enable(ext, motionp, ext_type);
David Herrmanncb992212011-11-17 14:12:01 +0100205}
206
207/* schedule work only once, otherwise mark for reschedule */
208static void wiiext_schedule(struct wiimote_ext *ext)
209{
210 queue_work(system_nrt_wq, &ext->worker);
211}
212
213/*
214 * Reacts on extension port events
215 * Whenever the driver gets an event from the wiimote that an extension has been
216 * plugged or unplugged, this funtion shall be called. It checks what extensions
217 * are connected and initializes and activates them.
218 * This can be called in atomic context. The initialization is done in a
219 * separate worker thread. The state.lock spinlock must be held by the caller.
220 */
221void wiiext_event(struct wiimote_data *wdata, bool plugged)
222{
223 if (!wdata->ext)
224 return;
225
226 if (wdata->ext->plugged == plugged)
227 return;
228
229 wdata->ext->plugged = plugged;
David Herrmannb17b57a2011-11-17 14:12:07 +0100230
231 if (!plugged)
232 wdata->ext->mp_plugged = false;
233
David Herrmanncb992212011-11-17 14:12:01 +0100234 /*
235 * We need to call wiiext_schedule(wdata->ext) here, however, the
236 * extension initialization logic is not fully understood and so
237 * automatic initialization is not supported, yet.
238 */
239}
240
241/*
242 * Returns true if the current DRM mode should contain extension data and false
243 * if there is no interest in extension data.
244 * All supported extensions send 6 byte extension data so any DRM that contains
245 * extension bytes is fine.
246 * The caller must hold the state.lock spinlock.
247 */
248bool wiiext_active(struct wiimote_data *wdata)
249{
250 if (!wdata->ext)
251 return false;
252
253 return wdata->ext->motionp || wdata->ext->ext_type;
254}
255
David Herrmann0b6815d2011-11-17 14:12:06 +0100256static void handler_motionp(struct wiimote_ext *ext, const __u8 *payload)
257{
David Herrmannb17b57a2011-11-17 14:12:07 +0100258 __s32 x, y, z;
259 bool plugged;
260
261 /* | 8 7 6 5 4 3 | 2 | 1 |
262 * -----+------------------------------+-----+-----+
263 * 1 | Yaw Speed <7:0> |
264 * 2 | Roll Speed <7:0> |
265 * 3 | Pitch Speed <7:0> |
266 * -----+------------------------------+-----+-----+
267 * 4 | Yaw Speed <13:8> | Yaw |Pitch|
268 * -----+------------------------------+-----+-----+
269 * 5 | Roll Speed <13:8> |Roll | Ext |
270 * -----+------------------------------+-----+-----+
271 * 6 | Pitch Speed <13:8> | 1 | 0 |
272 * -----+------------------------------+-----+-----+
273 * The single bits Yaw, Roll, Pitch in the lower right corner specify
274 * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
275 * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
276 * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
277 * and 9 for slow.
278 * If the wiimote is not rotating the sensor reports 2^13 = 8192.
279 * Ext specifies whether an extension is connected to the motionp.
280 */
281
282 x = payload[0];
283 y = payload[1];
284 z = payload[2];
285
286 x |= (((__u16)payload[3]) << 6) & 0xff00;
287 y |= (((__u16)payload[4]) << 6) & 0xff00;
288 z |= (((__u16)payload[5]) << 6) & 0xff00;
289
290 x -= 8192;
291 y -= 8192;
292 z -= 8192;
293
294 if (!(payload[3] & 0x02))
295 x *= 18;
296 else
297 x *= 9;
298 if (!(payload[4] & 0x02))
299 y *= 18;
300 else
301 y *= 9;
302 if (!(payload[3] & 0x01))
303 z *= 18;
304 else
305 z *= 9;
306
307 input_report_abs(ext->mp_input, ABS_RX, x);
308 input_report_abs(ext->mp_input, ABS_RY, y);
309 input_report_abs(ext->mp_input, ABS_RZ, z);
310 input_sync(ext->mp_input);
311
312 plugged = payload[5] & 0x01;
313 if (plugged != ext->mp_plugged)
314 ext->mp_plugged = plugged;
David Herrmann0b6815d2011-11-17 14:12:06 +0100315}
316
317static void handler_nunchuck(struct wiimote_ext *ext, const __u8 *payload)
318{
David Herrmanna5353502011-11-17 14:12:08 +0100319 __s16 x, y, z, bx, by;
320
321 /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 |
322 * -----+----------+---------+---------+----+-----+
323 * 1 | Button X <7:0> |
324 * 2 | Button Y <7:0> |
325 * -----+----------+---------+---------+----+-----+
326 * 3 | Speed X <9:2> |
327 * 4 | Speed Y <9:2> |
328 * 5 | Speed Z <9:2> |
329 * -----+----------+---------+---------+----+-----+
330 * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ |
331 * -----+----------+---------+---------+----+-----+
332 * Button X/Y is the analog stick. Speed X, Y and Z are the
333 * accelerometer data in the same format as the wiimote's accelerometer.
334 * The 6th byte contains the LSBs of the accelerometer data.
335 * BC and BZ are the C and Z buttons: 0 means pressed
336 *
337 * If reported interleaved with motionp, then the layout changes. The
338 * 5th and 6th byte changes to:
339 * -----+-----------------------------------+-----+
340 * 5 | Speed Z <9:3> | EXT |
341 * -----+--------+-----+-----+----+----+----+-----+
342 * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 |
343 * -----+--------+-----+-----+----+----+----+-----+
344 * All three accelerometer values lose their LSB. The other data is
345 * still available but slightly moved.
346 *
347 * Center data for button values is 128. Center value for accelerometer
348 * values it 512 / 0x200
349 */
350
351 bx = payload[0];
352 by = payload[1];
353 bx -= 128;
354 by -= 128;
355
356 x = payload[2] << 2;
357 y = payload[3] << 2;
358 z = payload[4] << 2;
359
360 if (ext->motionp) {
361 x |= (payload[5] >> 3) & 0x02;
362 y |= (payload[5] >> 4) & 0x02;
363 z &= ~0x4;
364 z |= (payload[5] >> 5) & 0x06;
365 } else {
366 x |= (payload[5] >> 2) & 0x03;
367 y |= (payload[5] >> 4) & 0x03;
368 z |= (payload[5] >> 6) & 0x03;
369 }
370
371 x -= 0x200;
372 y -= 0x200;
373 z -= 0x200;
374
375 input_report_abs(ext->input, ABS_HAT0X, bx);
376 input_report_abs(ext->input, ABS_HAT0Y, by);
377
378 input_report_abs(ext->input, ABS_RX, x);
379 input_report_abs(ext->input, ABS_RY, y);
380 input_report_abs(ext->input, ABS_RZ, z);
381
382 if (ext->motionp) {
383 input_report_key(ext->input,
384 wiiext_keymap[WIIEXT_KEY_Z], !!(payload[5] & 0x04));
385 input_report_key(ext->input,
386 wiiext_keymap[WIIEXT_KEY_C], !!(payload[5] & 0x08));
387 } else {
388 input_report_key(ext->input,
389 wiiext_keymap[WIIEXT_KEY_Z], !!(payload[5] & 0x01));
390 input_report_key(ext->input,
391 wiiext_keymap[WIIEXT_KEY_C], !!(payload[5] & 0x02));
392 }
393
394 input_sync(ext->input);
David Herrmann0b6815d2011-11-17 14:12:06 +0100395}
396
397static void handler_classic(struct wiimote_ext *ext, const __u8 *payload)
398{
David Herrmann59062152011-11-17 14:12:09 +0100399 __s8 rx, ry, lx, ly, lt, rt;
400
401 /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
402 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
403 * 1 | RX <5:4> | LX <5:0> |
404 * 2 | RX <3:2> | LY <5:0> |
405 * -----+-----+-----+-----+-----------------------------+
406 * 3 |RX<1>| LT <5:4> | RY <5:1> |
407 * -----+-----+-----------+-----------------------------+
408 * 4 | LT <3:1> | RT <5:1> |
409 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
410 * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
411 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
412 * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
413 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
414 * All buttons are 0 if pressed
415 * RX and RY are right analog stick
416 * LX and LY are left analog stick
417 * LT is left trigger, RT is right trigger
418 * BLT is 0 if left trigger is fully pressed
419 * BRT is 0 if right trigger is fully pressed
420 * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
421 * BZL is left Z button and BZR is right Z button
422 * B-, BH, B+ are +, HOME and - buttons
423 * BB, BY, BA, BX are A, B, X, Y buttons
424 * LSB of RX, RY, LT, and RT are not transmitted and always 0.
425 *
426 * With motionp enabled it changes slightly to this:
427 * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
428 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
429 * 1 | RX <4:3> | LX <5:1> | BDU |
430 * 2 | RX <2:1> | LY <5:1> | BDL |
431 * -----+-----+-----+-----+-----------------------+-----+
432 * 3 |RX<0>| LT <4:3> | RY <4:0> |
433 * -----+-----+-----------+-----------------------------+
434 * 4 | LT <2:0> | RT <4:0> |
435 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
436 * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT |
437 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
438 * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 |
439 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
440 * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
441 * is the same as before.
442 */
443
444 if (ext->motionp) {
445 lx = payload[0] & 0x3e;
446 ly = payload[0] & 0x3e;
447 } else {
448 lx = payload[0] & 0x3f;
449 ly = payload[0] & 0x3f;
450 }
451
452 rx = (payload[0] >> 3) & 0x14;
453 rx |= (payload[1] >> 5) & 0x06;
454 rx |= (payload[2] >> 7) & 0x01;
455 ry = payload[2] & 0x1f;
456
457 rt = payload[3] & 0x1f;
458 lt = (payload[2] >> 2) & 0x18;
459 lt |= (payload[3] >> 5) & 0x07;
460
461 rx <<= 1;
462 ry <<= 1;
463 rt <<= 1;
464 lt <<= 1;
465
466 input_report_abs(ext->input, ABS_HAT1X, lx - 0x20);
467 input_report_abs(ext->input, ABS_HAT1Y, ly - 0x20);
468 input_report_abs(ext->input, ABS_HAT2X, rx - 0x20);
469 input_report_abs(ext->input, ABS_HAT2Y, ry - 0x20);
470 input_report_abs(ext->input, ABS_HAT3X, rt - 0x20);
471 input_report_abs(ext->input, ABS_HAT3Y, lt - 0x20);
472
473 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_RIGHT],
474 !!(payload[4] & 0x80));
475 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_DOWN],
476 !!(payload[4] & 0x40));
477 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LT],
478 !!(payload[4] & 0x20));
479 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_MINUS],
480 !!(payload[4] & 0x10));
481 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_HOME],
482 !!(payload[4] & 0x08));
483 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_PLUS],
484 !!(payload[4] & 0x04));
485 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_RT],
486 !!(payload[4] & 0x02));
487 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_ZL],
488 !!(payload[5] & 0x80));
489 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_B],
490 !!(payload[5] & 0x40));
491 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_Y],
492 !!(payload[5] & 0x20));
493 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_A],
494 !!(payload[5] & 0x10));
495 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_X],
496 !!(payload[5] & 0x08));
497 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_ZR],
498 !!(payload[5] & 0x04));
499
500 if (ext->motionp) {
501 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_UP],
502 !!(payload[0] & 0x01));
503 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LEFT],
504 !!(payload[1] & 0x01));
505 } else {
506 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_UP],
507 !!(payload[5] & 0x01));
508 input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LEFT],
509 !!(payload[5] & 0x02));
510 }
511
512 input_sync(ext->input);
David Herrmann0b6815d2011-11-17 14:12:06 +0100513}
514
David Herrmann5ad67fb2012-09-17 12:31:35 +0200515static void handler_balance_board(struct wiimote_ext *ext, const __u8 *payload)
516{
517 __s32 val[4];
518
519 /* Byte | 8 7 6 5 4 3 2 1 |
520 * -----+--------------------------+
521 * 1 | Top Right <15:8> |
522 * 2 | Top Right <7:0> |
523 * -----+--------------------------+
524 * 3 | Bottom Right <15:8> |
525 * 4 | Bottom Right <7:0> |
526 * -----+--------------------------+
527 * 5 | Top Left <15:8> |
528 * 6 | Top Left <7:0> |
529 * -----+--------------------------+
530 * 7 | Bottom Left <15:8> |
531 * 8 | Bottom Left <7:0> |
532 * -----+--------------------------+
533 *
534 * These values represent the weight-measurements of the Wii-balance
535 * board with 16bit precision.
536 *
537 * The balance-board is never reported interleaved with motionp.
538 */
539
540 val[0] = payload[0];
541 val[0] <<= 8;
542 val[0] |= payload[1];
543
544 val[1] = payload[2];
545 val[1] <<= 8;
546 val[1] |= payload[3];
547
548 val[2] = payload[4];
549 val[2] <<= 8;
550 val[2] |= payload[5];
551
552 val[3] = payload[6];
553 val[3] <<= 8;
554 val[3] |= payload[7];
555
556 input_report_abs(ext->input, ABS_HAT0X, val[0]);
557 input_report_abs(ext->input, ABS_HAT0Y, val[1]);
558 input_report_abs(ext->input, ABS_HAT1X, val[2]);
559 input_report_abs(ext->input, ABS_HAT1Y, val[3]);
560
561 input_sync(ext->input);
562}
563
David Herrmann0b6815d2011-11-17 14:12:06 +0100564/* call this with state.lock spinlock held */
565void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload)
566{
567 struct wiimote_ext *ext = wdata->ext;
568
569 if (!ext)
570 return;
571
572 if (ext->motionp && (payload[5] & 0x02)) {
573 handler_motionp(ext, payload);
574 } else if (ext->ext_type == WIIEXT_NUNCHUCK) {
575 handler_nunchuck(ext, payload);
576 } else if (ext->ext_type == WIIEXT_CLASSIC) {
577 handler_classic(ext, payload);
David Herrmann5ad67fb2012-09-17 12:31:35 +0200578 } else if (ext->ext_type == WIIEXT_BALANCE_BOARD) {
579 handler_balance_board(ext, payload);
David Herrmann0b6815d2011-11-17 14:12:06 +0100580 }
581}
582
David Herrmannc1e51392011-11-17 14:12:04 +0100583static ssize_t wiiext_show(struct device *dev, struct device_attribute *attr,
584 char *buf)
585{
586 struct wiimote_data *wdata = dev_to_wii(dev);
587 __u8 type = WIIEXT_NONE;
588 bool motionp = false;
589 unsigned long flags;
590
591 spin_lock_irqsave(&wdata->state.lock, flags);
592 if (wdata->ext) {
593 motionp = wdata->ext->motionp;
594 type = wdata->ext->ext_type;
595 }
596 spin_unlock_irqrestore(&wdata->state.lock, flags);
597
598 if (type == WIIEXT_NUNCHUCK) {
599 if (motionp)
600 return sprintf(buf, "motionp+nunchuck\n");
601 else
602 return sprintf(buf, "nunchuck\n");
603 } else if (type == WIIEXT_CLASSIC) {
604 if (motionp)
605 return sprintf(buf, "motionp+classic\n");
606 else
607 return sprintf(buf, "classic\n");
David Herrmann5ad67fb2012-09-17 12:31:35 +0200608 } else if (type == WIIEXT_BALANCE_BOARD) {
609 if (motionp)
610 return sprintf(buf, "motionp+balanceboard\n");
611 else
612 return sprintf(buf, "balanceboard\n");
David Herrmannc1e51392011-11-17 14:12:04 +0100613 } else {
614 if (motionp)
615 return sprintf(buf, "motionp\n");
616 else
617 return sprintf(buf, "none\n");
618 }
619}
620
621static DEVICE_ATTR(extension, S_IRUGO, wiiext_show, NULL);
622
David Herrmann479901b2011-11-17 14:12:05 +0100623static int wiiext_input_open(struct input_dev *dev)
624{
625 struct wiimote_ext *ext = input_get_drvdata(dev);
626 int ret;
627
628 ret = hid_hw_open(ext->wdata->hdev);
629 if (ret)
630 return ret;
631
632 atomic_inc(&ext->opened);
633 wiiext_schedule(ext);
634
635 return 0;
636}
637
638static void wiiext_input_close(struct input_dev *dev)
639{
640 struct wiimote_ext *ext = input_get_drvdata(dev);
641
642 atomic_dec(&ext->opened);
643 wiiext_schedule(ext);
644 hid_hw_close(ext->wdata->hdev);
645}
646
647static int wiiext_mp_open(struct input_dev *dev)
648{
649 struct wiimote_ext *ext = input_get_drvdata(dev);
650 int ret;
651
652 ret = hid_hw_open(ext->wdata->hdev);
653 if (ret)
654 return ret;
655
656 atomic_inc(&ext->mp_opened);
657 wiiext_schedule(ext);
658
659 return 0;
660}
661
662static void wiiext_mp_close(struct input_dev *dev)
663{
664 struct wiimote_ext *ext = input_get_drvdata(dev);
665
666 atomic_dec(&ext->mp_opened);
667 wiiext_schedule(ext);
668 hid_hw_close(ext->wdata->hdev);
669}
670
David Herrmanncb992212011-11-17 14:12:01 +0100671/* Initializes the extension driver of a wiimote */
672int wiiext_init(struct wiimote_data *wdata)
673{
674 struct wiimote_ext *ext;
675 unsigned long flags;
David Herrmanna5353502011-11-17 14:12:08 +0100676 int ret, i;
David Herrmanncb992212011-11-17 14:12:01 +0100677
678 ext = kzalloc(sizeof(*ext), GFP_KERNEL);
679 if (!ext)
680 return -ENOMEM;
681
682 ext->wdata = wdata;
683 INIT_WORK(&ext->worker, wiiext_worker);
684
David Herrmann479901b2011-11-17 14:12:05 +0100685 ext->input = input_allocate_device();
686 if (!ext->input) {
687 ret = -ENOMEM;
688 goto err_input;
689 }
690
691 input_set_drvdata(ext->input, ext);
692 ext->input->open = wiiext_input_open;
693 ext->input->close = wiiext_input_close;
694 ext->input->dev.parent = &wdata->hdev->dev;
695 ext->input->id.bustype = wdata->hdev->bus;
696 ext->input->id.vendor = wdata->hdev->vendor;
697 ext->input->id.product = wdata->hdev->product;
698 ext->input->id.version = wdata->hdev->version;
699 ext->input->name = WIIMOTE_NAME " Extension";
700
David Herrmanna5353502011-11-17 14:12:08 +0100701 set_bit(EV_KEY, ext->input->evbit);
702 for (i = 0; i < WIIEXT_KEY_COUNT; ++i)
703 set_bit(wiiext_keymap[i], ext->input->keybit);
704
705 set_bit(EV_ABS, ext->input->evbit);
706 set_bit(ABS_HAT0X, ext->input->absbit);
707 set_bit(ABS_HAT0Y, ext->input->absbit);
David Herrmann59062152011-11-17 14:12:09 +0100708 set_bit(ABS_HAT1X, ext->input->absbit);
709 set_bit(ABS_HAT1Y, ext->input->absbit);
710 set_bit(ABS_HAT2X, ext->input->absbit);
711 set_bit(ABS_HAT2Y, ext->input->absbit);
712 set_bit(ABS_HAT3X, ext->input->absbit);
713 set_bit(ABS_HAT3Y, ext->input->absbit);
David Herrmanna5353502011-11-17 14:12:08 +0100714 input_set_abs_params(ext->input, ABS_HAT0X, -120, 120, 2, 4);
715 input_set_abs_params(ext->input, ABS_HAT0Y, -120, 120, 2, 4);
David Herrmann59062152011-11-17 14:12:09 +0100716 input_set_abs_params(ext->input, ABS_HAT1X, -30, 30, 1, 1);
717 input_set_abs_params(ext->input, ABS_HAT1Y, -30, 30, 1, 1);
718 input_set_abs_params(ext->input, ABS_HAT2X, -30, 30, 1, 1);
719 input_set_abs_params(ext->input, ABS_HAT2Y, -30, 30, 1, 1);
720 input_set_abs_params(ext->input, ABS_HAT3X, -30, 30, 1, 1);
721 input_set_abs_params(ext->input, ABS_HAT3Y, -30, 30, 1, 1);
David Herrmanna5353502011-11-17 14:12:08 +0100722 set_bit(ABS_RX, ext->input->absbit);
723 set_bit(ABS_RY, ext->input->absbit);
724 set_bit(ABS_RZ, ext->input->absbit);
725 input_set_abs_params(ext->input, ABS_RX, -500, 500, 2, 4);
726 input_set_abs_params(ext->input, ABS_RY, -500, 500, 2, 4);
727 input_set_abs_params(ext->input, ABS_RZ, -500, 500, 2, 4);
728
David Herrmann479901b2011-11-17 14:12:05 +0100729 ret = input_register_device(ext->input);
730 if (ret) {
731 input_free_device(ext->input);
732 goto err_input;
733 }
734
735 ext->mp_input = input_allocate_device();
736 if (!ext->mp_input) {
737 ret = -ENOMEM;
738 goto err_mp;
739 }
740
741 input_set_drvdata(ext->mp_input, ext);
742 ext->mp_input->open = wiiext_mp_open;
743 ext->mp_input->close = wiiext_mp_close;
744 ext->mp_input->dev.parent = &wdata->hdev->dev;
745 ext->mp_input->id.bustype = wdata->hdev->bus;
746 ext->mp_input->id.vendor = wdata->hdev->vendor;
747 ext->mp_input->id.product = wdata->hdev->product;
748 ext->mp_input->id.version = wdata->hdev->version;
749 ext->mp_input->name = WIIMOTE_NAME " Motion+";
750
David Herrmannb17b57a2011-11-17 14:12:07 +0100751 set_bit(EV_ABS, ext->mp_input->evbit);
752 set_bit(ABS_RX, ext->mp_input->absbit);
753 set_bit(ABS_RY, ext->mp_input->absbit);
754 set_bit(ABS_RZ, ext->mp_input->absbit);
755 input_set_abs_params(ext->mp_input, ABS_RX, -160000, 160000, 4, 8);
756 input_set_abs_params(ext->mp_input, ABS_RY, -160000, 160000, 4, 8);
757 input_set_abs_params(ext->mp_input, ABS_RZ, -160000, 160000, 4, 8);
758
David Herrmann479901b2011-11-17 14:12:05 +0100759 ret = input_register_device(ext->mp_input);
760 if (ret) {
761 input_free_device(ext->mp_input);
762 goto err_mp;
763 }
764
David Herrmannc1e51392011-11-17 14:12:04 +0100765 ret = device_create_file(&wdata->hdev->dev, &dev_attr_extension);
766 if (ret)
David Herrmann479901b2011-11-17 14:12:05 +0100767 goto err_dev;
David Herrmannc1e51392011-11-17 14:12:04 +0100768
David Herrmanncb992212011-11-17 14:12:01 +0100769 spin_lock_irqsave(&wdata->state.lock, flags);
770 wdata->ext = ext;
771 spin_unlock_irqrestore(&wdata->state.lock, flags);
772
773 return 0;
David Herrmannc1e51392011-11-17 14:12:04 +0100774
David Herrmann479901b2011-11-17 14:12:05 +0100775err_dev:
776 input_unregister_device(ext->mp_input);
777err_mp:
778 input_unregister_device(ext->input);
779err_input:
David Herrmannc1e51392011-11-17 14:12:04 +0100780 kfree(ext);
781 return ret;
David Herrmanncb992212011-11-17 14:12:01 +0100782}
783
784/* Deinitializes the extension driver of a wiimote */
785void wiiext_deinit(struct wiimote_data *wdata)
786{
787 struct wiimote_ext *ext = wdata->ext;
788 unsigned long flags;
789
790 if (!ext)
791 return;
792
793 /*
794 * We first unset wdata->ext to avoid further input from the wiimote
795 * core. The worker thread does not access this pointer so it is not
796 * affected by this.
797 * We kill the worker after this so it does not get respawned during
798 * deinitialization.
799 */
800
801 spin_lock_irqsave(&wdata->state.lock, flags);
802 wdata->ext = NULL;
803 spin_unlock_irqrestore(&wdata->state.lock, flags);
804
David Herrmannc1e51392011-11-17 14:12:04 +0100805 device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
David Herrmann479901b2011-11-17 14:12:05 +0100806 input_unregister_device(ext->mp_input);
807 input_unregister_device(ext->input);
David Herrmannc1e51392011-11-17 14:12:04 +0100808
David Herrmanncb992212011-11-17 14:12:01 +0100809 cancel_work_sync(&ext->worker);
810 kfree(ext);
811}