File Index
Files and directories
Sign in
FILE

i2c-tiny-usb.c

File preview with syntax highlighting.

linux-6.19.14/drivers/i2c/busses/i2c-tiny-usb.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * driver for the i2c-tiny-usb adapter - 1.0
  4 * http://www.harbaum.org/till/i2c_tiny_usb
  5 *
  6 * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
  7 */
  8
  9#include <linux/kernel.h>
 10#include <linux/errno.h>
 11#include <linux/module.h>
 12#include <linux/slab.h>
 13#include <linux/string_choices.h>
 14#include <linux/types.h>
 15
 16/* include interfaces to usb layer */
 17#include <linux/usb.h>
 18
 19/* include interface to i2c layer */
 20#include <linux/i2c.h>
 21
 22/* commands via USB, must match command ids in the firmware */
 23#define CMD_ECHO		0
 24#define CMD_GET_FUNC		1
 25#define CMD_SET_DELAY		2
 26#define CMD_GET_STATUS		3
 27
 28#define CMD_I2C_IO		4
 29#define CMD_I2C_IO_BEGIN	(1<<0)
 30#define CMD_I2C_IO_END		(1<<1)
 31
 32/* i2c bit delay, default is 10us -> 100kHz max
 33   (in practice, due to additional delays in the i2c bitbanging
 34   code this results in a i2c clock of about 50kHz) */
 35static unsigned short delay = 10;
 36module_param(delay, ushort, 0);
 37MODULE_PARM_DESC(delay, "bit delay in microseconds "
 38		 "(default is 10us for 100kHz max)");
 39
 40static int usb_read(struct i2c_adapter *adapter, int cmd,
 41		    int value, int index, void *data, int len);
 42
 43static int usb_write(struct i2c_adapter *adapter, int cmd,
 44		     int value, int index, void *data, int len);
 45
 46/* ----- begin of i2c layer ---------------------------------------------- */
 47
 48#define STATUS_IDLE		0
 49#define STATUS_ADDRESS_ACK	1
 50#define STATUS_ADDRESS_NAK	2
 51
 52static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
 53{
 54	unsigned char *pstatus;
 55	struct i2c_msg *pmsg;
 56	int i, ret;
 57
 58	pstatus = kmalloc(sizeof(*pstatus), GFP_KERNEL);
 59	if (!pstatus)
 60		return -ENOMEM;
 61
 62	for (i = 0 ; i < num ; i++) {
 63		int cmd = CMD_I2C_IO;
 64
 65		if (i == 0)
 66			cmd |= CMD_I2C_IO_BEGIN;
 67
 68		if (i == num-1)
 69			cmd |= CMD_I2C_IO_END;
 70
 71		pmsg = &msgs[i];
 72
 73		dev_dbg(&adapter->dev,
 74			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
 75			i, str_read_write(pmsg->flags & I2C_M_RD),
 76			pmsg->flags, pmsg->len, pmsg->addr);
 77
 78		/* and directly send the message */
 79		if (pmsg->flags & I2C_M_RD) {
 80			/* read data */
 81			if (usb_read(adapter, cmd,
 82				     pmsg->flags, pmsg->addr,
 83				     pmsg->buf, pmsg->len) != pmsg->len) {
 84				dev_err(&adapter->dev,
 85					"failure reading data\n");
 86				ret = -EIO;
 87				goto out;
 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				ret = -EIO;
 97				goto out;
 98			}
 99		}
100
101		/* read status */
102		if (usb_read(adapter, CMD_GET_STATUS, 0, 0, pstatus, 1) != 1) {
103			dev_err(&adapter->dev, "failure reading status\n");
104			ret = -EIO;
105			goto out;
106		}
107
108		dev_dbg(&adapter->dev, "  status = %d\n", *pstatus);
109		if (*pstatus == STATUS_ADDRESS_NAK) {
110			ret = -ENXIO;
111			goto out;
112		}
113	}
114
115	ret = i;
116out:
117	kfree(pstatus);
118	return ret;
119}
120
121static u32 usb_func(struct i2c_adapter *adapter)
122{
123	__le32 *pfunc;
124	u32 ret;
125
126	pfunc = kmalloc(sizeof(*pfunc), GFP_KERNEL);
127
128	/* get functionality from adapter */
129	if (!pfunc || usb_read(adapter, CMD_GET_FUNC, 0, 0, pfunc,
130			       sizeof(*pfunc)) != sizeof(*pfunc)) {
131		dev_err(&adapter->dev, "failure reading functionality\n");
132		ret = 0;
133		goto out;
134	}
135
136	ret = le32_to_cpup(pfunc);
137out:
138	kfree(pfunc);
139	return ret;
140}
141
142/* prevent invalid 0-length usb_control_msg */
143static const struct i2c_adapter_quirks usb_quirks = {
144	.flags = I2C_AQ_NO_ZERO_LEN_READ,
145};
146
147/* This is the actual algorithm we define */
148static const struct i2c_algorithm usb_algorithm = {
149	.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 */
162static 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
168MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
169
170/* Structure to hold all of our device specific stuff */
171struct 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
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	/* do control transfer */
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	/* do control transfer */
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	if (interface->intf_assoc &&
230	    interface->intf_assoc->bFunctionClass != USB_CLASS_VENDOR_SPEC)
231		return -ENODEV;
232
233	dev_dbg(&interface->dev, "probing usb device\n");
234
235	/* allocate memory for our device state and initialize it */
236	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
237	if (!dev)
238		goto error;
239
240	dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
241	dev->interface = interface;
242
243	/* save our data pointer in this interface device */
244	usb_set_intfdata(interface, dev);
245
246	version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
247	dev_info(&interface->dev,
248		 "version %x.%02x found at bus %03d address %03d\n",
249		 version >> 8, version & 0xff,
250		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
251
252	/* setup i2c adapter description */
253	dev->adapter.owner = THIS_MODULE;
254	dev->adapter.class = I2C_CLASS_HWMON;
255	dev->adapter.quirks = &usb_quirks;
256	dev->adapter.algo = &usb_algorithm;
257	dev->adapter.algo_data = dev;
258	snprintf(dev->adapter.name, sizeof(dev->adapter.name),
259		 "i2c-tiny-usb at bus %03d device %03d",
260		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
261
262	if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
263		dev_err(&dev->adapter.dev,
264			"failure setting delay to %dus\n", delay);
265		retval = -EIO;
266		goto error;
267	}
268
269	dev->adapter.dev.parent = &dev->interface->dev;
270
271	/* and finally attach to i2c layer */
272	i2c_add_adapter(&dev->adapter);
273
274	/* inform user about successful attachment to i2c layer */
275	dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
276
277	return 0;
278
279 error:
280	if (dev)
281		i2c_tiny_usb_free(dev);
282
283	return retval;
284}
285
286static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
287{
288	struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
289
290	i2c_del_adapter(&dev->adapter);
291	usb_set_intfdata(interface, NULL);
292	i2c_tiny_usb_free(dev);
293
294	dev_dbg(&interface->dev, "disconnected\n");
295}
296
297static struct usb_driver i2c_tiny_usb_driver = {
298	.name		= "i2c-tiny-usb",
299	.probe		= i2c_tiny_usb_probe,
300	.disconnect	= i2c_tiny_usb_disconnect,
301	.id_table	= i2c_tiny_usb_table,
302};
303
304module_usb_driver(i2c_tiny_usb_driver);
305
306/* ----- end of usb layer ------------------------------------------------ */
307
308MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
309MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
310MODULE_LICENSE("GPL");
~/ Read-only file preview.