devices.c 18 KB

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