Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  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>
 16 #include <linux/types.h>
 17 
 18 /* include interfaces to usb layer */
 19 #include <linux/usb.h>
 20 
 21 /* include interface to i2c layer */
 22 #include <linux/i2c.h>
 23 
 24 /* commands via USB, must match command ids in the firmware */
 25 #define CMD_ECHO                0
 26 #define CMD_GET_FUNC            1
 27 #define CMD_SET_DELAY           2
 28 #define CMD_GET_STATUS          3
 29 
 30 #define CMD_I2C_IO              4
 31 #define CMD_I2C_IO_BEGIN        (1<<0)
 32 #define CMD_I2C_IO_END          (1<<1)
 33 
 34 /* i2c bit delay, default is 10us -> 100kHz */
 35 static unsigned short delay = 10;
 36 module_param(delay, ushort, 0);
 37 MODULE_PARM_DESC(delay, "bit delay in microseconds, "
 38                  "e.g. 10 for 100kHz (default is 100kHz)");
 39 
 40 static int usb_read(struct i2c_adapter *adapter, int cmd,
 41                     int value, int index, void *data, int len);
 42 
 43 static 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 
 52 static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
 53 {
 54         unsigned char status;
 55         struct i2c_msg *pmsg;
 56         int i;
 57 
 58         dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
 59 
 60         for (i = 0 ; i < num ; i++) {
 61                 int cmd = CMD_I2C_IO;
 62 
 63                 if (i == 0)
 64                         cmd |= CMD_I2C_IO_BEGIN;
 65 
 66                 if (i == num-1)
 67                         cmd |= CMD_I2C_IO_END;
 68 
 69                 pmsg = &msgs[i];
 70 
 71                 dev_dbg(&adapter->dev,
 72                         "  %d: %s (flags %d) %d bytes to 0x%02x\n",
 73                         i, pmsg->flags & I2C_M_RD ? "read" : "write",
 74                         pmsg->flags, pmsg->len, pmsg->addr);
 75 
 76                 /* and directly send the message */
 77                 if (pmsg->flags & I2C_M_RD) {
 78                         /* read data */
 79                         if (usb_read(adapter, cmd,
 80                                      pmsg->flags, pmsg->addr,
 81                                      pmsg->buf, pmsg->len) != pmsg->len) {
 82                                 dev_err(&adapter->dev,
 83                                         "failure reading data\n");
 84                                 return -EREMOTEIO;
 85                         }
 86                 } else {
 87                         /* write data */
 88                         if (usb_write(adapter, cmd,
 89                                       pmsg->flags, pmsg->addr,
 90                                       pmsg->buf, pmsg->len) != pmsg->len) {
 91                                 dev_err(&adapter->dev,
 92                                         "failure writing data\n");
 93                                 return -EREMOTEIO;
 94                         }
 95                 }
 96 
 97                 /* read status */
 98                 if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
 99                         dev_err(&adapter->dev, "failure reading status\n");
100                         return -EREMOTEIO;
101                 }
102 
103                 dev_dbg(&adapter->dev, "  status = %d\n", status);
104                 if (status == STATUS_ADDRESS_NAK)
105                         return -EREMOTEIO;
106         }
107 
108         return i;
109 }
110 
111 static u32 usb_func(struct i2c_adapter *adapter)
112 {
113         __le32 func;
114 
115         /* get functionality from adapter */
116         if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
117             sizeof(func)) {
118                 dev_err(&adapter->dev, "failure reading functionality\n");
119                 return 0;
120         }
121 
122         return le32_to_cpu(func);
123 }
124 
125 /* This is the actual algorithm we define */
126 static const struct i2c_algorithm usb_algorithm = {
127         .master_xfer    = usb_xfer,
128         .functionality  = usb_func,
129 };
130 
131 /* ----- end of i2c layer ------------------------------------------------ */
132 
133 /* ----- begin of usb layer ---------------------------------------------- */
134 
135 /*
136  * Initially the usb i2c interface uses a vid/pid pair donated by
137  * Future Technology Devices International Ltd., later a pair was
138  * bought from EZPrototypes
139  */
140 static struct usb_device_id i2c_tiny_usb_table [] = {
141         { USB_DEVICE(0x0403, 0xc631) },   /* FTDI */
142         { USB_DEVICE(0x1c40, 0x0534) },   /* EZPrototypes */
143         { }                               /* Terminating entry */
144 };
145 
146 MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
147 
148 /* Structure to hold all of our device specific stuff */
149 struct i2c_tiny_usb {
150         struct usb_device *usb_dev; /* the usb device for this device */
151         struct usb_interface *interface; /* the interface for this device */
152         struct i2c_adapter adapter; /* i2c related things */
153 };
154 
155 static int usb_read(struct i2c_adapter *adapter, int cmd,
156                     int value, int index, void *data, int len)
157 {
158         struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
159 
160         /* do control transfer */
161         return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
162                                cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
163                                USB_DIR_IN, value, index, data, len, 2000);
164 }
165 
166 static int usb_write(struct i2c_adapter *adapter, int cmd,
167                      int value, int index, void *data, int len)
168 {
169         struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
170 
171         /* do control transfer */
172         return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
173                                cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
174                                value, index, data, len, 2000);
175 }
176 
177 static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
178 {
179         usb_put_dev(dev->usb_dev);
180         kfree(dev);
181 }
182 
183 static int i2c_tiny_usb_probe(struct usb_interface *interface,
184                               const struct usb_device_id *id)
185 {
186         struct i2c_tiny_usb *dev;
187         int retval = -ENOMEM;
188         u16 version;
189 
190         dev_dbg(&interface->dev, "probing usb device\n");
191 
192         /* allocate memory for our device state and initialize it */
193         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
194         if (dev == NULL) {
195                 dev_err(&interface->dev, "Out of memory\n");
196                 goto error;
197         }
198 
199         dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
200         dev->interface = interface;
201 
202         /* save our data pointer in this interface device */
203         usb_set_intfdata(interface, dev);
204 
205         version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
206         dev_info(&interface->dev,
207                  "version %x.%02x found at bus %03d address %03d\n",
208                  version >> 8, version & 0xff,
209                  dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
210 
211         /* setup i2c adapter description */
212         dev->adapter.owner = THIS_MODULE;
213         dev->adapter.class = I2C_CLASS_HWMON;
214         dev->adapter.algo = &usb_algorithm;
215         dev->adapter.algo_data = dev;
216         snprintf(dev->adapter.name, sizeof(dev->adapter.name),
217                  "i2c-tiny-usb at bus %03d device %03d",
218                  dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
219 
220         if (usb_write(&dev->adapter, CMD_SET_DELAY, delay, 0, NULL, 0) != 0) {
221                 dev_err(&dev->adapter.dev,
222                         "failure setting delay to %dus\n", delay);
223                 retval = -EIO;
224                 goto error;
225         }
226 
227         dev->adapter.dev.parent = &dev->interface->dev;
228 
229         /* and finally attach to i2c layer */
230         i2c_add_adapter(&dev->adapter);
231 
232         /* inform user about successful attachment to i2c layer */
233         dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
234 
235         return 0;
236 
237  error:
238         if (dev)
239                 i2c_tiny_usb_free(dev);
240 
241         return retval;
242 }
243 
244 static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
245 {
246         struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
247 
248         i2c_del_adapter(&dev->adapter);
249         usb_set_intfdata(interface, NULL);
250         i2c_tiny_usb_free(dev);
251 
252         dev_dbg(&interface->dev, "disconnected\n");
253 }
254 
255 static struct usb_driver i2c_tiny_usb_driver = {
256         .name           = "i2c-tiny-usb",
257         .probe          = i2c_tiny_usb_probe,
258         .disconnect     = i2c_tiny_usb_disconnect,
259         .id_table       = i2c_tiny_usb_table,
260 };
261 
262 static int __init usb_i2c_tiny_usb_init(void)
263 {
264         /* register this driver with the USB subsystem */
265         return usb_register(&i2c_tiny_usb_driver);
266 }
267 
268 static void __exit usb_i2c_tiny_usb_exit(void)
269 {
270         /* deregister this driver with the USB subsystem */
271         usb_deregister(&i2c_tiny_usb_driver);
272 }
273 
274 module_init(usb_i2c_tiny_usb_init);
275 module_exit(usb_i2c_tiny_usb_exit);
276 
277 /* ----- end of usb layer ------------------------------------------------ */
278 
279 MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
280 MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
281 MODULE_LICENSE("GPL");
282 
  This page was automatically generated by the LXR engine.