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