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

Merge branch 'for-3.15/hid-core-ll-transport-cleanup' into for-linus

Conflicts:
drivers/hid/hid-ids.h
drivers/hid/hid-sony.c
drivers/hid/i2c-hid/i2c-hid.c

+2386 -444
+317
Documentation/hid/hid-transport.txt
··· 1 + HID I/O Transport Drivers 2 + =========================== 3 + 4 + The HID subsystem is independent of the underlying transport driver. Initially, 5 + only USB was supported, but other specifications adopted the HID design and 6 + provided new transport drivers. The kernel includes at least support for USB, 7 + Bluetooth, I2C and user-space I/O drivers. 8 + 9 + 1) HID Bus 10 + ========== 11 + 12 + The HID subsystem is designed as a bus. Any I/O subsystem may provide HID 13 + devices and register them with the HID bus. HID core then loads generic device 14 + drivers on top of it. The transport drivers are responsible of raw data 15 + transport and device setup/management. HID core is responsible of 16 + report-parsing, report interpretation and the user-space API. Device specifics 17 + and quirks are handled by all layers depending on the quirk. 18 + 19 + +-----------+ +-----------+ +-----------+ +-----------+ 20 + | Device #1 | | Device #i | | Device #j | | Device #k | 21 + +-----------+ +-----------+ +-----------+ +-----------+ 22 + \\ // \\ // 23 + +------------+ +------------+ 24 + | I/O Driver | | I/O Driver | 25 + +------------+ +------------+ 26 + || || 27 + +------------------+ +------------------+ 28 + | Transport Driver | | Transport Driver | 29 + +------------------+ +------------------+ 30 + \___ ___/ 31 + \ / 32 + +----------------+ 33 + | HID Core | 34 + +----------------+ 35 + / | | \ 36 + / | | \ 37 + ____________/ | | \_________________ 38 + / | | \ 39 + / | | \ 40 + +----------------+ +-----------+ +------------------+ +------------------+ 41 + | Generic Driver | | MT Driver | | Custom Driver #1 | | Custom Driver #2 | 42 + +----------------+ +-----------+ +------------------+ +------------------+ 43 + 44 + Example Drivers: 45 + I/O: USB, I2C, Bluetooth-l2cap 46 + Transport: USB-HID, I2C-HID, BT-HIDP 47 + 48 + Everything below "HID Core" is simplified in this graph as it is only of 49 + interest to HID device drivers. Transport drivers do not need to know the 50 + specifics. 51 + 52 + 1.1) Device Setup 53 + ----------------- 54 + 55 + I/O drivers normally provide hotplug detection or device enumeration APIs to the 56 + transport drivers. Transport drivers use this to find any suitable HID device. 57 + They allocate HID device objects and register them with HID core. Transport 58 + drivers are not required to register themselves with HID core. HID core is never 59 + aware of which transport drivers are available and is not interested in it. It 60 + is only interested in devices. 61 + 62 + Transport drivers attach a constant "struct hid_ll_driver" object with each 63 + device. Once a device is registered with HID core, the callbacks provided via 64 + this struct are used by HID core to communicate with the device. 65 + 66 + Transport drivers are responsible of detecting device failures and unplugging. 67 + HID core will operate a device as long as it is registered regardless of any 68 + device failures. Once transport drivers detect unplug or failure events, they 69 + must unregister the device from HID core and HID core will stop using the 70 + provided callbacks. 71 + 72 + 1.2) Transport Driver Requirements 73 + ---------------------------------- 74 + 75 + The terms "asynchronous" and "synchronous" in this document describe the 76 + transmission behavior regarding acknowledgements. An asynchronous channel must 77 + not perform any synchronous operations like waiting for acknowledgements or 78 + verifications. Generally, HID calls operating on asynchronous channels must be 79 + running in atomic-context just fine. 80 + On the other hand, synchronous channels can be implemented by the transport 81 + driver in whatever way they like. They might just be the same as asynchronous 82 + channels, but they can also provide acknowledgement reports, automatic 83 + retransmission on failure, etc. in a blocking manner. If such functionality is 84 + required on asynchronous channels, a transport-driver must implement that via 85 + its own worker threads. 86 + 87 + HID core requires transport drivers to follow a given design. A Transport 88 + driver must provide two bi-directional I/O channels to each HID device. These 89 + channels must not necessarily be bi-directional in the hardware itself. A 90 + transport driver might just provide 4 uni-directional channels. Or it might 91 + multiplex all four on a single physical channel. However, in this document we 92 + will describe them as two bi-directional channels as they have several 93 + properties in common. 94 + 95 + - Interrupt Channel (intr): The intr channel is used for asynchronous data 96 + reports. No management commands or data acknowledgements are sent on this 97 + channel. Any unrequested incoming or outgoing data report must be sent on 98 + this channel and is never acknowledged by the remote side. Devices usually 99 + send their input events on this channel. Outgoing events are normally 100 + not send via intr, except if high throughput is required. 101 + - Control Channel (ctrl): The ctrl channel is used for synchronous requests and 102 + device management. Unrequested data input events must not be sent on this 103 + channel and are normally ignored. Instead, devices only send management 104 + events or answers to host requests on this channel. 105 + The control-channel is used for direct blocking queries to the device 106 + independent of any events on the intr-channel. 107 + Outgoing reports are usually sent on the ctrl channel via synchronous 108 + SET_REPORT requests. 109 + 110 + Communication between devices and HID core is mostly done via HID reports. A 111 + report can be of one of three types: 112 + 113 + - INPUT Report: Input reports provide data from device to host. This 114 + data may include button events, axis events, battery status or more. This 115 + data is generated by the device and sent to the host with or without 116 + requiring explicit requests. Devices can choose to send data continuously or 117 + only on change. 118 + - OUTPUT Report: Output reports change device states. They are sent from host 119 + to device and may include LED requests, rumble requests or more. Output 120 + reports are never sent from device to host, but a host can retrieve their 121 + current state. 122 + Hosts may choose to send output reports either continuously or only on 123 + change. 124 + - FEATURE Report: Feature reports are used for specific static device features 125 + and never reported spontaneously. A host can read and/or write them to access 126 + data like battery-state or device-settings. 127 + Feature reports are never sent without requests. A host must explicitly set 128 + or retrieve a feature report. This also means, feature reports are never sent 129 + on the intr channel as this channel is asynchronous. 130 + 131 + INPUT and OUTPUT reports can be sent as pure data reports on the intr channel. 132 + For INPUT reports this is the usual operational mode. But for OUTPUT reports, 133 + this is rarely done as OUTPUT reports are normally quite scarce. But devices are 134 + free to make excessive use of asynchronous OUTPUT reports (for instance, custom 135 + HID audio speakers make great use of it). 136 + 137 + Plain reports must not be sent on the ctrl channel, though. Instead, the ctrl 138 + channel provides synchronous GET/SET_REPORT requests. Plain reports are only 139 + allowed on the intr channel and are the only means of data there. 140 + 141 + - GET_REPORT: A GET_REPORT request has a report ID as payload and is sent 142 + from host to device. The device must answer with a data report for the 143 + requested report ID on the ctrl channel as a synchronous acknowledgement. 144 + Only one GET_REPORT request can be pending for each device. This restriction 145 + is enforced by HID core as several transport drivers don't allow multiple 146 + simultaneous GET_REPORT requests. 147 + Note that data reports which are sent as answer to a GET_REPORT request are 148 + not handled as generic device events. That is, if a device does not operate 149 + in continuous data reporting mode, an answer to GET_REPORT does not replace 150 + the raw data report on the intr channel on state change. 151 + GET_REPORT is only used by custom HID device drivers to query device state. 152 + Normally, HID core caches any device state so this request is not necessary 153 + on devices that follow the HID specs except during device initialization to 154 + retrieve the current state. 155 + GET_REPORT requests can be sent for any of the 3 report types and shall 156 + return the current report state of the device. However, OUTPUT reports as 157 + payload may be blocked by the underlying transport driver if the 158 + specification does not allow them. 159 + - SET_REPORT: A SET_REPORT request has a report ID plus data as payload. It is 160 + sent from host to device and a device must update it's current report state 161 + according to the given data. Any of the 3 report types can be used. However, 162 + INPUT reports as payload might be blocked by the underlying transport driver 163 + if the specification does not allow them. 164 + A device must answer with a synchronous acknowledgement. However, HID core 165 + does not require transport drivers to forward this acknowledgement to HID 166 + core. 167 + Same as for GET_REPORT, only one SET_REPORT can be pending at a time. This 168 + restriction is enforced by HID core as some transport drivers do not support 169 + multiple synchronous SET_REPORT requests. 170 + 171 + Other ctrl-channel requests are supported by USB-HID but are not available 172 + (or deprecated) in most other transport level specifications: 173 + 174 + - GET/SET_IDLE: Only used by USB-HID and I2C-HID. 175 + - GET/SET_PROTOCOL: Not used by HID core. 176 + - RESET: Used by I2C-HID, not hooked up in HID core. 177 + - SET_POWER: Used by I2C-HID, not hooked up in HID core. 178 + 179 + 2) HID API 180 + ========== 181 + 182 + 2.1) Initialization 183 + ------------------- 184 + 185 + Transport drivers normally use the following procedure to register a new device 186 + with HID core: 187 + 188 + struct hid_device *hid; 189 + int ret; 190 + 191 + hid = hid_allocate_device(); 192 + if (IS_ERR(hid)) { 193 + ret = PTR_ERR(hid); 194 + goto err_<...>; 195 + } 196 + 197 + strlcpy(hid->name, <device-name-src>, 127); 198 + strlcpy(hid->phys, <device-phys-src>, 63); 199 + strlcpy(hid->uniq, <device-uniq-src>, 63); 200 + 201 + hid->ll_driver = &custom_ll_driver; 202 + hid->bus = <device-bus>; 203 + hid->vendor = <device-vendor>; 204 + hid->product = <device-product>; 205 + hid->version = <device-version>; 206 + hid->country = <device-country>; 207 + hid->dev.parent = <pointer-to-parent-device>; 208 + hid->driver_data = <transport-driver-data-field>; 209 + 210 + ret = hid_add_device(hid); 211 + if (ret) 212 + goto err_<...>; 213 + 214 + Once hid_add_device() is entered, HID core might use the callbacks provided in 215 + "custom_ll_driver". Note that fields like "country" can be ignored by underlying 216 + transport-drivers if not supported. 217 + 218 + To unregister a device, use: 219 + 220 + hid_destroy_device(hid); 221 + 222 + Once hid_destroy_device() returns, HID core will no longer make use of any 223 + driver callbacks. 224 + 225 + 2.2) hid_ll_driver operations 226 + ----------------------------- 227 + 228 + The available HID callbacks are: 229 + - int (*start) (struct hid_device *hdev) 230 + Called from HID device drivers once they want to use the device. Transport 231 + drivers can choose to setup their device in this callback. However, normally 232 + devices are already set up before transport drivers register them to HID core 233 + so this is mostly only used by USB-HID. 234 + 235 + - void (*stop) (struct hid_device *hdev) 236 + Called from HID device drivers once they are done with a device. Transport 237 + drivers can free any buffers and deinitialize the device. But note that 238 + ->start() might be called again if another HID device driver is loaded on the 239 + device. 240 + Transport drivers are free to ignore it and deinitialize devices after they 241 + destroyed them via hid_destroy_device(). 242 + 243 + - int (*open) (struct hid_device *hdev) 244 + Called from HID device drivers once they are interested in data reports. 245 + Usually, while user-space didn't open any input API/etc., device drivers are 246 + not interested in device data and transport drivers can put devices asleep. 247 + However, once ->open() is called, transport drivers must be ready for I/O. 248 + ->open() calls are nested for each client that opens the HID device. 249 + 250 + - void (*close) (struct hid_device *hdev) 251 + Called from HID device drivers after ->open() was called but they are no 252 + longer interested in device reports. (Usually if user-space closed any input 253 + devices of the driver). 254 + Transport drivers can put devices asleep and terminate any I/O of all 255 + ->open() calls have been followed by a ->close() call. However, ->start() may 256 + be called again if the device driver is interested in input reports again. 257 + 258 + - int (*parse) (struct hid_device *hdev) 259 + Called once during device setup after ->start() has been called. Transport 260 + drivers must read the HID report-descriptor from the device and tell HID core 261 + about it via hid_parse_report(). 262 + 263 + - int (*power) (struct hid_device *hdev, int level) 264 + Called by HID core to give PM hints to transport drivers. Usually this is 265 + analogical to the ->open() and ->close() hints and redundant. 266 + 267 + - void (*request) (struct hid_device *hdev, struct hid_report *report, 268 + int reqtype) 269 + Send an HID request on the ctrl channel. "report" contains the report that 270 + should be sent and "reqtype" the request type. Request-type can be 271 + HID_REQ_SET_REPORT or HID_REQ_GET_REPORT. 272 + This callback is optional. If not provided, HID core will assemble a raw 273 + report following the HID specs and send it via the ->raw_request() callback. 274 + The transport driver is free to implement this asynchronously. 275 + 276 + - int (*wait) (struct hid_device *hdev) 277 + Used by HID core before calling ->request() again. A transport driver can use 278 + it to wait for any pending requests to complete if only one request is 279 + allowed at a time. 280 + 281 + - int (*raw_request) (struct hid_device *hdev, unsigned char reportnum, 282 + __u8 *buf, size_t count, unsigned char rtype, 283 + int reqtype) 284 + Same as ->request() but provides the report as raw buffer. This request shall 285 + be synchronous. A transport driver must not use ->wait() to complete such 286 + requests. This request is mandatory and hid core will reject the device if 287 + it is missing. 288 + 289 + - int (*output_report) (struct hid_device *hdev, __u8 *buf, size_t len) 290 + Send raw output report via intr channel. Used by some HID device drivers 291 + which require high throughput for outgoing requests on the intr channel. This 292 + must not cause SET_REPORT calls! This must be implemented as asynchronous 293 + output report on the intr channel! 294 + 295 + - int (*idle) (struct hid_device *hdev, int report, int idle, int reqtype) 296 + Perform SET/GET_IDLE request. Only used by USB-HID, do not implement! 297 + 298 + 2.3) Data Path 299 + -------------- 300 + 301 + Transport drivers are responsible of reading data from I/O devices. They must 302 + handle any I/O-related state-tracking themselves. HID core does not implement 303 + protocol handshakes or other management commands which can be required by the 304 + given HID transport specification. 305 + 306 + Every raw data packet read from a device must be fed into HID core via 307 + hid_input_report(). You must specify the channel-type (intr or ctrl) and report 308 + type (input/output/feature). Under normal conditions, only input reports are 309 + provided via this API. 310 + 311 + Responses to GET_REPORT requests via ->request() must also be provided via this 312 + API. Responses to ->raw_request() are synchronous and must be intercepted by the 313 + transport driver and not passed to hid_input_report(). 314 + Acknowledgements to SET_REPORT requests are not of interest to HID core. 315 + 316 + ---------------------------------------------------- 317 + Written 2013, David Herrmann <dh.herrmann@gmail.com>
+15 -4
drivers/hid/Kconfig
··· 175 175 multimedia keyboard, but will lack support for the musical keyboard 176 176 and some additional multimedia keys. 177 177 178 + config HID_CP2112 179 + tristate "Silicon Labs CP2112 HID USB-to-SMBus Bridge support" 180 + depends on USB_HID && I2C && GPIOLIB 181 + ---help--- 182 + Support for Silicon Labs CP2112 HID USB to SMBus Master Bridge. 183 + This is a HID device driver which registers as an i2c adapter 184 + and gpiochip to expose these functions of the CP2112. The 185 + customizable USB descriptor fields are exposed as sysfs attributes. 186 + 178 187 config HID_CYPRESS 179 188 tristate "Cypress mouse and barcode readers" if EXPERT 180 189 depends on HID ··· 617 608 Support for Samsung InfraRed remote control or keyboards. 618 609 619 610 config HID_SONY 620 - tristate "Sony PS2/3 accessories" 611 + tristate "Sony PS2/3/4 accessories" 621 612 depends on USB_HID 622 613 depends on NEW_LEDS 623 614 depends on LEDS_CLASS 615 + select POWER_SUPPLY 624 616 ---help--- 625 617 Support for 626 618 627 619 * Sony PS3 6-axis controllers 620 + * Sony PS4 DualShock 4 controllers 628 621 * Buzz controllers 629 622 * Sony PS3 Blue-ray Disk Remote Control (Bluetooth) 630 623 * Logitech Harmony adapter for Sony Playstation 3 (Bluetooth) 631 624 632 625 config SONY_FF 633 - bool "Sony PS2/3 accessories force feedback support" 626 + bool "Sony PS2/3/4 accessories force feedback support" 634 627 depends on HID_SONY 635 628 select INPUT_FF_MEMLESS 636 629 ---help--- 637 - Say Y here if you have a Sony PS2/3 accessory and want to enable force 638 - feedback support for it. 630 + Say Y here if you have a Sony PS2/3/4 accessory and want to enable 631 + force feedback support for it. 639 632 640 633 config HID_SPEEDLINK 641 634 tristate "Speedlink VAD Cezanne mouse support"
+1
drivers/hid/Makefile
··· 41 41 obj-$(CONFIG_HID_BELKIN) += hid-belkin.o 42 42 obj-$(CONFIG_HID_CHERRY) += hid-cherry.o 43 43 obj-$(CONFIG_HID_CHICONY) += hid-chicony.o 44 + obj-$(CONFIG_HID_CP2112) += hid-cp2112.o 44 45 obj-$(CONFIG_HID_CYPRESS) += hid-cypress.o 45 46 obj-$(CONFIG_HID_DRAGONRISE) += hid-dr.o 46 47 obj-$(CONFIG_HID_EMS_FF) += hid-emsff.o
+50 -1
drivers/hid/hid-core.c
··· 1248 1248 } 1249 1249 EXPORT_SYMBOL_GPL(hid_output_report); 1250 1250 1251 + static int hid_report_len(struct hid_report *report) 1252 + { 1253 + return ((report->size - 1) >> 3) + 1 + (report->id > 0) + 7; 1254 + } 1255 + 1251 1256 /* 1252 1257 * Allocator for buffer that is going to be passed to hid_output_report() 1253 1258 */ ··· 1263 1258 * of implement() working on 8 byte chunks 1264 1259 */ 1265 1260 1266 - int len = ((report->size - 1) >> 3) + 1 + (report->id > 0) + 7; 1261 + int len = hid_report_len(report); 1267 1262 1268 1263 return kmalloc(len, flags); 1269 1264 } ··· 1318 1313 1319 1314 return report; 1320 1315 } 1316 + 1317 + /* 1318 + * Implement a generic .request() callback, using .raw_request() 1319 + * DO NOT USE in hid drivers directly, but through hid_hw_request instead. 1320 + */ 1321 + void __hid_request(struct hid_device *hid, struct hid_report *report, 1322 + int reqtype) 1323 + { 1324 + char *buf; 1325 + int ret; 1326 + int len; 1327 + 1328 + buf = hid_alloc_report_buf(report, GFP_KERNEL); 1329 + if (!buf) 1330 + return; 1331 + 1332 + len = hid_report_len(report); 1333 + 1334 + if (reqtype == HID_REQ_SET_REPORT) 1335 + hid_output_report(report, buf); 1336 + 1337 + ret = hid->ll_driver->raw_request(hid, report->id, buf, len, 1338 + report->type, reqtype); 1339 + if (ret < 0) { 1340 + dbg_hid("unable to complete request: %d\n", ret); 1341 + goto out; 1342 + } 1343 + 1344 + if (reqtype == HID_REQ_GET_REPORT) 1345 + hid_input_report(hid, report->type, buf, ret, 0); 1346 + 1347 + out: 1348 + kfree(buf); 1349 + } 1350 + EXPORT_SYMBOL_GPL(__hid_request); 1321 1351 1322 1352 int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size, 1323 1353 int interrupt) ··· 1733 1693 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) }, 1734 1694 { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_AK1D) }, 1735 1695 { HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_PRODIKEYS_PCMIDI) }, 1696 + { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) }, 1736 1697 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) }, 1737 1698 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) }, 1738 1699 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) }, ··· 2471 2430 * wait for coming driver */ 2472 2431 if (hid_ignore(hdev)) 2473 2432 return -ENODEV; 2433 + 2434 + /* 2435 + * Check for the mandatory transport channel. 2436 + */ 2437 + if (!hdev->ll_driver->raw_request) { 2438 + hid_err(hdev, "transport driver missing .raw_request()\n"); 2439 + return -EINVAL; 2440 + } 2474 2441 2475 2442 /* 2476 2443 * Read the device report descriptor once and use as template
+1073
drivers/hid/hid-cp2112.c
··· 1 + /* 2 + * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge 3 + * Copyright (c) 2013,2014 Uplogix, Inc. 4 + * David Barksdale <dbarksdale@uplogix.com> 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + */ 15 + 16 + /* 17 + * The Silicon Labs CP2112 chip is a USB HID device which provides an 18 + * SMBus controller for talking to slave devices and 8 GPIO pins. The 19 + * host communicates with the CP2112 via raw HID reports. 20 + * 21 + * Data Sheet: 22 + * http://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf 23 + * Programming Interface Specification: 24 + * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN495.pdf 25 + */ 26 + 27 + #include <linux/gpio.h> 28 + #include <linux/hid.h> 29 + #include <linux/i2c.h> 30 + #include <linux/module.h> 31 + #include <linux/nls.h> 32 + #include <linux/usb/ch9.h> 33 + #include "hid-ids.h" 34 + 35 + enum { 36 + CP2112_GPIO_CONFIG = 0x02, 37 + CP2112_GPIO_GET = 0x03, 38 + CP2112_GPIO_SET = 0x04, 39 + CP2112_GET_VERSION_INFO = 0x05, 40 + CP2112_SMBUS_CONFIG = 0x06, 41 + CP2112_DATA_READ_REQUEST = 0x10, 42 + CP2112_DATA_WRITE_READ_REQUEST = 0x11, 43 + CP2112_DATA_READ_FORCE_SEND = 0x12, 44 + CP2112_DATA_READ_RESPONSE = 0x13, 45 + CP2112_DATA_WRITE_REQUEST = 0x14, 46 + CP2112_TRANSFER_STATUS_REQUEST = 0x15, 47 + CP2112_TRANSFER_STATUS_RESPONSE = 0x16, 48 + CP2112_CANCEL_TRANSFER = 0x17, 49 + CP2112_LOCK_BYTE = 0x20, 50 + CP2112_USB_CONFIG = 0x21, 51 + CP2112_MANUFACTURER_STRING = 0x22, 52 + CP2112_PRODUCT_STRING = 0x23, 53 + CP2112_SERIAL_STRING = 0x24, 54 + }; 55 + 56 + enum { 57 + STATUS0_IDLE = 0x00, 58 + STATUS0_BUSY = 0x01, 59 + STATUS0_COMPLETE = 0x02, 60 + STATUS0_ERROR = 0x03, 61 + }; 62 + 63 + enum { 64 + STATUS1_TIMEOUT_NACK = 0x00, 65 + STATUS1_TIMEOUT_BUS = 0x01, 66 + STATUS1_ARBITRATION_LOST = 0x02, 67 + STATUS1_READ_INCOMPLETE = 0x03, 68 + STATUS1_WRITE_INCOMPLETE = 0x04, 69 + STATUS1_SUCCESS = 0x05, 70 + }; 71 + 72 + struct cp2112_smbus_config_report { 73 + u8 report; /* CP2112_SMBUS_CONFIG */ 74 + __be32 clock_speed; /* Hz */ 75 + u8 device_address; /* Stored in the upper 7 bits */ 76 + u8 auto_send_read; /* 1 = enabled, 0 = disabled */ 77 + __be16 write_timeout; /* ms, 0 = no timeout */ 78 + __be16 read_timeout; /* ms, 0 = no timeout */ 79 + u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */ 80 + __be16 retry_time; /* # of retries, 0 = no limit */ 81 + } __packed; 82 + 83 + struct cp2112_usb_config_report { 84 + u8 report; /* CP2112_USB_CONFIG */ 85 + __le16 vid; /* Vendor ID */ 86 + __le16 pid; /* Product ID */ 87 + u8 max_power; /* Power requested in 2mA units */ 88 + u8 power_mode; /* 0x00 = bus powered 89 + 0x01 = self powered & regulator off 90 + 0x02 = self powered & regulator on */ 91 + u8 release_major; 92 + u8 release_minor; 93 + u8 mask; /* What fields to program */ 94 + } __packed; 95 + 96 + struct cp2112_read_req_report { 97 + u8 report; /* CP2112_DATA_READ_REQUEST */ 98 + u8 slave_address; 99 + __be16 length; 100 + } __packed; 101 + 102 + struct cp2112_write_read_req_report { 103 + u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */ 104 + u8 slave_address; 105 + __be16 length; 106 + u8 target_address_length; 107 + u8 target_address[16]; 108 + } __packed; 109 + 110 + struct cp2112_write_req_report { 111 + u8 report; /* CP2112_DATA_WRITE_REQUEST */ 112 + u8 slave_address; 113 + u8 length; 114 + u8 data[61]; 115 + } __packed; 116 + 117 + struct cp2112_force_read_report { 118 + u8 report; /* CP2112_DATA_READ_FORCE_SEND */ 119 + __be16 length; 120 + } __packed; 121 + 122 + struct cp2112_xfer_status_report { 123 + u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */ 124 + u8 status0; /* STATUS0_* */ 125 + u8 status1; /* STATUS1_* */ 126 + __be16 retries; 127 + __be16 length; 128 + } __packed; 129 + 130 + struct cp2112_string_report { 131 + u8 dummy; /* force .string to be aligned */ 132 + u8 report; /* CP2112_*_STRING */ 133 + u8 length; /* length in bytes of everyting after .report */ 134 + u8 type; /* USB_DT_STRING */ 135 + wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */ 136 + } __packed; 137 + 138 + /* Number of times to request transfer status before giving up waiting for a 139 + transfer to complete. This may need to be changed if SMBUS clock, retries, 140 + or read/write/scl_low timeout settings are changed. */ 141 + static const int XFER_STATUS_RETRIES = 10; 142 + 143 + /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or 144 + CP2112_TRANSFER_STATUS_RESPONSE. */ 145 + static const int RESPONSE_TIMEOUT = 50; 146 + 147 + static const struct hid_device_id cp2112_devices[] = { 148 + { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) }, 149 + { } 150 + }; 151 + MODULE_DEVICE_TABLE(hid, cp2112_devices); 152 + 153 + struct cp2112_device { 154 + struct i2c_adapter adap; 155 + struct hid_device *hdev; 156 + wait_queue_head_t wait; 157 + u8 read_data[61]; 158 + u8 read_length; 159 + int xfer_status; 160 + atomic_t read_avail; 161 + atomic_t xfer_avail; 162 + struct gpio_chip gc; 163 + }; 164 + 165 + static int gpio_push_pull = 0xFF; 166 + module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR); 167 + MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask"); 168 + 169 + static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset) 170 + { 171 + struct cp2112_device *dev = container_of(chip, struct cp2112_device, 172 + gc); 173 + struct hid_device *hdev = dev->hdev; 174 + u8 buf[5]; 175 + int ret; 176 + 177 + ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, 178 + sizeof(buf), HID_FEATURE_REPORT, 179 + HID_REQ_GET_REPORT); 180 + if (ret != sizeof(buf)) { 181 + hid_err(hdev, "error requesting GPIO config: %d\n", ret); 182 + return ret; 183 + } 184 + 185 + buf[1] &= ~(1 << offset); 186 + buf[2] = gpio_push_pull; 187 + 188 + ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf), 189 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 190 + if (ret < 0) { 191 + hid_err(hdev, "error setting GPIO config: %d\n", ret); 192 + return ret; 193 + } 194 + 195 + return 0; 196 + } 197 + 198 + static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value) 199 + { 200 + struct cp2112_device *dev = container_of(chip, struct cp2112_device, 201 + gc); 202 + struct hid_device *hdev = dev->hdev; 203 + u8 buf[3]; 204 + int ret; 205 + 206 + buf[0] = CP2112_GPIO_SET; 207 + buf[1] = value ? 0xff : 0; 208 + buf[2] = 1 << offset; 209 + 210 + ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf, sizeof(buf), 211 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 212 + if (ret < 0) 213 + hid_err(hdev, "error setting GPIO values: %d\n", ret); 214 + } 215 + 216 + static int cp2112_gpio_get(struct gpio_chip *chip, unsigned offset) 217 + { 218 + struct cp2112_device *dev = container_of(chip, struct cp2112_device, 219 + gc); 220 + struct hid_device *hdev = dev->hdev; 221 + u8 buf[2]; 222 + int ret; 223 + 224 + ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf, sizeof(buf), 225 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 226 + if (ret != sizeof(buf)) { 227 + hid_err(hdev, "error requesting GPIO values: %d\n", ret); 228 + return ret; 229 + } 230 + 231 + return (buf[1] >> offset) & 1; 232 + } 233 + 234 + static int cp2112_gpio_direction_output(struct gpio_chip *chip, 235 + unsigned offset, int value) 236 + { 237 + struct cp2112_device *dev = container_of(chip, struct cp2112_device, 238 + gc); 239 + struct hid_device *hdev = dev->hdev; 240 + u8 buf[5]; 241 + int ret; 242 + 243 + cp2112_gpio_set(chip, offset, value); 244 + 245 + ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, 246 + sizeof(buf), HID_FEATURE_REPORT, 247 + HID_REQ_GET_REPORT); 248 + if (ret != sizeof(buf)) { 249 + hid_err(hdev, "error requesting GPIO config: %d\n", ret); 250 + return ret; 251 + } 252 + 253 + buf[1] |= 1 << offset; 254 + buf[2] = gpio_push_pull; 255 + 256 + ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, sizeof(buf), 257 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 258 + if (ret < 0) { 259 + hid_err(hdev, "error setting GPIO config: %d\n", ret); 260 + return ret; 261 + } 262 + 263 + return 0; 264 + } 265 + 266 + static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number, 267 + u8 *data, size_t count, unsigned char report_type) 268 + { 269 + u8 *buf; 270 + int ret; 271 + 272 + buf = kmalloc(count, GFP_KERNEL); 273 + if (!buf) 274 + return -ENOMEM; 275 + 276 + ret = hid_hw_raw_request(hdev, report_number, buf, count, 277 + report_type, HID_REQ_GET_REPORT); 278 + memcpy(data, buf, count); 279 + kfree(buf); 280 + return ret; 281 + } 282 + 283 + static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count, 284 + unsigned char report_type) 285 + { 286 + u8 *buf; 287 + int ret; 288 + 289 + buf = kmemdup(data, count, GFP_KERNEL); 290 + if (!buf) 291 + return -ENOMEM; 292 + 293 + if (report_type == HID_OUTPUT_REPORT) 294 + ret = hid_hw_output_report(hdev, buf, count); 295 + else 296 + ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type, 297 + HID_REQ_SET_REPORT); 298 + 299 + kfree(buf); 300 + return ret; 301 + } 302 + 303 + static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail) 304 + { 305 + int ret = 0; 306 + 307 + /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a 308 + * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to 309 + * come in cp2112_raw_event or timeout. There will only be one of these 310 + * in flight at any one time. The timeout is extremely large and is a 311 + * last resort if the CP2112 has died. If we do timeout we don't expect 312 + * to receive the response which would cause data races, it's not like 313 + * we can do anything about it anyway. 314 + */ 315 + ret = wait_event_interruptible_timeout(dev->wait, 316 + atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT)); 317 + if (-ERESTARTSYS == ret) 318 + return ret; 319 + if (!ret) 320 + return -ETIMEDOUT; 321 + 322 + atomic_set(avail, 0); 323 + return 0; 324 + } 325 + 326 + static int cp2112_xfer_status(struct cp2112_device *dev) 327 + { 328 + struct hid_device *hdev = dev->hdev; 329 + u8 buf[2]; 330 + int ret; 331 + 332 + buf[0] = CP2112_TRANSFER_STATUS_REQUEST; 333 + buf[1] = 0x01; 334 + atomic_set(&dev->xfer_avail, 0); 335 + 336 + ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); 337 + if (ret < 0) { 338 + hid_warn(hdev, "Error requesting status: %d\n", ret); 339 + return ret; 340 + } 341 + 342 + ret = cp2112_wait(dev, &dev->xfer_avail); 343 + if (ret) 344 + return ret; 345 + 346 + return dev->xfer_status; 347 + } 348 + 349 + static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size) 350 + { 351 + struct hid_device *hdev = dev->hdev; 352 + struct cp2112_force_read_report report; 353 + int ret; 354 + 355 + report.report = CP2112_DATA_READ_FORCE_SEND; 356 + report.length = cpu_to_be16(size); 357 + 358 + atomic_set(&dev->read_avail, 0); 359 + 360 + ret = cp2112_hid_output(hdev, &report.report, sizeof(report), 361 + HID_OUTPUT_REPORT); 362 + if (ret < 0) { 363 + hid_warn(hdev, "Error requesting data: %d\n", ret); 364 + return ret; 365 + } 366 + 367 + ret = cp2112_wait(dev, &dev->read_avail); 368 + if (ret) 369 + return ret; 370 + 371 + hid_dbg(hdev, "read %d of %zd bytes requested\n", 372 + dev->read_length, size); 373 + 374 + if (size > dev->read_length) 375 + size = dev->read_length; 376 + 377 + memcpy(data, dev->read_data, size); 378 + return dev->read_length; 379 + } 380 + 381 + static int cp2112_read_req(void *buf, u8 slave_address, u16 length) 382 + { 383 + struct cp2112_read_req_report *report = buf; 384 + 385 + if (length < 1 || length > 512) 386 + return -EINVAL; 387 + 388 + report->report = CP2112_DATA_READ_REQUEST; 389 + report->slave_address = slave_address << 1; 390 + report->length = cpu_to_be16(length); 391 + return sizeof(*report); 392 + } 393 + 394 + static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length, 395 + u8 command, u8 *data, u8 data_length) 396 + { 397 + struct cp2112_write_read_req_report *report = buf; 398 + 399 + if (length < 1 || length > 512 400 + || data_length > sizeof(report->target_address) - 1) 401 + return -EINVAL; 402 + 403 + report->report = CP2112_DATA_WRITE_READ_REQUEST; 404 + report->slave_address = slave_address << 1; 405 + report->length = cpu_to_be16(length); 406 + report->target_address_length = data_length + 1; 407 + report->target_address[0] = command; 408 + memcpy(&report->target_address[1], data, data_length); 409 + return data_length + 6; 410 + } 411 + 412 + static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data, 413 + u8 data_length) 414 + { 415 + struct cp2112_write_req_report *report = buf; 416 + 417 + if (data_length > sizeof(report->data) - 1) 418 + return -EINVAL; 419 + 420 + report->report = CP2112_DATA_WRITE_REQUEST; 421 + report->slave_address = slave_address << 1; 422 + report->length = data_length + 1; 423 + report->data[0] = command; 424 + memcpy(&report->data[1], data, data_length); 425 + return data_length + 4; 426 + } 427 + 428 + static int cp2112_xfer(struct i2c_adapter *adap, u16 addr, 429 + unsigned short flags, char read_write, u8 command, 430 + int size, union i2c_smbus_data *data) 431 + { 432 + struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data; 433 + struct hid_device *hdev = dev->hdev; 434 + u8 buf[64]; 435 + __be16 word; 436 + ssize_t count; 437 + size_t read_length = 0; 438 + unsigned int retries; 439 + int ret; 440 + 441 + hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n", 442 + read_write == I2C_SMBUS_WRITE ? "write" : "read", 443 + addr, flags, command, size); 444 + 445 + switch (size) { 446 + case I2C_SMBUS_BYTE: 447 + read_length = 1; 448 + 449 + if (I2C_SMBUS_READ == read_write) 450 + count = cp2112_read_req(buf, addr, read_length); 451 + else 452 + count = cp2112_write_req(buf, addr, data->byte, NULL, 453 + 0); 454 + break; 455 + case I2C_SMBUS_BYTE_DATA: 456 + read_length = 1; 457 + 458 + if (I2C_SMBUS_READ == read_write) 459 + count = cp2112_write_read_req(buf, addr, read_length, 460 + command, NULL, 0); 461 + else 462 + count = cp2112_write_req(buf, addr, command, 463 + &data->byte, 1); 464 + break; 465 + case I2C_SMBUS_WORD_DATA: 466 + read_length = 2; 467 + word = cpu_to_be16(data->word); 468 + 469 + if (I2C_SMBUS_READ == read_write) 470 + count = cp2112_write_read_req(buf, addr, read_length, 471 + command, NULL, 0); 472 + else 473 + count = cp2112_write_req(buf, addr, command, 474 + (u8 *)&word, 2); 475 + break; 476 + case I2C_SMBUS_PROC_CALL: 477 + size = I2C_SMBUS_WORD_DATA; 478 + read_write = I2C_SMBUS_READ; 479 + read_length = 2; 480 + word = cpu_to_be16(data->word); 481 + 482 + count = cp2112_write_read_req(buf, addr, read_length, command, 483 + (u8 *)&word, 2); 484 + break; 485 + case I2C_SMBUS_I2C_BLOCK_DATA: 486 + size = I2C_SMBUS_BLOCK_DATA; 487 + /* fallthrough */ 488 + case I2C_SMBUS_BLOCK_DATA: 489 + if (I2C_SMBUS_READ == read_write) { 490 + count = cp2112_write_read_req(buf, addr, 491 + I2C_SMBUS_BLOCK_MAX, 492 + command, NULL, 0); 493 + } else { 494 + count = cp2112_write_req(buf, addr, command, 495 + data->block, 496 + data->block[0] + 1); 497 + } 498 + break; 499 + case I2C_SMBUS_BLOCK_PROC_CALL: 500 + size = I2C_SMBUS_BLOCK_DATA; 501 + read_write = I2C_SMBUS_READ; 502 + 503 + count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX, 504 + command, data->block, 505 + data->block[0] + 1); 506 + break; 507 + default: 508 + hid_warn(hdev, "Unsupported transaction %d\n", size); 509 + return -EOPNOTSUPP; 510 + } 511 + 512 + if (count < 0) 513 + return count; 514 + 515 + ret = hid_hw_power(hdev, PM_HINT_FULLON); 516 + if (ret < 0) { 517 + hid_err(hdev, "power management error: %d\n", ret); 518 + return ret; 519 + } 520 + 521 + ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT); 522 + if (ret < 0) { 523 + hid_warn(hdev, "Error starting transaction: %d\n", ret); 524 + goto power_normal; 525 + } 526 + 527 + for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) { 528 + ret = cp2112_xfer_status(dev); 529 + if (-EBUSY == ret) 530 + continue; 531 + if (ret < 0) 532 + goto power_normal; 533 + break; 534 + } 535 + 536 + if (XFER_STATUS_RETRIES <= retries) { 537 + hid_warn(hdev, "Transfer timed out, cancelling.\n"); 538 + buf[0] = CP2112_CANCEL_TRANSFER; 539 + buf[1] = 0x01; 540 + 541 + ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); 542 + if (ret < 0) 543 + hid_warn(hdev, "Error cancelling transaction: %d\n", 544 + ret); 545 + 546 + ret = -ETIMEDOUT; 547 + goto power_normal; 548 + } 549 + 550 + if (I2C_SMBUS_WRITE == read_write) { 551 + ret = 0; 552 + goto power_normal; 553 + } 554 + 555 + if (I2C_SMBUS_BLOCK_DATA == size) 556 + read_length = ret; 557 + 558 + ret = cp2112_read(dev, buf, read_length); 559 + if (ret < 0) 560 + goto power_normal; 561 + if (ret != read_length) { 562 + hid_warn(hdev, "short read: %d < %zd\n", ret, read_length); 563 + ret = -EIO; 564 + goto power_normal; 565 + } 566 + 567 + switch (size) { 568 + case I2C_SMBUS_BYTE: 569 + case I2C_SMBUS_BYTE_DATA: 570 + data->byte = buf[0]; 571 + break; 572 + case I2C_SMBUS_WORD_DATA: 573 + data->word = be16_to_cpup((__be16 *)buf); 574 + break; 575 + case I2C_SMBUS_BLOCK_DATA: 576 + if (read_length > I2C_SMBUS_BLOCK_MAX) { 577 + ret = -EPROTO; 578 + goto power_normal; 579 + } 580 + 581 + memcpy(data->block, buf, read_length); 582 + break; 583 + } 584 + 585 + ret = 0; 586 + power_normal: 587 + hid_hw_power(hdev, PM_HINT_NORMAL); 588 + hid_dbg(hdev, "transfer finished: %d\n", ret); 589 + return ret; 590 + } 591 + 592 + static u32 cp2112_functionality(struct i2c_adapter *adap) 593 + { 594 + return I2C_FUNC_SMBUS_BYTE | 595 + I2C_FUNC_SMBUS_BYTE_DATA | 596 + I2C_FUNC_SMBUS_WORD_DATA | 597 + I2C_FUNC_SMBUS_BLOCK_DATA | 598 + I2C_FUNC_SMBUS_I2C_BLOCK | 599 + I2C_FUNC_SMBUS_PROC_CALL | 600 + I2C_FUNC_SMBUS_BLOCK_PROC_CALL; 601 + } 602 + 603 + static const struct i2c_algorithm smbus_algorithm = { 604 + .smbus_xfer = cp2112_xfer, 605 + .functionality = cp2112_functionality, 606 + }; 607 + 608 + static int cp2112_get_usb_config(struct hid_device *hdev, 609 + struct cp2112_usb_config_report *cfg) 610 + { 611 + int ret; 612 + 613 + ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg), 614 + HID_FEATURE_REPORT); 615 + if (ret != sizeof(*cfg)) { 616 + hid_err(hdev, "error reading usb config: %d\n", ret); 617 + if (ret < 0) 618 + return ret; 619 + return -EIO; 620 + } 621 + 622 + return 0; 623 + } 624 + 625 + static int cp2112_set_usb_config(struct hid_device *hdev, 626 + struct cp2112_usb_config_report *cfg) 627 + { 628 + int ret; 629 + 630 + BUG_ON(cfg->report != CP2112_USB_CONFIG); 631 + 632 + ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg), 633 + HID_FEATURE_REPORT); 634 + if (ret != sizeof(*cfg)) { 635 + hid_err(hdev, "error writing usb config: %d\n", ret); 636 + if (ret < 0) 637 + return ret; 638 + return -EIO; 639 + } 640 + 641 + return 0; 642 + } 643 + 644 + static void chmod_sysfs_attrs(struct hid_device *hdev); 645 + 646 + #define CP2112_CONFIG_ATTR(name, store, format, ...) \ 647 + static ssize_t name##_store(struct device *kdev, \ 648 + struct device_attribute *attr, const char *buf, \ 649 + size_t count) \ 650 + { \ 651 + struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \ 652 + struct cp2112_usb_config_report cfg; \ 653 + int ret = cp2112_get_usb_config(hdev, &cfg); \ 654 + if (ret) \ 655 + return ret; \ 656 + store; \ 657 + ret = cp2112_set_usb_config(hdev, &cfg); \ 658 + if (ret) \ 659 + return ret; \ 660 + chmod_sysfs_attrs(hdev); \ 661 + return count; \ 662 + } \ 663 + static ssize_t name##_show(struct device *kdev, \ 664 + struct device_attribute *attr, char *buf) \ 665 + { \ 666 + struct hid_device *hdev = container_of(kdev, struct hid_device, dev); \ 667 + struct cp2112_usb_config_report cfg; \ 668 + int ret = cp2112_get_usb_config(hdev, &cfg); \ 669 + if (ret) \ 670 + return ret; \ 671 + return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \ 672 + } \ 673 + static DEVICE_ATTR_RW(name); 674 + 675 + CP2112_CONFIG_ATTR(vendor_id, ({ 676 + u16 vid; 677 + 678 + if (sscanf(buf, "%hi", &vid) != 1) 679 + return -EINVAL; 680 + 681 + cfg.vid = cpu_to_le16(vid); 682 + cfg.mask = 0x01; 683 + }), "0x%04x\n", le16_to_cpu(cfg.vid)); 684 + 685 + CP2112_CONFIG_ATTR(product_id, ({ 686 + u16 pid; 687 + 688 + if (sscanf(buf, "%hi", &pid) != 1) 689 + return -EINVAL; 690 + 691 + cfg.pid = cpu_to_le16(pid); 692 + cfg.mask = 0x02; 693 + }), "0x%04x\n", le16_to_cpu(cfg.pid)); 694 + 695 + CP2112_CONFIG_ATTR(max_power, ({ 696 + int mA; 697 + 698 + if (sscanf(buf, "%i", &mA) != 1) 699 + return -EINVAL; 700 + 701 + cfg.max_power = (mA + 1) / 2; 702 + cfg.mask = 0x04; 703 + }), "%u mA\n", cfg.max_power * 2); 704 + 705 + CP2112_CONFIG_ATTR(power_mode, ({ 706 + if (sscanf(buf, "%hhi", &cfg.power_mode) != 1) 707 + return -EINVAL; 708 + 709 + cfg.mask = 0x08; 710 + }), "%u\n", cfg.power_mode); 711 + 712 + CP2112_CONFIG_ATTR(release_version, ({ 713 + if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor) 714 + != 2) 715 + return -EINVAL; 716 + 717 + cfg.mask = 0x10; 718 + }), "%u.%u\n", cfg.release_major, cfg.release_minor); 719 + 720 + #undef CP2112_CONFIG_ATTR 721 + 722 + struct cp2112_pstring_attribute { 723 + struct device_attribute attr; 724 + unsigned char report; 725 + }; 726 + 727 + static ssize_t pstr_store(struct device *kdev, 728 + struct device_attribute *kattr, const char *buf, 729 + size_t count) 730 + { 731 + struct hid_device *hdev = container_of(kdev, struct hid_device, dev); 732 + struct cp2112_pstring_attribute *attr = 733 + container_of(kattr, struct cp2112_pstring_attribute, attr); 734 + struct cp2112_string_report report; 735 + int ret; 736 + 737 + memset(&report, 0, sizeof(report)); 738 + 739 + ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN, 740 + report.string, ARRAY_SIZE(report.string)); 741 + report.report = attr->report; 742 + report.length = ret * sizeof(report.string[0]) + 2; 743 + report.type = USB_DT_STRING; 744 + 745 + ret = cp2112_hid_output(hdev, &report.report, report.length + 1, 746 + HID_FEATURE_REPORT); 747 + if (ret != report.length + 1) { 748 + hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name, 749 + ret); 750 + if (ret < 0) 751 + return ret; 752 + return -EIO; 753 + } 754 + 755 + chmod_sysfs_attrs(hdev); 756 + return count; 757 + } 758 + 759 + static ssize_t pstr_show(struct device *kdev, 760 + struct device_attribute *kattr, char *buf) 761 + { 762 + struct hid_device *hdev = container_of(kdev, struct hid_device, dev); 763 + struct cp2112_pstring_attribute *attr = 764 + container_of(kattr, struct cp2112_pstring_attribute, attr); 765 + struct cp2112_string_report report; 766 + u8 length; 767 + int ret; 768 + 769 + ret = cp2112_hid_get(hdev, attr->report, &report.report, 770 + sizeof(report) - 1, HID_FEATURE_REPORT); 771 + if (ret < 3) { 772 + hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name, 773 + ret); 774 + if (ret < 0) 775 + return ret; 776 + return -EIO; 777 + } 778 + 779 + if (report.length < 2) { 780 + hid_err(hdev, "invalid %s string length: %d\n", 781 + kattr->attr.name, report.length); 782 + return -EIO; 783 + } 784 + 785 + length = report.length > ret - 1 ? ret - 1 : report.length; 786 + length = (length - 2) / sizeof(report.string[0]); 787 + ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf, 788 + PAGE_SIZE - 1); 789 + buf[ret++] = '\n'; 790 + return ret; 791 + } 792 + 793 + #define CP2112_PSTR_ATTR(name, _report) \ 794 + static struct cp2112_pstring_attribute dev_attr_##name = { \ 795 + .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \ 796 + .report = _report, \ 797 + }; 798 + 799 + CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING); 800 + CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING); 801 + CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING); 802 + 803 + #undef CP2112_PSTR_ATTR 804 + 805 + static const struct attribute_group cp2112_attr_group = { 806 + .attrs = (struct attribute *[]){ 807 + &dev_attr_vendor_id.attr, 808 + &dev_attr_product_id.attr, 809 + &dev_attr_max_power.attr, 810 + &dev_attr_power_mode.attr, 811 + &dev_attr_release_version.attr, 812 + &dev_attr_manufacturer.attr.attr, 813 + &dev_attr_product.attr.attr, 814 + &dev_attr_serial.attr.attr, 815 + NULL 816 + } 817 + }; 818 + 819 + /* Chmoding our sysfs attributes is simply a way to expose which fields in the 820 + * PROM have already been programmed. We do not depend on this preventing 821 + * writing to these attributes since the CP2112 will simply ignore writes to 822 + * already-programmed fields. This is why there is no sense in fixing this 823 + * racy behaviour. 824 + */ 825 + static void chmod_sysfs_attrs(struct hid_device *hdev) 826 + { 827 + struct attribute **attr; 828 + u8 buf[2]; 829 + int ret; 830 + 831 + ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf), 832 + HID_FEATURE_REPORT); 833 + if (ret != sizeof(buf)) { 834 + hid_err(hdev, "error reading lock byte: %d\n", ret); 835 + return; 836 + } 837 + 838 + for (attr = cp2112_attr_group.attrs; *attr; ++attr) { 839 + umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO; 840 + ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode); 841 + if (ret < 0) 842 + hid_err(hdev, "error chmoding sysfs file %s\n", 843 + (*attr)->name); 844 + buf[1] >>= 1; 845 + } 846 + } 847 + 848 + static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id) 849 + { 850 + struct cp2112_device *dev; 851 + u8 buf[3]; 852 + struct cp2112_smbus_config_report config; 853 + int ret; 854 + 855 + ret = hid_parse(hdev); 856 + if (ret) { 857 + hid_err(hdev, "parse failed\n"); 858 + return ret; 859 + } 860 + 861 + ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); 862 + if (ret) { 863 + hid_err(hdev, "hw start failed\n"); 864 + return ret; 865 + } 866 + 867 + ret = hid_hw_open(hdev); 868 + if (ret) { 869 + hid_err(hdev, "hw open failed\n"); 870 + goto err_hid_stop; 871 + } 872 + 873 + ret = hid_hw_power(hdev, PM_HINT_FULLON); 874 + if (ret < 0) { 875 + hid_err(hdev, "power management error: %d\n", ret); 876 + goto err_hid_close; 877 + } 878 + 879 + ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf), 880 + HID_FEATURE_REPORT); 881 + if (ret != sizeof(buf)) { 882 + hid_err(hdev, "error requesting version\n"); 883 + if (ret >= 0) 884 + ret = -EIO; 885 + goto err_power_normal; 886 + } 887 + 888 + hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n", 889 + buf[1], buf[2]); 890 + 891 + ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config, 892 + sizeof(config), HID_FEATURE_REPORT); 893 + if (ret != sizeof(config)) { 894 + hid_err(hdev, "error requesting SMBus config\n"); 895 + if (ret >= 0) 896 + ret = -EIO; 897 + goto err_power_normal; 898 + } 899 + 900 + config.retry_time = cpu_to_be16(1); 901 + 902 + ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config), 903 + HID_FEATURE_REPORT); 904 + if (ret != sizeof(config)) { 905 + hid_err(hdev, "error setting SMBus config\n"); 906 + if (ret >= 0) 907 + ret = -EIO; 908 + goto err_power_normal; 909 + } 910 + 911 + dev = kzalloc(sizeof(*dev), GFP_KERNEL); 912 + if (!dev) { 913 + ret = -ENOMEM; 914 + goto err_power_normal; 915 + } 916 + 917 + hid_set_drvdata(hdev, (void *)dev); 918 + dev->hdev = hdev; 919 + dev->adap.owner = THIS_MODULE; 920 + dev->adap.class = I2C_CLASS_HWMON; 921 + dev->adap.algo = &smbus_algorithm; 922 + dev->adap.algo_data = dev; 923 + dev->adap.dev.parent = &hdev->dev; 924 + snprintf(dev->adap.name, sizeof(dev->adap.name), 925 + "CP2112 SMBus Bridge on hiddev%d", hdev->minor); 926 + init_waitqueue_head(&dev->wait); 927 + 928 + hid_device_io_start(hdev); 929 + ret = i2c_add_adapter(&dev->adap); 930 + hid_device_io_stop(hdev); 931 + 932 + if (ret) { 933 + hid_err(hdev, "error registering i2c adapter\n"); 934 + goto err_free_dev; 935 + } 936 + 937 + hid_dbg(hdev, "adapter registered\n"); 938 + 939 + dev->gc.label = "cp2112_gpio"; 940 + dev->gc.direction_input = cp2112_gpio_direction_input; 941 + dev->gc.direction_output = cp2112_gpio_direction_output; 942 + dev->gc.set = cp2112_gpio_set; 943 + dev->gc.get = cp2112_gpio_get; 944 + dev->gc.base = -1; 945 + dev->gc.ngpio = 8; 946 + dev->gc.can_sleep = 1; 947 + dev->gc.dev = &hdev->dev; 948 + 949 + ret = gpiochip_add(&dev->gc); 950 + if (ret < 0) { 951 + hid_err(hdev, "error registering gpio chip\n"); 952 + goto err_free_i2c; 953 + } 954 + 955 + ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group); 956 + if (ret < 0) { 957 + hid_err(hdev, "error creating sysfs attrs\n"); 958 + goto err_gpiochip_remove; 959 + } 960 + 961 + chmod_sysfs_attrs(hdev); 962 + hid_hw_power(hdev, PM_HINT_NORMAL); 963 + 964 + return ret; 965 + 966 + err_gpiochip_remove: 967 + if (gpiochip_remove(&dev->gc) < 0) 968 + hid_err(hdev, "error removing gpio chip\n"); 969 + err_free_i2c: 970 + i2c_del_adapter(&dev->adap); 971 + err_free_dev: 972 + kfree(dev); 973 + err_power_normal: 974 + hid_hw_power(hdev, PM_HINT_NORMAL); 975 + err_hid_close: 976 + hid_hw_close(hdev); 977 + err_hid_stop: 978 + hid_hw_stop(hdev); 979 + return ret; 980 + } 981 + 982 + static void cp2112_remove(struct hid_device *hdev) 983 + { 984 + struct cp2112_device *dev = hid_get_drvdata(hdev); 985 + 986 + sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group); 987 + if (gpiochip_remove(&dev->gc)) 988 + hid_err(hdev, "unable to remove gpio chip\n"); 989 + i2c_del_adapter(&dev->adap); 990 + /* i2c_del_adapter has finished removing all i2c devices from our 991 + * adapter. Well behaved devices should no longer call our cp2112_xfer 992 + * and should have waited for any pending calls to finish. It has also 993 + * waited for device_unregister(&adap->dev) to complete. Therefore we 994 + * can safely free our struct cp2112_device. 995 + */ 996 + hid_hw_close(hdev); 997 + hid_hw_stop(hdev); 998 + kfree(dev); 999 + } 1000 + 1001 + static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report, 1002 + u8 *data, int size) 1003 + { 1004 + struct cp2112_device *dev = hid_get_drvdata(hdev); 1005 + struct cp2112_xfer_status_report *xfer = (void *)data; 1006 + 1007 + switch (data[0]) { 1008 + case CP2112_TRANSFER_STATUS_RESPONSE: 1009 + hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n", 1010 + xfer->status0, xfer->status1, 1011 + be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length)); 1012 + 1013 + switch (xfer->status0) { 1014 + case STATUS0_IDLE: 1015 + dev->xfer_status = -EAGAIN; 1016 + break; 1017 + case STATUS0_BUSY: 1018 + dev->xfer_status = -EBUSY; 1019 + break; 1020 + case STATUS0_COMPLETE: 1021 + dev->xfer_status = be16_to_cpu(xfer->length); 1022 + break; 1023 + case STATUS0_ERROR: 1024 + switch (xfer->status1) { 1025 + case STATUS1_TIMEOUT_NACK: 1026 + case STATUS1_TIMEOUT_BUS: 1027 + dev->xfer_status = -ETIMEDOUT; 1028 + break; 1029 + default: 1030 + dev->xfer_status = -EIO; 1031 + break; 1032 + } 1033 + break; 1034 + default: 1035 + dev->xfer_status = -EINVAL; 1036 + break; 1037 + } 1038 + 1039 + atomic_set(&dev->xfer_avail, 1); 1040 + break; 1041 + case CP2112_DATA_READ_RESPONSE: 1042 + hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]); 1043 + 1044 + dev->read_length = data[2]; 1045 + if (dev->read_length > sizeof(dev->read_data)) 1046 + dev->read_length = sizeof(dev->read_data); 1047 + 1048 + memcpy(dev->read_data, &data[3], dev->read_length); 1049 + atomic_set(&dev->read_avail, 1); 1050 + break; 1051 + default: 1052 + hid_err(hdev, "unknown report\n"); 1053 + 1054 + return 0; 1055 + } 1056 + 1057 + wake_up_interruptible(&dev->wait); 1058 + return 1; 1059 + } 1060 + 1061 + static struct hid_driver cp2112_driver = { 1062 + .name = "cp2112", 1063 + .id_table = cp2112_devices, 1064 + .probe = cp2112_probe, 1065 + .remove = cp2112_remove, 1066 + .raw_event = cp2112_raw_event, 1067 + }; 1068 + 1069 + module_hid_driver(cp2112_driver); 1070 + MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge"); 1071 + MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>"); 1072 + MODULE_LICENSE("GPL"); 1073 +
+10
drivers/hid/hid-hyperv.c
··· 460 460 { 461 461 } 462 462 463 + static int mousevsc_hid_raw_request(struct hid_device *hid, 464 + unsigned char report_num, 465 + __u8 *buf, size_t len, 466 + unsigned char rtype, 467 + int reqtype) 468 + { 469 + return 0; 470 + } 471 + 463 472 static struct hid_ll_driver mousevsc_ll_driver = { 464 473 .parse = mousevsc_hid_parse, 465 474 .open = mousevsc_hid_open, 466 475 .close = mousevsc_hid_close, 467 476 .start = mousevsc_hid_start, 468 477 .stop = mousevsc_hid_stop, 478 + .raw_request = mousevsc_hid_raw_request, 469 479 }; 470 480 471 481 static struct hid_driver mousevsc_hid_driver;
+1
drivers/hid/hid-ids.h
··· 242 242 #define USB_VENDOR_ID_CYGNAL 0x10c4 243 243 #define USB_DEVICE_ID_CYGNAL_RADIO_SI470X 0x818a 244 244 #define USB_DEVICE_ID_FOCALTECH_FTXXXX_MULTITOUCH 0x81b9 245 + #define USB_DEVICE_ID_CYGNAL_CP2112 0xea90 245 246 246 247 #define USB_VENDOR_ID_CYPRESS 0x04b4 247 248 #define USB_DEVICE_ID_CYPRESS_MOUSE 0x0001
+9 -9
drivers/hid/hid-input.c
··· 350 350 ret = -ENOMEM; 351 351 break; 352 352 } 353 - ret = dev->hid_get_raw_report(dev, dev->battery_report_id, 354 - buf, 2, 355 - dev->battery_report_type); 353 + ret = hid_hw_raw_request(dev, dev->battery_report_id, buf, 2, 354 + dev->battery_report_type, 355 + HID_REQ_GET_REPORT); 356 356 357 357 if (ret != 2) { 358 358 ret = -ENODATA; ··· 1150 1150 led_work); 1151 1151 struct hid_field *field; 1152 1152 struct hid_report *report; 1153 - int len; 1153 + int len, ret; 1154 1154 __u8 *buf; 1155 1155 1156 1156 field = hidinput_get_led_field(hid); ··· 1184 1184 1185 1185 hid_output_report(report, buf); 1186 1186 /* synchronous output report */ 1187 - hid->hid_output_raw_report(hid, buf, len, HID_OUTPUT_REPORT); 1187 + ret = hid_hw_output_report(hid, buf, len); 1188 + if (ret == -ENOSYS) 1189 + hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT, 1190 + HID_REQ_SET_REPORT); 1188 1191 kfree(buf); 1189 1192 } 1190 1193 ··· 1266 1263 } 1267 1264 1268 1265 input_set_drvdata(input_dev, hid); 1269 - if (hid->ll_driver->hidinput_input_event) 1270 - input_dev->event = hid->ll_driver->hidinput_input_event; 1271 - else if (hid->ll_driver->request || hid->hid_output_raw_report) 1272 - input_dev->event = hidinput_input_event; 1266 + input_dev->event = hidinput_input_event; 1273 1267 input_dev->open = hidinput_open; 1274 1268 input_dev->close = hidinput_close; 1275 1269 input_dev->setkeycode = hidinput_setkeycode;
+4 -2
drivers/hid/hid-lg.c
··· 692 692 if (hdev->product == USB_DEVICE_ID_LOGITECH_WII_WHEEL) { 693 693 unsigned char buf[] = { 0x00, 0xAF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 694 694 695 - ret = hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT); 695 + ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf), 696 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 696 697 697 698 if (ret >= 0) { 698 699 /* insert a little delay of 10 jiffies ~ 40ms */ ··· 705 704 buf[1] = 0xB2; 706 705 get_random_bytes(&buf[2], 2); 707 706 708 - ret = hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT); 707 + ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf), 708 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 709 709 } 710 710 } 711 711
+40 -71
drivers/hid/hid-logitech-dj.c
··· 44 44 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */ 45 45 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */ 46 46 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 47 - 0x95, 0x05, /* REPORT COUNT (5) */ 48 - 0x05, 0x08, /* USAGE PAGE (LED page) */ 49 - 0x19, 0x01, /* USAGE MINIMUM (1) */ 50 - 0x29, 0x05, /* USAGE MAXIMUM (5) */ 51 - 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */ 52 - 0x95, 0x01, /* REPORT COUNT (1) */ 53 - 0x75, 0x03, /* REPORT SIZE (3) */ 54 - 0x91, 0x01, /* OUTPUT (Constant) */ 55 47 0x95, 0x06, /* REPORT_COUNT (6) */ 56 48 0x75, 0x08, /* REPORT_SIZE (8) */ 57 49 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ ··· 52 60 0x19, 0x00, /* USAGE_MINIMUM (no event) */ 53 61 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */ 54 62 0x81, 0x00, /* INPUT (Data,Ary,Abs) */ 63 + 0x85, 0x0e, /* REPORT_ID (14) */ 64 + 0x05, 0x08, /* USAGE PAGE (LED page) */ 65 + 0x95, 0x05, /* REPORT COUNT (5) */ 66 + 0x75, 0x01, /* REPORT SIZE (1) */ 67 + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 68 + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 69 + 0x19, 0x01, /* USAGE MINIMUM (1) */ 70 + 0x29, 0x05, /* USAGE MAXIMUM (5) */ 71 + 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */ 72 + 0x95, 0x01, /* REPORT COUNT (1) */ 73 + 0x75, 0x03, /* REPORT SIZE (3) */ 74 + 0x91, 0x01, /* OUTPUT (Constant) */ 55 75 0xC0 56 76 }; 57 77 ··· 193 189 194 190 static struct hid_ll_driver logi_dj_ll_driver; 195 191 196 - static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf, 197 - size_t count, 198 - unsigned char report_type); 199 192 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev); 200 193 201 194 static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev, ··· 259 258 } 260 259 261 260 dj_hiddev->ll_driver = &logi_dj_ll_driver; 262 - dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report; 263 261 264 262 dj_hiddev->dev.parent = &djrcv_hdev->dev; 265 263 dj_hiddev->bus = BUS_USB; ··· 540 540 dbg_hid("%s:%s\n", __func__, hid->phys); 541 541 } 542 542 543 - static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf, 544 - size_t count, 545 - unsigned char report_type) 543 + static int logi_dj_ll_raw_request(struct hid_device *hid, 544 + unsigned char reportnum, __u8 *buf, 545 + size_t count, unsigned char report_type, 546 + int reqtype) 546 547 { 547 - /* Called by hid raw to send data */ 548 - dbg_hid("%s\n", __func__); 548 + struct dj_device *djdev = hid->driver_data; 549 + struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev; 550 + u8 *out_buf; 551 + int ret; 549 552 550 - return 0; 553 + if (buf[0] != REPORT_TYPE_LEDS) 554 + return -EINVAL; 555 + 556 + out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC); 557 + if (!out_buf) 558 + return -ENOMEM; 559 + 560 + if (count < DJREPORT_SHORT_LENGTH - 2) 561 + count = DJREPORT_SHORT_LENGTH - 2; 562 + 563 + out_buf[0] = REPORT_ID_DJ_SHORT; 564 + out_buf[1] = djdev->device_index; 565 + memcpy(out_buf + 2, buf, count); 566 + 567 + ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf, 568 + DJREPORT_SHORT_LENGTH, report_type, reqtype); 569 + 570 + kfree(out_buf); 571 + return ret; 551 572 } 552 573 553 574 static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size) ··· 634 613 return retval; 635 614 } 636 615 637 - static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type, 638 - unsigned int code, int value) 639 - { 640 - /* Sent by the input layer to handle leds and Force Feedback */ 641 - struct hid_device *dj_hiddev = input_get_drvdata(dev); 642 - struct dj_device *dj_dev = dj_hiddev->driver_data; 643 - 644 - struct dj_receiver_dev *djrcv_dev = 645 - dev_get_drvdata(dj_hiddev->dev.parent); 646 - struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev; 647 - struct hid_report_enum *output_report_enum; 648 - 649 - struct hid_field *field; 650 - struct hid_report *report; 651 - unsigned char *data; 652 - int offset; 653 - 654 - dbg_hid("%s: %s, type:%d | code:%d | value:%d\n", 655 - __func__, dev->phys, type, code, value); 656 - 657 - if (type != EV_LED) 658 - return -1; 659 - 660 - offset = hidinput_find_field(dj_hiddev, type, code, &field); 661 - 662 - if (offset == -1) { 663 - dev_warn(&dev->dev, "event field not found\n"); 664 - return -1; 665 - } 666 - hid_set_field(field, offset, value); 667 - 668 - data = hid_alloc_report_buf(field->report, GFP_ATOMIC); 669 - if (!data) { 670 - dev_warn(&dev->dev, "failed to allocate report buf memory\n"); 671 - return -1; 672 - } 673 - 674 - hid_output_report(field->report, &data[0]); 675 - 676 - output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT]; 677 - report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT]; 678 - hid_set_field(report->field[0], 0, dj_dev->device_index); 679 - hid_set_field(report->field[0], 1, REPORT_TYPE_LEDS); 680 - hid_set_field(report->field[0], 2, data[1]); 681 - 682 - hid_hw_request(dj_rcv_hiddev, report, HID_REQ_SET_REPORT); 683 - 684 - kfree(data); 685 - 686 - return 0; 687 - } 688 - 689 616 static int logi_dj_ll_start(struct hid_device *hid) 690 617 { 691 618 dbg_hid("%s\n", __func__); ··· 652 683 .stop = logi_dj_ll_stop, 653 684 .open = logi_dj_ll_open, 654 685 .close = logi_dj_ll_close, 655 - .hidinput_input_event = logi_dj_ll_input_event, 686 + .raw_request = logi_dj_ll_raw_request, 656 687 }; 657 688 658 689
+2 -2
drivers/hid/hid-magicmouse.c
··· 538 538 * but there seems to be no other way of switching the mode. 539 539 * Thus the super-ugly hacky success check below. 540 540 */ 541 - ret = hdev->hid_output_raw_report(hdev, feature, sizeof(feature), 542 - HID_FEATURE_REPORT); 541 + ret = hid_hw_raw_request(hdev, feature[0], feature, sizeof(feature), 542 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 543 543 if (ret != -EIO && ret != sizeof(feature)) { 544 544 hid_err(hdev, "unable to request touch data (%d)\n", ret); 545 545 goto err_stop_hw;
+704 -96
drivers/hid/hid-sony.c
··· 17 17 * any later version. 18 18 */ 19 19 20 - /* NOTE: in order for the Sony PS3 BD Remote Control to be found by 20 + /* 21 + * NOTE: in order for the Sony PS3 BD Remote Control to be found by 21 22 * a Bluetooth host, the key combination Start+Enter has to be kept pressed 22 23 * for about 7 seconds with the Bluetooth Host Controller in discovering mode. 23 24 * ··· 29 28 #include <linux/hid.h> 30 29 #include <linux/module.h> 31 30 #include <linux/slab.h> 32 - #include <linux/usb.h> 33 31 #include <linux/leds.h> 32 + #include <linux/power_supply.h> 33 + #include <linux/spinlock.h> 34 + #include <linux/list.h> 35 + #include <linux/input/mt.h> 34 36 35 37 #include "hid-ids.h" 36 38 ··· 45 41 #define DUALSHOCK4_CONTROLLER_USB BIT(5) 46 42 #define DUALSHOCK4_CONTROLLER_BT BIT(6) 47 43 48 - #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB) 49 - #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER_USB | DUALSHOCK4_CONTROLLER_USB) 44 + #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT) 45 + #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\ 46 + DUALSHOCK4_CONTROLLER_BT) 47 + #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\ 48 + DUALSHOCK4_CONTROLLER) 49 + #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER) 50 + #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER) 50 51 51 52 #define MAX_LEDS 4 52 53 ··· 83 74 0xb1, 0x02, 0xc0, 0xc0, 84 75 }; 85 76 86 - /* The default descriptor doesn't provide mapping for the accelerometers 77 + /* 78 + * The default descriptor doesn't provide mapping for the accelerometers 87 79 * or orientation sensors. This fixed descriptor maps the accelerometers 88 80 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors 89 81 * to usage values 0x43, 0x44 and 0x45. ··· 343 333 0xC0 /* End Collection */ 344 334 }; 345 335 336 + /* 337 + * The default behavior of the Dualshock 4 is to send reports using report 338 + * type 1 when running over Bluetooth. However, as soon as it receives a 339 + * report of type 17 to set the LEDs or rumble it starts returning it's state 340 + * in report 17 instead of 1. Since report 17 is undefined in the default HID 341 + * descriptor the button and axis definitions must be moved to report 17 or 342 + * the HID layer won't process the received input once a report is sent. 343 + */ 344 + static u8 dualshock4_bt_rdesc[] = { 345 + 0x05, 0x01, /* Usage Page (Desktop), */ 346 + 0x09, 0x05, /* Usage (Gamepad), */ 347 + 0xA1, 0x01, /* Collection (Application), */ 348 + 0x85, 0x01, /* Report ID (1), */ 349 + 0x75, 0x08, /* Report Size (8), */ 350 + 0x95, 0x0A, /* Report Count (9), */ 351 + 0x81, 0x02, /* Input (Variable), */ 352 + 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */ 353 + 0x85, 0x02, /* Report ID (2), */ 354 + 0x09, 0x24, /* Usage (24h), */ 355 + 0x95, 0x24, /* Report Count (36), */ 356 + 0xB1, 0x02, /* Feature (Variable), */ 357 + 0x85, 0xA3, /* Report ID (163), */ 358 + 0x09, 0x25, /* Usage (25h), */ 359 + 0x95, 0x30, /* Report Count (48), */ 360 + 0xB1, 0x02, /* Feature (Variable), */ 361 + 0x85, 0x05, /* Report ID (5), */ 362 + 0x09, 0x26, /* Usage (26h), */ 363 + 0x95, 0x28, /* Report Count (40), */ 364 + 0xB1, 0x02, /* Feature (Variable), */ 365 + 0x85, 0x06, /* Report ID (6), */ 366 + 0x09, 0x27, /* Usage (27h), */ 367 + 0x95, 0x34, /* Report Count (52), */ 368 + 0xB1, 0x02, /* Feature (Variable), */ 369 + 0x85, 0x07, /* Report ID (7), */ 370 + 0x09, 0x28, /* Usage (28h), */ 371 + 0x95, 0x30, /* Report Count (48), */ 372 + 0xB1, 0x02, /* Feature (Variable), */ 373 + 0x85, 0x08, /* Report ID (8), */ 374 + 0x09, 0x29, /* Usage (29h), */ 375 + 0x95, 0x2F, /* Report Count (47), */ 376 + 0xB1, 0x02, /* Feature (Variable), */ 377 + 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */ 378 + 0x85, 0x03, /* Report ID (3), */ 379 + 0x09, 0x21, /* Usage (21h), */ 380 + 0x95, 0x26, /* Report Count (38), */ 381 + 0xB1, 0x02, /* Feature (Variable), */ 382 + 0x85, 0x04, /* Report ID (4), */ 383 + 0x09, 0x22, /* Usage (22h), */ 384 + 0x95, 0x2E, /* Report Count (46), */ 385 + 0xB1, 0x02, /* Feature (Variable), */ 386 + 0x85, 0xF0, /* Report ID (240), */ 387 + 0x09, 0x47, /* Usage (47h), */ 388 + 0x95, 0x3F, /* Report Count (63), */ 389 + 0xB1, 0x02, /* Feature (Variable), */ 390 + 0x85, 0xF1, /* Report ID (241), */ 391 + 0x09, 0x48, /* Usage (48h), */ 392 + 0x95, 0x3F, /* Report Count (63), */ 393 + 0xB1, 0x02, /* Feature (Variable), */ 394 + 0x85, 0xF2, /* Report ID (242), */ 395 + 0x09, 0x49, /* Usage (49h), */ 396 + 0x95, 0x0F, /* Report Count (15), */ 397 + 0xB1, 0x02, /* Feature (Variable), */ 398 + 0x85, 0x11, /* Report ID (17), */ 399 + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 400 + 0x09, 0x20, /* Usage (20h), */ 401 + 0x95, 0x02, /* Report Count (2), */ 402 + 0x81, 0x02, /* Input (Variable), */ 403 + 0x05, 0x01, /* Usage Page (Desktop), */ 404 + 0x09, 0x30, /* Usage (X), */ 405 + 0x09, 0x31, /* Usage (Y), */ 406 + 0x09, 0x32, /* Usage (Z), */ 407 + 0x09, 0x35, /* Usage (Rz), */ 408 + 0x15, 0x00, /* Logical Minimum (0), */ 409 + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 410 + 0x75, 0x08, /* Report Size (8), */ 411 + 0x95, 0x04, /* Report Count (4), */ 412 + 0x81, 0x02, /* Input (Variable), */ 413 + 0x09, 0x39, /* Usage (Hat Switch), */ 414 + 0x15, 0x00, /* Logical Minimum (0), */ 415 + 0x25, 0x07, /* Logical Maximum (7), */ 416 + 0x75, 0x04, /* Report Size (4), */ 417 + 0x95, 0x01, /* Report Count (1), */ 418 + 0x81, 0x42, /* Input (Variable, Null State), */ 419 + 0x05, 0x09, /* Usage Page (Button), */ 420 + 0x19, 0x01, /* Usage Minimum (01h), */ 421 + 0x29, 0x0E, /* Usage Maximum (0Eh), */ 422 + 0x15, 0x00, /* Logical Minimum (0), */ 423 + 0x25, 0x01, /* Logical Maximum (1), */ 424 + 0x75, 0x01, /* Report Size (1), */ 425 + 0x95, 0x0E, /* Report Count (14), */ 426 + 0x81, 0x02, /* Input (Variable), */ 427 + 0x75, 0x06, /* Report Size (6), */ 428 + 0x95, 0x01, /* Report Count (1), */ 429 + 0x81, 0x01, /* Input (Constant), */ 430 + 0x05, 0x01, /* Usage Page (Desktop), */ 431 + 0x09, 0x33, /* Usage (Rx), */ 432 + 0x09, 0x34, /* Usage (Ry), */ 433 + 0x15, 0x00, /* Logical Minimum (0), */ 434 + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 435 + 0x75, 0x08, /* Report Size (8), */ 436 + 0x95, 0x02, /* Report Count (2), */ 437 + 0x81, 0x02, /* Input (Variable), */ 438 + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 439 + 0x09, 0x20, /* Usage (20h), */ 440 + 0x95, 0x03, /* Report Count (3), */ 441 + 0x81, 0x02, /* Input (Variable), */ 442 + 0x05, 0x01, /* Usage Page (Desktop), */ 443 + 0x19, 0x40, /* Usage Minimum (40h), */ 444 + 0x29, 0x42, /* Usage Maximum (42h), */ 445 + 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */ 446 + 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */ 447 + 0x75, 0x10, /* Report Size (16), */ 448 + 0x95, 0x03, /* Report Count (3), */ 449 + 0x81, 0x02, /* Input (Variable), */ 450 + 0x19, 0x43, /* Usage Minimum (43h), */ 451 + 0x29, 0x45, /* Usage Maximum (45h), */ 452 + 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */ 453 + 0x26, 0x00, 0x40, /* Logical Maximum (16384), */ 454 + 0x95, 0x03, /* Report Count (3), */ 455 + 0x81, 0x02, /* Input (Variable), */ 456 + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 457 + 0x09, 0x20, /* Usage (20h), */ 458 + 0x15, 0x00, /* Logical Minimum (0), */ 459 + 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 460 + 0x75, 0x08, /* Report Size (8), */ 461 + 0x95, 0x31, /* Report Count (51), */ 462 + 0x81, 0x02, /* Input (Variable), */ 463 + 0x09, 0x21, /* Usage (21h), */ 464 + 0x75, 0x08, /* Report Size (8), */ 465 + 0x95, 0x4D, /* Report Count (77), */ 466 + 0x91, 0x02, /* Output (Variable), */ 467 + 0x85, 0x12, /* Report ID (18), */ 468 + 0x09, 0x22, /* Usage (22h), */ 469 + 0x95, 0x8D, /* Report Count (141), */ 470 + 0x81, 0x02, /* Input (Variable), */ 471 + 0x09, 0x23, /* Usage (23h), */ 472 + 0x91, 0x02, /* Output (Variable), */ 473 + 0x85, 0x13, /* Report ID (19), */ 474 + 0x09, 0x24, /* Usage (24h), */ 475 + 0x95, 0xCD, /* Report Count (205), */ 476 + 0x81, 0x02, /* Input (Variable), */ 477 + 0x09, 0x25, /* Usage (25h), */ 478 + 0x91, 0x02, /* Output (Variable), */ 479 + 0x85, 0x14, /* Report ID (20), */ 480 + 0x09, 0x26, /* Usage (26h), */ 481 + 0x96, 0x0D, 0x01, /* Report Count (269), */ 482 + 0x81, 0x02, /* Input (Variable), */ 483 + 0x09, 0x27, /* Usage (27h), */ 484 + 0x91, 0x02, /* Output (Variable), */ 485 + 0x85, 0x15, /* Report ID (21), */ 486 + 0x09, 0x28, /* Usage (28h), */ 487 + 0x96, 0x4D, 0x01, /* Report Count (333), */ 488 + 0x81, 0x02, /* Input (Variable), */ 489 + 0x09, 0x29, /* Usage (29h), */ 490 + 0x91, 0x02, /* Output (Variable), */ 491 + 0x85, 0x16, /* Report ID (22), */ 492 + 0x09, 0x2A, /* Usage (2Ah), */ 493 + 0x96, 0x8D, 0x01, /* Report Count (397), */ 494 + 0x81, 0x02, /* Input (Variable), */ 495 + 0x09, 0x2B, /* Usage (2Bh), */ 496 + 0x91, 0x02, /* Output (Variable), */ 497 + 0x85, 0x17, /* Report ID (23), */ 498 + 0x09, 0x2C, /* Usage (2Ch), */ 499 + 0x96, 0xCD, 0x01, /* Report Count (461), */ 500 + 0x81, 0x02, /* Input (Variable), */ 501 + 0x09, 0x2D, /* Usage (2Dh), */ 502 + 0x91, 0x02, /* Output (Variable), */ 503 + 0x85, 0x18, /* Report ID (24), */ 504 + 0x09, 0x2E, /* Usage (2Eh), */ 505 + 0x96, 0x0D, 0x02, /* Report Count (525), */ 506 + 0x81, 0x02, /* Input (Variable), */ 507 + 0x09, 0x2F, /* Usage (2Fh), */ 508 + 0x91, 0x02, /* Output (Variable), */ 509 + 0x85, 0x19, /* Report ID (25), */ 510 + 0x09, 0x30, /* Usage (30h), */ 511 + 0x96, 0x22, 0x02, /* Report Count (546), */ 512 + 0x81, 0x02, /* Input (Variable), */ 513 + 0x09, 0x31, /* Usage (31h), */ 514 + 0x91, 0x02, /* Output (Variable), */ 515 + 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */ 516 + 0x85, 0x82, /* Report ID (130), */ 517 + 0x09, 0x22, /* Usage (22h), */ 518 + 0x95, 0x3F, /* Report Count (63), */ 519 + 0xB1, 0x02, /* Feature (Variable), */ 520 + 0x85, 0x83, /* Report ID (131), */ 521 + 0x09, 0x23, /* Usage (23h), */ 522 + 0xB1, 0x02, /* Feature (Variable), */ 523 + 0x85, 0x84, /* Report ID (132), */ 524 + 0x09, 0x24, /* Usage (24h), */ 525 + 0xB1, 0x02, /* Feature (Variable), */ 526 + 0x85, 0x90, /* Report ID (144), */ 527 + 0x09, 0x30, /* Usage (30h), */ 528 + 0xB1, 0x02, /* Feature (Variable), */ 529 + 0x85, 0x91, /* Report ID (145), */ 530 + 0x09, 0x31, /* Usage (31h), */ 531 + 0xB1, 0x02, /* Feature (Variable), */ 532 + 0x85, 0x92, /* Report ID (146), */ 533 + 0x09, 0x32, /* Usage (32h), */ 534 + 0xB1, 0x02, /* Feature (Variable), */ 535 + 0x85, 0x93, /* Report ID (147), */ 536 + 0x09, 0x33, /* Usage (33h), */ 537 + 0xB1, 0x02, /* Feature (Variable), */ 538 + 0x85, 0xA0, /* Report ID (160), */ 539 + 0x09, 0x40, /* Usage (40h), */ 540 + 0xB1, 0x02, /* Feature (Variable), */ 541 + 0x85, 0xA4, /* Report ID (164), */ 542 + 0x09, 0x44, /* Usage (44h), */ 543 + 0xB1, 0x02, /* Feature (Variable), */ 544 + 0xC0 /* End Collection */ 545 + }; 546 + 346 547 static __u8 ps3remote_rdesc[] = { 347 548 0x05, 0x01, /* GUsagePage Generic Desktop */ 348 549 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ ··· 671 450 }; 672 451 673 452 static const unsigned int buzz_keymap[] = { 674 - /* The controller has 4 remote buzzers, each with one LED and 5 453 + /* 454 + * The controller has 4 remote buzzers, each with one LED and 5 675 455 * buttons. 676 456 * 677 457 * We use the mapping chosen by the controller, which is: ··· 710 488 [20] = BTN_TRIGGER_HAPPY20, 711 489 }; 712 490 491 + static enum power_supply_property sony_battery_props[] = { 492 + POWER_SUPPLY_PROP_PRESENT, 493 + POWER_SUPPLY_PROP_CAPACITY, 494 + POWER_SUPPLY_PROP_SCOPE, 495 + POWER_SUPPLY_PROP_STATUS, 496 + }; 497 + 498 + static spinlock_t sony_dev_list_lock; 499 + static LIST_HEAD(sony_device_list); 500 + 713 501 struct sony_sc { 502 + spinlock_t lock; 503 + struct list_head list_node; 714 504 struct hid_device *hdev; 715 505 struct led_classdev *leds[MAX_LEDS]; 716 - struct hid_report *output_report; 717 506 unsigned long quirks; 718 507 struct work_struct state_worker; 508 + struct power_supply battery; 719 509 720 510 #ifdef CONFIG_SONY_FF 721 511 __u8 left; 722 512 __u8 right; 723 513 #endif 724 514 515 + __u8 mac_address[6]; 725 516 __u8 worker_initialized; 517 + __u8 cable_state; 518 + __u8 battery_charging; 519 + __u8 battery_capacity; 726 520 __u8 led_state[MAX_LEDS]; 727 521 __u8 led_count; 728 522 }; ··· 816 578 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n"); 817 579 rdesc = dualshock4_usb_rdesc; 818 580 *rsize = sizeof(dualshock4_usb_rdesc); 581 + } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) { 582 + hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n"); 583 + rdesc = dualshock4_bt_rdesc; 584 + *rsize = sizeof(dualshock4_bt_rdesc); 819 585 } 820 586 821 587 /* The HID descriptor exposed over BT has a trailing zero byte */ ··· 843 601 return rdesc; 844 602 } 845 603 604 + static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size) 605 + { 606 + static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 }; 607 + unsigned long flags; 608 + __u8 cable_state, battery_capacity, battery_charging; 609 + 610 + /* 611 + * The sixaxis is charging if the battery value is 0xee 612 + * and it is fully charged if the value is 0xef. 613 + * It does not report the actual level while charging so it 614 + * is set to 100% while charging is in progress. 615 + */ 616 + if (rd[30] >= 0xee) { 617 + battery_capacity = 100; 618 + battery_charging = !(rd[30] & 0x01); 619 + } else { 620 + __u8 index = rd[30] <= 5 ? rd[30] : 5; 621 + battery_capacity = sixaxis_battery_capacity[index]; 622 + battery_charging = 0; 623 + } 624 + cable_state = !((rd[31] >> 4) & 0x01); 625 + 626 + spin_lock_irqsave(&sc->lock, flags); 627 + sc->cable_state = cable_state; 628 + sc->battery_capacity = battery_capacity; 629 + sc->battery_charging = battery_charging; 630 + spin_unlock_irqrestore(&sc->lock, flags); 631 + } 632 + 633 + static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size) 634 + { 635 + struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, 636 + struct hid_input, list); 637 + struct input_dev *input_dev = hidinput->input; 638 + unsigned long flags; 639 + int n, offset; 640 + __u8 cable_state, battery_capacity, battery_charging; 641 + 642 + /* 643 + * Battery and touchpad data starts at byte 30 in the USB report and 644 + * 32 in Bluetooth report. 645 + */ 646 + offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32; 647 + 648 + /* 649 + * The lower 4 bits of byte 30 contain the battery level 650 + * and the 5th bit contains the USB cable state. 651 + */ 652 + cable_state = (rd[offset] >> 4) & 0x01; 653 + battery_capacity = rd[offset] & 0x0F; 654 + 655 + /* 656 + * When a USB power source is connected the battery level ranges from 657 + * 0 to 10, and when running on battery power it ranges from 0 to 9. 658 + * A battery level above 10 when plugged in means charge completed. 659 + */ 660 + if (!cable_state || battery_capacity > 10) 661 + battery_charging = 0; 662 + else 663 + battery_charging = 1; 664 + 665 + if (!cable_state) 666 + battery_capacity++; 667 + if (battery_capacity > 10) 668 + battery_capacity = 10; 669 + 670 + battery_capacity *= 10; 671 + 672 + spin_lock_irqsave(&sc->lock, flags); 673 + sc->cable_state = cable_state; 674 + sc->battery_capacity = battery_capacity; 675 + sc->battery_charging = battery_charging; 676 + spin_unlock_irqrestore(&sc->lock, flags); 677 + 678 + offset += 5; 679 + 680 + /* 681 + * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB 682 + * and 37 on Bluetooth. 683 + * The first 7 bits of the first byte is a counter and bit 8 is a touch 684 + * indicator that is 0 when pressed and 1 when not pressed. 685 + * The next 3 bytes are two 12 bit touch coordinates, X and Y. 686 + * The data for the second touch is in the same format and immediatly 687 + * follows the data for the first. 688 + */ 689 + for (n = 0; n < 2; n++) { 690 + __u16 x, y; 691 + 692 + x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8); 693 + y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4); 694 + 695 + input_mt_slot(input_dev, n); 696 + input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 697 + !(rd[offset] >> 7)); 698 + input_report_abs(input_dev, ABS_MT_POSITION_X, x); 699 + input_report_abs(input_dev, ABS_MT_POSITION_Y, y); 700 + 701 + offset += 4; 702 + } 703 + } 704 + 846 705 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report, 847 706 __u8 *rd, int size) 848 707 { 849 708 struct sony_sc *sc = hid_get_drvdata(hdev); 850 709 851 - /* Sixaxis HID report has acclerometers/gyro with MSByte first, this 710 + /* 711 + * Sixaxis HID report has acclerometers/gyro with MSByte first, this 852 712 * has to be BYTE_SWAPPED before passing up to joystick interface 853 713 */ 854 - if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) && 855 - rd[0] == 0x01 && size == 49) { 714 + if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) { 856 715 swap(rd[41], rd[42]); 857 716 swap(rd[43], rd[44]); 858 717 swap(rd[45], rd[46]); 859 718 swap(rd[47], rd[48]); 719 + 720 + sixaxis_parse_report(sc, rd, size); 721 + } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 && 722 + size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) 723 + && rd[0] == 0x11 && size == 78)) { 724 + dualshock4_parse_report(sc, rd, size); 860 725 } 861 726 862 727 return 0; ··· 1006 657 } 1007 658 1008 659 /* 1009 - * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP 1010 - * like it should according to usbhid/hid-core.c::usbhid_output_raw_report() 1011 - * so we need to override that forcing HID Output Reports on the Control EP. 1012 - * 1013 - * There is also another issue about HID Output Reports via USB, the Sixaxis 1014 - * does not want the report_id as part of the data packet, so we have to 1015 - * discard buf[0] when sending the actual control message, even for numbered 1016 - * reports, humpf! 1017 - */ 1018 - static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf, 1019 - size_t count, unsigned char report_type) 1020 - { 1021 - struct usb_interface *intf = to_usb_interface(hid->dev.parent); 1022 - struct usb_device *dev = interface_to_usbdev(intf); 1023 - struct usb_host_interface *interface = intf->cur_altsetting; 1024 - int report_id = buf[0]; 1025 - int ret; 1026 - 1027 - if (report_type == HID_OUTPUT_REPORT) { 1028 - /* Don't send the Report ID */ 1029 - buf++; 1030 - count--; 1031 - } 1032 - 1033 - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1034 - HID_REQ_SET_REPORT, 1035 - USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1036 - ((report_type + 1) << 8) | report_id, 1037 - interface->desc.bInterfaceNumber, buf, count, 1038 - USB_CTRL_SET_TIMEOUT); 1039 - 1040 - /* Count also the Report ID, in case of an Output report. */ 1041 - if (ret > 0 && report_type == HID_OUTPUT_REPORT) 1042 - ret++; 1043 - 1044 - return ret; 1045 - } 1046 - 1047 - /* 1048 660 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller 1049 661 * to "operational". Without this, the ps3 controller will not report any 1050 662 * events. ··· 1018 708 if (!buf) 1019 709 return -ENOMEM; 1020 710 1021 - ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT); 711 + ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT, 712 + HID_REQ_GET_REPORT); 1022 713 1023 714 if (ret < 0) 1024 715 hid_err(hdev, "can't set operational mode\n"); ··· 1032 721 static int sixaxis_set_operational_bt(struct hid_device *hdev) 1033 722 { 1034 723 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 }; 1035 - return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT); 724 + return hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf), 725 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 726 + } 727 + 728 + /* 729 + * Requesting feature report 0x02 in Bluetooth mode changes the state of the 730 + * controller so that it sends full input reports of type 0x11. 731 + */ 732 + static int dualshock4_set_operational_bt(struct hid_device *hdev) 733 + { 734 + __u8 buf[37] = { 0 }; 735 + 736 + return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf), 737 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1036 738 } 1037 739 1038 740 static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds) ··· 1075 751 1076 752 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) { 1077 753 buzz_set_leds(hdev, leds); 1078 - } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) || 1079 - (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) { 754 + } else { 1080 755 for (n = 0; n < count; n++) 1081 756 drv_data->led_state[n] = leds[n]; 1082 757 schedule_work(&drv_data->state_worker); ··· 1115 792 struct sony_sc *drv_data; 1116 793 1117 794 int n; 1118 - int on = 0; 1119 795 1120 796 drv_data = hid_get_drvdata(hdev); 1121 797 if (!drv_data) { ··· 1123 801 } 1124 802 1125 803 for (n = 0; n < drv_data->led_count; n++) { 1126 - if (led == drv_data->leds[n]) { 1127 - on = !!(drv_data->led_state[n]); 1128 - break; 1129 - } 804 + if (led == drv_data->leds[n]) 805 + return drv_data->led_state[n]; 1130 806 } 1131 807 1132 - return on ? LED_FULL : LED_OFF; 808 + return LED_OFF; 1133 809 } 1134 810 1135 811 static void sony_leds_remove(struct hid_device *hdev) ··· 1177 857 /* Validate expected report characteristics. */ 1178 858 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7)) 1179 859 return -ENODEV; 1180 - } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) { 860 + } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER) { 1181 861 drv_data->led_count = 3; 1182 862 max_brightness = 255; 1183 863 use_colors = 1; ··· 1191 871 name_fmt = "%s::sony%d"; 1192 872 } 1193 873 1194 - /* Clear LEDs as we have no way of reading their initial state. This is 874 + /* 875 + * Clear LEDs as we have no way of reading their initial state. This is 1195 876 * only relevant if the driver is loaded after somebody actively set the 1196 - * LEDs to on */ 877 + * LEDs to on 878 + */ 1197 879 sony_set_leds(hdev, initial_values, drv_data->led_count); 1198 880 1199 881 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1; ··· 1265 943 buf[10] |= sc->led_state[2] << 3; 1266 944 buf[10] |= sc->led_state[3] << 4; 1267 945 1268 - sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf), 1269 - HID_OUTPUT_REPORT); 946 + hid_hw_raw_request(sc->hdev, 0x01, buf, sizeof(buf), HID_OUTPUT_REPORT, 947 + HID_REQ_SET_REPORT); 1270 948 } 1271 949 1272 950 static void dualshock4_state_worker(struct work_struct *work) 1273 951 { 1274 952 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker); 1275 953 struct hid_device *hdev = sc->hdev; 1276 - struct hid_report *report = sc->output_report; 1277 - __s32 *value = report->field[0]->value; 954 + int offset; 1278 955 1279 - value[0] = 0x03; 956 + __u8 buf[78] = { 0 }; 957 + 958 + if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { 959 + buf[0] = 0x05; 960 + buf[1] = 0x03; 961 + offset = 4; 962 + } else { 963 + buf[0] = 0x11; 964 + buf[1] = 0xB0; 965 + buf[3] = 0x0F; 966 + offset = 6; 967 + } 1280 968 1281 969 #ifdef CONFIG_SONY_FF 1282 - value[3] = sc->right; 1283 - value[4] = sc->left; 970 + buf[offset++] = sc->right; 971 + buf[offset++] = sc->left; 972 + #else 973 + offset += 2; 1284 974 #endif 1285 975 1286 - value[5] = sc->led_state[0]; 1287 - value[6] = sc->led_state[1]; 1288 - value[7] = sc->led_state[2]; 976 + buf[offset++] = sc->led_state[0]; 977 + buf[offset++] = sc->led_state[1]; 978 + buf[offset++] = sc->led_state[2]; 1289 979 1290 - hid_hw_request(hdev, report, HID_REQ_SET_REPORT); 980 + if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) 981 + hid_hw_output_report(hdev, buf, 32); 982 + else 983 + hid_hw_raw_request(hdev, 0x11, buf, 78, 984 + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); 1291 985 } 1292 986 1293 987 #ifdef CONFIG_SONY_FF ··· 1338 1000 { 1339 1001 return 0; 1340 1002 } 1003 + 1341 1004 #endif 1342 1005 1343 - static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size) 1006 + static int sony_battery_get_property(struct power_supply *psy, 1007 + enum power_supply_property psp, 1008 + union power_supply_propval *val) 1344 1009 { 1345 - struct list_head *head, *list; 1346 - struct hid_report *report; 1010 + struct sony_sc *sc = container_of(psy, struct sony_sc, battery); 1011 + unsigned long flags; 1012 + int ret = 0; 1013 + u8 battery_charging, battery_capacity, cable_state; 1014 + 1015 + spin_lock_irqsave(&sc->lock, flags); 1016 + battery_charging = sc->battery_charging; 1017 + battery_capacity = sc->battery_capacity; 1018 + cable_state = sc->cable_state; 1019 + spin_unlock_irqrestore(&sc->lock, flags); 1020 + 1021 + switch (psp) { 1022 + case POWER_SUPPLY_PROP_PRESENT: 1023 + val->intval = 1; 1024 + break; 1025 + case POWER_SUPPLY_PROP_SCOPE: 1026 + val->intval = POWER_SUPPLY_SCOPE_DEVICE; 1027 + break; 1028 + case POWER_SUPPLY_PROP_CAPACITY: 1029 + val->intval = battery_capacity; 1030 + break; 1031 + case POWER_SUPPLY_PROP_STATUS: 1032 + if (battery_charging) 1033 + val->intval = POWER_SUPPLY_STATUS_CHARGING; 1034 + else 1035 + if (battery_capacity == 100 && cable_state) 1036 + val->intval = POWER_SUPPLY_STATUS_FULL; 1037 + else 1038 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 1039 + break; 1040 + default: 1041 + ret = -EINVAL; 1042 + break; 1043 + } 1044 + return ret; 1045 + } 1046 + 1047 + static int sony_battery_probe(struct sony_sc *sc) 1048 + { 1049 + static atomic_t power_id_seq = ATOMIC_INIT(0); 1050 + unsigned long power_id; 1347 1051 struct hid_device *hdev = sc->hdev; 1052 + int ret; 1348 1053 1349 - list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list; 1054 + /* 1055 + * Set the default battery level to 100% to avoid low battery warnings 1056 + * if the battery is polled before the first device report is received. 1057 + */ 1058 + sc->battery_capacity = 100; 1350 1059 1351 - list_for_each(head, list) { 1352 - report = list_entry(head, struct hid_report, list); 1060 + power_id = (unsigned long)atomic_inc_return(&power_id_seq); 1353 1061 1354 - if (report->id == req_id) { 1355 - if (report->size < req_size) { 1356 - hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n", 1357 - req_id, report->size, req_size); 1358 - return -EINVAL; 1359 - } 1360 - sc->output_report = report; 1361 - return 0; 1062 + sc->battery.properties = sony_battery_props; 1063 + sc->battery.num_properties = ARRAY_SIZE(sony_battery_props); 1064 + sc->battery.get_property = sony_battery_get_property; 1065 + sc->battery.type = POWER_SUPPLY_TYPE_BATTERY; 1066 + sc->battery.use_for_apm = 0; 1067 + sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu", 1068 + power_id); 1069 + if (!sc->battery.name) 1070 + return -ENOMEM; 1071 + 1072 + ret = power_supply_register(&hdev->dev, &sc->battery); 1073 + if (ret) { 1074 + hid_err(hdev, "Unable to register battery device\n"); 1075 + goto err_free; 1076 + } 1077 + 1078 + power_supply_powers(&sc->battery, &hdev->dev); 1079 + return 0; 1080 + 1081 + err_free: 1082 + kfree(sc->battery.name); 1083 + sc->battery.name = NULL; 1084 + return ret; 1085 + } 1086 + 1087 + static void sony_battery_remove(struct sony_sc *sc) 1088 + { 1089 + if (!sc->battery.name) 1090 + return; 1091 + 1092 + power_supply_unregister(&sc->battery); 1093 + kfree(sc->battery.name); 1094 + sc->battery.name = NULL; 1095 + } 1096 + 1097 + static int sony_register_touchpad(struct sony_sc *sc, int touch_count, 1098 + int w, int h) 1099 + { 1100 + struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, 1101 + struct hid_input, list); 1102 + struct input_dev *input_dev = hidinput->input; 1103 + int ret; 1104 + 1105 + ret = input_mt_init_slots(input_dev, touch_count, 0); 1106 + if (ret < 0) { 1107 + hid_err(sc->hdev, "Unable to initialize multi-touch slots\n"); 1108 + return ret; 1109 + } 1110 + 1111 + input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0); 1112 + input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0); 1113 + 1114 + return 0; 1115 + } 1116 + 1117 + /* 1118 + * If a controller is plugged in via USB while already connected via Bluetooth 1119 + * it will show up as two devices. A global list of connected controllers and 1120 + * their MAC addresses is maintained to ensure that a device is only connected 1121 + * once. 1122 + */ 1123 + static int sony_check_add_dev_list(struct sony_sc *sc) 1124 + { 1125 + struct sony_sc *entry; 1126 + unsigned long flags; 1127 + int ret; 1128 + 1129 + spin_lock_irqsave(&sony_dev_list_lock, flags); 1130 + 1131 + list_for_each_entry(entry, &sony_device_list, list_node) { 1132 + ret = memcmp(sc->mac_address, entry->mac_address, 1133 + sizeof(sc->mac_address)); 1134 + if (!ret) { 1135 + ret = -EEXIST; 1136 + hid_info(sc->hdev, "controller with MAC address %pMR already connected\n", 1137 + sc->mac_address); 1138 + goto unlock; 1362 1139 } 1363 1140 } 1364 1141 1365 - hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id); 1142 + ret = 0; 1143 + list_add(&(sc->list_node), &sony_device_list); 1366 1144 1367 - return -EINVAL; 1145 + unlock: 1146 + spin_unlock_irqrestore(&sony_dev_list_lock, flags); 1147 + return ret; 1368 1148 } 1149 + 1150 + static void sony_remove_dev_list(struct sony_sc *sc) 1151 + { 1152 + unsigned long flags; 1153 + 1154 + if (sc->list_node.next) { 1155 + spin_lock_irqsave(&sony_dev_list_lock, flags); 1156 + list_del(&(sc->list_node)); 1157 + spin_unlock_irqrestore(&sony_dev_list_lock, flags); 1158 + } 1159 + } 1160 + 1161 + static int sony_get_bt_devaddr(struct sony_sc *sc) 1162 + { 1163 + int ret; 1164 + 1165 + /* HIDP stores the device MAC address as a string in the uniq field. */ 1166 + ret = strlen(sc->hdev->uniq); 1167 + if (ret != 17) 1168 + return -EINVAL; 1169 + 1170 + ret = sscanf(sc->hdev->uniq, 1171 + "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", 1172 + &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3], 1173 + &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]); 1174 + 1175 + if (ret != 6) 1176 + return -EINVAL; 1177 + 1178 + return 0; 1179 + } 1180 + 1181 + static int sony_check_add(struct sony_sc *sc) 1182 + { 1183 + int n, ret; 1184 + 1185 + if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) || 1186 + (sc->quirks & SIXAXIS_CONTROLLER_BT)) { 1187 + /* 1188 + * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC 1189 + * address from the uniq string where HIDP stores it. 1190 + * As uniq cannot be guaranteed to be a MAC address in all cases 1191 + * a failure of this function should not prevent the connection. 1192 + */ 1193 + if (sony_get_bt_devaddr(sc) < 0) { 1194 + hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n"); 1195 + return 0; 1196 + } 1197 + } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { 1198 + __u8 buf[7]; 1199 + 1200 + /* 1201 + * The MAC address of a DS4 controller connected via USB can be 1202 + * retrieved with feature report 0x81. The address begins at 1203 + * offset 1. 1204 + */ 1205 + ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf), 1206 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1207 + 1208 + if (ret != 7) { 1209 + hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n"); 1210 + return ret < 0 ? ret : -EINVAL; 1211 + } 1212 + 1213 + memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address)); 1214 + } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) { 1215 + __u8 buf[18]; 1216 + 1217 + /* 1218 + * The MAC address of a Sixaxis controller connected via USB can 1219 + * be retrieved with feature report 0xf2. The address begins at 1220 + * offset 4. 1221 + */ 1222 + ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf), 1223 + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 1224 + 1225 + if (ret != 18) { 1226 + hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n"); 1227 + return ret < 0 ? ret : -EINVAL; 1228 + } 1229 + 1230 + /* 1231 + * The Sixaxis device MAC in the report is big-endian and must 1232 + * be byte-swapped. 1233 + */ 1234 + for (n = 0; n < 6; n++) 1235 + sc->mac_address[5-n] = buf[4+n]; 1236 + } else { 1237 + return 0; 1238 + } 1239 + 1240 + return sony_check_add_dev_list(sc); 1241 + } 1242 + 1369 1243 1370 1244 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) 1371 1245 { ··· 1616 1066 } 1617 1067 1618 1068 if (sc->quirks & SIXAXIS_CONTROLLER_USB) { 1619 - hdev->hid_output_raw_report = sixaxis_usb_output_raw_report; 1069 + /* 1070 + * The Sony Sixaxis does not handle HID Output Reports on the 1071 + * Interrupt EP like it could, so we need to force HID Output 1072 + * Reports to use HID_REQ_SET_REPORT on the Control EP. 1073 + * 1074 + * There is also another issue about HID Output Reports via USB, 1075 + * the Sixaxis does not want the report_id as part of the data 1076 + * packet, so we have to discard buf[0] when sending the actual 1077 + * control message, even for numbered reports, humpf! 1078 + */ 1079 + hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; 1080 + hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID; 1620 1081 ret = sixaxis_set_operational_usb(hdev); 1621 - 1622 1082 sc->worker_initialized = 1; 1623 1083 INIT_WORK(&sc->state_worker, sixaxis_state_worker); 1624 - } 1625 - else if (sc->quirks & SIXAXIS_CONTROLLER_BT) 1084 + } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) { 1626 1085 ret = sixaxis_set_operational_bt(hdev); 1627 - else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { 1628 - /* Report 5 (31 bytes) is used to send data to the controller via USB */ 1629 - ret = sony_set_output_report(sc, 0x05, 248); 1086 + sc->worker_initialized = 1; 1087 + INIT_WORK(&sc->state_worker, sixaxis_state_worker); 1088 + } else if (sc->quirks & DUALSHOCK4_CONTROLLER) { 1089 + if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) { 1090 + ret = dualshock4_set_operational_bt(hdev); 1091 + if (ret < 0) { 1092 + hid_err(hdev, "failed to set the Dualshock 4 operational mode\n"); 1093 + goto err_stop; 1094 + } 1095 + } 1096 + /* 1097 + * The Dualshock 4 touchpad supports 2 touches and has a 1098 + * resolution of 1920x940. 1099 + */ 1100 + ret = sony_register_touchpad(sc, 2, 1920, 940); 1630 1101 if (ret < 0) 1631 1102 goto err_stop; 1632 1103 ··· 1657 1086 ret = 0; 1658 1087 } 1659 1088 1089 + if (ret < 0) 1090 + goto err_stop; 1091 + 1092 + ret = sony_check_add(sc); 1660 1093 if (ret < 0) 1661 1094 goto err_stop; 1662 1095 ··· 1674 1099 ret = sony_init_ff(hdev); 1675 1100 if (ret < 0) 1676 1101 goto err_stop; 1102 + if (sc->quirks & SONY_BATTERY_SUPPORT) { 1103 + ret = sony_battery_probe(sc); 1104 + if (ret < 0) 1105 + goto err_stop; 1106 + 1107 + /* Open the device to receive reports with battery info */ 1108 + ret = hid_hw_open(hdev); 1109 + if (ret < 0) { 1110 + hid_err(hdev, "hw open failed\n"); 1111 + goto err_stop; 1112 + } 1113 + } 1114 + 1115 + if (sc->quirks & SONY_FF_SUPPORT) { 1116 + ret = sony_init_ff(hdev); 1117 + if (ret < 0) 1118 + goto err_close; 1677 1119 } 1678 1120 1679 1121 return 0; 1122 + err_close: 1123 + hid_hw_close(hdev); 1680 1124 err_stop: 1681 1125 if (sc->quirks & SONY_LED_SUPPORT) 1682 1126 sony_leds_remove(hdev); 1127 + if (sc->quirks & SONY_BATTERY_SUPPORT) 1128 + sony_battery_remove(sc); 1129 + if (sc->worker_initialized) 1130 + cancel_work_sync(&sc->state_worker); 1131 + sony_remove_dev_list(sc); 1683 1132 hid_hw_stop(hdev); 1684 1133 return ret; 1685 1134 } ··· 1717 1118 1718 1119 if (sc->worker_initialized) 1719 1120 cancel_work_sync(&sc->state_worker); 1121 + if (sc->quirks & SONY_BATTERY_SUPPORT) { 1122 + hid_hw_close(hdev); 1123 + sony_battery_remove(sc); 1124 + } 1125 + 1126 + if (sc->worker_initialized) 1127 + cancel_work_sync(&sc->state_worker); 1128 + 1129 + sony_remove_dev_list(sc); 1720 1130 1721 1131 hid_hw_stop(hdev); 1722 1132 }
+2 -2
drivers/hid/hid-thingm.c
··· 48 48 buf[0], buf[1], buf[2], buf[3], buf[4], 49 49 buf[5], buf[6], buf[7], buf[8]); 50 50 51 - ret = data->hdev->hid_output_raw_report(data->hdev, buf, 52 - BLINK1_CMD_SIZE, HID_FEATURE_REPORT); 51 + ret = hid_hw_raw_request(data->hdev, buf[0], buf, BLINK1_CMD_SIZE, 52 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 53 53 54 54 return ret < 0 ? ret : 0; 55 55 }
+15 -13
drivers/hid/hid-wacom.c
··· 128 128 129 129 rep_data[0] = WAC_CMD_ICON_START_STOP; 130 130 rep_data[1] = 0; 131 - ret = hdev->hid_output_raw_report(hdev, rep_data, 2, 132 - HID_FEATURE_REPORT); 131 + ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2, 132 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 133 133 if (ret < 0) 134 134 goto err; 135 135 ··· 143 143 rep_data[j + 3] = p[(i << 6) + j]; 144 144 145 145 rep_data[2] = i; 146 - ret = hdev->hid_output_raw_report(hdev, rep_data, 67, 147 - HID_FEATURE_REPORT); 146 + ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 67, 147 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 148 148 } 149 149 150 150 rep_data[0] = WAC_CMD_ICON_START_STOP; 151 151 rep_data[1] = 0; 152 152 153 - ret = hdev->hid_output_raw_report(hdev, rep_data, 2, 154 - HID_FEATURE_REPORT); 153 + ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2, 154 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 155 155 156 156 err: 157 157 return; ··· 183 183 buf[3] = value; 184 184 /* use fixed brightness for OLEDs */ 185 185 buf[4] = 0x08; 186 - hdev->hid_output_raw_report(hdev, buf, 9, HID_FEATURE_REPORT); 186 + hid_hw_raw_request(hdev, buf[0], buf, 9, HID_FEATURE_REPORT, 187 + HID_REQ_SET_REPORT); 187 188 kfree(buf); 188 189 } 189 190 ··· 340 339 rep_data[0] = 0x03 ; rep_data[1] = 0x00; 341 340 limit = 3; 342 341 do { 343 - ret = hdev->hid_output_raw_report(hdev, rep_data, 2, 344 - HID_FEATURE_REPORT); 342 + ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2, 343 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 345 344 } while (ret < 0 && limit-- > 0); 346 345 347 346 if (ret >= 0) { ··· 353 352 rep_data[1] = 0x00; 354 353 limit = 3; 355 354 do { 356 - ret = hdev->hid_output_raw_report(hdev, 357 - rep_data, 2, HID_FEATURE_REPORT); 355 + ret = hid_hw_raw_request(hdev, rep_data[0], 356 + rep_data, 2, HID_FEATURE_REPORT, 357 + HID_REQ_SET_REPORT); 358 358 } while (ret < 0 && limit-- > 0); 359 359 360 360 if (ret >= 0) { ··· 380 378 rep_data[0] = 0x03; 381 379 rep_data[1] = wdata->features; 382 380 383 - ret = hdev->hid_output_raw_report(hdev, rep_data, 2, 384 - HID_FEATURE_REPORT); 381 + ret = hid_hw_raw_request(hdev, rep_data[0], rep_data, 2, 382 + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 385 383 if (ret >= 0) 386 384 wdata->high_speed = speed; 387 385 break;
+2 -2
drivers/hid/hid-wiimote-core.c
··· 28 28 __u8 *buf; 29 29 int ret; 30 30 31 - if (!hdev->hid_output_raw_report) 31 + if (!hdev->ll_driver->output_report) 32 32 return -ENODEV; 33 33 34 34 buf = kmemdup(buffer, count, GFP_KERNEL); 35 35 if (!buf) 36 36 return -ENOMEM; 37 37 38 - ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT); 38 + ret = hid_hw_output_report(hdev, buf, count); 39 39 40 40 kfree(buf); 41 41 return ret;
+19 -8
drivers/hid/hidraw.c
··· 123 123 124 124 dev = hidraw_table[minor]->hid; 125 125 126 - if (!dev->hid_output_raw_report) { 127 - ret = -ENODEV; 128 - goto out; 129 - } 130 126 131 127 if (count > HID_MAX_BUFFER_SIZE) { 132 128 hid_warn(dev, "pid %d passed too large report\n", ··· 149 153 goto out_free; 150 154 } 151 155 152 - ret = dev->hid_output_raw_report(dev, buf, count, report_type); 156 + if ((report_type == HID_OUTPUT_REPORT) && 157 + !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) { 158 + ret = hid_hw_output_report(dev, buf, count); 159 + /* 160 + * compatibility with old implementation of USB-HID and I2C-HID: 161 + * if the device does not support receiving output reports, 162 + * on an interrupt endpoint, fallback to SET_REPORT HID command. 163 + */ 164 + if (ret != -ENOSYS) 165 + goto out_free; 166 + } 167 + 168 + ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type, 169 + HID_REQ_SET_REPORT); 170 + 153 171 out_free: 154 172 kfree(buf); 155 173 out: ··· 199 189 200 190 dev = hidraw_table[minor]->hid; 201 191 202 - if (!dev->hid_get_raw_report) { 192 + if (!dev->ll_driver->raw_request) { 203 193 ret = -ENODEV; 204 194 goto out; 205 195 } ··· 226 216 227 217 /* 228 218 * Read the first byte from the user. This is the report number, 229 - * which is passed to dev->hid_get_raw_report(). 219 + * which is passed to hid_hw_raw_request(). 230 220 */ 231 221 if (copy_from_user(&report_number, buffer, 1)) { 232 222 ret = -EFAULT; 233 223 goto out_free; 234 224 } 235 225 236 - ret = dev->hid_get_raw_report(dev, report_number, buf, count, report_type); 226 + ret = hid_hw_raw_request(dev, report_number, buf, count, report_type, 227 + HID_REQ_GET_REPORT); 237 228 238 229 if (ret < 0) 239 230 goto out_free;
+38 -33
drivers/hid/i2c-hid/i2c-hid.c
··· 257 257 return 0; 258 258 } 259 259 260 - static int i2c_hid_set_report(struct i2c_client *client, u8 reportType, 261 - u8 reportID, unsigned char *buf, size_t data_len) 260 + /** 261 + * i2c_hid_set_or_send_report: forward an incoming report to the device 262 + * @client: the i2c_client of the device 263 + * @reportType: 0x03 for HID_FEATURE_REPORT ; 0x02 for HID_OUTPUT_REPORT 264 + * @reportID: the report ID 265 + * @buf: the actual data to transfer, without the report ID 266 + * @len: size of buf 267 + * @use_data: true: use SET_REPORT HID command, false: send plain OUTPUT report 268 + */ 269 + static int i2c_hid_set_or_send_report(struct i2c_client *client, u8 reportType, 270 + u8 reportID, unsigned char *buf, size_t data_len, bool use_data) 262 271 { 263 272 struct i2c_hid *ihid = i2c_get_clientdata(client); 264 273 u8 *args = ihid->argsbuf; 265 - const struct i2c_hid_cmd * hidcmd = &hid_set_report_cmd; 274 + const struct i2c_hid_cmd *hidcmd; 266 275 int ret; 267 276 u16 dataRegister = le16_to_cpu(ihid->hdesc.wDataRegister); 268 277 u16 outputRegister = le16_to_cpu(ihid->hdesc.wOutputRegister); 269 278 u16 maxOutputLength = le16_to_cpu(ihid->hdesc.wMaxOutputLength); 270 279 271 - /* hidraw already checked that data_len < HID_MAX_BUFFER_SIZE */ 280 + /* hid_hw_* already checked that data_len < HID_MAX_BUFFER_SIZE */ 272 281 u16 size = 2 /* size */ + 273 282 (reportID ? 1 : 0) /* reportID */ + 274 283 data_len /* buf */; ··· 288 279 289 280 i2c_hid_dbg(ihid, "%s\n", __func__); 290 281 282 + if (!use_data && maxOutputLength == 0) 283 + return -ENOSYS; 284 + 291 285 if (reportID >= 0x0F) { 292 286 args[index++] = reportID; 293 287 reportID = 0x0F; ··· 300 288 * use the data register for feature reports or if the device does not 301 289 * support the output register 302 290 */ 303 - if (reportType == 0x03 || maxOutputLength == 0) { 291 + if (use_data) { 304 292 args[index++] = dataRegister & 0xFF; 305 293 args[index++] = dataRegister >> 8; 294 + hidcmd = &hid_set_report_cmd; 306 295 } else { 307 296 args[index++] = outputRegister & 0xFF; 308 297 args[index++] = outputRegister >> 8; ··· 572 559 } 573 560 574 561 static int i2c_hid_output_raw_report(struct hid_device *hid, __u8 *buf, 575 - size_t count, unsigned char report_type) 562 + size_t count, unsigned char report_type, bool use_data) 576 563 { 577 564 struct i2c_client *client = hid->driver_data; 578 565 int report_id = buf[0]; ··· 586 573 count--; 587 574 } 588 575 589 - ret = i2c_hid_set_report(client, 576 + ret = i2c_hid_set_or_send_report(client, 590 577 report_type == HID_FEATURE_REPORT ? 0x03 : 0x02, 591 - report_id, buf, count); 578 + report_id, buf, count, use_data); 592 579 593 580 if (report_id && ret >= 0) 594 581 ret++; /* add report_id to the number of transfered bytes */ ··· 596 583 return ret; 597 584 } 598 585 599 - static void i2c_hid_request(struct hid_device *hid, struct hid_report *rep, 600 - int reqtype) 586 + static int i2c_hid_output_report(struct hid_device *hid, __u8 *buf, 587 + size_t count) 601 588 { 602 - struct i2c_client *client = hid->driver_data; 603 - char *buf; 604 - int ret; 605 - int len = i2c_hid_get_report_length(rep) - 2; 589 + return i2c_hid_output_raw_report(hid, buf, count, HID_OUTPUT_REPORT, 590 + false); 591 + } 606 592 607 - buf = hid_alloc_report_buf(rep, GFP_KERNEL); 608 - if (!buf) 609 - return; 610 - 593 + static int i2c_hid_raw_request(struct hid_device *hid, unsigned char reportnum, 594 + __u8 *buf, size_t len, unsigned char rtype, 595 + int reqtype) 596 + { 611 597 switch (reqtype) { 612 598 case HID_REQ_GET_REPORT: 613 - ret = i2c_hid_get_raw_report(hid, rep->id, buf, len, rep->type); 614 - if (ret < 0) 615 - dev_err(&client->dev, "%s: unable to get report: %d\n", 616 - __func__, ret); 617 - else 618 - hid_input_report(hid, rep->type, buf, ret, 0); 619 - break; 599 + return i2c_hid_get_raw_report(hid, reportnum, buf, len, rtype); 620 600 case HID_REQ_SET_REPORT: 621 - hid_output_report(rep, buf); 622 - i2c_hid_output_raw_report(hid, buf, len, rep->type); 623 - break; 601 + if (buf[0] != reportnum) 602 + return -EINVAL; 603 + return i2c_hid_output_raw_report(hid, buf, len, rtype, true); 604 + default: 605 + return -EIO; 624 606 } 625 - 626 - kfree(buf); 627 607 } 628 608 629 609 static int i2c_hid_parse(struct hid_device *hid) ··· 774 768 .open = i2c_hid_open, 775 769 .close = i2c_hid_close, 776 770 .power = i2c_hid_power, 777 - .request = i2c_hid_request, 771 + .output_report = i2c_hid_output_report, 772 + .raw_request = i2c_hid_raw_request, 778 773 }; 779 774 780 775 static int i2c_hid_init_irq(struct i2c_client *client) ··· 1024 1017 1025 1018 hid->driver_data = client; 1026 1019 hid->ll_driver = &i2c_hid_ll_driver; 1027 - hid->hid_get_raw_report = i2c_hid_get_raw_report; 1028 - hid->hid_output_raw_report = i2c_hid_output_raw_report; 1029 1020 hid->dev.parent = &client->dev; 1030 1021 ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev)); 1031 1022 hid->bus = BUS_I2C;
+16 -22
drivers/hid/uhid.c
··· 247 247 static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf, 248 248 size_t count) 249 249 { 250 - struct uhid_device *uhid = hid->driver_data; 251 - unsigned long flags; 252 - struct uhid_event *ev; 250 + return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT); 251 + } 253 252 254 - if (count < 1 || count > UHID_DATA_MAX) 255 - return -EINVAL; 256 - 257 - ev = kzalloc(sizeof(*ev), GFP_KERNEL); 258 - if (!ev) 259 - return -ENOMEM; 260 - 261 - ev->type = UHID_OUTPUT; 262 - ev->u.output.size = count; 263 - ev->u.output.rtype = UHID_OUTPUT_REPORT; 264 - memcpy(ev->u.output.data, buf, count); 265 - 266 - spin_lock_irqsave(&uhid->qlock, flags); 267 - uhid_queue(uhid, ev); 268 - spin_unlock_irqrestore(&uhid->qlock, flags); 269 - 270 - return count; 253 + static int uhid_raw_request(struct hid_device *hid, unsigned char reportnum, 254 + __u8 *buf, size_t len, unsigned char rtype, 255 + int reqtype) 256 + { 257 + switch (reqtype) { 258 + case HID_REQ_GET_REPORT: 259 + return uhid_hid_get_raw(hid, reportnum, buf, len, rtype); 260 + case HID_REQ_SET_REPORT: 261 + /* TODO: implement proper SET_REPORT functionality */ 262 + return -ENOSYS; 263 + default: 264 + return -EIO; 265 + } 271 266 } 272 267 273 268 static struct hid_ll_driver uhid_hid_driver = { ··· 272 277 .close = uhid_hid_close, 273 278 .parse = uhid_hid_parse, 274 279 .output_report = uhid_hid_output_report, 280 + .raw_request = uhid_raw_request, 275 281 }; 276 282 277 283 #ifdef CONFIG_COMPAT ··· 400 404 hid->uniq[63] = 0; 401 405 402 406 hid->ll_driver = &uhid_hid_driver; 403 - hid->hid_get_raw_report = uhid_hid_get_raw; 404 - hid->hid_output_raw_report = uhid_hid_output_raw; 405 407 hid->bus = ev->u.create.bus; 406 408 hid->vendor = ev->u.create.vendor; 407 409 hid->product = ev->u.create.product;
+7 -57
drivers/hid/usbhid/hid-core.c
··· 894 894 int ret, skipped_report_id = 0; 895 895 896 896 /* Byte 0 is the report number. Report data starts at byte 1.*/ 897 - buf[0] = reportnum; 897 + if ((rtype == HID_OUTPUT_REPORT) && 898 + (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORT_ID)) 899 + buf[0] = 0; 900 + else 901 + buf[0] = reportnum; 902 + 898 903 if (buf[0] == 0x0) { 899 904 /* Don't send the Report ID */ 900 905 buf++; ··· 920 915 return ret; 921 916 } 922 917 923 - 924 - static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count, 925 - unsigned char report_type) 926 - { 927 - struct usbhid_device *usbhid = hid->driver_data; 928 - struct usb_device *dev = hid_to_usb_dev(hid); 929 - struct usb_interface *intf = usbhid->intf; 930 - struct usb_host_interface *interface = intf->cur_altsetting; 931 - int ret; 932 - 933 - if (usbhid->urbout && report_type != HID_FEATURE_REPORT) { 934 - int actual_length; 935 - int skipped_report_id = 0; 936 - 937 - if (buf[0] == 0x0) { 938 - /* Don't send the Report ID */ 939 - buf++; 940 - count--; 941 - skipped_report_id = 1; 942 - } 943 - ret = usb_interrupt_msg(dev, usbhid->urbout->pipe, 944 - buf, count, &actual_length, 945 - USB_CTRL_SET_TIMEOUT); 946 - /* return the number of bytes transferred */ 947 - if (ret == 0) { 948 - ret = actual_length; 949 - /* count also the report id */ 950 - if (skipped_report_id) 951 - ret++; 952 - } 953 - } else { 954 - int skipped_report_id = 0; 955 - int report_id = buf[0]; 956 - if (buf[0] == 0x0) { 957 - /* Don't send the Report ID */ 958 - buf++; 959 - count--; 960 - skipped_report_id = 1; 961 - } 962 - ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 963 - HID_REQ_SET_REPORT, 964 - USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 965 - ((report_type + 1) << 8) | report_id, 966 - interface->desc.bInterfaceNumber, buf, count, 967 - USB_CTRL_SET_TIMEOUT); 968 - /* count also the report id, if this was a numbered report. */ 969 - if (ret > 0 && skipped_report_id) 970 - ret++; 971 - } 972 - 973 - return ret; 974 - } 975 - 976 918 static int usbhid_output_report(struct hid_device *hid, __u8 *buf, size_t count) 977 919 { 978 920 struct usbhid_device *usbhid = hid->driver_data; ··· 927 975 int actual_length, skipped_report_id = 0, ret; 928 976 929 977 if (!usbhid->urbout) 930 - return -EIO; 978 + return -ENOSYS; 931 979 932 980 if (buf[0] == 0x0) { 933 981 /* Don't send the Report ID */ ··· 1283 1331 1284 1332 usb_set_intfdata(intf, hid); 1285 1333 hid->ll_driver = &usb_hid_driver; 1286 - hid->hid_get_raw_report = usbhid_get_raw_report; 1287 - hid->hid_output_raw_report = usbhid_output_raw_report; 1288 1334 hid->ff_init = hid_pidff_init; 1289 1335 #ifdef CONFIG_USB_HIDDEV 1290 1336 hid->hiddev_connect = hiddev_connect;
+54 -11
include/linux/hid.h
··· 287 287 #define HID_QUIRK_NO_EMPTY_INPUT 0x00000100 288 288 #define HID_QUIRK_NO_INIT_INPUT_REPORTS 0x00000200 289 289 #define HID_QUIRK_SKIP_OUTPUT_REPORTS 0x00010000 290 + #define HID_QUIRK_SKIP_OUTPUT_REPORT_ID 0x00020000 291 + #define HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP 0x00040000 290 292 #define HID_QUIRK_FULLSPEED_INTERVAL 0x10000000 291 293 #define HID_QUIRK_NO_INIT_REPORTS 0x20000000 292 294 #define HID_QUIRK_NO_IGNORE 0x40000000 ··· 510 508 struct hid_usage *, __s32); 511 509 void (*hiddev_report_event) (struct hid_device *, struct hid_report *); 512 510 513 - /* handler for raw input (Get_Report) data, used by hidraw */ 514 - int (*hid_get_raw_report) (struct hid_device *, unsigned char, __u8 *, size_t, unsigned char); 515 - 516 - /* handler for raw output data, used by hidraw */ 517 - int (*hid_output_raw_report) (struct hid_device *, __u8 *, size_t, unsigned char); 518 - 519 511 /* debugging support via debugfs */ 520 512 unsigned short debug; 521 513 struct dentry *debug_dir; ··· 671 675 * @stop: called on remove 672 676 * @open: called by input layer on open 673 677 * @close: called by input layer on close 674 - * @hidinput_input_event: event input event (e.g. ff or leds) 675 678 * @parse: this method is called only once to parse the device data, 676 679 * shouldn't allocate anything to not leak memory 677 680 * @request: send report request to device (e.g. feature report) 678 681 * @wait: wait for buffered io to complete (send/recv reports) 682 + * @raw_request: send raw report request to device (e.g. feature report) 683 + * @output_report: send output report to device 679 684 * @idle: send idle request to device 680 685 */ 681 686 struct hid_ll_driver { ··· 687 690 void (*close)(struct hid_device *hdev); 688 691 689 692 int (*power)(struct hid_device *hdev, int level); 690 - 691 - int (*hidinput_input_event) (struct input_dev *idev, unsigned int type, 692 - unsigned int code, int value); 693 693 694 694 int (*parse)(struct hid_device *hdev); 695 695 ··· 752 758 unsigned int hidinput_count_leds(struct hid_device *hid); 753 759 __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code); 754 760 void hid_output_report(struct hid_report *report, __u8 *data); 761 + void __hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype); 755 762 u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags); 756 763 struct hid_device *hid_allocate_device(void); 757 764 struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id); ··· 965 970 struct hid_report *report, int reqtype) 966 971 { 967 972 if (hdev->ll_driver->request) 968 - hdev->ll_driver->request(hdev, report, reqtype); 973 + return hdev->ll_driver->request(hdev, report, reqtype); 974 + 975 + __hid_request(hdev, report, reqtype); 976 + } 977 + 978 + /** 979 + * hid_hw_raw_request - send report request to device 980 + * 981 + * @hdev: hid device 982 + * @reportnum: report ID 983 + * @buf: in/out data to transfer 984 + * @len: length of buf 985 + * @rtype: HID report type 986 + * @reqtype: HID_REQ_GET_REPORT or HID_REQ_SET_REPORT 987 + * 988 + * @return: count of data transfered, negative if error 989 + * 990 + * Same behavior as hid_hw_request, but with raw buffers instead. 991 + */ 992 + static inline int hid_hw_raw_request(struct hid_device *hdev, 993 + unsigned char reportnum, __u8 *buf, 994 + size_t len, unsigned char rtype, int reqtype) 995 + { 996 + if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf) 997 + return -EINVAL; 998 + 999 + return hdev->ll_driver->raw_request(hdev, reportnum, buf, len, 1000 + rtype, reqtype); 1001 + } 1002 + 1003 + /** 1004 + * hid_hw_output_report - send output report to device 1005 + * 1006 + * @hdev: hid device 1007 + * @buf: raw data to transfer 1008 + * @len: length of buf 1009 + * 1010 + * @return: count of data transfered, negative if error 1011 + */ 1012 + static inline int hid_hw_output_report(struct hid_device *hdev, __u8 *buf, 1013 + size_t len) 1014 + { 1015 + if (len < 1 || len > HID_MAX_BUFFER_SIZE || !buf) 1016 + return -EINVAL; 1017 + 1018 + if (hdev->ll_driver->output_report) 1019 + return hdev->ll_driver->output_report(hdev, buf, len); 1020 + 1021 + return -ENOSYS; 969 1022 } 970 1023 971 1024 /**
+7 -111
net/bluetooth/hidp/core.c
··· 223 223 input_sync(dev); 224 224 } 225 225 226 - static int hidp_send_report(struct hidp_session *session, struct hid_report *report) 227 - { 228 - unsigned char hdr; 229 - u8 *buf; 230 - int rsize, ret; 231 - 232 - buf = hid_alloc_report_buf(report, GFP_ATOMIC); 233 - if (!buf) 234 - return -EIO; 235 - 236 - hid_output_report(report, buf); 237 - hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT; 238 - 239 - rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0); 240 - ret = hidp_send_intr_message(session, hdr, buf, rsize); 241 - 242 - kfree(buf); 243 - return ret; 244 - } 245 - 246 - static int hidp_hidinput_event(struct input_dev *dev, unsigned int type, 247 - unsigned int code, int value) 248 - { 249 - struct hid_device *hid = input_get_drvdata(dev); 250 - struct hidp_session *session = hid->driver_data; 251 - struct hid_field *field; 252 - int offset; 253 - 254 - BT_DBG("session %p type %d code %d value %d", 255 - session, type, code, value); 256 - 257 - if (type != EV_LED) 258 - return -1; 259 - 260 - offset = hidinput_find_field(hid, type, code, &field); 261 - if (offset == -1) { 262 - hid_warn(dev, "event field not found\n"); 263 - return -1; 264 - } 265 - 266 - hid_set_field(field, offset, value); 267 - 268 - return hidp_send_report(session, field->report); 269 - } 270 - 271 226 static int hidp_get_raw_report(struct hid_device *hid, 272 227 unsigned char report_number, 273 228 unsigned char *data, size_t count, ··· 373 418 return ret; 374 419 } 375 420 376 - static int hidp_output_raw_report(struct hid_device *hid, unsigned char *data, size_t count, 377 - unsigned char report_type) 421 + static int hidp_output_report(struct hid_device *hid, __u8 *data, size_t count) 378 422 { 379 423 struct hidp_session *session = hid->driver_data; 380 - int ret; 381 424 382 - if (report_type == HID_OUTPUT_REPORT) { 383 - report_type = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT; 384 - return hidp_send_intr_message(session, report_type, 385 - data, count); 386 - } else if (report_type != HID_FEATURE_REPORT) { 387 - return -EINVAL; 388 - } 389 - 390 - if (mutex_lock_interruptible(&session->report_mutex)) 391 - return -ERESTARTSYS; 392 - 393 - /* Set up our wait, and send the report request to the device. */ 394 - set_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); 395 - report_type = HIDP_TRANS_SET_REPORT | HIDP_DATA_RTYPE_FEATURE; 396 - ret = hidp_send_ctrl_message(session, report_type, data, count); 397 - if (ret) 398 - goto err; 399 - 400 - /* Wait for the ACK from the device. */ 401 - while (test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) && 402 - !atomic_read(&session->terminate)) { 403 - int res; 404 - 405 - res = wait_event_interruptible_timeout(session->report_queue, 406 - !test_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags) 407 - || atomic_read(&session->terminate), 408 - 10*HZ); 409 - if (res == 0) { 410 - /* timeout */ 411 - ret = -EIO; 412 - goto err; 413 - } 414 - if (res < 0) { 415 - /* signal */ 416 - ret = -ERESTARTSYS; 417 - goto err; 418 - } 419 - } 420 - 421 - if (!session->output_report_success) { 422 - ret = -EIO; 423 - goto err; 424 - } 425 - 426 - ret = count; 427 - 428 - err: 429 - clear_bit(HIDP_WAITING_FOR_SEND_ACK, &session->flags); 430 - mutex_unlock(&session->report_mutex); 431 - return ret; 425 + return hidp_send_intr_message(session, 426 + HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT, 427 + data, count); 432 428 } 433 429 434 430 static int hidp_raw_request(struct hid_device *hid, unsigned char reportnum, ··· 394 488 default: 395 489 return -EIO; 396 490 } 397 - } 398 - 399 - static int hidp_output_report(struct hid_device *hid, __u8 *data, size_t count) 400 - { 401 - struct hidp_session *session = hid->driver_data; 402 - 403 - return hidp_send_intr_message(session, 404 - HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT, 405 - data, count); 406 491 } 407 492 408 493 static void hidp_idle_timeout(unsigned long arg) ··· 726 829 .close = hidp_close, 727 830 .raw_request = hidp_raw_request, 728 831 .output_report = hidp_output_report, 729 - .hidinput_input_event = hidp_hidinput_event, 730 832 }; 731 833 732 834 /* This function sets up the hid device. It does not add it ··· 767 871 snprintf(hid->phys, sizeof(hid->phys), "%pMR", 768 872 &l2cap_pi(session->ctrl_sock->sk)->chan->src); 769 873 874 + /* NOTE: Some device modules depend on the dst address being stored in 875 + * uniq. Please be aware of this before making changes to this behavior. 876 + */ 770 877 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR", 771 878 &l2cap_pi(session->ctrl_sock->sk)->chan->dst); 772 879 773 880 hid->dev.parent = &session->conn->hcon->dev; 774 881 hid->ll_driver = &hidp_hid_driver; 775 - 776 - hid->hid_get_raw_report = hidp_get_raw_report; 777 - hid->hid_output_raw_report = hidp_output_raw_report; 778 882 779 883 /* True if device is blacklisted in drivers/hid/hid-core.c */ 780 884 if (hid_ignore(hid)) {