devices.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. *************************************************************
  10. *
  11. * <mountpoint>/devices contains USB topology, device, config, class,
  12. * interface, & endpoint data.
  13. *
  14. * I considered using /dev/bus/usb/device# for each device
  15. * as it is attached or detached, but I didn't like this for some
  16. * reason -- maybe it's just too deep of a directory structure.
  17. * I also don't like looking in multiple places to gather and view
  18. * the data. Having only one file for ./devices also prevents race
  19. * conditions that could arise if a program was reading device info
  20. * for devices that are being removed (unplugged). (That is, the
  21. * program may find a directory for devnum_12 then try to open it,
  22. * but it was just unplugged, so the directory is now deleted.
  23. * But programs would just have to be prepared for situations like
  24. * this in any plug-and-play environment.)
  25. *
  26. * 1999-12-16: Thomas Sailer <sailer@ife.ee.ethz.ch>
  27. * Converted the whole proc stuff to real
  28. * read methods. Now not the whole device list needs to fit
  29. * into one page, only the device list for one bus.
  30. * Added a poll method to /sys/kernel/debug/usb/devices, to wake
  31. * up an eventual usbd
  32. * 2000-01-04: Thomas Sailer <sailer@ife.ee.ethz.ch>
  33. * Turned into its own filesystem
  34. * 2000-07-05: Ashley Montanaro <ashley@compsoc.man.ac.uk>
  35. * Converted file reading routine to dump to buffer once
  36. * per device, not per bus
  37. */
  38. #include <linux/fs.h>
  39. #include <linux/mm.h>
  40. #include <linux/gfp.h>
  41. #include <linux/usb.h>
  42. #include <linux/usbdevice_fs.h>
  43. #include <linux/usb/hcd.h>
  44. #include <linux/mutex.h>
  45. #include <linux/uaccess.h>
  46. #include "usb.h"
  47. /* Define ALLOW_SERIAL_NUMBER if you want to see the serial number of devices */
  48. #define ALLOW_SERIAL_NUMBER
  49. static const char format_topo[] =
  50. /* T: Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=dddd MxCh=dd */
  51. "\nT: Bus=%2.2d Lev=%2.2d Prnt=%2.2d Port=%2.2d Cnt=%2.2d Dev#=%3d Spd=%-4s MxCh=%2d\n";
  52. static const char format_string_manufacturer[] =
  53. /* S: Manufacturer=xxxx */
  54. "S: Manufacturer=%.100s\n";
  55. static const char format_string_product[] =
  56. /* S: Product=xxxx */
  57. "S: Product=%.100s\n";
  58. #ifdef ALLOW_SERIAL_NUMBER
  59. static const char format_string_serialnumber[] =
  60. /* S: SerialNumber=xxxx */
  61. "S: SerialNumber=%.100s\n";
  62. #endif
  63. static const char format_bandwidth[] =
  64. /* B: Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd */
  65. "B: Alloc=%3d/%3d us (%2d%%), #Int=%3d, #Iso=%3d\n";
  66. static const char format_device1[] =
  67. /* D: Ver=xx.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd */
  68. "D: Ver=%2x.%02x Cls=%02x(%-5s) Sub=%02x Prot=%02x MxPS=%2d #Cfgs=%3d\n";
  69. static const char format_device2[] =
  70. /* P: Vendor=xxxx ProdID=xxxx Rev=xx.xx */
  71. "P: Vendor=%04x ProdID=%04x Rev=%2x.%02x\n";
  72. static const char format_config[] =
  73. /* C: #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA */
  74. "C:%c #Ifs=%2d Cfg#=%2d Atr=%02x MxPwr=%3dmA\n";
  75. static const char format_iad[] =
  76. /* A: FirstIf#=dd IfCount=dd Cls=xx(sssss) Sub=xx Prot=xx */
  77. "A: FirstIf#=%2d IfCount=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x\n";
  78. static const char format_iface[] =
  79. /* I: If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=xxxx*/
  80. "I:%c If#=%2d Alt=%2d #EPs=%2d Cls=%02x(%-5s) Sub=%02x Prot=%02x Driver=%s\n";
  81. static const char format_endpt[] =
  82. /* E: Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=D?s */
  83. "E: Ad=%02x(%c) Atr=%02x(%-4s) MxPS=%4d Ivl=%d%cs\n";
  84. struct class_info {
  85. int class;
  86. char *class_name;
  87. };
  88. static const struct class_info clas_info[] = {
  89. /* max. 5 chars. per name string */
  90. {USB_CLASS_PER_INTERFACE, ">ifc"},
  91. {USB_CLASS_AUDIO, "audio"},
  92. {USB_CLASS_COMM, "comm."},
  93. {USB_CLASS_HID, "HID"},
  94. {USB_CLASS_PHYSICAL, "PID"},
  95. {USB_CLASS_STILL_IMAGE, "still"},
  96. {USB_CLASS_PRINTER, "print"},
  97. {USB_CLASS_MASS_STORAGE, "stor."},
  98. {USB_CLASS_HUB, "hub"},
  99. {USB_CLASS_CDC_DATA, "data"},
  100. {USB_CLASS_CSCID, "scard"},
  101. {USB_CLASS_CONTENT_SEC, "c-sec"},
  102. {USB_CLASS_VIDEO, "video"},
  103. {USB_CLASS_PERSONAL_HEALTHCARE, "perhc"},
  104. {USB_CLASS_AUDIO_VIDEO, "av"},
  105. {USB_CLASS_BILLBOARD, "blbrd"},
  106. {USB_CLASS_USB_TYPE_C_BRIDGE, "bridg"},
  107. {USB_CLASS_WIRELESS_CONTROLLER, "wlcon"},
  108. {USB_CLASS_MISC, "misc"},
  109. {USB_CLASS_APP_SPEC, "app."},
  110. {USB_CLASS_VENDOR_SPEC, "vend."},
  111. {-1, "unk."} /* leave as last */
  112. };
  113. /*****************************************************************/
  114. static const char *class_decode(const int class)
  115. {
  116. int ix;
  117. for (ix = 0; clas_info[ix].class != -1; ix++)
  118. if (clas_info[ix].class == class)
  119. break;
  120. return clas_info[ix].class_name;
  121. }
  122. static char *usb_dump_endpoint_descriptor(int speed, char *start, char *end,
  123. const struct usb_endpoint_descriptor *desc)
  124. {
  125. char dir, unit, *type;
  126. unsigned interval, bandwidth = 1;
  127. if (start > end)
  128. return start;
  129. dir = usb_endpoint_dir_in(desc) ? 'I' : 'O';
  130. if (speed == USB_SPEED_HIGH)
  131. bandwidth = usb_endpoint_maxp_mult(desc);
  132. /* this isn't checking for illegal values */
  133. switch (usb_endpoint_type(desc)) {
  134. case USB_ENDPOINT_XFER_CONTROL:
  135. type = "Ctrl";
  136. dir = 'B'; /* ctrl is bidirectional */
  137. break;
  138. case USB_ENDPOINT_XFER_ISOC:
  139. type = "Isoc";
  140. break;
  141. case USB_ENDPOINT_XFER_BULK:
  142. type = "Bulk";
  143. break;
  144. case USB_ENDPOINT_XFER_INT:
  145. type = "Int.";
  146. break;
  147. default: /* "can't happen" */
  148. return start;
  149. }
  150. interval = usb_decode_interval(desc, speed);
  151. if (interval % 1000) {
  152. unit = 'u';
  153. } else {
  154. unit = 'm';
  155. interval /= 1000;
  156. }
  157. start += sprintf(start, format_endpt, desc->bEndpointAddress, dir,
  158. desc->bmAttributes, type,
  159. usb_endpoint_maxp(desc) *
  160. bandwidth,
  161. interval, unit);
  162. return start;
  163. }
  164. static char *usb_dump_interface_descriptor(char *start, char *end,
  165. const struct usb_interface_cache *intfc,
  166. const struct usb_interface *iface,
  167. int setno)
  168. {
  169. const struct usb_interface_descriptor *desc;
  170. const char *driver_name = "";
  171. int active = 0;
  172. if (start > end)
  173. return start;
  174. desc = &intfc->altsetting[setno].desc;
  175. if (iface) {
  176. driver_name = (iface->dev.driver
  177. ? iface->dev.driver->name
  178. : "(none)");
  179. active = (desc == &iface->cur_altsetting->desc);
  180. }
  181. start += sprintf(start, format_iface,
  182. active ? '*' : ' ', /* mark active altsetting */
  183. desc->bInterfaceNumber,
  184. desc->bAlternateSetting,
  185. desc->bNumEndpoints,
  186. desc->bInterfaceClass,
  187. class_decode(desc->bInterfaceClass),
  188. desc->bInterfaceSubClass,
  189. desc->bInterfaceProtocol,
  190. driver_name);
  191. return start;
  192. }
  193. static char *usb_dump_interface(int speed, char *start, char *end,
  194. const struct usb_interface_cache *intfc,
  195. const struct usb_interface *iface, int setno)
  196. {
  197. const struct usb_host_interface *desc = &intfc->altsetting[setno];
  198. int i;
  199. start = usb_dump_interface_descriptor(start, end, intfc, iface, setno);
  200. for (i = 0; i < desc->desc.bNumEndpoints; i++) {
  201. if (start > end)
  202. return start;
  203. start = usb_dump_endpoint_descriptor(speed,
  204. start, end, &desc->endpoint[i].desc);
  205. }
  206. return start;
  207. }
  208. static char *usb_dump_iad_descriptor(char *start, char *end,
  209. const struct usb_interface_assoc_descriptor *iad)
  210. {
  211. if (start > end)
  212. return start;
  213. start += sprintf(start, format_iad,
  214. iad->bFirstInterface,
  215. iad->bInterfaceCount,
  216. iad->bFunctionClass,
  217. class_decode(iad->bFunctionClass),
  218. iad->bFunctionSubClass,
  219. iad->bFunctionProtocol);
  220. return start;
  221. }
  222. /* TBD:
  223. * 0. TBDs
  224. * 1. marking active interface altsettings (code lists all, but should mark
  225. * which ones are active, if any)
  226. */
  227. static char *usb_dump_config_descriptor(char *start, char *end,
  228. const struct usb_config_descriptor *desc,
  229. int active, int speed)
  230. {
  231. int mul;
  232. if (start > end)
  233. return start;
  234. if (speed >= USB_SPEED_SUPER)
  235. mul = 8;
  236. else
  237. mul = 2;
  238. start += sprintf(start, format_config,
  239. /* mark active/actual/current cfg. */
  240. active ? '*' : ' ',
  241. desc->bNumInterfaces,
  242. desc->bConfigurationValue,
  243. desc->bmAttributes,
  244. desc->bMaxPower * mul);
  245. return start;
  246. }
  247. static char *usb_dump_config(int speed, char *start, char *end,
  248. const struct usb_host_config *config, int active)
  249. {
  250. int i, j;
  251. struct usb_interface_cache *intfc;
  252. struct usb_interface *interface;
  253. if (start > end)
  254. return start;
  255. if (!config)
  256. /* getting these some in 2.3.7; none in 2.3.6 */
  257. return start + sprintf(start, "(null Cfg. desc.)\n");
  258. start = usb_dump_config_descriptor(start, end, &config->desc, active,
  259. speed);
  260. for (i = 0; i < USB_MAXIADS; i++) {
  261. if (config->intf_assoc[i] == NULL)
  262. break;
  263. start = usb_dump_iad_descriptor(start, end,
  264. config->intf_assoc[i]);
  265. }
  266. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  267. intfc = config->intf_cache[i];
  268. interface = config->interface[i];
  269. for (j = 0; j < intfc->num_altsetting; j++) {
  270. if (start > end)
  271. return start;
  272. start = usb_dump_interface(speed,
  273. start, end, intfc, interface, j);
  274. }
  275. }
  276. return start;
  277. }
  278. /*
  279. * Dump the different USB descriptors.
  280. */
  281. static char *usb_dump_device_descriptor(char *start, char *end,
  282. const struct usb_device_descriptor *desc)
  283. {
  284. u16 bcdUSB = le16_to_cpu(desc->bcdUSB);
  285. u16 bcdDevice = le16_to_cpu(desc->bcdDevice);
  286. if (start > end)
  287. return start;
  288. start += sprintf(start, format_device1,
  289. bcdUSB >> 8, bcdUSB & 0xff,
  290. desc->bDeviceClass,
  291. class_decode(desc->bDeviceClass),
  292. desc->bDeviceSubClass,
  293. desc->bDeviceProtocol,
  294. desc->bMaxPacketSize0,
  295. desc->bNumConfigurations);
  296. if (start > end)
  297. return start;
  298. start += sprintf(start, format_device2,
  299. le16_to_cpu(desc->idVendor),
  300. le16_to_cpu(desc->idProduct),
  301. bcdDevice >> 8, bcdDevice & 0xff);
  302. return start;
  303. }
  304. /*
  305. * Dump the different strings that this device holds.
  306. */
  307. static char *usb_dump_device_strings(char *start, char *end,
  308. struct usb_device *dev)
  309. {
  310. if (start > end)
  311. return start;
  312. if (dev->manufacturer)
  313. start += sprintf(start, format_string_manufacturer,
  314. dev->manufacturer);
  315. if (start > end)
  316. goto out;
  317. if (dev->product)
  318. start += sprintf(start, format_string_product, dev->product);
  319. if (start > end)
  320. goto out;
  321. #ifdef ALLOW_SERIAL_NUMBER
  322. if (dev->serial)
  323. start += sprintf(start, format_string_serialnumber,
  324. dev->serial);
  325. #endif
  326. out:
  327. return start;
  328. }
  329. static char *usb_dump_desc(char *start, char *end, struct usb_device *dev)
  330. {
  331. int i;
  332. if (start > end)
  333. return start;
  334. start = usb_dump_device_descriptor(start, end, &dev->descriptor);
  335. if (start > end)
  336. return start;
  337. start = usb_dump_device_strings(start, end, dev);
  338. for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
  339. if (start > end)
  340. return start;
  341. start = usb_dump_config(dev->speed,
  342. start, end, dev->config + i,
  343. /* active ? */
  344. (dev->config + i) == dev->actconfig);
  345. }
  346. return start;
  347. }
  348. #ifdef PROC_EXTRA /* TBD: may want to add this code later */
  349. static char *usb_dump_hub_descriptor(char *start, char *end,
  350. const struct usb_hub_descriptor *desc)
  351. {
  352. int leng = USB_DT_HUB_NONVAR_SIZE;
  353. unsigned char *ptr = (unsigned char *)desc;
  354. if (start > end)
  355. return start;
  356. start += sprintf(start, "Interface:");
  357. while (leng && start <= end) {
  358. start += sprintf(start, " %02x", *ptr);
  359. ptr++; leng--;
  360. }
  361. *start++ = '\n';
  362. return start;
  363. }
  364. static char *usb_dump_string(char *start, char *end,
  365. const struct usb_device *dev, char *id, int index)
  366. {
  367. if (start > end)
  368. return start;
  369. start += sprintf(start, "Interface:");
  370. if (index <= dev->maxstring && dev->stringindex &&
  371. dev->stringindex[index])
  372. start += sprintf(start, "%s: %.100s ", id,
  373. dev->stringindex[index]);
  374. return start;
  375. }
  376. #endif /* PROC_EXTRA */
  377. /*****************************************************************/
  378. /* This is a recursive function. Parameters:
  379. * buffer - the user-space buffer to write data into
  380. * nbytes - the maximum number of bytes to write
  381. * skip_bytes - the number of bytes to skip before writing anything
  382. * file_offset - the offset into the devices file on completion
  383. * The caller must own the device lock.
  384. */
  385. static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes,
  386. loff_t *skip_bytes, loff_t *file_offset,
  387. struct usb_device *usbdev, struct usb_bus *bus,
  388. int level, int index, int count)
  389. {
  390. int chix;
  391. int ret, cnt = 0;
  392. int parent_devnum = 0;
  393. char *pages_start, *data_end, *speed;
  394. unsigned int length;
  395. ssize_t total_written = 0;
  396. struct usb_device *childdev = NULL;
  397. /* don't bother with anything else if we're not writing any data */
  398. if (*nbytes <= 0)
  399. return 0;
  400. if (level > MAX_TOPO_LEVEL)
  401. return 0;
  402. /* allocate 2^1 pages = 8K (on i386);
  403. * should be more than enough for one device */
  404. pages_start = (char *)__get_free_pages(GFP_NOIO, 1);
  405. if (!pages_start)
  406. return -ENOMEM;
  407. if (usbdev->parent && usbdev->parent->devnum != -1)
  408. parent_devnum = usbdev->parent->devnum;
  409. /*
  410. * So the root hub's parent is 0 and any device that is
  411. * plugged into the root hub has a parent of 0.
  412. */
  413. switch (usbdev->speed) {
  414. case USB_SPEED_LOW:
  415. speed = "1.5"; break;
  416. case USB_SPEED_UNKNOWN: /* usb 1.1 root hub code */
  417. case USB_SPEED_FULL:
  418. speed = "12"; break;
  419. case USB_SPEED_WIRELESS: /* Wireless has no real fixed speed */
  420. case USB_SPEED_HIGH:
  421. speed = "480"; break;
  422. case USB_SPEED_SUPER:
  423. speed = "5000"; break;
  424. case USB_SPEED_SUPER_PLUS:
  425. speed = "10000"; break;
  426. default:
  427. speed = "??";
  428. }
  429. data_end = pages_start + sprintf(pages_start, format_topo,
  430. bus->busnum, level, parent_devnum,
  431. index, count, usbdev->devnum,
  432. speed, usbdev->maxchild);
  433. /*
  434. * level = topology-tier level;
  435. * parent_devnum = parent device number;
  436. * index = parent's connector number;
  437. * count = device count at this level
  438. */
  439. /* If this is the root hub, display the bandwidth information */
  440. if (level == 0) {
  441. int max;
  442. /* super/high speed reserves 80%, full/low reserves 90% */
  443. if (usbdev->speed == USB_SPEED_HIGH ||
  444. usbdev->speed >= USB_SPEED_SUPER)
  445. max = 800;
  446. else
  447. max = FRAME_TIME_MAX_USECS_ALLOC;
  448. /* report "average" periodic allocation over a microsecond.
  449. * the schedules are actually bursty, HCDs need to deal with
  450. * that and just compute/report this average.
  451. */
  452. data_end += sprintf(data_end, format_bandwidth,
  453. bus->bandwidth_allocated, max,
  454. (100 * bus->bandwidth_allocated + max / 2)
  455. / max,
  456. bus->bandwidth_int_reqs,
  457. bus->bandwidth_isoc_reqs);
  458. }
  459. data_end = usb_dump_desc(data_end, pages_start + (2 * PAGE_SIZE) - 256,
  460. usbdev);
  461. if (data_end > (pages_start + (2 * PAGE_SIZE) - 256))
  462. data_end += sprintf(data_end, "(truncated)\n");
  463. length = data_end - pages_start;
  464. /* if we can start copying some data to the user */
  465. if (length > *skip_bytes) {
  466. length -= *skip_bytes;
  467. if (length > *nbytes)
  468. length = *nbytes;
  469. if (copy_to_user(*buffer, pages_start + *skip_bytes, length)) {
  470. free_pages((unsigned long)pages_start, 1);
  471. return -EFAULT;
  472. }
  473. *nbytes -= length;
  474. *file_offset += length;
  475. total_written += length;
  476. *buffer += length;
  477. *skip_bytes = 0;
  478. } else
  479. *skip_bytes -= length;
  480. free_pages((unsigned long)pages_start, 1);
  481. /* Now look at all of this device's children. */
  482. usb_hub_for_each_child(usbdev, chix, childdev) {
  483. usb_lock_device(childdev);
  484. ret = usb_device_dump(buffer, nbytes, skip_bytes,
  485. file_offset, childdev, bus,
  486. level + 1, chix - 1, ++cnt);
  487. usb_unlock_device(childdev);
  488. if (ret == -EFAULT)
  489. return total_written;
  490. total_written += ret;
  491. }
  492. return total_written;
  493. }
  494. static ssize_t usb_device_read(struct file *file, char __user *buf,
  495. size_t nbytes, loff_t *ppos)
  496. {
  497. struct usb_bus *bus;
  498. ssize_t ret, total_written = 0;
  499. loff_t skip_bytes = *ppos;
  500. int id;
  501. if (*ppos < 0)
  502. return -EINVAL;
  503. if (nbytes <= 0)
  504. return 0;
  505. mutex_lock(&usb_bus_idr_lock);
  506. /* print devices for all busses */
  507. idr_for_each_entry(&usb_bus_idr, bus, id) {
  508. /* recurse through all children of the root hub */
  509. if (!bus_to_hcd(bus)->rh_registered)
  510. continue;
  511. usb_lock_device(bus->root_hub);
  512. ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos,
  513. bus->root_hub, bus, 0, 0, 0);
  514. usb_unlock_device(bus->root_hub);
  515. if (ret < 0) {
  516. mutex_unlock(&usb_bus_idr_lock);
  517. return ret;
  518. }
  519. total_written += ret;
  520. }
  521. mutex_unlock(&usb_bus_idr_lock);
  522. return total_written;
  523. }
  524. const struct file_operations usbfs_devices_fops = {
  525. .llseek = no_seek_end_llseek,
  526. .read = usb_device_read,
  527. };