📄 devices.c
字号:
char *buf; if (start > end) return start; buf = kmalloc(128, GFP_KERNEL); if (!buf) return start; if (dev->descriptor.iManufacturer) { if (usb_string(dev, dev->descriptor.iManufacturer, buf, 128) > 0) start += sprintf(start, format_string_manufacturer, buf); } if (start > end) goto out; if (dev->descriptor.iProduct) { if (usb_string(dev, dev->descriptor.iProduct, buf, 128) > 0) start += sprintf(start, format_string_product, buf); } if (start > end) goto out;#ifdef ALLOW_SERIAL_NUMBER if (dev->descriptor.iSerialNumber) { if (usb_string(dev, dev->descriptor.iSerialNumber, buf, 128) > 0) start += sprintf(start, format_string_serialnumber, buf); }#endif out: kfree(buf); return start;}static char *usb_dump_desc(char *start, char *end, struct usb_device *dev){ int i; if (start > end) return start; start = usb_dump_device_descriptor(start, end, &dev->descriptor); if (start > end) return start; start = usb_dump_device_strings (start, end, dev); for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { if (start > end) return start; start = usb_dump_config(start, end, dev->config + i, (dev->config + i) == dev->actconfig); /* active ? */ } return start;}#ifdef PROC_EXTRA /* TBD: may want to add this code later */static char *usb_dump_hub_descriptor(char *start, char *end, const struct usb_hub_descriptor * desc){ int leng = USB_DT_HUB_NONVAR_SIZE; unsigned char *ptr = (unsigned char *)desc; if (start > end) return start; start += sprintf(start, "Interface:"); while (leng && start <= end) { start += sprintf(start, " %02x", *ptr); ptr++; leng--; } *start++ = '\n'; return start;}static char *usb_dump_string(char *start, char *end, const struct usb_device *dev, char *id, int index){ if (start > end) return start; start += sprintf(start, "Interface:"); if (index <= dev->maxstring && dev->stringindex && dev->stringindex[index]) start += sprintf(start, "%s: %.100s ", id, dev->stringindex[index]); return start;}#endif /* PROC_EXTRA *//*****************************************************************//* This is a recursive function. Parameters: * buffer - the user-space buffer to write data into * nbytes - the maximum number of bytes to write * skip_bytes - the number of bytes to skip before writing anything * file_offset - the offset into the devices file on completion */static ssize_t usb_device_dump(char **buffer, size_t *nbytes, loff_t *skip_bytes, loff_t *file_offset, struct usb_device *usbdev, struct usb_bus *bus, int level, int index, int count){ int chix; int ret, cnt = 0; int parent_devnum = 0; char *pages_start, *data_end, *speed; unsigned int length; ssize_t total_written = 0; /* don't bother with anything else if we're not writing any data */ if (*nbytes <= 0) return 0; if (level > MAX_TOPO_LEVEL) return total_written; /* allocate 2^1 pages = 8K (on i386); should be more than enough for one device */ if (!(pages_start = (char*) __get_free_pages(GFP_KERNEL,1))) return -ENOMEM; if (usbdev->parent && usbdev->parent->devnum != -1) parent_devnum = usbdev->parent->devnum; /* * So the root hub's parent is 0 and any device that is * plugged into the root hub has a parent of 0. */ switch (usbdev->speed) { case USB_SPEED_LOW: speed = "1.5"; break; case USB_SPEED_UNKNOWN: /* usb 1.1 root hub code */ case USB_SPEED_FULL: speed = "12 "; break; case USB_SPEED_HIGH: speed = "480"; break; default: speed = "?? "; } data_end = pages_start + sprintf(pages_start, format_topo, bus->busnum, level, parent_devnum, index, count, usbdev->devnum, speed, usbdev->maxchild); /* * level = topology-tier level; * parent_devnum = parent device number; * index = parent's connector number; * count = device count at this level */ /* If this is the root hub, display the bandwidth information */ if (level == 0) data_end += sprintf(data_end, format_bandwidth, bus->bandwidth_allocated, FRAME_TIME_MAX_USECS_ALLOC, (100 * bus->bandwidth_allocated + FRAME_TIME_MAX_USECS_ALLOC / 2) / FRAME_TIME_MAX_USECS_ALLOC, bus->bandwidth_int_reqs, bus->bandwidth_isoc_reqs); data_end = usb_dump_desc(data_end, pages_start + (2 * PAGE_SIZE) - 256, usbdev); if (data_end > (pages_start + (2 * PAGE_SIZE) - 256)) data_end += sprintf(data_end, "(truncated)\n"); length = data_end - pages_start; /* if we can start copying some data to the user */ if (length > *skip_bytes) { length -= *skip_bytes; if (length > *nbytes) length = *nbytes; if (copy_to_user(*buffer, pages_start + *skip_bytes, length)) { free_pages((unsigned long)pages_start, 1); if (total_written == 0) return -EFAULT; return total_written; } *nbytes -= length; *file_offset += length; total_written += length; *buffer += length; *skip_bytes = 0; } else *skip_bytes -= length; free_pages((unsigned long)pages_start, 1); /* Now look at all of this device's children. */ for (chix = 0; chix < usbdev->maxchild; chix++) { if (usbdev->children[chix]) { ret = usb_device_dump(buffer, nbytes, skip_bytes, file_offset, usbdev->children[chix], bus, level + 1, chix, ++cnt); if (ret == -EFAULT) return total_written; total_written += ret; } } return total_written;}static ssize_t usb_device_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos){ struct list_head *buslist; struct usb_bus *bus; ssize_t ret, total_written = 0; loff_t skip_bytes = *ppos; if (*ppos < 0) return -EINVAL; if (nbytes <= 0) return 0; if (!access_ok(VERIFY_WRITE, buf, nbytes)) return -EFAULT; /* enumerate busses */ down (&usb_bus_list_lock); for (buslist = usb_bus_list.next; buslist != &usb_bus_list; buslist = buslist->next) { /* print devices for this bus */ bus = list_entry(buslist, struct usb_bus, bus_list); /* recurse through all children of the root hub */ ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0); if (ret < 0) return ret; total_written += ret; } up (&usb_bus_list_lock); return total_written;}/* Kernel lock for "lastev" protection */static unsigned int usb_device_poll(struct file *file, struct poll_table_struct *wait){ struct usb_device_status *st = (struct usb_device_status *)file->private_data; unsigned int mask = 0; lock_kernel(); if (!st) { st = kmalloc(sizeof(struct usb_device_status), GFP_KERNEL); if (!st) { unlock_kernel(); return POLLIN; } /* * need to prevent the module from being unloaded, since * proc_unregister does not call the release method and * we would have a memory leak */ st->lastev = conndiscevcnt; file->private_data = st; mask = POLLIN; } if (file->f_mode & FMODE_READ) poll_wait(file, &deviceconndiscwq, wait); if (st->lastev != conndiscevcnt) mask |= POLLIN; st->lastev = conndiscevcnt; unlock_kernel(); return mask;}static int usb_device_open(struct inode *inode, struct file *file){ file->private_data = NULL; return 0;}static int usb_device_release(struct inode *inode, struct file *file){ if (file->private_data) { kfree(file->private_data); file->private_data = NULL; } return 0;}static loff_t usb_device_lseek(struct file * file, loff_t offset, int orig){ switch (orig) { case 0: file->f_pos = offset; return file->f_pos; case 1: file->f_pos += offset; return file->f_pos; case 2: return -EINVAL; default: return -EINVAL; }}struct file_operations usbdevfs_devices_fops = { llseek: usb_device_lseek, read: usb_device_read, poll: usb_device_poll, open: usb_device_open, release: usb_device_release,};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -