[PATCH] USB: move the endpoint specific sysfs code to it's own file
[linux-2.6] / drivers / usb / core / endpoint.c
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
12 #include <linux/kernel.h>
13 #include <linux/usb.h>
14 #include "usb.h"
15
16 /* endpoint stuff */
17 struct ep_object {
18         struct usb_endpoint_descriptor *desc;
19         struct usb_device *udev;
20         struct kobject kobj;
21 };
22 #define to_ep_object(_kobj) \
23         container_of(_kobj, struct ep_object, kobj)
24
25 struct ep_attribute {
26         struct attribute attr;
27         ssize_t (*show)(struct usb_device *,
28                         struct usb_endpoint_descriptor *, char *);
29 };
30 #define to_ep_attribute(_attr) \
31         container_of(_attr, struct ep_attribute, attr)
32
33 #define EP_ATTR(_name)                                          \
34 struct ep_attribute ep_##_name = {                              \
35         .attr = {.name = #_name, .owner = THIS_MODULE,          \
36                         .mode = S_IRUGO},                       \
37         .show = show_ep_##_name}
38
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,           \
42                 char *buf)                                      \
43 {                                                               \
44         return sprintf(buf, format_string, desc->field);        \
45 }                                                               \
46 static EP_ATTR(field);
47
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")
52
53 static ssize_t show_ep_wMaxPacketSize(struct usb_device *udev,
54                 struct usb_endpoint_descriptor *desc, char *buf)
55 {
56         return sprintf(buf, "%04x\n",
57                         le16_to_cpu(desc->wMaxPacketSize) & 0x07ff);
58 }
59 static EP_ATTR(wMaxPacketSize);
60
61 static ssize_t show_ep_type(struct usb_device *udev,
62                 struct usb_endpoint_descriptor *desc, char *buf)
63 {
64         char *type = "unknown";
65
66         switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
67         case USB_ENDPOINT_XFER_CONTROL:
68                 type = "Control";
69                 break;
70         case USB_ENDPOINT_XFER_ISOC:
71                 type = "Isoc";
72                 break;
73         case USB_ENDPOINT_XFER_BULK:
74                 type = "Bulk";
75                 break;
76         case USB_ENDPOINT_XFER_INT:
77                 type = "Interrupt";
78                 break;
79         }
80         return sprintf(buf, "%s\n", type);
81 }
82 static EP_ATTR(type);
83
84 static ssize_t show_ep_interval(struct usb_device *udev,
85                 struct usb_endpoint_descriptor *desc, char *buf)
86 {
87         char unit;
88         unsigned interval = 0;
89         unsigned in;
90
91         in = (desc->bEndpointAddress & USB_DIR_IN);
92
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;
97                 break;
98         case USB_ENDPOINT_XFER_ISOC:
99                 interval = 1 << (desc->bInterval - 1);
100                 break;
101         case USB_ENDPOINT_XFER_BULK:
102                 if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
103                         interval = desc->bInterval;
104                 break;
105         case USB_ENDPOINT_XFER_INT:
106                 if (udev->speed == USB_SPEED_HIGH)
107                         interval = 1 << (desc->bInterval - 1);
108                 else
109                         interval = desc->bInterval;
110                 break;
111         }
112         interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
113         if (interval % 1000)
114                 unit = 'u';
115         else {
116                 unit = 'm';
117                 interval /= 1000;
118         }
119
120         return sprintf(buf, "%d%cs\n", interval, unit);
121 }
122 static EP_ATTR(interval);
123
124 static ssize_t show_ep_direction(struct usb_device *udev,
125                 struct usb_endpoint_descriptor *desc, char *buf)
126 {
127         char *direction;
128
129         if ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
130                         USB_ENDPOINT_XFER_CONTROL)
131                 direction = "both";
132         else if (desc->bEndpointAddress & USB_DIR_IN)
133                 direction = "in";
134         else
135                 direction = "out";
136         return sprintf(buf, "%s\n", direction);
137 }
138 static EP_ATTR(direction);
139
140 static struct attribute *ep_attrs[] = {
141         &ep_bLength.attr,
142         &ep_bEndpointAddress.attr,
143         &ep_bmAttributes.attr,
144         &ep_bInterval.attr,
145         &ep_wMaxPacketSize.attr,
146         &ep_type.attr,
147         &ep_interval.attr,
148         &ep_direction.attr,
149         NULL,
150 };
151
152 static void ep_object_release(struct kobject *kobj)
153 {
154         kfree(to_ep_object(kobj));
155 }
156
157 static ssize_t ep_object_show(struct kobject *kobj, struct attribute *attr,
158                 char *buf)
159 {
160         struct ep_object *ep_obj = to_ep_object(kobj);
161         struct ep_attribute *ep_attr = to_ep_attribute(attr);
162
163         return (ep_attr->show)(ep_obj->udev, ep_obj->desc, buf);
164 }
165
166 static struct sysfs_ops ep_object_sysfs_ops = {
167         .show =                 ep_object_show,
168 };
169
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,
174 };
175
176 void usb_create_ep_files(struct kobject *parent,
177                          struct usb_host_endpoint *endpoint,
178                          struct usb_device *udev)
179 {
180         struct ep_object *ep_obj;
181         struct kobject *kobj;
182
183         ep_obj = kzalloc(sizeof(struct ep_object), GFP_KERNEL);
184         if (!ep_obj)
185                 return;
186
187         ep_obj->desc = &endpoint->desc;
188         ep_obj->udev = udev;
189
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;
194
195         /* Don't use kobject_register, because it generates a hotplug event */
196         kobject_init(kobj);
197         if (kobject_add(kobj) == 0)
198                 endpoint->kobj = kobj;
199         else
200                 kobject_put(kobj);
201 }
202
203 void usb_remove_ep_files(struct usb_host_endpoint *endpoint)
204 {
205
206         if (endpoint->kobj) {
207                 kobject_del(endpoint->kobj);
208                 kobject_put(endpoint->kobj);
209                 endpoint->kobj = NULL;
210         }
211 }
212
213