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