blob: 429d64e6794141cb212a35c4317d2baeb6d58596 [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{
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +090063 struct device *dev = gadget->dev.parent;
64
Felipe Balbia6989082011-12-15 13:31:48 +020065 if (req->length == 0)
66 return 0;
67
68 if (req->num_sgs) {
69 int mapped;
70
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +090071 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
Felipe Balbia6989082011-12-15 13:31:48 +020072 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 if (mapped == 0) {
74 dev_err(&gadget->dev, "failed to map SGs\n");
75 return -EFAULT;
76 }
77
78 req->num_mapped_sgs = mapped;
79 } else {
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +090080 req->dma = dma_map_single(dev, req->buf, req->length,
Felipe Balbia6989082011-12-15 13:31:48 +020081 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
82
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +090083 if (dma_mapping_error(dev, req->dma)) {
84 dev_err(dev, "failed to map buffer\n");
Felipe Balbia6989082011-12-15 13:31:48 +020085 return -EFAULT;
86 }
87 }
88
89 return 0;
90}
91EXPORT_SYMBOL_GPL(usb_gadget_map_request);
92
93void usb_gadget_unmap_request(struct usb_gadget *gadget,
94 struct usb_request *req, int is_in)
95{
96 if (req->length == 0)
97 return;
98
99 if (req->num_mapped_sgs) {
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900100 dma_unmap_sg(gadget->dev.parent, req->sg, req->num_mapped_sgs,
Felipe Balbia6989082011-12-15 13:31:48 +0200101 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
102
103 req->num_mapped_sgs = 0;
104 } else {
Yoshihiro Shimoda7ace8fc2015-07-13 18:10:05 +0900105 dma_unmap_single(gadget->dev.parent, req->dma, req->length,
Felipe Balbia6989082011-12-15 13:31:48 +0200106 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
107 }
108}
109EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
110
Alan Stern908b9612013-07-12 11:06:21 -0400111#endif /* CONFIG_HAS_DMA */
112
Felipe Balbia6989082011-12-15 13:31:48 +0200113/* ------------------------------------------------------------------------- */
114
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200115/**
116 * usb_gadget_giveback_request - give the request back to the gadget layer
117 * Context: in_interrupt()
118 *
119 * This is called by device controller drivers in order to return the
120 * completed request back to the gadget layer.
121 */
122void usb_gadget_giveback_request(struct usb_ep *ep,
123 struct usb_request *req)
124{
Michal Sojka0cfbd322014-09-24 22:43:21 +0200125 if (likely(req->status == 0))
126 usb_led_activity(USB_LED_EVENT_GADGET);
127
Michal Sojka3fc2aa52014-09-24 22:43:18 +0200128 req->complete(ep, req);
129}
130EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
131
132/* ------------------------------------------------------------------------- */
133
Robert Baldygab0aea002015-08-06 14:11:12 +0200134/**
135 * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
136 * in second parameter or NULL if searched endpoint not found
137 * @g: controller to check for quirk
138 * @name: name of searched endpoint
139 */
140struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
141{
142 struct usb_ep *ep;
143
144 gadget_for_each_ep(ep, g) {
145 if (!strcmp(ep->name, name))
146 return ep;
147 }
148
149 return NULL;
150}
151EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
152
153/* ------------------------------------------------------------------------- */
154
Robert Baldyga4278c682015-08-06 14:11:11 +0200155int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
156 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
157 struct usb_ss_ep_comp_descriptor *ep_comp)
158{
159 u8 type;
160 u16 max;
161 int num_req_streams = 0;
162
163 /* endpoint already claimed? */
164 if (ep->claimed)
165 return 0;
166
167 type = usb_endpoint_type(desc);
168 max = 0x7ff & usb_endpoint_maxp(desc);
169
170 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
171 return 0;
172 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
173 return 0;
174
175 if (max > ep->maxpacket_limit)
176 return 0;
177
178 /* "high bandwidth" works only at high speed */
179 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp(desc) & (3<<11))
180 return 0;
181
182 switch (type) {
183 case USB_ENDPOINT_XFER_CONTROL:
184 /* only support ep0 for portable CONTROL traffic */
185 return 0;
186 case USB_ENDPOINT_XFER_ISOC:
187 if (!ep->caps.type_iso)
188 return 0;
189 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
190 if (!gadget_is_dualspeed(gadget) && max > 1023)
191 return 0;
192 break;
193 case USB_ENDPOINT_XFER_BULK:
194 if (!ep->caps.type_bulk)
195 return 0;
196 if (ep_comp && gadget_is_superspeed(gadget)) {
197 /* Get the number of required streams from the
198 * EP companion descriptor and see if the EP
199 * matches it
200 */
201 num_req_streams = ep_comp->bmAttributes & 0x1f;
202 if (num_req_streams > ep->max_streams)
203 return 0;
204 }
205 break;
206 case USB_ENDPOINT_XFER_INT:
207 /* Bulk endpoints handle interrupt transfers,
208 * except the toggle-quirky iso-synch kind
209 */
210 if (!ep->caps.type_int && !ep->caps.type_bulk)
211 return 0;
212 /* INT: limit 64 bytes full speed, 1024 high/super speed */
213 if (!gadget_is_dualspeed(gadget) && max > 64)
214 return 0;
215 break;
216 }
217
218 return 1;
219}
220EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
221
222/* ------------------------------------------------------------------------- */
223
Felipe Balbi5702f752013-07-17 11:09:49 +0300224static void usb_gadget_state_work(struct work_struct *work)
225{
Peter Chendfea9c92015-03-06 10:36:02 +0800226 struct usb_gadget *gadget = work_to_gadget(work);
227 struct usb_udc *udc = gadget->udc;
Felipe Balbi5702f752013-07-17 11:09:49 +0300228
Peter Chendfea9c92015-03-06 10:36:02 +0800229 if (udc)
230 sysfs_notify(&udc->dev.kobj, NULL, "state");
Felipe Balbi5702f752013-07-17 11:09:49 +0300231}
232
Felipe Balbi49401f42011-12-19 12:57:04 +0200233void usb_gadget_set_state(struct usb_gadget *gadget,
234 enum usb_device_state state)
235{
236 gadget->state = state;
Felipe Balbi5702f752013-07-17 11:09:49 +0300237 schedule_work(&gadget->work);
Felipe Balbi49401f42011-12-19 12:57:04 +0200238}
239EXPORT_SYMBOL_GPL(usb_gadget_set_state);
240
241/* ------------------------------------------------------------------------- */
242
Peter Chen628ef0d2015-03-06 10:36:03 +0800243static void usb_udc_connect_control(struct usb_udc *udc)
244{
245 if (udc->vbus)
246 usb_gadget_connect(udc->gadget);
247 else
248 usb_gadget_disconnect(udc->gadget);
249}
250
251/**
252 * usb_udc_vbus_handler - updates the udc core vbus status, and try to
253 * connect or disconnect gadget
254 * @gadget: The gadget which vbus change occurs
255 * @status: The vbus status
256 *
257 * The udc driver calls it when it wants to connect or disconnect gadget
258 * according to vbus status.
259 */
260void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
261{
262 struct usb_udc *udc = gadget->udc;
263
264 if (udc) {
265 udc->vbus = status;
266 usb_udc_connect_control(udc);
267 }
268}
269EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
270
Felipe Balbi2ccea032011-06-28 16:33:46 +0300271/**
Peter Chen974a70b2014-09-12 09:32:41 +0800272 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
273 * @gadget: The gadget which bus reset occurs
274 * @driver: The gadget driver we want to notify
275 *
276 * If the udc driver has bus reset handler, it needs to call this when the bus
277 * reset occurs, it notifies the gadget driver that the bus reset occurs as
278 * well as updates gadget state.
279 */
280void usb_gadget_udc_reset(struct usb_gadget *gadget,
281 struct usb_gadget_driver *driver)
282{
283 driver->reset(gadget);
284 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
285}
286EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
287
288/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200289 * usb_gadget_udc_start - tells usb device controller to start up
Felipe Balbi2c683342014-10-17 11:34:07 -0500290 * @udc: The UDC to be started
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200291 *
292 * This call is issued by the UDC Class driver when it's about
293 * to register a gadget driver to the device controller, before
294 * calling gadget driver's bind() method.
295 *
296 * It allows the controller to be powered off until strictly
297 * necessary to have it powered on.
298 *
299 * Returns zero on success, else negative errno.
300 */
Felipe Balbi2c683342014-10-17 11:34:07 -0500301static inline int usb_gadget_udc_start(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200302{
Felipe Balbi2c683342014-10-17 11:34:07 -0500303 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200304}
305
306/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200307 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
308 * @gadget: The device we want to stop activity
309 * @driver: The driver to unbind from @gadget
310 *
311 * This call is issued by the UDC Class driver after calling
312 * gadget driver's unbind() method.
313 *
314 * The details are implementation specific, but it can go as
315 * far as powering off UDC completely and disable its data
316 * line pullups.
317 */
Felipe Balbi2c683342014-10-17 11:34:07 -0500318static inline void usb_gadget_udc_stop(struct usb_udc *udc)
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200319{
Felipe Balbi22835b82014-10-17 12:05:12 -0500320 udc->gadget->ops->udc_stop(udc->gadget);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200321}
322
323/**
Felipe Balbi2ccea032011-06-28 16:33:46 +0300324 * usb_udc_release - release the usb_udc struct
325 * @dev: the dev member within usb_udc
326 *
327 * This is called by driver's core in order to free memory once the last
328 * reference is released.
329 */
330static void usb_udc_release(struct device *dev)
331{
332 struct usb_udc *udc;
333
334 udc = container_of(dev, struct usb_udc, dev);
335 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
336 kfree(udc);
337}
338
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200339static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200340
341static void usb_udc_nop_release(struct device *dev)
342{
343 dev_vdbg(dev, "%s\n", __func__);
344}
345
Felipe Balbi2ccea032011-06-28 16:33:46 +0300346/**
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200347 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
348 * @parent: the parent device to this udc. Usually the controller driver's
349 * device.
350 * @gadget: the gadget to be added to the list.
351 * @release: a gadget release function.
Felipe Balbi2ccea032011-06-28 16:33:46 +0300352 *
353 * Returns zero on success, negative errno otherwise.
354 */
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200355int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
356 void (*release)(struct device *dev))
Felipe Balbi2ccea032011-06-28 16:33:46 +0300357{
358 struct usb_udc *udc;
359 int ret = -ENOMEM;
360
361 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
362 if (!udc)
363 goto err1;
364
Felipe Balbi7bce4012013-01-24 17:41:00 +0200365 dev_set_name(&gadget->dev, "gadget");
Felipe Balbi5702f752013-07-17 11:09:49 +0300366 INIT_WORK(&gadget->work, usb_gadget_state_work);
Felipe Balbi2ed14322013-02-26 10:59:41 +0200367 gadget->dev.parent = parent;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200368
Alan Stern908b9612013-07-12 11:06:21 -0400369#ifdef CONFIG_HAS_DMA
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200370 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
371 gadget->dev.dma_parms = parent->dma_parms;
372 gadget->dev.dma_mask = parent->dma_mask;
Alan Stern908b9612013-07-12 11:06:21 -0400373#endif
Felipe Balbi658c8cf2013-02-26 10:50:42 +0200374
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200375 if (release)
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200376 gadget->dev.release = release;
Felipe Balbiddf47cc2013-02-26 15:25:41 +0200377 else
378 gadget->dev.release = usb_udc_nop_release;
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200379
Felipe Balbi7bce4012013-01-24 17:41:00 +0200380 ret = device_register(&gadget->dev);
381 if (ret)
382 goto err2;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200383
Felipe Balbi2ccea032011-06-28 16:33:46 +0300384 device_initialize(&udc->dev);
385 udc->dev.release = usb_udc_release;
386 udc->dev.class = udc_class;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200387 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300388 udc->dev.parent = parent;
389 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
390 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200391 goto err3;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300392
393 udc->gadget = gadget;
Peter Chendfea9c92015-03-06 10:36:02 +0800394 gadget->udc = udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300395
396 mutex_lock(&udc_lock);
397 list_add_tail(&udc->list, &udc_list);
398
399 ret = device_add(&udc->dev);
400 if (ret)
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200401 goto err4;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300402
Felipe Balbi49401f42011-12-19 12:57:04 +0200403 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
Peter Chen628ef0d2015-03-06 10:36:03 +0800404 udc->vbus = true;
Felipe Balbi49401f42011-12-19 12:57:04 +0200405
Felipe Balbi2ccea032011-06-28 16:33:46 +0300406 mutex_unlock(&udc_lock);
407
408 return 0;
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200409
410err4:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300411 list_del(&udc->list);
412 mutex_unlock(&udc_lock);
413
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200414err3:
Felipe Balbi2ccea032011-06-28 16:33:46 +0300415 put_device(&udc->dev);
Alan Sternc93e64e2015-01-16 11:32:51 -0500416 device_del(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300417
Felipe Balbif07bd56b2013-01-24 14:52:24 +0200418err2:
Felipe Balbi7bce4012013-01-24 17:41:00 +0200419 put_device(&gadget->dev);
Felipe Balbic5dbc222013-04-02 17:06:28 +0300420 kfree(udc);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200421
Felipe Balbi2ccea032011-06-28 16:33:46 +0300422err1:
423 return ret;
424}
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200425EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
426
Felipe Balbi2ccea032011-06-28 16:33:46 +0300427/**
428 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
429 * @parent: the parent device to this udc. Usually the controller
430 * driver's device.
431 * @gadget: the gadget to be added to the list
432 *
433 * Returns zero on success, negative errno otherwise.
434 */
435int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
436{
Felipe Balbi792bfcf2013-02-26 14:47:44 +0200437 return usb_add_gadget_udc_release(parent, gadget, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300438}
439EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
440
441static void usb_gadget_remove_driver(struct usb_udc *udc)
442{
443 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
Felipe Balbi8da9fe82014-10-17 11:54:46 -0500444 udc->driver->function);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300445
446 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
447
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200448 usb_gadget_disconnect(udc->gadget);
449 udc->driver->disconnect(udc->gadget);
450 udc->driver->unbind(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -0500451 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300452
453 udc->driver = NULL;
454 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200455 udc->gadget->dev.driver = NULL;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300456}
457
458/**
459 * usb_del_gadget_udc - deletes @udc from udc_list
460 * @gadget: the gadget to be removed.
461 *
462 * This, will call usb_gadget_unregister_driver() if
463 * the @udc is still busy.
464 */
465void usb_del_gadget_udc(struct usb_gadget *gadget)
466{
Peter Chendfea9c92015-03-06 10:36:02 +0800467 struct usb_udc *udc = gadget->udc;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300468
Peter Chendfea9c92015-03-06 10:36:02 +0800469 if (!udc)
470 return;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300471
Felipe Balbi2ccea032011-06-28 16:33:46 +0300472 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
473
Peter Chendfea9c92015-03-06 10:36:02 +0800474 mutex_lock(&udc_lock);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300475 list_del(&udc->list);
476 mutex_unlock(&udc_lock);
477
478 if (udc->driver)
479 usb_gadget_remove_driver(udc);
480
481 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
Felipe Balbi5702f752013-07-17 11:09:49 +0300482 flush_work(&gadget->work);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300483 device_unregister(&udc->dev);
Felipe Balbi7bce4012013-01-24 17:41:00 +0200484 device_unregister(&gadget->dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300485}
486EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
487
488/* ------------------------------------------------------------------------- */
489
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100490static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300491{
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100492 int ret;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300493
Felipe Balbi2ccea032011-06-28 16:33:46 +0300494 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
495 driver->function);
496
497 udc->driver = driver;
498 udc->dev.driver = &driver->driver;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200499 udc->gadget->dev.driver = &driver->driver;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300500
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200501 ret = driver->bind(udc->gadget, driver);
502 if (ret)
503 goto err1;
Felipe Balbi2c683342014-10-17 11:34:07 -0500504 ret = usb_gadget_udc_start(udc);
Felipe Balbi2d7ebbb2013-01-24 11:00:15 +0200505 if (ret) {
506 driver->unbind(udc->gadget);
507 goto err1;
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200508 }
Peter Chen628ef0d2015-03-06 10:36:03 +0800509 usb_udc_connect_control(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300510
511 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300512 return 0;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300513err1:
Fabio Estevamf8cffc82013-09-28 00:05:34 -0300514 if (ret != -EISNAM)
515 dev_err(&udc->dev, "failed to start %s: %d\n",
Felipe Balbi2ccea032011-06-28 16:33:46 +0300516 udc->driver->function, ret);
517 udc->driver = NULL;
518 udc->dev.driver = NULL;
Felipe Balbi70d3a492013-02-26 13:51:24 +0200519 udc->gadget->dev.driver = NULL;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100520 return ret;
521}
522
Felipe Balbi02e8c962014-10-17 18:57:06 -0500523int usb_udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100524{
525 struct usb_udc *udc = NULL;
526 int ret = -ENODEV;
527
528 mutex_lock(&udc_lock);
529 list_for_each_entry(udc, &udc_list, list) {
530 ret = strcmp(name, dev_name(&udc->dev));
531 if (!ret)
532 break;
533 }
534 if (ret) {
535 ret = -ENODEV;
536 goto out;
537 }
538 if (udc->driver) {
539 ret = -EBUSY;
540 goto out;
541 }
542 ret = udc_bind_to_driver(udc, driver);
543out:
544 mutex_unlock(&udc_lock);
545 return ret;
546}
Felipe Balbi02e8c962014-10-17 18:57:06 -0500547EXPORT_SYMBOL_GPL(usb_udc_attach_driver);
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100548
549int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
550{
551 struct usb_udc *udc = NULL;
Ruslan Bilovol2284b292015-11-23 09:56:35 +0100552 int ret = -ENODEV;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100553
554 if (!driver || !driver->bind || !driver->setup)
555 return -EINVAL;
556
557 mutex_lock(&udc_lock);
Ruslan Bilovol2284b292015-11-23 09:56:35 +0100558 if (driver->udc_name) {
559 list_for_each_entry(udc, &udc_list, list) {
560 ret = strcmp(driver->udc_name, dev_name(&udc->dev));
561 if (!ret)
562 break;
563 }
564 if (ret)
565 ret = -ENODEV;
566 else if (udc->driver)
567 ret = -EBUSY;
568 else
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100569 goto found;
Ruslan Bilovol2284b292015-11-23 09:56:35 +0100570 } else {
571 list_for_each_entry(udc, &udc_list, list) {
572 /* For now we take the first one */
573 if (!udc->driver)
574 goto found;
575 }
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100576 }
577
578 pr_debug("couldn't find an available UDC\n");
579 mutex_unlock(&udc_lock);
Ruslan Bilovol2284b292015-11-23 09:56:35 +0100580 return ret;
Sebastian Andrzej Siewior4c49a5f2012-12-23 21:10:19 +0100581found:
582 ret = udc_bind_to_driver(udc, driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300583 mutex_unlock(&udc_lock);
584 return ret;
585}
586EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
587
588int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
589{
590 struct usb_udc *udc = NULL;
591 int ret = -ENODEV;
592
593 if (!driver || !driver->unbind)
594 return -EINVAL;
595
596 mutex_lock(&udc_lock);
597 list_for_each_entry(udc, &udc_list, list)
598 if (udc->driver == driver) {
599 usb_gadget_remove_driver(udc);
Peter Chenb5fb8d02014-04-29 13:26:29 +0800600 usb_gadget_set_state(udc->gadget,
601 USB_STATE_NOTATTACHED);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300602 ret = 0;
603 break;
604 }
605
606 mutex_unlock(&udc_lock);
607 return ret;
608}
609EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
610
611/* ------------------------------------------------------------------------- */
612
613static ssize_t usb_udc_srp_store(struct device *dev,
614 struct device_attribute *attr, const char *buf, size_t n)
615{
Felipe Balbi1d91a962011-09-08 14:11:17 +0300616 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300617
618 if (sysfs_streq(buf, "1"))
619 usb_gadget_wakeup(udc->gadget);
620
621 return n;
622}
623static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
624
625static ssize_t usb_udc_softconn_store(struct device *dev,
626 struct device_attribute *attr, const char *buf, size_t n)
627{
Felipe Balbi865569b2011-09-08 14:12:18 +0300628 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300629
Felipe Balbibfa6b182014-10-17 11:10:25 -0500630 if (!udc->driver) {
631 dev_err(dev, "soft-connect without a gadget driver\n");
632 return -EOPNOTSUPP;
633 }
634
Felipe Balbi2ccea032011-06-28 16:33:46 +0300635 if (sysfs_streq(buf, "connect")) {
Felipe Balbi2c683342014-10-17 11:34:07 -0500636 usb_gadget_udc_start(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300637 usb_gadget_connect(udc->gadget);
638 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +0300639 usb_gadget_disconnect(udc->gadget);
Felipe Balbi0abd0692014-10-09 10:12:24 -0500640 udc->driver->disconnect(udc->gadget);
Felipe Balbi2c683342014-10-17 11:34:07 -0500641 usb_gadget_udc_stop(udc);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300642 } else {
643 dev_err(dev, "unsupported command '%s'\n", buf);
644 return -EINVAL;
645 }
646
647 return n;
648}
649static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
650
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700651static ssize_t state_show(struct device *dev, struct device_attribute *attr,
652 char *buf)
Felipe Balbi49401f42011-12-19 12:57:04 +0200653{
654 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
655 struct usb_gadget *gadget = udc->gadget;
656
657 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
658}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700659static DEVICE_ATTR_RO(state);
Felipe Balbi49401f42011-12-19 12:57:04 +0200660
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100661#define USB_UDC_SPEED_ATTR(name, param) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700662ssize_t name##_show(struct device *dev, \
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100663 struct device_attribute *attr, char *buf) \
664{ \
665 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
666 return snprintf(buf, PAGE_SIZE, "%s\n", \
667 usb_speed_string(udc->gadget->param)); \
668} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700669static DEVICE_ATTR_RO(name)
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100670
671static USB_UDC_SPEED_ATTR(current_speed, speed);
672static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
673
Felipe Balbi2ccea032011-06-28 16:33:46 +0300674#define USB_UDC_ATTR(name) \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700675ssize_t name##_show(struct device *dev, \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300676 struct device_attribute *attr, char *buf) \
677{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200678 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300679 struct usb_gadget *gadget = udc->gadget; \
680 \
681 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
682} \
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700683static DEVICE_ATTR_RO(name)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300684
Felipe Balbi2ccea032011-06-28 16:33:46 +0300685static USB_UDC_ATTR(is_otg);
686static USB_UDC_ATTR(is_a_peripheral);
687static USB_UDC_ATTR(b_hnp_enable);
688static USB_UDC_ATTR(a_hnp_support);
689static USB_UDC_ATTR(a_alt_hnp_support);
Peter Chen3f6dd4f2015-01-28 16:32:42 +0800690static USB_UDC_ATTR(is_selfpowered);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300691
692static struct attribute *usb_udc_attrs[] = {
693 &dev_attr_srp.attr,
694 &dev_attr_soft_connect.attr,
Felipe Balbi49401f42011-12-19 12:57:04 +0200695 &dev_attr_state.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100696 &dev_attr_current_speed.attr,
697 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300698
Felipe Balbi2ccea032011-06-28 16:33:46 +0300699 &dev_attr_is_otg.attr,
700 &dev_attr_is_a_peripheral.attr,
701 &dev_attr_b_hnp_enable.attr,
702 &dev_attr_a_hnp_support.attr,
703 &dev_attr_a_alt_hnp_support.attr,
Peter Chen3f6dd4f2015-01-28 16:32:42 +0800704 &dev_attr_is_selfpowered.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300705 NULL,
706};
707
708static const struct attribute_group usb_udc_attr_group = {
709 .attrs = usb_udc_attrs,
710};
711
712static const struct attribute_group *usb_udc_attr_groups[] = {
713 &usb_udc_attr_group,
714 NULL,
715};
716
717static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
718{
719 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
720 int ret;
721
722 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
723 if (ret) {
724 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
725 return ret;
726 }
727
728 if (udc->driver) {
729 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
730 udc->driver->function);
731 if (ret) {
732 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
733 return ret;
734 }
735 }
736
737 return 0;
738}
739
740static int __init usb_udc_init(void)
741{
742 udc_class = class_create(THIS_MODULE, "udc");
743 if (IS_ERR(udc_class)) {
744 pr_err("failed to create udc class --> %ld\n",
745 PTR_ERR(udc_class));
746 return PTR_ERR(udc_class);
747 }
748
749 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300750 return 0;
751}
752subsys_initcall(usb_udc_init);
753
754static void __exit usb_udc_exit(void)
755{
756 class_destroy(udc_class);
757}
758module_exit(usb_udc_exit);
759
760MODULE_DESCRIPTION("UDC Framework");
761MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
762MODULE_LICENSE("GPL v2");