0001
0002
0003
0004
0005
0006
0007
0008
0009
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
0041
0042
0043
0044
0045
0046
0047
0048 #define EM28XX_I2C_XFER_TIMEOUT 35
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;
0057 break;
0058 case EM28XX_I2C_FREQ_100_KHZ:
0059 case EM28XX_I2C_FREQ_400_KHZ:
0060 time += 1;
0061 break;
0062 default:
0063 break;
0064 }
0065
0066 return msecs_to_jiffies(time);
0067 }
0068
0069
0070
0071
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
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
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
0123
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
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
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
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
0183
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
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
0209
0210
0211
0212
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
0228 while (time_is_after_jiffies(timeout)) {
0229 ret = dev->em28xx_read_reg(dev, 0x05);
0230 if (ret == 0)
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
0246
0247
0248
0249 }
0250
0251 if (ret == 0x02 || ret == 0x04) {
0252
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
0267
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
0277
0278
0279
0280
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
0294
0295
0296
0297
0298
0299
0300
0301
0302 ret = dev->em28xx_read_reg(dev, 0x05);
0303 if (ret == 0)
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
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
0333
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
0348
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
0359
0360
0361
0362
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
0378 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
0379
0380
0381
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
0394
0395
0396
0397 }
0398
0399
0400
0401
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
0412
0413
0414
0415
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
0425
0426
0427
0428
0429
0430
0431
0432
0433 ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
0434
0435
0436
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
0449
0450
0451
0452 }
0453
0454
0455
0456
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
0470
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
0522
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
0535
0536
0537
0538 if (dev->disconnected)
0539 return -ENODEV;
0540
0541 if (!rt_mutex_trylock(&dev->i2c_bus_lock))
0542 return -EAGAIN;
0543
0544
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
0561
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
0569 rc = i2c_recv_bytes(i2c_bus, msgs[i]);
0570 } else {
0571
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
0602
0603
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
0630
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
0639 if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
0640 return -EINVAL;
0641
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
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
0676
0677
0678
0679 int err;
0680 struct em28xx_eeprom *dev_config;
0681 u8 buf, *data;
0682
0683 *eedata = NULL;
0684 *eedata_len = 0;
0685
0686
0687
0688 dev->i2c_client[bus].addr = 0xa0 >> 1;
0689
0690
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
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
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
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;
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
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
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
0762 hwconf_offset = mc_start + data[0] + (data[1] << 8);
0763
0764
0765
0766
0767
0768
0769
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
0781
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
0792
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
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
0914
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
0938
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
0966
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
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
1020
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 }