⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux.c

📁 usb user mode lib
💻 C
📖 第 1 页 / 共 2 页
字号:
  DIR *dir;  struct dirent *entry;  dir = opendir(usb_path);  if (!dir)    USB_ERROR_STR(-errno, "couldn't opendir(%s): %s", usb_path,	strerror(errno));  while ((entry = readdir(dir)) != NULL) {    struct usb_bus *bus;    /* Skip anything starting with a . */    if (entry->d_name[0] == '.')      continue;    if (!strchr("0123456789", entry->d_name[strlen(entry->d_name) - 1])) {      if (usb_debug >= 2)        fprintf(stderr, "usb_os_find_busses: Skipping non bus directory %s\n",		entry->d_name);      continue;    }    bus = malloc(sizeof(*bus));    if (!bus)      USB_ERROR(-ENOMEM);    memset((void *)bus, 0, sizeof(*bus));    strncpy(bus->dirname, entry->d_name, sizeof(bus->dirname) - 1);    bus->dirname[sizeof(bus->dirname) - 1] = 0;    LIST_ADD(fbus, bus);    if (usb_debug >= 2)       fprintf(stderr, "usb_os_find_busses: Found %s\n", bus->dirname);  }  closedir(dir);  *busses = fbus;  return 0;}int usb_os_find_devices(struct usb_bus *bus, struct usb_device **devices){  struct usb_device *fdev = NULL;  DIR *dir;  struct dirent *entry;  char dirpath[PATH_MAX + 1];  snprintf(dirpath, PATH_MAX, "%s/%s", usb_path, bus->dirname);  dir = opendir(dirpath);  if (!dir)    USB_ERROR_STR(-errno, "couldn't opendir(%s): %s", dirpath,	strerror(errno));  while ((entry = readdir(dir)) != NULL) {    char filename[PATH_MAX + 1];    struct usb_device *dev;    int i, fd, ret;    /* Skip anything starting with a . */    if (entry->d_name[0] == '.')      continue;    dev = malloc(sizeof(*dev));    if (!dev)      USB_ERROR(-ENOMEM);    memset((void *)dev, 0, sizeof(*dev));    dev->bus = bus;    strncpy(dev->filename, entry->d_name, sizeof(dev->filename) - 1);    dev->filename[sizeof(dev->filename) - 1] = 0;    snprintf(filename, sizeof(filename) - 1, "%s/%s", dirpath, entry->d_name);    fd = open(filename, O_RDONLY);    if (fd < 0) {      if (usb_debug >= 2)        fprintf(stderr, "usb_os_find_devices: Couldn't open %s\n",		filename);      free(dev);      continue;    }    ret = read(fd, (void *)&dev->descriptor, sizeof(dev->descriptor));    if (ret < 0) {      if (usb_debug)        fprintf(stderr, "usb_os_find_devices: Couldn't read descriptor\n");      free(dev);      goto err;    }    LIST_ADD(fdev, dev);    if (usb_debug >= 2)      fprintf(stderr, "usb_os_find_devices: Found %s on %s\n",		dev->filename, bus->dirname);    /* Now try to fetch the rest of the descriptors */    if (dev->descriptor.bNumConfigurations > USB_MAXCONFIG)      /* Silent since we'll try again later */      goto err;    if (dev->descriptor.bNumConfigurations < 1)      /* Silent since we'll try again later */      goto err;    dev->config = (struct usb_config_descriptor *)malloc(dev->descriptor.bNumConfigurations * sizeof(struct usb_config_descriptor));    if (!dev->config)      /* Silent since we'll try again later */      goto err;    memset(dev->config, 0, dev->descriptor.bNumConfigurations *          sizeof(struct usb_config_descriptor));    for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {      char buffer[8], *bigbuffer;      struct usb_config_descriptor *desc = (struct usb_config_descriptor *)buffer;      /* Get the first 8 bytes so we can figure out what the total length is */      ret = read(fd, (void *)buffer, 8);      if (ret < 8) {        if (usb_debug >= 1) {          if (ret < 0)            fprintf(stderr, "Unable to get descriptor (%d)\n", ret);          else            fprintf(stderr, "Config descriptor too short (expected %d, got %d)\n", 8, ret);        }        goto err;      }      USB_LE16_TO_CPU(desc->wTotalLength);      bigbuffer = malloc(desc->wTotalLength);      if (!bigbuffer) {        if (usb_debug >= 1)          fprintf(stderr, "Unable to allocate memory for descriptors\n");        goto err;      }      /* Copy over the first 8 bytes we read */      memcpy(bigbuffer, buffer, 8);      ret = read(fd, (void *)(bigbuffer + 8), desc->wTotalLength - 8);      if (ret < desc->wTotalLength - 8) {        if (usb_debug >= 1) {          if (ret < 0)            fprintf(stderr, "Unable to get descriptor (%d)\n", ret);          else            fprintf(stderr, "Config descriptor too short (expected %d, got %d)\n", desc->wTotalLength, ret);        }        free(bigbuffer);        goto err;      }      ret = usb_parse_configuration(&dev->config[i], bigbuffer);      if (usb_debug >= 2) {        if (ret > 0)          fprintf(stderr, "Descriptor data still left\n");        else if (ret < 0)          fprintf(stderr, "Unable to parse descriptors\n");      }      free(bigbuffer);    }err:    close(fd);  }  closedir(dir);  *devices = fdev;  return 0;}static int check_usb_vfs(const unsigned char *dirname){  DIR *dir;  struct dirent *entry;  int found = 0;  dir = opendir(dirname);  if (!dir)    return 0;  while ((entry = readdir(dir)) != NULL) {    /* Skip anything starting with a . */    if (entry->d_name[0] == '.')      continue;    /* We assume if we find any files that it must be the right place */    found = 1;    break;  }  closedir(dir);  return found;}void usb_os_init(void){  /* Find the path to the virtual filesystem */  if (getenv("USB_DEVFS_PATH")) {    if (check_usb_vfs(getenv("USB_DEVFS_PATH"))) {      strncpy(usb_path, getenv("USB_DEVFS_PATH"), sizeof(usb_path) - 1);      usb_path[sizeof(usb_path) - 1] = 0;    } else if (usb_debug)      fprintf(stderr, "usb_os_init: couldn't find USB VFS in USB_DEVFS_PATH\n");  }  if (!usb_path[0]) {    if (check_usb_vfs("/proc/bus/usb")) {      strncpy(usb_path, "/proc/bus/usb", sizeof(usb_path) - 1);      usb_path[sizeof(usb_path) - 1] = 0;    } else if (check_usb_vfs("/sys/bus/usb")) { /* 2.6 Kernel with sysfs */      strncpy(usb_path, "/sys/bus/usb", sizeof(usb_path) -1);      usb_path[sizeof(usb_path) - 1] = 0;    } else if (check_usb_vfs("/dev/usb")) {      strncpy(usb_path, "/dev/usb", sizeof(usb_path) - 1);      usb_path[sizeof(usb_path) - 1] = 0;    } else      usb_path[0] = 0;	/* No path, no USB support */  }  if (usb_debug) {    if (usb_path[0])      fprintf(stderr, "usb_os_init: Found USB VFS at %s\n", usb_path);    else      fprintf(stderr, "usb_os_init: No USB VFS found, is it mounted?\n");  }}int usb_resetep(usb_dev_handle *dev, unsigned int ep){  int ret;  ret = ioctl(dev->fd, IOCTL_USB_RESETEP, &ep);  if (ret)    USB_ERROR_STR(-errno, "could not reset ep %d: %s", ep,    	strerror(errno));  return 0;}int usb_clear_halt(usb_dev_handle *dev, unsigned int ep){  int ret;  ret = ioctl(dev->fd, IOCTL_USB_CLEAR_HALT, &ep);  if (ret)    USB_ERROR_STR(-errno, "could not clear/halt ep %d: %s", ep,    	strerror(errno));  return 0;}int usb_reset(usb_dev_handle *dev){  int ret;  ret = ioctl(dev->fd, IOCTL_USB_RESET, NULL);  if (ret)     USB_ERROR_STR(-errno, "could not reset: %s", strerror(errno));  return 0;}int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name,	unsigned int namelen){  struct usb_getdriver getdrv;  int ret;  getdrv.interface = interface;  ret = ioctl(dev->fd, IOCTL_USB_GETDRIVER, &getdrv);  if (ret)    USB_ERROR_STR(-errno, "could not get bound driver: %s", strerror(errno));  strncpy(name, getdrv.driver, namelen - 1);  name[namelen - 1] = 0;  return 0;}int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface){  struct usb_ioctl command;  int ret;  command.ifno = interface;  command.ioctl_code = IOCTL_USB_DISCONNECT;  command.data = NULL;  ret = ioctl(dev->fd, IOCTL_USB_IOCTL, &command);  if (ret)    USB_ERROR_STR(-errno, "could not detach kernel driver from interface %d: %s",        interface, strerror(errno));  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -