devices.c 19 KB

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