Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[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/spinlock.h>
14 #include <linux/idr.h>
15 #include <linux/usb.h>
16 #include "usb.h"
17
18 #define MAX_ENDPOINT_MINORS (64*128*32)
19 static int usb_endpoint_major;
20 static DEFINE_IDR(endpoint_idr);
21
22 struct ep_device {
23         struct usb_endpoint_descriptor *desc;
24         struct usb_device *udev;
25         struct device dev;
26         int minor;
27 };
28 #define to_ep_device(_dev) \
29         container_of(_dev, struct ep_device, dev)
30
31 struct ep_attribute {
32         struct attribute attr;
33         ssize_t (*show)(struct usb_device *,
34                         struct usb_endpoint_descriptor *, char *);
35 };
36 #define to_ep_attribute(_attr) \
37         container_of(_attr, struct ep_attribute, attr)
38
39 #define usb_ep_attr(field, format_string)                       \
40 static ssize_t show_ep_##field(struct device *dev,              \
41                                struct device_attribute *attr,   \
42                                char *buf)                       \
43 {                                                               \
44         struct ep_device *ep = to_ep_device(dev);               \
45         return sprintf(buf, format_string, ep->desc->field);    \
46 }                                                               \
47 static DEVICE_ATTR(field, S_IRUGO, show_ep_##field, NULL);
48
49 usb_ep_attr(bLength, "%02x\n")
50 usb_ep_attr(bEndpointAddress, "%02x\n")
51 usb_ep_attr(bmAttributes, "%02x\n")
52 usb_ep_attr(bInterval, "%02x\n")
53
54 static ssize_t show_ep_wMaxPacketSize(struct device *dev,
55                                       struct device_attribute *attr, char *buf)
56 {
57         struct ep_device *ep = to_ep_device(dev);
58         return sprintf(buf, "%04x\n",
59                         le16_to_cpu(ep->desc->wMaxPacketSize) & 0x07ff);
60 }
61 static DEVICE_ATTR(wMaxPacketSize, S_IRUGO, show_ep_wMaxPacketSize, NULL);
62
63 static ssize_t show_ep_type(struct device *dev, struct device_attribute *attr,
64                             char *buf)
65 {
66         struct ep_device *ep = to_ep_device(dev);
67         char *type = "unknown";
68
69         switch (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
70         case USB_ENDPOINT_XFER_CONTROL:
71                 type = "Control";
72                 break;
73         case USB_ENDPOINT_XFER_ISOC:
74                 type = "Isoc";
75                 break;
76         case USB_ENDPOINT_XFER_BULK:
77                 type = "Bulk";
78                 break;
79         case USB_ENDPOINT_XFER_INT:
80                 type = "Interrupt";
81                 break;
82         }
83         return sprintf(buf, "%s\n", type);
84 }
85 static DEVICE_ATTR(type, S_IRUGO, show_ep_type, NULL);
86
87 static ssize_t show_ep_interval(struct device *dev,
88                                 struct device_attribute *attr, char *buf)
89 {
90         struct ep_device *ep = to_ep_device(dev);
91         char unit;
92         unsigned interval = 0;
93         unsigned in;
94
95         in = (ep->desc->bEndpointAddress & USB_DIR_IN);
96
97         switch (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
98         case USB_ENDPOINT_XFER_CONTROL:
99                 if (ep->udev->speed == USB_SPEED_HIGH)  /* uframes per NAK */
100                         interval = ep->desc->bInterval;
101                 break;
102         case USB_ENDPOINT_XFER_ISOC:
103                 interval = 1 << (ep->desc->bInterval - 1);
104                 break;
105         case USB_ENDPOINT_XFER_BULK:
106                 if (ep->udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
107                         interval = ep->desc->bInterval;
108                 break;
109         case USB_ENDPOINT_XFER_INT:
110                 if (ep->udev->speed == USB_SPEED_HIGH)
111                         interval = 1 << (ep->desc->bInterval - 1);
112                 else
113                         interval = ep->desc->bInterval;
114                 break;
115         }
116         interval *= (ep->udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
117         if (interval % 1000)
118                 unit = 'u';
119         else {
120                 unit = 'm';
121                 interval /= 1000;
122         }
123
124         return sprintf(buf, "%d%cs\n", interval, unit);
125 }
126 static DEVICE_ATTR(interval, S_IRUGO, show_ep_interval, NULL);
127
128 static ssize_t show_ep_direction(struct device *dev,
129                                  struct device_attribute *attr, char *buf)
130 {
131         struct ep_device *ep = to_ep_device(dev);
132         char *direction;
133
134         if ((ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
135                         USB_ENDPOINT_XFER_CONTROL)
136                 direction = "both";
137         else if (ep->desc->bEndpointAddress & USB_DIR_IN)
138                 direction = "in";
139         else
140                 direction = "out";
141         return sprintf(buf, "%s\n", direction);
142 }
143 static DEVICE_ATTR(direction, S_IRUGO, show_ep_direction, NULL);
144
145 static struct attribute *ep_dev_attrs[] = {
146         &dev_attr_bLength.attr,
147         &dev_attr_bEndpointAddress.attr,
148         &dev_attr_bmAttributes.attr,
149         &dev_attr_bInterval.attr,
150         &dev_attr_wMaxPacketSize.attr,
151         &dev_attr_interval.attr,
152         &dev_attr_type.attr,
153         &dev_attr_direction.attr,
154         NULL,
155 };
156 static struct attribute_group ep_dev_attr_grp = {
157         .attrs = ep_dev_attrs,
158 };
159
160 static int usb_endpoint_major_init(void)
161 {
162         dev_t dev;
163         int error;
164
165         error = alloc_chrdev_region(&dev, 0, MAX_ENDPOINT_MINORS,
166                                     "usb_endpoint");
167         if (error) {
168                 err("unable to get a dynamic major for usb endpoints");
169                 return error;
170         }
171         usb_endpoint_major = MAJOR(dev);
172
173         return error;
174 }
175
176 static void usb_endpoint_major_cleanup(void)
177 {
178         unregister_chrdev_region(MKDEV(usb_endpoint_major, 0),
179                                  MAX_ENDPOINT_MINORS);
180 }
181
182 static int endpoint_get_minor(struct ep_device *ep_dev)
183 {
184         static DEFINE_MUTEX(minor_lock);
185         int retval = -ENOMEM;
186         int id;
187
188         mutex_lock(&minor_lock);
189         if (idr_pre_get(&endpoint_idr, GFP_KERNEL) == 0)
190                 goto exit;
191
192         retval = idr_get_new(&endpoint_idr, ep_dev, &id);
193         if (retval < 0) {
194                 if (retval == -EAGAIN)
195                         retval = -ENOMEM;
196                 goto exit;
197         }
198         ep_dev->minor = id & MAX_ID_MASK;
199 exit:
200         mutex_unlock(&minor_lock);
201         return retval;
202 }
203
204 static void endpoint_free_minor(struct ep_device *ep_dev)
205 {
206         idr_remove(&endpoint_idr, ep_dev->minor);
207 }
208
209 static struct endpoint_class {
210         struct kref kref;
211         struct class *class;
212 } *ep_class;
213
214 static int init_endpoint_class(void)
215 {
216         int result = 0;
217
218         if (ep_class != NULL) {
219                 kref_get(&ep_class->kref);
220                 goto exit;
221         }
222
223         ep_class = kmalloc(sizeof(*ep_class), GFP_KERNEL);
224         if (!ep_class) {
225                 result = -ENOMEM;
226                 goto exit;
227         }
228
229         kref_init(&ep_class->kref);
230         ep_class->class = class_create(THIS_MODULE, "usb_endpoint");
231         if (IS_ERR(ep_class->class)) {
232                 result = PTR_ERR(ep_class->class);
233                 goto class_create_error;
234         }
235
236         result = usb_endpoint_major_init();
237         if (result)
238                 goto endpoint_major_error;
239
240         goto exit;
241
242 endpoint_major_error:
243         class_destroy(ep_class->class);
244 class_create_error:
245         kfree(ep_class);
246         ep_class = NULL;
247 exit:
248         return result;
249 }
250
251 static void release_endpoint_class(struct kref *kref)
252 {
253         /* Ok, we cheat as we know we only have one ep_class */
254         class_destroy(ep_class->class);
255         kfree(ep_class);
256         ep_class = NULL;
257         usb_endpoint_major_cleanup();
258 }
259
260 static void destroy_endpoint_class(void)
261 {
262         if (ep_class)
263                 kref_put(&ep_class->kref, release_endpoint_class);
264 }
265
266 static void ep_device_release(struct device *dev)
267 {
268         struct ep_device *ep_dev = to_ep_device(dev);
269
270         dev_dbg(dev, "%s called for %s\n", __FUNCTION__, dev->bus_id);
271         endpoint_free_minor(ep_dev);
272         kfree(ep_dev);
273 }
274
275 int usb_create_ep_files(struct device *parent,
276                         struct usb_host_endpoint *endpoint,
277                         struct usb_device *udev)
278 {
279         char name[8];
280         struct ep_device *ep_dev;
281         int retval;
282
283         retval = init_endpoint_class();
284         if (retval)
285                 goto exit;
286
287         ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
288         if (!ep_dev) {
289                 retval = -ENOMEM;
290                 goto error_alloc;
291         }
292
293         retval = endpoint_get_minor(ep_dev);
294         if (retval) {
295                 dev_err(parent, "can not allocate minor number for %s",
296                         ep_dev->dev.bus_id);
297                 goto error_register;
298         }
299
300         ep_dev->desc = &endpoint->desc;
301         ep_dev->udev = udev;
302         ep_dev->dev.devt = MKDEV(usb_endpoint_major, ep_dev->minor);
303         ep_dev->dev.class = ep_class->class;
304         ep_dev->dev.parent = parent;
305         ep_dev->dev.release = ep_device_release;
306         snprintf(ep_dev->dev.bus_id, BUS_ID_SIZE, "usbdev%d.%d_ep%02x",
307                  udev->bus->busnum, udev->devnum,
308                  endpoint->desc.bEndpointAddress);
309
310         retval = device_register(&ep_dev->dev);
311         if (retval)
312                 goto error_chrdev;
313         retval = sysfs_create_group(&ep_dev->dev.kobj, &ep_dev_attr_grp);
314         if (retval)
315                 goto error_group;
316
317         /* create the symlink to the old-style "ep_XX" directory */
318         sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
319         retval = sysfs_create_link(&parent->kobj, &ep_dev->dev.kobj, name);
320         if (retval)
321                 goto error_link;
322         endpoint->ep_dev = ep_dev;
323         return retval;
324
325 error_link:
326         sysfs_remove_group(&ep_dev->dev.kobj, &ep_dev_attr_grp);
327 error_group:
328         device_unregister(&ep_dev->dev);
329         destroy_endpoint_class();
330         return retval;
331
332 error_chrdev:
333         endpoint_free_minor(ep_dev);
334
335 error_register:
336         kfree(ep_dev);
337 error_alloc:
338         destroy_endpoint_class();
339 exit:
340         return retval;
341 }
342
343 void usb_remove_ep_files(struct usb_host_endpoint *endpoint)
344 {
345         struct ep_device *ep_dev = endpoint->ep_dev;
346
347         if (ep_dev) {
348                 char name[8];
349
350                 sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
351                 sysfs_remove_link(&ep_dev->dev.parent->kobj, name);
352                 sysfs_remove_group(&ep_dev->dev.kobj, &ep_dev_attr_grp);
353                 device_unregister(&ep_dev->dev);
354                 endpoint->ep_dev = NULL;
355                 destroy_endpoint_class();
356         }
357 }