0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/kernel.h>
0015 #include <linux/spinlock.h>
0016 #include <linux/slab.h>
0017 #include <linux/usb.h>
0018 #include "usb.h"
0019
0020 struct ep_device {
0021 struct usb_endpoint_descriptor *desc;
0022 struct usb_device *udev;
0023 struct device dev;
0024 };
0025 #define to_ep_device(_dev) \
0026 container_of(_dev, struct ep_device, dev)
0027
0028 struct ep_attribute {
0029 struct attribute attr;
0030 ssize_t (*show)(struct usb_device *,
0031 struct usb_endpoint_descriptor *, char *);
0032 };
0033 #define to_ep_attribute(_attr) \
0034 container_of(_attr, struct ep_attribute, attr)
0035
0036 #define usb_ep_attr(field, format_string) \
0037 static ssize_t field##_show(struct device *dev, \
0038 struct device_attribute *attr, \
0039 char *buf) \
0040 { \
0041 struct ep_device *ep = to_ep_device(dev); \
0042 return sprintf(buf, format_string, ep->desc->field); \
0043 } \
0044 static DEVICE_ATTR_RO(field)
0045
0046 usb_ep_attr(bLength, "%02x\n");
0047 usb_ep_attr(bEndpointAddress, "%02x\n");
0048 usb_ep_attr(bmAttributes, "%02x\n");
0049 usb_ep_attr(bInterval, "%02x\n");
0050
0051 static ssize_t wMaxPacketSize_show(struct device *dev,
0052 struct device_attribute *attr, char *buf)
0053 {
0054 struct ep_device *ep = to_ep_device(dev);
0055 return sprintf(buf, "%04x\n", usb_endpoint_maxp(ep->desc));
0056 }
0057 static DEVICE_ATTR_RO(wMaxPacketSize);
0058
0059 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
0060 char *buf)
0061 {
0062 struct ep_device *ep = to_ep_device(dev);
0063 char *type = "unknown";
0064
0065 switch (usb_endpoint_type(ep->desc)) {
0066 case USB_ENDPOINT_XFER_CONTROL:
0067 type = "Control";
0068 break;
0069 case USB_ENDPOINT_XFER_ISOC:
0070 type = "Isoc";
0071 break;
0072 case USB_ENDPOINT_XFER_BULK:
0073 type = "Bulk";
0074 break;
0075 case USB_ENDPOINT_XFER_INT:
0076 type = "Interrupt";
0077 break;
0078 }
0079 return sprintf(buf, "%s\n", type);
0080 }
0081 static DEVICE_ATTR_RO(type);
0082
0083 static ssize_t interval_show(struct device *dev, struct device_attribute *attr,
0084 char *buf)
0085 {
0086 struct ep_device *ep = to_ep_device(dev);
0087 unsigned int interval;
0088 char unit;
0089
0090 interval = usb_decode_interval(ep->desc, ep->udev->speed);
0091 if (interval % 1000) {
0092 unit = 'u';
0093 } else {
0094 unit = 'm';
0095 interval /= 1000;
0096 }
0097
0098 return sprintf(buf, "%d%cs\n", interval, unit);
0099 }
0100 static DEVICE_ATTR_RO(interval);
0101
0102 static ssize_t direction_show(struct device *dev, struct device_attribute *attr,
0103 char *buf)
0104 {
0105 struct ep_device *ep = to_ep_device(dev);
0106 char *direction;
0107
0108 if (usb_endpoint_xfer_control(ep->desc))
0109 direction = "both";
0110 else if (usb_endpoint_dir_in(ep->desc))
0111 direction = "in";
0112 else
0113 direction = "out";
0114 return sprintf(buf, "%s\n", direction);
0115 }
0116 static DEVICE_ATTR_RO(direction);
0117
0118 static struct attribute *ep_dev_attrs[] = {
0119 &dev_attr_bLength.attr,
0120 &dev_attr_bEndpointAddress.attr,
0121 &dev_attr_bmAttributes.attr,
0122 &dev_attr_bInterval.attr,
0123 &dev_attr_wMaxPacketSize.attr,
0124 &dev_attr_interval.attr,
0125 &dev_attr_type.attr,
0126 &dev_attr_direction.attr,
0127 NULL,
0128 };
0129 static const struct attribute_group ep_dev_attr_grp = {
0130 .attrs = ep_dev_attrs,
0131 };
0132 static const struct attribute_group *ep_dev_groups[] = {
0133 &ep_dev_attr_grp,
0134 NULL
0135 };
0136
0137 static void ep_device_release(struct device *dev)
0138 {
0139 struct ep_device *ep_dev = to_ep_device(dev);
0140
0141 kfree(ep_dev);
0142 }
0143
0144 struct device_type usb_ep_device_type = {
0145 .name = "usb_endpoint",
0146 .release = ep_device_release,
0147 };
0148
0149 int usb_create_ep_devs(struct device *parent,
0150 struct usb_host_endpoint *endpoint,
0151 struct usb_device *udev)
0152 {
0153 struct ep_device *ep_dev;
0154 int retval;
0155
0156 ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
0157 if (!ep_dev) {
0158 retval = -ENOMEM;
0159 goto exit;
0160 }
0161
0162 ep_dev->desc = &endpoint->desc;
0163 ep_dev->udev = udev;
0164 ep_dev->dev.groups = ep_dev_groups;
0165 ep_dev->dev.type = &usb_ep_device_type;
0166 ep_dev->dev.parent = parent;
0167 dev_set_name(&ep_dev->dev, "ep_%02x", endpoint->desc.bEndpointAddress);
0168
0169 retval = device_register(&ep_dev->dev);
0170 if (retval)
0171 goto error_register;
0172
0173 device_enable_async_suspend(&ep_dev->dev);
0174 endpoint->ep_dev = ep_dev;
0175 return retval;
0176
0177 error_register:
0178 put_device(&ep_dev->dev);
0179 exit:
0180 return retval;
0181 }
0182
0183 void usb_remove_ep_devs(struct usb_host_endpoint *endpoint)
0184 {
0185 struct ep_device *ep_dev = endpoint->ep_dev;
0186
0187 if (ep_dev) {
0188 device_unregister(&ep_dev->dev);
0189 endpoint->ep_dev = NULL;
0190 }
0191 }