endpoint.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/idr.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 device_type usb_ep_device_type = {
  24. .name = "usb_endpoint",
  25. };
  26. struct ep_attribute {
  27. struct attribute attr;
  28. ssize_t (*show)(struct usb_device *,
  29. struct usb_endpoint_descriptor *, char *);
  30. };
  31. #define to_ep_attribute(_attr) \
  32. container_of(_attr, struct ep_attribute, attr)
  33. #define usb_ep_attr(field, format_string) \
  34. static ssize_t show_ep_##field(struct device *dev, \
  35. struct device_attribute *attr, \
  36. char *buf) \
  37. { \
  38. struct ep_device *ep = to_ep_device(dev); \
  39. return sprintf(buf, format_string, ep->desc->field); \
  40. } \
  41. static DEVICE_ATTR(field, S_IRUGO, show_ep_##field, NULL);
  42. usb_ep_attr(bLength, "%02x\n")
  43. usb_ep_attr(bEndpointAddress, "%02x\n")
  44. usb_ep_attr(bmAttributes, "%02x\n")
  45. usb_ep_attr(bInterval, "%02x\n")
  46. static ssize_t show_ep_wMaxPacketSize(struct device *dev,
  47. struct device_attribute *attr, char *buf)
  48. {
  49. struct ep_device *ep = to_ep_device(dev);
  50. return sprintf(buf, "%04x\n",
  51. le16_to_cpu(ep->desc->wMaxPacketSize) & 0x07ff);
  52. }
  53. static DEVICE_ATTR(wMaxPacketSize, S_IRUGO, show_ep_wMaxPacketSize, NULL);
  54. static ssize_t show_ep_type(struct device *dev, struct device_attribute *attr,
  55. char *buf)
  56. {
  57. struct ep_device *ep = to_ep_device(dev);
  58. char *type = "unknown";
  59. switch (usb_endpoint_type(ep->desc)) {
  60. case USB_ENDPOINT_XFER_CONTROL:
  61. type = "Control";
  62. break;
  63. case USB_ENDPOINT_XFER_ISOC:
  64. type = "Isoc";
  65. break;
  66. case USB_ENDPOINT_XFER_BULK:
  67. type = "Bulk";
  68. break;
  69. case USB_ENDPOINT_XFER_INT:
  70. type = "Interrupt";
  71. break;
  72. }
  73. return sprintf(buf, "%s\n", type);
  74. }
  75. static DEVICE_ATTR(type, S_IRUGO, show_ep_type, NULL);
  76. static ssize_t show_ep_interval(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct ep_device *ep = to_ep_device(dev);
  80. char unit;
  81. unsigned interval = 0;
  82. unsigned in;
  83. in = (ep->desc->bEndpointAddress & USB_DIR_IN);
  84. switch (usb_endpoint_type(ep->desc)) {
  85. case USB_ENDPOINT_XFER_CONTROL:
  86. if (ep->udev->speed == USB_SPEED_HIGH) /* uframes per NAK */
  87. interval = ep->desc->bInterval;
  88. break;
  89. case USB_ENDPOINT_XFER_ISOC:
  90. interval = 1 << (ep->desc->bInterval - 1);
  91. break;
  92. case USB_ENDPOINT_XFER_BULK:
  93. if (ep->udev->speed == USB_SPEED_HIGH && !in) /* 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(interval, S_IRUGO, show_ep_interval, NULL);
  113. static ssize_t show_ep_direction(struct device *dev,
  114. struct device_attribute *attr, 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(direction, S_IRUGO, show_ep_direction, NULL);
  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 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. int usb_create_ep_devs(struct device *parent,
  151. struct usb_host_endpoint *endpoint,
  152. struct usb_device *udev)
  153. {
  154. struct ep_device *ep_dev;
  155. int retval;
  156. ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
  157. if (!ep_dev) {
  158. retval = -ENOMEM;
  159. goto exit;
  160. }
  161. ep_dev->desc = &endpoint->desc;
  162. ep_dev->udev = udev;
  163. ep_dev->dev.groups = ep_dev_groups;
  164. ep_dev->dev.type = &usb_ep_device_type;
  165. ep_dev->dev.parent = parent;
  166. ep_dev->dev.release = ep_device_release;
  167. dev_set_name(&ep_dev->dev, "ep_%02x", endpoint->desc.bEndpointAddress);
  168. retval = device_register(&ep_dev->dev);
  169. if (retval)
  170. goto error_register;
  171. endpoint->ep_dev = ep_dev;
  172. return retval;
  173. error_register:
  174. kfree(ep_dev);
  175. exit:
  176. return retval;
  177. }
  178. void usb_remove_ep_devs(struct usb_host_endpoint *endpoint)
  179. {
  180. struct ep_device *ep_dev = endpoint->ep_dev;
  181. if (ep_dev) {
  182. device_unregister(&ep_dev->dev);
  183. endpoint->ep_dev = NULL;
  184. }
  185. }