endpoint.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * drivers/usb/core/endpoint.c
  3. *
  4. * (C) Copyright 2002,2004,2006 Greg Kroah-Hartman
  5. * (C) Copyright 2002,2004 IBM Corp.
  6. * (C) Copyright 2006 Novell Inc.
  7. *
  8. * Released under the GPLv2 only.
  9. * SPDX-License-Identifier: GPL-2.0
  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. char unit;
  79. unsigned interval = 0;
  80. unsigned in;
  81. in = (ep->desc->bEndpointAddress & USB_DIR_IN);
  82. switch (usb_endpoint_type(ep->desc)) {
  83. case USB_ENDPOINT_XFER_CONTROL:
  84. if (ep->udev->speed == USB_SPEED_HIGH)
  85. /* uframes per NAK */
  86. interval = ep->desc->bInterval;
  87. break;
  88. case USB_ENDPOINT_XFER_ISOC:
  89. interval = 1 << (ep->desc->bInterval - 1);
  90. break;
  91. case USB_ENDPOINT_XFER_BULK:
  92. if (ep->udev->speed == USB_SPEED_HIGH && !in)
  93. /* uframes per NAK */
  94. interval = ep->desc->bInterval;
  95. break;
  96. case USB_ENDPOINT_XFER_INT:
  97. if (ep->udev->speed == USB_SPEED_HIGH)
  98. interval = 1 << (ep->desc->bInterval - 1);
  99. else
  100. interval = ep->desc->bInterval;
  101. break;
  102. }
  103. interval *= (ep->udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
  104. if (interval % 1000)
  105. unit = 'u';
  106. else {
  107. unit = 'm';
  108. interval /= 1000;
  109. }
  110. return sprintf(buf, "%d%cs\n", interval, unit);
  111. }
  112. static DEVICE_ATTR_RO(interval);
  113. static ssize_t direction_show(struct device *dev, struct device_attribute *attr,
  114. char *buf)
  115. {
  116. struct ep_device *ep = to_ep_device(dev);
  117. char *direction;
  118. if (usb_endpoint_xfer_control(ep->desc))
  119. direction = "both";
  120. else if (usb_endpoint_dir_in(ep->desc))
  121. direction = "in";
  122. else
  123. direction = "out";
  124. return sprintf(buf, "%s\n", direction);
  125. }
  126. static DEVICE_ATTR_RO(direction);
  127. static struct attribute *ep_dev_attrs[] = {
  128. &dev_attr_bLength.attr,
  129. &dev_attr_bEndpointAddress.attr,
  130. &dev_attr_bmAttributes.attr,
  131. &dev_attr_bInterval.attr,
  132. &dev_attr_wMaxPacketSize.attr,
  133. &dev_attr_interval.attr,
  134. &dev_attr_type.attr,
  135. &dev_attr_direction.attr,
  136. NULL,
  137. };
  138. static struct attribute_group ep_dev_attr_grp = {
  139. .attrs = ep_dev_attrs,
  140. };
  141. static const struct attribute_group *ep_dev_groups[] = {
  142. &ep_dev_attr_grp,
  143. NULL
  144. };
  145. static void ep_device_release(struct device *dev)
  146. {
  147. struct ep_device *ep_dev = to_ep_device(dev);
  148. kfree(ep_dev);
  149. }
  150. struct device_type usb_ep_device_type = {
  151. .name = "usb_endpoint",
  152. .release = ep_device_release,
  153. };
  154. int usb_create_ep_devs(struct device *parent,
  155. struct usb_host_endpoint *endpoint,
  156. struct usb_device *udev)
  157. {
  158. struct ep_device *ep_dev;
  159. int retval;
  160. ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
  161. if (!ep_dev) {
  162. retval = -ENOMEM;
  163. goto exit;
  164. }
  165. ep_dev->desc = &endpoint->desc;
  166. ep_dev->udev = udev;
  167. ep_dev->dev.groups = ep_dev_groups;
  168. ep_dev->dev.type = &usb_ep_device_type;
  169. ep_dev->dev.parent = parent;
  170. dev_set_name(&ep_dev->dev, "ep_%02x", endpoint->desc.bEndpointAddress);
  171. retval = device_register(&ep_dev->dev);
  172. if (retval)
  173. goto error_register;
  174. device_enable_async_suspend(&ep_dev->dev);
  175. endpoint->ep_dev = ep_dev;
  176. return retval;
  177. error_register:
  178. put_device(&ep_dev->dev);
  179. exit:
  180. return retval;
  181. }
  182. void usb_remove_ep_devs(struct usb_host_endpoint *endpoint)
  183. {
  184. struct ep_device *ep_dev = endpoint->ep_dev;
  185. if (ep_dev) {
  186. device_unregister(&ep_dev->dev);
  187. endpoint->ep_dev = NULL;
  188. }
  189. }