blob: fbc09c8810b8628064de09cf4ded0cab13b0d425 [file] [log] [blame]
David Herrmann27f06942013-05-05 23:12:51 +02001/*
2 * Device Modules for Nintendo Wii / Wii U HID Driver
3 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
4 */
5
6/*
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 */
12
13/*
14 * Wiimote Modules
15 * Nintendo devices provide different peripherals and many new devices lack
16 * initial features like the IR camera. Therefore, each peripheral device is
17 * implemented as an independent module and we probe on each device only the
18 * modules for the hardware that really is available.
19 *
20 * Module registration is sequential. Unregistration is done in reverse order.
21 * After device detection, the needed modules are loaded. Users can trigger
22 * re-detection which causes all modules to be unloaded and then reload the
23 * modules for the new detected device.
24 *
25 * wdata->input is a shared input device. It is always initialized prior to
26 * module registration. If at least one registered module is marked as
27 * WIIMOD_FLAG_INPUT, then the input device will get registered after all
28 * modules were registered.
29 * Please note that it is unregistered _before_ the "remove" callbacks are
30 * called. This guarantees that no input interaction is done, anymore. However,
31 * the wiimote core keeps a reference to the input device so it is freed only
32 * after all modules were removed. It is safe to send events to unregistered
33 * input devices.
34 */
35
36#include <linux/device.h>
37#include <linux/hid.h>
38#include <linux/input.h>
39#include <linux/spinlock.h>
40#include "hid-wiimote.h"
41
David Herrmann20cef812013-05-05 23:12:52 +020042/*
43 * Keys
44 * The initial Wii Remote provided a bunch of buttons that are reported as
45 * part of the core protocol. Many later devices dropped these and report
46 * invalid data in the core button reports. Load this only on devices which
47 * correctly send button reports.
48 * It uses the shared input device.
49 */
50
51static const __u16 wiimod_keys_map[] = {
52 KEY_LEFT, /* WIIPROTO_KEY_LEFT */
53 KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
54 KEY_UP, /* WIIPROTO_KEY_UP */
55 KEY_DOWN, /* WIIPROTO_KEY_DOWN */
56 KEY_NEXT, /* WIIPROTO_KEY_PLUS */
57 KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
58 BTN_1, /* WIIPROTO_KEY_ONE */
59 BTN_2, /* WIIPROTO_KEY_TWO */
60 BTN_A, /* WIIPROTO_KEY_A */
61 BTN_B, /* WIIPROTO_KEY_B */
62 BTN_MODE, /* WIIPROTO_KEY_HOME */
63};
64
65static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
66{
67 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
68 !!(keys[0] & 0x01));
69 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
70 !!(keys[0] & 0x02));
71 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
72 !!(keys[0] & 0x04));
73 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
74 !!(keys[0] & 0x08));
75 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
76 !!(keys[0] & 0x10));
77 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
78 !!(keys[1] & 0x01));
79 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
80 !!(keys[1] & 0x02));
81 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
82 !!(keys[1] & 0x04));
83 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
84 !!(keys[1] & 0x08));
85 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
86 !!(keys[1] & 0x10));
87 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
88 !!(keys[1] & 0x80));
89 input_sync(wdata->input);
90}
91
92static int wiimod_keys_probe(const struct wiimod_ops *ops,
93 struct wiimote_data *wdata)
94{
95 unsigned int i;
96
97 set_bit(EV_KEY, wdata->input->evbit);
98 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
99 set_bit(wiimod_keys_map[i], wdata->input->keybit);
100
101 return 0;
102}
103
104static const struct wiimod_ops wiimod_keys = {
105 .flags = WIIMOD_FLAG_INPUT,
106 .arg = 0,
107 .probe = wiimod_keys_probe,
108 .remove = NULL,
109 .in_keys = wiimod_keys_in_keys,
110};
111
112/*
113 * Rumble
114 * Nearly all devices provide a rumble feature. A small motor for
115 * force-feedback effects. We provide an FF_RUMBLE memless ff device on the
116 * shared input device if this module is loaded.
117 * The rumble motor is controlled via a flag on almost every output report so
118 * the wiimote core handles the rumble flag. But if a device doesn't provide
119 * the rumble motor, this flag shouldn't be set.
120 */
121
122static int wiimod_rumble_play(struct input_dev *dev, void *data,
123 struct ff_effect *eff)
124{
125 struct wiimote_data *wdata = input_get_drvdata(dev);
126 __u8 value;
127 unsigned long flags;
128
129 /*
130 * The wiimote supports only a single rumble motor so if any magnitude
131 * is set to non-zero then we start the rumble motor. If both are set to
132 * zero, we stop the rumble motor.
133 */
134
135 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
136 value = 1;
137 else
138 value = 0;
139
140 spin_lock_irqsave(&wdata->state.lock, flags);
141 wiiproto_req_rumble(wdata, value);
142 spin_unlock_irqrestore(&wdata->state.lock, flags);
143
144 return 0;
145}
146
147static int wiimod_rumble_probe(const struct wiimod_ops *ops,
148 struct wiimote_data *wdata)
149{
150 set_bit(FF_RUMBLE, wdata->input->ffbit);
151 if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
152 return -ENOMEM;
153
154 return 0;
155}
156
157static void wiimod_rumble_remove(const struct wiimod_ops *ops,
158 struct wiimote_data *wdata)
159{
160 unsigned long flags;
161
162 spin_lock_irqsave(&wdata->state.lock, flags);
163 wiiproto_req_rumble(wdata, 0);
164 spin_unlock_irqrestore(&wdata->state.lock, flags);
165}
166
167static const struct wiimod_ops wiimod_rumble = {
168 .flags = WIIMOD_FLAG_INPUT,
169 .arg = 0,
170 .probe = wiimod_rumble_probe,
171 .remove = wiimod_rumble_remove,
172};
173
David Herrmanndcf39232013-05-05 23:12:53 +0200174/*
175 * Battery
176 * 1 byte of battery capacity information is sent along every protocol status
177 * report. The wiimote core caches it but we try to update it on every
178 * user-space request.
179 * This is supported by nearly every device so it's almost always enabled.
180 */
181
182static enum power_supply_property wiimod_battery_props[] = {
183 POWER_SUPPLY_PROP_CAPACITY,
184 POWER_SUPPLY_PROP_SCOPE,
185};
186
187static int wiimod_battery_get_property(struct power_supply *psy,
188 enum power_supply_property psp,
189 union power_supply_propval *val)
190{
191 struct wiimote_data *wdata = container_of(psy, struct wiimote_data,
192 battery);
193 int ret = 0, state;
194 unsigned long flags;
195
196 if (psp == POWER_SUPPLY_PROP_SCOPE) {
197 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
198 return 0;
199 } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
200 return -EINVAL;
201 }
202
203 ret = wiimote_cmd_acquire(wdata);
204 if (ret)
205 return ret;
206
207 spin_lock_irqsave(&wdata->state.lock, flags);
208 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
209 wiiproto_req_status(wdata);
210 spin_unlock_irqrestore(&wdata->state.lock, flags);
211
212 wiimote_cmd_wait(wdata);
213 wiimote_cmd_release(wdata);
214
215 spin_lock_irqsave(&wdata->state.lock, flags);
216 state = wdata->state.cmd_battery;
217 spin_unlock_irqrestore(&wdata->state.lock, flags);
218
219 val->intval = state * 100 / 255;
220 return ret;
221}
222
223static int wiimod_battery_probe(const struct wiimod_ops *ops,
224 struct wiimote_data *wdata)
225{
226 int ret;
227
228 wdata->battery.properties = wiimod_battery_props;
229 wdata->battery.num_properties = ARRAY_SIZE(wiimod_battery_props);
230 wdata->battery.get_property = wiimod_battery_get_property;
231 wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
232 wdata->battery.use_for_apm = 0;
233 wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
234 wdata->hdev->uniq);
235 if (!wdata->battery.name)
236 return -ENOMEM;
237
238 ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
239 if (ret) {
240 hid_err(wdata->hdev, "cannot register battery device\n");
241 goto err_free;
242 }
243
244 power_supply_powers(&wdata->battery, &wdata->hdev->dev);
245 return 0;
246
247err_free:
248 kfree(wdata->battery.name);
249 wdata->battery.name = NULL;
250 return ret;
251}
252
253static void wiimod_battery_remove(const struct wiimod_ops *ops,
254 struct wiimote_data *wdata)
255{
256 if (!wdata->battery.name)
257 return;
258
259 power_supply_unregister(&wdata->battery);
260 kfree(wdata->battery.name);
261 wdata->battery.name = NULL;
262}
263
264static const struct wiimod_ops wiimod_battery = {
265 .flags = 0,
266 .arg = 0,
267 .probe = wiimod_battery_probe,
268 .remove = wiimod_battery_remove,
269};
270
David Herrmann6c5ae012013-05-05 23:12:54 +0200271/*
272 * LED
273 * 0 to 4 player LEDs are supported by devices. The "arg" field of the
274 * wiimod_ops structure specifies which LED this module controls. This allows
275 * to register a limited number of LEDs.
276 * State is managed by wiimote core.
277 */
278
279static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
280{
281 struct wiimote_data *wdata;
282 struct device *dev = led_dev->dev->parent;
283 int i;
284 unsigned long flags;
285 bool value = false;
286
287 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
288
289 for (i = 0; i < 4; ++i) {
290 if (wdata->leds[i] == led_dev) {
291 spin_lock_irqsave(&wdata->state.lock, flags);
292 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
293 spin_unlock_irqrestore(&wdata->state.lock, flags);
294 break;
295 }
296 }
297
298 return value ? LED_FULL : LED_OFF;
299}
300
301static void wiimod_led_set(struct led_classdev *led_dev,
302 enum led_brightness value)
303{
304 struct wiimote_data *wdata;
305 struct device *dev = led_dev->dev->parent;
306 int i;
307 unsigned long flags;
308 __u8 state, flag;
309
310 wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
311
312 for (i = 0; i < 4; ++i) {
313 if (wdata->leds[i] == led_dev) {
314 flag = WIIPROTO_FLAG_LED(i + 1);
315 spin_lock_irqsave(&wdata->state.lock, flags);
316 state = wdata->state.flags;
317 if (value == LED_OFF)
318 wiiproto_req_leds(wdata, state & ~flag);
319 else
320 wiiproto_req_leds(wdata, state | flag);
321 spin_unlock_irqrestore(&wdata->state.lock, flags);
322 break;
323 }
324 }
325}
326
327static int wiimod_led_probe(const struct wiimod_ops *ops,
328 struct wiimote_data *wdata)
329{
330 struct device *dev = &wdata->hdev->dev;
331 size_t namesz = strlen(dev_name(dev)) + 9;
332 struct led_classdev *led;
333 unsigned long flags;
334 char *name;
335 int ret;
336
337 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
338 if (!led)
339 return -ENOMEM;
340
341 name = (void*)&led[1];
342 snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
343 led->name = name;
344 led->brightness = 0;
345 led->max_brightness = 1;
346 led->brightness_get = wiimod_led_get;
347 led->brightness_set = wiimod_led_set;
348
349 wdata->leds[ops->arg] = led;
350 ret = led_classdev_register(dev, led);
351 if (ret)
352 goto err_free;
353
354 /* enable LED1 to stop initial LED-blinking */
355 if (ops->arg == 0) {
356 spin_lock_irqsave(&wdata->state.lock, flags);
357 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
358 spin_unlock_irqrestore(&wdata->state.lock, flags);
359 }
360
361 return 0;
362
363err_free:
364 wdata->leds[ops->arg] = NULL;
365 kfree(led);
366 return ret;
367}
368
369static void wiimod_led_remove(const struct wiimod_ops *ops,
370 struct wiimote_data *wdata)
371{
372 if (!wdata->leds[ops->arg])
373 return;
374
375 led_classdev_unregister(wdata->leds[ops->arg]);
376 kfree(wdata->leds[ops->arg]);
377 wdata->leds[ops->arg] = NULL;
378}
379
380static const struct wiimod_ops wiimod_leds[4] = {
381 {
382 .flags = 0,
383 .arg = 0,
384 .probe = wiimod_led_probe,
385 .remove = wiimod_led_remove,
386 },
387 {
388 .flags = 0,
389 .arg = 1,
390 .probe = wiimod_led_probe,
391 .remove = wiimod_led_remove,
392 },
393 {
394 .flags = 0,
395 .arg = 2,
396 .probe = wiimod_led_probe,
397 .remove = wiimod_led_remove,
398 },
399 {
400 .flags = 0,
401 .arg = 3,
402 .probe = wiimod_led_probe,
403 .remove = wiimod_led_remove,
404 },
405};
406
David Herrmann0ea16752013-05-05 23:12:55 +0200407/*
408 * Accelerometer
409 * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
410 * device, it's mostly cleared to 0. This module parses this data and provides
411 * it via a separate input device.
412 */
413
414static void wiimod_accel_in_accel(struct wiimote_data *wdata,
415 const __u8 *accel)
416{
417 __u16 x, y, z;
418
419 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
420 return;
421
422 /*
423 * payload is: BB BB XX YY ZZ
424 * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
425 * contain the upper 8 bits of each value. The lower 2 bits are
426 * contained in the buttons data BB BB.
427 * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
428 * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
429 * accel value and bit 6 is the second bit of the Z value.
430 * The first bit of Y and Z values is not available and always set to 0.
431 * 0x200 is returned on no movement.
432 */
433
434 x = accel[2] << 2;
435 y = accel[3] << 2;
436 z = accel[4] << 2;
437
438 x |= (accel[0] >> 5) & 0x3;
439 y |= (accel[1] >> 4) & 0x2;
440 z |= (accel[1] >> 5) & 0x2;
441
442 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
443 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
444 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
445 input_sync(wdata->accel);
446}
447
448static int wiimod_accel_open(struct input_dev *dev)
449{
450 struct wiimote_data *wdata = input_get_drvdata(dev);
451 unsigned long flags;
452
453 spin_lock_irqsave(&wdata->state.lock, flags);
454 wiiproto_req_accel(wdata, true);
455 spin_unlock_irqrestore(&wdata->state.lock, flags);
456
457 return 0;
458}
459
460static void wiimod_accel_close(struct input_dev *dev)
461{
462 struct wiimote_data *wdata = input_get_drvdata(dev);
463 unsigned long flags;
464
465 spin_lock_irqsave(&wdata->state.lock, flags);
466 wiiproto_req_accel(wdata, false);
467 spin_unlock_irqrestore(&wdata->state.lock, flags);
468}
469
470static int wiimod_accel_probe(const struct wiimod_ops *ops,
471 struct wiimote_data *wdata)
472{
473 int ret;
474
475 wdata->accel = input_allocate_device();
476 if (!wdata->accel)
477 return -ENOMEM;
478
479 input_set_drvdata(wdata->accel, wdata);
480 wdata->accel->open = wiimod_accel_open;
481 wdata->accel->close = wiimod_accel_close;
482 wdata->accel->dev.parent = &wdata->hdev->dev;
483 wdata->accel->id.bustype = wdata->hdev->bus;
484 wdata->accel->id.vendor = wdata->hdev->vendor;
485 wdata->accel->id.product = wdata->hdev->product;
486 wdata->accel->id.version = wdata->hdev->version;
487 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
488
489 set_bit(EV_ABS, wdata->accel->evbit);
490 set_bit(ABS_RX, wdata->accel->absbit);
491 set_bit(ABS_RY, wdata->accel->absbit);
492 set_bit(ABS_RZ, wdata->accel->absbit);
493 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
494 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
495 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
496
497 ret = input_register_device(wdata->accel);
498 if (ret) {
499 hid_err(wdata->hdev, "cannot register input device\n");
500 goto err_free;
501 }
502
503 return 0;
504
505err_free:
506 input_free_device(wdata->accel);
507 wdata->accel = NULL;
508 return ret;
509}
510
511static void wiimod_accel_remove(const struct wiimod_ops *ops,
512 struct wiimote_data *wdata)
513{
514 if (!wdata->accel)
515 return;
516
517 input_unregister_device(wdata->accel);
518 wdata->accel = NULL;
519}
520
521static const struct wiimod_ops wiimod_accel = {
522 .flags = 0,
523 .arg = 0,
524 .probe = wiimod_accel_probe,
525 .remove = wiimod_accel_remove,
526 .in_accel = wiimod_accel_in_accel,
527};
528
David Herrmann27f06942013-05-05 23:12:51 +0200529/* module table */
530
531const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
David Herrmann20cef812013-05-05 23:12:52 +0200532 [WIIMOD_KEYS] = &wiimod_keys,
533 [WIIMOD_RUMBLE] = &wiimod_rumble,
David Herrmanndcf39232013-05-05 23:12:53 +0200534 [WIIMOD_BATTERY] = &wiimod_battery,
David Herrmann6c5ae012013-05-05 23:12:54 +0200535 [WIIMOD_LED1] = &wiimod_leds[0],
536 [WIIMOD_LED2] = &wiimod_leds[1],
537 [WIIMOD_LED3] = &wiimod_leds[2],
538 [WIIMOD_LED4] = &wiimod_leds[3],
David Herrmann0ea16752013-05-05 23:12:55 +0200539 [WIIMOD_ACCEL] = &wiimod_accel,
David Herrmann27f06942013-05-05 23:12:51 +0200540};