• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @vbus - for udcs who care about vbus status, this value is real vbus status;
39  * for udcs who do not care about vbus status, this value is always true
40  *
41  * This represents the internal data structure which is used by the UDC-class
42  * to hold information about udc driver and gadget together.
43  */
44 struct usb_udc {
45 	struct usb_gadget_driver	*driver;
46 	struct usb_gadget		*gadget;
47 	struct device			dev;
48 	struct list_head		list;
49 	bool				vbus;
50 };
51 
52 static struct class *udc_class;
53 static LIST_HEAD(udc_list);
54 static DEFINE_MUTEX(udc_lock);
55 
56 /* ------------------------------------------------------------------------- */
57 
58 #ifdef	CONFIG_HAS_DMA
59 
usb_gadget_map_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)60 int usb_gadget_map_request(struct usb_gadget *gadget,
61 		struct usb_request *req, int is_in)
62 {
63 	struct device *dev = gadget->dev.parent;
64 
65 	if (req->length == 0)
66 		return 0;
67 
68 	if (req->num_sgs) {
69 		int     mapped;
70 
71 		mapped = dma_map_sg(dev, req->sg, req->num_sgs,
72 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 		if (mapped == 0) {
74 			dev_err(dev, "failed to map SGs\n");
75 			return -EFAULT;
76 		}
77 
78 		req->num_mapped_sgs = mapped;
79 	} else {
80 		req->dma = dma_map_single(dev, req->buf, req->length,
81 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
82 
83 		if (dma_mapping_error(dev, req->dma)) {
84 			dev_err(dev, "failed to map buffer\n");
85 			return -EFAULT;
86 		}
87 	}
88 
89 	return 0;
90 }
91 EXPORT_SYMBOL_GPL(usb_gadget_map_request);
92 
usb_gadget_unmap_request(struct usb_gadget * gadget,struct usb_request * req,int is_in)93 void usb_gadget_unmap_request(struct usb_gadget *gadget,
94 		struct usb_request *req, int is_in)
95 {
96 	if (req->length == 0)
97 		return;
98 
99 	if (req->num_mapped_sgs) {
100 		dma_unmap_sg(gadget->dev.parent, req->sg, req->num_sgs,
101 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
102 
103 		req->num_mapped_sgs = 0;
104 	} else {
105 		dma_unmap_single(gadget->dev.parent, req->dma, req->length,
106 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
107 	}
108 }
109 EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
110 
111 #endif	/* CONFIG_HAS_DMA */
112 
113 /* ------------------------------------------------------------------------- */
114 
115 /**
116  * usb_gadget_giveback_request - give the request back to the gadget layer
117  * Context: in_interrupt()
118  *
119  * This is called by device controller drivers in order to return the
120  * completed request back to the gadget layer.
121  */
usb_gadget_giveback_request(struct usb_ep * ep,struct usb_request * req)122 void usb_gadget_giveback_request(struct usb_ep *ep,
123 		struct usb_request *req)
124 {
125 	if (likely(req->status == 0))
126 		usb_led_activity(USB_LED_EVENT_GADGET);
127 
128 	req->complete(ep, req);
129 }
130 EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
131 
132 /* ------------------------------------------------------------------------- */
133 
134 /**
135  * gadget_find_ep_by_name - returns ep whose name is the same as sting passed
136  *	in second parameter or NULL if searched endpoint not found
137  * @g: controller to check for quirk
138  * @name: name of searched endpoint
139  */
gadget_find_ep_by_name(struct usb_gadget * g,const char * name)140 struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
141 {
142 	struct usb_ep *ep;
143 
144 	gadget_for_each_ep(ep, g) {
145 		if (!strcmp(ep->name, name))
146 			return ep;
147 	}
148 
149 	return NULL;
150 }
151 EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
152 
153 /* ------------------------------------------------------------------------- */
154 
usb_gadget_ep_match_desc(struct usb_gadget * gadget,struct usb_ep * ep,struct usb_endpoint_descriptor * desc,struct usb_ss_ep_comp_descriptor * ep_comp)155 int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
156 		struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
157 		struct usb_ss_ep_comp_descriptor *ep_comp)
158 {
159 	u8		type;
160 	u16		max;
161 	int		num_req_streams = 0;
162 
163 	/* endpoint already claimed? */
164 	if (ep->claimed)
165 		return 0;
166 
167 	type = usb_endpoint_type(desc);
168 	max = 0x7ff & usb_endpoint_maxp(desc);
169 
170 	if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
171 		return 0;
172 	if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
173 		return 0;
174 
175 	if (max > ep->maxpacket_limit)
176 		return 0;
177 
178 	/* "high bandwidth" works only at high speed */
179 	if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp(desc) & (3<<11))
180 		return 0;
181 
182 	switch (type) {
183 	case USB_ENDPOINT_XFER_CONTROL:
184 		/* only support ep0 for portable CONTROL traffic */
185 		return 0;
186 	case USB_ENDPOINT_XFER_ISOC:
187 		if (!ep->caps.type_iso)
188 			return 0;
189 		/* ISO:  limit 1023 bytes full speed, 1024 high/super speed */
190 		if (!gadget_is_dualspeed(gadget) && max > 1023)
191 			return 0;
192 		break;
193 	case USB_ENDPOINT_XFER_BULK:
194 		if (!ep->caps.type_bulk)
195 			return 0;
196 		if (ep_comp && gadget_is_superspeed(gadget)) {
197 			/* Get the number of required streams from the
198 			 * EP companion descriptor and see if the EP
199 			 * matches it
200 			 */
201 			num_req_streams = ep_comp->bmAttributes & 0x1f;
202 			if (num_req_streams > ep->max_streams)
203 				return 0;
204 		}
205 		break;
206 	case USB_ENDPOINT_XFER_INT:
207 		/* Bulk endpoints handle interrupt transfers,
208 		 * except the toggle-quirky iso-synch kind
209 		 */
210 		if (!ep->caps.type_int && !ep->caps.type_bulk)
211 			return 0;
212 		/* INT:  limit 64 bytes full speed, 1024 high/super speed */
213 		if (!gadget_is_dualspeed(gadget) && max > 64)
214 			return 0;
215 		break;
216 	}
217 
218 	return 1;
219 }
220 EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
221 
222 /* ------------------------------------------------------------------------- */
223 
usb_gadget_state_work(struct work_struct * work)224 static void usb_gadget_state_work(struct work_struct *work)
225 {
226 	struct usb_gadget *gadget = work_to_gadget(work);
227 	struct usb_udc *udc = gadget->udc;
228 
229 	if (udc)
230 		sysfs_notify(&udc->dev.kobj, NULL, "state");
231 }
232 
usb_gadget_set_state(struct usb_gadget * gadget,enum usb_device_state state)233 void usb_gadget_set_state(struct usb_gadget *gadget,
234 		enum usb_device_state state)
235 {
236 	gadget->state = state;
237 	schedule_work(&gadget->work);
238 }
239 EXPORT_SYMBOL_GPL(usb_gadget_set_state);
240 
241 /* ------------------------------------------------------------------------- */
242 
usb_udc_connect_control(struct usb_udc * udc)243 static void usb_udc_connect_control(struct usb_udc *udc)
244 {
245 	if (udc->vbus)
246 		usb_gadget_connect(udc->gadget);
247 	else
248 		usb_gadget_disconnect(udc->gadget);
249 }
250 
251 /**
252  * usb_udc_vbus_handler - updates the udc core vbus status, and try to
253  * connect or disconnect gadget
254  * @gadget: The gadget which vbus change occurs
255  * @status: The vbus status
256  *
257  * The udc driver calls it when it wants to connect or disconnect gadget
258  * according to vbus status.
259  */
usb_udc_vbus_handler(struct usb_gadget * gadget,bool status)260 void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
261 {
262 	struct usb_udc *udc = gadget->udc;
263 
264 	if (udc) {
265 		udc->vbus = status;
266 		usb_udc_connect_control(udc);
267 	}
268 }
269 EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
270 
271 /**
272  * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
273  * @gadget: The gadget which bus reset occurs
274  * @driver: The gadget driver we want to notify
275  *
276  * If the udc driver has bus reset handler, it needs to call this when the bus
277  * reset occurs, it notifies the gadget driver that the bus reset occurs as
278  * well as updates gadget state.
279  */
usb_gadget_udc_reset(struct usb_gadget * gadget,struct usb_gadget_driver * driver)280 void usb_gadget_udc_reset(struct usb_gadget *gadget,
281 		struct usb_gadget_driver *driver)
282 {
283 	driver->reset(gadget);
284 	usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
285 }
286 EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
287 
288 /**
289  * usb_gadget_udc_start - tells usb device controller to start up
290  * @udc: The UDC to be started
291  *
292  * This call is issued by the UDC Class driver when it's about
293  * to register a gadget driver to the device controller, before
294  * calling gadget driver's bind() method.
295  *
296  * It allows the controller to be powered off until strictly
297  * necessary to have it powered on.
298  *
299  * Returns zero on success, else negative errno.
300  */
usb_gadget_udc_start(struct usb_udc * udc)301 static inline int usb_gadget_udc_start(struct usb_udc *udc)
302 {
303 	return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
304 }
305 
306 /**
307  * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
308  * @gadget: The device we want to stop activity
309  * @driver: The driver to unbind from @gadget
310  *
311  * This call is issued by the UDC Class driver after calling
312  * gadget driver's unbind() method.
313  *
314  * The details are implementation specific, but it can go as
315  * far as powering off UDC completely and disable its data
316  * line pullups.
317  */
usb_gadget_udc_stop(struct usb_udc * udc)318 static inline void usb_gadget_udc_stop(struct usb_udc *udc)
319 {
320 	udc->gadget->ops->udc_stop(udc->gadget);
321 }
322 
323 /**
324  * usb_udc_release - release the usb_udc struct
325  * @dev: the dev member within usb_udc
326  *
327  * This is called by driver's core in order to free memory once the last
328  * reference is released.
329  */
usb_udc_release(struct device * dev)330 static void usb_udc_release(struct device *dev)
331 {
332 	struct usb_udc *udc;
333 
334 	udc = container_of(dev, struct usb_udc, dev);
335 	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
336 	kfree(udc);
337 }
338 
339 static const struct attribute_group *usb_udc_attr_groups[];
340 
usb_udc_nop_release(struct device * dev)341 static void usb_udc_nop_release(struct device *dev)
342 {
343 	dev_vdbg(dev, "%s\n", __func__);
344 }
345 
346 /**
347  * usb_add_gadget_udc_release - adds a new gadget to the udc class driver list
348  * @parent: the parent device to this udc. Usually the controller driver's
349  * device.
350  * @gadget: the gadget to be added to the list.
351  * @release: a gadget release function.
352  *
353  * Returns zero on success, negative errno otherwise.
354  */
usb_add_gadget_udc_release(struct device * parent,struct usb_gadget * gadget,void (* release)(struct device * dev))355 int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
356 		void (*release)(struct device *dev))
357 {
358 	struct usb_udc		*udc;
359 	int			ret = -ENOMEM;
360 
361 	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
362 	if (!udc)
363 		goto err1;
364 
365 	dev_set_name(&gadget->dev, "gadget");
366 	INIT_WORK(&gadget->work, usb_gadget_state_work);
367 	gadget->dev.parent = parent;
368 
369 #ifdef	CONFIG_HAS_DMA
370 	dma_set_coherent_mask(&gadget->dev, parent->coherent_dma_mask);
371 	gadget->dev.dma_parms = parent->dma_parms;
372 	gadget->dev.dma_mask = parent->dma_mask;
373 #endif
374 
375 	if (release)
376 		gadget->dev.release = release;
377 	else
378 		gadget->dev.release = usb_udc_nop_release;
379 
380 	ret = device_register(&gadget->dev);
381 	if (ret)
382 		goto err2;
383 
384 	device_initialize(&udc->dev);
385 	udc->dev.release = usb_udc_release;
386 	udc->dev.class = udc_class;
387 	udc->dev.groups = usb_udc_attr_groups;
388 	udc->dev.parent = parent;
389 	ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
390 	if (ret)
391 		goto err3;
392 
393 	udc->gadget = gadget;
394 	gadget->udc = udc;
395 
396 	mutex_lock(&udc_lock);
397 	list_add_tail(&udc->list, &udc_list);
398 
399 	ret = device_add(&udc->dev);
400 	if (ret)
401 		goto err4;
402 
403 	usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
404 	udc->vbus = true;
405 
406 	mutex_unlock(&udc_lock);
407 
408 	return 0;
409 
410 err4:
411 	list_del(&udc->list);
412 	mutex_unlock(&udc_lock);
413 
414 err3:
415 	put_device(&udc->dev);
416 	device_del(&gadget->dev);
417 
418 err2:
419 	put_device(&gadget->dev);
420 	kfree(udc);
421 
422 err1:
423 	return ret;
424 }
425 EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
426 
427 /**
428  * usb_add_gadget_udc - adds a new gadget to the udc class driver list
429  * @parent: the parent device to this udc. Usually the controller
430  * driver's device.
431  * @gadget: the gadget to be added to the list
432  *
433  * Returns zero on success, negative errno otherwise.
434  */
usb_add_gadget_udc(struct device * parent,struct usb_gadget * gadget)435 int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
436 {
437 	return usb_add_gadget_udc_release(parent, gadget, NULL);
438 }
439 EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
440 
usb_gadget_remove_driver(struct usb_udc * udc)441 static void usb_gadget_remove_driver(struct usb_udc *udc)
442 {
443 	dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
444 			udc->driver->function);
445 
446 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
447 
448 	usb_gadget_disconnect(udc->gadget);
449 	udc->driver->disconnect(udc->gadget);
450 	udc->driver->unbind(udc->gadget);
451 	usb_gadget_udc_stop(udc);
452 
453 	udc->driver = NULL;
454 	udc->dev.driver = NULL;
455 	udc->gadget->dev.driver = NULL;
456 }
457 
458 /**
459  * usb_del_gadget_udc - deletes @udc from udc_list
460  * @gadget: the gadget to be removed.
461  *
462  * This, will call usb_gadget_unregister_driver() if
463  * the @udc is still busy.
464  */
usb_del_gadget_udc(struct usb_gadget * gadget)465 void usb_del_gadget_udc(struct usb_gadget *gadget)
466 {
467 	struct usb_udc *udc = gadget->udc;
468 
469 	if (!udc)
470 		return;
471 
472 	dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
473 
474 	mutex_lock(&udc_lock);
475 	list_del(&udc->list);
476 	mutex_unlock(&udc_lock);
477 
478 	if (udc->driver)
479 		usb_gadget_remove_driver(udc);
480 
481 	kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
482 	flush_work(&gadget->work);
483 	device_unregister(&udc->dev);
484 	device_unregister(&gadget->dev);
485 }
486 EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
487 
488 /* ------------------------------------------------------------------------- */
489 
udc_bind_to_driver(struct usb_udc * udc,struct usb_gadget_driver * driver)490 static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
491 {
492 	int ret;
493 
494 	dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
495 			driver->function);
496 
497 	udc->driver = driver;
498 	udc->dev.driver = &driver->driver;
499 	udc->gadget->dev.driver = &driver->driver;
500 
501 	ret = driver->bind(udc->gadget, driver);
502 	if (ret)
503 		goto err1;
504 	ret = usb_gadget_udc_start(udc);
505 	if (ret) {
506 		driver->unbind(udc->gadget);
507 		goto err1;
508 	}
509 	usb_udc_connect_control(udc);
510 
511 	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
512 	return 0;
513 err1:
514 	if (ret != -EISNAM)
515 		dev_err(&udc->dev, "failed to start %s: %d\n",
516 			udc->driver->function, ret);
517 	udc->driver = NULL;
518 	udc->dev.driver = NULL;
519 	udc->gadget->dev.driver = NULL;
520 	return ret;
521 }
522 
usb_udc_attach_driver(const char * name,struct usb_gadget_driver * driver)523 int usb_udc_attach_driver(const char *name, struct usb_gadget_driver *driver)
524 {
525 	struct usb_udc *udc = NULL;
526 	int ret = -ENODEV;
527 
528 	mutex_lock(&udc_lock);
529 	list_for_each_entry(udc, &udc_list, list) {
530 		ret = strcmp(name, dev_name(&udc->dev));
531 		if (!ret)
532 			break;
533 	}
534 	if (ret) {
535 		ret = -ENODEV;
536 		goto out;
537 	}
538 	if (udc->driver) {
539 		ret = -EBUSY;
540 		goto out;
541 	}
542 	ret = udc_bind_to_driver(udc, driver);
543 out:
544 	mutex_unlock(&udc_lock);
545 	return ret;
546 }
547 EXPORT_SYMBOL_GPL(usb_udc_attach_driver);
548 
usb_gadget_probe_driver(struct usb_gadget_driver * driver)549 int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
550 {
551 	struct usb_udc		*udc = NULL;
552 	int			ret;
553 
554 	if (!driver || !driver->bind || !driver->setup)
555 		return -EINVAL;
556 
557 	mutex_lock(&udc_lock);
558 	list_for_each_entry(udc, &udc_list, list) {
559 		/* For now we take the first one */
560 		if (!udc->driver)
561 			goto found;
562 	}
563 
564 	pr_debug("couldn't find an available UDC\n");
565 	mutex_unlock(&udc_lock);
566 	return -ENODEV;
567 found:
568 	ret = udc_bind_to_driver(udc, driver);
569 	mutex_unlock(&udc_lock);
570 	return ret;
571 }
572 EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
573 
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)574 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
575 {
576 	struct usb_udc		*udc = NULL;
577 	int			ret = -ENODEV;
578 
579 	if (!driver || !driver->unbind)
580 		return -EINVAL;
581 
582 	mutex_lock(&udc_lock);
583 	list_for_each_entry(udc, &udc_list, list)
584 		if (udc->driver == driver) {
585 			usb_gadget_remove_driver(udc);
586 			usb_gadget_set_state(udc->gadget,
587 					USB_STATE_NOTATTACHED);
588 			ret = 0;
589 			break;
590 		}
591 
592 	mutex_unlock(&udc_lock);
593 	return ret;
594 }
595 EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
596 
597 /* ------------------------------------------------------------------------- */
598 
usb_udc_srp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)599 static ssize_t usb_udc_srp_store(struct device *dev,
600 		struct device_attribute *attr, const char *buf, size_t n)
601 {
602 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
603 
604 	if (sysfs_streq(buf, "1"))
605 		usb_gadget_wakeup(udc->gadget);
606 
607 	return n;
608 }
609 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
610 
usb_udc_softconn_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)611 static ssize_t usb_udc_softconn_store(struct device *dev,
612 		struct device_attribute *attr, const char *buf, size_t n)
613 {
614 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
615 	ssize_t			ret;
616 
617 	mutex_lock(&udc_lock);
618 	if (!udc->driver) {
619 		dev_err(dev, "soft-connect without a gadget driver\n");
620 		ret = -EOPNOTSUPP;
621 		goto out;
622 	}
623 
624 	if (sysfs_streq(buf, "connect")) {
625 		usb_gadget_udc_start(udc);
626 		usb_gadget_connect(udc->gadget);
627 	} else if (sysfs_streq(buf, "disconnect")) {
628 		usb_gadget_disconnect(udc->gadget);
629 		udc->driver->disconnect(udc->gadget);
630 		usb_gadget_udc_stop(udc);
631 	} else {
632 		dev_err(dev, "unsupported command '%s'\n", buf);
633 		ret = -EINVAL;
634 		goto out;
635 	}
636 
637 	ret = n;
638 out:
639 	mutex_unlock(&udc_lock);
640 	return ret;
641 }
642 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
643 
state_show(struct device * dev,struct device_attribute * attr,char * buf)644 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
645 			  char *buf)
646 {
647 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
648 	struct usb_gadget	*gadget = udc->gadget;
649 
650 	return sprintf(buf, "%s\n", usb_state_string(gadget->state));
651 }
652 static DEVICE_ATTR_RO(state);
653 
654 #define USB_UDC_SPEED_ATTR(name, param)					\
655 ssize_t name##_show(struct device *dev,					\
656 		struct device_attribute *attr, char *buf)		\
657 {									\
658 	struct usb_udc *udc = container_of(dev, struct usb_udc, dev);	\
659 	return snprintf(buf, PAGE_SIZE, "%s\n",				\
660 			usb_speed_string(udc->gadget->param));		\
661 }									\
662 static DEVICE_ATTR_RO(name)
663 
664 static USB_UDC_SPEED_ATTR(current_speed, speed);
665 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
666 
667 #define USB_UDC_ATTR(name)					\
668 ssize_t name##_show(struct device *dev,				\
669 		struct device_attribute *attr, char *buf)	\
670 {								\
671 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev); \
672 	struct usb_gadget	*gadget = udc->gadget;		\
673 								\
674 	return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name);	\
675 }								\
676 static DEVICE_ATTR_RO(name)
677 
678 static USB_UDC_ATTR(is_otg);
679 static USB_UDC_ATTR(is_a_peripheral);
680 static USB_UDC_ATTR(b_hnp_enable);
681 static USB_UDC_ATTR(a_hnp_support);
682 static USB_UDC_ATTR(a_alt_hnp_support);
683 static USB_UDC_ATTR(is_selfpowered);
684 
685 static struct attribute *usb_udc_attrs[] = {
686 	&dev_attr_srp.attr,
687 	&dev_attr_soft_connect.attr,
688 	&dev_attr_state.attr,
689 	&dev_attr_current_speed.attr,
690 	&dev_attr_maximum_speed.attr,
691 
692 	&dev_attr_is_otg.attr,
693 	&dev_attr_is_a_peripheral.attr,
694 	&dev_attr_b_hnp_enable.attr,
695 	&dev_attr_a_hnp_support.attr,
696 	&dev_attr_a_alt_hnp_support.attr,
697 	&dev_attr_is_selfpowered.attr,
698 	NULL,
699 };
700 
701 static const struct attribute_group usb_udc_attr_group = {
702 	.attrs = usb_udc_attrs,
703 };
704 
705 static const struct attribute_group *usb_udc_attr_groups[] = {
706 	&usb_udc_attr_group,
707 	NULL,
708 };
709 
usb_udc_uevent(struct device * dev,struct kobj_uevent_env * env)710 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
711 {
712 	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
713 	int			ret;
714 
715 	ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
716 	if (ret) {
717 		dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
718 		return ret;
719 	}
720 
721 	if (udc->driver) {
722 		ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
723 				udc->driver->function);
724 		if (ret) {
725 			dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
726 			return ret;
727 		}
728 	}
729 
730 	return 0;
731 }
732 
usb_udc_init(void)733 static int __init usb_udc_init(void)
734 {
735 	udc_class = class_create(THIS_MODULE, "udc");
736 	if (IS_ERR(udc_class)) {
737 		pr_err("failed to create udc class --> %ld\n",
738 				PTR_ERR(udc_class));
739 		return PTR_ERR(udc_class);
740 	}
741 
742 	udc_class->dev_uevent = usb_udc_uevent;
743 	return 0;
744 }
745 subsys_initcall(usb_udc_init);
746 
usb_udc_exit(void)747 static void __exit usb_udc_exit(void)
748 {
749 	class_destroy(udc_class);
750 }
751 module_exit(usb_udc_exit);
752 
753 MODULE_DESCRIPTION("UDC Framework");
754 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
755 MODULE_LICENSE("GPL v2");
756