blob: f2054659f25b8e6701bd634f902de1e6a76c6fe1 [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
177 * @gadget: The gadget we want to get started
178 * @driver: The driver we want to bind to @gadget
179 *
180 * This call is issued by the UDC Class driver when it's about
181 * to register a gadget driver to the device controller, before
182 * calling gadget driver's bind() method.
183 *
184 * It allows the controller to be powered off until strictly
185 * necessary to have it powered on.
186 *
187 * Returns zero on success, else negative errno.
188 */
189static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
190 struct usb_gadget_driver *driver)
191{
192 return gadget->ops->udc_start(gadget, driver);
193}
194
195/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200196 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
197 * @gadget: The device we want to stop activity
198 * @driver: The driver to unbind from @gadget
199 *
200 * This call is issued by the UDC Class driver after calling
201 * gadget driver's unbind() method.
202 *
203 * The details are implementation specific, but it can go as
204 * far as powering off UDC completely and disable its data
205 * line pullups.
206 */
207static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
208 struct usb_gadget_driver *driver)
209{
210 gadget->ops->udc_stop(gadget, driver);
211}
212
213/**
Felipe Balbi2ccea032011-06-28 16:33:46 +0300214 * usb_udc_release - release the usb_udc struct
215 * @dev: the dev member within usb_udc
216 *
217 * This is called by driver's core in order to free memory once the last
218 * reference is released.
219 */
220static void usb_udc_release(struct device *dev)
221{
222 struct usb_udc *udc;
223
224 udc = container_of(dev, struct usb_udc, dev);
225 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
226 kfree(udc);
227}
228
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200229static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200230
231static void usb_udc_nop_release(struct device *dev)
232{
233 dev_vdbg(dev, "%s\n", __func__);
234}
235
Felipe Balbi2ccea032011-06-28 16:33:46 +0300236/**
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200237 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
238 * @parent: the parent device to this udc. Usually the controller driver's
239 * device.
240 * @gadget: the gadget to be added to the list.
241 * @release: a gadget release function.
Felipe Balbi2ccea032011-06-28 16:33:46 +0300242 *
243 * Returns zero on success, negative errno otherwise.
244 */
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200245int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
246 void (*release)(struct device *dev))
Felipe Balbi2ccea032011-06-28 16:33:46 +0300247{
248 struct usb_udc *udc;
249 int ret = -ENOMEM;
250
251 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
252 if (!udc)
253 goto err1;
254
Felipe Balbi7bce4012013-01-24 17:41:00 +0200255 dev_set_name(&gadget->dev, "gadget");
Felipe Balbi5702f752013-07-17 11:09:49 +0300256 INIT_WORK(&gadget->work, usb_gadget_state_work);
Felipe Balbi2ed14322013-02-26 10:59:41 +0200257 gadget->dev.parent = parent;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200258
Alan Stern908b9612013-07-12 11:06:21 -0400259#ifdef CONFIG_HAS_DMA
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200260 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
261 gadget->dev.dma_parms = parent->dma_parms;
262 gadget->dev.dma_mask = parent->dma_mask;
Alan Stern908b9612013-07-12 11:06:21 -0400263#endif
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200264
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200265 if (release)
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200266 gadget->dev.release = release;
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200267 else
268 gadget->dev.release = usb_udc_nop_release;
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200269
Felipe Balbi7bce4012013-01-24 17:41:00 +0200270 ret = device_register(&gadget->dev);
271 if (ret)
272 goto err2;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200273
Felipe Balbi2ccea032011-06-28 16:33:46 +0300274 device_initialize(&udc->dev);
275 udc->dev.release = usb_udc_release;
276 udc->dev.class = udc_class;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200277 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300278 udc->dev.parent = parent;
279 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
280 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200281 goto err3;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300282
283 udc->gadget = gadget;
284
285 mutex_lock(&udc_lock);
286 list_add_tail(&udc->list, &udc_list);
287
288 ret = device_add(&udc->dev);
289 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200290 goto err4;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300291
Felipe Balbi49401f42011-12-19 12:57:04 +0200292 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
293
Felipe Balbi2ccea032011-06-28 16:33:46 +0300294 mutex_unlock(&udc_lock);
295
296 return 0;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200297
298err4:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300299 list_del(&udc->list);
300 mutex_unlock(&udc_lock);
301
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200302err3:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300303 put_device(&udc->dev);
304
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200305err2:
Felipe Balbi7bce4012013-01-24 17:41:00 +0200306 put_device(&gadget->dev);
Felipe Balbic5dbc222013-04-02 17:06:28 +0300307 kfree(udc);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200308
Felipe Balbi2ccea032011-06-28 16:33:46 +0300309err1:
310 return ret;
311}
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200312EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
313
Felipe Balbi2ccea032011-06-28 16:33:46 +0300314/**
315 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
316 * @parent: the parent device to this udc. Usually the controller
317 * driver's device.
318 * @gadget: the gadget to be added to the list
319 *
320 * Returns zero on success, negative errno otherwise.
321 */
322int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
323{
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200324 return usb_add_gadget_udc_release(parent, gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300325}
326EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
327
328static void usb_gadget_remove_driver(struct usb_udc *udc)
329{
330 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
331 udc->gadget->name);
332
333 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
334
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200335 usb_gadget_disconnect(udc->gadget);
336 udc->driver->disconnect(udc->gadget);
337 udc->driver->unbind(udc->gadget);
Alan Stern511f3c52013-03-15 14:02:14 -0400338 usb_gadget_udc_stop(udc->gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300339
340 udc->driver = NULL;
341 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200342 udc->gadget->dev.driver = NULL;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300343}
344
345/**
346 * usb_del_gadget_udc - deletes @udc from udc_list
347 * @gadget: the gadget to be removed.
348 *
349 * This, will call usb_gadget_unregister_driver() if
350 * the @udc is still busy.
351 */
352void usb_del_gadget_udc(struct usb_gadget *gadget)
353{
354 struct usb_udc *udc = NULL;
355
356 mutex_lock(&udc_lock);
357 list_for_each_entry(udc, &udc_list, list)
358 if (udc->gadget == gadget)
359 goto found;
360
361 dev_err(gadget->dev.parent, "gadget not registered.\n");
362 mutex_unlock(&udc_lock);
363
364 return;
365
366found:
367 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
368
369 list_del(&udc->list);
370 mutex_unlock(&udc_lock);
371
372 if (udc->driver)
373 usb_gadget_remove_driver(udc);
374
375 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
Felipe Balbi5702f752013-07-17 11:09:49 +0300376 flush_work(&gadget->work);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300377 device_unregister(&udc->dev);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200378 device_unregister(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300379}
380EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
381
382/* ------------------------------------------------------------------------- */
383
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100384static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300385{
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100386 int ret;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300387
Felipe Balbi2ccea032011-06-28 16:33:46 +0300388 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
389 driver->function);
390
391 udc->driver = driver;
392 udc->dev.driver = &driver->driver;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200393 udc->gadget->dev.driver = &driver->driver;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300394
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200395 ret = driver->bind(udc->gadget, driver);
396 if (ret)
397 goto err1;
398 ret = usb_gadget_udc_start(udc->gadget, driver);
399 if (ret) {
400 driver->unbind(udc->gadget);
401 goto err1;
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200402 }
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200403 usb_gadget_connect(udc->gadget);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300404
405 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300406 return 0;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300407err1:
Fabio Estevamf8cffc82013-09-28 00:05:34 -0300408 if (ret != -EISNAM)
409 dev_err(&udc->dev, "failed to start %s: %d\n",
Felipe Balbi2ccea032011-06-28 16:33:46 +0300410 udc->driver->function, ret);
411 udc->driver = NULL;
412 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200413 udc->gadget->dev.driver = NULL;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100414 return ret;
415}
416
417int udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
418{
419 struct usb_udc *udc = NULL;
420 int ret = -ENODEV;
421
422 mutex_lock(&udc_lock);
423 list_for_each_entry(udc, &udc_list, list) {
424 ret = strcmp(name, dev_name(&udc->dev));
425 if (!ret)
426 break;
427 }
428 if (ret) {
429 ret = -ENODEV;
430 goto out;
431 }
432 if (udc->driver) {
433 ret = -EBUSY;
434 goto out;
435 }
436 ret = udc_bind_to_driver(udc, driver);
437out:
438 mutex_unlock(&udc_lock);
439 return ret;
440}
441EXPORT_SYMBOL_GPL(udc_attach_driver);
442
443int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
444{
445 struct usb_udc *udc = NULL;
446 int ret;
447
448 if (!driver || !driver->bind || !driver->setup)
449 return -EINVAL;
450
451 mutex_lock(&udc_lock);
452 list_for_each_entry(udc, &udc_list, list) {
453 /* For now we take the first one */
454 if (!udc->driver)
455 goto found;
456 }
457
458 pr_debug("couldn't find an available UDC\n");
459 mutex_unlock(&udc_lock);
460 return -ENODEV;
461found:
462 ret = udc_bind_to_driver(udc, driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300463 mutex_unlock(&udc_lock);
464 return ret;
465}
466EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
467
468int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
469{
470 struct usb_udc *udc = NULL;
471 int ret = -ENODEV;
472
473 if (!driver || !driver->unbind)
474 return -EINVAL;
475
476 mutex_lock(&udc_lock);
477 list_for_each_entry(udc, &udc_list, list)
478 if (udc->driver == driver) {
479 usb_gadget_remove_driver(udc);
Peter Chenb5fb8d02014-04-29 13:26:29 +0800480 usb_gadget_set_state(udc->gadget,
481 USB_STATE_NOTATTACHED);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300482 ret = 0;
483 break;
484 }
485
486 mutex_unlock(&udc_lock);
487 return ret;
488}
489EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
490
491/* ------------------------------------------------------------------------- */
492
493static ssize_t usb_udc_srp_store(struct device *dev,
494 struct device_attribute *attr, const char *buf, size_t n)
495{
Felipe Balbi1d91a962011-09-08 14:11:17 +0300496 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300497
498 if (sysfs_streq(buf, "1"))
499 usb_gadget_wakeup(udc->gadget);
500
501 return n;
502}
503static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
504
505static ssize_t usb_udc_softconn_store(struct device *dev,
506 struct device_attribute *attr, const char *buf, size_t n)
507{
Felipe Balbi865569b2011-09-08 14:12:18 +0300508 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300509
Felipe Balbibfa6b182014-10-17 11:10:25 -0500510 if (!udc->driver) {
511 dev_err(dev, "soft-connect without a gadget driver\n");
512 return -EOPNOTSUPP;
513 }
514
Felipe Balbi2ccea032011-06-28 16:33:46 +0300515 if (sysfs_streq(buf, "connect")) {
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200516 usb_gadget_udc_start(udc->gadget, udc->driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300517 usb_gadget_connect(udc->gadget);
518 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +0300519 usb_gadget_disconnect(udc->gadget);
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200520 usb_gadget_udc_stop(udc->gadget, udc->driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300521 } else {
522 dev_err(dev, "unsupported command '%s'\n", buf);
523 return -EINVAL;
524 }
525
526 return n;
527}
528static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
529
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700530static ssize_t state_show(struct device *dev, struct device_attribute *attr,
531 char *buf)
Felipe Balbi49401f42011-12-19 12:57:04 +0200532{
533 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
534 struct usb_gadget *gadget = udc->gadget;
535
536 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
537}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700538static DEVICE_ATTR_RO(state);
Felipe Balbi49401f42011-12-19 12:57:04 +0200539
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100540#define USB_UDC_SPEED_ATTR(name, param) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700541ssize_t name##_show(struct device *dev, \
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100542 struct device_attribute *attr, char *buf) \
543{ \
544 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
545 return snprintf(buf, PAGE_SIZE, "%s\n", \
546 usb_speed_string(udc->gadget->param)); \
547} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700548static DEVICE_ATTR_RO(name)
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100549
550static USB_UDC_SPEED_ATTR(current_speed, speed);
551static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
552
Felipe Balbi2ccea032011-06-28 16:33:46 +0300553#define USB_UDC_ATTR(name) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700554ssize_t name##_show(struct device *dev, \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300555 struct device_attribute *attr, char *buf) \
556{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200557 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300558 struct usb_gadget *gadget = udc->gadget; \
559 \
560 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
561} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700562static DEVICE_ATTR_RO(name)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300563
Felipe Balbi2ccea032011-06-28 16:33:46 +0300564static USB_UDC_ATTR(is_otg);
565static USB_UDC_ATTR(is_a_peripheral);
566static USB_UDC_ATTR(b_hnp_enable);
567static USB_UDC_ATTR(a_hnp_support);
568static USB_UDC_ATTR(a_alt_hnp_support);
569
570static struct attribute *usb_udc_attrs[] = {
571 &dev_attr_srp.attr,
572 &dev_attr_soft_connect.attr,
Felipe Balbi49401f42011-12-19 12:57:04 +0200573 &dev_attr_state.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100574 &dev_attr_current_speed.attr,
575 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300576
Felipe Balbi2ccea032011-06-28 16:33:46 +0300577 &dev_attr_is_otg.attr,
578 &dev_attr_is_a_peripheral.attr,
579 &dev_attr_b_hnp_enable.attr,
580 &dev_attr_a_hnp_support.attr,
581 &dev_attr_a_alt_hnp_support.attr,
582 NULL,
583};
584
585static const struct attribute_group usb_udc_attr_group = {
586 .attrs = usb_udc_attrs,
587};
588
589static const struct attribute_group *usb_udc_attr_groups[] = {
590 &usb_udc_attr_group,
591 NULL,
592};
593
594static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
595{
596 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
597 int ret;
598
599 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
600 if (ret) {
601 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
602 return ret;
603 }
604
605 if (udc->driver) {
606 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
607 udc->driver->function);
608 if (ret) {
609 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
610 return ret;
611 }
612 }
613
614 return 0;
615}
616
617static int __init usb_udc_init(void)
618{
619 udc_class = class_create(THIS_MODULE, "udc");
620 if (IS_ERR(udc_class)) {
621 pr_err("failed to create udc class --> %ld\n",
622 PTR_ERR(udc_class));
623 return PTR_ERR(udc_class);
624 }
625
626 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300627 return 0;
628}
629subsys_initcall(usb_udc_init);
630
631static void __exit usb_udc_exit(void)
632{
633 class_destroy(udc_class);
634}
635module_exit(usb_udc_exit);
636
637MODULE_DESCRIPTION("UDC Framework");
638MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
639MODULE_LICENSE("GPL v2");