Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
udc-core.c
Go to the documentation of this file.
1 
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 
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 
40 struct usb_udc {
42  struct usb_gadget *gadget;
43  struct device dev;
44  struct list_head list;
45 };
46 
47 static struct class *udc_class;
48 static LIST_HEAD(udc_list);
49 static DEFINE_MUTEX(udc_lock);
50 
51 /* ------------------------------------------------------------------------- */
52 
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 }
83 
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 }
101 
102 /* ------------------------------------------------------------------------- */
103 
119 static inline int usb_gadget_start(struct usb_gadget *gadget,
120  struct usb_gadget_driver *driver,
121  int (*bind)(struct usb_gadget *, struct usb_gadget_driver *))
122 {
123  return gadget->ops->start(driver, bind);
124 }
125 
140 static 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 
158 static inline void usb_gadget_stop(struct usb_gadget *gadget,
159  struct usb_gadget_driver *driver)
160 {
161  gadget->ops->stop(driver);
162 }
163 
176 static 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 
189 static 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 
198 static const struct attribute_group *usb_udc_attr_groups[];
207 int 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;
219  udc->dev.groups = usb_udc_attr_groups;
220  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;
237 err3:
238  list_del(&udc->list);
239  mutex_unlock(&udc_lock);
240 
241 err2:
242  put_device(&udc->dev);
243 
244 err1:
245  return ret;
246 }
248 
249 static 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 
257 static 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 
264  if (udc_is_newstyle(udc)) {
265  usb_gadget_disconnect(udc->gadget);
266  udc->driver->disconnect(udc->gadget);
267  udc->driver->unbind(udc->gadget);
268  usb_gadget_udc_stop(udc->gadget, udc->driver);
269  } else {
270  usb_gadget_stop(udc->gadget, udc->driver);
271  }
272 
273  udc->driver = NULL;
274  udc->dev.driver = NULL;
275 }
276 
284 void 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 
298 found:
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 }
311 
312 /* ------------------------------------------------------------------------- */
313 
315 {
316  struct usb_udc *udc = NULL;
317  int ret;
318 
319  if (!driver || !driver->bind || !driver->setup)
320  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 
333 found:
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 
340  if (udc_is_newstyle(udc)) {
341  ret = driver->bind(udc->gadget, driver);
342  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 
352  ret = usb_gadget_start(udc->gadget, driver, driver->bind);
353  if (ret)
354  goto err1;
355 
356  }
357 
358  kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
359  mutex_unlock(&udc_lock);
360  return 0;
361 
362 err1:
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 }
371 
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 }
392 
393 /* ------------------------------------------------------------------------- */
394 
395 static ssize_t usb_udc_srp_store(struct device *dev,
396  struct device_attribute *attr, const char *buf, size_t n)
397 {
398  struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
399 
400  if (sysfs_streq(buf, "1"))
401  usb_gadget_wakeup(udc->gadget);
402 
403  return n;
404 }
405 static DEVICE_ATTR(srp, S_IWUSR, NULL, usb_udc_srp_store);
406 
407 static ssize_t usb_udc_softconn_store(struct device *dev,
408  struct device_attribute *attr, const char *buf, size_t n)
409 {
410  struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
411 
412  if (sysfs_streq(buf, "connect")) {
413  if (udc_is_newstyle(udc))
414  usb_gadget_udc_start(udc->gadget, udc->driver);
415  usb_gadget_connect(udc->gadget);
416  } else if (sysfs_streq(buf, "disconnect")) {
417  usb_gadget_disconnect(udc->gadget);
418  if (udc_is_newstyle(udc))
419  usb_gadget_udc_stop(udc->gadget, udc->driver);
420  } else {
421  dev_err(dev, "unsupported command '%s'\n", buf);
422  return -EINVAL;
423  }
424 
425  return n;
426 }
427 static DEVICE_ATTR(soft_connect, S_IWUSR, NULL, usb_udc_softconn_store);
428 
429 #define USB_UDC_SPEED_ATTR(name, param) \
430 ssize_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 } \
437 static DEVICE_ATTR(name, S_IRUSR, usb_udc_##param##_show, NULL)
438 
439 static USB_UDC_SPEED_ATTR(current_speed, speed);
440 static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
441 
442 /* TODO: Scheduled for removal in 3.8. */
443 static ssize_t usb_udc_is_dualspeed_show(struct device *dev,
444  struct device_attribute *attr, char *buf)
445 {
446  struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
447  return snprintf(buf, PAGE_SIZE, "%d\n",
448  gadget_is_dualspeed(udc->gadget));
449 }
450 static DEVICE_ATTR(is_dualspeed, S_IRUSR, usb_udc_is_dualspeed_show, NULL);
451 
452 #define USB_UDC_ATTR(name) \
453 ssize_t usb_udc_##name##_show(struct device *dev, \
454  struct device_attribute *attr, char *buf) \
455 { \
456  struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
457  struct usb_gadget *gadget = udc->gadget; \
458  \
459  return snprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
460 } \
461 static DEVICE_ATTR(name, S_IRUGO, usb_udc_##name##_show, NULL)
462 
463 static USB_UDC_ATTR(is_otg);
464 static USB_UDC_ATTR(is_a_peripheral);
465 static USB_UDC_ATTR(b_hnp_enable);
466 static USB_UDC_ATTR(a_hnp_support);
467 static USB_UDC_ATTR(a_alt_hnp_support);
468 
469 static struct attribute *usb_udc_attrs[] = {
470  &dev_attr_srp.attr,
471  &dev_attr_soft_connect.attr,
472  &dev_attr_current_speed.attr,
473  &dev_attr_maximum_speed.attr,
474 
475  &dev_attr_is_dualspeed.attr,
476  &dev_attr_is_otg.attr,
477  &dev_attr_is_a_peripheral.attr,
478  &dev_attr_b_hnp_enable.attr,
479  &dev_attr_a_hnp_support.attr,
480  &dev_attr_a_alt_hnp_support.attr,
481  NULL,
482 };
483 
484 static const struct attribute_group usb_udc_attr_group = {
485  .attrs = usb_udc_attrs,
486 };
487 
488 static const struct attribute_group *usb_udc_attr_groups[] = {
489  &usb_udc_attr_group,
490  NULL,
491 };
492 
493 static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
494 {
495  struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
496  int ret;
497 
498  ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
499  if (ret) {
500  dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
501  return ret;
502  }
503 
504  if (udc->driver) {
505  ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
506  udc->driver->function);
507  if (ret) {
508  dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
509  return ret;
510  }
511  }
512 
513  return 0;
514 }
515 
516 static int __init usb_udc_init(void)
517 {
518  udc_class = class_create(THIS_MODULE, "udc");
519  if (IS_ERR(udc_class)) {
520  pr_err("failed to create udc class --> %ld\n",
521  PTR_ERR(udc_class));
522  return PTR_ERR(udc_class);
523  }
524 
525  udc_class->dev_uevent = usb_udc_uevent;
526  return 0;
527 }
528 subsys_initcall(usb_udc_init);
529 
530 static void __exit usb_udc_exit(void)
531 {
532  class_destroy(udc_class);
533 }
534 module_exit(usb_udc_exit);
535 
536 MODULE_DESCRIPTION("UDC Framework");
537 MODULE_AUTHOR("Felipe Balbi <[email protected]>");
538 MODULE_LICENSE("GPL v2");