blob: d69c35558f6852beecd1bfc85acee35c26781ec8 [file] [log] [blame]
Felipe Balbi2ccea032011-06-28 16:33:46 +03001/**
2 * udc.c - Core UDC Framework
3 *
4 * Copyright (C) 2010 Texas Instruments
5 * Author: Felipe Balbi <balbi@ti.com>
6 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
Felipe Balbia6989082011-12-15 13:31:48 +020025#include <linux/dma-mapping.h>
Felipe Balbi5702f752013-07-17 11:09:49 +030026#include <linux/workqueue.h>
Felipe Balbi2ccea032011-06-28 16:33:46 +030027
28#include <linux/usb/ch9.h>
29#include <linux/usb/gadget.h>
Michal Sojka0cfbd322014-09-24 22:43:21 +020030#include <linux/usb.h>
Felipe Balbi2ccea032011-06-28 16:33:46 +030031
32/**
33 * struct usb_udc - describes one usb device controller
34 * @driver - the gadget driver pointer. For use by the class code
35 * @dev - the child device to the actual controller
36 * @gadget - the gadget. For use by the class code
37 * @list - for use by the udc class driver
Peter Chen628ef0d2015-03-06 10:36:03 +080038 * @vbus - for udcs who care about vbus status, this value is real vbus status;
39 * for udcs who do not care about vbus status, this value is always true
Felipe Balbi2ccea032011-06-28 16:33:46 +030040 *
41 * This represents the internal data structure which is used by the UDC-class
42 * to hold information about udc driver and gadget together.
43 */
44struct usb_udc {
45 struct usb_gadget_driver *driver;
46 struct usb_gadget *gadget;
47 struct device dev;
48 struct list_head list;
Peter Chen628ef0d2015-03-06 10:36:03 +080049 bool vbus;
Felipe Balbi2ccea032011-06-28 16:33:46 +030050};
51
52static struct class *udc_class;
Felipe Balbi2ccea032011-06-28 16:33:46 +030053static LIST_HEAD(udc_list);
54static DEFINE_MUTEX(udc_lock);
55
56/* ------------------------------------------------------------------------- */
57
Alan Stern908b9612013-07-12 11:06:21 -040058#ifdef CONFIG_HAS_DMA
59
Felipe Balbia6989082011-12-15 13:31:48 +020060int usb_gadget_map_request(struct usb_gadget *gadget,
61 struct usb_request *req, int is_in)
62{
63 if (req->length == 0)
64 return 0;
65
66 if (req->num_sgs) {
67 int mapped;
68
69 mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs,
70 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
71 if (mapped == 0) {
72 dev_err(&gadget->dev, "failed to map SGs\n");
73 return -EFAULT;
74 }
75
76 req->num_mapped_sgs = mapped;
77 } else {
78 req->dma = dma_map_single(&gadget->dev, req->buf, req->length,
79 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
80
81 if (dma_mapping_error(&gadget->dev, req->dma)) {
82 dev_err(&gadget->dev, "failed to map buffer\n");
83 return -EFAULT;
84 }
85 }
86
87 return 0;
88}
89EXPORT_SYMBOL_GPL(usb_gadget_map_request);
90
91void usb_gadget_unmap_request(struct usb_gadget *gadget,
92 struct usb_request *req, int is_in)
93{
94 if (req->length == 0)
95 return;
96
97 if (req->num_mapped_sgs) {
98 dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs,
99 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
100
101 req->num_mapped_sgs = 0;
102 } else {
103 dma_unmap_single(&gadget->dev, req->dma, req->length,
104 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
105 }
106}
107EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
108
Alan Stern908b9612013-07-12 11:06:21 -0400109#endif /* CONFIG_HAS_DMA */
110
Felipe Balbia6989082011-12-15 13:31:48 +0200111/* ------------------------------------------------------------------------- */
112
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200113/**
114 * usb_gadget_giveback_request - give the request back to the gadget layer
115 * Context: in_interrupt()
116 *
117 * This is called by device controller drivers in order to return the
118 * completed request back to the gadget layer.
119 */
120void usb_gadget_giveback_request(struct usb_ep *ep,
121 struct usb_request *req)
122{
Michal Sojka0cfbd322014-09-24 22:43:21 +0200123 if (likely(req->status == 0))
124 usb_led_activity(USB_LED_EVENT_GADGET);
125
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200126 req->complete(ep, req);
127}
128EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
129
130/* ------------------------------------------------------------------------- */
131
Felipe Balbi5702f752013-07-17 11:09:49 +0300132static void usb_gadget_state_work(struct work_struct *work)
133{
Peter Chendfea9c92015-03-06 10:36:02 +0800134 struct usb_gadget *gadget = work_to_gadget(work);
135 struct usb_udc *udc = gadget->udc;
Felipe Balbi5702f752013-07-17 11:09:49 +0300136
Peter Chendfea9c92015-03-06 10:36:02 +0800137 if (udc)
138 sysfs_notify(&udc->dev.kobj, NULL, "state");
Felipe Balbi5702f752013-07-17 11:09:49 +0300139}
140
Felipe Balbi49401f42011-12-19 12:57:04 +0200141void usb_gadget_set_state(struct usb_gadget *gadget,
142 enum usb_device_state state)
143{
144 gadget->state = state;
Felipe Balbi5702f752013-07-17 11:09:49 +0300145 schedule_work(&gadget->work);
Felipe Balbi49401f42011-12-19 12:57:04 +0200146}
147EXPORT_SYMBOL_GPL(usb_gadget_set_state);
148
149/* ------------------------------------------------------------------------- */
150
Peter Chen628ef0d2015-03-06 10:36:03 +0800151static void usb_udc_connect_control(struct usb_udc *udc)
152{
153 if (udc->vbus)
154 usb_gadget_connect(udc->gadget);
155 else
156 usb_gadget_disconnect(udc->gadget);
157}
158
159/**
160 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
161 * connect or disconnect gadget
162 * @gadget: The gadget which vbus change occurs
163 * @status: The vbus status
164 *
165 * The udc driver calls it when it wants to connect or disconnect gadget
166 * according to vbus status.
167 */
168void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
169{
170 struct usb_udc *udc = gadget->udc;
171
172 if (udc) {
173 udc->vbus = status;
174 usb_udc_connect_control(udc);
175 }
176}
177EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
178
Felipe Balbi2ccea032011-06-28 16:33:46 +0300179/**
Peter Chen974a70b2014-09-12 09:32:41 +0800180 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
181 * @gadget: The gadget which bus reset occurs
182 * @driver: The gadget driver we want to notify
183 *
184 * If the udc driver has bus reset handler, it needs to call this when the bus
185 * reset occurs, it notifies the gadget driver that the bus reset occurs as
186 * well as updates gadget state.
187 */
188void usb_gadget_udc_reset(struct usb_gadget *gadget,
189 struct usb_gadget_driver *driver)
190{
191 driver->reset(gadget);
192 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
193}
194EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
195
196/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200197 * usb_gadget_udc_start - tells usb device controller to start up
Felipe Balbi2c683342014-10-17 11:34:07 -0500198 * @udc: The UDC to be started
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200199 *
200 * This call is issued by the UDC Class driver when it's about
201 * to register a gadget driver to the device controller, before
202 * calling gadget driver's bind() method.
203 *
204 * It allows the controller to be powered off until strictly
205 * necessary to have it powered on.
206 *
207 * Returns zero on success, else negative errno.
208 */
Felipe Balbi2c683342014-10-17 11:34:07 -0500209static inline int usb_gadget_udc_start(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200210{
Felipe Balbi2c683342014-10-17 11:34:07 -0500211 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200212}
213
214/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200215 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
216 * @gadget: The device we want to stop activity
217 * @driver: The driver to unbind from @gadget
218 *
219 * This call is issued by the UDC Class driver after calling
220 * gadget driver's unbind() method.
221 *
222 * The details are implementation specific, but it can go as
223 * far as powering off UDC completely and disable its data
224 * line pullups.
225 */
Felipe Balbi2c683342014-10-17 11:34:07 -0500226static inline void usb_gadget_udc_stop(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200227{
Felipe Balbi22835b82014-10-17 12:05:12 -0500228 udc->gadget->ops->udc_stop(udc->gadget);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200229}
230
231/**
Felipe Balbi2ccea032011-06-28 16:33:46 +0300232 * usb_udc_release - release the usb_udc struct
233 * @dev: the dev member within usb_udc
234 *
235 * This is called by driver's core in order to free memory once the last
236 * reference is released.
237 */
238static void usb_udc_release(struct device *dev)
239{
240 struct usb_udc *udc;
241
242 udc = container_of(dev, struct usb_udc, dev);
243 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
244 kfree(udc);
245}
246
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200247static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200248
249static void usb_udc_nop_release(struct device *dev)
250{
251 dev_vdbg(dev, "%s\n", __func__);
252}
253
Felipe Balbi2ccea032011-06-28 16:33:46 +0300254/**
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200255 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
256 * @parent: the parent device to this udc. Usually the controller driver's
257 * device.
258 * @gadget: the gadget to be added to the list.
259 * @release: a gadget release function.
Felipe Balbi2ccea032011-06-28 16:33:46 +0300260 *
261 * Returns zero on success, negative errno otherwise.
262 */
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200263int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
264 void (*release)(struct device *dev))
Felipe Balbi2ccea032011-06-28 16:33:46 +0300265{
266 struct usb_udc *udc;
267 int ret = -ENOMEM;
268
269 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
270 if (!udc)
271 goto err1;
272
Felipe Balbi7bce4012013-01-24 17:41:00 +0200273 dev_set_name(&gadget->dev, "gadget");
Felipe Balbi5702f752013-07-17 11:09:49 +0300274 INIT_WORK(&gadget->work, usb_gadget_state_work);
Felipe Balbi2ed14322013-02-26 10:59:41 +0200275 gadget->dev.parent = parent;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200276
Alan Stern908b9612013-07-12 11:06:21 -0400277#ifdef CONFIG_HAS_DMA
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200278 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
279 gadget->dev.dma_parms = parent->dma_parms;
280 gadget->dev.dma_mask = parent->dma_mask;
Alan Stern908b9612013-07-12 11:06:21 -0400281#endif
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200282
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200283 if (release)
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200284 gadget->dev.release = release;
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200285 else
286 gadget->dev.release = usb_udc_nop_release;
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200287
Felipe Balbi7bce4012013-01-24 17:41:00 +0200288 ret = device_register(&gadget->dev);
289 if (ret)
290 goto err2;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200291
Felipe Balbi2ccea032011-06-28 16:33:46 +0300292 device_initialize(&udc->dev);
293 udc->dev.release = usb_udc_release;
294 udc->dev.class = udc_class;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200295 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300296 udc->dev.parent = parent;
297 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
298 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200299 goto err3;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300300
301 udc->gadget = gadget;
Peter Chendfea9c92015-03-06 10:36:02 +0800302 gadget->udc = udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300303
304 mutex_lock(&udc_lock);
305 list_add_tail(&udc->list, &udc_list);
306
307 ret = device_add(&udc->dev);
308 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200309 goto err4;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300310
Felipe Balbi49401f42011-12-19 12:57:04 +0200311 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
Peter Chen628ef0d2015-03-06 10:36:03 +0800312 udc->vbus = true;
Felipe Balbi49401f42011-12-19 12:57:04 +0200313
Felipe Balbi2ccea032011-06-28 16:33:46 +0300314 mutex_unlock(&udc_lock);
315
316 return 0;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200317
318err4:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300319 list_del(&udc->list);
320 mutex_unlock(&udc_lock);
321
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200322err3:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300323 put_device(&udc->dev);
324
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200325err2:
Felipe Balbi7bce4012013-01-24 17:41:00 +0200326 put_device(&gadget->dev);
Felipe Balbic5dbc222013-04-02 17:06:28 +0300327 kfree(udc);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200328
Felipe Balbi2ccea032011-06-28 16:33:46 +0300329err1:
330 return ret;
331}
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200332EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
333
Felipe Balbi2ccea032011-06-28 16:33:46 +0300334/**
335 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
336 * @parent: the parent device to this udc. Usually the controller
337 * driver's device.
338 * @gadget: the gadget to be added to the list
339 *
340 * Returns zero on success, negative errno otherwise.
341 */
342int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
343{
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200344 return usb_add_gadget_udc_release(parent, gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300345}
346EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
347
348static void usb_gadget_remove_driver(struct usb_udc *udc)
349{
350 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
Felipe Balbi8da9fe82014-10-17 11:54:46 -0500351 udc->driver->function);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300352
353 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
354
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200355 usb_gadget_disconnect(udc->gadget);
356 udc->driver->disconnect(udc->gadget);
357 udc->driver->unbind(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -0500358 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300359
360 udc->driver = NULL;
361 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200362 udc->gadget->dev.driver = NULL;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300363}
364
365/**
366 * usb_del_gadget_udc - deletes @udc from udc_list
367 * @gadget: the gadget to be removed.
368 *
369 * This, will call usb_gadget_unregister_driver() if
370 * the @udc is still busy.
371 */
372void usb_del_gadget_udc(struct usb_gadget *gadget)
373{
Peter Chendfea9c92015-03-06 10:36:02 +0800374 struct usb_udc *udc = gadget->udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300375
Peter Chendfea9c92015-03-06 10:36:02 +0800376 if (!udc)
377 return;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300378
Felipe Balbi2ccea032011-06-28 16:33:46 +0300379 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
380
Peter Chendfea9c92015-03-06 10:36:02 +0800381 mutex_lock(&udc_lock);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300382 list_del(&udc->list);
383 mutex_unlock(&udc_lock);
384
385 if (udc->driver)
386 usb_gadget_remove_driver(udc);
387
388 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
Felipe Balbi5702f752013-07-17 11:09:49 +0300389 flush_work(&gadget->work);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300390 device_unregister(&udc->dev);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200391 device_unregister(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300392}
393EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
394
395/* ------------------------------------------------------------------------- */
396
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100397static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300398{
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100399 int ret;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300400
Felipe Balbi2ccea032011-06-28 16:33:46 +0300401 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
402 driver->function);
403
404 udc->driver = driver;
405 udc->dev.driver = &driver->driver;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200406 udc->gadget->dev.driver = &driver->driver;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300407
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200408 ret = driver->bind(udc->gadget, driver);
409 if (ret)
410 goto err1;
Felipe Balbi2c683342014-10-17 11:34:07 -0500411 ret = usb_gadget_udc_start(udc);
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200412 if (ret) {
413 driver->unbind(udc->gadget);
414 goto err1;
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200415 }
Peter Chen628ef0d2015-03-06 10:36:03 +0800416 usb_udc_connect_control(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300417
418 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300419 return 0;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300420err1:
Fabio Estevamf8cffc82013-09-28 00:05:34 -0300421 if (ret != -EISNAM)
422 dev_err(&udc->dev, "failed to start %s: %d\n",
Felipe Balbi2ccea032011-06-28 16:33:46 +0300423 udc->driver->function, ret);
424 udc->driver = NULL;
425 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200426 udc->gadget->dev.driver = NULL;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100427 return ret;
428}
429
Felipe Balbi02e8c962014-10-17 18:57:06 -0500430int usb_udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100431{
432 struct usb_udc *udc = NULL;
433 int ret = -ENODEV;
434
435 mutex_lock(&udc_lock);
436 list_for_each_entry(udc, &udc_list, list) {
437 ret = strcmp(name, dev_name(&udc->dev));
438 if (!ret)
439 break;
440 }
441 if (ret) {
442 ret = -ENODEV;
443 goto out;
444 }
445 if (udc->driver) {
446 ret = -EBUSY;
447 goto out;
448 }
449 ret = udc_bind_to_driver(udc, driver);
450out:
451 mutex_unlock(&udc_lock);
452 return ret;
453}
Felipe Balbi02e8c962014-10-17 18:57:06 -0500454EXPORT_SYMBOL_GPL(usb_udc_attach_driver);
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100455
456int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
457{
458 struct usb_udc *udc = NULL;
459 int ret;
460
461 if (!driver || !driver->bind || !driver->setup)
462 return -EINVAL;
463
464 mutex_lock(&udc_lock);
465 list_for_each_entry(udc, &udc_list, list) {
466 /* For now we take the first one */
467 if (!udc->driver)
468 goto found;
469 }
470
471 pr_debug("couldn't find an available UDC\n");
472 mutex_unlock(&udc_lock);
473 return -ENODEV;
474found:
475 ret = udc_bind_to_driver(udc, driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300476 mutex_unlock(&udc_lock);
477 return ret;
478}
479EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
480
481int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
482{
483 struct usb_udc *udc = NULL;
484 int ret = -ENODEV;
485
486 if (!driver || !driver->unbind)
487 return -EINVAL;
488
489 mutex_lock(&udc_lock);
490 list_for_each_entry(udc, &udc_list, list)
491 if (udc->driver == driver) {
492 usb_gadget_remove_driver(udc);
Peter Chenb5fb8d02014-04-29 13:26:29 +0800493 usb_gadget_set_state(udc->gadget,
494 USB_STATE_NOTATTACHED);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300495 ret = 0;
496 break;
497 }
498
499 mutex_unlock(&udc_lock);
500 return ret;
501}
502EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
503
504/* ------------------------------------------------------------------------- */
505
506static ssize_t usb_udc_srp_store(struct device *dev,
507 struct device_attribute *attr, const char *buf, size_t n)
508{
Felipe Balbi1d91a962011-09-08 14:11:17 +0300509 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300510
511 if (sysfs_streq(buf, "1"))
512 usb_gadget_wakeup(udc->gadget);
513
514 return n;
515}
516static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
517
518static ssize_t usb_udc_softconn_store(struct device *dev,
519 struct device_attribute *attr, const char *buf, size_t n)
520{
Felipe Balbi865569b2011-09-08 14:12:18 +0300521 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300522
Felipe Balbibfa6b182014-10-17 11:10:25 -0500523 if (!udc->driver) {
524 dev_err(dev, "soft-connect without a gadget driver\n");
525 return -EOPNOTSUPP;
526 }
527
Felipe Balbi2ccea032011-06-28 16:33:46 +0300528 if (sysfs_streq(buf, "connect")) {
Felipe Balbi2c683342014-10-17 11:34:07 -0500529 usb_gadget_udc_start(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300530 usb_gadget_connect(udc->gadget);
531 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +0300532 usb_gadget_disconnect(udc->gadget);
Felipe Balbi0abd0692014-10-09 10:12:24 -0500533 udc->driver->disconnect(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -0500534 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300535 } else {
536 dev_err(dev, "unsupported command '%s'\n", buf);
537 return -EINVAL;
538 }
539
540 return n;
541}
542static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
543
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700544static ssize_t state_show(struct device *dev, struct device_attribute *attr,
545 char *buf)
Felipe Balbi49401f42011-12-19 12:57:04 +0200546{
547 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
548 struct usb_gadget *gadget = udc->gadget;
549
550 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
551}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700552static DEVICE_ATTR_RO(state);
Felipe Balbi49401f42011-12-19 12:57:04 +0200553
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100554#define USB_UDC_SPEED_ATTR(name, param) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700555ssize_t name##_show(struct device *dev, \
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100556 struct device_attribute *attr, char *buf) \
557{ \
558 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
559 return snprintf(buf, PAGE_SIZE, "%s\n", \
560 usb_speed_string(udc->gadget->param)); \
561} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700562static DEVICE_ATTR_RO(name)
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100563
564static USB_UDC_SPEED_ATTR(current_speed, speed);
565static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
566
Felipe Balbi2ccea032011-06-28 16:33:46 +0300567#define USB_UDC_ATTR(name) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700568ssize_t name##_show(struct device *dev, \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300569 struct device_attribute *attr, char *buf) \
570{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200571 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300572 struct usb_gadget *gadget = udc->gadget; \
573 \
574 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
575} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700576static DEVICE_ATTR_RO(name)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300577
Felipe Balbi2ccea032011-06-28 16:33:46 +0300578static USB_UDC_ATTR(is_otg);
579static USB_UDC_ATTR(is_a_peripheral);
580static USB_UDC_ATTR(b_hnp_enable);
581static USB_UDC_ATTR(a_hnp_support);
582static USB_UDC_ATTR(a_alt_hnp_support);
Peter Chen3f6dd4f2015-01-28 16:32:42 +0800583static USB_UDC_ATTR(is_selfpowered);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300584
585static struct attribute *usb_udc_attrs[] = {
586 &dev_attr_srp.attr,
587 &dev_attr_soft_connect.attr,
Felipe Balbi49401f42011-12-19 12:57:04 +0200588 &dev_attr_state.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100589 &dev_attr_current_speed.attr,
590 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300591
Felipe Balbi2ccea032011-06-28 16:33:46 +0300592 &dev_attr_is_otg.attr,
593 &dev_attr_is_a_peripheral.attr,
594 &dev_attr_b_hnp_enable.attr,
595 &dev_attr_a_hnp_support.attr,
596 &dev_attr_a_alt_hnp_support.attr,
Peter Chen3f6dd4f2015-01-28 16:32:42 +0800597 &dev_attr_is_selfpowered.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300598 NULL,
599};
600
601static const struct attribute_group usb_udc_attr_group = {
602 .attrs = usb_udc_attrs,
603};
604
605static const struct attribute_group *usb_udc_attr_groups[] = {
606 &usb_udc_attr_group,
607 NULL,
608};
609
610static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
611{
612 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
613 int ret;
614
615 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
616 if (ret) {
617 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
618 return ret;
619 }
620
621 if (udc->driver) {
622 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
623 udc->driver->function);
624 if (ret) {
625 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
626 return ret;
627 }
628 }
629
630 return 0;
631}
632
633static int __init usb_udc_init(void)
634{
635 udc_class = class_create(THIS_MODULE, "udc");
636 if (IS_ERR(udc_class)) {
637 pr_err("failed to create udc class --> %ld\n",
638 PTR_ERR(udc_class));
639 return PTR_ERR(udc_class);
640 }
641
642 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300643 return 0;
644}
645subsys_initcall(usb_udc_init);
646
647static void __exit usb_udc_exit(void)
648{
649 class_destroy(udc_class);
650}
651module_exit(usb_udc_exit);
652
653MODULE_DESCRIPTION("UDC Framework");
654MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
655MODULE_LICENSE("GPL v2");