1 /*
2  * driver for the i2c-tiny-usb adapter - 1.0
3  * http://www.harbaum.org/till/i2c_tiny_usb
4  *
5  * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation, version 2.
10  *
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 
19 /* include interfaces to usb layer */
20 #include <linux/usb.h>
21 
22 /* include interface to i2c layer */
23 #include <linux/i2c.h>
24 
25 /* commands via USB, must match command ids in the firmware */
26 #define CMD_ECHO		0
27 #define CMD_GET_FUNC		1
28 #define CMD_SET_DELAY		2
29 #define CMD_GET_STATUS		3
30 
31 #define CMD_I2C_IO		4
32 #define CMD_I2C_IO_BEGIN	(1<<0)
33 #define CMD_I2C_IO_END		(1<<1)
34 
35 /* i2c bit delay, default is 10us -> 100kHz max
36    (in practice, due to additional delays in the i2c bitbanging
37    code this results in a i2c clock of about 50kHz) */
38 static unsigned short delay = 10;
39 module_param(delay, ushort, 0);
40 MODULE_PARM_DESC(delay, "bit delay in microseconds "
41 		 "(default is 10us for 100kHz max)");
42 
43 static int usb_read(struct i2c_adapter *adapter, int cmd,
44 		    int value, int index, void *data, int len);
45 
46 static int usb_write(struct i2c_adapter *adapter, int cmd,
47 		     int value, int index, void *data, int len);
48 
49 /* ----- begin of i2c layer ---------------------------------------------- */
50 
51 #define STATUS_IDLE		0
52 #define STATUS_ADDRESS_ACK	1
53 #define STATUS_ADDRESS_NAK	2
54 
55 static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
56 {
57 	unsigned char *pstatus;
58 	struct i2c_msg *pmsg;
59 	int i, ret;
60 
61 	dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
62 
63 	pstatus = kmalloc(sizeof(*pstatus), GFP_KERNEL);
64 	if (!pstatus)
65 		return -ENOMEM;
66 
67 	for (i = 0 ; i < num ; i++) {
68 		int cmd = CMD_I2C_IO;
69 
70 		if (i == 0)
71 			cmd |= CMD_I2C_IO_BEGIN;
72 
73 		if (i == num-1)
74 			cmd |= CMD_I2C_IO_END;
75 
76 		pmsg = &msgs[i];
77 
78 		dev_dbg(&adapter->dev,
79 			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
80 			i, pmsg->flags & I2C_M_RD ? "read" : "write",
81 			pmsg->flags, pmsg->len, pmsg->addr);
82 
83 		/* and directly send the message */
84 		if (pmsg->flags & I2C_M_RD) {
85 			/* read data */
86 			if (usb_read(adapter, cmd,
87 				     pmsg->flags, pmsg->addr,
88 				     pmsg->buf, pmsg->len) != pmsg->len) {
89 				dev_err(&adapter->dev,
90 					"failure reading data\n");
91 				ret = -EREMOTEIO;
92 				goto out;
93 			}
94 		} else {
95 			/* write data */
96 			if (usb_write(adapter, cmd,
97 				      pmsg->flags, pmsg->addr,
98 				      pmsg->buf, pmsg->len) != pmsg->len) {
99 				dev_err(&adapter->dev,
100 					"failure writing data\n");
101 				ret = -EREMOTEIO;
102 				goto out;
103 			}
104 		}
105 
106 		/* read status */
107 		if (usb_read(adapter, CMD_GET_STATUS, 0, 0, pstatus, 1) != 1) {
108 			dev_err(&adapter->dev, "failure reading status\n");
109 			ret = -EREMOTEIO;
110 			goto out;
111 		}
112 
113 		dev_dbg(&adapter->dev, "  status = %d\n", *pstatus);
114 		if (*pstatus == STATUS_ADDRESS_NAK) {
115 			ret = -EREMOTEIO;
116 			goto out;
117 		}
118 	}
119 
120 	ret = i;
121 out:
122 	kfree(pstatus);
123 	return ret;
124 }
125 
126 static u32 usb_func(struct i2c_adapter *adapter)
127 {
128 	__le32 *pfunc;
129 	u32 ret;
130 
131 	pfunc = kmalloc(sizeof(*pfunc), GFP_KERNEL);
132 
133 	/* get functionality from adapter */
134 	if (!pfunc || usb_read(adapter, CMD_GET_FUNC, 0, 0, pfunc,
135 			       sizeof(*pfunc)) != sizeof(*pfunc)) {
136 		dev_err(&adapter->dev, "failure reading functionality\n");
137 		ret = 0;
138 		goto out;
139 	}
140 
141 	ret = le32_to_cpup(pfunc);
142 out:
143 	kfree(pfunc);
144 	return ret;
145 }
146 
147 /* This is the actual algorithm we define */
148 static const struct i2c_algorithm usb_algorithm = {
149 	.master_xfer	= usb_xfer,
150 	.functionality	= usb_func,
151 };
152 
153 /* ----- end of i2c layer ------------------------------------------------ */
154 
155 /* ----- begin of usb layer ---------------------------------------------- */
156 
157 /*
158  * Initially the usb i2c interface uses a vid/pid pair donated by
159  * Future Technology Devices International Ltd., later a pair was
160  * bought from EZPrototypes
161  */
162 static const struct usb_device_id i2c_tiny_usb_table[] = {
163 	{ USB_DEVICE(0x0403, 0xc631) },   /* FTDI */
164 	{ USB_DEVICE(0x1c40, 0x0534) },   /* EZPrototypes */
165 	{ }                               /* Terminating entry */
166 };
167 
168 MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
169 
170 /* Structure to hold all of our device specific stuff */
171 struct i2c_tiny_usb {
172 	struct usb_device *usb_dev; /* the usb device for this device */
173 	struct usb_interface *interface; /* the interface for this device */
174 	struct i2c_adapter adapter; /* i2c related things */
175 };
176 
177 static int usb_read(struct i2c_adapter *adapter, int cmd,
178 		    int value, int index, void *data, int len)
179 {
180 	struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
181 
182 	/* do control transfer */
183 	return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
184 			       cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
185 			       USB_DIR_IN, value, index, data, len, 2000);
186 }
187 
188 static int usb_write(struct i2c_adapter *adapter, int cmd,
189 		     int value, int index, void *data, int len)
190 {
191 	struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
192 
193 	/* do control transfer */
194 	return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
195 			       cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
196 			       value, index, data, len, 2000);
197 }
198 
199 static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
200 {
201 	usb_put_dev(dev->usb_dev);
202 	kfree(dev);
203 }
204 
205 static int i2c_tiny_usb_probe(struct usb_interface *interface,
206 			      const struct usb_device_id *id)
207 {
208 	struct i2c_tiny_usb *dev;
209 	int retval = -ENOMEM;
210 	u16 version;
211 
212 	dev_dbg(&interface->dev, "probing usb device\n");
213 
214 	/* allocate memory for our device state and initialize it */
215 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
216 	if (dev == NULL) {
217 		dev_err(&interface->dev, "Out of memory\n");
218 		goto error;
219 	}
220 
221 	dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
222 	dev->interface = interface;
223 
224 	/* save our data pointer in this interface device */
225 	usb_set_intfdata(interface, dev);
226 
227 	version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
228 	dev_info(&interface->dev,
229 		 "version %x.%02x found at bus %03d address %03d\n",
230 		 version >> 8, version & 0xff,
231 		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
232 
233 	/* setup i2c adapter description */
234 	dev->adapter.owner = THIS_MODULE;
235 	dev->adapter.class = I2C_CLASS_HWMON;
236 	dev->adapter.algo = &usb_algorithm;
237 	dev->adapter.algo_data = dev;
238 	snprintf(dev->adapter.name, sizeof(dev->adapter.name),
239 		 "i2c-tiny-usb at bus %03d device %03d",
240 		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
241 
242 	if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
243 		dev_err(&dev->adapter.dev,
244 			"failure setting delay to %dus\n", delay);
245 		retval = -EIO;
246 		goto error;
247 	}
248 
249 	dev->adapter.dev.parent = &dev->interface->dev;
250 
251 	/* and finally attach to i2c layer */
252 	i2c_add_adapter(&dev->adapter);
253 
254 	/* inform user about successful attachment to i2c layer */
255 	dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
256 
257 	return 0;
258 
259  error:
260 	if (dev)
261 		i2c_tiny_usb_free(dev);
262 
263 	return retval;
264 }
265 
266 static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
267 {
268 	struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
269 
270 	i2c_del_adapter(&dev->adapter);
271 	usb_set_intfdata(interface, NULL);
272 	i2c_tiny_usb_free(dev);
273 
274 	dev_dbg(&interface->dev, "disconnected\n");
275 }
276 
277 static struct usb_driver i2c_tiny_usb_driver = {
278 	.name		= "i2c-tiny-usb",
279 	.probe		= i2c_tiny_usb_probe,
280 	.disconnect	= i2c_tiny_usb_disconnect,
281 	.id_table	= i2c_tiny_usb_table,
282 };
283 
284 module_usb_driver(i2c_tiny_usb_driver);
285 
286 /* ----- end of usb layer ------------------------------------------------ */
287 
288 MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
289 MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
290 MODULE_LICENSE("GPL");
291