blob: 71adf9e60b13f4aafa22ba94183bb4dcb8a3a21d [file] [log] [blame]
David Herrmann27f06942013-05-05 23:12:51 +02001/*
2 * Device Modules for Nintendo Wii / Wii U HID Driver
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
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/*
14 * Wiimote Modules
15 * Nintendo devices provide different peripherals and many new devices lack
16 * initial features like the IR camera. Therefore, each peripheral device is
17 * implemented as an independent module and we probe on each device only the
18 * modules for the hardware that really is available.
19 *
20 * Module registration is sequential. Unregistration is done in reverse order.
21 * After device detection, the needed modules are loaded. Users can trigger
22 * re-detection which causes all modules to be unloaded and then reload the
23 * modules for the new detected device.
24 *
25 * wdata->input is a shared input device. It is always initialized prior to
26 * module registration. If at least one registered module is marked as
27 * WIIMOD_FLAG_INPUT, then the input device will get registered after all
28 * modules were registered.
29 * Please note that it is unregistered _before_ the "remove" callbacks are
30 * called. This guarantees that no input interaction is done, anymore. However,
31 * the wiimote core keeps a reference to the input device so it is freed only
32 * after all modules were removed. It is safe to send events to unregistered
33 * input devices.
34 */
35
36#include <linux/device.h>
37#include <linux/hid.h>
38#include <linux/input.h>
39#include <linux/spinlock.h>
40#include "hid-wiimote.h"
41
David Herrmann20cef812013-05-05 23:12:52 +020042/*
43 * Keys
44 * The initial Wii Remote provided a bunch of buttons that are reported as
45 * part of the core protocol. Many later devices dropped these and report
46 * invalid data in the core button reports. Load this only on devices which
47 * correctly send button reports.
48 * It uses the shared input device.
49 */
50
51static const __u16 wiimod_keys_map[] = {
52 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
53 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
54 KEY_UP, /* WIIPROTO_KEY_UP */
55 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
56 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
57 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
58 BTN_1, /* WIIPROTO_KEY_ONE */
59 BTN_2, /* WIIPROTO_KEY_TWO */
60 BTN_A, /* WIIPROTO_KEY_A */
61 BTN_B, /* WIIPROTO_KEY_B */
62 BTN_MODE, /* WIIPROTO_KEY_HOME */
63};
64
65static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
66{
67 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
68 !!(keys[0] & 0x01));
69 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
70 !!(keys[0] & 0x02));
71 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
72 !!(keys[0] & 0x04));
73 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
74 !!(keys[0] & 0x08));
75 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
76 !!(keys[0] & 0x10));
77 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
78 !!(keys[1] & 0x01));
79 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
80 !!(keys[1] & 0x02));
81 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
82 !!(keys[1] & 0x04));
83 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
84 !!(keys[1] & 0x08));
85 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
86 !!(keys[1] & 0x10));
87 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
88 !!(keys[1] & 0x80));
89 input_sync(wdata->input);
90}
91
92static int wiimod_keys_probe(const struct wiimod_ops *ops,
93 struct wiimote_data *wdata)
94{
95 unsigned int i;
96
97 set_bit(EV_KEY, wdata->input->evbit);
98 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
99 set_bit(wiimod_keys_map[i], wdata->input->keybit);
100
101 return 0;
102}
103
104static const struct wiimod_ops wiimod_keys = {
105 .flags = WIIMOD_FLAG_INPUT,
106 .arg = 0,
107 .probe = wiimod_keys_probe,
108 .remove = NULL,
109 .in_keys = wiimod_keys_in_keys,
110};
111
112/*
113 * Rumble
114 * Nearly all devices provide a rumble feature. A small motor for
115 * force-feedback effects. We provide an FF_RUMBLE memless ff device on the
116 * shared input device if this module is loaded.
117 * The rumble motor is controlled via a flag on almost every output report so
118 * the wiimote core handles the rumble flag. But if a device doesn't provide
119 * the rumble motor, this flag shouldn't be set.
120 */
121
David Herrmannf50f9aa2013-10-02 13:47:28 +0200122/* used by wiimod_rumble and wiipro_rumble */
123static void wiimod_rumble_worker(struct work_struct *work)
124{
125 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
126 rumble_worker);
127
128 spin_lock_irq(&wdata->state.lock);
129 wiiproto_req_rumble(wdata, wdata->state.cache_rumble);
130 spin_unlock_irq(&wdata->state.lock);
131}
132
David Herrmann20cef812013-05-05 23:12:52 +0200133static int wiimod_rumble_play(struct input_dev *dev, void *data,
134 struct ff_effect *eff)
135{
136 struct wiimote_data *wdata = input_get_drvdata(dev);
137 __u8 value;
David Herrmann20cef812013-05-05 23:12:52 +0200138
139 /*
140 * The wiimote supports only a single rumble motor so if any magnitude
141 * is set to non-zero then we start the rumble motor. If both are set to
142 * zero, we stop the rumble motor.
143 */
144
145 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
146 value = 1;
147 else
148 value = 0;
149
David Herrmannf50f9aa2013-10-02 13:47:28 +0200150 /* Locking state.lock here might deadlock with input_event() calls.
151 * schedule_work acts as barrier. Merging multiple changes is fine. */
152 wdata->state.cache_rumble = value;
153 schedule_work(&wdata->rumble_worker);
David Herrmann20cef812013-05-05 23:12:52 +0200154
155 return 0;
156}
157
158static int wiimod_rumble_probe(const struct wiimod_ops *ops,
159 struct wiimote_data *wdata)
160{
David Herrmannf50f9aa2013-10-02 13:47:28 +0200161 INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
162
David Herrmann20cef812013-05-05 23:12:52 +0200163 set_bit(FF_RUMBLE, wdata->input->ffbit);
164 if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
165 return -ENOMEM;
166
167 return 0;
168}
169
170static void wiimod_rumble_remove(const struct wiimod_ops *ops,
171 struct wiimote_data *wdata)
172{
173 unsigned long flags;
174
David Herrmannf50f9aa2013-10-02 13:47:28 +0200175 cancel_work_sync(&wdata->rumble_worker);
176
David Herrmann20cef812013-05-05 23:12:52 +0200177 spin_lock_irqsave(&wdata->state.lock, flags);
178 wiiproto_req_rumble(wdata, 0);
179 spin_unlock_irqrestore(&wdata->state.lock, flags);
180}
181
182static const struct wiimod_ops wiimod_rumble = {
183 .flags = WIIMOD_FLAG_INPUT,
184 .arg = 0,
185 .probe = wiimod_rumble_probe,
186 .remove = wiimod_rumble_remove,
187};
188
David Herrmanndcf39232013-05-05 23:12:53 +0200189/*
190 * Battery
191 * 1 byte of battery capacity information is sent along every protocol status
192 * report. The wiimote core caches it but we try to update it on every
193 * user-space request.
194 * This is supported by nearly every device so it's almost always enabled.
195 */
196
197static enum power_supply_property wiimod_battery_props[] = {
198 POWER_SUPPLY_PROP_CAPACITY,
199 POWER_SUPPLY_PROP_SCOPE,
200};
201
202static int wiimod_battery_get_property(struct power_supply *psy,
203 enum power_supply_property psp,
204 union power_supply_propval *val)
205{
206 struct wiimote_data *wdata = container_of(psy, struct wiimote_data,
207 battery);
208 int ret = 0, state;
209 unsigned long flags;
210
211 if (psp == POWER_SUPPLY_PROP_SCOPE) {
212 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
213 return 0;
214 } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
215 return -EINVAL;
216 }
217
218 ret = wiimote_cmd_acquire(wdata);
219 if (ret)
220 return ret;
221
222 spin_lock_irqsave(&wdata->state.lock, flags);
223 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
224 wiiproto_req_status(wdata);
225 spin_unlock_irqrestore(&wdata->state.lock, flags);
226
227 wiimote_cmd_wait(wdata);
228 wiimote_cmd_release(wdata);
229
230 spin_lock_irqsave(&wdata->state.lock, flags);
231 state = wdata->state.cmd_battery;
232 spin_unlock_irqrestore(&wdata->state.lock, flags);
233
234 val->intval = state * 100 / 255;
235 return ret;
236}
237
238static int wiimod_battery_probe(const struct wiimod_ops *ops,
239 struct wiimote_data *wdata)
240{
241 int ret;
242
243 wdata->battery.properties = wiimod_battery_props;
244 wdata->battery.num_properties = ARRAY_SIZE(wiimod_battery_props);
245 wdata->battery.get_property = wiimod_battery_get_property;
246 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
247 wdata->battery.use_for_apm = 0;
248 wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
249 wdata->hdev->uniq);
250 if (!wdata->battery.name)
251 return -ENOMEM;
252
253 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
254 if (ret) {
255 hid_err(wdata->hdev, "cannot register battery device\n");
256 goto err_free;
257 }
258
259 power_supply_powers(&wdata->battery, &wdata->hdev->dev);
260 return 0;
261
262err_free:
263 kfree(wdata->battery.name);
264 wdata->battery.name = NULL;
265 return ret;
266}
267
268static void wiimod_battery_remove(const struct wiimod_ops *ops,
269 struct wiimote_data *wdata)
270{
271 if (!wdata->battery.name)
272 return;
273
274 power_supply_unregister(&wdata->battery);
275 kfree(wdata->battery.name);
276 wdata->battery.name = NULL;
277}
278
279static const struct wiimod_ops wiimod_battery = {
280 .flags = 0,
281 .arg = 0,
282 .probe = wiimod_battery_probe,
283 .remove = wiimod_battery_remove,
284};
285
David Herrmann6c5ae012013-05-05 23:12:54 +0200286/*
287 * LED
288 * 0 to 4 player LEDs are supported by devices. The "arg" field of the
289 * wiimod_ops structure specifies which LED this module controls. This allows
290 * to register a limited number of LEDs.
291 * State is managed by wiimote core.
292 */
293
294static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
295{
296 struct wiimote_data *wdata;
297 struct device *dev = led_dev->dev->parent;
298 int i;
299 unsigned long flags;
300 bool value = false;
301
302 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
303
304 for (i = 0; i < 4; ++i) {
305 if (wdata->leds[i] == led_dev) {
306 spin_lock_irqsave(&wdata->state.lock, flags);
307 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
308 spin_unlock_irqrestore(&wdata->state.lock, flags);
309 break;
310 }
311 }
312
313 return value ? LED_FULL : LED_OFF;
314}
315
316static void wiimod_led_set(struct led_classdev *led_dev,
317 enum led_brightness value)
318{
319 struct wiimote_data *wdata;
320 struct device *dev = led_dev->dev->parent;
321 int i;
322 unsigned long flags;
323 __u8 state, flag;
324
325 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
326
327 for (i = 0; i < 4; ++i) {
328 if (wdata->leds[i] == led_dev) {
329 flag = WIIPROTO_FLAG_LED(i + 1);
330 spin_lock_irqsave(&wdata->state.lock, flags);
331 state = wdata->state.flags;
332 if (value == LED_OFF)
333 wiiproto_req_leds(wdata, state & ~flag);
334 else
335 wiiproto_req_leds(wdata, state | flag);
336 spin_unlock_irqrestore(&wdata->state.lock, flags);
337 break;
338 }
339 }
340}
341
342static int wiimod_led_probe(const struct wiimod_ops *ops,
343 struct wiimote_data *wdata)
344{
345 struct device *dev = &wdata->hdev->dev;
346 size_t namesz = strlen(dev_name(dev)) + 9;
347 struct led_classdev *led;
348 unsigned long flags;
349 char *name;
350 int ret;
351
352 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
353 if (!led)
354 return -ENOMEM;
355
356 name = (void*)&led[1];
357 snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
358 led->name = name;
359 led->brightness = 0;
360 led->max_brightness = 1;
361 led->brightness_get = wiimod_led_get;
362 led->brightness_set = wiimod_led_set;
363
364 wdata->leds[ops->arg] = led;
365 ret = led_classdev_register(dev, led);
366 if (ret)
367 goto err_free;
368
369 /* enable LED1 to stop initial LED-blinking */
370 if (ops->arg == 0) {
371 spin_lock_irqsave(&wdata->state.lock, flags);
372 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
373 spin_unlock_irqrestore(&wdata->state.lock, flags);
374 }
375
376 return 0;
377
378err_free:
379 wdata->leds[ops->arg] = NULL;
380 kfree(led);
381 return ret;
382}
383
384static void wiimod_led_remove(const struct wiimod_ops *ops,
385 struct wiimote_data *wdata)
386{
387 if (!wdata->leds[ops->arg])
388 return;
389
390 led_classdev_unregister(wdata->leds[ops->arg]);
391 kfree(wdata->leds[ops->arg]);
392 wdata->leds[ops->arg] = NULL;
393}
394
395static const struct wiimod_ops wiimod_leds[4] = {
396 {
397 .flags = 0,
398 .arg = 0,
399 .probe = wiimod_led_probe,
400 .remove = wiimod_led_remove,
401 },
402 {
403 .flags = 0,
404 .arg = 1,
405 .probe = wiimod_led_probe,
406 .remove = wiimod_led_remove,
407 },
408 {
409 .flags = 0,
410 .arg = 2,
411 .probe = wiimod_led_probe,
412 .remove = wiimod_led_remove,
413 },
414 {
415 .flags = 0,
416 .arg = 3,
417 .probe = wiimod_led_probe,
418 .remove = wiimod_led_remove,
419 },
420};
421
David Herrmann0ea16752013-05-05 23:12:55 +0200422/*
423 * Accelerometer
424 * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
425 * device, it's mostly cleared to 0. This module parses this data and provides
426 * it via a separate input device.
427 */
428
429static void wiimod_accel_in_accel(struct wiimote_data *wdata,
430 const __u8 *accel)
431{
432 __u16 x, y, z;
433
434 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
435 return;
436
437 /*
438 * payload is: BB BB XX YY ZZ
439 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
440 * contain the upper 8 bits of each value. The lower 2 bits are
441 * contained in the buttons data BB BB.
442 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
443 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
444 * accel value and bit 6 is the second bit of the Z value.
445 * The first bit of Y and Z values is not available and always set to 0.
446 * 0x200 is returned on no movement.
447 */
448
449 x = accel[2] << 2;
450 y = accel[3] << 2;
451 z = accel[4] << 2;
452
453 x |= (accel[0] >> 5) & 0x3;
454 y |= (accel[1] >> 4) & 0x2;
455 z |= (accel[1] >> 5) & 0x2;
456
457 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
458 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
459 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
460 input_sync(wdata->accel);
461}
462
463static int wiimod_accel_open(struct input_dev *dev)
464{
465 struct wiimote_data *wdata = input_get_drvdata(dev);
466 unsigned long flags;
467
468 spin_lock_irqsave(&wdata->state.lock, flags);
469 wiiproto_req_accel(wdata, true);
470 spin_unlock_irqrestore(&wdata->state.lock, flags);
471
472 return 0;
473}
474
475static void wiimod_accel_close(struct input_dev *dev)
476{
477 struct wiimote_data *wdata = input_get_drvdata(dev);
478 unsigned long flags;
479
480 spin_lock_irqsave(&wdata->state.lock, flags);
481 wiiproto_req_accel(wdata, false);
482 spin_unlock_irqrestore(&wdata->state.lock, flags);
483}
484
485static int wiimod_accel_probe(const struct wiimod_ops *ops,
486 struct wiimote_data *wdata)
487{
488 int ret;
489
490 wdata->accel = input_allocate_device();
491 if (!wdata->accel)
492 return -ENOMEM;
493
494 input_set_drvdata(wdata->accel, wdata);
495 wdata->accel->open = wiimod_accel_open;
496 wdata->accel->close = wiimod_accel_close;
497 wdata->accel->dev.parent = &wdata->hdev->dev;
498 wdata->accel->id.bustype = wdata->hdev->bus;
499 wdata->accel->id.vendor = wdata->hdev->vendor;
500 wdata->accel->id.product = wdata->hdev->product;
501 wdata->accel->id.version = wdata->hdev->version;
502 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
503
504 set_bit(EV_ABS, wdata->accel->evbit);
505 set_bit(ABS_RX, wdata->accel->absbit);
506 set_bit(ABS_RY, wdata->accel->absbit);
507 set_bit(ABS_RZ, wdata->accel->absbit);
508 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
509 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
510 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
511
512 ret = input_register_device(wdata->accel);
513 if (ret) {
514 hid_err(wdata->hdev, "cannot register input device\n");
515 goto err_free;
516 }
517
518 return 0;
519
520err_free:
521 input_free_device(wdata->accel);
522 wdata->accel = NULL;
523 return ret;
524}
525
526static void wiimod_accel_remove(const struct wiimod_ops *ops,
527 struct wiimote_data *wdata)
528{
529 if (!wdata->accel)
530 return;
531
532 input_unregister_device(wdata->accel);
533 wdata->accel = NULL;
534}
535
536static const struct wiimod_ops wiimod_accel = {
537 .flags = 0,
538 .arg = 0,
539 .probe = wiimod_accel_probe,
540 .remove = wiimod_accel_remove,
541 .in_accel = wiimod_accel_in_accel,
542};
543
David Herrmann3b5f03c2013-05-05 23:12:56 +0200544/*
545 * IR Cam
546 * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs
547 * to be initialized with a fairly complex procedure and consumes a lot of
548 * power. Therefore, as long as no application uses the IR input device, it is
549 * kept offline.
550 * Nearly no other device than the normal Wii Remotes supports the IR cam so
551 * you can disable this module for these devices.
552 */
553
554static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
555 bool packed, unsigned int id)
556{
557 __u16 x, y;
558 __u8 xid, yid;
559 bool sync = false;
560
561 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
562 return;
563
564 switch (id) {
565 case 0:
566 xid = ABS_HAT0X;
567 yid = ABS_HAT0Y;
568 break;
569 case 1:
570 xid = ABS_HAT1X;
571 yid = ABS_HAT1Y;
572 break;
573 case 2:
574 xid = ABS_HAT2X;
575 yid = ABS_HAT2Y;
576 break;
577 case 3:
578 xid = ABS_HAT3X;
579 yid = ABS_HAT3Y;
580 sync = true;
581 break;
582 default:
583 return;
Jiri Kosina5b22b912013-06-18 16:05:07 +0200584 }
David Herrmann3b5f03c2013-05-05 23:12:56 +0200585
586 /*
587 * Basic IR data is encoded into 3 bytes. The first two bytes are the
588 * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
589 * of both.
590 * If data is packed, then the 3rd byte is put first and slightly
591 * reordered. This allows to interleave packed and non-packed data to
592 * have two IR sets in 5 bytes instead of 6.
593 * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev.
594 */
595
596 if (packed) {
597 x = ir[1] | ((ir[0] & 0x03) << 8);
598 y = ir[2] | ((ir[0] & 0x0c) << 6);
599 } else {
600 x = ir[0] | ((ir[2] & 0x30) << 4);
601 y = ir[1] | ((ir[2] & 0xc0) << 2);
602 }
603
604 input_report_abs(wdata->ir, xid, x);
605 input_report_abs(wdata->ir, yid, y);
606
607 if (sync)
608 input_sync(wdata->ir);
609}
610
611static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
612{
613 int ret;
614 unsigned long flags;
615 __u8 format = 0;
616 static const __u8 data_enable[] = { 0x01 };
617 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
618 0x00, 0xaa, 0x00, 0x64 };
619 static const __u8 data_sens2[] = { 0x63, 0x03 };
620 static const __u8 data_fin[] = { 0x08 };
621
622 spin_lock_irqsave(&wdata->state.lock, flags);
623
624 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
625 spin_unlock_irqrestore(&wdata->state.lock, flags);
626 return 0;
627 }
628
629 if (mode == 0) {
630 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
631 wiiproto_req_ir1(wdata, 0);
632 wiiproto_req_ir2(wdata, 0);
633 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
634 spin_unlock_irqrestore(&wdata->state.lock, flags);
635 return 0;
636 }
637
638 spin_unlock_irqrestore(&wdata->state.lock, flags);
639
640 ret = wiimote_cmd_acquire(wdata);
641 if (ret)
642 return ret;
643
644 /* send PIXEL CLOCK ENABLE cmd first */
645 spin_lock_irqsave(&wdata->state.lock, flags);
646 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
647 wiiproto_req_ir1(wdata, 0x06);
648 spin_unlock_irqrestore(&wdata->state.lock, flags);
649
650 ret = wiimote_cmd_wait(wdata);
651 if (ret)
652 goto unlock;
653 if (wdata->state.cmd_err) {
654 ret = -EIO;
655 goto unlock;
656 }
657
658 /* enable IR LOGIC */
659 spin_lock_irqsave(&wdata->state.lock, flags);
660 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
661 wiiproto_req_ir2(wdata, 0x06);
662 spin_unlock_irqrestore(&wdata->state.lock, flags);
663
664 ret = wiimote_cmd_wait(wdata);
665 if (ret)
666 goto unlock;
667 if (wdata->state.cmd_err) {
668 ret = -EIO;
669 goto unlock;
670 }
671
672 /* enable IR cam but do not make it send data, yet */
673 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
674 sizeof(data_enable));
675 if (ret)
676 goto unlock;
677
678 /* write first sensitivity block */
679 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
680 sizeof(data_sens1));
681 if (ret)
682 goto unlock;
683
684 /* write second sensitivity block */
685 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
686 sizeof(data_sens2));
687 if (ret)
688 goto unlock;
689
690 /* put IR cam into desired state */
691 switch (mode) {
692 case WIIPROTO_FLAG_IR_FULL:
693 format = 5;
694 break;
695 case WIIPROTO_FLAG_IR_EXT:
696 format = 3;
697 break;
698 case WIIPROTO_FLAG_IR_BASIC:
699 format = 1;
700 break;
701 }
702 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
703 if (ret)
704 goto unlock;
705
706 /* make IR cam send data */
707 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
708 if (ret)
709 goto unlock;
710
711 /* request new DRM mode compatible to IR mode */
712 spin_lock_irqsave(&wdata->state.lock, flags);
713 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
714 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
715 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
716 spin_unlock_irqrestore(&wdata->state.lock, flags);
717
718unlock:
719 wiimote_cmd_release(wdata);
720 return ret;
721}
722
723static int wiimod_ir_open(struct input_dev *dev)
724{
725 struct wiimote_data *wdata = input_get_drvdata(dev);
726
727 return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
728}
729
730static void wiimod_ir_close(struct input_dev *dev)
731{
732 struct wiimote_data *wdata = input_get_drvdata(dev);
733
734 wiimod_ir_change(wdata, 0);
735}
736
737static int wiimod_ir_probe(const struct wiimod_ops *ops,
738 struct wiimote_data *wdata)
739{
740 int ret;
741
742 wdata->ir = input_allocate_device();
743 if (!wdata->ir)
744 return -ENOMEM;
745
746 input_set_drvdata(wdata->ir, wdata);
747 wdata->ir->open = wiimod_ir_open;
748 wdata->ir->close = wiimod_ir_close;
749 wdata->ir->dev.parent = &wdata->hdev->dev;
750 wdata->ir->id.bustype = wdata->hdev->bus;
751 wdata->ir->id.vendor = wdata->hdev->vendor;
752 wdata->ir->id.product = wdata->hdev->product;
753 wdata->ir->id.version = wdata->hdev->version;
754 wdata->ir->name = WIIMOTE_NAME " IR";
755
756 set_bit(EV_ABS, wdata->ir->evbit);
757 set_bit(ABS_HAT0X, wdata->ir->absbit);
758 set_bit(ABS_HAT0Y, wdata->ir->absbit);
759 set_bit(ABS_HAT1X, wdata->ir->absbit);
760 set_bit(ABS_HAT1Y, wdata->ir->absbit);
761 set_bit(ABS_HAT2X, wdata->ir->absbit);
762 set_bit(ABS_HAT2Y, wdata->ir->absbit);
763 set_bit(ABS_HAT3X, wdata->ir->absbit);
764 set_bit(ABS_HAT3Y, wdata->ir->absbit);
765 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
766 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
767 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
768 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
769 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
770 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
771 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
772 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
773
774 ret = input_register_device(wdata->ir);
775 if (ret) {
776 hid_err(wdata->hdev, "cannot register input device\n");
777 goto err_free;
778 }
779
780 return 0;
781
782err_free:
783 input_free_device(wdata->ir);
784 wdata->ir = NULL;
785 return ret;
786}
787
788static void wiimod_ir_remove(const struct wiimod_ops *ops,
789 struct wiimote_data *wdata)
790{
791 if (!wdata->ir)
792 return;
793
794 input_unregister_device(wdata->ir);
795 wdata->ir = NULL;
796}
797
798static const struct wiimod_ops wiimod_ir = {
799 .flags = 0,
800 .arg = 0,
801 .probe = wiimod_ir_probe,
802 .remove = wiimod_ir_remove,
803 .in_ir = wiimod_ir_in_ir,
804};
805
David Herrmann4148b6b2013-05-05 23:12:57 +0200806/*
David Herrmannb6ee67b2013-05-05 23:12:59 +0200807 * Nunchuk Extension
808 * The Nintendo Wii Nunchuk was the first official extension published by
809 * Nintendo. It provides two additional keys and a separate accelerometer. It
810 * can be hotplugged to standard Wii Remotes.
811 */
812
813enum wiimod_nunchuk_keys {
814 WIIMOD_NUNCHUK_KEY_C,
815 WIIMOD_NUNCHUK_KEY_Z,
816 WIIMOD_NUNCHUK_KEY_NUM,
817};
818
819static const __u16 wiimod_nunchuk_map[] = {
820 BTN_C, /* WIIMOD_NUNCHUK_KEY_C */
821 BTN_Z, /* WIIMOD_NUNCHUK_KEY_Z */
822};
823
824static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
825{
826 __s16 x, y, z, bx, by;
827
828 /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 |
829 * -----+----------+---------+---------+----+-----+
830 * 1 | Button X <7:0> |
831 * 2 | Button Y <7:0> |
832 * -----+----------+---------+---------+----+-----+
833 * 3 | Speed X <9:2> |
834 * 4 | Speed Y <9:2> |
835 * 5 | Speed Z <9:2> |
836 * -----+----------+---------+---------+----+-----+
837 * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ |
838 * -----+----------+---------+---------+----+-----+
839 * Button X/Y is the analog stick. Speed X, Y and Z are the
840 * accelerometer data in the same format as the wiimote's accelerometer.
841 * The 6th byte contains the LSBs of the accelerometer data.
842 * BC and BZ are the C and Z buttons: 0 means pressed
843 *
844 * If reported interleaved with motionp, then the layout changes. The
845 * 5th and 6th byte changes to:
846 * -----+-----------------------------------+-----+
847 * 5 | Speed Z <9:3> | EXT |
848 * -----+--------+-----+-----+----+----+----+-----+
849 * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 |
850 * -----+--------+-----+-----+----+----+----+-----+
851 * All three accelerometer values lose their LSB. The other data is
852 * still available but slightly moved.
853 *
854 * Center data for button values is 128. Center value for accelerometer
855 * values it 512 / 0x200
856 */
857
858 bx = ext[0];
859 by = ext[1];
860 bx -= 128;
861 by -= 128;
862
863 x = ext[2] << 2;
864 y = ext[3] << 2;
865 z = ext[4] << 2;
866
867 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
868 x |= (ext[5] >> 3) & 0x02;
869 y |= (ext[5] >> 4) & 0x02;
870 z &= ~0x4;
871 z |= (ext[5] >> 5) & 0x06;
872 } else {
873 x |= (ext[5] >> 2) & 0x03;
874 y |= (ext[5] >> 4) & 0x03;
875 z |= (ext[5] >> 6) & 0x03;
876 }
877
878 x -= 0x200;
879 y -= 0x200;
880 z -= 0x200;
881
882 input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
883 input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
884
885 input_report_abs(wdata->extension.input, ABS_RX, x);
886 input_report_abs(wdata->extension.input, ABS_RY, y);
887 input_report_abs(wdata->extension.input, ABS_RZ, z);
888
889 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
890 input_report_key(wdata->extension.input,
891 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
892 !(ext[5] & 0x04));
893 input_report_key(wdata->extension.input,
894 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
895 !(ext[5] & 0x08));
896 } else {
897 input_report_key(wdata->extension.input,
898 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
899 !(ext[5] & 0x01));
900 input_report_key(wdata->extension.input,
901 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
902 !(ext[5] & 0x02));
903 }
904
905 input_sync(wdata->extension.input);
906}
907
908static int wiimod_nunchuk_open(struct input_dev *dev)
909{
910 struct wiimote_data *wdata = input_get_drvdata(dev);
911 unsigned long flags;
912
913 spin_lock_irqsave(&wdata->state.lock, flags);
914 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
915 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
916 spin_unlock_irqrestore(&wdata->state.lock, flags);
917
918 return 0;
919}
920
921static void wiimod_nunchuk_close(struct input_dev *dev)
922{
923 struct wiimote_data *wdata = input_get_drvdata(dev);
924 unsigned long flags;
925
926 spin_lock_irqsave(&wdata->state.lock, flags);
927 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
928 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
929 spin_unlock_irqrestore(&wdata->state.lock, flags);
930}
931
932static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
933 struct wiimote_data *wdata)
934{
935 int ret, i;
936
937 wdata->extension.input = input_allocate_device();
938 if (!wdata->extension.input)
939 return -ENOMEM;
940
941 input_set_drvdata(wdata->extension.input, wdata);
942 wdata->extension.input->open = wiimod_nunchuk_open;
943 wdata->extension.input->close = wiimod_nunchuk_close;
944 wdata->extension.input->dev.parent = &wdata->hdev->dev;
945 wdata->extension.input->id.bustype = wdata->hdev->bus;
946 wdata->extension.input->id.vendor = wdata->hdev->vendor;
947 wdata->extension.input->id.product = wdata->hdev->product;
948 wdata->extension.input->id.version = wdata->hdev->version;
949 wdata->extension.input->name = WIIMOTE_NAME " Nunchuk";
950
951 set_bit(EV_KEY, wdata->extension.input->evbit);
952 for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i)
953 set_bit(wiimod_nunchuk_map[i],
954 wdata->extension.input->keybit);
955
956 set_bit(EV_ABS, wdata->extension.input->evbit);
957 set_bit(ABS_HAT0X, wdata->extension.input->absbit);
958 set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
959 input_set_abs_params(wdata->extension.input,
960 ABS_HAT0X, -120, 120, 2, 4);
961 input_set_abs_params(wdata->extension.input,
962 ABS_HAT0Y, -120, 120, 2, 4);
963 set_bit(ABS_RX, wdata->extension.input->absbit);
964 set_bit(ABS_RY, wdata->extension.input->absbit);
965 set_bit(ABS_RZ, wdata->extension.input->absbit);
966 input_set_abs_params(wdata->extension.input,
967 ABS_RX, -500, 500, 2, 4);
968 input_set_abs_params(wdata->extension.input,
969 ABS_RY, -500, 500, 2, 4);
970 input_set_abs_params(wdata->extension.input,
971 ABS_RZ, -500, 500, 2, 4);
972
973 ret = input_register_device(wdata->extension.input);
974 if (ret)
975 goto err_free;
976
977 return 0;
978
979err_free:
980 input_free_device(wdata->extension.input);
981 wdata->extension.input = NULL;
982 return ret;
983}
984
985static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
986 struct wiimote_data *wdata)
987{
988 if (!wdata->extension.input)
989 return;
990
991 input_unregister_device(wdata->extension.input);
992 wdata->extension.input = NULL;
993}
994
995static const struct wiimod_ops wiimod_nunchuk = {
996 .flags = 0,
997 .arg = 0,
998 .probe = wiimod_nunchuk_probe,
999 .remove = wiimod_nunchuk_remove,
1000 .in_ext = wiimod_nunchuk_in_ext,
1001};
1002
1003/*
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001004 * Classic Controller
1005 * Another official extension from Nintendo. It provides a classic
1006 * gamecube-like controller that can be hotplugged on the Wii Remote.
1007 * It has several hardware buttons and switches that are all reported via
1008 * a normal extension device.
1009 */
1010
1011enum wiimod_classic_keys {
1012 WIIMOD_CLASSIC_KEY_A,
1013 WIIMOD_CLASSIC_KEY_B,
1014 WIIMOD_CLASSIC_KEY_X,
1015 WIIMOD_CLASSIC_KEY_Y,
1016 WIIMOD_CLASSIC_KEY_ZL,
1017 WIIMOD_CLASSIC_KEY_ZR,
1018 WIIMOD_CLASSIC_KEY_PLUS,
1019 WIIMOD_CLASSIC_KEY_MINUS,
1020 WIIMOD_CLASSIC_KEY_HOME,
1021 WIIMOD_CLASSIC_KEY_LEFT,
1022 WIIMOD_CLASSIC_KEY_RIGHT,
1023 WIIMOD_CLASSIC_KEY_UP,
1024 WIIMOD_CLASSIC_KEY_DOWN,
1025 WIIMOD_CLASSIC_KEY_LT,
1026 WIIMOD_CLASSIC_KEY_RT,
1027 WIIMOD_CLASSIC_KEY_NUM,
1028};
1029
1030static const __u16 wiimod_classic_map[] = {
1031 BTN_A, /* WIIMOD_CLASSIC_KEY_A */
1032 BTN_B, /* WIIMOD_CLASSIC_KEY_B */
1033 BTN_X, /* WIIMOD_CLASSIC_KEY_X */
1034 BTN_Y, /* WIIMOD_CLASSIC_KEY_Y */
1035 BTN_TL2, /* WIIMOD_CLASSIC_KEY_ZL */
1036 BTN_TR2, /* WIIMOD_CLASSIC_KEY_ZR */
1037 KEY_NEXT, /* WIIMOD_CLASSIC_KEY_PLUS */
1038 KEY_PREVIOUS, /* WIIMOD_CLASSIC_KEY_MINUS */
1039 BTN_MODE, /* WIIMOD_CLASSIC_KEY_HOME */
1040 KEY_LEFT, /* WIIMOD_CLASSIC_KEY_LEFT */
1041 KEY_RIGHT, /* WIIMOD_CLASSIC_KEY_RIGHT */
1042 KEY_UP, /* WIIMOD_CLASSIC_KEY_UP */
1043 KEY_DOWN, /* WIIMOD_CLASSIC_KEY_DOWN */
1044 BTN_TL, /* WIIMOD_CLASSIC_KEY_LT */
1045 BTN_TR, /* WIIMOD_CLASSIC_KEY_RT */
1046};
1047
1048static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1049{
1050 __s8 rx, ry, lx, ly, lt, rt;
1051
1052 /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1053 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1054 * 1 | RX <5:4> | LX <5:0> |
1055 * 2 | RX <3:2> | LY <5:0> |
1056 * -----+-----+-----+-----+-----------------------------+
1057 * 3 |RX<1>| LT <5:4> | RY <5:1> |
1058 * -----+-----+-----------+-----------------------------+
1059 * 4 | LT <3:1> | RT <5:1> |
1060 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1061 * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
1062 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1063 * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
1064 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1065 * All buttons are 0 if pressed
1066 * RX and RY are right analog stick
1067 * LX and LY are left analog stick
1068 * LT is left trigger, RT is right trigger
1069 * BLT is 0 if left trigger is fully pressed
1070 * BRT is 0 if right trigger is fully pressed
1071 * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1072 * BZL is left Z button and BZR is right Z button
1073 * B-, BH, B+ are +, HOME and - buttons
1074 * BB, BY, BA, BX are A, B, X, Y buttons
1075 * LSB of RX, RY, LT, and RT are not transmitted and always 0.
1076 *
1077 * With motionp enabled it changes slightly to this:
1078 * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1079 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
David Herrmannee286c22013-05-26 22:55:04 +02001080 * 1 | RX <5:4> | LX <5:1> | BDU |
1081 * 2 | RX <3:2> | LY <5:1> | BDL |
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001082 * -----+-----+-----+-----+-----------------------+-----+
David Herrmannee286c22013-05-26 22:55:04 +02001083 * 3 |RX<1>| LT <5:4> | RY <5:1> |
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001084 * -----+-----+-----------+-----------------------------+
David Herrmannee286c22013-05-26 22:55:04 +02001085 * 4 | LT <3:1> | RT <5:1> |
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001086 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1087 * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT |
1088 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1089 * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 |
1090 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1091 * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
1092 * is the same as before.
1093 */
1094
1095 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1096 lx = ext[0] & 0x3e;
David Herrmannee286c22013-05-26 22:55:04 +02001097 ly = ext[1] & 0x3e;
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001098 } else {
1099 lx = ext[0] & 0x3f;
David Herrmannee286c22013-05-26 22:55:04 +02001100 ly = ext[1] & 0x3f;
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001101 }
1102
David Herrmannee286c22013-05-26 22:55:04 +02001103 rx = (ext[0] >> 3) & 0x18;
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001104 rx |= (ext[1] >> 5) & 0x06;
1105 rx |= (ext[2] >> 7) & 0x01;
1106 ry = ext[2] & 0x1f;
1107
1108 rt = ext[3] & 0x1f;
1109 lt = (ext[2] >> 2) & 0x18;
1110 lt |= (ext[3] >> 5) & 0x07;
1111
1112 rx <<= 1;
1113 ry <<= 1;
1114 rt <<= 1;
1115 lt <<= 1;
1116
1117 input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
1118 input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
1119 input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
1120 input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
David Herrmannee286c22013-05-26 22:55:04 +02001121 input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
1122 input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
David Herrmann9d6f9ec2013-05-05 23:13:00 +02001123
1124 input_report_key(wdata->extension.input,
1125 wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
1126 !(ext[4] & 0x80));
1127 input_report_key(wdata->extension.input,
1128 wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
1129 !(ext[4] & 0x40));
1130 input_report_key(wdata->extension.input,
1131 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
1132 !(ext[4] & 0x20));
1133 input_report_key(wdata->extension.input,
1134 wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS],
1135 !(ext[4] & 0x10));
1136 input_report_key(wdata->extension.input,
1137 wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME],
1138 !(ext[4] & 0x08));
1139 input_report_key(wdata->extension.input,
1140 wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS],
1141 !(ext[4] & 0x04));
1142 input_report_key(wdata->extension.input,
1143 wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT],
1144 !(ext[4] & 0x02));
1145 input_report_key(wdata->extension.input,
1146 wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL],
1147 !(ext[5] & 0x80));
1148 input_report_key(wdata->extension.input,
1149 wiimod_classic_map[WIIMOD_CLASSIC_KEY_B],
1150 !(ext[5] & 0x40));
1151 input_report_key(wdata->extension.input,
1152 wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y],
1153 !(ext[5] & 0x20));
1154 input_report_key(wdata->extension.input,
1155 wiimod_classic_map[WIIMOD_CLASSIC_KEY_A],
1156 !(ext[5] & 0x10));
1157 input_report_key(wdata->extension.input,
1158 wiimod_classic_map[WIIMOD_CLASSIC_KEY_X],
1159 !(ext[5] & 0x08));
1160 input_report_key(wdata->extension.input,
1161 wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
1162 !(ext[5] & 0x04));
1163
1164 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1165 input_report_key(wdata->extension.input,
1166 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1167 !(ext[1] & 0x01));
1168 input_report_key(wdata->extension.input,
1169 wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1170 !(ext[0] & 0x01));
1171 } else {
1172 input_report_key(wdata->extension.input,
1173 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1174 !(ext[5] & 0x02));
1175 input_report_key(wdata->extension.input,
1176 wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1177 !(ext[5] & 0x01));
1178 }
1179
1180 input_sync(wdata->extension.input);
1181}
1182
1183static int wiimod_classic_open(struct input_dev *dev)
1184{
1185 struct wiimote_data *wdata = input_get_drvdata(dev);
1186 unsigned long flags;
1187
1188 spin_lock_irqsave(&wdata->state.lock, flags);
1189 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1190 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1191 spin_unlock_irqrestore(&wdata->state.lock, flags);
1192
1193 return 0;
1194}
1195
1196static void wiimod_classic_close(struct input_dev *dev)
1197{
1198 struct wiimote_data *wdata = input_get_drvdata(dev);
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&wdata->state.lock, flags);
1202 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1203 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1204 spin_unlock_irqrestore(&wdata->state.lock, flags);
1205}
1206
1207static int wiimod_classic_probe(const struct wiimod_ops *ops,
1208 struct wiimote_data *wdata)
1209{
1210 int ret, i;
1211
1212 wdata->extension.input = input_allocate_device();
1213 if (!wdata->extension.input)
1214 return -ENOMEM;
1215
1216 input_set_drvdata(wdata->extension.input, wdata);
1217 wdata->extension.input->open = wiimod_classic_open;
1218 wdata->extension.input->close = wiimod_classic_close;
1219 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1220 wdata->extension.input->id.bustype = wdata->hdev->bus;
1221 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1222 wdata->extension.input->id.product = wdata->hdev->product;
1223 wdata->extension.input->id.version = wdata->hdev->version;
1224 wdata->extension.input->name = WIIMOTE_NAME " Classic Controller";
1225
1226 set_bit(EV_KEY, wdata->extension.input->evbit);
1227 for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i)
1228 set_bit(wiimod_classic_map[i],
1229 wdata->extension.input->keybit);
1230
1231 set_bit(EV_ABS, wdata->extension.input->evbit);
1232 set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1233 set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1234 set_bit(ABS_HAT2X, wdata->extension.input->absbit);
1235 set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
1236 set_bit(ABS_HAT3X, wdata->extension.input->absbit);
1237 set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
1238 input_set_abs_params(wdata->extension.input,
1239 ABS_HAT1X, -30, 30, 1, 1);
1240 input_set_abs_params(wdata->extension.input,
1241 ABS_HAT1Y, -30, 30, 1, 1);
1242 input_set_abs_params(wdata->extension.input,
1243 ABS_HAT2X, -30, 30, 1, 1);
1244 input_set_abs_params(wdata->extension.input,
1245 ABS_HAT2Y, -30, 30, 1, 1);
1246 input_set_abs_params(wdata->extension.input,
1247 ABS_HAT3X, -30, 30, 1, 1);
1248 input_set_abs_params(wdata->extension.input,
1249 ABS_HAT3Y, -30, 30, 1, 1);
1250
1251 ret = input_register_device(wdata->extension.input);
1252 if (ret)
1253 goto err_free;
1254
1255 return 0;
1256
1257err_free:
1258 input_free_device(wdata->extension.input);
1259 wdata->extension.input = NULL;
1260 return ret;
1261}
1262
1263static void wiimod_classic_remove(const struct wiimod_ops *ops,
1264 struct wiimote_data *wdata)
1265{
1266 if (!wdata->extension.input)
1267 return;
1268
1269 input_unregister_device(wdata->extension.input);
1270 wdata->extension.input = NULL;
1271}
1272
1273static const struct wiimod_ops wiimod_classic = {
1274 .flags = 0,
1275 .arg = 0,
1276 .probe = wiimod_classic_probe,
1277 .remove = wiimod_classic_remove,
1278 .in_ext = wiimod_classic_in_ext,
1279};
1280
1281/*
David Herrmannf1d4bed2013-05-05 23:12:58 +02001282 * Balance Board Extension
1283 * The Nintendo Wii Balance Board provides four hardware weight sensor plus a
1284 * single push button. No other peripherals are available. However, the
1285 * balance-board data is sent via a standard Wii Remote extension. All other
1286 * data for non-present hardware is zeroed out.
1287 * Some 3rd party devices react allergic if we try to access normal Wii Remote
1288 * hardware, so this extension module should be the only module that is loaded
1289 * on balance boards.
1290 * The balance board needs 8 bytes extension data instead of basic 6 bytes so
1291 * it needs the WIIMOD_FLAG_EXT8 flag.
1292 */
1293
1294static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
1295{
1296 input_report_key(wdata->extension.input, BTN_A,
1297 !!(keys[1] & 0x08));
1298 input_sync(wdata->extension.input);
1299}
1300
1301static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
1302 const __u8 *ext)
1303{
1304 __s32 val[4], tmp, div;
1305 unsigned int i;
1306 struct wiimote_state *s = &wdata->state;
1307
1308 /*
1309 * Balance board data layout:
1310 *
1311 * Byte | 8 7 6 5 4 3 2 1 |
1312 * -----+--------------------------+
1313 * 1 | Top Right <15:8> |
1314 * 2 | Top Right <7:0> |
1315 * -----+--------------------------+
1316 * 3 | Bottom Right <15:8> |
1317 * 4 | Bottom Right <7:0> |
1318 * -----+--------------------------+
1319 * 5 | Top Left <15:8> |
1320 * 6 | Top Left <7:0> |
1321 * -----+--------------------------+
1322 * 7 | Bottom Left <15:8> |
1323 * 8 | Bottom Left <7:0> |
1324 * -----+--------------------------+
1325 *
1326 * These values represent the weight-measurements of the Wii-balance
1327 * board with 16bit precision.
1328 *
1329 * The balance-board is never reported interleaved with motionp.
1330 */
1331
1332 val[0] = ext[0];
1333 val[0] <<= 8;
1334 val[0] |= ext[1];
1335
1336 val[1] = ext[2];
1337 val[1] <<= 8;
1338 val[1] |= ext[3];
1339
1340 val[2] = ext[4];
1341 val[2] <<= 8;
1342 val[2] |= ext[5];
1343
1344 val[3] = ext[6];
1345 val[3] <<= 8;
1346 val[3] |= ext[7];
1347
1348 /* apply calibration data */
1349 for (i = 0; i < 4; i++) {
1350 if (val[i] <= s->calib_bboard[i][0]) {
1351 tmp = 0;
1352 } else if (val[i] < s->calib_bboard[i][1]) {
1353 tmp = val[i] - s->calib_bboard[i][0];
1354 tmp *= 1700;
1355 div = s->calib_bboard[i][1] - s->calib_bboard[i][0];
1356 tmp /= div ? div : 1;
1357 } else {
1358 tmp = val[i] - s->calib_bboard[i][1];
1359 tmp *= 1700;
1360 div = s->calib_bboard[i][2] - s->calib_bboard[i][1];
1361 tmp /= div ? div : 1;
1362 tmp += 1700;
1363 }
1364 val[i] = tmp;
1365 }
1366
1367 input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]);
1368 input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]);
1369 input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]);
1370 input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]);
1371 input_sync(wdata->extension.input);
1372}
1373
1374static int wiimod_bboard_open(struct input_dev *dev)
1375{
1376 struct wiimote_data *wdata = input_get_drvdata(dev);
1377 unsigned long flags;
1378
1379 spin_lock_irqsave(&wdata->state.lock, flags);
1380 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1381 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1382 spin_unlock_irqrestore(&wdata->state.lock, flags);
1383
1384 return 0;
1385}
1386
1387static void wiimod_bboard_close(struct input_dev *dev)
1388{
1389 struct wiimote_data *wdata = input_get_drvdata(dev);
1390 unsigned long flags;
1391
1392 spin_lock_irqsave(&wdata->state.lock, flags);
1393 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1394 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1395 spin_unlock_irqrestore(&wdata->state.lock, flags);
1396}
1397
David Herrmann8b1fded2013-05-05 23:13:05 +02001398static ssize_t wiimod_bboard_calib_show(struct device *dev,
1399 struct device_attribute *attr,
1400 char *out)
1401{
1402 struct wiimote_data *wdata = dev_to_wii(dev);
1403 int i, j, ret;
1404 __u16 val;
1405 __u8 buf[24], offs;
1406
1407 ret = wiimote_cmd_acquire(wdata);
1408 if (ret)
1409 return ret;
1410
1411 ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1412 if (ret != 12) {
1413 wiimote_cmd_release(wdata);
1414 return ret < 0 ? ret : -EIO;
1415 }
1416 ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1417 if (ret != 12) {
1418 wiimote_cmd_release(wdata);
1419 return ret < 0 ? ret : -EIO;
1420 }
1421
1422 wiimote_cmd_release(wdata);
1423
1424 spin_lock_irq(&wdata->state.lock);
1425 offs = 0;
1426 for (i = 0; i < 3; ++i) {
1427 for (j = 0; j < 4; ++j) {
1428 wdata->state.calib_bboard[j][i] = buf[offs];
1429 wdata->state.calib_bboard[j][i] <<= 8;
1430 wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1431 offs += 2;
1432 }
1433 }
1434 spin_unlock_irq(&wdata->state.lock);
1435
1436 ret = 0;
1437 for (i = 0; i < 3; ++i) {
1438 for (j = 0; j < 4; ++j) {
1439 val = wdata->state.calib_bboard[j][i];
1440 if (i == 2 && j == 3)
1441 ret += sprintf(&out[ret], "%04x\n", val);
1442 else
1443 ret += sprintf(&out[ret], "%04x:", val);
1444 }
1445 }
1446
1447 return ret;
1448}
1449
1450static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
1451
David Herrmannf1d4bed2013-05-05 23:12:58 +02001452static int wiimod_bboard_probe(const struct wiimod_ops *ops,
1453 struct wiimote_data *wdata)
1454{
1455 int ret, i, j;
1456 __u8 buf[24], offs;
1457
1458 wiimote_cmd_acquire_noint(wdata);
1459
1460 ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1461 if (ret != 12) {
1462 wiimote_cmd_release(wdata);
1463 return ret < 0 ? ret : -EIO;
1464 }
1465 ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1466 if (ret != 12) {
1467 wiimote_cmd_release(wdata);
1468 return ret < 0 ? ret : -EIO;
1469 }
1470
1471 wiimote_cmd_release(wdata);
1472
1473 offs = 0;
1474 for (i = 0; i < 3; ++i) {
1475 for (j = 0; j < 4; ++j) {
1476 wdata->state.calib_bboard[j][i] = buf[offs];
1477 wdata->state.calib_bboard[j][i] <<= 8;
1478 wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1479 offs += 2;
1480 }
1481 }
1482
1483 wdata->extension.input = input_allocate_device();
1484 if (!wdata->extension.input)
1485 return -ENOMEM;
1486
David Herrmann8b1fded2013-05-05 23:13:05 +02001487 ret = device_create_file(&wdata->hdev->dev,
1488 &dev_attr_bboard_calib);
1489 if (ret) {
1490 hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1491 goto err_free;
1492 }
1493
David Herrmannf1d4bed2013-05-05 23:12:58 +02001494 input_set_drvdata(wdata->extension.input, wdata);
1495 wdata->extension.input->open = wiimod_bboard_open;
1496 wdata->extension.input->close = wiimod_bboard_close;
1497 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1498 wdata->extension.input->id.bustype = wdata->hdev->bus;
1499 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1500 wdata->extension.input->id.product = wdata->hdev->product;
1501 wdata->extension.input->id.version = wdata->hdev->version;
1502 wdata->extension.input->name = WIIMOTE_NAME " Balance Board";
1503
1504 set_bit(EV_KEY, wdata->extension.input->evbit);
1505 set_bit(BTN_A, wdata->extension.input->keybit);
1506
1507 set_bit(EV_ABS, wdata->extension.input->evbit);
1508 set_bit(ABS_HAT0X, wdata->extension.input->absbit);
1509 set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
1510 set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1511 set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1512 input_set_abs_params(wdata->extension.input,
1513 ABS_HAT0X, 0, 65535, 2, 4);
1514 input_set_abs_params(wdata->extension.input,
1515 ABS_HAT0Y, 0, 65535, 2, 4);
1516 input_set_abs_params(wdata->extension.input,
1517 ABS_HAT1X, 0, 65535, 2, 4);
1518 input_set_abs_params(wdata->extension.input,
1519 ABS_HAT1Y, 0, 65535, 2, 4);
1520
1521 ret = input_register_device(wdata->extension.input);
1522 if (ret)
David Herrmann8b1fded2013-05-05 23:13:05 +02001523 goto err_file;
David Herrmannf1d4bed2013-05-05 23:12:58 +02001524
1525 return 0;
1526
David Herrmann8b1fded2013-05-05 23:13:05 +02001527err_file:
1528 device_remove_file(&wdata->hdev->dev,
1529 &dev_attr_bboard_calib);
David Herrmannf1d4bed2013-05-05 23:12:58 +02001530err_free:
1531 input_free_device(wdata->extension.input);
1532 wdata->extension.input = NULL;
1533 return ret;
1534}
1535
1536static void wiimod_bboard_remove(const struct wiimod_ops *ops,
1537 struct wiimote_data *wdata)
1538{
1539 if (!wdata->extension.input)
1540 return;
1541
1542 input_unregister_device(wdata->extension.input);
1543 wdata->extension.input = NULL;
David Herrmann8b1fded2013-05-05 23:13:05 +02001544 device_remove_file(&wdata->hdev->dev,
1545 &dev_attr_bboard_calib);
David Herrmannf1d4bed2013-05-05 23:12:58 +02001546}
1547
1548static const struct wiimod_ops wiimod_bboard = {
1549 .flags = WIIMOD_FLAG_EXT8,
1550 .arg = 0,
1551 .probe = wiimod_bboard_probe,
1552 .remove = wiimod_bboard_remove,
1553 .in_keys = wiimod_bboard_in_keys,
1554 .in_ext = wiimod_bboard_in_ext,
1555};
1556
1557/*
David Herrmannb8e0fe32013-06-15 15:32:45 +02001558 * Pro Controller
1559 * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not
1560 * work together with the classic Wii, but only with the new Wii U. However, it
1561 * uses the same protocol and provides a builtin "classic controller pro"
1562 * extension, few standard buttons, a rumble motor, 4 LEDs and a battery.
1563 * We provide all these via a standard extension device as the device doesn't
1564 * feature an extension port.
1565 */
1566
1567enum wiimod_pro_keys {
1568 WIIMOD_PRO_KEY_A,
1569 WIIMOD_PRO_KEY_B,
1570 WIIMOD_PRO_KEY_X,
1571 WIIMOD_PRO_KEY_Y,
1572 WIIMOD_PRO_KEY_PLUS,
1573 WIIMOD_PRO_KEY_MINUS,
1574 WIIMOD_PRO_KEY_HOME,
1575 WIIMOD_PRO_KEY_LEFT,
1576 WIIMOD_PRO_KEY_RIGHT,
1577 WIIMOD_PRO_KEY_UP,
1578 WIIMOD_PRO_KEY_DOWN,
1579 WIIMOD_PRO_KEY_TL,
1580 WIIMOD_PRO_KEY_TR,
1581 WIIMOD_PRO_KEY_ZL,
1582 WIIMOD_PRO_KEY_ZR,
1583 WIIMOD_PRO_KEY_THUMBL,
1584 WIIMOD_PRO_KEY_THUMBR,
1585 WIIMOD_PRO_KEY_NUM,
1586};
1587
1588static const __u16 wiimod_pro_map[] = {
1589 BTN_EAST, /* WIIMOD_PRO_KEY_A */
1590 BTN_SOUTH, /* WIIMOD_PRO_KEY_B */
1591 BTN_NORTH, /* WIIMOD_PRO_KEY_X */
1592 BTN_WEST, /* WIIMOD_PRO_KEY_Y */
1593 BTN_START, /* WIIMOD_PRO_KEY_PLUS */
1594 BTN_SELECT, /* WIIMOD_PRO_KEY_MINUS */
1595 BTN_MODE, /* WIIMOD_PRO_KEY_HOME */
1596 BTN_DPAD_LEFT, /* WIIMOD_PRO_KEY_LEFT */
1597 BTN_DPAD_RIGHT, /* WIIMOD_PRO_KEY_RIGHT */
1598 BTN_DPAD_UP, /* WIIMOD_PRO_KEY_UP */
1599 BTN_DPAD_DOWN, /* WIIMOD_PRO_KEY_DOWN */
1600 BTN_TL, /* WIIMOD_PRO_KEY_TL */
1601 BTN_TR, /* WIIMOD_PRO_KEY_TR */
1602 BTN_TL2, /* WIIMOD_PRO_KEY_ZL */
1603 BTN_TR2, /* WIIMOD_PRO_KEY_ZR */
1604 BTN_THUMBL, /* WIIMOD_PRO_KEY_THUMBL */
1605 BTN_THUMBR, /* WIIMOD_PRO_KEY_THUMBR */
1606};
1607
1608static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1609{
1610 __s16 rx, ry, lx, ly;
1611
1612 /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1613 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1614 * 1 | LX <7:0> |
1615 * -----+-----------------------+-----------------------+
1616 * 2 | 0 0 0 0 | LX <11:8> |
1617 * -----+-----------------------+-----------------------+
1618 * 3 | RX <7:0> |
1619 * -----+-----------------------+-----------------------+
1620 * 4 | 0 0 0 0 | RX <11:8> |
1621 * -----+-----------------------+-----------------------+
1622 * 5 | LY <7:0> |
1623 * -----+-----------------------+-----------------------+
1624 * 6 | 0 0 0 0 | LY <11:8> |
1625 * -----+-----------------------+-----------------------+
1626 * 7 | RY <7:0> |
1627 * -----+-----------------------+-----------------------+
1628 * 8 | 0 0 0 0 | RY <11:8> |
1629 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1630 * 9 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
1631 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1632 * 10 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
1633 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1634 * 11 | 1 | BATTERY | USB |CHARG|LTHUM|RTHUM|
1635 * -----+-----+-----------------+-----------+-----+-----+
1636 * All buttons are low-active (0 if pressed)
1637 * RX and RY are right analog stick
1638 * LX and LY are left analog stick
1639 * BLT is left trigger, BRT is right trigger.
1640 * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1641 * BZL is left Z button and BZR is right Z button
1642 * B-, BH, B+ are +, HOME and - buttons
1643 * BB, BY, BA, BX are A, B, X, Y buttons
1644 *
1645 * Bits marked as 0/1 are unknown and never changed during tests.
1646 *
1647 * Not entirely verified:
1648 * CHARG: 1 if uncharging, 0 if charging
1649 * USB: 1 if not connected, 0 if connected
1650 * BATTERY: battery capacity from 000 (empty) to 100 (full)
1651 */
1652
1653 lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8);
1654 rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8);
1655 ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8);
1656 ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8);
1657
1658 input_report_abs(wdata->extension.input, ABS_X, lx - 0x800);
1659 input_report_abs(wdata->extension.input, ABS_Y, ly - 0x800);
1660 input_report_abs(wdata->extension.input, ABS_RX, rx - 0x800);
1661 input_report_abs(wdata->extension.input, ABS_RY, ry - 0x800);
1662
1663 input_report_key(wdata->extension.input,
1664 wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
1665 !(ext[8] & 0x80));
1666 input_report_key(wdata->extension.input,
1667 wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
1668 !(ext[8] & 0x40));
1669 input_report_key(wdata->extension.input,
1670 wiimod_pro_map[WIIMOD_PRO_KEY_TL],
1671 !(ext[8] & 0x20));
1672 input_report_key(wdata->extension.input,
1673 wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
1674 !(ext[8] & 0x10));
1675 input_report_key(wdata->extension.input,
1676 wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
1677 !(ext[8] & 0x08));
1678 input_report_key(wdata->extension.input,
1679 wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
1680 !(ext[8] & 0x04));
1681 input_report_key(wdata->extension.input,
1682 wiimod_pro_map[WIIMOD_PRO_KEY_TR],
1683 !(ext[8] & 0x02));
1684
1685 input_report_key(wdata->extension.input,
1686 wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
1687 !(ext[9] & 0x80));
1688 input_report_key(wdata->extension.input,
1689 wiimod_pro_map[WIIMOD_PRO_KEY_B],
1690 !(ext[9] & 0x40));
1691 input_report_key(wdata->extension.input,
1692 wiimod_pro_map[WIIMOD_PRO_KEY_Y],
1693 !(ext[9] & 0x20));
1694 input_report_key(wdata->extension.input,
1695 wiimod_pro_map[WIIMOD_PRO_KEY_A],
1696 !(ext[9] & 0x10));
1697 input_report_key(wdata->extension.input,
1698 wiimod_pro_map[WIIMOD_PRO_KEY_X],
1699 !(ext[9] & 0x08));
1700 input_report_key(wdata->extension.input,
1701 wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
1702 !(ext[9] & 0x04));
1703 input_report_key(wdata->extension.input,
1704 wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
1705 !(ext[9] & 0x02));
1706 input_report_key(wdata->extension.input,
1707 wiimod_pro_map[WIIMOD_PRO_KEY_UP],
1708 !(ext[9] & 0x01));
1709
1710 input_report_key(wdata->extension.input,
1711 wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
1712 !(ext[10] & 0x02));
1713 input_report_key(wdata->extension.input,
1714 wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
1715 !(ext[10] & 0x01));
1716
1717 input_sync(wdata->extension.input);
1718}
1719
1720static int wiimod_pro_open(struct input_dev *dev)
1721{
1722 struct wiimote_data *wdata = input_get_drvdata(dev);
1723 unsigned long flags;
1724
1725 spin_lock_irqsave(&wdata->state.lock, flags);
1726 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1727 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1728 spin_unlock_irqrestore(&wdata->state.lock, flags);
1729
1730 return 0;
1731}
1732
1733static void wiimod_pro_close(struct input_dev *dev)
1734{
1735 struct wiimote_data *wdata = input_get_drvdata(dev);
1736 unsigned long flags;
1737
1738 spin_lock_irqsave(&wdata->state.lock, flags);
1739 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1740 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1741 spin_unlock_irqrestore(&wdata->state.lock, flags);
1742}
1743
1744static int wiimod_pro_play(struct input_dev *dev, void *data,
1745 struct ff_effect *eff)
1746{
1747 struct wiimote_data *wdata = input_get_drvdata(dev);
1748 __u8 value;
David Herrmannb8e0fe32013-06-15 15:32:45 +02001749
1750 /*
1751 * The wiimote supports only a single rumble motor so if any magnitude
1752 * is set to non-zero then we start the rumble motor. If both are set to
1753 * zero, we stop the rumble motor.
1754 */
1755
1756 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
1757 value = 1;
1758 else
1759 value = 0;
1760
David Herrmannf50f9aa2013-10-02 13:47:28 +02001761 /* Locking state.lock here might deadlock with input_event() calls.
1762 * schedule_work acts as barrier. Merging multiple changes is fine. */
1763 wdata->state.cache_rumble = value;
1764 schedule_work(&wdata->rumble_worker);
David Herrmannb8e0fe32013-06-15 15:32:45 +02001765
1766 return 0;
1767}
1768
1769static int wiimod_pro_probe(const struct wiimod_ops *ops,
1770 struct wiimote_data *wdata)
1771{
1772 int ret, i;
1773
David Herrmannf50f9aa2013-10-02 13:47:28 +02001774 INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
1775
David Herrmannb8e0fe32013-06-15 15:32:45 +02001776 wdata->extension.input = input_allocate_device();
1777 if (!wdata->extension.input)
1778 return -ENOMEM;
1779
1780 set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
1781 input_set_drvdata(wdata->extension.input, wdata);
1782
1783 if (input_ff_create_memless(wdata->extension.input, NULL,
1784 wiimod_pro_play)) {
1785 ret = -ENOMEM;
1786 goto err_free;
1787 }
1788
1789 wdata->extension.input->open = wiimod_pro_open;
1790 wdata->extension.input->close = wiimod_pro_close;
1791 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1792 wdata->extension.input->id.bustype = wdata->hdev->bus;
1793 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1794 wdata->extension.input->id.product = wdata->hdev->product;
1795 wdata->extension.input->id.version = wdata->hdev->version;
1796 wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
1797
1798 set_bit(EV_KEY, wdata->extension.input->evbit);
1799 for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
1800 set_bit(wiimod_pro_map[i],
1801 wdata->extension.input->keybit);
1802
1803 set_bit(EV_ABS, wdata->extension.input->evbit);
1804 set_bit(ABS_X, wdata->extension.input->absbit);
1805 set_bit(ABS_Y, wdata->extension.input->absbit);
1806 set_bit(ABS_RX, wdata->extension.input->absbit);
1807 set_bit(ABS_RY, wdata->extension.input->absbit);
1808 input_set_abs_params(wdata->extension.input,
1809 ABS_X, -0x800, 0x800, 2, 4);
1810 input_set_abs_params(wdata->extension.input,
1811 ABS_Y, -0x800, 0x800, 2, 4);
1812 input_set_abs_params(wdata->extension.input,
1813 ABS_RX, -0x800, 0x800, 2, 4);
1814 input_set_abs_params(wdata->extension.input,
1815 ABS_RY, -0x800, 0x800, 2, 4);
1816
1817 ret = input_register_device(wdata->extension.input);
1818 if (ret)
1819 goto err_free;
1820
1821 return 0;
1822
1823err_free:
1824 input_free_device(wdata->extension.input);
1825 wdata->extension.input = NULL;
1826 return ret;
1827}
1828
1829static void wiimod_pro_remove(const struct wiimod_ops *ops,
1830 struct wiimote_data *wdata)
1831{
1832 unsigned long flags;
1833
1834 if (!wdata->extension.input)
1835 return;
1836
David Herrmannf50f9aa2013-10-02 13:47:28 +02001837 input_unregister_device(wdata->extension.input);
1838 wdata->extension.input = NULL;
1839 cancel_work_sync(&wdata->rumble_worker);
1840
David Herrmannb8e0fe32013-06-15 15:32:45 +02001841 spin_lock_irqsave(&wdata->state.lock, flags);
1842 wiiproto_req_rumble(wdata, 0);
1843 spin_unlock_irqrestore(&wdata->state.lock, flags);
David Herrmannb8e0fe32013-06-15 15:32:45 +02001844}
1845
1846static const struct wiimod_ops wiimod_pro = {
1847 .flags = WIIMOD_FLAG_EXT16,
1848 .arg = 0,
1849 .probe = wiimod_pro_probe,
1850 .remove = wiimod_pro_remove,
1851 .in_ext = wiimod_pro_in_ext,
1852};
1853
1854/*
David Herrmann9f329742013-05-05 23:13:07 +02001855 * Builtin Motion Plus
1856 * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
1857 * disables polling for Motion-Plus. This should be set only for devices which
1858 * don't allow MP hotplugging.
1859 */
1860
1861static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
1862 struct wiimote_data *wdata)
1863{
1864 unsigned long flags;
1865
1866 spin_lock_irqsave(&wdata->state.lock, flags);
1867 wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
1868 spin_unlock_irqrestore(&wdata->state.lock, flags);
1869
1870 return 0;
1871}
1872
1873static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
1874 struct wiimote_data *wdata)
1875{
1876 unsigned long flags;
1877
1878 spin_lock_irqsave(&wdata->state.lock, flags);
1879 wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
1880 spin_unlock_irqrestore(&wdata->state.lock, flags);
1881}
1882
1883static const struct wiimod_ops wiimod_builtin_mp = {
1884 .flags = 0,
1885 .arg = 0,
1886 .probe = wiimod_builtin_mp_probe,
1887 .remove = wiimod_builtin_mp_remove,
1888};
1889
1890/*
1891 * No Motion Plus
1892 * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
1893 * disables motion-plus. This is needed for devices that advertise this but we
1894 * don't know how to use it (or whether it is actually present).
1895 */
1896
1897static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
1898 struct wiimote_data *wdata)
1899{
1900 unsigned long flags;
1901
1902 spin_lock_irqsave(&wdata->state.lock, flags);
1903 wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
1904 spin_unlock_irqrestore(&wdata->state.lock, flags);
1905
1906 return 0;
1907}
1908
1909static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
1910 struct wiimote_data *wdata)
1911{
1912 unsigned long flags;
1913
1914 spin_lock_irqsave(&wdata->state.lock, flags);
1915 wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
1916 spin_unlock_irqrestore(&wdata->state.lock, flags);
1917}
1918
1919static const struct wiimod_ops wiimod_no_mp = {
1920 .flags = 0,
1921 .arg = 0,
1922 .probe = wiimod_no_mp_probe,
1923 .remove = wiimod_no_mp_remove,
1924};
1925
1926/*
David Herrmann4148b6b2013-05-05 23:12:57 +02001927 * Motion Plus
David Herrmann34472d372013-05-05 23:13:01 +02001928 * The Motion Plus extension provides rotation sensors (gyro) as a small
1929 * extension device for Wii Remotes. Many devices have them built-in so
1930 * you cannot see them from the outside.
1931 * Motion Plus extensions are special because they are on a separate extension
1932 * port and allow other extensions to be used simultaneously. This is all
1933 * handled by the Wiimote Core so we don't have to deal with it.
David Herrmann4148b6b2013-05-05 23:12:57 +02001934 */
1935
David Herrmann34472d372013-05-05 23:13:01 +02001936static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
1937{
1938 __s32 x, y, z;
1939
1940 /* | 8 7 6 5 4 3 | 2 | 1 |
1941 * -----+------------------------------+-----+-----+
1942 * 1 | Yaw Speed <7:0> |
1943 * 2 | Roll Speed <7:0> |
1944 * 3 | Pitch Speed <7:0> |
1945 * -----+------------------------------+-----+-----+
1946 * 4 | Yaw Speed <13:8> | Yaw |Pitch|
1947 * -----+------------------------------+-----+-----+
1948 * 5 | Roll Speed <13:8> |Roll | Ext |
1949 * -----+------------------------------+-----+-----+
1950 * 6 | Pitch Speed <13:8> | 1 | 0 |
1951 * -----+------------------------------+-----+-----+
1952 * The single bits Yaw, Roll, Pitch in the lower right corner specify
1953 * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
1954 * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
1955 * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
1956 * and 9 for slow.
1957 * If the wiimote is not rotating the sensor reports 2^13 = 8192.
1958 * Ext specifies whether an extension is connected to the motionp.
1959 * which is parsed by wiimote-core.
1960 */
1961
1962 x = ext[0];
1963 y = ext[1];
1964 z = ext[2];
1965
1966 x |= (((__u16)ext[3]) << 6) & 0xff00;
1967 y |= (((__u16)ext[4]) << 6) & 0xff00;
1968 z |= (((__u16)ext[5]) << 6) & 0xff00;
1969
1970 x -= 8192;
1971 y -= 8192;
1972 z -= 8192;
1973
1974 if (!(ext[3] & 0x02))
1975 x *= 18;
1976 else
1977 x *= 9;
1978 if (!(ext[4] & 0x02))
1979 y *= 18;
1980 else
1981 y *= 9;
1982 if (!(ext[3] & 0x01))
1983 z *= 18;
1984 else
1985 z *= 9;
1986
1987 input_report_abs(wdata->mp, ABS_RX, x);
1988 input_report_abs(wdata->mp, ABS_RY, y);
1989 input_report_abs(wdata->mp, ABS_RZ, z);
1990 input_sync(wdata->mp);
1991}
1992
1993static int wiimod_mp_open(struct input_dev *dev)
1994{
1995 struct wiimote_data *wdata = input_get_drvdata(dev);
1996 unsigned long flags;
1997
1998 spin_lock_irqsave(&wdata->state.lock, flags);
1999 wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
2000 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2001 __wiimote_schedule(wdata);
2002 spin_unlock_irqrestore(&wdata->state.lock, flags);
2003
2004 return 0;
2005}
2006
2007static void wiimod_mp_close(struct input_dev *dev)
2008{
2009 struct wiimote_data *wdata = input_get_drvdata(dev);
2010 unsigned long flags;
2011
2012 spin_lock_irqsave(&wdata->state.lock, flags);
2013 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
2014 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2015 __wiimote_schedule(wdata);
2016 spin_unlock_irqrestore(&wdata->state.lock, flags);
2017}
2018
2019static int wiimod_mp_probe(const struct wiimod_ops *ops,
2020 struct wiimote_data *wdata)
2021{
2022 int ret;
2023
2024 wdata->mp = input_allocate_device();
2025 if (!wdata->mp)
2026 return -ENOMEM;
2027
2028 input_set_drvdata(wdata->mp, wdata);
2029 wdata->mp->open = wiimod_mp_open;
2030 wdata->mp->close = wiimod_mp_close;
2031 wdata->mp->dev.parent = &wdata->hdev->dev;
2032 wdata->mp->id.bustype = wdata->hdev->bus;
2033 wdata->mp->id.vendor = wdata->hdev->vendor;
2034 wdata->mp->id.product = wdata->hdev->product;
2035 wdata->mp->id.version = wdata->hdev->version;
2036 wdata->mp->name = WIIMOTE_NAME " Motion Plus";
2037
2038 set_bit(EV_ABS, wdata->mp->evbit);
2039 set_bit(ABS_RX, wdata->mp->absbit);
2040 set_bit(ABS_RY, wdata->mp->absbit);
2041 set_bit(ABS_RZ, wdata->mp->absbit);
2042 input_set_abs_params(wdata->mp,
2043 ABS_RX, -16000, 16000, 4, 8);
2044 input_set_abs_params(wdata->mp,
2045 ABS_RY, -16000, 16000, 4, 8);
2046 input_set_abs_params(wdata->mp,
2047 ABS_RZ, -16000, 16000, 4, 8);
2048
2049 ret = input_register_device(wdata->mp);
2050 if (ret)
2051 goto err_free;
2052
2053 return 0;
2054
2055err_free:
2056 input_free_device(wdata->mp);
2057 wdata->mp = NULL;
2058 return ret;
2059}
2060
2061static void wiimod_mp_remove(const struct wiimod_ops *ops,
2062 struct wiimote_data *wdata)
2063{
2064 if (!wdata->mp)
2065 return;
2066
2067 input_unregister_device(wdata->mp);
2068 wdata->mp = NULL;
2069}
2070
David Herrmann4148b6b2013-05-05 23:12:57 +02002071const struct wiimod_ops wiimod_mp = {
2072 .flags = 0,
2073 .arg = 0,
David Herrmann34472d372013-05-05 23:13:01 +02002074 .probe = wiimod_mp_probe,
2075 .remove = wiimod_mp_remove,
2076 .in_mp = wiimod_mp_in_mp,
David Herrmann4148b6b2013-05-05 23:12:57 +02002077};
2078
David Herrmann27f06942013-05-05 23:12:51 +02002079/* module table */
2080
David Herrmann4148b6b2013-05-05 23:12:57 +02002081static const struct wiimod_ops wiimod_dummy;
2082
David Herrmann27f06942013-05-05 23:12:51 +02002083const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
David Herrmann20cef812013-05-05 23:12:52 +02002084 [WIIMOD_KEYS] = &wiimod_keys,
2085 [WIIMOD_RUMBLE] = &wiimod_rumble,
David Herrmanndcf39232013-05-05 23:12:53 +02002086 [WIIMOD_BATTERY] = &wiimod_battery,
David Herrmann6c5ae012013-05-05 23:12:54 +02002087 [WIIMOD_LED1] = &wiimod_leds[0],
2088 [WIIMOD_LED2] = &wiimod_leds[1],
2089 [WIIMOD_LED3] = &wiimod_leds[2],
2090 [WIIMOD_LED4] = &wiimod_leds[3],
David Herrmann0ea16752013-05-05 23:12:55 +02002091 [WIIMOD_ACCEL] = &wiimod_accel,
David Herrmann3b5f03c2013-05-05 23:12:56 +02002092 [WIIMOD_IR] = &wiimod_ir,
David Herrmann9f329742013-05-05 23:13:07 +02002093 [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
2094 [WIIMOD_NO_MP] = &wiimod_no_mp,
David Herrmann27f06942013-05-05 23:12:51 +02002095};
David Herrmann4148b6b2013-05-05 23:12:57 +02002096
2097const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
2098 [WIIMOTE_EXT_NONE] = &wiimod_dummy,
2099 [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
David Herrmannb6ee67b2013-05-05 23:12:59 +02002100 [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
David Herrmann9d6f9ec2013-05-05 23:13:00 +02002101 [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
David Herrmannf1d4bed2013-05-05 23:12:58 +02002102 [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
David Herrmannb8e0fe32013-06-15 15:32:45 +02002103 [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,
David Herrmann4148b6b2013-05-05 23:12:57 +02002104};