2 * drivers/usb/core/endpoint.c
4 * (C) Copyright 2002,2004,2006 Greg Kroah-Hartman
5 * (C) Copyright 2002,2004 IBM Corp.
6 * (C) Copyright 2006 Novell Inc.
12 #include <linux/kernel.h>
13 #include <linux/usb.h>
18 struct usb_endpoint_descriptor *desc;
19 struct usb_device *udev;
22 #define to_ep_object(_kobj) \
23 container_of(_kobj, struct ep_object, kobj)
26 struct attribute attr;
27 ssize_t (*show)(struct usb_device *,
28 struct usb_endpoint_descriptor *, char *);
30 #define to_ep_attribute(_attr) \
31 container_of(_attr, struct ep_attribute, attr)
33 #define EP_ATTR(_name) \
34 struct ep_attribute ep_##_name = { \
35 .attr = {.name = #_name, .owner = THIS_MODULE, \
37 .show = show_ep_##_name}
39 #define usb_ep_attr(field, format_string) \
40 static ssize_t show_ep_##field(struct usb_device *udev, \
41 struct usb_endpoint_descriptor *desc, \
44 return sprintf(buf, format_string, desc->field); \
46 static EP_ATTR(field);
48 usb_ep_attr(bLength, "%02x\n")
49 usb_ep_attr(bEndpointAddress, "%02x\n")
50 usb_ep_attr(bmAttributes, "%02x\n")
51 usb_ep_attr(bInterval, "%02x\n")
53 static ssize_t show_ep_wMaxPacketSize(struct usb_device *udev,
54 struct usb_endpoint_descriptor *desc, char *buf)
56 return sprintf(buf, "%04x\n",
57 le16_to_cpu(desc->wMaxPacketSize) & 0x07ff);
59 static EP_ATTR(wMaxPacketSize);
61 static ssize_t show_ep_type(struct usb_device *udev,
62 struct usb_endpoint_descriptor *desc, char *buf)
64 char *type = "unknown";
66 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
67 case USB_ENDPOINT_XFER_CONTROL:
70 case USB_ENDPOINT_XFER_ISOC:
73 case USB_ENDPOINT_XFER_BULK:
76 case USB_ENDPOINT_XFER_INT:
80 return sprintf(buf, "%s\n", type);
84 static ssize_t show_ep_interval(struct usb_device *udev,
85 struct usb_endpoint_descriptor *desc, char *buf)
88 unsigned interval = 0;
91 in = (desc->bEndpointAddress & USB_DIR_IN);
93 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
94 case USB_ENDPOINT_XFER_CONTROL:
95 if (udev->speed == USB_SPEED_HIGH) /* uframes per NAK */
96 interval = desc->bInterval;
98 case USB_ENDPOINT_XFER_ISOC:
99 interval = 1 << (desc->bInterval - 1);
101 case USB_ENDPOINT_XFER_BULK:
102 if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
103 interval = desc->bInterval;
105 case USB_ENDPOINT_XFER_INT:
106 if (udev->speed == USB_SPEED_HIGH)
107 interval = 1 << (desc->bInterval - 1);
109 interval = desc->bInterval;
112 interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
120 return sprintf(buf, "%d%cs\n", interval, unit);
122 static EP_ATTR(interval);
124 static ssize_t show_ep_direction(struct usb_device *udev,
125 struct usb_endpoint_descriptor *desc, char *buf)
129 if ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
130 USB_ENDPOINT_XFER_CONTROL)
132 else if (desc->bEndpointAddress & USB_DIR_IN)
136 return sprintf(buf, "%s\n", direction);
138 static EP_ATTR(direction);
140 static struct attribute *ep_attrs[] = {
142 &ep_bEndpointAddress.attr,
143 &ep_bmAttributes.attr,
145 &ep_wMaxPacketSize.attr,
152 static void ep_object_release(struct kobject *kobj)
154 kfree(to_ep_object(kobj));
157 static ssize_t ep_object_show(struct kobject *kobj, struct attribute *attr,
160 struct ep_object *ep_obj = to_ep_object(kobj);
161 struct ep_attribute *ep_attr = to_ep_attribute(attr);
163 return (ep_attr->show)(ep_obj->udev, ep_obj->desc, buf);
166 static struct sysfs_ops ep_object_sysfs_ops = {
167 .show = ep_object_show,
170 static struct kobj_type ep_object_ktype = {
171 .release = ep_object_release,
172 .sysfs_ops = &ep_object_sysfs_ops,
173 .default_attrs = ep_attrs,
176 void usb_create_ep_files(struct kobject *parent,
177 struct usb_host_endpoint *endpoint,
178 struct usb_device *udev)
180 struct ep_object *ep_obj;
181 struct kobject *kobj;
183 ep_obj = kzalloc(sizeof(struct ep_object), GFP_KERNEL);
187 ep_obj->desc = &endpoint->desc;
190 kobj = &ep_obj->kobj;
191 kobject_set_name(kobj, "ep_%02x", endpoint->desc.bEndpointAddress);
192 kobj->parent = parent;
193 kobj->ktype = &ep_object_ktype;
195 /* Don't use kobject_register, because it generates a hotplug event */
197 if (kobject_add(kobj) == 0)
198 endpoint->kobj = kobj;
203 void usb_remove_ep_files(struct usb_host_endpoint *endpoint)
206 if (endpoint->kobj) {
207 kobject_del(endpoint->kobj);
208 kobject_put(endpoint->kobj);
209 endpoint->kobj = NULL;