blob: 40b79a23476371d7dc3edec9969af00df95de529 [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,
121 int (*bind)(struct usb_gadget *))
122{
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 udc->driver->disconnect(udc->gadget);
Felipe Balbi6f395042011-10-03 16:39:30 +0300266 usb_gadget_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
314int 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) {
Ido Shayevitzf0fb3002012-06-26 13:31:56 +0300325 /* Match according to usb_core_id */
326 if (!udc->driver && udc->gadget &&
327 udc->gadget->usb_core_id == driver->usb_core_id)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300328 goto found;
329 }
330
331 pr_debug("couldn't find an available UDC\n");
332 mutex_unlock(&udc_lock);
333 return -ENODEV;
334
335found:
336 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
337 driver->function);
338
339 udc->driver = driver;
340 udc->dev.driver = &driver->driver;
341
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200342 if (udc_is_newstyle(udc)) {
343 ret = bind(udc->gadget);
344 if (ret)
345 goto err1;
346 ret = usb_gadget_udc_start(udc->gadget, driver);
347 if (ret) {
348 driver->unbind(udc->gadget);
349 goto err1;
350 }
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200351 } else {
352
353 ret = usb_gadget_start(udc->gadget, driver, bind);
354 if (ret)
355 goto err1;
356
357 }
Felipe Balbi2ccea032011-06-28 16:33:46 +0300358
359 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
360 mutex_unlock(&udc_lock);
361 return 0;
362
363err1:
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}
371EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
372
373int 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}
392EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
393
394/* ------------------------------------------------------------------------- */
395
396static ssize_t usb_udc_srp_store(struct device *dev,
397 struct device_attribute *attr, const char *buf, size_t n)
398{
Felipe Balbi1d91a962011-09-08 14:11:17 +0300399 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300400
401 if (sysfs_streq(buf, "1"))
402 usb_gadget_wakeup(udc->gadget);
403
404 return n;
405}
406static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
407
408static ssize_t usb_udc_softconn_store(struct device *dev,
409 struct device_attribute *attr, const char *buf, size_t n)
410{
Felipe Balbi865569b2011-09-08 14:12:18 +0300411 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300412
413 if (sysfs_streq(buf, "connect")) {
Felipe Balbi6d258a42012-03-15 16:37:18 +0200414 if (udc_is_newstyle(udc))
415 usb_gadget_udc_start(udc->gadget, udc->driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300416 usb_gadget_connect(udc->gadget);
417 } else if (sysfs_streq(buf, "disconnect")) {
Felipe Balbi83a787a2012-04-27 11:02:15 +0300418 usb_gadget_disconnect(udc->gadget);
Felipe Balbi6d258a42012-03-15 16:37:18 +0200419 if (udc_is_newstyle(udc))
420 usb_gadget_udc_stop(udc->gadget, udc->driver);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300421 } else {
422 dev_err(dev, "unsupported command '%s'\n", buf);
423 return -EINVAL;
424 }
425
426 return n;
427}
428static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
429
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100430#define USB_UDC_SPEED_ATTR(name, param) \
431ssize_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} \
438static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
439
440static USB_UDC_SPEED_ATTR(current_speed, speed);
441static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
442
443/* TODO: Scheduled for removal in 3.8. */
444static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300445 struct device_attribute *attr, char *buf)
446{
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200447 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100448 return snprintf(buf, PAGE_SIZE, "%d\n",
449 gadget_is_dualspeed(udc->gadget));
Felipe Balbi2ccea032011-06-28 16:33:46 +0300450}
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100451static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_show, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300452
453#define USB_UDC_ATTR(name) \
454ssize_t usb_udc_##name##_show(struct device *dev, \
455 struct device_attribute *attr, char *buf) \
456{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200457 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300458 struct usb_gadget *gadget = udc->gadget; \
459 \
460 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
461} \
Felipe Balbi59d81f82011-10-03 16:43:56 +0300462static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300463
Felipe Balbi2ccea032011-06-28 16:33:46 +0300464static USB_UDC_ATTR(is_otg);
465static USB_UDC_ATTR(is_a_peripheral);
466static USB_UDC_ATTR(b_hnp_enable);
467static USB_UDC_ATTR(a_hnp_support);
468static USB_UDC_ATTR(a_alt_hnp_support);
469
470static struct attribute *usb_udc_attrs[] = {
471 &dev_attr_srp.attr,
472 &dev_attr_soft_connect.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100473 &dev_attr_current_speed.attr,
474 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300475
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
485static const struct attribute_group usb_udc_attr_group = {
486 .attrs = usb_udc_attrs,
487};
488
489static const struct attribute_group *usb_udc_attr_groups[] = {
490 &usb_udc_attr_group,
491 NULL,
492};
493
494static 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
517static 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;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300527 return 0;
528}
529subsys_initcall(usb_udc_init);
530
531static void __exit usb_udc_exit(void)
532{
533 class_destroy(udc_class);
534}
535module_exit(usb_udc_exit);
536
537MODULE_DESCRIPTION("UDC Framework");
538MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
539MODULE_LICENSE("GPL v2");