1 /**
2  * udc.c - Core UDC Framework
3  *
4  * Copyright (C) 2010 Texas Instruments
5  * Author: Felipe Balbi <balbi@ti.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2  of
9  * the License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
24 #include <linux/err.h>
25 #include <linux/dma-mapping.h>
26 
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 
30 /**
31  * struct usb_udc - describes one usb device controller
32  * @driver - the gadget driver pointer. For use by the class code
33  * @dev - the child device to the actual controller
34  * @gadget - the gadget. For use by the class code
35  * @list - for use by the udc class driver
36  *
37  * This represents the internal data structure which is used by the UDC-class
38  * to hold information about udc driver and gadget together.
39  */
40 struct usb_udc {
41 	struct usb_gadget_driver	*driver;
42 	struct usb_gadget		*gadget;
43 	struct device			dev;
44 	struct list_head		list;
45 };
46 
47 static struct class *udc_class;
48 static LIST_HEAD(udc_list);
49 static DEFINE_MUTEX(udc_lock);
50 
51 /* ------------------------------------------------------------------------- */
52 
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)53 int usb_gadget_map_request(struct usb_gadget *gadget,
54 		struct usb_request *req, int is_in)
55 {
56 	if (req->length == 0)
57 		return 0;
58 
59 	if (req->num_sgs) {
60 		int     mapped;
61 
62 		mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs,
63 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
64 		if (mapped == 0) {
65 			dev_err(&gadget->dev, "failed to map SGs\n");
66 			return -EFAULT;
67 		}
68 
69 		req->num_mapped_sgs = mapped;
70 	} else {
71 		req->dma = dma_map_single(&gadget->dev, req->buf, req->length,
72 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 
74 		if (dma_mapping_error(&gadget->dev, req->dma)) {
75 			dev_err(&gadget->dev, "failed to map buffer\n");
76 			return -EFAULT;
77 		}
78 	}
79 
80 	return 0;
81 }
82 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
83 
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)84 void usb_gadget_unmap_request(struct usb_gadget *gadget,
85 		struct usb_request *req, int is_in)
86 {
87 	if (req->length == 0)
88 		return;
89 
90 	if (req->num_mapped_sgs) {
91 		dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs,
92 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
93 
94 		req->num_mapped_sgs = 0;
95 	} else {
96 		dma_unmap_single(&gadget->dev, req->dma, req->length,
97 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
98 	}
99 }
100 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
101 
102 /* ------------------------------------------------------------------------- */
103 
104 /**
105  * usb_gadget_start - tells usb device controller to start up
106  * @gadget: The gadget we want to get started
107  * @driver: The driver we want to bind to @gadget
108  * @bind: The bind function for @driver
109  *
110  * This call is issued by the UDC Class driver when it's about
111  * to register a gadget driver to the device controller, before
112  * calling gadget driver's bind() method.
113  *
114  * It allows the controller to be powered off until strictly
115  * necessary to have it powered on.
116  *
117  * Returns zero on success, else negative errno.
118  */
usb_gadget_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver,int (* bind)(struct usb_gadget *))119 static inline int usb_gadget_start(struct usb_gadget *gadget,
120 		struct usb_gadget_driver *driver,
121 		int (*bind)(struct usb_gadget *))
122 {
123 	return gadget->ops->start(driver, bind);
124 }
125 
126 /**
127  * usb_gadget_udc_start - tells usb device controller to start up
128  * @gadget: The gadget we want to get started
129  * @driver: The driver we want to bind to @gadget
130  *
131  * This call is issued by the UDC Class driver when it's about
132  * to register a gadget driver to the device controller, before
133  * calling gadget driver's bind() method.
134  *
135  * It allows the controller to be powered off until strictly
136  * necessary to have it powered on.
137  *
138  * Returns zero on success, else negative errno.
139  */
usb_gadget_udc_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver)140 static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
141 		struct usb_gadget_driver *driver)
142 {
143 	return gadget->ops->udc_start(gadget, driver);
144 }
145 
146 /**
147  * usb_gadget_stop - tells usb device controller we don't need it anymore
148  * @gadget: The device we want to stop activity
149  * @driver: The driver to unbind from @gadget
150  *
151  * This call is issued by the UDC Class driver after calling
152  * gadget driver's unbind() method.
153  *
154  * The details are implementation specific, but it can go as
155  * far as powering off UDC completely and disable its data
156  * line pullups.
157  */
usb_gadget_stop(struct usb_gadget * gadget,struct usb_gadget_driver * driver)158 static inline void usb_gadget_stop(struct usb_gadget *gadget,
159 		struct usb_gadget_driver *driver)
160 {
161 	gadget->ops->stop(driver);
162 }
163 
164 /**
165  * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
166  * @gadget: The device we want to stop activity
167  * @driver: The driver to unbind from @gadget
168  *
169  * This call is issued by the UDC Class driver after calling
170  * gadget driver's unbind() method.
171  *
172  * The details are implementation specific, but it can go as
173  * far as powering off UDC completely and disable its data
174  * line pullups.
175  */
usb_gadget_udc_stop(struct usb_gadget * gadget,struct usb_gadget_driver * driver)176 static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
177 		struct usb_gadget_driver *driver)
178 {
179 	gadget->ops->udc_stop(gadget, driver);
180 }
181 
182 /**
183  * usb_udc_release - release the usb_udc struct
184  * @dev: the dev member within usb_udc
185  *
186  * This is called by driver's core in order to free memory once the last
187  * reference is released.
188  */
usb_udc_release(struct device * dev)189 static void usb_udc_release(struct device *dev)
190 {
191 	struct usb_udc *udc;
192 
193 	udc = container_of(dev, struct usb_udc, dev);
194 	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
195 	kfree(udc);
196 }
197 
198 static const struct attribute_group *usb_udc_attr_groups[];
199 /**
200  * usb_add_gadget_udc - adds a new gadget to the udc class driver list
201  * @parent: the parent device to this udc. Usually the controller
202  * driver's device.
203  * @gadget: the gadget to be added to the list
204  *
205  * Returns zero on success, negative errno otherwise.
206  */
usb_add_gadget_udc(struct device * parent,struct usb_gadget * gadget)207 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
208 {
209 	struct usb_udc		*udc;
210 	int			ret = -ENOMEM;
211 
212 	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
213 	if (!udc)
214 		goto err1;
215 
216 	device_initialize(&udc->dev);
217 	udc->dev.release = usb_udc_release;
218 	udc->dev.class = udc_class;
219 	udc->dev.groups = usb_udc_attr_groups;
220 	udc->dev.parent = parent;
221 	ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
222 	if (ret)
223 		goto err2;
224 
225 	udc->gadget = gadget;
226 
227 	mutex_lock(&udc_lock);
228 	list_add_tail(&udc->list, &udc_list);
229 
230 	ret = device_add(&udc->dev);
231 	if (ret)
232 		goto err3;
233 
234 	mutex_unlock(&udc_lock);
235 
236 	return 0;
237 err3:
238 	list_del(&udc->list);
239 	mutex_unlock(&udc_lock);
240 
241 err2:
242 	put_device(&udc->dev);
243 
244 err1:
245 	return ret;
246 }
247 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
248 
udc_is_newstyle(struct usb_udc * udc)249 static int udc_is_newstyle(struct usb_udc *udc)
250 {
251 	if (udc->gadget->ops->udc_start && udc->gadget->ops->udc_stop)
252 		return 1;
253 	return 0;
254 }
255 
256 
usb_gadget_remove_driver(struct usb_udc * udc)257 static void usb_gadget_remove_driver(struct usb_udc *udc)
258 {
259 	dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
260 			udc->gadget->name);
261 
262 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
263 
264 	if (udc_is_newstyle(udc)) {
265 		udc->driver->disconnect(udc->gadget);
266 		usb_gadget_disconnect(udc->gadget);
267 		udc->driver->unbind(udc->gadget);
268 		usb_gadget_udc_stop(udc->gadget, NULL);
269 	} else {
270 		usb_gadget_stop(udc->gadget, udc->driver);
271 	}
272 
273 	udc->driver = NULL;
274 	udc->dev.driver = NULL;
275 }
276 
277 /**
278  * usb_del_gadget_udc - deletes @udc from udc_list
279  * @gadget: the gadget to be removed.
280  *
281  * This, will call usb_gadget_unregister_driver() if
282  * the @udc is still busy.
283  */
usb_del_gadget_udc(struct usb_gadget * gadget)284 void usb_del_gadget_udc(struct usb_gadget *gadget)
285 {
286 	struct usb_udc		*udc = NULL;
287 
288 	mutex_lock(&udc_lock);
289 	list_for_each_entry(udc, &udc_list, list)
290 		if (udc->gadget == gadget)
291 			goto found;
292 
293 	dev_err(gadget->dev.parent, "gadget not registered.\n");
294 	mutex_unlock(&udc_lock);
295 
296 	return;
297 
298 found:
299 	dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
300 
301 	list_del(&udc->list);
302 	mutex_unlock(&udc_lock);
303 
304 	if (udc->driver)
305 		usb_gadget_remove_driver(udc);
306 
307 	kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
308 	device_unregister(&udc->dev);
309 }
310 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
311 
312 /* ------------------------------------------------------------------------- */
313 
usb_gadget_probe_driver(struct usb_gadget_driver * driver,int (* bind)(struct usb_gadget *))314 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
315 		int (*bind)(struct usb_gadget *))
316 {
317 	struct usb_udc		*udc = NULL;
318 	int			ret;
319 
320 	if (!driver || !bind || !driver->setup)
321 		return -EINVAL;
322 
323 	mutex_lock(&udc_lock);
324 	list_for_each_entry(udc, &udc_list, list) {
325 		/* For now we take the first one */
326 		if (!udc->driver)
327 			goto found;
328 	}
329 
330 	pr_debug("couldn't find an available UDC\n");
331 	mutex_unlock(&udc_lock);
332 	return -ENODEV;
333 
334 found:
335 	dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
336 			driver->function);
337 
338 	udc->driver = driver;
339 	udc->dev.driver = &driver->driver;
340 
341 	if (udc_is_newstyle(udc)) {
342 		ret = bind(udc->gadget);
343 		if (ret)
344 			goto err1;
345 		ret = usb_gadget_udc_start(udc->gadget, driver);
346 		if (ret) {
347 			driver->unbind(udc->gadget);
348 			goto err1;
349 		}
350 		usb_gadget_connect(udc->gadget);
351 	} else {
352 
353 		ret = usb_gadget_start(udc->gadget, driver, bind);
354 		if (ret)
355 			goto err1;
356 
357 	}
358 
359 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
360 	mutex_unlock(&udc_lock);
361 	return 0;
362 
363 err1:
364 	dev_err(&udc->dev, "failed to start %s: %d\n",
365 			udc->driver->function, ret);
366 	udc->driver = NULL;
367 	udc->dev.driver = NULL;
368 	mutex_unlock(&udc_lock);
369 	return ret;
370 }
371 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
372 
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)373 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
374 {
375 	struct usb_udc		*udc = NULL;
376 	int			ret = -ENODEV;
377 
378 	if (!driver || !driver->unbind)
379 		return -EINVAL;
380 
381 	mutex_lock(&udc_lock);
382 	list_for_each_entry(udc, &udc_list, list)
383 		if (udc->driver == driver) {
384 			usb_gadget_remove_driver(udc);
385 			ret = 0;
386 			break;
387 		}
388 
389 	mutex_unlock(&udc_lock);
390 	return ret;
391 }
392 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
393 
394 /* ------------------------------------------------------------------------- */
395 
usb_udc_srp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)396 static ssize_t usb_udc_srp_store(struct device *dev,
397 		struct device_attribute *attr, const char *buf, size_t n)
398 {
399 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
400 
401 	if (sysfs_streq(buf, "1"))
402 		usb_gadget_wakeup(udc->gadget);
403 
404 	return n;
405 }
406 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
407 
usb_udc_softconn_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)408 static ssize_t usb_udc_softconn_store(struct device *dev,
409 		struct device_attribute *attr, const char *buf, size_t n)
410 {
411 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
412 
413 	if (sysfs_streq(buf, "connect")) {
414 		if (udc_is_newstyle(udc))
415 			usb_gadget_udc_start(udc->gadget, udc->driver);
416 		usb_gadget_connect(udc->gadget);
417 	} else if (sysfs_streq(buf, "disconnect")) {
418 		usb_gadget_disconnect(udc->gadget);
419 		if (udc_is_newstyle(udc))
420 			usb_gadget_udc_stop(udc->gadget, udc->driver);
421 	} else {
422 		dev_err(dev, "unsupported command '%s'\n", buf);
423 		return -EINVAL;
424 	}
425 
426 	return n;
427 }
428 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
429 
430 #define USB_UDC_SPEED_ATTR(name, param)					\
431 ssize_t usb_udc_##param##_show(struct device *dev,			\
432 		struct device_attribute *attr, char *buf)		\
433 {									\
434 	struct usb_udc *udc = container_of(dev, struct usb_udc, dev);	\
435 	return snprintf(buf, PAGE_SIZE, "%s\n",				\
436 			usb_speed_string(udc->gadget->param));		\
437 }									\
438 static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
439 
440 static USB_UDC_SPEED_ATTR(current_speed, speed);
441 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
442 
443 /* TODO: Scheduled for removal in 3.8. */
usb_udc_is_dualspeed_show(struct device * dev,struct device_attribute * attr,char * buf)444 static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
445 		struct device_attribute *attr, char *buf)
446 {
447 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
448 	return snprintf(buf, PAGE_SIZE, "%d\n",
449 			gadget_is_dualspeed(udc->gadget));
450 }
451 static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_show, NULL);
452 
453 #define USB_UDC_ATTR(name)					\
454 ssize_t usb_udc_##name##_show(struct device *dev,		\
455 		struct device_attribute *attr, char *buf)	\
456 {								\
457 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev); \
458 	struct usb_gadget	*gadget = udc->gadget;		\
459 								\
460 	return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name);	\
461 }								\
462 static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
463 
464 static USB_UDC_ATTR(is_otg);
465 static USB_UDC_ATTR(is_a_peripheral);
466 static USB_UDC_ATTR(b_hnp_enable);
467 static USB_UDC_ATTR(a_hnp_support);
468 static USB_UDC_ATTR(a_alt_hnp_support);
469 
470 static struct attribute *usb_udc_attrs[] = {
471 	&dev_attr_srp.attr,
472 	&dev_attr_soft_connect.attr,
473 	&dev_attr_current_speed.attr,
474 	&dev_attr_maximum_speed.attr,
475 
476 	&dev_attr_is_dualspeed.attr,
477 	&dev_attr_is_otg.attr,
478 	&dev_attr_is_a_peripheral.attr,
479 	&dev_attr_b_hnp_enable.attr,
480 	&dev_attr_a_hnp_support.attr,
481 	&dev_attr_a_alt_hnp_support.attr,
482 	NULL,
483 };
484 
485 static const struct attribute_group usb_udc_attr_group = {
486 	.attrs = usb_udc_attrs,
487 };
488 
489 static const struct attribute_group *usb_udc_attr_groups[] = {
490 	&usb_udc_attr_group,
491 	NULL,
492 };
493 
usb_udc_uevent(struct device * dev,struct kobj_uevent_env * env)494 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
495 {
496 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
497 	int			ret;
498 
499 	ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
500 	if (ret) {
501 		dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
502 		return ret;
503 	}
504 
505 	if (udc->driver) {
506 		ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
507 				udc->driver->function);
508 		if (ret) {
509 			dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
510 			return ret;
511 		}
512 	}
513 
514 	return 0;
515 }
516 
usb_udc_init(void)517 static int __init usb_udc_init(void)
518 {
519 	udc_class = class_create(THIS_MODULE, "udc");
520 	if (IS_ERR(udc_class)) {
521 		pr_err("failed to create udc class --> %ld\n",
522 				PTR_ERR(udc_class));
523 		return PTR_ERR(udc_class);
524 	}
525 
526 	udc_class->dev_uevent = usb_udc_uevent;
527 	return 0;
528 }
529 subsys_initcall(usb_udc_init);
530 
usb_udc_exit(void)531 static void __exit usb_udc_exit(void)
532 {
533 	class_destroy(udc_class);
534 }
535 module_exit(usb_udc_exit);
536 
537 MODULE_DESCRIPTION("UDC Framework");
538 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
539 MODULE_LICENSE("GPL v2");
540