blob: 3fed7a2742a0fbe87377939ad633ac416d368b0e [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
38 *
39 * This represents the internal data structure which is used by the UDC-class
40 * to hold information about udc driver and gadget together.
41 */
42struct usb_udc {
43 struct usb_gadget_driver *driver;
44 struct usb_gadget *gadget;
45 struct device dev;
46 struct list_head list;
47};
48
49static struct class *udc_class;
Felipe Balbi2ccea032011-06-28 16:33:46 +030050static LIST_HEAD(udc_list);
51static DEFINE_MUTEX(udc_lock);
52
53/* ------------------------------------------------------------------------- */
54
Alan Stern908b9612013-07-12 11:06:21 -040055#ifdef CONFIG_HAS_DMA
56
Felipe Balbia6989082011-12-15 13:31:48 +020057int usb_gadget_map_request(struct usb_gadget *gadget,
58 struct usb_request *req, int is_in)
59{
60 if (req->length == 0)
61 return 0;
62
63 if (req->num_sgs) {
64 int mapped;
65
66 mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs,
67 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
68 if (mapped == 0) {
69 dev_err(&gadget->dev, "failed to map SGs\n");
70 return -EFAULT;
71 }
72
73 req->num_mapped_sgs = mapped;
74 } else {
75 req->dma = dma_map_single(&gadget->dev, req->buf, req->length,
76 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
77
78 if (dma_mapping_error(&gadget->dev, req->dma)) {
79 dev_err(&gadget->dev, "failed to map buffer\n");
80 return -EFAULT;
81 }
82 }
83
84 return 0;
85}
86EXPORT_SYMBOL_GPL(usb_gadget_map_request);
87
88void usb_gadget_unmap_request(struct usb_gadget *gadget,
89 struct usb_request *req, int is_in)
90{
91 if (req->length == 0)
92 return;
93
94 if (req->num_mapped_sgs) {
95 dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs,
96 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
97
98 req->num_mapped_sgs = 0;
99 } else {
100 dma_unmap_single(&gadget->dev, req->dma, req->length,
101 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
102 }
103}
104EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
105
Alan Stern908b9612013-07-12 11:06:21 -0400106#endif /* CONFIG_HAS_DMA */
107
Felipe Balbia6989082011-12-15 13:31:48 +0200108/* ------------------------------------------------------------------------- */
109
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200110/**
111 * usb_gadget_giveback_request - give the request back to the gadget layer
112 * Context: in_interrupt()
113 *
114 * This is called by device controller drivers in order to return the
115 * completed request back to the gadget layer.
116 */
117void usb_gadget_giveback_request(struct usb_ep *ep,
118 struct usb_request *req)
119{
Michal Sojka0cfbd322014-09-24 22:43:21 +0200120 if (likely(req->status == 0))
121 usb_led_activity(USB_LED_EVENT_GADGET);
122
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200123 req->complete(ep, req);
124}
125EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
126
127/* ------------------------------------------------------------------------- */
128
Felipe Balbi5702f752013-07-17 11:09:49 +0300129static void usb_gadget_state_work(struct work_struct *work)
130{
131 struct usb_gadget *gadget = work_to_gadget(work);
Andreas Larssonaf549542014-09-15 12:42:27 +0200132 struct usb_udc *udc = NULL;
Felipe Balbi5702f752013-07-17 11:09:49 +0300133
Andreas Larssonaf549542014-09-15 12:42:27 +0200134 mutex_lock(&udc_lock);
135 list_for_each_entry(udc, &udc_list, list)
136 if (udc->gadget == gadget)
137 goto found;
138 mutex_unlock(&udc_lock);
139
140 return;
141
142found:
143 mutex_unlock(&udc_lock);
144
145 sysfs_notify(&udc->dev.kobj, NULL, "state");
Felipe Balbi5702f752013-07-17 11:09:49 +0300146}
147
Felipe Balbi49401f42011-12-19 12:57:04 +0200148void usb_gadget_set_state(struct usb_gadget *gadget,
149 enum usb_device_state state)
150{
151 gadget->state = state;
Felipe Balbi5702f752013-07-17 11:09:49 +0300152 schedule_work(&gadget->work);
Felipe Balbi49401f42011-12-19 12:57:04 +0200153}
154EXPORT_SYMBOL_GPL(usb_gadget_set_state);
155
156/* ------------------------------------------------------------------------- */
157
Felipe Balbi2ccea032011-06-28 16:33:46 +0300158/**
Peter Chen974a70b2014-09-12 09:32:41 +0800159 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
160 * @gadget: The gadget which bus reset occurs
161 * @driver: The gadget driver we want to notify
162 *
163 * If the udc driver has bus reset handler, it needs to call this when the bus
164 * reset occurs, it notifies the gadget driver that the bus reset occurs as
165 * well as updates gadget state.
166 */
167void usb_gadget_udc_reset(struct usb_gadget *gadget,
168 struct usb_gadget_driver *driver)
169{
170 driver->reset(gadget);
171 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
172}
173EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
174
175/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200176 * usb_gadget_udc_start - tells usb device controller to start up
Felipe Balbi2c683342014-10-17 11:34:07 -0500177 * @udc: The UDC to be started
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200178 *
179 * This call is issued by the UDC Class driver when it's about
180 * to register a gadget driver to the device controller, before
181 * calling gadget driver's bind() method.
182 *
183 * It allows the controller to be powered off until strictly
184 * necessary to have it powered on.
185 *
186 * Returns zero on success, else negative errno.
187 */
Felipe Balbi2c683342014-10-17 11:34:07 -0500188static inline int usb_gadget_udc_start(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200189{
Felipe Balbi2c683342014-10-17 11:34:07 -0500190 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200191}
192
193/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200194 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
195 * @gadget: The device we want to stop activity
196 * @driver: The driver to unbind from @gadget
197 *
198 * This call is issued by the UDC Class driver after calling
199 * gadget driver's unbind() method.
200 *
201 * The details are implementation specific, but it can go as
202 * far as powering off UDC completely and disable its data
203 * line pullups.
204 */
Felipe Balbi2c683342014-10-17 11:34:07 -0500205static inline void usb_gadget_udc_stop(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200206{
Felipe Balbi2c683342014-10-17 11:34:07 -0500207 udc->gadget->ops->udc_stop(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200208}
209
210/**
Felipe Balbi2ccea032011-06-28 16:33:46 +0300211 * usb_udc_release - release the usb_udc struct
212 * @dev: the dev member within usb_udc
213 *
214 * This is called by driver's core in order to free memory once the last
215 * reference is released.
216 */
217static void usb_udc_release(struct device *dev)
218{
219 struct usb_udc *udc;
220
221 udc = container_of(dev, struct usb_udc, dev);
222 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
223 kfree(udc);
224}
225
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200226static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200227
228static void usb_udc_nop_release(struct device *dev)
229{
230 dev_vdbg(dev, "%s\n", __func__);
231}
232
Felipe Balbi2ccea032011-06-28 16:33:46 +0300233/**
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200234 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
235 * @parent: the parent device to this udc. Usually the controller driver's
236 * device.
237 * @gadget: the gadget to be added to the list.
238 * @release: a gadget release function.
Felipe Balbi2ccea032011-06-28 16:33:46 +0300239 *
240 * Returns zero on success, negative errno otherwise.
241 */
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200242int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
243 void (*release)(struct device *dev))
Felipe Balbi2ccea032011-06-28 16:33:46 +0300244{
245 struct usb_udc *udc;
246 int ret = -ENOMEM;
247
248 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
249 if (!udc)
250 goto err1;
251
Felipe Balbi7bce4012013-01-24 17:41:00 +0200252 dev_set_name(&gadget->dev, "gadget");
Felipe Balbi5702f752013-07-17 11:09:49 +0300253 INIT_WORK(&gadget->work, usb_gadget_state_work);
Felipe Balbi2ed14322013-02-26 10:59:41 +0200254 gadget->dev.parent = parent;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200255
Alan Stern908b9612013-07-12 11:06:21 -0400256#ifdef CONFIG_HAS_DMA
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200257 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
258 gadget->dev.dma_parms = parent->dma_parms;
259 gadget->dev.dma_mask = parent->dma_mask;
Alan Stern908b9612013-07-12 11:06:21 -0400260#endif
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200261
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200262 if (release)
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200263 gadget->dev.release = release;
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200264 else
265 gadget->dev.release = usb_udc_nop_release;
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200266
Felipe Balbi7bce4012013-01-24 17:41:00 +0200267 ret = device_register(&gadget->dev);
268 if (ret)
269 goto err2;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200270
Felipe Balbi2ccea032011-06-28 16:33:46 +0300271 device_initialize(&udc->dev);
272 udc->dev.release = usb_udc_release;
273 udc->dev.class = udc_class;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200274 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300275 udc->dev.parent = parent;
276 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
277 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200278 goto err3;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300279
280 udc->gadget = gadget;
281
282 mutex_lock(&udc_lock);
283 list_add_tail(&udc->list, &udc_list);
284
285 ret = device_add(&udc->dev);
286 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200287 goto err4;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300288
Felipe Balbi49401f42011-12-19 12:57:04 +0200289 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
290
Felipe Balbi2ccea032011-06-28 16:33:46 +0300291 mutex_unlock(&udc_lock);
292
293 return 0;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200294
295err4:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300296 list_del(&udc->list);
297 mutex_unlock(&udc_lock);
298
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200299err3:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300300 put_device(&udc->dev);
301
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200302err2:
Felipe Balbi7bce4012013-01-24 17:41:00 +0200303 put_device(&gadget->dev);
Felipe Balbic5dbc222013-04-02 17:06:28 +0300304 kfree(udc);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200305
Felipe Balbi2ccea032011-06-28 16:33:46 +0300306err1:
307 return ret;
308}
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200309EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
310
Felipe Balbi2ccea032011-06-28 16:33:46 +0300311/**
312 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
313 * @parent: the parent device to this udc. Usually the controller
314 * driver's device.
315 * @gadget: the gadget to be added to the list
316 *
317 * Returns zero on success, negative errno otherwise.
318 */
319int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
320{
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200321 return usb_add_gadget_udc_release(parent, gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300322}
323EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
324
325static void usb_gadget_remove_driver(struct usb_udc *udc)
326{
327 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
328 udc->gadget->name);
329
330 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
331
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200332 usb_gadget_disconnect(udc->gadget);
333 udc->driver->disconnect(udc->gadget);
334 udc->driver->unbind(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -0500335 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300336
337 udc->driver = NULL;
338 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200339 udc->gadget->dev.driver = NULL;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300340}
341
342/**
343 * usb_del_gadget_udc - deletes @udc from udc_list
344 * @gadget: the gadget to be removed.
345 *
346 * This, will call usb_gadget_unregister_driver() if
347 * the @udc is still busy.
348 */
349void usb_del_gadget_udc(struct usb_gadget *gadget)
350{
351 struct usb_udc *udc = NULL;
352
353 mutex_lock(&udc_lock);
354 list_for_each_entry(udc, &udc_list, list)
355 if (udc->gadget == gadget)
356 goto found;
357
358 dev_err(gadget->dev.parent, "gadget not registered.\n");
359 mutex_unlock(&udc_lock);
360
361 return;
362
363found:
364 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
365
366 list_del(&udc->list);
367 mutex_unlock(&udc_lock);
368
369 if (udc->driver)
370 usb_gadget_remove_driver(udc);
371
372 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
Felipe Balbi5702f752013-07-17 11:09:49 +0300373 flush_work(&gadget->work);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300374 device_unregister(&udc->dev);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200375 device_unregister(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300376}
377EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
378
379/* ------------------------------------------------------------------------- */
380
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100381static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300382{
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100383 int ret;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300384
Felipe Balbi2ccea032011-06-28 16:33:46 +0300385 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
386 driver->function);
387
388 udc->driver = driver;
389 udc->dev.driver = &driver->driver;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200390 udc->gadget->dev.driver = &driver->driver;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300391
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200392 ret = driver->bind(udc->gadget, driver);
393 if (ret)
394 goto err1;
Felipe Balbi2c683342014-10-17 11:34:07 -0500395 ret = usb_gadget_udc_start(udc);
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200396 if (ret) {
397 driver->unbind(udc->gadget);
398 goto err1;
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200399 }
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200400 usb_gadget_connect(udc->gadget);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300401
402 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300403 return 0;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300404err1:
Fabio Estevamf8cffc82013-09-28 00:05:34 -0300405 if (ret != -EISNAM)
406 dev_err(&udc->dev, "failed to start %s: %d\n",
Felipe Balbi2ccea032011-06-28 16:33:46 +0300407 udc->driver->function, ret);
408 udc->driver = NULL;
409 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200410 udc->gadget->dev.driver = NULL;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100411 return ret;
412}
413
414int udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
415{
416 struct usb_udc *udc = NULL;
417 int ret = -ENODEV;
418
419 mutex_lock(&udc_lock);
420 list_for_each_entry(udc, &udc_list, list) {
421 ret = strcmp(name, dev_name(&udc->dev));
422 if (!ret)
423 break;
424 }
425 if (ret) {
426 ret = -ENODEV;
427 goto out;
428 }
429 if (udc->driver) {
430 ret = -EBUSY;
431 goto out;
432 }
433 ret = udc_bind_to_driver(udc, driver);
434out:
435 mutex_unlock(&udc_lock);
436 return ret;
437}
438EXPORT_SYMBOL_GPL(udc_attach_driver);
439
440int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
441{
442 struct usb_udc *udc = NULL;
443 int ret;
444
445 if (!driver || !driver->bind || !driver->setup)
446 return -EINVAL;
447
448 mutex_lock(&udc_lock);
449 list_for_each_entry(udc, &udc_list, list) {
450 /* For now we take the first one */
451 if (!udc->driver)
452 goto found;
453 }
454
455 pr_debug("couldn't find an available UDC\n");
456 mutex_unlock(&udc_lock);
457 return -ENODEV;
458found:
459 ret = udc_bind_to_driver(udc, driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300460 mutex_unlock(&udc_lock);
461 return ret;
462}
463EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
464
465int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
466{
467 struct usb_udc *udc = NULL;
468 int ret = -ENODEV;
469
470 if (!driver || !driver->unbind)
471 return -EINVAL;
472
473 mutex_lock(&udc_lock);
474 list_for_each_entry(udc, &udc_list, list)
475 if (udc->driver == driver) {
476 usb_gadget_remove_driver(udc);
Peter Chenb5fb8d02014-04-29 13:26:29 +0800477 usb_gadget_set_state(udc->gadget,
478 USB_STATE_NOTATTACHED);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300479 ret = 0;
480 break;
481 }
482
483 mutex_unlock(&udc_lock);
484 return ret;
485}
486EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
487
488/* ------------------------------------------------------------------------- */
489
490static ssize_t usb_udc_srp_store(struct device *dev,
491 struct device_attribute *attr, const char *buf, size_t n)
492{
Felipe Balbi1d91a962011-09-08 14:11:17 +0300493 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300494
495 if (sysfs_streq(buf, "1"))
496 usb_gadget_wakeup(udc->gadget);
497
498 return n;
499}
500static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
501
502static ssize_t usb_udc_softconn_store(struct device *dev,
503 struct device_attribute *attr, const char *buf, size_t n)
504{
Felipe Balbi865569b2011-09-08 14:12:18 +0300505 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300506
Felipe Balbibfa6b182014-10-17 11:10:25 -0500507 if (!udc->driver) {
508 dev_err(dev, "soft-connect without a gadget driver\n");
509 return -EOPNOTSUPP;
510 }
511
Felipe Balbi2ccea032011-06-28 16:33:46 +0300512 if (sysfs_streq(buf, "connect")) {
Felipe Balbi2c683342014-10-17 11:34:07 -0500513 usb_gadget_udc_start(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300514 usb_gadget_connect(udc->gadget);
515 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +0300516 usb_gadget_disconnect(udc->gadget);
Felipe Balbi0abd0692014-10-09 10:12:24 -0500517 udc->driver->disconnect(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -0500518 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300519 } else {
520 dev_err(dev, "unsupported command '%s'\n", buf);
521 return -EINVAL;
522 }
523
524 return n;
525}
526static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
527
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700528static ssize_t state_show(struct device *dev, struct device_attribute *attr,
529 char *buf)
Felipe Balbi49401f42011-12-19 12:57:04 +0200530{
531 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
532 struct usb_gadget *gadget = udc->gadget;
533
534 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
535}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700536static DEVICE_ATTR_RO(state);
Felipe Balbi49401f42011-12-19 12:57:04 +0200537
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100538#define USB_UDC_SPEED_ATTR(name, param) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700539ssize_t name##_show(struct device *dev, \
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100540 struct device_attribute *attr, char *buf) \
541{ \
542 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
543 return snprintf(buf, PAGE_SIZE, "%s\n", \
544 usb_speed_string(udc->gadget->param)); \
545} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700546static DEVICE_ATTR_RO(name)
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100547
548static USB_UDC_SPEED_ATTR(current_speed, speed);
549static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
550
Felipe Balbi2ccea032011-06-28 16:33:46 +0300551#define USB_UDC_ATTR(name) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700552ssize_t name##_show(struct device *dev, \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300553 struct device_attribute *attr, char *buf) \
554{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200555 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300556 struct usb_gadget *gadget = udc->gadget; \
557 \
558 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
559} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700560static DEVICE_ATTR_RO(name)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300561
Felipe Balbi2ccea032011-06-28 16:33:46 +0300562static USB_UDC_ATTR(is_otg);
563static USB_UDC_ATTR(is_a_peripheral);
564static USB_UDC_ATTR(b_hnp_enable);
565static USB_UDC_ATTR(a_hnp_support);
566static USB_UDC_ATTR(a_alt_hnp_support);
567
568static struct attribute *usb_udc_attrs[] = {
569 &dev_attr_srp.attr,
570 &dev_attr_soft_connect.attr,
Felipe Balbi49401f42011-12-19 12:57:04 +0200571 &dev_attr_state.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100572 &dev_attr_current_speed.attr,
573 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300574
Felipe Balbi2ccea032011-06-28 16:33:46 +0300575 &dev_attr_is_otg.attr,
576 &dev_attr_is_a_peripheral.attr,
577 &dev_attr_b_hnp_enable.attr,
578 &dev_attr_a_hnp_support.attr,
579 &dev_attr_a_alt_hnp_support.attr,
580 NULL,
581};
582
583static const struct attribute_group usb_udc_attr_group = {
584 .attrs = usb_udc_attrs,
585};
586
587static const struct attribute_group *usb_udc_attr_groups[] = {
588 &usb_udc_attr_group,
589 NULL,
590};
591
592static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
593{
594 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
595 int ret;
596
597 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
598 if (ret) {
599 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
600 return ret;
601 }
602
603 if (udc->driver) {
604 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
605 udc->driver->function);
606 if (ret) {
607 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
608 return ret;
609 }
610 }
611
612 return 0;
613}
614
615static int __init usb_udc_init(void)
616{
617 udc_class = class_create(THIS_MODULE, "udc");
618 if (IS_ERR(udc_class)) {
619 pr_err("failed to create udc class --> %ld\n",
620 PTR_ERR(udc_class));
621 return PTR_ERR(udc_class);
622 }
623
624 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300625 return 0;
626}
627subsys_initcall(usb_udc_init);
628
629static void __exit usb_udc_exit(void)
630{
631 class_destroy(udc_class);
632}
633module_exit(usb_udc_exit);
634
635MODULE_DESCRIPTION("UDC Framework");
636MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
637MODULE_LICENSE("GPL v2");