1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * devices.c 4 * (C) Copyright 1999 Randy Dunlap. 5 * (C) Copyright 1999,2000 Thomas Sailer <sailer@ife.ee.ethz.ch>. 6 * (proc file per device) 7 * (C) Copyright 1999 Deti Fliegl (new USB architecture) 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 /dev/bus/usb/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 /sys/kernel/debug/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 53 #include <linux/fs.h> 54 #include <linux/mm.h> 55 #include <linux/gfp.h> 56 #include <linux/poll.h> 57 #include <linux/usb.h> 58 #include <linux/usbdevice_fs.h> 59 #include <linux/usb/hcd.h> 60 #include <linux/mutex.h> 61 #include <linux/uaccess.h> 62 63 #include "usb.h" 64 65 /* Define ALLOW_SERIAL_NUMBER if you want to see the serial number of devices */ 66 #define ALLOW_SERIAL_NUMBER 67 68 static const char format_topo[] = 69 /* T: Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=dddd MxCh=dd */ 70 "\nT: Bus=%2.2d Lev=%2.2d Prnt=%2.2d Port=%2.2d Cnt=%2.2d Dev#=%3d Spd=%-4s MxCh=%2d\n"; 71 72 static const char format_string_manufacturer[] = 73 /* S: Manufacturer=xxxx */ 74 "S: Manufacturer=%.100s\n"; 75 76 static const char format_string_product[] = 77 /* S: Product=xxxx */ 78 "S: Product=%.100s\n"; 79 80 #ifdef ALLOW_SERIAL_NUMBER 81 static const char format_string_serialnumber[] = 82 /* S: SerialNumber=xxxx */ 83 "S: SerialNumber=%.100s\n"; 84 #endif 85 86 static const char format_bandwidth[] = 87 /* B: Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd */ 88 "B: Alloc=%3d/%3d us (%2d%%), #Int=%3d, #Iso=%3d\n"; 89 90 static const char format_device1[] = 91 /* D: Ver=xx.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd */ 92 "D: Ver=%2x.%02x Cls=%02x(%-5s) Sub=%02x Prot=%02x MxPS=%2d #Cfgs=%3d\n"; 93 94 static const char format_device2[] = 95 /* P: Vendor=xxxx ProdID=xxxx Rev=xx.xx */ 96 "P: Vendor=%04x ProdID=%04x Rev=%2x.%02x\n"; 97 98 static const char format_config[] = 99 /* C: #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA */ 100 "C:%c #Ifs=%2d Cfg#=%2d Atr=%02x MxPwr=%3dmA\n"; 101 102 static const char format_iad[] = 103 /* A: FirstIf#=dd IfCount=dd Cls=xx(sssss) Sub=xx Prot=xx */ 104 "A: FirstIf#=%2d IfCount=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x\n"; 105 106 static const char format_iface[] = 107 /* I: If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=xxxx*/ 108 "I:%c If#=%2d Alt=%2d #EPs=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x Driver=%s\n"; 109 110 static const char format_endpt[] = 111 /* E: Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=D?s */ 112 "E: Ad=%02x(%c) Atr=%02x(%-4s) MxPS=%4d Ivl=%d%cs\n"; 113 114 /* 115 * Wait for an connect/disconnect event to happen. We initialize 116 * the event counter with an odd number, and each event will increment 117 * the event counter by two, so it will always _stay_ odd. That means 118 * that it will never be zero, so "event 0" will never match a current 119 * event, and thus 'poll' will always trigger as readable for the first 120 * time it gets called. 121 */ 122 static struct device_connect_event { 123 atomic_t count; 124 wait_queue_head_t wait; 125 } device_event = { 126 .count = ATOMIC_INIT(1), 127 .wait = __WAIT_QUEUE_HEAD_INITIALIZER(device_event.wait) 128 }; 129 130 struct class_info { 131 int class; 132 char *class_name; 133 }; 134 135 static const struct class_info clas_info[] = { 136 /* max. 5 chars. per name string */ 137 {USB_CLASS_PER_INTERFACE, ">ifc"}, 138 {USB_CLASS_AUDIO, "audio"}, 139 {USB_CLASS_COMM, "comm."}, 140 {USB_CLASS_HID, "HID"}, 141 {USB_CLASS_PHYSICAL, "PID"}, 142 {USB_CLASS_STILL_IMAGE, "still"}, 143 {USB_CLASS_PRINTER, "print"}, 144 {USB_CLASS_MASS_STORAGE, "stor."}, 145 {USB_CLASS_HUB, "hub"}, 146 {USB_CLASS_CDC_DATA, "data"}, 147 {USB_CLASS_CSCID, "scard"}, 148 {USB_CLASS_CONTENT_SEC, "c-sec"}, 149 {USB_CLASS_VIDEO, "video"}, 150 {USB_CLASS_WIRELESS_CONTROLLER, "wlcon"}, 151 {USB_CLASS_MISC, "misc"}, 152 {USB_CLASS_APP_SPEC, "app."}, 153 {USB_CLASS_VENDOR_SPEC, "vend."}, 154 {-1, "unk."} /* leave as last */ 155 }; 156 157 /*****************************************************************/ 158 159 void usbfs_conn_disc_event(void) 160 { 161 atomic_add(2, &device_event.count); 162 wake_up(&device_event.wait); 163 } 164 165 static const char *class_decode(const int class) 166 { 167 int ix; 168 169 for (ix = 0; clas_info[ix].class != -1; ix++) 170 if (clas_info[ix].class == class) 171 break; 172 return clas_info[ix].class_name; 173 } 174 175 static char *usb_dump_endpoint_descriptor(int speed, char *start, char *end, 176 const struct usb_endpoint_descriptor *desc) 177 { 178 char dir, unit, *type; 179 unsigned interval, bandwidth = 1; 180 181 if (start > end) 182 return start; 183 184 dir = usb_endpoint_dir_in(desc) ? 'I' : 'O'; 185 186 if (speed == USB_SPEED_HIGH) 187 bandwidth = usb_endpoint_maxp_mult(desc); 188 189 /* this isn't checking for illegal values */ 190 switch (usb_endpoint_type(desc)) { 191 case USB_ENDPOINT_XFER_CONTROL: 192 type = "Ctrl"; 193 if (speed == USB_SPEED_HIGH) /* uframes per NAK */ 194 interval = desc->bInterval; 195 else 196 interval = 0; 197 dir = 'B'; /* ctrl is bidirectional */ 198 break; 199 case USB_ENDPOINT_XFER_ISOC: 200 type = "Isoc"; 201 interval = 1 << (desc->bInterval - 1); 202 break; 203 case USB_ENDPOINT_XFER_BULK: 204 type = "Bulk"; 205 if (speed == USB_SPEED_HIGH && dir == 'O') /* uframes per NAK */ 206 interval = desc->bInterval; 207 else 208 interval = 0; 209 break; 210 case USB_ENDPOINT_XFER_INT: 211 type = "Int."; 212 if (speed == USB_SPEED_HIGH || speed >= USB_SPEED_SUPER) 213 interval = 1 << (desc->bInterval - 1); 214 else 215 interval = desc->bInterval; 216 break; 217 default: /* "can't happen" */ 218 return start; 219 } 220 interval *= (speed == USB_SPEED_HIGH || 221 speed >= USB_SPEED_SUPER) ? 125 : 1000; 222 if (interval % 1000) 223 unit = 'u'; 224 else { 225 unit = 'm'; 226 interval /= 1000; 227 } 228 229 start += sprintf(start, format_endpt, desc->bEndpointAddress, dir, 230 desc->bmAttributes, type, 231 usb_endpoint_maxp(desc) * 232 bandwidth, 233 interval, unit); 234 return start; 235 } 236 237 static char *usb_dump_interface_descriptor(char *start, char *end, 238 const struct usb_interface_cache *intfc, 239 const struct usb_interface *iface, 240 int setno) 241 { 242 const struct usb_interface_descriptor *desc; 243 const char *driver_name = ""; 244 int active = 0; 245 246 if (start > end) 247 return start; 248 desc = &intfc->altsetting[setno].desc; 249 if (iface) { 250 driver_name = (iface->dev.driver 251 ? iface->dev.driver->name 252 : "(none)"); 253 active = (desc == &iface->cur_altsetting->desc); 254 } 255 start += sprintf(start, format_iface, 256 active ? '*' : ' ', /* mark active altsetting */ 257 desc->bInterfaceNumber, 258 desc->bAlternateSetting, 259 desc->bNumEndpoints, 260 desc->bInterfaceClass, 261 class_decode(desc->bInterfaceClass), 262 desc->bInterfaceSubClass, 263 desc->bInterfaceProtocol, 264 driver_name); 265 return start; 266 } 267 268 static char *usb_dump_interface(int speed, char *start, char *end, 269 const struct usb_interface_cache *intfc, 270 const struct usb_interface *iface, int setno) 271 { 272 const struct usb_host_interface *desc = &intfc->altsetting[setno]; 273 int i; 274 275 start = usb_dump_interface_descriptor(start, end, intfc, iface, setno); 276 for (i = 0; i < desc->desc.bNumEndpoints; i++) { 277 if (start > end) 278 return start; 279 start = usb_dump_endpoint_descriptor(speed, 280 start, end, &desc->endpoint[i].desc); 281 } 282 return start; 283 } 284 285 static char *usb_dump_iad_descriptor(char *start, char *end, 286 const struct usb_interface_assoc_descriptor *iad) 287 { 288 if (start > end) 289 return start; 290 start += sprintf(start, format_iad, 291 iad->bFirstInterface, 292 iad->bInterfaceCount, 293 iad->bFunctionClass, 294 class_decode(iad->bFunctionClass), 295 iad->bFunctionSubClass, 296 iad->bFunctionProtocol); 297 return start; 298 } 299 300 /* TBD: 301 * 0. TBDs 302 * 1. marking active interface altsettings (code lists all, but should mark 303 * which ones are active, if any) 304 */ 305 static char *usb_dump_config_descriptor(char *start, char *end, 306 const struct usb_config_descriptor *desc, 307 int active, int speed) 308 { 309 int mul; 310 311 if (start > end) 312 return start; 313 if (speed >= USB_SPEED_SUPER) 314 mul = 8; 315 else 316 mul = 2; 317 start += sprintf(start, format_config, 318 /* mark active/actual/current cfg. */ 319 active ? '*' : ' ', 320 desc->bNumInterfaces, 321 desc->bConfigurationValue, 322 desc->bmAttributes, 323 desc->bMaxPower * mul); 324 return start; 325 } 326 327 static char *usb_dump_config(int speed, char *start, char *end, 328 const struct usb_host_config *config, int active) 329 { 330 int i, j; 331 struct usb_interface_cache *intfc; 332 struct usb_interface *interface; 333 334 if (start > end) 335 return start; 336 if (!config) 337 /* getting these some in 2.3.7; none in 2.3.6 */ 338 return start + sprintf(start, "(null Cfg. desc.)\n"); 339 start = usb_dump_config_descriptor(start, end, &config->desc, active, 340 speed); 341 for (i = 0; i < USB_MAXIADS; i++) { 342 if (config->intf_assoc[i] == NULL) 343 break; 344 start = usb_dump_iad_descriptor(start, end, 345 config->intf_assoc[i]); 346 } 347 for (i = 0; i < config->desc.bNumInterfaces; i++) { 348 intfc = config->intf_cache[i]; 349 interface = config->interface[i]; 350 for (j = 0; j < intfc->num_altsetting; j++) { 351 if (start > end) 352 return start; 353 start = usb_dump_interface(speed, 354 start, end, intfc, interface, j); 355 } 356 } 357 return start; 358 } 359 360 /* 361 * Dump the different USB descriptors. 362 */ 363 static char *usb_dump_device_descriptor(char *start, char *end, 364 const struct usb_device_descriptor *desc) 365 { 366 u16 bcdUSB = le16_to_cpu(desc->bcdUSB); 367 u16 bcdDevice = le16_to_cpu(desc->bcdDevice); 368 369 if (start > end) 370 return start; 371 start += sprintf(start, format_device1, 372 bcdUSB >> 8, bcdUSB & 0xff, 373 desc->bDeviceClass, 374 class_decode(desc->bDeviceClass), 375 desc->bDeviceSubClass, 376 desc->bDeviceProtocol, 377 desc->bMaxPacketSize0, 378 desc->bNumConfigurations); 379 if (start > end) 380 return start; 381 start += sprintf(start, format_device2, 382 le16_to_cpu(desc->idVendor), 383 le16_to_cpu(desc->idProduct), 384 bcdDevice >> 8, bcdDevice & 0xff); 385 return start; 386 } 387 388 /* 389 * Dump the different strings that this device holds. 390 */ 391 static char *usb_dump_device_strings(char *start, char *end, 392 struct usb_device *dev) 393 { 394 if (start > end) 395 return start; 396 if (dev->manufacturer) 397 start += sprintf(start, format_string_manufacturer, 398 dev->manufacturer); 399 if (start > end) 400 goto out; 401 if (dev->product) 402 start += sprintf(start, format_string_product, dev->product); 403 if (start > end) 404 goto out; 405 #ifdef ALLOW_SERIAL_NUMBER 406 if (dev->serial) 407 start += sprintf(start, format_string_serialnumber, 408 dev->serial); 409 #endif 410 out: 411 return start; 412 } 413 414 static char *usb_dump_desc(char *start, char *end, struct usb_device *dev) 415 { 416 int i; 417 418 if (start > end) 419 return start; 420 421 start = usb_dump_device_descriptor(start, end, &dev->descriptor); 422 423 if (start > end) 424 return start; 425 426 start = usb_dump_device_strings(start, end, dev); 427 428 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { 429 if (start > end) 430 return start; 431 start = usb_dump_config(dev->speed, 432 start, end, dev->config + i, 433 /* active ? */ 434 (dev->config + i) == dev->actconfig); 435 } 436 return start; 437 } 438 439 440 #ifdef PROC_EXTRA /* TBD: may want to add this code later */ 441 442 static char *usb_dump_hub_descriptor(char *start, char *end, 443 const struct usb_hub_descriptor *desc) 444 { 445 int leng = USB_DT_HUB_NONVAR_SIZE; 446 unsigned char *ptr = (unsigned char *)desc; 447 448 if (start > end) 449 return start; 450 start += sprintf(start, "Interface:"); 451 while (leng && start <= end) { 452 start += sprintf(start, " %02x", *ptr); 453 ptr++; leng--; 454 } 455 *start++ = '\n'; 456 return start; 457 } 458 459 static char *usb_dump_string(char *start, char *end, 460 const struct usb_device *dev, char *id, int index) 461 { 462 if (start > end) 463 return start; 464 start += sprintf(start, "Interface:"); 465 if (index <= dev->maxstring && dev->stringindex && 466 dev->stringindex[index]) 467 start += sprintf(start, "%s: %.100s ", id, 468 dev->stringindex[index]); 469 return start; 470 } 471 472 #endif /* PROC_EXTRA */ 473 474 /*****************************************************************/ 475 476 /* This is a recursive function. Parameters: 477 * buffer - the user-space buffer to write data into 478 * nbytes - the maximum number of bytes to write 479 * skip_bytes - the number of bytes to skip before writing anything 480 * file_offset - the offset into the devices file on completion 481 * The caller must own the device lock. 482 */ 483 static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes, 484 loff_t *skip_bytes, loff_t *file_offset, 485 struct usb_device *usbdev, struct usb_bus *bus, 486 int level, int index, int count) 487 { 488 int chix; 489 int ret, cnt = 0; 490 int parent_devnum = 0; 491 char *pages_start, *data_end, *speed; 492 unsigned int length; 493 ssize_t total_written = 0; 494 struct usb_device *childdev = NULL; 495 496 /* don't bother with anything else if we're not writing any data */ 497 if (*nbytes <= 0) 498 return 0; 499 500 if (level > MAX_TOPO_LEVEL) 501 return 0; 502 /* allocate 2^1 pages = 8K (on i386); 503 * should be more than enough for one device */ 504 pages_start = (char *)__get_free_pages(GFP_NOIO, 1); 505 if (!pages_start) 506 return -ENOMEM; 507 508 if (usbdev->parent && usbdev->parent->devnum != -1) 509 parent_devnum = usbdev->parent->devnum; 510 /* 511 * So the root hub's parent is 0 and any device that is 512 * plugged into the root hub has a parent of 0. 513 */ 514 switch (usbdev->speed) { 515 case USB_SPEED_LOW: 516 speed = "1.5"; break; 517 case USB_SPEED_UNKNOWN: /* usb 1.1 root hub code */ 518 case USB_SPEED_FULL: 519 speed = "12"; break; 520 case USB_SPEED_WIRELESS: /* Wireless has no real fixed speed */ 521 case USB_SPEED_HIGH: 522 speed = "480"; break; 523 case USB_SPEED_SUPER: 524 speed = "5000"; break; 525 case USB_SPEED_SUPER_PLUS: 526 speed = "10000"; break; 527 default: 528 speed = "??"; 529 } 530 data_end = pages_start + sprintf(pages_start, format_topo, 531 bus->busnum, level, parent_devnum, 532 index, count, usbdev->devnum, 533 speed, usbdev->maxchild); 534 /* 535 * level = topology-tier level; 536 * parent_devnum = parent device number; 537 * index = parent's connector number; 538 * count = device count at this level 539 */ 540 /* If this is the root hub, display the bandwidth information */ 541 if (level == 0) { 542 int max; 543 544 /* super/high speed reserves 80%, full/low reserves 90% */ 545 if (usbdev->speed == USB_SPEED_HIGH || 546 usbdev->speed >= USB_SPEED_SUPER) 547 max = 800; 548 else 549 max = FRAME_TIME_MAX_USECS_ALLOC; 550 551 /* report "average" periodic allocation over a microsecond. 552 * the schedules are actually bursty, HCDs need to deal with 553 * that and just compute/report this average. 554 */ 555 data_end += sprintf(data_end, format_bandwidth, 556 bus->bandwidth_allocated, max, 557 (100 * bus->bandwidth_allocated + max / 2) 558 / max, 559 bus->bandwidth_int_reqs, 560 bus->bandwidth_isoc_reqs); 561 562 } 563 data_end = usb_dump_desc(data_end, pages_start + (2 * PAGE_SIZE) - 256, 564 usbdev); 565 566 if (data_end > (pages_start + (2 * PAGE_SIZE) - 256)) 567 data_end += sprintf(data_end, "(truncated)\n"); 568 569 length = data_end - pages_start; 570 /* if we can start copying some data to the user */ 571 if (length > *skip_bytes) { 572 length -= *skip_bytes; 573 if (length > *nbytes) 574 length = *nbytes; 575 if (copy_to_user(*buffer, pages_start + *skip_bytes, length)) { 576 free_pages((unsigned long)pages_start, 1); 577 return -EFAULT; 578 } 579 *nbytes -= length; 580 *file_offset += length; 581 total_written += length; 582 *buffer += length; 583 *skip_bytes = 0; 584 } else 585 *skip_bytes -= length; 586 587 free_pages((unsigned long)pages_start, 1); 588 589 /* Now look at all of this device's children. */ 590 usb_hub_for_each_child(usbdev, chix, childdev) { 591 usb_lock_device(childdev); 592 ret = usb_device_dump(buffer, nbytes, skip_bytes, 593 file_offset, childdev, bus, 594 level + 1, chix - 1, ++cnt); 595 usb_unlock_device(childdev); 596 if (ret == -EFAULT) 597 return total_written; 598 total_written += ret; 599 } 600 return total_written; 601 } 602 603 static ssize_t usb_device_read(struct file *file, char __user *buf, 604 size_t nbytes, loff_t *ppos) 605 { 606 struct usb_bus *bus; 607 ssize_t ret, total_written = 0; 608 loff_t skip_bytes = *ppos; 609 int id; 610 611 if (*ppos < 0) 612 return -EINVAL; 613 if (nbytes <= 0) 614 return 0; 615 if (!access_ok(VERIFY_WRITE, buf, nbytes)) 616 return -EFAULT; 617 618 mutex_lock(&usb_bus_idr_lock); 619 /* print devices for all busses */ 620 idr_for_each_entry(&usb_bus_idr, bus, id) { 621 /* recurse through all children of the root hub */ 622 if (!bus_to_hcd(bus)->rh_registered) 623 continue; 624 usb_lock_device(bus->root_hub); 625 ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, 626 bus->root_hub, bus, 0, 0, 0); 627 usb_unlock_device(bus->root_hub); 628 if (ret < 0) { 629 mutex_unlock(&usb_bus_idr_lock); 630 return ret; 631 } 632 total_written += ret; 633 } 634 mutex_unlock(&usb_bus_idr_lock); 635 return total_written; 636 } 637 638 /* Kernel lock for "lastev" protection */ 639 static unsigned int usb_device_poll(struct file *file, 640 struct poll_table_struct *wait) 641 { 642 unsigned int event_count; 643 644 poll_wait(file, &device_event.wait, wait); 645 646 event_count = atomic_read(&device_event.count); 647 if (file->f_version != event_count) { 648 file->f_version = event_count; 649 return POLLIN | POLLRDNORM; 650 } 651 652 return 0; 653 } 654 655 const struct file_operations usbfs_devices_fops = { 656 .llseek = no_seek_end_llseek, 657 .read = usb_device_read, 658 .poll = usb_device_poll, 659 }; 660