Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * devices.c
0004  * (C) Copyright 1999 Randy Dunlap.
0005  * (C) Copyright 1999,2000 Thomas Sailer <sailer@ife.ee.ethz.ch>.
0006  *     (proc file per device)
0007  * (C) Copyright 1999 Deti Fliegl (new USB architecture)
0008  *
0009  *************************************************************
0010  *
0011  * <mountpoint>/devices contains USB topology, device, config, class,
0012  * interface, & endpoint data.
0013  *
0014  * I considered using /dev/bus/usb/device# for each device
0015  * as it is attached or detached, but I didn't like this for some
0016  * reason -- maybe it's just too deep of a directory structure.
0017  * I also don't like looking in multiple places to gather and view
0018  * the data.  Having only one file for ./devices also prevents race
0019  * conditions that could arise if a program was reading device info
0020  * for devices that are being removed (unplugged).  (That is, the
0021  * program may find a directory for devnum_12 then try to open it,
0022  * but it was just unplugged, so the directory is now deleted.
0023  * But programs would just have to be prepared for situations like
0024  * this in any plug-and-play environment.)
0025  *
0026  * 1999-12-16: Thomas Sailer <sailer@ife.ee.ethz.ch>
0027  *   Converted the whole proc stuff to real
0028  *   read methods. Now not the whole device list needs to fit
0029  *   into one page, only the device list for one bus.
0030  *   Added a poll method to /sys/kernel/debug/usb/devices, to wake
0031  *   up an eventual usbd
0032  * 2000-01-04: Thomas Sailer <sailer@ife.ee.ethz.ch>
0033  *   Turned into its own filesystem
0034  * 2000-07-05: Ashley Montanaro <ashley@compsoc.man.ac.uk>
0035  *   Converted file reading routine to dump to buffer once
0036  *   per device, not per bus
0037  */
0038 
0039 #include <linux/fs.h>
0040 #include <linux/mm.h>
0041 #include <linux/gfp.h>
0042 #include <linux/usb.h>
0043 #include <linux/usbdevice_fs.h>
0044 #include <linux/usb/hcd.h>
0045 #include <linux/mutex.h>
0046 #include <linux/uaccess.h>
0047 
0048 #include "usb.h"
0049 
0050 /* Define ALLOW_SERIAL_NUMBER if you want to see the serial number of devices */
0051 #define ALLOW_SERIAL_NUMBER
0052 
0053 static const char format_topo[] =
0054 /* T:  Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=dddd MxCh=dd */
0055 "\nT:  Bus=%2.2d Lev=%2.2d Prnt=%2.2d Port=%2.2d Cnt=%2.2d Dev#=%3d Spd=%-4s MxCh=%2d\n";
0056 
0057 static const char format_string_manufacturer[] =
0058 /* S:  Manufacturer=xxxx */
0059   "S:  Manufacturer=%.100s\n";
0060 
0061 static const char format_string_product[] =
0062 /* S:  Product=xxxx */
0063   "S:  Product=%.100s\n";
0064 
0065 #ifdef ALLOW_SERIAL_NUMBER
0066 static const char format_string_serialnumber[] =
0067 /* S:  SerialNumber=xxxx */
0068   "S:  SerialNumber=%.100s\n";
0069 #endif
0070 
0071 static const char format_bandwidth[] =
0072 /* B:  Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd */
0073   "B:  Alloc=%3d/%3d us (%2d%%), #Int=%3d, #Iso=%3d\n";
0074 
0075 static const char format_device1[] =
0076 /* D:  Ver=xx.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd */
0077   "D:  Ver=%2x.%02x Cls=%02x(%-5s) Sub=%02x Prot=%02x MxPS=%2d #Cfgs=%3d\n";
0078 
0079 static const char format_device2[] =
0080 /* P:  Vendor=xxxx ProdID=xxxx Rev=xx.xx */
0081   "P:  Vendor=%04x ProdID=%04x Rev=%2x.%02x\n";
0082 
0083 static const char format_config[] =
0084 /* C:  #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA */
0085   "C:%c #Ifs=%2d Cfg#=%2d Atr=%02x MxPwr=%3dmA\n";
0086 
0087 static const char format_iad[] =
0088 /* A:  FirstIf#=dd IfCount=dd Cls=xx(sssss) Sub=xx Prot=xx */
0089   "A:  FirstIf#=%2d IfCount=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x\n";
0090 
0091 static const char format_iface[] =
0092 /* I:  If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=xxxx*/
0093   "I:%c If#=%2d Alt=%2d #EPs=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x Driver=%s\n";
0094 
0095 static const char format_endpt[] =
0096 /* E:  Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=D?s */
0097   "E:  Ad=%02x(%c) Atr=%02x(%-4s) MxPS=%4d Ivl=%d%cs\n";
0098 
0099 struct class_info {
0100     int class;
0101     char *class_name;
0102 };
0103 
0104 static const struct class_info clas_info[] = {
0105     /* max. 5 chars. per name string */
0106     {USB_CLASS_PER_INTERFACE,   ">ifc"},
0107     {USB_CLASS_AUDIO,       "audio"},
0108     {USB_CLASS_COMM,        "comm."},
0109     {USB_CLASS_HID,         "HID"},
0110     {USB_CLASS_PHYSICAL,        "PID"},
0111     {USB_CLASS_STILL_IMAGE,     "still"},
0112     {USB_CLASS_PRINTER,     "print"},
0113     {USB_CLASS_MASS_STORAGE,    "stor."},
0114     {USB_CLASS_HUB,         "hub"},
0115     {USB_CLASS_CDC_DATA,        "data"},
0116     {USB_CLASS_CSCID,       "scard"},
0117     {USB_CLASS_CONTENT_SEC,     "c-sec"},
0118     {USB_CLASS_VIDEO,       "video"},
0119     {USB_CLASS_PERSONAL_HEALTHCARE, "perhc"},
0120     {USB_CLASS_AUDIO_VIDEO,     "av"},
0121     {USB_CLASS_BILLBOARD,       "blbrd"},
0122     {USB_CLASS_USB_TYPE_C_BRIDGE,   "bridg"},
0123     {USB_CLASS_WIRELESS_CONTROLLER, "wlcon"},
0124     {USB_CLASS_MISC,        "misc"},
0125     {USB_CLASS_APP_SPEC,        "app."},
0126     {USB_CLASS_VENDOR_SPEC,     "vend."},
0127     {-1,                "unk."}     /* leave as last */
0128 };
0129 
0130 /*****************************************************************/
0131 
0132 static const char *class_decode(const int class)
0133 {
0134     int ix;
0135 
0136     for (ix = 0; clas_info[ix].class != -1; ix++)
0137         if (clas_info[ix].class == class)
0138             break;
0139     return clas_info[ix].class_name;
0140 }
0141 
0142 static char *usb_dump_endpoint_descriptor(int speed, char *start, char *end,
0143                 const struct usb_endpoint_descriptor *desc)
0144 {
0145     char dir, unit, *type;
0146     unsigned interval, bandwidth = 1;
0147 
0148     if (start > end)
0149         return start;
0150 
0151     dir = usb_endpoint_dir_in(desc) ? 'I' : 'O';
0152 
0153     if (speed == USB_SPEED_HIGH)
0154         bandwidth = usb_endpoint_maxp_mult(desc);
0155 
0156     /* this isn't checking for illegal values */
0157     switch (usb_endpoint_type(desc)) {
0158     case USB_ENDPOINT_XFER_CONTROL:
0159         type = "Ctrl";
0160         dir = 'B';          /* ctrl is bidirectional */
0161         break;
0162     case USB_ENDPOINT_XFER_ISOC:
0163         type = "Isoc";
0164         break;
0165     case USB_ENDPOINT_XFER_BULK:
0166         type = "Bulk";
0167         break;
0168     case USB_ENDPOINT_XFER_INT:
0169         type = "Int.";
0170         break;
0171     default:    /* "can't happen" */
0172         return start;
0173     }
0174 
0175     interval = usb_decode_interval(desc, speed);
0176     if (interval % 1000) {
0177         unit = 'u';
0178     } else {
0179         unit = 'm';
0180         interval /= 1000;
0181     }
0182 
0183     start += sprintf(start, format_endpt, desc->bEndpointAddress, dir,
0184              desc->bmAttributes, type,
0185              usb_endpoint_maxp(desc) *
0186              bandwidth,
0187              interval, unit);
0188     return start;
0189 }
0190 
0191 static char *usb_dump_interface_descriptor(char *start, char *end,
0192                     const struct usb_interface_cache *intfc,
0193                     const struct usb_interface *iface,
0194                     int setno)
0195 {
0196     const struct usb_interface_descriptor *desc;
0197     const char *driver_name = "";
0198     int active = 0;
0199 
0200     if (start > end)
0201         return start;
0202     desc = &intfc->altsetting[setno].desc;
0203     if (iface) {
0204         driver_name = (iface->dev.driver
0205                 ? iface->dev.driver->name
0206                 : "(none)");
0207         active = (desc == &iface->cur_altsetting->desc);
0208     }
0209     start += sprintf(start, format_iface,
0210              active ? '*' : ' ',    /* mark active altsetting */
0211              desc->bInterfaceNumber,
0212              desc->bAlternateSetting,
0213              desc->bNumEndpoints,
0214              desc->bInterfaceClass,
0215              class_decode(desc->bInterfaceClass),
0216              desc->bInterfaceSubClass,
0217              desc->bInterfaceProtocol,
0218              driver_name);
0219     return start;
0220 }
0221 
0222 static char *usb_dump_interface(int speed, char *start, char *end,
0223                 const struct usb_interface_cache *intfc,
0224                 const struct usb_interface *iface, int setno)
0225 {
0226     const struct usb_host_interface *desc = &intfc->altsetting[setno];
0227     int i;
0228 
0229     start = usb_dump_interface_descriptor(start, end, intfc, iface, setno);
0230     for (i = 0; i < desc->desc.bNumEndpoints; i++) {
0231         start = usb_dump_endpoint_descriptor(speed,
0232                 start, end, &desc->endpoint[i].desc);
0233     }
0234     return start;
0235 }
0236 
0237 static char *usb_dump_iad_descriptor(char *start, char *end,
0238             const struct usb_interface_assoc_descriptor *iad)
0239 {
0240     if (start > end)
0241         return start;
0242     start += sprintf(start, format_iad,
0243              iad->bFirstInterface,
0244              iad->bInterfaceCount,
0245              iad->bFunctionClass,
0246              class_decode(iad->bFunctionClass),
0247              iad->bFunctionSubClass,
0248              iad->bFunctionProtocol);
0249     return start;
0250 }
0251 
0252 /* TBD:
0253  * 0. TBDs
0254  * 1. marking active interface altsettings (code lists all, but should mark
0255  *    which ones are active, if any)
0256  */
0257 static char *usb_dump_config_descriptor(char *start, char *end,
0258                 const struct usb_config_descriptor *desc,
0259                 int active, int speed)
0260 {
0261     int mul;
0262 
0263     if (start > end)
0264         return start;
0265     if (speed >= USB_SPEED_SUPER)
0266         mul = 8;
0267     else
0268         mul = 2;
0269     start += sprintf(start, format_config,
0270              /* mark active/actual/current cfg. */
0271              active ? '*' : ' ',
0272              desc->bNumInterfaces,
0273              desc->bConfigurationValue,
0274              desc->bmAttributes,
0275              desc->bMaxPower * mul);
0276     return start;
0277 }
0278 
0279 static char *usb_dump_config(int speed, char *start, char *end,
0280                  const struct usb_host_config *config, int active)
0281 {
0282     int i, j;
0283     struct usb_interface_cache *intfc;
0284     struct usb_interface *interface;
0285 
0286     if (start > end)
0287         return start;
0288     if (!config)
0289         /* getting these some in 2.3.7; none in 2.3.6 */
0290         return start + sprintf(start, "(null Cfg. desc.)\n");
0291     start = usb_dump_config_descriptor(start, end, &config->desc, active,
0292             speed);
0293     for (i = 0; i < USB_MAXIADS; i++) {
0294         if (config->intf_assoc[i] == NULL)
0295             break;
0296         start = usb_dump_iad_descriptor(start, end,
0297                     config->intf_assoc[i]);
0298     }
0299     for (i = 0; i < config->desc.bNumInterfaces; i++) {
0300         intfc = config->intf_cache[i];
0301         interface = config->interface[i];
0302         for (j = 0; j < intfc->num_altsetting; j++) {
0303             start = usb_dump_interface(speed,
0304                 start, end, intfc, interface, j);
0305         }
0306     }
0307     return start;
0308 }
0309 
0310 /*
0311  * Dump the different USB descriptors.
0312  */
0313 static char *usb_dump_device_descriptor(char *start, char *end,
0314                 const struct usb_device_descriptor *desc)
0315 {
0316     u16 bcdUSB = le16_to_cpu(desc->bcdUSB);
0317     u16 bcdDevice = le16_to_cpu(desc->bcdDevice);
0318 
0319     if (start > end)
0320         return start;
0321     start += sprintf(start, format_device1,
0322               bcdUSB >> 8, bcdUSB & 0xff,
0323               desc->bDeviceClass,
0324               class_decode(desc->bDeviceClass),
0325               desc->bDeviceSubClass,
0326               desc->bDeviceProtocol,
0327               desc->bMaxPacketSize0,
0328               desc->bNumConfigurations);
0329     if (start > end)
0330         return start;
0331     start += sprintf(start, format_device2,
0332              le16_to_cpu(desc->idVendor),
0333              le16_to_cpu(desc->idProduct),
0334              bcdDevice >> 8, bcdDevice & 0xff);
0335     return start;
0336 }
0337 
0338 /*
0339  * Dump the different strings that this device holds.
0340  */
0341 static char *usb_dump_device_strings(char *start, char *end,
0342                      struct usb_device *dev)
0343 {
0344     if (start > end)
0345         return start;
0346     if (dev->manufacturer)
0347         start += sprintf(start, format_string_manufacturer,
0348                  dev->manufacturer);
0349     if (start > end)
0350         goto out;
0351     if (dev->product)
0352         start += sprintf(start, format_string_product, dev->product);
0353     if (start > end)
0354         goto out;
0355 #ifdef ALLOW_SERIAL_NUMBER
0356     if (dev->serial)
0357         start += sprintf(start, format_string_serialnumber,
0358                  dev->serial);
0359 #endif
0360  out:
0361     return start;
0362 }
0363 
0364 static char *usb_dump_desc(char *start, char *end, struct usb_device *dev)
0365 {
0366     int i;
0367 
0368     start = usb_dump_device_descriptor(start, end, &dev->descriptor);
0369 
0370     start = usb_dump_device_strings(start, end, dev);
0371 
0372     for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
0373         start = usb_dump_config(dev->speed,
0374                 start, end, dev->config + i,
0375                 /* active ? */
0376                 (dev->config + i) == dev->actconfig);
0377     }
0378     return start;
0379 }
0380 
0381 /*****************************************************************/
0382 
0383 /* This is a recursive function. Parameters:
0384  * buffer - the user-space buffer to write data into
0385  * nbytes - the maximum number of bytes to write
0386  * skip_bytes - the number of bytes to skip before writing anything
0387  * file_offset - the offset into the devices file on completion
0388  * The caller must own the device lock.
0389  */
0390 static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes,
0391                    loff_t *skip_bytes, loff_t *file_offset,
0392                    struct usb_device *usbdev, struct usb_bus *bus,
0393                    int level, int index, int count)
0394 {
0395     int chix;
0396     int ret, cnt = 0;
0397     int parent_devnum = 0;
0398     char *pages_start, *data_end, *speed;
0399     unsigned int length;
0400     ssize_t total_written = 0;
0401     struct usb_device *childdev = NULL;
0402 
0403     /* don't bother with anything else if we're not writing any data */
0404     if (*nbytes <= 0)
0405         return 0;
0406 
0407     if (level > MAX_TOPO_LEVEL)
0408         return 0;
0409     /* allocate 2^1 pages = 8K (on i386);
0410      * should be more than enough for one device */
0411     pages_start = (char *)__get_free_pages(GFP_NOIO, 1);
0412     if (!pages_start)
0413         return -ENOMEM;
0414 
0415     if (usbdev->parent && usbdev->parent->devnum != -1)
0416         parent_devnum = usbdev->parent->devnum;
0417     /*
0418      * So the root hub's parent is 0 and any device that is
0419      * plugged into the root hub has a parent of 0.
0420      */
0421     switch (usbdev->speed) {
0422     case USB_SPEED_LOW:
0423         speed = "1.5"; break;
0424     case USB_SPEED_UNKNOWN:     /* usb 1.1 root hub code */
0425     case USB_SPEED_FULL:
0426         speed = "12"; break;
0427     case USB_SPEED_WIRELESS:    /* Wireless has no real fixed speed */
0428     case USB_SPEED_HIGH:
0429         speed = "480"; break;
0430     case USB_SPEED_SUPER:
0431         speed = "5000"; break;
0432     case USB_SPEED_SUPER_PLUS:
0433         speed = "10000"; break;
0434     default:
0435         speed = "??";
0436     }
0437     data_end = pages_start + sprintf(pages_start, format_topo,
0438             bus->busnum, level, parent_devnum,
0439             index, count, usbdev->devnum,
0440             speed, usbdev->maxchild);
0441     /*
0442      * level = topology-tier level;
0443      * parent_devnum = parent device number;
0444      * index = parent's connector number;
0445      * count = device count at this level
0446      */
0447     /* If this is the root hub, display the bandwidth information */
0448     if (level == 0) {
0449         int max;
0450 
0451         /* super/high speed reserves 80%, full/low reserves 90% */
0452         if (usbdev->speed == USB_SPEED_HIGH ||
0453             usbdev->speed >= USB_SPEED_SUPER)
0454             max = 800;
0455         else
0456             max = FRAME_TIME_MAX_USECS_ALLOC;
0457 
0458         /* report "average" periodic allocation over a microsecond.
0459          * the schedules are actually bursty, HCDs need to deal with
0460          * that and just compute/report this average.
0461          */
0462         data_end += sprintf(data_end, format_bandwidth,
0463                 bus->bandwidth_allocated, max,
0464                 (100 * bus->bandwidth_allocated + max / 2)
0465                     / max,
0466                 bus->bandwidth_int_reqs,
0467                 bus->bandwidth_isoc_reqs);
0468 
0469     }
0470     data_end = usb_dump_desc(data_end, pages_start + (2 * PAGE_SIZE) - 256,
0471                  usbdev);
0472 
0473     if (data_end > (pages_start + (2 * PAGE_SIZE) - 256))
0474         data_end += sprintf(data_end, "(truncated)\n");
0475 
0476     length = data_end - pages_start;
0477     /* if we can start copying some data to the user */
0478     if (length > *skip_bytes) {
0479         length -= *skip_bytes;
0480         if (length > *nbytes)
0481             length = *nbytes;
0482         if (copy_to_user(*buffer, pages_start + *skip_bytes, length)) {
0483             free_pages((unsigned long)pages_start, 1);
0484             return -EFAULT;
0485         }
0486         *nbytes -= length;
0487         *file_offset += length;
0488         total_written += length;
0489         *buffer += length;
0490         *skip_bytes = 0;
0491     } else
0492         *skip_bytes -= length;
0493 
0494     free_pages((unsigned long)pages_start, 1);
0495 
0496     /* Now look at all of this device's children. */
0497     usb_hub_for_each_child(usbdev, chix, childdev) {
0498         usb_lock_device(childdev);
0499         ret = usb_device_dump(buffer, nbytes, skip_bytes,
0500                       file_offset, childdev, bus,
0501                       level + 1, chix - 1, ++cnt);
0502         usb_unlock_device(childdev);
0503         if (ret == -EFAULT)
0504             return total_written;
0505         total_written += ret;
0506     }
0507     return total_written;
0508 }
0509 
0510 static ssize_t usb_device_read(struct file *file, char __user *buf,
0511                    size_t nbytes, loff_t *ppos)
0512 {
0513     struct usb_bus *bus;
0514     ssize_t ret, total_written = 0;
0515     loff_t skip_bytes = *ppos;
0516     int id;
0517 
0518     if (*ppos < 0)
0519         return -EINVAL;
0520     if (nbytes <= 0)
0521         return 0;
0522 
0523     mutex_lock(&usb_bus_idr_lock);
0524     /* print devices for all busses */
0525     idr_for_each_entry(&usb_bus_idr, bus, id) {
0526         /* recurse through all children of the root hub */
0527         if (!bus_to_hcd(bus)->rh_registered)
0528             continue;
0529         usb_lock_device(bus->root_hub);
0530         ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos,
0531                       bus->root_hub, bus, 0, 0, 0);
0532         usb_unlock_device(bus->root_hub);
0533         if (ret < 0) {
0534             mutex_unlock(&usb_bus_idr_lock);
0535             return ret;
0536         }
0537         total_written += ret;
0538     }
0539     mutex_unlock(&usb_bus_idr_lock);
0540     return total_written;
0541 }
0542 
0543 const struct file_operations usbfs_devices_fops = {
0544     .llseek =   no_seek_end_llseek,
0545     .read =     usb_device_read,
0546 };