3 * i2c algorithm for USB-I2C Bridges
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/version.h>
32 #include <linux/utsname.h>
33 #include <linux/init.h>
34 #include <asm/uaccess.h>
35 #include <linux/ioport.h>
36 #include <linux/errno.h>
37 #include <linux/sched.h>
38 #include <linux/usb.h>
39 #include <linux/i2c.h>
40 #include "usbvision-i2c.h"
42 static int debug_i2c_usb = 0;
44 #if defined(module_param) // Showing parameters under SYSFS
45 module_param (debug_i2c_usb, int, 0444); // debug_i2c_usb mode of the device driver
47 MODULE_PARM(debug_i2c_usb, "i"); // debug_i2c_usb mode of the device driver
51 static inline int try_write_address(struct i2c_adapter *i2c_adap,
52 unsigned char addr, int retries)
54 struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
59 data = i2c_get_adapdata(i2c_adap);
61 for (i = 0; i <= retries; i++) {
62 ret = (adap->outb(data, addr, buf, 1));
65 udelay(5 /*adap->udelay */ );
66 if (i == retries) /* no success */
72 info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
73 info("%s: Maybe there's no device at this address", __FUNCTION__);
79 static inline int try_read_address(struct i2c_adapter *i2c_adap,
80 unsigned char addr, int retries)
82 struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
87 data = i2c_get_adapdata(i2c_adap);
88 for (i = 0; i <= retries; i++) {
89 ret = (adap->inb(data, addr, buf, 1));
92 udelay(5 /*adap->udelay */ );
93 if (i == retries) /* no success */
99 info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
100 info("%s: Maybe there's no device at this address", __FUNCTION__);
106 static inline int usb_find_address(struct i2c_adapter *i2c_adap,
107 struct i2c_msg *msg, int retries,
110 unsigned short flags = msg->flags;
114 if ((flags & I2C_M_TEN)) {
115 /* a ten bit address */
116 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
117 /* try extended address code... */
118 ret = try_write_address(i2c_adap, addr, retries);
120 err("died at extended address code, while writing");
124 if (flags & I2C_M_RD) {
125 /* okay, now switch into reading mode */
127 ret = try_read_address(i2c_adap, addr, retries);
129 err("died at extended address code, while reading");
134 } else { /* normal 7bit address */
135 addr = (msg->addr << 1);
136 if (flags & I2C_M_RD)
138 if (flags & I2C_M_REV_DIR_ADDR)
142 if (flags & I2C_M_RD)
143 ret = try_read_address(i2c_adap, addr, retries);
145 ret = try_write_address(i2c_adap, addr, retries);
155 usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
157 struct i2c_msg *pmsg;
158 struct i2c_algo_usb_data *adap = i2c_adap->algo_data;
163 data = i2c_get_adapdata(i2c_adap);
165 for (i = 0; i < num; i++) {
167 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
170 info("%s: got NAK from device, message #%d\n", __FUNCTION__, i);
172 return (ret < 0) ? ret : -EREMOTEIO;
175 if (pmsg->flags & I2C_M_RD) {
176 /* read bytes into buffer */
177 ret = (adap->inb(data, addr, pmsg->buf, pmsg->len));
178 if (ret < pmsg->len) {
179 return (ret < 0) ? ret : -EREMOTEIO;
182 /* write bytes from buffer */
183 ret = (adap->outb(data, addr, pmsg->buf, pmsg->len));
184 if (ret < pmsg->len) {
185 return (ret < 0) ? ret : -EREMOTEIO;
192 static int algo_control(struct i2c_adapter *adapter, unsigned int cmd, unsigned long arg)
197 static u32 usb_func(struct i2c_adapter *adap)
199 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
203 /* -----exported algorithm data: ------------------------------------- */
205 static struct i2c_algorithm i2c_usb_algo = {
206 .master_xfer = usb_xfer,
210 .algo_control = algo_control,
211 .functionality = usb_func,
216 * registering functions to load algorithms at runtime
218 int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
220 /* register new adapter to i2c module... */
222 adap->algo = &i2c_usb_algo;
224 adap->timeout = 100; /* default values, should */
225 adap->retries = 3; /* be replaced by defines */
228 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
233 i2c_add_adapter(adap);
236 info("i2c bus for %s registered", adap->name);
243 int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
246 i2c_del_adapter(adap);
249 info("i2c bus for %s unregistered", adap->name);
252 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
260 EXPORT_SYMBOL(usbvision_i2c_usb_add_bus);
261 EXPORT_SYMBOL(usbvision_i2c_usb_del_bus);