Skip to content
Snippets Groups Projects
Select Git revision
  • 1c010ff8912cbc08d80e865aab9c32b6b00c527d
  • seco_lf-6.6.52-2.2.1 default protected
  • MODV-209-e-39-add-gpio-line-names-in-kernel-device-tree
  • niccolor/e88-lt9611uxc-dsi-complete-support
  • seco_lf-6.6.52-2.2.1_e88-lt9611uxc-i2s
  • seco_lf-6.6.52-2.2.1_e39-nxpbtuart
  • chka-modv-206-poc
  • seco_lf-6.6.52-2.2.1-rtl8211f_led
  • seco_lf-5.10.y protected
  • seco_lf_v2024.04_6.6.52_2.2.x_e39_spidev-overlay
  • seco_lf_v2024.04_6.6.52_2.2.x_e39_sdcard-card-detect-gpio-mode
  • integrate/gitlab-ci/cleaos-833-add-cnfluence-labels-into-config/into/seco_lf-5.10.y
  • integrate/gitlab-ci/cleaos-448-bitbake-logs-in-failed-job/into/seco_lf-6.6.52-2.2.1
  • integrate/gitlab-ci/cleaos-448-bitbake-logs-in-failed-job/into/seco_lf-5.10.y
  • chka-seco_lf-6.6.52-2.2.1-fix-eth-led-modv-198
  • didi/spi-cs
  • seco_lf-6.6.23-2.0.0_e39-e83-temperature-sensor
  • seco_lf-6.6.23-2.0.0_e39-e83-status-led
  • seco_lf-6.6.23-2.0.0_e39-e83-lvds-7inch-powersequence
  • chka-fix-eth-led-modv-198
  • seco_lf-6.6.52-2.2.1_e88-e83-dev
  • lf-6.6.52-2.2.1
  • lf-6.1.55-2.2.1
  • lf-6.6.3-1.0.0
  • lf-6.6.3-imx95-er2
  • lf-6.1.55-2.2.0
  • lf-6.6.y-imx95-er1
  • lf-5.15.71-2.2.2
  • lf-6.1.36-2.1.0
  • lf-5.15.71-2.2.1
  • lf-6.1.22-2.0.0
  • lf-6.1.1-1.0.1
  • rel_imx_5.4.24_2.1.4
  • rel_imx_4.9.88_2.0.13
  • rel_imx_4.14.98_2.3.5
  • lf-6.1.1-1.0.0
  • rel_imx_5.4.3_2.0.2
  • lf-5.15.71-2.2.0
  • lf-5.10.72-2.2.3
  • lf-5.15.52-2.1.0
  • imx_5.15.52_imx8ulp_er1
41 results

i2c-tiny-usb.c

Blame
  • user avatar
    Jean Delvare authored
    The functionality bit vector is always returned as a little-endian
    32-bit number by the device, so it must be byte-swapped to the host
    endianness.
    
    On the other hand, the delay value is handled by the USB stack, so no
    byte swapping is needed on our side.
    
    This fixes bug #15105:
    http://bugzilla.kernel.org/show_bug.cgi?id=15105
    
    
    
    Reported-by: default avatarJens Richter <jens@richter-stutensee.de>
    Signed-off-by: default avatarJean Delvare <khali@linux-fr.org>
    Tested-by: default avatarJens Richter <jens@richter-stutensee.de>
    Cc: Till Harbaum <till@harbaum.org>
    Cc: stable@kernel.org
    1c010ff8
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    i2c-tiny-usb.c 7.46 KiB
    /*
     * driver for the i2c-tiny-usb adapter - 1.0
     * http://www.harbaum.org/till/i2c_tiny_usb
     *
     * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License as
     * published by the Free Software Foundation, version 2.
     *
     */
    
    #include <linux/kernel.h>
    #include <linux/errno.h>
    #include <linux/module.h>
    #include <linux/types.h>
    
    /* include interfaces to usb layer */
    #include <linux/usb.h>
    
    /* include interface to i2c layer */
    #include <linux/i2c.h>
    
    /* commands via USB, must match command ids in the firmware */
    #define CMD_ECHO		0
    #define CMD_GET_FUNC		1
    #define CMD_SET_DELAY		2
    #define CMD_GET_STATUS		3
    
    #define CMD_I2C_IO		4
    #define CMD_I2C_IO_BEGIN	(1<<0)
    #define CMD_I2C_IO_END		(1<<1)
    
    /* i2c bit delay, default is 10us -> 100kHz */
    static unsigned short delay = 10;
    module_param(delay, ushort, 0);
    MODULE_PARM_DESC(delay, "bit delay in microseconds, "
    		 "e.g. 10 for 100kHz (default is 100kHz)");
    
    static int usb_read(struct i2c_adapter *adapter, int cmd,
    		    int value, int index, void *data, int len);
    
    static int usb_write(struct i2c_adapter *adapter, int cmd,
    		     int value, int index, void *data, int len);
    
    /* ----- begin of i2c layer ---------------------------------------------- */
    
    #define STATUS_IDLE		0
    #define STATUS_ADDRESS_ACK	1
    #define STATUS_ADDRESS_NAK	2
    
    static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
    {
    	unsigned char status;
    	struct i2c_msg *pmsg;
    	int i;
    
    	dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
    
    	for (i = 0 ; i < num ; i++) {
    		int cmd = CMD_I2C_IO;
    
    		if (i == 0)
    			cmd |= CMD_I2C_IO_BEGIN;
    
    		if (i == num-1)
    			cmd |= CMD_I2C_IO_END;
    
    		pmsg = &msgs[i];
    
    		dev_dbg(&adapter->dev,
    			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
    			i, pmsg->flags & I2C_M_RD ? "read" : "write",
    			pmsg->flags, pmsg->len, pmsg->addr);
    
    		/* and directly send the message */
    		if (pmsg->flags & I2C_M_RD) {
    			/* read data */
    			if (usb_read(adapter, cmd,
    				     pmsg->flags, pmsg->addr,
    				     pmsg->buf, pmsg->len) != pmsg->len) {
    				dev_err(&adapter->dev,
    					"failure reading data\n");
    				return -EREMOTEIO;
    			}
    		} else {
    			/* write data */
    			if (usb_write(adapter, cmd,
    				      pmsg->flags, pmsg->addr,
    				      pmsg->buf, pmsg->len) != pmsg->len) {
    				dev_err(&adapter->dev,
    					"failure writing data\n");
    				return -EREMOTEIO;
    			}
    		}
    
    		/* read status */
    		if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
    			dev_err(&adapter->dev, "failure reading status\n");
    			return -EREMOTEIO;
    		}
    
    		dev_dbg(&adapter->dev, "  status = %d\n", status);
    		if (status == STATUS_ADDRESS_NAK)
    			return -EREMOTEIO;
    	}
    
    	return i;
    }
    
    static u32 usb_func(struct i2c_adapter *adapter)
    {
    	__le32 func;
    
    	/* get functionality from adapter */
    	if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
    	    sizeof(func)) {
    		dev_err(&adapter->dev, "failure reading functionality\n");
    		return 0;
    	}
    
    	return le32_to_cpu(func);
    }
    
    /* This is the actual algorithm we define */
    static const struct i2c_algorithm usb_algorithm = {
    	.master_xfer	= usb_xfer,
    	.functionality	= usb_func,
    };
    
    /* ----- end of i2c layer ------------------------------------------------ */
    
    /* ----- begin of usb layer ---------------------------------------------- */
    
    /*
     * Initially the usb i2c interface uses a vid/pid pair donated by
     * Future Technology Devices International Ltd., later a pair was
     * bought from EZPrototypes
     */
    static struct usb_device_id i2c_tiny_usb_table [] = {
    	{ USB_DEVICE(0x0403, 0xc631) },   /* FTDI */
    	{ USB_DEVICE(0x1c40, 0x0534) },   /* EZPrototypes */
    	{ }                               /* Terminating entry */
    };
    
    MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
    
    /* Structure to hold all of our device specific stuff */
    struct i2c_tiny_usb {
    	struct usb_device *usb_dev; /* the usb device for this device */
    	struct usb_interface *interface; /* the interface for this device */
    	struct i2c_adapter adapter; /* i2c related things */
    };
    
    static int usb_read(struct i2c_adapter *adapter, int cmd,
    		    int value, int index, void *data, int len)
    {
    	struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
    
    	/* do control transfer */
    	return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
    			       cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
    			       USB_DIR_IN, value, index, data, len, 2000);
    }
    
    static int usb_write(struct i2c_adapter *adapter, int cmd,
    		     int value, int index, void *data, int len)
    {
    	struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
    
    	/* do control transfer */
    	return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
    			       cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
    			       value, index, data, len, 2000);
    }
    
    static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
    {
    	usb_put_dev(dev->usb_dev);
    	kfree(dev);
    }
    
    static int i2c_tiny_usb_probe(struct usb_interface *interface,
    			      const struct usb_device_id *id)
    {
    	struct i2c_tiny_usb *dev;
    	int retval = -ENOMEM;
    	u16 version;
    
    	dev_dbg(&interface->dev, "probing usb device\n");
    
    	/* allocate memory for our device state and initialize it */
    	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
    	if (dev == NULL) {
    		dev_err(&interface->dev, "Out of memory\n");
    		goto error;
    	}
    
    	dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
    	dev->interface = interface;
    
    	/* save our data pointer in this interface device */
    	usb_set_intfdata(interface, dev);
    
    	version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
    	dev_info(&interface->dev,
    		 "version %x.%02x found at bus %03d address %03d\n",
    		 version >> 8, version & 0xff,
    		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
    
    	/* setup i2c adapter description */
    	dev->adapter.owner = THIS_MODULE;
    	dev->adapter.class = I2C_CLASS_HWMON;
    	dev->adapter.algo = &usb_algorithm;
    	dev->adapter.algo_data = dev;
    	snprintf(dev->adapter.name, sizeof(dev->adapter.name),
    		 "i2c-tiny-usb at bus %03d device %03d",
    		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
    
    	if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
    		dev_err(&dev->adapter.dev,
    			"failure setting delay to %dus\n", delay);
    		retval = -EIO;
    		goto error;
    	}
    
    	dev->adapter.dev.parent = &dev->interface->dev;
    
    	/* and finally attach to i2c layer */
    	i2c_add_adapter(&dev->adapter);
    
    	/* inform user about successful attachment to i2c layer */
    	dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
    
    	return 0;
    
     error:
    	if (dev)
    		i2c_tiny_usb_free(dev);
    
    	return retval;
    }
    
    static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
    {
    	struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
    
    	i2c_del_adapter(&dev->adapter);
    	usb_set_intfdata(interface, NULL);
    	i2c_tiny_usb_free(dev);
    
    	dev_dbg(&interface->dev, "disconnected\n");
    }
    
    static struct usb_driver i2c_tiny_usb_driver = {
    	.name		= "i2c-tiny-usb",
    	.probe		= i2c_tiny_usb_probe,
    	.disconnect	= i2c_tiny_usb_disconnect,
    	.id_table	= i2c_tiny_usb_table,
    };
    
    static int __init usb_i2c_tiny_usb_init(void)
    {
    	/* register this driver with the USB subsystem */
    	return usb_register(&i2c_tiny_usb_driver);
    }
    
    static void __exit usb_i2c_tiny_usb_exit(void)
    {
    	/* deregister this driver with the USB subsystem */
    	usb_deregister(&i2c_tiny_usb_driver);
    }
    
    module_init(usb_i2c_tiny_usb_init);
    module_exit(usb_i2c_tiny_usb_exit);
    
    /* ----- end of usb layer ------------------------------------------------ */
    
    MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
    MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
    MODULE_LICENSE("GPL");