Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
0004 //
0005 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
0006 //            Markus Rechberger <mrechberger@gmail.com>
0007 //            Mauro Carvalho Chehab <mchehab@kernel.org>
0008 //            Sascha Sommer <saschasommer@freenet.de>
0009 // Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
0010 
0011 #include "em28xx.h"
0012 
0013 #include <linux/module.h>
0014 #include <linux/kernel.h>
0015 #include <linux/usb.h>
0016 #include <linux/i2c.h>
0017 #include <linux/jiffies.h>
0018 
0019 #include "xc2028.h"
0020 #include <media/v4l2-common.h>
0021 #include <media/tuner.h>
0022 
0023 /* ----------------------------------------------------------- */
0024 
0025 static unsigned int i2c_scan;
0026 module_param(i2c_scan, int, 0444);
0027 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
0028 
0029 static unsigned int i2c_debug;
0030 module_param(i2c_debug, int, 0644);
0031 MODULE_PARM_DESC(i2c_debug, "i2c debug message level (1: normal debug, 2: show I2C transfers)");
0032 
0033 #define dprintk(level, fmt, arg...) do {                \
0034     if (i2c_debug > level)                      \
0035         dev_printk(KERN_DEBUG, &dev->intf->dev,         \
0036                "i2c: %s: " fmt, __func__, ## arg);      \
0037 } while (0)
0038 
0039 /*
0040  * Time in msecs to wait for i2c xfers to finish.
0041  * 35ms is the maximum time a SMBUS device could wait when
0042  * clock stretching is used. As the transfer itself will take
0043  * some time to happen, set it to 35 ms.
0044  *
0045  * Ok, I2C doesn't specify any limit. So, eventually, we may need
0046  * to increase this timeout.
0047  */
0048 #define EM28XX_I2C_XFER_TIMEOUT         35 /* ms */
0049 
0050 static int em28xx_i2c_timeout(struct em28xx *dev)
0051 {
0052     int time = EM28XX_I2C_XFER_TIMEOUT;
0053 
0054     switch (dev->i2c_speed & 0x03) {
0055     case EM28XX_I2C_FREQ_25_KHZ:
0056         time += 4;      /* Assume 4 ms for transfers */
0057         break;
0058     case EM28XX_I2C_FREQ_100_KHZ:
0059     case EM28XX_I2C_FREQ_400_KHZ:
0060         time += 1;      /* Assume 1 ms for transfers */
0061         break;
0062     default: /* EM28XX_I2C_FREQ_1_5_MHZ */
0063         break;
0064     }
0065 
0066     return msecs_to_jiffies(time);
0067 }
0068 
0069 /*
0070  * em2800_i2c_send_bytes()
0071  * send up to 4 bytes to the em2800 i2c device
0072  */
0073 static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
0074 {
0075     unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
0076     int ret;
0077     u8 b2[6];
0078 
0079     if (len < 1 || len > 4)
0080         return -EOPNOTSUPP;
0081 
0082     b2[5] = 0x80 + len - 1;
0083     b2[4] = addr;
0084     b2[3] = buf[0];
0085     if (len > 1)
0086         b2[2] = buf[1];
0087     if (len > 2)
0088         b2[1] = buf[2];
0089     if (len > 3)
0090         b2[0] = buf[3];
0091 
0092     /* trigger write */
0093     ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
0094     if (ret != 2 + len) {
0095         dev_warn(&dev->intf->dev,
0096              "failed to trigger write to i2c address 0x%x (error=%i)\n",
0097                 addr, ret);
0098         return (ret < 0) ? ret : -EIO;
0099     }
0100     /* wait for completion */
0101     while (time_is_after_jiffies(timeout)) {
0102         ret = dev->em28xx_read_reg(dev, 0x05);
0103         if (ret == 0x80 + len - 1)
0104             return len;
0105         if (ret == 0x94 + len - 1) {
0106             dprintk(1, "R05 returned 0x%02x: I2C ACK error\n", ret);
0107             return -ENXIO;
0108         }
0109         if (ret < 0) {
0110             dev_warn(&dev->intf->dev,
0111                  "failed to get i2c transfer status from bridge register (error=%i)\n",
0112                 ret);
0113             return ret;
0114         }
0115         usleep_range(5000, 6000);
0116     }
0117     dprintk(0, "write to i2c device at 0x%x timed out\n", addr);
0118     return -ETIMEDOUT;
0119 }
0120 
0121 /*
0122  * em2800_i2c_recv_bytes()
0123  * read up to 4 bytes from the em2800 i2c device
0124  */
0125 static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
0126 {
0127     unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
0128     u8 buf2[4];
0129     int ret;
0130     int i;
0131 
0132     if (len < 1 || len > 4)
0133         return -EOPNOTSUPP;
0134 
0135     /* trigger read */
0136     buf2[1] = 0x84 + len - 1;
0137     buf2[0] = addr;
0138     ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
0139     if (ret != 2) {
0140         dev_warn(&dev->intf->dev,
0141              "failed to trigger read from i2c address 0x%x (error=%i)\n",
0142              addr, ret);
0143         return (ret < 0) ? ret : -EIO;
0144     }
0145 
0146     /* wait for completion */
0147     while (time_is_after_jiffies(timeout)) {
0148         ret = dev->em28xx_read_reg(dev, 0x05);
0149         if (ret == 0x84 + len - 1)
0150             break;
0151         if (ret == 0x94 + len - 1) {
0152             dprintk(1, "R05 returned 0x%02x: I2C ACK error\n",
0153                 ret);
0154             return -ENXIO;
0155         }
0156         if (ret < 0) {
0157             dev_warn(&dev->intf->dev,
0158                  "failed to get i2c transfer status from bridge register (error=%i)\n",
0159                  ret);
0160             return ret;
0161         }
0162         usleep_range(5000, 6000);
0163     }
0164     if (ret != 0x84 + len - 1)
0165         dprintk(0, "read from i2c device at 0x%x timed out\n", addr);
0166 
0167     /* get the received message */
0168     ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4 - len, buf2, len);
0169     if (ret != len) {
0170         dev_warn(&dev->intf->dev,
0171              "reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
0172              addr, ret);
0173         return (ret < 0) ? ret : -EIO;
0174     }
0175     for (i = 0; i < len; i++)
0176         buf[i] = buf2[len - 1 - i];
0177 
0178     return ret;
0179 }
0180 
0181 /*
0182  * em2800_i2c_check_for_device()
0183  * check if there is an i2c device at the supplied address
0184  */
0185 static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
0186 {
0187     u8 buf;
0188     int ret;
0189 
0190     ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
0191     if (ret == 1)
0192         return 0;
0193     return (ret < 0) ? ret : -EIO;
0194 }
0195 
0196 /*
0197  * em28xx_i2c_send_bytes()
0198  */
0199 static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
0200                  u16 len, int stop)
0201 {
0202     unsigned long timeout = jiffies + em28xx_i2c_timeout(dev);
0203     int ret;
0204 
0205     if (len < 1 || len > 64)
0206         return -EOPNOTSUPP;
0207     /*
0208      * NOTE: limited by the USB ctrl message constraints
0209      * Zero length reads always succeed, even if no device is connected
0210      */
0211 
0212     /* Write to i2c device */
0213     ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
0214     if (ret != len) {
0215         if (ret < 0) {
0216             dev_warn(&dev->intf->dev,
0217                  "writing to i2c device at 0x%x failed (error=%i)\n",
0218                  addr, ret);
0219             return ret;
0220         }
0221         dev_warn(&dev->intf->dev,
0222              "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
0223                 len, addr, ret);
0224         return -EIO;
0225     }
0226 
0227     /* wait for completion */
0228     while (time_is_after_jiffies(timeout)) {
0229         ret = dev->em28xx_read_reg(dev, 0x05);
0230         if (ret == 0) /* success */
0231             return len;
0232         if (ret == 0x10) {
0233             dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
0234                 addr);
0235             return -ENXIO;
0236         }
0237         if (ret < 0) {
0238             dev_warn(&dev->intf->dev,
0239                  "failed to get i2c transfer status from bridge register (error=%i)\n",
0240                  ret);
0241             return ret;
0242         }
0243         usleep_range(5000, 6000);
0244         /*
0245          * NOTE: do we really have to wait for success ?
0246          * Never seen anything else than 0x00 or 0x10
0247          * (even with high payload) ...
0248          */
0249     }
0250 
0251     if (ret == 0x02 || ret == 0x04) {
0252         /* NOTE: these errors seem to be related to clock stretching */
0253         dprintk(0,
0254             "write to i2c device at 0x%x timed out (status=%i)\n",
0255             addr, ret);
0256         return -ETIMEDOUT;
0257     }
0258 
0259     dev_warn(&dev->intf->dev,
0260          "write to i2c device at 0x%x failed with unknown error (status=%i)\n",
0261          addr, ret);
0262     return -EIO;
0263 }
0264 
0265 /*
0266  * em28xx_i2c_recv_bytes()
0267  * read a byte from the i2c device
0268  */
0269 static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
0270 {
0271     int ret;
0272 
0273     if (len < 1 || len > 64)
0274         return -EOPNOTSUPP;
0275     /*
0276      * NOTE: limited by the USB ctrl message constraints
0277      * Zero length reads always succeed, even if no device is connected
0278      */
0279 
0280     /* Read data from i2c device */
0281     ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
0282     if (ret < 0) {
0283         dev_warn(&dev->intf->dev,
0284              "reading from i2c device at 0x%x failed (error=%i)\n",
0285              addr, ret);
0286         return ret;
0287     } else if (ret != len) {
0288         dev_dbg(&dev->intf->dev,
0289             "%i bytes read from i2c device at 0x%x requested, but %i bytes written\n",
0290                 ret, addr, len);
0291     }
0292     /*
0293      * NOTE: some devices with two i2c buses have the bad habit to return 0
0294      * bytes if we are on bus B AND there was no write attempt to the
0295      * specified slave address before AND no device is present at the
0296      * requested slave address.
0297      * Anyway, the next check will fail with -ENXIO in this case, so avoid
0298      * spamming the system log on device probing and do nothing here.
0299      */
0300 
0301     /* Check success of the i2c operation */
0302     ret = dev->em28xx_read_reg(dev, 0x05);
0303     if (ret == 0) /* success */
0304         return len;
0305     if (ret < 0) {
0306         dev_warn(&dev->intf->dev,
0307              "failed to get i2c transfer status from bridge register (error=%i)\n",
0308              ret);
0309         return ret;
0310     }
0311     if (ret == 0x10) {
0312         dprintk(1, "I2C ACK error on writing to addr 0x%02x\n",
0313             addr);
0314         return -ENXIO;
0315     }
0316 
0317     if (ret == 0x02 || ret == 0x04) {
0318         /* NOTE: these errors seem to be related to clock stretching */
0319         dprintk(0,
0320             "write to i2c device at 0x%x timed out (status=%i)\n",
0321             addr, ret);
0322         return -ETIMEDOUT;
0323     }
0324 
0325     dev_warn(&dev->intf->dev,
0326          "read from i2c device at 0x%x failed with unknown error (status=%i)\n",
0327          addr, ret);
0328     return -EIO;
0329 }
0330 
0331 /*
0332  * em28xx_i2c_check_for_device()
0333  * check if there is a i2c_device at the supplied address
0334  */
0335 static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
0336 {
0337     int ret;
0338     u8 buf;
0339 
0340     ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
0341     if (ret == 1)
0342         return 0;
0343     return (ret < 0) ? ret : -EIO;
0344 }
0345 
0346 /*
0347  * em25xx_bus_B_send_bytes
0348  * write bytes to the i2c device
0349  */
0350 static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
0351                    u16 len)
0352 {
0353     int ret;
0354 
0355     if (len < 1 || len > 64)
0356         return -EOPNOTSUPP;
0357     /*
0358      * NOTE: limited by the USB ctrl message constraints
0359      * Zero length reads always succeed, even if no device is connected
0360      */
0361 
0362     /* Set register and write value */
0363     ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
0364     if (ret != len) {
0365         if (ret < 0) {
0366             dev_warn(&dev->intf->dev,
0367                  "writing to i2c device at 0x%x failed (error=%i)\n",
0368                  addr, ret);
0369             return ret;
0370         }
0371 
0372         dev_warn(&dev->intf->dev,
0373              "%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
0374              len, addr, ret);
0375         return -EIO;
0376     }
0377     /* Check success */
0378     ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
0379     /*
0380      * NOTE: the only error we've seen so far is
0381      * 0x01 when the slave device is not present
0382      */
0383     if (!ret)
0384         return len;
0385 
0386     if (ret > 0) {
0387         dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
0388         return -ENXIO;
0389     }
0390 
0391     return ret;
0392     /*
0393      * NOTE: With chip types (other chip IDs) which actually don't support
0394      * this operation, it seems to succeed ALWAYS ! (even if there is no
0395      * slave device or even no second i2c bus provided)
0396      */
0397 }
0398 
0399 /*
0400  * em25xx_bus_B_recv_bytes
0401  * read bytes from the i2c device
0402  */
0403 static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
0404                    u16 len)
0405 {
0406     int ret;
0407 
0408     if (len < 1 || len > 64)
0409         return -EOPNOTSUPP;
0410     /*
0411      * NOTE: limited by the USB ctrl message constraints
0412      * Zero length reads always succeed, even if no device is connected
0413      */
0414 
0415     /* Read value */
0416     ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
0417     if (ret < 0) {
0418         dev_warn(&dev->intf->dev,
0419              "reading from i2c device at 0x%x failed (error=%i)\n",
0420              addr, ret);
0421         return ret;
0422     }
0423     /*
0424      * NOTE: some devices with two i2c buses have the bad habit to return 0
0425      * bytes if we are on bus B AND there was no write attempt to the
0426      * specified slave address before AND no device is present at the
0427      * requested slave address.
0428      * Anyway, the next check will fail with -ENXIO in this case, so avoid
0429      * spamming the system log on device probing and do nothing here.
0430      */
0431 
0432     /* Check success */
0433     ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
0434     /*
0435      * NOTE: the only error we've seen so far is
0436      * 0x01 when the slave device is not present
0437      */
0438     if (!ret)
0439         return len;
0440 
0441     if (ret > 0) {
0442         dprintk(1, "Bus B R08 returned 0x%02x: I2C ACK error\n", ret);
0443         return -ENXIO;
0444     }
0445 
0446     return ret;
0447     /*
0448      * NOTE: With chip types (other chip IDs) which actually don't support
0449      * this operation, it seems to succeed ALWAYS ! (even if there is no
0450      * slave device or even no second i2c bus provided)
0451      */
0452 }
0453 
0454 /*
0455  * em25xx_bus_B_check_for_device()
0456  * check if there is a i2c device at the supplied address
0457  */
0458 static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
0459 {
0460     u8 buf;
0461     int ret;
0462 
0463     ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
0464     if (ret < 0)
0465         return ret;
0466 
0467     return 0;
0468     /*
0469      * NOTE: With chips which do not support this operation,
0470      * it seems to succeed ALWAYS ! (even if no device connected)
0471      */
0472 }
0473 
0474 static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
0475 {
0476     struct em28xx *dev = i2c_bus->dev;
0477     int rc = -EOPNOTSUPP;
0478 
0479     if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
0480         rc = em28xx_i2c_check_for_device(dev, addr);
0481     else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
0482         rc = em2800_i2c_check_for_device(dev, addr);
0483     else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
0484         rc = em25xx_bus_B_check_for_device(dev, addr);
0485     return rc;
0486 }
0487 
0488 static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
0489                  struct i2c_msg msg)
0490 {
0491     struct em28xx *dev = i2c_bus->dev;
0492     u16 addr = msg.addr << 1;
0493     int rc = -EOPNOTSUPP;
0494 
0495     if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
0496         rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
0497     else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
0498         rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
0499     else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
0500         rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
0501     return rc;
0502 }
0503 
0504 static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
0505                  struct i2c_msg msg, int stop)
0506 {
0507     struct em28xx *dev = i2c_bus->dev;
0508     u16 addr = msg.addr << 1;
0509     int rc = -EOPNOTSUPP;
0510 
0511     if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
0512         rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
0513     else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
0514         rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
0515     else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
0516         rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
0517     return rc;
0518 }
0519 
0520 /*
0521  * em28xx_i2c_xfer()
0522  * the main i2c transfer function
0523  */
0524 static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
0525                struct i2c_msg msgs[], int num)
0526 {
0527     struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
0528     struct em28xx *dev = i2c_bus->dev;
0529     unsigned int bus = i2c_bus->bus;
0530     int addr, rc, i;
0531     u8 reg;
0532 
0533     /*
0534      * prevent i2c xfer attempts after device is disconnected
0535      * some fe's try to do i2c writes/reads from their release
0536      * interfaces when called in disconnect path
0537      */
0538     if (dev->disconnected)
0539         return -ENODEV;
0540 
0541     if (!rt_mutex_trylock(&dev->i2c_bus_lock))
0542         return -EAGAIN;
0543 
0544     /* Switch I2C bus if needed */
0545     if (bus != dev->cur_i2c_bus &&
0546         i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
0547         if (bus == 1)
0548             reg = EM2874_I2C_SECONDARY_BUS_SELECT;
0549         else
0550             reg = 0;
0551         em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
0552                       EM2874_I2C_SECONDARY_BUS_SELECT);
0553         dev->cur_i2c_bus = bus;
0554     }
0555 
0556     for (i = 0; i < num; i++) {
0557         addr = msgs[i].addr << 1;
0558         if (!msgs[i].len) {
0559             /*
0560              * no len: check only for device presence
0561              * This code is only called during device probe.
0562              */
0563             rc = i2c_check_for_device(i2c_bus, addr);
0564 
0565             if (rc == -ENXIO)
0566                 rc = -ENODEV;
0567         } else if (msgs[i].flags & I2C_M_RD) {
0568             /* read bytes */
0569             rc = i2c_recv_bytes(i2c_bus, msgs[i]);
0570         } else {
0571             /* write bytes */
0572             rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
0573         }
0574 
0575         if (rc < 0)
0576             goto error;
0577 
0578         dprintk(2, "%s %s addr=%02x len=%d: %*ph\n",
0579             (msgs[i].flags & I2C_M_RD) ? "read" : "write",
0580             i == num - 1 ? "stop" : "nonstop",
0581             addr, msgs[i].len,
0582             msgs[i].len, msgs[i].buf);
0583     }
0584 
0585     rt_mutex_unlock(&dev->i2c_bus_lock);
0586     return num;
0587 
0588 error:
0589     dprintk(2, "%s %s addr=%02x len=%d: %sERROR: %i\n",
0590         (msgs[i].flags & I2C_M_RD) ? "read" : "write",
0591         i == num - 1 ? "stop" : "nonstop",
0592         addr, msgs[i].len,
0593         (rc == -ENODEV) ? "no device " : "",
0594         rc);
0595 
0596     rt_mutex_unlock(&dev->i2c_bus_lock);
0597     return rc;
0598 }
0599 
0600 /*
0601  * based on linux/sunrpc/svcauth.h and linux/hash.h
0602  * The original hash function returns a different value, if arch is x86_64
0603  * or i386.
0604  */
0605 static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
0606 {
0607     unsigned long hash = 0;
0608     unsigned long l = 0;
0609     int len = 0;
0610     unsigned char c;
0611 
0612     do {
0613         if (len == length) {
0614             c = (char)len;
0615             len = -1;
0616         } else {
0617             c = *buf++;
0618         }
0619         l = (l << 8) | c;
0620         len++;
0621         if ((len & (32 / 8 - 1)) == 0)
0622             hash = ((hash ^ l) * 0x9e370001UL);
0623     } while (len);
0624 
0625     return (hash >> (32 - bits)) & 0xffffffffUL;
0626 }
0627 
0628 /*
0629  * Helper function to read data blocks from i2c clients with 8 or 16 bit
0630  * address width, 8 bit register width and auto incrementation been activated
0631  */
0632 static int em28xx_i2c_read_block(struct em28xx *dev, unsigned int bus, u16 addr,
0633                  bool addr_w16, u16 len, u8 *data)
0634 {
0635     int remain = len, rsize, rsize_max, ret;
0636     u8 buf[2];
0637 
0638     /* Sanity check */
0639     if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
0640         return -EINVAL;
0641     /* Select address */
0642     buf[0] = addr >> 8;
0643     buf[1] = addr & 0xff;
0644     ret = i2c_master_send(&dev->i2c_client[bus],
0645                   buf + !addr_w16, 1 + addr_w16);
0646     if (ret < 0)
0647         return ret;
0648     /* Read data */
0649     if (dev->board.is_em2800)
0650         rsize_max = 4;
0651     else
0652         rsize_max = 64;
0653     while (remain > 0) {
0654         if (remain > rsize_max)
0655             rsize = rsize_max;
0656         else
0657             rsize = remain;
0658 
0659         ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
0660         if (ret < 0)
0661             return ret;
0662 
0663         remain -= rsize;
0664         data += rsize;
0665     }
0666 
0667     return len;
0668 }
0669 
0670 static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned int bus,
0671                  u8 **eedata, u16 *eedata_len)
0672 {
0673     const u16 len = 256;
0674     /*
0675      * FIXME common length/size for bytes to read, to display, hash
0676      * calculation and returned device dataset. Simplifies the code a lot,
0677      * but we might have to deal with multiple sizes in the future !
0678      */
0679     int err;
0680     struct em28xx_eeprom *dev_config;
0681     u8 buf, *data;
0682 
0683     *eedata = NULL;
0684     *eedata_len = 0;
0685 
0686     /* EEPROM is always on i2c bus 0 on all known devices. */
0687 
0688     dev->i2c_client[bus].addr = 0xa0 >> 1;
0689 
0690     /* Check if board has eeprom */
0691     err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
0692     if (err < 0) {
0693         dev_info(&dev->intf->dev, "board has no eeprom\n");
0694         return -ENODEV;
0695     }
0696 
0697     data = kzalloc(len, GFP_KERNEL);
0698     if (!data)
0699         return -ENOMEM;
0700 
0701     /* Read EEPROM content */
0702     err = em28xx_i2c_read_block(dev, bus, 0x0000,
0703                     dev->eeprom_addrwidth_16bit,
0704                     len, data);
0705     if (err != len) {
0706         dev_err(&dev->intf->dev,
0707             "failed to read eeprom (err=%d)\n", err);
0708         goto error;
0709     }
0710 
0711     if (i2c_debug) {
0712         /* Display eeprom content */
0713         print_hex_dump(KERN_DEBUG, "em28xx eeprom ", DUMP_PREFIX_OFFSET,
0714                    16, 1, data, len, true);
0715 
0716         if (dev->eeprom_addrwidth_16bit)
0717             dev_info(&dev->intf->dev,
0718                  "eeprom %06x: ... (skipped)\n", 256);
0719     }
0720 
0721     if (dev->eeprom_addrwidth_16bit &&
0722         data[0] == 0x26 && data[3] == 0x00) {
0723         /* new eeprom format; size 4-64kb */
0724         u16 mc_start;
0725         u16 hwconf_offset;
0726 
0727         dev->hash = em28xx_hash_mem(data, len, 32);
0728         mc_start = (data[1] << 8) + 4;  /* usually 0x0004 */
0729 
0730         dev_info(&dev->intf->dev,
0731              "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
0732              data, dev->hash);
0733         dev_info(&dev->intf->dev,
0734              "EEPROM info:\n");
0735         dev_info(&dev->intf->dev,
0736              "\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
0737              mc_start, data[2]);
0738         /*
0739          * boot configuration (address 0x0002):
0740          * [0]   microcode download speed: 1 = 400 kHz; 0 = 100 kHz
0741          * [1]   always selects 12 kb RAM
0742          * [2]   USB device speed: 1 = force Full Speed; 0 = auto detect
0743          * [4]   1 = force fast mode and no suspend for device testing
0744          * [5:7] USB PHY tuning registers; determined by device
0745          *       characterization
0746          */
0747 
0748         /*
0749          * Read hardware config dataset offset from address
0750          * (microcode start + 46)
0751          */
0752         err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
0753                         data);
0754         if (err != 2) {
0755             dev_err(&dev->intf->dev,
0756                 "failed to read hardware configuration data from eeprom (err=%d)\n",
0757                 err);
0758             goto error;
0759         }
0760 
0761         /* Calculate hardware config dataset start address */
0762         hwconf_offset = mc_start + data[0] + (data[1] << 8);
0763 
0764         /* Read hardware config dataset */
0765         /*
0766          * NOTE: the microcode copy can be multiple pages long, but
0767          * we assume the hardware config dataset is the same as in
0768          * the old eeprom and not longer than 256 bytes.
0769          * tveeprom is currently also limited to 256 bytes.
0770          */
0771         err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
0772                         data);
0773         if (err != len) {
0774             dev_err(&dev->intf->dev,
0775                 "failed to read hardware configuration data from eeprom (err=%d)\n",
0776                 err);
0777             goto error;
0778         }
0779 
0780         /* Verify hardware config dataset */
0781         /* NOTE: not all devices provide this type of dataset */
0782         if (data[0] != 0x1a || data[1] != 0xeb ||
0783             data[2] != 0x67 || data[3] != 0x95) {
0784             dev_info(&dev->intf->dev,
0785                  "\tno hardware configuration dataset found in eeprom\n");
0786             kfree(data);
0787             return 0;
0788         }
0789 
0790         /*
0791          * TODO: decrypt eeprom data for camera bridges
0792          * (em25xx, em276x+)
0793          */
0794 
0795     } else if (!dev->eeprom_addrwidth_16bit &&
0796            data[0] == 0x1a && data[1] == 0xeb &&
0797            data[2] == 0x67 && data[3] == 0x95) {
0798         dev->hash = em28xx_hash_mem(data, len, 32);
0799         dev_info(&dev->intf->dev,
0800              "EEPROM ID = %4ph, EEPROM hash = 0x%08lx\n",
0801              data, dev->hash);
0802         dev_info(&dev->intf->dev,
0803              "EEPROM info:\n");
0804     } else {
0805         dev_info(&dev->intf->dev,
0806              "unknown eeprom format or eeprom corrupted !\n");
0807         err = -ENODEV;
0808         goto error;
0809     }
0810 
0811     *eedata = data;
0812     *eedata_len = len;
0813     dev_config = (void *)*eedata;
0814 
0815     switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
0816     case 0:
0817         dev_info(&dev->intf->dev, "\tNo audio on board.\n");
0818         break;
0819     case 1:
0820         dev_info(&dev->intf->dev, "\tAC97 audio (5 sample rates)\n");
0821         break;
0822     case 2:
0823         if (dev->chip_id < CHIP_ID_EM2860)
0824             dev_info(&dev->intf->dev,
0825                  "\tI2S audio, sample rate=32k\n");
0826         else
0827             dev_info(&dev->intf->dev,
0828                  "\tI2S audio, 3 sample rates\n");
0829         break;
0830     case 3:
0831         if (dev->chip_id < CHIP_ID_EM2860)
0832             dev_info(&dev->intf->dev,
0833                  "\tI2S audio, 3 sample rates\n");
0834         else
0835             dev_info(&dev->intf->dev,
0836                  "\tI2S audio, 5 sample rates\n");
0837         break;
0838     }
0839 
0840     if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
0841         dev_info(&dev->intf->dev, "\tUSB Remote wakeup capable\n");
0842 
0843     if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
0844         dev_info(&dev->intf->dev, "\tUSB Self power capable\n");
0845 
0846     switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
0847     case 0:
0848         dev_info(&dev->intf->dev, "\t500mA max power\n");
0849         break;
0850     case 1:
0851         dev_info(&dev->intf->dev, "\t400mA max power\n");
0852         break;
0853     case 2:
0854         dev_info(&dev->intf->dev, "\t300mA max power\n");
0855         break;
0856     case 3:
0857         dev_info(&dev->intf->dev, "\t200mA max power\n");
0858         break;
0859     }
0860     dev_info(&dev->intf->dev,
0861          "\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
0862          dev_config->string_idx_table,
0863          le16_to_cpu(dev_config->string1),
0864          le16_to_cpu(dev_config->string2),
0865          le16_to_cpu(dev_config->string3));
0866 
0867     return 0;
0868 
0869 error:
0870     kfree(data);
0871     return err;
0872 }
0873 
0874 /* ----------------------------------------------------------- */
0875 
0876 /*
0877  * functionality()
0878  */
0879 static u32 functionality(struct i2c_adapter *i2c_adap)
0880 {
0881     struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
0882 
0883     if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX ||
0884         i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B) {
0885         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0886     } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)  {
0887         return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
0888             ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
0889     }
0890 
0891     WARN(1, "Unknown i2c bus algorithm.\n");
0892     return 0;
0893 }
0894 
0895 static const struct i2c_algorithm em28xx_algo = {
0896     .master_xfer   = em28xx_i2c_xfer,
0897     .functionality = functionality,
0898 };
0899 
0900 static const struct i2c_adapter em28xx_adap_template = {
0901     .owner = THIS_MODULE,
0902     .name = "em28xx",
0903     .algo = &em28xx_algo,
0904 };
0905 
0906 static const struct i2c_client em28xx_client_template = {
0907     .name = "em28xx internal",
0908 };
0909 
0910 /* ----------------------------------------------------------- */
0911 
0912 /*
0913  * i2c_devs
0914  * incomplete list of known devices
0915  */
0916 static char *i2c_devs[128] = {
0917     [0x1c >> 1] = "lgdt330x",
0918     [0x3e >> 1] = "remote IR sensor",
0919     [0x4a >> 1] = "saa7113h",
0920     [0x52 >> 1] = "drxk",
0921     [0x60 >> 1] = "remote IR sensor",
0922     [0x8e >> 1] = "remote IR sensor",
0923     [0x86 >> 1] = "tda9887",
0924     [0x80 >> 1] = "msp34xx",
0925     [0x88 >> 1] = "msp34xx",
0926     [0xa0 >> 1] = "eeprom",
0927     [0xb0 >> 1] = "tda9874",
0928     [0xb8 >> 1] = "tvp5150a",
0929     [0xba >> 1] = "webcam sensor or tvp5150a",
0930     [0xc0 >> 1] = "tuner (analog)",
0931     [0xc2 >> 1] = "tuner (analog)",
0932     [0xc4 >> 1] = "tuner (analog)",
0933     [0xc6 >> 1] = "tuner (analog)",
0934 };
0935 
0936 /*
0937  * do_i2c_scan()
0938  * check i2c address range for devices
0939  */
0940 void em28xx_do_i2c_scan(struct em28xx *dev, unsigned int bus)
0941 {
0942     u8 i2c_devicelist[128];
0943     unsigned char buf;
0944     int i, rc;
0945 
0946     memset(i2c_devicelist, 0, sizeof(i2c_devicelist));
0947 
0948     for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
0949         dev->i2c_client[bus].addr = i;
0950         rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
0951         if (rc < 0)
0952             continue;
0953         i2c_devicelist[i] = i;
0954         dev_info(&dev->intf->dev,
0955              "found i2c device @ 0x%x on bus %d [%s]\n",
0956              i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
0957     }
0958 
0959     if (bus == dev->def_i2c_bus)
0960         dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
0961                         sizeof(i2c_devicelist), 32);
0962 }
0963 
0964 /*
0965  * em28xx_i2c_register()
0966  * register i2c bus
0967  */
0968 int em28xx_i2c_register(struct em28xx *dev, unsigned int bus,
0969             enum em28xx_i2c_algo_type algo_type)
0970 {
0971     int retval;
0972 
0973     if (WARN_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg ||
0974             !dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req))
0975         return -ENODEV;
0976 
0977     if (bus >= NUM_I2C_BUSES)
0978         return -ENODEV;
0979 
0980     dev->i2c_adap[bus] = em28xx_adap_template;
0981     dev->i2c_adap[bus].dev.parent = &dev->intf->dev;
0982     strscpy(dev->i2c_adap[bus].name, dev_name(&dev->intf->dev),
0983         sizeof(dev->i2c_adap[bus].name));
0984 
0985     dev->i2c_bus[bus].bus = bus;
0986     dev->i2c_bus[bus].algo_type = algo_type;
0987     dev->i2c_bus[bus].dev = dev;
0988     dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
0989 
0990     retval = i2c_add_adapter(&dev->i2c_adap[bus]);
0991     if (retval < 0) {
0992         dev_err(&dev->intf->dev,
0993             "%s: i2c_add_adapter failed! retval [%d]\n",
0994             __func__, retval);
0995         return retval;
0996     }
0997 
0998     dev->i2c_client[bus] = em28xx_client_template;
0999     dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
1000 
1001     /* Up to now, all eeproms are at bus 0 */
1002     if (!bus) {
1003         retval = em28xx_i2c_eeprom(dev, bus,
1004                        &dev->eedata, &dev->eedata_len);
1005         if (retval < 0 && retval != -ENODEV) {
1006             dev_err(&dev->intf->dev,
1007                 "%s: em28xx_i2_eeprom failed! retval [%d]\n",
1008                 __func__, retval);
1009         }
1010     }
1011 
1012     if (i2c_scan)
1013         em28xx_do_i2c_scan(dev, bus);
1014 
1015     return 0;
1016 }
1017 
1018 /*
1019  * em28xx_i2c_unregister()
1020  * unregister i2c_bus
1021  */
1022 int em28xx_i2c_unregister(struct em28xx *dev, unsigned int bus)
1023 {
1024     if (bus >= NUM_I2C_BUSES)
1025         return -ENODEV;
1026 
1027     i2c_del_adapter(&dev->i2c_adap[bus]);
1028     return 0;
1029 }