David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 1 | /* |
| 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 Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 42 | /* |
| 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 | |
| 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 */ |
| 63 | }; |
| 64 | |
| 65 | static 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 | |
| 92 | static 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 | |
| 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, |
| 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 | |
| 122 | static 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 | |
| 147 | static 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 | |
| 157 | static 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 | |
| 167 | static 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 Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 174 | /* |
| 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 | |
| 182 | static enum power_supply_property wiimod_battery_props[] = { |
| 183 | POWER_SUPPLY_PROP_CAPACITY, |
| 184 | POWER_SUPPLY_PROP_SCOPE, |
| 185 | }; |
| 186 | |
| 187 | static 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 | |
| 223 | static 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 | |
| 247 | err_free: |
| 248 | kfree(wdata->battery.name); |
| 249 | wdata->battery.name = NULL; |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static 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 | |
| 264 | static 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 Herrmann | 6c5ae01 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 271 | /* |
| 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 | |
| 279 | static 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 | |
| 301 | static 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 | |
| 327 | static 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 | |
| 363 | err_free: |
| 364 | wdata->leds[ops->arg] = NULL; |
| 365 | kfree(led); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static 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 | |
| 380 | static 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 Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 407 | /* |
| 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 | |
| 414 | static 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 | |
| 448 | static 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 | |
| 460 | static 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 | |
| 470 | static 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 | |
| 505 | err_free: |
| 506 | input_free_device(wdata->accel); |
| 507 | wdata->accel = NULL; |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | static 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 | |
| 521 | static 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 Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 529 | /* |
| 530 | * IR Cam |
| 531 | * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs |
| 532 | * to be initialized with a fairly complex procedure and consumes a lot of |
| 533 | * power. Therefore, as long as no application uses the IR input device, it is |
| 534 | * kept offline. |
| 535 | * Nearly no other device than the normal Wii Remotes supports the IR cam so |
| 536 | * you can disable this module for these devices. |
| 537 | */ |
| 538 | |
| 539 | static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir, |
| 540 | bool packed, unsigned int id) |
| 541 | { |
| 542 | __u16 x, y; |
| 543 | __u8 xid, yid; |
| 544 | bool sync = false; |
| 545 | |
| 546 | if (!(wdata->state.flags & WIIPROTO_FLAGS_IR)) |
| 547 | return; |
| 548 | |
| 549 | switch (id) { |
| 550 | case 0: |
| 551 | xid = ABS_HAT0X; |
| 552 | yid = ABS_HAT0Y; |
| 553 | break; |
| 554 | case 1: |
| 555 | xid = ABS_HAT1X; |
| 556 | yid = ABS_HAT1Y; |
| 557 | break; |
| 558 | case 2: |
| 559 | xid = ABS_HAT2X; |
| 560 | yid = ABS_HAT2Y; |
| 561 | break; |
| 562 | case 3: |
| 563 | xid = ABS_HAT3X; |
| 564 | yid = ABS_HAT3Y; |
| 565 | sync = true; |
| 566 | break; |
| 567 | default: |
| 568 | return; |
| 569 | }; |
| 570 | |
| 571 | /* |
| 572 | * Basic IR data is encoded into 3 bytes. The first two bytes are the |
| 573 | * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits |
| 574 | * of both. |
| 575 | * If data is packed, then the 3rd byte is put first and slightly |
| 576 | * reordered. This allows to interleave packed and non-packed data to |
| 577 | * have two IR sets in 5 bytes instead of 6. |
| 578 | * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev. |
| 579 | */ |
| 580 | |
| 581 | if (packed) { |
| 582 | x = ir[1] | ((ir[0] & 0x03) << 8); |
| 583 | y = ir[2] | ((ir[0] & 0x0c) << 6); |
| 584 | } else { |
| 585 | x = ir[0] | ((ir[2] & 0x30) << 4); |
| 586 | y = ir[1] | ((ir[2] & 0xc0) << 2); |
| 587 | } |
| 588 | |
| 589 | input_report_abs(wdata->ir, xid, x); |
| 590 | input_report_abs(wdata->ir, yid, y); |
| 591 | |
| 592 | if (sync) |
| 593 | input_sync(wdata->ir); |
| 594 | } |
| 595 | |
| 596 | static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode) |
| 597 | { |
| 598 | int ret; |
| 599 | unsigned long flags; |
| 600 | __u8 format = 0; |
| 601 | static const __u8 data_enable[] = { 0x01 }; |
| 602 | static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01, |
| 603 | 0x00, 0xaa, 0x00, 0x64 }; |
| 604 | static const __u8 data_sens2[] = { 0x63, 0x03 }; |
| 605 | static const __u8 data_fin[] = { 0x08 }; |
| 606 | |
| 607 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 608 | |
| 609 | if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) { |
| 610 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | if (mode == 0) { |
| 615 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; |
| 616 | wiiproto_req_ir1(wdata, 0); |
| 617 | wiiproto_req_ir2(wdata, 0); |
| 618 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 619 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 624 | |
| 625 | ret = wiimote_cmd_acquire(wdata); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | |
| 629 | /* send PIXEL CLOCK ENABLE cmd first */ |
| 630 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 631 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0); |
| 632 | wiiproto_req_ir1(wdata, 0x06); |
| 633 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 634 | |
| 635 | ret = wiimote_cmd_wait(wdata); |
| 636 | if (ret) |
| 637 | goto unlock; |
| 638 | if (wdata->state.cmd_err) { |
| 639 | ret = -EIO; |
| 640 | goto unlock; |
| 641 | } |
| 642 | |
| 643 | /* enable IR LOGIC */ |
| 644 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 645 | wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0); |
| 646 | wiiproto_req_ir2(wdata, 0x06); |
| 647 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 648 | |
| 649 | ret = wiimote_cmd_wait(wdata); |
| 650 | if (ret) |
| 651 | goto unlock; |
| 652 | if (wdata->state.cmd_err) { |
| 653 | ret = -EIO; |
| 654 | goto unlock; |
| 655 | } |
| 656 | |
| 657 | /* enable IR cam but do not make it send data, yet */ |
| 658 | ret = wiimote_cmd_write(wdata, 0xb00030, data_enable, |
| 659 | sizeof(data_enable)); |
| 660 | if (ret) |
| 661 | goto unlock; |
| 662 | |
| 663 | /* write first sensitivity block */ |
| 664 | ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1, |
| 665 | sizeof(data_sens1)); |
| 666 | if (ret) |
| 667 | goto unlock; |
| 668 | |
| 669 | /* write second sensitivity block */ |
| 670 | ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2, |
| 671 | sizeof(data_sens2)); |
| 672 | if (ret) |
| 673 | goto unlock; |
| 674 | |
| 675 | /* put IR cam into desired state */ |
| 676 | switch (mode) { |
| 677 | case WIIPROTO_FLAG_IR_FULL: |
| 678 | format = 5; |
| 679 | break; |
| 680 | case WIIPROTO_FLAG_IR_EXT: |
| 681 | format = 3; |
| 682 | break; |
| 683 | case WIIPROTO_FLAG_IR_BASIC: |
| 684 | format = 1; |
| 685 | break; |
| 686 | } |
| 687 | ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format)); |
| 688 | if (ret) |
| 689 | goto unlock; |
| 690 | |
| 691 | /* make IR cam send data */ |
| 692 | ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin)); |
| 693 | if (ret) |
| 694 | goto unlock; |
| 695 | |
| 696 | /* request new DRM mode compatible to IR mode */ |
| 697 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 698 | wdata->state.flags &= ~WIIPROTO_FLAGS_IR; |
| 699 | wdata->state.flags |= mode & WIIPROTO_FLAGS_IR; |
| 700 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 701 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 702 | |
| 703 | unlock: |
| 704 | wiimote_cmd_release(wdata); |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | static int wiimod_ir_open(struct input_dev *dev) |
| 709 | { |
| 710 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 711 | |
| 712 | return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC); |
| 713 | } |
| 714 | |
| 715 | static void wiimod_ir_close(struct input_dev *dev) |
| 716 | { |
| 717 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 718 | |
| 719 | wiimod_ir_change(wdata, 0); |
| 720 | } |
| 721 | |
| 722 | static int wiimod_ir_probe(const struct wiimod_ops *ops, |
| 723 | struct wiimote_data *wdata) |
| 724 | { |
| 725 | int ret; |
| 726 | |
| 727 | wdata->ir = input_allocate_device(); |
| 728 | if (!wdata->ir) |
| 729 | return -ENOMEM; |
| 730 | |
| 731 | input_set_drvdata(wdata->ir, wdata); |
| 732 | wdata->ir->open = wiimod_ir_open; |
| 733 | wdata->ir->close = wiimod_ir_close; |
| 734 | wdata->ir->dev.parent = &wdata->hdev->dev; |
| 735 | wdata->ir->id.bustype = wdata->hdev->bus; |
| 736 | wdata->ir->id.vendor = wdata->hdev->vendor; |
| 737 | wdata->ir->id.product = wdata->hdev->product; |
| 738 | wdata->ir->id.version = wdata->hdev->version; |
| 739 | wdata->ir->name = WIIMOTE_NAME " IR"; |
| 740 | |
| 741 | set_bit(EV_ABS, wdata->ir->evbit); |
| 742 | set_bit(ABS_HAT0X, wdata->ir->absbit); |
| 743 | set_bit(ABS_HAT0Y, wdata->ir->absbit); |
| 744 | set_bit(ABS_HAT1X, wdata->ir->absbit); |
| 745 | set_bit(ABS_HAT1Y, wdata->ir->absbit); |
| 746 | set_bit(ABS_HAT2X, wdata->ir->absbit); |
| 747 | set_bit(ABS_HAT2Y, wdata->ir->absbit); |
| 748 | set_bit(ABS_HAT3X, wdata->ir->absbit); |
| 749 | set_bit(ABS_HAT3Y, wdata->ir->absbit); |
| 750 | input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4); |
| 751 | input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4); |
| 752 | input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4); |
| 753 | input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4); |
| 754 | input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4); |
| 755 | input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4); |
| 756 | input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4); |
| 757 | input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4); |
| 758 | |
| 759 | ret = input_register_device(wdata->ir); |
| 760 | if (ret) { |
| 761 | hid_err(wdata->hdev, "cannot register input device\n"); |
| 762 | goto err_free; |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | |
| 767 | err_free: |
| 768 | input_free_device(wdata->ir); |
| 769 | wdata->ir = NULL; |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | static void wiimod_ir_remove(const struct wiimod_ops *ops, |
| 774 | struct wiimote_data *wdata) |
| 775 | { |
| 776 | if (!wdata->ir) |
| 777 | return; |
| 778 | |
| 779 | input_unregister_device(wdata->ir); |
| 780 | wdata->ir = NULL; |
| 781 | } |
| 782 | |
| 783 | static const struct wiimod_ops wiimod_ir = { |
| 784 | .flags = 0, |
| 785 | .arg = 0, |
| 786 | .probe = wiimod_ir_probe, |
| 787 | .remove = wiimod_ir_remove, |
| 788 | .in_ir = wiimod_ir_in_ir, |
| 789 | }; |
| 790 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 791 | /* |
David Herrmann | b6ee67b | 2013-05-05 23:12:59 +0200 | [diff] [blame] | 792 | * Nunchuk Extension |
| 793 | * The Nintendo Wii Nunchuk was the first official extension published by |
| 794 | * Nintendo. It provides two additional keys and a separate accelerometer. It |
| 795 | * can be hotplugged to standard Wii Remotes. |
| 796 | */ |
| 797 | |
| 798 | enum wiimod_nunchuk_keys { |
| 799 | WIIMOD_NUNCHUK_KEY_C, |
| 800 | WIIMOD_NUNCHUK_KEY_Z, |
| 801 | WIIMOD_NUNCHUK_KEY_NUM, |
| 802 | }; |
| 803 | |
| 804 | static const __u16 wiimod_nunchuk_map[] = { |
| 805 | BTN_C, /* WIIMOD_NUNCHUK_KEY_C */ |
| 806 | BTN_Z, /* WIIMOD_NUNCHUK_KEY_Z */ |
| 807 | }; |
| 808 | |
| 809 | static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext) |
| 810 | { |
| 811 | __s16 x, y, z, bx, by; |
| 812 | |
| 813 | /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 | |
| 814 | * -----+----------+---------+---------+----+-----+ |
| 815 | * 1 | Button X <7:0> | |
| 816 | * 2 | Button Y <7:0> | |
| 817 | * -----+----------+---------+---------+----+-----+ |
| 818 | * 3 | Speed X <9:2> | |
| 819 | * 4 | Speed Y <9:2> | |
| 820 | * 5 | Speed Z <9:2> | |
| 821 | * -----+----------+---------+---------+----+-----+ |
| 822 | * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ | |
| 823 | * -----+----------+---------+---------+----+-----+ |
| 824 | * Button X/Y is the analog stick. Speed X, Y and Z are the |
| 825 | * accelerometer data in the same format as the wiimote's accelerometer. |
| 826 | * The 6th byte contains the LSBs of the accelerometer data. |
| 827 | * BC and BZ are the C and Z buttons: 0 means pressed |
| 828 | * |
| 829 | * If reported interleaved with motionp, then the layout changes. The |
| 830 | * 5th and 6th byte changes to: |
| 831 | * -----+-----------------------------------+-----+ |
| 832 | * 5 | Speed Z <9:3> | EXT | |
| 833 | * -----+--------+-----+-----+----+----+----+-----+ |
| 834 | * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 | |
| 835 | * -----+--------+-----+-----+----+----+----+-----+ |
| 836 | * All three accelerometer values lose their LSB. The other data is |
| 837 | * still available but slightly moved. |
| 838 | * |
| 839 | * Center data for button values is 128. Center value for accelerometer |
| 840 | * values it 512 / 0x200 |
| 841 | */ |
| 842 | |
| 843 | bx = ext[0]; |
| 844 | by = ext[1]; |
| 845 | bx -= 128; |
| 846 | by -= 128; |
| 847 | |
| 848 | x = ext[2] << 2; |
| 849 | y = ext[3] << 2; |
| 850 | z = ext[4] << 2; |
| 851 | |
| 852 | if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { |
| 853 | x |= (ext[5] >> 3) & 0x02; |
| 854 | y |= (ext[5] >> 4) & 0x02; |
| 855 | z &= ~0x4; |
| 856 | z |= (ext[5] >> 5) & 0x06; |
| 857 | } else { |
| 858 | x |= (ext[5] >> 2) & 0x03; |
| 859 | y |= (ext[5] >> 4) & 0x03; |
| 860 | z |= (ext[5] >> 6) & 0x03; |
| 861 | } |
| 862 | |
| 863 | x -= 0x200; |
| 864 | y -= 0x200; |
| 865 | z -= 0x200; |
| 866 | |
| 867 | input_report_abs(wdata->extension.input, ABS_HAT0X, bx); |
| 868 | input_report_abs(wdata->extension.input, ABS_HAT0Y, by); |
| 869 | |
| 870 | input_report_abs(wdata->extension.input, ABS_RX, x); |
| 871 | input_report_abs(wdata->extension.input, ABS_RY, y); |
| 872 | input_report_abs(wdata->extension.input, ABS_RZ, z); |
| 873 | |
| 874 | if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { |
| 875 | input_report_key(wdata->extension.input, |
| 876 | wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z], |
| 877 | !(ext[5] & 0x04)); |
| 878 | input_report_key(wdata->extension.input, |
| 879 | wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C], |
| 880 | !(ext[5] & 0x08)); |
| 881 | } else { |
| 882 | input_report_key(wdata->extension.input, |
| 883 | wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z], |
| 884 | !(ext[5] & 0x01)); |
| 885 | input_report_key(wdata->extension.input, |
| 886 | wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C], |
| 887 | !(ext[5] & 0x02)); |
| 888 | } |
| 889 | |
| 890 | input_sync(wdata->extension.input); |
| 891 | } |
| 892 | |
| 893 | static int wiimod_nunchuk_open(struct input_dev *dev) |
| 894 | { |
| 895 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 896 | unsigned long flags; |
| 897 | |
| 898 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 899 | wdata->state.flags |= WIIPROTO_FLAG_EXT_USED; |
| 900 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 901 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static void wiimod_nunchuk_close(struct input_dev *dev) |
| 907 | { |
| 908 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 909 | unsigned long flags; |
| 910 | |
| 911 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 912 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED; |
| 913 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 914 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 915 | } |
| 916 | |
| 917 | static int wiimod_nunchuk_probe(const struct wiimod_ops *ops, |
| 918 | struct wiimote_data *wdata) |
| 919 | { |
| 920 | int ret, i; |
| 921 | |
| 922 | wdata->extension.input = input_allocate_device(); |
| 923 | if (!wdata->extension.input) |
| 924 | return -ENOMEM; |
| 925 | |
| 926 | input_set_drvdata(wdata->extension.input, wdata); |
| 927 | wdata->extension.input->open = wiimod_nunchuk_open; |
| 928 | wdata->extension.input->close = wiimod_nunchuk_close; |
| 929 | wdata->extension.input->dev.parent = &wdata->hdev->dev; |
| 930 | wdata->extension.input->id.bustype = wdata->hdev->bus; |
| 931 | wdata->extension.input->id.vendor = wdata->hdev->vendor; |
| 932 | wdata->extension.input->id.product = wdata->hdev->product; |
| 933 | wdata->extension.input->id.version = wdata->hdev->version; |
| 934 | wdata->extension.input->name = WIIMOTE_NAME " Nunchuk"; |
| 935 | |
| 936 | set_bit(EV_KEY, wdata->extension.input->evbit); |
| 937 | for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i) |
| 938 | set_bit(wiimod_nunchuk_map[i], |
| 939 | wdata->extension.input->keybit); |
| 940 | |
| 941 | set_bit(EV_ABS, wdata->extension.input->evbit); |
| 942 | set_bit(ABS_HAT0X, wdata->extension.input->absbit); |
| 943 | set_bit(ABS_HAT0Y, wdata->extension.input->absbit); |
| 944 | input_set_abs_params(wdata->extension.input, |
| 945 | ABS_HAT0X, -120, 120, 2, 4); |
| 946 | input_set_abs_params(wdata->extension.input, |
| 947 | ABS_HAT0Y, -120, 120, 2, 4); |
| 948 | set_bit(ABS_RX, wdata->extension.input->absbit); |
| 949 | set_bit(ABS_RY, wdata->extension.input->absbit); |
| 950 | set_bit(ABS_RZ, wdata->extension.input->absbit); |
| 951 | input_set_abs_params(wdata->extension.input, |
| 952 | ABS_RX, -500, 500, 2, 4); |
| 953 | input_set_abs_params(wdata->extension.input, |
| 954 | ABS_RY, -500, 500, 2, 4); |
| 955 | input_set_abs_params(wdata->extension.input, |
| 956 | ABS_RZ, -500, 500, 2, 4); |
| 957 | |
| 958 | ret = input_register_device(wdata->extension.input); |
| 959 | if (ret) |
| 960 | goto err_free; |
| 961 | |
| 962 | return 0; |
| 963 | |
| 964 | err_free: |
| 965 | input_free_device(wdata->extension.input); |
| 966 | wdata->extension.input = NULL; |
| 967 | return ret; |
| 968 | } |
| 969 | |
| 970 | static void wiimod_nunchuk_remove(const struct wiimod_ops *ops, |
| 971 | struct wiimote_data *wdata) |
| 972 | { |
| 973 | if (!wdata->extension.input) |
| 974 | return; |
| 975 | |
| 976 | input_unregister_device(wdata->extension.input); |
| 977 | wdata->extension.input = NULL; |
| 978 | } |
| 979 | |
| 980 | static const struct wiimod_ops wiimod_nunchuk = { |
| 981 | .flags = 0, |
| 982 | .arg = 0, |
| 983 | .probe = wiimod_nunchuk_probe, |
| 984 | .remove = wiimod_nunchuk_remove, |
| 985 | .in_ext = wiimod_nunchuk_in_ext, |
| 986 | }; |
| 987 | |
| 988 | /* |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 989 | * Classic Controller |
| 990 | * Another official extension from Nintendo. It provides a classic |
| 991 | * gamecube-like controller that can be hotplugged on the Wii Remote. |
| 992 | * It has several hardware buttons and switches that are all reported via |
| 993 | * a normal extension device. |
| 994 | */ |
| 995 | |
| 996 | enum wiimod_classic_keys { |
| 997 | WIIMOD_CLASSIC_KEY_A, |
| 998 | WIIMOD_CLASSIC_KEY_B, |
| 999 | WIIMOD_CLASSIC_KEY_X, |
| 1000 | WIIMOD_CLASSIC_KEY_Y, |
| 1001 | WIIMOD_CLASSIC_KEY_ZL, |
| 1002 | WIIMOD_CLASSIC_KEY_ZR, |
| 1003 | WIIMOD_CLASSIC_KEY_PLUS, |
| 1004 | WIIMOD_CLASSIC_KEY_MINUS, |
| 1005 | WIIMOD_CLASSIC_KEY_HOME, |
| 1006 | WIIMOD_CLASSIC_KEY_LEFT, |
| 1007 | WIIMOD_CLASSIC_KEY_RIGHT, |
| 1008 | WIIMOD_CLASSIC_KEY_UP, |
| 1009 | WIIMOD_CLASSIC_KEY_DOWN, |
| 1010 | WIIMOD_CLASSIC_KEY_LT, |
| 1011 | WIIMOD_CLASSIC_KEY_RT, |
| 1012 | WIIMOD_CLASSIC_KEY_NUM, |
| 1013 | }; |
| 1014 | |
| 1015 | static const __u16 wiimod_classic_map[] = { |
| 1016 | BTN_A, /* WIIMOD_CLASSIC_KEY_A */ |
| 1017 | BTN_B, /* WIIMOD_CLASSIC_KEY_B */ |
| 1018 | BTN_X, /* WIIMOD_CLASSIC_KEY_X */ |
| 1019 | BTN_Y, /* WIIMOD_CLASSIC_KEY_Y */ |
| 1020 | BTN_TL2, /* WIIMOD_CLASSIC_KEY_ZL */ |
| 1021 | BTN_TR2, /* WIIMOD_CLASSIC_KEY_ZR */ |
| 1022 | KEY_NEXT, /* WIIMOD_CLASSIC_KEY_PLUS */ |
| 1023 | KEY_PREVIOUS, /* WIIMOD_CLASSIC_KEY_MINUS */ |
| 1024 | BTN_MODE, /* WIIMOD_CLASSIC_KEY_HOME */ |
| 1025 | KEY_LEFT, /* WIIMOD_CLASSIC_KEY_LEFT */ |
| 1026 | KEY_RIGHT, /* WIIMOD_CLASSIC_KEY_RIGHT */ |
| 1027 | KEY_UP, /* WIIMOD_CLASSIC_KEY_UP */ |
| 1028 | KEY_DOWN, /* WIIMOD_CLASSIC_KEY_DOWN */ |
| 1029 | BTN_TL, /* WIIMOD_CLASSIC_KEY_LT */ |
| 1030 | BTN_TR, /* WIIMOD_CLASSIC_KEY_RT */ |
| 1031 | }; |
| 1032 | |
| 1033 | static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext) |
| 1034 | { |
| 1035 | __s8 rx, ry, lx, ly, lt, rt; |
| 1036 | |
| 1037 | /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | |
| 1038 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1039 | * 1 | RX <5:4> | LX <5:0> | |
| 1040 | * 2 | RX <3:2> | LY <5:0> | |
| 1041 | * -----+-----+-----+-----+-----------------------------+ |
| 1042 | * 3 |RX<1>| LT <5:4> | RY <5:1> | |
| 1043 | * -----+-----+-----------+-----------------------------+ |
| 1044 | * 4 | LT <3:1> | RT <5:1> | |
| 1045 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1046 | * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 | |
| 1047 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1048 | * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU | |
| 1049 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1050 | * All buttons are 0 if pressed |
| 1051 | * RX and RY are right analog stick |
| 1052 | * LX and LY are left analog stick |
| 1053 | * LT is left trigger, RT is right trigger |
| 1054 | * BLT is 0 if left trigger is fully pressed |
| 1055 | * BRT is 0 if right trigger is fully pressed |
| 1056 | * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons |
| 1057 | * BZL is left Z button and BZR is right Z button |
| 1058 | * B-, BH, B+ are +, HOME and - buttons |
| 1059 | * BB, BY, BA, BX are A, B, X, Y buttons |
| 1060 | * LSB of RX, RY, LT, and RT are not transmitted and always 0. |
| 1061 | * |
| 1062 | * With motionp enabled it changes slightly to this: |
| 1063 | * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | |
| 1064 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1065 | * 1 | RX <5:4> | LX <5:1> | BDU | |
| 1066 | * 2 | RX <3:2> | LY <5:1> | BDL | |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1067 | * -----+-----+-----+-----+-----------------------+-----+ |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1068 | * 3 |RX<1>| LT <5:4> | RY <5:1> | |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1069 | * -----+-----+-----------+-----------------------------+ |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1070 | * 4 | LT <3:1> | RT <5:1> | |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1071 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1072 | * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT | |
| 1073 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1074 | * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 | |
| 1075 | * -----+-----+-----+-----+-----+-----+-----+-----+-----+ |
| 1076 | * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest |
| 1077 | * is the same as before. |
| 1078 | */ |
| 1079 | |
| 1080 | if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { |
| 1081 | lx = ext[0] & 0x3e; |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1082 | ly = ext[1] & 0x3e; |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1083 | } else { |
| 1084 | lx = ext[0] & 0x3f; |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1085 | ly = ext[1] & 0x3f; |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1086 | } |
| 1087 | |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1088 | rx = (ext[0] >> 3) & 0x18; |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1089 | rx |= (ext[1] >> 5) & 0x06; |
| 1090 | rx |= (ext[2] >> 7) & 0x01; |
| 1091 | ry = ext[2] & 0x1f; |
| 1092 | |
| 1093 | rt = ext[3] & 0x1f; |
| 1094 | lt = (ext[2] >> 2) & 0x18; |
| 1095 | lt |= (ext[3] >> 5) & 0x07; |
| 1096 | |
| 1097 | rx <<= 1; |
| 1098 | ry <<= 1; |
| 1099 | rt <<= 1; |
| 1100 | lt <<= 1; |
| 1101 | |
| 1102 | input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20); |
| 1103 | input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20); |
| 1104 | input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20); |
| 1105 | input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20); |
David Herrmann | ee286c2 | 2013-05-26 22:55:04 +0200 | [diff] [blame^] | 1106 | input_report_abs(wdata->extension.input, ABS_HAT3X, rt); |
| 1107 | input_report_abs(wdata->extension.input, ABS_HAT3Y, lt); |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1108 | |
| 1109 | input_report_key(wdata->extension.input, |
| 1110 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT], |
| 1111 | !(ext[4] & 0x80)); |
| 1112 | input_report_key(wdata->extension.input, |
| 1113 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN], |
| 1114 | !(ext[4] & 0x40)); |
| 1115 | input_report_key(wdata->extension.input, |
| 1116 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT], |
| 1117 | !(ext[4] & 0x20)); |
| 1118 | input_report_key(wdata->extension.input, |
| 1119 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS], |
| 1120 | !(ext[4] & 0x10)); |
| 1121 | input_report_key(wdata->extension.input, |
| 1122 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME], |
| 1123 | !(ext[4] & 0x08)); |
| 1124 | input_report_key(wdata->extension.input, |
| 1125 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS], |
| 1126 | !(ext[4] & 0x04)); |
| 1127 | input_report_key(wdata->extension.input, |
| 1128 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT], |
| 1129 | !(ext[4] & 0x02)); |
| 1130 | input_report_key(wdata->extension.input, |
| 1131 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL], |
| 1132 | !(ext[5] & 0x80)); |
| 1133 | input_report_key(wdata->extension.input, |
| 1134 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_B], |
| 1135 | !(ext[5] & 0x40)); |
| 1136 | input_report_key(wdata->extension.input, |
| 1137 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y], |
| 1138 | !(ext[5] & 0x20)); |
| 1139 | input_report_key(wdata->extension.input, |
| 1140 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_A], |
| 1141 | !(ext[5] & 0x10)); |
| 1142 | input_report_key(wdata->extension.input, |
| 1143 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_X], |
| 1144 | !(ext[5] & 0x08)); |
| 1145 | input_report_key(wdata->extension.input, |
| 1146 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR], |
| 1147 | !(ext[5] & 0x04)); |
| 1148 | |
| 1149 | if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { |
| 1150 | input_report_key(wdata->extension.input, |
| 1151 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT], |
| 1152 | !(ext[1] & 0x01)); |
| 1153 | input_report_key(wdata->extension.input, |
| 1154 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP], |
| 1155 | !(ext[0] & 0x01)); |
| 1156 | } else { |
| 1157 | input_report_key(wdata->extension.input, |
| 1158 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT], |
| 1159 | !(ext[5] & 0x02)); |
| 1160 | input_report_key(wdata->extension.input, |
| 1161 | wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP], |
| 1162 | !(ext[5] & 0x01)); |
| 1163 | } |
| 1164 | |
| 1165 | input_sync(wdata->extension.input); |
| 1166 | } |
| 1167 | |
| 1168 | static int wiimod_classic_open(struct input_dev *dev) |
| 1169 | { |
| 1170 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 1171 | unsigned long flags; |
| 1172 | |
| 1173 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1174 | wdata->state.flags |= WIIPROTO_FLAG_EXT_USED; |
| 1175 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 1176 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | static void wiimod_classic_close(struct input_dev *dev) |
| 1182 | { |
| 1183 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 1184 | unsigned long flags; |
| 1185 | |
| 1186 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1187 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED; |
| 1188 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 1189 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1190 | } |
| 1191 | |
| 1192 | static int wiimod_classic_probe(const struct wiimod_ops *ops, |
| 1193 | struct wiimote_data *wdata) |
| 1194 | { |
| 1195 | int ret, i; |
| 1196 | |
| 1197 | wdata->extension.input = input_allocate_device(); |
| 1198 | if (!wdata->extension.input) |
| 1199 | return -ENOMEM; |
| 1200 | |
| 1201 | input_set_drvdata(wdata->extension.input, wdata); |
| 1202 | wdata->extension.input->open = wiimod_classic_open; |
| 1203 | wdata->extension.input->close = wiimod_classic_close; |
| 1204 | wdata->extension.input->dev.parent = &wdata->hdev->dev; |
| 1205 | wdata->extension.input->id.bustype = wdata->hdev->bus; |
| 1206 | wdata->extension.input->id.vendor = wdata->hdev->vendor; |
| 1207 | wdata->extension.input->id.product = wdata->hdev->product; |
| 1208 | wdata->extension.input->id.version = wdata->hdev->version; |
| 1209 | wdata->extension.input->name = WIIMOTE_NAME " Classic Controller"; |
| 1210 | |
| 1211 | set_bit(EV_KEY, wdata->extension.input->evbit); |
| 1212 | for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i) |
| 1213 | set_bit(wiimod_classic_map[i], |
| 1214 | wdata->extension.input->keybit); |
| 1215 | |
| 1216 | set_bit(EV_ABS, wdata->extension.input->evbit); |
| 1217 | set_bit(ABS_HAT1X, wdata->extension.input->absbit); |
| 1218 | set_bit(ABS_HAT1Y, wdata->extension.input->absbit); |
| 1219 | set_bit(ABS_HAT2X, wdata->extension.input->absbit); |
| 1220 | set_bit(ABS_HAT2Y, wdata->extension.input->absbit); |
| 1221 | set_bit(ABS_HAT3X, wdata->extension.input->absbit); |
| 1222 | set_bit(ABS_HAT3Y, wdata->extension.input->absbit); |
| 1223 | input_set_abs_params(wdata->extension.input, |
| 1224 | ABS_HAT1X, -30, 30, 1, 1); |
| 1225 | input_set_abs_params(wdata->extension.input, |
| 1226 | ABS_HAT1Y, -30, 30, 1, 1); |
| 1227 | input_set_abs_params(wdata->extension.input, |
| 1228 | ABS_HAT2X, -30, 30, 1, 1); |
| 1229 | input_set_abs_params(wdata->extension.input, |
| 1230 | ABS_HAT2Y, -30, 30, 1, 1); |
| 1231 | input_set_abs_params(wdata->extension.input, |
| 1232 | ABS_HAT3X, -30, 30, 1, 1); |
| 1233 | input_set_abs_params(wdata->extension.input, |
| 1234 | ABS_HAT3Y, -30, 30, 1, 1); |
| 1235 | |
| 1236 | ret = input_register_device(wdata->extension.input); |
| 1237 | if (ret) |
| 1238 | goto err_free; |
| 1239 | |
| 1240 | return 0; |
| 1241 | |
| 1242 | err_free: |
| 1243 | input_free_device(wdata->extension.input); |
| 1244 | wdata->extension.input = NULL; |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | static void wiimod_classic_remove(const struct wiimod_ops *ops, |
| 1249 | struct wiimote_data *wdata) |
| 1250 | { |
| 1251 | if (!wdata->extension.input) |
| 1252 | return; |
| 1253 | |
| 1254 | input_unregister_device(wdata->extension.input); |
| 1255 | wdata->extension.input = NULL; |
| 1256 | } |
| 1257 | |
| 1258 | static const struct wiimod_ops wiimod_classic = { |
| 1259 | .flags = 0, |
| 1260 | .arg = 0, |
| 1261 | .probe = wiimod_classic_probe, |
| 1262 | .remove = wiimod_classic_remove, |
| 1263 | .in_ext = wiimod_classic_in_ext, |
| 1264 | }; |
| 1265 | |
| 1266 | /* |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1267 | * Balance Board Extension |
| 1268 | * The Nintendo Wii Balance Board provides four hardware weight sensor plus a |
| 1269 | * single push button. No other peripherals are available. However, the |
| 1270 | * balance-board data is sent via a standard Wii Remote extension. All other |
| 1271 | * data for non-present hardware is zeroed out. |
| 1272 | * Some 3rd party devices react allergic if we try to access normal Wii Remote |
| 1273 | * hardware, so this extension module should be the only module that is loaded |
| 1274 | * on balance boards. |
| 1275 | * The balance board needs 8 bytes extension data instead of basic 6 bytes so |
| 1276 | * it needs the WIIMOD_FLAG_EXT8 flag. |
| 1277 | */ |
| 1278 | |
| 1279 | static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys) |
| 1280 | { |
| 1281 | input_report_key(wdata->extension.input, BTN_A, |
| 1282 | !!(keys[1] & 0x08)); |
| 1283 | input_sync(wdata->extension.input); |
| 1284 | } |
| 1285 | |
| 1286 | static void wiimod_bboard_in_ext(struct wiimote_data *wdata, |
| 1287 | const __u8 *ext) |
| 1288 | { |
| 1289 | __s32 val[4], tmp, div; |
| 1290 | unsigned int i; |
| 1291 | struct wiimote_state *s = &wdata->state; |
| 1292 | |
| 1293 | /* |
| 1294 | * Balance board data layout: |
| 1295 | * |
| 1296 | * Byte | 8 7 6 5 4 3 2 1 | |
| 1297 | * -----+--------------------------+ |
| 1298 | * 1 | Top Right <15:8> | |
| 1299 | * 2 | Top Right <7:0> | |
| 1300 | * -----+--------------------------+ |
| 1301 | * 3 | Bottom Right <15:8> | |
| 1302 | * 4 | Bottom Right <7:0> | |
| 1303 | * -----+--------------------------+ |
| 1304 | * 5 | Top Left <15:8> | |
| 1305 | * 6 | Top Left <7:0> | |
| 1306 | * -----+--------------------------+ |
| 1307 | * 7 | Bottom Left <15:8> | |
| 1308 | * 8 | Bottom Left <7:0> | |
| 1309 | * -----+--------------------------+ |
| 1310 | * |
| 1311 | * These values represent the weight-measurements of the Wii-balance |
| 1312 | * board with 16bit precision. |
| 1313 | * |
| 1314 | * The balance-board is never reported interleaved with motionp. |
| 1315 | */ |
| 1316 | |
| 1317 | val[0] = ext[0]; |
| 1318 | val[0] <<= 8; |
| 1319 | val[0] |= ext[1]; |
| 1320 | |
| 1321 | val[1] = ext[2]; |
| 1322 | val[1] <<= 8; |
| 1323 | val[1] |= ext[3]; |
| 1324 | |
| 1325 | val[2] = ext[4]; |
| 1326 | val[2] <<= 8; |
| 1327 | val[2] |= ext[5]; |
| 1328 | |
| 1329 | val[3] = ext[6]; |
| 1330 | val[3] <<= 8; |
| 1331 | val[3] |= ext[7]; |
| 1332 | |
| 1333 | /* apply calibration data */ |
| 1334 | for (i = 0; i < 4; i++) { |
| 1335 | if (val[i] <= s->calib_bboard[i][0]) { |
| 1336 | tmp = 0; |
| 1337 | } else if (val[i] < s->calib_bboard[i][1]) { |
| 1338 | tmp = val[i] - s->calib_bboard[i][0]; |
| 1339 | tmp *= 1700; |
| 1340 | div = s->calib_bboard[i][1] - s->calib_bboard[i][0]; |
| 1341 | tmp /= div ? div : 1; |
| 1342 | } else { |
| 1343 | tmp = val[i] - s->calib_bboard[i][1]; |
| 1344 | tmp *= 1700; |
| 1345 | div = s->calib_bboard[i][2] - s->calib_bboard[i][1]; |
| 1346 | tmp /= div ? div : 1; |
| 1347 | tmp += 1700; |
| 1348 | } |
| 1349 | val[i] = tmp; |
| 1350 | } |
| 1351 | |
| 1352 | input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]); |
| 1353 | input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]); |
| 1354 | input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]); |
| 1355 | input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]); |
| 1356 | input_sync(wdata->extension.input); |
| 1357 | } |
| 1358 | |
| 1359 | static int wiimod_bboard_open(struct input_dev *dev) |
| 1360 | { |
| 1361 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 1362 | unsigned long flags; |
| 1363 | |
| 1364 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1365 | wdata->state.flags |= WIIPROTO_FLAG_EXT_USED; |
| 1366 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 1367 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1368 | |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | static void wiimod_bboard_close(struct input_dev *dev) |
| 1373 | { |
| 1374 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 1375 | unsigned long flags; |
| 1376 | |
| 1377 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1378 | wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED; |
| 1379 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 1380 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1381 | } |
| 1382 | |
David Herrmann | 8b1fded | 2013-05-05 23:13:05 +0200 | [diff] [blame] | 1383 | static ssize_t wiimod_bboard_calib_show(struct device *dev, |
| 1384 | struct device_attribute *attr, |
| 1385 | char *out) |
| 1386 | { |
| 1387 | struct wiimote_data *wdata = dev_to_wii(dev); |
| 1388 | int i, j, ret; |
| 1389 | __u16 val; |
| 1390 | __u8 buf[24], offs; |
| 1391 | |
| 1392 | ret = wiimote_cmd_acquire(wdata); |
| 1393 | if (ret) |
| 1394 | return ret; |
| 1395 | |
| 1396 | ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12); |
| 1397 | if (ret != 12) { |
| 1398 | wiimote_cmd_release(wdata); |
| 1399 | return ret < 0 ? ret : -EIO; |
| 1400 | } |
| 1401 | ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12); |
| 1402 | if (ret != 12) { |
| 1403 | wiimote_cmd_release(wdata); |
| 1404 | return ret < 0 ? ret : -EIO; |
| 1405 | } |
| 1406 | |
| 1407 | wiimote_cmd_release(wdata); |
| 1408 | |
| 1409 | spin_lock_irq(&wdata->state.lock); |
| 1410 | offs = 0; |
| 1411 | for (i = 0; i < 3; ++i) { |
| 1412 | for (j = 0; j < 4; ++j) { |
| 1413 | wdata->state.calib_bboard[j][i] = buf[offs]; |
| 1414 | wdata->state.calib_bboard[j][i] <<= 8; |
| 1415 | wdata->state.calib_bboard[j][i] |= buf[offs + 1]; |
| 1416 | offs += 2; |
| 1417 | } |
| 1418 | } |
| 1419 | spin_unlock_irq(&wdata->state.lock); |
| 1420 | |
| 1421 | ret = 0; |
| 1422 | for (i = 0; i < 3; ++i) { |
| 1423 | for (j = 0; j < 4; ++j) { |
| 1424 | val = wdata->state.calib_bboard[j][i]; |
| 1425 | if (i == 2 && j == 3) |
| 1426 | ret += sprintf(&out[ret], "%04x\n", val); |
| 1427 | else |
| 1428 | ret += sprintf(&out[ret], "%04x:", val); |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL); |
| 1436 | |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1437 | static int wiimod_bboard_probe(const struct wiimod_ops *ops, |
| 1438 | struct wiimote_data *wdata) |
| 1439 | { |
| 1440 | int ret, i, j; |
| 1441 | __u8 buf[24], offs; |
| 1442 | |
| 1443 | wiimote_cmd_acquire_noint(wdata); |
| 1444 | |
| 1445 | ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12); |
| 1446 | if (ret != 12) { |
| 1447 | wiimote_cmd_release(wdata); |
| 1448 | return ret < 0 ? ret : -EIO; |
| 1449 | } |
| 1450 | ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12); |
| 1451 | if (ret != 12) { |
| 1452 | wiimote_cmd_release(wdata); |
| 1453 | return ret < 0 ? ret : -EIO; |
| 1454 | } |
| 1455 | |
| 1456 | wiimote_cmd_release(wdata); |
| 1457 | |
| 1458 | offs = 0; |
| 1459 | for (i = 0; i < 3; ++i) { |
| 1460 | for (j = 0; j < 4; ++j) { |
| 1461 | wdata->state.calib_bboard[j][i] = buf[offs]; |
| 1462 | wdata->state.calib_bboard[j][i] <<= 8; |
| 1463 | wdata->state.calib_bboard[j][i] |= buf[offs + 1]; |
| 1464 | offs += 2; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | wdata->extension.input = input_allocate_device(); |
| 1469 | if (!wdata->extension.input) |
| 1470 | return -ENOMEM; |
| 1471 | |
David Herrmann | 8b1fded | 2013-05-05 23:13:05 +0200 | [diff] [blame] | 1472 | ret = device_create_file(&wdata->hdev->dev, |
| 1473 | &dev_attr_bboard_calib); |
| 1474 | if (ret) { |
| 1475 | hid_err(wdata->hdev, "cannot create sysfs attribute\n"); |
| 1476 | goto err_free; |
| 1477 | } |
| 1478 | |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1479 | input_set_drvdata(wdata->extension.input, wdata); |
| 1480 | wdata->extension.input->open = wiimod_bboard_open; |
| 1481 | wdata->extension.input->close = wiimod_bboard_close; |
| 1482 | wdata->extension.input->dev.parent = &wdata->hdev->dev; |
| 1483 | wdata->extension.input->id.bustype = wdata->hdev->bus; |
| 1484 | wdata->extension.input->id.vendor = wdata->hdev->vendor; |
| 1485 | wdata->extension.input->id.product = wdata->hdev->product; |
| 1486 | wdata->extension.input->id.version = wdata->hdev->version; |
| 1487 | wdata->extension.input->name = WIIMOTE_NAME " Balance Board"; |
| 1488 | |
| 1489 | set_bit(EV_KEY, wdata->extension.input->evbit); |
| 1490 | set_bit(BTN_A, wdata->extension.input->keybit); |
| 1491 | |
| 1492 | set_bit(EV_ABS, wdata->extension.input->evbit); |
| 1493 | set_bit(ABS_HAT0X, wdata->extension.input->absbit); |
| 1494 | set_bit(ABS_HAT0Y, wdata->extension.input->absbit); |
| 1495 | set_bit(ABS_HAT1X, wdata->extension.input->absbit); |
| 1496 | set_bit(ABS_HAT1Y, wdata->extension.input->absbit); |
| 1497 | input_set_abs_params(wdata->extension.input, |
| 1498 | ABS_HAT0X, 0, 65535, 2, 4); |
| 1499 | input_set_abs_params(wdata->extension.input, |
| 1500 | ABS_HAT0Y, 0, 65535, 2, 4); |
| 1501 | input_set_abs_params(wdata->extension.input, |
| 1502 | ABS_HAT1X, 0, 65535, 2, 4); |
| 1503 | input_set_abs_params(wdata->extension.input, |
| 1504 | ABS_HAT1Y, 0, 65535, 2, 4); |
| 1505 | |
| 1506 | ret = input_register_device(wdata->extension.input); |
| 1507 | if (ret) |
David Herrmann | 8b1fded | 2013-05-05 23:13:05 +0200 | [diff] [blame] | 1508 | goto err_file; |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1509 | |
| 1510 | return 0; |
| 1511 | |
David Herrmann | 8b1fded | 2013-05-05 23:13:05 +0200 | [diff] [blame] | 1512 | err_file: |
| 1513 | device_remove_file(&wdata->hdev->dev, |
| 1514 | &dev_attr_bboard_calib); |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1515 | err_free: |
| 1516 | input_free_device(wdata->extension.input); |
| 1517 | wdata->extension.input = NULL; |
| 1518 | return ret; |
| 1519 | } |
| 1520 | |
| 1521 | static void wiimod_bboard_remove(const struct wiimod_ops *ops, |
| 1522 | struct wiimote_data *wdata) |
| 1523 | { |
| 1524 | if (!wdata->extension.input) |
| 1525 | return; |
| 1526 | |
| 1527 | input_unregister_device(wdata->extension.input); |
| 1528 | wdata->extension.input = NULL; |
David Herrmann | 8b1fded | 2013-05-05 23:13:05 +0200 | [diff] [blame] | 1529 | device_remove_file(&wdata->hdev->dev, |
| 1530 | &dev_attr_bboard_calib); |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static const struct wiimod_ops wiimod_bboard = { |
| 1534 | .flags = WIIMOD_FLAG_EXT8, |
| 1535 | .arg = 0, |
| 1536 | .probe = wiimod_bboard_probe, |
| 1537 | .remove = wiimod_bboard_remove, |
| 1538 | .in_keys = wiimod_bboard_in_keys, |
| 1539 | .in_ext = wiimod_bboard_in_ext, |
| 1540 | }; |
| 1541 | |
| 1542 | /* |
David Herrmann | 9f32974 | 2013-05-05 23:13:07 +0200 | [diff] [blame] | 1543 | * Builtin Motion Plus |
| 1544 | * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which |
| 1545 | * disables polling for Motion-Plus. This should be set only for devices which |
| 1546 | * don't allow MP hotplugging. |
| 1547 | */ |
| 1548 | |
| 1549 | static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops, |
| 1550 | struct wiimote_data *wdata) |
| 1551 | { |
| 1552 | unsigned long flags; |
| 1553 | |
| 1554 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1555 | wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP; |
| 1556 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops, |
| 1562 | struct wiimote_data *wdata) |
| 1563 | { |
| 1564 | unsigned long flags; |
| 1565 | |
| 1566 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1567 | wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP; |
| 1568 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1569 | } |
| 1570 | |
| 1571 | static const struct wiimod_ops wiimod_builtin_mp = { |
| 1572 | .flags = 0, |
| 1573 | .arg = 0, |
| 1574 | .probe = wiimod_builtin_mp_probe, |
| 1575 | .remove = wiimod_builtin_mp_remove, |
| 1576 | }; |
| 1577 | |
| 1578 | /* |
| 1579 | * No Motion Plus |
| 1580 | * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which |
| 1581 | * disables motion-plus. This is needed for devices that advertise this but we |
| 1582 | * don't know how to use it (or whether it is actually present). |
| 1583 | */ |
| 1584 | |
| 1585 | static int wiimod_no_mp_probe(const struct wiimod_ops *ops, |
| 1586 | struct wiimote_data *wdata) |
| 1587 | { |
| 1588 | unsigned long flags; |
| 1589 | |
| 1590 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1591 | wdata->state.flags |= WIIPROTO_FLAG_NO_MP; |
| 1592 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | static void wiimod_no_mp_remove(const struct wiimod_ops *ops, |
| 1598 | struct wiimote_data *wdata) |
| 1599 | { |
| 1600 | unsigned long flags; |
| 1601 | |
| 1602 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1603 | wdata->state.flags |= WIIPROTO_FLAG_NO_MP; |
| 1604 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1605 | } |
| 1606 | |
| 1607 | static const struct wiimod_ops wiimod_no_mp = { |
| 1608 | .flags = 0, |
| 1609 | .arg = 0, |
| 1610 | .probe = wiimod_no_mp_probe, |
| 1611 | .remove = wiimod_no_mp_remove, |
| 1612 | }; |
| 1613 | |
| 1614 | /* |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1615 | * Motion Plus |
David Herrmann | 34472d37 | 2013-05-05 23:13:01 +0200 | [diff] [blame] | 1616 | * The Motion Plus extension provides rotation sensors (gyro) as a small |
| 1617 | * extension device for Wii Remotes. Many devices have them built-in so |
| 1618 | * you cannot see them from the outside. |
| 1619 | * Motion Plus extensions are special because they are on a separate extension |
| 1620 | * port and allow other extensions to be used simultaneously. This is all |
| 1621 | * handled by the Wiimote Core so we don't have to deal with it. |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1622 | */ |
| 1623 | |
David Herrmann | 34472d37 | 2013-05-05 23:13:01 +0200 | [diff] [blame] | 1624 | static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext) |
| 1625 | { |
| 1626 | __s32 x, y, z; |
| 1627 | |
| 1628 | /* | 8 7 6 5 4 3 | 2 | 1 | |
| 1629 | * -----+------------------------------+-----+-----+ |
| 1630 | * 1 | Yaw Speed <7:0> | |
| 1631 | * 2 | Roll Speed <7:0> | |
| 1632 | * 3 | Pitch Speed <7:0> | |
| 1633 | * -----+------------------------------+-----+-----+ |
| 1634 | * 4 | Yaw Speed <13:8> | Yaw |Pitch| |
| 1635 | * -----+------------------------------+-----+-----+ |
| 1636 | * 5 | Roll Speed <13:8> |Roll | Ext | |
| 1637 | * -----+------------------------------+-----+-----+ |
| 1638 | * 6 | Pitch Speed <13:8> | 1 | 0 | |
| 1639 | * -----+------------------------------+-----+-----+ |
| 1640 | * The single bits Yaw, Roll, Pitch in the lower right corner specify |
| 1641 | * whether the wiimote is rotating fast (0) or slow (1). Speed for slow |
| 1642 | * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a |
| 1643 | * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast |
| 1644 | * and 9 for slow. |
| 1645 | * If the wiimote is not rotating the sensor reports 2^13 = 8192. |
| 1646 | * Ext specifies whether an extension is connected to the motionp. |
| 1647 | * which is parsed by wiimote-core. |
| 1648 | */ |
| 1649 | |
| 1650 | x = ext[0]; |
| 1651 | y = ext[1]; |
| 1652 | z = ext[2]; |
| 1653 | |
| 1654 | x |= (((__u16)ext[3]) << 6) & 0xff00; |
| 1655 | y |= (((__u16)ext[4]) << 6) & 0xff00; |
| 1656 | z |= (((__u16)ext[5]) << 6) & 0xff00; |
| 1657 | |
| 1658 | x -= 8192; |
| 1659 | y -= 8192; |
| 1660 | z -= 8192; |
| 1661 | |
| 1662 | if (!(ext[3] & 0x02)) |
| 1663 | x *= 18; |
| 1664 | else |
| 1665 | x *= 9; |
| 1666 | if (!(ext[4] & 0x02)) |
| 1667 | y *= 18; |
| 1668 | else |
| 1669 | y *= 9; |
| 1670 | if (!(ext[3] & 0x01)) |
| 1671 | z *= 18; |
| 1672 | else |
| 1673 | z *= 9; |
| 1674 | |
| 1675 | input_report_abs(wdata->mp, ABS_RX, x); |
| 1676 | input_report_abs(wdata->mp, ABS_RY, y); |
| 1677 | input_report_abs(wdata->mp, ABS_RZ, z); |
| 1678 | input_sync(wdata->mp); |
| 1679 | } |
| 1680 | |
| 1681 | static int wiimod_mp_open(struct input_dev *dev) |
| 1682 | { |
| 1683 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 1684 | unsigned long flags; |
| 1685 | |
| 1686 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1687 | wdata->state.flags |= WIIPROTO_FLAG_MP_USED; |
| 1688 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 1689 | __wiimote_schedule(wdata); |
| 1690 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1691 | |
| 1692 | return 0; |
| 1693 | } |
| 1694 | |
| 1695 | static void wiimod_mp_close(struct input_dev *dev) |
| 1696 | { |
| 1697 | struct wiimote_data *wdata = input_get_drvdata(dev); |
| 1698 | unsigned long flags; |
| 1699 | |
| 1700 | spin_lock_irqsave(&wdata->state.lock, flags); |
| 1701 | wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED; |
| 1702 | wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); |
| 1703 | __wiimote_schedule(wdata); |
| 1704 | spin_unlock_irqrestore(&wdata->state.lock, flags); |
| 1705 | } |
| 1706 | |
| 1707 | static int wiimod_mp_probe(const struct wiimod_ops *ops, |
| 1708 | struct wiimote_data *wdata) |
| 1709 | { |
| 1710 | int ret; |
| 1711 | |
| 1712 | wdata->mp = input_allocate_device(); |
| 1713 | if (!wdata->mp) |
| 1714 | return -ENOMEM; |
| 1715 | |
| 1716 | input_set_drvdata(wdata->mp, wdata); |
| 1717 | wdata->mp->open = wiimod_mp_open; |
| 1718 | wdata->mp->close = wiimod_mp_close; |
| 1719 | wdata->mp->dev.parent = &wdata->hdev->dev; |
| 1720 | wdata->mp->id.bustype = wdata->hdev->bus; |
| 1721 | wdata->mp->id.vendor = wdata->hdev->vendor; |
| 1722 | wdata->mp->id.product = wdata->hdev->product; |
| 1723 | wdata->mp->id.version = wdata->hdev->version; |
| 1724 | wdata->mp->name = WIIMOTE_NAME " Motion Plus"; |
| 1725 | |
| 1726 | set_bit(EV_ABS, wdata->mp->evbit); |
| 1727 | set_bit(ABS_RX, wdata->mp->absbit); |
| 1728 | set_bit(ABS_RY, wdata->mp->absbit); |
| 1729 | set_bit(ABS_RZ, wdata->mp->absbit); |
| 1730 | input_set_abs_params(wdata->mp, |
| 1731 | ABS_RX, -16000, 16000, 4, 8); |
| 1732 | input_set_abs_params(wdata->mp, |
| 1733 | ABS_RY, -16000, 16000, 4, 8); |
| 1734 | input_set_abs_params(wdata->mp, |
| 1735 | ABS_RZ, -16000, 16000, 4, 8); |
| 1736 | |
| 1737 | ret = input_register_device(wdata->mp); |
| 1738 | if (ret) |
| 1739 | goto err_free; |
| 1740 | |
| 1741 | return 0; |
| 1742 | |
| 1743 | err_free: |
| 1744 | input_free_device(wdata->mp); |
| 1745 | wdata->mp = NULL; |
| 1746 | return ret; |
| 1747 | } |
| 1748 | |
| 1749 | static void wiimod_mp_remove(const struct wiimod_ops *ops, |
| 1750 | struct wiimote_data *wdata) |
| 1751 | { |
| 1752 | if (!wdata->mp) |
| 1753 | return; |
| 1754 | |
| 1755 | input_unregister_device(wdata->mp); |
| 1756 | wdata->mp = NULL; |
| 1757 | } |
| 1758 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1759 | const struct wiimod_ops wiimod_mp = { |
| 1760 | .flags = 0, |
| 1761 | .arg = 0, |
David Herrmann | 34472d37 | 2013-05-05 23:13:01 +0200 | [diff] [blame] | 1762 | .probe = wiimod_mp_probe, |
| 1763 | .remove = wiimod_mp_remove, |
| 1764 | .in_mp = wiimod_mp_in_mp, |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1765 | }; |
| 1766 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 1767 | /* module table */ |
| 1768 | |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1769 | static const struct wiimod_ops wiimod_dummy; |
| 1770 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 1771 | const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = { |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 1772 | [WIIMOD_KEYS] = &wiimod_keys, |
| 1773 | [WIIMOD_RUMBLE] = &wiimod_rumble, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame] | 1774 | [WIIMOD_BATTERY] = &wiimod_battery, |
David Herrmann | 6c5ae01 | 2013-05-05 23:12:54 +0200 | [diff] [blame] | 1775 | [WIIMOD_LED1] = &wiimod_leds[0], |
| 1776 | [WIIMOD_LED2] = &wiimod_leds[1], |
| 1777 | [WIIMOD_LED3] = &wiimod_leds[2], |
| 1778 | [WIIMOD_LED4] = &wiimod_leds[3], |
David Herrmann | 0ea1675 | 2013-05-05 23:12:55 +0200 | [diff] [blame] | 1779 | [WIIMOD_ACCEL] = &wiimod_accel, |
David Herrmann | 3b5f03c | 2013-05-05 23:12:56 +0200 | [diff] [blame] | 1780 | [WIIMOD_IR] = &wiimod_ir, |
David Herrmann | 9f32974 | 2013-05-05 23:13:07 +0200 | [diff] [blame] | 1781 | [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp, |
| 1782 | [WIIMOD_NO_MP] = &wiimod_no_mp, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 1783 | }; |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1784 | |
| 1785 | const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = { |
| 1786 | [WIIMOTE_EXT_NONE] = &wiimod_dummy, |
| 1787 | [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy, |
David Herrmann | b6ee67b | 2013-05-05 23:12:59 +0200 | [diff] [blame] | 1788 | [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk, |
David Herrmann | 9d6f9ec | 2013-05-05 23:13:00 +0200 | [diff] [blame] | 1789 | [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic, |
David Herrmann | f1d4bed | 2013-05-05 23:12:58 +0200 | [diff] [blame] | 1790 | [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard, |
David Herrmann | 4148b6b | 2013-05-05 23:12:57 +0200 | [diff] [blame] | 1791 | }; |