drivers.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * drivers.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.  * $id$
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  *
  23.  *************************************************************
  24.  *
  25.  * 1999-12-16: Thomas Sailer <sailer@ife.ee.ethz.ch>
  26.  *   Converted the whole proc stuff to real
  27.  *   read methods. Now not the whole device list needs to fit
  28.  *   into one page, only the device list for one bus.
  29.  *   Added a poll method to /proc/bus/usb/devices, to wake
  30.  *   up an eventual usbd
  31.  * 2000-01-04: Thomas Sailer <sailer@ife.ee.ethz.ch>
  32.  *   Turned into its own filesystem
  33.  *
  34.  * $Id: drivers.c,v 1.3 2000/01/11 13:58:24 tom Exp $
  35.  */
  36. #include <linux/fs.h>
  37. #include <linux/mm.h>
  38. #include <linux/usb.h>
  39. #include <linux/usbdevice_fs.h>
  40. #include <asm/uaccess.h>
  41. /*****************************************************************/
  42. /*
  43.  * Dump usb_driver_list.
  44.  *
  45.  * We now walk the list of registered USB drivers.
  46.  */
  47. static ssize_t usb_driver_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
  48. {
  49. struct list_head *tmp = usb_driver_list.next;
  50. char *page, *start, *end;
  51. ssize_t ret = 0;
  52. unsigned int pos, len;
  53. if (*ppos < 0)
  54. return -EINVAL;
  55. if (nbytes <= 0)
  56. return 0;
  57. if (!access_ok(VERIFY_WRITE, buf, nbytes))
  58. return -EFAULT;
  59.         if (!(page = (char*) __get_free_page(GFP_KERNEL)))
  60.                 return -ENOMEM;
  61. start = page;
  62. end = page + (PAGE_SIZE - 100);
  63. pos = *ppos;
  64. for (; tmp != &usb_driver_list; tmp = tmp->next) {
  65. struct usb_driver *driver = list_entry(tmp, struct usb_driver, driver_list);
  66. int minor = driver->fops ? driver->minor : -1;
  67. if (minor == -1)
  68. start += sprintf (start, "         %sn", driver->name);
  69. else
  70. start += sprintf (start, "%3d-%3d: %sn", minor, minor + 15, driver->name);
  71. if (start > end) {
  72. start += sprintf(start, "(truncated)n");
  73. break;
  74. }
  75. }
  76. if (start == page)
  77. start += sprintf(start, "(none)n");
  78. len = start - page;
  79. if (len > pos) {
  80. len -= pos;
  81. if (len > nbytes)
  82. len = nbytes;
  83. ret = len;
  84. if (copy_to_user(buf, page + pos, len))
  85. ret = -EFAULT;
  86. else
  87. *ppos += len;
  88. }
  89. free_page((unsigned long)page);
  90. return ret;
  91. }
  92. static loff_t usb_driver_lseek(struct file * file, loff_t offset, int orig)
  93. {
  94. switch (orig) {
  95. case 0:
  96. file->f_pos = offset;
  97. return file->f_pos;
  98. case 1:
  99. file->f_pos += offset;
  100. return file->f_pos;
  101. case 2:
  102. return -EINVAL;
  103. default:
  104. return -EINVAL;
  105. }
  106. }
  107. struct file_operations usbdevfs_drivers_fops = {
  108. llseek: usb_driver_lseek,
  109. read: usb_driver_read,
  110. };