27 #include <linux/kernel.h>
28 #include <linux/module.h>
33 #include <linux/errno.h>
35 #include <linux/i2c.h>
38 #define DBG_I2C (1 << 0)
45 #define PDEBUG(level, fmt, args...) { \
46 if (i2c_debug & (level)) \
47 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
48 __func__, __LINE__ , ## args); \
56 static inline int try_write_address(
struct i2c_adapter *i2c_adap,
63 usbvision = (
struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
65 for (i = 0; i <=
retries; i++) {
66 ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
75 PDEBUG(
DBG_I2C,
"Needed %d retries for address %#2x", i, addr);
81 static inline int try_read_address(
struct i2c_adapter *i2c_adap,
82 unsigned char addr,
int retries)
88 usbvision = (
struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
89 for (i = 0; i <=
retries; i++) {
90 ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
99 PDEBUG(
DBG_I2C,
"Needed %d retries for address %#2x", i, addr);
105 static inline int usb_find_address(
struct i2c_adapter *i2c_adap,
114 addr = (msg->
addr << 1);
119 if (flags & I2C_M_RD)
120 ret = try_read_address(i2c_adap, addr, retries);
122 ret = try_write_address(i2c_adap, addr, retries);
136 unsigned char addr = 0;
138 usbvision = (
struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
140 for (i = 0; i < num; i++) {
142 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->
retries, &addr);
148 if (pmsg->
flags & I2C_M_RD) {
150 ret = (usbvision_i2c_read(usbvision, addr, pmsg->
buf, pmsg->
len));
155 ret = (usbvision_i2c_write(usbvision, addr, pmsg->
buf, pmsg->
len));
171 .master_xfer = usbvision_i2c_xfer,
184 static unsigned short saa711x_addrs[] = {
185 0x4a >> 1, 0x48 >> 1,
186 0x42 >> 1, 0x40 >> 1,
196 usbvision->
dev->bus->busnum, usbvision->
dev->devpath);
198 usbvision->
i2c_adap.dev.parent = &usbvision->
dev->dev;
212 usbvision->
i2c_adap.algo = &usbvision_algo;
230 "saa7115_auto", 0, saa711x_addrs);
236 struct tuner_setup tun_setup;
252 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
277 usbvision_i2c_read_max4(
struct usb_usbvision *usbvision,
unsigned char addr,
278 char *buf,
short len)
282 for (retries = 5;;) {
291 (len & 0x07) | 0x18);
299 }
while (rc > 0 && ((rc & 0x10) != 0));
304 if ((rc & 0x20) == 0)
328 "usbvision_i2c_read_max4: buffer length > 4\n");
334 for (idx = 0; idx < len; idx++)
335 PDEBUG(DBG_I2C,
"read %x from address %x", (
unsigned char)buf[idx], addr);
341 static int usbvision_i2c_write_max4(
struct usb_usbvision *usbvision,
342 unsigned char addr,
const char *buf,
347 unsigned char value[6];
348 unsigned char ser_cont;
350 ser_cont = (len & 0x07) | 0x10;
354 for (i = 0; i < len; i++)
355 value[i + 2] = buf[i];
357 for (retries = 5;;) {
359 usb_sndctrlpipe(usbvision->
dev, 1),
370 (len & 0x07) | 0x10);
377 }
while (rc > 0 && ((rc & 0x10) != 0));
381 if ((rc & 0x20) == 0)
395 for (idx = 0; idx < len; idx++)
396 PDEBUG(DBG_I2C,
"wrote %x at address %x", (
unsigned char)buf[idx], addr);
401 static int usbvision_i2c_write(
struct usb_usbvision *usbvision,
unsigned char addr,
char *buf,
411 count = (len > max_len) ? max_len : len;
412 retval = usbvision_i2c_write_max4(usbvision, addr, buf_ptr, count);
418 return (retval < 0) ? retval : -
EFAULT;
423 static int usbvision_i2c_read(
struct usb_usbvision *usbvision,
unsigned char addr,
char *buf,
432 count = (len > 3) ? 4 : len;
433 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
435 for (i = 0; i < len; i++)
436 buf[rdcount + i] = temp[i];
440 return (retval < 0) ? retval : -
EFAULT;