blob: 56da49f31d6c35a0f61611c1bb651b51e59732ab [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);
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200266 udc->driver->unbind(udc->gadget);
267 usb_gadget_udc_stop(udc->gadget, udc->driver);
Felipe Balbi6f395042011-10-03 16:39:30 +0300268 usb_gadget_disconnect(udc->gadget);
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) {
325 /* For now we take the first one */
326 if (!udc->driver)
327 goto found;
328 }
329
330 pr_debug("couldn't find an available UDC\n");
331 mutex_unlock(&udc_lock);
332 return -ENODEV;
333
334found:
335 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
336 driver->function);
337
338 udc->driver = driver;
339 udc->dev.driver = &driver->driver;
340
Sebastian Andrzej Siewior352c2dc2011-06-23 14:26:15 +0200341 if (udc_is_newstyle(udc)) {
342 ret = bind(udc->gadget);
343 if (ret)
344 goto err1;
345 ret = usb_gadget_udc_start(udc->gadget, driver);
346 if (ret) {
347 driver->unbind(udc->gadget);
348 goto err1;
349 }
350 usb_gadget_connect(udc->gadget);
351 } 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")) {
414 usb_gadget_connect(udc->gadget);
415 } else if (sysfs_streq(buf, "disconnect")) {
416 usb_gadget_disconnect(udc->gadget);
417 } else {
418 dev_err(dev, "unsupported command '%s'\n", buf);
419 return -EINVAL;
420 }
421
422 return n;
423}
424static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
425
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100426#define USB_UDC_SPEED_ATTR(name, param) \
427ssize_t usb_udc_##param##_show(struct device *dev, \
428 struct device_attribute *attr, char *buf) \
429{ \
430 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
431 return snprintf(buf, PAGE_SIZE, "%s\n", \
432 usb_speed_string(udc->gadget->param)); \
433} \
434static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
435
436static USB_UDC_SPEED_ATTR(current_speed, speed);
437static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
438
439/* TODO: Scheduled for removal in 3.8. */
440static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300441 struct device_attribute *attr, char *buf)
442{
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200443 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100444 return snprintf(buf, PAGE_SIZE, "%d\n",
445 gadget_is_dualspeed(udc->gadget));
Felipe Balbi2ccea032011-06-28 16:33:46 +0300446}
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100447static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_show, NULL);
Felipe Balbi2ccea032011-06-28 16:33:46 +0300448
449#define USB_UDC_ATTR(name) \
450ssize_t usb_udc_##name##_show(struct device *dev, \
451 struct device_attribute *attr, char *buf) \
452{ \
Sebastian Andrzej Siewior019f9762011-06-23 14:28:38 +0200453 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
Felipe Balbi2ccea032011-06-28 16:33:46 +0300454 struct usb_gadget *gadget = udc->gadget; \
455 \
456 return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
457} \
Felipe Balbi59d81f82011-10-03 16:43:56 +0300458static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
Felipe Balbi2ccea032011-06-28 16:33:46 +0300459
Felipe Balbi2ccea032011-06-28 16:33:46 +0300460static USB_UDC_ATTR(is_otg);
461static USB_UDC_ATTR(is_a_peripheral);
462static USB_UDC_ATTR(b_hnp_enable);
463static USB_UDC_ATTR(a_hnp_support);
464static USB_UDC_ATTR(a_alt_hnp_support);
465
466static struct attribute *usb_udc_attrs[] = {
467 &dev_attr_srp.attr,
468 &dev_attr_soft_connect.attr,
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100469 &dev_attr_current_speed.attr,
470 &dev_attr_maximum_speed.attr,
Felipe Balbi2ccea032011-06-28 16:33:46 +0300471
472 &dev_attr_is_dualspeed.attr,
473 &dev_attr_is_otg.attr,
474 &dev_attr_is_a_peripheral.attr,
475 &dev_attr_b_hnp_enable.attr,
476 &dev_attr_a_hnp_support.attr,
477 &dev_attr_a_alt_hnp_support.attr,
478 NULL,
479};
480
481static const struct attribute_group usb_udc_attr_group = {
482 .attrs = usb_udc_attrs,
483};
484
485static const struct attribute_group *usb_udc_attr_groups[] = {
486 &usb_udc_attr_group,
487 NULL,
488};
489
490static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
491{
492 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
493 int ret;
494
495 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
496 if (ret) {
497 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
498 return ret;
499 }
500
501 if (udc->driver) {
502 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
503 udc->driver->function);
504 if (ret) {
505 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
506 return ret;
507 }
508 }
509
510 return 0;
511}
512
513static int __init usb_udc_init(void)
514{
515 udc_class = class_create(THIS_MODULE, "udc");
516 if (IS_ERR(udc_class)) {
517 pr_err("failed to create udc class --> %ld\n",
518 PTR_ERR(udc_class));
519 return PTR_ERR(udc_class);
520 }
521
522 udc_class->dev_uevent = usb_udc_uevent;
Felipe Balbi2ccea032011-06-28 16:33:46 +0300523 return 0;
524}
525subsys_initcall(usb_udc_init);
526
527static void __exit usb_udc_exit(void)
528{
529 class_destroy(udc_class);
530}
531module_exit(usb_udc_exit);
532
533MODULE_DESCRIPTION("UDC Framework");
534MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
535MODULE_LICENSE("GPL v2");