blob: 05106368d405d21c1f4aae261394f25ea7b29d7b [file] [log] [blame]
Till Harbaume8c76ee2007-05-01 23:26:35 +02001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Jean Delvare1c010ff2010-02-05 17:48:13 +010017#include <linux/types.h>
Till Harbaume8c76ee2007-05-01 23:26:35 +020018
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
Jean Delvarec05d4902010-03-02 12:23:40 +010035/* 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) */
Jean Delvare1c010ff2010-02-05 17:48:13 +010038static unsigned short delay = 10;
39module_param(delay, ushort, 0);
Jean Delvarec05d4902010-03-02 12:23:40 +010040MODULE_PARM_DESC(delay, "bit delay in microseconds "
41 "(default is 10us for 100kHz max)");
Till Harbaume8c76ee2007-05-01 23:26:35 +020042
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/* ----- begin of i2c layer ---------------------------------------------- */
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 status;
58 struct i2c_msg *pmsg;
59 int i;
60
61 dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
62
63 for (i = 0 ; i < num ; i++) {
64 int cmd = CMD_I2C_IO;
65
66 if (i == 0)
67 cmd |= CMD_I2C_IO_BEGIN;
68
69 if (i == num-1)
70 cmd |= CMD_I2C_IO_END;
71
72 pmsg = &msgs[i];
73
74 dev_dbg(&adapter->dev,
75 " %d: %s (flags %d) %d bytes to 0x%02x\n",
76 i, pmsg->flags & I2C_M_RD ? "read" : "write",
77 pmsg->flags, pmsg->len, pmsg->addr);
78
79 /* and directly send the message */
80 if (pmsg->flags & I2C_M_RD) {
81 /* read data */
82 if (usb_read(adapter, cmd,
83 pmsg->flags, pmsg->addr,
84 pmsg->buf, pmsg->len) != pmsg->len) {
85 dev_err(&adapter->dev,
86 "failure reading data\n");
87 return -EREMOTEIO;
88 }
89 } else {
90 /* write data */
91 if (usb_write(adapter, cmd,
92 pmsg->flags, pmsg->addr,
93 pmsg->buf, pmsg->len) != pmsg->len) {
94 dev_err(&adapter->dev,
95 "failure writing data\n");
96 return -EREMOTEIO;
97 }
98 }
99
100 /* read status */
101 if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
102 dev_err(&adapter->dev, "failure reading status\n");
103 return -EREMOTEIO;
104 }
105
106 dev_dbg(&adapter->dev, " status = %d\n", status);
107 if (status == STATUS_ADDRESS_NAK)
108 return -EREMOTEIO;
109 }
110
111 return i;
112}
113
114static u32 usb_func(struct i2c_adapter *adapter)
115{
Jean Delvare1c010ff2010-02-05 17:48:13 +0100116 __le32 func;
Till Harbaume8c76ee2007-05-01 23:26:35 +0200117
118 /* get functionality from adapter */
119 if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
120 sizeof(func)) {
121 dev_err(&adapter->dev, "failure reading functionality\n");
122 return 0;
123 }
124
Jean Delvare1c010ff2010-02-05 17:48:13 +0100125 return le32_to_cpu(func);
Till Harbaume8c76ee2007-05-01 23:26:35 +0200126}
127
128/* This is the actual algorithm we define */
129static const struct i2c_algorithm usb_algorithm = {
130 .master_xfer = usb_xfer,
131 .functionality = usb_func,
132};
133
134/* ----- end of i2c layer ------------------------------------------------ */
135
136/* ----- begin of usb layer ---------------------------------------------- */
137
Till Harbaumfa16eef2008-04-11 12:07:05 +0200138/*
139 * Initially the usb i2c interface uses a vid/pid pair donated by
140 * Future Technology Devices International Ltd., later a pair was
141 * bought from EZPrototypes
142 */
Márton Németh4111ecd2010-03-02 12:23:37 +0100143static const struct usb_device_id i2c_tiny_usb_table[] = {
Till Harbaumfa16eef2008-04-11 12:07:05 +0200144 { USB_DEVICE(0x0403, 0xc631) }, /* FTDI */
145 { USB_DEVICE(0x1c40, 0x0534) }, /* EZPrototypes */
Emmanuel Deloget68a76022012-07-24 14:13:59 +0200146 { USB_DEVICE(0x1964, 0x0001) }, /* Robofuzz OSIF */
Till Harbaumfa16eef2008-04-11 12:07:05 +0200147 { } /* Terminating entry */
Till Harbaume8c76ee2007-05-01 23:26:35 +0200148};
149
150MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
151
152/* Structure to hold all of our device specific stuff */
153struct i2c_tiny_usb {
154 struct usb_device *usb_dev; /* the usb device for this device */
155 struct usb_interface *interface; /* the interface for this device */
156 struct i2c_adapter adapter; /* i2c related things */
157};
158
159static int usb_read(struct i2c_adapter *adapter, int cmd,
160 int value, int index, void *data, int len)
161{
162 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
163
164 /* do control transfer */
165 return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
166 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
167 USB_DIR_IN, value, index, data, len, 2000);
168}
169
170static int usb_write(struct i2c_adapter *adapter, int cmd,
171 int value, int index, void *data, int len)
172{
173 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
174
175 /* do control transfer */
176 return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
177 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
178 value, index, data, len, 2000);
179}
180
181static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
182{
183 usb_put_dev(dev->usb_dev);
184 kfree(dev);
185}
186
187static int i2c_tiny_usb_probe(struct usb_interface *interface,
188 const struct usb_device_id *id)
189{
190 struct i2c_tiny_usb *dev;
191 int retval = -ENOMEM;
192 u16 version;
193
194 dev_dbg(&interface->dev, "probing usb device\n");
195
196 /* allocate memory for our device state and initialize it */
197 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
198 if (dev == NULL) {
199 dev_err(&interface->dev, "Out of memory\n");
200 goto error;
201 }
202
203 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
204 dev->interface = interface;
205
206 /* save our data pointer in this interface device */
207 usb_set_intfdata(interface, dev);
208
209 version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
210 dev_info(&interface->dev,
211 "version %x.%02x found at bus %03d address %03d\n",
212 version >> 8, version & 0xff,
213 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
214
215 /* setup i2c adapter description */
216 dev->adapter.owner = THIS_MODULE;
217 dev->adapter.class = I2C_CLASS_HWMON;
218 dev->adapter.algo = &usb_algorithm;
219 dev->adapter.algo_data = dev;
Jean Delvare6a7ce822007-05-22 19:49:16 +0200220 snprintf(dev->adapter.name, sizeof(dev->adapter.name),
Till Harbaume8c76ee2007-05-01 23:26:35 +0200221 "i2c-tiny-usb at bus %03d device %03d",
222 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
223
Jean Delvare1c010ff2010-02-05 17:48:13 +0100224 if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
Till Harbaume8c76ee2007-05-01 23:26:35 +0200225 dev_err(&dev->adapter.dev,
226 "failure setting delay to %dus\n", delay);
227 retval = -EIO;
228 goto error;
229 }
230
231 dev->adapter.dev.parent = &dev->interface->dev;
232
233 /* and finally attach to i2c layer */
234 i2c_add_adapter(&dev->adapter);
235
236 /* inform user about successful attachment to i2c layer */
237 dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
238
239 return 0;
240
241 error:
242 if (dev)
243 i2c_tiny_usb_free(dev);
244
245 return retval;
246}
247
248static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
249{
250 struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
251
252 i2c_del_adapter(&dev->adapter);
253 usb_set_intfdata(interface, NULL);
254 i2c_tiny_usb_free(dev);
255
256 dev_dbg(&interface->dev, "disconnected\n");
257}
258
259static struct usb_driver i2c_tiny_usb_driver = {
260 .name = "i2c-tiny-usb",
261 .probe = i2c_tiny_usb_probe,
262 .disconnect = i2c_tiny_usb_disconnect,
263 .id_table = i2c_tiny_usb_table,
264};
265
Greg Kroah-Hartmanfe748482011-11-18 09:52:10 -0800266module_usb_driver(i2c_tiny_usb_driver);
Till Harbaume8c76ee2007-05-01 23:26:35 +0200267
268/* ----- end of usb layer ------------------------------------------------ */
269
270MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
271MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
272MODULE_LICENSE("GPL");