endpoint.c 4.9 KB

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