linux/drivers/hid/hid-ft260.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * hid-ft260.c - FTDI FT260 USB HID to I2C host bridge
   4 *
   5 * Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
   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
  18static int ft260_debug = 1;
  19#else
  20static int ft260_debug;
  21#endif
  22module_param_named(debug, ft260_debug, int, 0600);
  23MODULE_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 * 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.
  37 */
  38#define FT260_RD_DATA_MAX (60)
  39#define FT260_WR_DATA_MAX (60)
  40
  41/*
  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.
  46 */
  47enum {
  48        FT260_MODE_ALL                  = 0x00,
  49        FT260_MODE_I2C                  = 0x01,
  50        FT260_MODE_UART                 = 0x02,
  51        FT260_MODE_BOTH                 = 0x03,
  52};
  53
  54/* Control pipe */
  55enum {
  56        FT260_GET_RQST_TYPE             = 0xA1,
  57        FT260_GET_REPORT                = 0x01,
  58        FT260_SET_RQST_TYPE             = 0x21,
  59        FT260_SET_REPORT                = 0x09,
  60        FT260_FEATURE                   = 0x03,
  61};
  62
  63/* Report IDs / Feature In */
  64enum {
  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,
  71        FT260_GPIO                      = 0xB0,
  72        FT260_UART_INTERRUPT_STATUS     = 0xB1,
  73        FT260_UART_STATUS               = 0xE0,
  74        FT260_UART_RI_DCD_STATUS        = 0xE1,
  75        FT260_UART_REPORT               = 0xF0,
  76};
  77
  78/* Feature Out */
  79enum {
  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,
 102};
 103
 104/* Response codes in I2C status report */
 105enum {
 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,
 114};
 115
 116/* I2C Conditions flags */
 117enum {
 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,
 124};
 125
 126#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | report_id)
 127
 128/* Feature In reports */
 129
 130struct ft260_get_chip_version_report {
 131        u8 report;              /* FT260_CHIP_VERSION */
 132        u8 chip_code[4];        /* FTDI chip identification code */
 133        u8 reserved[8];
 134} __packed;
 135
 136struct 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 */
 155        u8 reserved[10];
 156} __packed;
 157
 158struct 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 */
 162        u8 reserved;
 163} __packed;
 164
 165/* Feature Out reports */
 166
 167struct 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 */
 171} __packed;
 172
 173struct 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 */
 177} __packed;
 178
 179struct 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 */
 184} __packed;
 185
 186struct ft260_set_i2c_reset_report {
 187        u8 report;              /* FT260_SYSTEM_SETTINGS */
 188        u8 request;             /* FT260_SET_I2C_RESET */
 189} __packed;
 190
 191struct 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 */
 195} __packed;
 196
 197/* Data transfer reports */
 198
 199struct 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[FT260_WR_DATA_MAX]; /* data payload */
 205} __packed;
 206
 207struct 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 */
 212} __packed;
 213
 214struct ft260_i2c_input_report {
 215        u8 report;              /* FT260_I2C_REPORT */
 216        u8 length;              /* data payload length */
 217        u8 data[2];             /* data payload */
 218} __packed;
 219
 220static 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 */ }
 224};
 225MODULE_DEVICE_TABLE(hid, ft260_devices);
 226
 227struct ft260_device {
 228        struct i2c_adapter adap;
 229        struct hid_device *hdev;
 230        struct completion wait;
 231        struct mutex lock;
 232        u8 write_buf[FT260_REPORT_MAX_LENGTH];
 233        u8 *read_buf;
 234        u16 read_idx;
 235        u16 read_len;
 236        u16 clock;
 237};
 238
 239static int ft260_hid_feature_report_get(struct hid_device *hdev,
 240                                        unsigned char report_id, u8 *data,
 241                                        size_t len)
 242{
 243        u8 *buf;
 244        int ret;
 245
 246        buf = kmalloc(len, GFP_KERNEL);
 247        if (!buf)
 248                return -ENOMEM;
 249
 250        ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
 251                                 HID_REQ_GET_REPORT);
 252        if (likely(ret == len))
 253                memcpy(data, buf, len);
 254        else if (ret >= 0)
 255                ret = -EIO;
 256        kfree(buf);
 257        return ret;
 258}
 259
 260static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
 261                                        size_t len)
 262{
 263        u8 *buf;
 264        int ret;
 265
 266        buf = kmemdup(data, len, GFP_KERNEL);
 267        if (!buf)
 268                return -ENOMEM;
 269
 270        buf[0] = FT260_SYSTEM_SETTINGS;
 271
 272        ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
 273                                 HID_REQ_SET_REPORT);
 274
 275        kfree(buf);
 276        return ret;
 277}
 278
 279static int ft260_i2c_reset(struct hid_device *hdev)
 280{
 281        struct ft260_set_i2c_reset_report report;
 282        int ret;
 283
 284        report.request = FT260_SET_I2C_RESET;
 285
 286        ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
 287        if (ret < 0) {
 288                hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
 289                return ret;
 290        }
 291
 292        ft260_dbg("done\n");
 293        return ret;
 294}
 295
 296static int ft260_xfer_status(struct ft260_device *dev)
 297{
 298        struct hid_device *hdev = dev->hdev;
 299        struct ft260_get_i2c_status_report report;
 300        int ret;
 301
 302        ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
 303                                           (u8 *)&report, sizeof(report));
 304        if (unlikely(ret < 0)) {
 305                hid_err(hdev, "failed to retrieve status: %d\n", ret);
 306                return ret;
 307        }
 308
 309        dev->clock = le16_to_cpu(report.clock);
 310        ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
 311                  dev->clock);
 312
 313        if (report.bus_status & FT260_I2C_STATUS_CTRL_BUSY)
 314                return -EAGAIN;
 315
 316        if (report.bus_status & FT260_I2C_STATUS_BUS_BUSY)
 317                return -EBUSY;
 318
 319        if (report.bus_status & FT260_I2C_STATUS_ERROR)
 320                return -EIO;
 321
 322        ret = -EIO;
 323
 324        if (report.bus_status & FT260_I2C_STATUS_ADDR_NO_ACK)
 325                ft260_dbg("unacknowledged address\n");
 326
 327        if (report.bus_status & FT260_I2C_STATUS_DATA_NO_ACK)
 328                ft260_dbg("unacknowledged data\n");
 329
 330        if (report.bus_status & FT260_I2C_STATUS_ARBITR_LOST)
 331                ft260_dbg("arbitration loss\n");
 332
 333        if (report.bus_status & FT260_I2C_STATUS_CTRL_IDLE)
 334                ret = 0;
 335
 336        return ret;
 337}
 338
 339static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
 340                                   size_t len)
 341{
 342        u8 *buf;
 343        int ret;
 344
 345        buf = kmemdup(data, len, GFP_KERNEL);
 346        if (!buf)
 347                return -ENOMEM;
 348
 349        ret = hid_hw_output_report(hdev, buf, len);
 350
 351        kfree(buf);
 352        return ret;
 353}
 354
 355static int ft260_hid_output_report_check_status(struct ft260_device *dev,
 356                                                u8 *data, int len)
 357{
 358        int ret, usec, try = 3;
 359        struct hid_device *hdev = dev->hdev;
 360
 361        ret = ft260_hid_output_report(hdev, data, len);
 362        if (ret < 0) {
 363                hid_err(hdev, "%s: failed to start transfer, ret %d\n",
 364                        __func__, ret);
 365                ft260_i2c_reset(hdev);
 366                return ret;
 367        }
 368
 369        /* transfer time = 1 / clock(KHz) * 10 bits * bytes */
 370        usec = 10000 / dev->clock * len;
 371        usleep_range(usec, usec + 100);
 372        ft260_dbg("wait %d usec, len %d\n", usec, len);
 373        do {
 374                ret = ft260_xfer_status(dev);
 375                if (ret != -EAGAIN)
 376                        break;
 377        } while (--try);
 378
 379        if (ret == 0 || ret == -EBUSY)
 380                return 0;
 381
 382        ft260_i2c_reset(hdev);
 383        return -EIO;
 384}
 385
 386static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
 387                           int data_len, u8 flag)
 388{
 389        int len, ret, idx = 0;
 390        struct hid_device *hdev = dev->hdev;
 391        struct ft260_i2c_write_request_report *rep =
 392                (struct ft260_i2c_write_request_report *)dev->write_buf;
 393
 394        do {
 395                if (data_len <= FT260_WR_DATA_MAX)
 396                        len = data_len;
 397                else
 398                        len = FT260_WR_DATA_MAX;
 399
 400                rep->report = FT260_I2C_DATA_REPORT_ID(len);
 401                rep->address = addr;
 402                rep->length = len;
 403                rep->flag = flag;
 404
 405                memcpy(rep->data, &data[idx], len);
 406
 407                ft260_dbg("rep %#02x addr %#02x off %d len %d d[0] %#02x\n",
 408                          rep->report, addr, idx, len, data[0]);
 409
 410                ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
 411                                                           len + 4);
 412                if (ret < 0) {
 413                        hid_err(hdev, "%s: failed to start transfer, ret %d\n",
 414                                __func__, ret);
 415                        return ret;
 416                }
 417
 418                data_len -= len;
 419                idx += len;
 420
 421        } while (data_len > 0);
 422
 423        return 0;
 424}
 425
 426static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
 427                             u8 *data, u8 data_len, u8 flag)
 428{
 429        int ret = 0;
 430        int len = 4;
 431
 432        struct ft260_i2c_write_request_report *rep =
 433                (struct ft260_i2c_write_request_report *)dev->write_buf;
 434
 435        if (data_len >= sizeof(rep->data))
 436                return -EINVAL;
 437
 438        rep->address = addr;
 439        rep->data[0] = cmd;
 440        rep->length = data_len + 1;
 441        rep->flag = flag;
 442        len += rep->length;
 443
 444        rep->report = FT260_I2C_DATA_REPORT_ID(len);
 445
 446        if (data_len > 0)
 447                memcpy(&rep->data[1], data, data_len);
 448
 449        ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
 450                  rep->report, addr, cmd, rep->length, len);
 451
 452        ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
 453
 454        return ret;
 455}
 456
 457static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
 458                          u16 len, u8 flag)
 459{
 460        struct ft260_i2c_read_request_report rep;
 461        struct hid_device *hdev = dev->hdev;
 462        int timeout;
 463        int ret;
 464
 465        if (len > FT260_RD_DATA_MAX) {
 466                hid_err(hdev, "%s: unsupported rd len: %d\n", __func__, len);
 467                return -EINVAL;
 468        }
 469
 470        dev->read_idx = 0;
 471        dev->read_buf = data;
 472        dev->read_len = len;
 473
 474        rep.report = FT260_I2C_READ_REQ;
 475        rep.length = cpu_to_le16(len);
 476        rep.address = addr;
 477        rep.flag = flag;
 478
 479        ft260_dbg("rep %#02x addr %#02x len %d\n", rep.report, rep.address,
 480                  rep.length);
 481
 482        reinit_completion(&dev->wait);
 483
 484        ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
 485        if (ret < 0) {
 486                hid_err(hdev, "%s: failed to start transaction, ret %d\n",
 487                        __func__, ret);
 488                return ret;
 489        }
 490
 491        timeout = msecs_to_jiffies(5000);
 492        if (!wait_for_completion_timeout(&dev->wait, timeout)) {
 493                ft260_i2c_reset(hdev);
 494                return -ETIMEDOUT;
 495        }
 496
 497        ret = ft260_xfer_status(dev);
 498        if (ret == 0)
 499                return 0;
 500
 501        ft260_i2c_reset(hdev);
 502        return -EIO;
 503}
 504
 505/*
 506 * A random read operation is implemented as a dummy write operation, followed
 507 * by a current address read operation. The dummy write operation is used to
 508 * load the target byte address into the current byte address counter, from
 509 * which the subsequent current address read operation then reads.
 510 */
 511static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
 512{
 513        int len, ret;
 514        u16 left_len = msgs[1].len;
 515        u8 *read_buf = msgs[1].buf;
 516        u8 addr = msgs[0].addr;
 517        u16 read_off = 0;
 518        struct hid_device *hdev = dev->hdev;
 519
 520        if (msgs[0].len > 2) {
 521                hid_err(hdev, "%s: unsupported wr len: %d\n", __func__,
 522                        msgs[0].len);
 523                return -EOPNOTSUPP;
 524        }
 525
 526        memcpy(&read_off, msgs[0].buf, msgs[0].len);
 527
 528        do {
 529                if (left_len <= FT260_RD_DATA_MAX)
 530                        len = left_len;
 531                else
 532                        len = FT260_RD_DATA_MAX;
 533
 534                ft260_dbg("read_off %#x left_len %d len %d\n", read_off,
 535                          left_len, len);
 536
 537                ret = ft260_i2c_write(dev, addr, (u8 *)&read_off, msgs[0].len,
 538                                      FT260_FLAG_START);
 539                if (ret < 0)
 540                        return ret;
 541
 542                ret = ft260_i2c_read(dev, addr, read_buf, len,
 543                                     FT260_FLAG_START_STOP);
 544                if (ret < 0)
 545                        return ret;
 546
 547                left_len -= len;
 548                read_buf += len;
 549                read_off += len;
 550
 551        } while (left_len > 0);
 552
 553        return 0;
 554}
 555
 556static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
 557                          int num)
 558{
 559        int ret;
 560        struct ft260_device *dev = i2c_get_adapdata(adapter);
 561        struct hid_device *hdev = dev->hdev;
 562
 563        mutex_lock(&dev->lock);
 564
 565        ret = hid_hw_power(hdev, PM_HINT_FULLON);
 566        if (ret < 0) {
 567                hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
 568                mutex_unlock(&dev->lock);
 569                return ret;
 570        }
 571
 572        if (num == 1) {
 573                if (msgs->flags & I2C_M_RD)
 574                        ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
 575                                             msgs->len, FT260_FLAG_START_STOP);
 576                else
 577                        ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
 578                                              msgs->len, FT260_FLAG_START_STOP);
 579                if (ret < 0)
 580                        goto i2c_exit;
 581
 582        } else {
 583                /* Combined write then read message */
 584                ret = ft260_i2c_write_read(dev, msgs);
 585                if (ret < 0)
 586                        goto i2c_exit;
 587        }
 588
 589        ret = num;
 590i2c_exit:
 591        hid_hw_power(hdev, PM_HINT_NORMAL);
 592        mutex_unlock(&dev->lock);
 593        return ret;
 594}
 595
 596static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
 597                            char read_write, u8 cmd, int size,
 598                            union i2c_smbus_data *data)
 599{
 600        int ret;
 601        struct ft260_device *dev = i2c_get_adapdata(adapter);
 602        struct hid_device *hdev = dev->hdev;
 603
 604        ft260_dbg("smbus size %d\n", size);
 605
 606        mutex_lock(&dev->lock);
 607
 608        ret = hid_hw_power(hdev, PM_HINT_FULLON);
 609        if (ret < 0) {
 610                hid_err(hdev, "power management error: %d\n", ret);
 611                mutex_unlock(&dev->lock);
 612                return ret;
 613        }
 614
 615        switch (size) {
 616        case I2C_SMBUS_QUICK:
 617                if (read_write == I2C_SMBUS_READ)
 618                        ret = ft260_i2c_read(dev, addr, &data->byte, 0,
 619                                             FT260_FLAG_START_STOP);
 620                else
 621                        ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
 622                                                FT260_FLAG_START_STOP);
 623                break;
 624        case I2C_SMBUS_BYTE:
 625                if (read_write == I2C_SMBUS_READ)
 626                        ret = ft260_i2c_read(dev, addr, &data->byte, 1,
 627                                             FT260_FLAG_START_STOP);
 628                else
 629                        ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
 630                                                FT260_FLAG_START_STOP);
 631                break;
 632        case I2C_SMBUS_BYTE_DATA:
 633                if (read_write == I2C_SMBUS_READ) {
 634                        ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
 635                                                FT260_FLAG_START);
 636                        if (ret)
 637                                goto smbus_exit;
 638
 639                        ret = ft260_i2c_read(dev, addr, &data->byte, 1,
 640                                             FT260_FLAG_START_STOP_REPEATED);
 641                } else {
 642                        ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
 643                                                FT260_FLAG_START_STOP);
 644                }
 645                break;
 646        case I2C_SMBUS_WORD_DATA:
 647                if (read_write == I2C_SMBUS_READ) {
 648                        ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
 649                                                FT260_FLAG_START);
 650                        if (ret)
 651                                goto smbus_exit;
 652
 653                        ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
 654                                             FT260_FLAG_START_STOP_REPEATED);
 655                } else {
 656                        ret = ft260_smbus_write(dev, addr, cmd,
 657                                                (u8 *)&data->word, 2,
 658                                                FT260_FLAG_START_STOP);
 659                }
 660                break;
 661        case I2C_SMBUS_BLOCK_DATA:
 662                if (read_write == I2C_SMBUS_READ) {
 663                        ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
 664                                                FT260_FLAG_START);
 665                        if (ret)
 666                                goto smbus_exit;
 667
 668                        ret = ft260_i2c_read(dev, addr, data->block,
 669                                             data->block[0] + 1,
 670                                             FT260_FLAG_START_STOP_REPEATED);
 671                } else {
 672                        ret = ft260_smbus_write(dev, addr, cmd, data->block,
 673                                                data->block[0] + 1,
 674                                                FT260_FLAG_START_STOP);
 675                }
 676                break;
 677        case I2C_SMBUS_I2C_BLOCK_DATA:
 678                if (read_write == I2C_SMBUS_READ) {
 679                        ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
 680                                                FT260_FLAG_START);
 681                        if (ret)
 682                                goto smbus_exit;
 683
 684                        ret = ft260_i2c_read(dev, addr, data->block + 1,
 685                                             data->block[0],
 686                                             FT260_FLAG_START_STOP_REPEATED);
 687                } else {
 688                        ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
 689                                                data->block[0],
 690                                                FT260_FLAG_START_STOP);
 691                }
 692                break;
 693        default:
 694                hid_err(hdev, "unsupported smbus transaction size %d\n", size);
 695                ret = -EOPNOTSUPP;
 696        }
 697
 698smbus_exit:
 699        hid_hw_power(hdev, PM_HINT_NORMAL);
 700        mutex_unlock(&dev->lock);
 701        return ret;
 702}
 703
 704static u32 ft260_functionality(struct i2c_adapter *adap)
 705{
 706        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_QUICK |
 707               I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
 708               I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
 709}
 710
 711static const struct i2c_adapter_quirks ft260_i2c_quirks = {
 712        .flags = I2C_AQ_COMB_WRITE_THEN_READ,
 713        .max_comb_1st_msg_len = 2,
 714};
 715
 716static const struct i2c_algorithm ft260_i2c_algo = {
 717        .master_xfer = ft260_i2c_xfer,
 718        .smbus_xfer = ft260_smbus_xfer,
 719        .functionality = ft260_functionality,
 720};
 721
 722static int ft260_get_system_config(struct hid_device *hdev,
 723                                   struct ft260_get_system_status_report *cfg)
 724{
 725        int ret;
 726        int len = sizeof(struct ft260_get_system_status_report);
 727
 728        ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS,
 729                                           (u8 *)cfg, len);
 730        if (ret < 0) {
 731                hid_err(hdev, "failed to retrieve system status\n");
 732                return ret;
 733        }
 734        return 0;
 735}
 736
 737static int ft260_is_interface_enabled(struct hid_device *hdev)
 738{
 739        struct ft260_get_system_status_report cfg;
 740        struct usb_interface *usbif = to_usb_interface(hdev->dev.parent);
 741        int interface = usbif->cur_altsetting->desc.bInterfaceNumber;
 742        int ret;
 743
 744        ret = ft260_get_system_config(hdev, &cfg);
 745        if (ret < 0)
 746                return ret;
 747
 748        ft260_dbg("interface:  0x%02x\n", interface);
 749        ft260_dbg("chip mode:  0x%02x\n", cfg.chip_mode);
 750        ft260_dbg("clock_ctl:  0x%02x\n", cfg.clock_ctl);
 751        ft260_dbg("i2c_enable: 0x%02x\n", cfg.i2c_enable);
 752        ft260_dbg("uart_mode:  0x%02x\n", cfg.uart_mode);
 753
 754        switch (cfg.chip_mode) {
 755        case FT260_MODE_ALL:
 756        case FT260_MODE_BOTH:
 757                if (interface == 1)
 758                        hid_info(hdev, "uart interface is not supported\n");
 759                else
 760                        ret = 1;
 761                break;
 762        case FT260_MODE_UART:
 763                hid_info(hdev, "uart interface is not supported\n");
 764                break;
 765        case FT260_MODE_I2C:
 766                ret = 1;
 767                break;
 768        }
 769        return ret;
 770}
 771
 772static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len,
 773                           u8 *field, u8 *buf)
 774{
 775        int ret;
 776
 777        ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
 778        if (ret < 0)
 779                return ret;
 780
 781        return scnprintf(buf, PAGE_SIZE, "%d\n", *field);
 782}
 783
 784static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len,
 785                           u16 *field, u8 *buf)
 786{
 787        int ret;
 788
 789        ret = ft260_hid_feature_report_get(hdev, id, cfg, len);
 790        if (ret < 0)
 791                return ret;
 792
 793        return scnprintf(buf, PAGE_SIZE, "%d\n", le16_to_cpu(*field));
 794}
 795
 796#define FT260_ATTR_SHOW(name, reptype, id, type, func)                         \
 797        static ssize_t name##_show(struct device *kdev,                        \
 798                                   struct device_attribute *attr, char *buf)   \
 799        {                                                                      \
 800                struct reptype rep;                                            \
 801                struct hid_device *hdev = to_hid_device(kdev);                 \
 802                type *field = &rep.name;                                       \
 803                int len = sizeof(rep);                                         \
 804                                                                               \
 805                return func(hdev, id, (u8 *)&rep, len, field, buf);            \
 806        }
 807
 808#define FT260_SSTAT_ATTR_SHOW(name)                                            \
 809                FT260_ATTR_SHOW(name, ft260_get_system_status_report,          \
 810                                FT260_SYSTEM_SETTINGS, u8, ft260_byte_show)
 811
 812#define FT260_I2CST_ATTR_SHOW(name)                                            \
 813                FT260_ATTR_SHOW(name, ft260_get_i2c_status_report,             \
 814                                FT260_I2C_STATUS, u16, ft260_word_show)
 815
 816#define FT260_ATTR_STORE(name, reptype, id, req, type, func)                   \
 817        static ssize_t name##_store(struct device *kdev,                       \
 818                                    struct device_attribute *attr,             \
 819                                    const char *buf, size_t count)             \
 820        {                                                                      \
 821                struct reptype rep;                                            \
 822                struct hid_device *hdev = to_hid_device(kdev);                 \
 823                type name;                                                     \
 824                int ret;                                                       \
 825                                                                               \
 826                if (!func(buf, 10, &name)) {                                   \
 827                        rep.name = name;                                       \
 828                        rep.report = id;                                       \
 829                        rep.request = req;                                     \
 830                        ret = ft260_hid_feature_report_set(hdev, (u8 *)&rep,   \
 831                                                           sizeof(rep));       \
 832                        if (!ret)                                              \
 833                                ret = count;                                   \
 834                } else {                                                       \
 835                        ret = -EINVAL;                                         \
 836                }                                                              \
 837                return ret;                                                    \
 838        }
 839
 840#define FT260_BYTE_ATTR_STORE(name, reptype, req)                              \
 841                FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req,    \
 842                                 u8, kstrtou8)
 843
 844#define FT260_WORD_ATTR_STORE(name, reptype, req)                              \
 845                FT260_ATTR_STORE(name, reptype, FT260_SYSTEM_SETTINGS, req,    \
 846                                 u16, kstrtou16)
 847
 848FT260_SSTAT_ATTR_SHOW(chip_mode);
 849static DEVICE_ATTR_RO(chip_mode);
 850
 851FT260_SSTAT_ATTR_SHOW(pwren_status);
 852static DEVICE_ATTR_RO(pwren_status);
 853
 854FT260_SSTAT_ATTR_SHOW(suspend_status);
 855static DEVICE_ATTR_RO(suspend_status);
 856
 857FT260_SSTAT_ATTR_SHOW(hid_over_i2c_en);
 858static DEVICE_ATTR_RO(hid_over_i2c_en);
 859
 860FT260_SSTAT_ATTR_SHOW(power_saving_en);
 861static DEVICE_ATTR_RO(power_saving_en);
 862
 863FT260_SSTAT_ATTR_SHOW(i2c_enable);
 864FT260_BYTE_ATTR_STORE(i2c_enable, ft260_set_i2c_mode_report,
 865                      FT260_SET_I2C_MODE);
 866static DEVICE_ATTR_RW(i2c_enable);
 867
 868FT260_SSTAT_ATTR_SHOW(uart_mode);
 869FT260_BYTE_ATTR_STORE(uart_mode, ft260_set_uart_mode_report,
 870                      FT260_SET_UART_MODE);
 871static DEVICE_ATTR_RW(uart_mode);
 872
 873FT260_SSTAT_ATTR_SHOW(clock_ctl);
 874FT260_BYTE_ATTR_STORE(clock_ctl, ft260_set_system_clock_report,
 875                      FT260_SET_CLOCK);
 876static DEVICE_ATTR_RW(clock_ctl);
 877
 878FT260_I2CST_ATTR_SHOW(clock);
 879FT260_WORD_ATTR_STORE(clock, ft260_set_i2c_speed_report,
 880                      FT260_SET_I2C_CLOCK_SPEED);
 881static DEVICE_ATTR_RW(clock);
 882
 883static ssize_t i2c_reset_store(struct device *kdev,
 884                               struct device_attribute *attr, const char *buf,
 885                               size_t count)
 886{
 887        struct hid_device *hdev = to_hid_device(kdev);
 888        int ret = ft260_i2c_reset(hdev);
 889
 890        if (ret)
 891                return ret;
 892        return count;
 893}
 894static DEVICE_ATTR_WO(i2c_reset);
 895
 896static const struct attribute_group ft260_attr_group = {
 897        .attrs = (struct attribute *[]) {
 898                  &dev_attr_chip_mode.attr,
 899                  &dev_attr_pwren_status.attr,
 900                  &dev_attr_suspend_status.attr,
 901                  &dev_attr_hid_over_i2c_en.attr,
 902                  &dev_attr_power_saving_en.attr,
 903                  &dev_attr_i2c_enable.attr,
 904                  &dev_attr_uart_mode.attr,
 905                  &dev_attr_clock_ctl.attr,
 906                  &dev_attr_i2c_reset.attr,
 907                  &dev_attr_clock.attr,
 908                  NULL
 909        }
 910};
 911
 912static int ft260_probe(struct hid_device *hdev, const struct hid_device_id *id)
 913{
 914        struct ft260_device *dev;
 915        struct ft260_get_chip_version_report version;
 916        int ret;
 917
 918        dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
 919        if (!dev)
 920                return -ENOMEM;
 921
 922        ret = hid_parse(hdev);
 923        if (ret) {
 924                hid_err(hdev, "failed to parse HID\n");
 925                return ret;
 926        }
 927
 928        ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
 929        if (ret) {
 930                hid_err(hdev, "failed to start HID HW\n");
 931                return ret;
 932        }
 933
 934        ret = hid_hw_open(hdev);
 935        if (ret) {
 936                hid_err(hdev, "failed to open HID HW\n");
 937                goto err_hid_stop;
 938        }
 939
 940        ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION,
 941                                           (u8 *)&version, sizeof(version));
 942        if (ret < 0) {
 943                hid_err(hdev, "failed to retrieve chip version\n");
 944                goto err_hid_close;
 945        }
 946
 947        hid_info(hdev, "chip code: %02x%02x %02x%02x\n",
 948                 version.chip_code[0], version.chip_code[1],
 949                 version.chip_code[2], version.chip_code[3]);
 950
 951        ret = ft260_is_interface_enabled(hdev);
 952        if (ret <= 0)
 953                goto err_hid_close;
 954
 955        hid_set_drvdata(hdev, dev);
 956        dev->hdev = hdev;
 957        dev->adap.owner = THIS_MODULE;
 958        dev->adap.class = I2C_CLASS_HWMON;
 959        dev->adap.algo = &ft260_i2c_algo;
 960        dev->adap.quirks = &ft260_i2c_quirks;
 961        dev->adap.dev.parent = &hdev->dev;
 962        snprintf(dev->adap.name, sizeof(dev->adap.name),
 963                 "FT260 usb-i2c bridge on hidraw%d",
 964                 ((struct hidraw *)hdev->hidraw)->minor);
 965
 966        mutex_init(&dev->lock);
 967        init_completion(&dev->wait);
 968
 969        ret = i2c_add_adapter(&dev->adap);
 970        if (ret) {
 971                hid_err(hdev, "failed to add i2c adapter\n");
 972                goto err_hid_close;
 973        }
 974
 975        i2c_set_adapdata(&dev->adap, dev);
 976
 977        ret = sysfs_create_group(&hdev->dev.kobj, &ft260_attr_group);
 978        if (ret < 0) {
 979                hid_err(hdev, "failed to create sysfs attrs\n");
 980                goto err_i2c_free;
 981        }
 982
 983        ret = ft260_xfer_status(dev);
 984        if (ret)
 985                ft260_i2c_reset(hdev);
 986
 987        return 0;
 988
 989err_i2c_free:
 990        i2c_del_adapter(&dev->adap);
 991err_hid_close:
 992        hid_hw_close(hdev);
 993err_hid_stop:
 994        hid_hw_stop(hdev);
 995        return ret;
 996}
 997
 998static void ft260_remove(struct hid_device *hdev)
 999{
1000        struct ft260_device *dev = hid_get_drvdata(hdev);
1001
1002        if (!dev)
1003                return;
1004
1005        sysfs_remove_group(&hdev->dev.kobj, &ft260_attr_group);
1006        i2c_del_adapter(&dev->adap);
1007
1008        hid_hw_close(hdev);
1009        hid_hw_stop(hdev);
1010}
1011
1012static int ft260_raw_event(struct hid_device *hdev, struct hid_report *report,
1013                           u8 *data, int size)
1014{
1015        struct ft260_device *dev = hid_get_drvdata(hdev);
1016        struct ft260_i2c_input_report *xfer = (void *)data;
1017
1018        if (xfer->report >= FT260_I2C_REPORT_MIN &&
1019            xfer->report <= FT260_I2C_REPORT_MAX) {
1020                ft260_dbg("i2c resp: rep %#02x len %d\n", xfer->report,
1021                          xfer->length);
1022
1023                memcpy(&dev->read_buf[dev->read_idx], &xfer->data,
1024                       xfer->length);
1025                dev->read_idx += xfer->length;
1026
1027                if (dev->read_idx == dev->read_len)
1028                        complete(&dev->wait);
1029
1030        } else {
1031                hid_err(hdev, "unknown report: %#02x\n", xfer->report);
1032                return 0;
1033        }
1034        return 1;
1035}
1036
1037static struct hid_driver ft260_driver = {
1038        .name           = "ft260",
1039        .id_table       = ft260_devices,
1040        .probe          = ft260_probe,
1041        .remove         = ft260_remove,
1042        .raw_event      = ft260_raw_event,
1043};
1044
1045module_hid_driver(ft260_driver);
1046MODULE_DESCRIPTION("FTDI FT260 USB HID to I2C host bridge");
1047MODULE_AUTHOR("Michael Zaidman <michael.zaidman@gmail.com>");
1048MODULE_LICENSE("GPL v2");
1049