Home | History | Annotate | Download | only in udc
      1 // SPDX-License-Identifier: GPL-2.0
      2 /**
      3  * udc-core.c - Core UDC Framework
      4  *
      5  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
      6  *
      7  * Author: Felipe Balbi <balbi (at) ti.com>
      8  *
      9  * Taken from Linux Kernel v3.19-rc1 (drivers/usb/gadget/udc-core.c) and ported
     10  * to uboot.
     11  *
     12  * commit 02e8c96627 : usb: gadget: udc: core: prepend udc_attach_driver with
     13  *		       usb_
     14  */
     15 
     16 #include <linux/compat.h>
     17 #include <malloc.h>
     18 #include <asm/cache.h>
     19 #include <asm/dma-mapping.h>
     20 #include <common.h>
     21 
     22 #include <linux/usb/ch9.h>
     23 #include <linux/usb/gadget.h>
     24 
     25 /**
     26  * struct usb_udc - describes one usb device controller
     27  * @driver - the gadget driver pointer. For use by the class code
     28  * @dev - the child device to the actual controller
     29  * @gadget - the gadget. For use by the class code
     30  * @list - for use by the udc class driver
     31  *
     32  * This represents the internal data structure which is used by the UDC-class
     33  * to hold information about udc driver and gadget together.
     34  */
     35 struct usb_udc {
     36 	struct usb_gadget_driver	*driver;
     37 	struct usb_gadget		*gadget;
     38 	struct device			dev;
     39 	struct list_head		list;
     40 };
     41 
     42 static struct class *udc_class;
     43 static LIST_HEAD(udc_list);
     44 DEFINE_MUTEX(udc_lock);
     45 
     46 /* ------------------------------------------------------------------------- */
     47 
     48 int usb_gadget_map_request(struct usb_gadget *gadget,
     49 		struct usb_request *req, int is_in)
     50 {
     51 	if (req->length == 0)
     52 		return 0;
     53 
     54 	req->dma = dma_map_single(req->buf, req->length,
     55 				  is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
     56 
     57 	return 0;
     58 }
     59 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
     60 
     61 void usb_gadget_unmap_request(struct usb_gadget *gadget,
     62 		struct usb_request *req, int is_in)
     63 {
     64 	if (req->length == 0)
     65 		return;
     66 
     67 	dma_unmap_single((void *)(uintptr_t)req->dma, req->length,
     68 			 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
     69 }
     70 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
     71 
     72 /* ------------------------------------------------------------------------- */
     73 
     74 /**
     75  * usb_gadget_giveback_request - give the request back to the gadget layer
     76  * Context: in_interrupt()
     77  *
     78  * This is called by device controller drivers in order to return the
     79  * completed request back to the gadget layer.
     80  */
     81 void usb_gadget_giveback_request(struct usb_ep *ep,
     82 		struct usb_request *req)
     83 {
     84 	req->complete(ep, req);
     85 }
     86 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
     87 
     88 /* ------------------------------------------------------------------------- */
     89 
     90 void usb_gadget_set_state(struct usb_gadget *gadget,
     91 		enum usb_device_state state)
     92 {
     93 	gadget->state = state;
     94 }
     95 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
     96 
     97 /* ------------------------------------------------------------------------- */
     98 
     99 /**
    100  * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
    101  * @gadget: The gadget which bus reset occurs
    102  * @driver: The gadget driver we want to notify
    103  *
    104  * If the udc driver has bus reset handler, it needs to call this when the bus
    105  * reset occurs, it notifies the gadget driver that the bus reset occurs as
    106  * well as updates gadget state.
    107  */
    108 void usb_gadget_udc_reset(struct usb_gadget *gadget,
    109 		struct usb_gadget_driver *driver)
    110 {
    111 	driver->reset(gadget);
    112 	usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
    113 }
    114 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
    115 
    116 /**
    117  * usb_gadget_udc_start - tells usb device controller to start up
    118  * @udc: The UDC to be started
    119  *
    120  * This call is issued by the UDC Class driver when it's about
    121  * to register a gadget driver to the device controller, before
    122  * calling gadget driver's bind() method.
    123  *
    124  * It allows the controller to be powered off until strictly
    125  * necessary to have it powered on.
    126  *
    127  * Returns zero on success, else negative errno.
    128  */
    129 static inline int usb_gadget_udc_start(struct usb_udc *udc)
    130 {
    131 	return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
    132 }
    133 
    134 /**
    135  * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
    136  * @gadget: The device we want to stop activity
    137  * @driver: The driver to unbind from @gadget
    138  *
    139  * This call is issued by the UDC Class driver after calling
    140  * gadget driver's unbind() method.
    141  *
    142  * The details are implementation specific, but it can go as
    143  * far as powering off UDC completely and disable its data
    144  * line pullups.
    145  */
    146 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
    147 {
    148 	udc->gadget->ops->udc_stop(udc->gadget);
    149 }
    150 
    151 /**
    152  * usb_udc_release - release the usb_udc struct
    153  * @dev: the dev member within usb_udc
    154  *
    155  * This is called by driver's core in order to free memory once the last
    156  * reference is released.
    157  */
    158 static void usb_udc_release(struct device *dev)
    159 {
    160 	struct usb_udc *udc;
    161 
    162 	udc = container_of(dev, struct usb_udc, dev);
    163 	kfree(udc);
    164 }
    165 
    166 /**
    167  * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
    168  * @parent: the parent device to this udc. Usually the controller driver's
    169  * device.
    170  * @gadget: the gadget to be added to the list.
    171  * @release: a gadget release function.
    172  *
    173  * Returns zero on success, negative errno otherwise.
    174  */
    175 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
    176 		void (*release)(struct device *dev))
    177 {
    178 	struct usb_udc		*udc;
    179 	int			ret = -ENOMEM;
    180 
    181 	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
    182 	if (!udc)
    183 		goto err1;
    184 
    185 	dev_set_name(&gadget->dev, "gadget");
    186 	gadget->dev.parent = parent;
    187 
    188 	udc->dev.release = usb_udc_release;
    189 	udc->dev.class = udc_class;
    190 	udc->dev.parent = parent;
    191 
    192 	udc->gadget = gadget;
    193 
    194 	mutex_lock(&udc_lock);
    195 	list_add_tail(&udc->list, &udc_list);
    196 
    197 	usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
    198 
    199 	mutex_unlock(&udc_lock);
    200 
    201 	return 0;
    202 
    203 err1:
    204 	return ret;
    205 }
    206 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
    207 
    208 /**
    209  * usb_add_gadget_udc - adds a new gadget to the udc class driver list
    210  * @parent: the parent device to this udc. Usually the controller
    211  * driver's device.
    212  * @gadget: the gadget to be added to the list
    213  *
    214  * Returns zero on success, negative errno otherwise.
    215  */
    216 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
    217 {
    218 	return usb_add_gadget_udc_release(parent, gadget, NULL);
    219 }
    220 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
    221 
    222 static void usb_gadget_remove_driver(struct usb_udc *udc)
    223 {
    224 	dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
    225 			udc->driver->function);
    226 
    227 	usb_gadget_disconnect(udc->gadget);
    228 	udc->driver->disconnect(udc->gadget);
    229 	udc->driver->unbind(udc->gadget);
    230 	usb_gadget_udc_stop(udc);
    231 
    232 	udc->driver = NULL;
    233 }
    234 
    235 /**
    236  * usb_del_gadget_udc - deletes @udc from udc_list
    237  * @gadget: the gadget to be removed.
    238  *
    239  * This, will call usb_gadget_unregister_driver() if
    240  * the @udc is still busy.
    241  */
    242 void usb_del_gadget_udc(struct usb_gadget *gadget)
    243 {
    244 	struct usb_udc		*udc = NULL;
    245 
    246 	mutex_lock(&udc_lock);
    247 	list_for_each_entry(udc, &udc_list, list)
    248 		if (udc->gadget == gadget)
    249 			goto found;
    250 
    251 	dev_err(gadget->dev.parent, "gadget not registered.\n");
    252 	mutex_unlock(&udc_lock);
    253 
    254 	return;
    255 
    256 found:
    257 	dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
    258 
    259 	list_del(&udc->list);
    260 	mutex_unlock(&udc_lock);
    261 
    262 	if (udc->driver)
    263 		usb_gadget_remove_driver(udc);
    264 }
    265 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
    266 
    267 /* ------------------------------------------------------------------------- */
    268 
    269 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
    270 {
    271 	int ret;
    272 
    273 	dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
    274 			driver->function);
    275 
    276 	udc->driver = driver;
    277 
    278 	ret = driver->bind(udc->gadget);
    279 	if (ret)
    280 		goto err1;
    281 	ret = usb_gadget_udc_start(udc);
    282 	if (ret) {
    283 		driver->unbind(udc->gadget);
    284 		goto err1;
    285 	}
    286 	usb_gadget_connect(udc->gadget);
    287 
    288 	return 0;
    289 err1:
    290 	if (ret != -EISNAM)
    291 		dev_err(&udc->dev, "failed to start %s: %d\n",
    292 			udc->driver->function, ret);
    293 	udc->driver = NULL;
    294 	return ret;
    295 }
    296 
    297 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
    298 {
    299 	struct usb_udc		*udc = NULL;
    300 	int			ret;
    301 
    302 	if (!driver || !driver->bind || !driver->setup)
    303 		return -EINVAL;
    304 
    305 	mutex_lock(&udc_lock);
    306 	list_for_each_entry(udc, &udc_list, list) {
    307 		/* For now we take the first one */
    308 		if (!udc->driver)
    309 			goto found;
    310 	}
    311 
    312 	printf("couldn't find an available UDC\n");
    313 	mutex_unlock(&udc_lock);
    314 	return -ENODEV;
    315 found:
    316 	ret = udc_bind_to_driver(udc, driver);
    317 	mutex_unlock(&udc_lock);
    318 	return ret;
    319 }
    320 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
    321 
    322 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
    323 {
    324 	return usb_gadget_probe_driver(driver);
    325 }
    326 EXPORT_SYMBOL_GPL(usb_gadget_register_driver);
    327 
    328 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
    329 {
    330 	struct usb_udc		*udc = NULL;
    331 	int			ret = -ENODEV;
    332 
    333 	if (!driver || !driver->unbind)
    334 		return -EINVAL;
    335 
    336 	mutex_lock(&udc_lock);
    337 	list_for_each_entry(udc, &udc_list, list)
    338 		if (udc->driver == driver) {
    339 			usb_gadget_remove_driver(udc);
    340 			usb_gadget_set_state(udc->gadget,
    341 					USB_STATE_NOTATTACHED);
    342 			ret = 0;
    343 			break;
    344 		}
    345 
    346 	mutex_unlock(&udc_lock);
    347 	return ret;
    348 }
    349 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
    350 
    351 MODULE_DESCRIPTION("UDC Framework");
    352 MODULE_AUTHOR("Felipe Balbi <balbi (at) ti.com>");
    353 MODULE_LICENSE("GPL v2");
    354