iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-tiny-usb.c
blobcb9abe7565a7106cf3155322a2a3061522a12b0a
1 /*
2 * driver for the i2c-tiny-usb adapter - 1.0
3 * http://www.harbaum.org/till/i2c_tiny_usb
5 * Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
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.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
17 /* include interfaces to usb layer */
18 #include <linux/usb.h>
20 /* include interface to i2c layer */
21 #include <linux/i2c.h>
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
29 #define CMD_I2C_IO 4
30 #define CMD_I2C_IO_BEGIN (1<<0)
31 #define CMD_I2C_IO_END (1<<1)
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)");
39 static int usb_read(struct i2c_adapter *adapter, int cmd,
40 int value, int index, void *data, int len);
42 static int usb_write(struct i2c_adapter *adapter, int cmd,
43 int value, int index, void *data, int len);
45 /* ----- begin of i2c layer ---------------------------------------------- */
47 #define STATUS_IDLE 0
48 #define STATUS_ADDRESS_ACK 1
49 #define STATUS_ADDRESS_NAK 2
51 static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
53 unsigned char status;
54 struct i2c_msg *pmsg;
55 int i;
57 dev_dbg(&adapter->dev, "master xfer %d messages:\n", num);
59 for (i = 0 ; i < num ; i++) {
60 int cmd = CMD_I2C_IO;
62 if (i == 0)
63 cmd |= CMD_I2C_IO_BEGIN;
65 if (i == num-1)
66 cmd |= CMD_I2C_IO_END;
68 pmsg = &msgs[i];
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);
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;
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;
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;
102 dev_dbg(&adapter->dev, " status = %d\n", status);
103 if (status == STATUS_ADDRESS_NAK)
104 return -EREMOTEIO;
107 return i;
110 static u32 usb_func(struct i2c_adapter *adapter)
112 u32 func;
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;
121 return func;
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,
130 /* ----- end of i2c layer ------------------------------------------------ */
132 /* ----- begin of usb layer ---------------------------------------------- */
134 /* The usb i2c interface uses a vid/pid pair donated by */
135 /* Future Technology Devices International Ltd. */
136 static struct usb_device_id i2c_tiny_usb_table [] = {
137 { USB_DEVICE(0x0403, 0xc631) },
138 { } /* Terminating entry */
141 MODULE_DEVICE_TABLE(usb, i2c_tiny_usb_table);
143 /* Structure to hold all of our device specific stuff */
144 struct i2c_tiny_usb {
145 struct usb_device *usb_dev; /* the usb device for this device */
146 struct usb_interface *interface; /* the interface for this device */
147 struct i2c_adapter adapter; /* i2c related things */
150 static int usb_read(struct i2c_adapter *adapter, int cmd,
151 int value, int index, void *data, int len)
153 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
155 /* do control transfer */
156 return usb_control_msg(dev->usb_dev, usb_rcvctrlpipe(dev->usb_dev, 0),
157 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE |
158 USB_DIR_IN, value, index, data, len, 2000);
161 static int usb_write(struct i2c_adapter *adapter, int cmd,
162 int value, int index, void *data, int len)
164 struct i2c_tiny_usb *dev = (struct i2c_tiny_usb *)adapter->algo_data;
166 /* do control transfer */
167 return usb_control_msg(dev->usb_dev, usb_sndctrlpipe(dev->usb_dev, 0),
168 cmd, USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
169 value, index, data, len, 2000);
172 static void i2c_tiny_usb_free(struct i2c_tiny_usb *dev)
174 usb_put_dev(dev->usb_dev);
175 kfree(dev);
178 static int i2c_tiny_usb_probe(struct usb_interface *interface,
179 const struct usb_device_id *id)
181 struct i2c_tiny_usb *dev;
182 int retval = -ENOMEM;
183 u16 version;
185 dev_dbg(&interface->dev, "probing usb device\n");
187 /* allocate memory for our device state and initialize it */
188 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
189 if (dev == NULL) {
190 dev_err(&interface->dev, "Out of memory\n");
191 goto error;
194 dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
195 dev->interface = interface;
197 /* save our data pointer in this interface device */
198 usb_set_intfdata(interface, dev);
200 version = le16_to_cpu(dev->usb_dev->descriptor.bcdDevice);
201 dev_info(&interface->dev,
202 "version %x.%02x found at bus %03d address %03d\n",
203 version >> 8, version & 0xff,
204 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
206 /* setup i2c adapter description */
207 dev->adapter.owner = THIS_MODULE;
208 dev->adapter.class = I2C_CLASS_HWMON;
209 dev->adapter.algo = &usb_algorithm;
210 dev->adapter.algo_data = dev;
211 snprintf(dev->adapter.name, sizeof(dev->adapter.name),
212 "i2c-tiny-usb at bus %03d device %03d",
213 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
215 if (usb_write(&dev->adapter, CMD_SET_DELAY,
216 cpu_to_le16(delay), 0, NULL, 0) != 0) {
217 dev_err(&dev->adapter.dev,
218 "failure setting delay to %dus\n", delay);
219 retval = -EIO;
220 goto error;
223 dev->adapter.dev.parent = &dev->interface->dev;
225 /* and finally attach to i2c layer */
226 i2c_add_adapter(&dev->adapter);
228 /* inform user about successful attachment to i2c layer */
229 dev_info(&dev->adapter.dev, "connected i2c-tiny-usb device\n");
231 return 0;
233 error:
234 if (dev)
235 i2c_tiny_usb_free(dev);
237 return retval;
240 static void i2c_tiny_usb_disconnect(struct usb_interface *interface)
242 struct i2c_tiny_usb *dev = usb_get_intfdata(interface);
244 i2c_del_adapter(&dev->adapter);
245 usb_set_intfdata(interface, NULL);
246 i2c_tiny_usb_free(dev);
248 dev_dbg(&interface->dev, "disconnected\n");
251 static struct usb_driver i2c_tiny_usb_driver = {
252 .name = "i2c-tiny-usb",
253 .probe = i2c_tiny_usb_probe,
254 .disconnect = i2c_tiny_usb_disconnect,
255 .id_table = i2c_tiny_usb_table,
258 static int __init usb_i2c_tiny_usb_init(void)
260 /* register this driver with the USB subsystem */
261 return usb_register(&i2c_tiny_usb_driver);
264 static void __exit usb_i2c_tiny_usb_exit(void)
266 /* deregister this driver with the USB subsystem */
267 usb_deregister(&i2c_tiny_usb_driver);
270 module_init(usb_i2c_tiny_usb_init);
271 module_exit(usb_i2c_tiny_usb_exit);
273 /* ----- end of usb layer ------------------------------------------------ */
275 MODULE_AUTHOR("Till Harbaum <Till@Harbaum.org>");
276 MODULE_DESCRIPTION("i2c-tiny-usb driver v1.0");
277 MODULE_LICENSE("GPL");