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 #include <linux/workqueue.h>
27
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb.h>
31
32 /**
33 * struct usb_udc - describes one usb device controller
34 * @driver - the gadget driver pointer. For use by the class code
35 * @dev - the child device to the actual controller
36 * @gadget - the gadget. For use by the class code
37 * @list - for use by the udc class driver
38 *
39 * This represents the internal data structure which is used by the UDC-class
40 * to hold information about udc driver and gadget together.
41 */
42 struct usb_udc {
43 struct usb_gadget_driver *driver;
44 struct usb_gadget *gadget;
45 struct device dev;
46 struct list_head list;
47 };
48
49 static struct class *udc_class;
50 static LIST_HEAD(udc_list);
51 static DEFINE_MUTEX(udc_lock);
52
53 /* ------------------------------------------------------------------------- */
54
55 #ifdef CONFIG_HAS_DMA
56
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)57 int usb_gadget_map_request(struct usb_gadget *gadget,
58 struct usb_request *req, int is_in)
59 {
60 if (req->length == 0)
61 return 0;
62
63 if (req->num_sgs) {
64 int mapped;
65
66 mapped = dma_map_sg(&gadget->dev, req->sg, req->num_sgs,
67 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
68 if (mapped == 0) {
69 dev_err(&gadget->dev, "failed to map SGs\n");
70 return -EFAULT;
71 }
72
73 req->num_mapped_sgs = mapped;
74 } else {
75 req->dma = dma_map_single(&gadget->dev, req->buf, req->length,
76 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
77
78 if (dma_mapping_error(&gadget->dev, req->dma)) {
79 dev_err(&gadget->dev, "failed to map buffer\n");
80 return -EFAULT;
81 }
82 }
83
84 return 0;
85 }
86 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
87
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)88 void usb_gadget_unmap_request(struct usb_gadget *gadget,
89 struct usb_request *req, int is_in)
90 {
91 if (req->length == 0)
92 return;
93
94 if (req->num_mapped_sgs) {
95 dma_unmap_sg(&gadget->dev, req->sg, req->num_mapped_sgs,
96 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
97
98 req->num_mapped_sgs = 0;
99 } else {
100 dma_unmap_single(&gadget->dev, req->dma, req->length,
101 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
102 }
103 }
104 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
105
106 #endif /* CONFIG_HAS_DMA */
107
108 /* ------------------------------------------------------------------------- */
109
110 /**
111 * usb_gadget_giveback_request - give the request back to the gadget layer
112 * Context: in_interrupt()
113 *
114 * This is called by device controller drivers in order to return the
115 * completed request back to the gadget layer.
116 */
usb_gadget_giveback_request(struct usb_ep * ep,struct usb_request * req)117 void usb_gadget_giveback_request(struct usb_ep *ep,
118 struct usb_request *req)
119 {
120 if (likely(req->status == 0))
121 usb_led_activity(USB_LED_EVENT_GADGET);
122
123 req->complete(ep, req);
124 }
125 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
126
127 /* ------------------------------------------------------------------------- */
128
usb_gadget_state_work(struct work_struct * work)129 static void usb_gadget_state_work(struct work_struct *work)
130 {
131 struct usb_gadget *gadget = work_to_gadget(work);
132 struct usb_udc *udc = NULL;
133
134 mutex_lock(&udc_lock);
135 list_for_each_entry(udc, &udc_list, list)
136 if (udc->gadget == gadget)
137 goto found;
138 mutex_unlock(&udc_lock);
139
140 return;
141
142 found:
143 mutex_unlock(&udc_lock);
144
145 sysfs_notify(&udc->dev.kobj, NULL, "state");
146 }
147
usb_gadget_set_state(struct usb_gadget * gadget,enum usb_device_state state)148 void usb_gadget_set_state(struct usb_gadget *gadget,
149 enum usb_device_state state)
150 {
151 gadget->state = state;
152 schedule_work(&gadget->work);
153 }
154 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
155
156 /* ------------------------------------------------------------------------- */
157
158 /**
159 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
160 * @gadget: The gadget which bus reset occurs
161 * @driver: The gadget driver we want to notify
162 *
163 * If the udc driver has bus reset handler, it needs to call this when the bus
164 * reset occurs, it notifies the gadget driver that the bus reset occurs as
165 * well as updates gadget state.
166 */
usb_gadget_udc_reset(struct usb_gadget * gadget,struct usb_gadget_driver * driver)167 void usb_gadget_udc_reset(struct usb_gadget *gadget,
168 struct usb_gadget_driver *driver)
169 {
170 driver->reset(gadget);
171 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
172 }
173 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
174
175 /**
176 * usb_gadget_udc_start - tells usb device controller to start up
177 * @gadget: The gadget we want to get started
178 * @driver: The driver we want to bind to @gadget
179 *
180 * This call is issued by the UDC Class driver when it's about
181 * to register a gadget driver to the device controller, before
182 * calling gadget driver's bind() method.
183 *
184 * It allows the controller to be powered off until strictly
185 * necessary to have it powered on.
186 *
187 * Returns zero on success, else negative errno.
188 */
usb_gadget_udc_start(struct usb_gadget * gadget,struct usb_gadget_driver * driver)189 static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
190 struct usb_gadget_driver *driver)
191 {
192 return gadget->ops->udc_start(gadget, driver);
193 }
194
195 /**
196 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
197 * @gadget: The device we want to stop activity
198 * @driver: The driver to unbind from @gadget
199 *
200 * This call is issued by the UDC Class driver after calling
201 * gadget driver's unbind() method.
202 *
203 * The details are implementation specific, but it can go as
204 * far as powering off UDC completely and disable its data
205 * line pullups.
206 */
usb_gadget_udc_stop(struct usb_gadget * gadget,struct usb_gadget_driver * driver)207 static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
208 struct usb_gadget_driver *driver)
209 {
210 gadget->ops->udc_stop(gadget, driver);
211 }
212
213 /**
214 * usb_udc_release - release the usb_udc struct
215 * @dev: the dev member within usb_udc
216 *
217 * This is called by driver's core in order to free memory once the last
218 * reference is released.
219 */
usb_udc_release(struct device * dev)220 static void usb_udc_release(struct device *dev)
221 {
222 struct usb_udc *udc;
223
224 udc = container_of(dev, struct usb_udc, dev);
225 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
226 kfree(udc);
227 }
228
229 static const struct attribute_group *usb_udc_attr_groups[];
230
usb_udc_nop_release(struct device * dev)231 static void usb_udc_nop_release(struct device *dev)
232 {
233 dev_vdbg(dev, "%s\n", __func__);
234 }
235
236 /**
237 * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
238 * @parent: the parent device to this udc. Usually the controller driver's
239 * device.
240 * @gadget: the gadget to be added to the list.
241 * @release: a gadget release function.
242 *
243 * Returns zero on success, negative errno otherwise.
244 */
usb_add_gadget_udc_release(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))245 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
246 void (*release)(struct device *dev))
247 {
248 struct usb_udc *udc;
249 int ret = -ENOMEM;
250
251 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
252 if (!udc)
253 goto err1;
254
255 dev_set_name(&gadget->dev, "gadget");
256 INIT_WORK(&gadget->work, usb_gadget_state_work);
257 gadget->dev.parent = parent;
258
259 #ifdef CONFIG_HAS_DMA
260 dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
261 gadget->dev.dma_parms = parent->dma_parms;
262 gadget->dev.dma_mask = parent->dma_mask;
263 #endif
264
265 if (release)
266 gadget->dev.release = release;
267 else
268 gadget->dev.release = usb_udc_nop_release;
269
270 ret = device_register(&gadget->dev);
271 if (ret)
272 goto err2;
273
274 device_initialize(&udc->dev);
275 udc->dev.release = usb_udc_release;
276 udc->dev.class = udc_class;
277 udc->dev.groups = usb_udc_attr_groups;
278 udc->dev.parent = parent;
279 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
280 if (ret)
281 goto err3;
282
283 udc->gadget = gadget;
284
285 mutex_lock(&udc_lock);
286 list_add_tail(&udc->list, &udc_list);
287
288 ret = device_add(&udc->dev);
289 if (ret)
290 goto err4;
291
292 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
293
294 mutex_unlock(&udc_lock);
295
296 return 0;
297
298 err4:
299 list_del(&udc->list);
300 mutex_unlock(&udc_lock);
301
302 err3:
303 put_device(&udc->dev);
304 device_del(&gadget->dev);
305
306 err2:
307 put_device(&gadget->dev);
308 kfree(udc);
309
310 err1:
311 return ret;
312 }
313 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
314
315 /**
316 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
317 * @parent: the parent device to this udc. Usually the controller
318 * driver's device.
319 * @gadget: the gadget to be added to the list
320 *
321 * Returns zero on success, negative errno otherwise.
322 */
usb_add_gadget_udc(struct device * parent,struct usb_gadget * gadget)323 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
324 {
325 return usb_add_gadget_udc_release(parent, gadget, NULL);
326 }
327 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
328
usb_gadget_remove_driver(struct usb_udc * udc)329 static void usb_gadget_remove_driver(struct usb_udc *udc)
330 {
331 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
332 udc->gadget->name);
333
334 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
335
336 usb_gadget_disconnect(udc->gadget);
337 udc->driver->disconnect(udc->gadget);
338 udc->driver->unbind(udc->gadget);
339 usb_gadget_udc_stop(udc->gadget, NULL);
340
341 udc->driver = NULL;
342 udc->dev.driver = NULL;
343 udc->gadget->dev.driver = NULL;
344 }
345
346 /**
347 * usb_del_gadget_udc - deletes @udc from udc_list
348 * @gadget: the gadget to be removed.
349 *
350 * This, will call usb_gadget_unregister_driver() if
351 * the @udc is still busy.
352 */
usb_del_gadget_udc(struct usb_gadget * gadget)353 void usb_del_gadget_udc(struct usb_gadget *gadget)
354 {
355 struct usb_udc *udc = NULL;
356
357 mutex_lock(&udc_lock);
358 list_for_each_entry(udc, &udc_list, list)
359 if (udc->gadget == gadget)
360 goto found;
361
362 dev_err(gadget->dev.parent, "gadget not registered.\n");
363 mutex_unlock(&udc_lock);
364
365 return;
366
367 found:
368 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
369
370 list_del(&udc->list);
371 mutex_unlock(&udc_lock);
372
373 if (udc->driver)
374 usb_gadget_remove_driver(udc);
375
376 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
377 flush_work(&gadget->work);
378 device_unregister(&udc->dev);
379 device_unregister(&gadget->dev);
380 }
381 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
382
383 /* ------------------------------------------------------------------------- */
384
udc_bind_to_driver(struct usb_udc * udc,struct usb_gadget_driver * driver)385 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
386 {
387 int ret;
388
389 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
390 driver->function);
391
392 udc->driver = driver;
393 udc->dev.driver = &driver->driver;
394 udc->gadget->dev.driver = &driver->driver;
395
396 ret = driver->bind(udc->gadget, driver);
397 if (ret)
398 goto err1;
399 ret = usb_gadget_udc_start(udc->gadget, driver);
400 if (ret) {
401 driver->unbind(udc->gadget);
402 goto err1;
403 }
404 usb_gadget_connect(udc->gadget);
405
406 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
407 return 0;
408 err1:
409 if (ret != -EISNAM)
410 dev_err(&udc->dev, "failed to start %s: %d\n",
411 udc->driver->function, ret);
412 udc->driver = NULL;
413 udc->dev.driver = NULL;
414 udc->gadget->dev.driver = NULL;
415 return ret;
416 }
417
udc_attach_driver(const char * name,struct usb_gadget_driver * driver)418 int udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
419 {
420 struct usb_udc *udc = NULL;
421 int ret = -ENODEV;
422
423 mutex_lock(&udc_lock);
424 list_for_each_entry(udc, &udc_list, list) {
425 ret = strcmp(name, dev_name(&udc->dev));
426 if (!ret)
427 break;
428 }
429 if (ret) {
430 ret = -ENODEV;
431 goto out;
432 }
433 if (udc->driver) {
434 ret = -EBUSY;
435 goto out;
436 }
437 ret = udc_bind_to_driver(udc, driver);
438 out:
439 mutex_unlock(&udc_lock);
440 return ret;
441 }
442 EXPORT_SYMBOL_GPL(udc_attach_driver);
443
usb_gadget_probe_driver(struct usb_gadget_driver * driver)444 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
445 {
446 struct usb_udc *udc = NULL;
447 int ret;
448
449 if (!driver || !driver->bind || !driver->setup)
450 return -EINVAL;
451
452 mutex_lock(&udc_lock);
453 list_for_each_entry(udc, &udc_list, list) {
454 /* For now we take the first one */
455 if (!udc->driver)
456 goto found;
457 }
458
459 pr_debug("couldn't find an available UDC\n");
460 mutex_unlock(&udc_lock);
461 return -ENODEV;
462 found:
463 ret = udc_bind_to_driver(udc, driver);
464 mutex_unlock(&udc_lock);
465 return ret;
466 }
467 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
468
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)469 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
470 {
471 struct usb_udc *udc = NULL;
472 int ret = -ENODEV;
473
474 if (!driver || !driver->unbind)
475 return -EINVAL;
476
477 mutex_lock(&udc_lock);
478 list_for_each_entry(udc, &udc_list, list)
479 if (udc->driver == driver) {
480 usb_gadget_remove_driver(udc);
481 usb_gadget_set_state(udc->gadget,
482 USB_STATE_NOTATTACHED);
483 ret = 0;
484 break;
485 }
486
487 mutex_unlock(&udc_lock);
488 return ret;
489 }
490 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
491
492 /* ------------------------------------------------------------------------- */
493
usb_udc_srp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)494 static ssize_t usb_udc_srp_store(struct device *dev,
495 struct device_attribute *attr, const char *buf, size_t n)
496 {
497 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
498
499 if (sysfs_streq(buf, "1"))
500 usb_gadget_wakeup(udc->gadget);
501
502 return n;
503 }
504 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
505
usb_udc_softconn_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)506 static ssize_t usb_udc_softconn_store(struct device *dev,
507 struct device_attribute *attr, const char *buf, size_t n)
508 {
509 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
510
511 if (!udc->driver) {
512 dev_err(dev, "soft-connect without a gadget driver\n");
513 return -EOPNOTSUPP;
514 }
515
516 if (sysfs_streq(buf, "connect")) {
517 usb_gadget_udc_start(udc->gadget, udc->driver);
518 usb_gadget_connect(udc->gadget);
519 } else if (sysfs_streq(buf, "disconnect")) {
520 usb_gadget_disconnect(udc->gadget);
521 usb_gadget_udc_stop(udc->gadget, udc->driver);
522 } else {
523 dev_err(dev, "unsupported command '%s'\n", buf);
524 return -EINVAL;
525 }
526
527 return n;
528 }
529 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
530
state_show(struct device * dev,struct device_attribute * attr,char * buf)531 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
532 char *buf)
533 {
534 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
535 struct usb_gadget *gadget = udc->gadget;
536
537 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
538 }
539 static DEVICE_ATTR_RO(state);
540
541 #define USB_UDC_SPEED_ATTR(name, param) \
542 ssize_t name##_show(struct device *dev, \
543 struct device_attribute *attr, char *buf) \
544 { \
545 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
546 return snprintf(buf, PAGE_SIZE, "%s\n", \
547 usb_speed_string(udc->gadget->param)); \
548 } \
549 static DEVICE_ATTR_RO(name)
550
551 static USB_UDC_SPEED_ATTR(current_speed, speed);
552 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
553
554 #define USB_UDC_ATTR(name) \
555 ssize_t name##_show(struct device *dev, \
556 struct device_attribute *attr, char *buf) \
557 { \
558 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
559 struct usb_gadget *gadget = udc->gadget; \
560 \
561 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
562 } \
563 static DEVICE_ATTR_RO(name)
564
565 static USB_UDC_ATTR(is_otg);
566 static USB_UDC_ATTR(is_a_peripheral);
567 static USB_UDC_ATTR(b_hnp_enable);
568 static USB_UDC_ATTR(a_hnp_support);
569 static USB_UDC_ATTR(a_alt_hnp_support);
570
571 static struct attribute *usb_udc_attrs[] = {
572 &dev_attr_srp.attr,
573 &dev_attr_soft_connect.attr,
574 &dev_attr_state.attr,
575 &dev_attr_current_speed.attr,
576 &dev_attr_maximum_speed.attr,
577
578 &dev_attr_is_otg.attr,
579 &dev_attr_is_a_peripheral.attr,
580 &dev_attr_b_hnp_enable.attr,
581 &dev_attr_a_hnp_support.attr,
582 &dev_attr_a_alt_hnp_support.attr,
583 NULL,
584 };
585
586 static const struct attribute_group usb_udc_attr_group = {
587 .attrs = usb_udc_attrs,
588 };
589
590 static const struct attribute_group *usb_udc_attr_groups[] = {
591 &usb_udc_attr_group,
592 NULL,
593 };
594
usb_udc_uevent(struct device * dev,struct kobj_uevent_env * env)595 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
596 {
597 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
598 int ret;
599
600 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
601 if (ret) {
602 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
603 return ret;
604 }
605
606 if (udc->driver) {
607 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
608 udc->driver->function);
609 if (ret) {
610 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
611 return ret;
612 }
613 }
614
615 return 0;
616 }
617
usb_udc_init(void)618 static int __init usb_udc_init(void)
619 {
620 udc_class = class_create(THIS_MODULE, "udc");
621 if (IS_ERR(udc_class)) {
622 pr_err("failed to create udc class --> %ld\n",
623 PTR_ERR(udc_class));
624 return PTR_ERR(udc_class);
625 }
626
627 udc_class->dev_uevent = usb_udc_uevent;
628 return 0;
629 }
630 subsys_initcall(usb_udc_init);
631
usb_udc_exit(void)632 static void __exit usb_udc_exit(void)
633 {
634 class_destroy(udc_class);
635 }
636 module_exit(usb_udc_exit);
637
638 MODULE_DESCRIPTION("UDC Framework");
639 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
640 MODULE_LICENSE("GPL v2");
641