• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/compat.h>
17 #include <malloc.h>
18 #include <asm/cache.h>
19 #include <asm/dma-mapping.h>
20 #include <common.h>
21 #include <dm.h>
22 #include <dm/device-internal.h>
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 
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)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 
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)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 *)(uintptr_t)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  */
usb_gadget_giveback_request(struct usb_ep * ep,struct usb_request * req)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 
usb_gadget_set_state(struct usb_gadget * gadget,enum usb_device_state state)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  */
usb_gadget_udc_reset(struct usb_gadget * gadget,struct usb_gadget_driver * driver)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  */
usb_gadget_udc_start(struct usb_udc * udc)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  */
usb_gadget_udc_stop(struct usb_udc * udc)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  */
usb_udc_release(struct device * dev)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  */
usb_add_gadget_udc_release(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))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  */
usb_add_gadget_udc(struct device * parent,struct usb_gadget * gadget)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 
usb_gadget_remove_driver(struct usb_udc * udc)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  */
usb_del_gadget_udc(struct usb_gadget * gadget)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 /**
271  * usb_gadget_udc_set_speed - tells usb device controller speed supported by
272  *    current driver
273  * @udc: The device we want to set maximum speed
274  * @speed: The maximum speed to allowed to run
275  *
276  * This call is issued by the UDC Class driver before calling
277  * usb_gadget_udc_start() in order to make sure that we don't try to
278  * connect on speeds the gadget driver doesn't support.
279  */
usb_gadget_udc_set_speed(struct usb_udc * udc,enum usb_device_speed speed)280 static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
281 					    enum usb_device_speed speed)
282 {
283 	if (udc->gadget->ops->udc_set_speed) {
284 		enum usb_device_speed s;
285 
286 		s = min(speed, udc->gadget->max_speed);
287 		udc->gadget->ops->udc_set_speed(udc->gadget, s);
288 	}
289 }
290 
udc_bind_to_driver(struct usb_udc * udc,struct usb_gadget_driver * driver)291 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
292 {
293 	int ret;
294 
295 	dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
296 			driver->function);
297 
298 	udc->driver = driver;
299 
300 	usb_gadget_udc_set_speed(udc, driver->speed);
301 
302 	ret = driver->bind(udc->gadget);
303 	if (ret)
304 		goto err1;
305 	ret = usb_gadget_udc_start(udc);
306 	if (ret) {
307 		driver->unbind(udc->gadget);
308 		goto err1;
309 	}
310 	usb_gadget_connect(udc->gadget);
311 
312 	return 0;
313 err1:
314 	if (ret != -EISNAM)
315 		dev_err(&udc->dev, "failed to start %s: %d\n",
316 			udc->driver->function, ret);
317 	udc->driver = NULL;
318 	return ret;
319 }
320 
usb_gadget_probe_driver(struct usb_gadget_driver * driver)321 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
322 {
323 	struct usb_udc		*udc = NULL;
324 	int			ret;
325 
326 	if (!driver || !driver->bind || !driver->setup)
327 		return -EINVAL;
328 
329 	mutex_lock(&udc_lock);
330 	list_for_each_entry(udc, &udc_list, list) {
331 		/* For now we take the first one */
332 		if (!udc->driver)
333 			goto found;
334 	}
335 
336 	printf("couldn't find an available UDC\n");
337 	mutex_unlock(&udc_lock);
338 	return -ENODEV;
339 found:
340 	ret = udc_bind_to_driver(udc, driver);
341 	mutex_unlock(&udc_lock);
342 	return ret;
343 }
344 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
345 
usb_gadget_register_driver(struct usb_gadget_driver * driver)346 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
347 {
348 	return usb_gadget_probe_driver(driver);
349 }
350 EXPORT_SYMBOL_GPL(usb_gadget_register_driver);
351 
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)352 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
353 {
354 	struct usb_udc		*udc = NULL;
355 	int			ret = -ENODEV;
356 
357 	if (!driver || !driver->unbind)
358 		return -EINVAL;
359 
360 	mutex_lock(&udc_lock);
361 	list_for_each_entry(udc, &udc_list, list)
362 		if (udc->driver == driver) {
363 			usb_gadget_remove_driver(udc);
364 			usb_gadget_set_state(udc->gadget,
365 					USB_STATE_NOTATTACHED);
366 			ret = 0;
367 			break;
368 		}
369 
370 	mutex_unlock(&udc_lock);
371 	return ret;
372 }
373 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
374 
375 MODULE_DESCRIPTION("UDC Framework");
376 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
377 MODULE_LICENSE("GPL v2");
378