1 // SPDX-License-Identifier: GPL-2.0-only
3 * hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
5 * Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
8 * https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT260.pdf
12 #include <linux/hidraw.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
18 static int ft260_debug = 1;
20 static int ft260_debug;
22 module_param_named(debug, ft260_debug, int, 0600);
23 MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
25 #define ft260_dbg(format, arg...) \
28 pr_info("%s: " format, __func__, ##arg); \
31 #define FT260_REPORT_MAX_LENGTH (64)
32 #define FT260_I2C_DATA_REPORT_ID(len) (FT260_I2C_REPORT_MIN + (len - 1) / 4)
34 * The input report format assigns 62 bytes for the data payload, but ft260
35 * returns 60 and 2 in two separate transactions. To minimize transfer time
36 * in reading chunks mode, set the maximum read payload length to 60 bytes.
38 #define FT260_RD_DATA_MAX (60)
39 #define FT260_WR_DATA_MAX (60)
42 * Device interface configuration.
43 * The FT260 has 2 interfaces that are controlled by DCNF0 and DCNF1 pins.
44 * First implementes USB HID to I2C bridge function and
45 * second - USB HID to UART bridge function.
48 FT260_MODE_ALL = 0x00,
49 FT260_MODE_I2C = 0x01,
50 FT260_MODE_UART = 0x02,
51 FT260_MODE_BOTH = 0x03,
56 FT260_GET_RQST_TYPE = 0xA1,
57 FT260_GET_REPORT = 0x01,
58 FT260_SET_RQST_TYPE = 0x21,
59 FT260_SET_REPORT = 0x09,
63 /* Report IDs / Feature In */
65 FT260_CHIP_VERSION = 0xA0,
66 FT260_SYSTEM_SETTINGS = 0xA1,
67 FT260_I2C_STATUS = 0xC0,
68 FT260_I2C_READ_REQ = 0xC2,
69 FT260_I2C_REPORT_MIN = 0xD0,
70 FT260_I2C_REPORT_MAX = 0xDE,
72 FT260_UART_INTERRUPT_STATUS = 0xB1,
73 FT260_UART_STATUS = 0xE0,
74 FT260_UART_RI_DCD_STATUS = 0xE1,
75 FT260_UART_REPORT = 0xF0,
80 FT260_SET_CLOCK = 0x01,
81 FT260_SET_I2C_MODE = 0x02,
82 FT260_SET_UART_MODE = 0x03,
83 FT260_ENABLE_INTERRUPT = 0x05,
84 FT260_SELECT_GPIO2_FUNC = 0x06,
85 FT260_ENABLE_UART_DCD_RI = 0x07,
86 FT260_SELECT_GPIOA_FUNC = 0x08,
87 FT260_SELECT_GPIOG_FUNC = 0x09,
88 FT260_SET_INTERRUPT_TRIGGER = 0x0A,
89 FT260_SET_SUSPEND_OUT_POLAR = 0x0B,
90 FT260_ENABLE_UART_RI_WAKEUP = 0x0C,
91 FT260_SET_UART_RI_WAKEUP_CFG = 0x0D,
92 FT260_SET_I2C_RESET = 0x20,
93 FT260_SET_I2C_CLOCK_SPEED = 0x22,
94 FT260_SET_UART_RESET = 0x40,
95 FT260_SET_UART_CONFIG = 0x41,
96 FT260_SET_UART_BAUD_RATE = 0x42,
97 FT260_SET_UART_DATA_BIT = 0x43,
98 FT260_SET_UART_PARITY = 0x44,
99 FT260_SET_UART_STOP_BIT = 0x45,
100 FT260_SET_UART_BREAKING = 0x46,
101 FT260_SET_UART_XON_XOFF = 0x49,
104 /* Response codes in I2C status report */
106 FT260_I2C_STATUS_SUCCESS = 0x00,
107 FT260_I2C_STATUS_CTRL_BUSY = 0x01,
108 FT260_I2C_STATUS_ERROR = 0x02,
109 FT260_I2C_STATUS_ADDR_NO_ACK = 0x04,
110 FT260_I2C_STATUS_DATA_NO_ACK = 0x08,
111 FT260_I2C_STATUS_ARBITR_LOST = 0x10,
112 FT260_I2C_STATUS_CTRL_IDLE = 0x20,
113 FT260_I2C_STATUS_BUS_BUSY = 0x40,
116 /* I2C Conditions flags */
118 FT260_FLAG_NONE = 0x00,
119 FT260_FLAG_START = 0x02,
120 FT260_FLAG_START_REPEATED = 0x03,
121 FT260_FLAG_STOP = 0x04,
122 FT260_FLAG_START_STOP = 0x06,
123 FT260_FLAG_START_STOP_REPEATED = 0x07,
126 #define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
128 /* Feature In reports */
130 struct ft260_get_chip_version_report {
131 u8 report; /* FT260_CHIP_VERSION */
132 u8 chip_code[4]; /* FTDI chip identification code */
136 struct ft260_get_system_status_report {
137 u8 report; /* FT260_SYSTEM_SETTINGS */
138 u8 chip_mode; /* DCNF0 and DCNF1 status, bits 0-1 */
139 u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
140 u8 suspend_status; /* 0 - not suspended, 1 - suspended */
141 u8 pwren_status; /* 0 - FT260 is not ready, 1 - ready */
142 u8 i2c_enable; /* 0 - disabled, 1 - enabled */
143 u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
144 /* 3 - XON_XOFF, 4 - No flow control */
145 u8 hid_over_i2c_en; /* 0 - disabled, 1 - enabled */
146 u8 gpio2_function; /* 0 - GPIO, 1 - SUSPOUT, */
147 /* 2 - PWREN, 4 - TX_LED */
148 u8 gpioA_function; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
149 u8 gpioG_function; /* 0 - GPIO, 2 - PWREN, */
150 /* 5 - RX_LED, 6 - BCD_DET */
151 u8 suspend_out_pol; /* 0 - active-high, 1 - active-low */
152 u8 enable_wakeup_int; /* 0 - disabled, 1 - enabled */
153 u8 intr_cond; /* Interrupt trigger conditions */
154 u8 power_saving_en; /* 0 - disabled, 1 - enabled */
158 struct ft260_get_i2c_status_report {
159 u8 report; /* FT260_I2C_STATUS */
160 u8 bus_status; /* I2C bus status */
161 __le16 clock; /* I2C bus clock in range 60-3400 KHz */
165 /* Feature Out reports */
167 struct ft260_set_system_clock_report {
168 u8 report; /* FT260_SYSTEM_SETTINGS */
169 u8 request; /* FT260_SET_CLOCK */
170 u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
173 struct ft260_set_i2c_mode_report {
174 u8 report; /* FT260_SYSTEM_SETTINGS */
175 u8 request; /* FT260_SET_I2C_MODE */
176 u8 i2c_enable; /* 0 - disabled, 1 - enabled */
179 struct ft260_set_uart_mode_report {
180 u8 report; /* FT260_SYSTEM_SETTINGS */
181 u8 request; /* FT260_SET_UART_MODE */
182 u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
183 /* 3 - XON_XOFF, 4 - No flow control */
186 struct ft260_set_i2c_reset_report {
187 u8 report; /* FT260_SYSTEM_SETTINGS */
188 u8 request; /* FT260_SET_I2C_RESET */
191 struct ft260_set_i2c_speed_report {
192 u8 report; /* FT260_SYSTEM_SETTINGS */
193 u8 request; /* FT260_SET_I2C_CLOCK_SPEED */
194 __le16 clock; /* I2C bus clock in range 60-3400 KHz */
197 /* Data transfer reports */
199 struct ft260_i2c_write_request_report {
200 u8 report; /* FT260_I2C_REPORT */
201 u8 address; /* 7-bit I2C address */
202 u8 flag; /* I2C transaction condition */
203 u8 length; /* data payload length */
204 u8 data[60]; /* data payload */
207 struct ft260_i2c_read_request_report {
208 u8 report; /* FT260_I2C_READ_REQ */
209 u8 address; /* 7-bit I2C address */
210 u8 flag; /* I2C transaction condition */
211 __le16 length; /* data payload length */
214 struct ft260_i2c_input_report {
215 u8 report; /* FT260_I2C_REPORT */
216 u8 length; /* data payload length */
217 u8 data[2]; /* data payload */
220 static const struct hid_device_id ft260_devices[] = {
221 { HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
222 USB_DEVICE_ID_FT260) },
223 { /* END OF LIST */ }
225 MODULE_DEVICE_TABLE(hid, ft260_devices);
227 struct ft260_device {
228 struct i2c_adapter adap;
229 struct hid_device *hdev;
230 struct completion wait;
232 u8 write_buf[FT260_REPORT_MAX_LENGTH];
239 static int ft260_hid_feature_report_get(struct hid_device *hdev,
240 unsigned char report_id, u8 *data,
246 buf = kmalloc(len, GFP_KERNEL);
250 ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
252 memcpy(data, buf, len);
257 static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
263 buf = kmemdup(data, len, GFP_KERNEL);
267 buf[0] = FT260_SYSTEM_SETTINGS;
269 ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
276 static int ft260_i2c_reset(struct hid_device *hdev)
278 struct ft260_set_i2c_reset_report report;
281 report.request = FT260_SET_I2C_RESET;
283 ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
285 hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
293 static int ft260_xfer_status(struct ft260_device *dev)
295 struct hid_device *hdev = dev->hdev;
296 struct ft260_get_i2c_status_report report;
299 ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
300 (u8 *)&report, sizeof(report));
302 hid_err(hdev, "failed to retrieve status: %d\n", ret);
306 dev->clock = le16_to_cpu(report.clock);
307 ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
310 if (report.bus_status & FT260_I2C_STATUS_CTRL_BUSY)
313 if (report.bus_status & FT260_I2C_STATUS_BUS_BUSY)
316 if (report.bus_status & FT260_I2C_STATUS_ERROR)
321 if (report.bus_status & FT260_I2C_STATUS_ADDR_NO_ACK)
322 ft260_dbg("unacknowledged address\n");
324 if (report.bus_status & FT260_I2C_STATUS_DATA_NO_ACK)
325 ft260_dbg("unacknowledged data\n");
327 if (report.bus_status & FT260_I2C_STATUS_ARBITR_LOST)
328 ft260_dbg("arbitration loss\n");
330 if (report.bus_status & FT260_I2C_STATUS_CTRL_IDLE)
336 static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
342 buf = kmemdup(data, len, GFP_KERNEL);
346 ret = hid_hw_output_report(hdev, buf, len);
352 static int ft260_hid_output_report_check_status(struct ft260_device *dev,
355 int ret, usec, try = 3;
356 struct hid_device *hdev = dev->hdev;
358 ret = ft260_hid_output_report(hdev, data, len);
360 hid_err(hdev, "%s: failed to start transfer, ret %d\n",
362 ft260_i2c_reset(hdev);
366 /* transfer time = 1 / clock(KHz) * 10 bits * bytes */
367 usec = 10000 / dev->clock * len;
368 usleep_range(usec, usec + 100);
369 ft260_dbg("wait %d usec, len %d\n", usec, len);
371 ret = ft260_xfer_status(dev);
376 if (ret == 0 || ret == -EBUSY)
379 ft260_i2c_reset(hdev);
383 static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
384 int data_len, u8 flag)
386 int len, ret, idx = 0;
387 struct hid_device *hdev = dev->hdev;
388 struct ft260_i2c_write_request_report *rep =
389 (struct ft260_i2c_write_request_report *)dev->write_buf;
392 if (data_len <= FT260_WR_DATA_MAX)
395 len = FT260_WR_DATA_MAX;
397 rep->report = FT260_I2C_DATA_REPORT_ID(len);
402 memcpy(rep->data, &data[idx], len);
404 ft260_dbg("rep %#02x addr %#02x off %d len %d d[0] %#02x\n",
405 rep->report, addr, idx, len, data[0]);
407 ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
410 hid_err(hdev, "%s: failed to start transfer, ret %d\n",
418 } while (data_len > 0);
423 static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
424 u8 *data, u8 data_len, u8 flag)
429 struct ft260_i2c_write_request_report *rep =
430 (struct ft260_i2c_write_request_report *)dev->write_buf;
434 rep->length = data_len + 1;
438 rep->report = FT260_I2C_DATA_REPORT_ID(len);
441 memcpy(&rep->data[1], data, data_len);
443 ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
444 rep->report, addr, cmd, rep->length, len);
446 ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
451 static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
454 struct ft260_i2c_read_request_report rep;
455 struct hid_device *hdev = dev->hdev;
459 if (len > FT260_RD_DATA_MAX) {
460 hid_err(hdev, "%s: unsupported rd len: %d\n", __func__, len);
465 dev->read_buf = data;
468 rep.report = FT260_I2C_READ_REQ;
469 rep.length = cpu_to_le16(len);
473 ft260_dbg("rep %#02x addr %#02x len %d\n", rep.report, rep.address,
476 reinit_completion(&dev->wait);
478 ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
480 hid_err(hdev, "%s: failed to start transaction, ret %d\n",
485 timeout = msecs_to_jiffies(5000);
486 if (!wait_for_completion_timeout(&dev->wait, timeout)) {
487 ft260_i2c_reset(hdev);
491 ret = ft260_xfer_status(dev);
495 ft260_i2c_reset(hdev);
500 * A random read operation is implemented as a dummy write operation, followed
501 * by a current address read operation. The dummy write operation is used to
502 * load the target byte address into the current byte address counter, from
503 * which the subsequent current address read operation then reads.
505 static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
508 u16 left_len = msgs[1].len;
509 u8 *read_buf = msgs[1].buf;
510 u8 addr = msgs[0].addr;
512 struct hid_device *hdev = dev->hdev;
514 if (msgs[0].len > 2) {
515 hid_err(hdev, "%s: unsupported wr len: %d\n", __func__,
520 memcpy(&read_off, msgs[0].buf, msgs[0].len);
523 if (left_len <= FT260_RD_DATA_MAX)
526 len = FT260_RD_DATA_MAX;
528 ft260_dbg("read_off %#x left_len %d len %d\n", read_off,
531 ret = ft260_i2c_write(dev, addr, (u8 *)&read_off, msgs[0].len,
536 ret = ft260_i2c_read(dev, addr, read_buf, len,
537 FT260_FLAG_START_STOP);
545 } while (left_len > 0);
550 static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
554 struct ft260_device *dev = i2c_get_adapdata(adapter);
555 struct hid_device *hdev = dev->hdev;
557 mutex_lock(&dev->lock);
559 ret = hid_hw_power(hdev, PM_HINT_FULLON);
561 hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
562 mutex_unlock(&dev->lock);
567 if (msgs->flags & I2C_M_RD)
568 ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
569 msgs->len, FT260_FLAG_START_STOP);
571 ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
572 msgs->len, FT260_FLAG_START_STOP);
577 /* Combined write then read message */
578 ret = ft260_i2c_write_read(dev, msgs);
585 hid_hw_power(hdev, PM_HINT_NORMAL);
586 mutex_unlock(&dev->lock);
590 static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
591 char read_write, u8 cmd, int size,
592 union i2c_smbus_data *data)
595 struct ft260_device *dev = i2c_get_adapdata(adapter);
596 struct hid_device *hdev = dev->hdev;
598 ft260_dbg("smbus size %d\n", size);
600 mutex_lock(&dev->lock);
602 ret = hid_hw_power(hdev, PM_HINT_FULLON);
604 hid_err(hdev, "power management error: %d\n", ret);
605 mutex_unlock(&dev->lock);
610 case I2C_SMBUS_QUICK:
611 if (read_write == I2C_SMBUS_READ)
612 ret = ft260_i2c_read(dev, addr, &data->byte, 0,
613 FT260_FLAG_START_STOP);
615 ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
616 FT260_FLAG_START_STOP);
619 if (read_write == I2C_SMBUS_READ)
620 ret = ft260_i2c_read(dev, addr, &data->byte, 1,
621 FT260_FLAG_START_STOP);
623 ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
624 FT260_FLAG_START_STOP);
626 case I2C_SMBUS_BYTE_DATA:
627 if (read_write == I2C_SMBUS_READ) {
628 ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
633 ret = ft260_i2c_read(dev, addr, &data->byte, 1,
634 FT260_FLAG_START_STOP_REPEATED);
636 ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
637 FT260_FLAG_START_STOP);
640 case I2C_SMBUS_WORD_DATA:
641 if (read_write == I2C_SMBUS_READ) {
642 ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
647 ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
648 FT260_FLAG_START_STOP_REPEATED);
650 ret = ft260_smbus_write(dev, addr, cmd,
651 (u8 *)&data->word, 2,
652 FT260_FLAG_START_STOP);
655 case I2C_SMBUS_BLOCK_DATA:
656 if (read_write == I2C_SMBUS_READ) {
657 ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
662 ret = ft260_i2c_read(dev, addr, data->block,
664 FT260_FLAG_START_STOP_REPEATED);
666 ret = ft260_smbus_write(dev, addr, cmd, data->block,
668 FT260_FLAG_START_STOP);
671 case I2C_SMBUS_I2C_BLOCK_DATA:
672 if (read_write == I2C_SMBUS_READ) {
673 ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
678 ret = ft260_i2c_read(dev, addr, data->block + 1,
680 FT260_FLAG_START_STOP_REPEATED);
682 ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
684 FT260_FLAG_START_STOP);
688 hid_err(hdev, "unsupported smbus transaction size %d\n", size);
693 hid_hw_power(hdev, PM_HINT_NORMAL);
694 mutex_unlock(&dev->lock);
698 static u32 ft260_functionality(struct i2c_adapter *adap)
700 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_QUICK |
701 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
702 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
705 static const struct i2c_adapter_quirks ft260_i2c_quirks = {
706 .flags = I2C_AQ_COMB_WRITE_THEN_READ,
707 .max_comb_1st_msg_len = 2,
710 static const struct i2c_algorithm ft260_i2c_algo = {
711 .master_xfer = ft260_i2c_xfer,
712 .smbus_xfer = ft260_smbus_xfer,
713 .functionality = ft260_functionality,
716 static int ft260_get_system_config(struct hid_device *hdev,
717 struct ft260_get_system_status_report *cfg)
720 int len = sizeof(struct ft260_get_system_status_report);
722 ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
725 hid_err(hdev, "failed to retrieve system status\n");
732 static int ft260_is_interface_enabled(struct hid_device *hdev)
734 struct ft260_get_system_status_report cfg;
735 struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
736 int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
739 ret = ft260_get_system_config(hdev, &cfg);
743 ft260_dbg("interface: 0x%02x\n", interface);
744 ft260_dbg("chip mode: 0x%02x\n", cfg.chip_mode);
745 ft260_dbg("clock_ctl: 0x%02x\n", cfg.clock_ctl);
746 ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
747 ft260_dbg("uart_mode: 0x%02x\n", cfg.uart_mode);
749 switch (cfg.chip_mode) {
751 case FT260_MODE_BOTH:
752 if (interface == 1) {
753 hid_info(hdev, "uart interface is not supported\n");
758 case FT260_MODE_UART:
759 if (interface == 0) {
760 hid_info(hdev, "uart is unsupported on interface 0\n");
765 if (interface == 1) {
766 hid_info(hdev, "i2c is unsupported on interface 1\n");
774 static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
779 ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
780 if (ret != len && ret >= 0)
783 return scnprintf(buf, PAGE_SIZE, "%hi\n", *field);
786 static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
791 ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
792 if (ret != len && ret >= 0)
795 return scnprintf(buf, PAGE_SIZE, "%hi\n", le16_to_cpu(*field));
798 #define FT260_ATTR_SHOW(name, reptype, id, type, func) \
799 static ssize_t name##_show(struct device *kdev, \
800 struct device_attribute *attr, char *buf) \
802 struct reptype rep; \
803 struct hid_device *hdev = to_hid_device(kdev); \
804 type *field = &rep.name; \
805 int len = sizeof(rep); \
807 return func(hdev, id, (u8 *)&rep, len, field, buf); \
810 #define FT260_SSTAT_ATTR_SHOW(name) \
811 FT260_ATTR_SHOW(name, ft260_get_system_status_report, \
812 FT260_SYSTEM_SETTINGS, u8, ft260_byte_show)
814 #define FT260_I2CST_ATTR_SHOW(name) \
815 FT260_ATTR_SHOW(name, ft260_get_i2c_status_report, \
816 FT260_I2C_STATUS, u16, ft260_word_show)
818 #define FT260_ATTR_STORE(name, reptype, id, req, type, func) \
819 static ssize_t name##_store(struct device *kdev, \
820 struct device_attribute *attr, \
821 const char *buf, size_t count) \
823 struct reptype rep; \
824 struct hid_device *hdev = to_hid_device(kdev); \
828 if (!func(buf, 10, &name)) { \
832 ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep, \
842 #define FT260_BYTE_ATTR_STORE(name, reptype, req) \
843 FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
846 #define FT260_WORD_ATTR_STORE(name, reptype, req) \
847 FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req, \
850 FT260_SSTAT_ATTR_SHOW(chip_mode);
851 static DEVICE_ATTR_RO(chip_mode);
853 FT260_SSTAT_ATTR_SHOW(pwren_status);
854 static DEVICE_ATTR_RO(pwren_status);
856 FT260_SSTAT_ATTR_SHOW(suspend_status);
857 static DEVICE_ATTR_RO(suspend_status);
859 FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
860 static DEVICE_ATTR_RO(hid_over_i2c_en);
862 FT260_SSTAT_ATTR_SHOW(power_saving_en);
863 static DEVICE_ATTR_RO(power_saving_en);
865 FT260_SSTAT_ATTR_SHOW(i2c_enable);
866 FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
868 static DEVICE_ATTR_RW(i2c_enable);
870 FT260_SSTAT_ATTR_SHOW(uart_mode);
871 FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
872 FT260_SET_UART_MODE);
873 static DEVICE_ATTR_RW(uart_mode);
875 FT260_SSTAT_ATTR_SHOW(clock_ctl);
876 FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
878 static DEVICE_ATTR_RW(clock_ctl);
880 FT260_I2CST_ATTR_SHOW(clock);
881 FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
882 FT260_SET_I2C_CLOCK_SPEED);
883 static DEVICE_ATTR_RW(clock);
885 static ssize_t i2c_reset_store(struct device *kdev,
886 struct device_attribute *attr, const char *buf,
889 struct hid_device *hdev = to_hid_device(kdev);
890 int ret = ft260_i2c_reset(hdev);
896 static DEVICE_ATTR_WO(i2c_reset);
898 static const struct attribute_group ft260_attr_group = {
899 .attrs = (struct attribute *[]) {
900 &dev_attr_chip_mode.attr,
901 &dev_attr_pwren_status.attr,
902 &dev_attr_suspend_status.attr,
903 &dev_attr_hid_over_i2c_en.attr,
904 &dev_attr_power_saving_en.attr,
905 &dev_attr_i2c_enable.attr,
906 &dev_attr_uart_mode.attr,
907 &dev_attr_clock_ctl.attr,
908 &dev_attr_i2c_reset.attr,
909 &dev_attr_clock.attr,
914 static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
916 struct ft260_device *dev;
917 struct ft260_get_chip_version_report version;
920 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
924 ret = hid_parse(hdev);
926 hid_err(hdev, "failed to parse HID\n");
930 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
932 hid_err(hdev, "failed to start HID HW\n");
936 ret = hid_hw_open(hdev);
938 hid_err(hdev, "failed to open HID HW\n");
942 ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
943 (u8 *)&version, sizeof(version));
944 if (ret != sizeof(version)) {
945 hid_err(hdev, "failed to retrieve chip version\n");
951 hid_info(hdev, "chip code: %02x%02x %02x%02x\n",
952 version.chip_code[0], version.chip_code[1],
953 version.chip_code[2], version.chip_code[3]);
955 ret = ft260_is_interface_enabled(hdev);
959 hid_set_drvdata(hdev, dev);
961 dev->adap.owner = THIS_MODULE;
962 dev->adap.class = I2C_CLASS_HWMON;
963 dev->adap.algo = &ft260_i2c_algo;
964 dev->adap.quirks = &ft260_i2c_quirks;
965 dev->adap.dev.parent = &hdev->dev;
966 snprintf(dev->adap.name, sizeof(dev->adap.name),
967 "FT260 usb-i2c bridge on hidraw%d",
968 ((struct hidraw *)hdev->hidraw)->minor);
970 mutex_init(&dev->lock);
971 init_completion(&dev->wait);
973 ret = i2c_add_adapter(&dev->adap);
975 hid_err(hdev, "failed to add i2c adapter\n");
979 i2c_set_adapdata(&dev->adap, dev);
981 ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
983 hid_err(hdev, "failed to create sysfs attrs\n");
987 ret = ft260_xfer_status(dev);
989 ft260_i2c_reset(hdev);
994 i2c_del_adapter(&dev->adap);
1002 static void ft260_remove(struct hid_device *hdev)
1005 struct ft260_device *dev = hid_get_drvdata(hdev);
1007 ret = ft260_is_interface_enabled(hdev);
1011 sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
1012 i2c_del_adapter(&dev->adap);
1018 static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
1021 struct ft260_device *dev = hid_get_drvdata(hdev);
1022 struct ft260_i2c_input_report *xfer = (void *)data;
1024 if (xfer->report >= FT260_I2C_REPORT_MIN &&
1025 xfer->report <= FT260_I2C_REPORT_MAX) {
1026 ft260_dbg("i2c resp: rep %#02x len %d\n", xfer->report,
1029 memcpy(&dev->read_buf[dev->read_idx], &xfer->data,
1031 dev->read_idx += xfer->length;
1033 if (dev->read_idx == dev->read_len)
1034 complete(&dev->wait);
1037 hid_err(hdev, "unknown report: %#02x\n", xfer->report);
1043 static struct hid_driver ft260_driver = {
1045 .id_table = ft260_devices,
1046 .probe = ft260_probe,
1047 .remove = ft260_remove,
1048 .raw_event = ft260_raw_event,
1051 module_hid_driver(ft260_driver);
1052 MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
1053 MODULE_AUTHOR("Michael Zaidman <michael.zaidman@gmail.com>");
1054 MODULE_LICENSE("GPL v2");