devices.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. start = usb_dump_endpoint_descriptor(speed,
  202. start, end, &desc->endpoint[i].desc);
  203. }
  204. return start;
  205. }
  206. static char *usb_dump_iad_descriptor(char *start, char *end,
  207. const struct usb_interface_assoc_descriptor *iad)
  208. {
  209. if (start > end)
  210. return start;
  211. start += sprintf(start, format_iad,
  212. iad->bFirstInterface,
  213. iad->bInterfaceCount,
  214. iad->bFunctionClass,
  215. class_decode(iad->bFunctionClass),
  216. iad->bFunctionSubClass,
  217. iad->bFunctionProtocol);
  218. return start;
  219. }
  220. /* TBD:
  221. * 0. TBDs
  222. * 1. marking active interface altsettings (code lists all, but should mark
  223. * which ones are active, if any)
  224. */
  225. static char *usb_dump_config_descriptor(char *start, char *end,
  226. const struct usb_config_descriptor *desc,
  227. int active, int speed)
  228. {
  229. int mul;
  230. if (start > end)
  231. return start;
  232. if (speed >= USB_SPEED_SUPER)
  233. mul = 8;
  234. else
  235. mul = 2;
  236. start += sprintf(start, format_config,
  237. /* mark active/actual/current cfg. */
  238. active ? '*' : ' ',
  239. desc->bNumInterfaces,
  240. desc->bConfigurationValue,
  241. desc->bmAttributes,
  242. desc->bMaxPower * mul);
  243. return start;
  244. }
  245. static char *usb_dump_config(int speed, char *start, char *end,
  246. const struct usb_host_config *config, int active)
  247. {
  248. int i, j;
  249. struct usb_interface_cache *intfc;
  250. struct usb_interface *interface;
  251. if (start > end)
  252. return start;
  253. if (!config)
  254. /* getting these some in 2.3.7; none in 2.3.6 */
  255. return start + sprintf(start, "(null Cfg. desc.)\n");
  256. start = usb_dump_config_descriptor(start, end, &config->desc, active,
  257. speed);
  258. for (i = 0; i < USB_MAXIADS; i++) {
  259. if (config->intf_assoc[i] == NULL)
  260. break;
  261. start = usb_dump_iad_descriptor(start, end,
  262. config->intf_assoc[i]);
  263. }
  264. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  265. intfc = config->intf_cache[i];
  266. interface = config->interface[i];
  267. for (j = 0; j < intfc->num_altsetting; j++) {
  268. start = usb_dump_interface(speed,
  269. start, end, intfc, interface, j);
  270. }
  271. }
  272. return start;
  273. }
  274. /*
  275. * Dump the different USB descriptors.
  276. */
  277. static char *usb_dump_device_descriptor(char *start, char *end,
  278. const struct usb_device_descriptor *desc)
  279. {
  280. u16 bcdUSB = le16_to_cpu(desc->bcdUSB);
  281. u16 bcdDevice = le16_to_cpu(desc->bcdDevice);
  282. if (start > end)
  283. return start;
  284. start += sprintf(start, format_device1,
  285. bcdUSB >> 8, bcdUSB & 0xff,
  286. desc->bDeviceClass,
  287. class_decode(desc->bDeviceClass),
  288. desc->bDeviceSubClass,
  289. desc->bDeviceProtocol,
  290. desc->bMaxPacketSize0,
  291. desc->bNumConfigurations);
  292. if (start > end)
  293. return start;
  294. start += sprintf(start, format_device2,
  295. le16_to_cpu(desc->idVendor),
  296. le16_to_cpu(desc->idProduct),
  297. bcdDevice >> 8, bcdDevice & 0xff);
  298. return start;
  299. }
  300. /*
  301. * Dump the different strings that this device holds.
  302. */
  303. static char *usb_dump_device_strings(char *start, char *end,
  304. struct usb_device *dev)
  305. {
  306. if (start > end)
  307. return start;
  308. if (dev->manufacturer)
  309. start += sprintf(start, format_string_manufacturer,
  310. dev->manufacturer);
  311. if (start > end)
  312. goto out;
  313. if (dev->product)
  314. start += sprintf(start, format_string_product, dev->product);
  315. if (start > end)
  316. goto out;
  317. #ifdef ALLOW_SERIAL_NUMBER
  318. if (dev->serial)
  319. start += sprintf(start, format_string_serialnumber,
  320. dev->serial);
  321. #endif
  322. out:
  323. return start;
  324. }
  325. static char *usb_dump_desc(char *start, char *end, struct usb_device *dev)
  326. {
  327. int i;
  328. start = usb_dump_device_descriptor(start, end, &dev->descriptor);
  329. start = usb_dump_device_strings(start, end, dev);
  330. for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
  331. start = usb_dump_config(dev->speed,
  332. start, end, dev->config + i,
  333. /* active ? */
  334. (dev->config + i) == dev->actconfig);
  335. }
  336. return start;
  337. }
  338. /*****************************************************************/
  339. /* This is a recursive function. Parameters:
  340. * buffer - the user-space buffer to write data into
  341. * nbytes - the maximum number of bytes to write
  342. * skip_bytes - the number of bytes to skip before writing anything
  343. * file_offset - the offset into the devices file on completion
  344. * The caller must own the device lock.
  345. */
  346. static ssize_t usb_device_dump(char __user **buffer, size_t *nbytes,
  347. loff_t *skip_bytes, loff_t *file_offset,
  348. struct usb_device *usbdev, struct usb_bus *bus,
  349. int level, int index, int count)
  350. {
  351. int chix;
  352. int ret, cnt = 0;
  353. int parent_devnum = 0;
  354. char *pages_start, *data_end, *speed;
  355. unsigned int length;
  356. ssize_t total_written = 0;
  357. struct usb_device *childdev = NULL;
  358. /* don't bother with anything else if we're not writing any data */
  359. if (*nbytes <= 0)
  360. return 0;
  361. if (level > MAX_TOPO_LEVEL)
  362. return 0;
  363. /* allocate 2^1 pages = 8K (on i386);
  364. * should be more than enough for one device */
  365. pages_start = (char *)__get_free_pages(GFP_NOIO, 1);
  366. if (!pages_start)
  367. return -ENOMEM;
  368. if (usbdev->parent && usbdev->parent->devnum != -1)
  369. parent_devnum = usbdev->parent->devnum;
  370. /*
  371. * So the root hub's parent is 0 and any device that is
  372. * plugged into the root hub has a parent of 0.
  373. */
  374. switch (usbdev->speed) {
  375. case USB_SPEED_LOW:
  376. speed = "1.5"; break;
  377. case USB_SPEED_UNKNOWN: /* usb 1.1 root hub code */
  378. case USB_SPEED_FULL:
  379. speed = "12"; break;
  380. case USB_SPEED_HIGH:
  381. speed = "480"; break;
  382. case USB_SPEED_SUPER:
  383. speed = "5000"; break;
  384. case USB_SPEED_SUPER_PLUS:
  385. speed = "10000"; break;
  386. default:
  387. speed = "??";
  388. }
  389. data_end = pages_start + sprintf(pages_start, format_topo,
  390. bus->busnum, level, parent_devnum,
  391. index, count, usbdev->devnum,
  392. speed, usbdev->maxchild);
  393. /*
  394. * level = topology-tier level;
  395. * parent_devnum = parent device number;
  396. * index = parent's connector number;
  397. * count = device count at this level
  398. */
  399. /* If this is the root hub, display the bandwidth information */
  400. if (level == 0) {
  401. int max;
  402. /* super/high speed reserves 80%, full/low reserves 90% */
  403. if (usbdev->speed == USB_SPEED_HIGH ||
  404. usbdev->speed >= USB_SPEED_SUPER)
  405. max = 800;
  406. else
  407. max = FRAME_TIME_MAX_USECS_ALLOC;
  408. /* report "average" periodic allocation over a microsecond.
  409. * the schedules are actually bursty, HCDs need to deal with
  410. * that and just compute/report this average.
  411. */
  412. data_end += sprintf(data_end, format_bandwidth,
  413. bus->bandwidth_allocated, max,
  414. (100 * bus->bandwidth_allocated + max / 2)
  415. / max,
  416. bus->bandwidth_int_reqs,
  417. bus->bandwidth_isoc_reqs);
  418. }
  419. data_end = usb_dump_desc(data_end, pages_start + (2 * PAGE_SIZE) - 256,
  420. usbdev);
  421. if (data_end > (pages_start + (2 * PAGE_SIZE) - 256))
  422. data_end += sprintf(data_end, "(truncated)\n");
  423. length = data_end - pages_start;
  424. /* if we can start copying some data to the user */
  425. if (length > *skip_bytes) {
  426. length -= *skip_bytes;
  427. if (length > *nbytes)
  428. length = *nbytes;
  429. if (copy_to_user(*buffer, pages_start + *skip_bytes, length)) {
  430. free_pages((unsigned long)pages_start, 1);
  431. return -EFAULT;
  432. }
  433. *nbytes -= length;
  434. *file_offset += length;
  435. total_written += length;
  436. *buffer += length;
  437. *skip_bytes = 0;
  438. } else
  439. *skip_bytes -= length;
  440. free_pages((unsigned long)pages_start, 1);
  441. /* Now look at all of this device's children. */
  442. usb_hub_for_each_child(usbdev, chix, childdev) {
  443. usb_lock_device(childdev);
  444. ret = usb_device_dump(buffer, nbytes, skip_bytes,
  445. file_offset, childdev, bus,
  446. level + 1, chix - 1, ++cnt);
  447. usb_unlock_device(childdev);
  448. if (ret == -EFAULT)
  449. return total_written;
  450. total_written += ret;
  451. }
  452. return total_written;
  453. }
  454. static ssize_t usb_device_read(struct file *file, char __user *buf,
  455. size_t nbytes, loff_t *ppos)
  456. {
  457. struct usb_bus *bus;
  458. ssize_t ret, total_written = 0;
  459. loff_t skip_bytes = *ppos;
  460. int id;
  461. if (*ppos < 0)
  462. return -EINVAL;
  463. if (nbytes <= 0)
  464. return 0;
  465. mutex_lock(&usb_bus_idr_lock);
  466. /* print devices for all busses */
  467. idr_for_each_entry(&usb_bus_idr, bus, id) {
  468. /* recurse through all children of the root hub */
  469. if (!bus_to_hcd(bus)->rh_registered)
  470. continue;
  471. usb_lock_device(bus->root_hub);
  472. ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos,
  473. bus->root_hub, bus, 0, 0, 0);
  474. usb_unlock_device(bus->root_hub);
  475. if (ret < 0) {
  476. mutex_unlock(&usb_bus_idr_lock);
  477. return ret;
  478. }
  479. total_written += ret;
  480. }
  481. mutex_unlock(&usb_bus_idr_lock);
  482. return total_written;
  483. }
  484. const struct file_operations usbfs_devices_fops = {
  485. .llseek = no_seek_end_llseek,
  486. .read = usb_device_read,
  487. };