~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/devices.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * devices.c
  3  * (C) Copyright 1999 Randy Dunlap.
  4  * (C) Copyright 1999,2000 Thomas Sailer <sailer@ife.ee.ethz.ch>. (proc file per device)
  5  * (C) Copyright 1999 Deti Fliegl (new USB architecture)
  6  *
  7  * $id$
  8  *
  9  * This program is free software; you can redistribute it and/or modify
 10  * it under the terms of the GNU General Public License as published by
 11  * the Free Software Foundation; either version 2 of the License, or
 12  * (at your option) any later version.
 13  *
 14  * This program is distributed in the hope that it will be useful,
 15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17  * GNU General Public License for more details.
 18  *
 19  * You should have received a copy of the GNU General Public License
 20  * along with this program; if not, write to the Free Software
 21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 22  *
 23  *************************************************************
 24  *
 25  * <mountpoint>/devices contains USB topology, device, config, class,
 26  * interface, & endpoint data.
 27  *
 28  * I considered using /proc/bus/usb/devices/device# for each device
 29  * as it is attached or detached, but I didn't like this for some
 30  * reason -- maybe it's just too deep of a directory structure.
 31  * I also don't like looking in multiple places to gather and view
 32  * the data.  Having only one file for ./devices also prevents race
 33  * conditions that could arise if a program was reading device info
 34  * for devices that are being removed (unplugged).  (That is, the
 35  * program may find a directory for devnum_12 then try to open it,
 36  * but it was just unplugged, so the directory is now deleted.
 37  * But programs would just have to be prepared for situations like
 38  * this in any plug-and-play environment.)
 39  *
 40  * 1999-12-16: Thomas Sailer <sailer@ife.ee.ethz.ch>
 41  *   Converted the whole proc stuff to real
 42  *   read methods. Now not the whole device list needs to fit
 43  *   into one page, only the device list for one bus.
 44  *   Added a poll method to /proc/bus/usb/devices, to wake
 45  *   up an eventual usbd
 46  * 2000-01-04: Thomas Sailer <sailer@ife.ee.ethz.ch>
 47  *   Turned into its own filesystem
 48  * 2000-07-05: Ashley Montanaro <ashley@compsoc.man.ac.uk>
 49  *   Converted file reading routine to dump to buffer once
 50  *   per device, not per bus
 51  *
 52  * $Id: devices.c,v 1.5 2000/01/11 13:58:21 tom Exp $
 53  */
 54 
 55 #include <linux/fs.h>
 56 #include <linux/mm.h>
 57 #include <linux/slab.h>
 58 #include <linux/poll.h>
 59 #include <linux/usb.h>
 60 #include <linux/smp_lock.h>
 61 #include <linux/usbdevice_fs.h>
 62 #include <asm/uaccess.h>
 63 
 64 #define MAX_TOPO_LEVEL          6
 65 
 66 /* Define ALLOW_SERIAL_NUMBER if you want to see the serial number of devices */
 67 #define ALLOW_SERIAL_NUMBER
 68 
 69 static char *format_topo =
 70 /* T:  Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=ddd MxCh=dd */
 71   "T:  Bus=%2.2d Lev=%2.2d Prnt=%2.2d Port=%2.2d Cnt=%2.2d Dev#=%3d Spd=%3s MxCh=%2d\n";
 72 
 73 static char *format_string_manufacturer =
 74 /* S:  Manufacturer=xxxx */
 75   "S:  Manufacturer=%.100s\n";
 76 
 77 static char *format_string_product =
 78 /* S:  Product=xxxx */
 79   "S:  Product=%.100s\n";
 80 
 81 #ifdef ALLOW_SERIAL_NUMBER
 82 static char *format_string_serialnumber =
 83 /* S:  SerialNumber=xxxx */
 84   "S:  SerialNumber=%.100s\n";
 85 #endif
 86 
 87 static char *format_bandwidth =
 88 /* B:  Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd */
 89   "B:  Alloc=%3d/%3d us (%2d%%), #Int=%3d, #Iso=%3d\n";
 90   
 91 static char *format_device1 =
 92 /* D:  Ver=xx.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd */
 93   "D:  Ver=%2x.%02x Cls=%02x(%-5s) Sub=%02x Prot=%02x MxPS=%2d #Cfgs=%3d\n";
 94 
 95 static char *format_device2 =
 96 /* P:  Vendor=xxxx ProdID=xxxx Rev=xx.xx */
 97   "P:  Vendor=%04x ProdID=%04x Rev=%2x.%02x\n";
 98 
 99 static char *format_config =
100 /* C:  #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA */
101   "C:%c #Ifs=%2d Cfg#=%2d Atr=%02x MxPwr=%3dmA\n";
102   
103 static char *format_iface =
104 /* I:  If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=xxxx*/
105   "I:  If#=%2d Alt=%2d #EPs=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x Driver=%s\n";
106 
107 static char *format_endpt =
108 /* E:  Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=dddms */
109   "E:  Ad=%02x(%c) Atr=%02x(%-4s) MxPS=%4d Ivl=%3dms\n";
110 
111 
112 /*
113  * Need access to the driver and USB bus lists.
114  * extern struct list_head usb_driver_list;
115  * extern struct list_head usb_bus_list;
116  * However, these will come from functions that return ptrs to each of them.
117  */
118 
119 static DECLARE_WAIT_QUEUE_HEAD(deviceconndiscwq);
120 static unsigned int conndiscevcnt = 0;
121 
122 /* this struct stores the poll state for <mountpoint>/devices pollers */
123 struct usb_device_status {
124         unsigned int lastev;
125 };
126 
127 struct class_info {
128         int class;
129         char *class_name;
130 };
131 
132 static const struct class_info clas_info[] =
133 {                                       /* max. 5 chars. per name string */
134         {USB_CLASS_PER_INTERFACE,       ">ifc"},
135         {USB_CLASS_AUDIO,               "audio"},
136         {USB_CLASS_COMM,                "comm."},
137         {USB_CLASS_HID,                 "HID"},
138         {USB_CLASS_HUB,                 "hub"},
139         {USB_CLASS_PHYSICAL,            "PID"},
140         {USB_CLASS_PRINTER,             "print"},
141         {USB_CLASS_MASS_STORAGE,        "stor."},
142         {USB_CLASS_DATA,                "data"},
143         {USB_CLASS_APP_SPEC,            "app."},
144         {USB_CLASS_VENDOR_SPEC,         "vend."},
145         {-1,                            "unk."}         /* leave as last */
146 };
147 
148 /*****************************************************************/
149 
150 void usbdevfs_conn_disc_event(void)
151 {
152         wake_up(&deviceconndiscwq);
153         conndiscevcnt++;
154 }
155 
156 static const char *class_decode(const int class)
157 {
158         int ix;
159 
160         for (ix = 0; clas_info[ix].class != -1; ix++)
161                 if (clas_info[ix].class == class)
162                         break;
163         return (clas_info[ix].class_name);
164 }
165 
166 static char *usb_dump_endpoint_descriptor(char *start, char *end, const struct usb_endpoint_descriptor *desc)
167 {
168         char *EndpointType [4] = {"Ctrl", "Isoc", "Bulk", "Int."};
169 
170         if (start > end)
171                 return start;
172         start += sprintf(start, format_endpt, desc->bEndpointAddress,
173                          (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_CONTROL
174                                 ? 'B' :                 /* bidirectional */
175                          (desc->bEndpointAddress & USB_DIR_IN) ? 'I' : 'O',
176                          desc->bmAttributes, EndpointType[desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK],
177                          desc->wMaxPacketSize, desc->bInterval);
178         return start;
179 }
180 
181 static char *usb_dump_endpoint(char *start, char *end, const struct usb_endpoint_descriptor *endpoint)
182 {
183         return usb_dump_endpoint_descriptor(start, end, endpoint);
184 }
185 
186 static char *usb_dump_interface_descriptor(char *start, char *end, const struct usb_interface *iface, int setno)
187 {
188         struct usb_interface_descriptor *desc = &iface->altsetting[setno];
189 
190         if (start > end)
191                 return start;
192         start += sprintf(start, format_iface,
193                          desc->bInterfaceNumber,
194                          desc->bAlternateSetting,
195                          desc->bNumEndpoints,
196                          desc->bInterfaceClass,
197                          class_decode(desc->bInterfaceClass),
198                          desc->bInterfaceSubClass,
199                          desc->bInterfaceProtocol,
200                          iface->driver ? iface->driver->name : "(none)");
201         return start;
202 }
203 
204 static char *usb_dump_interface(char *start, char *end, const struct usb_interface *iface, int setno)
205 {
206         struct usb_interface_descriptor *desc = &iface->altsetting[setno];
207         int i;
208 
209         start = usb_dump_interface_descriptor(start, end, iface, setno);
210         for (i = 0; i < desc->bNumEndpoints; i++) {
211                 if (start > end)
212                         return start;
213                 start = usb_dump_endpoint(start, end, desc->endpoint + i);
214         }
215         return start;
216 }
217 
218 /* TBD:
219  * 0. TBDs
220  * 1. marking active config and ifaces (code lists all, but should mark
221  *    which ones are active, if any)
222  * 2. add <halted> status to each endpoint line
223  */
224 
225 static char *usb_dump_config_descriptor(char *start, char *end, const struct usb_config_descriptor *desc, int active)
226 {
227         if (start > end)
228                 return start;
229         start += sprintf(start, format_config,
230                          active ? '*' : ' ',    /* mark active/actual/current cfg. */
231                          desc->bNumInterfaces,
232                          desc->bConfigurationValue,
233                          desc->bmAttributes,
234                          desc->MaxPower * 2);
235         return start;
236 }
237 
238 static char *usb_dump_config(char *start, char *end, const struct usb_config_descriptor *config, int active)
239 {
240         int i, j;
241         struct usb_interface *interface;
242 
243         if (start > end)
244                 return start;
245         if (!config)            /* getting these some in 2.3.7; none in 2.3.6 */
246                 return start + sprintf(start, "(null Cfg. desc.)\n");
247         start = usb_dump_config_descriptor(start, end, config, active);
248         for (i = 0; i < config->bNumInterfaces; i++) {
249                 interface = config->interface + i;
250                 if (!interface)
251                         break;
252                 for (j = 0; j < interface->num_altsetting; j++) {
253                         if (start > end)
254                                 return start;
255                         start = usb_dump_interface(start, end, interface, j);
256                 }
257         }
258         return start;
259 }
260 
261 /*
262  * Dump the different USB descriptors.
263  */
264 static char *usb_dump_device_descriptor(char *start, char *end, const struct usb_device_descriptor *desc)
265 {
266         if (start > end)
267                 return start;
268         start += sprintf (start, format_device1,
269                           desc->bcdUSB >> 8, desc->bcdUSB & 0xff,
270                           desc->bDeviceClass,
271                           class_decode (desc->bDeviceClass),
272                           desc->bDeviceSubClass,
273                           desc->bDeviceProtocol,
274                           desc->bMaxPacketSize0,
275                           desc->bNumConfigurations);
276         if (start > end)
277                 return start;
278         start += sprintf(start, format_device2,
279                          desc->idVendor, desc->idProduct,
280                          desc->bcdDevice >> 8, desc->bcdDevice & 0xff);
281         return start;
282 }
283 
284 /*
285  * Dump the different strings that this device holds.
286  */
287 static char *usb_dump_device_strings (char *start, char *end, struct usb_device *dev)
288 {
289         char *buf;
290 
291         if (start > end)
292                 return start;
293         buf = kmalloc(128, GFP_KERNEL);
294         if (!buf)
295                 return start;
296         if (dev->descriptor.iManufacturer) {
297                 if (usb_string(dev, dev->descriptor.iManufacturer, buf, 128) > 0)
298                         start += sprintf(start, format_string_manufacturer, buf);
299         }                               
300         if (start > end)
301                 goto out;
302         if (dev->descriptor.iProduct) {
303                 if (usb_string(dev, dev->descriptor.iProduct, buf, 128) > 0)
304                         start += sprintf(start, format_string_product, buf);
305         }
306         if (start > end)
307                 goto out;
308 #ifdef ALLOW_SERIAL_NUMBER
309         if (dev->descriptor.iSerialNumber) {
310                 if (usb_string(dev, dev->descriptor.iSerialNumber, buf, 128) > 0)
311                         start += sprintf(start, format_string_serialnumber, buf);
312         }
313 #endif
314  out:
315         kfree(buf);
316         return start;
317 }
318 
319 static char *usb_dump_desc(char *start, char *end, struct usb_device *dev)
320 {
321         int i;
322 
323         if (start > end)
324                 return start;
325                 
326         start = usb_dump_device_descriptor(start, end, &dev->descriptor);
327 
328         if (start > end)
329                 return start;
330         
331         start = usb_dump_device_strings (start, end, dev);
332         
333         for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
334                 if (start > end)
335                         return start;
336                 start = usb_dump_config(start, end, dev->config + i,
337                                         (dev->config + i) == dev->actconfig); /* active ? */
338         }
339         return start;
340 }
341 
342 
343 #ifdef PROC_EXTRA /* TBD: may want to add this code later */
344 
345 static char *usb_dump_hub_descriptor(char *start, char *end, const struct usb_hub_descriptor * desc)
346 {
347         int leng = USB_DT_HUB_NONVAR_SIZE;
348         unsigned char *ptr = (unsigned char *)desc;
349 
350         if (start > end)
351                 return start;
352         start += sprintf(start, "Interface:");
353         while (leng && start <= end) {
354                 start += sprintf(start, " %02x", *ptr);
355                 ptr++; leng--;
356         }
357         *start++ = '\n';
358         return start;
359 }
360 
361 static char *usb_dump_string(char *start, char *end, const struct usb_device *dev, char *id, int index)
362 {
363         if (start > end)
364                 return start;
365         start += sprintf(start, "Interface:");
366         if (index <= dev->maxstring && dev->stringindex && dev->stringindex[index])
367                 start += sprintf(start, "%s: %.100s ", id, dev->stringindex[index]);
368         return start;
369 }
370 
371 #endif /* PROC_EXTRA */
372 
373 /*****************************************************************/
374 
375 /* This is a recursive function. Parameters:
376  * buffer - the user-space buffer to write data into
377  * nbytes - the maximum number of bytes to write
378  * skip_bytes - the number of bytes to skip before writing anything
379  * file_offset - the offset into the devices file on completion
380  */
381 static ssize_t usb_device_dump(char **buffer, size_t *nbytes, loff_t *skip_bytes, loff_t *file_offset,
382                                 struct usb_device *usbdev, struct usb_bus *bus, int level, int index, int count)
383 {
384         int chix;
385         int ret, cnt = 0;
386         int parent_devnum = 0;
387         char *pages_start, *data_end;
388         unsigned int length;
389         ssize_t total_written = 0;
390         
391         /* don't bother with anything else if we're not writing any data */
392         if (*nbytes <= 0)
393                 return 0;
394         
395         if (level > MAX_TOPO_LEVEL)
396                 return total_written;
397         /* allocate 2^1 pages = 8K (on i386); should be more than enough for one device */
398         if (!(pages_start = (char*) __get_free_pages(GFP_KERNEL,1)))
399                 return -ENOMEM;
400                 
401         if (usbdev->parent && usbdev->parent->devnum != -1)
402                 parent_devnum = usbdev->parent->devnum;
403         /*
404          * So the root hub's parent is 0 and any device that is
405          * plugged into the root hub has a parent of 0.
406          */
407         data_end = pages_start + sprintf(pages_start, format_topo, bus->busnum, level, parent_devnum, index, count,
408                                         usbdev->devnum, usbdev->slow ? "1.5" : "12 ", usbdev->maxchild);
409         /*
410          * level = topology-tier level;
411          * parent_devnum = parent device number;
412          * index = parent's connector number;
413          * count = device count at this level
414          */
415         /* If this is the root hub, display the bandwidth information */
416         if (level == 0)
417                 data_end += sprintf(data_end, format_bandwidth, bus->bandwidth_allocated,
418                                 FRAME_TIME_MAX_USECS_ALLOC,
419                                 (100 * bus->bandwidth_allocated + FRAME_TIME_MAX_USECS_ALLOC / 2) / FRAME_TIME_MAX_USECS_ALLOC,
420                                  bus->bandwidth_int_reqs, bus->bandwidth_isoc_reqs);
421         
422         data_end = usb_dump_desc(data_end, pages_start + (2 * PAGE_SIZE) - 256, usbdev);
423         
424         if (data_end > (pages_start + (2 * PAGE_SIZE) - 256))
425                 data_end += sprintf(data_end, "(truncated)\n");
426         
427         length = data_end - pages_start;
428         /* if we can start copying some data to the user */
429         if (length > *skip_bytes) {
430                 length -= *skip_bytes;
431                 if (length > *nbytes)
432                         length = *nbytes;
433                 if (copy_to_user(*buffer, pages_start + *skip_bytes, length)) {
434                         free_pages((unsigned long)pages_start, 1);
435                         
436                         if (total_written == 0)
437                                 return -EFAULT;
438                         return total_written;
439                 }
440                 *nbytes -= length;
441                 *file_offset += length;
442                 total_written += length;
443                 *buffer += length;
444                 *skip_bytes = 0;
445         } else
446                 *skip_bytes -= length;
447         
448         free_pages((unsigned long)pages_start, 1);
449         
450         /* Now look at all of this device's children. */
451         for (chix = 0; chix < usbdev->maxchild; chix++) {
452                 if (usbdev->children[chix]) {
453                         ret = usb_device_dump(buffer, nbytes, skip_bytes, file_offset, usbdev->children[chix],
454                                         bus, level + 1, chix, ++cnt);
455                         if (ret == -EFAULT)
456                                 return total_written;
457                         total_written += ret;
458                 }
459         }
460         return total_written;
461 }
462 
463 static ssize_t usb_device_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
464 {
465         struct list_head *buslist;
466         struct usb_bus *bus;
467         ssize_t ret, total_written = 0;
468         loff_t skip_bytes = *ppos;
469 
470         if (*ppos < 0)
471                 return -EINVAL;
472         if (nbytes <= 0)
473                 return 0;
474         if (!access_ok(VERIFY_WRITE, buf, nbytes))
475                 return -EFAULT;
476 
477         /* enumerate busses */
478         for (buslist = usb_bus_list.next; buslist != &usb_bus_list; buslist = buslist->next) {
479                 /* print devices for this bus */
480                 bus = list_entry(buslist, struct usb_bus, bus_list);
481                 /* recurse through all children of the root hub */
482                 ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0);
483                 if (ret < 0)
484                         return ret;
485                 total_written += ret;
486         }
487         return total_written;
488 }
489 
490 /* Kernel lock for "lastev" protection */
491 static unsigned int usb_device_poll(struct file *file, struct poll_table_struct *wait)
492 {
493         struct usb_device_status *st = (struct usb_device_status *)file->private_data;
494         unsigned int mask = 0;
495 
496         lock_kernel();
497         if (!st) {
498                 st = kmalloc(sizeof(struct usb_device_status), GFP_KERNEL);
499                 if (!st) {
500                         unlock_kernel();
501                         return POLLIN;
502                 }
503                 /*
504                  * need to prevent the module from being unloaded, since
505                  * proc_unregister does not call the release method and
506                  * we would have a memory leak
507                  */
508                 st->lastev = conndiscevcnt;
509                 file->private_data = st;
510                 mask = POLLIN;
511         }
512         if (file->f_mode & FMODE_READ)
513                 poll_wait(file, &deviceconndiscwq, wait);
514         if (st->lastev != conndiscevcnt)
515                 mask |= POLLIN;
516         st->lastev = conndiscevcnt;
517         unlock_kernel();
518         return mask;
519 }
520 
521 static int usb_device_open(struct inode *inode, struct file *file)
522 {
523         file->private_data = NULL;
524         return 0;
525 }
526 
527 static int usb_device_release(struct inode *inode, struct file *file)
528 {
529         if (file->private_data) {
530                 kfree(file->private_data);
531                 file->private_data = NULL;
532         }
533 
534         return 0;
535 }
536 
537 static loff_t usb_device_lseek(struct file * file, loff_t offset, int orig)
538 {
539         switch (orig) {
540         case 0:
541                 file->f_pos = offset;
542                 return file->f_pos;
543 
544         case 1:
545                 file->f_pos += offset;
546                 return file->f_pos;
547 
548         case 2:
549                 return -EINVAL;
550 
551         default:
552                 return -EINVAL;
553         }
554 }
555 
556 struct file_operations usbdevfs_devices_fops = {
557         llseek:         usb_device_lseek,
558         read:           usb_device_read,
559         poll:           usb_device_poll,
560         open:           usb_device_open,
561         release:        usb_device_release,
562 };
563 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.