devices.c 18 KB

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