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