David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 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 | |
Linus Walleij | 47513c2 | 2015-12-08 16:26:28 +0100 | [diff] [blame] | 27 | #include <linux/gpio/driver.h> |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 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 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 35 | #define CP2112_REPORT_MAX_LENGTH 64 |
| 36 | #define CP2112_GPIO_CONFIG_LENGTH 5 |
| 37 | #define CP2112_GPIO_GET_LENGTH 2 |
| 38 | #define CP2112_GPIO_SET_LENGTH 3 |
| 39 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 40 | enum { |
| 41 | CP2112_GPIO_CONFIG = 0x02, |
| 42 | CP2112_GPIO_GET = 0x03, |
| 43 | CP2112_GPIO_SET = 0x04, |
| 44 | CP2112_GET_VERSION_INFO = 0x05, |
| 45 | CP2112_SMBUS_CONFIG = 0x06, |
| 46 | CP2112_DATA_READ_REQUEST = 0x10, |
| 47 | CP2112_DATA_WRITE_READ_REQUEST = 0x11, |
| 48 | CP2112_DATA_READ_FORCE_SEND = 0x12, |
| 49 | CP2112_DATA_READ_RESPONSE = 0x13, |
| 50 | CP2112_DATA_WRITE_REQUEST = 0x14, |
| 51 | CP2112_TRANSFER_STATUS_REQUEST = 0x15, |
| 52 | CP2112_TRANSFER_STATUS_RESPONSE = 0x16, |
| 53 | CP2112_CANCEL_TRANSFER = 0x17, |
| 54 | CP2112_LOCK_BYTE = 0x20, |
| 55 | CP2112_USB_CONFIG = 0x21, |
| 56 | CP2112_MANUFACTURER_STRING = 0x22, |
| 57 | CP2112_PRODUCT_STRING = 0x23, |
| 58 | CP2112_SERIAL_STRING = 0x24, |
| 59 | }; |
| 60 | |
| 61 | enum { |
| 62 | STATUS0_IDLE = 0x00, |
| 63 | STATUS0_BUSY = 0x01, |
| 64 | STATUS0_COMPLETE = 0x02, |
| 65 | STATUS0_ERROR = 0x03, |
| 66 | }; |
| 67 | |
| 68 | enum { |
| 69 | STATUS1_TIMEOUT_NACK = 0x00, |
| 70 | STATUS1_TIMEOUT_BUS = 0x01, |
| 71 | STATUS1_ARBITRATION_LOST = 0x02, |
| 72 | STATUS1_READ_INCOMPLETE = 0x03, |
| 73 | STATUS1_WRITE_INCOMPLETE = 0x04, |
| 74 | STATUS1_SUCCESS = 0x05, |
| 75 | }; |
| 76 | |
| 77 | struct cp2112_smbus_config_report { |
| 78 | u8 report; /* CP2112_SMBUS_CONFIG */ |
| 79 | __be32 clock_speed; /* Hz */ |
| 80 | u8 device_address; /* Stored in the upper 7 bits */ |
| 81 | u8 auto_send_read; /* 1 = enabled, 0 = disabled */ |
| 82 | __be16 write_timeout; /* ms, 0 = no timeout */ |
| 83 | __be16 read_timeout; /* ms, 0 = no timeout */ |
| 84 | u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */ |
| 85 | __be16 retry_time; /* # of retries, 0 = no limit */ |
| 86 | } __packed; |
| 87 | |
| 88 | struct cp2112_usb_config_report { |
| 89 | u8 report; /* CP2112_USB_CONFIG */ |
| 90 | __le16 vid; /* Vendor ID */ |
| 91 | __le16 pid; /* Product ID */ |
| 92 | u8 max_power; /* Power requested in 2mA units */ |
| 93 | u8 power_mode; /* 0x00 = bus powered |
| 94 | 0x01 = self powered & regulator off |
| 95 | 0x02 = self powered & regulator on */ |
| 96 | u8 release_major; |
| 97 | u8 release_minor; |
| 98 | u8 mask; /* What fields to program */ |
| 99 | } __packed; |
| 100 | |
| 101 | struct cp2112_read_req_report { |
| 102 | u8 report; /* CP2112_DATA_READ_REQUEST */ |
| 103 | u8 slave_address; |
| 104 | __be16 length; |
| 105 | } __packed; |
| 106 | |
| 107 | struct cp2112_write_read_req_report { |
| 108 | u8 report; /* CP2112_DATA_WRITE_READ_REQUEST */ |
| 109 | u8 slave_address; |
| 110 | __be16 length; |
| 111 | u8 target_address_length; |
| 112 | u8 target_address[16]; |
| 113 | } __packed; |
| 114 | |
| 115 | struct cp2112_write_req_report { |
| 116 | u8 report; /* CP2112_DATA_WRITE_REQUEST */ |
| 117 | u8 slave_address; |
| 118 | u8 length; |
| 119 | u8 data[61]; |
| 120 | } __packed; |
| 121 | |
| 122 | struct cp2112_force_read_report { |
| 123 | u8 report; /* CP2112_DATA_READ_FORCE_SEND */ |
| 124 | __be16 length; |
| 125 | } __packed; |
| 126 | |
| 127 | struct cp2112_xfer_status_report { |
| 128 | u8 report; /* CP2112_TRANSFER_STATUS_RESPONSE */ |
| 129 | u8 status0; /* STATUS0_* */ |
| 130 | u8 status1; /* STATUS1_* */ |
| 131 | __be16 retries; |
| 132 | __be16 length; |
| 133 | } __packed; |
| 134 | |
| 135 | struct cp2112_string_report { |
| 136 | u8 dummy; /* force .string to be aligned */ |
| 137 | u8 report; /* CP2112_*_STRING */ |
| 138 | u8 length; /* length in bytes of everyting after .report */ |
| 139 | u8 type; /* USB_DT_STRING */ |
| 140 | wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */ |
| 141 | } __packed; |
| 142 | |
| 143 | /* Number of times to request transfer status before giving up waiting for a |
| 144 | transfer to complete. This may need to be changed if SMBUS clock, retries, |
| 145 | or read/write/scl_low timeout settings are changed. */ |
| 146 | static const int XFER_STATUS_RETRIES = 10; |
| 147 | |
| 148 | /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or |
| 149 | CP2112_TRANSFER_STATUS_RESPONSE. */ |
| 150 | static const int RESPONSE_TIMEOUT = 50; |
| 151 | |
| 152 | static const struct hid_device_id cp2112_devices[] = { |
| 153 | { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) }, |
| 154 | { } |
| 155 | }; |
| 156 | MODULE_DEVICE_TABLE(hid, cp2112_devices); |
| 157 | |
| 158 | struct cp2112_device { |
| 159 | struct i2c_adapter adap; |
| 160 | struct hid_device *hdev; |
| 161 | wait_queue_head_t wait; |
| 162 | u8 read_data[61]; |
| 163 | u8 read_length; |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 164 | u8 hwversion; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 165 | int xfer_status; |
| 166 | atomic_t read_avail; |
| 167 | atomic_t xfer_avail; |
| 168 | struct gpio_chip gc; |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 169 | u8 *in_out_buffer; |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 170 | struct mutex lock; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | static int gpio_push_pull = 0xFF; |
| 174 | module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR); |
| 175 | MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask"); |
| 176 | |
| 177 | static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 178 | { |
Linus Walleij | 47513c2 | 2015-12-08 16:26:28 +0100 | [diff] [blame] | 179 | struct cp2112_device *dev = gpiochip_get_data(chip); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 180 | struct hid_device *hdev = dev->hdev; |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 181 | u8 *buf = dev->in_out_buffer; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 182 | int ret; |
| 183 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 184 | mutex_lock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 185 | |
Jiri Kosina | 490051a | 2014-02-18 00:39:39 +0100 | [diff] [blame] | 186 | ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 187 | CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, |
| 188 | HID_REQ_GET_REPORT); |
| 189 | if (ret != CP2112_GPIO_CONFIG_LENGTH) { |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 190 | hid_err(hdev, "error requesting GPIO config: %d\n", ret); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 191 | goto exit; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | buf[1] &= ~(1 << offset); |
| 195 | buf[2] = gpio_push_pull; |
| 196 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 197 | ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, |
| 198 | CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, |
| 199 | HID_REQ_SET_REPORT); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 200 | if (ret < 0) { |
| 201 | hid_err(hdev, "error setting GPIO config: %d\n", ret); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 202 | goto exit; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 203 | } |
| 204 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 205 | ret = 0; |
| 206 | |
| 207 | exit: |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 208 | mutex_unlock(&dev->lock); |
Johan Hovold | 7396685 | 2017-01-30 11:26:39 +0100 | [diff] [blame] | 209 | return ret < 0 ? ret : -EIO; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 213 | { |
Linus Walleij | 47513c2 | 2015-12-08 16:26:28 +0100 | [diff] [blame] | 214 | struct cp2112_device *dev = gpiochip_get_data(chip); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 215 | struct hid_device *hdev = dev->hdev; |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 216 | u8 *buf = dev->in_out_buffer; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 217 | int ret; |
| 218 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 219 | mutex_lock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 220 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 221 | buf[0] = CP2112_GPIO_SET; |
| 222 | buf[1] = value ? 0xff : 0; |
| 223 | buf[2] = 1 << offset; |
| 224 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 225 | ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf, |
| 226 | CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT, |
| 227 | HID_REQ_SET_REPORT); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 228 | if (ret < 0) |
| 229 | hid_err(hdev, "error setting GPIO values: %d\n", ret); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 230 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 231 | mutex_unlock(&dev->lock); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int cp2112_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 235 | { |
Linus Walleij | 47513c2 | 2015-12-08 16:26:28 +0100 | [diff] [blame] | 236 | struct cp2112_device *dev = gpiochip_get_data(chip); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 237 | struct hid_device *hdev = dev->hdev; |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 238 | u8 *buf = dev->in_out_buffer; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 239 | int ret; |
| 240 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 241 | mutex_lock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 242 | |
| 243 | ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf, |
| 244 | CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT, |
| 245 | HID_REQ_GET_REPORT); |
| 246 | if (ret != CP2112_GPIO_GET_LENGTH) { |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 247 | hid_err(hdev, "error requesting GPIO values: %d\n", ret); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 248 | ret = ret < 0 ? ret : -EIO; |
| 249 | goto exit; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 252 | ret = (buf[1] >> offset) & 1; |
| 253 | |
| 254 | exit: |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 255 | mutex_unlock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 256 | |
| 257 | return ret; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int cp2112_gpio_direction_output(struct gpio_chip *chip, |
| 261 | unsigned offset, int value) |
| 262 | { |
Linus Walleij | 47513c2 | 2015-12-08 16:26:28 +0100 | [diff] [blame] | 263 | struct cp2112_device *dev = gpiochip_get_data(chip); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 264 | struct hid_device *hdev = dev->hdev; |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 265 | u8 *buf = dev->in_out_buffer; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 266 | int ret; |
| 267 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 268 | mutex_lock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 269 | |
Jiri Kosina | 490051a | 2014-02-18 00:39:39 +0100 | [diff] [blame] | 270 | ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 271 | CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, |
| 272 | HID_REQ_GET_REPORT); |
| 273 | if (ret != CP2112_GPIO_CONFIG_LENGTH) { |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 274 | hid_err(hdev, "error requesting GPIO config: %d\n", ret); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 275 | goto fail; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | buf[1] |= 1 << offset; |
| 279 | buf[2] = gpio_push_pull; |
| 280 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 281 | ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf, |
| 282 | CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT, |
| 283 | HID_REQ_SET_REPORT); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 284 | if (ret < 0) { |
| 285 | hid_err(hdev, "error setting GPIO config: %d\n", ret); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 286 | goto fail; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 287 | } |
| 288 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 289 | mutex_unlock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 290 | |
Antonio Borneo | beb9d00 | 2014-06-29 14:13:48 +0800 | [diff] [blame] | 291 | /* |
| 292 | * Set gpio value when output direction is already set, |
| 293 | * as specified in AN495, Rev. 0.2, cpt. 4.4 |
| 294 | */ |
| 295 | cp2112_gpio_set(chip, offset, value); |
| 296 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 297 | return 0; |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 298 | |
| 299 | fail: |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 300 | mutex_unlock(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 301 | return ret < 0 ? ret : -EIO; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number, |
| 305 | u8 *data, size_t count, unsigned char report_type) |
| 306 | { |
| 307 | u8 *buf; |
| 308 | int ret; |
| 309 | |
| 310 | buf = kmalloc(count, GFP_KERNEL); |
| 311 | if (!buf) |
| 312 | return -ENOMEM; |
| 313 | |
Jiri Kosina | 490051a | 2014-02-18 00:39:39 +0100 | [diff] [blame] | 314 | ret = hid_hw_raw_request(hdev, report_number, buf, count, |
| 315 | report_type, HID_REQ_GET_REPORT); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 316 | memcpy(data, buf, count); |
| 317 | kfree(buf); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count, |
| 322 | unsigned char report_type) |
| 323 | { |
| 324 | u8 *buf; |
| 325 | int ret; |
| 326 | |
| 327 | buf = kmemdup(data, count, GFP_KERNEL); |
| 328 | if (!buf) |
| 329 | return -ENOMEM; |
| 330 | |
Benjamin Tissoires | 866e479 | 2014-03-08 22:52:41 -0500 | [diff] [blame] | 331 | if (report_type == HID_OUTPUT_REPORT) |
| 332 | ret = hid_hw_output_report(hdev, buf, count); |
| 333 | else |
| 334 | ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type, |
| 335 | HID_REQ_SET_REPORT); |
| 336 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 337 | kfree(buf); |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail) |
| 342 | { |
| 343 | int ret = 0; |
| 344 | |
| 345 | /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a |
| 346 | * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to |
| 347 | * come in cp2112_raw_event or timeout. There will only be one of these |
| 348 | * in flight at any one time. The timeout is extremely large and is a |
| 349 | * last resort if the CP2112 has died. If we do timeout we don't expect |
| 350 | * to receive the response which would cause data races, it's not like |
| 351 | * we can do anything about it anyway. |
| 352 | */ |
| 353 | ret = wait_event_interruptible_timeout(dev->wait, |
| 354 | atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT)); |
| 355 | if (-ERESTARTSYS == ret) |
| 356 | return ret; |
| 357 | if (!ret) |
| 358 | return -ETIMEDOUT; |
| 359 | |
| 360 | atomic_set(avail, 0); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int cp2112_xfer_status(struct cp2112_device *dev) |
| 365 | { |
| 366 | struct hid_device *hdev = dev->hdev; |
| 367 | u8 buf[2]; |
| 368 | int ret; |
| 369 | |
| 370 | buf[0] = CP2112_TRANSFER_STATUS_REQUEST; |
| 371 | buf[1] = 0x01; |
| 372 | atomic_set(&dev->xfer_avail, 0); |
| 373 | |
| 374 | ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); |
| 375 | if (ret < 0) { |
| 376 | hid_warn(hdev, "Error requesting status: %d\n", ret); |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | ret = cp2112_wait(dev, &dev->xfer_avail); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | |
| 384 | return dev->xfer_status; |
| 385 | } |
| 386 | |
| 387 | static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size) |
| 388 | { |
| 389 | struct hid_device *hdev = dev->hdev; |
| 390 | struct cp2112_force_read_report report; |
| 391 | int ret; |
| 392 | |
Antonio Borneo | 6debce6 | 2015-06-21 14:20:25 +0800 | [diff] [blame] | 393 | if (size > sizeof(dev->read_data)) |
| 394 | size = sizeof(dev->read_data); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 395 | report.report = CP2112_DATA_READ_FORCE_SEND; |
| 396 | report.length = cpu_to_be16(size); |
| 397 | |
| 398 | atomic_set(&dev->read_avail, 0); |
| 399 | |
| 400 | ret = cp2112_hid_output(hdev, &report.report, sizeof(report), |
| 401 | HID_OUTPUT_REPORT); |
| 402 | if (ret < 0) { |
| 403 | hid_warn(hdev, "Error requesting data: %d\n", ret); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | ret = cp2112_wait(dev, &dev->read_avail); |
| 408 | if (ret) |
| 409 | return ret; |
| 410 | |
Jiri Kosina | 5a673fc | 2014-02-17 23:44:54 +0100 | [diff] [blame] | 411 | hid_dbg(hdev, "read %d of %zd bytes requested\n", |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 412 | dev->read_length, size); |
| 413 | |
| 414 | if (size > dev->read_length) |
| 415 | size = dev->read_length; |
| 416 | |
| 417 | memcpy(data, dev->read_data, size); |
| 418 | return dev->read_length; |
| 419 | } |
| 420 | |
| 421 | static int cp2112_read_req(void *buf, u8 slave_address, u16 length) |
| 422 | { |
| 423 | struct cp2112_read_req_report *report = buf; |
| 424 | |
| 425 | if (length < 1 || length > 512) |
| 426 | return -EINVAL; |
| 427 | |
| 428 | report->report = CP2112_DATA_READ_REQUEST; |
| 429 | report->slave_address = slave_address << 1; |
| 430 | report->length = cpu_to_be16(length); |
| 431 | return sizeof(*report); |
| 432 | } |
| 433 | |
| 434 | static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length, |
| 435 | u8 command, u8 *data, u8 data_length) |
| 436 | { |
| 437 | struct cp2112_write_read_req_report *report = buf; |
| 438 | |
| 439 | if (length < 1 || length > 512 |
| 440 | || data_length > sizeof(report->target_address) - 1) |
| 441 | return -EINVAL; |
| 442 | |
| 443 | report->report = CP2112_DATA_WRITE_READ_REQUEST; |
| 444 | report->slave_address = slave_address << 1; |
| 445 | report->length = cpu_to_be16(length); |
| 446 | report->target_address_length = data_length + 1; |
| 447 | report->target_address[0] = command; |
| 448 | memcpy(&report->target_address[1], data, data_length); |
| 449 | return data_length + 6; |
| 450 | } |
| 451 | |
| 452 | static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data, |
| 453 | u8 data_length) |
| 454 | { |
| 455 | struct cp2112_write_req_report *report = buf; |
| 456 | |
| 457 | if (data_length > sizeof(report->data) - 1) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | report->report = CP2112_DATA_WRITE_REQUEST; |
| 461 | report->slave_address = slave_address << 1; |
| 462 | report->length = data_length + 1; |
| 463 | report->data[0] = command; |
| 464 | memcpy(&report->data[1], data, data_length); |
| 465 | return data_length + 4; |
| 466 | } |
| 467 | |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 468 | static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data, |
| 469 | u8 data_length) |
| 470 | { |
| 471 | struct cp2112_write_req_report *report = buf; |
| 472 | |
| 473 | if (data_length > sizeof(report->data)) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | report->report = CP2112_DATA_WRITE_REQUEST; |
| 477 | report->slave_address = slave_address << 1; |
| 478 | report->length = data_length; |
| 479 | memcpy(report->data, data, data_length); |
| 480 | return data_length + 3; |
| 481 | } |
| 482 | |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 483 | static int cp2112_i2c_write_read_req(void *buf, u8 slave_address, |
| 484 | u8 *addr, int addr_length, |
| 485 | int read_length) |
| 486 | { |
| 487 | struct cp2112_write_read_req_report *report = buf; |
| 488 | |
| 489 | if (read_length < 1 || read_length > 512 || |
| 490 | addr_length > sizeof(report->target_address)) |
| 491 | return -EINVAL; |
| 492 | |
| 493 | report->report = CP2112_DATA_WRITE_READ_REQUEST; |
| 494 | report->slave_address = slave_address << 1; |
| 495 | report->length = cpu_to_be16(read_length); |
| 496 | report->target_address_length = addr_length; |
| 497 | memcpy(report->target_address, addr, addr_length); |
| 498 | return addr_length + 5; |
| 499 | } |
| 500 | |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 501 | static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, |
| 502 | int num) |
| 503 | { |
| 504 | struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data; |
| 505 | struct hid_device *hdev = dev->hdev; |
| 506 | u8 buf[64]; |
| 507 | ssize_t count; |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 508 | ssize_t read_length = 0; |
| 509 | u8 *read_buf = NULL; |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 510 | unsigned int retries; |
| 511 | int ret; |
| 512 | |
| 513 | hid_dbg(hdev, "I2C %d messages\n", num); |
| 514 | |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 515 | if (num == 1) { |
| 516 | if (msgs->flags & I2C_M_RD) { |
| 517 | hid_dbg(hdev, "I2C read %#04x len %d\n", |
| 518 | msgs->addr, msgs->len); |
| 519 | read_length = msgs->len; |
| 520 | read_buf = msgs->buf; |
| 521 | count = cp2112_read_req(buf, msgs->addr, msgs->len); |
| 522 | } else { |
| 523 | hid_dbg(hdev, "I2C write %#04x len %d\n", |
| 524 | msgs->addr, msgs->len); |
| 525 | count = cp2112_i2c_write_req(buf, msgs->addr, |
| 526 | msgs->buf, msgs->len); |
| 527 | } |
| 528 | if (count < 0) |
| 529 | return count; |
| 530 | } else if (dev->hwversion > 1 && /* no repeated start in rev 1 */ |
| 531 | num == 2 && |
| 532 | msgs[0].addr == msgs[1].addr && |
| 533 | !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) { |
| 534 | hid_dbg(hdev, "I2C write-read %#04x wlen %d rlen %d\n", |
| 535 | msgs[0].addr, msgs[0].len, msgs[1].len); |
| 536 | read_length = msgs[1].len; |
| 537 | read_buf = msgs[1].buf; |
| 538 | count = cp2112_i2c_write_read_req(buf, msgs[0].addr, |
| 539 | msgs[0].buf, msgs[0].len, msgs[1].len); |
| 540 | if (count < 0) |
| 541 | return count; |
| 542 | } else { |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 543 | hid_err(hdev, |
| 544 | "Multi-message I2C transactions not supported\n"); |
| 545 | return -EOPNOTSUPP; |
| 546 | } |
| 547 | |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 548 | ret = hid_hw_power(hdev, PM_HINT_FULLON); |
| 549 | if (ret < 0) { |
| 550 | hid_err(hdev, "power management error: %d\n", ret); |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT); |
| 555 | if (ret < 0) { |
| 556 | hid_warn(hdev, "Error starting transaction: %d\n", ret); |
| 557 | goto power_normal; |
| 558 | } |
| 559 | |
| 560 | for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) { |
| 561 | ret = cp2112_xfer_status(dev); |
| 562 | if (-EBUSY == ret) |
| 563 | continue; |
| 564 | if (ret < 0) |
| 565 | goto power_normal; |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | if (XFER_STATUS_RETRIES <= retries) { |
| 570 | hid_warn(hdev, "Transfer timed out, cancelling.\n"); |
| 571 | buf[0] = CP2112_CANCEL_TRANSFER; |
| 572 | buf[1] = 0x01; |
| 573 | |
| 574 | ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); |
| 575 | if (ret < 0) |
| 576 | hid_warn(hdev, "Error cancelling transaction: %d\n", |
| 577 | ret); |
| 578 | |
| 579 | ret = -ETIMEDOUT; |
| 580 | goto power_normal; |
| 581 | } |
| 582 | |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 583 | for (count = 0; count < read_length;) { |
| 584 | ret = cp2112_read(dev, read_buf + count, read_length - count); |
Ellen Wang | 5ddfb12 | 2015-07-08 11:17:39 -0700 | [diff] [blame] | 585 | if (ret < 0) |
| 586 | goto power_normal; |
| 587 | if (ret == 0) { |
| 588 | hid_err(hdev, "read returned 0\n"); |
| 589 | ret = -EIO; |
| 590 | goto power_normal; |
| 591 | } |
| 592 | count += ret; |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 593 | if (count > read_length) { |
Ellen Wang | 5ddfb12 | 2015-07-08 11:17:39 -0700 | [diff] [blame] | 594 | /* |
| 595 | * The hardware returned too much data. |
| 596 | * This is mostly harmless because cp2112_read() |
| 597 | * has a limit check so didn't overrun our |
| 598 | * buffer. Nevertheless, we return an error |
| 599 | * because something is seriously wrong and |
| 600 | * it shouldn't go unnoticed. |
| 601 | */ |
| 602 | hid_err(hdev, "long read: %d > %zd\n", |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 603 | ret, read_length - count + ret); |
Ellen Wang | 5ddfb12 | 2015-07-08 11:17:39 -0700 | [diff] [blame] | 604 | ret = -EIO; |
| 605 | goto power_normal; |
| 606 | } |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 607 | } |
| 608 | |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 609 | /* return the number of transferred messages */ |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 610 | ret = num; |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 611 | |
| 612 | power_normal: |
| 613 | hid_hw_power(hdev, PM_HINT_NORMAL); |
| 614 | hid_dbg(hdev, "I2C transfer finished: %d\n", ret); |
| 615 | return ret; |
| 616 | } |
| 617 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 618 | static int cp2112_xfer(struct i2c_adapter *adap, u16 addr, |
| 619 | unsigned short flags, char read_write, u8 command, |
| 620 | int size, union i2c_smbus_data *data) |
| 621 | { |
| 622 | struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data; |
| 623 | struct hid_device *hdev = dev->hdev; |
| 624 | u8 buf[64]; |
Ellen Wang | 29e2d6d | 2015-07-09 22:04:31 -0700 | [diff] [blame] | 625 | __le16 word; |
Jiri Kosina | 0438ee7 | 2014-02-18 09:43:53 +0100 | [diff] [blame] | 626 | ssize_t count; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 627 | size_t read_length = 0; |
| 628 | unsigned int retries; |
| 629 | int ret; |
| 630 | |
| 631 | hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n", |
| 632 | read_write == I2C_SMBUS_WRITE ? "write" : "read", |
| 633 | addr, flags, command, size); |
| 634 | |
| 635 | switch (size) { |
| 636 | case I2C_SMBUS_BYTE: |
| 637 | read_length = 1; |
| 638 | |
| 639 | if (I2C_SMBUS_READ == read_write) |
| 640 | count = cp2112_read_req(buf, addr, read_length); |
| 641 | else |
Ellen Wang | 6d00d15 | 2015-07-13 15:23:54 -0700 | [diff] [blame] | 642 | count = cp2112_write_req(buf, addr, command, NULL, |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 643 | 0); |
| 644 | break; |
| 645 | case I2C_SMBUS_BYTE_DATA: |
| 646 | read_length = 1; |
| 647 | |
| 648 | if (I2C_SMBUS_READ == read_write) |
| 649 | count = cp2112_write_read_req(buf, addr, read_length, |
| 650 | command, NULL, 0); |
| 651 | else |
| 652 | count = cp2112_write_req(buf, addr, command, |
| 653 | &data->byte, 1); |
| 654 | break; |
| 655 | case I2C_SMBUS_WORD_DATA: |
| 656 | read_length = 2; |
Ellen Wang | 29e2d6d | 2015-07-09 22:04:31 -0700 | [diff] [blame] | 657 | word = cpu_to_le16(data->word); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 658 | |
| 659 | if (I2C_SMBUS_READ == read_write) |
| 660 | count = cp2112_write_read_req(buf, addr, read_length, |
| 661 | command, NULL, 0); |
| 662 | else |
| 663 | count = cp2112_write_req(buf, addr, command, |
| 664 | (u8 *)&word, 2); |
| 665 | break; |
| 666 | case I2C_SMBUS_PROC_CALL: |
| 667 | size = I2C_SMBUS_WORD_DATA; |
| 668 | read_write = I2C_SMBUS_READ; |
| 669 | read_length = 2; |
Ellen Wang | 29e2d6d | 2015-07-09 22:04:31 -0700 | [diff] [blame] | 670 | word = cpu_to_le16(data->word); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 671 | |
| 672 | count = cp2112_write_read_req(buf, addr, read_length, command, |
| 673 | (u8 *)&word, 2); |
| 674 | break; |
| 675 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 676 | size = I2C_SMBUS_BLOCK_DATA; |
| 677 | /* fallthrough */ |
| 678 | case I2C_SMBUS_BLOCK_DATA: |
| 679 | if (I2C_SMBUS_READ == read_write) { |
| 680 | count = cp2112_write_read_req(buf, addr, |
| 681 | I2C_SMBUS_BLOCK_MAX, |
| 682 | command, NULL, 0); |
| 683 | } else { |
| 684 | count = cp2112_write_req(buf, addr, command, |
| 685 | data->block, |
| 686 | data->block[0] + 1); |
| 687 | } |
| 688 | break; |
| 689 | case I2C_SMBUS_BLOCK_PROC_CALL: |
| 690 | size = I2C_SMBUS_BLOCK_DATA; |
| 691 | read_write = I2C_SMBUS_READ; |
| 692 | |
| 693 | count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX, |
| 694 | command, data->block, |
| 695 | data->block[0] + 1); |
| 696 | break; |
| 697 | default: |
| 698 | hid_warn(hdev, "Unsupported transaction %d\n", size); |
| 699 | return -EOPNOTSUPP; |
| 700 | } |
| 701 | |
| 702 | if (count < 0) |
| 703 | return count; |
| 704 | |
| 705 | ret = hid_hw_power(hdev, PM_HINT_FULLON); |
| 706 | if (ret < 0) { |
| 707 | hid_err(hdev, "power management error: %d\n", ret); |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT); |
| 712 | if (ret < 0) { |
| 713 | hid_warn(hdev, "Error starting transaction: %d\n", ret); |
| 714 | goto power_normal; |
| 715 | } |
| 716 | |
| 717 | for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) { |
| 718 | ret = cp2112_xfer_status(dev); |
| 719 | if (-EBUSY == ret) |
| 720 | continue; |
| 721 | if (ret < 0) |
| 722 | goto power_normal; |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | if (XFER_STATUS_RETRIES <= retries) { |
| 727 | hid_warn(hdev, "Transfer timed out, cancelling.\n"); |
| 728 | buf[0] = CP2112_CANCEL_TRANSFER; |
| 729 | buf[1] = 0x01; |
| 730 | |
| 731 | ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT); |
| 732 | if (ret < 0) |
| 733 | hid_warn(hdev, "Error cancelling transaction: %d\n", |
| 734 | ret); |
| 735 | |
| 736 | ret = -ETIMEDOUT; |
| 737 | goto power_normal; |
| 738 | } |
| 739 | |
| 740 | if (I2C_SMBUS_WRITE == read_write) { |
| 741 | ret = 0; |
| 742 | goto power_normal; |
| 743 | } |
| 744 | |
| 745 | if (I2C_SMBUS_BLOCK_DATA == size) |
| 746 | read_length = ret; |
| 747 | |
| 748 | ret = cp2112_read(dev, buf, read_length); |
| 749 | if (ret < 0) |
| 750 | goto power_normal; |
| 751 | if (ret != read_length) { |
Jiri Kosina | 5a673fc | 2014-02-17 23:44:54 +0100 | [diff] [blame] | 752 | hid_warn(hdev, "short read: %d < %zd\n", ret, read_length); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 753 | ret = -EIO; |
| 754 | goto power_normal; |
| 755 | } |
| 756 | |
| 757 | switch (size) { |
| 758 | case I2C_SMBUS_BYTE: |
| 759 | case I2C_SMBUS_BYTE_DATA: |
| 760 | data->byte = buf[0]; |
| 761 | break; |
| 762 | case I2C_SMBUS_WORD_DATA: |
Ellen Wang | 29e2d6d | 2015-07-09 22:04:31 -0700 | [diff] [blame] | 763 | data->word = le16_to_cpup((__le16 *)buf); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 764 | break; |
| 765 | case I2C_SMBUS_BLOCK_DATA: |
| 766 | if (read_length > I2C_SMBUS_BLOCK_MAX) { |
| 767 | ret = -EPROTO; |
| 768 | goto power_normal; |
| 769 | } |
| 770 | |
| 771 | memcpy(data->block, buf, read_length); |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | ret = 0; |
| 776 | power_normal: |
| 777 | hid_hw_power(hdev, PM_HINT_NORMAL); |
| 778 | hid_dbg(hdev, "transfer finished: %d\n", ret); |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | static u32 cp2112_functionality(struct i2c_adapter *adap) |
| 783 | { |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 784 | return I2C_FUNC_I2C | |
| 785 | I2C_FUNC_SMBUS_BYTE | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 786 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 787 | I2C_FUNC_SMBUS_WORD_DATA | |
| 788 | I2C_FUNC_SMBUS_BLOCK_DATA | |
| 789 | I2C_FUNC_SMBUS_I2C_BLOCK | |
| 790 | I2C_FUNC_SMBUS_PROC_CALL | |
| 791 | I2C_FUNC_SMBUS_BLOCK_PROC_CALL; |
| 792 | } |
| 793 | |
| 794 | static const struct i2c_algorithm smbus_algorithm = { |
Antonio Borneo | b902934 | 2014-07-08 07:25:39 +0800 | [diff] [blame] | 795 | .master_xfer = cp2112_i2c_xfer, |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 796 | .smbus_xfer = cp2112_xfer, |
| 797 | .functionality = cp2112_functionality, |
| 798 | }; |
| 799 | |
| 800 | static int cp2112_get_usb_config(struct hid_device *hdev, |
| 801 | struct cp2112_usb_config_report *cfg) |
| 802 | { |
| 803 | int ret; |
| 804 | |
| 805 | ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg), |
| 806 | HID_FEATURE_REPORT); |
| 807 | if (ret != sizeof(*cfg)) { |
| 808 | hid_err(hdev, "error reading usb config: %d\n", ret); |
| 809 | if (ret < 0) |
| 810 | return ret; |
| 811 | return -EIO; |
| 812 | } |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | static int cp2112_set_usb_config(struct hid_device *hdev, |
| 818 | struct cp2112_usb_config_report *cfg) |
| 819 | { |
| 820 | int ret; |
| 821 | |
| 822 | BUG_ON(cfg->report != CP2112_USB_CONFIG); |
| 823 | |
| 824 | ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg), |
| 825 | HID_FEATURE_REPORT); |
| 826 | if (ret != sizeof(*cfg)) { |
| 827 | hid_err(hdev, "error writing usb config: %d\n", ret); |
| 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | return -EIO; |
| 831 | } |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | static void chmod_sysfs_attrs(struct hid_device *hdev); |
| 837 | |
| 838 | #define CP2112_CONFIG_ATTR(name, store, format, ...) \ |
| 839 | static ssize_t name##_store(struct device *kdev, \ |
| 840 | struct device_attribute *attr, const char *buf, \ |
| 841 | size_t count) \ |
| 842 | { \ |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 843 | struct hid_device *hdev = to_hid_device(kdev); \ |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 844 | struct cp2112_usb_config_report cfg; \ |
| 845 | int ret = cp2112_get_usb_config(hdev, &cfg); \ |
| 846 | if (ret) \ |
| 847 | return ret; \ |
| 848 | store; \ |
| 849 | ret = cp2112_set_usb_config(hdev, &cfg); \ |
| 850 | if (ret) \ |
| 851 | return ret; \ |
| 852 | chmod_sysfs_attrs(hdev); \ |
| 853 | return count; \ |
| 854 | } \ |
| 855 | static ssize_t name##_show(struct device *kdev, \ |
| 856 | struct device_attribute *attr, char *buf) \ |
| 857 | { \ |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 858 | struct hid_device *hdev = to_hid_device(kdev); \ |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 859 | struct cp2112_usb_config_report cfg; \ |
| 860 | int ret = cp2112_get_usb_config(hdev, &cfg); \ |
| 861 | if (ret) \ |
| 862 | return ret; \ |
| 863 | return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \ |
| 864 | } \ |
Jiri Kosina | c3c041b | 2014-02-17 23:40:20 +0100 | [diff] [blame] | 865 | static DEVICE_ATTR_RW(name); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 866 | |
| 867 | CP2112_CONFIG_ATTR(vendor_id, ({ |
| 868 | u16 vid; |
| 869 | |
| 870 | if (sscanf(buf, "%hi", &vid) != 1) |
| 871 | return -EINVAL; |
| 872 | |
| 873 | cfg.vid = cpu_to_le16(vid); |
| 874 | cfg.mask = 0x01; |
| 875 | }), "0x%04x\n", le16_to_cpu(cfg.vid)); |
| 876 | |
| 877 | CP2112_CONFIG_ATTR(product_id, ({ |
| 878 | u16 pid; |
| 879 | |
| 880 | if (sscanf(buf, "%hi", &pid) != 1) |
| 881 | return -EINVAL; |
| 882 | |
| 883 | cfg.pid = cpu_to_le16(pid); |
| 884 | cfg.mask = 0x02; |
| 885 | }), "0x%04x\n", le16_to_cpu(cfg.pid)); |
| 886 | |
| 887 | CP2112_CONFIG_ATTR(max_power, ({ |
| 888 | int mA; |
| 889 | |
| 890 | if (sscanf(buf, "%i", &mA) != 1) |
| 891 | return -EINVAL; |
| 892 | |
| 893 | cfg.max_power = (mA + 1) / 2; |
| 894 | cfg.mask = 0x04; |
| 895 | }), "%u mA\n", cfg.max_power * 2); |
| 896 | |
| 897 | CP2112_CONFIG_ATTR(power_mode, ({ |
| 898 | if (sscanf(buf, "%hhi", &cfg.power_mode) != 1) |
| 899 | return -EINVAL; |
| 900 | |
| 901 | cfg.mask = 0x08; |
| 902 | }), "%u\n", cfg.power_mode); |
| 903 | |
| 904 | CP2112_CONFIG_ATTR(release_version, ({ |
| 905 | if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor) |
| 906 | != 2) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | cfg.mask = 0x10; |
| 910 | }), "%u.%u\n", cfg.release_major, cfg.release_minor); |
| 911 | |
| 912 | #undef CP2112_CONFIG_ATTR |
| 913 | |
| 914 | struct cp2112_pstring_attribute { |
| 915 | struct device_attribute attr; |
| 916 | unsigned char report; |
| 917 | }; |
| 918 | |
| 919 | static ssize_t pstr_store(struct device *kdev, |
| 920 | struct device_attribute *kattr, const char *buf, |
| 921 | size_t count) |
| 922 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 923 | struct hid_device *hdev = to_hid_device(kdev); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 924 | struct cp2112_pstring_attribute *attr = |
| 925 | container_of(kattr, struct cp2112_pstring_attribute, attr); |
| 926 | struct cp2112_string_report report; |
| 927 | int ret; |
| 928 | |
| 929 | memset(&report, 0, sizeof(report)); |
| 930 | |
| 931 | ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN, |
| 932 | report.string, ARRAY_SIZE(report.string)); |
| 933 | report.report = attr->report; |
| 934 | report.length = ret * sizeof(report.string[0]) + 2; |
| 935 | report.type = USB_DT_STRING; |
| 936 | |
| 937 | ret = cp2112_hid_output(hdev, &report.report, report.length + 1, |
| 938 | HID_FEATURE_REPORT); |
| 939 | if (ret != report.length + 1) { |
| 940 | hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name, |
| 941 | ret); |
| 942 | if (ret < 0) |
| 943 | return ret; |
| 944 | return -EIO; |
| 945 | } |
| 946 | |
| 947 | chmod_sysfs_attrs(hdev); |
| 948 | return count; |
| 949 | } |
| 950 | |
| 951 | static ssize_t pstr_show(struct device *kdev, |
| 952 | struct device_attribute *kattr, char *buf) |
| 953 | { |
Geliang Tang | ee79a8f | 2015-12-27 17:25:21 +0800 | [diff] [blame] | 954 | struct hid_device *hdev = to_hid_device(kdev); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 955 | struct cp2112_pstring_attribute *attr = |
| 956 | container_of(kattr, struct cp2112_pstring_attribute, attr); |
| 957 | struct cp2112_string_report report; |
| 958 | u8 length; |
| 959 | int ret; |
| 960 | |
| 961 | ret = cp2112_hid_get(hdev, attr->report, &report.report, |
| 962 | sizeof(report) - 1, HID_FEATURE_REPORT); |
| 963 | if (ret < 3) { |
| 964 | hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name, |
| 965 | ret); |
| 966 | if (ret < 0) |
| 967 | return ret; |
| 968 | return -EIO; |
| 969 | } |
| 970 | |
| 971 | if (report.length < 2) { |
| 972 | hid_err(hdev, "invalid %s string length: %d\n", |
| 973 | kattr->attr.name, report.length); |
| 974 | return -EIO; |
| 975 | } |
| 976 | |
| 977 | length = report.length > ret - 1 ? ret - 1 : report.length; |
| 978 | length = (length - 2) / sizeof(report.string[0]); |
| 979 | ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf, |
| 980 | PAGE_SIZE - 1); |
| 981 | buf[ret++] = '\n'; |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | #define CP2112_PSTR_ATTR(name, _report) \ |
Jiri Kosina | c3c041b | 2014-02-17 23:40:20 +0100 | [diff] [blame] | 986 | static struct cp2112_pstring_attribute dev_attr_##name = { \ |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 987 | .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \ |
| 988 | .report = _report, \ |
| 989 | }; |
| 990 | |
| 991 | CP2112_PSTR_ATTR(manufacturer, CP2112_MANUFACTURER_STRING); |
| 992 | CP2112_PSTR_ATTR(product, CP2112_PRODUCT_STRING); |
| 993 | CP2112_PSTR_ATTR(serial, CP2112_SERIAL_STRING); |
| 994 | |
| 995 | #undef CP2112_PSTR_ATTR |
| 996 | |
| 997 | static const struct attribute_group cp2112_attr_group = { |
| 998 | .attrs = (struct attribute *[]){ |
| 999 | &dev_attr_vendor_id.attr, |
| 1000 | &dev_attr_product_id.attr, |
| 1001 | &dev_attr_max_power.attr, |
| 1002 | &dev_attr_power_mode.attr, |
| 1003 | &dev_attr_release_version.attr, |
| 1004 | &dev_attr_manufacturer.attr.attr, |
| 1005 | &dev_attr_product.attr.attr, |
| 1006 | &dev_attr_serial.attr.attr, |
| 1007 | NULL |
| 1008 | } |
| 1009 | }; |
| 1010 | |
| 1011 | /* Chmoding our sysfs attributes is simply a way to expose which fields in the |
| 1012 | * PROM have already been programmed. We do not depend on this preventing |
| 1013 | * writing to these attributes since the CP2112 will simply ignore writes to |
| 1014 | * already-programmed fields. This is why there is no sense in fixing this |
| 1015 | * racy behaviour. |
| 1016 | */ |
| 1017 | static void chmod_sysfs_attrs(struct hid_device *hdev) |
| 1018 | { |
| 1019 | struct attribute **attr; |
| 1020 | u8 buf[2]; |
| 1021 | int ret; |
| 1022 | |
| 1023 | ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf), |
| 1024 | HID_FEATURE_REPORT); |
| 1025 | if (ret != sizeof(buf)) { |
| 1026 | hid_err(hdev, "error reading lock byte: %d\n", ret); |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | for (attr = cp2112_attr_group.attrs; *attr; ++attr) { |
| 1031 | umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO; |
| 1032 | ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode); |
| 1033 | if (ret < 0) |
| 1034 | hid_err(hdev, "error chmoding sysfs file %s\n", |
| 1035 | (*attr)->name); |
| 1036 | buf[1] >>= 1; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 1041 | { |
| 1042 | struct cp2112_device *dev; |
| 1043 | u8 buf[3]; |
| 1044 | struct cp2112_smbus_config_report config; |
| 1045 | int ret; |
| 1046 | |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 1047 | dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL); |
| 1048 | if (!dev) |
| 1049 | return -ENOMEM; |
| 1050 | |
| 1051 | dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH, |
| 1052 | GFP_KERNEL); |
| 1053 | if (!dev->in_out_buffer) |
| 1054 | return -ENOMEM; |
| 1055 | |
Johan Hovold | a18c458 | 2017-01-30 11:26:38 +0100 | [diff] [blame] | 1056 | mutex_init(&dev->lock); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 1057 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1058 | ret = hid_parse(hdev); |
| 1059 | if (ret) { |
| 1060 | hid_err(hdev, "parse failed\n"); |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); |
| 1065 | if (ret) { |
| 1066 | hid_err(hdev, "hw start failed\n"); |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | ret = hid_hw_open(hdev); |
| 1071 | if (ret) { |
| 1072 | hid_err(hdev, "hw open failed\n"); |
| 1073 | goto err_hid_stop; |
| 1074 | } |
| 1075 | |
| 1076 | ret = hid_hw_power(hdev, PM_HINT_FULLON); |
| 1077 | if (ret < 0) { |
| 1078 | hid_err(hdev, "power management error: %d\n", ret); |
| 1079 | goto err_hid_close; |
| 1080 | } |
| 1081 | |
| 1082 | ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf), |
| 1083 | HID_FEATURE_REPORT); |
| 1084 | if (ret != sizeof(buf)) { |
| 1085 | hid_err(hdev, "error requesting version\n"); |
| 1086 | if (ret >= 0) |
| 1087 | ret = -EIO; |
| 1088 | goto err_power_normal; |
| 1089 | } |
| 1090 | |
| 1091 | hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n", |
| 1092 | buf[1], buf[2]); |
| 1093 | |
| 1094 | ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config, |
| 1095 | sizeof(config), HID_FEATURE_REPORT); |
| 1096 | if (ret != sizeof(config)) { |
| 1097 | hid_err(hdev, "error requesting SMBus config\n"); |
| 1098 | if (ret >= 0) |
| 1099 | ret = -EIO; |
| 1100 | goto err_power_normal; |
| 1101 | } |
| 1102 | |
| 1103 | config.retry_time = cpu_to_be16(1); |
| 1104 | |
| 1105 | ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config), |
| 1106 | HID_FEATURE_REPORT); |
| 1107 | if (ret != sizeof(config)) { |
| 1108 | hid_err(hdev, "error setting SMBus config\n"); |
| 1109 | if (ret >= 0) |
| 1110 | ret = -EIO; |
| 1111 | goto err_power_normal; |
| 1112 | } |
| 1113 | |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1114 | hid_set_drvdata(hdev, (void *)dev); |
| 1115 | dev->hdev = hdev; |
| 1116 | dev->adap.owner = THIS_MODULE; |
| 1117 | dev->adap.class = I2C_CLASS_HWMON; |
| 1118 | dev->adap.algo = &smbus_algorithm; |
| 1119 | dev->adap.algo_data = dev; |
| 1120 | dev->adap.dev.parent = &hdev->dev; |
| 1121 | snprintf(dev->adap.name, sizeof(dev->adap.name), |
| 1122 | "CP2112 SMBus Bridge on hiddev%d", hdev->minor); |
Ellen Wang | 44eda78 | 2015-07-09 21:55:06 -0700 | [diff] [blame] | 1123 | dev->hwversion = buf[2]; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1124 | init_waitqueue_head(&dev->wait); |
| 1125 | |
| 1126 | hid_device_io_start(hdev); |
| 1127 | ret = i2c_add_adapter(&dev->adap); |
| 1128 | hid_device_io_stop(hdev); |
| 1129 | |
| 1130 | if (ret) { |
| 1131 | hid_err(hdev, "error registering i2c adapter\n"); |
Benjamin Tissoires | 1ffb3c4 | 2016-11-21 11:48:39 +0100 | [diff] [blame] | 1132 | goto err_power_normal; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | hid_dbg(hdev, "adapter registered\n"); |
| 1136 | |
| 1137 | dev->gc.label = "cp2112_gpio"; |
| 1138 | dev->gc.direction_input = cp2112_gpio_direction_input; |
| 1139 | dev->gc.direction_output = cp2112_gpio_direction_output; |
| 1140 | dev->gc.set = cp2112_gpio_set; |
| 1141 | dev->gc.get = cp2112_gpio_get; |
| 1142 | dev->gc.base = -1; |
| 1143 | dev->gc.ngpio = 8; |
| 1144 | dev->gc.can_sleep = 1; |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1145 | dev->gc.parent = &hdev->dev; |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1146 | |
Linus Walleij | 47513c2 | 2015-12-08 16:26:28 +0100 | [diff] [blame] | 1147 | ret = gpiochip_add_data(&dev->gc, dev); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1148 | if (ret < 0) { |
| 1149 | hid_err(hdev, "error registering gpio chip\n"); |
| 1150 | goto err_free_i2c; |
| 1151 | } |
| 1152 | |
| 1153 | ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group); |
| 1154 | if (ret < 0) { |
| 1155 | hid_err(hdev, "error creating sysfs attrs\n"); |
| 1156 | goto err_gpiochip_remove; |
| 1157 | } |
| 1158 | |
| 1159 | chmod_sysfs_attrs(hdev); |
| 1160 | hid_hw_power(hdev, PM_HINT_NORMAL); |
| 1161 | |
| 1162 | return ret; |
| 1163 | |
| 1164 | err_gpiochip_remove: |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 1165 | gpiochip_remove(&dev->gc); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1166 | err_free_i2c: |
| 1167 | i2c_del_adapter(&dev->adap); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1168 | err_power_normal: |
| 1169 | hid_hw_power(hdev, PM_HINT_NORMAL); |
| 1170 | err_hid_close: |
| 1171 | hid_hw_close(hdev); |
| 1172 | err_hid_stop: |
| 1173 | hid_hw_stop(hdev); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | static void cp2112_remove(struct hid_device *hdev) |
| 1178 | { |
| 1179 | struct cp2112_device *dev = hid_get_drvdata(hdev); |
| 1180 | |
| 1181 | sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group); |
abdoulaye berthe | 88d5e52 | 2014-07-12 22:30:14 +0200 | [diff] [blame] | 1182 | gpiochip_remove(&dev->gc); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1183 | i2c_del_adapter(&dev->adap); |
| 1184 | /* i2c_del_adapter has finished removing all i2c devices from our |
| 1185 | * adapter. Well behaved devices should no longer call our cp2112_xfer |
| 1186 | * and should have waited for any pending calls to finish. It has also |
| 1187 | * waited for device_unregister(&adap->dev) to complete. Therefore we |
| 1188 | * can safely free our struct cp2112_device. |
| 1189 | */ |
| 1190 | hid_hw_close(hdev); |
| 1191 | hid_hw_stop(hdev); |
David Barksdale | e932d81 | 2014-02-04 12:42:48 -0600 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 1195 | u8 *data, int size) |
| 1196 | { |
| 1197 | struct cp2112_device *dev = hid_get_drvdata(hdev); |
| 1198 | struct cp2112_xfer_status_report *xfer = (void *)data; |
| 1199 | |
| 1200 | switch (data[0]) { |
| 1201 | case CP2112_TRANSFER_STATUS_RESPONSE: |
| 1202 | hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n", |
| 1203 | xfer->status0, xfer->status1, |
| 1204 | be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length)); |
| 1205 | |
| 1206 | switch (xfer->status0) { |
| 1207 | case STATUS0_IDLE: |
| 1208 | dev->xfer_status = -EAGAIN; |
| 1209 | break; |
| 1210 | case STATUS0_BUSY: |
| 1211 | dev->xfer_status = -EBUSY; |
| 1212 | break; |
| 1213 | case STATUS0_COMPLETE: |
| 1214 | dev->xfer_status = be16_to_cpu(xfer->length); |
| 1215 | break; |
| 1216 | case STATUS0_ERROR: |
| 1217 | switch (xfer->status1) { |
| 1218 | case STATUS1_TIMEOUT_NACK: |
| 1219 | case STATUS1_TIMEOUT_BUS: |
| 1220 | dev->xfer_status = -ETIMEDOUT; |
| 1221 | break; |
| 1222 | default: |
| 1223 | dev->xfer_status = -EIO; |
| 1224 | break; |
| 1225 | } |
| 1226 | break; |
| 1227 | default: |
| 1228 | dev->xfer_status = -EINVAL; |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | atomic_set(&dev->xfer_avail, 1); |
| 1233 | break; |
| 1234 | case CP2112_DATA_READ_RESPONSE: |
| 1235 | hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]); |
| 1236 | |
| 1237 | dev->read_length = data[2]; |
| 1238 | if (dev->read_length > sizeof(dev->read_data)) |
| 1239 | dev->read_length = sizeof(dev->read_data); |
| 1240 | |
| 1241 | memcpy(dev->read_data, &data[3], dev->read_length); |
| 1242 | atomic_set(&dev->read_avail, 1); |
| 1243 | break; |
| 1244 | default: |
| 1245 | hid_err(hdev, "unknown report\n"); |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | wake_up_interruptible(&dev->wait); |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
| 1254 | static struct hid_driver cp2112_driver = { |
| 1255 | .name = "cp2112", |
| 1256 | .id_table = cp2112_devices, |
| 1257 | .probe = cp2112_probe, |
| 1258 | .remove = cp2112_remove, |
| 1259 | .raw_event = cp2112_raw_event, |
| 1260 | }; |
| 1261 | |
| 1262 | module_hid_driver(cp2112_driver); |
| 1263 | MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge"); |
| 1264 | MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>"); |
| 1265 | MODULE_LICENSE("GPL"); |
| 1266 | |