1
2
3
4
5
6
7
8
9
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
20#include <linux/usb.h>
21
22
23#include <linux/i2c.h>
24
25
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
36
37
38static unsigned short delay = 10;
39module_param(delay, ushort, 0);
40MODULE_PARM_DESC(delay, "bit delay in microseconds "
41 "(default is 10us for 100kHz max)");
42
43static int usb_read(struct i2c_adapter *adapter, int cmd,
44 int value, int index, void *data, int len);
45
46static int usb_write(struct i2c_adapter *adapter, int cmd,
47 int value, int index, void *data, int len);
48
49
50
51#define STATUS_IDLE 0
52#define STATUS_ADDRESS_ACK 1
53#define STATUS_ADDRESS_NAK 2
54
55static 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
84 if (pmsg->flags & I2C_M_RD) {
85
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
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
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;
121out:
122 kfree(pstatus);
123 return ret;
124}
125
126static u32 usb_func(struct i2c_adapter *adapter)
127{
128 __le32 *pfunc;
129 u32 ret;
130
131 pfunc = kmalloc(sizeof(*pfunc), GFP_KERNEL);
132
133
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);
142out:
143 kfree(pfunc);
144 return ret;
145}
146
147
148static const struct i2c_algorithm usb_algorithm = {
149 .master_xfer = usb_xfer,
150 .functionality = usb_func,
151};
152
153
154
155
156
157
158
159
160
161
162static const struct usb_device_id i2c_tiny_usb_table[] = {
163 { USB_DEVICE(0x0403, 0xc631) },
164 { USB_DEVICE(0x1c40, 0x0534) },
165 { }
166};
167
168MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
169
170
171struct i2c_tiny_usb {
172 struct usb_device *usb_dev;
173 struct usb_interface *interface;
174 struct i2c_adapter adapter;
175};
176
177static 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 void *dmadata = kmalloc(len, GFP_KERNEL);
182 int ret;
183
184 if (!dmadata)
185 return -ENOMEM;
186
187
188 ret = usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
189 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
190 USB_DIR_IN, value, index, dmadata, len, 2000);
191
192 memcpy(data, dmadata, len);
193 kfree(dmadata);
194 return ret;
195}
196
197static int usb_write(struct i2c_adapter *adapter, int cmd,
198 int value, int index, void *data, int len)
199{
200 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
201 void *dmadata = kmemdup(data, len, GFP_KERNEL);
202 int ret;
203
204 if (!dmadata)
205 return -ENOMEM;
206
207
208 ret = usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
209 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
210 value, index, dmadata, len, 2000);
211
212 kfree(dmadata);
213 return ret;
214}
215
216static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
217{
218 usb_put_dev(dev->usb_dev);
219 kfree(dev);
220}
221
222static int i2c_tiny_usb_probe(struct usb_interface *interface,
223 const struct usb_device_id *id)
224{
225 struct i2c_tiny_usb *dev;
226 int retval = -ENOMEM;
227 u16 version;
228
229 dev_dbg(&interface->dev, "probing usb device\n");
230
231
232 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
233 if (dev == NULL) {
234 dev_err(&interface->dev, "Out of memory\n");
235 goto error;
236 }
237
238 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
239 dev->interface = interface;
240
241
242 usb_set_intfdata(interface, dev);
243
244 version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
245 dev_info(&interface->dev,
246 "version %x.%02x found at bus %03d address %03d\n",
247 version >> 8, version & 0xff,
248 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
249
250
251 dev->adapter.owner = THIS_MODULE;
252 dev->adapter.class = I2C_CLASS_HWMON;
253 dev->adapter.algo = &usb_algorithm;
254 dev->adapter.algo_data = dev;
255 snprintf(dev->adapter.name, sizeof(dev->adapter.name),
256 "i2c-tiny-usb at bus %03d device %03d",
257 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
258
259 if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
260 dev_err(&dev->adapter.dev,
261 "failure setting delay to %dus\n", delay);
262 retval = -EIO;
263 goto error;
264 }
265
266 dev->adapter.dev.parent = &dev->interface->dev;
267
268
269 i2c_add_adapter(&dev->adapter);
270
271
272 dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
273
274 return 0;
275
276 error:
277 if (dev)
278 i2c_tiny_usb_free(dev);
279
280 return retval;
281}
282
283static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
284{
285 struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
286
287 i2c_del_adapter(&dev->adapter);
288 usb_set_intfdata(interface, NULL);
289 i2c_tiny_usb_free(dev);
290
291 dev_dbg(&interface->dev, "disconnected\n");
292}
293
294static struct usb_driver i2c_tiny_usb_driver = {
295 .name = "i2c-tiny-usb",
296 .probe = i2c_tiny_usb_probe,
297 .disconnect = i2c_tiny_usb_disconnect,
298 .id_table = i2c_tiny_usb_table,
299};
300
301module_usb_driver(i2c_tiny_usb_driver);
302
303
304
305MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
306MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
307MODULE_LICENSE("GPL");
308