[media] disable COMPILE_TEST for omap1_camera
[linux/fpc-iii.git] / drivers / hid / hid-wiimote-modules.c
blob6b61f01e01e7fad839f8dd960050596a26a9abfe
1 /*
2 * Device Modules for Nintendo Wii / Wii U HID Driver
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
4 */
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.
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.
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.
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.
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"
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.
51 static 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 */
65 static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
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);
92 static int wiimod_keys_probe(const struct wiimod_ops *ops,
93 struct wiimote_data *wdata)
95 unsigned int i;
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);
101 return 0;
104 static 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,
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.
122 /* used by wiimod_rumble and wiipro_rumble */
123 static void wiimod_rumble_worker(struct work_struct *work)
125 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
126 rumble_worker);
128 spin_lock_irq(&wdata->state.lock);
129 wiiproto_req_rumble(wdata, wdata->state.cache_rumble);
130 spin_unlock_irq(&wdata->state.lock);
133 static int wiimod_rumble_play(struct input_dev *dev, void *data,
134 struct ff_effect *eff)
136 struct wiimote_data *wdata = input_get_drvdata(dev);
137 __u8 value;
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.
145 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
146 value = 1;
147 else
148 value = 0;
150 /* 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);
155 return 0;
158 static int wiimod_rumble_probe(const struct wiimod_ops *ops,
159 struct wiimote_data *wdata)
161 INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
163 set_bit(FF_RUMBLE, wdata->input->ffbit);
164 if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
165 return -ENOMEM;
167 return 0;
170 static void wiimod_rumble_remove(const struct wiimod_ops *ops,
171 struct wiimote_data *wdata)
173 unsigned long flags;
175 cancel_work_sync(&wdata->rumble_worker);
177 spin_lock_irqsave(&wdata->state.lock, flags);
178 wiiproto_req_rumble(wdata, 0);
179 spin_unlock_irqrestore(&wdata->state.lock, flags);
182 static const struct wiimod_ops wiimod_rumble = {
183 .flags = WIIMOD_FLAG_INPUT,
184 .arg = 0,
185 .probe = wiimod_rumble_probe,
186 .remove = wiimod_rumble_remove,
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.
197 static enum power_supply_property wiimod_battery_props[] = {
198 POWER_SUPPLY_PROP_CAPACITY,
199 POWER_SUPPLY_PROP_SCOPE,
202 static int wiimod_battery_get_property(struct power_supply *psy,
203 enum power_supply_property psp,
204 union power_supply_propval *val)
206 struct wiimote_data *wdata = container_of(psy, struct wiimote_data,
207 battery);
208 int ret = 0, state;
209 unsigned long flags;
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;
218 ret = wiimote_cmd_acquire(wdata);
219 if (ret)
220 return ret;
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);
227 wiimote_cmd_wait(wdata);
228 wiimote_cmd_release(wdata);
230 spin_lock_irqsave(&wdata->state.lock, flags);
231 state = wdata->state.cmd_battery;
232 spin_unlock_irqrestore(&wdata->state.lock, flags);
234 val->intval = state * 100 / 255;
235 return ret;
238 static int wiimod_battery_probe(const struct wiimod_ops *ops,
239 struct wiimote_data *wdata)
241 int ret;
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;
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;
259 power_supply_powers(&wdata->battery, &wdata->hdev->dev);
260 return 0;
262 err_free:
263 kfree(wdata->battery.name);
264 wdata->battery.name = NULL;
265 return ret;
268 static void wiimod_battery_remove(const struct wiimod_ops *ops,
269 struct wiimote_data *wdata)
271 if (!wdata->battery.name)
272 return;
274 power_supply_unregister(&wdata->battery);
275 kfree(wdata->battery.name);
276 wdata->battery.name = NULL;
279 static const struct wiimod_ops wiimod_battery = {
280 .flags = 0,
281 .arg = 0,
282 .probe = wiimod_battery_probe,
283 .remove = wiimod_battery_remove,
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.
294 static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
296 struct wiimote_data *wdata;
297 struct device *dev = led_dev->dev->parent;
298 int i;
299 unsigned long flags;
300 bool value = false;
302 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
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;
313 return value ? LED_FULL : LED_OFF;
316 static void wiimod_led_set(struct led_classdev *led_dev,
317 enum led_brightness value)
319 struct wiimote_data *wdata;
320 struct device *dev = led_dev->dev->parent;
321 int i;
322 unsigned long flags;
323 __u8 state, flag;
325 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
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;
342 static int wiimod_led_probe(const struct wiimod_ops *ops,
343 struct wiimote_data *wdata)
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;
352 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
353 if (!led)
354 return -ENOMEM;
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;
364 wdata->leds[ops->arg] = led;
365 ret = led_classdev_register(dev, led);
366 if (ret)
367 goto err_free;
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);
376 return 0;
378 err_free:
379 wdata->leds[ops->arg] = NULL;
380 kfree(led);
381 return ret;
384 static void wiimod_led_remove(const struct wiimod_ops *ops,
385 struct wiimote_data *wdata)
387 if (!wdata->leds[ops->arg])
388 return;
390 led_classdev_unregister(wdata->leds[ops->arg]);
391 kfree(wdata->leds[ops->arg]);
392 wdata->leds[ops->arg] = NULL;
395 static const struct wiimod_ops wiimod_leds[4] = {
397 .flags = 0,
398 .arg = 0,
399 .probe = wiimod_led_probe,
400 .remove = wiimod_led_remove,
403 .flags = 0,
404 .arg = 1,
405 .probe = wiimod_led_probe,
406 .remove = wiimod_led_remove,
409 .flags = 0,
410 .arg = 2,
411 .probe = wiimod_led_probe,
412 .remove = wiimod_led_remove,
415 .flags = 0,
416 .arg = 3,
417 .probe = wiimod_led_probe,
418 .remove = wiimod_led_remove,
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.
429 static void wiimod_accel_in_accel(struct wiimote_data *wdata,
430 const __u8 *accel)
432 __u16 x, y, z;
434 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
435 return;
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.
449 x = accel[2] << 2;
450 y = accel[3] << 2;
451 z = accel[4] << 2;
453 x |= (accel[0] >> 5) & 0x3;
454 y |= (accel[1] >> 4) & 0x2;
455 z |= (accel[1] >> 5) & 0x2;
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);
463 static int wiimod_accel_open(struct input_dev *dev)
465 struct wiimote_data *wdata = input_get_drvdata(dev);
466 unsigned long flags;
468 spin_lock_irqsave(&wdata->state.lock, flags);
469 wiiproto_req_accel(wdata, true);
470 spin_unlock_irqrestore(&wdata->state.lock, flags);
472 return 0;
475 static void wiimod_accel_close(struct input_dev *dev)
477 struct wiimote_data *wdata = input_get_drvdata(dev);
478 unsigned long flags;
480 spin_lock_irqsave(&wdata->state.lock, flags);
481 wiiproto_req_accel(wdata, false);
482 spin_unlock_irqrestore(&wdata->state.lock, flags);
485 static int wiimod_accel_probe(const struct wiimod_ops *ops,
486 struct wiimote_data *wdata)
488 int ret;
490 wdata->accel = input_allocate_device();
491 if (!wdata->accel)
492 return -ENOMEM;
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";
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);
512 ret = input_register_device(wdata->accel);
513 if (ret) {
514 hid_err(wdata->hdev, "cannot register input device\n");
515 goto err_free;
518 return 0;
520 err_free:
521 input_free_device(wdata->accel);
522 wdata->accel = NULL;
523 return ret;
526 static void wiimod_accel_remove(const struct wiimod_ops *ops,
527 struct wiimote_data *wdata)
529 if (!wdata->accel)
530 return;
532 input_unregister_device(wdata->accel);
533 wdata->accel = NULL;
536 static 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,
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.
554 static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
555 bool packed, unsigned int id)
557 __u16 x, y;
558 __u8 xid, yid;
559 bool sync = false;
561 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
562 return;
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;
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.
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);
604 input_report_abs(wdata->ir, xid, x);
605 input_report_abs(wdata->ir, yid, y);
607 if (sync)
608 input_sync(wdata->ir);
611 static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
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 };
622 spin_lock_irqsave(&wdata->state.lock, flags);
624 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
625 spin_unlock_irqrestore(&wdata->state.lock, flags);
626 return 0;
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;
638 spin_unlock_irqrestore(&wdata->state.lock, flags);
640 ret = wiimote_cmd_acquire(wdata);
641 if (ret)
642 return ret;
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);
650 ret = wiimote_cmd_wait(wdata);
651 if (ret)
652 goto unlock;
653 if (wdata->state.cmd_err) {
654 ret = -EIO;
655 goto unlock;
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);
664 ret = wiimote_cmd_wait(wdata);
665 if (ret)
666 goto unlock;
667 if (wdata->state.cmd_err) {
668 ret = -EIO;
669 goto unlock;
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;
678 /* write first sensitivity block */
679 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
680 sizeof(data_sens1));
681 if (ret)
682 goto unlock;
684 /* write second sensitivity block */
685 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
686 sizeof(data_sens2));
687 if (ret)
688 goto unlock;
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;
702 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
703 if (ret)
704 goto unlock;
706 /* make IR cam send data */
707 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
708 if (ret)
709 goto unlock;
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);
718 unlock:
719 wiimote_cmd_release(wdata);
720 return ret;
723 static int wiimod_ir_open(struct input_dev *dev)
725 struct wiimote_data *wdata = input_get_drvdata(dev);
727 return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
730 static void wiimod_ir_close(struct input_dev *dev)
732 struct wiimote_data *wdata = input_get_drvdata(dev);
734 wiimod_ir_change(wdata, 0);
737 static int wiimod_ir_probe(const struct wiimod_ops *ops,
738 struct wiimote_data *wdata)
740 int ret;
742 wdata->ir = input_allocate_device();
743 if (!wdata->ir)
744 return -ENOMEM;
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";
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);
774 ret = input_register_device(wdata->ir);
775 if (ret) {
776 hid_err(wdata->hdev, "cannot register input device\n");
777 goto err_free;
780 return 0;
782 err_free:
783 input_free_device(wdata->ir);
784 wdata->ir = NULL;
785 return ret;
788 static void wiimod_ir_remove(const struct wiimod_ops *ops,
789 struct wiimote_data *wdata)
791 if (!wdata->ir)
792 return;
794 input_unregister_device(wdata->ir);
795 wdata->ir = NULL;
798 static 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,
807 * 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.
813 enum wiimod_nunchuk_keys {
814 WIIMOD_NUNCHUK_KEY_C,
815 WIIMOD_NUNCHUK_KEY_Z,
816 WIIMOD_NUNCHUK_KEY_NUM,
819 static const __u16 wiimod_nunchuk_map[] = {
820 BTN_C, /* WIIMOD_NUNCHUK_KEY_C */
821 BTN_Z, /* WIIMOD_NUNCHUK_KEY_Z */
824 static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
826 __s16 x, y, z, bx, by;
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
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.
854 * Center data for button values is 128. Center value for accelerometer
855 * values it 512 / 0x200
858 bx = ext[0];
859 by = ext[1];
860 bx -= 128;
861 by -= 128;
863 x = ext[2] << 2;
864 y = ext[3] << 2;
865 z = ext[4] << 2;
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;
878 x -= 0x200;
879 y -= 0x200;
880 z -= 0x200;
882 input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
883 input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
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);
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));
905 input_sync(wdata->extension.input);
908 static int wiimod_nunchuk_open(struct input_dev *dev)
910 struct wiimote_data *wdata = input_get_drvdata(dev);
911 unsigned long flags;
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);
918 return 0;
921 static void wiimod_nunchuk_close(struct input_dev *dev)
923 struct wiimote_data *wdata = input_get_drvdata(dev);
924 unsigned long flags;
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);
932 static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
933 struct wiimote_data *wdata)
935 int ret, i;
937 wdata->extension.input = input_allocate_device();
938 if (!wdata->extension.input)
939 return -ENOMEM;
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";
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);
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);
973 ret = input_register_device(wdata->extension.input);
974 if (ret)
975 goto err_free;
977 return 0;
979 err_free:
980 input_free_device(wdata->extension.input);
981 wdata->extension.input = NULL;
982 return ret;
985 static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
986 struct wiimote_data *wdata)
988 if (!wdata->extension.input)
989 return;
991 input_unregister_device(wdata->extension.input);
992 wdata->extension.input = NULL;
995 static 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,
1004 * 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.
1011 enum 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,
1030 static 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 */
1048 static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1050 __s8 rx, ry, lx, ly, lt, rt;
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.
1077 * With motionp enabled it changes slightly to this:
1078 * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1079 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1080 * 1 | RX <5:4> | LX <5:1> | BDU |
1081 * 2 | RX <3:2> | LY <5:1> | BDL |
1082 * -----+-----+-----+-----+-----------------------+-----+
1083 * 3 |RX<1>| LT <5:4> | RY <5:1> |
1084 * -----+-----+-----------+-----------------------------+
1085 * 4 | LT <3:1> | RT <5:1> |
1086 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
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.
1095 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1096 lx = ext[0] & 0x3e;
1097 ly = ext[1] & 0x3e;
1098 } else {
1099 lx = ext[0] & 0x3f;
1100 ly = ext[1] & 0x3f;
1103 rx = (ext[0] >> 3) & 0x18;
1104 rx |= (ext[1] >> 5) & 0x06;
1105 rx |= (ext[2] >> 7) & 0x01;
1106 ry = ext[2] & 0x1f;
1108 rt = ext[3] & 0x1f;
1109 lt = (ext[2] >> 2) & 0x18;
1110 lt |= (ext[3] >> 5) & 0x07;
1112 rx <<= 1;
1113 ry <<= 1;
1114 rt <<= 1;
1115 lt <<= 1;
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);
1121 input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
1122 input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
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));
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));
1180 input_sync(wdata->extension.input);
1183 static int wiimod_classic_open(struct input_dev *dev)
1185 struct wiimote_data *wdata = input_get_drvdata(dev);
1186 unsigned long flags;
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);
1193 return 0;
1196 static void wiimod_classic_close(struct input_dev *dev)
1198 struct wiimote_data *wdata = input_get_drvdata(dev);
1199 unsigned long flags;
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);
1207 static int wiimod_classic_probe(const struct wiimod_ops *ops,
1208 struct wiimote_data *wdata)
1210 int ret, i;
1212 wdata->extension.input = input_allocate_device();
1213 if (!wdata->extension.input)
1214 return -ENOMEM;
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";
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);
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);
1251 ret = input_register_device(wdata->extension.input);
1252 if (ret)
1253 goto err_free;
1255 return 0;
1257 err_free:
1258 input_free_device(wdata->extension.input);
1259 wdata->extension.input = NULL;
1260 return ret;
1263 static void wiimod_classic_remove(const struct wiimod_ops *ops,
1264 struct wiimote_data *wdata)
1266 if (!wdata->extension.input)
1267 return;
1269 input_unregister_device(wdata->extension.input);
1270 wdata->extension.input = NULL;
1273 static 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,
1282 * 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.
1294 static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
1296 input_report_key(wdata->extension.input, BTN_A,
1297 !!(keys[1] & 0x08));
1298 input_sync(wdata->extension.input);
1301 static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
1302 const __u8 *ext)
1304 __s32 val[4], tmp, div;
1305 unsigned int i;
1306 struct wiimote_state *s = &wdata->state;
1309 * Balance board data layout:
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 * -----+--------------------------+
1326 * These values represent the weight-measurements of the Wii-balance
1327 * board with 16bit precision.
1329 * The balance-board is never reported interleaved with motionp.
1332 val[0] = ext[0];
1333 val[0] <<= 8;
1334 val[0] |= ext[1];
1336 val[1] = ext[2];
1337 val[1] <<= 8;
1338 val[1] |= ext[3];
1340 val[2] = ext[4];
1341 val[2] <<= 8;
1342 val[2] |= ext[5];
1344 val[3] = ext[6];
1345 val[3] <<= 8;
1346 val[3] |= ext[7];
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;
1364 val[i] = tmp;
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);
1374 static int wiimod_bboard_open(struct input_dev *dev)
1376 struct wiimote_data *wdata = input_get_drvdata(dev);
1377 unsigned long flags;
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);
1384 return 0;
1387 static void wiimod_bboard_close(struct input_dev *dev)
1389 struct wiimote_data *wdata = input_get_drvdata(dev);
1390 unsigned long flags;
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);
1398 static ssize_t wiimod_bboard_calib_show(struct device *dev,
1399 struct device_attribute *attr,
1400 char *out)
1402 struct wiimote_data *wdata = dev_to_wii(dev);
1403 int i, j, ret;
1404 __u16 val;
1405 __u8 buf[24], offs;
1407 ret = wiimote_cmd_acquire(wdata);
1408 if (ret)
1409 return ret;
1411 ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1412 if (ret != 12) {
1413 wiimote_cmd_release(wdata);
1414 return ret < 0 ? ret : -EIO;
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;
1422 wiimote_cmd_release(wdata);
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;
1434 spin_unlock_irq(&wdata->state.lock);
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);
1447 return ret;
1450 static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
1452 static int wiimod_bboard_probe(const struct wiimod_ops *ops,
1453 struct wiimote_data *wdata)
1455 int ret, i, j;
1456 __u8 buf[24], offs;
1458 wiimote_cmd_acquire_noint(wdata);
1460 ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1461 if (ret != 12) {
1462 wiimote_cmd_release(wdata);
1463 return ret < 0 ? ret : -EIO;
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;
1471 wiimote_cmd_release(wdata);
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;
1483 wdata->extension.input = input_allocate_device();
1484 if (!wdata->extension.input)
1485 return -ENOMEM;
1487 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;
1494 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";
1504 set_bit(EV_KEY, wdata->extension.input->evbit);
1505 set_bit(BTN_A, wdata->extension.input->keybit);
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);
1521 ret = input_register_device(wdata->extension.input);
1522 if (ret)
1523 goto err_file;
1525 return 0;
1527 err_file:
1528 device_remove_file(&wdata->hdev->dev,
1529 &dev_attr_bboard_calib);
1530 err_free:
1531 input_free_device(wdata->extension.input);
1532 wdata->extension.input = NULL;
1533 return ret;
1536 static void wiimod_bboard_remove(const struct wiimod_ops *ops,
1537 struct wiimote_data *wdata)
1539 if (!wdata->extension.input)
1540 return;
1542 input_unregister_device(wdata->extension.input);
1543 wdata->extension.input = NULL;
1544 device_remove_file(&wdata->hdev->dev,
1545 &dev_attr_bboard_calib);
1548 static 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,
1558 * 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.
1567 enum 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,
1588 static 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 */
1608 static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1610 __s16 rx, ry, lx, ly;
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
1645 * Bits marked as 0/1 are unknown and never changed during tests.
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)
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);
1658 /* zero-point offsets */
1659 lx -= 0x800;
1660 ly = 0x800 - ly;
1661 rx -= 0x800;
1662 ry = 0x800 - ry;
1664 /* Trivial automatic calibration. We don't know any calibration data
1665 * in the EEPROM so we must use the first report to calibrate the
1666 * null-position of the analog sticks. Users can retrigger calibration
1667 * via sysfs, or set it explicitly. If data is off more than abs(500),
1668 * we skip calibration as the sticks are likely to be moved already. */
1669 if (!(wdata->state.flags & WIIPROTO_FLAG_PRO_CALIB_DONE)) {
1670 wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1671 if (abs(lx) < 500)
1672 wdata->state.calib_pro_sticks[0] = -lx;
1673 if (abs(ly) < 500)
1674 wdata->state.calib_pro_sticks[1] = -ly;
1675 if (abs(rx) < 500)
1676 wdata->state.calib_pro_sticks[2] = -rx;
1677 if (abs(ry) < 500)
1678 wdata->state.calib_pro_sticks[3] = -ry;
1681 /* apply calibration data */
1682 lx += wdata->state.calib_pro_sticks[0];
1683 ly += wdata->state.calib_pro_sticks[1];
1684 rx += wdata->state.calib_pro_sticks[2];
1685 ry += wdata->state.calib_pro_sticks[3];
1687 input_report_abs(wdata->extension.input, ABS_X, lx);
1688 input_report_abs(wdata->extension.input, ABS_Y, ly);
1689 input_report_abs(wdata->extension.input, ABS_RX, rx);
1690 input_report_abs(wdata->extension.input, ABS_RY, ry);
1692 input_report_key(wdata->extension.input,
1693 wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
1694 !(ext[8] & 0x80));
1695 input_report_key(wdata->extension.input,
1696 wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
1697 !(ext[8] & 0x40));
1698 input_report_key(wdata->extension.input,
1699 wiimod_pro_map[WIIMOD_PRO_KEY_TL],
1700 !(ext[8] & 0x20));
1701 input_report_key(wdata->extension.input,
1702 wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
1703 !(ext[8] & 0x10));
1704 input_report_key(wdata->extension.input,
1705 wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
1706 !(ext[8] & 0x08));
1707 input_report_key(wdata->extension.input,
1708 wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
1709 !(ext[8] & 0x04));
1710 input_report_key(wdata->extension.input,
1711 wiimod_pro_map[WIIMOD_PRO_KEY_TR],
1712 !(ext[8] & 0x02));
1714 input_report_key(wdata->extension.input,
1715 wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
1716 !(ext[9] & 0x80));
1717 input_report_key(wdata->extension.input,
1718 wiimod_pro_map[WIIMOD_PRO_KEY_B],
1719 !(ext[9] & 0x40));
1720 input_report_key(wdata->extension.input,
1721 wiimod_pro_map[WIIMOD_PRO_KEY_Y],
1722 !(ext[9] & 0x20));
1723 input_report_key(wdata->extension.input,
1724 wiimod_pro_map[WIIMOD_PRO_KEY_A],
1725 !(ext[9] & 0x10));
1726 input_report_key(wdata->extension.input,
1727 wiimod_pro_map[WIIMOD_PRO_KEY_X],
1728 !(ext[9] & 0x08));
1729 input_report_key(wdata->extension.input,
1730 wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
1731 !(ext[9] & 0x04));
1732 input_report_key(wdata->extension.input,
1733 wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
1734 !(ext[9] & 0x02));
1735 input_report_key(wdata->extension.input,
1736 wiimod_pro_map[WIIMOD_PRO_KEY_UP],
1737 !(ext[9] & 0x01));
1739 input_report_key(wdata->extension.input,
1740 wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
1741 !(ext[10] & 0x02));
1742 input_report_key(wdata->extension.input,
1743 wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
1744 !(ext[10] & 0x01));
1746 input_sync(wdata->extension.input);
1749 static int wiimod_pro_open(struct input_dev *dev)
1751 struct wiimote_data *wdata = input_get_drvdata(dev);
1752 unsigned long flags;
1754 spin_lock_irqsave(&wdata->state.lock, flags);
1755 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1756 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1757 spin_unlock_irqrestore(&wdata->state.lock, flags);
1759 return 0;
1762 static void wiimod_pro_close(struct input_dev *dev)
1764 struct wiimote_data *wdata = input_get_drvdata(dev);
1765 unsigned long flags;
1767 spin_lock_irqsave(&wdata->state.lock, flags);
1768 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1769 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1770 spin_unlock_irqrestore(&wdata->state.lock, flags);
1773 static int wiimod_pro_play(struct input_dev *dev, void *data,
1774 struct ff_effect *eff)
1776 struct wiimote_data *wdata = input_get_drvdata(dev);
1777 __u8 value;
1780 * The wiimote supports only a single rumble motor so if any magnitude
1781 * is set to non-zero then we start the rumble motor. If both are set to
1782 * zero, we stop the rumble motor.
1785 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
1786 value = 1;
1787 else
1788 value = 0;
1790 /* Locking state.lock here might deadlock with input_event() calls.
1791 * schedule_work acts as barrier. Merging multiple changes is fine. */
1792 wdata->state.cache_rumble = value;
1793 schedule_work(&wdata->rumble_worker);
1795 return 0;
1798 static ssize_t wiimod_pro_calib_show(struct device *dev,
1799 struct device_attribute *attr,
1800 char *out)
1802 struct wiimote_data *wdata = dev_to_wii(dev);
1803 int r;
1805 r = 0;
1806 r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[0]);
1807 r += sprintf(&out[r], "%+06hd ", wdata->state.calib_pro_sticks[1]);
1808 r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[2]);
1809 r += sprintf(&out[r], "%+06hd\n", wdata->state.calib_pro_sticks[3]);
1811 return r;
1814 static ssize_t wiimod_pro_calib_store(struct device *dev,
1815 struct device_attribute *attr,
1816 const char *buf, size_t count)
1818 struct wiimote_data *wdata = dev_to_wii(dev);
1819 int r;
1820 s16 x1, y1, x2, y2;
1822 if (!strncmp(buf, "scan\n", 5)) {
1823 spin_lock_irq(&wdata->state.lock);
1824 wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1825 spin_unlock_irq(&wdata->state.lock);
1826 } else {
1827 r = sscanf(buf, "%hd:%hd %hd:%hd", &x1, &y1, &x2, &y2);
1828 if (r != 4)
1829 return -EINVAL;
1831 spin_lock_irq(&wdata->state.lock);
1832 wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1833 spin_unlock_irq(&wdata->state.lock);
1835 wdata->state.calib_pro_sticks[0] = x1;
1836 wdata->state.calib_pro_sticks[1] = y1;
1837 wdata->state.calib_pro_sticks[2] = x2;
1838 wdata->state.calib_pro_sticks[3] = y2;
1841 return strnlen(buf, PAGE_SIZE);
1844 static DEVICE_ATTR(pro_calib, S_IRUGO|S_IWUSR|S_IWGRP, wiimod_pro_calib_show,
1845 wiimod_pro_calib_store);
1847 static int wiimod_pro_probe(const struct wiimod_ops *ops,
1848 struct wiimote_data *wdata)
1850 int ret, i;
1851 unsigned long flags;
1853 INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
1854 wdata->state.calib_pro_sticks[0] = 0;
1855 wdata->state.calib_pro_sticks[1] = 0;
1856 wdata->state.calib_pro_sticks[2] = 0;
1857 wdata->state.calib_pro_sticks[3] = 0;
1859 spin_lock_irqsave(&wdata->state.lock, flags);
1860 wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1861 spin_unlock_irqrestore(&wdata->state.lock, flags);
1863 wdata->extension.input = input_allocate_device();
1864 if (!wdata->extension.input)
1865 return -ENOMEM;
1867 set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
1868 input_set_drvdata(wdata->extension.input, wdata);
1870 if (input_ff_create_memless(wdata->extension.input, NULL,
1871 wiimod_pro_play)) {
1872 ret = -ENOMEM;
1873 goto err_free;
1876 ret = device_create_file(&wdata->hdev->dev,
1877 &dev_attr_pro_calib);
1878 if (ret) {
1879 hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1880 goto err_free;
1883 wdata->extension.input->open = wiimod_pro_open;
1884 wdata->extension.input->close = wiimod_pro_close;
1885 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1886 wdata->extension.input->id.bustype = wdata->hdev->bus;
1887 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1888 wdata->extension.input->id.product = wdata->hdev->product;
1889 wdata->extension.input->id.version = wdata->hdev->version;
1890 wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
1892 set_bit(EV_KEY, wdata->extension.input->evbit);
1893 for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
1894 set_bit(wiimod_pro_map[i],
1895 wdata->extension.input->keybit);
1897 set_bit(EV_ABS, wdata->extension.input->evbit);
1898 set_bit(ABS_X, wdata->extension.input->absbit);
1899 set_bit(ABS_Y, wdata->extension.input->absbit);
1900 set_bit(ABS_RX, wdata->extension.input->absbit);
1901 set_bit(ABS_RY, wdata->extension.input->absbit);
1902 input_set_abs_params(wdata->extension.input,
1903 ABS_X, -0x400, 0x400, 4, 100);
1904 input_set_abs_params(wdata->extension.input,
1905 ABS_Y, -0x400, 0x400, 4, 100);
1906 input_set_abs_params(wdata->extension.input,
1907 ABS_RX, -0x400, 0x400, 4, 100);
1908 input_set_abs_params(wdata->extension.input,
1909 ABS_RY, -0x400, 0x400, 4, 100);
1911 ret = input_register_device(wdata->extension.input);
1912 if (ret)
1913 goto err_file;
1915 return 0;
1917 err_file:
1918 device_remove_file(&wdata->hdev->dev,
1919 &dev_attr_pro_calib);
1920 err_free:
1921 input_free_device(wdata->extension.input);
1922 wdata->extension.input = NULL;
1923 return ret;
1926 static void wiimod_pro_remove(const struct wiimod_ops *ops,
1927 struct wiimote_data *wdata)
1929 unsigned long flags;
1931 if (!wdata->extension.input)
1932 return;
1934 input_unregister_device(wdata->extension.input);
1935 wdata->extension.input = NULL;
1936 cancel_work_sync(&wdata->rumble_worker);
1937 device_remove_file(&wdata->hdev->dev,
1938 &dev_attr_pro_calib);
1940 spin_lock_irqsave(&wdata->state.lock, flags);
1941 wiiproto_req_rumble(wdata, 0);
1942 spin_unlock_irqrestore(&wdata->state.lock, flags);
1945 static const struct wiimod_ops wiimod_pro = {
1946 .flags = WIIMOD_FLAG_EXT16,
1947 .arg = 0,
1948 .probe = wiimod_pro_probe,
1949 .remove = wiimod_pro_remove,
1950 .in_ext = wiimod_pro_in_ext,
1954 * Builtin Motion Plus
1955 * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
1956 * disables polling for Motion-Plus. This should be set only for devices which
1957 * don't allow MP hotplugging.
1960 static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
1961 struct wiimote_data *wdata)
1963 unsigned long flags;
1965 spin_lock_irqsave(&wdata->state.lock, flags);
1966 wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
1967 spin_unlock_irqrestore(&wdata->state.lock, flags);
1969 return 0;
1972 static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
1973 struct wiimote_data *wdata)
1975 unsigned long flags;
1977 spin_lock_irqsave(&wdata->state.lock, flags);
1978 wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
1979 spin_unlock_irqrestore(&wdata->state.lock, flags);
1982 static const struct wiimod_ops wiimod_builtin_mp = {
1983 .flags = 0,
1984 .arg = 0,
1985 .probe = wiimod_builtin_mp_probe,
1986 .remove = wiimod_builtin_mp_remove,
1990 * No Motion Plus
1991 * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
1992 * disables motion-plus. This is needed for devices that advertise this but we
1993 * don't know how to use it (or whether it is actually present).
1996 static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
1997 struct wiimote_data *wdata)
1999 unsigned long flags;
2001 spin_lock_irqsave(&wdata->state.lock, flags);
2002 wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2003 spin_unlock_irqrestore(&wdata->state.lock, flags);
2005 return 0;
2008 static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
2009 struct wiimote_data *wdata)
2011 unsigned long flags;
2013 spin_lock_irqsave(&wdata->state.lock, flags);
2014 wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2015 spin_unlock_irqrestore(&wdata->state.lock, flags);
2018 static const struct wiimod_ops wiimod_no_mp = {
2019 .flags = 0,
2020 .arg = 0,
2021 .probe = wiimod_no_mp_probe,
2022 .remove = wiimod_no_mp_remove,
2026 * Motion Plus
2027 * The Motion Plus extension provides rotation sensors (gyro) as a small
2028 * extension device for Wii Remotes. Many devices have them built-in so
2029 * you cannot see them from the outside.
2030 * Motion Plus extensions are special because they are on a separate extension
2031 * port and allow other extensions to be used simultaneously. This is all
2032 * handled by the Wiimote Core so we don't have to deal with it.
2035 static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
2037 __s32 x, y, z;
2039 /* | 8 7 6 5 4 3 | 2 | 1 |
2040 * -----+------------------------------+-----+-----+
2041 * 1 | Yaw Speed <7:0> |
2042 * 2 | Roll Speed <7:0> |
2043 * 3 | Pitch Speed <7:0> |
2044 * -----+------------------------------+-----+-----+
2045 * 4 | Yaw Speed <13:8> | Yaw |Pitch|
2046 * -----+------------------------------+-----+-----+
2047 * 5 | Roll Speed <13:8> |Roll | Ext |
2048 * -----+------------------------------+-----+-----+
2049 * 6 | Pitch Speed <13:8> | 1 | 0 |
2050 * -----+------------------------------+-----+-----+
2051 * The single bits Yaw, Roll, Pitch in the lower right corner specify
2052 * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
2053 * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
2054 * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
2055 * and 9 for slow.
2056 * If the wiimote is not rotating the sensor reports 2^13 = 8192.
2057 * Ext specifies whether an extension is connected to the motionp.
2058 * which is parsed by wiimote-core.
2061 x = ext[0];
2062 y = ext[1];
2063 z = ext[2];
2065 x |= (((__u16)ext[3]) << 6) & 0xff00;
2066 y |= (((__u16)ext[4]) << 6) & 0xff00;
2067 z |= (((__u16)ext[5]) << 6) & 0xff00;
2069 x -= 8192;
2070 y -= 8192;
2071 z -= 8192;
2073 if (!(ext[3] & 0x02))
2074 x *= 18;
2075 else
2076 x *= 9;
2077 if (!(ext[4] & 0x02))
2078 y *= 18;
2079 else
2080 y *= 9;
2081 if (!(ext[3] & 0x01))
2082 z *= 18;
2083 else
2084 z *= 9;
2086 input_report_abs(wdata->mp, ABS_RX, x);
2087 input_report_abs(wdata->mp, ABS_RY, y);
2088 input_report_abs(wdata->mp, ABS_RZ, z);
2089 input_sync(wdata->mp);
2092 static int wiimod_mp_open(struct input_dev *dev)
2094 struct wiimote_data *wdata = input_get_drvdata(dev);
2095 unsigned long flags;
2097 spin_lock_irqsave(&wdata->state.lock, flags);
2098 wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
2099 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2100 __wiimote_schedule(wdata);
2101 spin_unlock_irqrestore(&wdata->state.lock, flags);
2103 return 0;
2106 static void wiimod_mp_close(struct input_dev *dev)
2108 struct wiimote_data *wdata = input_get_drvdata(dev);
2109 unsigned long flags;
2111 spin_lock_irqsave(&wdata->state.lock, flags);
2112 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
2113 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2114 __wiimote_schedule(wdata);
2115 spin_unlock_irqrestore(&wdata->state.lock, flags);
2118 static int wiimod_mp_probe(const struct wiimod_ops *ops,
2119 struct wiimote_data *wdata)
2121 int ret;
2123 wdata->mp = input_allocate_device();
2124 if (!wdata->mp)
2125 return -ENOMEM;
2127 input_set_drvdata(wdata->mp, wdata);
2128 wdata->mp->open = wiimod_mp_open;
2129 wdata->mp->close = wiimod_mp_close;
2130 wdata->mp->dev.parent = &wdata->hdev->dev;
2131 wdata->mp->id.bustype = wdata->hdev->bus;
2132 wdata->mp->id.vendor = wdata->hdev->vendor;
2133 wdata->mp->id.product = wdata->hdev->product;
2134 wdata->mp->id.version = wdata->hdev->version;
2135 wdata->mp->name = WIIMOTE_NAME " Motion Plus";
2137 set_bit(EV_ABS, wdata->mp->evbit);
2138 set_bit(ABS_RX, wdata->mp->absbit);
2139 set_bit(ABS_RY, wdata->mp->absbit);
2140 set_bit(ABS_RZ, wdata->mp->absbit);
2141 input_set_abs_params(wdata->mp,
2142 ABS_RX, -16000, 16000, 4, 8);
2143 input_set_abs_params(wdata->mp,
2144 ABS_RY, -16000, 16000, 4, 8);
2145 input_set_abs_params(wdata->mp,
2146 ABS_RZ, -16000, 16000, 4, 8);
2148 ret = input_register_device(wdata->mp);
2149 if (ret)
2150 goto err_free;
2152 return 0;
2154 err_free:
2155 input_free_device(wdata->mp);
2156 wdata->mp = NULL;
2157 return ret;
2160 static void wiimod_mp_remove(const struct wiimod_ops *ops,
2161 struct wiimote_data *wdata)
2163 if (!wdata->mp)
2164 return;
2166 input_unregister_device(wdata->mp);
2167 wdata->mp = NULL;
2170 const struct wiimod_ops wiimod_mp = {
2171 .flags = 0,
2172 .arg = 0,
2173 .probe = wiimod_mp_probe,
2174 .remove = wiimod_mp_remove,
2175 .in_mp = wiimod_mp_in_mp,
2178 /* module table */
2180 static const struct wiimod_ops wiimod_dummy;
2182 const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
2183 [WIIMOD_KEYS] = &wiimod_keys,
2184 [WIIMOD_RUMBLE] = &wiimod_rumble,
2185 [WIIMOD_BATTERY] = &wiimod_battery,
2186 [WIIMOD_LED1] = &wiimod_leds[0],
2187 [WIIMOD_LED2] = &wiimod_leds[1],
2188 [WIIMOD_LED3] = &wiimod_leds[2],
2189 [WIIMOD_LED4] = &wiimod_leds[3],
2190 [WIIMOD_ACCEL] = &wiimod_accel,
2191 [WIIMOD_IR] = &wiimod_ir,
2192 [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
2193 [WIIMOD_NO_MP] = &wiimod_no_mp,
2196 const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
2197 [WIIMOTE_EXT_NONE] = &wiimod_dummy,
2198 [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
2199 [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
2200 [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
2201 [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
2202 [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,