endpoint.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/usb/core/endpoint.c
  4. *
  5. * (C) Copyright 2002,2004,2006 Greg Kroah-Hartman
  6. * (C) Copyright 2002,2004 IBM Corp.
  7. * (C) Copyright 2006 Novell Inc.
  8. *
  9. * Released under the GPLv2 only.
  10. *
  11. * Endpoint sysfs stuff
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/slab.h>
  16. #include <linux/usb.h>
  17. #include "usb.h"
  18. struct ep_device {
  19. struct usb_endpoint_descriptor *desc;
  20. struct usb_device *udev;
  21. struct device dev;
  22. };
  23. #define to_ep_device(_dev) \
  24. container_of(_dev, struct ep_device, dev)
  25. struct ep_attribute {
  26. struct attribute attr;
  27. ssize_t (*show)(struct usb_device *,
  28. struct usb_endpoint_descriptor *, char *);
  29. };
  30. #define to_ep_attribute(_attr) \
  31. container_of(_attr, struct ep_attribute, attr)
  32. #define usb_ep_attr(field, format_string) \
  33. static ssize_t field##_show(struct device *dev, \
  34. struct device_attribute *attr, \
  35. char *buf) \
  36. { \
  37. struct ep_device *ep = to_ep_device(dev); \
  38. return sprintf(buf, format_string, ep->desc->field); \
  39. } \
  40. static DEVICE_ATTR_RO(field)
  41. usb_ep_attr(bLength, "%02x\n");
  42. usb_ep_attr(bEndpointAddress, "%02x\n");
  43. usb_ep_attr(bmAttributes, "%02x\n");
  44. usb_ep_attr(bInterval, "%02x\n");
  45. static ssize_t wMaxPacketSize_show(struct device *dev,
  46. struct device_attribute *attr, char *buf)
  47. {
  48. struct ep_device *ep = to_ep_device(dev);
  49. return sprintf(buf, "%04x\n", usb_endpoint_maxp(ep->desc));
  50. }
  51. static DEVICE_ATTR_RO(wMaxPacketSize);
  52. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  53. char *buf)
  54. {
  55. struct ep_device *ep = to_ep_device(dev);
  56. char *type = "unknown";
  57. switch (usb_endpoint_type(ep->desc)) {
  58. case USB_ENDPOINT_XFER_CONTROL:
  59. type = "Control";
  60. break;
  61. case USB_ENDPOINT_XFER_ISOC:
  62. type = "Isoc";
  63. break;
  64. case USB_ENDPOINT_XFER_BULK:
  65. type = "Bulk";
  66. break;
  67. case USB_ENDPOINT_XFER_INT:
  68. type = "Interrupt";
  69. break;
  70. }
  71. return sprintf(buf, "%s\n", type);
  72. }
  73. static DEVICE_ATTR_RO(type);
  74. static ssize_t interval_show(struct device *dev, struct device_attribute *attr,
  75. char *buf)
  76. {
  77. struct ep_device *ep = to_ep_device(dev);
  78. unsigned int interval;
  79. char unit;
  80. interval = usb_decode_interval(ep->desc, ep->udev->speed);
  81. if (interval % 1000) {
  82. unit = 'u';
  83. } else {
  84. unit = 'm';
  85. interval /= 1000;
  86. }
  87. return sprintf(buf, "%d%cs\n", interval, unit);
  88. }
  89. static DEVICE_ATTR_RO(interval);
  90. static ssize_t direction_show(struct device *dev, struct device_attribute *attr,
  91. char *buf)
  92. {
  93. struct ep_device *ep = to_ep_device(dev);
  94. char *direction;
  95. if (usb_endpoint_xfer_control(ep->desc))
  96. direction = "both";
  97. else if (usb_endpoint_dir_in(ep->desc))
  98. direction = "in";
  99. else
  100. direction = "out";
  101. return sprintf(buf, "%s\n", direction);
  102. }
  103. static DEVICE_ATTR_RO(direction);
  104. static struct attribute *ep_dev_attrs[] = {
  105. &dev_attr_bLength.attr,
  106. &dev_attr_bEndpointAddress.attr,
  107. &dev_attr_bmAttributes.attr,
  108. &dev_attr_bInterval.attr,
  109. &dev_attr_wMaxPacketSize.attr,
  110. &dev_attr_interval.attr,
  111. &dev_attr_type.attr,
  112. &dev_attr_direction.attr,
  113. NULL,
  114. };
  115. static struct attribute_group ep_dev_attr_grp = {
  116. .attrs = ep_dev_attrs,
  117. };
  118. static const struct attribute_group *ep_dev_groups[] = {
  119. &ep_dev_attr_grp,
  120. NULL
  121. };
  122. static void ep_device_release(struct device *dev)
  123. {
  124. struct ep_device *ep_dev = to_ep_device(dev);
  125. kfree(ep_dev);
  126. }
  127. struct device_type usb_ep_device_type = {
  128. .name = "usb_endpoint",
  129. .release = ep_device_release,
  130. };
  131. int usb_create_ep_devs(struct device *parent,
  132. struct usb_host_endpoint *endpoint,
  133. struct usb_device *udev)
  134. {
  135. struct ep_device *ep_dev;
  136. int retval;
  137. ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
  138. if (!ep_dev) {
  139. retval = -ENOMEM;
  140. goto exit;
  141. }
  142. ep_dev->desc = &endpoint->desc;
  143. ep_dev->udev = udev;
  144. ep_dev->dev.groups = ep_dev_groups;
  145. ep_dev->dev.type = &usb_ep_device_type;
  146. ep_dev->dev.parent = parent;
  147. dev_set_name(&ep_dev->dev, "ep_%02x", endpoint->desc.bEndpointAddress);
  148. retval = device_register(&ep_dev->dev);
  149. if (retval)
  150. goto error_register;
  151. device_enable_async_suspend(&ep_dev->dev);
  152. endpoint->ep_dev = ep_dev;
  153. return retval;
  154. error_register:
  155. put_device(&ep_dev->dev);
  156. exit:
  157. return retval;
  158. }
  159. void usb_remove_ep_devs(struct usb_host_endpoint *endpoint)
  160. {
  161. struct ep_device *ep_dev = endpoint->ep_dev;
  162. if (ep_dev) {
  163. device_unregister(&ep_dev->dev);
  164. endpoint->ep_dev = NULL;
  165. }
  166. }