i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / usb / gadget / udc-core.c
blob4e2e13eb14bb58ac40684a5fce230732cd606914
1 /**
2 * udc.c - Core UDC Framework
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Felipe Balbi <balbi@ti.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
24 #include <linux/err.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
29 /**
30 * struct usb_udc - describes one usb device controller
31 * @driver - the gadget driver pointer. For use by the class code
32 * @dev - the child device to the actual controller
33 * @gadget - the gadget. For use by the class code
34 * @list - for use by the udc class driver
36 * This represents the internal data structure which is used by the UDC-class
37 * to hold information about udc driver and gadget together.
39 struct usb_udc {
40 struct usb_gadget_driver *driver;
41 struct usb_gadget *gadget;
42 struct device dev;
43 struct list_head list;
46 static struct class *udc_class;
47 static LIST_HEAD(udc_list);
48 static DEFINE_MUTEX(udc_lock);
50 /* ------------------------------------------------------------------------- */
52 /**
53 * usb_gadget_start - tells usb device controller to start up
54 * @gadget: The gadget we want to get started
55 * @driver: The driver we want to bind to @gadget
56 * @bind: The bind function for @driver
58 * This call is issued by the UDC Class driver when it's about
59 * to register a gadget driver to the device controller, before
60 * calling gadget driver's bind() method.
62 * It allows the controller to be powered off until strictly
63 * necessary to have it powered on.
65 * Returns zero on success, else negative errno.
67 static inline int usb_gadget_start(struct usb_gadget *gadget,
68 struct usb_gadget_driver *driver,
69 int (*bind)(struct usb_gadget *))
71 return gadget->ops->start(driver, bind);
74 /**
75 * usb_gadget_udc_start - tells usb device controller to start up
76 * @gadget: The gadget we want to get started
77 * @driver: The driver we want to bind to @gadget
79 * This call is issued by the UDC Class driver when it's about
80 * to register a gadget driver to the device controller, before
81 * calling gadget driver's bind() method.
83 * It allows the controller to be powered off until strictly
84 * necessary to have it powered on.
86 * Returns zero on success, else negative errno.
88 static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
89 struct usb_gadget_driver *driver)
91 return gadget->ops->udc_start(gadget, driver);
94 /**
95 * usb_gadget_stop - tells usb device controller we don't need it anymore
96 * @gadget: The device we want to stop activity
97 * @driver: The driver to unbind from @gadget
99 * This call is issued by the UDC Class driver after calling
100 * gadget driver's unbind() method.
102 * The details are implementation specific, but it can go as
103 * far as powering off UDC completely and disable its data
104 * line pullups.
106 static inline void usb_gadget_stop(struct usb_gadget *gadget,
107 struct usb_gadget_driver *driver)
109 gadget->ops->stop(driver);
113 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
114 * @gadget: The device we want to stop activity
115 * @driver: The driver to unbind from @gadget
117 * This call is issued by the UDC Class driver after calling
118 * gadget driver's unbind() method.
120 * The details are implementation specific, but it can go as
121 * far as powering off UDC completely and disable its data
122 * line pullups.
124 static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
125 struct usb_gadget_driver *driver)
127 gadget->ops->udc_stop(gadget, driver);
131 * usb_udc_release - release the usb_udc struct
132 * @dev: the dev member within usb_udc
134 * This is called by driver's core in order to free memory once the last
135 * reference is released.
137 static void usb_udc_release(struct device *dev)
139 struct usb_udc *udc;
141 udc = container_of(dev, struct usb_udc, dev);
142 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
143 kfree(udc);
146 static const struct attribute_group *usb_udc_attr_groups[];
148 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
149 * @parent: the parent device to this udc. Usually the controller
150 * driver's device.
151 * @gadget: the gadget to be added to the list
153 * Returns zero on success, negative errno otherwise.
155 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
157 struct usb_udc *udc;
158 int ret = -ENOMEM;
160 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
161 if (!udc)
162 goto err1;
164 device_initialize(&udc->dev);
165 udc->dev.release = usb_udc_release;
166 udc->dev.class = udc_class;
167 udc->dev.groups = usb_udc_attr_groups;
168 udc->dev.parent = parent;
169 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
170 if (ret)
171 goto err2;
173 udc->gadget = gadget;
175 mutex_lock(&udc_lock);
176 list_add_tail(&udc->list, &udc_list);
178 ret = device_add(&udc->dev);
179 if (ret)
180 goto err3;
182 mutex_unlock(&udc_lock);
184 return 0;
185 err3:
186 list_del(&udc->list);
187 mutex_unlock(&udc_lock);
189 err2:
190 put_device(&udc->dev);
192 err1:
193 return ret;
195 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
197 static int udc_is_newstyle(struct usb_udc *udc)
199 if (udc->gadget->ops->udc_start && udc->gadget->ops->udc_stop)
200 return 1;
201 return 0;
205 static void usb_gadget_remove_driver(struct usb_udc *udc)
207 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
208 udc->gadget->name);
210 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
212 if (udc_is_newstyle(udc)) {
213 udc->driver->disconnect(udc->gadget);
214 usb_gadget_disconnect(udc->gadget);
215 udc->driver->unbind(udc->gadget);
216 usb_gadget_udc_stop(udc->gadget, udc->driver);
217 } else {
218 usb_gadget_stop(udc->gadget, udc->driver);
221 udc->driver = NULL;
222 udc->dev.driver = NULL;
226 * usb_del_gadget_udc - deletes @udc from udc_list
227 * @gadget: the gadget to be removed.
229 * This, will call usb_gadget_unregister_driver() if
230 * the @udc is still busy.
232 void usb_del_gadget_udc(struct usb_gadget *gadget)
234 struct usb_udc *udc = NULL;
236 mutex_lock(&udc_lock);
237 list_for_each_entry(udc, &udc_list, list)
238 if (udc->gadget == gadget)
239 goto found;
241 dev_err(gadget->dev.parent, "gadget not registered.\n");
242 mutex_unlock(&udc_lock);
244 return;
246 found:
247 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
249 list_del(&udc->list);
250 mutex_unlock(&udc_lock);
252 if (udc->driver)
253 usb_gadget_remove_driver(udc);
255 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
256 device_unregister(&udc->dev);
258 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
260 /* ------------------------------------------------------------------------- */
262 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
263 int (*bind)(struct usb_gadget *))
265 struct usb_udc *udc = NULL;
266 int ret;
268 if (!driver || !bind || !driver->setup)
269 return -EINVAL;
271 mutex_lock(&udc_lock);
272 list_for_each_entry(udc, &udc_list, list) {
273 /* For now we take the first one */
274 if (!udc->driver)
275 goto found;
278 pr_debug("couldn't find an available UDC\n");
279 mutex_unlock(&udc_lock);
280 return -ENODEV;
282 found:
283 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
284 driver->function);
286 udc->driver = driver;
287 udc->dev.driver = &driver->driver;
289 if (udc_is_newstyle(udc)) {
290 ret = bind(udc->gadget);
291 if (ret)
292 goto err1;
293 ret = usb_gadget_udc_start(udc->gadget, driver);
294 if (ret) {
295 driver->unbind(udc->gadget);
296 goto err1;
298 usb_gadget_connect(udc->gadget);
299 } else {
301 ret = usb_gadget_start(udc->gadget, driver, bind);
302 if (ret)
303 goto err1;
307 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
308 mutex_unlock(&udc_lock);
309 return 0;
311 err1:
312 dev_err(&udc->dev, "failed to start %s: %d\n",
313 udc->driver->function, ret);
314 udc->driver = NULL;
315 udc->dev.driver = NULL;
316 mutex_unlock(&udc_lock);
317 return ret;
319 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
321 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
323 struct usb_udc *udc = NULL;
324 int ret = -ENODEV;
326 if (!driver || !driver->unbind)
327 return -EINVAL;
329 mutex_lock(&udc_lock);
330 list_for_each_entry(udc, &udc_list, list)
331 if (udc->driver == driver) {
332 usb_gadget_remove_driver(udc);
333 ret = 0;
334 break;
337 mutex_unlock(&udc_lock);
338 return ret;
340 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
342 /* ------------------------------------------------------------------------- */
344 static ssize_t usb_udc_srp_store(struct device *dev,
345 struct device_attribute *attr, const char *buf, size_t n)
347 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
349 if (sysfs_streq(buf, "1"))
350 usb_gadget_wakeup(udc->gadget);
352 return n;
354 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
356 static ssize_t usb_udc_softconn_store(struct device *dev,
357 struct device_attribute *attr, const char *buf, size_t n)
359 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
361 if (sysfs_streq(buf, "connect")) {
362 if (udc_is_newstyle(udc))
363 usb_gadget_udc_start(udc->gadget, udc->driver);
364 usb_gadget_connect(udc->gadget);
365 } else if (sysfs_streq(buf, "disconnect")) {
366 usb_gadget_disconnect(udc->gadget);
367 if (udc_is_newstyle(udc))
368 usb_gadget_udc_stop(udc->gadget, udc->driver);
369 } else {
370 dev_err(dev, "unsupported command '%s'\n", buf);
371 return -EINVAL;
374 return n;
376 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
378 #define USB_UDC_SPEED_ATTR(name, param) \
379 ssize_t usb_udc_##param##_show(struct device *dev, \
380 struct device_attribute *attr, char *buf) \
382 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
383 return snprintf(buf, PAGE_SIZE, "%s\n", \
384 usb_speed_string(udc->gadget->param)); \
386 static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
388 static USB_UDC_SPEED_ATTR(current_speed, speed);
389 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
391 /* TODO: Scheduled for removal in 3.8. */
392 static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
396 return snprintf(buf, PAGE_SIZE, "%d\n",
397 gadget_is_dualspeed(udc->gadget));
399 static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_show, NULL);
401 #define USB_UDC_ATTR(name) \
402 ssize_t usb_udc_##name##_show(struct device *dev, \
403 struct device_attribute *attr, char *buf) \
405 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
406 struct usb_gadget *gadget = udc->gadget; \
408 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
410 static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
412 static USB_UDC_ATTR(is_otg);
413 static USB_UDC_ATTR(is_a_peripheral);
414 static USB_UDC_ATTR(b_hnp_enable);
415 static USB_UDC_ATTR(a_hnp_support);
416 static USB_UDC_ATTR(a_alt_hnp_support);
418 static struct attribute *usb_udc_attrs[] = {
419 &dev_attr_srp.attr,
420 &dev_attr_soft_connect.attr,
421 &dev_attr_current_speed.attr,
422 &dev_attr_maximum_speed.attr,
424 &dev_attr_is_dualspeed.attr,
425 &dev_attr_is_otg.attr,
426 &dev_attr_is_a_peripheral.attr,
427 &dev_attr_b_hnp_enable.attr,
428 &dev_attr_a_hnp_support.attr,
429 &dev_attr_a_alt_hnp_support.attr,
430 NULL,
433 static const struct attribute_group usb_udc_attr_group = {
434 .attrs = usb_udc_attrs,
437 static const struct attribute_group *usb_udc_attr_groups[] = {
438 &usb_udc_attr_group,
439 NULL,
442 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
444 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
445 int ret;
447 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
448 if (ret) {
449 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
450 return ret;
453 if (udc->driver) {
454 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
455 udc->driver->function);
456 if (ret) {
457 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
458 return ret;
462 return 0;
465 static int __init usb_udc_init(void)
467 udc_class = class_create(THIS_MODULE, "udc");
468 if (IS_ERR(udc_class)) {
469 pr_err("failed to create udc class --> %ld\n",
470 PTR_ERR(udc_class));
471 return PTR_ERR(udc_class);
474 udc_class->dev_uevent = usb_udc_uevent;
475 return 0;
477 subsys_initcall(usb_udc_init);
479 static void __exit usb_udc_exit(void)
481 class_destroy(udc_class);
483 module_exit(usb_udc_exit);
485 MODULE_DESCRIPTION("UDC Framework");
486 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
487 MODULE_LICENSE("GPL v2");