blob: 4d90a800063ce93a66a2486f95bfd1273c348d7d [file] [log] [blame]
Felipe Balbi2ccea032011-06-28 16:33:46 +03001/**
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>
Felipe Balbia6989082011-12-15 13:31:48 +020025#include <linux/dma-mapping.h>
Felipe Balbi2ccea032011-06-28 16:33:46 +030026
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 */
40struct usb_udc {
41 struct usb_gadget_driver *driver;
42 struct usb_gadget *gadget;
43 struct device dev;
44 struct list_head list;
45};
46
47static struct class *udc_class;
Felipe Balbi2ccea032011-06-28 16:33:46 +030048static LIST_HEAD(udc_list);
49static DEFINE_MUTEX(udc_lock);
50
51/* ------------------------------------------------------------------------- */
52
Felipe Balbia6989082011-12-15 13:31:48 +020053int 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}
82EXPORT_SYMBOL_GPL(usb_gadget_map_request);
83
84void 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}
100EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
101
102/* ------------------------------------------------------------------------- */
103
Felipe Balbi2ccea032011-06-28 16:33:46 +0300104/**
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 */
119static inline int usb_gadget_start(struct usb_gadget *gadget,
120 struct usb_gadget_driver *driver,
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200121 int (*bind)(struct usb_gadget *, struct usb_gadget_driver *))
Felipe Balbi2ccea032011-06-28 16:33:46 +0300122{
123 return gadget->ops->start(driver, bind);
124}
125
126/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200127 * 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 */
140static 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/**
Felipe Balbi2ccea032011-06-28 16:33:46 +0300147 * 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 */
158static inline void usb_gadget_stop(struct usb_gadget *gadget,
159 struct usb_gadget_driver *driver)
160{
161 gadget->ops->stop(driver);
162}
163
164/**
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200165 * 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 */
176static 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/**
Felipe Balbi2ccea032011-06-28 16:33:46 +0300183 * 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 */
189static 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
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200198static const struct attribute_group *usb_udc_attr_groups[];
Felipe Balbi2ccea032011-06-28 16:33:46 +0300199/**
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 */
207int 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;
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200219 udc->dev.groups = usb_udc_attr_groups;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300220 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;
237err3:
238 list_del(&udc->list);
239 mutex_unlock(&udc_lock);
240
241err2:
242 put_device(&udc->dev);
243
244err1:
245 return ret;
246}
247EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
248
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200249static 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
Felipe Balbi2ccea032011-06-28 16:33:46 +0300257static 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
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200264 if (udc_is_newstyle(udc)) {
Felipe Balbi6f395042011-10-03 16:39:30 +0300265 usb_gadget_disconnect(udc->gadget);
Kevin Cernekee974e9322012-08-09 11:23:52 +0300266 udc->driver->disconnect(udc->gadget);
Alan Stern320cd1e2012-04-26 11:31:57 -0400267 udc->driver->unbind(udc->gadget);
Kishon Vijay Abraham I8ae80902012-03-21 21:34:30 +0530268 usb_gadget_udc_stop(udc->gadget, udc->driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200269 } else {
270 usb_gadget_stop(udc->gadget, udc->driver);
271 }
Felipe Balbi2ccea032011-06-28 16:33:46 +0300272
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 */
284void 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
298found:
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}
310EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
311
312/* ------------------------------------------------------------------------- */
313
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +0200314int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300315{
316 struct usb_udc *udc = NULL;
317 int ret;
318
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +0200319 if (!driver || !driver->bind || !driver->setup)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300320 return -EINVAL;
321
322 mutex_lock(&udc_lock);
323 list_for_each_entry(udc, &udc_list, list) {
324 /* For now we take the first one */
325 if (!udc->driver)
326 goto found;
327 }
328
329 pr_debug("couldn't find an available UDC\n");
330 mutex_unlock(&udc_lock);
331 return -ENODEV;
332
333found:
334 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
335 driver->function);
336
337 udc->driver = driver;
338 udc->dev.driver = &driver->driver;
339
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200340 if (udc_is_newstyle(udc)) {
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200341 ret = driver->bind(udc->gadget, driver);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200342 if (ret)
343 goto err1;
344 ret = usb_gadget_udc_start(udc->gadget, driver);
345 if (ret) {
346 driver->unbind(udc->gadget);
347 goto err1;
348 }
349 usb_gadget_connect(udc->gadget);
350 } else {
351
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +0200352 ret = usb_gadget_start(udc->gadget, driver, driver->bind);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200353 if (ret)
354 goto err1;
355
356 }
Felipe Balbi2ccea032011-06-28 16:33:46 +0300357
358 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
359 mutex_unlock(&udc_lock);
360 return 0;
361
362err1:
363 dev_err(&udc->dev, "failed to start %s: %d\n",
364 udc->driver->function, ret);
365 udc->driver = NULL;
366 udc->dev.driver = NULL;
367 mutex_unlock(&udc_lock);
368 return ret;
369}
370EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
371
372int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
373{
374 struct usb_udc *udc = NULL;
375 int ret = -ENODEV;
376
377 if (!driver || !driver->unbind)
378 return -EINVAL;
379
380 mutex_lock(&udc_lock);
381 list_for_each_entry(udc, &udc_list, list)
382 if (udc->driver == driver) {
383 usb_gadget_remove_driver(udc);
384 ret = 0;
385 break;
386 }
387
388 mutex_unlock(&udc_lock);
389 return ret;
390}
391EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
392
393/* ------------------------------------------------------------------------- */
394
395static ssize_t usb_udc_srp_store(struct device *dev,
396 struct device_attribute *attr, const char *buf, size_t n)
397{
Felipe Balbi1d91a962011-09-08 14:11:17 +0300398 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300399
400 if (sysfs_streq(buf, "1"))
401 usb_gadget_wakeup(udc->gadget);
402
403 return n;
404}
405static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
406
407static ssize_t usb_udc_softconn_store(struct device *dev,
408 struct device_attribute *attr, const char *buf, size_t n)
409{
Felipe Balbi865569b2011-09-08 14:12:18 +0300410 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300411
412 if (sysfs_streq(buf, "connect")) {
Felipe Balbi6d258a42012-03-15 16:37:18 +0200413 if (udc_is_newstyle(udc))
414 usb_gadget_udc_start(udc->gadget, udc->driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300415 usb_gadget_connect(udc->gadget);
416 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +0300417 usb_gadget_disconnect(udc->gadget);
Felipe Balbi6d258a42012-03-15 16:37:18 +0200418 if (udc_is_newstyle(udc))
419 usb_gadget_udc_stop(udc->gadget, udc->driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300420 } else {
421 dev_err(dev, "unsupported command '%s'\n", buf);
422 return -EINVAL;
423 }
424
425 return n;
426}
427static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
428
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100429#define USB_UDC_SPEED_ATTR(name, param) \
430ssize_t usb_udc_##param##_show(struct device *dev, \
431 struct device_attribute *attr, char *buf) \
432{ \
433 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
434 return snprintf(buf, PAGE_SIZE, "%s\n", \
435 usb_speed_string(udc->gadget->param)); \
436} \
437static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
438
439static USB_UDC_SPEED_ATTR(current_speed, speed);
440static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
441
Felipe Balbi2ccea032011-06-28 16:33:46 +0300442#define USB_UDC_ATTR(name) \
443ssize_t usb_udc_##name##_show(struct device *dev, \
444 struct device_attribute *attr, char *buf) \
445{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200446 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300447 struct usb_gadget *gadget = udc->gadget; \
448 \
449 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
450} \
Felipe Balbi59d81f82011-10-03 16:43:56 +0300451static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300452
Felipe Balbi2ccea032011-06-28 16:33:46 +0300453static USB_UDC_ATTR(is_otg);
454static USB_UDC_ATTR(is_a_peripheral);
455static USB_UDC_ATTR(b_hnp_enable);
456static USB_UDC_ATTR(a_hnp_support);
457static USB_UDC_ATTR(a_alt_hnp_support);
458
459static struct attribute *usb_udc_attrs[] = {
460 &dev_attr_srp.attr,
461 &dev_attr_soft_connect.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100462 &dev_attr_current_speed.attr,
463 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300464
Felipe Balbi2ccea032011-06-28 16:33:46 +0300465 &dev_attr_is_otg.attr,
466 &dev_attr_is_a_peripheral.attr,
467 &dev_attr_b_hnp_enable.attr,
468 &dev_attr_a_hnp_support.attr,
469 &dev_attr_a_alt_hnp_support.attr,
470 NULL,
471};
472
473static const struct attribute_group usb_udc_attr_group = {
474 .attrs = usb_udc_attrs,
475};
476
477static const struct attribute_group *usb_udc_attr_groups[] = {
478 &usb_udc_attr_group,
479 NULL,
480};
481
482static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
483{
484 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
485 int ret;
486
487 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
488 if (ret) {
489 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
490 return ret;
491 }
492
493 if (udc->driver) {
494 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
495 udc->driver->function);
496 if (ret) {
497 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
498 return ret;
499 }
500 }
501
502 return 0;
503}
504
505static int __init usb_udc_init(void)
506{
507 udc_class = class_create(THIS_MODULE, "udc");
508 if (IS_ERR(udc_class)) {
509 pr_err("failed to create udc class --> %ld\n",
510 PTR_ERR(udc_class));
511 return PTR_ERR(udc_class);
512 }
513
514 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300515 return 0;
516}
517subsys_initcall(usb_udc_init);
518
519static void __exit usb_udc_exit(void)
520{
521 class_destroy(udc_class);
522}
523module_exit(usb_udc_exit);
524
525MODULE_DESCRIPTION("UDC Framework");
526MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
527MODULE_LICENSE("GPL v2");