devices.c 19 KB

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