Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * hid-cp2112.c - Silicon Labs HID USB to SMBus master bridge
0004  * Copyright (c) 2013,2014 Uplogix, Inc.
0005  * David Barksdale <dbarksdale@uplogix.com>
0006  */
0007 
0008 /*
0009  * The Silicon Labs CP2112 chip is a USB HID device which provides an
0010  * SMBus controller for talking to slave devices and 8 GPIO pins. The
0011  * host communicates with the CP2112 via raw HID reports.
0012  *
0013  * Data Sheet:
0014  *   https://www.silabs.com/Support%20Documents/TechnicalDocs/CP2112.pdf
0015  * Programming Interface Specification:
0016  *   https://www.silabs.com/documents/public/application-notes/an495-cp2112-interface-specification.pdf
0017  */
0018 
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/gpio/machine.h>
0021 #include <linux/gpio/driver.h>
0022 #include <linux/hid.h>
0023 #include <linux/hidraw.h>
0024 #include <linux/i2c.h>
0025 #include <linux/module.h>
0026 #include <linux/nls.h>
0027 #include <linux/usb/ch9.h>
0028 #include "hid-ids.h"
0029 
0030 #define CP2112_REPORT_MAX_LENGTH        64
0031 #define CP2112_GPIO_CONFIG_LENGTH       5
0032 #define CP2112_GPIO_GET_LENGTH          2
0033 #define CP2112_GPIO_SET_LENGTH          3
0034 
0035 enum {
0036     CP2112_GPIO_CONFIG      = 0x02,
0037     CP2112_GPIO_GET         = 0x03,
0038     CP2112_GPIO_SET         = 0x04,
0039     CP2112_GET_VERSION_INFO     = 0x05,
0040     CP2112_SMBUS_CONFIG     = 0x06,
0041     CP2112_DATA_READ_REQUEST    = 0x10,
0042     CP2112_DATA_WRITE_READ_REQUEST  = 0x11,
0043     CP2112_DATA_READ_FORCE_SEND = 0x12,
0044     CP2112_DATA_READ_RESPONSE   = 0x13,
0045     CP2112_DATA_WRITE_REQUEST   = 0x14,
0046     CP2112_TRANSFER_STATUS_REQUEST  = 0x15,
0047     CP2112_TRANSFER_STATUS_RESPONSE = 0x16,
0048     CP2112_CANCEL_TRANSFER      = 0x17,
0049     CP2112_LOCK_BYTE        = 0x20,
0050     CP2112_USB_CONFIG       = 0x21,
0051     CP2112_MANUFACTURER_STRING  = 0x22,
0052     CP2112_PRODUCT_STRING       = 0x23,
0053     CP2112_SERIAL_STRING        = 0x24,
0054 };
0055 
0056 enum {
0057     STATUS0_IDLE        = 0x00,
0058     STATUS0_BUSY        = 0x01,
0059     STATUS0_COMPLETE    = 0x02,
0060     STATUS0_ERROR       = 0x03,
0061 };
0062 
0063 enum {
0064     STATUS1_TIMEOUT_NACK        = 0x00,
0065     STATUS1_TIMEOUT_BUS     = 0x01,
0066     STATUS1_ARBITRATION_LOST    = 0x02,
0067     STATUS1_READ_INCOMPLETE     = 0x03,
0068     STATUS1_WRITE_INCOMPLETE    = 0x04,
0069     STATUS1_SUCCESS         = 0x05,
0070 };
0071 
0072 struct cp2112_smbus_config_report {
0073     u8 report;      /* CP2112_SMBUS_CONFIG */
0074     __be32 clock_speed; /* Hz */
0075     u8 device_address;  /* Stored in the upper 7 bits */
0076     u8 auto_send_read;  /* 1 = enabled, 0 = disabled */
0077     __be16 write_timeout;   /* ms, 0 = no timeout */
0078     __be16 read_timeout;    /* ms, 0 = no timeout */
0079     u8 scl_low_timeout; /* 1 = enabled, 0 = disabled */
0080     __be16 retry_time;  /* # of retries, 0 = no limit */
0081 } __packed;
0082 
0083 struct cp2112_usb_config_report {
0084     u8 report;  /* CP2112_USB_CONFIG */
0085     __le16 vid; /* Vendor ID */
0086     __le16 pid; /* Product ID */
0087     u8 max_power;   /* Power requested in 2mA units */
0088     u8 power_mode;  /* 0x00 = bus powered
0089                0x01 = self powered & regulator off
0090                0x02 = self powered & regulator on */
0091     u8 release_major;
0092     u8 release_minor;
0093     u8 mask;    /* What fields to program */
0094 } __packed;
0095 
0096 struct cp2112_read_req_report {
0097     u8 report;  /* CP2112_DATA_READ_REQUEST */
0098     u8 slave_address;
0099     __be16 length;
0100 } __packed;
0101 
0102 struct cp2112_write_read_req_report {
0103     u8 report;  /* CP2112_DATA_WRITE_READ_REQUEST */
0104     u8 slave_address;
0105     __be16 length;
0106     u8 target_address_length;
0107     u8 target_address[16];
0108 } __packed;
0109 
0110 struct cp2112_write_req_report {
0111     u8 report;  /* CP2112_DATA_WRITE_REQUEST */
0112     u8 slave_address;
0113     u8 length;
0114     u8 data[61];
0115 } __packed;
0116 
0117 struct cp2112_force_read_report {
0118     u8 report;  /* CP2112_DATA_READ_FORCE_SEND */
0119     __be16 length;
0120 } __packed;
0121 
0122 struct cp2112_xfer_status_report {
0123     u8 report;  /* CP2112_TRANSFER_STATUS_RESPONSE */
0124     u8 status0; /* STATUS0_* */
0125     u8 status1; /* STATUS1_* */
0126     __be16 retries;
0127     __be16 length;
0128 } __packed;
0129 
0130 struct cp2112_string_report {
0131     u8 dummy;       /* force .string to be aligned */
0132     struct_group_attr(contents, __packed,
0133         u8 report;      /* CP2112_*_STRING */
0134         u8 length;      /* length in bytes of everything after .report */
0135         u8 type;        /* USB_DT_STRING */
0136         wchar_t string[30]; /* UTF16_LITTLE_ENDIAN string */
0137     );
0138 } __packed;
0139 
0140 /* Number of times to request transfer status before giving up waiting for a
0141    transfer to complete. This may need to be changed if SMBUS clock, retries,
0142    or read/write/scl_low timeout settings are changed. */
0143 static const int XFER_STATUS_RETRIES = 10;
0144 
0145 /* Time in ms to wait for a CP2112_DATA_READ_RESPONSE or
0146    CP2112_TRANSFER_STATUS_RESPONSE. */
0147 static const int RESPONSE_TIMEOUT = 50;
0148 
0149 static const struct hid_device_id cp2112_devices[] = {
0150     { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL, USB_DEVICE_ID_CYGNAL_CP2112) },
0151     { }
0152 };
0153 MODULE_DEVICE_TABLE(hid, cp2112_devices);
0154 
0155 struct cp2112_device {
0156     struct i2c_adapter adap;
0157     struct hid_device *hdev;
0158     wait_queue_head_t wait;
0159     u8 read_data[61];
0160     u8 read_length;
0161     u8 hwversion;
0162     int xfer_status;
0163     atomic_t read_avail;
0164     atomic_t xfer_avail;
0165     struct gpio_chip gc;
0166     struct irq_chip irq;
0167     u8 *in_out_buffer;
0168     struct mutex lock;
0169 
0170     struct gpio_desc *desc[8];
0171     bool gpio_poll;
0172     struct delayed_work gpio_poll_worker;
0173     unsigned long irq_mask;
0174     u8 gpio_prev_state;
0175 };
0176 
0177 static int gpio_push_pull = 0xFF;
0178 module_param(gpio_push_pull, int, S_IRUGO | S_IWUSR);
0179 MODULE_PARM_DESC(gpio_push_pull, "GPIO push-pull configuration bitmask");
0180 
0181 static int cp2112_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
0182 {
0183     struct cp2112_device *dev = gpiochip_get_data(chip);
0184     struct hid_device *hdev = dev->hdev;
0185     u8 *buf = dev->in_out_buffer;
0186     int ret;
0187 
0188     mutex_lock(&dev->lock);
0189 
0190     ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
0191                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
0192                  HID_REQ_GET_REPORT);
0193     if (ret != CP2112_GPIO_CONFIG_LENGTH) {
0194         hid_err(hdev, "error requesting GPIO config: %d\n", ret);
0195         if (ret >= 0)
0196             ret = -EIO;
0197         goto exit;
0198     }
0199 
0200     buf[1] &= ~(1 << offset);
0201     buf[2] = gpio_push_pull;
0202 
0203     ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
0204                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
0205                  HID_REQ_SET_REPORT);
0206     if (ret != CP2112_GPIO_CONFIG_LENGTH) {
0207         hid_err(hdev, "error setting GPIO config: %d\n", ret);
0208         if (ret >= 0)
0209             ret = -EIO;
0210         goto exit;
0211     }
0212 
0213     ret = 0;
0214 
0215 exit:
0216     mutex_unlock(&dev->lock);
0217     return ret;
0218 }
0219 
0220 static void cp2112_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
0221 {
0222     struct cp2112_device *dev = gpiochip_get_data(chip);
0223     struct hid_device *hdev = dev->hdev;
0224     u8 *buf = dev->in_out_buffer;
0225     int ret;
0226 
0227     mutex_lock(&dev->lock);
0228 
0229     buf[0] = CP2112_GPIO_SET;
0230     buf[1] = value ? 0xff : 0;
0231     buf[2] = 1 << offset;
0232 
0233     ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf,
0234                  CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT,
0235                  HID_REQ_SET_REPORT);
0236     if (ret < 0)
0237         hid_err(hdev, "error setting GPIO values: %d\n", ret);
0238 
0239     mutex_unlock(&dev->lock);
0240 }
0241 
0242 static int cp2112_gpio_get_all(struct gpio_chip *chip)
0243 {
0244     struct cp2112_device *dev = gpiochip_get_data(chip);
0245     struct hid_device *hdev = dev->hdev;
0246     u8 *buf = dev->in_out_buffer;
0247     int ret;
0248 
0249     mutex_lock(&dev->lock);
0250 
0251     ret = hid_hw_raw_request(hdev, CP2112_GPIO_GET, buf,
0252                  CP2112_GPIO_GET_LENGTH, HID_FEATURE_REPORT,
0253                  HID_REQ_GET_REPORT);
0254     if (ret != CP2112_GPIO_GET_LENGTH) {
0255         hid_err(hdev, "error requesting GPIO values: %d\n", ret);
0256         ret = ret < 0 ? ret : -EIO;
0257         goto exit;
0258     }
0259 
0260     ret = buf[1];
0261 
0262 exit:
0263     mutex_unlock(&dev->lock);
0264 
0265     return ret;
0266 }
0267 
0268 static int cp2112_gpio_get(struct gpio_chip *chip, unsigned int offset)
0269 {
0270     int ret;
0271 
0272     ret = cp2112_gpio_get_all(chip);
0273     if (ret < 0)
0274         return ret;
0275 
0276     return (ret >> offset) & 1;
0277 }
0278 
0279 static int cp2112_gpio_direction_output(struct gpio_chip *chip,
0280                     unsigned offset, int value)
0281 {
0282     struct cp2112_device *dev = gpiochip_get_data(chip);
0283     struct hid_device *hdev = dev->hdev;
0284     u8 *buf = dev->in_out_buffer;
0285     int ret;
0286 
0287     mutex_lock(&dev->lock);
0288 
0289     ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
0290                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
0291                  HID_REQ_GET_REPORT);
0292     if (ret != CP2112_GPIO_CONFIG_LENGTH) {
0293         hid_err(hdev, "error requesting GPIO config: %d\n", ret);
0294         goto fail;
0295     }
0296 
0297     buf[1] |= 1 << offset;
0298     buf[2] = gpio_push_pull;
0299 
0300     ret = hid_hw_raw_request(hdev, CP2112_GPIO_CONFIG, buf,
0301                  CP2112_GPIO_CONFIG_LENGTH, HID_FEATURE_REPORT,
0302                  HID_REQ_SET_REPORT);
0303     if (ret < 0) {
0304         hid_err(hdev, "error setting GPIO config: %d\n", ret);
0305         goto fail;
0306     }
0307 
0308     mutex_unlock(&dev->lock);
0309 
0310     /*
0311      * Set gpio value when output direction is already set,
0312      * as specified in AN495, Rev. 0.2, cpt. 4.4
0313      */
0314     cp2112_gpio_set(chip, offset, value);
0315 
0316     return 0;
0317 
0318 fail:
0319     mutex_unlock(&dev->lock);
0320     return ret < 0 ? ret : -EIO;
0321 }
0322 
0323 static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,
0324               u8 *data, size_t count, unsigned char report_type)
0325 {
0326     u8 *buf;
0327     int ret;
0328 
0329     buf = kmalloc(count, GFP_KERNEL);
0330     if (!buf)
0331         return -ENOMEM;
0332 
0333     ret = hid_hw_raw_request(hdev, report_number, buf, count,
0334                        report_type, HID_REQ_GET_REPORT);
0335     memcpy(data, buf, count);
0336     kfree(buf);
0337     return ret;
0338 }
0339 
0340 static int cp2112_hid_output(struct hid_device *hdev, u8 *data, size_t count,
0341                  unsigned char report_type)
0342 {
0343     u8 *buf;
0344     int ret;
0345 
0346     buf = kmemdup(data, count, GFP_KERNEL);
0347     if (!buf)
0348         return -ENOMEM;
0349 
0350     if (report_type == HID_OUTPUT_REPORT)
0351         ret = hid_hw_output_report(hdev, buf, count);
0352     else
0353         ret = hid_hw_raw_request(hdev, buf[0], buf, count, report_type,
0354                 HID_REQ_SET_REPORT);
0355 
0356     kfree(buf);
0357     return ret;
0358 }
0359 
0360 static int cp2112_wait(struct cp2112_device *dev, atomic_t *avail)
0361 {
0362     int ret = 0;
0363 
0364     /* We have sent either a CP2112_TRANSFER_STATUS_REQUEST or a
0365      * CP2112_DATA_READ_FORCE_SEND and we are waiting for the response to
0366      * come in cp2112_raw_event or timeout. There will only be one of these
0367      * in flight at any one time. The timeout is extremely large and is a
0368      * last resort if the CP2112 has died. If we do timeout we don't expect
0369      * to receive the response which would cause data races, it's not like
0370      * we can do anything about it anyway.
0371      */
0372     ret = wait_event_interruptible_timeout(dev->wait,
0373         atomic_read(avail), msecs_to_jiffies(RESPONSE_TIMEOUT));
0374     if (-ERESTARTSYS == ret)
0375         return ret;
0376     if (!ret)
0377         return -ETIMEDOUT;
0378 
0379     atomic_set(avail, 0);
0380     return 0;
0381 }
0382 
0383 static int cp2112_xfer_status(struct cp2112_device *dev)
0384 {
0385     struct hid_device *hdev = dev->hdev;
0386     u8 buf[2];
0387     int ret;
0388 
0389     buf[0] = CP2112_TRANSFER_STATUS_REQUEST;
0390     buf[1] = 0x01;
0391     atomic_set(&dev->xfer_avail, 0);
0392 
0393     ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
0394     if (ret < 0) {
0395         hid_warn(hdev, "Error requesting status: %d\n", ret);
0396         return ret;
0397     }
0398 
0399     ret = cp2112_wait(dev, &dev->xfer_avail);
0400     if (ret)
0401         return ret;
0402 
0403     return dev->xfer_status;
0404 }
0405 
0406 static int cp2112_read(struct cp2112_device *dev, u8 *data, size_t size)
0407 {
0408     struct hid_device *hdev = dev->hdev;
0409     struct cp2112_force_read_report report;
0410     int ret;
0411 
0412     if (size > sizeof(dev->read_data))
0413         size = sizeof(dev->read_data);
0414     report.report = CP2112_DATA_READ_FORCE_SEND;
0415     report.length = cpu_to_be16(size);
0416 
0417     atomic_set(&dev->read_avail, 0);
0418 
0419     ret = cp2112_hid_output(hdev, &report.report, sizeof(report),
0420                 HID_OUTPUT_REPORT);
0421     if (ret < 0) {
0422         hid_warn(hdev, "Error requesting data: %d\n", ret);
0423         return ret;
0424     }
0425 
0426     ret = cp2112_wait(dev, &dev->read_avail);
0427     if (ret)
0428         return ret;
0429 
0430     hid_dbg(hdev, "read %d of %zd bytes requested\n",
0431         dev->read_length, size);
0432 
0433     if (size > dev->read_length)
0434         size = dev->read_length;
0435 
0436     memcpy(data, dev->read_data, size);
0437     return dev->read_length;
0438 }
0439 
0440 static int cp2112_read_req(void *buf, u8 slave_address, u16 length)
0441 {
0442     struct cp2112_read_req_report *report = buf;
0443 
0444     if (length < 1 || length > 512)
0445         return -EINVAL;
0446 
0447     report->report = CP2112_DATA_READ_REQUEST;
0448     report->slave_address = slave_address << 1;
0449     report->length = cpu_to_be16(length);
0450     return sizeof(*report);
0451 }
0452 
0453 static int cp2112_write_read_req(void *buf, u8 slave_address, u16 length,
0454                  u8 command, u8 *data, u8 data_length)
0455 {
0456     struct cp2112_write_read_req_report *report = buf;
0457 
0458     if (length < 1 || length > 512
0459         || data_length > sizeof(report->target_address) - 1)
0460         return -EINVAL;
0461 
0462     report->report = CP2112_DATA_WRITE_READ_REQUEST;
0463     report->slave_address = slave_address << 1;
0464     report->length = cpu_to_be16(length);
0465     report->target_address_length = data_length + 1;
0466     report->target_address[0] = command;
0467     memcpy(&report->target_address[1], data, data_length);
0468     return data_length + 6;
0469 }
0470 
0471 static int cp2112_write_req(void *buf, u8 slave_address, u8 command, u8 *data,
0472                 u8 data_length)
0473 {
0474     struct cp2112_write_req_report *report = buf;
0475 
0476     if (data_length > sizeof(report->data) - 1)
0477         return -EINVAL;
0478 
0479     report->report = CP2112_DATA_WRITE_REQUEST;
0480     report->slave_address = slave_address << 1;
0481     report->length = data_length + 1;
0482     report->data[0] = command;
0483     memcpy(&report->data[1], data, data_length);
0484     return data_length + 4;
0485 }
0486 
0487 static int cp2112_i2c_write_req(void *buf, u8 slave_address, u8 *data,
0488                 u8 data_length)
0489 {
0490     struct cp2112_write_req_report *report = buf;
0491 
0492     if (data_length > sizeof(report->data))
0493         return -EINVAL;
0494 
0495     report->report = CP2112_DATA_WRITE_REQUEST;
0496     report->slave_address = slave_address << 1;
0497     report->length = data_length;
0498     memcpy(report->data, data, data_length);
0499     return data_length + 3;
0500 }
0501 
0502 static int cp2112_i2c_write_read_req(void *buf, u8 slave_address,
0503                      u8 *addr, int addr_length,
0504                      int read_length)
0505 {
0506     struct cp2112_write_read_req_report *report = buf;
0507 
0508     if (read_length < 1 || read_length > 512 ||
0509         addr_length > sizeof(report->target_address))
0510         return -EINVAL;
0511 
0512     report->report = CP2112_DATA_WRITE_READ_REQUEST;
0513     report->slave_address = slave_address << 1;
0514     report->length = cpu_to_be16(read_length);
0515     report->target_address_length = addr_length;
0516     memcpy(report->target_address, addr, addr_length);
0517     return addr_length + 5;
0518 }
0519 
0520 static int cp2112_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
0521                int num)
0522 {
0523     struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
0524     struct hid_device *hdev = dev->hdev;
0525     u8 buf[64];
0526     ssize_t count;
0527     ssize_t read_length = 0;
0528     u8 *read_buf = NULL;
0529     unsigned int retries;
0530     int ret;
0531 
0532     hid_dbg(hdev, "I2C %d messages\n", num);
0533 
0534     if (num == 1) {
0535         if (msgs->flags & I2C_M_RD) {
0536             hid_dbg(hdev, "I2C read %#04x len %d\n",
0537                 msgs->addr, msgs->len);
0538             read_length = msgs->len;
0539             read_buf = msgs->buf;
0540             count = cp2112_read_req(buf, msgs->addr, msgs->len);
0541         } else {
0542             hid_dbg(hdev, "I2C write %#04x len %d\n",
0543                 msgs->addr, msgs->len);
0544             count = cp2112_i2c_write_req(buf, msgs->addr,
0545                              msgs->buf, msgs->len);
0546         }
0547         if (count < 0)
0548             return count;
0549     } else if (dev->hwversion > 1 &&  /* no repeated start in rev 1 */
0550            num == 2 &&
0551            msgs[0].addr == msgs[1].addr &&
0552            !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
0553         hid_dbg(hdev, "I2C write-read %#04x wlen %d rlen %d\n",
0554             msgs[0].addr, msgs[0].len, msgs[1].len);
0555         read_length = msgs[1].len;
0556         read_buf = msgs[1].buf;
0557         count = cp2112_i2c_write_read_req(buf, msgs[0].addr,
0558                 msgs[0].buf, msgs[0].len, msgs[1].len);
0559         if (count < 0)
0560             return count;
0561     } else {
0562         hid_err(hdev,
0563             "Multi-message I2C transactions not supported\n");
0564         return -EOPNOTSUPP;
0565     }
0566 
0567     ret = hid_hw_power(hdev, PM_HINT_FULLON);
0568     if (ret < 0) {
0569         hid_err(hdev, "power management error: %d\n", ret);
0570         return ret;
0571     }
0572 
0573     ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
0574     if (ret < 0) {
0575         hid_warn(hdev, "Error starting transaction: %d\n", ret);
0576         goto power_normal;
0577     }
0578 
0579     for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
0580         ret = cp2112_xfer_status(dev);
0581         if (-EBUSY == ret)
0582             continue;
0583         if (ret < 0)
0584             goto power_normal;
0585         break;
0586     }
0587 
0588     if (XFER_STATUS_RETRIES <= retries) {
0589         hid_warn(hdev, "Transfer timed out, cancelling.\n");
0590         buf[0] = CP2112_CANCEL_TRANSFER;
0591         buf[1] = 0x01;
0592 
0593         ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
0594         if (ret < 0)
0595             hid_warn(hdev, "Error cancelling transaction: %d\n",
0596                  ret);
0597 
0598         ret = -ETIMEDOUT;
0599         goto power_normal;
0600     }
0601 
0602     for (count = 0; count < read_length;) {
0603         ret = cp2112_read(dev, read_buf + count, read_length - count);
0604         if (ret < 0)
0605             goto power_normal;
0606         if (ret == 0) {
0607             hid_err(hdev, "read returned 0\n");
0608             ret = -EIO;
0609             goto power_normal;
0610         }
0611         count += ret;
0612         if (count > read_length) {
0613             /*
0614              * The hardware returned too much data.
0615              * This is mostly harmless because cp2112_read()
0616              * has a limit check so didn't overrun our
0617              * buffer.  Nevertheless, we return an error
0618              * because something is seriously wrong and
0619              * it shouldn't go unnoticed.
0620              */
0621             hid_err(hdev, "long read: %d > %zd\n",
0622                 ret, read_length - count + ret);
0623             ret = -EIO;
0624             goto power_normal;
0625         }
0626     }
0627 
0628     /* return the number of transferred messages */
0629     ret = num;
0630 
0631 power_normal:
0632     hid_hw_power(hdev, PM_HINT_NORMAL);
0633     hid_dbg(hdev, "I2C transfer finished: %d\n", ret);
0634     return ret;
0635 }
0636 
0637 static int cp2112_xfer(struct i2c_adapter *adap, u16 addr,
0638                unsigned short flags, char read_write, u8 command,
0639                int size, union i2c_smbus_data *data)
0640 {
0641     struct cp2112_device *dev = (struct cp2112_device *)adap->algo_data;
0642     struct hid_device *hdev = dev->hdev;
0643     u8 buf[64];
0644     __le16 word;
0645     ssize_t count;
0646     size_t read_length = 0;
0647     unsigned int retries;
0648     int ret;
0649 
0650     hid_dbg(hdev, "%s addr 0x%x flags 0x%x cmd 0x%x size %d\n",
0651         read_write == I2C_SMBUS_WRITE ? "write" : "read",
0652         addr, flags, command, size);
0653 
0654     switch (size) {
0655     case I2C_SMBUS_BYTE:
0656         read_length = 1;
0657 
0658         if (I2C_SMBUS_READ == read_write)
0659             count = cp2112_read_req(buf, addr, read_length);
0660         else
0661             count = cp2112_write_req(buf, addr, command, NULL,
0662                          0);
0663         break;
0664     case I2C_SMBUS_BYTE_DATA:
0665         read_length = 1;
0666 
0667         if (I2C_SMBUS_READ == read_write)
0668             count = cp2112_write_read_req(buf, addr, read_length,
0669                               command, NULL, 0);
0670         else
0671             count = cp2112_write_req(buf, addr, command,
0672                          &data->byte, 1);
0673         break;
0674     case I2C_SMBUS_WORD_DATA:
0675         read_length = 2;
0676         word = cpu_to_le16(data->word);
0677 
0678         if (I2C_SMBUS_READ == read_write)
0679             count = cp2112_write_read_req(buf, addr, read_length,
0680                               command, NULL, 0);
0681         else
0682             count = cp2112_write_req(buf, addr, command,
0683                          (u8 *)&word, 2);
0684         break;
0685     case I2C_SMBUS_PROC_CALL:
0686         size = I2C_SMBUS_WORD_DATA;
0687         read_write = I2C_SMBUS_READ;
0688         read_length = 2;
0689         word = cpu_to_le16(data->word);
0690 
0691         count = cp2112_write_read_req(buf, addr, read_length, command,
0692                           (u8 *)&word, 2);
0693         break;
0694     case I2C_SMBUS_I2C_BLOCK_DATA:
0695         if (read_write == I2C_SMBUS_READ) {
0696             read_length = data->block[0];
0697             count = cp2112_write_read_req(buf, addr, read_length,
0698                               command, NULL, 0);
0699         } else {
0700             count = cp2112_write_req(buf, addr, command,
0701                          data->block + 1,
0702                          data->block[0]);
0703         }
0704         break;
0705     case I2C_SMBUS_BLOCK_DATA:
0706         if (I2C_SMBUS_READ == read_write) {
0707             count = cp2112_write_read_req(buf, addr,
0708                               I2C_SMBUS_BLOCK_MAX,
0709                               command, NULL, 0);
0710         } else {
0711             count = cp2112_write_req(buf, addr, command,
0712                          data->block,
0713                          data->block[0] + 1);
0714         }
0715         break;
0716     case I2C_SMBUS_BLOCK_PROC_CALL:
0717         size = I2C_SMBUS_BLOCK_DATA;
0718         read_write = I2C_SMBUS_READ;
0719 
0720         count = cp2112_write_read_req(buf, addr, I2C_SMBUS_BLOCK_MAX,
0721                           command, data->block,
0722                           data->block[0] + 1);
0723         break;
0724     default:
0725         hid_warn(hdev, "Unsupported transaction %d\n", size);
0726         return -EOPNOTSUPP;
0727     }
0728 
0729     if (count < 0)
0730         return count;
0731 
0732     ret = hid_hw_power(hdev, PM_HINT_FULLON);
0733     if (ret < 0) {
0734         hid_err(hdev, "power management error: %d\n", ret);
0735         return ret;
0736     }
0737 
0738     ret = cp2112_hid_output(hdev, buf, count, HID_OUTPUT_REPORT);
0739     if (ret < 0) {
0740         hid_warn(hdev, "Error starting transaction: %d\n", ret);
0741         goto power_normal;
0742     }
0743 
0744     for (retries = 0; retries < XFER_STATUS_RETRIES; ++retries) {
0745         ret = cp2112_xfer_status(dev);
0746         if (-EBUSY == ret)
0747             continue;
0748         if (ret < 0)
0749             goto power_normal;
0750         break;
0751     }
0752 
0753     if (XFER_STATUS_RETRIES <= retries) {
0754         hid_warn(hdev, "Transfer timed out, cancelling.\n");
0755         buf[0] = CP2112_CANCEL_TRANSFER;
0756         buf[1] = 0x01;
0757 
0758         ret = cp2112_hid_output(hdev, buf, 2, HID_OUTPUT_REPORT);
0759         if (ret < 0)
0760             hid_warn(hdev, "Error cancelling transaction: %d\n",
0761                  ret);
0762 
0763         ret = -ETIMEDOUT;
0764         goto power_normal;
0765     }
0766 
0767     if (I2C_SMBUS_WRITE == read_write) {
0768         ret = 0;
0769         goto power_normal;
0770     }
0771 
0772     if (I2C_SMBUS_BLOCK_DATA == size)
0773         read_length = ret;
0774 
0775     ret = cp2112_read(dev, buf, read_length);
0776     if (ret < 0)
0777         goto power_normal;
0778     if (ret != read_length) {
0779         hid_warn(hdev, "short read: %d < %zd\n", ret, read_length);
0780         ret = -EIO;
0781         goto power_normal;
0782     }
0783 
0784     switch (size) {
0785     case I2C_SMBUS_BYTE:
0786     case I2C_SMBUS_BYTE_DATA:
0787         data->byte = buf[0];
0788         break;
0789     case I2C_SMBUS_WORD_DATA:
0790         data->word = le16_to_cpup((__le16 *)buf);
0791         break;
0792     case I2C_SMBUS_I2C_BLOCK_DATA:
0793         if (read_length > I2C_SMBUS_BLOCK_MAX) {
0794             ret = -EINVAL;
0795             goto power_normal;
0796         }
0797 
0798         memcpy(data->block + 1, buf, read_length);
0799         break;
0800     case I2C_SMBUS_BLOCK_DATA:
0801         if (read_length > I2C_SMBUS_BLOCK_MAX) {
0802             ret = -EPROTO;
0803             goto power_normal;
0804         }
0805 
0806         memcpy(data->block, buf, read_length);
0807         break;
0808     }
0809 
0810     ret = 0;
0811 power_normal:
0812     hid_hw_power(hdev, PM_HINT_NORMAL);
0813     hid_dbg(hdev, "transfer finished: %d\n", ret);
0814     return ret;
0815 }
0816 
0817 static u32 cp2112_functionality(struct i2c_adapter *adap)
0818 {
0819     return I2C_FUNC_I2C |
0820         I2C_FUNC_SMBUS_BYTE |
0821         I2C_FUNC_SMBUS_BYTE_DATA |
0822         I2C_FUNC_SMBUS_WORD_DATA |
0823         I2C_FUNC_SMBUS_BLOCK_DATA |
0824         I2C_FUNC_SMBUS_I2C_BLOCK |
0825         I2C_FUNC_SMBUS_PROC_CALL |
0826         I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
0827 }
0828 
0829 static const struct i2c_algorithm smbus_algorithm = {
0830     .master_xfer    = cp2112_i2c_xfer,
0831     .smbus_xfer = cp2112_xfer,
0832     .functionality  = cp2112_functionality,
0833 };
0834 
0835 static int cp2112_get_usb_config(struct hid_device *hdev,
0836                  struct cp2112_usb_config_report *cfg)
0837 {
0838     int ret;
0839 
0840     ret = cp2112_hid_get(hdev, CP2112_USB_CONFIG, (u8 *)cfg, sizeof(*cfg),
0841                  HID_FEATURE_REPORT);
0842     if (ret != sizeof(*cfg)) {
0843         hid_err(hdev, "error reading usb config: %d\n", ret);
0844         if (ret < 0)
0845             return ret;
0846         return -EIO;
0847     }
0848 
0849     return 0;
0850 }
0851 
0852 static int cp2112_set_usb_config(struct hid_device *hdev,
0853                  struct cp2112_usb_config_report *cfg)
0854 {
0855     int ret;
0856 
0857     BUG_ON(cfg->report != CP2112_USB_CONFIG);
0858 
0859     ret = cp2112_hid_output(hdev, (u8 *)cfg, sizeof(*cfg),
0860                 HID_FEATURE_REPORT);
0861     if (ret != sizeof(*cfg)) {
0862         hid_err(hdev, "error writing usb config: %d\n", ret);
0863         if (ret < 0)
0864             return ret;
0865         return -EIO;
0866     }
0867 
0868     return 0;
0869 }
0870 
0871 static void chmod_sysfs_attrs(struct hid_device *hdev);
0872 
0873 #define CP2112_CONFIG_ATTR(name, store, format, ...) \
0874 static ssize_t name##_store(struct device *kdev, \
0875                 struct device_attribute *attr, const char *buf, \
0876                 size_t count) \
0877 { \
0878     struct hid_device *hdev = to_hid_device(kdev); \
0879     struct cp2112_usb_config_report cfg; \
0880     int ret = cp2112_get_usb_config(hdev, &cfg); \
0881     if (ret) \
0882         return ret; \
0883     store; \
0884     ret = cp2112_set_usb_config(hdev, &cfg); \
0885     if (ret) \
0886         return ret; \
0887     chmod_sysfs_attrs(hdev); \
0888     return count; \
0889 } \
0890 static ssize_t name##_show(struct device *kdev, \
0891                struct device_attribute *attr, char *buf) \
0892 { \
0893     struct hid_device *hdev = to_hid_device(kdev); \
0894     struct cp2112_usb_config_report cfg; \
0895     int ret = cp2112_get_usb_config(hdev, &cfg); \
0896     if (ret) \
0897         return ret; \
0898     return scnprintf(buf, PAGE_SIZE, format, ##__VA_ARGS__); \
0899 } \
0900 static DEVICE_ATTR_RW(name);
0901 
0902 CP2112_CONFIG_ATTR(vendor_id, ({
0903     u16 vid;
0904 
0905     if (sscanf(buf, "%hi", &vid) != 1)
0906         return -EINVAL;
0907 
0908     cfg.vid = cpu_to_le16(vid);
0909     cfg.mask = 0x01;
0910 }), "0x%04x\n", le16_to_cpu(cfg.vid));
0911 
0912 CP2112_CONFIG_ATTR(product_id, ({
0913     u16 pid;
0914 
0915     if (sscanf(buf, "%hi", &pid) != 1)
0916         return -EINVAL;
0917 
0918     cfg.pid = cpu_to_le16(pid);
0919     cfg.mask = 0x02;
0920 }), "0x%04x\n", le16_to_cpu(cfg.pid));
0921 
0922 CP2112_CONFIG_ATTR(max_power, ({
0923     int mA;
0924 
0925     if (sscanf(buf, "%i", &mA) != 1)
0926         return -EINVAL;
0927 
0928     cfg.max_power = (mA + 1) / 2;
0929     cfg.mask = 0x04;
0930 }), "%u mA\n", cfg.max_power * 2);
0931 
0932 CP2112_CONFIG_ATTR(power_mode, ({
0933     if (sscanf(buf, "%hhi", &cfg.power_mode) != 1)
0934         return -EINVAL;
0935 
0936     cfg.mask = 0x08;
0937 }), "%u\n", cfg.power_mode);
0938 
0939 CP2112_CONFIG_ATTR(release_version, ({
0940     if (sscanf(buf, "%hhi.%hhi", &cfg.release_major, &cfg.release_minor)
0941         != 2)
0942         return -EINVAL;
0943 
0944     cfg.mask = 0x10;
0945 }), "%u.%u\n", cfg.release_major, cfg.release_minor);
0946 
0947 #undef CP2112_CONFIG_ATTR
0948 
0949 struct cp2112_pstring_attribute {
0950     struct device_attribute attr;
0951     unsigned char report;
0952 };
0953 
0954 static ssize_t pstr_store(struct device *kdev,
0955               struct device_attribute *kattr, const char *buf,
0956               size_t count)
0957 {
0958     struct hid_device *hdev = to_hid_device(kdev);
0959     struct cp2112_pstring_attribute *attr =
0960         container_of(kattr, struct cp2112_pstring_attribute, attr);
0961     struct cp2112_string_report report;
0962     int ret;
0963 
0964     memset(&report, 0, sizeof(report));
0965 
0966     ret = utf8s_to_utf16s(buf, count, UTF16_LITTLE_ENDIAN,
0967                   report.string, ARRAY_SIZE(report.string));
0968     report.report = attr->report;
0969     report.length = ret * sizeof(report.string[0]) + 2;
0970     report.type = USB_DT_STRING;
0971 
0972     ret = cp2112_hid_output(hdev, &report.report, report.length + 1,
0973                 HID_FEATURE_REPORT);
0974     if (ret != report.length + 1) {
0975         hid_err(hdev, "error writing %s string: %d\n", kattr->attr.name,
0976             ret);
0977         if (ret < 0)
0978             return ret;
0979         return -EIO;
0980     }
0981 
0982     chmod_sysfs_attrs(hdev);
0983     return count;
0984 }
0985 
0986 static ssize_t pstr_show(struct device *kdev,
0987              struct device_attribute *kattr, char *buf)
0988 {
0989     struct hid_device *hdev = to_hid_device(kdev);
0990     struct cp2112_pstring_attribute *attr =
0991         container_of(kattr, struct cp2112_pstring_attribute, attr);
0992     struct cp2112_string_report report;
0993     u8 length;
0994     int ret;
0995 
0996     ret = cp2112_hid_get(hdev, attr->report, (u8 *)&report.contents,
0997                  sizeof(report.contents), HID_FEATURE_REPORT);
0998     if (ret < 3) {
0999         hid_err(hdev, "error reading %s string: %d\n", kattr->attr.name,
1000             ret);
1001         if (ret < 0)
1002             return ret;
1003         return -EIO;
1004     }
1005 
1006     if (report.length < 2) {
1007         hid_err(hdev, "invalid %s string length: %d\n",
1008             kattr->attr.name, report.length);
1009         return -EIO;
1010     }
1011 
1012     length = report.length > ret - 1 ? ret - 1 : report.length;
1013     length = (length - 2) / sizeof(report.string[0]);
1014     ret = utf16s_to_utf8s(report.string, length, UTF16_LITTLE_ENDIAN, buf,
1015                   PAGE_SIZE - 1);
1016     buf[ret++] = '\n';
1017     return ret;
1018 }
1019 
1020 #define CP2112_PSTR_ATTR(name, _report) \
1021 static struct cp2112_pstring_attribute dev_attr_##name = { \
1022     .attr = __ATTR(name, (S_IWUSR | S_IRUGO), pstr_show, pstr_store), \
1023     .report = _report, \
1024 };
1025 
1026 CP2112_PSTR_ATTR(manufacturer,  CP2112_MANUFACTURER_STRING);
1027 CP2112_PSTR_ATTR(product,   CP2112_PRODUCT_STRING);
1028 CP2112_PSTR_ATTR(serial,    CP2112_SERIAL_STRING);
1029 
1030 #undef CP2112_PSTR_ATTR
1031 
1032 static const struct attribute_group cp2112_attr_group = {
1033     .attrs = (struct attribute *[]){
1034         &dev_attr_vendor_id.attr,
1035         &dev_attr_product_id.attr,
1036         &dev_attr_max_power.attr,
1037         &dev_attr_power_mode.attr,
1038         &dev_attr_release_version.attr,
1039         &dev_attr_manufacturer.attr.attr,
1040         &dev_attr_product.attr.attr,
1041         &dev_attr_serial.attr.attr,
1042         NULL
1043     }
1044 };
1045 
1046 /* Chmoding our sysfs attributes is simply a way to expose which fields in the
1047  * PROM have already been programmed. We do not depend on this preventing
1048  * writing to these attributes since the CP2112 will simply ignore writes to
1049  * already-programmed fields. This is why there is no sense in fixing this
1050  * racy behaviour.
1051  */
1052 static void chmod_sysfs_attrs(struct hid_device *hdev)
1053 {
1054     struct attribute **attr;
1055     u8 buf[2];
1056     int ret;
1057 
1058     ret = cp2112_hid_get(hdev, CP2112_LOCK_BYTE, buf, sizeof(buf),
1059                  HID_FEATURE_REPORT);
1060     if (ret != sizeof(buf)) {
1061         hid_err(hdev, "error reading lock byte: %d\n", ret);
1062         return;
1063     }
1064 
1065     for (attr = cp2112_attr_group.attrs; *attr; ++attr) {
1066         umode_t mode = (buf[1] & 1) ? S_IWUSR | S_IRUGO : S_IRUGO;
1067         ret = sysfs_chmod_file(&hdev->dev.kobj, *attr, mode);
1068         if (ret < 0)
1069             hid_err(hdev, "error chmoding sysfs file %s\n",
1070                 (*attr)->name);
1071         buf[1] >>= 1;
1072     }
1073 }
1074 
1075 static void cp2112_gpio_irq_ack(struct irq_data *d)
1076 {
1077 }
1078 
1079 static void cp2112_gpio_irq_mask(struct irq_data *d)
1080 {
1081     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1082     struct cp2112_device *dev = gpiochip_get_data(gc);
1083 
1084     __clear_bit(d->hwirq, &dev->irq_mask);
1085 }
1086 
1087 static void cp2112_gpio_irq_unmask(struct irq_data *d)
1088 {
1089     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1090     struct cp2112_device *dev = gpiochip_get_data(gc);
1091 
1092     __set_bit(d->hwirq, &dev->irq_mask);
1093 }
1094 
1095 static void cp2112_gpio_poll_callback(struct work_struct *work)
1096 {
1097     struct cp2112_device *dev = container_of(work, struct cp2112_device,
1098                          gpio_poll_worker.work);
1099     struct irq_data *d;
1100     u8 gpio_mask;
1101     u8 virqs = (u8)dev->irq_mask;
1102     u32 irq_type;
1103     int irq, virq, ret;
1104 
1105     ret = cp2112_gpio_get_all(&dev->gc);
1106     if (ret == -ENODEV) /* the hardware has been disconnected */
1107         return;
1108     if (ret < 0)
1109         goto exit;
1110 
1111     gpio_mask = ret;
1112 
1113     while (virqs) {
1114         virq = ffs(virqs) - 1;
1115         virqs &= ~BIT(virq);
1116 
1117         if (!dev->gc.to_irq)
1118             break;
1119 
1120         irq = dev->gc.to_irq(&dev->gc, virq);
1121 
1122         d = irq_get_irq_data(irq);
1123         if (!d)
1124             continue;
1125 
1126         irq_type = irqd_get_trigger_type(d);
1127 
1128         if (gpio_mask & BIT(virq)) {
1129             /* Level High */
1130 
1131             if (irq_type & IRQ_TYPE_LEVEL_HIGH)
1132                 handle_nested_irq(irq);
1133 
1134             if ((irq_type & IRQ_TYPE_EDGE_RISING) &&
1135                 !(dev->gpio_prev_state & BIT(virq)))
1136                 handle_nested_irq(irq);
1137         } else {
1138             /* Level Low */
1139 
1140             if (irq_type & IRQ_TYPE_LEVEL_LOW)
1141                 handle_nested_irq(irq);
1142 
1143             if ((irq_type & IRQ_TYPE_EDGE_FALLING) &&
1144                 (dev->gpio_prev_state & BIT(virq)))
1145                 handle_nested_irq(irq);
1146         }
1147     }
1148 
1149     dev->gpio_prev_state = gpio_mask;
1150 
1151 exit:
1152     if (dev->gpio_poll)
1153         schedule_delayed_work(&dev->gpio_poll_worker, 10);
1154 }
1155 
1156 
1157 static unsigned int cp2112_gpio_irq_startup(struct irq_data *d)
1158 {
1159     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1160     struct cp2112_device *dev = gpiochip_get_data(gc);
1161 
1162     INIT_DELAYED_WORK(&dev->gpio_poll_worker, cp2112_gpio_poll_callback);
1163 
1164     if (!dev->gpio_poll) {
1165         dev->gpio_poll = true;
1166         schedule_delayed_work(&dev->gpio_poll_worker, 0);
1167     }
1168 
1169     cp2112_gpio_irq_unmask(d);
1170     return 0;
1171 }
1172 
1173 static void cp2112_gpio_irq_shutdown(struct irq_data *d)
1174 {
1175     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1176     struct cp2112_device *dev = gpiochip_get_data(gc);
1177 
1178     cancel_delayed_work_sync(&dev->gpio_poll_worker);
1179 }
1180 
1181 static int cp2112_gpio_irq_type(struct irq_data *d, unsigned int type)
1182 {
1183     return 0;
1184 }
1185 
1186 static int __maybe_unused cp2112_allocate_irq(struct cp2112_device *dev,
1187                           int pin)
1188 {
1189     int ret;
1190 
1191     if (dev->desc[pin])
1192         return -EINVAL;
1193 
1194     dev->desc[pin] = gpiochip_request_own_desc(&dev->gc, pin,
1195                            "HID/I2C:Event",
1196                            GPIO_ACTIVE_HIGH,
1197                            GPIOD_IN);
1198     if (IS_ERR(dev->desc[pin])) {
1199         dev_err(dev->gc.parent, "Failed to request GPIO\n");
1200         return PTR_ERR(dev->desc[pin]);
1201     }
1202 
1203     ret = cp2112_gpio_direction_input(&dev->gc, pin);
1204     if (ret < 0) {
1205         dev_err(dev->gc.parent, "Failed to set GPIO to input dir\n");
1206         goto err_desc;
1207     }
1208 
1209     ret = gpiochip_lock_as_irq(&dev->gc, pin);
1210     if (ret) {
1211         dev_err(dev->gc.parent, "Failed to lock GPIO as interrupt\n");
1212         goto err_desc;
1213     }
1214 
1215     ret = gpiod_to_irq(dev->desc[pin]);
1216     if (ret < 0) {
1217         dev_err(dev->gc.parent, "Failed to translate GPIO to IRQ\n");
1218         goto err_lock;
1219     }
1220 
1221     return ret;
1222 
1223 err_lock:
1224     gpiochip_unlock_as_irq(&dev->gc, pin);
1225 err_desc:
1226     gpiochip_free_own_desc(dev->desc[pin]);
1227     dev->desc[pin] = NULL;
1228     return ret;
1229 }
1230 
1231 static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
1232 {
1233     struct cp2112_device *dev;
1234     u8 buf[3];
1235     struct cp2112_smbus_config_report config;
1236     struct gpio_irq_chip *girq;
1237     int ret;
1238 
1239     dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
1240     if (!dev)
1241         return -ENOMEM;
1242 
1243     dev->in_out_buffer = devm_kzalloc(&hdev->dev, CP2112_REPORT_MAX_LENGTH,
1244                       GFP_KERNEL);
1245     if (!dev->in_out_buffer)
1246         return -ENOMEM;
1247 
1248     mutex_init(&dev->lock);
1249 
1250     ret = hid_parse(hdev);
1251     if (ret) {
1252         hid_err(hdev, "parse failed\n");
1253         return ret;
1254     }
1255 
1256     ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1257     if (ret) {
1258         hid_err(hdev, "hw start failed\n");
1259         return ret;
1260     }
1261 
1262     ret = hid_hw_open(hdev);
1263     if (ret) {
1264         hid_err(hdev, "hw open failed\n");
1265         goto err_hid_stop;
1266     }
1267 
1268     ret = hid_hw_power(hdev, PM_HINT_FULLON);
1269     if (ret < 0) {
1270         hid_err(hdev, "power management error: %d\n", ret);
1271         goto err_hid_close;
1272     }
1273 
1274     ret = cp2112_hid_get(hdev, CP2112_GET_VERSION_INFO, buf, sizeof(buf),
1275                  HID_FEATURE_REPORT);
1276     if (ret != sizeof(buf)) {
1277         hid_err(hdev, "error requesting version\n");
1278         if (ret >= 0)
1279             ret = -EIO;
1280         goto err_power_normal;
1281     }
1282 
1283     hid_info(hdev, "Part Number: 0x%02X Device Version: 0x%02X\n",
1284          buf[1], buf[2]);
1285 
1286     ret = cp2112_hid_get(hdev, CP2112_SMBUS_CONFIG, (u8 *)&config,
1287                  sizeof(config), HID_FEATURE_REPORT);
1288     if (ret != sizeof(config)) {
1289         hid_err(hdev, "error requesting SMBus config\n");
1290         if (ret >= 0)
1291             ret = -EIO;
1292         goto err_power_normal;
1293     }
1294 
1295     config.retry_time = cpu_to_be16(1);
1296 
1297     ret = cp2112_hid_output(hdev, (u8 *)&config, sizeof(config),
1298                 HID_FEATURE_REPORT);
1299     if (ret != sizeof(config)) {
1300         hid_err(hdev, "error setting SMBus config\n");
1301         if (ret >= 0)
1302             ret = -EIO;
1303         goto err_power_normal;
1304     }
1305 
1306     hid_set_drvdata(hdev, (void *)dev);
1307     dev->hdev       = hdev;
1308     dev->adap.owner     = THIS_MODULE;
1309     dev->adap.class     = I2C_CLASS_HWMON;
1310     dev->adap.algo      = &smbus_algorithm;
1311     dev->adap.algo_data = dev;
1312     dev->adap.dev.parent    = &hdev->dev;
1313     snprintf(dev->adap.name, sizeof(dev->adap.name),
1314          "CP2112 SMBus Bridge on hidraw%d",
1315          ((struct hidraw *)hdev->hidraw)->minor);
1316     dev->hwversion = buf[2];
1317     init_waitqueue_head(&dev->wait);
1318 
1319     hid_device_io_start(hdev);
1320     ret = i2c_add_adapter(&dev->adap);
1321     hid_device_io_stop(hdev);
1322 
1323     if (ret) {
1324         hid_err(hdev, "error registering i2c adapter\n");
1325         goto err_power_normal;
1326     }
1327 
1328     hid_dbg(hdev, "adapter registered\n");
1329 
1330     dev->gc.label           = "cp2112_gpio";
1331     dev->gc.direction_input     = cp2112_gpio_direction_input;
1332     dev->gc.direction_output    = cp2112_gpio_direction_output;
1333     dev->gc.set         = cp2112_gpio_set;
1334     dev->gc.get         = cp2112_gpio_get;
1335     dev->gc.base            = -1;
1336     dev->gc.ngpio           = 8;
1337     dev->gc.can_sleep       = 1;
1338     dev->gc.parent          = &hdev->dev;
1339 
1340     dev->irq.name = "cp2112-gpio";
1341     dev->irq.irq_startup = cp2112_gpio_irq_startup;
1342     dev->irq.irq_shutdown = cp2112_gpio_irq_shutdown;
1343     dev->irq.irq_ack = cp2112_gpio_irq_ack;
1344     dev->irq.irq_mask = cp2112_gpio_irq_mask;
1345     dev->irq.irq_unmask = cp2112_gpio_irq_unmask;
1346     dev->irq.irq_set_type = cp2112_gpio_irq_type;
1347     dev->irq.flags = IRQCHIP_MASK_ON_SUSPEND;
1348 
1349     girq = &dev->gc.irq;
1350     girq->chip = &dev->irq;
1351     /* The event comes from the outside so no parent handler */
1352     girq->parent_handler = NULL;
1353     girq->num_parents = 0;
1354     girq->parents = NULL;
1355     girq->default_type = IRQ_TYPE_NONE;
1356     girq->handler = handle_simple_irq;
1357 
1358     ret = gpiochip_add_data(&dev->gc, dev);
1359     if (ret < 0) {
1360         hid_err(hdev, "error registering gpio chip\n");
1361         goto err_free_i2c;
1362     }
1363 
1364     ret = sysfs_create_group(&hdev->dev.kobj, &cp2112_attr_group);
1365     if (ret < 0) {
1366         hid_err(hdev, "error creating sysfs attrs\n");
1367         goto err_gpiochip_remove;
1368     }
1369 
1370     chmod_sysfs_attrs(hdev);
1371     hid_hw_power(hdev, PM_HINT_NORMAL);
1372 
1373     return ret;
1374 
1375 err_gpiochip_remove:
1376     gpiochip_remove(&dev->gc);
1377 err_free_i2c:
1378     i2c_del_adapter(&dev->adap);
1379 err_power_normal:
1380     hid_hw_power(hdev, PM_HINT_NORMAL);
1381 err_hid_close:
1382     hid_hw_close(hdev);
1383 err_hid_stop:
1384     hid_hw_stop(hdev);
1385     return ret;
1386 }
1387 
1388 static void cp2112_remove(struct hid_device *hdev)
1389 {
1390     struct cp2112_device *dev = hid_get_drvdata(hdev);
1391     int i;
1392 
1393     sysfs_remove_group(&hdev->dev.kobj, &cp2112_attr_group);
1394     i2c_del_adapter(&dev->adap);
1395 
1396     if (dev->gpio_poll) {
1397         dev->gpio_poll = false;
1398         cancel_delayed_work_sync(&dev->gpio_poll_worker);
1399     }
1400 
1401     for (i = 0; i < ARRAY_SIZE(dev->desc); i++) {
1402         gpiochip_unlock_as_irq(&dev->gc, i);
1403         gpiochip_free_own_desc(dev->desc[i]);
1404     }
1405 
1406     gpiochip_remove(&dev->gc);
1407     /* i2c_del_adapter has finished removing all i2c devices from our
1408      * adapter. Well behaved devices should no longer call our cp2112_xfer
1409      * and should have waited for any pending calls to finish. It has also
1410      * waited for device_unregister(&adap->dev) to complete. Therefore we
1411      * can safely free our struct cp2112_device.
1412      */
1413     hid_hw_close(hdev);
1414     hid_hw_stop(hdev);
1415 }
1416 
1417 static int cp2112_raw_event(struct hid_device *hdev, struct hid_report *report,
1418                 u8 *data, int size)
1419 {
1420     struct cp2112_device *dev = hid_get_drvdata(hdev);
1421     struct cp2112_xfer_status_report *xfer = (void *)data;
1422 
1423     switch (data[0]) {
1424     case CP2112_TRANSFER_STATUS_RESPONSE:
1425         hid_dbg(hdev, "xfer status: %02x %02x %04x %04x\n",
1426             xfer->status0, xfer->status1,
1427             be16_to_cpu(xfer->retries), be16_to_cpu(xfer->length));
1428 
1429         switch (xfer->status0) {
1430         case STATUS0_IDLE:
1431             dev->xfer_status = -EAGAIN;
1432             break;
1433         case STATUS0_BUSY:
1434             dev->xfer_status = -EBUSY;
1435             break;
1436         case STATUS0_COMPLETE:
1437             dev->xfer_status = be16_to_cpu(xfer->length);
1438             break;
1439         case STATUS0_ERROR:
1440             switch (xfer->status1) {
1441             case STATUS1_TIMEOUT_NACK:
1442             case STATUS1_TIMEOUT_BUS:
1443                 dev->xfer_status = -ETIMEDOUT;
1444                 break;
1445             default:
1446                 dev->xfer_status = -EIO;
1447                 break;
1448             }
1449             break;
1450         default:
1451             dev->xfer_status = -EINVAL;
1452             break;
1453         }
1454 
1455         atomic_set(&dev->xfer_avail, 1);
1456         break;
1457     case CP2112_DATA_READ_RESPONSE:
1458         hid_dbg(hdev, "read response: %02x %02x\n", data[1], data[2]);
1459 
1460         dev->read_length = data[2];
1461         if (dev->read_length > sizeof(dev->read_data))
1462             dev->read_length = sizeof(dev->read_data);
1463 
1464         memcpy(dev->read_data, &data[3], dev->read_length);
1465         atomic_set(&dev->read_avail, 1);
1466         break;
1467     default:
1468         hid_err(hdev, "unknown report\n");
1469 
1470         return 0;
1471     }
1472 
1473     wake_up_interruptible(&dev->wait);
1474     return 1;
1475 }
1476 
1477 static struct hid_driver cp2112_driver = {
1478     .name       = "cp2112",
1479     .id_table   = cp2112_devices,
1480     .probe      = cp2112_probe,
1481     .remove     = cp2112_remove,
1482     .raw_event  = cp2112_raw_event,
1483 };
1484 
1485 module_hid_driver(cp2112_driver);
1486 MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
1487 MODULE_AUTHOR("David Barksdale <dbarksdale@uplogix.com>");
1488 MODULE_LICENSE("GPL");
1489