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