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