Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid

Pull HID updates from Jiri Kosina:
- HID battery handling cleanup by David Herrmann
- ELO 4000/4500 driver, which has been finally ported to be proper HID
driver by Jiri Slaby
- ps3remote driver functionality is now provided by generic sony
driver, by Jiri Kosina
- PS2/3 Buzz controllers support, by Colin Leitner
- rework of wiimote driver including full extensions hotpluggin
support, sub-device modularization and speaker support by David
Herrmann

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid: (55 commits)
HID: wacom: Intuos4 battery charging changes
HID: i2c-hid: support sending HID output reports using the output register
HID: kye: Add report fixup for Genius Gila Gaming mouse
HID: wiimote: support Nintendo Wii U Pro Controller
Input: make gamepad API keycodes more clear
input: document gamepad API and add extra keycodes
HID: explain out-of-range check better
HID: fix false positive out of range values
HID: wiimote: fix coccinelle warnings
HID: roccat: check cdev_add return value
HID: fold ps3remote driver into generic Sony driver
HID: hyperv: convert alloc+memcpy to memdup
HID: core: fix reporting of raw events
HID: wiimote: discard invalid EXT data reports
HID: wiimote: fix classic controller parsing
HID: wiimote: init EXT/MP during device detection
HID: wiimote: fix DRM debug-attr to correctly parse input
HID: wiimote: add MP quirks
HID: wiimote: remove old static extension support
HID: wiimote: add "bboard_calib" attribute
...

+4710 -1798
+38 -1
Documentation/ABI/testing/sysfs-driver-hid-wiimote
··· 12 12 What: /sys/bus/hid/drivers/wiimote/<dev>/extension 13 13 Date: August 2011 14 14 KernelVersion: 3.2 15 - Contact: David Herrmann <dh.herrmann@googlemail.com> 15 + Contact: David Herrmann <dh.herrmann@gmail.com> 16 16 Description: This file contains the currently connected and initialized 17 17 extensions. It can be one of: none, motionp, nunchuck, classic, 18 18 motionp+nunchuck, motionp+classic ··· 20 20 the official Nintendo Nunchuck extension and classic is the 21 21 Nintendo Classic Controller extension. The motionp extension can 22 22 be combined with the other two. 23 + Starting with kernel-version 3.11 Motion Plus hotplugging is 24 + supported and if detected, it's no longer reported as static 25 + extension. You will get uevent notifications for the motion-plus 26 + device then. 27 + 28 + What: /sys/bus/hid/drivers/wiimote/<dev>/devtype 29 + Date: May 2013 30 + KernelVersion: 3.11 31 + Contact: David Herrmann <dh.herrmann@gmail.com> 32 + Description: While a device is initialized by the wiimote driver, we perform 33 + a device detection and signal a "change" uevent after it is 34 + done. This file shows the detected device type. "pending" means 35 + that the detection is still ongoing, "unknown" means, that the 36 + device couldn't be detected or loaded. "generic" means, that the 37 + device couldn't be detected but supports basic Wii Remote 38 + features and can be used. 39 + Other strings for each device-type are available and may be 40 + added if new device-specific detections are added. 41 + Currently supported are: 42 + gen10: First Wii Remote generation 43 + gen20: Second Wii Remote Plus generation (builtin MP) 44 + balanceboard: Wii Balance Board 45 + 46 + What: /sys/bus/hid/drivers/wiimote/<dev>/bboard_calib 47 + Date: May 2013 48 + KernelVersion: 3.11 49 + Contact: David Herrmann <dh.herrmann@gmail.com> 50 + Description: This attribute is only provided if the device was detected as a 51 + balance board. It provides a single line with 3 calibration 52 + values for all 4 sensors. The values are separated by colons and 53 + are each 2 bytes long (encoded as 4 digit hexadecimal value). 54 + First, 0kg values for all 4 sensors are written, followed by the 55 + 17kg values for all 4 sensors and last the 34kg values for all 4 56 + sensors. 57 + Calibration data is already applied by the kernel to all input 58 + values but may be used by user-space to perform other 59 + transformations.
+41 -22
drivers/hid/Kconfig
··· 217 217 ---help--- 218 218 Support for the ELECOM BM084 (bluetooth mouse). 219 219 220 + config HID_ELO 221 + tristate "ELO USB 4000/4500 touchscreen" 222 + depends on USB_HID 223 + ---help--- 224 + Support for the ELO USB 4000/4500 touchscreens. Note that this is for 225 + different devices than those handled by CONFIG_TOUCHSCREEN_USB_ELO. 226 + 220 227 config HID_EZKEY 221 228 tristate "Ezkey BTC 8193 keyboard" if EXPERT 222 229 depends on HID ··· 238 231 Support for Holtek based devices: 239 232 - Holtek On Line Grip based game controller 240 233 - Trust GXT 18 Gaming Keyboard 234 + - Sharkoon Drakonia / Perixx MX-2000 gaming mice 235 + - Tracer Sniper TRM-503 / NOVA Gaming Slider X200 / 236 + Zalman ZM-GM1 241 237 242 238 config HOLTEK_FF 243 239 bool "Holtek On Line Grip force feedback support" ··· 249 239 ---help--- 250 240 Say Y here if you have a Holtek On Line Grip based game controller 251 241 and want to have force feedback support for it. 242 + 243 + config HID_HUION 244 + tristate "Huion tablets" 245 + depends on USB_HID 246 + ---help--- 247 + Support for Huion 580 tablet. 252 248 253 249 config HID_KEYTOUCH 254 250 tristate "Keytouch HID devices" ··· 577 561 Support for Primax devices that are not fully compliant with the 578 562 HID standard. 579 563 580 - config HID_PS3REMOTE 581 - tristate "Sony PS3 BD Remote Control" 582 - depends on HID 583 - ---help--- 584 - Support for the Sony PS3 Blue-ray Disk Remote Control and Logitech 585 - Harmony Adapter for PS3, which connect over Bluetooth. 586 - 587 - Support for the 6-axis controllers is provided by HID_SONY. 588 - 589 564 config HID_ROCCAT 590 565 tristate "Roccat device support" 591 566 depends on USB_HID ··· 601 594 Support for Samsung InfraRed remote control or keyboards. 602 595 603 596 config HID_SONY 604 - tristate "Sony PS3 controller" 597 + tristate "Sony PS2/3 accessories" 605 598 depends on USB_HID 599 + depends on NEW_LEDS 600 + depends on LEDS_CLASS 606 601 ---help--- 607 - Support for Sony PS3 6-axis controllers. 602 + Support for 608 603 609 - Support for the Sony PS3 BD Remote is provided by HID_PS3REMOTE. 604 + * Sony PS3 6-axis controllers 605 + * Buzz controllers 606 + * Sony PS3 Blue-ray Disk Remote Control (Bluetooth) 607 + * Logitech Harmony adapter for Sony Playstation 3 (Bluetooth) 610 608 611 609 config HID_SPEEDLINK 612 610 tristate "Speedlink VAD Cezanne mouse support" ··· 719 707 Support for Wacom Graphire Bluetooth and Intuos4 WL tablets. 720 708 721 709 config HID_WIIMOTE 722 - tristate "Nintendo Wii Remote support" 710 + tristate "Nintendo Wii / Wii U peripherals" 723 711 depends on HID 724 712 depends on LEDS_CLASS 725 713 select POWER_SUPPLY 726 714 select INPUT_FF_MEMLESS 727 715 ---help--- 728 - Support for the Nintendo Wii Remote bluetooth device. 716 + Support for Nintendo Wii and Wii U Bluetooth peripherals. Supported 717 + devices are the Wii Remote and its extension devices, but also devices 718 + based on the Wii Remote like the Wii U Pro Controller or the 719 + Wii Balance Board. 729 720 730 - config HID_WIIMOTE_EXT 731 - bool "Nintendo Wii Remote Extension support" 732 - depends on HID_WIIMOTE 733 - default HID_WIIMOTE 734 - ---help--- 735 - Support for extension controllers of the Nintendo Wii Remote. Say yes 736 - here if you want to use the Nintendo Motion+, Nunchuck or Classic 737 - extension controllers with your Wii Remote. 721 + Support for all official Nintendo extensions is available, however, 3rd 722 + party extensions might not be supported. Please report these devices to: 723 + http://github.com/dvdhrm/xwiimote/issues 724 + 725 + Other Nintendo Wii U peripherals that are IEEE 802.11 based (including 726 + the Wii U Gamepad) might be supported in the future. But currently 727 + support is limited to Bluetooth based devices. 728 + 729 + If unsure, say N. 730 + 731 + To compile this driver as a module, choose M here: the 732 + module will be called hid-wiimote. 738 733 739 734 config HID_ZEROPLUS 740 735 tristate "Zeroplus based game controller support"
+4 -5
drivers/hid/Makefile
··· 28 28 hid-logitech-y += hid-lg4ff.o 29 29 endif 30 30 31 - hid-wiimote-y := hid-wiimote-core.o 32 - ifdef CONFIG_HID_WIIMOTE_EXT 33 - hid-wiimote-y += hid-wiimote-ext.o 34 - endif 31 + hid-wiimote-y := hid-wiimote-core.o hid-wiimote-modules.o 35 32 ifdef CONFIG_DEBUG_FS 36 33 hid-wiimote-y += hid-wiimote-debug.o 37 34 endif ··· 45 48 obj-$(CONFIG_HID_DRAGONRISE) += hid-dr.o 46 49 obj-$(CONFIG_HID_EMS_FF) += hid-emsff.o 47 50 obj-$(CONFIG_HID_ELECOM) += hid-elecom.o 51 + obj-$(CONFIG_HID_ELO) += hid-elo.o 48 52 obj-$(CONFIG_HID_EZKEY) += hid-ezkey.o 49 53 obj-$(CONFIG_HID_GYRATION) += hid-gyration.o 50 54 obj-$(CONFIG_HID_HOLTEK) += hid-holtek-kbd.o 55 + obj-$(CONFIG_HID_HOLTEK) += hid-holtek-mouse.o 51 56 obj-$(CONFIG_HID_HOLTEK) += hid-holtekff.o 57 + obj-$(CONFIG_HID_HUION) += hid-huion.o 52 58 obj-$(CONFIG_HID_HYPERV_MOUSE) += hid-hyperv.o 53 59 obj-$(CONFIG_HID_ICADE) += hid-icade.o 54 60 obj-$(CONFIG_HID_KENSINGTON) += hid-kensington.o ··· 92 92 endif 93 93 94 94 obj-$(CONFIG_HID_PRIMAX) += hid-primax.o 95 - obj-$(CONFIG_HID_PS3REMOTE) += hid-ps3remote.o 96 95 obj-$(CONFIG_HID_ROCCAT) += hid-roccat.o hid-roccat-common.o \ 97 96 hid-roccat-arvo.o hid-roccat-isku.o hid-roccat-kone.o \ 98 97 hid-roccat-koneplus.o hid-roccat-konepure.o hid-roccat-kovaplus.o \
+11 -1
drivers/hid/hid-core.c
··· 1293 1293 1294 1294 if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) { 1295 1295 ret = hdrv->raw_event(hid, report, data, size); 1296 - if (ret != 0) { 1296 + if (ret < 0) { 1297 1297 ret = ret < 0 ? ret : 0; 1298 1298 goto unlock; 1299 1299 } ··· 1573 1573 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006) }, 1574 1574 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011) }, 1575 1575 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM, USB_DEVICE_ID_ELECOM_BM084) }, 1576 + { HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0009) }, 1577 + { HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0030) }, 1576 1578 { HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) }, 1577 1579 { HID_USB_DEVICE(USB_VENDOR_ID_EZKEY, USB_DEVICE_ID_BTC_8193) }, 1578 1580 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR) }, ··· 1586 1584 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) }, 1587 1585 { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) }, 1588 1586 { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) }, 1587 + { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A04A) }, 1588 + { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A067) }, 1589 + { HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_580) }, 1589 1590 { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD) }, 1590 1591 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ION, USB_DEVICE_ID_ICADE) }, 1591 1592 { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) }, 1592 1593 { HID_USB_DEVICE(USB_VENDOR_ID_KEYTOUCH, USB_DEVICE_ID_KEYTOUCH_IEC) }, 1594 + { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE) }, 1593 1595 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) }, 1594 1596 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_EASYPEN_I405X) }, 1595 1597 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_MOUSEPEN_I608X) }, ··· 1686 1680 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_IR_REMOTE) }, 1687 1681 { HID_USB_DEVICE(USB_VENDOR_ID_SAMSUNG, USB_DEVICE_ID_SAMSUNG_WIRELESS_KBD_MOUSE) }, 1688 1682 { HID_USB_DEVICE(USB_VENDOR_ID_SKYCABLE, USB_DEVICE_ID_SKYCABLE_WIRELESS_PRESENTER) }, 1683 + { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER) }, 1684 + { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER) }, 1689 1685 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE) }, 1690 1686 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER) }, 1691 1687 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER) }, ··· 2050 2042 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1006) }, 2051 2043 { HID_USB_DEVICE(USB_VENDOR_ID_GTCO, USB_DEVICE_ID_GTCO_1007) }, 2052 2044 { HID_USB_DEVICE(USB_VENDOR_ID_IMATION, USB_DEVICE_ID_DISC_STAKKA) }, 2045 + { HID_USB_DEVICE(USB_VENDOR_ID_JABRA, USB_DEVICE_ID_JABRA_SPEAK_410) }, 2046 + { HID_USB_DEVICE(USB_VENDOR_ID_JABRA, USB_DEVICE_ID_JABRA_SPEAK_510) }, 2053 2047 { HID_USB_DEVICE(USB_VENDOR_ID_KBGEAR, USB_DEVICE_ID_KBGEAR_JAMSTUDIO) }, 2054 2048 { HID_USB_DEVICE(USB_VENDOR_ID_KWORLD, USB_DEVICE_ID_KWORLD_RADIO_FM700) }, 2055 2049 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_GPEN_560) },
+273
drivers/hid/hid-elo.c
··· 1 + /* 2 + * HID driver for ELO usb touchscreen 4000/4500 3 + * 4 + * Copyright (c) 2013 Jiri Slaby 5 + * 6 + * Data parsing taken from elousb driver by Vojtech Pavlik. 7 + * 8 + * This driver is licensed under the terms of GPLv2. 9 + */ 10 + 11 + #include <linux/hid.h> 12 + #include <linux/input.h> 13 + #include <linux/module.h> 14 + #include <linux/usb.h> 15 + #include <linux/workqueue.h> 16 + 17 + #include "hid-ids.h" 18 + 19 + #define ELO_PERIODIC_READ_INTERVAL HZ 20 + #define ELO_SMARTSET_CMD_TIMEOUT 2000 /* msec */ 21 + 22 + /* Elo SmartSet commands */ 23 + #define ELO_FLUSH_SMARTSET_RESPONSES 0x02 /* Flush all pending smartset responses */ 24 + #define ELO_SEND_SMARTSET_COMMAND 0x05 /* Send a smartset command */ 25 + #define ELO_GET_SMARTSET_RESPONSE 0x06 /* Get a smartset response */ 26 + #define ELO_DIAG 0x64 /* Diagnostics command */ 27 + #define ELO_SMARTSET_PACKET_SIZE 8 28 + 29 + struct elo_priv { 30 + struct usb_device *usbdev; 31 + struct delayed_work work; 32 + unsigned char buffer[ELO_SMARTSET_PACKET_SIZE]; 33 + }; 34 + 35 + static struct workqueue_struct *wq; 36 + static bool use_fw_quirk = true; 37 + module_param(use_fw_quirk, bool, S_IRUGO); 38 + MODULE_PARM_DESC(use_fw_quirk, "Do periodic pokes for broken M firmwares (default = true)"); 39 + 40 + static void elo_input_configured(struct hid_device *hdev, 41 + struct hid_input *hidinput) 42 + { 43 + struct input_dev *input = hidinput->input; 44 + 45 + set_bit(BTN_TOUCH, input->keybit); 46 + set_bit(ABS_PRESSURE, input->absbit); 47 + input_set_abs_params(input, ABS_PRESSURE, 0, 256, 0, 0); 48 + } 49 + 50 + static void elo_process_data(struct input_dev *input, const u8 *data, int size) 51 + { 52 + int press; 53 + 54 + input_report_abs(input, ABS_X, (data[3] << 8) | data[2]); 55 + input_report_abs(input, ABS_Y, (data[5] << 8) | data[4]); 56 + 57 + press = 0; 58 + if (data[1] & 0x80) 59 + press = (data[7] << 8) | data[6]; 60 + input_report_abs(input, ABS_PRESSURE, press); 61 + 62 + if (data[1] & 0x03) { 63 + input_report_key(input, BTN_TOUCH, 1); 64 + input_sync(input); 65 + } 66 + 67 + if (data[1] & 0x04) 68 + input_report_key(input, BTN_TOUCH, 0); 69 + 70 + input_sync(input); 71 + } 72 + 73 + static int elo_raw_event(struct hid_device *hdev, struct hid_report *report, 74 + u8 *data, int size) 75 + { 76 + struct hid_input *hidinput; 77 + 78 + if (!(hdev->claimed & HID_CLAIMED_INPUT) || list_empty(&hdev->inputs)) 79 + return 0; 80 + 81 + hidinput = list_first_entry(&hdev->inputs, struct hid_input, list); 82 + 83 + switch (report->id) { 84 + case 0: 85 + if (data[0] == 'T') { /* Mandatory ELO packet marker */ 86 + elo_process_data(hidinput->input, data, size); 87 + return 1; 88 + } 89 + break; 90 + default: /* unknown report */ 91 + /* Unknown report type; pass upstream */ 92 + hid_info(hdev, "unknown report type %d\n", report->id); 93 + break; 94 + } 95 + 96 + return 0; 97 + } 98 + 99 + static int elo_smartset_send_get(struct usb_device *dev, u8 command, 100 + void *data) 101 + { 102 + unsigned int pipe; 103 + u8 dir; 104 + 105 + if (command == ELO_SEND_SMARTSET_COMMAND) { 106 + pipe = usb_sndctrlpipe(dev, 0); 107 + dir = USB_DIR_OUT; 108 + } else if (command == ELO_GET_SMARTSET_RESPONSE) { 109 + pipe = usb_rcvctrlpipe(dev, 0); 110 + dir = USB_DIR_IN; 111 + } else 112 + return -EINVAL; 113 + 114 + return usb_control_msg(dev, pipe, command, 115 + dir | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 116 + 0, 0, data, ELO_SMARTSET_PACKET_SIZE, 117 + ELO_SMARTSET_CMD_TIMEOUT); 118 + } 119 + 120 + static int elo_flush_smartset_responses(struct usb_device *dev) 121 + { 122 + return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 123 + ELO_FLUSH_SMARTSET_RESPONSES, 124 + USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 125 + 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 126 + } 127 + 128 + static void elo_work(struct work_struct *work) 129 + { 130 + struct elo_priv *priv = container_of(work, struct elo_priv, work.work); 131 + struct usb_device *dev = priv->usbdev; 132 + unsigned char *buffer = priv->buffer; 133 + int ret; 134 + 135 + ret = elo_flush_smartset_responses(dev); 136 + if (ret < 0) { 137 + dev_err(&dev->dev, "initial FLUSH_SMARTSET_RESPONSES failed, error %d\n", 138 + ret); 139 + goto fail; 140 + } 141 + 142 + /* send Diagnostics command */ 143 + *buffer = ELO_DIAG; 144 + ret = elo_smartset_send_get(dev, ELO_SEND_SMARTSET_COMMAND, buffer); 145 + if (ret < 0) { 146 + dev_err(&dev->dev, "send Diagnostics Command failed, error %d\n", 147 + ret); 148 + goto fail; 149 + } 150 + 151 + /* get the result */ 152 + ret = elo_smartset_send_get(dev, ELO_GET_SMARTSET_RESPONSE, buffer); 153 + if (ret < 0) { 154 + dev_err(&dev->dev, "get Diagnostics Command response failed, error %d\n", 155 + ret); 156 + goto fail; 157 + } 158 + 159 + /* read the ack */ 160 + if (*buffer != 'A') { 161 + ret = elo_smartset_send_get(dev, ELO_GET_SMARTSET_RESPONSE, 162 + buffer); 163 + if (ret < 0) { 164 + dev_err(&dev->dev, "get acknowledge response failed, error %d\n", 165 + ret); 166 + goto fail; 167 + } 168 + } 169 + 170 + fail: 171 + ret = elo_flush_smartset_responses(dev); 172 + if (ret < 0) 173 + dev_err(&dev->dev, "final FLUSH_SMARTSET_RESPONSES failed, error %d\n", 174 + ret); 175 + queue_delayed_work(wq, &priv->work, ELO_PERIODIC_READ_INTERVAL); 176 + } 177 + 178 + /* 179 + * Not all Elo devices need the periodic HID descriptor reads. 180 + * Only firmware version M needs this. 181 + */ 182 + static bool elo_broken_firmware(struct usb_device *dev) 183 + { 184 + return use_fw_quirk && le16_to_cpu(dev->descriptor.bcdDevice) == 0x10d; 185 + } 186 + 187 + static int elo_probe(struct hid_device *hdev, const struct hid_device_id *id) 188 + { 189 + struct elo_priv *priv; 190 + int ret; 191 + 192 + priv = kzalloc(sizeof(*priv), GFP_KERNEL); 193 + if (!priv) 194 + return -ENOMEM; 195 + 196 + INIT_DELAYED_WORK(&priv->work, elo_work); 197 + priv->usbdev = interface_to_usbdev(to_usb_interface(hdev->dev.parent)); 198 + 199 + hid_set_drvdata(hdev, priv); 200 + 201 + ret = hid_parse(hdev); 202 + if (ret) { 203 + hid_err(hdev, "parse failed\n"); 204 + goto err_free; 205 + } 206 + 207 + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 208 + if (ret) { 209 + hid_err(hdev, "hw start failed\n"); 210 + goto err_free; 211 + } 212 + 213 + if (elo_broken_firmware(priv->usbdev)) { 214 + hid_info(hdev, "broken firmware found, installing workaround\n"); 215 + queue_delayed_work(wq, &priv->work, ELO_PERIODIC_READ_INTERVAL); 216 + } 217 + 218 + return 0; 219 + err_free: 220 + kfree(priv); 221 + return ret; 222 + } 223 + 224 + static void elo_remove(struct hid_device *hdev) 225 + { 226 + struct elo_priv *priv = hid_get_drvdata(hdev); 227 + 228 + hid_hw_stop(hdev); 229 + flush_workqueue(wq); 230 + kfree(priv); 231 + } 232 + 233 + static const struct hid_device_id elo_devices[] = { 234 + { HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0009), }, 235 + { HID_USB_DEVICE(USB_VENDOR_ID_ELO, 0x0030), }, 236 + { } 237 + }; 238 + MODULE_DEVICE_TABLE(hid, elo_devices); 239 + 240 + static struct hid_driver elo_driver = { 241 + .name = "elo", 242 + .id_table = elo_devices, 243 + .probe = elo_probe, 244 + .remove = elo_remove, 245 + .raw_event = elo_raw_event, 246 + .input_configured = elo_input_configured, 247 + }; 248 + 249 + static int __init elo_driver_init(void) 250 + { 251 + int ret; 252 + 253 + wq = create_singlethread_workqueue("elousb"); 254 + if (!wq) 255 + return -ENOMEM; 256 + 257 + ret = hid_register_driver(&elo_driver); 258 + if (ret) 259 + destroy_workqueue(wq); 260 + 261 + return ret; 262 + } 263 + module_init(elo_driver_init); 264 + 265 + static void __exit elo_driver_exit(void) 266 + { 267 + hid_unregister_driver(&elo_driver); 268 + destroy_workqueue(wq); 269 + } 270 + module_exit(elo_driver_exit); 271 + 272 + MODULE_AUTHOR("Jiri Slaby <jslaby@suse.cz>"); 273 + MODULE_LICENSE("GPL");
+77
drivers/hid/hid-holtek-mouse.c
··· 1 + /* 2 + * HID driver for Holtek gaming mice 3 + * Copyright (c) 2013 Christian Ohm 4 + * Heavily inspired by various other HID drivers that adjust the report 5 + * descriptor. 6 + */ 7 + 8 + /* 9 + * This program is free software; you can redistribute it and/or modify it 10 + * under the terms of the GNU General Public License as published by the Free 11 + * Software Foundation; either version 2 of the License, or (at your option) 12 + * any later version. 13 + */ 14 + 15 + #include <linux/hid.h> 16 + #include <linux/module.h> 17 + #include <linux/usb.h> 18 + 19 + #include "hid-ids.h" 20 + 21 + /* 22 + * The report descriptor of some Holtek based gaming mice specifies an 23 + * excessively large number of consumer usages (2^15), which is more than 24 + * HID_MAX_USAGES. This prevents proper parsing of the report descriptor. 25 + * 26 + * This driver fixes the report descriptor for: 27 + * - USB ID 04d9:a067, sold as Sharkoon Drakonia and Perixx MX-2000 28 + * - USB ID 04d9:a04a, sold as Tracer Sniper TRM-503, NOVA Gaming Slider X200 29 + * and Zalman ZM-GM1 30 + */ 31 + 32 + static __u8 *holtek_mouse_report_fixup(struct hid_device *hdev, __u8 *rdesc, 33 + unsigned int *rsize) 34 + { 35 + struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 36 + 37 + if (intf->cur_altsetting->desc.bInterfaceNumber == 1) { 38 + /* Change usage maximum and logical maximum from 0x7fff to 39 + * 0x2fff, so they don't exceed HID_MAX_USAGES */ 40 + switch (hdev->product) { 41 + case USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A067: 42 + if (*rsize >= 122 && rdesc[115] == 0xff && rdesc[116] == 0x7f 43 + && rdesc[120] == 0xff && rdesc[121] == 0x7f) { 44 + hid_info(hdev, "Fixing up report descriptor\n"); 45 + rdesc[116] = rdesc[121] = 0x2f; 46 + } 47 + break; 48 + case USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A04A: 49 + if (*rsize >= 113 && rdesc[106] == 0xff && rdesc[107] == 0x7f 50 + && rdesc[111] == 0xff && rdesc[112] == 0x7f) { 51 + hid_info(hdev, "Fixing up report descriptor\n"); 52 + rdesc[107] = rdesc[112] = 0x2f; 53 + } 54 + break; 55 + } 56 + 57 + } 58 + return rdesc; 59 + } 60 + 61 + static const struct hid_device_id holtek_mouse_devices[] = { 62 + { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, 63 + USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A067) }, 64 + { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT, 65 + USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A04A) }, 66 + { } 67 + }; 68 + MODULE_DEVICE_TABLE(hid, holtek_mouse_devices); 69 + 70 + static struct hid_driver holtek_mouse_driver = { 71 + .name = "holtek_mouse", 72 + .id_table = holtek_mouse_devices, 73 + .report_fixup = holtek_mouse_report_fixup, 74 + }; 75 + 76 + module_hid_driver(holtek_mouse_driver); 77 + MODULE_LICENSE("GPL");
+177
drivers/hid/hid-huion.c
··· 1 + /* 2 + * HID driver for Huion devices not fully compliant with HID standard 3 + * 4 + * Copyright (c) 2013 Martin Rusko 5 + */ 6 + 7 + /* 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms of the GNU General Public License as published by the Free 10 + * Software Foundation; either version 2 of the License, or (at your option) 11 + * any later version. 12 + */ 13 + 14 + #include <linux/device.h> 15 + #include <linux/hid.h> 16 + #include <linux/module.h> 17 + #include <linux/usb.h> 18 + #include "usbhid/usbhid.h" 19 + 20 + #include "hid-ids.h" 21 + 22 + /* Original Huion 580 report descriptor size */ 23 + #define HUION_580_RDESC_ORIG_SIZE 177 24 + 25 + /* Fixed Huion 580 report descriptor */ 26 + static __u8 huion_580_rdesc_fixed[] = { 27 + 0x05, 0x0D, /* Usage Page (Digitizer), */ 28 + 0x09, 0x02, /* Usage (Pen), */ 29 + 0xA1, 0x01, /* Collection (Application), */ 30 + 0x85, 0x07, /* Report ID (7), */ 31 + 0x09, 0x20, /* Usage (Stylus), */ 32 + 0xA0, /* Collection (Physical), */ 33 + 0x14, /* Logical Minimum (0), */ 34 + 0x25, 0x01, /* Logical Maximum (1), */ 35 + 0x75, 0x01, /* Report Size (1), */ 36 + 0x09, 0x42, /* Usage (Tip Switch), */ 37 + 0x09, 0x44, /* Usage (Barrel Switch), */ 38 + 0x09, 0x46, /* Usage (Tablet Pick), */ 39 + 0x95, 0x03, /* Report Count (3), */ 40 + 0x81, 0x02, /* Input (Variable), */ 41 + 0x95, 0x03, /* Report Count (3), */ 42 + 0x81, 0x03, /* Input (Constant, Variable), */ 43 + 0x09, 0x32, /* Usage (In Range), */ 44 + 0x95, 0x01, /* Report Count (1), */ 45 + 0x81, 0x02, /* Input (Variable), */ 46 + 0x95, 0x01, /* Report Count (1), */ 47 + 0x81, 0x03, /* Input (Constant, Variable), */ 48 + 0x75, 0x10, /* Report Size (16), */ 49 + 0x95, 0x01, /* Report Count (1), */ 50 + 0xA4, /* Push, */ 51 + 0x05, 0x01, /* Usage Page (Desktop), */ 52 + 0x65, 0x13, /* Unit (Inch), */ 53 + 0x55, 0xFD, /* Unit Exponent (-3), */ 54 + 0x34, /* Physical Minimum (0), */ 55 + 0x09, 0x30, /* Usage (X), */ 56 + 0x46, 0x40, 0x1F, /* Physical Maximum (8000), */ 57 + 0x26, 0x00, 0x7D, /* Logical Maximum (32000), */ 58 + 0x81, 0x02, /* Input (Variable), */ 59 + 0x09, 0x31, /* Usage (Y), */ 60 + 0x46, 0x88, 0x13, /* Physical Maximum (5000), */ 61 + 0x26, 0x20, 0x4E, /* Logical Maximum (20000), */ 62 + 0x81, 0x02, /* Input (Variable), */ 63 + 0xB4, /* Pop, */ 64 + 0x09, 0x30, /* Usage (Tip Pressure), */ 65 + 0x26, 0xFF, 0x07, /* Logical Maximum (2047), */ 66 + 0x81, 0x02, /* Input (Variable), */ 67 + 0xC0, /* End Collection, */ 68 + 0xC0 /* End Collection */ 69 + }; 70 + 71 + static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc, 72 + unsigned int *rsize) 73 + { 74 + switch (hdev->product) { 75 + case USB_DEVICE_ID_HUION_580: 76 + if (*rsize == HUION_580_RDESC_ORIG_SIZE) { 77 + rdesc = huion_580_rdesc_fixed; 78 + *rsize = sizeof(huion_580_rdesc_fixed); 79 + } 80 + break; 81 + } 82 + return rdesc; 83 + } 84 + 85 + /** 86 + * Enable fully-functional tablet mode by reading special string 87 + * descriptor. 88 + * 89 + * @hdev: HID device 90 + * 91 + * The specific string descriptor and data were discovered by sniffing 92 + * the Windows driver traffic. 93 + */ 94 + static int huion_tablet_enable(struct hid_device *hdev) 95 + { 96 + int rc; 97 + char buf[22]; 98 + 99 + rc = usb_string(hid_to_usb_dev(hdev), 0x64, buf, sizeof(buf)); 100 + if (rc < 0) 101 + return rc; 102 + 103 + return 0; 104 + } 105 + 106 + static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id) 107 + { 108 + int ret; 109 + struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 110 + 111 + /* Ignore interfaces 1 (mouse) and 2 (keyboard) for Huion 580 tablet, 112 + * as they are not used 113 + */ 114 + switch (id->product) { 115 + case USB_DEVICE_ID_HUION_580: 116 + if (intf->cur_altsetting->desc.bInterfaceNumber != 0x00) 117 + return -ENODEV; 118 + break; 119 + } 120 + 121 + ret = hid_parse(hdev); 122 + if (ret) { 123 + hid_err(hdev, "parse failed\n"); 124 + goto err; 125 + } 126 + 127 + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 128 + if (ret) { 129 + hid_err(hdev, "hw start failed\n"); 130 + goto err; 131 + } 132 + 133 + switch (id->product) { 134 + case USB_DEVICE_ID_HUION_580: 135 + ret = huion_tablet_enable(hdev); 136 + if (ret) { 137 + hid_err(hdev, "tablet enabling failed\n"); 138 + goto enabling_err; 139 + } 140 + break; 141 + } 142 + 143 + return 0; 144 + enabling_err: 145 + hid_hw_stop(hdev); 146 + err: 147 + return ret; 148 + } 149 + 150 + static int huion_raw_event(struct hid_device *hdev, struct hid_report *report, 151 + u8 *data, int size) 152 + { 153 + /* If this is a pen input report then invert the in-range bit */ 154 + if (report->type == HID_INPUT_REPORT && report->id == 0x07 && size >= 2) 155 + data[1] ^= 0x40; 156 + 157 + return 0; 158 + } 159 + 160 + static const struct hid_device_id huion_devices[] = { 161 + { HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_580) }, 162 + { } 163 + }; 164 + MODULE_DEVICE_TABLE(hid, huion_devices); 165 + 166 + static struct hid_driver huion_driver = { 167 + .name = "huion", 168 + .id_table = huion_devices, 169 + .probe = huion_probe, 170 + .report_fixup = huion_report_fixup, 171 + .raw_event = huion_raw_event, 172 + }; 173 + module_hid_driver(huion_driver); 174 + 175 + MODULE_AUTHOR("Martin Rusko"); 176 + MODULE_DESCRIPTION("Huion HID driver"); 177 + MODULE_LICENSE("GPL");
+1 -3
drivers/hid/hid-hyperv.c
··· 199 199 if (desc->bLength == 0) 200 200 goto cleanup; 201 201 202 - input_device->hid_desc = kzalloc(desc->bLength, GFP_ATOMIC); 202 + input_device->hid_desc = kmemdup(desc, desc->bLength, GFP_ATOMIC); 203 203 204 204 if (!input_device->hid_desc) 205 205 goto cleanup; 206 - 207 - memcpy(input_device->hid_desc, desc, desc->bLength); 208 206 209 207 input_device->report_desc_size = desc->desc[0].wDescriptorLength; 210 208 if (input_device->report_desc_size == 0) {
+18 -4
drivers/hid/hid-ids.h
··· 248 248 #define USB_DEVICE_ID_CYPRESS_BARCODE_4 0xed81 249 249 #define USB_DEVICE_ID_CYPRESS_TRUETOUCH 0xc001 250 250 251 + #define USB_VENDOR_ID_DATA_MODUL 0x7374 252 + #define USB_VENDOR_ID_DATA_MODUL_EASYMAXTOUCH 0x1201 253 + 251 254 #define USB_VENDOR_ID_DEALEXTREAME 0x10c5 252 255 #define USB_DEVICE_ID_DEALEXTREAME_RADIO_SI4701 0x819a 253 256 ··· 275 272 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E 0x725e 276 273 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262 0x7262 277 274 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B 0x726b 278 - #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA 0x72aa 279 275 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1 0x72a1 276 + #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA 0x72aa 277 + #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4 0x72c4 278 + #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0 0x72d0 280 279 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA 0x72fa 281 280 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7302 0x7302 282 281 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7349 0x7349 283 282 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_73F7 0x73f7 284 283 #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001 0xa001 285 - #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224 0x7224 286 - #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0 0x72d0 287 - #define USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4 0x72c4 288 284 289 285 #define USB_VENDOR_ID_ELECOM 0x056e 290 286 #define USB_DEVICE_ID_ELECOM_BM084 0x0061 ··· 427 425 #define USB_DEVICE_ID_UGCI_FLYING 0x0020 428 426 #define USB_DEVICE_ID_UGCI_FIGHTING 0x0030 429 427 428 + #define USB_VENDOR_ID_HUION 0x256c 429 + #define USB_DEVICE_ID_HUION_580 0x006e 430 + 430 431 #define USB_VENDOR_ID_IDEACOM 0x1cb6 431 432 #define USB_DEVICE_ID_IDEACOM_IDC6650 0x6650 432 433 #define USB_DEVICE_ID_IDEACOM_IDC6651 0x6651 ··· 445 440 446 441 #define USB_VENDOR_ID_HOLTEK_ALT 0x04d9 447 442 #define USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD 0xa055 443 + #define USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A067 0xa067 444 + #define USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A04A 0xa04a 448 445 449 446 #define USB_VENDOR_ID_IMATION 0x0718 450 447 #define USB_DEVICE_ID_DISC_STAKKA 0xd000 451 448 452 449 #define USB_VENDOR_ID_IRTOUCHSYSTEMS 0x6615 453 450 #define USB_DEVICE_ID_IRTOUCH_INFRARED_USB 0x0070 451 + 452 + #define USB_VENDOR_ID_JABRA 0x0b0e 453 + #define USB_DEVICE_ID_JABRA_SPEAK_410 0x0412 454 + #define USB_DEVICE_ID_JABRA_SPEAK_510 0x0420 454 455 455 456 #define USB_VENDOR_ID_JESS 0x0c45 456 457 #define USB_DEVICE_ID_JESS_YUREX 0x1010 ··· 478 467 479 468 #define USB_VENDOR_ID_KYE 0x0458 480 469 #define USB_DEVICE_ID_KYE_ERGO_525V 0x0087 470 + #define USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE 0x0138 481 471 #define USB_DEVICE_ID_KYE_GPEN_560 0x5003 482 472 #define USB_DEVICE_ID_KYE_EASYPEN_I405X 0x5010 483 473 #define USB_DEVICE_ID_KYE_MOUSEPEN_I608X 0x5011 ··· 746 734 #define USB_DEVICE_ID_SONY_PS3_BDREMOTE 0x0306 747 735 #define USB_DEVICE_ID_SONY_PS3_CONTROLLER 0x0268 748 736 #define USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER 0x042f 737 + #define USB_DEVICE_ID_SONY_BUZZ_CONTROLLER 0x0002 738 + #define USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER 0x1000 749 739 750 740 #define USB_VENDOR_ID_SOUNDGRAPH 0x15c2 751 741 #define USB_DEVICE_ID_SOUNDGRAPH_IMON_FIRST 0x0034
+8 -3
drivers/hid/hid-input.c
··· 354 354 dev->battery_report_type); 355 355 356 356 if (ret != 2) { 357 - if (ret >= 0) 358 - ret = -EINVAL; 357 + ret = -ENODATA; 359 358 break; 360 359 } 360 + ret = 0; 361 361 362 362 if (dev->battery_min < dev->battery_max && 363 363 buf[1] >= dev->battery_min && ··· 1042 1042 1043 1043 /* 1044 1044 * Ignore out-of-range values as per HID specification, 1045 - * section 5.10 and 6.2.25 1045 + * section 5.10 and 6.2.25. 1046 + * 1047 + * The logical_minimum < logical_maximum check is done so that we 1048 + * don't unintentionally discard values sent by devices which 1049 + * don't specify logical min and max. 1046 1050 */ 1047 1051 if ((field->flags & HID_MAIN_ITEM_VARIABLE) && 1052 + (field->logical_minimum < field->logical_maximum) && 1048 1053 (value < field->logical_minimum || 1049 1054 value > field->logical_maximum)) { 1050 1055 dbg_hid("Ignoring out-of-range value %x\n", value);
+21
drivers/hid/hid-kye.c
··· 314 314 *rsize = sizeof(easypen_m610x_rdesc_fixed); 315 315 } 316 316 break; 317 + case USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE: 318 + /* 319 + * the fixup that need to be done: 320 + * - change Usage Maximum in the Comsumer Control 321 + * (report ID 3) to a reasonable value 322 + */ 323 + if (*rsize >= 135 && 324 + /* Usage Page (Consumer Devices) */ 325 + rdesc[104] == 0x05 && rdesc[105] == 0x0c && 326 + /* Usage (Consumer Control) */ 327 + rdesc[106] == 0x09 && rdesc[107] == 0x01 && 328 + /* Usage Maximum > 12287 */ 329 + rdesc[114] == 0x2a && rdesc[116] > 0x2f) { 330 + hid_info(hdev, 331 + "fixing up Genius Gila Gaming Mouse " 332 + "report descriptor\n"); 333 + rdesc[116] = 0x2f; 334 + } 335 + break; 317 336 } 318 337 return rdesc; 319 338 } ··· 426 407 USB_DEVICE_ID_KYE_MOUSEPEN_I608X) }, 427 408 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, 428 409 USB_DEVICE_ID_KYE_EASYPEN_M610X) }, 410 + { HID_USB_DEVICE(USB_VENDOR_ID_KYE, 411 + USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE) }, 429 412 { } 430 413 }; 431 414 MODULE_DEVICE_TABLE(hid, kye_devices);
+18 -16
drivers/hid/hid-multitouch.c
··· 1111 1111 HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, 1112 1112 USB_DEVICE_ID_CYPRESS_TRUETOUCH) }, 1113 1113 1114 + /* Data Modul easyMaxTouch */ 1115 + { .driver_data = MT_CLS_DEFAULT, 1116 + MT_USB_DEVICE(USB_VENDOR_ID_DATA_MODUL, 1117 + USB_VENDOR_ID_DATA_MODUL_EASYMAXTOUCH) }, 1118 + 1114 1119 /* eGalax devices (resistive) */ 1115 1120 { .driver_data = MT_CLS_EGALAX, 1116 1121 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, ··· 1125 1120 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_480E) }, 1126 1121 1127 1122 /* eGalax devices (capacitive) */ 1128 - { .driver_data = MT_CLS_EGALAX, 1129 - MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1130 - USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) }, 1131 1123 { .driver_data = MT_CLS_EGALAX_SERIAL, 1132 1124 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1133 1125 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7207) }, 1134 - { .driver_data = MT_CLS_EGALAX_SERIAL, 1126 + { .driver_data = MT_CLS_EGALAX, 1135 1127 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1136 - USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) }, 1128 + USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_720C) }, 1137 1129 { .driver_data = MT_CLS_EGALAX_SERIAL, 1138 1130 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1139 1131 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) }, 1140 1132 { .driver_data = MT_CLS_EGALAX_SERIAL, 1141 1133 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1142 1134 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_722A) }, 1143 - { .driver_data = MT_CLS_EGALAX, 1135 + { .driver_data = MT_CLS_EGALAX_SERIAL, 1144 1136 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1145 - USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) }, 1137 + USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_725E) }, 1146 1138 { .driver_data = MT_CLS_EGALAX_SERIAL, 1147 1139 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1148 1140 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7262) }, 1141 + { .driver_data = MT_CLS_EGALAX, 1142 + MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1143 + USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_726B) }, 1149 1144 { .driver_data = MT_CLS_EGALAX, 1150 1145 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1151 1146 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72A1) }, 1152 1147 { .driver_data = MT_CLS_EGALAX_SERIAL, 1153 1148 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1154 1149 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72AA) }, 1150 + { .driver_data = MT_CLS_EGALAX, 1151 + HID_USB_DEVICE(USB_VENDOR_ID_DWAV, 1152 + USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) }, 1153 + { .driver_data = MT_CLS_EGALAX, 1154 + HID_USB_DEVICE(USB_VENDOR_ID_DWAV, 1155 + USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) }, 1155 1156 { .driver_data = MT_CLS_EGALAX, 1156 1157 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1157 1158 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72FA) }, ··· 1173 1162 { .driver_data = MT_CLS_EGALAX_SERIAL, 1174 1163 MT_USB_DEVICE(USB_VENDOR_ID_DWAV, 1175 1164 USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_A001) }, 1176 - { .driver_data = MT_CLS_EGALAX, 1177 - HID_USB_DEVICE(USB_VENDOR_ID_DWAV, 1178 - USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_7224) }, 1179 - { .driver_data = MT_CLS_EGALAX, 1180 - HID_USB_DEVICE(USB_VENDOR_ID_DWAV, 1181 - USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72D0) }, 1182 - { .driver_data = MT_CLS_EGALAX, 1183 - HID_USB_DEVICE(USB_VENDOR_ID_DWAV, 1184 - USB_DEVICE_ID_DWAV_EGALAX_MULTITOUCH_72C4) }, 1185 1165 1186 1166 /* Elo TouchSystems IntelliTouch Plus panel */ 1187 1167 { .driver_data = MT_CLS_DUAL_CONTACT_ID,
-204
drivers/hid/hid-ps3remote.c
··· 1 - /* 2 - * HID driver for Sony PS3 BD Remote Control 3 - * 4 - * Copyright (c) 2012 David Dillow <dave@thedillows.org> 5 - * Based on a blend of the bluez fakehid user-space code by Marcel Holtmann 6 - * and other kernel HID drivers. 7 - */ 8 - 9 - /* 10 - * This program is free software; you can redistribute it and/or modify it 11 - * under the terms of the GNU General Public License as published by the Free 12 - * Software Foundation; either version 2 of the License, or (at your option) 13 - * any later version. 14 - */ 15 - 16 - /* NOTE: in order for the Sony PS3 BD Remote Control to be found by 17 - * a Bluetooth host, the key combination Start+Enter has to be kept pressed 18 - * for about 7 seconds with the Bluetooth Host Controller in discovering mode. 19 - * 20 - * There will be no PIN request from the device. 21 - */ 22 - 23 - #include <linux/device.h> 24 - #include <linux/hid.h> 25 - #include <linux/module.h> 26 - 27 - #include "hid-ids.h" 28 - 29 - static __u8 ps3remote_rdesc[] = { 30 - 0x05, 0x01, /* GUsagePage Generic Desktop */ 31 - 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ 32 - 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */ 33 - 34 - /* Use collection 1 for joypad buttons */ 35 - 0xA1, 0x02, /* MCollection Logical (interrelated data) */ 36 - 37 - /* Ignore the 1st byte, maybe it is used for a controller 38 - * number but it's not needed for correct operation */ 39 - 0x75, 0x08, /* GReportSize 0x08 [8] */ 40 - 0x95, 0x01, /* GReportCount 0x01 [1] */ 41 - 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */ 42 - 43 - /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these 44 - * buttons multiple keypresses are allowed */ 45 - 0x05, 0x09, /* GUsagePage Button */ 46 - 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */ 47 - 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */ 48 - 0x14, /* GLogicalMinimum [0] */ 49 - 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */ 50 - 0x75, 0x01, /* GReportSize 0x01 [1] */ 51 - 0x95, 0x18, /* GReportCount 0x18 [24] */ 52 - 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */ 53 - 54 - 0xC0, /* MEndCollection */ 55 - 56 - /* Use collection 2 for remote control buttons */ 57 - 0xA1, 0x02, /* MCollection Logical (interrelated data) */ 58 - 59 - /* 5th byte is used for remote control buttons */ 60 - 0x05, 0x09, /* GUsagePage Button */ 61 - 0x18, /* LUsageMinimum [No button pressed] */ 62 - 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */ 63 - 0x14, /* GLogicalMinimum [0] */ 64 - 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */ 65 - 0x75, 0x08, /* GReportSize 0x08 [8] */ 66 - 0x95, 0x01, /* GReportCount 0x01 [1] */ 67 - 0x80, /* MInput */ 68 - 69 - /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at 70 - * 0xff and 11th is for press indication */ 71 - 0x75, 0x08, /* GReportSize 0x08 [8] */ 72 - 0x95, 0x06, /* GReportCount 0x06 [6] */ 73 - 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */ 74 - 75 - /* 12th byte is for battery strength */ 76 - 0x05, 0x06, /* GUsagePage Generic Device Controls */ 77 - 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */ 78 - 0x14, /* GLogicalMinimum [0] */ 79 - 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */ 80 - 0x75, 0x08, /* GReportSize 0x08 [8] */ 81 - 0x95, 0x01, /* GReportCount 0x01 [1] */ 82 - 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */ 83 - 84 - 0xC0, /* MEndCollection */ 85 - 86 - 0xC0 /* MEndCollection [Game Pad] */ 87 - }; 88 - 89 - static const unsigned int ps3remote_keymap_joypad_buttons[] = { 90 - [0x01] = KEY_SELECT, 91 - [0x02] = BTN_THUMBL, /* L3 */ 92 - [0x03] = BTN_THUMBR, /* R3 */ 93 - [0x04] = BTN_START, 94 - [0x05] = KEY_UP, 95 - [0x06] = KEY_RIGHT, 96 - [0x07] = KEY_DOWN, 97 - [0x08] = KEY_LEFT, 98 - [0x09] = BTN_TL2, /* L2 */ 99 - [0x0a] = BTN_TR2, /* R2 */ 100 - [0x0b] = BTN_TL, /* L1 */ 101 - [0x0c] = BTN_TR, /* R1 */ 102 - [0x0d] = KEY_OPTION, /* options/triangle */ 103 - [0x0e] = KEY_BACK, /* back/circle */ 104 - [0x0f] = BTN_0, /* cross */ 105 - [0x10] = KEY_SCREEN, /* view/square */ 106 - [0x11] = KEY_HOMEPAGE, /* PS button */ 107 - [0x14] = KEY_ENTER, 108 - }; 109 - static const unsigned int ps3remote_keymap_remote_buttons[] = { 110 - [0x00] = KEY_1, 111 - [0x01] = KEY_2, 112 - [0x02] = KEY_3, 113 - [0x03] = KEY_4, 114 - [0x04] = KEY_5, 115 - [0x05] = KEY_6, 116 - [0x06] = KEY_7, 117 - [0x07] = KEY_8, 118 - [0x08] = KEY_9, 119 - [0x09] = KEY_0, 120 - [0x0e] = KEY_ESC, /* return */ 121 - [0x0f] = KEY_CLEAR, 122 - [0x16] = KEY_EJECTCD, 123 - [0x1a] = KEY_MENU, /* top menu */ 124 - [0x28] = KEY_TIME, 125 - [0x30] = KEY_PREVIOUS, 126 - [0x31] = KEY_NEXT, 127 - [0x32] = KEY_PLAY, 128 - [0x33] = KEY_REWIND, /* scan back */ 129 - [0x34] = KEY_FORWARD, /* scan forward */ 130 - [0x38] = KEY_STOP, 131 - [0x39] = KEY_PAUSE, 132 - [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */ 133 - [0x60] = KEY_FRAMEBACK, /* slow/step back */ 134 - [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */ 135 - [0x63] = KEY_SUBTITLE, 136 - [0x64] = KEY_AUDIO, 137 - [0x65] = KEY_ANGLE, 138 - [0x70] = KEY_INFO, /* display */ 139 - [0x80] = KEY_BLUE, 140 - [0x81] = KEY_RED, 141 - [0x82] = KEY_GREEN, 142 - [0x83] = KEY_YELLOW, 143 - }; 144 - 145 - static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc, 146 - unsigned int *rsize) 147 - { 148 - *rsize = sizeof(ps3remote_rdesc); 149 - return ps3remote_rdesc; 150 - } 151 - 152 - static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi, 153 - struct hid_field *field, struct hid_usage *usage, 154 - unsigned long **bit, int *max) 155 - { 156 - unsigned int key = usage->hid & HID_USAGE; 157 - 158 - if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) 159 - return -1; 160 - 161 - switch (usage->collection_index) { 162 - case 1: 163 - if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons)) 164 - return -1; 165 - 166 - key = ps3remote_keymap_joypad_buttons[key]; 167 - if (!key) 168 - return -1; 169 - break; 170 - case 2: 171 - if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons)) 172 - return -1; 173 - 174 - key = ps3remote_keymap_remote_buttons[key]; 175 - if (!key) 176 - return -1; 177 - break; 178 - default: 179 - return -1; 180 - } 181 - 182 - hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); 183 - return 1; 184 - } 185 - 186 - static const struct hid_device_id ps3remote_devices[] = { 187 - /* PS3 BD Remote Control */ 188 - { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE) }, 189 - /* Logitech Harmony Adapter for PS3 */ 190 - { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3) }, 191 - { } 192 - }; 193 - MODULE_DEVICE_TABLE(hid, ps3remote_devices); 194 - 195 - static struct hid_driver ps3remote_driver = { 196 - .name = "ps3_remote", 197 - .id_table = ps3remote_devices, 198 - .report_fixup = ps3remote_fixup, 199 - .input_mapping = ps3remote_mapping, 200 - }; 201 - module_hid_driver(ps3remote_driver); 202 - 203 - MODULE_LICENSE("GPL"); 204 - MODULE_AUTHOR("David Dillow <dave@thedillows.org>, Antonio Ospite <ospite@studenti.unina.it>");
+13 -3
drivers/hid/hid-roccat.c
··· 366 366 mutex_lock(&devices_lock); 367 367 devices[minor] = NULL; 368 368 mutex_unlock(&devices_lock); 369 - 369 + 370 370 if (device->open) { 371 371 hid_hw_close(device->hid); 372 372 wake_up_interruptible(&device->wait); ··· 426 426 427 427 if (retval < 0) { 428 428 pr_warn("can't get major number\n"); 429 - return retval; 429 + goto error; 430 430 } 431 431 432 432 cdev_init(&roccat_cdev, &roccat_ops); 433 - cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES); 433 + retval = cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES); 434 434 435 + if (retval < 0) { 436 + pr_warn("cannot add cdev\n"); 437 + goto cleanup_alloc_chrdev_region; 438 + } 435 439 return 0; 440 + 441 + 442 + cleanup_alloc_chrdev_region: 443 + unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES); 444 + error: 445 + return retval; 436 446 } 437 447 438 448 static void __exit roccat_exit(void)
+462 -11
drivers/hid/hid-sony.c
··· 1 1 /* 2 - * HID driver for some sony "special" devices 2 + * HID driver for Sony / PS2 / PS3 BD devices. 3 3 * 4 4 * Copyright (c) 1999 Andreas Gal 5 5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> 6 6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc 7 7 * Copyright (c) 2008 Jiri Slaby 8 - * Copyright (c) 2006-2008 Jiri Kosina 8 + * Copyright (c) 2012 David Dillow <dave@thedillows.org> 9 + * Copyright (c) 2006-2013 Jiri Kosina 10 + * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com> 9 11 */ 10 12 11 13 /* ··· 17 15 * any later version. 18 16 */ 19 17 18 + /* NOTE: in order for the Sony PS3 BD Remote Control to be found by 19 + * a Bluetooth host, the key combination Start+Enter has to be kept pressed 20 + * for about 7 seconds with the Bluetooth Host Controller in discovering mode. 21 + * 22 + * There will be no PIN request from the device. 23 + */ 24 + 20 25 #include <linux/device.h> 21 26 #include <linux/hid.h> 22 27 #include <linux/module.h> 23 28 #include <linux/slab.h> 24 29 #include <linux/usb.h> 30 + #include <linux/leds.h> 25 31 26 32 #include "hid-ids.h" 27 33 28 34 #define VAIO_RDESC_CONSTANT (1 << 0) 29 35 #define SIXAXIS_CONTROLLER_USB (1 << 1) 30 36 #define SIXAXIS_CONTROLLER_BT (1 << 2) 37 + #define BUZZ_CONTROLLER (1 << 3) 38 + #define PS3REMOTE (1 << 4) 31 39 32 40 static const u8 sixaxis_rdesc_fixup[] = { 33 41 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C, ··· 67 55 0xb1, 0x02, 0xc0, 0xc0, 68 56 }; 69 57 58 + static __u8 ps3remote_rdesc[] = { 59 + 0x05, 0x01, /* GUsagePage Generic Desktop */ 60 + 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ 61 + 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */ 62 + 63 + /* Use collection 1 for joypad buttons */ 64 + 0xA1, 0x02, /* MCollection Logical (interrelated data) */ 65 + 66 + /* Ignore the 1st byte, maybe it is used for a controller 67 + * number but it's not needed for correct operation */ 68 + 0x75, 0x08, /* GReportSize 0x08 [8] */ 69 + 0x95, 0x01, /* GReportCount 0x01 [1] */ 70 + 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */ 71 + 72 + /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these 73 + * buttons multiple keypresses are allowed */ 74 + 0x05, 0x09, /* GUsagePage Button */ 75 + 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */ 76 + 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */ 77 + 0x14, /* GLogicalMinimum [0] */ 78 + 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */ 79 + 0x75, 0x01, /* GReportSize 0x01 [1] */ 80 + 0x95, 0x18, /* GReportCount 0x18 [24] */ 81 + 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */ 82 + 83 + 0xC0, /* MEndCollection */ 84 + 85 + /* Use collection 2 for remote control buttons */ 86 + 0xA1, 0x02, /* MCollection Logical (interrelated data) */ 87 + 88 + /* 5th byte is used for remote control buttons */ 89 + 0x05, 0x09, /* GUsagePage Button */ 90 + 0x18, /* LUsageMinimum [No button pressed] */ 91 + 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */ 92 + 0x14, /* GLogicalMinimum [0] */ 93 + 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */ 94 + 0x75, 0x08, /* GReportSize 0x08 [8] */ 95 + 0x95, 0x01, /* GReportCount 0x01 [1] */ 96 + 0x80, /* MInput */ 97 + 98 + /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at 99 + * 0xff and 11th is for press indication */ 100 + 0x75, 0x08, /* GReportSize 0x08 [8] */ 101 + 0x95, 0x06, /* GReportCount 0x06 [6] */ 102 + 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */ 103 + 104 + /* 12th byte is for battery strength */ 105 + 0x05, 0x06, /* GUsagePage Generic Device Controls */ 106 + 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */ 107 + 0x14, /* GLogicalMinimum [0] */ 108 + 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */ 109 + 0x75, 0x08, /* GReportSize 0x08 [8] */ 110 + 0x95, 0x01, /* GReportCount 0x01 [1] */ 111 + 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */ 112 + 113 + 0xC0, /* MEndCollection */ 114 + 115 + 0xC0 /* MEndCollection [Game Pad] */ 116 + }; 117 + 118 + static const unsigned int ps3remote_keymap_joypad_buttons[] = { 119 + [0x01] = KEY_SELECT, 120 + [0x02] = BTN_THUMBL, /* L3 */ 121 + [0x03] = BTN_THUMBR, /* R3 */ 122 + [0x04] = BTN_START, 123 + [0x05] = KEY_UP, 124 + [0x06] = KEY_RIGHT, 125 + [0x07] = KEY_DOWN, 126 + [0x08] = KEY_LEFT, 127 + [0x09] = BTN_TL2, /* L2 */ 128 + [0x0a] = BTN_TR2, /* R2 */ 129 + [0x0b] = BTN_TL, /* L1 */ 130 + [0x0c] = BTN_TR, /* R1 */ 131 + [0x0d] = KEY_OPTION, /* options/triangle */ 132 + [0x0e] = KEY_BACK, /* back/circle */ 133 + [0x0f] = BTN_0, /* cross */ 134 + [0x10] = KEY_SCREEN, /* view/square */ 135 + [0x11] = KEY_HOMEPAGE, /* PS button */ 136 + [0x14] = KEY_ENTER, 137 + }; 138 + static const unsigned int ps3remote_keymap_remote_buttons[] = { 139 + [0x00] = KEY_1, 140 + [0x01] = KEY_2, 141 + [0x02] = KEY_3, 142 + [0x03] = KEY_4, 143 + [0x04] = KEY_5, 144 + [0x05] = KEY_6, 145 + [0x06] = KEY_7, 146 + [0x07] = KEY_8, 147 + [0x08] = KEY_9, 148 + [0x09] = KEY_0, 149 + [0x0e] = KEY_ESC, /* return */ 150 + [0x0f] = KEY_CLEAR, 151 + [0x16] = KEY_EJECTCD, 152 + [0x1a] = KEY_MENU, /* top menu */ 153 + [0x28] = KEY_TIME, 154 + [0x30] = KEY_PREVIOUS, 155 + [0x31] = KEY_NEXT, 156 + [0x32] = KEY_PLAY, 157 + [0x33] = KEY_REWIND, /* scan back */ 158 + [0x34] = KEY_FORWARD, /* scan forward */ 159 + [0x38] = KEY_STOP, 160 + [0x39] = KEY_PAUSE, 161 + [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */ 162 + [0x60] = KEY_FRAMEBACK, /* slow/step back */ 163 + [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */ 164 + [0x63] = KEY_SUBTITLE, 165 + [0x64] = KEY_AUDIO, 166 + [0x65] = KEY_ANGLE, 167 + [0x70] = KEY_INFO, /* display */ 168 + [0x80] = KEY_BLUE, 169 + [0x81] = KEY_RED, 170 + [0x82] = KEY_GREEN, 171 + [0x83] = KEY_YELLOW, 172 + }; 173 + 174 + static const unsigned int buzz_keymap[] = { 175 + /* The controller has 4 remote buzzers, each with one LED and 5 176 + * buttons. 177 + * 178 + * We use the mapping chosen by the controller, which is: 179 + * 180 + * Key Offset 181 + * ------------------- 182 + * Buzz 1 183 + * Blue 5 184 + * Orange 4 185 + * Green 3 186 + * Yellow 2 187 + * 188 + * So, for example, the orange button on the third buzzer is mapped to 189 + * BTN_TRIGGER_HAPPY14 190 + */ 191 + [ 1] = BTN_TRIGGER_HAPPY1, 192 + [ 2] = BTN_TRIGGER_HAPPY2, 193 + [ 3] = BTN_TRIGGER_HAPPY3, 194 + [ 4] = BTN_TRIGGER_HAPPY4, 195 + [ 5] = BTN_TRIGGER_HAPPY5, 196 + [ 6] = BTN_TRIGGER_HAPPY6, 197 + [ 7] = BTN_TRIGGER_HAPPY7, 198 + [ 8] = BTN_TRIGGER_HAPPY8, 199 + [ 9] = BTN_TRIGGER_HAPPY9, 200 + [10] = BTN_TRIGGER_HAPPY10, 201 + [11] = BTN_TRIGGER_HAPPY11, 202 + [12] = BTN_TRIGGER_HAPPY12, 203 + [13] = BTN_TRIGGER_HAPPY13, 204 + [14] = BTN_TRIGGER_HAPPY14, 205 + [15] = BTN_TRIGGER_HAPPY15, 206 + [16] = BTN_TRIGGER_HAPPY16, 207 + [17] = BTN_TRIGGER_HAPPY17, 208 + [18] = BTN_TRIGGER_HAPPY18, 209 + [19] = BTN_TRIGGER_HAPPY19, 210 + [20] = BTN_TRIGGER_HAPPY20, 211 + }; 212 + 70 213 struct sony_sc { 71 214 unsigned long quirks; 215 + 216 + void *extra; 72 217 }; 218 + 219 + struct buzz_extra { 220 + int led_state; 221 + struct led_classdev *leds[4]; 222 + }; 223 + 224 + static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc, 225 + unsigned int *rsize) 226 + { 227 + *rsize = sizeof(ps3remote_rdesc); 228 + return ps3remote_rdesc; 229 + } 230 + 231 + static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi, 232 + struct hid_field *field, struct hid_usage *usage, 233 + unsigned long **bit, int *max) 234 + { 235 + unsigned int key = usage->hid & HID_USAGE; 236 + 237 + if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) 238 + return -1; 239 + 240 + switch (usage->collection_index) { 241 + case 1: 242 + if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons)) 243 + return -1; 244 + 245 + key = ps3remote_keymap_joypad_buttons[key]; 246 + if (!key) 247 + return -1; 248 + break; 249 + case 2: 250 + if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons)) 251 + return -1; 252 + 253 + key = ps3remote_keymap_remote_buttons[key]; 254 + if (!key) 255 + return -1; 256 + break; 257 + default: 258 + return -1; 259 + } 260 + 261 + hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); 262 + return 1; 263 + } 264 + 73 265 74 266 /* Sony Vaio VGX has wrongly mouse pointer declared as constant */ 75 267 static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc, ··· 311 95 *rsize = sizeof(sixaxis_rdesc_fixup2); 312 96 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize); 313 97 } 98 + 99 + if (sc->quirks & PS3REMOTE) 100 + return ps3remote_fixup(hdev, rdesc, rsize); 101 + 314 102 return rdesc; 315 103 } 316 104 ··· 335 115 } 336 116 337 117 return 0; 118 + } 119 + 120 + static int sony_mapping(struct hid_device *hdev, struct hid_input *hi, 121 + struct hid_field *field, struct hid_usage *usage, 122 + unsigned long **bit, int *max) 123 + { 124 + struct sony_sc *sc = hid_get_drvdata(hdev); 125 + 126 + if (sc->quirks & BUZZ_CONTROLLER) { 127 + unsigned int key = usage->hid & HID_USAGE; 128 + 129 + if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) 130 + return -1; 131 + 132 + switch (usage->collection_index) { 133 + case 1: 134 + if (key >= ARRAY_SIZE(buzz_keymap)) 135 + return -1; 136 + 137 + key = buzz_keymap[key]; 138 + if (!key) 139 + return -1; 140 + break; 141 + default: 142 + return -1; 143 + } 144 + 145 + hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); 146 + return 1; 147 + } 148 + 149 + if (sc->quirks & PS3REMOTE) 150 + return ps3remote_mapping(hdev, hi, field, usage, bit, max); 151 + 152 + return -1; 338 153 } 339 154 340 155 /* ··· 447 192 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT); 448 193 } 449 194 195 + static void buzz_set_leds(struct hid_device *hdev, int leds) 196 + { 197 + struct list_head *report_list = 198 + &hdev->report_enum[HID_OUTPUT_REPORT].report_list; 199 + struct hid_report *report = list_entry(report_list->next, 200 + struct hid_report, list); 201 + __s32 *value = report->field[0]->value; 202 + 203 + value[0] = 0x00; 204 + value[1] = (leds & 1) ? 0xff : 0x00; 205 + value[2] = (leds & 2) ? 0xff : 0x00; 206 + value[3] = (leds & 4) ? 0xff : 0x00; 207 + value[4] = (leds & 8) ? 0xff : 0x00; 208 + value[5] = 0x00; 209 + value[6] = 0x00; 210 + hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 211 + } 212 + 213 + static void buzz_led_set_brightness(struct led_classdev *led, 214 + enum led_brightness value) 215 + { 216 + struct device *dev = led->dev->parent; 217 + struct hid_device *hdev = container_of(dev, struct hid_device, dev); 218 + struct sony_sc *drv_data; 219 + struct buzz_extra *buzz; 220 + 221 + int n; 222 + 223 + drv_data = hid_get_drvdata(hdev); 224 + if (!drv_data || !drv_data->extra) { 225 + hid_err(hdev, "No device data\n"); 226 + return; 227 + } 228 + buzz = drv_data->extra; 229 + 230 + for (n = 0; n < 4; n++) { 231 + if (led == buzz->leds[n]) { 232 + int on = !! (buzz->led_state & (1 << n)); 233 + if (value == LED_OFF && on) { 234 + buzz->led_state &= ~(1 << n); 235 + buzz_set_leds(hdev, buzz->led_state); 236 + } else if (value != LED_OFF && !on) { 237 + buzz->led_state |= (1 << n); 238 + buzz_set_leds(hdev, buzz->led_state); 239 + } 240 + break; 241 + } 242 + } 243 + } 244 + 245 + static enum led_brightness buzz_led_get_brightness(struct led_classdev *led) 246 + { 247 + struct device *dev = led->dev->parent; 248 + struct hid_device *hdev = container_of(dev, struct hid_device, dev); 249 + struct sony_sc *drv_data; 250 + struct buzz_extra *buzz; 251 + 252 + int n; 253 + int on = 0; 254 + 255 + drv_data = hid_get_drvdata(hdev); 256 + if (!drv_data || !drv_data->extra) { 257 + hid_err(hdev, "No device data\n"); 258 + return LED_OFF; 259 + } 260 + buzz = drv_data->extra; 261 + 262 + for (n = 0; n < 4; n++) { 263 + if (led == buzz->leds[n]) { 264 + on = !! (buzz->led_state & (1 << n)); 265 + break; 266 + } 267 + } 268 + 269 + return on ? LED_FULL : LED_OFF; 270 + } 271 + 272 + static int buzz_init(struct hid_device *hdev) 273 + { 274 + struct sony_sc *drv_data; 275 + struct buzz_extra *buzz; 276 + int n, ret = 0; 277 + struct led_classdev *led; 278 + size_t name_sz; 279 + char *name; 280 + 281 + drv_data = hid_get_drvdata(hdev); 282 + BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER)); 283 + 284 + buzz = kzalloc(sizeof(*buzz), GFP_KERNEL); 285 + if (!buzz) { 286 + hid_err(hdev, "Insufficient memory, cannot allocate driver data\n"); 287 + return -ENOMEM; 288 + } 289 + drv_data->extra = buzz; 290 + 291 + /* Clear LEDs as we have no way of reading their initial state. This is 292 + * only relevant if the driver is loaded after somebody actively set the 293 + * LEDs to on */ 294 + buzz_set_leds(hdev, 0x00); 295 + 296 + name_sz = strlen(dev_name(&hdev->dev)) + strlen("::buzz#") + 1; 297 + 298 + for (n = 0; n < 4; n++) { 299 + led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL); 300 + if (!led) { 301 + hid_err(hdev, "Couldn't allocate memory for LED %d\n", n); 302 + goto error_leds; 303 + } 304 + 305 + name = (void *)(&led[1]); 306 + snprintf(name, name_sz, "%s::buzz%d", dev_name(&hdev->dev), n + 1); 307 + led->name = name; 308 + led->brightness = 0; 309 + led->max_brightness = 1; 310 + led->brightness_get = buzz_led_get_brightness; 311 + led->brightness_set = buzz_led_set_brightness; 312 + 313 + if (led_classdev_register(&hdev->dev, led)) { 314 + hid_err(hdev, "Failed to register LED %d\n", n); 315 + kfree(led); 316 + goto error_leds; 317 + } 318 + 319 + buzz->leds[n] = led; 320 + } 321 + 322 + return ret; 323 + 324 + error_leds: 325 + for (n = 0; n < 4; n++) { 326 + led = buzz->leds[n]; 327 + buzz->leds[n] = NULL; 328 + if (!led) 329 + continue; 330 + led_classdev_unregister(led); 331 + kfree(led); 332 + } 333 + 334 + kfree(drv_data->extra); 335 + drv_data->extra = NULL; 336 + return ret; 337 + } 338 + 339 + static void buzz_remove(struct hid_device *hdev) 340 + { 341 + struct sony_sc *drv_data; 342 + struct buzz_extra *buzz; 343 + struct led_classdev *led; 344 + int n; 345 + 346 + drv_data = hid_get_drvdata(hdev); 347 + BUG_ON(!(drv_data->quirks & BUZZ_CONTROLLER)); 348 + 349 + buzz = drv_data->extra; 350 + 351 + for (n = 0; n < 4; n++) { 352 + led = buzz->leds[n]; 353 + buzz->leds[n] = NULL; 354 + if (!led) 355 + continue; 356 + led_classdev_unregister(led); 357 + kfree(led); 358 + } 359 + 360 + kfree(drv_data->extra); 361 + drv_data->extra = NULL; 362 + } 363 + 450 364 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) 451 365 { 452 366 int ret; 453 367 unsigned long quirks = id->driver_data; 454 368 struct sony_sc *sc; 369 + unsigned int connect_mask = HID_CONNECT_DEFAULT; 455 370 456 371 sc = kzalloc(sizeof(*sc), GFP_KERNEL); 457 372 if (sc == NULL) { ··· 638 213 goto err_free; 639 214 } 640 215 641 - ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | 642 - HID_CONNECT_HIDDEV_FORCE); 216 + if (sc->quirks & VAIO_RDESC_CONSTANT) 217 + connect_mask |= HID_CONNECT_HIDDEV_FORCE; 218 + else if (sc->quirks & SIXAXIS_CONTROLLER_USB) 219 + connect_mask |= HID_CONNECT_HIDDEV_FORCE; 220 + else if (sc->quirks & SIXAXIS_CONTROLLER_BT) 221 + connect_mask |= HID_CONNECT_HIDDEV_FORCE; 222 + 223 + ret = hid_hw_start(hdev, connect_mask); 643 224 if (ret) { 644 225 hid_err(hdev, "hw start failed\n"); 645 226 goto err_free; ··· 657 226 } 658 227 else if (sc->quirks & SIXAXIS_CONTROLLER_BT) 659 228 ret = sixaxis_set_operational_bt(hdev); 229 + else if (sc->quirks & BUZZ_CONTROLLER) 230 + ret = buzz_init(hdev); 660 231 else 661 232 ret = 0; 662 233 ··· 675 242 676 243 static void sony_remove(struct hid_device *hdev) 677 244 { 245 + struct sony_sc *sc = hid_get_drvdata(hdev); 246 + 247 + if (sc->quirks & BUZZ_CONTROLLER) 248 + buzz_remove(hdev); 249 + 678 250 hid_hw_stop(hdev); 679 - kfree(hid_get_drvdata(hdev)); 251 + kfree(sc); 680 252 } 681 253 682 254 static const struct hid_device_id sony_devices[] = { ··· 695 257 .driver_data = VAIO_RDESC_CONSTANT }, 696 258 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE), 697 259 .driver_data = VAIO_RDESC_CONSTANT }, 260 + /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as 261 + * Logitech joystick from the device descriptor. */ 262 + { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER), 263 + .driver_data = BUZZ_CONTROLLER }, 264 + { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER), 265 + .driver_data = BUZZ_CONTROLLER }, 266 + /* PS3 BD Remote Control */ 267 + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE), 268 + .driver_data = PS3REMOTE }, 269 + /* Logitech Harmony Adapter for PS3 */ 270 + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3), 271 + .driver_data = PS3REMOTE }, 698 272 { } 699 273 }; 700 274 MODULE_DEVICE_TABLE(hid, sony_devices); 701 275 702 276 static struct hid_driver sony_driver = { 703 - .name = "sony", 704 - .id_table = sony_devices, 705 - .probe = sony_probe, 706 - .remove = sony_remove, 707 - .report_fixup = sony_report_fixup, 708 - .raw_event = sony_raw_event 277 + .name = "sony", 278 + .id_table = sony_devices, 279 + .input_mapping = sony_mapping, 280 + .probe = sony_probe, 281 + .remove = sony_remove, 282 + .report_fixup = sony_report_fixup, 283 + .raw_event = sony_raw_event 709 284 }; 710 285 module_hid_driver(sony_driver); 711 286
+13 -1
drivers/hid/hid-wacom.c
··· 46 46 __u8 battery_capacity; 47 47 __u8 power_raw; 48 48 __u8 ps_connected; 49 + __u8 bat_charging; 49 50 struct power_supply battery; 50 51 struct power_supply ac; 51 52 __u8 led_selector; ··· 63 62 POWER_SUPPLY_PROP_PRESENT, 64 63 POWER_SUPPLY_PROP_CAPACITY, 65 64 POWER_SUPPLY_PROP_SCOPE, 65 + POWER_SUPPLY_PROP_STATUS, 66 66 }; 67 67 68 68 static enum power_supply_property wacom_ac_props[] = { ··· 288 286 break; 289 287 case POWER_SUPPLY_PROP_CAPACITY: 290 288 val->intval = wdata->battery_capacity; 289 + break; 290 + case POWER_SUPPLY_PROP_STATUS: 291 + if (wdata->bat_charging) 292 + val->intval = POWER_SUPPLY_STATUS_CHARGING; 293 + else 294 + if (wdata->battery_capacity == 100 && wdata->ps_connected) 295 + val->intval = POWER_SUPPLY_STATUS_FULL; 296 + else 297 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 291 298 break; 292 299 default: 293 300 ret = -EINVAL; ··· 738 727 if (power_raw != wdata->power_raw) { 739 728 wdata->power_raw = power_raw; 740 729 wdata->battery_capacity = batcap_i4[power_raw & 0x07]; 741 - wdata->ps_connected = power_raw & 0x08; 730 + wdata->bat_charging = (power_raw & 0x08) ? 1 : 0; 731 + wdata->ps_connected = (power_raw & 0x10) ? 1 : 0; 742 732 } 743 733 744 734 break;
+1171 -627
drivers/hid/hid-wiimote-core.c
··· 1 1 /* 2 - * HID driver for Nintendo Wiimote devices 3 - * Copyright (c) 2011 David Herrmann 2 + * HID driver for Nintendo Wii / Wii U peripherals 3 + * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com> 4 4 */ 5 5 6 6 /* ··· 14 14 #include <linux/device.h> 15 15 #include <linux/hid.h> 16 16 #include <linux/input.h> 17 - #include <linux/leds.h> 18 17 #include <linux/module.h> 19 18 #include <linux/mutex.h> 20 - #include <linux/power_supply.h> 21 19 #include <linux/spinlock.h> 22 20 #include "hid-ids.h" 23 21 #include "hid-wiimote.h" 24 22 25 - enum wiiproto_keys { 26 - WIIPROTO_KEY_LEFT, 27 - WIIPROTO_KEY_RIGHT, 28 - WIIPROTO_KEY_UP, 29 - WIIPROTO_KEY_DOWN, 30 - WIIPROTO_KEY_PLUS, 31 - WIIPROTO_KEY_MINUS, 32 - WIIPROTO_KEY_ONE, 33 - WIIPROTO_KEY_TWO, 34 - WIIPROTO_KEY_A, 35 - WIIPROTO_KEY_B, 36 - WIIPROTO_KEY_HOME, 37 - WIIPROTO_KEY_COUNT 38 - }; 23 + /* output queue handling */ 39 24 40 - static __u16 wiiproto_keymap[] = { 41 - KEY_LEFT, /* WIIPROTO_KEY_LEFT */ 42 - KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */ 43 - KEY_UP, /* WIIPROTO_KEY_UP */ 44 - KEY_DOWN, /* WIIPROTO_KEY_DOWN */ 45 - KEY_NEXT, /* WIIPROTO_KEY_PLUS */ 46 - KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */ 47 - BTN_1, /* WIIPROTO_KEY_ONE */ 48 - BTN_2, /* WIIPROTO_KEY_TWO */ 49 - BTN_A, /* WIIPROTO_KEY_A */ 50 - BTN_B, /* WIIPROTO_KEY_B */ 51 - BTN_MODE, /* WIIPROTO_KEY_HOME */ 52 - }; 53 - 54 - static enum power_supply_property wiimote_battery_props[] = { 55 - POWER_SUPPLY_PROP_CAPACITY, 56 - POWER_SUPPLY_PROP_SCOPE, 57 - }; 58 - 59 - static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, 60 - size_t count) 25 + static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, 26 + size_t count) 61 27 { 62 28 __u8 *buf; 63 - ssize_t ret; 29 + int ret; 64 30 65 31 if (!hdev->hid_output_raw_report) 66 32 return -ENODEV; ··· 41 75 return ret; 42 76 } 43 77 44 - static void wiimote_worker(struct work_struct *work) 78 + static void wiimote_queue_worker(struct work_struct *work) 45 79 { 46 - struct wiimote_data *wdata = container_of(work, struct wiimote_data, 47 - worker); 80 + struct wiimote_queue *queue = container_of(work, struct wiimote_queue, 81 + worker); 82 + struct wiimote_data *wdata = container_of(queue, struct wiimote_data, 83 + queue); 48 84 unsigned long flags; 85 + int ret; 49 86 50 - spin_lock_irqsave(&wdata->qlock, flags); 87 + spin_lock_irqsave(&wdata->queue.lock, flags); 51 88 52 - while (wdata->head != wdata->tail) { 53 - spin_unlock_irqrestore(&wdata->qlock, flags); 54 - wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data, 55 - wdata->outq[wdata->tail].size); 56 - spin_lock_irqsave(&wdata->qlock, flags); 89 + while (wdata->queue.head != wdata->queue.tail) { 90 + spin_unlock_irqrestore(&wdata->queue.lock, flags); 91 + ret = wiimote_hid_send(wdata->hdev, 92 + wdata->queue.outq[wdata->queue.tail].data, 93 + wdata->queue.outq[wdata->queue.tail].size); 94 + if (ret < 0) { 95 + spin_lock_irqsave(&wdata->state.lock, flags); 96 + wiimote_cmd_abort(wdata); 97 + spin_unlock_irqrestore(&wdata->state.lock, flags); 98 + } 99 + spin_lock_irqsave(&wdata->queue.lock, flags); 57 100 58 - wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE; 101 + wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE; 59 102 } 60 103 61 - spin_unlock_irqrestore(&wdata->qlock, flags); 104 + spin_unlock_irqrestore(&wdata->queue.lock, flags); 62 105 } 63 106 64 107 static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer, ··· 78 103 79 104 if (count > HID_MAX_BUFFER_SIZE) { 80 105 hid_warn(wdata->hdev, "Sending too large output report\n"); 81 - return; 106 + 107 + spin_lock_irqsave(&wdata->queue.lock, flags); 108 + goto out_error; 82 109 } 83 110 84 111 /* ··· 93 116 * will reschedule itself until the queue is empty. 94 117 */ 95 118 96 - spin_lock_irqsave(&wdata->qlock, flags); 119 + spin_lock_irqsave(&wdata->queue.lock, flags); 97 120 98 - memcpy(wdata->outq[wdata->head].data, buffer, count); 99 - wdata->outq[wdata->head].size = count; 100 - newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE; 121 + memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count); 122 + wdata->queue.outq[wdata->queue.head].size = count; 123 + newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE; 101 124 102 - if (wdata->head == wdata->tail) { 103 - wdata->head = newhead; 104 - schedule_work(&wdata->worker); 105 - } else if (newhead != wdata->tail) { 106 - wdata->head = newhead; 125 + if (wdata->queue.head == wdata->queue.tail) { 126 + wdata->queue.head = newhead; 127 + schedule_work(&wdata->queue.worker); 128 + } else if (newhead != wdata->queue.tail) { 129 + wdata->queue.head = newhead; 107 130 } else { 108 131 hid_warn(wdata->hdev, "Output queue is full"); 132 + goto out_error; 109 133 } 110 134 111 - spin_unlock_irqrestore(&wdata->qlock, flags); 135 + goto out_unlock; 136 + 137 + out_error: 138 + wiimote_cmd_abort(wdata); 139 + out_unlock: 140 + spin_unlock_irqrestore(&wdata->queue.lock, flags); 112 141 } 113 142 114 143 /* ··· 130 147 *cmd1 |= 0x01; 131 148 } 132 149 133 - static void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) 150 + void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble) 134 151 { 135 152 __u8 cmd[2]; 136 153 ··· 150 167 wiimote_queue(wdata, cmd, sizeof(cmd)); 151 168 } 152 169 153 - static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) 170 + void wiiproto_req_leds(struct wiimote_data *wdata, int leds) 154 171 { 155 172 __u8 cmd[2]; 156 173 ··· 179 196 * Check what peripherals of the wiimote are currently 180 197 * active and select a proper DRM that supports all of 181 198 * the requested data inputs. 199 + * 200 + * Not all combinations are actually supported. The following 201 + * combinations work only with limitations: 202 + * - IR cam in extended or full mode disables any data transmission 203 + * of extension controllers. There is no DRM mode that supports 204 + * extension bytes plus extended/full IR. 205 + * - IR cam with accelerometer and extension *_EXT8 is not supported. 206 + * However, all extensions that need *_EXT8 are devices that don't 207 + * support IR cameras. Hence, this shouldn't happen under normal 208 + * operation. 209 + * - *_EXT16 is only supported in combination with buttons and 210 + * accelerometer. No IR or similar can be active simultaneously. As 211 + * above, all modules that require it are mutually exclusive with 212 + * IR/etc. so this doesn't matter. 182 213 */ 183 214 static __u8 select_drm(struct wiimote_data *wdata) 184 215 { 185 216 __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR; 186 - bool ext = wiiext_active(wdata); 217 + bool ext; 218 + 219 + ext = (wdata->state.flags & WIIPROTO_FLAG_EXT_USED) || 220 + (wdata->state.flags & WIIPROTO_FLAG_MP_USED); 221 + 222 + /* some 3rd-party balance-boards are hard-coded to KEE, *sigh* */ 223 + if (wdata->state.devtype == WIIMOTE_DEV_BALANCE_BOARD) { 224 + if (ext) 225 + return WIIPROTO_REQ_DRM_KEE; 226 + else 227 + return WIIPROTO_REQ_DRM_K; 228 + } 187 229 188 230 if (ir == WIIPROTO_FLAG_IR_BASIC) { 189 - if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) 190 - return WIIPROTO_REQ_DRM_KAIE; 191 - else 231 + if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) { 232 + if (ext) 233 + return WIIPROTO_REQ_DRM_KAIE; 234 + else 235 + return WIIPROTO_REQ_DRM_KAI; 236 + } else { 192 237 return WIIPROTO_REQ_DRM_KIE; 238 + } 193 239 } else if (ir == WIIPROTO_FLAG_IR_EXT) { 194 240 return WIIPROTO_REQ_DRM_KAI; 195 241 } else if (ir == WIIPROTO_FLAG_IR_FULL) { ··· 231 219 return WIIPROTO_REQ_DRM_KA; 232 220 } else { 233 221 if (ext) 234 - return WIIPROTO_REQ_DRM_KE; 222 + return WIIPROTO_REQ_DRM_KEE; 235 223 else 236 224 return WIIPROTO_REQ_DRM_K; 237 225 } ··· 242 230 { 243 231 __u8 cmd[3]; 244 232 245 - if (drm == WIIPROTO_REQ_NULL) 233 + if (wdata->state.flags & WIIPROTO_FLAG_DRM_LOCKED) 234 + drm = wdata->state.drm; 235 + else if (drm == WIIPROTO_REQ_NULL) 246 236 drm = select_drm(wdata); 247 237 248 238 cmd[0] = WIIPROTO_REQ_DRM; ··· 256 242 wiimote_queue(wdata, cmd, sizeof(cmd)); 257 243 } 258 244 259 - static void wiiproto_req_status(struct wiimote_data *wdata) 245 + void wiiproto_req_status(struct wiimote_data *wdata) 260 246 { 261 247 __u8 cmd[2]; 262 248 ··· 267 253 wiimote_queue(wdata, cmd, sizeof(cmd)); 268 254 } 269 255 270 - static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) 256 + void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel) 271 257 { 272 258 accel = !!accel; 273 259 if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) ··· 281 267 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 282 268 } 283 269 284 - static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) 270 + void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags) 285 271 { 286 272 __u8 cmd[2]; 287 273 ··· 292 278 wiimote_queue(wdata, cmd, sizeof(cmd)); 293 279 } 294 280 295 - static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) 281 + void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags) 296 282 { 297 283 __u8 cmd[2]; 298 284 ··· 408 394 return ret; 409 395 } 410 396 411 - static int wiimote_battery_get_property(struct power_supply *psy, 412 - enum power_supply_property psp, 413 - union power_supply_propval *val) 397 + /* requires the cmd-mutex to be held */ 398 + static int wiimote_cmd_init_ext(struct wiimote_data *wdata) 414 399 { 415 - struct wiimote_data *wdata = container_of(psy, 416 - struct wiimote_data, battery); 417 - int ret = 0, state; 418 - unsigned long flags; 400 + __u8 wmem; 401 + int ret; 419 402 420 - if (psp == POWER_SUPPLY_PROP_SCOPE) { 421 - val->intval = POWER_SUPPLY_SCOPE_DEVICE; 422 - return 0; 423 - } 424 - 425 - ret = wiimote_cmd_acquire(wdata); 403 + /* initialize extension */ 404 + wmem = 0x55; 405 + ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem)); 426 406 if (ret) 427 407 return ret; 428 408 409 + /* disable default encryption */ 410 + wmem = 0x0; 411 + ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem)); 412 + if (ret) 413 + return ret; 414 + 415 + return 0; 416 + } 417 + 418 + /* requires the cmd-mutex to be held */ 419 + static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem) 420 + { 421 + int ret; 422 + 423 + /* read extension ID */ 424 + ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6); 425 + if (ret != 6) 426 + return WIIMOTE_EXT_NONE; 427 + 428 + hid_dbg(wdata->hdev, "extension ID: %02x:%02x %02x:%02x %02x:%02x\n", 429 + rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); 430 + 431 + if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff && 432 + rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff) 433 + return WIIMOTE_EXT_NONE; 434 + 435 + if (rmem[4] == 0x00 && rmem[5] == 0x00) 436 + return WIIMOTE_EXT_NUNCHUK; 437 + if (rmem[4] == 0x01 && rmem[5] == 0x01) 438 + return WIIMOTE_EXT_CLASSIC_CONTROLLER; 439 + if (rmem[4] == 0x04 && rmem[5] == 0x02) 440 + return WIIMOTE_EXT_BALANCE_BOARD; 441 + if (rmem[4] == 0x01 && rmem[5] == 0x20) 442 + return WIIMOTE_EXT_PRO_CONTROLLER; 443 + 444 + return WIIMOTE_EXT_UNKNOWN; 445 + } 446 + 447 + /* requires the cmd-mutex to be held */ 448 + static int wiimote_cmd_init_mp(struct wiimote_data *wdata) 449 + { 450 + __u8 wmem; 451 + int ret; 452 + 453 + /* initialize MP */ 454 + wmem = 0x55; 455 + ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem)); 456 + if (ret) 457 + return ret; 458 + 459 + /* disable default encryption */ 460 + wmem = 0x0; 461 + ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem)); 462 + if (ret) 463 + return ret; 464 + 465 + return 0; 466 + } 467 + 468 + /* requires the cmd-mutex to be held */ 469 + static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype) 470 + { 471 + __u8 wmem; 472 + 473 + /* map MP with correct pass-through mode */ 474 + switch (exttype) { 475 + case WIIMOTE_EXT_CLASSIC_CONTROLLER: 476 + wmem = 0x07; 477 + break; 478 + case WIIMOTE_EXT_NUNCHUK: 479 + wmem = 0x05; 480 + break; 481 + default: 482 + wmem = 0x04; 483 + break; 484 + } 485 + 486 + return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem)); 487 + } 488 + 489 + /* requires the cmd-mutex to be held */ 490 + static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem) 491 + { 492 + int ret; 493 + 494 + /* read motion plus ID */ 495 + ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6); 496 + if (ret != 6) 497 + return false; 498 + 499 + hid_dbg(wdata->hdev, "motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n", 500 + rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); 501 + 502 + if (rmem[5] == 0x05) 503 + return true; 504 + 505 + hid_info(wdata->hdev, "unknown motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n", 506 + rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); 507 + 508 + return false; 509 + } 510 + 511 + /* requires the cmd-mutex to be held */ 512 + static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata) 513 + { 514 + int ret; 515 + __u8 rmem[6]; 516 + 517 + /* read motion plus ID */ 518 + ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6); 519 + if (ret != 6) 520 + return WIIMOTE_MP_NONE; 521 + 522 + hid_dbg(wdata->hdev, "mapped motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n", 523 + rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]); 524 + 525 + if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff && 526 + rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff) 527 + return WIIMOTE_MP_NONE; 528 + 529 + if (rmem[4] == 0x04 && rmem[5] == 0x05) 530 + return WIIMOTE_MP_SINGLE; 531 + else if (rmem[4] == 0x05 && rmem[5] == 0x05) 532 + return WIIMOTE_MP_PASSTHROUGH_NUNCHUK; 533 + else if (rmem[4] == 0x07 && rmem[5] == 0x05) 534 + return WIIMOTE_MP_PASSTHROUGH_CLASSIC; 535 + 536 + return WIIMOTE_MP_UNKNOWN; 537 + } 538 + 539 + /* device module handling */ 540 + 541 + static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = { 542 + [WIIMOTE_DEV_PENDING] = (const __u8[]){ 543 + WIIMOD_NULL, 544 + }, 545 + [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){ 546 + WIIMOD_NO_MP, 547 + WIIMOD_NULL, 548 + }, 549 + [WIIMOTE_DEV_GENERIC] = (const __u8[]){ 550 + WIIMOD_KEYS, 551 + WIIMOD_RUMBLE, 552 + WIIMOD_BATTERY, 553 + WIIMOD_LED1, 554 + WIIMOD_LED2, 555 + WIIMOD_LED3, 556 + WIIMOD_LED4, 557 + WIIMOD_ACCEL, 558 + WIIMOD_IR, 559 + WIIMOD_NULL, 560 + }, 561 + [WIIMOTE_DEV_GEN10] = (const __u8[]){ 562 + WIIMOD_KEYS, 563 + WIIMOD_RUMBLE, 564 + WIIMOD_BATTERY, 565 + WIIMOD_LED1, 566 + WIIMOD_LED2, 567 + WIIMOD_LED3, 568 + WIIMOD_LED4, 569 + WIIMOD_ACCEL, 570 + WIIMOD_IR, 571 + WIIMOD_NULL, 572 + }, 573 + [WIIMOTE_DEV_GEN20] = (const __u8[]){ 574 + WIIMOD_KEYS, 575 + WIIMOD_RUMBLE, 576 + WIIMOD_BATTERY, 577 + WIIMOD_LED1, 578 + WIIMOD_LED2, 579 + WIIMOD_LED3, 580 + WIIMOD_LED4, 581 + WIIMOD_ACCEL, 582 + WIIMOD_IR, 583 + WIIMOD_BUILTIN_MP, 584 + WIIMOD_NULL, 585 + }, 586 + [WIIMOTE_DEV_BALANCE_BOARD] = (const __u8[]) { 587 + WIIMOD_BATTERY, 588 + WIIMOD_LED1, 589 + WIIMOD_NO_MP, 590 + WIIMOD_NULL, 591 + }, 592 + [WIIMOTE_DEV_PRO_CONTROLLER] = (const __u8[]) { 593 + WIIMOD_BATTERY, 594 + WIIMOD_LED1, 595 + WIIMOD_LED2, 596 + WIIMOD_LED3, 597 + WIIMOD_LED4, 598 + WIIMOD_NO_MP, 599 + WIIMOD_NULL, 600 + }, 601 + }; 602 + 603 + static void wiimote_modules_load(struct wiimote_data *wdata, 604 + unsigned int devtype) 605 + { 606 + bool need_input = false; 607 + const __u8 *mods, *iter; 608 + const struct wiimod_ops *ops; 609 + int ret; 610 + 611 + mods = wiimote_devtype_mods[devtype]; 612 + 613 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) { 614 + if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) { 615 + need_input = true; 616 + break; 617 + } 618 + } 619 + 620 + if (need_input) { 621 + wdata->input = input_allocate_device(); 622 + if (!wdata->input) 623 + return; 624 + 625 + input_set_drvdata(wdata->input, wdata); 626 + wdata->input->dev.parent = &wdata->hdev->dev; 627 + wdata->input->id.bustype = wdata->hdev->bus; 628 + wdata->input->id.vendor = wdata->hdev->vendor; 629 + wdata->input->id.product = wdata->hdev->product; 630 + wdata->input->id.version = wdata->hdev->version; 631 + wdata->input->name = WIIMOTE_NAME; 632 + } 633 + 634 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) { 635 + ops = wiimod_table[*iter]; 636 + if (!ops->probe) 637 + continue; 638 + 639 + ret = ops->probe(ops, wdata); 640 + if (ret) 641 + goto error; 642 + } 643 + 644 + if (wdata->input) { 645 + ret = input_register_device(wdata->input); 646 + if (ret) 647 + goto error; 648 + } 649 + 650 + spin_lock_irq(&wdata->state.lock); 651 + wdata->state.devtype = devtype; 652 + spin_unlock_irq(&wdata->state.lock); 653 + return; 654 + 655 + error: 656 + for ( ; iter-- != mods; ) { 657 + ops = wiimod_table[*iter]; 658 + if (ops->remove) 659 + ops->remove(ops, wdata); 660 + } 661 + 662 + if (wdata->input) { 663 + input_free_device(wdata->input); 664 + wdata->input = NULL; 665 + } 666 + } 667 + 668 + static void wiimote_modules_unload(struct wiimote_data *wdata) 669 + { 670 + const __u8 *mods, *iter; 671 + const struct wiimod_ops *ops; 672 + unsigned long flags; 673 + 674 + mods = wiimote_devtype_mods[wdata->state.devtype]; 675 + 429 676 spin_lock_irqsave(&wdata->state.lock, flags); 677 + wdata->state.devtype = WIIMOTE_DEV_UNKNOWN; 678 + spin_unlock_irqrestore(&wdata->state.lock, flags); 679 + 680 + /* find end of list */ 681 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) 682 + /* empty */ ; 683 + 684 + if (wdata->input) { 685 + input_get_device(wdata->input); 686 + input_unregister_device(wdata->input); 687 + } 688 + 689 + for ( ; iter-- != mods; ) { 690 + ops = wiimod_table[*iter]; 691 + if (ops->remove) 692 + ops->remove(ops, wdata); 693 + } 694 + 695 + if (wdata->input) { 696 + input_put_device(wdata->input); 697 + wdata->input = NULL; 698 + } 699 + } 700 + 701 + /* device extension handling */ 702 + 703 + static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext) 704 + { 705 + unsigned long flags; 706 + const struct wiimod_ops *ops; 707 + int ret; 708 + 709 + ops = wiimod_ext_table[ext]; 710 + 711 + if (ops->probe) { 712 + ret = ops->probe(ops, wdata); 713 + if (ret) 714 + ext = WIIMOTE_EXT_UNKNOWN; 715 + } 716 + 717 + spin_lock_irqsave(&wdata->state.lock, flags); 718 + wdata->state.exttype = ext; 719 + spin_unlock_irqrestore(&wdata->state.lock, flags); 720 + } 721 + 722 + static void wiimote_ext_unload(struct wiimote_data *wdata) 723 + { 724 + unsigned long flags; 725 + const struct wiimod_ops *ops; 726 + 727 + ops = wiimod_ext_table[wdata->state.exttype]; 728 + 729 + spin_lock_irqsave(&wdata->state.lock, flags); 730 + wdata->state.exttype = WIIMOTE_EXT_UNKNOWN; 731 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED; 732 + spin_unlock_irqrestore(&wdata->state.lock, flags); 733 + 734 + if (ops->remove) 735 + ops->remove(ops, wdata); 736 + } 737 + 738 + static void wiimote_mp_load(struct wiimote_data *wdata) 739 + { 740 + unsigned long flags; 741 + const struct wiimod_ops *ops; 742 + int ret; 743 + __u8 mode = 2; 744 + 745 + ops = &wiimod_mp; 746 + if (ops->probe) { 747 + ret = ops->probe(ops, wdata); 748 + if (ret) 749 + mode = 1; 750 + } 751 + 752 + spin_lock_irqsave(&wdata->state.lock, flags); 753 + wdata->state.mp = mode; 754 + spin_unlock_irqrestore(&wdata->state.lock, flags); 755 + } 756 + 757 + static void wiimote_mp_unload(struct wiimote_data *wdata) 758 + { 759 + unsigned long flags; 760 + const struct wiimod_ops *ops; 761 + 762 + if (wdata->state.mp < 2) 763 + return; 764 + 765 + ops = &wiimod_mp; 766 + 767 + spin_lock_irqsave(&wdata->state.lock, flags); 768 + wdata->state.mp = 0; 769 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED; 770 + spin_unlock_irqrestore(&wdata->state.lock, flags); 771 + 772 + if (ops->remove) 773 + ops->remove(ops, wdata); 774 + } 775 + 776 + /* device (re-)initialization and detection */ 777 + 778 + static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = { 779 + [WIIMOTE_DEV_PENDING] = "Pending", 780 + [WIIMOTE_DEV_UNKNOWN] = "Unknown", 781 + [WIIMOTE_DEV_GENERIC] = "Generic", 782 + [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)", 783 + [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)", 784 + [WIIMOTE_DEV_BALANCE_BOARD] = "Nintendo Wii Balance Board", 785 + [WIIMOTE_DEV_PRO_CONTROLLER] = "Nintendo Wii U Pro Controller", 786 + }; 787 + 788 + /* Try to guess the device type based on all collected information. We 789 + * first try to detect by static extension types, then VID/PID and the 790 + * device name. If we cannot detect the device, we use 791 + * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */ 792 + static void wiimote_init_set_type(struct wiimote_data *wdata, 793 + __u8 exttype) 794 + { 795 + __u8 devtype = WIIMOTE_DEV_GENERIC; 796 + __u16 vendor, product; 797 + const char *name; 798 + 799 + vendor = wdata->hdev->vendor; 800 + product = wdata->hdev->product; 801 + name = wdata->hdev->name; 802 + 803 + if (exttype == WIIMOTE_EXT_BALANCE_BOARD) { 804 + devtype = WIIMOTE_DEV_BALANCE_BOARD; 805 + goto done; 806 + } else if (exttype == WIIMOTE_EXT_PRO_CONTROLLER) { 807 + devtype = WIIMOTE_DEV_PRO_CONTROLLER; 808 + goto done; 809 + } 810 + 811 + if (!strcmp(name, "Nintendo RVL-CNT-01")) { 812 + devtype = WIIMOTE_DEV_GEN10; 813 + goto done; 814 + } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) { 815 + devtype = WIIMOTE_DEV_GEN20; 816 + goto done; 817 + } else if (!strcmp(name, "Nintendo RVL-WBC-01")) { 818 + devtype = WIIMOTE_DEV_BALANCE_BOARD; 819 + goto done; 820 + } else if (!strcmp(name, "Nintendo RVL-CNT-01-UC")) { 821 + devtype = WIIMOTE_DEV_PRO_CONTROLLER; 822 + goto done; 823 + } 824 + 825 + if (vendor == USB_VENDOR_ID_NINTENDO) { 826 + if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) { 827 + devtype = WIIMOTE_DEV_GEN10; 828 + goto done; 829 + } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) { 830 + devtype = WIIMOTE_DEV_GEN20; 831 + goto done; 832 + } 833 + } 834 + 835 + done: 836 + if (devtype == WIIMOTE_DEV_GENERIC) 837 + hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n", 838 + name, vendor, product, exttype); 839 + else 840 + hid_info(wdata->hdev, "detected device: %s\n", 841 + wiimote_devtype_names[devtype]); 842 + 843 + wiimote_modules_load(wdata, devtype); 844 + } 845 + 846 + static void wiimote_init_detect(struct wiimote_data *wdata) 847 + { 848 + __u8 exttype = WIIMOTE_EXT_NONE, extdata[6]; 849 + bool ext; 850 + int ret; 851 + 852 + wiimote_cmd_acquire_noint(wdata); 853 + 854 + spin_lock_irq(&wdata->state.lock); 855 + wdata->state.devtype = WIIMOTE_DEV_UNKNOWN; 430 856 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0); 431 857 wiiproto_req_status(wdata); 432 - spin_unlock_irqrestore(&wdata->state.lock, flags); 858 + spin_unlock_irq(&wdata->state.lock); 433 859 434 - ret = wiimote_cmd_wait(wdata); 435 - state = wdata->state.cmd_battery; 860 + ret = wiimote_cmd_wait_noint(wdata); 861 + if (ret) 862 + goto out_release; 863 + 864 + spin_lock_irq(&wdata->state.lock); 865 + ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED; 866 + spin_unlock_irq(&wdata->state.lock); 867 + 868 + if (!ext) 869 + goto out_release; 870 + 871 + wiimote_cmd_init_ext(wdata); 872 + exttype = wiimote_cmd_read_ext(wdata, extdata); 873 + 874 + out_release: 875 + wiimote_cmd_release(wdata); 876 + wiimote_init_set_type(wdata, exttype); 877 + 878 + /* schedule MP timer */ 879 + spin_lock_irq(&wdata->state.lock); 880 + if (!(wdata->state.flags & WIIPROTO_FLAG_BUILTIN_MP) && 881 + !(wdata->state.flags & WIIPROTO_FLAG_NO_MP)) 882 + mod_timer(&wdata->timer, jiffies + HZ * 4); 883 + spin_unlock_irq(&wdata->state.lock); 884 + } 885 + 886 + /* 887 + * MP hotplug events are not generated by the wiimote. Therefore, we need 888 + * polling to detect it. We use a 4s interval for polling MP registers. This 889 + * seems reasonable considering applications can trigger it manually via 890 + * sysfs requests. 891 + */ 892 + static void wiimote_init_poll_mp(struct wiimote_data *wdata) 893 + { 894 + bool mp; 895 + __u8 mpdata[6]; 896 + 897 + wiimote_cmd_acquire_noint(wdata); 898 + wiimote_cmd_init_mp(wdata); 899 + mp = wiimote_cmd_read_mp(wdata, mpdata); 436 900 wiimote_cmd_release(wdata); 437 901 438 - if (ret) 439 - return ret; 440 - 441 - switch (psp) { 442 - case POWER_SUPPLY_PROP_CAPACITY: 443 - val->intval = state * 100 / 255; 444 - break; 445 - default: 446 - ret = -EINVAL; 447 - break; 902 + /* load/unload MP module if it changed */ 903 + if (mp) { 904 + if (!wdata->state.mp) { 905 + hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n"); 906 + wiimote_mp_load(wdata); 907 + } 908 + } else if (wdata->state.mp) { 909 + wiimote_mp_unload(wdata); 448 910 } 911 + 912 + mod_timer(&wdata->timer, jiffies + HZ * 4); 913 + } 914 + 915 + /* 916 + * Check whether the wiimote is in the expected state. The extension registers 917 + * may change during hotplug and initialization so we might get hotplug events 918 + * that we caused by remapping some memory. 919 + * We use some heuristics here to check known states. If the wiimote is in the 920 + * expected state, we can ignore the hotplug event. 921 + * 922 + * Returns "true" if the device is in expected state, "false" if we should 923 + * redo hotplug handling and extension initialization. 924 + */ 925 + static bool wiimote_init_check(struct wiimote_data *wdata) 926 + { 927 + __u32 flags; 928 + __u8 type, data[6]; 929 + bool ret, poll_mp; 930 + 931 + spin_lock_irq(&wdata->state.lock); 932 + flags = wdata->state.flags; 933 + spin_unlock_irq(&wdata->state.lock); 934 + 935 + wiimote_cmd_acquire_noint(wdata); 936 + 937 + /* If MP is used and active, but the extension is not, we expect: 938 + * read_mp_mapped() == WIIMOTE_MP_SINGLE 939 + * state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE 940 + * We do not check EXT_PLUGGED because it might change during 941 + * initialization of MP without extensions. 942 + * - If MP is unplugged/replugged, read_mp_mapped() fails 943 + * - If EXT is plugged, MP_PLUGGED will get set */ 944 + if (wdata->state.exttype == WIIMOTE_EXT_NONE && 945 + wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) { 946 + type = wiimote_cmd_read_mp_mapped(wdata); 947 + ret = type == WIIMOTE_MP_SINGLE; 948 + 949 + spin_lock_irq(&wdata->state.lock); 950 + ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); 951 + ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED); 952 + ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); 953 + spin_unlock_irq(&wdata->state.lock); 954 + 955 + if (!ret) 956 + hid_dbg(wdata->hdev, "state left: !EXT && MP\n"); 957 + 958 + /* while MP is mapped, we get EXT_PLUGGED events */ 959 + poll_mp = false; 960 + 961 + goto out_release; 962 + } 963 + 964 + /* If MP is unused, but the extension port is used, we expect: 965 + * read_ext == state.exttype 966 + * state.flags == !MP_ACTIVE && EXT_ACTIVE 967 + * - If MP is plugged/unplugged, our timer detects it 968 + * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */ 969 + if (!(flags & WIIPROTO_FLAG_MP_USED) && 970 + wdata->state.exttype != WIIMOTE_EXT_NONE) { 971 + type = wiimote_cmd_read_ext(wdata, data); 972 + ret = type == wdata->state.exttype; 973 + 974 + spin_lock_irq(&wdata->state.lock); 975 + ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); 976 + ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); 977 + spin_unlock_irq(&wdata->state.lock); 978 + 979 + if (!ret) 980 + hid_dbg(wdata->hdev, "state left: EXT && !MP\n"); 981 + 982 + /* poll MP for hotplug events */ 983 + poll_mp = true; 984 + 985 + goto out_release; 986 + } 987 + 988 + /* If neither MP nor an extension are used, we expect: 989 + * read_ext() == WIIMOTE_EXT_NONE 990 + * state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED 991 + * No need to perform any action in this case as everything is 992 + * disabled already. 993 + * - If MP is plugged/unplugged, our timer detects it 994 + * - If EXT is plugged, EXT_PLUGGED will be set */ 995 + if (!(flags & WIIPROTO_FLAG_MP_USED) && 996 + wdata->state.exttype == WIIMOTE_EXT_NONE) { 997 + type = wiimote_cmd_read_ext(wdata, data); 998 + ret = type == wdata->state.exttype; 999 + 1000 + spin_lock_irq(&wdata->state.lock); 1001 + ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); 1002 + ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); 1003 + ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED); 1004 + spin_unlock_irq(&wdata->state.lock); 1005 + 1006 + if (!ret) 1007 + hid_dbg(wdata->hdev, "state left: !EXT && !MP\n"); 1008 + 1009 + /* poll MP for hotplug events */ 1010 + poll_mp = true; 1011 + 1012 + goto out_release; 1013 + } 1014 + 1015 + /* The trickiest part is if both EXT and MP are active. We cannot read 1016 + * the EXT ID, anymore, because MP is mapped over it. However, we use 1017 + * a handy trick here: 1018 + * - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent 1019 + * MP_PLUGGED might be re-sent again before we are scheduled, but 1020 + * EXT_ACTIVE will stay unset. 1021 + * So it is enough to check for mp_mapped() and MP_ACTIVE and 1022 + * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */ 1023 + if (wdata->state.exttype != WIIMOTE_EXT_NONE && 1024 + wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) { 1025 + type = wiimote_cmd_read_mp_mapped(wdata); 1026 + ret = type != WIIMOTE_MP_NONE; 1027 + ret = ret && type != WIIMOTE_MP_UNKNOWN; 1028 + ret = ret && type != WIIMOTE_MP_SINGLE; 1029 + 1030 + spin_lock_irq(&wdata->state.lock); 1031 + ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED); 1032 + ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE); 1033 + ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE); 1034 + spin_unlock_irq(&wdata->state.lock); 1035 + 1036 + if (!ret) 1037 + hid_dbg(wdata->hdev, "state left: EXT && MP\n"); 1038 + 1039 + /* while MP is mapped, we get EXT_PLUGGED events */ 1040 + poll_mp = false; 1041 + 1042 + goto out_release; 1043 + } 1044 + 1045 + /* unknown state */ 1046 + ret = false; 1047 + 1048 + out_release: 1049 + wiimote_cmd_release(wdata); 1050 + 1051 + /* only poll for MP if requested and if state didn't change */ 1052 + if (ret && poll_mp && !(flags & WIIPROTO_FLAG_BUILTIN_MP) && 1053 + !(flags & WIIPROTO_FLAG_NO_MP)) 1054 + wiimote_init_poll_mp(wdata); 449 1055 450 1056 return ret; 451 1057 } 452 1058 453 - static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode) 1059 + static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = { 1060 + [WIIMOTE_EXT_NONE] = "None", 1061 + [WIIMOTE_EXT_UNKNOWN] = "Unknown", 1062 + [WIIMOTE_EXT_NUNCHUK] = "Nintendo Wii Nunchuk", 1063 + [WIIMOTE_EXT_CLASSIC_CONTROLLER] = "Nintendo Wii Classic Controller", 1064 + [WIIMOTE_EXT_BALANCE_BOARD] = "Nintendo Wii Balance Board", 1065 + [WIIMOTE_EXT_PRO_CONTROLLER] = "Nintendo Wii U Pro Controller", 1066 + }; 1067 + 1068 + /* 1069 + * Handle hotplug events 1070 + * If we receive an hotplug event and the device-check failed, we deinitialize 1071 + * the extension ports, re-read all extension IDs and set the device into 1072 + * the desired state. This involves mapping MP into the main extension 1073 + * registers, setting up extension passthrough modes and initializing the 1074 + * requested extensions. 1075 + */ 1076 + static void wiimote_init_hotplug(struct wiimote_data *wdata) 454 1077 { 455 - int ret; 456 - unsigned long flags; 457 - __u8 format = 0; 458 - static const __u8 data_enable[] = { 0x01 }; 459 - static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01, 460 - 0x00, 0xaa, 0x00, 0x64 }; 461 - static const __u8 data_sens2[] = { 0x63, 0x03 }; 462 - static const __u8 data_fin[] = { 0x08 }; 1078 + __u8 exttype, extdata[6], mpdata[6]; 1079 + __u32 flags; 1080 + bool mp; 463 1081 464 - spin_lock_irqsave(&wdata->state.lock, flags); 1082 + hid_dbg(wdata->hdev, "detect extensions..\n"); 465 1083 466 - if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) { 467 - spin_unlock_irqrestore(&wdata->state.lock, flags); 468 - return 0; 1084 + wiimote_cmd_acquire_noint(wdata); 1085 + 1086 + spin_lock_irq(&wdata->state.lock); 1087 + 1088 + /* get state snapshot that we will then work on */ 1089 + flags = wdata->state.flags; 1090 + 1091 + /* disable event forwarding temporarily */ 1092 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE; 1093 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE; 1094 + 1095 + spin_unlock_irq(&wdata->state.lock); 1096 + 1097 + /* init extension and MP (deactivates current extension or MP) */ 1098 + wiimote_cmd_init_ext(wdata); 1099 + if (flags & WIIPROTO_FLAG_NO_MP) { 1100 + mp = false; 1101 + } else { 1102 + wiimote_cmd_init_mp(wdata); 1103 + mp = wiimote_cmd_read_mp(wdata, mpdata); 469 1104 } 1105 + exttype = wiimote_cmd_read_ext(wdata, extdata); 470 1106 471 - if (mode == 0) { 472 - wdata->state.flags &= ~WIIPROTO_FLAGS_IR; 473 - wiiproto_req_ir1(wdata, 0); 474 - wiiproto_req_ir2(wdata, 0); 475 - wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 476 - spin_unlock_irqrestore(&wdata->state.lock, flags); 477 - return 0; 478 - } 479 - 480 - spin_unlock_irqrestore(&wdata->state.lock, flags); 481 - 482 - ret = wiimote_cmd_acquire(wdata); 483 - if (ret) 484 - return ret; 485 - 486 - /* send PIXEL CLOCK ENABLE cmd first */ 487 - spin_lock_irqsave(&wdata->state.lock, flags); 488 - wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0); 489 - wiiproto_req_ir1(wdata, 0x06); 490 - spin_unlock_irqrestore(&wdata->state.lock, flags); 491 - 492 - ret = wiimote_cmd_wait(wdata); 493 - if (ret) 494 - goto unlock; 495 - if (wdata->state.cmd_err) { 496 - ret = -EIO; 497 - goto unlock; 498 - } 499 - 500 - /* enable IR LOGIC */ 501 - spin_lock_irqsave(&wdata->state.lock, flags); 502 - wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0); 503 - wiiproto_req_ir2(wdata, 0x06); 504 - spin_unlock_irqrestore(&wdata->state.lock, flags); 505 - 506 - ret = wiimote_cmd_wait(wdata); 507 - if (ret) 508 - goto unlock; 509 - if (wdata->state.cmd_err) { 510 - ret = -EIO; 511 - goto unlock; 512 - } 513 - 514 - /* enable IR cam but do not make it send data, yet */ 515 - ret = wiimote_cmd_write(wdata, 0xb00030, data_enable, 516 - sizeof(data_enable)); 517 - if (ret) 518 - goto unlock; 519 - 520 - /* write first sensitivity block */ 521 - ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1, 522 - sizeof(data_sens1)); 523 - if (ret) 524 - goto unlock; 525 - 526 - /* write second sensitivity block */ 527 - ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2, 528 - sizeof(data_sens2)); 529 - if (ret) 530 - goto unlock; 531 - 532 - /* put IR cam into desired state */ 533 - switch (mode) { 534 - case WIIPROTO_FLAG_IR_FULL: 535 - format = 5; 536 - break; 537 - case WIIPROTO_FLAG_IR_EXT: 538 - format = 3; 539 - break; 540 - case WIIPROTO_FLAG_IR_BASIC: 541 - format = 1; 542 - break; 543 - } 544 - ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format)); 545 - if (ret) 546 - goto unlock; 547 - 548 - /* make IR cam send data */ 549 - ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin)); 550 - if (ret) 551 - goto unlock; 552 - 553 - /* request new DRM mode compatible to IR mode */ 554 - spin_lock_irqsave(&wdata->state.lock, flags); 555 - wdata->state.flags &= ~WIIPROTO_FLAGS_IR; 556 - wdata->state.flags |= mode & WIIPROTO_FLAGS_IR; 557 - wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 558 - spin_unlock_irqrestore(&wdata->state.lock, flags); 559 - 560 - unlock: 561 1107 wiimote_cmd_release(wdata); 562 - return ret; 563 - } 564 1108 565 - static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev) 566 - { 567 - struct wiimote_data *wdata; 568 - struct device *dev = led_dev->dev->parent; 569 - int i; 570 - unsigned long flags; 571 - bool value = false; 1109 + /* load/unload extension module if it changed */ 1110 + if (exttype != wdata->state.exttype) { 1111 + /* unload previous extension */ 1112 + wiimote_ext_unload(wdata); 572 1113 573 - wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); 574 - 575 - for (i = 0; i < 4; ++i) { 576 - if (wdata->leds[i] == led_dev) { 577 - spin_lock_irqsave(&wdata->state.lock, flags); 578 - value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1); 579 - spin_unlock_irqrestore(&wdata->state.lock, flags); 580 - break; 1114 + if (exttype == WIIMOTE_EXT_UNKNOWN) { 1115 + hid_info(wdata->hdev, "cannot detect extension; %02x:%02x %02x:%02x %02x:%02x\n", 1116 + extdata[0], extdata[1], extdata[2], 1117 + extdata[3], extdata[4], extdata[5]); 1118 + } else if (exttype == WIIMOTE_EXT_NONE) { 1119 + spin_lock_irq(&wdata->state.lock); 1120 + wdata->state.exttype = WIIMOTE_EXT_NONE; 1121 + spin_unlock_irq(&wdata->state.lock); 1122 + } else { 1123 + hid_info(wdata->hdev, "detected extension: %s\n", 1124 + wiimote_exttype_names[exttype]); 1125 + /* try loading new extension */ 1126 + wiimote_ext_load(wdata, exttype); 581 1127 } 582 1128 } 583 1129 584 - return value ? LED_FULL : LED_OFF; 585 - } 586 - 587 - static void wiimote_leds_set(struct led_classdev *led_dev, 588 - enum led_brightness value) 589 - { 590 - struct wiimote_data *wdata; 591 - struct device *dev = led_dev->dev->parent; 592 - int i; 593 - unsigned long flags; 594 - __u8 state, flag; 595 - 596 - wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev)); 597 - 598 - for (i = 0; i < 4; ++i) { 599 - if (wdata->leds[i] == led_dev) { 600 - flag = WIIPROTO_FLAG_LED(i + 1); 601 - spin_lock_irqsave(&wdata->state.lock, flags); 602 - state = wdata->state.flags; 603 - if (value == LED_OFF) 604 - wiiproto_req_leds(wdata, state & ~flag); 605 - else 606 - wiiproto_req_leds(wdata, state | flag); 607 - spin_unlock_irqrestore(&wdata->state.lock, flags); 608 - break; 1130 + /* load/unload MP module if it changed */ 1131 + if (mp) { 1132 + if (!wdata->state.mp) { 1133 + hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n"); 1134 + wiimote_mp_load(wdata); 609 1135 } 610 - } 611 - } 612 - 613 - static int wiimote_ff_play(struct input_dev *dev, void *data, 614 - struct ff_effect *eff) 615 - { 616 - struct wiimote_data *wdata = input_get_drvdata(dev); 617 - __u8 value; 618 - unsigned long flags; 619 - 620 - /* 621 - * The wiimote supports only a single rumble motor so if any magnitude 622 - * is set to non-zero then we start the rumble motor. If both are set to 623 - * zero, we stop the rumble motor. 624 - */ 625 - 626 - if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) 627 - value = 1; 628 - else 629 - value = 0; 630 - 631 - spin_lock_irqsave(&wdata->state.lock, flags); 632 - wiiproto_req_rumble(wdata, value); 633 - spin_unlock_irqrestore(&wdata->state.lock, flags); 634 - 635 - return 0; 636 - } 637 - 638 - static int wiimote_input_open(struct input_dev *dev) 639 - { 640 - struct wiimote_data *wdata = input_get_drvdata(dev); 641 - 642 - return hid_hw_open(wdata->hdev); 643 - } 644 - 645 - static void wiimote_input_close(struct input_dev *dev) 646 - { 647 - struct wiimote_data *wdata = input_get_drvdata(dev); 648 - 649 - hid_hw_close(wdata->hdev); 650 - } 651 - 652 - static int wiimote_accel_open(struct input_dev *dev) 653 - { 654 - struct wiimote_data *wdata = input_get_drvdata(dev); 655 - int ret; 656 - unsigned long flags; 657 - 658 - ret = hid_hw_open(wdata->hdev); 659 - if (ret) 660 - return ret; 661 - 662 - spin_lock_irqsave(&wdata->state.lock, flags); 663 - wiiproto_req_accel(wdata, true); 664 - spin_unlock_irqrestore(&wdata->state.lock, flags); 665 - 666 - return 0; 667 - } 668 - 669 - static void wiimote_accel_close(struct input_dev *dev) 670 - { 671 - struct wiimote_data *wdata = input_get_drvdata(dev); 672 - unsigned long flags; 673 - 674 - spin_lock_irqsave(&wdata->state.lock, flags); 675 - wiiproto_req_accel(wdata, false); 676 - spin_unlock_irqrestore(&wdata->state.lock, flags); 677 - 678 - hid_hw_close(wdata->hdev); 679 - } 680 - 681 - static int wiimote_ir_open(struct input_dev *dev) 682 - { 683 - struct wiimote_data *wdata = input_get_drvdata(dev); 684 - int ret; 685 - 686 - ret = hid_hw_open(wdata->hdev); 687 - if (ret) 688 - return ret; 689 - 690 - ret = wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC); 691 - if (ret) { 692 - hid_hw_close(wdata->hdev); 693 - return ret; 1136 + } else if (wdata->state.mp) { 1137 + wiimote_mp_unload(wdata); 694 1138 } 695 1139 696 - return 0; 1140 + /* if MP is not used, do not map or activate it */ 1141 + if (!(flags & WIIPROTO_FLAG_MP_USED)) 1142 + mp = false; 1143 + 1144 + /* map MP into main extension registers if used */ 1145 + if (mp) { 1146 + wiimote_cmd_acquire_noint(wdata); 1147 + wiimote_cmd_map_mp(wdata, exttype); 1148 + wiimote_cmd_release(wdata); 1149 + 1150 + /* delete MP hotplug timer */ 1151 + del_timer_sync(&wdata->timer); 1152 + } else { 1153 + /* reschedule MP hotplug timer */ 1154 + if (!(flags & WIIPROTO_FLAG_BUILTIN_MP) && 1155 + !(flags & WIIPROTO_FLAG_NO_MP)) 1156 + mod_timer(&wdata->timer, jiffies + HZ * 4); 1157 + } 1158 + 1159 + spin_lock_irq(&wdata->state.lock); 1160 + 1161 + /* enable data forwarding again and set expected hotplug state */ 1162 + if (mp) { 1163 + wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE; 1164 + if (wdata->state.exttype == WIIMOTE_EXT_NONE) { 1165 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; 1166 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED; 1167 + } else { 1168 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; 1169 + wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED; 1170 + wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE; 1171 + } 1172 + } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) { 1173 + wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE; 1174 + } 1175 + 1176 + /* request status report for hotplug state updates */ 1177 + wiiproto_req_status(wdata); 1178 + 1179 + spin_unlock_irq(&wdata->state.lock); 1180 + 1181 + hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n", 1182 + wdata->state.mp, wdata->state.exttype); 697 1183 } 698 1184 699 - static void wiimote_ir_close(struct input_dev *dev) 1185 + static void wiimote_init_worker(struct work_struct *work) 700 1186 { 701 - struct wiimote_data *wdata = input_get_drvdata(dev); 1187 + struct wiimote_data *wdata = container_of(work, struct wiimote_data, 1188 + init_worker); 1189 + bool changed = false; 702 1190 703 - wiimote_init_ir(wdata, 0); 704 - hid_hw_close(wdata->hdev); 1191 + if (wdata->state.devtype == WIIMOTE_DEV_PENDING) { 1192 + wiimote_init_detect(wdata); 1193 + changed = true; 1194 + } 1195 + 1196 + if (changed || !wiimote_init_check(wdata)) 1197 + wiimote_init_hotplug(wdata); 1198 + 1199 + if (changed) 1200 + kobject_uevent(&wdata->hdev->dev.kobj, KOBJ_CHANGE); 705 1201 } 1202 + 1203 + void __wiimote_schedule(struct wiimote_data *wdata) 1204 + { 1205 + if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING)) 1206 + schedule_work(&wdata->init_worker); 1207 + } 1208 + 1209 + static void wiimote_schedule(struct wiimote_data *wdata) 1210 + { 1211 + unsigned long flags; 1212 + 1213 + spin_lock_irqsave(&wdata->state.lock, flags); 1214 + __wiimote_schedule(wdata); 1215 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1216 + } 1217 + 1218 + static void wiimote_init_timeout(unsigned long arg) 1219 + { 1220 + struct wiimote_data *wdata = (void*)arg; 1221 + 1222 + wiimote_schedule(wdata); 1223 + } 1224 + 1225 + /* protocol handlers */ 706 1226 707 1227 static void handler_keys(struct wiimote_data *wdata, const __u8 *payload) 708 1228 { 709 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT], 710 - !!(payload[0] & 0x01)); 711 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_RIGHT], 712 - !!(payload[0] & 0x02)); 713 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_DOWN], 714 - !!(payload[0] & 0x04)); 715 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_UP], 716 - !!(payload[0] & 0x08)); 717 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_PLUS], 718 - !!(payload[0] & 0x10)); 719 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_TWO], 720 - !!(payload[1] & 0x01)); 721 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_ONE], 722 - !!(payload[1] & 0x02)); 723 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_B], 724 - !!(payload[1] & 0x04)); 725 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_A], 726 - !!(payload[1] & 0x08)); 727 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_MINUS], 728 - !!(payload[1] & 0x10)); 729 - input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_HOME], 730 - !!(payload[1] & 0x80)); 731 - input_sync(wdata->input); 1229 + const __u8 *iter, *mods; 1230 + const struct wiimod_ops *ops; 1231 + 1232 + ops = wiimod_ext_table[wdata->state.exttype]; 1233 + if (ops->in_keys) { 1234 + ops->in_keys(wdata, payload); 1235 + return; 1236 + } 1237 + 1238 + mods = wiimote_devtype_mods[wdata->state.devtype]; 1239 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) { 1240 + ops = wiimod_table[*iter]; 1241 + if (ops->in_keys) { 1242 + ops->in_keys(wdata, payload); 1243 + break; 1244 + } 1245 + } 732 1246 } 733 1247 734 1248 static void handler_accel(struct wiimote_data *wdata, const __u8 *payload) 735 1249 { 736 - __u16 x, y, z; 1250 + const __u8 *iter, *mods; 1251 + const struct wiimod_ops *ops; 737 1252 738 - if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL)) 1253 + ops = wiimod_ext_table[wdata->state.exttype]; 1254 + if (ops->in_accel) { 1255 + ops->in_accel(wdata, payload); 739 1256 return; 740 - 741 - /* 742 - * payload is: BB BB XX YY ZZ 743 - * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ 744 - * contain the upper 8 bits of each value. The lower 2 bits are 745 - * contained in the buttons data BB BB. 746 - * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the 747 - * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y 748 - * accel value and bit 6 is the second bit of the Z value. 749 - * The first bit of Y and Z values is not available and always set to 0. 750 - * 0x200 is returned on no movement. 751 - */ 752 - 753 - x = payload[2] << 2; 754 - y = payload[3] << 2; 755 - z = payload[4] << 2; 756 - 757 - x |= (payload[0] >> 5) & 0x3; 758 - y |= (payload[1] >> 4) & 0x2; 759 - z |= (payload[1] >> 5) & 0x2; 760 - 761 - input_report_abs(wdata->accel, ABS_RX, x - 0x200); 762 - input_report_abs(wdata->accel, ABS_RY, y - 0x200); 763 - input_report_abs(wdata->accel, ABS_RZ, z - 0x200); 764 - input_sync(wdata->accel); 765 - } 766 - 767 - #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ 768 - ABS_HAT0X, ABS_HAT0Y) 769 - #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ 770 - ABS_HAT1X, ABS_HAT1Y) 771 - #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ 772 - ABS_HAT2X, ABS_HAT2Y) 773 - #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \ 774 - ABS_HAT3X, ABS_HAT3Y) 775 - 776 - static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir, 777 - bool packed, __u8 xid, __u8 yid) 778 - { 779 - __u16 x, y; 780 - 781 - if (!(wdata->state.flags & WIIPROTO_FLAGS_IR)) 782 - return; 783 - 784 - /* 785 - * Basic IR data is encoded into 3 bytes. The first two bytes are the 786 - * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits 787 - * of both. 788 - * If data is packed, then the 3rd byte is put first and slightly 789 - * reordered. This allows to interleave packed and non-packed data to 790 - * have two IR sets in 5 bytes instead of 6. 791 - * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev. 792 - */ 793 - 794 - if (packed) { 795 - x = ir[1] | ((ir[0] & 0x03) << 8); 796 - y = ir[2] | ((ir[0] & 0x0c) << 6); 797 - } else { 798 - x = ir[0] | ((ir[2] & 0x30) << 4); 799 - y = ir[1] | ((ir[2] & 0xc0) << 2); 800 1257 } 801 1258 802 - input_report_abs(wdata->ir, xid, x); 803 - input_report_abs(wdata->ir, yid, y); 1259 + mods = wiimote_devtype_mods[wdata->state.devtype]; 1260 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) { 1261 + ops = wiimod_table[*iter]; 1262 + if (ops->in_accel) { 1263 + ops->in_accel(wdata, payload); 1264 + break; 1265 + } 1266 + } 1267 + } 1268 + 1269 + static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len) 1270 + { 1271 + if (!ops->in_ext) 1272 + return false; 1273 + if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8) 1274 + return false; 1275 + if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16) 1276 + return false; 1277 + 1278 + return true; 1279 + } 1280 + 1281 + static void handler_ext(struct wiimote_data *wdata, const __u8 *payload, 1282 + size_t len) 1283 + { 1284 + static const __u8 invalid[21] = { 0xff, 0xff, 0xff, 0xff, 1285 + 0xff, 0xff, 0xff, 0xff, 1286 + 0xff, 0xff, 0xff, 0xff, 1287 + 0xff, 0xff, 0xff, 0xff, 1288 + 0xff, 0xff, 0xff, 0xff, 1289 + 0xff }; 1290 + const __u8 *iter, *mods; 1291 + const struct wiimod_ops *ops; 1292 + bool is_mp; 1293 + 1294 + if (len > 21) 1295 + len = 21; 1296 + if (len < 6 || !memcmp(payload, invalid, len)) 1297 + return; 1298 + 1299 + /* if MP is active, track MP slot hotplugging */ 1300 + if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) { 1301 + /* this bit is set for invalid events (eg. during hotplug) */ 1302 + if (payload[5] & 0x01) 1303 + return; 1304 + 1305 + if (payload[4] & 0x01) { 1306 + if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) { 1307 + hid_dbg(wdata->hdev, "MP hotplug: 1\n"); 1308 + wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED; 1309 + __wiimote_schedule(wdata); 1310 + } 1311 + } else { 1312 + if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) { 1313 + hid_dbg(wdata->hdev, "MP hotplug: 0\n"); 1314 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED; 1315 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE; 1316 + __wiimote_schedule(wdata); 1317 + } 1318 + } 1319 + 1320 + /* detect MP data that is sent interleaved with EXT data */ 1321 + is_mp = payload[5] & 0x02; 1322 + } else { 1323 + is_mp = false; 1324 + } 1325 + 1326 + /* ignore EXT events if no extension is active */ 1327 + if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp) 1328 + return; 1329 + 1330 + /* try forwarding to extension handler, first */ 1331 + ops = wiimod_ext_table[wdata->state.exttype]; 1332 + if (is_mp && ops->in_mp) { 1333 + ops->in_mp(wdata, payload); 1334 + return; 1335 + } else if (!is_mp && valid_ext_handler(ops, len)) { 1336 + ops->in_ext(wdata, payload); 1337 + return; 1338 + } 1339 + 1340 + /* try forwarding to MP handler */ 1341 + ops = &wiimod_mp; 1342 + if (is_mp && ops->in_mp) { 1343 + ops->in_mp(wdata, payload); 1344 + return; 1345 + } else if (!is_mp && valid_ext_handler(ops, len)) { 1346 + ops->in_ext(wdata, payload); 1347 + return; 1348 + } 1349 + 1350 + /* try forwarding to loaded modules */ 1351 + mods = wiimote_devtype_mods[wdata->state.devtype]; 1352 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) { 1353 + ops = wiimod_table[*iter]; 1354 + if (is_mp && ops->in_mp) { 1355 + ops->in_mp(wdata, payload); 1356 + return; 1357 + } else if (!is_mp && valid_ext_handler(ops, len)) { 1358 + ops->in_ext(wdata, payload); 1359 + return; 1360 + } 1361 + } 1362 + } 1363 + 1364 + #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0) 1365 + #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1) 1366 + #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2) 1367 + #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3) 1368 + 1369 + static void handler_ir(struct wiimote_data *wdata, const __u8 *payload, 1370 + bool packed, unsigned int id) 1371 + { 1372 + const __u8 *iter, *mods; 1373 + const struct wiimod_ops *ops; 1374 + 1375 + ops = wiimod_ext_table[wdata->state.exttype]; 1376 + if (ops->in_ir) { 1377 + ops->in_ir(wdata, payload, packed, id); 1378 + return; 1379 + } 1380 + 1381 + mods = wiimote_devtype_mods[wdata->state.devtype]; 1382 + for (iter = mods; *iter != WIIMOD_NULL; ++iter) { 1383 + ops = wiimod_table[*iter]; 1384 + if (ops->in_ir) { 1385 + ops->in_ir(wdata, payload, packed, id); 1386 + break; 1387 + } 1388 + } 804 1389 } 805 1390 806 1391 /* reduced status report with "BB BB" key data only */ ··· 1417 804 { 1418 805 handler_status_K(wdata, payload); 1419 806 1420 - wiiext_event(wdata, payload[2] & 0x02); 1421 - 1422 - if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) { 1423 - wdata->state.cmd_battery = payload[5]; 1424 - wiimote_cmd_complete(wdata); 807 + /* update extension status */ 808 + if (payload[2] & 0x02) { 809 + if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) { 810 + hid_dbg(wdata->hdev, "EXT hotplug: 1\n"); 811 + wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED; 812 + __wiimote_schedule(wdata); 813 + } 814 + } else { 815 + if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) { 816 + hid_dbg(wdata->hdev, "EXT hotplug: 0\n"); 817 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED; 818 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED; 819 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE; 820 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE; 821 + __wiimote_schedule(wdata); 822 + } 1425 823 } 824 + 825 + wdata->state.cmd_battery = payload[5]; 826 + if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0)) 827 + wiimote_cmd_complete(wdata); 1426 828 } 1427 829 1428 830 /* reduced generic report with "BB BB" key data only */ ··· 1492 864 static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload) 1493 865 { 1494 866 handler_keys(wdata, payload); 1495 - wiiext_handle(wdata, &payload[2]); 867 + handler_ext(wdata, &payload[2], 8); 1496 868 } 1497 869 1498 870 static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload) ··· 1503 875 ir_to_input1(wdata, &payload[8], false); 1504 876 ir_to_input2(wdata, &payload[11], false); 1505 877 ir_to_input3(wdata, &payload[14], false); 1506 - input_sync(wdata->ir); 1507 878 } 1508 879 1509 880 static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload) 1510 881 { 1511 882 handler_keys(wdata, payload); 1512 - wiiext_handle(wdata, &payload[2]); 883 + handler_ext(wdata, &payload[2], 19); 1513 884 } 1514 885 1515 886 static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload) ··· 1518 891 ir_to_input1(wdata, &payload[4], true); 1519 892 ir_to_input2(wdata, &payload[7], false); 1520 893 ir_to_input3(wdata, &payload[9], true); 1521 - input_sync(wdata->ir); 1522 - wiiext_handle(wdata, &payload[12]); 894 + handler_ext(wdata, &payload[12], 9); 1523 895 } 1524 896 1525 897 static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload) 1526 898 { 1527 899 handler_keys(wdata, payload); 1528 900 handler_accel(wdata, payload); 1529 - wiiext_handle(wdata, &payload[5]); 901 + handler_ext(wdata, &payload[5], 16); 1530 902 } 1531 903 1532 904 static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload) ··· 1536 910 ir_to_input1(wdata, &payload[7], true); 1537 911 ir_to_input2(wdata, &payload[10], false); 1538 912 ir_to_input3(wdata, &payload[12], true); 1539 - input_sync(wdata->ir); 1540 - wiiext_handle(wdata, &payload[15]); 913 + handler_ext(wdata, &payload[15], 6); 1541 914 } 1542 915 1543 916 static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload) 1544 917 { 1545 - wiiext_handle(wdata, payload); 918 + handler_ext(wdata, payload, 21); 1546 919 } 1547 920 1548 921 static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload) ··· 1554 929 1555 930 ir_to_input0(wdata, &payload[3], false); 1556 931 ir_to_input1(wdata, &payload[12], false); 1557 - input_sync(wdata->ir); 1558 932 } 1559 933 1560 934 static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload) ··· 1574 950 1575 951 ir_to_input2(wdata, &payload[3], false); 1576 952 ir_to_input3(wdata, &payload[12], false); 1577 - input_sync(wdata->ir); 1578 953 } 1579 954 1580 955 struct wiiproto_handler { ··· 1640 1017 return 0; 1641 1018 } 1642 1019 1643 - static void wiimote_leds_destroy(struct wiimote_data *wdata) 1020 + static ssize_t wiimote_ext_show(struct device *dev, 1021 + struct device_attribute *attr, 1022 + char *buf) 1644 1023 { 1645 - int i; 1646 - struct led_classdev *led; 1024 + struct wiimote_data *wdata = dev_to_wii(dev); 1025 + __u8 type; 1026 + unsigned long flags; 1647 1027 1648 - for (i = 0; i < 4; ++i) { 1649 - if (wdata->leds[i]) { 1650 - led = wdata->leds[i]; 1651 - wdata->leds[i] = NULL; 1652 - led_classdev_unregister(led); 1653 - kfree(led); 1654 - } 1028 + spin_lock_irqsave(&wdata->state.lock, flags); 1029 + type = wdata->state.exttype; 1030 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1031 + 1032 + switch (type) { 1033 + case WIIMOTE_EXT_NONE: 1034 + return sprintf(buf, "none\n"); 1035 + case WIIMOTE_EXT_NUNCHUK: 1036 + return sprintf(buf, "nunchuk\n"); 1037 + case WIIMOTE_EXT_CLASSIC_CONTROLLER: 1038 + return sprintf(buf, "classic\n"); 1039 + case WIIMOTE_EXT_BALANCE_BOARD: 1040 + return sprintf(buf, "balanceboard\n"); 1041 + case WIIMOTE_EXT_PRO_CONTROLLER: 1042 + return sprintf(buf, "procontroller\n"); 1043 + case WIIMOTE_EXT_UNKNOWN: 1044 + /* fallthrough */ 1045 + default: 1046 + return sprintf(buf, "unknown\n"); 1655 1047 } 1656 1048 } 1657 1049 1658 - static int wiimote_leds_create(struct wiimote_data *wdata) 1050 + static ssize_t wiimote_ext_store(struct device *dev, 1051 + struct device_attribute *attr, 1052 + const char *buf, size_t count) 1659 1053 { 1660 - int i, ret; 1661 - struct device *dev = &wdata->hdev->dev; 1662 - size_t namesz = strlen(dev_name(dev)) + 9; 1663 - struct led_classdev *led; 1664 - char *name; 1054 + struct wiimote_data *wdata = dev_to_wii(dev); 1665 1055 1666 - for (i = 0; i < 4; ++i) { 1667 - led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL); 1668 - if (!led) { 1669 - ret = -ENOMEM; 1670 - goto err; 1671 - } 1672 - name = (void*)&led[1]; 1673 - snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i); 1674 - led->name = name; 1675 - led->brightness = 0; 1676 - led->max_brightness = 1; 1677 - led->brightness_get = wiimote_leds_get; 1678 - led->brightness_set = wiimote_leds_set; 1679 - 1680 - ret = led_classdev_register(dev, led); 1681 - if (ret) { 1682 - kfree(led); 1683 - goto err; 1684 - } 1685 - wdata->leds[i] = led; 1056 + if (!strcmp(buf, "scan")) { 1057 + wiimote_schedule(wdata); 1058 + } else { 1059 + return -EINVAL; 1686 1060 } 1687 1061 1688 - return 0; 1689 - 1690 - err: 1691 - wiimote_leds_destroy(wdata); 1692 - return ret; 1062 + return strnlen(buf, PAGE_SIZE); 1693 1063 } 1064 + 1065 + static DEVICE_ATTR(extension, S_IRUGO | S_IWUSR | S_IWGRP, wiimote_ext_show, 1066 + wiimote_ext_store); 1067 + 1068 + static ssize_t wiimote_dev_show(struct device *dev, 1069 + struct device_attribute *attr, 1070 + char *buf) 1071 + { 1072 + struct wiimote_data *wdata = dev_to_wii(dev); 1073 + __u8 type; 1074 + unsigned long flags; 1075 + 1076 + spin_lock_irqsave(&wdata->state.lock, flags); 1077 + type = wdata->state.devtype; 1078 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1079 + 1080 + switch (type) { 1081 + case WIIMOTE_DEV_GENERIC: 1082 + return sprintf(buf, "generic\n"); 1083 + case WIIMOTE_DEV_GEN10: 1084 + return sprintf(buf, "gen10\n"); 1085 + case WIIMOTE_DEV_GEN20: 1086 + return sprintf(buf, "gen20\n"); 1087 + case WIIMOTE_DEV_BALANCE_BOARD: 1088 + return sprintf(buf, "balanceboard\n"); 1089 + case WIIMOTE_DEV_PRO_CONTROLLER: 1090 + return sprintf(buf, "procontroller\n"); 1091 + case WIIMOTE_DEV_PENDING: 1092 + return sprintf(buf, "pending\n"); 1093 + case WIIMOTE_DEV_UNKNOWN: 1094 + /* fallthrough */ 1095 + default: 1096 + return sprintf(buf, "unknown\n"); 1097 + } 1098 + } 1099 + 1100 + static DEVICE_ATTR(devtype, S_IRUGO, wiimote_dev_show, NULL); 1694 1101 1695 1102 static struct wiimote_data *wiimote_create(struct hid_device *hdev) 1696 1103 { 1697 1104 struct wiimote_data *wdata; 1698 - int i; 1699 1105 1700 1106 wdata = kzalloc(sizeof(*wdata), GFP_KERNEL); 1701 1107 if (!wdata) 1702 1108 return NULL; 1703 1109 1704 - wdata->input = input_allocate_device(); 1705 - if (!wdata->input) 1706 - goto err; 1707 - 1708 1110 wdata->hdev = hdev; 1709 1111 hid_set_drvdata(hdev, wdata); 1710 1112 1711 - input_set_drvdata(wdata->input, wdata); 1712 - wdata->input->open = wiimote_input_open; 1713 - wdata->input->close = wiimote_input_close; 1714 - wdata->input->dev.parent = &wdata->hdev->dev; 1715 - wdata->input->id.bustype = wdata->hdev->bus; 1716 - wdata->input->id.vendor = wdata->hdev->vendor; 1717 - wdata->input->id.product = wdata->hdev->product; 1718 - wdata->input->id.version = wdata->hdev->version; 1719 - wdata->input->name = WIIMOTE_NAME; 1720 - 1721 - set_bit(EV_KEY, wdata->input->evbit); 1722 - for (i = 0; i < WIIPROTO_KEY_COUNT; ++i) 1723 - set_bit(wiiproto_keymap[i], wdata->input->keybit); 1724 - 1725 - set_bit(FF_RUMBLE, wdata->input->ffbit); 1726 - if (input_ff_create_memless(wdata->input, NULL, wiimote_ff_play)) 1727 - goto err_input; 1728 - 1729 - wdata->accel = input_allocate_device(); 1730 - if (!wdata->accel) 1731 - goto err_input; 1732 - 1733 - input_set_drvdata(wdata->accel, wdata); 1734 - wdata->accel->open = wiimote_accel_open; 1735 - wdata->accel->close = wiimote_accel_close; 1736 - wdata->accel->dev.parent = &wdata->hdev->dev; 1737 - wdata->accel->id.bustype = wdata->hdev->bus; 1738 - wdata->accel->id.vendor = wdata->hdev->vendor; 1739 - wdata->accel->id.product = wdata->hdev->product; 1740 - wdata->accel->id.version = wdata->hdev->version; 1741 - wdata->accel->name = WIIMOTE_NAME " Accelerometer"; 1742 - 1743 - set_bit(EV_ABS, wdata->accel->evbit); 1744 - set_bit(ABS_RX, wdata->accel->absbit); 1745 - set_bit(ABS_RY, wdata->accel->absbit); 1746 - set_bit(ABS_RZ, wdata->accel->absbit); 1747 - input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4); 1748 - input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4); 1749 - input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4); 1750 - 1751 - wdata->ir = input_allocate_device(); 1752 - if (!wdata->ir) 1753 - goto err_ir; 1754 - 1755 - input_set_drvdata(wdata->ir, wdata); 1756 - wdata->ir->open = wiimote_ir_open; 1757 - wdata->ir->close = wiimote_ir_close; 1758 - wdata->ir->dev.parent = &wdata->hdev->dev; 1759 - wdata->ir->id.bustype = wdata->hdev->bus; 1760 - wdata->ir->id.vendor = wdata->hdev->vendor; 1761 - wdata->ir->id.product = wdata->hdev->product; 1762 - wdata->ir->id.version = wdata->hdev->version; 1763 - wdata->ir->name = WIIMOTE_NAME " IR"; 1764 - 1765 - set_bit(EV_ABS, wdata->ir->evbit); 1766 - set_bit(ABS_HAT0X, wdata->ir->absbit); 1767 - set_bit(ABS_HAT0Y, wdata->ir->absbit); 1768 - set_bit(ABS_HAT1X, wdata->ir->absbit); 1769 - set_bit(ABS_HAT1Y, wdata->ir->absbit); 1770 - set_bit(ABS_HAT2X, wdata->ir->absbit); 1771 - set_bit(ABS_HAT2Y, wdata->ir->absbit); 1772 - set_bit(ABS_HAT3X, wdata->ir->absbit); 1773 - set_bit(ABS_HAT3Y, wdata->ir->absbit); 1774 - input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4); 1775 - input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4); 1776 - input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4); 1777 - input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4); 1778 - input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4); 1779 - input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4); 1780 - input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4); 1781 - input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4); 1782 - 1783 - spin_lock_init(&wdata->qlock); 1784 - INIT_WORK(&wdata->worker, wiimote_worker); 1113 + spin_lock_init(&wdata->queue.lock); 1114 + INIT_WORK(&wdata->queue.worker, wiimote_queue_worker); 1785 1115 1786 1116 spin_lock_init(&wdata->state.lock); 1787 1117 init_completion(&wdata->state.ready); 1788 1118 mutex_init(&wdata->state.sync); 1789 1119 wdata->state.drm = WIIPROTO_REQ_DRM_K; 1120 + wdata->state.cmd_battery = 0xff; 1121 + 1122 + INIT_WORK(&wdata->init_worker, wiimote_init_worker); 1123 + setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata); 1790 1124 1791 1125 return wdata; 1792 - 1793 - err_ir: 1794 - input_free_device(wdata->accel); 1795 - err_input: 1796 - input_free_device(wdata->input); 1797 - err: 1798 - kfree(wdata); 1799 - return NULL; 1800 1126 } 1801 1127 1802 1128 static void wiimote_destroy(struct wiimote_data *wdata) 1803 1129 { 1804 - wiidebug_deinit(wdata); 1805 - wiiext_deinit(wdata); 1806 - wiimote_leds_destroy(wdata); 1130 + unsigned long flags; 1807 1131 1808 - power_supply_unregister(&wdata->battery); 1809 - kfree(wdata->battery.name); 1810 - input_unregister_device(wdata->accel); 1811 - input_unregister_device(wdata->ir); 1812 - input_unregister_device(wdata->input); 1813 - cancel_work_sync(&wdata->worker); 1132 + wiidebug_deinit(wdata); 1133 + 1134 + /* prevent init_worker from being scheduled again */ 1135 + spin_lock_irqsave(&wdata->state.lock, flags); 1136 + wdata->state.flags |= WIIPROTO_FLAG_EXITING; 1137 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1138 + 1139 + cancel_work_sync(&wdata->init_worker); 1140 + del_timer_sync(&wdata->timer); 1141 + 1142 + device_remove_file(&wdata->hdev->dev, &dev_attr_devtype); 1143 + device_remove_file(&wdata->hdev->dev, &dev_attr_extension); 1144 + 1145 + wiimote_mp_unload(wdata); 1146 + wiimote_ext_unload(wdata); 1147 + wiimote_modules_unload(wdata); 1148 + cancel_work_sync(&wdata->queue.worker); 1149 + hid_hw_close(wdata->hdev); 1814 1150 hid_hw_stop(wdata->hdev); 1815 1151 1816 1152 kfree(wdata); ··· 1801 1219 goto err; 1802 1220 } 1803 1221 1804 - ret = input_register_device(wdata->accel); 1222 + ret = hid_hw_open(hdev); 1805 1223 if (ret) { 1806 - hid_err(hdev, "Cannot register input device\n"); 1224 + hid_err(hdev, "cannot start hardware I/O\n"); 1807 1225 goto err_stop; 1808 1226 } 1809 1227 1810 - ret = input_register_device(wdata->ir); 1228 + ret = device_create_file(&hdev->dev, &dev_attr_extension); 1811 1229 if (ret) { 1812 - hid_err(hdev, "Cannot register input device\n"); 1813 - goto err_ir; 1230 + hid_err(hdev, "cannot create sysfs attribute\n"); 1231 + goto err_close; 1814 1232 } 1815 1233 1816 - ret = input_register_device(wdata->input); 1234 + ret = device_create_file(&hdev->dev, &dev_attr_devtype); 1817 1235 if (ret) { 1818 - hid_err(hdev, "Cannot register input device\n"); 1819 - goto err_input; 1236 + hid_err(hdev, "cannot create sysfs attribute\n"); 1237 + goto err_ext; 1820 1238 } 1821 - 1822 - wdata->battery.properties = wiimote_battery_props; 1823 - wdata->battery.num_properties = ARRAY_SIZE(wiimote_battery_props); 1824 - wdata->battery.get_property = wiimote_battery_get_property; 1825 - wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY; 1826 - wdata->battery.use_for_apm = 0; 1827 - wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s", 1828 - wdata->hdev->uniq); 1829 - if (!wdata->battery.name) { 1830 - ret = -ENOMEM; 1831 - goto err_battery_name; 1832 - } 1833 - 1834 - ret = power_supply_register(&wdata->hdev->dev, &wdata->battery); 1835 - if (ret) { 1836 - hid_err(hdev, "Cannot register battery device\n"); 1837 - goto err_battery; 1838 - } 1839 - 1840 - power_supply_powers(&wdata->battery, &hdev->dev); 1841 - 1842 - ret = wiimote_leds_create(wdata); 1843 - if (ret) 1844 - goto err_free; 1845 - 1846 - ret = wiiext_init(wdata); 1847 - if (ret) 1848 - goto err_free; 1849 1239 1850 1240 ret = wiidebug_init(wdata); 1851 1241 if (ret) ··· 1825 1271 1826 1272 hid_info(hdev, "New device registered\n"); 1827 1273 1828 - /* by default set led1 after device initialization */ 1829 - spin_lock_irq(&wdata->state.lock); 1830 - wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1); 1831 - spin_unlock_irq(&wdata->state.lock); 1274 + /* schedule device detection */ 1275 + wiimote_schedule(wdata); 1832 1276 1833 1277 return 0; 1834 1278 ··· 1834 1282 wiimote_destroy(wdata); 1835 1283 return ret; 1836 1284 1837 - err_battery: 1838 - kfree(wdata->battery.name); 1839 - err_battery_name: 1840 - input_unregister_device(wdata->input); 1841 - wdata->input = NULL; 1842 - err_input: 1843 - input_unregister_device(wdata->ir); 1844 - wdata->ir = NULL; 1845 - err_ir: 1846 - input_unregister_device(wdata->accel); 1847 - wdata->accel = NULL; 1285 + err_ext: 1286 + device_remove_file(&wdata->hdev->dev, &dev_attr_extension); 1287 + err_close: 1288 + hid_hw_close(hdev); 1848 1289 err_stop: 1849 1290 hid_hw_stop(hdev); 1850 1291 err: 1851 1292 input_free_device(wdata->ir); 1852 1293 input_free_device(wdata->accel); 1853 - input_free_device(wdata->input); 1854 1294 kfree(wdata); 1855 1295 return ret; 1856 1296 } ··· 1875 1331 1876 1332 MODULE_LICENSE("GPL"); 1877 1333 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>"); 1878 - MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver"); 1334 + MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");
+9 -5
drivers/hid/hid-wiimote-debug.c
··· 1 1 /* 2 - * Debug support for HID Nintendo Wiimote devices 3 - * Copyright (c) 2011 David Herrmann 2 + * Debug support for HID Nintendo Wii / Wii U peripherals 3 + * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com> 4 4 */ 5 5 6 6 /* ··· 127 127 static ssize_t wiidebug_drm_write(struct file *f, const char __user *u, 128 128 size_t s, loff_t *off) 129 129 { 130 - struct wiimote_debug *dbg = f->private_data; 130 + struct seq_file *sf = f->private_data; 131 + struct wiimote_debug *dbg = sf->private; 131 132 unsigned long flags; 132 133 char buf[16]; 133 134 ssize_t len; ··· 141 140 if (copy_from_user(buf, u, len)) 142 141 return -EFAULT; 143 142 144 - buf[15] = 0; 143 + buf[len] = 0; 145 144 146 145 for (i = 0; i < WIIPROTO_REQ_MAX; ++i) { 147 146 if (!wiidebug_drmmap[i]) ··· 151 150 } 152 151 153 152 if (i == WIIPROTO_REQ_MAX) 154 - i = simple_strtoul(buf, NULL, 10); 153 + i = simple_strtoul(buf, NULL, 16); 155 154 156 155 spin_lock_irqsave(&dbg->wdata->state.lock, flags); 156 + dbg->wdata->state.flags &= ~WIIPROTO_FLAG_DRM_LOCKED; 157 157 wiiproto_req_drm(dbg->wdata, (__u8) i); 158 + if (i != WIIPROTO_REQ_NULL) 159 + dbg->wdata->state.flags |= WIIPROTO_FLAG_DRM_LOCKED; 158 160 spin_unlock_irqrestore(&dbg->wdata->state.lock, flags); 159 161 160 162 return len;
-849
drivers/hid/hid-wiimote-ext.c
··· 1 - /* 2 - * HID driver for Nintendo Wiimote extension devices 3 - * Copyright (c) 2011 David Herrmann 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 - #include <linux/atomic.h> 14 - #include <linux/module.h> 15 - #include <linux/spinlock.h> 16 - #include <linux/workqueue.h> 17 - #include "hid-wiimote.h" 18 - 19 - struct wiimote_ext { 20 - struct wiimote_data *wdata; 21 - struct work_struct worker; 22 - struct input_dev *input; 23 - struct input_dev *mp_input; 24 - 25 - atomic_t opened; 26 - atomic_t mp_opened; 27 - bool plugged; 28 - bool mp_plugged; 29 - bool motionp; 30 - __u8 ext_type; 31 - __u16 calib[4][3]; 32 - }; 33 - 34 - enum wiiext_type { 35 - WIIEXT_NONE, /* placeholder */ 36 - WIIEXT_CLASSIC, /* Nintendo classic controller */ 37 - WIIEXT_NUNCHUCK, /* Nintendo nunchuck controller */ 38 - WIIEXT_BALANCE_BOARD, /* Nintendo balance board controller */ 39 - }; 40 - 41 - enum wiiext_keys { 42 - WIIEXT_KEY_C, 43 - WIIEXT_KEY_Z, 44 - WIIEXT_KEY_A, 45 - WIIEXT_KEY_B, 46 - WIIEXT_KEY_X, 47 - WIIEXT_KEY_Y, 48 - WIIEXT_KEY_ZL, 49 - WIIEXT_KEY_ZR, 50 - WIIEXT_KEY_PLUS, 51 - WIIEXT_KEY_MINUS, 52 - WIIEXT_KEY_HOME, 53 - WIIEXT_KEY_LEFT, 54 - WIIEXT_KEY_RIGHT, 55 - WIIEXT_KEY_UP, 56 - WIIEXT_KEY_DOWN, 57 - WIIEXT_KEY_LT, 58 - WIIEXT_KEY_RT, 59 - WIIEXT_KEY_COUNT 60 - }; 61 - 62 - static __u16 wiiext_keymap[] = { 63 - BTN_C, /* WIIEXT_KEY_C */ 64 - BTN_Z, /* WIIEXT_KEY_Z */ 65 - BTN_A, /* WIIEXT_KEY_A */ 66 - BTN_B, /* WIIEXT_KEY_B */ 67 - BTN_X, /* WIIEXT_KEY_X */ 68 - BTN_Y, /* WIIEXT_KEY_Y */ 69 - BTN_TL2, /* WIIEXT_KEY_ZL */ 70 - BTN_TR2, /* WIIEXT_KEY_ZR */ 71 - KEY_NEXT, /* WIIEXT_KEY_PLUS */ 72 - KEY_PREVIOUS, /* WIIEXT_KEY_MINUS */ 73 - BTN_MODE, /* WIIEXT_KEY_HOME */ 74 - KEY_LEFT, /* WIIEXT_KEY_LEFT */ 75 - KEY_RIGHT, /* WIIEXT_KEY_RIGHT */ 76 - KEY_UP, /* WIIEXT_KEY_UP */ 77 - KEY_DOWN, /* WIIEXT_KEY_DOWN */ 78 - BTN_TL, /* WIIEXT_KEY_LT */ 79 - BTN_TR, /* WIIEXT_KEY_RT */ 80 - }; 81 - 82 - /* disable all extensions */ 83 - static void ext_disable(struct wiimote_ext *ext) 84 - { 85 - unsigned long flags; 86 - __u8 wmem = 0x55; 87 - 88 - if (!wiimote_cmd_acquire(ext->wdata)) { 89 - wiimote_cmd_write(ext->wdata, 0xa400f0, &wmem, sizeof(wmem)); 90 - wiimote_cmd_release(ext->wdata); 91 - } 92 - 93 - spin_lock_irqsave(&ext->wdata->state.lock, flags); 94 - ext->motionp = false; 95 - ext->ext_type = WIIEXT_NONE; 96 - wiiproto_req_drm(ext->wdata, WIIPROTO_REQ_NULL); 97 - spin_unlock_irqrestore(&ext->wdata->state.lock, flags); 98 - } 99 - 100 - static bool motionp_read(struct wiimote_ext *ext) 101 - { 102 - __u8 rmem[2], wmem; 103 - ssize_t ret; 104 - bool avail = false; 105 - 106 - if (!atomic_read(&ext->mp_opened)) 107 - return false; 108 - 109 - if (wiimote_cmd_acquire(ext->wdata)) 110 - return false; 111 - 112 - /* initialize motion plus */ 113 - wmem = 0x55; 114 - ret = wiimote_cmd_write(ext->wdata, 0xa600f0, &wmem, sizeof(wmem)); 115 - if (ret) 116 - goto error; 117 - 118 - /* read motion plus ID */ 119 - ret = wiimote_cmd_read(ext->wdata, 0xa600fe, rmem, 2); 120 - if (ret == 2 || rmem[1] == 0x5) 121 - avail = true; 122 - 123 - error: 124 - wiimote_cmd_release(ext->wdata); 125 - return avail; 126 - } 127 - 128 - static __u8 ext_read(struct wiimote_ext *ext) 129 - { 130 - ssize_t ret; 131 - __u8 buf[24], i, j, offs = 0; 132 - __u8 rmem[2], wmem; 133 - __u8 type = WIIEXT_NONE; 134 - 135 - if (!ext->plugged || !atomic_read(&ext->opened)) 136 - return WIIEXT_NONE; 137 - 138 - if (wiimote_cmd_acquire(ext->wdata)) 139 - return WIIEXT_NONE; 140 - 141 - /* initialize extension */ 142 - wmem = 0x55; 143 - ret = wiimote_cmd_write(ext->wdata, 0xa400f0, &wmem, sizeof(wmem)); 144 - if (!ret) { 145 - /* disable encryption */ 146 - wmem = 0x0; 147 - wiimote_cmd_write(ext->wdata, 0xa400fb, &wmem, sizeof(wmem)); 148 - } 149 - 150 - /* read extension ID */ 151 - ret = wiimote_cmd_read(ext->wdata, 0xa400fe, rmem, 2); 152 - if (ret == 2) { 153 - if (rmem[0] == 0 && rmem[1] == 0) 154 - type = WIIEXT_NUNCHUCK; 155 - else if (rmem[0] == 0x01 && rmem[1] == 0x01) 156 - type = WIIEXT_CLASSIC; 157 - else if (rmem[0] == 0x04 && rmem[1] == 0x02) 158 - type = WIIEXT_BALANCE_BOARD; 159 - } 160 - 161 - /* get balance board calibration data */ 162 - if (type == WIIEXT_BALANCE_BOARD) { 163 - ret = wiimote_cmd_read(ext->wdata, 0xa40024, buf, 12); 164 - ret += wiimote_cmd_read(ext->wdata, 0xa40024 + 12, 165 - buf + 12, 12); 166 - 167 - if (ret != 24) { 168 - type = WIIEXT_NONE; 169 - } else { 170 - for (i = 0; i < 3; i++) { 171 - for (j = 0; j < 4; j++) { 172 - ext->calib[j][i] = buf[offs]; 173 - ext->calib[j][i] <<= 8; 174 - ext->calib[j][i] |= buf[offs + 1]; 175 - offs += 2; 176 - } 177 - } 178 - } 179 - } 180 - 181 - wiimote_cmd_release(ext->wdata); 182 - 183 - return type; 184 - } 185 - 186 - static void ext_enable(struct wiimote_ext *ext, bool motionp, __u8 ext_type) 187 - { 188 - unsigned long flags; 189 - __u8 wmem; 190 - int ret; 191 - 192 - if (motionp) { 193 - if (wiimote_cmd_acquire(ext->wdata)) 194 - return; 195 - 196 - if (ext_type == WIIEXT_CLASSIC) 197 - wmem = 0x07; 198 - else if (ext_type == WIIEXT_NUNCHUCK) 199 - wmem = 0x05; 200 - else 201 - wmem = 0x04; 202 - 203 - ret = wiimote_cmd_write(ext->wdata, 0xa600fe, &wmem, sizeof(wmem)); 204 - wiimote_cmd_release(ext->wdata); 205 - if (ret) 206 - return; 207 - } 208 - 209 - spin_lock_irqsave(&ext->wdata->state.lock, flags); 210 - ext->motionp = motionp; 211 - ext->ext_type = ext_type; 212 - wiiproto_req_drm(ext->wdata, WIIPROTO_REQ_NULL); 213 - spin_unlock_irqrestore(&ext->wdata->state.lock, flags); 214 - } 215 - 216 - static void wiiext_worker(struct work_struct *work) 217 - { 218 - struct wiimote_ext *ext = container_of(work, struct wiimote_ext, 219 - worker); 220 - bool motionp; 221 - __u8 ext_type; 222 - 223 - ext_disable(ext); 224 - motionp = motionp_read(ext); 225 - ext_type = ext_read(ext); 226 - ext_enable(ext, motionp, ext_type); 227 - } 228 - 229 - /* schedule work only once, otherwise mark for reschedule */ 230 - static void wiiext_schedule(struct wiimote_ext *ext) 231 - { 232 - schedule_work(&ext->worker); 233 - } 234 - 235 - /* 236 - * Reacts on extension port events 237 - * Whenever the driver gets an event from the wiimote that an extension has been 238 - * plugged or unplugged, this funtion shall be called. It checks what extensions 239 - * are connected and initializes and activates them. 240 - * This can be called in atomic context. The initialization is done in a 241 - * separate worker thread. The state.lock spinlock must be held by the caller. 242 - */ 243 - void wiiext_event(struct wiimote_data *wdata, bool plugged) 244 - { 245 - if (!wdata->ext) 246 - return; 247 - 248 - if (wdata->ext->plugged == plugged) 249 - return; 250 - 251 - wdata->ext->plugged = plugged; 252 - 253 - if (!plugged) 254 - wdata->ext->mp_plugged = false; 255 - 256 - /* 257 - * We need to call wiiext_schedule(wdata->ext) here, however, the 258 - * extension initialization logic is not fully understood and so 259 - * automatic initialization is not supported, yet. 260 - */ 261 - } 262 - 263 - /* 264 - * Returns true if the current DRM mode should contain extension data and false 265 - * if there is no interest in extension data. 266 - * All supported extensions send 6 byte extension data so any DRM that contains 267 - * extension bytes is fine. 268 - * The caller must hold the state.lock spinlock. 269 - */ 270 - bool wiiext_active(struct wiimote_data *wdata) 271 - { 272 - if (!wdata->ext) 273 - return false; 274 - 275 - return wdata->ext->motionp || wdata->ext->ext_type; 276 - } 277 - 278 - static void handler_motionp(struct wiimote_ext *ext, const __u8 *payload) 279 - { 280 - __s32 x, y, z; 281 - bool plugged; 282 - 283 - /* | 8 7 6 5 4 3 | 2 | 1 | 284 - * -----+------------------------------+-----+-----+ 285 - * 1 | Yaw Speed <7:0> | 286 - * 2 | Roll Speed <7:0> | 287 - * 3 | Pitch Speed <7:0> | 288 - * -----+------------------------------+-----+-----+ 289 - * 4 | Yaw Speed <13:8> | Yaw |Pitch| 290 - * -----+------------------------------+-----+-----+ 291 - * 5 | Roll Speed <13:8> |Roll | Ext | 292 - * -----+------------------------------+-----+-----+ 293 - * 6 | Pitch Speed <13:8> | 1 | 0 | 294 - * -----+------------------------------+-----+-----+ 295 - * The single bits Yaw, Roll, Pitch in the lower right corner specify 296 - * whether the wiimote is rotating fast (0) or slow (1). Speed for slow 297 - * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a 298 - * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast 299 - * and 9 for slow. 300 - * If the wiimote is not rotating the sensor reports 2^13 = 8192. 301 - * Ext specifies whether an extension is connected to the motionp. 302 - */ 303 - 304 - x = payload[0]; 305 - y = payload[1]; 306 - z = payload[2]; 307 - 308 - x |= (((__u16)payload[3]) << 6) & 0xff00; 309 - y |= (((__u16)payload[4]) << 6) & 0xff00; 310 - z |= (((__u16)payload[5]) << 6) & 0xff00; 311 - 312 - x -= 8192; 313 - y -= 8192; 314 - z -= 8192; 315 - 316 - if (!(payload[3] & 0x02)) 317 - x *= 18; 318 - else 319 - x *= 9; 320 - if (!(payload[4] & 0x02)) 321 - y *= 18; 322 - else 323 - y *= 9; 324 - if (!(payload[3] & 0x01)) 325 - z *= 18; 326 - else 327 - z *= 9; 328 - 329 - input_report_abs(ext->mp_input, ABS_RX, x); 330 - input_report_abs(ext->mp_input, ABS_RY, y); 331 - input_report_abs(ext->mp_input, ABS_RZ, z); 332 - input_sync(ext->mp_input); 333 - 334 - plugged = payload[5] & 0x01; 335 - if (plugged != ext->mp_plugged) 336 - ext->mp_plugged = plugged; 337 - } 338 - 339 - static void handler_nunchuck(struct wiimote_ext *ext, const __u8 *payload) 340 - { 341 - __s16 x, y, z, bx, by; 342 - 343 - /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 | 344 - * -----+----------+---------+---------+----+-----+ 345 - * 1 | Button X <7:0> | 346 - * 2 | Button Y <7:0> | 347 - * -----+----------+---------+---------+----+-----+ 348 - * 3 | Speed X <9:2> | 349 - * 4 | Speed Y <9:2> | 350 - * 5 | Speed Z <9:2> | 351 - * -----+----------+---------+---------+----+-----+ 352 - * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ | 353 - * -----+----------+---------+---------+----+-----+ 354 - * Button X/Y is the analog stick. Speed X, Y and Z are the 355 - * accelerometer data in the same format as the wiimote's accelerometer. 356 - * The 6th byte contains the LSBs of the accelerometer data. 357 - * BC and BZ are the C and Z buttons: 0 means pressed 358 - * 359 - * If reported interleaved with motionp, then the layout changes. The 360 - * 5th and 6th byte changes to: 361 - * -----+-----------------------------------+-----+ 362 - * 5 | Speed Z <9:3> | EXT | 363 - * -----+--------+-----+-----+----+----+----+-----+ 364 - * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 | 365 - * -----+--------+-----+-----+----+----+----+-----+ 366 - * All three accelerometer values lose their LSB. The other data is 367 - * still available but slightly moved. 368 - * 369 - * Center data for button values is 128. Center value for accelerometer 370 - * values it 512 / 0x200 371 - */ 372 - 373 - bx = payload[0]; 374 - by = payload[1]; 375 - bx -= 128; 376 - by -= 128; 377 - 378 - x = payload[2] << 2; 379 - y = payload[3] << 2; 380 - z = payload[4] << 2; 381 - 382 - if (ext->motionp) { 383 - x |= (payload[5] >> 3) & 0x02; 384 - y |= (payload[5] >> 4) & 0x02; 385 - z &= ~0x4; 386 - z |= (payload[5] >> 5) & 0x06; 387 - } else { 388 - x |= (payload[5] >> 2) & 0x03; 389 - y |= (payload[5] >> 4) & 0x03; 390 - z |= (payload[5] >> 6) & 0x03; 391 - } 392 - 393 - x -= 0x200; 394 - y -= 0x200; 395 - z -= 0x200; 396 - 397 - input_report_abs(ext->input, ABS_HAT0X, bx); 398 - input_report_abs(ext->input, ABS_HAT0Y, by); 399 - 400 - input_report_abs(ext->input, ABS_RX, x); 401 - input_report_abs(ext->input, ABS_RY, y); 402 - input_report_abs(ext->input, ABS_RZ, z); 403 - 404 - if (ext->motionp) { 405 - input_report_key(ext->input, 406 - wiiext_keymap[WIIEXT_KEY_Z], !(payload[5] & 0x04)); 407 - input_report_key(ext->input, 408 - wiiext_keymap[WIIEXT_KEY_C], !(payload[5] & 0x08)); 409 - } else { 410 - input_report_key(ext->input, 411 - wiiext_keymap[WIIEXT_KEY_Z], !(payload[5] & 0x01)); 412 - input_report_key(ext->input, 413 - wiiext_keymap[WIIEXT_KEY_C], !(payload[5] & 0x02)); 414 - } 415 - 416 - input_sync(ext->input); 417 - } 418 - 419 - static void handler_classic(struct wiimote_ext *ext, const __u8 *payload) 420 - { 421 - __s8 rx, ry, lx, ly, lt, rt; 422 - 423 - /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 424 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 425 - * 1 | RX <5:4> | LX <5:0> | 426 - * 2 | RX <3:2> | LY <5:0> | 427 - * -----+-----+-----+-----+-----------------------------+ 428 - * 3 |RX<1>| LT <5:4> | RY <5:1> | 429 - * -----+-----+-----------+-----------------------------+ 430 - * 4 | LT <3:1> | RT <5:1> | 431 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 432 - * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 | 433 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 434 - * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU | 435 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 436 - * All buttons are 0 if pressed 437 - * RX and RY are right analog stick 438 - * LX and LY are left analog stick 439 - * LT is left trigger, RT is right trigger 440 - * BLT is 0 if left trigger is fully pressed 441 - * BRT is 0 if right trigger is fully pressed 442 - * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons 443 - * BZL is left Z button and BZR is right Z button 444 - * B-, BH, B+ are +, HOME and - buttons 445 - * BB, BY, BA, BX are A, B, X, Y buttons 446 - * LSB of RX, RY, LT, and RT are not transmitted and always 0. 447 - * 448 - * With motionp enabled it changes slightly to this: 449 - * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 450 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 451 - * 1 | RX <4:3> | LX <5:1> | BDU | 452 - * 2 | RX <2:1> | LY <5:1> | BDL | 453 - * -----+-----+-----+-----+-----------------------+-----+ 454 - * 3 |RX<0>| LT <4:3> | RY <4:0> | 455 - * -----+-----+-----------+-----------------------------+ 456 - * 4 | LT <2:0> | RT <4:0> | 457 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 458 - * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT | 459 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 460 - * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 | 461 - * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 462 - * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest 463 - * is the same as before. 464 - */ 465 - 466 - if (ext->motionp) { 467 - lx = payload[0] & 0x3e; 468 - ly = payload[0] & 0x3e; 469 - } else { 470 - lx = payload[0] & 0x3f; 471 - ly = payload[0] & 0x3f; 472 - } 473 - 474 - rx = (payload[0] >> 3) & 0x14; 475 - rx |= (payload[1] >> 5) & 0x06; 476 - rx |= (payload[2] >> 7) & 0x01; 477 - ry = payload[2] & 0x1f; 478 - 479 - rt = payload[3] & 0x1f; 480 - lt = (payload[2] >> 2) & 0x18; 481 - lt |= (payload[3] >> 5) & 0x07; 482 - 483 - rx <<= 1; 484 - ry <<= 1; 485 - rt <<= 1; 486 - lt <<= 1; 487 - 488 - input_report_abs(ext->input, ABS_HAT1X, lx - 0x20); 489 - input_report_abs(ext->input, ABS_HAT1Y, ly - 0x20); 490 - input_report_abs(ext->input, ABS_HAT2X, rx - 0x20); 491 - input_report_abs(ext->input, ABS_HAT2Y, ry - 0x20); 492 - input_report_abs(ext->input, ABS_HAT3X, rt - 0x20); 493 - input_report_abs(ext->input, ABS_HAT3Y, lt - 0x20); 494 - 495 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_RIGHT], 496 - !!(payload[4] & 0x80)); 497 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_DOWN], 498 - !!(payload[4] & 0x40)); 499 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LT], 500 - !!(payload[4] & 0x20)); 501 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_MINUS], 502 - !!(payload[4] & 0x10)); 503 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_HOME], 504 - !!(payload[4] & 0x08)); 505 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_PLUS], 506 - !!(payload[4] & 0x04)); 507 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_RT], 508 - !!(payload[4] & 0x02)); 509 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_ZL], 510 - !!(payload[5] & 0x80)); 511 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_B], 512 - !!(payload[5] & 0x40)); 513 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_Y], 514 - !!(payload[5] & 0x20)); 515 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_A], 516 - !!(payload[5] & 0x10)); 517 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_X], 518 - !!(payload[5] & 0x08)); 519 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_ZR], 520 - !!(payload[5] & 0x04)); 521 - 522 - if (ext->motionp) { 523 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_UP], 524 - !!(payload[0] & 0x01)); 525 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LEFT], 526 - !!(payload[1] & 0x01)); 527 - } else { 528 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_UP], 529 - !!(payload[5] & 0x01)); 530 - input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LEFT], 531 - !!(payload[5] & 0x02)); 532 - } 533 - 534 - input_sync(ext->input); 535 - } 536 - 537 - static void handler_balance_board(struct wiimote_ext *ext, const __u8 *payload) 538 - { 539 - __s32 val[4], tmp; 540 - unsigned int i; 541 - 542 - /* Byte | 8 7 6 5 4 3 2 1 | 543 - * -----+--------------------------+ 544 - * 1 | Top Right <15:8> | 545 - * 2 | Top Right <7:0> | 546 - * -----+--------------------------+ 547 - * 3 | Bottom Right <15:8> | 548 - * 4 | Bottom Right <7:0> | 549 - * -----+--------------------------+ 550 - * 5 | Top Left <15:8> | 551 - * 6 | Top Left <7:0> | 552 - * -----+--------------------------+ 553 - * 7 | Bottom Left <15:8> | 554 - * 8 | Bottom Left <7:0> | 555 - * -----+--------------------------+ 556 - * 557 - * These values represent the weight-measurements of the Wii-balance 558 - * board with 16bit precision. 559 - * 560 - * The balance-board is never reported interleaved with motionp. 561 - */ 562 - 563 - val[0] = payload[0]; 564 - val[0] <<= 8; 565 - val[0] |= payload[1]; 566 - 567 - val[1] = payload[2]; 568 - val[1] <<= 8; 569 - val[1] |= payload[3]; 570 - 571 - val[2] = payload[4]; 572 - val[2] <<= 8; 573 - val[2] |= payload[5]; 574 - 575 - val[3] = payload[6]; 576 - val[3] <<= 8; 577 - val[3] |= payload[7]; 578 - 579 - /* apply calibration data */ 580 - for (i = 0; i < 4; i++) { 581 - if (val[i] < ext->calib[i][1]) { 582 - tmp = val[i] - ext->calib[i][0]; 583 - tmp *= 1700; 584 - tmp /= ext->calib[i][1] - ext->calib[i][0]; 585 - } else { 586 - tmp = val[i] - ext->calib[i][1]; 587 - tmp *= 1700; 588 - tmp /= ext->calib[i][2] - ext->calib[i][1]; 589 - tmp += 1700; 590 - } 591 - val[i] = tmp; 592 - } 593 - 594 - input_report_abs(ext->input, ABS_HAT0X, val[0]); 595 - input_report_abs(ext->input, ABS_HAT0Y, val[1]); 596 - input_report_abs(ext->input, ABS_HAT1X, val[2]); 597 - input_report_abs(ext->input, ABS_HAT1Y, val[3]); 598 - 599 - input_sync(ext->input); 600 - } 601 - 602 - /* call this with state.lock spinlock held */ 603 - void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload) 604 - { 605 - struct wiimote_ext *ext = wdata->ext; 606 - 607 - if (!ext) 608 - return; 609 - 610 - if (ext->motionp && (payload[5] & 0x02)) { 611 - handler_motionp(ext, payload); 612 - } else if (ext->ext_type == WIIEXT_NUNCHUCK) { 613 - handler_nunchuck(ext, payload); 614 - } else if (ext->ext_type == WIIEXT_CLASSIC) { 615 - handler_classic(ext, payload); 616 - } else if (ext->ext_type == WIIEXT_BALANCE_BOARD) { 617 - handler_balance_board(ext, payload); 618 - } 619 - } 620 - 621 - static ssize_t wiiext_show(struct device *dev, struct device_attribute *attr, 622 - char *buf) 623 - { 624 - struct wiimote_data *wdata = dev_to_wii(dev); 625 - __u8 type = WIIEXT_NONE; 626 - bool motionp = false; 627 - unsigned long flags; 628 - 629 - spin_lock_irqsave(&wdata->state.lock, flags); 630 - if (wdata->ext) { 631 - motionp = wdata->ext->motionp; 632 - type = wdata->ext->ext_type; 633 - } 634 - spin_unlock_irqrestore(&wdata->state.lock, flags); 635 - 636 - if (type == WIIEXT_NUNCHUCK) { 637 - if (motionp) 638 - return sprintf(buf, "motionp+nunchuck\n"); 639 - else 640 - return sprintf(buf, "nunchuck\n"); 641 - } else if (type == WIIEXT_CLASSIC) { 642 - if (motionp) 643 - return sprintf(buf, "motionp+classic\n"); 644 - else 645 - return sprintf(buf, "classic\n"); 646 - } else if (type == WIIEXT_BALANCE_BOARD) { 647 - if (motionp) 648 - return sprintf(buf, "motionp+balanceboard\n"); 649 - else 650 - return sprintf(buf, "balanceboard\n"); 651 - } else { 652 - if (motionp) 653 - return sprintf(buf, "motionp\n"); 654 - else 655 - return sprintf(buf, "none\n"); 656 - } 657 - } 658 - 659 - static DEVICE_ATTR(extension, S_IRUGO, wiiext_show, NULL); 660 - 661 - static int wiiext_input_open(struct input_dev *dev) 662 - { 663 - struct wiimote_ext *ext = input_get_drvdata(dev); 664 - int ret; 665 - 666 - ret = hid_hw_open(ext->wdata->hdev); 667 - if (ret) 668 - return ret; 669 - 670 - atomic_inc(&ext->opened); 671 - wiiext_schedule(ext); 672 - 673 - return 0; 674 - } 675 - 676 - static void wiiext_input_close(struct input_dev *dev) 677 - { 678 - struct wiimote_ext *ext = input_get_drvdata(dev); 679 - 680 - atomic_dec(&ext->opened); 681 - wiiext_schedule(ext); 682 - hid_hw_close(ext->wdata->hdev); 683 - } 684 - 685 - static int wiiext_mp_open(struct input_dev *dev) 686 - { 687 - struct wiimote_ext *ext = input_get_drvdata(dev); 688 - int ret; 689 - 690 - ret = hid_hw_open(ext->wdata->hdev); 691 - if (ret) 692 - return ret; 693 - 694 - atomic_inc(&ext->mp_opened); 695 - wiiext_schedule(ext); 696 - 697 - return 0; 698 - } 699 - 700 - static void wiiext_mp_close(struct input_dev *dev) 701 - { 702 - struct wiimote_ext *ext = input_get_drvdata(dev); 703 - 704 - atomic_dec(&ext->mp_opened); 705 - wiiext_schedule(ext); 706 - hid_hw_close(ext->wdata->hdev); 707 - } 708 - 709 - /* Initializes the extension driver of a wiimote */ 710 - int wiiext_init(struct wiimote_data *wdata) 711 - { 712 - struct wiimote_ext *ext; 713 - unsigned long flags; 714 - int ret, i; 715 - 716 - ext = kzalloc(sizeof(*ext), GFP_KERNEL); 717 - if (!ext) 718 - return -ENOMEM; 719 - 720 - ext->wdata = wdata; 721 - INIT_WORK(&ext->worker, wiiext_worker); 722 - 723 - ext->input = input_allocate_device(); 724 - if (!ext->input) { 725 - ret = -ENOMEM; 726 - goto err_input; 727 - } 728 - 729 - input_set_drvdata(ext->input, ext); 730 - ext->input->open = wiiext_input_open; 731 - ext->input->close = wiiext_input_close; 732 - ext->input->dev.parent = &wdata->hdev->dev; 733 - ext->input->id.bustype = wdata->hdev->bus; 734 - ext->input->id.vendor = wdata->hdev->vendor; 735 - ext->input->id.product = wdata->hdev->product; 736 - ext->input->id.version = wdata->hdev->version; 737 - ext->input->name = WIIMOTE_NAME " Extension"; 738 - 739 - set_bit(EV_KEY, ext->input->evbit); 740 - for (i = 0; i < WIIEXT_KEY_COUNT; ++i) 741 - set_bit(wiiext_keymap[i], ext->input->keybit); 742 - 743 - set_bit(EV_ABS, ext->input->evbit); 744 - set_bit(ABS_HAT0X, ext->input->absbit); 745 - set_bit(ABS_HAT0Y, ext->input->absbit); 746 - set_bit(ABS_HAT1X, ext->input->absbit); 747 - set_bit(ABS_HAT1Y, ext->input->absbit); 748 - set_bit(ABS_HAT2X, ext->input->absbit); 749 - set_bit(ABS_HAT2Y, ext->input->absbit); 750 - set_bit(ABS_HAT3X, ext->input->absbit); 751 - set_bit(ABS_HAT3Y, ext->input->absbit); 752 - input_set_abs_params(ext->input, ABS_HAT0X, -120, 120, 2, 4); 753 - input_set_abs_params(ext->input, ABS_HAT0Y, -120, 120, 2, 4); 754 - input_set_abs_params(ext->input, ABS_HAT1X, -30, 30, 1, 1); 755 - input_set_abs_params(ext->input, ABS_HAT1Y, -30, 30, 1, 1); 756 - input_set_abs_params(ext->input, ABS_HAT2X, -30, 30, 1, 1); 757 - input_set_abs_params(ext->input, ABS_HAT2Y, -30, 30, 1, 1); 758 - input_set_abs_params(ext->input, ABS_HAT3X, -30, 30, 1, 1); 759 - input_set_abs_params(ext->input, ABS_HAT3Y, -30, 30, 1, 1); 760 - set_bit(ABS_RX, ext->input->absbit); 761 - set_bit(ABS_RY, ext->input->absbit); 762 - set_bit(ABS_RZ, ext->input->absbit); 763 - input_set_abs_params(ext->input, ABS_RX, -500, 500, 2, 4); 764 - input_set_abs_params(ext->input, ABS_RY, -500, 500, 2, 4); 765 - input_set_abs_params(ext->input, ABS_RZ, -500, 500, 2, 4); 766 - 767 - ret = input_register_device(ext->input); 768 - if (ret) { 769 - input_free_device(ext->input); 770 - goto err_input; 771 - } 772 - 773 - ext->mp_input = input_allocate_device(); 774 - if (!ext->mp_input) { 775 - ret = -ENOMEM; 776 - goto err_mp; 777 - } 778 - 779 - input_set_drvdata(ext->mp_input, ext); 780 - ext->mp_input->open = wiiext_mp_open; 781 - ext->mp_input->close = wiiext_mp_close; 782 - ext->mp_input->dev.parent = &wdata->hdev->dev; 783 - ext->mp_input->id.bustype = wdata->hdev->bus; 784 - ext->mp_input->id.vendor = wdata->hdev->vendor; 785 - ext->mp_input->id.product = wdata->hdev->product; 786 - ext->mp_input->id.version = wdata->hdev->version; 787 - ext->mp_input->name = WIIMOTE_NAME " Motion+"; 788 - 789 - set_bit(EV_ABS, ext->mp_input->evbit); 790 - set_bit(ABS_RX, ext->mp_input->absbit); 791 - set_bit(ABS_RY, ext->mp_input->absbit); 792 - set_bit(ABS_RZ, ext->mp_input->absbit); 793 - input_set_abs_params(ext->mp_input, ABS_RX, -160000, 160000, 4, 8); 794 - input_set_abs_params(ext->mp_input, ABS_RY, -160000, 160000, 4, 8); 795 - input_set_abs_params(ext->mp_input, ABS_RZ, -160000, 160000, 4, 8); 796 - 797 - ret = input_register_device(ext->mp_input); 798 - if (ret) { 799 - input_free_device(ext->mp_input); 800 - goto err_mp; 801 - } 802 - 803 - ret = device_create_file(&wdata->hdev->dev, &dev_attr_extension); 804 - if (ret) 805 - goto err_dev; 806 - 807 - spin_lock_irqsave(&wdata->state.lock, flags); 808 - wdata->ext = ext; 809 - spin_unlock_irqrestore(&wdata->state.lock, flags); 810 - 811 - return 0; 812 - 813 - err_dev: 814 - input_unregister_device(ext->mp_input); 815 - err_mp: 816 - input_unregister_device(ext->input); 817 - err_input: 818 - kfree(ext); 819 - return ret; 820 - } 821 - 822 - /* Deinitializes the extension driver of a wiimote */ 823 - void wiiext_deinit(struct wiimote_data *wdata) 824 - { 825 - struct wiimote_ext *ext = wdata->ext; 826 - unsigned long flags; 827 - 828 - if (!ext) 829 - return; 830 - 831 - /* 832 - * We first unset wdata->ext to avoid further input from the wiimote 833 - * core. The worker thread does not access this pointer so it is not 834 - * affected by this. 835 - * We kill the worker after this so it does not get respawned during 836 - * deinitialization. 837 - */ 838 - 839 - spin_lock_irqsave(&wdata->state.lock, flags); 840 - wdata->ext = NULL; 841 - spin_unlock_irqrestore(&wdata->state.lock, flags); 842 - 843 - device_remove_file(&wdata->hdev->dev, &dev_attr_extension); 844 - input_unregister_device(ext->mp_input); 845 - input_unregister_device(ext->input); 846 - 847 - cancel_work_sync(&ext->worker); 848 - kfree(ext); 849 - }
+2086
drivers/hid/hid-wiimote-modules.c
··· 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 + 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 + 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 + 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 + 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 + 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 + 791 + /* 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 + /* 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 + * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 1065 + * 1 | RX <5:4> | LX <5:1> | BDU | 1066 + * 2 | RX <3:2> | LY <5:1> | BDL | 1067 + * -----+-----+-----+-----+-----------------------+-----+ 1068 + * 3 |RX<1>| LT <5:4> | RY <5:1> | 1069 + * -----+-----+-----------+-----------------------------+ 1070 + * 4 | LT <3:1> | RT <5:1> | 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; 1082 + ly = ext[1] & 0x3e; 1083 + } else { 1084 + lx = ext[0] & 0x3f; 1085 + ly = ext[1] & 0x3f; 1086 + } 1087 + 1088 + rx = (ext[0] >> 3) & 0x18; 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); 1106 + input_report_abs(wdata->extension.input, ABS_HAT3X, rt); 1107 + input_report_abs(wdata->extension.input, ABS_HAT3Y, lt); 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 + /* 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 + 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 + 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 + 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 + 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) 1508 + goto err_file; 1509 + 1510 + return 0; 1511 + 1512 + err_file: 1513 + device_remove_file(&wdata->hdev->dev, 1514 + &dev_attr_bboard_calib); 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; 1529 + device_remove_file(&wdata->hdev->dev, 1530 + &dev_attr_bboard_calib); 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 + /* 1543 + * Pro Controller 1544 + * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not 1545 + * work together with the classic Wii, but only with the new Wii U. However, it 1546 + * uses the same protocol and provides a builtin "classic controller pro" 1547 + * extension, few standard buttons, a rumble motor, 4 LEDs and a battery. 1548 + * We provide all these via a standard extension device as the device doesn't 1549 + * feature an extension port. 1550 + */ 1551 + 1552 + enum wiimod_pro_keys { 1553 + WIIMOD_PRO_KEY_A, 1554 + WIIMOD_PRO_KEY_B, 1555 + WIIMOD_PRO_KEY_X, 1556 + WIIMOD_PRO_KEY_Y, 1557 + WIIMOD_PRO_KEY_PLUS, 1558 + WIIMOD_PRO_KEY_MINUS, 1559 + WIIMOD_PRO_KEY_HOME, 1560 + WIIMOD_PRO_KEY_LEFT, 1561 + WIIMOD_PRO_KEY_RIGHT, 1562 + WIIMOD_PRO_KEY_UP, 1563 + WIIMOD_PRO_KEY_DOWN, 1564 + WIIMOD_PRO_KEY_TL, 1565 + WIIMOD_PRO_KEY_TR, 1566 + WIIMOD_PRO_KEY_ZL, 1567 + WIIMOD_PRO_KEY_ZR, 1568 + WIIMOD_PRO_KEY_THUMBL, 1569 + WIIMOD_PRO_KEY_THUMBR, 1570 + WIIMOD_PRO_KEY_NUM, 1571 + }; 1572 + 1573 + static const __u16 wiimod_pro_map[] = { 1574 + BTN_EAST, /* WIIMOD_PRO_KEY_A */ 1575 + BTN_SOUTH, /* WIIMOD_PRO_KEY_B */ 1576 + BTN_NORTH, /* WIIMOD_PRO_KEY_X */ 1577 + BTN_WEST, /* WIIMOD_PRO_KEY_Y */ 1578 + BTN_START, /* WIIMOD_PRO_KEY_PLUS */ 1579 + BTN_SELECT, /* WIIMOD_PRO_KEY_MINUS */ 1580 + BTN_MODE, /* WIIMOD_PRO_KEY_HOME */ 1581 + BTN_DPAD_LEFT, /* WIIMOD_PRO_KEY_LEFT */ 1582 + BTN_DPAD_RIGHT, /* WIIMOD_PRO_KEY_RIGHT */ 1583 + BTN_DPAD_UP, /* WIIMOD_PRO_KEY_UP */ 1584 + BTN_DPAD_DOWN, /* WIIMOD_PRO_KEY_DOWN */ 1585 + BTN_TL, /* WIIMOD_PRO_KEY_TL */ 1586 + BTN_TR, /* WIIMOD_PRO_KEY_TR */ 1587 + BTN_TL2, /* WIIMOD_PRO_KEY_ZL */ 1588 + BTN_TR2, /* WIIMOD_PRO_KEY_ZR */ 1589 + BTN_THUMBL, /* WIIMOD_PRO_KEY_THUMBL */ 1590 + BTN_THUMBR, /* WIIMOD_PRO_KEY_THUMBR */ 1591 + }; 1592 + 1593 + static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext) 1594 + { 1595 + __s16 rx, ry, lx, ly; 1596 + 1597 + /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 1598 + * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 1599 + * 1 | LX <7:0> | 1600 + * -----+-----------------------+-----------------------+ 1601 + * 2 | 0 0 0 0 | LX <11:8> | 1602 + * -----+-----------------------+-----------------------+ 1603 + * 3 | RX <7:0> | 1604 + * -----+-----------------------+-----------------------+ 1605 + * 4 | 0 0 0 0 | RX <11:8> | 1606 + * -----+-----------------------+-----------------------+ 1607 + * 5 | LY <7:0> | 1608 + * -----+-----------------------+-----------------------+ 1609 + * 6 | 0 0 0 0 | LY <11:8> | 1610 + * -----+-----------------------+-----------------------+ 1611 + * 7 | RY <7:0> | 1612 + * -----+-----------------------+-----------------------+ 1613 + * 8 | 0 0 0 0 | RY <11:8> | 1614 + * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 1615 + * 9 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 | 1616 + * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 1617 + * 10 | BZL | BB | BY | BA | BX | BZR | BDL | BDU | 1618 + * -----+-----+-----+-----+-----+-----+-----+-----+-----+ 1619 + * 11 | 1 | BATTERY | USB |CHARG|LTHUM|RTHUM| 1620 + * -----+-----+-----------------+-----------+-----+-----+ 1621 + * All buttons are low-active (0 if pressed) 1622 + * RX and RY are right analog stick 1623 + * LX and LY are left analog stick 1624 + * BLT is left trigger, BRT is right trigger. 1625 + * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons 1626 + * BZL is left Z button and BZR is right Z button 1627 + * B-, BH, B+ are +, HOME and - buttons 1628 + * BB, BY, BA, BX are A, B, X, Y buttons 1629 + * 1630 + * Bits marked as 0/1 are unknown and never changed during tests. 1631 + * 1632 + * Not entirely verified: 1633 + * CHARG: 1 if uncharging, 0 if charging 1634 + * USB: 1 if not connected, 0 if connected 1635 + * BATTERY: battery capacity from 000 (empty) to 100 (full) 1636 + */ 1637 + 1638 + lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8); 1639 + rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8); 1640 + ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8); 1641 + ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8); 1642 + 1643 + input_report_abs(wdata->extension.input, ABS_X, lx - 0x800); 1644 + input_report_abs(wdata->extension.input, ABS_Y, ly - 0x800); 1645 + input_report_abs(wdata->extension.input, ABS_RX, rx - 0x800); 1646 + input_report_abs(wdata->extension.input, ABS_RY, ry - 0x800); 1647 + 1648 + input_report_key(wdata->extension.input, 1649 + wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT], 1650 + !(ext[8] & 0x80)); 1651 + input_report_key(wdata->extension.input, 1652 + wiimod_pro_map[WIIMOD_PRO_KEY_DOWN], 1653 + !(ext[8] & 0x40)); 1654 + input_report_key(wdata->extension.input, 1655 + wiimod_pro_map[WIIMOD_PRO_KEY_TL], 1656 + !(ext[8] & 0x20)); 1657 + input_report_key(wdata->extension.input, 1658 + wiimod_pro_map[WIIMOD_PRO_KEY_MINUS], 1659 + !(ext[8] & 0x10)); 1660 + input_report_key(wdata->extension.input, 1661 + wiimod_pro_map[WIIMOD_PRO_KEY_HOME], 1662 + !(ext[8] & 0x08)); 1663 + input_report_key(wdata->extension.input, 1664 + wiimod_pro_map[WIIMOD_PRO_KEY_PLUS], 1665 + !(ext[8] & 0x04)); 1666 + input_report_key(wdata->extension.input, 1667 + wiimod_pro_map[WIIMOD_PRO_KEY_TR], 1668 + !(ext[8] & 0x02)); 1669 + 1670 + input_report_key(wdata->extension.input, 1671 + wiimod_pro_map[WIIMOD_PRO_KEY_ZL], 1672 + !(ext[9] & 0x80)); 1673 + input_report_key(wdata->extension.input, 1674 + wiimod_pro_map[WIIMOD_PRO_KEY_B], 1675 + !(ext[9] & 0x40)); 1676 + input_report_key(wdata->extension.input, 1677 + wiimod_pro_map[WIIMOD_PRO_KEY_Y], 1678 + !(ext[9] & 0x20)); 1679 + input_report_key(wdata->extension.input, 1680 + wiimod_pro_map[WIIMOD_PRO_KEY_A], 1681 + !(ext[9] & 0x10)); 1682 + input_report_key(wdata->extension.input, 1683 + wiimod_pro_map[WIIMOD_PRO_KEY_X], 1684 + !(ext[9] & 0x08)); 1685 + input_report_key(wdata->extension.input, 1686 + wiimod_pro_map[WIIMOD_PRO_KEY_ZR], 1687 + !(ext[9] & 0x04)); 1688 + input_report_key(wdata->extension.input, 1689 + wiimod_pro_map[WIIMOD_PRO_KEY_LEFT], 1690 + !(ext[9] & 0x02)); 1691 + input_report_key(wdata->extension.input, 1692 + wiimod_pro_map[WIIMOD_PRO_KEY_UP], 1693 + !(ext[9] & 0x01)); 1694 + 1695 + input_report_key(wdata->extension.input, 1696 + wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL], 1697 + !(ext[10] & 0x02)); 1698 + input_report_key(wdata->extension.input, 1699 + wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR], 1700 + !(ext[10] & 0x01)); 1701 + 1702 + input_sync(wdata->extension.input); 1703 + } 1704 + 1705 + static int wiimod_pro_open(struct input_dev *dev) 1706 + { 1707 + struct wiimote_data *wdata = input_get_drvdata(dev); 1708 + unsigned long flags; 1709 + 1710 + spin_lock_irqsave(&wdata->state.lock, flags); 1711 + wdata->state.flags |= WIIPROTO_FLAG_EXT_USED; 1712 + wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 1713 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1714 + 1715 + return 0; 1716 + } 1717 + 1718 + static void wiimod_pro_close(struct input_dev *dev) 1719 + { 1720 + struct wiimote_data *wdata = input_get_drvdata(dev); 1721 + unsigned long flags; 1722 + 1723 + spin_lock_irqsave(&wdata->state.lock, flags); 1724 + wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED; 1725 + wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 1726 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1727 + } 1728 + 1729 + static int wiimod_pro_play(struct input_dev *dev, void *data, 1730 + struct ff_effect *eff) 1731 + { 1732 + struct wiimote_data *wdata = input_get_drvdata(dev); 1733 + __u8 value; 1734 + unsigned long flags; 1735 + 1736 + /* 1737 + * The wiimote supports only a single rumble motor so if any magnitude 1738 + * is set to non-zero then we start the rumble motor. If both are set to 1739 + * zero, we stop the rumble motor. 1740 + */ 1741 + 1742 + if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) 1743 + value = 1; 1744 + else 1745 + value = 0; 1746 + 1747 + spin_lock_irqsave(&wdata->state.lock, flags); 1748 + wiiproto_req_rumble(wdata, value); 1749 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1750 + 1751 + return 0; 1752 + } 1753 + 1754 + static int wiimod_pro_probe(const struct wiimod_ops *ops, 1755 + struct wiimote_data *wdata) 1756 + { 1757 + int ret, i; 1758 + 1759 + wdata->extension.input = input_allocate_device(); 1760 + if (!wdata->extension.input) 1761 + return -ENOMEM; 1762 + 1763 + set_bit(FF_RUMBLE, wdata->extension.input->ffbit); 1764 + input_set_drvdata(wdata->extension.input, wdata); 1765 + 1766 + if (input_ff_create_memless(wdata->extension.input, NULL, 1767 + wiimod_pro_play)) { 1768 + ret = -ENOMEM; 1769 + goto err_free; 1770 + } 1771 + 1772 + wdata->extension.input->open = wiimod_pro_open; 1773 + wdata->extension.input->close = wiimod_pro_close; 1774 + wdata->extension.input->dev.parent = &wdata->hdev->dev; 1775 + wdata->extension.input->id.bustype = wdata->hdev->bus; 1776 + wdata->extension.input->id.vendor = wdata->hdev->vendor; 1777 + wdata->extension.input->id.product = wdata->hdev->product; 1778 + wdata->extension.input->id.version = wdata->hdev->version; 1779 + wdata->extension.input->name = WIIMOTE_NAME " Pro Controller"; 1780 + 1781 + set_bit(EV_KEY, wdata->extension.input->evbit); 1782 + for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i) 1783 + set_bit(wiimod_pro_map[i], 1784 + wdata->extension.input->keybit); 1785 + 1786 + set_bit(EV_ABS, wdata->extension.input->evbit); 1787 + set_bit(ABS_X, wdata->extension.input->absbit); 1788 + set_bit(ABS_Y, wdata->extension.input->absbit); 1789 + set_bit(ABS_RX, wdata->extension.input->absbit); 1790 + set_bit(ABS_RY, wdata->extension.input->absbit); 1791 + input_set_abs_params(wdata->extension.input, 1792 + ABS_X, -0x800, 0x800, 2, 4); 1793 + input_set_abs_params(wdata->extension.input, 1794 + ABS_Y, -0x800, 0x800, 2, 4); 1795 + input_set_abs_params(wdata->extension.input, 1796 + ABS_RX, -0x800, 0x800, 2, 4); 1797 + input_set_abs_params(wdata->extension.input, 1798 + ABS_RY, -0x800, 0x800, 2, 4); 1799 + 1800 + ret = input_register_device(wdata->extension.input); 1801 + if (ret) 1802 + goto err_free; 1803 + 1804 + return 0; 1805 + 1806 + err_free: 1807 + input_free_device(wdata->extension.input); 1808 + wdata->extension.input = NULL; 1809 + return ret; 1810 + } 1811 + 1812 + static void wiimod_pro_remove(const struct wiimod_ops *ops, 1813 + struct wiimote_data *wdata) 1814 + { 1815 + unsigned long flags; 1816 + 1817 + if (!wdata->extension.input) 1818 + return; 1819 + 1820 + spin_lock_irqsave(&wdata->state.lock, flags); 1821 + wiiproto_req_rumble(wdata, 0); 1822 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1823 + 1824 + input_unregister_device(wdata->extension.input); 1825 + wdata->extension.input = NULL; 1826 + } 1827 + 1828 + static const struct wiimod_ops wiimod_pro = { 1829 + .flags = WIIMOD_FLAG_EXT16, 1830 + .arg = 0, 1831 + .probe = wiimod_pro_probe, 1832 + .remove = wiimod_pro_remove, 1833 + .in_ext = wiimod_pro_in_ext, 1834 + }; 1835 + 1836 + /* 1837 + * Builtin Motion Plus 1838 + * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which 1839 + * disables polling for Motion-Plus. This should be set only for devices which 1840 + * don't allow MP hotplugging. 1841 + */ 1842 + 1843 + static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops, 1844 + struct wiimote_data *wdata) 1845 + { 1846 + unsigned long flags; 1847 + 1848 + spin_lock_irqsave(&wdata->state.lock, flags); 1849 + wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP; 1850 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1851 + 1852 + return 0; 1853 + } 1854 + 1855 + static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops, 1856 + struct wiimote_data *wdata) 1857 + { 1858 + unsigned long flags; 1859 + 1860 + spin_lock_irqsave(&wdata->state.lock, flags); 1861 + wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP; 1862 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1863 + } 1864 + 1865 + static const struct wiimod_ops wiimod_builtin_mp = { 1866 + .flags = 0, 1867 + .arg = 0, 1868 + .probe = wiimod_builtin_mp_probe, 1869 + .remove = wiimod_builtin_mp_remove, 1870 + }; 1871 + 1872 + /* 1873 + * No Motion Plus 1874 + * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which 1875 + * disables motion-plus. This is needed for devices that advertise this but we 1876 + * don't know how to use it (or whether it is actually present). 1877 + */ 1878 + 1879 + static int wiimod_no_mp_probe(const struct wiimod_ops *ops, 1880 + struct wiimote_data *wdata) 1881 + { 1882 + unsigned long flags; 1883 + 1884 + spin_lock_irqsave(&wdata->state.lock, flags); 1885 + wdata->state.flags |= WIIPROTO_FLAG_NO_MP; 1886 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1887 + 1888 + return 0; 1889 + } 1890 + 1891 + static void wiimod_no_mp_remove(const struct wiimod_ops *ops, 1892 + struct wiimote_data *wdata) 1893 + { 1894 + unsigned long flags; 1895 + 1896 + spin_lock_irqsave(&wdata->state.lock, flags); 1897 + wdata->state.flags |= WIIPROTO_FLAG_NO_MP; 1898 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1899 + } 1900 + 1901 + static const struct wiimod_ops wiimod_no_mp = { 1902 + .flags = 0, 1903 + .arg = 0, 1904 + .probe = wiimod_no_mp_probe, 1905 + .remove = wiimod_no_mp_remove, 1906 + }; 1907 + 1908 + /* 1909 + * Motion Plus 1910 + * The Motion Plus extension provides rotation sensors (gyro) as a small 1911 + * extension device for Wii Remotes. Many devices have them built-in so 1912 + * you cannot see them from the outside. 1913 + * Motion Plus extensions are special because they are on a separate extension 1914 + * port and allow other extensions to be used simultaneously. This is all 1915 + * handled by the Wiimote Core so we don't have to deal with it. 1916 + */ 1917 + 1918 + static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext) 1919 + { 1920 + __s32 x, y, z; 1921 + 1922 + /* | 8 7 6 5 4 3 | 2 | 1 | 1923 + * -----+------------------------------+-----+-----+ 1924 + * 1 | Yaw Speed <7:0> | 1925 + * 2 | Roll Speed <7:0> | 1926 + * 3 | Pitch Speed <7:0> | 1927 + * -----+------------------------------+-----+-----+ 1928 + * 4 | Yaw Speed <13:8> | Yaw |Pitch| 1929 + * -----+------------------------------+-----+-----+ 1930 + * 5 | Roll Speed <13:8> |Roll | Ext | 1931 + * -----+------------------------------+-----+-----+ 1932 + * 6 | Pitch Speed <13:8> | 1 | 0 | 1933 + * -----+------------------------------+-----+-----+ 1934 + * The single bits Yaw, Roll, Pitch in the lower right corner specify 1935 + * whether the wiimote is rotating fast (0) or slow (1). Speed for slow 1936 + * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a 1937 + * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast 1938 + * and 9 for slow. 1939 + * If the wiimote is not rotating the sensor reports 2^13 = 8192. 1940 + * Ext specifies whether an extension is connected to the motionp. 1941 + * which is parsed by wiimote-core. 1942 + */ 1943 + 1944 + x = ext[0]; 1945 + y = ext[1]; 1946 + z = ext[2]; 1947 + 1948 + x |= (((__u16)ext[3]) << 6) & 0xff00; 1949 + y |= (((__u16)ext[4]) << 6) & 0xff00; 1950 + z |= (((__u16)ext[5]) << 6) & 0xff00; 1951 + 1952 + x -= 8192; 1953 + y -= 8192; 1954 + z -= 8192; 1955 + 1956 + if (!(ext[3] & 0x02)) 1957 + x *= 18; 1958 + else 1959 + x *= 9; 1960 + if (!(ext[4] & 0x02)) 1961 + y *= 18; 1962 + else 1963 + y *= 9; 1964 + if (!(ext[3] & 0x01)) 1965 + z *= 18; 1966 + else 1967 + z *= 9; 1968 + 1969 + input_report_abs(wdata->mp, ABS_RX, x); 1970 + input_report_abs(wdata->mp, ABS_RY, y); 1971 + input_report_abs(wdata->mp, ABS_RZ, z); 1972 + input_sync(wdata->mp); 1973 + } 1974 + 1975 + static int wiimod_mp_open(struct input_dev *dev) 1976 + { 1977 + struct wiimote_data *wdata = input_get_drvdata(dev); 1978 + unsigned long flags; 1979 + 1980 + spin_lock_irqsave(&wdata->state.lock, flags); 1981 + wdata->state.flags |= WIIPROTO_FLAG_MP_USED; 1982 + wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 1983 + __wiimote_schedule(wdata); 1984 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1985 + 1986 + return 0; 1987 + } 1988 + 1989 + static void wiimod_mp_close(struct input_dev *dev) 1990 + { 1991 + struct wiimote_data *wdata = input_get_drvdata(dev); 1992 + unsigned long flags; 1993 + 1994 + spin_lock_irqsave(&wdata->state.lock, flags); 1995 + wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED; 1996 + wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL); 1997 + __wiimote_schedule(wdata); 1998 + spin_unlock_irqrestore(&wdata->state.lock, flags); 1999 + } 2000 + 2001 + static int wiimod_mp_probe(const struct wiimod_ops *ops, 2002 + struct wiimote_data *wdata) 2003 + { 2004 + int ret; 2005 + 2006 + wdata->mp = input_allocate_device(); 2007 + if (!wdata->mp) 2008 + return -ENOMEM; 2009 + 2010 + input_set_drvdata(wdata->mp, wdata); 2011 + wdata->mp->open = wiimod_mp_open; 2012 + wdata->mp->close = wiimod_mp_close; 2013 + wdata->mp->dev.parent = &wdata->hdev->dev; 2014 + wdata->mp->id.bustype = wdata->hdev->bus; 2015 + wdata->mp->id.vendor = wdata->hdev->vendor; 2016 + wdata->mp->id.product = wdata->hdev->product; 2017 + wdata->mp->id.version = wdata->hdev->version; 2018 + wdata->mp->name = WIIMOTE_NAME " Motion Plus"; 2019 + 2020 + set_bit(EV_ABS, wdata->mp->evbit); 2021 + set_bit(ABS_RX, wdata->mp->absbit); 2022 + set_bit(ABS_RY, wdata->mp->absbit); 2023 + set_bit(ABS_RZ, wdata->mp->absbit); 2024 + input_set_abs_params(wdata->mp, 2025 + ABS_RX, -16000, 16000, 4, 8); 2026 + input_set_abs_params(wdata->mp, 2027 + ABS_RY, -16000, 16000, 4, 8); 2028 + input_set_abs_params(wdata->mp, 2029 + ABS_RZ, -16000, 16000, 4, 8); 2030 + 2031 + ret = input_register_device(wdata->mp); 2032 + if (ret) 2033 + goto err_free; 2034 + 2035 + return 0; 2036 + 2037 + err_free: 2038 + input_free_device(wdata->mp); 2039 + wdata->mp = NULL; 2040 + return ret; 2041 + } 2042 + 2043 + static void wiimod_mp_remove(const struct wiimod_ops *ops, 2044 + struct wiimote_data *wdata) 2045 + { 2046 + if (!wdata->mp) 2047 + return; 2048 + 2049 + input_unregister_device(wdata->mp); 2050 + wdata->mp = NULL; 2051 + } 2052 + 2053 + const struct wiimod_ops wiimod_mp = { 2054 + .flags = 0, 2055 + .arg = 0, 2056 + .probe = wiimod_mp_probe, 2057 + .remove = wiimod_mp_remove, 2058 + .in_mp = wiimod_mp_in_mp, 2059 + }; 2060 + 2061 + /* module table */ 2062 + 2063 + static const struct wiimod_ops wiimod_dummy; 2064 + 2065 + const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = { 2066 + [WIIMOD_KEYS] = &wiimod_keys, 2067 + [WIIMOD_RUMBLE] = &wiimod_rumble, 2068 + [WIIMOD_BATTERY] = &wiimod_battery, 2069 + [WIIMOD_LED1] = &wiimod_leds[0], 2070 + [WIIMOD_LED2] = &wiimod_leds[1], 2071 + [WIIMOD_LED3] = &wiimod_leds[2], 2072 + [WIIMOD_LED4] = &wiimod_leds[3], 2073 + [WIIMOD_ACCEL] = &wiimod_accel, 2074 + [WIIMOD_IR] = &wiimod_ir, 2075 + [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp, 2076 + [WIIMOD_NO_MP] = &wiimod_no_mp, 2077 + }; 2078 + 2079 + const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = { 2080 + [WIIMOTE_EXT_NONE] = &wiimod_dummy, 2081 + [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy, 2082 + [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk, 2083 + [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic, 2084 + [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard, 2085 + [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro, 2086 + };
+190 -27
drivers/hid/hid-wiimote.h
··· 2 2 #define __HID_WIIMOTE_H 3 3 4 4 /* 5 - * HID driver for Nintendo Wiimote devices 6 - * Copyright (c) 2011 David Herrmann 5 + * HID driver for Nintendo Wii / Wii U peripherals 6 + * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com> 7 7 */ 8 8 9 9 /* ··· 22 22 #include <linux/mutex.h> 23 23 #include <linux/power_supply.h> 24 24 #include <linux/spinlock.h> 25 + #include <linux/timer.h> 25 26 26 27 #define WIIMOTE_NAME "Nintendo Wii Remote" 27 28 #define WIIMOTE_BUFSIZE 32 ··· 36 35 #define WIIPROTO_FLAG_IR_BASIC 0x40 37 36 #define WIIPROTO_FLAG_IR_EXT 0x80 38 37 #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */ 38 + #define WIIPROTO_FLAG_EXT_PLUGGED 0x0100 39 + #define WIIPROTO_FLAG_EXT_USED 0x0200 40 + #define WIIPROTO_FLAG_EXT_ACTIVE 0x0400 41 + #define WIIPROTO_FLAG_MP_PLUGGED 0x0800 42 + #define WIIPROTO_FLAG_MP_USED 0x1000 43 + #define WIIPROTO_FLAG_MP_ACTIVE 0x2000 44 + #define WIIPROTO_FLAG_EXITING 0x4000 45 + #define WIIPROTO_FLAG_DRM_LOCKED 0x8000 46 + #define WIIPROTO_FLAG_BUILTIN_MP 0x010000 47 + #define WIIPROTO_FLAG_NO_MP 0x020000 48 + 39 49 #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ 40 50 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) 41 51 #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ ··· 55 43 /* return flag for led \num */ 56 44 #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) 57 45 46 + enum wiiproto_keys { 47 + WIIPROTO_KEY_LEFT, 48 + WIIPROTO_KEY_RIGHT, 49 + WIIPROTO_KEY_UP, 50 + WIIPROTO_KEY_DOWN, 51 + WIIPROTO_KEY_PLUS, 52 + WIIPROTO_KEY_MINUS, 53 + WIIPROTO_KEY_ONE, 54 + WIIPROTO_KEY_TWO, 55 + WIIPROTO_KEY_A, 56 + WIIPROTO_KEY_B, 57 + WIIPROTO_KEY_HOME, 58 + WIIPROTO_KEY_COUNT 59 + }; 60 + 61 + enum wiimote_devtype { 62 + WIIMOTE_DEV_PENDING, 63 + WIIMOTE_DEV_UNKNOWN, 64 + WIIMOTE_DEV_GENERIC, 65 + WIIMOTE_DEV_GEN10, 66 + WIIMOTE_DEV_GEN20, 67 + WIIMOTE_DEV_BALANCE_BOARD, 68 + WIIMOTE_DEV_PRO_CONTROLLER, 69 + WIIMOTE_DEV_NUM, 70 + }; 71 + 72 + enum wiimote_exttype { 73 + WIIMOTE_EXT_NONE, 74 + WIIMOTE_EXT_UNKNOWN, 75 + WIIMOTE_EXT_NUNCHUK, 76 + WIIMOTE_EXT_CLASSIC_CONTROLLER, 77 + WIIMOTE_EXT_BALANCE_BOARD, 78 + WIIMOTE_EXT_PRO_CONTROLLER, 79 + WIIMOTE_EXT_NUM, 80 + }; 81 + 82 + enum wiimote_mptype { 83 + WIIMOTE_MP_NONE, 84 + WIIMOTE_MP_UNKNOWN, 85 + WIIMOTE_MP_SINGLE, 86 + WIIMOTE_MP_PASSTHROUGH_NUNCHUK, 87 + WIIMOTE_MP_PASSTHROUGH_CLASSIC, 88 + }; 89 + 58 90 struct wiimote_buf { 59 91 __u8 data[HID_MAX_BUFFER_SIZE]; 60 92 size_t size; 61 93 }; 62 94 95 + struct wiimote_queue { 96 + spinlock_t lock; 97 + struct work_struct worker; 98 + __u8 head; 99 + __u8 tail; 100 + struct wiimote_buf outq[WIIMOTE_BUFSIZE]; 101 + }; 102 + 63 103 struct wiimote_state { 64 104 spinlock_t lock; 65 - __u8 flags; 105 + __u32 flags; 66 106 __u8 accel_split[2]; 67 107 __u8 drm; 108 + __u8 devtype; 109 + __u8 exttype; 110 + __u8 mp; 68 111 69 112 /* synchronous cmd requests */ 70 113 struct mutex sync; ··· 132 65 __u8 cmd_err; 133 66 __u8 *cmd_read_buf; 134 67 __u8 cmd_read_size; 68 + 69 + /* calibration data */ 70 + __u16 calib_bboard[4][3]; 135 71 }; 136 72 137 73 struct wiimote_data { ··· 144 74 struct input_dev *accel; 145 75 struct input_dev *ir; 146 76 struct power_supply battery; 147 - struct wiimote_ext *ext; 77 + struct input_dev *mp; 78 + struct timer_list timer; 148 79 struct wiimote_debug *debug; 149 80 150 - spinlock_t qlock; 151 - __u8 head; 152 - __u8 tail; 153 - struct wiimote_buf outq[WIIMOTE_BUFSIZE]; 154 - struct work_struct worker; 81 + union { 82 + struct input_dev *input; 83 + } extension; 155 84 85 + struct wiimote_queue queue; 156 86 struct wiimote_state state; 87 + struct work_struct init_worker; 157 88 }; 89 + 90 + /* wiimote modules */ 91 + 92 + enum wiimod_module { 93 + WIIMOD_KEYS, 94 + WIIMOD_RUMBLE, 95 + WIIMOD_BATTERY, 96 + WIIMOD_LED1, 97 + WIIMOD_LED2, 98 + WIIMOD_LED3, 99 + WIIMOD_LED4, 100 + WIIMOD_ACCEL, 101 + WIIMOD_IR, 102 + WIIMOD_BUILTIN_MP, 103 + WIIMOD_NO_MP, 104 + WIIMOD_NUM, 105 + WIIMOD_NULL = WIIMOD_NUM, 106 + }; 107 + 108 + #define WIIMOD_FLAG_INPUT 0x0001 109 + #define WIIMOD_FLAG_EXT8 0x0002 110 + #define WIIMOD_FLAG_EXT16 0x0004 111 + 112 + struct wiimod_ops { 113 + __u16 flags; 114 + unsigned long arg; 115 + int (*probe) (const struct wiimod_ops *ops, 116 + struct wiimote_data *wdata); 117 + void (*remove) (const struct wiimod_ops *ops, 118 + struct wiimote_data *wdata); 119 + 120 + void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys); 121 + void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel); 122 + void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed, 123 + unsigned int id); 124 + void (*in_mp) (struct wiimote_data *wdata, const __u8 *mp); 125 + void (*in_ext) (struct wiimote_data *wdata, const __u8 *ext); 126 + }; 127 + 128 + extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM]; 129 + extern const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM]; 130 + extern const struct wiimod_ops wiimod_mp; 131 + 132 + /* wiimote requests */ 158 133 159 134 enum wiiproto_reqs { 160 135 WIIPROTO_REQ_NULL = 0x0, ··· 214 99 WIIPROTO_REQ_STATUS = 0x20, 215 100 WIIPROTO_REQ_DATA = 0x21, 216 101 WIIPROTO_REQ_RETURN = 0x22, 102 + 103 + /* DRM_K: BB*2 */ 217 104 WIIPROTO_REQ_DRM_K = 0x30, 105 + 106 + /* DRM_KA: BB*2 AA*3 */ 218 107 WIIPROTO_REQ_DRM_KA = 0x31, 108 + 109 + /* DRM_KE: BB*2 EE*8 */ 219 110 WIIPROTO_REQ_DRM_KE = 0x32, 111 + 112 + /* DRM_KAI: BB*2 AA*3 II*12 */ 220 113 WIIPROTO_REQ_DRM_KAI = 0x33, 114 + 115 + /* DRM_KEE: BB*2 EE*19 */ 221 116 WIIPROTO_REQ_DRM_KEE = 0x34, 117 + 118 + /* DRM_KAE: BB*2 AA*3 EE*16 */ 222 119 WIIPROTO_REQ_DRM_KAE = 0x35, 120 + 121 + /* DRM_KIE: BB*2 II*10 EE*9 */ 223 122 WIIPROTO_REQ_DRM_KIE = 0x36, 123 + 124 + /* DRM_KAIE: BB*2 AA*3 II*10 EE*6 */ 224 125 WIIPROTO_REQ_DRM_KAIE = 0x37, 126 + 127 + /* DRM_E: EE*21 */ 225 128 WIIPROTO_REQ_DRM_E = 0x3d, 129 + 130 + /* DRM_SKAI1: BB*2 AA*1 II*18 */ 226 131 WIIPROTO_REQ_DRM_SKAI1 = 0x3e, 132 + 133 + /* DRM_SKAI2: BB*2 AA*1 II*18 */ 227 134 WIIPROTO_REQ_DRM_SKAI2 = 0x3f, 135 + 228 136 WIIPROTO_REQ_MAX 229 137 }; 230 138 231 139 #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \ 232 140 dev)) 233 141 142 + void __wiimote_schedule(struct wiimote_data *wdata); 143 + 234 144 extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm); 145 + extern void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble); 146 + extern void wiiproto_req_leds(struct wiimote_data *wdata, int leds); 147 + extern void wiiproto_req_status(struct wiimote_data *wdata); 148 + extern void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel); 149 + extern void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags); 150 + extern void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags); 235 151 extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, 236 152 const __u8 *wmem, __u8 size); 237 153 extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, ··· 274 128 wiiproto_req_rmem((wdata), true, (os), (sz)) 275 129 extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, 276 130 __u32 offset, __u16 size); 277 - 278 - #ifdef CONFIG_HID_WIIMOTE_EXT 279 - 280 - extern int wiiext_init(struct wiimote_data *wdata); 281 - extern void wiiext_deinit(struct wiimote_data *wdata); 282 - extern void wiiext_event(struct wiimote_data *wdata, bool plugged); 283 - extern bool wiiext_active(struct wiimote_data *wdata); 284 - extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload); 285 - 286 - #else 287 - 288 - static inline int wiiext_init(void *u) { return 0; } 289 - static inline void wiiext_deinit(void *u) { } 290 - static inline void wiiext_event(void *u, bool p) { } 291 - static inline bool wiiext_active(void *u) { return false; } 292 - static inline void wiiext_handle(void *u, const __u8 *p) { } 293 - 294 - #endif 295 131 296 132 #ifdef CONFIG_DEBUG_FS 297 133 ··· 301 173 complete(&wdata->state.ready); 302 174 } 303 175 176 + /* requires the state.lock spinlock to be held */ 177 + static inline void wiimote_cmd_abort(struct wiimote_data *wdata) 178 + { 179 + /* Abort synchronous request by waking up the sleeping caller. But 180 + * reset the state.cmd field to an invalid value so no further event 181 + * handlers will work with it. */ 182 + wdata->state.cmd = WIIPROTO_REQ_MAX; 183 + complete(&wdata->state.ready); 184 + } 185 + 304 186 static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) 305 187 { 306 188 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; 189 + } 190 + 191 + static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata) 192 + { 193 + mutex_lock(&wdata->state.sync); 307 194 } 308 195 309 196 /* requires the state.lock spinlock to be held */ ··· 339 196 { 340 197 int ret; 341 198 199 + /* The completion acts as implicit memory barrier so we can safely 200 + * assume that state.cmd is set on success/failure and isn't accessed 201 + * by any other thread, anymore. */ 202 + 342 203 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ); 343 204 if (ret < 0) 344 205 return -ERESTARTSYS; 345 206 else if (ret == 0) 207 + return -EIO; 208 + else if (wdata->state.cmd != WIIPROTO_REQ_NULL) 209 + return -EIO; 210 + else 211 + return 0; 212 + } 213 + 214 + static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata) 215 + { 216 + unsigned long ret; 217 + 218 + /* no locking needed; see wiimote_cmd_wait() */ 219 + ret = wait_for_completion_timeout(&wdata->state.ready, HZ); 220 + if (!ret) 221 + return -EIO; 222 + else if (wdata->state.cmd != WIIPROTO_REQ_NULL) 346 223 return -EIO; 347 224 else 348 225 return 0;
+17 -3
drivers/hid/i2c-hid/i2c-hid.c
··· 108 108 static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) }; 109 109 static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) }; 110 110 static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; 111 + static const struct i2c_hid_cmd hid_no_cmd = { .length = 0 }; 111 112 112 113 /* 113 114 * These definitions are not used here, but are defined by the spec. ··· 260 259 { 261 260 struct i2c_hid *ihid = i2c_get_clientdata(client); 262 261 u8 *args = ihid->argsbuf; 262 + const struct i2c_hid_cmd * hidcmd = &hid_set_report_cmd; 263 263 int ret; 264 264 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); 265 + u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); 266 + u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); 265 267 266 268 /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */ 267 269 u16 size = 2 /* size */ + ··· 282 278 reportID = 0x0F; 283 279 } 284 280 285 - args[index++] = dataRegister & 0xFF; 286 - args[index++] = dataRegister >> 8; 281 + /* 282 + * use the data register for feature reports or if the device does not 283 + * support the output register 284 + */ 285 + if (reportType == 0x03 || maxOutputLength == 0) { 286 + args[index++] = dataRegister & 0xFF; 287 + args[index++] = dataRegister >> 8; 288 + } else { 289 + args[index++] = outputRegister & 0xFF; 290 + args[index++] = outputRegister >> 8; 291 + hidcmd = &hid_no_cmd; 292 + } 287 293 288 294 args[index++] = size & 0xFF; 289 295 args[index++] = size >> 8; ··· 303 289 304 290 memcpy(&args[index], buf, data_len); 305 291 306 - ret = __i2c_hid_command(client, &hid_set_report_cmd, reportID, 292 + ret = __i2c_hid_command(client, hidcmd, reportID, 307 293 reportType, args, args_len, NULL, 0); 308 294 if (ret) { 309 295 dev_err(&client->dev, "failed to set a report to device.\n");
+13 -4
include/uapi/linux/input.h
··· 506 506 #define BTN_DEAD 0x12f 507 507 508 508 #define BTN_GAMEPAD 0x130 509 - #define BTN_A 0x130 510 - #define BTN_B 0x131 509 + #define BTN_SOUTH 0x130 510 + #define BTN_A BTN_SOUTH 511 + #define BTN_EAST 0x131 512 + #define BTN_B BTN_EAST 511 513 #define BTN_C 0x132 512 - #define BTN_X 0x133 513 - #define BTN_Y 0x134 514 + #define BTN_NORTH 0x133 515 + #define BTN_X BTN_NORTH 516 + #define BTN_WEST 0x134 517 + #define BTN_Y BTN_WEST 514 518 #define BTN_Z 0x135 515 519 #define BTN_TL 0x136 516 520 #define BTN_TR 0x137 ··· 710 706 #define KEY_ATTENDANT_OFF 0x21c 711 707 #define KEY_ATTENDANT_TOGGLE 0x21d /* Attendant call on or off */ 712 708 #define KEY_LIGHTS_TOGGLE 0x21e /* Reading light on or off */ 709 + 710 + #define BTN_DPAD_UP 0x220 711 + #define BTN_DPAD_DOWN 0x221 712 + #define BTN_DPAD_LEFT 0x222 713 + #define BTN_DPAD_RIGHT 0x223 713 714 714 715 #define BTN_TRIGGER_HAPPY 0x2c0 715 716 #define BTN_TRIGGER_HAPPY1 0x2c0
+47 -9
net/bluetooth/hidp/core.c
··· 851 851 } 852 852 853 853 /* 854 + * Asynchronous device registration 855 + * HID device drivers might want to perform I/O during initialization to 856 + * detect device types. Therefore, call device registration in a separate 857 + * worker so the HIDP thread can schedule I/O operations. 858 + * Note that this must be called after the worker thread was initialized 859 + * successfully. This will then add the devices and increase session state 860 + * on success, otherwise it will terminate the session thread. 861 + */ 862 + static void hidp_session_dev_work(struct work_struct *work) 863 + { 864 + struct hidp_session *session = container_of(work, 865 + struct hidp_session, 866 + dev_init); 867 + int ret; 868 + 869 + ret = hidp_session_dev_add(session); 870 + if (!ret) 871 + atomic_inc(&session->state); 872 + else 873 + hidp_session_terminate(session); 874 + } 875 + 876 + /* 854 877 * Create new session object 855 878 * Allocate session object, initialize static fields, copy input data into the 856 879 * object and take a reference to all sub-objects. ··· 920 897 session->idle_to = req->idle_to; 921 898 922 899 /* device management */ 900 + INIT_WORK(&session->dev_init, hidp_session_dev_work); 923 901 setup_timer(&session->timer, hidp_idle_timeout, 924 902 (unsigned long)session); 925 903 ··· 1059 1035 * Probe HIDP session 1060 1036 * This is called from the l2cap_conn core when our l2cap_user object is bound 1061 1037 * to the hci-connection. We get the session via the \user object and can now 1062 - * start the session thread, register the HID/input devices and link it into 1063 - * the global session list. 1038 + * start the session thread, link it into the global session list and 1039 + * schedule HID/input device registration. 1064 1040 * The global session-list owns its own reference to the session object so you 1065 1041 * can drop your own reference after registering the l2cap_user object. 1066 1042 */ ··· 1082 1058 goto out_unlock; 1083 1059 } 1084 1060 1061 + if (session->input) { 1062 + ret = hidp_session_dev_add(session); 1063 + if (ret) 1064 + goto out_unlock; 1065 + } 1066 + 1085 1067 ret = hidp_session_start_sync(session); 1086 1068 if (ret) 1087 - goto out_unlock; 1069 + goto out_del; 1088 1070 1089 - ret = hidp_session_dev_add(session); 1090 - if (ret) 1091 - goto out_stop; 1071 + /* HID device registration is async to allow I/O during probe */ 1072 + if (session->input) 1073 + atomic_inc(&session->state); 1074 + else 1075 + schedule_work(&session->dev_init); 1092 1076 1093 1077 hidp_session_get(session); 1094 1078 list_add(&session->list, &hidp_session_list); 1095 1079 ret = 0; 1096 1080 goto out_unlock; 1097 1081 1098 - out_stop: 1099 - hidp_session_terminate(session); 1082 + out_del: 1083 + if (session->input) 1084 + hidp_session_dev_del(session); 1100 1085 out_unlock: 1101 1086 up_write(&hidp_session_sem); 1102 1087 return ret; ··· 1135 1102 down_write(&hidp_session_sem); 1136 1103 1137 1104 hidp_session_terminate(session); 1138 - hidp_session_dev_del(session); 1105 + 1106 + cancel_work_sync(&session->dev_init); 1107 + if (session->input || 1108 + atomic_read(&session->state) > HIDP_SESSION_PREPARING) 1109 + hidp_session_dev_del(session); 1110 + 1139 1111 list_del(&session->list); 1140 1112 1141 1113 up_write(&hidp_session_sem);
+2
net/bluetooth/hidp/hidp.h
··· 128 128 129 129 enum hidp_session_state { 130 130 HIDP_SESSION_IDLING, 131 + HIDP_SESSION_PREPARING, 131 132 HIDP_SESSION_RUNNING, 132 133 }; 133 134 ··· 157 156 unsigned long idle_to; 158 157 159 158 /* device management */ 159 + struct work_struct dev_init; 160 160 struct input_dev *input; 161 161 struct hid_device *hid; 162 162 struct timer_list timer;