devices.c 18 KB

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