2 * Device Modules for Nintendo Wii / Wii U HID Driver
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
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)
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
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"
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
],
69 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_RIGHT
],
71 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_DOWN
],
73 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_UP
],
75 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_PLUS
],
77 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_TWO
],
79 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_ONE
],
81 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_B
],
83 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_A
],
85 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_MINUS
],
87 input_report_key(wdata
->input
, wiimod_keys_map
[WIIPROTO_KEY_HOME
],
89 input_sync(wdata
->input
);
92 static int wiimod_keys_probe(const struct wiimod_ops
*ops
,
93 struct wiimote_data
*wdata
)
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
);
104 static const struct wiimod_ops wiimod_keys
= {
105 .flags
= WIIMOD_FLAG_INPUT
,
107 .probe
= wiimod_keys_probe
,
109 .in_keys
= wiimod_keys_in_keys
,
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
,
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
);
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
)
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
);
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
))
170 static void wiimod_rumble_remove(const struct wiimod_ops
*ops
,
171 struct wiimote_data
*wdata
)
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
,
185 .probe
= wiimod_rumble_probe
,
186 .remove
= wiimod_rumble_remove
,
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
= power_supply_get_drvdata(psy
);
210 if (psp
== POWER_SUPPLY_PROP_SCOPE
) {
211 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
213 } else if (psp
!= POWER_SUPPLY_PROP_CAPACITY
) {
217 ret
= wiimote_cmd_acquire(wdata
);
221 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
222 wiimote_cmd_set(wdata
, WIIPROTO_REQ_SREQ
, 0);
223 wiiproto_req_status(wdata
);
224 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
226 wiimote_cmd_wait(wdata
);
227 wiimote_cmd_release(wdata
);
229 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
230 state
= wdata
->state
.cmd_battery
;
231 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
233 val
->intval
= state
* 100 / 255;
237 static int wiimod_battery_probe(const struct wiimod_ops
*ops
,
238 struct wiimote_data
*wdata
)
240 struct power_supply_config psy_cfg
= { .drv_data
= wdata
, };
243 wdata
->battery_desc
.properties
= wiimod_battery_props
;
244 wdata
->battery_desc
.num_properties
= ARRAY_SIZE(wiimod_battery_props
);
245 wdata
->battery_desc
.get_property
= wiimod_battery_get_property
;
246 wdata
->battery_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
247 wdata
->battery_desc
.use_for_apm
= 0;
248 wdata
->battery_desc
.name
= kasprintf(GFP_KERNEL
, "wiimote_battery_%s",
250 if (!wdata
->battery_desc
.name
)
253 wdata
->battery
= power_supply_register(&wdata
->hdev
->dev
,
254 &wdata
->battery_desc
,
256 if (IS_ERR(wdata
->battery
)) {
257 hid_err(wdata
->hdev
, "cannot register battery device\n");
258 ret
= PTR_ERR(wdata
->battery
);
262 power_supply_powers(wdata
->battery
, &wdata
->hdev
->dev
);
266 kfree(wdata
->battery_desc
.name
);
267 wdata
->battery_desc
.name
= NULL
;
271 static void wiimod_battery_remove(const struct wiimod_ops
*ops
,
272 struct wiimote_data
*wdata
)
274 if (!wdata
->battery_desc
.name
)
277 power_supply_unregister(wdata
->battery
);
278 kfree(wdata
->battery_desc
.name
);
279 wdata
->battery_desc
.name
= NULL
;
282 static const struct wiimod_ops wiimod_battery
= {
285 .probe
= wiimod_battery_probe
,
286 .remove
= wiimod_battery_remove
,
291 * 0 to 4 player LEDs are supported by devices. The "arg" field of the
292 * wiimod_ops structure specifies which LED this module controls. This allows
293 * to register a limited number of LEDs.
294 * State is managed by wiimote core.
297 static enum led_brightness
wiimod_led_get(struct led_classdev
*led_dev
)
299 struct device
*dev
= led_dev
->dev
->parent
;
300 struct wiimote_data
*wdata
= dev_to_wii(dev
);
305 for (i
= 0; i
< 4; ++i
) {
306 if (wdata
->leds
[i
] == led_dev
) {
307 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
308 value
= wdata
->state
.flags
& WIIPROTO_FLAG_LED(i
+ 1);
309 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
314 return value
? LED_FULL
: LED_OFF
;
317 static void wiimod_led_set(struct led_classdev
*led_dev
,
318 enum led_brightness value
)
320 struct device
*dev
= led_dev
->dev
->parent
;
321 struct wiimote_data
*wdata
= dev_to_wii(dev
);
326 for (i
= 0; i
< 4; ++i
) {
327 if (wdata
->leds
[i
] == led_dev
) {
328 flag
= WIIPROTO_FLAG_LED(i
+ 1);
329 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
330 state
= wdata
->state
.flags
;
331 if (value
== LED_OFF
)
332 wiiproto_req_leds(wdata
, state
& ~flag
);
334 wiiproto_req_leds(wdata
, state
| flag
);
335 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
341 static int wiimod_led_probe(const struct wiimod_ops
*ops
,
342 struct wiimote_data
*wdata
)
344 struct device
*dev
= &wdata
->hdev
->dev
;
345 size_t namesz
= strlen(dev_name(dev
)) + 9;
346 struct led_classdev
*led
;
351 led
= kzalloc(sizeof(struct led_classdev
) + namesz
, GFP_KERNEL
);
355 name
= (void*)&led
[1];
356 snprintf(name
, namesz
, "%s:blue:p%lu", dev_name(dev
), ops
->arg
);
359 led
->max_brightness
= 1;
360 led
->brightness_get
= wiimod_led_get
;
361 led
->brightness_set
= wiimod_led_set
;
363 wdata
->leds
[ops
->arg
] = led
;
364 ret
= led_classdev_register(dev
, led
);
368 /* enable LED1 to stop initial LED-blinking */
370 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
371 wiiproto_req_leds(wdata
, WIIPROTO_FLAG_LED1
);
372 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
378 wdata
->leds
[ops
->arg
] = NULL
;
383 static void wiimod_led_remove(const struct wiimod_ops
*ops
,
384 struct wiimote_data
*wdata
)
386 if (!wdata
->leds
[ops
->arg
])
389 led_classdev_unregister(wdata
->leds
[ops
->arg
]);
390 kfree(wdata
->leds
[ops
->arg
]);
391 wdata
->leds
[ops
->arg
] = NULL
;
394 static const struct wiimod_ops wiimod_leds
[4] = {
398 .probe
= wiimod_led_probe
,
399 .remove
= wiimod_led_remove
,
404 .probe
= wiimod_led_probe
,
405 .remove
= wiimod_led_remove
,
410 .probe
= wiimod_led_probe
,
411 .remove
= wiimod_led_remove
,
416 .probe
= wiimod_led_probe
,
417 .remove
= wiimod_led_remove
,
423 * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
424 * device, it's mostly cleared to 0. This module parses this data and provides
425 * it via a separate input device.
428 static void wiimod_accel_in_accel(struct wiimote_data
*wdata
,
433 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_ACCEL
))
437 * payload is: BB BB XX YY ZZ
438 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
439 * contain the upper 8 bits of each value. The lower 2 bits are
440 * contained in the buttons data BB BB.
441 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
442 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
443 * accel value and bit 6 is the second bit of the Z value.
444 * The first bit of Y and Z values is not available and always set to 0.
445 * 0x200 is returned on no movement.
452 x
|= (accel
[0] >> 5) & 0x3;
453 y
|= (accel
[1] >> 4) & 0x2;
454 z
|= (accel
[1] >> 5) & 0x2;
456 input_report_abs(wdata
->accel
, ABS_RX
, x
- 0x200);
457 input_report_abs(wdata
->accel
, ABS_RY
, y
- 0x200);
458 input_report_abs(wdata
->accel
, ABS_RZ
, z
- 0x200);
459 input_sync(wdata
->accel
);
462 static int wiimod_accel_open(struct input_dev
*dev
)
464 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
467 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
468 wiiproto_req_accel(wdata
, true);
469 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
474 static void wiimod_accel_close(struct input_dev
*dev
)
476 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
479 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
480 wiiproto_req_accel(wdata
, false);
481 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
484 static int wiimod_accel_probe(const struct wiimod_ops
*ops
,
485 struct wiimote_data
*wdata
)
489 wdata
->accel
= input_allocate_device();
493 input_set_drvdata(wdata
->accel
, wdata
);
494 wdata
->accel
->open
= wiimod_accel_open
;
495 wdata
->accel
->close
= wiimod_accel_close
;
496 wdata
->accel
->dev
.parent
= &wdata
->hdev
->dev
;
497 wdata
->accel
->id
.bustype
= wdata
->hdev
->bus
;
498 wdata
->accel
->id
.vendor
= wdata
->hdev
->vendor
;
499 wdata
->accel
->id
.product
= wdata
->hdev
->product
;
500 wdata
->accel
->id
.version
= wdata
->hdev
->version
;
501 wdata
->accel
->name
= WIIMOTE_NAME
" Accelerometer";
503 set_bit(EV_ABS
, wdata
->accel
->evbit
);
504 set_bit(ABS_RX
, wdata
->accel
->absbit
);
505 set_bit(ABS_RY
, wdata
->accel
->absbit
);
506 set_bit(ABS_RZ
, wdata
->accel
->absbit
);
507 input_set_abs_params(wdata
->accel
, ABS_RX
, -500, 500, 2, 4);
508 input_set_abs_params(wdata
->accel
, ABS_RY
, -500, 500, 2, 4);
509 input_set_abs_params(wdata
->accel
, ABS_RZ
, -500, 500, 2, 4);
511 ret
= input_register_device(wdata
->accel
);
513 hid_err(wdata
->hdev
, "cannot register input device\n");
520 input_free_device(wdata
->accel
);
525 static void wiimod_accel_remove(const struct wiimod_ops
*ops
,
526 struct wiimote_data
*wdata
)
531 input_unregister_device(wdata
->accel
);
535 static const struct wiimod_ops wiimod_accel
= {
538 .probe
= wiimod_accel_probe
,
539 .remove
= wiimod_accel_remove
,
540 .in_accel
= wiimod_accel_in_accel
,
545 * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs
546 * to be initialized with a fairly complex procedure and consumes a lot of
547 * power. Therefore, as long as no application uses the IR input device, it is
549 * Nearly no other device than the normal Wii Remotes supports the IR cam so
550 * you can disable this module for these devices.
553 static void wiimod_ir_in_ir(struct wiimote_data
*wdata
, const __u8
*ir
,
554 bool packed
, unsigned int id
)
560 if (!(wdata
->state
.flags
& WIIPROTO_FLAGS_IR
))
586 * Basic IR data is encoded into 3 bytes. The first two bytes are the
587 * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
589 * If data is packed, then the 3rd byte is put first and slightly
590 * reordered. This allows to interleave packed and non-packed data to
591 * have two IR sets in 5 bytes instead of 6.
592 * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev.
596 x
= ir
[1] | ((ir
[0] & 0x03) << 8);
597 y
= ir
[2] | ((ir
[0] & 0x0c) << 6);
599 x
= ir
[0] | ((ir
[2] & 0x30) << 4);
600 y
= ir
[1] | ((ir
[2] & 0xc0) << 2);
603 input_report_abs(wdata
->ir
, xid
, x
);
604 input_report_abs(wdata
->ir
, yid
, y
);
607 input_sync(wdata
->ir
);
610 static int wiimod_ir_change(struct wiimote_data
*wdata
, __u16 mode
)
615 static const __u8 data_enable
[] = { 0x01 };
616 static const __u8 data_sens1
[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
617 0x00, 0xaa, 0x00, 0x64 };
618 static const __u8 data_sens2
[] = { 0x63, 0x03 };
619 static const __u8 data_fin
[] = { 0x08 };
621 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
623 if (mode
== (wdata
->state
.flags
& WIIPROTO_FLAGS_IR
)) {
624 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
629 wdata
->state
.flags
&= ~WIIPROTO_FLAGS_IR
;
630 wiiproto_req_ir1(wdata
, 0);
631 wiiproto_req_ir2(wdata
, 0);
632 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
633 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
637 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
639 ret
= wiimote_cmd_acquire(wdata
);
643 /* send PIXEL CLOCK ENABLE cmd first */
644 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
645 wiimote_cmd_set(wdata
, WIIPROTO_REQ_IR1
, 0);
646 wiiproto_req_ir1(wdata
, 0x06);
647 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
649 ret
= wiimote_cmd_wait(wdata
);
652 if (wdata
->state
.cmd_err
) {
657 /* enable IR LOGIC */
658 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
659 wiimote_cmd_set(wdata
, WIIPROTO_REQ_IR2
, 0);
660 wiiproto_req_ir2(wdata
, 0x06);
661 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
663 ret
= wiimote_cmd_wait(wdata
);
666 if (wdata
->state
.cmd_err
) {
671 /* enable IR cam but do not make it send data, yet */
672 ret
= wiimote_cmd_write(wdata
, 0xb00030, data_enable
,
673 sizeof(data_enable
));
677 /* write first sensitivity block */
678 ret
= wiimote_cmd_write(wdata
, 0xb00000, data_sens1
,
683 /* write second sensitivity block */
684 ret
= wiimote_cmd_write(wdata
, 0xb0001a, data_sens2
,
689 /* put IR cam into desired state */
691 case WIIPROTO_FLAG_IR_FULL
:
694 case WIIPROTO_FLAG_IR_EXT
:
697 case WIIPROTO_FLAG_IR_BASIC
:
701 ret
= wiimote_cmd_write(wdata
, 0xb00033, &format
, sizeof(format
));
705 /* make IR cam send data */
706 ret
= wiimote_cmd_write(wdata
, 0xb00030, data_fin
, sizeof(data_fin
));
710 /* request new DRM mode compatible to IR mode */
711 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
712 wdata
->state
.flags
&= ~WIIPROTO_FLAGS_IR
;
713 wdata
->state
.flags
|= mode
& WIIPROTO_FLAGS_IR
;
714 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
715 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
718 wiimote_cmd_release(wdata
);
722 static int wiimod_ir_open(struct input_dev
*dev
)
724 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
726 return wiimod_ir_change(wdata
, WIIPROTO_FLAG_IR_BASIC
);
729 static void wiimod_ir_close(struct input_dev
*dev
)
731 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
733 wiimod_ir_change(wdata
, 0);
736 static int wiimod_ir_probe(const struct wiimod_ops
*ops
,
737 struct wiimote_data
*wdata
)
741 wdata
->ir
= input_allocate_device();
745 input_set_drvdata(wdata
->ir
, wdata
);
746 wdata
->ir
->open
= wiimod_ir_open
;
747 wdata
->ir
->close
= wiimod_ir_close
;
748 wdata
->ir
->dev
.parent
= &wdata
->hdev
->dev
;
749 wdata
->ir
->id
.bustype
= wdata
->hdev
->bus
;
750 wdata
->ir
->id
.vendor
= wdata
->hdev
->vendor
;
751 wdata
->ir
->id
.product
= wdata
->hdev
->product
;
752 wdata
->ir
->id
.version
= wdata
->hdev
->version
;
753 wdata
->ir
->name
= WIIMOTE_NAME
" IR";
755 set_bit(EV_ABS
, wdata
->ir
->evbit
);
756 set_bit(ABS_HAT0X
, wdata
->ir
->absbit
);
757 set_bit(ABS_HAT0Y
, wdata
->ir
->absbit
);
758 set_bit(ABS_HAT1X
, wdata
->ir
->absbit
);
759 set_bit(ABS_HAT1Y
, wdata
->ir
->absbit
);
760 set_bit(ABS_HAT2X
, wdata
->ir
->absbit
);
761 set_bit(ABS_HAT2Y
, wdata
->ir
->absbit
);
762 set_bit(ABS_HAT3X
, wdata
->ir
->absbit
);
763 set_bit(ABS_HAT3Y
, wdata
->ir
->absbit
);
764 input_set_abs_params(wdata
->ir
, ABS_HAT0X
, 0, 1023, 2, 4);
765 input_set_abs_params(wdata
->ir
, ABS_HAT0Y
, 0, 767, 2, 4);
766 input_set_abs_params(wdata
->ir
, ABS_HAT1X
, 0, 1023, 2, 4);
767 input_set_abs_params(wdata
->ir
, ABS_HAT1Y
, 0, 767, 2, 4);
768 input_set_abs_params(wdata
->ir
, ABS_HAT2X
, 0, 1023, 2, 4);
769 input_set_abs_params(wdata
->ir
, ABS_HAT2Y
, 0, 767, 2, 4);
770 input_set_abs_params(wdata
->ir
, ABS_HAT3X
, 0, 1023, 2, 4);
771 input_set_abs_params(wdata
->ir
, ABS_HAT3Y
, 0, 767, 2, 4);
773 ret
= input_register_device(wdata
->ir
);
775 hid_err(wdata
->hdev
, "cannot register input device\n");
782 input_free_device(wdata
->ir
);
787 static void wiimod_ir_remove(const struct wiimod_ops
*ops
,
788 struct wiimote_data
*wdata
)
793 input_unregister_device(wdata
->ir
);
797 static const struct wiimod_ops wiimod_ir
= {
800 .probe
= wiimod_ir_probe
,
801 .remove
= wiimod_ir_remove
,
802 .in_ir
= wiimod_ir_in_ir
,
807 * The Nintendo Wii Nunchuk was the first official extension published by
808 * Nintendo. It provides two additional keys and a separate accelerometer. It
809 * can be hotplugged to standard Wii Remotes.
812 enum wiimod_nunchuk_keys
{
813 WIIMOD_NUNCHUK_KEY_C
,
814 WIIMOD_NUNCHUK_KEY_Z
,
815 WIIMOD_NUNCHUK_KEY_NUM
,
818 static const __u16 wiimod_nunchuk_map
[] = {
819 BTN_C
, /* WIIMOD_NUNCHUK_KEY_C */
820 BTN_Z
, /* WIIMOD_NUNCHUK_KEY_Z */
823 static void wiimod_nunchuk_in_ext(struct wiimote_data
*wdata
, const __u8
*ext
)
825 __s16 x
, y
, z
, bx
, by
;
827 /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 |
828 * -----+----------+---------+---------+----+-----+
829 * 1 | Button X <7:0> |
830 * 2 | Button Y <7:0> |
831 * -----+----------+---------+---------+----+-----+
832 * 3 | Speed X <9:2> |
833 * 4 | Speed Y <9:2> |
834 * 5 | Speed Z <9:2> |
835 * -----+----------+---------+---------+----+-----+
836 * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ |
837 * -----+----------+---------+---------+----+-----+
838 * Button X/Y is the analog stick. Speed X, Y and Z are the
839 * accelerometer data in the same format as the wiimote's accelerometer.
840 * The 6th byte contains the LSBs of the accelerometer data.
841 * BC and BZ are the C and Z buttons: 0 means pressed
843 * If reported interleaved with motionp, then the layout changes. The
844 * 5th and 6th byte changes to:
845 * -----+-----------------------------------+-----+
846 * 5 | Speed Z <9:3> | EXT |
847 * -----+--------+-----+-----+----+----+----+-----+
848 * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 |
849 * -----+--------+-----+-----+----+----+----+-----+
850 * All three accelerometer values lose their LSB. The other data is
851 * still available but slightly moved.
853 * Center data for button values is 128. Center value for accelerometer
854 * values it 512 / 0x200
866 if (wdata
->state
.flags
& WIIPROTO_FLAG_MP_ACTIVE
) {
867 x
|= (ext
[5] >> 3) & 0x02;
868 y
|= (ext
[5] >> 4) & 0x02;
870 z
|= (ext
[5] >> 5) & 0x06;
872 x
|= (ext
[5] >> 2) & 0x03;
873 y
|= (ext
[5] >> 4) & 0x03;
874 z
|= (ext
[5] >> 6) & 0x03;
881 input_report_abs(wdata
->extension
.input
, ABS_HAT0X
, bx
);
882 input_report_abs(wdata
->extension
.input
, ABS_HAT0Y
, by
);
884 input_report_abs(wdata
->extension
.input
, ABS_RX
, x
);
885 input_report_abs(wdata
->extension
.input
, ABS_RY
, y
);
886 input_report_abs(wdata
->extension
.input
, ABS_RZ
, z
);
888 if (wdata
->state
.flags
& WIIPROTO_FLAG_MP_ACTIVE
) {
889 input_report_key(wdata
->extension
.input
,
890 wiimod_nunchuk_map
[WIIMOD_NUNCHUK_KEY_Z
],
892 input_report_key(wdata
->extension
.input
,
893 wiimod_nunchuk_map
[WIIMOD_NUNCHUK_KEY_C
],
896 input_report_key(wdata
->extension
.input
,
897 wiimod_nunchuk_map
[WIIMOD_NUNCHUK_KEY_Z
],
899 input_report_key(wdata
->extension
.input
,
900 wiimod_nunchuk_map
[WIIMOD_NUNCHUK_KEY_C
],
904 input_sync(wdata
->extension
.input
);
907 static int wiimod_nunchuk_open(struct input_dev
*dev
)
909 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
912 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
913 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_USED
;
914 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
915 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
920 static void wiimod_nunchuk_close(struct input_dev
*dev
)
922 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
925 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
926 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_USED
;
927 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
928 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
931 static int wiimod_nunchuk_probe(const struct wiimod_ops
*ops
,
932 struct wiimote_data
*wdata
)
936 wdata
->extension
.input
= input_allocate_device();
937 if (!wdata
->extension
.input
)
940 input_set_drvdata(wdata
->extension
.input
, wdata
);
941 wdata
->extension
.input
->open
= wiimod_nunchuk_open
;
942 wdata
->extension
.input
->close
= wiimod_nunchuk_close
;
943 wdata
->extension
.input
->dev
.parent
= &wdata
->hdev
->dev
;
944 wdata
->extension
.input
->id
.bustype
= wdata
->hdev
->bus
;
945 wdata
->extension
.input
->id
.vendor
= wdata
->hdev
->vendor
;
946 wdata
->extension
.input
->id
.product
= wdata
->hdev
->product
;
947 wdata
->extension
.input
->id
.version
= wdata
->hdev
->version
;
948 wdata
->extension
.input
->name
= WIIMOTE_NAME
" Nunchuk";
950 set_bit(EV_KEY
, wdata
->extension
.input
->evbit
);
951 for (i
= 0; i
< WIIMOD_NUNCHUK_KEY_NUM
; ++i
)
952 set_bit(wiimod_nunchuk_map
[i
],
953 wdata
->extension
.input
->keybit
);
955 set_bit(EV_ABS
, wdata
->extension
.input
->evbit
);
956 set_bit(ABS_HAT0X
, wdata
->extension
.input
->absbit
);
957 set_bit(ABS_HAT0Y
, wdata
->extension
.input
->absbit
);
958 input_set_abs_params(wdata
->extension
.input
,
959 ABS_HAT0X
, -120, 120, 2, 4);
960 input_set_abs_params(wdata
->extension
.input
,
961 ABS_HAT0Y
, -120, 120, 2, 4);
962 set_bit(ABS_RX
, wdata
->extension
.input
->absbit
);
963 set_bit(ABS_RY
, wdata
->extension
.input
->absbit
);
964 set_bit(ABS_RZ
, wdata
->extension
.input
->absbit
);
965 input_set_abs_params(wdata
->extension
.input
,
966 ABS_RX
, -500, 500, 2, 4);
967 input_set_abs_params(wdata
->extension
.input
,
968 ABS_RY
, -500, 500, 2, 4);
969 input_set_abs_params(wdata
->extension
.input
,
970 ABS_RZ
, -500, 500, 2, 4);
972 ret
= input_register_device(wdata
->extension
.input
);
979 input_free_device(wdata
->extension
.input
);
980 wdata
->extension
.input
= NULL
;
984 static void wiimod_nunchuk_remove(const struct wiimod_ops
*ops
,
985 struct wiimote_data
*wdata
)
987 if (!wdata
->extension
.input
)
990 input_unregister_device(wdata
->extension
.input
);
991 wdata
->extension
.input
= NULL
;
994 static const struct wiimod_ops wiimod_nunchuk
= {
997 .probe
= wiimod_nunchuk_probe
,
998 .remove
= wiimod_nunchuk_remove
,
999 .in_ext
= wiimod_nunchuk_in_ext
,
1003 * Classic Controller
1004 * Another official extension from Nintendo. It provides a classic
1005 * gamecube-like controller that can be hotplugged on the Wii Remote.
1006 * It has several hardware buttons and switches that are all reported via
1007 * a normal extension device.
1010 enum wiimod_classic_keys
{
1011 WIIMOD_CLASSIC_KEY_A
,
1012 WIIMOD_CLASSIC_KEY_B
,
1013 WIIMOD_CLASSIC_KEY_X
,
1014 WIIMOD_CLASSIC_KEY_Y
,
1015 WIIMOD_CLASSIC_KEY_ZL
,
1016 WIIMOD_CLASSIC_KEY_ZR
,
1017 WIIMOD_CLASSIC_KEY_PLUS
,
1018 WIIMOD_CLASSIC_KEY_MINUS
,
1019 WIIMOD_CLASSIC_KEY_HOME
,
1020 WIIMOD_CLASSIC_KEY_LEFT
,
1021 WIIMOD_CLASSIC_KEY_RIGHT
,
1022 WIIMOD_CLASSIC_KEY_UP
,
1023 WIIMOD_CLASSIC_KEY_DOWN
,
1024 WIIMOD_CLASSIC_KEY_LT
,
1025 WIIMOD_CLASSIC_KEY_RT
,
1026 WIIMOD_CLASSIC_KEY_NUM
,
1029 static const __u16 wiimod_classic_map
[] = {
1030 BTN_A
, /* WIIMOD_CLASSIC_KEY_A */
1031 BTN_B
, /* WIIMOD_CLASSIC_KEY_B */
1032 BTN_X
, /* WIIMOD_CLASSIC_KEY_X */
1033 BTN_Y
, /* WIIMOD_CLASSIC_KEY_Y */
1034 BTN_TL2
, /* WIIMOD_CLASSIC_KEY_ZL */
1035 BTN_TR2
, /* WIIMOD_CLASSIC_KEY_ZR */
1036 KEY_NEXT
, /* WIIMOD_CLASSIC_KEY_PLUS */
1037 KEY_PREVIOUS
, /* WIIMOD_CLASSIC_KEY_MINUS */
1038 BTN_MODE
, /* WIIMOD_CLASSIC_KEY_HOME */
1039 KEY_LEFT
, /* WIIMOD_CLASSIC_KEY_LEFT */
1040 KEY_RIGHT
, /* WIIMOD_CLASSIC_KEY_RIGHT */
1041 KEY_UP
, /* WIIMOD_CLASSIC_KEY_UP */
1042 KEY_DOWN
, /* WIIMOD_CLASSIC_KEY_DOWN */
1043 BTN_TL
, /* WIIMOD_CLASSIC_KEY_LT */
1044 BTN_TR
, /* WIIMOD_CLASSIC_KEY_RT */
1047 static void wiimod_classic_in_ext(struct wiimote_data
*wdata
, const __u8
*ext
)
1049 __s8 rx
, ry
, lx
, ly
, lt
, rt
;
1051 /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1052 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1053 * 1 | RX <5:4> | LX <5:0> |
1054 * 2 | RX <3:2> | LY <5:0> |
1055 * -----+-----+-----+-----+-----------------------------+
1056 * 3 |RX<1>| LT <5:4> | RY <5:1> |
1057 * -----+-----+-----------+-----------------------------+
1058 * 4 | LT <3:1> | RT <5:1> |
1059 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1060 * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
1061 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1062 * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
1063 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1064 * All buttons are 0 if pressed
1065 * RX and RY are right analog stick
1066 * LX and LY are left analog stick
1067 * LT is left trigger, RT is right trigger
1068 * BLT is 0 if left trigger is fully pressed
1069 * BRT is 0 if right trigger is fully pressed
1070 * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1071 * BZL is left Z button and BZR is right Z button
1072 * B-, BH, B+ are +, HOME and - buttons
1073 * BB, BY, BA, BX are A, B, X, Y buttons
1074 * LSB of RX, RY, LT, and RT are not transmitted and always 0.
1076 * With motionp enabled it changes slightly to this:
1077 * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1078 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1079 * 1 | RX <5:4> | LX <5:1> | BDU |
1080 * 2 | RX <3:2> | LY <5:1> | BDL |
1081 * -----+-----+-----+-----+-----------------------+-----+
1082 * 3 |RX<1>| LT <5:4> | RY <5:1> |
1083 * -----+-----+-----------+-----------------------------+
1084 * 4 | LT <3:1> | RT <5:1> |
1085 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1086 * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT |
1087 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1088 * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 |
1089 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1090 * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
1091 * is the same as before.
1094 if (wdata
->state
.flags
& WIIPROTO_FLAG_MP_ACTIVE
) {
1102 rx
= (ext
[0] >> 3) & 0x18;
1103 rx
|= (ext
[1] >> 5) & 0x06;
1104 rx
|= (ext
[2] >> 7) & 0x01;
1108 lt
= (ext
[2] >> 2) & 0x18;
1109 lt
|= (ext
[3] >> 5) & 0x07;
1116 input_report_abs(wdata
->extension
.input
, ABS_HAT1X
, lx
- 0x20);
1117 input_report_abs(wdata
->extension
.input
, ABS_HAT1Y
, ly
- 0x20);
1118 input_report_abs(wdata
->extension
.input
, ABS_HAT2X
, rx
- 0x20);
1119 input_report_abs(wdata
->extension
.input
, ABS_HAT2Y
, ry
- 0x20);
1120 input_report_abs(wdata
->extension
.input
, ABS_HAT3X
, rt
);
1121 input_report_abs(wdata
->extension
.input
, ABS_HAT3Y
, lt
);
1123 input_report_key(wdata
->extension
.input
,
1124 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_RIGHT
],
1126 input_report_key(wdata
->extension
.input
,
1127 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_DOWN
],
1129 input_report_key(wdata
->extension
.input
,
1130 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_LT
],
1132 input_report_key(wdata
->extension
.input
,
1133 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_MINUS
],
1135 input_report_key(wdata
->extension
.input
,
1136 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_HOME
],
1138 input_report_key(wdata
->extension
.input
,
1139 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_PLUS
],
1141 input_report_key(wdata
->extension
.input
,
1142 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_RT
],
1144 input_report_key(wdata
->extension
.input
,
1145 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_ZL
],
1147 input_report_key(wdata
->extension
.input
,
1148 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_B
],
1150 input_report_key(wdata
->extension
.input
,
1151 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_Y
],
1153 input_report_key(wdata
->extension
.input
,
1154 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_A
],
1156 input_report_key(wdata
->extension
.input
,
1157 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_X
],
1159 input_report_key(wdata
->extension
.input
,
1160 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_ZR
],
1163 if (wdata
->state
.flags
& WIIPROTO_FLAG_MP_ACTIVE
) {
1164 input_report_key(wdata
->extension
.input
,
1165 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_LEFT
],
1167 input_report_key(wdata
->extension
.input
,
1168 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_UP
],
1171 input_report_key(wdata
->extension
.input
,
1172 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_LEFT
],
1174 input_report_key(wdata
->extension
.input
,
1175 wiimod_classic_map
[WIIMOD_CLASSIC_KEY_UP
],
1179 input_sync(wdata
->extension
.input
);
1182 static int wiimod_classic_open(struct input_dev
*dev
)
1184 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1185 unsigned long flags
;
1187 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1188 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_USED
;
1189 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1190 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1195 static void wiimod_classic_close(struct input_dev
*dev
)
1197 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1198 unsigned long flags
;
1200 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1201 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_USED
;
1202 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1203 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1206 static int wiimod_classic_probe(const struct wiimod_ops
*ops
,
1207 struct wiimote_data
*wdata
)
1211 wdata
->extension
.input
= input_allocate_device();
1212 if (!wdata
->extension
.input
)
1215 input_set_drvdata(wdata
->extension
.input
, wdata
);
1216 wdata
->extension
.input
->open
= wiimod_classic_open
;
1217 wdata
->extension
.input
->close
= wiimod_classic_close
;
1218 wdata
->extension
.input
->dev
.parent
= &wdata
->hdev
->dev
;
1219 wdata
->extension
.input
->id
.bustype
= wdata
->hdev
->bus
;
1220 wdata
->extension
.input
->id
.vendor
= wdata
->hdev
->vendor
;
1221 wdata
->extension
.input
->id
.product
= wdata
->hdev
->product
;
1222 wdata
->extension
.input
->id
.version
= wdata
->hdev
->version
;
1223 wdata
->extension
.input
->name
= WIIMOTE_NAME
" Classic Controller";
1225 set_bit(EV_KEY
, wdata
->extension
.input
->evbit
);
1226 for (i
= 0; i
< WIIMOD_CLASSIC_KEY_NUM
; ++i
)
1227 set_bit(wiimod_classic_map
[i
],
1228 wdata
->extension
.input
->keybit
);
1230 set_bit(EV_ABS
, wdata
->extension
.input
->evbit
);
1231 set_bit(ABS_HAT1X
, wdata
->extension
.input
->absbit
);
1232 set_bit(ABS_HAT1Y
, wdata
->extension
.input
->absbit
);
1233 set_bit(ABS_HAT2X
, wdata
->extension
.input
->absbit
);
1234 set_bit(ABS_HAT2Y
, wdata
->extension
.input
->absbit
);
1235 set_bit(ABS_HAT3X
, wdata
->extension
.input
->absbit
);
1236 set_bit(ABS_HAT3Y
, wdata
->extension
.input
->absbit
);
1237 input_set_abs_params(wdata
->extension
.input
,
1238 ABS_HAT1X
, -30, 30, 1, 1);
1239 input_set_abs_params(wdata
->extension
.input
,
1240 ABS_HAT1Y
, -30, 30, 1, 1);
1241 input_set_abs_params(wdata
->extension
.input
,
1242 ABS_HAT2X
, -30, 30, 1, 1);
1243 input_set_abs_params(wdata
->extension
.input
,
1244 ABS_HAT2Y
, -30, 30, 1, 1);
1245 input_set_abs_params(wdata
->extension
.input
,
1246 ABS_HAT3X
, -30, 30, 1, 1);
1247 input_set_abs_params(wdata
->extension
.input
,
1248 ABS_HAT3Y
, -30, 30, 1, 1);
1250 ret
= input_register_device(wdata
->extension
.input
);
1257 input_free_device(wdata
->extension
.input
);
1258 wdata
->extension
.input
= NULL
;
1262 static void wiimod_classic_remove(const struct wiimod_ops
*ops
,
1263 struct wiimote_data
*wdata
)
1265 if (!wdata
->extension
.input
)
1268 input_unregister_device(wdata
->extension
.input
);
1269 wdata
->extension
.input
= NULL
;
1272 static const struct wiimod_ops wiimod_classic
= {
1275 .probe
= wiimod_classic_probe
,
1276 .remove
= wiimod_classic_remove
,
1277 .in_ext
= wiimod_classic_in_ext
,
1281 * Balance Board Extension
1282 * The Nintendo Wii Balance Board provides four hardware weight sensor plus a
1283 * single push button. No other peripherals are available. However, the
1284 * balance-board data is sent via a standard Wii Remote extension. All other
1285 * data for non-present hardware is zeroed out.
1286 * Some 3rd party devices react allergic if we try to access normal Wii Remote
1287 * hardware, so this extension module should be the only module that is loaded
1288 * on balance boards.
1289 * The balance board needs 8 bytes extension data instead of basic 6 bytes so
1290 * it needs the WIIMOD_FLAG_EXT8 flag.
1293 static void wiimod_bboard_in_keys(struct wiimote_data
*wdata
, const __u8
*keys
)
1295 input_report_key(wdata
->extension
.input
, BTN_A
,
1296 !!(keys
[1] & 0x08));
1297 input_sync(wdata
->extension
.input
);
1300 static void wiimod_bboard_in_ext(struct wiimote_data
*wdata
,
1303 __s32 val
[4], tmp
, div
;
1305 struct wiimote_state
*s
= &wdata
->state
;
1308 * Balance board data layout:
1310 * Byte | 8 7 6 5 4 3 2 1 |
1311 * -----+--------------------------+
1312 * 1 | Top Right <15:8> |
1313 * 2 | Top Right <7:0> |
1314 * -----+--------------------------+
1315 * 3 | Bottom Right <15:8> |
1316 * 4 | Bottom Right <7:0> |
1317 * -----+--------------------------+
1318 * 5 | Top Left <15:8> |
1319 * 6 | Top Left <7:0> |
1320 * -----+--------------------------+
1321 * 7 | Bottom Left <15:8> |
1322 * 8 | Bottom Left <7:0> |
1323 * -----+--------------------------+
1325 * These values represent the weight-measurements of the Wii-balance
1326 * board with 16bit precision.
1328 * The balance-board is never reported interleaved with motionp.
1347 /* apply calibration data */
1348 for (i
= 0; i
< 4; i
++) {
1349 if (val
[i
] <= s
->calib_bboard
[i
][0]) {
1351 } else if (val
[i
] < s
->calib_bboard
[i
][1]) {
1352 tmp
= val
[i
] - s
->calib_bboard
[i
][0];
1354 div
= s
->calib_bboard
[i
][1] - s
->calib_bboard
[i
][0];
1355 tmp
/= div
? div
: 1;
1357 tmp
= val
[i
] - s
->calib_bboard
[i
][1];
1359 div
= s
->calib_bboard
[i
][2] - s
->calib_bboard
[i
][1];
1360 tmp
/= div
? div
: 1;
1366 input_report_abs(wdata
->extension
.input
, ABS_HAT0X
, val
[0]);
1367 input_report_abs(wdata
->extension
.input
, ABS_HAT0Y
, val
[1]);
1368 input_report_abs(wdata
->extension
.input
, ABS_HAT1X
, val
[2]);
1369 input_report_abs(wdata
->extension
.input
, ABS_HAT1Y
, val
[3]);
1370 input_sync(wdata
->extension
.input
);
1373 static int wiimod_bboard_open(struct input_dev
*dev
)
1375 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1376 unsigned long flags
;
1378 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1379 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_USED
;
1380 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1381 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1386 static void wiimod_bboard_close(struct input_dev
*dev
)
1388 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1389 unsigned long flags
;
1391 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1392 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_USED
;
1393 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1394 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1397 static ssize_t
wiimod_bboard_calib_show(struct device
*dev
,
1398 struct device_attribute
*attr
,
1401 struct wiimote_data
*wdata
= dev_to_wii(dev
);
1406 ret
= wiimote_cmd_acquire(wdata
);
1410 ret
= wiimote_cmd_read(wdata
, 0xa40024, buf
, 12);
1412 wiimote_cmd_release(wdata
);
1413 return ret
< 0 ? ret
: -EIO
;
1415 ret
= wiimote_cmd_read(wdata
, 0xa40024 + 12, buf
+ 12, 12);
1417 wiimote_cmd_release(wdata
);
1418 return ret
< 0 ? ret
: -EIO
;
1421 wiimote_cmd_release(wdata
);
1423 spin_lock_irq(&wdata
->state
.lock
);
1425 for (i
= 0; i
< 3; ++i
) {
1426 for (j
= 0; j
< 4; ++j
) {
1427 wdata
->state
.calib_bboard
[j
][i
] = buf
[offs
];
1428 wdata
->state
.calib_bboard
[j
][i
] <<= 8;
1429 wdata
->state
.calib_bboard
[j
][i
] |= buf
[offs
+ 1];
1433 spin_unlock_irq(&wdata
->state
.lock
);
1436 for (i
= 0; i
< 3; ++i
) {
1437 for (j
= 0; j
< 4; ++j
) {
1438 val
= wdata
->state
.calib_bboard
[j
][i
];
1439 if (i
== 2 && j
== 3)
1440 ret
+= sprintf(&out
[ret
], "%04x\n", val
);
1442 ret
+= sprintf(&out
[ret
], "%04x:", val
);
1449 static DEVICE_ATTR(bboard_calib
, S_IRUGO
, wiimod_bboard_calib_show
, NULL
);
1451 static int wiimod_bboard_probe(const struct wiimod_ops
*ops
,
1452 struct wiimote_data
*wdata
)
1457 wiimote_cmd_acquire_noint(wdata
);
1459 ret
= wiimote_cmd_read(wdata
, 0xa40024, buf
, 12);
1461 wiimote_cmd_release(wdata
);
1462 return ret
< 0 ? ret
: -EIO
;
1464 ret
= wiimote_cmd_read(wdata
, 0xa40024 + 12, buf
+ 12, 12);
1466 wiimote_cmd_release(wdata
);
1467 return ret
< 0 ? ret
: -EIO
;
1470 wiimote_cmd_release(wdata
);
1473 for (i
= 0; i
< 3; ++i
) {
1474 for (j
= 0; j
< 4; ++j
) {
1475 wdata
->state
.calib_bboard
[j
][i
] = buf
[offs
];
1476 wdata
->state
.calib_bboard
[j
][i
] <<= 8;
1477 wdata
->state
.calib_bboard
[j
][i
] |= buf
[offs
+ 1];
1482 wdata
->extension
.input
= input_allocate_device();
1483 if (!wdata
->extension
.input
)
1486 ret
= device_create_file(&wdata
->hdev
->dev
,
1487 &dev_attr_bboard_calib
);
1489 hid_err(wdata
->hdev
, "cannot create sysfs attribute\n");
1493 input_set_drvdata(wdata
->extension
.input
, wdata
);
1494 wdata
->extension
.input
->open
= wiimod_bboard_open
;
1495 wdata
->extension
.input
->close
= wiimod_bboard_close
;
1496 wdata
->extension
.input
->dev
.parent
= &wdata
->hdev
->dev
;
1497 wdata
->extension
.input
->id
.bustype
= wdata
->hdev
->bus
;
1498 wdata
->extension
.input
->id
.vendor
= wdata
->hdev
->vendor
;
1499 wdata
->extension
.input
->id
.product
= wdata
->hdev
->product
;
1500 wdata
->extension
.input
->id
.version
= wdata
->hdev
->version
;
1501 wdata
->extension
.input
->name
= WIIMOTE_NAME
" Balance Board";
1503 set_bit(EV_KEY
, wdata
->extension
.input
->evbit
);
1504 set_bit(BTN_A
, wdata
->extension
.input
->keybit
);
1506 set_bit(EV_ABS
, wdata
->extension
.input
->evbit
);
1507 set_bit(ABS_HAT0X
, wdata
->extension
.input
->absbit
);
1508 set_bit(ABS_HAT0Y
, wdata
->extension
.input
->absbit
);
1509 set_bit(ABS_HAT1X
, wdata
->extension
.input
->absbit
);
1510 set_bit(ABS_HAT1Y
, wdata
->extension
.input
->absbit
);
1511 input_set_abs_params(wdata
->extension
.input
,
1512 ABS_HAT0X
, 0, 65535, 2, 4);
1513 input_set_abs_params(wdata
->extension
.input
,
1514 ABS_HAT0Y
, 0, 65535, 2, 4);
1515 input_set_abs_params(wdata
->extension
.input
,
1516 ABS_HAT1X
, 0, 65535, 2, 4);
1517 input_set_abs_params(wdata
->extension
.input
,
1518 ABS_HAT1Y
, 0, 65535, 2, 4);
1520 ret
= input_register_device(wdata
->extension
.input
);
1527 device_remove_file(&wdata
->hdev
->dev
,
1528 &dev_attr_bboard_calib
);
1530 input_free_device(wdata
->extension
.input
);
1531 wdata
->extension
.input
= NULL
;
1535 static void wiimod_bboard_remove(const struct wiimod_ops
*ops
,
1536 struct wiimote_data
*wdata
)
1538 if (!wdata
->extension
.input
)
1541 input_unregister_device(wdata
->extension
.input
);
1542 wdata
->extension
.input
= NULL
;
1543 device_remove_file(&wdata
->hdev
->dev
,
1544 &dev_attr_bboard_calib
);
1547 static const struct wiimod_ops wiimod_bboard
= {
1548 .flags
= WIIMOD_FLAG_EXT8
,
1550 .probe
= wiimod_bboard_probe
,
1551 .remove
= wiimod_bboard_remove
,
1552 .in_keys
= wiimod_bboard_in_keys
,
1553 .in_ext
= wiimod_bboard_in_ext
,
1558 * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not
1559 * work together with the classic Wii, but only with the new Wii U. However, it
1560 * uses the same protocol and provides a builtin "classic controller pro"
1561 * extension, few standard buttons, a rumble motor, 4 LEDs and a battery.
1562 * We provide all these via a standard extension device as the device doesn't
1563 * feature an extension port.
1566 enum wiimod_pro_keys
{
1571 WIIMOD_PRO_KEY_PLUS
,
1572 WIIMOD_PRO_KEY_MINUS
,
1573 WIIMOD_PRO_KEY_HOME
,
1574 WIIMOD_PRO_KEY_LEFT
,
1575 WIIMOD_PRO_KEY_RIGHT
,
1577 WIIMOD_PRO_KEY_DOWN
,
1582 WIIMOD_PRO_KEY_THUMBL
,
1583 WIIMOD_PRO_KEY_THUMBR
,
1587 static const __u16 wiimod_pro_map
[] = {
1588 BTN_EAST
, /* WIIMOD_PRO_KEY_A */
1589 BTN_SOUTH
, /* WIIMOD_PRO_KEY_B */
1590 BTN_NORTH
, /* WIIMOD_PRO_KEY_X */
1591 BTN_WEST
, /* WIIMOD_PRO_KEY_Y */
1592 BTN_START
, /* WIIMOD_PRO_KEY_PLUS */
1593 BTN_SELECT
, /* WIIMOD_PRO_KEY_MINUS */
1594 BTN_MODE
, /* WIIMOD_PRO_KEY_HOME */
1595 BTN_DPAD_LEFT
, /* WIIMOD_PRO_KEY_LEFT */
1596 BTN_DPAD_RIGHT
, /* WIIMOD_PRO_KEY_RIGHT */
1597 BTN_DPAD_UP
, /* WIIMOD_PRO_KEY_UP */
1598 BTN_DPAD_DOWN
, /* WIIMOD_PRO_KEY_DOWN */
1599 BTN_TL
, /* WIIMOD_PRO_KEY_TL */
1600 BTN_TR
, /* WIIMOD_PRO_KEY_TR */
1601 BTN_TL2
, /* WIIMOD_PRO_KEY_ZL */
1602 BTN_TR2
, /* WIIMOD_PRO_KEY_ZR */
1603 BTN_THUMBL
, /* WIIMOD_PRO_KEY_THUMBL */
1604 BTN_THUMBR
, /* WIIMOD_PRO_KEY_THUMBR */
1607 static void wiimod_pro_in_ext(struct wiimote_data
*wdata
, const __u8
*ext
)
1609 __s16 rx
, ry
, lx
, ly
;
1611 /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
1612 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1614 * -----+-----------------------+-----------------------+
1615 * 2 | 0 0 0 0 | LX <11:8> |
1616 * -----+-----------------------+-----------------------+
1618 * -----+-----------------------+-----------------------+
1619 * 4 | 0 0 0 0 | RX <11:8> |
1620 * -----+-----------------------+-----------------------+
1622 * -----+-----------------------+-----------------------+
1623 * 6 | 0 0 0 0 | LY <11:8> |
1624 * -----+-----------------------+-----------------------+
1626 * -----+-----------------------+-----------------------+
1627 * 8 | 0 0 0 0 | RY <11:8> |
1628 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1629 * 9 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
1630 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1631 * 10 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
1632 * -----+-----+-----+-----+-----+-----+-----+-----+-----+
1633 * 11 | 1 | BATTERY | USB |CHARG|LTHUM|RTHUM|
1634 * -----+-----+-----------------+-----------+-----+-----+
1635 * All buttons are low-active (0 if pressed)
1636 * RX and RY are right analog stick
1637 * LX and LY are left analog stick
1638 * BLT is left trigger, BRT is right trigger.
1639 * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
1640 * BZL is left Z button and BZR is right Z button
1641 * B-, BH, B+ are +, HOME and - buttons
1642 * BB, BY, BA, BX are A, B, X, Y buttons
1644 * Bits marked as 0/1 are unknown and never changed during tests.
1646 * Not entirely verified:
1647 * CHARG: 1 if uncharging, 0 if charging
1648 * USB: 1 if not connected, 0 if connected
1649 * BATTERY: battery capacity from 000 (empty) to 100 (full)
1652 lx
= (ext
[0] & 0xff) | ((ext
[1] & 0x0f) << 8);
1653 rx
= (ext
[2] & 0xff) | ((ext
[3] & 0x0f) << 8);
1654 ly
= (ext
[4] & 0xff) | ((ext
[5] & 0x0f) << 8);
1655 ry
= (ext
[6] & 0xff) | ((ext
[7] & 0x0f) << 8);
1657 /* zero-point offsets */
1663 /* Trivial automatic calibration. We don't know any calibration data
1664 * in the EEPROM so we must use the first report to calibrate the
1665 * null-position of the analog sticks. Users can retrigger calibration
1666 * via sysfs, or set it explicitly. If data is off more than abs(500),
1667 * we skip calibration as the sticks are likely to be moved already. */
1668 if (!(wdata
->state
.flags
& WIIPROTO_FLAG_PRO_CALIB_DONE
)) {
1669 wdata
->state
.flags
|= WIIPROTO_FLAG_PRO_CALIB_DONE
;
1671 wdata
->state
.calib_pro_sticks
[0] = -lx
;
1673 wdata
->state
.calib_pro_sticks
[1] = -ly
;
1675 wdata
->state
.calib_pro_sticks
[2] = -rx
;
1677 wdata
->state
.calib_pro_sticks
[3] = -ry
;
1680 /* apply calibration data */
1681 lx
+= wdata
->state
.calib_pro_sticks
[0];
1682 ly
+= wdata
->state
.calib_pro_sticks
[1];
1683 rx
+= wdata
->state
.calib_pro_sticks
[2];
1684 ry
+= wdata
->state
.calib_pro_sticks
[3];
1686 input_report_abs(wdata
->extension
.input
, ABS_X
, lx
);
1687 input_report_abs(wdata
->extension
.input
, ABS_Y
, ly
);
1688 input_report_abs(wdata
->extension
.input
, ABS_RX
, rx
);
1689 input_report_abs(wdata
->extension
.input
, ABS_RY
, ry
);
1691 input_report_key(wdata
->extension
.input
,
1692 wiimod_pro_map
[WIIMOD_PRO_KEY_RIGHT
],
1694 input_report_key(wdata
->extension
.input
,
1695 wiimod_pro_map
[WIIMOD_PRO_KEY_DOWN
],
1697 input_report_key(wdata
->extension
.input
,
1698 wiimod_pro_map
[WIIMOD_PRO_KEY_TL
],
1700 input_report_key(wdata
->extension
.input
,
1701 wiimod_pro_map
[WIIMOD_PRO_KEY_MINUS
],
1703 input_report_key(wdata
->extension
.input
,
1704 wiimod_pro_map
[WIIMOD_PRO_KEY_HOME
],
1706 input_report_key(wdata
->extension
.input
,
1707 wiimod_pro_map
[WIIMOD_PRO_KEY_PLUS
],
1709 input_report_key(wdata
->extension
.input
,
1710 wiimod_pro_map
[WIIMOD_PRO_KEY_TR
],
1713 input_report_key(wdata
->extension
.input
,
1714 wiimod_pro_map
[WIIMOD_PRO_KEY_ZL
],
1716 input_report_key(wdata
->extension
.input
,
1717 wiimod_pro_map
[WIIMOD_PRO_KEY_B
],
1719 input_report_key(wdata
->extension
.input
,
1720 wiimod_pro_map
[WIIMOD_PRO_KEY_Y
],
1722 input_report_key(wdata
->extension
.input
,
1723 wiimod_pro_map
[WIIMOD_PRO_KEY_A
],
1725 input_report_key(wdata
->extension
.input
,
1726 wiimod_pro_map
[WIIMOD_PRO_KEY_X
],
1728 input_report_key(wdata
->extension
.input
,
1729 wiimod_pro_map
[WIIMOD_PRO_KEY_ZR
],
1731 input_report_key(wdata
->extension
.input
,
1732 wiimod_pro_map
[WIIMOD_PRO_KEY_LEFT
],
1734 input_report_key(wdata
->extension
.input
,
1735 wiimod_pro_map
[WIIMOD_PRO_KEY_UP
],
1738 input_report_key(wdata
->extension
.input
,
1739 wiimod_pro_map
[WIIMOD_PRO_KEY_THUMBL
],
1741 input_report_key(wdata
->extension
.input
,
1742 wiimod_pro_map
[WIIMOD_PRO_KEY_THUMBR
],
1745 input_sync(wdata
->extension
.input
);
1748 static int wiimod_pro_open(struct input_dev
*dev
)
1750 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1751 unsigned long flags
;
1753 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1754 wdata
->state
.flags
|= WIIPROTO_FLAG_EXT_USED
;
1755 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1756 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1761 static void wiimod_pro_close(struct input_dev
*dev
)
1763 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1764 unsigned long flags
;
1766 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1767 wdata
->state
.flags
&= ~WIIPROTO_FLAG_EXT_USED
;
1768 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
1769 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1772 static int wiimod_pro_play(struct input_dev
*dev
, void *data
,
1773 struct ff_effect
*eff
)
1775 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
1779 * The wiimote supports only a single rumble motor so if any magnitude
1780 * is set to non-zero then we start the rumble motor. If both are set to
1781 * zero, we stop the rumble motor.
1784 if (eff
->u
.rumble
.strong_magnitude
|| eff
->u
.rumble
.weak_magnitude
)
1789 /* Locking state.lock here might deadlock with input_event() calls.
1790 * schedule_work acts as barrier. Merging multiple changes is fine. */
1791 wdata
->state
.cache_rumble
= value
;
1792 schedule_work(&wdata
->rumble_worker
);
1797 static ssize_t
wiimod_pro_calib_show(struct device
*dev
,
1798 struct device_attribute
*attr
,
1801 struct wiimote_data
*wdata
= dev_to_wii(dev
);
1805 r
+= sprintf(&out
[r
], "%+06hd:", wdata
->state
.calib_pro_sticks
[0]);
1806 r
+= sprintf(&out
[r
], "%+06hd ", wdata
->state
.calib_pro_sticks
[1]);
1807 r
+= sprintf(&out
[r
], "%+06hd:", wdata
->state
.calib_pro_sticks
[2]);
1808 r
+= sprintf(&out
[r
], "%+06hd\n", wdata
->state
.calib_pro_sticks
[3]);
1813 static ssize_t
wiimod_pro_calib_store(struct device
*dev
,
1814 struct device_attribute
*attr
,
1815 const char *buf
, size_t count
)
1817 struct wiimote_data
*wdata
= dev_to_wii(dev
);
1821 if (!strncmp(buf
, "scan\n", 5)) {
1822 spin_lock_irq(&wdata
->state
.lock
);
1823 wdata
->state
.flags
&= ~WIIPROTO_FLAG_PRO_CALIB_DONE
;
1824 spin_unlock_irq(&wdata
->state
.lock
);
1826 r
= sscanf(buf
, "%hd:%hd %hd:%hd", &x1
, &y1
, &x2
, &y2
);
1830 spin_lock_irq(&wdata
->state
.lock
);
1831 wdata
->state
.flags
|= WIIPROTO_FLAG_PRO_CALIB_DONE
;
1832 spin_unlock_irq(&wdata
->state
.lock
);
1834 wdata
->state
.calib_pro_sticks
[0] = x1
;
1835 wdata
->state
.calib_pro_sticks
[1] = y1
;
1836 wdata
->state
.calib_pro_sticks
[2] = x2
;
1837 wdata
->state
.calib_pro_sticks
[3] = y2
;
1840 return strnlen(buf
, PAGE_SIZE
);
1843 static DEVICE_ATTR(pro_calib
, S_IRUGO
|S_IWUSR
|S_IWGRP
, wiimod_pro_calib_show
,
1844 wiimod_pro_calib_store
);
1846 static int wiimod_pro_probe(const struct wiimod_ops
*ops
,
1847 struct wiimote_data
*wdata
)
1850 unsigned long flags
;
1852 INIT_WORK(&wdata
->rumble_worker
, wiimod_rumble_worker
);
1853 wdata
->state
.calib_pro_sticks
[0] = 0;
1854 wdata
->state
.calib_pro_sticks
[1] = 0;
1855 wdata
->state
.calib_pro_sticks
[2] = 0;
1856 wdata
->state
.calib_pro_sticks
[3] = 0;
1858 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1859 wdata
->state
.flags
&= ~WIIPROTO_FLAG_PRO_CALIB_DONE
;
1860 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1862 wdata
->extension
.input
= input_allocate_device();
1863 if (!wdata
->extension
.input
)
1866 set_bit(FF_RUMBLE
, wdata
->extension
.input
->ffbit
);
1867 input_set_drvdata(wdata
->extension
.input
, wdata
);
1869 if (input_ff_create_memless(wdata
->extension
.input
, NULL
,
1875 ret
= device_create_file(&wdata
->hdev
->dev
,
1876 &dev_attr_pro_calib
);
1878 hid_err(wdata
->hdev
, "cannot create sysfs attribute\n");
1882 wdata
->extension
.input
->open
= wiimod_pro_open
;
1883 wdata
->extension
.input
->close
= wiimod_pro_close
;
1884 wdata
->extension
.input
->dev
.parent
= &wdata
->hdev
->dev
;
1885 wdata
->extension
.input
->id
.bustype
= wdata
->hdev
->bus
;
1886 wdata
->extension
.input
->id
.vendor
= wdata
->hdev
->vendor
;
1887 wdata
->extension
.input
->id
.product
= wdata
->hdev
->product
;
1888 wdata
->extension
.input
->id
.version
= wdata
->hdev
->version
;
1889 wdata
->extension
.input
->name
= WIIMOTE_NAME
" Pro Controller";
1891 set_bit(EV_KEY
, wdata
->extension
.input
->evbit
);
1892 for (i
= 0; i
< WIIMOD_PRO_KEY_NUM
; ++i
)
1893 set_bit(wiimod_pro_map
[i
],
1894 wdata
->extension
.input
->keybit
);
1896 set_bit(EV_ABS
, wdata
->extension
.input
->evbit
);
1897 set_bit(ABS_X
, wdata
->extension
.input
->absbit
);
1898 set_bit(ABS_Y
, wdata
->extension
.input
->absbit
);
1899 set_bit(ABS_RX
, wdata
->extension
.input
->absbit
);
1900 set_bit(ABS_RY
, wdata
->extension
.input
->absbit
);
1901 input_set_abs_params(wdata
->extension
.input
,
1902 ABS_X
, -0x400, 0x400, 4, 100);
1903 input_set_abs_params(wdata
->extension
.input
,
1904 ABS_Y
, -0x400, 0x400, 4, 100);
1905 input_set_abs_params(wdata
->extension
.input
,
1906 ABS_RX
, -0x400, 0x400, 4, 100);
1907 input_set_abs_params(wdata
->extension
.input
,
1908 ABS_RY
, -0x400, 0x400, 4, 100);
1910 ret
= input_register_device(wdata
->extension
.input
);
1917 device_remove_file(&wdata
->hdev
->dev
,
1918 &dev_attr_pro_calib
);
1920 input_free_device(wdata
->extension
.input
);
1921 wdata
->extension
.input
= NULL
;
1925 static void wiimod_pro_remove(const struct wiimod_ops
*ops
,
1926 struct wiimote_data
*wdata
)
1928 unsigned long flags
;
1930 if (!wdata
->extension
.input
)
1933 input_unregister_device(wdata
->extension
.input
);
1934 wdata
->extension
.input
= NULL
;
1935 cancel_work_sync(&wdata
->rumble_worker
);
1936 device_remove_file(&wdata
->hdev
->dev
,
1937 &dev_attr_pro_calib
);
1939 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1940 wiiproto_req_rumble(wdata
, 0);
1941 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1944 static const struct wiimod_ops wiimod_pro
= {
1945 .flags
= WIIMOD_FLAG_EXT16
,
1947 .probe
= wiimod_pro_probe
,
1948 .remove
= wiimod_pro_remove
,
1949 .in_ext
= wiimod_pro_in_ext
,
1953 * Builtin Motion Plus
1954 * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
1955 * disables polling for Motion-Plus. This should be set only for devices which
1956 * don't allow MP hotplugging.
1959 static int wiimod_builtin_mp_probe(const struct wiimod_ops
*ops
,
1960 struct wiimote_data
*wdata
)
1962 unsigned long flags
;
1964 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1965 wdata
->state
.flags
|= WIIPROTO_FLAG_BUILTIN_MP
;
1966 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1971 static void wiimod_builtin_mp_remove(const struct wiimod_ops
*ops
,
1972 struct wiimote_data
*wdata
)
1974 unsigned long flags
;
1976 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
1977 wdata
->state
.flags
|= WIIPROTO_FLAG_BUILTIN_MP
;
1978 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
1981 static const struct wiimod_ops wiimod_builtin_mp
= {
1984 .probe
= wiimod_builtin_mp_probe
,
1985 .remove
= wiimod_builtin_mp_remove
,
1990 * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
1991 * disables motion-plus. This is needed for devices that advertise this but we
1992 * don't know how to use it (or whether it is actually present).
1995 static int wiimod_no_mp_probe(const struct wiimod_ops
*ops
,
1996 struct wiimote_data
*wdata
)
1998 unsigned long flags
;
2000 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
2001 wdata
->state
.flags
|= WIIPROTO_FLAG_NO_MP
;
2002 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
2007 static void wiimod_no_mp_remove(const struct wiimod_ops
*ops
,
2008 struct wiimote_data
*wdata
)
2010 unsigned long flags
;
2012 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
2013 wdata
->state
.flags
|= WIIPROTO_FLAG_NO_MP
;
2014 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
2017 static const struct wiimod_ops wiimod_no_mp
= {
2020 .probe
= wiimod_no_mp_probe
,
2021 .remove
= wiimod_no_mp_remove
,
2026 * The Motion Plus extension provides rotation sensors (gyro) as a small
2027 * extension device for Wii Remotes. Many devices have them built-in so
2028 * you cannot see them from the outside.
2029 * Motion Plus extensions are special because they are on a separate extension
2030 * port and allow other extensions to be used simultaneously. This is all
2031 * handled by the Wiimote Core so we don't have to deal with it.
2034 static void wiimod_mp_in_mp(struct wiimote_data
*wdata
, const __u8
*ext
)
2038 /* | 8 7 6 5 4 3 | 2 | 1 |
2039 * -----+------------------------------+-----+-----+
2040 * 1 | Yaw Speed <7:0> |
2041 * 2 | Roll Speed <7:0> |
2042 * 3 | Pitch Speed <7:0> |
2043 * -----+------------------------------+-----+-----+
2044 * 4 | Yaw Speed <13:8> | Yaw |Pitch|
2045 * -----+------------------------------+-----+-----+
2046 * 5 | Roll Speed <13:8> |Roll | Ext |
2047 * -----+------------------------------+-----+-----+
2048 * 6 | Pitch Speed <13:8> | 1 | 0 |
2049 * -----+------------------------------+-----+-----+
2050 * The single bits Yaw, Roll, Pitch in the lower right corner specify
2051 * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
2052 * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
2053 * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
2055 * If the wiimote is not rotating the sensor reports 2^13 = 8192.
2056 * Ext specifies whether an extension is connected to the motionp.
2057 * which is parsed by wiimote-core.
2064 x
|= (((__u16
)ext
[3]) << 6) & 0xff00;
2065 y
|= (((__u16
)ext
[4]) << 6) & 0xff00;
2066 z
|= (((__u16
)ext
[5]) << 6) & 0xff00;
2072 if (!(ext
[3] & 0x02))
2076 if (!(ext
[4] & 0x02))
2080 if (!(ext
[3] & 0x01))
2085 input_report_abs(wdata
->mp
, ABS_RX
, x
);
2086 input_report_abs(wdata
->mp
, ABS_RY
, y
);
2087 input_report_abs(wdata
->mp
, ABS_RZ
, z
);
2088 input_sync(wdata
->mp
);
2091 static int wiimod_mp_open(struct input_dev
*dev
)
2093 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
2094 unsigned long flags
;
2096 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
2097 wdata
->state
.flags
|= WIIPROTO_FLAG_MP_USED
;
2098 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
2099 __wiimote_schedule(wdata
);
2100 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
2105 static void wiimod_mp_close(struct input_dev
*dev
)
2107 struct wiimote_data
*wdata
= input_get_drvdata(dev
);
2108 unsigned long flags
;
2110 spin_lock_irqsave(&wdata
->state
.lock
, flags
);
2111 wdata
->state
.flags
&= ~WIIPROTO_FLAG_MP_USED
;
2112 wiiproto_req_drm(wdata
, WIIPROTO_REQ_NULL
);
2113 __wiimote_schedule(wdata
);
2114 spin_unlock_irqrestore(&wdata
->state
.lock
, flags
);
2117 static int wiimod_mp_probe(const struct wiimod_ops
*ops
,
2118 struct wiimote_data
*wdata
)
2122 wdata
->mp
= input_allocate_device();
2126 input_set_drvdata(wdata
->mp
, wdata
);
2127 wdata
->mp
->open
= wiimod_mp_open
;
2128 wdata
->mp
->close
= wiimod_mp_close
;
2129 wdata
->mp
->dev
.parent
= &wdata
->hdev
->dev
;
2130 wdata
->mp
->id
.bustype
= wdata
->hdev
->bus
;
2131 wdata
->mp
->id
.vendor
= wdata
->hdev
->vendor
;
2132 wdata
->mp
->id
.product
= wdata
->hdev
->product
;
2133 wdata
->mp
->id
.version
= wdata
->hdev
->version
;
2134 wdata
->mp
->name
= WIIMOTE_NAME
" Motion Plus";
2136 set_bit(EV_ABS
, wdata
->mp
->evbit
);
2137 set_bit(ABS_RX
, wdata
->mp
->absbit
);
2138 set_bit(ABS_RY
, wdata
->mp
->absbit
);
2139 set_bit(ABS_RZ
, wdata
->mp
->absbit
);
2140 input_set_abs_params(wdata
->mp
,
2141 ABS_RX
, -16000, 16000, 4, 8);
2142 input_set_abs_params(wdata
->mp
,
2143 ABS_RY
, -16000, 16000, 4, 8);
2144 input_set_abs_params(wdata
->mp
,
2145 ABS_RZ
, -16000, 16000, 4, 8);
2147 ret
= input_register_device(wdata
->mp
);
2154 input_free_device(wdata
->mp
);
2159 static void wiimod_mp_remove(const struct wiimod_ops
*ops
,
2160 struct wiimote_data
*wdata
)
2165 input_unregister_device(wdata
->mp
);
2169 const struct wiimod_ops wiimod_mp
= {
2172 .probe
= wiimod_mp_probe
,
2173 .remove
= wiimod_mp_remove
,
2174 .in_mp
= wiimod_mp_in_mp
,
2179 static const struct wiimod_ops wiimod_dummy
;
2181 const struct wiimod_ops
*wiimod_table
[WIIMOD_NUM
] = {
2182 [WIIMOD_KEYS
] = &wiimod_keys
,
2183 [WIIMOD_RUMBLE
] = &wiimod_rumble
,
2184 [WIIMOD_BATTERY
] = &wiimod_battery
,
2185 [WIIMOD_LED1
] = &wiimod_leds
[0],
2186 [WIIMOD_LED2
] = &wiimod_leds
[1],
2187 [WIIMOD_LED3
] = &wiimod_leds
[2],
2188 [WIIMOD_LED4
] = &wiimod_leds
[3],
2189 [WIIMOD_ACCEL
] = &wiimod_accel
,
2190 [WIIMOD_IR
] = &wiimod_ir
,
2191 [WIIMOD_BUILTIN_MP
] = &wiimod_builtin_mp
,
2192 [WIIMOD_NO_MP
] = &wiimod_no_mp
,
2195 const struct wiimod_ops
*wiimod_ext_table
[WIIMOTE_EXT_NUM
] = {
2196 [WIIMOTE_EXT_NONE
] = &wiimod_dummy
,
2197 [WIIMOTE_EXT_UNKNOWN
] = &wiimod_dummy
,
2198 [WIIMOTE_EXT_NUNCHUK
] = &wiimod_nunchuk
,
2199 [WIIMOTE_EXT_CLASSIC_CONTROLLER
] = &wiimod_classic
,
2200 [WIIMOTE_EXT_BALANCE_BOARD
] = &wiimod_bboard
,
2201 [WIIMOTE_EXT_PRO_CONTROLLER
] = &wiimod_pro
,