devices.c 18 KB

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