1 // SPDX-License-Identifier: GPL-2.0
3 * udc-core.c - Core UDC Framework
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
9 * Taken from Linux Kernel v3.19-rc1 (drivers/usb/gadget/udc-core.c) and ported
12 * commit 02e8c96627 : usb: gadget: udc: core: prepend udc_attach_driver with
16 #include <dm/device_compat.h>
17 #include <dm/devres.h>
18 #include <linux/compat.h>
20 #include <asm/cache.h>
21 #include <asm/dma-mapping.h>
24 #include <dm/device-internal.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
29 * struct usb_udc - describes one usb device controller
30 * @driver - the gadget driver pointer. For use by the class code
31 * @dev - the child device to the actual controller
32 * @gadget - the gadget. For use by the class code
33 * @list - for use by the udc class driver
35 * This represents the internal data structure which is used by the UDC-class
36 * to hold information about udc driver and gadget together.
39 struct usb_gadget_driver *driver;
40 struct usb_gadget *gadget;
42 struct list_head list;
45 static struct class *udc_class;
46 static LIST_HEAD(udc_list);
47 DEFINE_MUTEX(udc_lock);
49 /* ------------------------------------------------------------------------- */
51 int usb_gadget_map_request(struct usb_gadget *gadget,
52 struct usb_request *req, int is_in)
57 req->dma = dma_map_single(req->buf, req->length,
58 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
62 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
64 void usb_gadget_unmap_request(struct usb_gadget *gadget,
65 struct usb_request *req, int is_in)
70 dma_unmap_single((void *)(uintptr_t)req->dma, req->length,
71 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
75 /* ------------------------------------------------------------------------- */
78 * usb_gadget_giveback_request - give the request back to the gadget layer
79 * Context: in_interrupt()
81 * This is called by device controller drivers in order to return the
82 * completed request back to the gadget layer.
84 void usb_gadget_giveback_request(struct usb_ep *ep,
85 struct usb_request *req)
87 req->complete(ep, req);
89 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
91 /* ------------------------------------------------------------------------- */
93 void usb_gadget_set_state(struct usb_gadget *gadget,
94 enum usb_device_state state)
96 gadget->state = state;
98 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
100 /* ------------------------------------------------------------------------- */
103 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
104 * @gadget: The gadget which bus reset occurs
105 * @driver: The gadget driver we want to notify
107 * If the udc driver has bus reset handler, it needs to call this when the bus
108 * reset occurs, it notifies the gadget driver that the bus reset occurs as
109 * well as updates gadget state.
111 void usb_gadget_udc_reset(struct usb_gadget *gadget,
112 struct usb_gadget_driver *driver)
114 driver->reset(gadget);
115 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
117 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
120 * usb_gadget_udc_start - tells usb device controller to start up
121 * @udc: The UDC to be started
123 * This call is issued by the UDC Class driver when it's about
124 * to register a gadget driver to the device controller, before
125 * calling gadget driver's bind() method.
127 * It allows the controller to be powered off until strictly
128 * necessary to have it powered on.
130 * Returns zero on success, else negative errno.
132 static inline int usb_gadget_udc_start(struct usb_udc *udc)
134 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
138 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
139 * @gadget: The device we want to stop activity
140 * @driver: The driver to unbind from @gadget
142 * This call is issued by the UDC Class driver after calling
143 * gadget driver's unbind() method.
145 * The details are implementation specific, but it can go as
146 * far as powering off UDC completely and disable its data
149 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
151 udc->gadget->ops->udc_stop(udc->gadget);
155 * usb_udc_release - release the usb_udc struct
156 * @dev: the dev member within usb_udc
158 * This is called by driver's core in order to free memory once the last
159 * reference is released.
161 static void usb_udc_release(struct device *dev)
165 udc = container_of(dev, struct usb_udc, dev);
170 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
171 * @parent: the parent device to this udc. Usually the controller driver's
173 * @gadget: the gadget to be added to the list.
174 * @release: a gadget release function.
176 * Returns zero on success, negative errno otherwise.
178 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
179 void (*release)(struct device *dev))
184 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
188 dev_set_name(&gadget->dev, "gadget");
189 gadget->dev.parent = parent;
191 udc->dev.release = usb_udc_release;
192 udc->dev.class = udc_class;
193 udc->dev.parent = parent;
195 udc->gadget = gadget;
197 mutex_lock(&udc_lock);
198 list_add_tail(&udc->list, &udc_list);
200 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
202 mutex_unlock(&udc_lock);
209 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
212 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
213 * @parent: the parent device to this udc. Usually the controller
215 * @gadget: the gadget to be added to the list
217 * Returns zero on success, negative errno otherwise.
219 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
221 return usb_add_gadget_udc_release(parent, gadget, NULL);
223 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
225 static void usb_gadget_remove_driver(struct usb_udc *udc)
227 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
228 udc->driver->function);
230 usb_gadget_disconnect(udc->gadget);
231 udc->driver->disconnect(udc->gadget);
232 udc->driver->unbind(udc->gadget);
233 usb_gadget_udc_stop(udc);
239 * usb_del_gadget_udc - deletes @udc from udc_list
240 * @gadget: the gadget to be removed.
242 * This, will call usb_gadget_unregister_driver() if
243 * the @udc is still busy.
245 void usb_del_gadget_udc(struct usb_gadget *gadget)
247 struct usb_udc *udc = NULL;
249 mutex_lock(&udc_lock);
250 list_for_each_entry(udc, &udc_list, list)
251 if (udc->gadget == gadget)
254 dev_err(gadget->dev.parent, "gadget not registered.\n");
255 mutex_unlock(&udc_lock);
260 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
262 list_del(&udc->list);
263 mutex_unlock(&udc_lock);
266 usb_gadget_remove_driver(udc);
268 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
270 /* ------------------------------------------------------------------------- */
273 * usb_gadget_udc_set_speed - tells usb device controller speed supported by
275 * @udc: The device we want to set maximum speed
276 * @speed: The maximum speed to allowed to run
278 * This call is issued by the UDC Class driver before calling
279 * usb_gadget_udc_start() in order to make sure that we don't try to
280 * connect on speeds the gadget driver doesn't support.
282 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
283 enum usb_device_speed speed)
285 if (udc->gadget->ops->udc_set_speed) {
286 enum usb_device_speed s;
288 s = min(speed, udc->gadget->max_speed);
289 udc->gadget->ops->udc_set_speed(udc->gadget, s);
293 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
297 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
300 udc->driver = driver;
302 usb_gadget_udc_set_speed(udc, driver->speed);
304 ret = driver->bind(udc->gadget);
307 ret = usb_gadget_udc_start(udc);
309 driver->unbind(udc->gadget);
312 usb_gadget_connect(udc->gadget);
317 dev_err(&udc->dev, "failed to start %s: %d\n",
318 udc->driver->function, ret);
323 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
325 struct usb_udc *udc = NULL;
328 if (!driver || !driver->bind || !driver->setup)
331 mutex_lock(&udc_lock);
332 list_for_each_entry(udc, &udc_list, list) {
333 /* For now we take the first one */
338 printf("couldn't find an available UDC\n");
339 mutex_unlock(&udc_lock);
342 ret = udc_bind_to_driver(udc, driver);
343 mutex_unlock(&udc_lock);
346 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
348 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
350 return usb_gadget_probe_driver(driver);
352 EXPORT_SYMBOL_GPL(usb_gadget_register_driver);
354 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
356 struct usb_udc *udc = NULL;
359 if (!driver || !driver->unbind)
362 mutex_lock(&udc_lock);
363 list_for_each_entry(udc, &udc_list, list)
364 if (udc->driver == driver) {
365 usb_gadget_remove_driver(udc);
366 usb_gadget_set_state(udc->gadget,
367 USB_STATE_NOTATTACHED);
372 mutex_unlock(&udc_lock);
375 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
377 MODULE_DESCRIPTION("UDC Framework");
379 MODULE_LICENSE("GPL v2");