]> Pileus Git - ~andy/linux/blob - drivers/media/usb/em28xx/em28xx-i2c.c
[media] em28xx: rename I2C timeout to EM28XX_I2C_XFER_TIMEOUT
[~andy/linux] / drivers / media / usb / em28xx / em28xx-i2c.c
1 /*
2    em28xx-i2c.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@infradead.org>
7                       Sascha Sommer <saschasommer@freenet.de>
8    Copyright (C) 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/usb.h>
28 #include <linux/i2c.h>
29 #include <linux/jiffies.h>
30
31 #include "em28xx.h"
32 #include "tuner-xc2028.h"
33 #include <media/v4l2-common.h>
34 #include <media/tuner.h>
35
36 /* ----------------------------------------------------------- */
37
38 static unsigned int i2c_scan;
39 module_param(i2c_scan, int, 0444);
40 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
41
42 static unsigned int i2c_debug;
43 module_param(i2c_debug, int, 0644);
44 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
45
46 /*
47  * em2800_i2c_send_bytes()
48  * send up to 4 bytes to the em2800 i2c device
49  */
50 static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
51 {
52         unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
53         int ret;
54         u8 b2[6];
55
56         if (len < 1 || len > 4)
57                 return -EOPNOTSUPP;
58
59         BUG_ON(len < 1 || len > 4);
60         b2[5] = 0x80 + len - 1;
61         b2[4] = addr;
62         b2[3] = buf[0];
63         if (len > 1)
64                 b2[2] = buf[1];
65         if (len > 2)
66                 b2[1] = buf[2];
67         if (len > 3)
68                 b2[0] = buf[3];
69
70         /* trigger write */
71         ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
72         if (ret != 2 + len) {
73                 em28xx_warn("failed to trigger write to i2c address 0x%x (error=%i)\n",
74                             addr, ret);
75                 return (ret < 0) ? ret : -EIO;
76         }
77         /* wait for completion */
78         while (time_is_after_jiffies(timeout)) {
79                 ret = dev->em28xx_read_reg(dev, 0x05);
80                 if (ret == 0x80 + len - 1)
81                         return len;
82                 if (ret == 0x94 + len - 1) {
83                         return -ENODEV;
84                 }
85                 if (ret < 0) {
86                         em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
87                                     ret);
88                         return ret;
89                 }
90                 msleep(5);
91         }
92         em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
93         return -EIO;
94 }
95
96 /*
97  * em2800_i2c_recv_bytes()
98  * read up to 4 bytes from the em2800 i2c device
99  */
100 static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
101 {
102         unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
103         u8 buf2[4];
104         int ret;
105         int i;
106
107         if (len < 1 || len > 4)
108                 return -EOPNOTSUPP;
109
110         /* trigger read */
111         buf2[1] = 0x84 + len - 1;
112         buf2[0] = addr;
113         ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
114         if (ret != 2) {
115                 em28xx_warn("failed to trigger read from i2c address 0x%x (error=%i)\n",
116                             addr, ret);
117                 return (ret < 0) ? ret : -EIO;
118         }
119
120         /* wait for completion */
121         while (time_is_after_jiffies(timeout)) {
122                 ret = dev->em28xx_read_reg(dev, 0x05);
123                 if (ret == 0x84 + len - 1)
124                         break;
125                 if (ret == 0x94 + len - 1) {
126                         return -ENODEV;
127                 }
128                 if (ret < 0) {
129                         em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
130                                     ret);
131                         return ret;
132                 }
133                 msleep(5);
134         }
135         if (ret != 0x84 + len - 1)
136                 em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
137
138         /* get the received message */
139         ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
140         if (ret != len) {
141                 em28xx_warn("reading from i2c device at 0x%x failed: couldn't get the received message from the bridge (error=%i)\n",
142                             addr, ret);
143                 return (ret < 0) ? ret : -EIO;
144         }
145         for (i = 0; i < len; i++)
146                 buf[i] = buf2[len - 1 - i];
147
148         return ret;
149 }
150
151 /*
152  * em2800_i2c_check_for_device()
153  * check if there is an i2c device at the supplied address
154  */
155 static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
156 {
157         u8 buf;
158         int ret;
159
160         ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
161         if (ret == 1)
162                 return 0;
163         return (ret < 0) ? ret : -EIO;
164 }
165
166 /*
167  * em28xx_i2c_send_bytes()
168  */
169 static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
170                                  u16 len, int stop)
171 {
172         unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_I2C_XFER_TIMEOUT);
173         int ret;
174
175         if (len < 1 || len > 64)
176                 return -EOPNOTSUPP;
177         /*
178          * NOTE: limited by the USB ctrl message constraints
179          * Zero length reads always succeed, even if no device is connected
180          */
181
182         /* Write to i2c device */
183         ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
184         if (ret != len) {
185                 if (ret < 0) {
186                         em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
187                                     addr, ret);
188                         return ret;
189                 } else {
190                         em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
191                                     len, addr, ret);
192                         return -EIO;
193                 }
194         }
195
196         /* wait for completion */
197         while (time_is_after_jiffies(timeout)) {
198                 ret = dev->em28xx_read_reg(dev, 0x05);
199                 if (ret == 0) /* success */
200                         return len;
201                 if (ret == 0x10) {
202                         return -ENODEV;
203                 }
204                 if (ret < 0) {
205                         em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
206                                     ret);
207                         return ret;
208                 }
209                 msleep(5);
210                 /*
211                  * NOTE: do we really have to wait for success ?
212                  * Never seen anything else than 0x00 or 0x10
213                  * (even with high payload) ...
214                  */
215         }
216
217         em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
218         return -EIO;
219 }
220
221 /*
222  * em28xx_i2c_recv_bytes()
223  * read a byte from the i2c device
224  */
225 static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
226 {
227         int ret;
228
229         if (len < 1 || len > 64)
230                 return -EOPNOTSUPP;
231         /*
232          * NOTE: limited by the USB ctrl message constraints
233          * Zero length reads always succeed, even if no device is connected
234          */
235
236         /* Read data from i2c device */
237         ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
238         if (ret < 0) {
239                 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
240                             addr, ret);
241                 return ret;
242         }
243         /*
244          * NOTE: some devices with two i2c busses have the bad habit to return 0
245          * bytes if we are on bus B AND there was no write attempt to the
246          * specified slave address before AND no device is present at the
247          * requested slave address.
248          * Anyway, the next check will fail with -ENODEV in this case, so avoid
249          * spamming the system log on device probing and do nothing here.
250          */
251
252         /* Check success of the i2c operation */
253         ret = dev->em28xx_read_reg(dev, 0x05);
254         if (ret == 0) /* success */
255                 return len;
256         if (ret < 0) {
257                 em28xx_warn("failed to get i2c transfer status from bridge register (error=%i)\n",
258                             ret);
259                 return ret;
260         }
261         if (ret == 0x10)
262                 return -ENODEV;
263
264         em28xx_warn("unknown i2c error (status=%i)\n", ret);
265         return -EIO;
266 }
267
268 /*
269  * em28xx_i2c_check_for_device()
270  * check if there is a i2c_device at the supplied address
271  */
272 static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
273 {
274         int ret;
275         u8 buf;
276
277         ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
278         if (ret == 1)
279                 return 0;
280         return (ret < 0) ? ret : -EIO;
281 }
282
283 /*
284  * em25xx_bus_B_send_bytes
285  * write bytes to the i2c device
286  */
287 static int em25xx_bus_B_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
288                                    u16 len)
289 {
290         int ret;
291
292         if (len < 1 || len > 64)
293                 return -EOPNOTSUPP;
294         /*
295          * NOTE: limited by the USB ctrl message constraints
296          * Zero length reads always succeed, even if no device is connected
297          */
298
299         /* Set register and write value */
300         ret = dev->em28xx_write_regs_req(dev, 0x06, addr, buf, len);
301         if (ret != len) {
302                 if (ret < 0) {
303                         em28xx_warn("writing to i2c device at 0x%x failed (error=%i)\n",
304                                     addr, ret);
305                         return ret;
306                 } else {
307                         em28xx_warn("%i bytes write to i2c device at 0x%x requested, but %i bytes written\n",
308                                     len, addr, ret);
309                         return -EIO;
310                 }
311         }
312         /* Check success */
313         ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
314         /*
315          * NOTE: the only error we've seen so far is
316          * 0x01 when the slave device is not present
317          */
318         if (!ret)
319                 return len;
320         else if (ret > 0)
321                 return -ENODEV;
322
323         return ret;
324         /*
325          * NOTE: With chip types (other chip IDs) which actually don't support
326          * this operation, it seems to succeed ALWAYS ! (even if there is no
327          * slave device or even no second i2c bus provided)
328          */
329 }
330
331 /*
332  * em25xx_bus_B_recv_bytes
333  * read bytes from the i2c device
334  */
335 static int em25xx_bus_B_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf,
336                                    u16 len)
337 {
338         int ret;
339
340         if (len < 1 || len > 64)
341                 return -EOPNOTSUPP;
342         /*
343          * NOTE: limited by the USB ctrl message constraints
344          * Zero length reads always succeed, even if no device is connected
345          */
346
347         /* Read value */
348         ret = dev->em28xx_read_reg_req_len(dev, 0x06, addr, buf, len);
349         if (ret < 0) {
350                 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)\n",
351                             addr, ret);
352                 return ret;
353         }
354         /*
355          * NOTE: some devices with two i2c busses have the bad habit to return 0
356          * bytes if we are on bus B AND there was no write attempt to the
357          * specified slave address before AND no device is present at the
358          * requested slave address.
359          * Anyway, the next check will fail with -ENODEV in this case, so avoid
360          * spamming the system log on device probing and do nothing here.
361          */
362
363         /* Check success */
364         ret = dev->em28xx_read_reg_req(dev, 0x08, 0x0000);
365         /*
366          * NOTE: the only error we've seen so far is
367          * 0x01 when the slave device is not present
368          */
369         if (!ret)
370                 return len;
371         else if (ret > 0)
372                 return -ENODEV;
373
374         return ret;
375         /*
376          * NOTE: With chip types (other chip IDs) which actually don't support
377          * this operation, it seems to succeed ALWAYS ! (even if there is no
378          * slave device or even no second i2c bus provided)
379          */
380 }
381
382 /*
383  * em25xx_bus_B_check_for_device()
384  * check if there is a i2c device at the supplied address
385  */
386 static int em25xx_bus_B_check_for_device(struct em28xx *dev, u16 addr)
387 {
388         u8 buf;
389         int ret;
390
391         ret = em25xx_bus_B_recv_bytes(dev, addr, &buf, 1);
392         if (ret < 0)
393                 return ret;
394
395         return 0;
396         /*
397          * NOTE: With chips which do not support this operation,
398          * it seems to succeed ALWAYS ! (even if no device connected)
399          */
400 }
401
402 static inline int i2c_check_for_device(struct em28xx_i2c_bus *i2c_bus, u16 addr)
403 {
404         struct em28xx *dev = i2c_bus->dev;
405         int rc = -EOPNOTSUPP;
406
407         if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
408                 rc = em28xx_i2c_check_for_device(dev, addr);
409         else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
410                 rc = em2800_i2c_check_for_device(dev, addr);
411         else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
412                 rc = em25xx_bus_B_check_for_device(dev, addr);
413         if (rc == -ENODEV) {
414                 if (i2c_debug)
415                         printk(" no device\n");
416         }
417         return rc;
418 }
419
420 static inline int i2c_recv_bytes(struct em28xx_i2c_bus *i2c_bus,
421                                  struct i2c_msg msg)
422 {
423         struct em28xx *dev = i2c_bus->dev;
424         u16 addr = msg.addr << 1;
425         int byte, rc = -EOPNOTSUPP;
426
427         if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
428                 rc = em28xx_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
429         else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
430                 rc = em2800_i2c_recv_bytes(dev, addr, msg.buf, msg.len);
431         else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
432                 rc = em25xx_bus_B_recv_bytes(dev, addr, msg.buf, msg.len);
433         if (i2c_debug) {
434                 for (byte = 0; byte < msg.len; byte++)
435                         printk(" %02x", msg.buf[byte]);
436         }
437         return rc;
438 }
439
440 static inline int i2c_send_bytes(struct em28xx_i2c_bus *i2c_bus,
441                                  struct i2c_msg msg, int stop)
442 {
443         struct em28xx *dev = i2c_bus->dev;
444         u16 addr = msg.addr << 1;
445         int byte, rc = -EOPNOTSUPP;
446
447         if (i2c_debug) {
448                 for (byte = 0; byte < msg.len; byte++)
449                         printk(" %02x", msg.buf[byte]);
450         }
451         if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX)
452                 rc = em28xx_i2c_send_bytes(dev, addr, msg.buf, msg.len, stop);
453         else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)
454                 rc = em2800_i2c_send_bytes(dev, addr, msg.buf, msg.len);
455         else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)
456                 rc = em25xx_bus_B_send_bytes(dev, addr, msg.buf, msg.len);
457         return rc;
458 }
459
460 /*
461  * em28xx_i2c_xfer()
462  * the main i2c transfer function
463  */
464 static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
465                            struct i2c_msg msgs[], int num)
466 {
467         struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
468         struct em28xx *dev = i2c_bus->dev;
469         unsigned bus = i2c_bus->bus;
470         int addr, rc, i;
471         u8 reg;
472
473         rc = rt_mutex_trylock(&dev->i2c_bus_lock);
474         if (rc < 0)
475                 return rc;
476
477         /* Switch I2C bus if needed */
478         if (bus != dev->cur_i2c_bus &&
479             i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) {
480                 if (bus == 1)
481                         reg = EM2874_I2C_SECONDARY_BUS_SELECT;
482                 else
483                         reg = 0;
484                 em28xx_write_reg_bits(dev, EM28XX_R06_I2C_CLK, reg,
485                                       EM2874_I2C_SECONDARY_BUS_SELECT);
486                 dev->cur_i2c_bus = bus;
487         }
488
489         if (num <= 0) {
490                 rt_mutex_unlock(&dev->i2c_bus_lock);
491                 return 0;
492         }
493         for (i = 0; i < num; i++) {
494                 addr = msgs[i].addr << 1;
495                 if (i2c_debug)
496                         printk(KERN_DEBUG "%s at %s: %s %s addr=%02x len=%d:",
497                                dev->name, __func__ ,
498                                (msgs[i].flags & I2C_M_RD) ? "read" : "write",
499                                i == num - 1 ? "stop" : "nonstop",
500                                addr, msgs[i].len);
501                 if (!msgs[i].len) { /* no len: check only for device presence */
502                         rc = i2c_check_for_device(i2c_bus, addr);
503                         if (rc == -ENODEV) {
504                                 rt_mutex_unlock(&dev->i2c_bus_lock);
505                                 return rc;
506                         }
507                 } else if (msgs[i].flags & I2C_M_RD) {
508                         /* read bytes */
509                         rc = i2c_recv_bytes(i2c_bus, msgs[i]);
510                 } else {
511                         /* write bytes */
512                         rc = i2c_send_bytes(i2c_bus, msgs[i], i == num - 1);
513                 }
514                 if (rc < 0) {
515                         if (i2c_debug)
516                                 printk(" ERROR: %i\n", rc);
517                         rt_mutex_unlock(&dev->i2c_bus_lock);
518                         return rc;
519                 }
520                 if (i2c_debug)
521                         printk("\n");
522         }
523
524         rt_mutex_unlock(&dev->i2c_bus_lock);
525         return num;
526 }
527
528 /*
529  * based on linux/sunrpc/svcauth.h and linux/hash.h
530  * The original hash function returns a different value, if arch is x86_64
531  * or i386.
532  */
533 static inline unsigned long em28xx_hash_mem(char *buf, int length, int bits)
534 {
535         unsigned long hash = 0;
536         unsigned long l = 0;
537         int len = 0;
538         unsigned char c;
539         do {
540                 if (len == length) {
541                         c = (char)len;
542                         len = -1;
543                 } else
544                         c = *buf++;
545                 l = (l << 8) | c;
546                 len++;
547                 if ((len & (32 / 8 - 1)) == 0)
548                         hash = ((hash^l) * 0x9e370001UL);
549         } while (len);
550
551         return (hash >> (32 - bits)) & 0xffffffffUL;
552 }
553
554 /*
555  * Helper function to read data blocks from i2c clients with 8 or 16 bit
556  * address width, 8 bit register width and auto incrementation been activated
557  */
558 static int em28xx_i2c_read_block(struct em28xx *dev, unsigned bus, u16 addr,
559                                  bool addr_w16, u16 len, u8 *data)
560 {
561         int remain = len, rsize, rsize_max, ret;
562         u8 buf[2];
563
564         /* Sanity check */
565         if (addr + remain > (addr_w16 * 0xff00 + 0xff + 1))
566                 return -EINVAL;
567         /* Select address */
568         buf[0] = addr >> 8;
569         buf[1] = addr & 0xff;
570         ret = i2c_master_send(&dev->i2c_client[bus], buf + !addr_w16, 1 + addr_w16);
571         if (ret < 0)
572                 return ret;
573         /* Read data */
574         if (dev->board.is_em2800)
575                 rsize_max = 4;
576         else
577                 rsize_max = 64;
578         while (remain > 0) {
579                 if (remain > rsize_max)
580                         rsize = rsize_max;
581                 else
582                         rsize = remain;
583
584                 ret = i2c_master_recv(&dev->i2c_client[bus], data, rsize);
585                 if (ret < 0)
586                         return ret;
587
588                 remain -= rsize;
589                 data += rsize;
590         }
591
592         return len;
593 }
594
595 static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned bus,
596                              u8 **eedata, u16 *eedata_len)
597 {
598         const u16 len = 256;
599         /*
600          * FIXME common length/size for bytes to read, to display, hash
601          * calculation and returned device dataset. Simplifies the code a lot,
602          * but we might have to deal with multiple sizes in the future !
603          */
604         int i, err;
605         struct em28xx_eeprom *dev_config;
606         u8 buf, *data;
607
608         *eedata = NULL;
609         *eedata_len = 0;
610
611         /* EEPROM is always on i2c bus 0 on all known devices. */
612
613         dev->i2c_client[bus].addr = 0xa0 >> 1;
614
615         /* Check if board has eeprom */
616         err = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
617         if (err < 0) {
618                 em28xx_info("board has no eeprom\n");
619                 return -ENODEV;
620         }
621
622         data = kzalloc(len, GFP_KERNEL);
623         if (data == NULL)
624                 return -ENOMEM;
625
626         /* Read EEPROM content */
627         err = em28xx_i2c_read_block(dev, bus, 0x0000,
628                                     dev->eeprom_addrwidth_16bit,
629                                     len, data);
630         if (err != len) {
631                 em28xx_errdev("failed to read eeprom (err=%d)\n", err);
632                 goto error;
633         }
634
635         /* Display eeprom content */
636         for (i = 0; i < len; i++) {
637                 if (0 == (i % 16)) {
638                         if (dev->eeprom_addrwidth_16bit)
639                                 em28xx_info("i2c eeprom %04x:", i);
640                         else
641                                 em28xx_info("i2c eeprom %02x:", i);
642                 }
643                 printk(" %02x", data[i]);
644                 if (15 == (i % 16))
645                         printk("\n");
646         }
647         if (dev->eeprom_addrwidth_16bit)
648                 em28xx_info("i2c eeprom %04x: ... (skipped)\n", i);
649
650         if (dev->eeprom_addrwidth_16bit &&
651             data[0] == 0x26 && data[3] == 0x00) {
652                 /* new eeprom format; size 4-64kb */
653                 u16 mc_start;
654                 u16 hwconf_offset;
655
656                 dev->hash = em28xx_hash_mem(data, len, 32);
657                 mc_start = (data[1] << 8) + 4;  /* usually 0x0004 */
658
659                 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
660                             data[0], data[1], data[2], data[3], dev->hash);
661                 em28xx_info("EEPROM info:\n");
662                 em28xx_info("\tmicrocode start address = 0x%04x, boot configuration = 0x%02x\n",
663                             mc_start, data[2]);
664                 /*
665                  * boot configuration (address 0x0002):
666                  * [0]   microcode download speed: 1 = 400 kHz; 0 = 100 kHz
667                  * [1]   always selects 12 kb RAM
668                  * [2]   USB device speed: 1 = force Full Speed; 0 = auto detect
669                  * [4]   1 = force fast mode and no suspend for device testing
670                  * [5:7] USB PHY tuning registers; determined by device
671                  *       characterization
672                  */
673
674                 /*
675                  * Read hardware config dataset offset from address
676                  * (microcode start + 46)
677                  */
678                 err = em28xx_i2c_read_block(dev, bus, mc_start + 46, 1, 2,
679                                             data);
680                 if (err != 2) {
681                         em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
682                                       err);
683                         goto error;
684                 }
685
686                 /* Calculate hardware config dataset start address */
687                 hwconf_offset = mc_start + data[0] + (data[1] << 8);
688
689                 /* Read hardware config dataset */
690                 /*
691                  * NOTE: the microcode copy can be multiple pages long, but
692                  * we assume the hardware config dataset is the same as in
693                  * the old eeprom and not longer than 256 bytes.
694                  * tveeprom is currently also limited to 256 bytes.
695                  */
696                 err = em28xx_i2c_read_block(dev, bus, hwconf_offset, 1, len,
697                                             data);
698                 if (err != len) {
699                         em28xx_errdev("failed to read hardware configuration data from eeprom (err=%d)\n",
700                                       err);
701                         goto error;
702                 }
703
704                 /* Verify hardware config dataset */
705                 /* NOTE: not all devices provide this type of dataset */
706                 if (data[0] != 0x1a || data[1] != 0xeb ||
707                     data[2] != 0x67 || data[3] != 0x95) {
708                         em28xx_info("\tno hardware configuration dataset found in eeprom\n");
709                         kfree(data);
710                         return 0;
711                 }
712
713                 /* TODO: decrypt eeprom data for camera bridges (em25xx, em276x+) */
714
715         } else if (!dev->eeprom_addrwidth_16bit &&
716                    data[0] == 0x1a && data[1] == 0xeb &&
717                    data[2] == 0x67 && data[3] == 0x95) {
718                 dev->hash = em28xx_hash_mem(data, len, 32);
719                 em28xx_info("EEPROM ID = %02x %02x %02x %02x, EEPROM hash = 0x%08lx\n",
720                             data[0], data[1], data[2], data[3], dev->hash);
721                 em28xx_info("EEPROM info:\n");
722         } else {
723                 em28xx_info("unknown eeprom format or eeprom corrupted !\n");
724                 err = -ENODEV;
725                 goto error;
726         }
727
728         *eedata = data;
729         *eedata_len = len;
730         dev_config = (void *)*eedata;
731
732         switch (le16_to_cpu(dev_config->chip_conf) >> 4 & 0x3) {
733         case 0:
734                 em28xx_info("\tNo audio on board.\n");
735                 break;
736         case 1:
737                 em28xx_info("\tAC97 audio (5 sample rates)\n");
738                 break;
739         case 2:
740                 if (dev->chip_id < CHIP_ID_EM2860)
741                         em28xx_info("\tI2S audio, sample rate=32k\n");
742                 else
743                         em28xx_info("\tI2S audio, 3 sample rates\n");
744                 break;
745         case 3:
746                 if (dev->chip_id < CHIP_ID_EM2860)
747                         em28xx_info("\tI2S audio, 3 sample rates\n");
748                 else
749                         em28xx_info("\tI2S audio, 5 sample rates\n");
750                 break;
751         }
752
753         if (le16_to_cpu(dev_config->chip_conf) & 1 << 3)
754                 em28xx_info("\tUSB Remote wakeup capable\n");
755
756         if (le16_to_cpu(dev_config->chip_conf) & 1 << 2)
757                 em28xx_info("\tUSB Self power capable\n");
758
759         switch (le16_to_cpu(dev_config->chip_conf) & 0x3) {
760         case 0:
761                 em28xx_info("\t500mA max power\n");
762                 break;
763         case 1:
764                 em28xx_info("\t400mA max power\n");
765                 break;
766         case 2:
767                 em28xx_info("\t300mA max power\n");
768                 break;
769         case 3:
770                 em28xx_info("\t200mA max power\n");
771                 break;
772         }
773         em28xx_info("\tTable at offset 0x%02x, strings=0x%04x, 0x%04x, 0x%04x\n",
774                     dev_config->string_idx_table,
775                     le16_to_cpu(dev_config->string1),
776                     le16_to_cpu(dev_config->string2),
777                     le16_to_cpu(dev_config->string3));
778
779         return 0;
780
781 error:
782         kfree(data);
783         return err;
784 }
785
786 /* ----------------------------------------------------------- */
787
788 /*
789  * functionality()
790  */
791 static u32 functionality(struct i2c_adapter *i2c_adap)
792 {
793         struct em28xx_i2c_bus *i2c_bus = i2c_adap->algo_data;
794
795         if ((i2c_bus->algo_type == EM28XX_I2C_ALGO_EM28XX) ||
796             (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM25XX_BUS_B)) {
797                 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
798         } else if (i2c_bus->algo_type == EM28XX_I2C_ALGO_EM2800)  {
799                 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL) &
800                         ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
801         }
802
803         WARN(1, "Unknown i2c bus algorithm.\n");
804         return 0;
805 }
806
807 static struct i2c_algorithm em28xx_algo = {
808         .master_xfer   = em28xx_i2c_xfer,
809         .functionality = functionality,
810 };
811
812 static struct i2c_adapter em28xx_adap_template = {
813         .owner = THIS_MODULE,
814         .name = "em28xx",
815         .algo = &em28xx_algo,
816 };
817
818 static struct i2c_client em28xx_client_template = {
819         .name = "em28xx internal",
820 };
821
822 /* ----------------------------------------------------------- */
823
824 /*
825  * i2c_devs
826  * incomplete list of known devices
827  */
828 static char *i2c_devs[128] = {
829         [0x3e >> 1] = "remote IR sensor",
830         [0x4a >> 1] = "saa7113h",
831         [0x52 >> 1] = "drxk",
832         [0x60 >> 1] = "remote IR sensor",
833         [0x8e >> 1] = "remote IR sensor",
834         [0x86 >> 1] = "tda9887",
835         [0x80 >> 1] = "msp34xx",
836         [0x88 >> 1] = "msp34xx",
837         [0xa0 >> 1] = "eeprom",
838         [0xb0 >> 1] = "tda9874",
839         [0xb8 >> 1] = "tvp5150a",
840         [0xba >> 1] = "webcam sensor or tvp5150a",
841         [0xc0 >> 1] = "tuner (analog)",
842         [0xc2 >> 1] = "tuner (analog)",
843         [0xc4 >> 1] = "tuner (analog)",
844         [0xc6 >> 1] = "tuner (analog)",
845 };
846
847 /*
848  * do_i2c_scan()
849  * check i2c address range for devices
850  */
851 void em28xx_do_i2c_scan(struct em28xx *dev, unsigned bus)
852 {
853         u8 i2c_devicelist[128];
854         unsigned char buf;
855         int i, rc;
856
857         memset(i2c_devicelist, 0, ARRAY_SIZE(i2c_devicelist));
858
859         for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
860                 dev->i2c_client[bus].addr = i;
861                 rc = i2c_master_recv(&dev->i2c_client[bus], &buf, 0);
862                 if (rc < 0)
863                         continue;
864                 i2c_devicelist[i] = i;
865                 em28xx_info("found i2c device @ 0x%x on bus %d [%s]\n",
866                             i << 1, bus, i2c_devs[i] ? i2c_devs[i] : "???");
867         }
868
869         if (bus == dev->def_i2c_bus)
870                 dev->i2c_hash = em28xx_hash_mem(i2c_devicelist,
871                                                 ARRAY_SIZE(i2c_devicelist), 32);
872 }
873
874 /*
875  * em28xx_i2c_register()
876  * register i2c bus
877  */
878 int em28xx_i2c_register(struct em28xx *dev, unsigned bus,
879                         enum em28xx_i2c_algo_type algo_type)
880 {
881         int retval;
882
883         BUG_ON(!dev->em28xx_write_regs || !dev->em28xx_read_reg);
884         BUG_ON(!dev->em28xx_write_regs_req || !dev->em28xx_read_reg_req);
885
886         if (bus >= NUM_I2C_BUSES)
887                 return -ENODEV;
888
889         dev->i2c_adap[bus] = em28xx_adap_template;
890         dev->i2c_adap[bus].dev.parent = &dev->udev->dev;
891         strcpy(dev->i2c_adap[bus].name, dev->name);
892
893         dev->i2c_bus[bus].bus = bus;
894         dev->i2c_bus[bus].algo_type = algo_type;
895         dev->i2c_bus[bus].dev = dev;
896         dev->i2c_adap[bus].algo_data = &dev->i2c_bus[bus];
897         i2c_set_adapdata(&dev->i2c_adap[bus], &dev->v4l2_dev);
898
899         retval = i2c_add_adapter(&dev->i2c_adap[bus]);
900         if (retval < 0) {
901                 em28xx_errdev("%s: i2c_add_adapter failed! retval [%d]\n",
902                         __func__, retval);
903                 return retval;
904         }
905
906         dev->i2c_client[bus] = em28xx_client_template;
907         dev->i2c_client[bus].adapter = &dev->i2c_adap[bus];
908
909         /* Up to now, all eeproms are at bus 0 */
910         if (!bus) {
911                 retval = em28xx_i2c_eeprom(dev, bus, &dev->eedata, &dev->eedata_len);
912                 if ((retval < 0) && (retval != -ENODEV)) {
913                         em28xx_errdev("%s: em28xx_i2_eeprom failed! retval [%d]\n",
914                                 __func__, retval);
915
916                         return retval;
917                 }
918         }
919
920         if (i2c_scan)
921                 em28xx_do_i2c_scan(dev, bus);
922
923         return 0;
924 }
925
926 /*
927  * em28xx_i2c_unregister()
928  * unregister i2c_bus
929  */
930 int em28xx_i2c_unregister(struct em28xx *dev, unsigned bus)
931 {
932         if (bus >= NUM_I2C_BUSES)
933                 return -ENODEV;
934
935         i2c_del_adapter(&dev->i2c_adap[bus]);
936         return 0;
937 }