Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.8.
  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
 26#include <linux/usb/ch9.h>
 27#include <linux/usb/gadget.h>
 28
 29/**
 30 * struct usb_udc - describes one usb device controller
 31 * @driver - the gadget driver pointer. For use by the class code
 32 * @dev - the child device to the actual controller
 33 * @gadget - the gadget. For use by the class code
 34 * @list - for use by the udc class driver
 35 *
 36 * This represents the internal data structure which is used by the UDC-class
 37 * to hold information about udc driver and gadget together.
 38 */
 39struct usb_udc {
 40	struct usb_gadget_driver	*driver;
 41	struct usb_gadget		*gadget;
 42	struct device			dev;
 43	struct list_head		list;
 44};
 45
 46static struct class *udc_class;
 47static LIST_HEAD(udc_list);
 48static DEFINE_MUTEX(udc_lock);
 49
 50/* ------------------------------------------------------------------------- */
 51
 52/**
 53 * usb_gadget_start - tells usb device controller to start up
 54 * @gadget: The gadget we want to get started
 55 * @driver: The driver we want to bind to @gadget
 56 * @bind: The bind function for @driver
 57 *
 58 * This call is issued by the UDC Class driver when it's about
 59 * to register a gadget driver to the device controller, before
 60 * calling gadget driver's bind() method.
 61 *
 62 * It allows the controller to be powered off until strictly
 63 * necessary to have it powered on.
 64 *
 65 * Returns zero on success, else negative errno.
 66 */
 67static inline int usb_gadget_start(struct usb_gadget *gadget,
 68		struct usb_gadget_driver *driver,
 69		int (*bind)(struct usb_gadget *))
 70{
 71	return gadget->ops->start(driver, bind);
 72}
 73
 74/**
 75 * usb_gadget_udc_start - tells usb device controller to start up
 76 * @gadget: The gadget we want to get started
 77 * @driver: The driver we want to bind to @gadget
 78 *
 79 * This call is issued by the UDC Class driver when it's about
 80 * to register a gadget driver to the device controller, before
 81 * calling gadget driver's bind() method.
 82 *
 83 * It allows the controller to be powered off until strictly
 84 * necessary to have it powered on.
 85 *
 86 * Returns zero on success, else negative errno.
 87 */
 88static inline int usb_gadget_udc_start(struct usb_gadget *gadget,
 89		struct usb_gadget_driver *driver)
 90{
 91	return gadget->ops->udc_start(gadget, driver);
 92}
 93
 94/**
 95 * usb_gadget_stop - tells usb device controller we don't need it anymore
 96 * @gadget: The device we want to stop activity
 97 * @driver: The driver to unbind from @gadget
 98 *
 99 * This call is issued by the UDC Class driver after calling
100 * gadget driver's unbind() method.
101 *
102 * The details are implementation specific, but it can go as
103 * far as powering off UDC completely and disable its data
104 * line pullups.
105 */
106static inline void usb_gadget_stop(struct usb_gadget *gadget,
107		struct usb_gadget_driver *driver)
108{
109	gadget->ops->stop(driver);
110}
111
112/**
113 * usb_gadget_udc_stop - tells usb device controller we don't need it anymore
114 * @gadget: The device we want to stop activity
115 * @driver: The driver to unbind from @gadget
116 *
117 * This call is issued by the UDC Class driver after calling
118 * gadget driver's unbind() method.
119 *
120 * The details are implementation specific, but it can go as
121 * far as powering off UDC completely and disable its data
122 * line pullups.
123 */
124static inline void usb_gadget_udc_stop(struct usb_gadget *gadget,
125		struct usb_gadget_driver *driver)
126{
127	gadget->ops->udc_stop(gadget, driver);
128}
129
130/**
131 * usb_udc_release - release the usb_udc struct
132 * @dev: the dev member within usb_udc
133 *
134 * This is called by driver's core in order to free memory once the last
135 * reference is released.
136 */
137static void usb_udc_release(struct device *dev)
138{
139	struct usb_udc *udc;
140
141	udc = container_of(dev, struct usb_udc, dev);
142	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
143	kfree(udc);
144}
145
146static const struct attribute_group *usb_udc_attr_groups[];
147/**
148 * usb_add_gadget_udc - adds a new gadget to the udc class driver list
149 * @parent: the parent device to this udc. Usually the controller
150 * driver's device.
151 * @gadget: the gadget to be added to the list
152 *
153 * Returns zero on success, negative errno otherwise.
154 */
155int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
156{
157	struct usb_udc		*udc;
158	int			ret = -ENOMEM;
159
160	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
161	if (!udc)
162		goto err1;
163
164	device_initialize(&udc->dev);
165	udc->dev.release = usb_udc_release;
166	udc->dev.class = udc_class;
167	udc->dev.groups = usb_udc_attr_groups;
168	udc->dev.parent = parent;
169	ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
170	if (ret)
171		goto err2;
172
173	udc->gadget = gadget;
174
175	mutex_lock(&udc_lock);
176	list_add_tail(&udc->list, &udc_list);
177
178	ret = device_add(&udc->dev);
179	if (ret)
180		goto err3;
181
182	mutex_unlock(&udc_lock);
183
184	return 0;
185err3:
186	list_del(&udc->list);
187	mutex_unlock(&udc_lock);
188
189err2:
190	put_device(&udc->dev);
191
192err1:
193	return ret;
194}
195EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
196
197static int udc_is_newstyle(struct usb_udc *udc)
198{
199	if (udc->gadget->ops->udc_start && udc->gadget->ops->udc_stop)
200		return 1;
201	return 0;
202}
203
204
205static void usb_gadget_remove_driver(struct usb_udc *udc)
206{
207	dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
208			udc->gadget->name);
209
210	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
211
212	if (udc_is_newstyle(udc)) {
213		usb_gadget_disconnect(udc->gadget);
214		udc->driver->unbind(udc->gadget);
215		usb_gadget_udc_stop(udc->gadget, udc->driver);
216
217	} else {
218		usb_gadget_stop(udc->gadget, udc->driver);
219	}
220
221	udc->driver = NULL;
222	udc->dev.driver = NULL;
223}
224
225/**
226 * usb_del_gadget_udc - deletes @udc from udc_list
227 * @gadget: the gadget to be removed.
228 *
229 * This, will call usb_gadget_unregister_driver() if
230 * the @udc is still busy.
231 */
232void usb_del_gadget_udc(struct usb_gadget *gadget)
233{
234	struct usb_udc		*udc = NULL;
235
236	mutex_lock(&udc_lock);
237	list_for_each_entry(udc, &udc_list, list)
238		if (udc->gadget == gadget)
239			goto found;
240
241	dev_err(gadget->dev.parent, "gadget not registered.\n");
242	mutex_unlock(&udc_lock);
243
244	return;
245
246found:
247	dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
248
249	list_del(&udc->list);
250	mutex_unlock(&udc_lock);
251
252	if (udc->driver)
253		usb_gadget_remove_driver(udc);
254
255	kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
256	device_unregister(&udc->dev);
257}
258EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
259
260/* ------------------------------------------------------------------------- */
261
262int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
263		int (*bind)(struct usb_gadget *))
264{
265	struct usb_udc		*udc = NULL;
266	int			ret;
267
268	if (!driver || !bind || !driver->setup)
269		return -EINVAL;
270
271	mutex_lock(&udc_lock);
272	list_for_each_entry(udc, &udc_list, list) {
273		/* For now we take the first one */
274		if (!udc->driver)
275			goto found;
276	}
277
278	pr_debug("couldn't find an available UDC\n");
279	mutex_unlock(&udc_lock);
280	return -ENODEV;
281
282found:
283	dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
284			driver->function);
285
286	udc->driver = driver;
287	udc->dev.driver = &driver->driver;
288
289	if (udc_is_newstyle(udc)) {
290		ret = bind(udc->gadget);
291		if (ret)
292			goto err1;
293		ret = usb_gadget_udc_start(udc->gadget, driver);
294		if (ret) {
295			driver->unbind(udc->gadget);
296			goto err1;
297		}
298		usb_gadget_connect(udc->gadget);
299	} else {
300
301		ret = usb_gadget_start(udc->gadget, driver, bind);
302		if (ret)
303			goto err1;
304
305	}
306
307	kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
308	mutex_unlock(&udc_lock);
309	return 0;
310
311err1:
312	dev_err(&udc->dev, "failed to start %s: %d\n",
313			udc->driver->function, ret);
314	udc->driver = NULL;
315	udc->dev.driver = NULL;
316	mutex_unlock(&udc_lock);
317	return ret;
318}
319EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
320
321int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
322{
323	struct usb_udc		*udc = NULL;
324	int			ret = -ENODEV;
325
326	if (!driver || !driver->unbind)
327		return -EINVAL;
328
329	mutex_lock(&udc_lock);
330	list_for_each_entry(udc, &udc_list, list)
331		if (udc->driver == driver) {
332			usb_gadget_remove_driver(udc);
333			ret = 0;
334			break;
335		}
336
337	mutex_unlock(&udc_lock);
338	return ret;
339}
340EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
341
342/* ------------------------------------------------------------------------- */
343
344static ssize_t usb_udc_srp_store(struct device *dev,
345		struct device_attribute *attr, const char *buf, size_t n)
346{
347	struct usb_udc		*udc = dev_get_drvdata(dev);
348
349	if (sysfs_streq(buf, "1"))
350		usb_gadget_wakeup(udc->gadget);
351
352	return n;
353}
354static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
355
356static ssize_t usb_udc_softconn_store(struct device *dev,
357		struct device_attribute *attr, const char *buf, size_t n)
358{
359	struct usb_udc		*udc = dev_get_drvdata(dev);
360
361	if (sysfs_streq(buf, "connect")) {
362		usb_gadget_connect(udc->gadget);
363	} else if (sysfs_streq(buf, "disconnect")) {
364		usb_gadget_disconnect(udc->gadget);
365	} else {
366		dev_err(dev, "unsupported command '%s'\n", buf);
367		return -EINVAL;
368	}
369
370	return n;
371}
372static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
373
374static ssize_t usb_udc_speed_show(struct device *dev,
375		struct device_attribute *attr, char *buf)
376{
377	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
378	struct usb_gadget	*gadget = udc->gadget;
379
380	switch (gadget->speed) {
381	case USB_SPEED_LOW:
382		return snprintf(buf, PAGE_SIZE, "low-speed\n");
383	case USB_SPEED_FULL:
384		return snprintf(buf, PAGE_SIZE, "full-speed\n");
385	case USB_SPEED_HIGH:
386		return snprintf(buf, PAGE_SIZE, "high-speed\n");
387	case USB_SPEED_WIRELESS:
388		return snprintf(buf, PAGE_SIZE, "wireless\n");
389	case USB_SPEED_SUPER:
390		return snprintf(buf, PAGE_SIZE, "super-speed\n");
391	case USB_SPEED_UNKNOWN:	/* FALLTHROUGH */
392	default:
393		return snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
394	}
395}
396static DEVICE_ATTR(speed, S_IRUSR, usb_udc_speed_show, NULL);
397
398#define USB_UDC_ATTR(name)					\
399ssize_t usb_udc_##name##_show(struct device *dev,		\
400		struct device_attribute *attr, char *buf)	\
401{								\
402	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev); \
403	struct usb_gadget	*gadget = udc->gadget;		\
404								\
405	return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name);	\
406}								\
407static DEVICE_ATTR(name, S_IRUSR, usb_udc_##name##_show, NULL)
408
409static USB_UDC_ATTR(is_dualspeed);
410static USB_UDC_ATTR(is_otg);
411static USB_UDC_ATTR(is_a_peripheral);
412static USB_UDC_ATTR(b_hnp_enable);
413static USB_UDC_ATTR(a_hnp_support);
414static USB_UDC_ATTR(a_alt_hnp_support);
415
416static struct attribute *usb_udc_attrs[] = {
417	&dev_attr_srp.attr,
418	&dev_attr_soft_connect.attr,
419	&dev_attr_speed.attr,
420
421	&dev_attr_is_dualspeed.attr,
422	&dev_attr_is_otg.attr,
423	&dev_attr_is_a_peripheral.attr,
424	&dev_attr_b_hnp_enable.attr,
425	&dev_attr_a_hnp_support.attr,
426	&dev_attr_a_alt_hnp_support.attr,
427	NULL,
428};
429
430static const struct attribute_group usb_udc_attr_group = {
431	.attrs = usb_udc_attrs,
432};
433
434static const struct attribute_group *usb_udc_attr_groups[] = {
435	&usb_udc_attr_group,
436	NULL,
437};
438
439static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
440{
441	struct usb_udc		*udc = container_of(dev, struct usb_udc, dev);
442	int			ret;
443
444	ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
445	if (ret) {
446		dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
447		return ret;
448	}
449
450	if (udc->driver) {
451		ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
452				udc->driver->function);
453		if (ret) {
454			dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
455			return ret;
456		}
457	}
458
459	return 0;
460}
461
462static int __init usb_udc_init(void)
463{
464	udc_class = class_create(THIS_MODULE, "udc");
465	if (IS_ERR(udc_class)) {
466		pr_err("failed to create udc class --> %ld\n",
467				PTR_ERR(udc_class));
468		return PTR_ERR(udc_class);
469	}
470
471	udc_class->dev_uevent = usb_udc_uevent;
472	return 0;
473}
474subsys_initcall(usb_udc_init);
475
476static void __exit usb_udc_exit(void)
477{
478	class_destroy(udc_class);
479}
480module_exit(usb_udc_exit);
481
482MODULE_DESCRIPTION("UDC Framework");
483MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
484MODULE_LICENSE("GPL v2");