| 1 | |
| 2 | |
| 3 | * hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
|
| 4 | *
|
| 5 | * Copyright (c) 2021, Michael Zaidman <[email protected]>
|
| 6 | *
|
| 7 | * Data Sheet:
|
| 8 | * https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT260.pdf
|
| 9 |
|
| 10 |
|
| 11 | #include "hid-ids.h"
|
| 12 | #include <linux/hidraw.h>
|
| 13 | #include <linux/i2c.h>
|
| 14 | #include <linux/module.h>
|
| 15 | #include <linux/usb.h>
|
| 16 |
|
| 17 | #ifdef DEBUG
|
| 18 | static int ft260_debug = 1;
|
| 19 | #else
|
| 20 | static int ft260_debug;
|
| 21 | #endif
|
| 22 | module_param_named(debug, ft260_debug, int, 0600);
|
| 23 | MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
|
| 24 |
|
| 25 | #define ft260_dbg(format, arg...) \
|
| 26 | do { \
|
| 27 | if (ft260_debug) \
|
| 28 | pr_info("%s: " format, __func__, ##arg); \
|
| 29 | } while (0)
|
| 30 |
|
| 31 | #define FT260_REPORT_MAX_LENGTH (64)
|
| 32 | #define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
|
| 33 |
|
| 34 | #define FT260_WAKEUP_NEEDED_AFTER_MS (4800)
|
| 35 |
|
| 36 | |
| 37 | * The ft260 input report format defines 62 bytes for the data payload, but
|
| 38 | * when requested 62 bytes, the controller returns 60 and 2 in separate input
|
| 39 | * reports. To achieve better performance with the multi-report read data
|
| 40 | * transfers, we set the maximum read payload length to a multiple of 60.
|
| 41 | * With a 100 kHz I2C clock, one 240 bytes read takes about 1/27 second,
|
| 42 | * which is excessive; On the other hand, some higher layer drivers like at24
|
| 43 | * or optoe limit the i2c reads to 128 bytes. To not block other drivers out
|
| 44 | * of I2C for potentially troublesome amounts of time, we select the maximum
|
| 45 | * read payload length to be 180 bytes.
|
| 46 |
|
| 47 | #define FT260_RD_DATA_MAX (180)
|
| 48 | #define FT260_WR_DATA_MAX (60)
|
| 49 |
|
| 50 | |
| 51 | * Device interface configuration.
|
| 52 | * The FT260 has 2 interfaces that are controlled by DCNF0 and DCNF1 pins.
|
| 53 | * First implementes USB HID to I2C bridge function and
|
| 54 | * second - USB HID to UART bridge function.
|
| 55 |
|
| 56 | enum {
|
| 57 | FT260_MODE_ALL = 0x00,
|
| 58 | FT260_MODE_I2C = 0x01,
|
| 59 | FT260_MODE_UART = 0x02,
|
| 60 | FT260_MODE_BOTH = 0x03,
|
| 61 | };
|
| 62 |
|
| 63 |
|
| 64 | enum {
|
| 65 | FT260_GET_RQST_TYPE = 0xA1,
|
| 66 | FT260_GET_REPORT = 0x01,
|
| 67 | FT260_SET_RQST_TYPE = 0x21,
|
| 68 | FT260_SET_REPORT = 0x09,
|
| 69 | FT260_FEATURE = 0x03,
|
| 70 | };
|
| 71 |
|
| 72 |
|
| 73 | enum {
|
| 74 | FT260_CHIP_VERSION = 0xA0,
|
| 75 | FT260_SYSTEM_SETTINGS = 0xA1,
|
| 76 | FT260_I2C_STATUS = 0xC0,
|
| 77 | FT260_I2C_READ_REQ = 0xC2,
|
| 78 | FT260_I2C_REPORT_MIN = 0xD0,
|
| 79 | FT260_I2C_REPORT_MAX = 0xDE,
|
| 80 | FT260_GPIO = 0xB0,
|
| 81 | FT260_UART_INTERRUPT_STATUS = 0xB1,
|
| 82 | FT260_UART_STATUS = 0xE0,
|
| 83 | FT260_UART_RI_DCD_STATUS = 0xE1,
|
| 84 | FT260_UART_REPORT = 0xF0,
|
| 85 | };
|
| 86 |
|
| 87 |
|
| 88 | enum {
|
| 89 | FT260_SET_CLOCK = 0x01,
|
| 90 | FT260_SET_I2C_MODE = 0x02,
|
| 91 | FT260_SET_UART_MODE = 0x03,
|
| 92 | FT260_ENABLE_INTERRUPT = 0x05,
|
| 93 | FT260_SELECT_GPIO2_FUNC = 0x06,
|
| 94 | FT260_ENABLE_UART_DCD_RI = 0x07,
|
| 95 | FT260_SELECT_GPIOA_FUNC = 0x08,
|
| 96 | FT260_SELECT_GPIOG_FUNC = 0x09,
|
| 97 | FT260_SET_INTERRUPT_TRIGGER = 0x0A,
|
| 98 | FT260_SET_SUSPEND_OUT_POLAR = 0x0B,
|
| 99 | FT260_ENABLE_UART_RI_WAKEUP = 0x0C,
|
| 100 | FT260_SET_UART_RI_WAKEUP_CFG = 0x0D,
|
| 101 | FT260_SET_I2C_RESET = 0x20,
|
| 102 | FT260_SET_I2C_CLOCK_SPEED = 0x22,
|
| 103 | FT260_SET_UART_RESET = 0x40,
|
| 104 | FT260_SET_UART_CONFIG = 0x41,
|
| 105 | FT260_SET_UART_BAUD_RATE = 0x42,
|
| 106 | FT260_SET_UART_DATA_BIT = 0x43,
|
| 107 | FT260_SET_UART_PARITY = 0x44,
|
| 108 | FT260_SET_UART_STOP_BIT = 0x45,
|
| 109 | FT260_SET_UART_BREAKING = 0x46,
|
| 110 | FT260_SET_UART_XON_XOFF = 0x49,
|
| 111 | };
|
| 112 |
|
| 113 |
|
| 114 | enum {
|
| 115 | FT260_I2C_STATUS_SUCCESS = 0x00,
|
| 116 | FT260_I2C_STATUS_CTRL_BUSY = 0x01,
|
| 117 | FT260_I2C_STATUS_ERROR = 0x02,
|
| 118 | FT260_I2C_STATUS_ADDR_NO_ACK = 0x04,
|
| 119 | FT260_I2C_STATUS_DATA_NO_ACK = 0x08,
|
| 120 | FT260_I2C_STATUS_ARBITR_LOST = 0x10,
|
| 121 | FT260_I2C_STATUS_CTRL_IDLE = 0x20,
|
| 122 | FT260_I2C_STATUS_BUS_BUSY = 0x40,
|
| 123 | };
|
| 124 |
|
| 125 |
|
| 126 | enum {
|
| 127 | FT260_FLAG_NONE = 0x00,
|
| 128 | FT260_FLAG_START = 0x02,
|
| 129 | FT260_FLAG_START_REPEATED = 0x03,
|
| 130 | FT260_FLAG_STOP = 0x04,
|
| 131 | FT260_FLAG_START_STOP = 0x06,
|
| 132 | FT260_FLAG_START_STOP_REPEATED = 0x07,
|
| 133 | };
|
| 134 |
|
| 135 | #define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 | struct ft260_get_chip_version_report {
|
| 140 | u8 report;
|
| 141 | u8 chip_code[4];
|
| 142 | u8 reserved[8];
|
| 143 | } __packed;
|
| 144 |
|
| 145 | struct ft260_get_system_status_report {
|
| 146 | u8 report;
|
| 147 | u8 chip_mode;
|
| 148 | u8 clock_ctl;
|
| 149 | u8 suspend_status;
|
| 150 | u8 pwren_status;
|
| 151 | u8 i2c_enable;
|
| 152 | u8 uart_mode;
|
| 153 |
|
| 154 | u8 hid_over_i2c_en;
|
| 155 | u8 gpio2_function;
|
| 156 |
|
| 157 | u8 gpioA_function;
|
| 158 | u8 gpioG_function;
|
| 159 |
|
| 160 | u8 suspend_out_pol;
|
| 161 | u8 enable_wakeup_int;
|
| 162 | u8 intr_cond;
|
| 163 | u8 power_saving_en;
|
| 164 | u8 reserved[10];
|
| 165 | } __packed;
|
| 166 |
|
| 167 | struct ft260_get_i2c_status_report {
|
| 168 | u8 report;
|
| 169 | u8 bus_status;
|
| 170 | __le16 clock;
|
| 171 | u8 reserved;
|
| 172 | } __packed;
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 | struct ft260_set_system_clock_report {
|
| 177 | u8 report;
|
| 178 | u8 request;
|
| 179 | u8 clock_ctl;
|
| 180 | } __packed;
|
| 181 |
|
| 182 | struct ft260_set_i2c_mode_report {
|
| 183 | u8 report;
|
| 184 | u8 request;
|
| 185 | u8 i2c_enable;
|
| 186 | } __packed;
|
| 187 |
|
| 188 | struct ft260_set_uart_mode_report {
|
| 189 | u8 report;
|
| 190 | u8 request;
|
| 191 | u8 uart_mode;
|
| 192 |
|
| 193 | } __packed;
|
| 194 |
|
| 195 | struct ft260_set_i2c_reset_report {
|
| 196 | u8 report;
|
| 197 | u8 request;
|
| 198 | } __packed;
|
| 199 |
|
| 200 | struct ft260_set_i2c_speed_report {
|
| 201 | u8 report;
|
| 202 | u8 request;
|
| 203 | __le16 clock;
|
| 204 | } __packed;
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 | struct ft260_i2c_write_request_report {
|
| 209 | u8 report;
|
| 210 | u8 address;
|
| 211 | u8 flag;
|
| 212 | u8 length;
|
| 213 | u8 data[FT260_WR_DATA_MAX];
|
| 214 | } __packed;
|
| 215 |
|
| 216 | struct ft260_i2c_read_request_report {
|
| 217 | u8 report;
|
| 218 | u8 address;
|
| 219 | u8 flag;
|
| 220 | __le16 length;
|
| 221 | } __packed;
|
| 222 |
|
| 223 | struct ft260_i2c_input_report {
|
| 224 | u8 report;
|
| 225 | u8 length;
|
| 226 | u8 data[2];
|
| 227 | } __packed;
|
| 228 |
|
| 229 | static const struct hid_device_id ft260_devices[] = {
|
| 230 | { HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
|
| 231 | USB_DEVICE_ID_FT260) },
|
| 232 | { }
|
| 233 | };
|
| 234 | MODULE_DEVICE_TABLE(hid, ft260_devices);
|
| 235 |
|
| 236 | struct ft260_device {
|
| 237 | struct i2c_adapter adap;
|
| 238 | struct hid_device *hdev;
|
| 239 | struct completion wait;
|
| 240 | struct mutex lock;
|
| 241 | u8 write_buf[FT260_REPORT_MAX_LENGTH];
|
| 242 | unsigned long need_wakeup_at;
|
| 243 |
|
| 244 | spinlock_t read_lock;
|
| 245 | u8 *read_buf;
|
| 246 | u16 read_idx;
|
| 247 | u16 read_len;
|
| 248 | u16 clock;
|
| 249 | };
|
| 250 |
|
| 251 | static int ft260_hid_feature_report_get(struct hid_device *hdev,
|
| 252 | unsigned char report_id, u8 *data,
|
| 253 | size_t len)
|
| 254 | {
|
| 255 | u8 *buf;
|
| 256 | int ret;
|
| 257 |
|
| 258 | buf = kmalloc(len, GFP_KERNEL);
|
| 259 | if (!buf)
|
| 260 | return -ENOMEM;
|
| 261 |
|
| 262 | ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
|
| 263 | HID_REQ_GET_REPORT);
|
| 264 | if (likely(ret == len))
|
| 265 | memcpy(data, buf, len);
|
| 266 | else if (ret >= 0)
|
| 267 | ret = -EIO;
|
| 268 | kfree(buf);
|
| 269 | return ret;
|
| 270 | }
|
| 271 |
|
| 272 | static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
|
| 273 | size_t len)
|
| 274 | {
|
| 275 | u8 *buf;
|
| 276 | int ret;
|
| 277 |
|
| 278 | buf = kmemdup(data, len, GFP_KERNEL);
|
| 279 | if (!buf)
|
| 280 | return -ENOMEM;
|
| 281 |
|
| 282 | buf[0] = FT260_SYSTEM_SETTINGS;
|
| 283 |
|
| 284 | ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
|
| 285 | HID_REQ_SET_REPORT);
|
| 286 |
|
| 287 | kfree(buf);
|
| 288 | return ret;
|
| 289 | }
|
| 290 |
|
| 291 | static int ft260_i2c_reset(struct hid_device *hdev)
|
| 292 | {
|
| 293 | struct ft260_set_i2c_reset_report report;
|
| 294 | int ret;
|
| 295 |
|
| 296 | report.request = FT260_SET_I2C_RESET;
|
| 297 |
|
| 298 | ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
|
| 299 | if (ret < 0) {
|
| 300 | hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
|
| 301 | return ret;
|
| 302 | }
|
| 303 |
|
| 304 | ft260_dbg("done\n");
|
| 305 | return ret;
|
| 306 | }
|
| 307 |
|
| 308 | static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
|
| 309 | {
|
| 310 | struct hid_device *hdev = dev->hdev;
|
| 311 | struct ft260_get_i2c_status_report report;
|
| 312 | int ret;
|
| 313 |
|
| 314 | if (time_is_before_jiffies(dev->need_wakeup_at)) {
|
| 315 | ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
|
| 316 | (u8 *)&report, sizeof(report));
|
| 317 | if (unlikely(ret < 0)) {
|
| 318 | hid_err(hdev, "failed to retrieve status: %d, no wakeup\n",
|
| 319 | ret);
|
| 320 | } else {
|
| 321 | dev->need_wakeup_at = jiffies +
|
| 322 | msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS);
|
| 323 | ft260_dbg("bus_status %#02x, wakeup\n",
|
| 324 | report.bus_status);
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
|
| 329 | (u8 *)&report, sizeof(report));
|
| 330 | if (unlikely(ret < 0)) {
|
| 331 | hid_err(hdev, "failed to retrieve status: %d\n", ret);
|
| 332 | return ret;
|
| 333 | }
|
| 334 |
|
| 335 | dev->clock = le16_to_cpu(report.clock);
|
| 336 | ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
|
| 337 | dev->clock);
|
| 338 |
|
| 339 | if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))
|
| 340 | return -EAGAIN;
|
| 341 |
|
| 342 | |
| 343 | * The error condition (bit 1) is a status bit reflecting any
|
| 344 | * error conditions. When any of the bits 2, 3, or 4 are raised
|
| 345 | * to 1, bit 1 is also set to 1.
|
| 346 |
|
| 347 | if (report.bus_status & FT260_I2C_STATUS_ERROR) {
|
| 348 | hid_err(hdev, "i2c bus error: %#02x\n", report.bus_status);
|
| 349 | return -EIO;
|
| 350 | }
|
| 351 |
|
| 352 | return 0;
|
| 353 | }
|
| 354 |
|
| 355 | static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
|
| 356 | size_t len)
|
| 357 | {
|
| 358 | u8 *buf;
|
| 359 | int ret;
|
| 360 |
|
| 361 | buf = kmemdup(data, len, GFP_KERNEL);
|
| 362 | if (!buf)
|
| 363 | return -ENOMEM;
|
| 364 |
|
| 365 | ret = hid_hw_output_report(hdev, buf, len);
|
| 366 |
|
| 367 | kfree(buf);
|
| 368 | return ret;
|
| 369 | }
|
| 370 |
|
| 371 | static int ft260_hid_output_report_check_status(struct ft260_device *dev,
|
| 372 | u8 *data, int len)
|
| 373 | {
|
| 374 | u8 bus_busy;
|
| 375 | int ret, usec, try = 100;
|
| 376 | struct hid_device *hdev = dev->hdev;
|
| 377 | struct ft260_i2c_write_request_report *rep =
|
| 378 | (struct ft260_i2c_write_request_report *)data;
|
| 379 |
|
| 380 | ret = ft260_hid_output_report(hdev, data, len);
|
| 381 | if (ret < 0) {
|
| 382 | hid_err(hdev, "%s: failed to start transfer, ret %d\n",
|
| 383 | __func__, ret);
|
| 384 | ft260_i2c_reset(hdev);
|
| 385 | return ret;
|
| 386 | }
|
| 387 |
|
| 388 |
|
| 389 | usec = len * 9000 / dev->clock;
|
| 390 | if (usec > 2000) {
|
| 391 | usec -= 1500;
|
| 392 | usleep_range(usec, usec + 100);
|
| 393 | ft260_dbg("wait %d usec, len %d\n", usec, len);
|
| 394 | }
|
| 395 |
|
| 396 | |
| 397 | * Do not check the busy bit for combined transactions
|
| 398 | * since the controller keeps the bus busy between writing
|
| 399 | * and reading IOs to ensure an atomic operation.
|
| 400 |
|
| 401 | if (rep->flag == FT260_FLAG_START)
|
| 402 | bus_busy = 0;
|
| 403 | else
|
| 404 | bus_busy = FT260_I2C_STATUS_BUS_BUSY;
|
| 405 |
|
| 406 | do {
|
| 407 | ret = ft260_xfer_status(dev, bus_busy);
|
| 408 | if (ret != -EAGAIN)
|
| 409 | break;
|
| 410 | } while (--try);
|
| 411 |
|
| 412 | if (ret == 0)
|
| 413 | return 0;
|
| 414 |
|
| 415 | ft260_i2c_reset(hdev);
|
| 416 | return -EIO;
|
| 417 | }
|
| 418 |
|
| 419 | static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
|
| 420 | int len, u8 flag)
|
| 421 | {
|
| 422 | int ret, wr_len, idx = 0;
|
| 423 | struct hid_device *hdev = dev->hdev;
|
| 424 | struct ft260_i2c_write_request_report *rep =
|
| 425 | (struct ft260_i2c_write_request_report *)dev->write_buf;
|
| 426 |
|
| 427 | if (len < 1)
|
| 428 | return -EINVAL;
|
| 429 |
|
| 430 | rep->flag = FT260_FLAG_START;
|
| 431 |
|
| 432 | do {
|
| 433 | if (len <= FT260_WR_DATA_MAX) {
|
| 434 | wr_len = len;
|
| 435 | if (flag == FT260_FLAG_START_STOP)
|
| 436 | rep->flag |= FT260_FLAG_STOP;
|
| 437 | } else {
|
| 438 | wr_len = FT260_WR_DATA_MAX;
|
| 439 | }
|
| 440 |
|
| 441 | rep->report = FT260_I2C_DATA_REPORT_ID(wr_len);
|
| 442 | rep->address = addr;
|
| 443 | rep->length = wr_len;
|
| 444 |
|
| 445 | memcpy(rep->data, &data[idx], wr_len);
|
| 446 |
|
| 447 | ft260_dbg("rep %#02x addr %#02x off %d len %d wlen %d flag %#x d[0] %#02x\n",
|
| 448 | rep->report, addr, idx, len, wr_len,
|
| 449 | rep->flag, data[0]);
|
| 450 |
|
| 451 | ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
|
| 452 | wr_len + 4);
|
| 453 | if (ret < 0) {
|
| 454 | hid_err(hdev, "%s: failed with %d\n", __func__, ret);
|
| 455 | return ret;
|
| 456 | }
|
| 457 |
|
| 458 | len -= wr_len;
|
| 459 | idx += wr_len;
|
| 460 | rep->flag = 0;
|
| 461 |
|
| 462 | } while (len > 0);
|
| 463 |
|
| 464 | return 0;
|
| 465 | }
|
| 466 |
|
| 467 | static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
|
| 468 | u8 *data, u8 data_len, u8 flag)
|
| 469 | {
|
| 470 | int ret = 0;
|
| 471 | int len = 4;
|
| 472 |
|
| 473 | struct ft260_i2c_write_request_report *rep =
|
| 474 | (struct ft260_i2c_write_request_report *)dev->write_buf;
|
| 475 |
|
| 476 | if (data_len >= sizeof(rep->data))
|
| 477 | return -EINVAL;
|
| 478 |
|
| 479 | rep->address = addr;
|
| 480 | rep->data[0] = cmd;
|
| 481 | rep->length = data_len + 1;
|
| 482 | rep->flag = flag;
|
| 483 | len += rep->length;
|
| 484 |
|
| 485 | rep->report = FT260_I2C_DATA_REPORT_ID(len);
|
| 486 |
|
| 487 | if (data_len > 0)
|
| 488 | memcpy(&rep->data[1], data, data_len);
|
| 489 |
|
| 490 | ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
|
| 491 | rep->report, addr, cmd, rep->length, len);
|
| 492 |
|
| 493 | ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
|
| 494 |
|
| 495 | return ret;
|
| 496 | }
|
| 497 |
|
| 498 | static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
|
| 499 | u16 len, u8 flag)
|
| 500 | {
|
| 501 | u16 rd_len;
|
| 502 | u16 rd_data_max = 60;
|
| 503 | int timeout, ret = 0;
|
| 504 | struct ft260_i2c_read_request_report rep;
|
| 505 | struct hid_device *hdev = dev->hdev;
|
| 506 | unsigned long irqflags;
|
| 507 | u8 bus_busy = 0;
|
| 508 | |
| 509 | * STOP terminates the last chunk; clear means hold the bus so a
|
| 510 | * follow-up call continues the same I2C transaction.
|
| 511 |
|
| 512 | bool want_stop = !!(flag & FT260_FLAG_STOP);
|
| 513 |
|
| 514 | if ((flag & FT260_FLAG_START_REPEATED) == FT260_FLAG_START_REPEATED)
|
| 515 | flag = FT260_FLAG_START_REPEATED;
|
| 516 | else if (flag & FT260_FLAG_START)
|
| 517 | flag = FT260_FLAG_START;
|
| 518 | else
|
| 519 | flag = 0;
|
| 520 | do {
|
| 521 | if (len <= rd_data_max) {
|
| 522 | rd_len = len;
|
| 523 | if (want_stop)
|
| 524 | flag |= FT260_FLAG_STOP;
|
| 525 | } else {
|
| 526 | rd_len = rd_data_max;
|
| 527 | }
|
| 528 | rd_data_max = FT260_RD_DATA_MAX;
|
| 529 |
|
| 530 | rep.report = FT260_I2C_READ_REQ;
|
| 531 | rep.length = cpu_to_le16(rd_len);
|
| 532 | rep.address = addr;
|
| 533 | rep.flag = flag;
|
| 534 |
|
| 535 | ft260_dbg("rep %#02x addr %#02x len %d rlen %d flag %#x\n",
|
| 536 | rep.report, rep.address, len, rd_len, flag);
|
| 537 |
|
| 538 | reinit_completion(&dev->wait);
|
| 539 |
|
| 540 | spin_lock_irqsave(&dev->read_lock, irqflags);
|
| 541 | dev->read_idx = 0;
|
| 542 | dev->read_buf = data;
|
| 543 | dev->read_len = rd_len;
|
| 544 | spin_unlock_irqrestore(&dev->read_lock, irqflags);
|
| 545 |
|
| 546 | ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
|
| 547 | if (ret < 0) {
|
| 548 | hid_err(hdev, "%s: failed with %d\n", __func__, ret);
|
| 549 | goto ft260_i2c_read_exit;
|
| 550 | }
|
| 551 |
|
| 552 | timeout = msecs_to_jiffies(5000);
|
| 553 | if (!wait_for_completion_timeout(&dev->wait, timeout)) {
|
| 554 | ret = -ETIMEDOUT;
|
| 555 | ft260_i2c_reset(hdev);
|
| 556 | goto ft260_i2c_read_exit;
|
| 557 | }
|
| 558 |
|
| 559 | spin_lock_irqsave(&dev->read_lock, irqflags);
|
| 560 | dev->read_buf = NULL;
|
| 561 | spin_unlock_irqrestore(&dev->read_lock, irqflags);
|
| 562 |
|
| 563 | if (flag & FT260_FLAG_STOP)
|
| 564 | bus_busy = FT260_I2C_STATUS_BUS_BUSY;
|
| 565 |
|
| 566 | ret = ft260_xfer_status(dev, bus_busy);
|
| 567 | if (ret < 0) {
|
| 568 | ret = -EIO;
|
| 569 | ft260_i2c_reset(hdev);
|
| 570 | goto ft260_i2c_read_exit;
|
| 571 | }
|
| 572 |
|
| 573 | len -= rd_len;
|
| 574 | data += rd_len;
|
| 575 | flag = 0;
|
| 576 |
|
| 577 | } while (len > 0);
|
| 578 |
|
| 579 | ft260_i2c_read_exit:
|
| 580 | spin_lock_irqsave(&dev->read_lock, irqflags);
|
| 581 | dev->read_buf = NULL;
|
| 582 | spin_unlock_irqrestore(&dev->read_lock, irqflags);
|
| 583 | return ret;
|
| 584 | }
|
| 585 |
|
| 586 | |
| 587 | * A random read operation is implemented as a dummy write operation, followed
|
| 588 | * by a current address read operation. The dummy write operation is used to
|
| 589 | * load the target byte address into the current byte address counter, from
|
| 590 | * which the subsequent current address read operation then reads.
|
| 591 |
|
| 592 | static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
|
| 593 | {
|
| 594 | int ret;
|
| 595 | int wr_len = msgs[0].len;
|
| 596 | int rd_len = msgs[1].len;
|
| 597 | struct hid_device *hdev = dev->hdev;
|
| 598 | u8 addr = msgs[0].addr;
|
| 599 | u16 read_off = 0;
|
| 600 |
|
| 601 | if (wr_len > 2) {
|
| 602 | hid_err(hdev, "%s: invalid wr_len: %d\n", __func__, wr_len);
|
| 603 | return -EOPNOTSUPP;
|
| 604 | }
|
| 605 |
|
| 606 | if (ft260_debug) {
|
| 607 | if (wr_len == 2)
|
| 608 | read_off = be16_to_cpu(*(__be16 *)msgs[0].buf);
|
| 609 | else
|
| 610 | read_off = *msgs[0].buf;
|
| 611 |
|
| 612 | pr_info("%s: off %#x rlen %d wlen %d\n", __func__,
|
| 613 | read_off, rd_len, wr_len);
|
| 614 | }
|
| 615 |
|
| 616 | ret = ft260_i2c_write(dev, addr, msgs[0].buf, wr_len,
|
| 617 | FT260_FLAG_START);
|
| 618 | if (ret < 0)
|
| 619 | return ret;
|
| 620 |
|
| 621 | ret = ft260_i2c_read(dev, addr, msgs[1].buf, rd_len,
|
| 622 | FT260_FLAG_START_STOP_REPEATED);
|
| 623 | if (ret < 0)
|
| 624 | return ret;
|
| 625 |
|
| 626 | return 0;
|
| 627 | }
|
| 628 |
|
| 629 | static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
|
| 630 | int num)
|
| 631 | {
|
| 632 | int ret;
|
| 633 | struct ft260_device *dev = i2c_get_adapdata(adapter);
|
| 634 | struct hid_device *hdev = dev->hdev;
|
| 635 |
|
| 636 | mutex_lock(&dev->lock);
|
| 637 |
|
| 638 | ret = hid_hw_power(hdev, PM_HINT_FULLON);
|
| 639 | if (ret < 0) {
|
| 640 | hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
|
| 641 | mutex_unlock(&dev->lock);
|
| 642 | return ret;
|
| 643 | }
|
| 644 |
|
| 645 | if (num == 1) {
|
| 646 | if (msgs->flags & I2C_M_RD)
|
| 647 | ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
|
| 648 | msgs->len, FT260_FLAG_START_STOP);
|
| 649 | else
|
| 650 | ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
|
| 651 | msgs->len, FT260_FLAG_START_STOP);
|
| 652 | if (ret < 0)
|
| 653 | goto i2c_exit;
|
| 654 |
|
| 655 | } else {
|
| 656 |
|
| 657 | ret = ft260_i2c_write_read(dev, msgs);
|
| 658 | if (ret < 0)
|
| 659 | goto i2c_exit;
|
| 660 | }
|
| 661 |
|
| 662 | ret = num;
|
| 663 | i2c_exit:
|
| 664 | hid_hw_power(hdev, PM_HINT_NORMAL);
|
| 665 | mutex_unlock(&dev->lock);
|
| 666 | return ret;
|
| 667 | }
|
| 668 |
|
| 669 | static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
|
| 670 | char read_write, u8 cmd, int size,
|
| 671 | union i2c_smbus_data *data)
|
| 672 | {
|
| 673 | int ret;
|
| 674 | struct ft260_device *dev = i2c_get_adapdata(adapter);
|
| 675 | struct hid_device *hdev = dev->hdev;
|
| 676 |
|
| 677 | ft260_dbg("smbus size %d\n", size);
|
| 678 |
|
| 679 | mutex_lock(&dev->lock);
|
| 680 |
|
| 681 | ret = hid_hw_power(hdev, PM_HINT_FULLON);
|
| 682 | if (ret < 0) {
|
| 683 | hid_err(hdev, "power management error: %d\n", ret);
|
| 684 | mutex_unlock(&dev->lock);
|
| 685 | return ret;
|
| 686 | }
|
| 687 |
|
| 688 | switch (size) {
|
| 689 | case I2C_SMBUS_BYTE:
|
| 690 | if (read_write == I2C_SMBUS_READ)
|
| 691 | ret = ft260_i2c_read(dev, addr, &data->byte, 1,
|
| 692 | FT260_FLAG_START_STOP);
|
| 693 | else
|
| 694 | ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
|
| 695 | FT260_FLAG_START_STOP);
|
| 696 | break;
|
| 697 | case I2C_SMBUS_BYTE_DATA:
|
| 698 | if (read_write == I2C_SMBUS_READ) {
|
| 699 | ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
|
| 700 | FT260_FLAG_START);
|
| 701 | if (ret)
|
| 702 | goto smbus_exit;
|
| 703 |
|
| 704 | ret = ft260_i2c_read(dev, addr, &data->byte, 1,
|
| 705 | FT260_FLAG_START_STOP_REPEATED);
|
| 706 | } else {
|
| 707 | ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
|
| 708 | FT260_FLAG_START_STOP);
|
| 709 | }
|
| 710 | break;
|
| 711 | case I2C_SMBUS_WORD_DATA:
|
| 712 | if (read_write == I2C_SMBUS_READ) {
|
| 713 | ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
|
| 714 | FT260_FLAG_START);
|
| 715 | if (ret)
|
| 716 | goto smbus_exit;
|
| 717 |
|
| 718 | ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
|
| 719 | FT260_FLAG_START_STOP_REPEATED);
|
| 720 | } else {
|
| 721 | ret = ft260_smbus_write(dev, addr, cmd,
|
| 722 | (u8 *)&data->word, 2,
|
| 723 | FT260_FLAG_START_STOP);
|
| 724 | }
|
| 725 | break;
|
| 726 | case I2C_SMBUS_BLOCK_DATA:
|
| 727 | if (read_write == I2C_SMBUS_READ) {
|
| 728 | u8 count = 0;
|
| 729 |
|
| 730 | |
| 731 | * SMBus 2.0 section 6.5.7 block read in one I2C
|
| 732 | * transaction:
|
| 733 | *
|
| 734 | * S Addr+Wr A Reg A Sr Addr+Rd A Count A Data... P
|
| 735 | *
|
| 736 | * The count is read separately and validated
|
| 737 | * before sizing the data read so a misbehaving
|
| 738 | * slave cannot drive a write past data->block[].
|
| 739 |
|
| 740 | ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
|
| 741 | FT260_FLAG_START);
|
| 742 | if (ret)
|
| 743 | goto smbus_exit;
|
| 744 |
|
| 745 | ret = ft260_i2c_read(dev, addr, &count, 1,
|
| 746 | FT260_FLAG_START_REPEATED);
|
| 747 | if (ret)
|
| 748 | goto smbus_exit;
|
| 749 |
|
| 750 | if (count == 0 || count > I2C_SMBUS_BLOCK_MAX) {
|
| 751 | hid_warn(hdev,
|
| 752 | "smbus block read: invalid count %u from slave 0x%02x\n",
|
| 753 | count, addr);
|
| 754 | ft260_i2c_reset(hdev);
|
| 755 | ret = -EPROTO;
|
| 756 | goto smbus_exit;
|
| 757 | }
|
| 758 |
|
| 759 | data->block[0] = count;
|
| 760 |
|
| 761 | ret = ft260_i2c_read(dev, addr, data->block + 1,
|
| 762 | count, FT260_FLAG_STOP);
|
| 763 | } else {
|
| 764 | ret = ft260_smbus_write(dev, addr, cmd, data->block,
|
| 765 | data->block[0] + 1,
|
| 766 | FT260_FLAG_START_STOP);
|
| 767 | }
|
| 768 | break;
|
| 769 | case I2C_SMBUS_I2C_BLOCK_DATA:
|
| 770 | if (read_write == I2C_SMBUS_READ) {
|
| 771 | ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
|
| 772 | FT260_FLAG_START);
|
| 773 | if (ret)
|
| 774 | goto smbus_exit;
|
| 775 |
|
| 776 | ret = ft260_i2c_read(dev, addr, data->block + 1,
|
| 777 | data->block[0],
|
| 778 | FT260_FLAG_START_STOP_REPEATED);
|
| 779 | } else {
|
| 780 | ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
|
| 781 | data->block[0],
|
| 782 | FT260_FLAG_START_STOP);
|
| 783 | }
|
| 784 | break;
|
| 785 | default:
|
| 786 | hid_err(hdev, "unsupported smbus transaction size %d\n", size);
|
| 787 | ret = -EOPNOTSUPP;
|
| 788 | }
|
| 789 |
|
| 790 | smbus_exit:
|
| 791 | hid_hw_power(hdev, PM_HINT_NORMAL);
|
| 792 | mutex_unlock(&dev->lock);
|
| 793 | return ret;
|
| 794 | }
|
| 795 |
|
| 796 | static u32 ft260_functionality(struct i2c_adapter *adap)
|
| 797 | {
|
| 798 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE |
|
| 799 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
|
| 800 | I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
|
| 801 | }
|
| 802 |
|
| 803 | static const struct i2c_adapter_quirks ft260_i2c_quirks = {
|
| 804 | .flags = I2C_AQ_COMB_WRITE_THEN_READ,
|
| 805 | .max_comb_1st_msg_len = 2,
|
| 806 | };
|
| 807 |
|
| 808 | static const struct i2c_algorithm ft260_i2c_algo = {
|
| 809 | .master_xfer = ft260_i2c_xfer,
|
| 810 | .smbus_xfer = ft260_smbus_xfer,
|
| 811 | .functionality = ft260_functionality,
|
| 812 | };
|
| 813 |
|
| 814 | static int ft260_get_system_config(struct hid_device *hdev,
|
| 815 | struct ft260_get_system_status_report *cfg)
|
| 816 | {
|
| 817 | int ret;
|
| 818 | int len = sizeof(struct ft260_get_system_status_report);
|
| 819 |
|
| 820 | ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
|
| 821 | (u8 *)cfg, len);
|
| 822 | if (ret < 0) {
|
| 823 | hid_err(hdev, "failed to retrieve system status\n");
|
| 824 | return ret;
|
| 825 | }
|
| 826 | return 0;
|
| 827 | }
|
| 828 |
|
| 829 | static int ft260_is_interface_enabled(struct hid_device *hdev)
|
| 830 | {
|
| 831 | struct ft260_get_system_status_report cfg;
|
| 832 | struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
|
| 833 | int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
|
| 834 | int ret;
|
| 835 |
|
| 836 | ret = ft260_get_system_config(hdev, &cfg);
|
| 837 | if (ret < 0)
|
| 838 | return ret;
|
| 839 |
|
| 840 | ft260_dbg("interface: 0x%02x\n", interface);
|
| 841 | ft260_dbg("chip mode: 0x%02x\n", cfg.chip_mode);
|
| 842 | ft260_dbg("clock_ctl: 0x%02x\n", cfg.clock_ctl);
|
| 843 | ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
|
| 844 | ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
|
| 845 |
|
| 846 | switch (cfg.chip_mode) {
|
| 847 | case FT260_MODE_ALL:
|
| 848 | case FT260_MODE_BOTH:
|
| 849 | if (interface == 1)
|
| 850 | hid_info(hdev, "uart interface is not supported\n");
|
| 851 | else
|
| 852 | ret = 1;
|
| 853 | break;
|
| 854 | case FT260_MODE_UART:
|
| 855 | hid_info(hdev, "uart interface is not supported\n");
|
| 856 | break;
|
| 857 | case FT260_MODE_I2C:
|
| 858 | ret = 1;
|
| 859 | break;
|
| 860 | }
|
| 861 | return ret;
|
| 862 | }
|
| 863 |
|
| 864 | static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
|
| 865 | u8 *field, u8 *buf)
|
| 866 | {
|
| 867 | int ret;
|
| 868 |
|
| 869 | ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
|
| 870 | if (ret < 0)
|
| 871 | return ret;
|
| 872 |
|
| 873 | return scnprintf(buf, PAGE_SIZE, "%d\n", *field);
|
| 874 | }
|
| 875 |
|
| 876 | static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
|
| 877 | __le16 *field, u8 *buf)
|
| 878 | {
|
| 879 | int ret;
|
| 880 |
|
| 881 | ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
|
| 882 | if (ret < 0)
|
| 883 | return ret;
|
| 884 |
|
| 885 | return scnprintf(buf, PAGE_SIZE, "%d\n", le16_to_cpu(*field));
|
| 886 | }
|
| 887 |
|
| 888 | #define FT260_ATTR_SHOW(name, reptype, id, type, func) \
|
| 889 | static ssize_t name##_show(struct device *kdev, \
|
| 890 | struct device_attribute *attr, char *buf) \
|
| 891 | { \
|
| 892 | struct reptype rep; \
|
| 893 | struct hid_device *hdev = to_hid_device(kdev); \
|
| 894 | type *field = &rep.name; \
|
| 895 | int len = sizeof(rep); \
|
| 896 | \
|
| 897 | return func(hdev, id, (u8 *)&rep, len, field, buf); \
|
| 898 | }
|
| 899 |
|
| 900 | #define FT260_SSTAT_ATTR_SHOW(name) \
|
| 901 | FT260_ATTR_SHOW(name, ft260_get_system_status_report, \
|
| 902 | FT260_SYSTEM_SETTINGS, u8, ft260_byte_show)
|
| 903 |
|
| 904 | #define FT260_I2CST_ATTR_SHOW(name) \
|
| 905 | FT260_ATTR_SHOW(name, ft260_get_i2c_status_report, \
|
| 906 | FT260_I2C_STATUS, __le16, ft260_word_show)
|
| 907 |
|
| 908 | #define FT260_ATTR_STORE(name, reptype, id, req, type, ctype, func) \
|
| 909 | static ssize_t name##_store(struct device *kdev, \
|
| 910 | struct device_attribute *attr, \
|
| 911 | const char *buf, size_t count) \
|
| 912 | { \
|
| 913 | struct reptype rep; \
|
| 914 | struct hid_device *hdev = to_hid_device(kdev); \
|
| 915 | type name; \
|
| 916 | int ret; \
|
| 917 | \
|
| 918 | if (!func(buf, 10, (ctype *)&name)) { \
|
| 919 | rep.name = name; \
|
| 920 | rep.report = id; \
|
| 921 | rep.request = req; \
|
| 922 | ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, \
|
| 923 | sizeof(rep)); \
|
| 924 | if (!ret) \
|
| 925 | ret = count; \
|
| 926 | } else { \
|
| 927 | ret = -EINVAL; \
|
| 928 | } \
|
| 929 | return ret; \
|
| 930 | }
|
| 931 |
|
| 932 | #define FT260_BYTE_ATTR_STORE(name, reptype, req) \
|
| 933 | FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
|
| 934 | u8, u8, kstrtou8)
|
| 935 |
|
| 936 | #define FT260_WORD_ATTR_STORE(name, reptype, req) \
|
| 937 | FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
|
| 938 | __le16, u16, kstrtou16)
|
| 939 |
|
| 940 | FT260_SSTAT_ATTR_SHOW(chip_mode);
|
| 941 | static DEVICE_ATTR_RO(chip_mode);
|
| 942 |
|
| 943 | FT260_SSTAT_ATTR_SHOW(pwren_status);
|
| 944 | static DEVICE_ATTR_RO(pwren_status);
|
| 945 |
|
| 946 | FT260_SSTAT_ATTR_SHOW(suspend_status);
|
| 947 | static DEVICE_ATTR_RO(suspend_status);
|
| 948 |
|
| 949 | FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
|
| 950 | static DEVICE_ATTR_RO(hid_over_i2c_en);
|
| 951 |
|
| 952 | FT260_SSTAT_ATTR_SHOW(power_saving_en);
|
| 953 | static DEVICE_ATTR_RO(power_saving_en);
|
| 954 |
|
| 955 | FT260_SSTAT_ATTR_SHOW(i2c_enable);
|
| 956 | FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
|
| 957 | FT260_SET_I2C_MODE);
|
| 958 | static DEVICE_ATTR_RW(i2c_enable);
|
| 959 |
|
| 960 | FT260_SSTAT_ATTR_SHOW(uart_mode);
|
| 961 | FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
|
| 962 | FT260_SET_UART_MODE);
|
| 963 | static DEVICE_ATTR_RW(uart_mode);
|
| 964 |
|
| 965 | FT260_SSTAT_ATTR_SHOW(clock_ctl);
|
| 966 | FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
|
| 967 | FT260_SET_CLOCK);
|
| 968 | static DEVICE_ATTR_RW(clock_ctl);
|
| 969 |
|
| 970 | FT260_I2CST_ATTR_SHOW(clock);
|
| 971 | FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
|
| 972 | FT260_SET_I2C_CLOCK_SPEED);
|
| 973 | static DEVICE_ATTR_RW(clock);
|
| 974 |
|
| 975 | static ssize_t i2c_reset_store(struct device *kdev,
|
| 976 | struct device_attribute *attr, const char *buf,
|
| 977 | size_t count)
|
| 978 | {
|
| 979 | struct hid_device *hdev = to_hid_device(kdev);
|
| 980 | int ret = ft260_i2c_reset(hdev);
|
| 981 |
|
| 982 | if (ret)
|
| 983 | return ret;
|
| 984 | return count;
|
| 985 | }
|
| 986 | static DEVICE_ATTR_WO(i2c_reset);
|
| 987 |
|
| 988 | static const struct attribute_group ft260_attr_group = {
|
| 989 | .attrs = (struct attribute *[]) {
|
| 990 | &dev_attr_chip_mode.attr,
|
| 991 | &dev_attr_pwren_status.attr,
|
| 992 | &dev_attr_suspend_status.attr,
|
| 993 | &dev_attr_hid_over_i2c_en.attr,
|
| 994 | &dev_attr_power_saving_en.attr,
|
| 995 | &dev_attr_i2c_enable.attr,
|
| 996 | &dev_attr_uart_mode.attr,
|
| 997 | &dev_attr_clock_ctl.attr,
|
| 998 | &dev_attr_i2c_reset.attr,
|
| 999 | &dev_attr_clock.attr,
|
| 1000 | NULL
|
| 1001 | }
|
| 1002 | };
|
| 1003 |
|
| 1004 | static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
|
| 1005 | {
|
| 1006 | struct ft260_device *dev;
|
| 1007 | struct ft260_get_chip_version_report version;
|
| 1008 | int ret;
|
| 1009 |
|
| 1010 | if (!hid_is_usb(hdev))
|
| 1011 | return -EINVAL;
|
| 1012 |
|
| 1013 | dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
|
| 1014 | if (!dev)
|
| 1015 | return -ENOMEM;
|
| 1016 |
|
| 1017 | ret = hid_parse(hdev);
|
| 1018 | if (ret) {
|
| 1019 | hid_err(hdev, "failed to parse HID\n");
|
| 1020 | return ret;
|
| 1021 | }
|
| 1022 |
|
| 1023 | ret = hid_hw_start(hdev, 0);
|
| 1024 | if (ret) {
|
| 1025 | hid_err(hdev, "failed to start HID HW\n");
|
| 1026 | return ret;
|
| 1027 | }
|
| 1028 |
|
| 1029 | ret = hid_hw_open(hdev);
|
| 1030 | if (ret) {
|
| 1031 | hid_err(hdev, "failed to open HID HW\n");
|
| 1032 | goto err_hid_stop;
|
| 1033 | }
|
| 1034 |
|
| 1035 | ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
|
| 1036 | (u8 *)&version, sizeof(version));
|
| 1037 | if (ret < 0) {
|
| 1038 | hid_err(hdev, "failed to retrieve chip version\n");
|
| 1039 | goto err_hid_close;
|
| 1040 | }
|
| 1041 |
|
| 1042 | hid_info(hdev, "chip code: %02x%02x %02x%02x\n",
|
| 1043 | version.chip_code[0], version.chip_code[1],
|
| 1044 | version.chip_code[2], version.chip_code[3]);
|
| 1045 |
|
| 1046 | ret = ft260_is_interface_enabled(hdev);
|
| 1047 | if (ret <= 0)
|
| 1048 | goto err_hid_close;
|
| 1049 |
|
| 1050 | hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n",
|
| 1051 | hdev->version >> 8, hdev->version & 0xff, hdev->name,
|
| 1052 | hdev->phys);
|
| 1053 |
|
| 1054 | hid_set_drvdata(hdev, dev);
|
| 1055 | dev->hdev = hdev;
|
| 1056 | dev->adap.owner = THIS_MODULE;
|
| 1057 | dev->adap.class = I2C_CLASS_HWMON;
|
| 1058 | dev->adap.algo = &ft260_i2c_algo;
|
| 1059 | dev->adap.quirks = &ft260_i2c_quirks;
|
| 1060 | dev->adap.dev.parent = &hdev->dev;
|
| 1061 | snprintf(dev->adap.name, sizeof(dev->adap.name),
|
| 1062 | "FT260 usb-i2c bridge");
|
| 1063 |
|
| 1064 | mutex_init(&dev->lock);
|
| 1065 | spin_lock_init(&dev->read_lock);
|
| 1066 | init_completion(&dev->wait);
|
| 1067 |
|
| 1068 | ret = ft260_xfer_status(dev, FT260_I2C_STATUS_BUS_BUSY);
|
| 1069 | if (ret)
|
| 1070 | ft260_i2c_reset(hdev);
|
| 1071 |
|
| 1072 | i2c_set_adapdata(&dev->adap, dev);
|
| 1073 | ret = i2c_add_adapter(&dev->adap);
|
| 1074 | if (ret) {
|
| 1075 | hid_err(hdev, "failed to add i2c adapter\n");
|
| 1076 | goto err_hid_close;
|
| 1077 | }
|
| 1078 |
|
| 1079 | ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
|
| 1080 | if (ret < 0) {
|
| 1081 | hid_err(hdev, "failed to create sysfs attrs\n");
|
| 1082 | goto err_i2c_free;
|
| 1083 | }
|
| 1084 |
|
| 1085 | return 0;
|
| 1086 |
|
| 1087 | err_i2c_free:
|
| 1088 | i2c_del_adapter(&dev->adap);
|
| 1089 | err_hid_close:
|
| 1090 | hid_hw_close(hdev);
|
| 1091 | err_hid_stop:
|
| 1092 | hid_hw_stop(hdev);
|
| 1093 | return ret;
|
| 1094 | }
|
| 1095 |
|
| 1096 | static void ft260_remove(struct hid_device *hdev)
|
| 1097 | {
|
| 1098 | struct ft260_device *dev = hid_get_drvdata(hdev);
|
| 1099 |
|
| 1100 | if (!dev)
|
| 1101 | return;
|
| 1102 |
|
| 1103 | sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
|
| 1104 | i2c_del_adapter(&dev->adap);
|
| 1105 |
|
| 1106 | hid_hw_close(hdev);
|
| 1107 | hid_hw_stop(hdev);
|
| 1108 | }
|
| 1109 |
|
| 1110 | static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
|
| 1111 | u8 *data, int size)
|
| 1112 | {
|
| 1113 | struct ft260_device *dev = hid_get_drvdata(hdev);
|
| 1114 | struct ft260_i2c_input_report *xfer = (void *)data;
|
| 1115 | unsigned long irqflags;
|
| 1116 |
|
| 1117 | if (size < offsetof(struct ft260_i2c_input_report, data)) {
|
| 1118 | hid_err(hdev, "short report %d\n", size);
|
| 1119 | return -1;
|
| 1120 | }
|
| 1121 |
|
| 1122 | if (xfer->report >= FT260_I2C_REPORT_MIN &&
|
| 1123 | xfer->report <= FT260_I2C_REPORT_MAX) {
|
| 1124 | bool complete_read;
|
| 1125 |
|
| 1126 | ft260_dbg("i2c resp: rep %#02x len %d size %d\n",
|
| 1127 | xfer->report, xfer->length, size);
|
| 1128 |
|
| 1129 | if (xfer->length > size -
|
| 1130 | offsetof(struct ft260_i2c_input_report, data)) {
|
| 1131 | hid_err(hdev, "report %#02x: length %d exceeds HID report size\n",
|
| 1132 | xfer->report, xfer->length);
|
| 1133 | return -1;
|
| 1134 | }
|
| 1135 |
|
| 1136 | |
| 1137 | * Hold read_lock so a timed-out ft260_i2c_read() cannot
|
| 1138 | * clear read_buf between the NULL check and the memcpy.
|
| 1139 |
|
| 1140 | spin_lock_irqsave(&dev->read_lock, irqflags);
|
| 1141 |
|
| 1142 | if ((dev->read_buf == NULL) ||
|
| 1143 | (xfer->length > dev->read_len - dev->read_idx)) {
|
| 1144 | spin_unlock_irqrestore(&dev->read_lock, irqflags);
|
| 1145 | hid_err(hdev, "unexpected report %#02x, length %d\n",
|
| 1146 | xfer->report, xfer->length);
|
| 1147 | return -1;
|
| 1148 | }
|
| 1149 |
|
| 1150 | memcpy(&dev->read_buf[dev->read_idx], &xfer->data,
|
| 1151 | xfer->length);
|
| 1152 | dev->read_idx += xfer->length;
|
| 1153 | complete_read = dev->read_idx == dev->read_len;
|
| 1154 |
|
| 1155 | spin_unlock_irqrestore(&dev->read_lock, irqflags);
|
| 1156 |
|
| 1157 | if (complete_read)
|
| 1158 | complete(&dev->wait);
|
| 1159 |
|
| 1160 | } else {
|
| 1161 | hid_err(hdev, "unhandled report %#02x\n", xfer->report);
|
| 1162 | }
|
| 1163 | return 0;
|
| 1164 | }
|
| 1165 |
|
| 1166 | static struct hid_driver ft260_driver = {
|
| 1167 | .name = "ft260",
|
| 1168 | .id_table = ft260_devices,
|
| 1169 | .probe = ft260_probe,
|
| 1170 | .remove = ft260_remove,
|
| 1171 | .raw_event = ft260_raw_event,
|
| 1172 | };
|
| 1173 |
|
| 1174 | module_hid_driver(ft260_driver);
|
| 1175 | MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
|
| 1176 | MODULE_AUTHOR("Michael Zaidman <[email protected]>");
|
| 1177 | MODULE_LICENSE("GPL v2");
|