devices.c 19 KB

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