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 
 17 /* include interfaces to usb layer */
 18 #include <linux/usb.h>
 19 
 20 /* include interface to i2c layer */
 21 #include <linux/i2c.h>
 22 
 23 /* commands via USB, must match command ids in the firmware */
 24 #define CMD_ECHO                0
 25 #define CMD_GET_FUNC            1
 26 #define CMD_SET_DELAY           2
 27 #define CMD_GET_STATUS          3
 28 
 29 #define CMD_I2C_IO              4
 30 #define CMD_I2C_IO_BEGIN        (1<<0)
 31 #define CMD_I2C_IO_END          (1<<1)
 32 
 33 /* i2c bit delay, default is 10us -> 100kHz */
 34 static int delay = 10;
 35 module_param(delay, int, 0);
 36 MODULE_PARM_DESC(delay, "bit delay in microseconds, "
 37                  "e.g. 10 for 100kHz (default is 100kHz)");
 38 
 39 static int usb_read(struct i2c_adapter *adapter, int cmd,
 40                     int value, int index, void *data, int len);
 41 
 42 static int usb_write(struct i2c_adapter *adapter, int cmd,
 43                      int value, int index, void *data, int len);
 44 
 45 /* ----- begin of i2c layer ---------------------------------------------- */
 46 
 47 #define STATUS_IDLE             0
 48 #define STATUS_ADDRESS_ACK      1
 49 #define STATUS_ADDRESS_NAK      2
 50 
 51 static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
 52 {
 53         unsigned char status;
 54         struct i2c_msg *pmsg;
 55         int i;
 56 
 57         dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
 58 
 59         for (i = 0 ; i < num ; i++) {
 60                 int cmd = CMD_I2C_IO;
 61 
 62                 if (i == 0)
 63                         cmd |= CMD_I2C_IO_BEGIN;
 64 
 65                 if (i == num-1)
 66                         cmd |= CMD_I2C_IO_END;
 67 
 68                 pmsg = &msgs[i];
 69 
 70                 dev_dbg(&adapter->dev,
 71                         "  %d: %s (flags %d) %d bytes to 0x%02x\n",
 72                         i, pmsg->flags & I2C_M_RD ? "read" : "write",
 73                         pmsg->flags, pmsg->len, pmsg->addr);
 74 
 75                 /* and directly send the message */
 76                 if (pmsg->flags & I2C_M_RD) {
 77                         /* read data */
 78                         if (usb_read(adapter, cmd,
 79                                      pmsg->flags, pmsg->addr,
 80                                      pmsg->buf, pmsg->len) != pmsg->len) {
 81                                 dev_err(&adapter->dev,
 82                                         "failure reading data\n");
 83                                 return -EREMOTEIO;
 84                         }
 85                 } else {
 86                         /* write data */
 87                         if (usb_write(adapter, cmd,
 88                                       pmsg->flags, pmsg->addr,
 89                                       pmsg->buf, pmsg->len) != pmsg->len) {
 90                                 dev_err(&adapter->dev,
 91                                         "failure writing data\n");
 92                                 return -EREMOTEIO;
 93                         }
 94                 }
 95 
 96                 /* read status */
 97                 if (usb_read(adapter, CMD_GET_STATUS, 0, 0, &status, 1) != 1) {
 98                         dev_err(&adapter->dev, "failure reading status\n");
 99                         return -EREMOTEIO;
100                 }
101 
102                 dev_dbg(&adapter->dev, "  status = %d\n", status);
103                 if (status == STATUS_ADDRESS_NAK)
104                         return -EREMOTEIO;
105         }
106 
107         return i;
108 }
109 
110 static u32 usb_func(struct i2c_adapter *adapter)
111 {
112         u32 func;
113 
114         /* get functionality from adapter */
115         if (usb_read(adapter, CMD_GET_FUNC, 0, 0, &func, sizeof(func)) !=
116             sizeof(func)) {
117                 dev_err(&adapter->dev, "failure reading functionality\n");
118                 return 0;
119         }
120 
121         return func;
122 }
123 
124 /* This is the actual algorithm we define */
125 static const struct i2c_algorithm usb_algorithm = {
126         .master_xfer    = usb_xfer,
127         .functionality  = usb_func,
128 };
129 
130 /* ----- end of i2c layer ------------------------------------------------ */
131 
132 /* ----- begin of usb layer ---------------------------------------------- */
133 
134 /*
135  * Initially the usb i2c interface uses a vid/pid pair donated by
136  * Future Technology Devices International Ltd., later a pair was
137  * bought from EZPrototypes
138  */
139 static struct usb_device_id i2c_tiny_usb_table [] = {
140         { USB_DEVICE(0x0403, 0xc631) },   /* FTDI */
141         { USB_DEVICE(0x1c40, 0x0534) },   /* EZPrototypes */
142         { }                               /* Terminating entry */
143 };
144 
145 MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
146 
147 /* Structure to hold all of our device specific stuff */
148 struct i2c_tiny_usb {
149         struct usb_device *usb_dev; /* the usb device for this device */
150         struct usb_interface *interface; /* the interface for this device */
151         struct i2c_adapter adapter; /* i2c related things */
152 };
153 
154 static int usb_read(struct i2c_adapter *adapter, int cmd,
155                     int value, int index, void *data, int len)
156 {
157         struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
158 
159         /* do control transfer */
160         return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
161                                cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
162                                USB_DIR_IN, value, index, data, len, 2000);
163 }
164 
165 static int usb_write(struct i2c_adapter *adapter, int cmd,
166                      int value, int index, void *data, int len)
167 {
168         struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
169 
170         /* do control transfer */
171         return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
172                                cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
173                                value, index, data, len, 2000);
174 }
175 
176 static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
177 {
178         usb_put_dev(dev->usb_dev);
179         kfree(dev);
180 }
181 
182 static int i2c_tiny_usb_probe(struct usb_interface *interface,
183                               const struct usb_device_id *id)
184 {
185         struct i2c_tiny_usb *dev;
186         int retval = -ENOMEM;
187         u16 version;
188 
189         dev_dbg(&interface->dev, "probing usb device\n");
190 
191         /* allocate memory for our device state and initialize it */
192         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
193         if (dev == NULL) {
194                 dev_err(&interface->dev, "Out of memory\n");
195                 goto error;
196         }
197 
198         dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
199         dev->interface = interface;
200 
201         /* save our data pointer in this interface device */
202         usb_set_intfdata(interface, dev);
203 
204         version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
205         dev_info(&interface->dev,
206                  "version %x.%02x found at bus %03d address %03d\n",
207                  version >> 8, version & 0xff,
208                  dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
209 
210         /* setup i2c adapter description */
211         dev->adapter.owner = THIS_MODULE;
212         dev->adapter.class = I2C_CLASS_HWMON;
213         dev->adapter.algo = &usb_algorithm;
214         dev->adapter.algo_data = dev;
215         snprintf(dev->adapter.name, sizeof(dev->adapter.name),
216                  "i2c-tiny-usb at bus %03d device %03d",
217                  dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
218 
219         if (usb_write(&dev->adapter, CMD_SET_DELAY,
220                       cpu_to_le16(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.