devices.c 18 KB

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