usb: gadget: r8a66597-udc: fix cannot connect after rmmod gadget driver
[linux/fpc-iii.git] / drivers / usb / gadget / udc-core.c
blob05ba47214361ba8625ab0331d8fc8e3e32d56090
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 usb_gadget_disconnect(udc->gadget);
214 udc->driver->unbind(udc->gadget);
215 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 = dev_get_drvdata(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 = dev_get_drvdata(dev);
361 if (sysfs_streq(buf, "connect")) {
362 usb_gadget_connect(udc->gadget);
363 } else if (sysfs_streq(buf, "disconnect")) {
364 usb_gadget_disconnect(udc->gadget);
365 } else {
366 dev_err(dev, "unsupported command '%s'\n", buf);
367 return -EINVAL;
370 return n;
372 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
374 static ssize_t usb_udc_speed_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
378 struct usb_gadget *gadget = udc->gadget;
380 switch (gadget->speed) {
381 case USB_SPEED_LOW:
382 return snprintf(buf, PAGE_SIZE, "low-speed\n");
383 case USB_SPEED_FULL:
384 return snprintf(buf, PAGE_SIZE, "full-speed\n");
385 case USB_SPEED_HIGH:
386 return snprintf(buf, PAGE_SIZE, "high-speed\n");
387 case USB_SPEED_WIRELESS:
388 return snprintf(buf, PAGE_SIZE, "wireless\n");
389 case USB_SPEED_SUPER:
390 return snprintf(buf, PAGE_SIZE, "super-speed\n");
391 case USB_SPEED_UNKNOWN: /* FALLTHROUGH */
392 default:
393 return snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
396 static DEVICE_ATTR(speed, S_IRUSR, usb_udc_speed_show, NULL);
398 #define USB_UDC_ATTR(name) \
399 ssize_t usb_udc_##name##_show(struct device *dev, \
400 struct device_attribute *attr, char *buf) \
402 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
403 struct usb_gadget *gadget = udc->gadget; \
405 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
407 static DEVICE_ATTR(name, S_IRUSR, usb_udc_##name##_show, NULL)
409 static USB_UDC_ATTR(is_dualspeed);
410 static USB_UDC_ATTR(is_otg);
411 static USB_UDC_ATTR(is_a_peripheral);
412 static USB_UDC_ATTR(b_hnp_enable);
413 static USB_UDC_ATTR(a_hnp_support);
414 static USB_UDC_ATTR(a_alt_hnp_support);
416 static struct attribute *usb_udc_attrs[] = {
417 &dev_attr_srp.attr,
418 &dev_attr_soft_connect.attr,
419 &dev_attr_speed.attr,
421 &dev_attr_is_dualspeed.attr,
422 &dev_attr_is_otg.attr,
423 &dev_attr_is_a_peripheral.attr,
424 &dev_attr_b_hnp_enable.attr,
425 &dev_attr_a_hnp_support.attr,
426 &dev_attr_a_alt_hnp_support.attr,
427 NULL,
430 static const struct attribute_group usb_udc_attr_group = {
431 .attrs = usb_udc_attrs,
434 static const struct attribute_group *usb_udc_attr_groups[] = {
435 &usb_udc_attr_group,
436 NULL,
439 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
441 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
442 int ret;
444 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
445 if (ret) {
446 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
447 return ret;
450 if (udc->driver) {
451 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
452 udc->driver->function);
453 if (ret) {
454 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
455 return ret;
459 return 0;
462 static int __init usb_udc_init(void)
464 udc_class = class_create(THIS_MODULE, "udc");
465 if (IS_ERR(udc_class)) {
466 pr_err("failed to create udc class --> %ld\n",
467 PTR_ERR(udc_class));
468 return PTR_ERR(udc_class);
471 udc_class->dev_uevent = usb_udc_uevent;
472 return 0;
474 subsys_initcall(usb_udc_init);
476 static void __exit usb_udc_exit(void)
478 class_destroy(udc_class);
480 module_exit(usb_udc_exit);
482 MODULE_DESCRIPTION("UDC Framework");
483 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
484 MODULE_LICENSE("GPL v2");