📄 usb.c
字号:
return -ENODEV; if (udev->state == USB_STATE_SUSPENDED) return -EHOSTUNREACH; if (iface && iface->condition != USB_INTERFACE_BOUND) return -EINTR; } return 1;}static struct usb_device *match_device(struct usb_device *dev, u16 vendor_id, u16 product_id){ struct usb_device *ret_dev = NULL; int child; dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n", le16_to_cpu(dev->descriptor.idVendor), le16_to_cpu(dev->descriptor.idProduct)); /* see if this device matches */ if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) && (product_id == le16_to_cpu(dev->descriptor.idProduct))) { dev_dbg(&dev->dev, "matched this device!\n"); ret_dev = usb_get_dev(dev); goto exit; } /* look through all of the children of this device */ for (child = 0; child < dev->maxchild; ++child) { if (dev->children[child]) { usb_lock_device(dev->children[child]); ret_dev = match_device(dev->children[child], vendor_id, product_id); usb_unlock_device(dev->children[child]); if (ret_dev) goto exit; } }exit: return ret_dev;}/** * usb_find_device - find a specific usb device in the system * @vendor_id: the vendor id of the device to find * @product_id: the product id of the device to find * * Returns a pointer to a struct usb_device if such a specified usb * device is present in the system currently. The usage count of the * device will be incremented if a device is found. Make sure to call * usb_put_dev() when the caller is finished with the device. * * If a device with the specified vendor and product id is not found, * NULL is returned. */struct usb_device *usb_find_device(u16 vendor_id, u16 product_id){ struct list_head *buslist; struct usb_bus *bus; struct usb_device *dev = NULL; mutex_lock(&usb_bus_list_lock); for (buslist = usb_bus_list.next; buslist != &usb_bus_list; buslist = buslist->next) { bus = container_of(buslist, struct usb_bus, bus_list); if (!bus->root_hub) continue; usb_lock_device(bus->root_hub); dev = match_device(bus->root_hub, vendor_id, product_id); usb_unlock_device(bus->root_hub); if (dev) goto exit; }exit: mutex_unlock(&usb_bus_list_lock); return dev;}/** * usb_get_current_frame_number - return current bus frame number * @dev: the device whose bus is being queried * * Returns the current frame number for the USB host controller * used with the given USB device. This can be used when scheduling * isochronous requests. * * Note that different kinds of host controller have different * "scheduling horizons". While one type might support scheduling only * 32 frames into the future, others could support scheduling up to * 1024 frames into the future. */int usb_get_current_frame_number(struct usb_device *dev){ return usb_hcd_get_frame_number(dev);}/*-------------------------------------------------------------------*//* * __usb_get_extra_descriptor() finds a descriptor of specific type in the * extra field of the interface and endpoint descriptor structs. */int __usb_get_extra_descriptor(char *buffer, unsigned size, unsigned char type, void **ptr){ struct usb_descriptor_header *header; while (size >= sizeof(struct usb_descriptor_header)) { header = (struct usb_descriptor_header *)buffer; if (header->bLength < 2) { printk(KERN_ERR "%s: bogus descriptor, type %d length %d\n", usbcore_name, header->bDescriptorType, header->bLength); return -1; } if (header->bDescriptorType == type) { *ptr = header; return 0; } buffer += header->bLength; size -= header->bLength; } return -1;}/** * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP * @dev: device the buffer will be used with * @size: requested buffer size * @mem_flags: affect whether allocation may block * @dma: used to return DMA address of buffer * * Return value is either null (indicating no buffer could be allocated), or * the cpu-space pointer to a buffer that may be used to perform DMA to the * specified device. Such cpu-space buffers are returned along with the DMA * address (through the pointer provided). * * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags * to avoid behaviors like using "DMA bounce buffers", or tying down I/O * mapping hardware for long idle periods. The implementation varies between * platforms, depending on details of how DMA will work to this device. * Using these buffers also helps prevent cacheline sharing problems on * architectures where CPU caches are not DMA-coherent. * * When the buffer is no longer used, free it with usb_buffer_free(). */void *usb_buffer_alloc( struct usb_device *dev, size_t size, gfp_t mem_flags, dma_addr_t *dma){ if (!dev || !dev->bus) return NULL; return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);}/** * usb_buffer_free - free memory allocated with usb_buffer_alloc() * @dev: device the buffer was used with * @size: requested buffer size * @addr: CPU address of buffer * @dma: DMA address of buffer * * This reclaims an I/O buffer, letting it be reused. The memory must have * been allocated using usb_buffer_alloc(), and the parameters must match * those provided in that allocation request. */void usb_buffer_free( struct usb_device *dev, size_t size, void *addr, dma_addr_t dma){ if (!dev || !dev->bus) return; if (!addr) return; hcd_buffer_free(dev->bus, size, addr, dma);}/** * usb_buffer_map - create DMA mapping(s) for an urb * @urb: urb whose transfer_buffer/setup_packet will be mapped * * Return value is either null (indicating no buffer could be mapped), or * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are * added to urb->transfer_flags if the operation succeeds. If the device * is connected to this system through a non-DMA controller, this operation * always succeeds. * * This call would normally be used for an urb which is reused, perhaps * as the target of a large periodic transfer, with usb_buffer_dmasync() * calls to synchronize memory and dma state. * * Reverse the effect of this call with usb_buffer_unmap(). *//* XXX DISABLED, no users currently. If you wish to re-enable this * XXX please determine whether the sync is to transfer ownership of * XXX the buffer from device to cpu or vice verse, and thusly use the * XXX appropriate _for_{cpu,device}() method. -DaveM *//** * usb_buffer_unmap - free DMA mapping(s) for an urb * @urb: urb whose transfer_buffer will be unmapped * * Reverses the effect of usb_buffer_map(). *//** * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint * @dev: device to which the scatterlist will be mapped * @pipe: endpoint defining the mapping direction * @sg: the scatterlist to map * @nents: the number of entries in the scatterlist * * Return value is either < 0 (indicating no buffers could be mapped), or * the number of DMA mapping array entries in the scatterlist. * * The caller is responsible for placing the resulting DMA addresses from * the scatterlist into URB transfer buffer pointers, and for setting the * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs. * * Top I/O rates come from queuing URBs, instead of waiting for each one * to complete before starting the next I/O. This is particularly easy * to do with scatterlists. Just allocate and submit one URB for each DMA * mapping entry returned, stopping on the first error or when all succeed. * Better yet, use the usb_sg_*() calls, which do that (and more) for you. * * This call would normally be used when translating scatterlist requests, * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it * may be able to coalesce mappings for improved I/O efficiency. * * Reverse the effect of this call with usb_buffer_unmap_sg(). */int usb_buffer_map_sg(const struct usb_device *dev, unsigned pipe, struct scatterlist *sg, int nents){ struct usb_bus *bus; struct device *controller; if (!dev || usb_pipecontrol(pipe) || !(bus = dev->bus) || !(controller = bus->controller) || !controller->dma_mask) return -1; // FIXME generic api broken like pci, can't report errors return dma_map_sg(controller, sg, nents, usb_pipein(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);}/* XXX DISABLED, no users currently. If you wish to re-enable this * XXX please determine whether the sync is to transfer ownership of * XXX the buffer from device to cpu or vice verse, and thusly use the * XXX appropriate _for_{cpu,device}() method. -DaveM *//** * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist * @dev: device to which the scatterlist will be mapped * @pipe: endpoint defining the mapping direction * @sg: the scatterlist to unmap * @n_hw_ents: the positive return value from usb_buffer_map_sg * * Reverses the effect of usb_buffer_map_sg(). */void usb_buffer_unmap_sg(const struct usb_device *dev, unsigned pipe, struct scatterlist *sg, int n_hw_ents){ struct usb_bus *bus; struct device *controller; if (!dev || !(bus = dev->bus) || !(controller = bus->controller) || !controller->dma_mask) return; dma_unmap_sg(controller, sg, n_hw_ents, usb_pipein(pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);}/* format to disable USB on kernel command line is: nousb */__module_param_call("", nousb, param_set_bool, param_get_bool, &nousb, 0444);/* * for external read access to <nousb> */int usb_disabled(void){ return nousb;}/* * Init */static int __init usb_init(void){ int retval; if (nousb) { pr_info("%s: USB support disabled\n", usbcore_name); return 0; } retval = ksuspend_usb_init(); if (retval) goto out; retval = bus_register(&usb_bus_type); if (retval) goto bus_register_failed; retval = usb_host_init(); if (retval) goto host_init_failed; retval = usb_major_init(); if (retval) goto major_init_failed; retval = usb_register(&usbfs_driver); if (retval) goto driver_register_failed; retval = usb_devio_init(); if (retval) goto usb_devio_init_failed; retval = usbfs_init(); if (retval) goto fs_init_failed; retval = usb_hub_init(); if (retval) goto hub_init_failed; retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE); if (!retval) goto out; usb_hub_cleanup();hub_init_failed: usbfs_cleanup();fs_init_failed: usb_devio_cleanup();usb_devio_init_failed: usb_deregister(&usbfs_driver);driver_register_failed: usb_major_cleanup();major_init_failed: usb_host_cleanup();host_init_failed: bus_unregister(&usb_bus_type);bus_register_failed: ksuspend_usb_cleanup();out: return retval;}/* * Cleanup */static void __exit usb_exit(void){ /* This will matter if shutdown/reboot does exitcalls. */ if (nousb) return; usb_deregister_device_driver(&usb_generic_driver); usb_major_cleanup(); usbfs_cleanup(); usb_deregister(&usbfs_driver); usb_devio_cleanup(); usb_hub_cleanup(); usb_host_cleanup(); bus_unregister(&usb_bus_type); ksuspend_usb_cleanup();}subsys_initcall(usb_init);module_exit(usb_exit);/* * USB may be built into the kernel or be built as modules. * These symbols are exported for device (or host controller) * driver modules to use. */EXPORT_SYMBOL(usb_disabled);EXPORT_SYMBOL_GPL(usb_get_intf);EXPORT_SYMBOL_GPL(usb_put_intf);EXPORT_SYMBOL(usb_put_dev);EXPORT_SYMBOL(usb_get_dev);EXPORT_SYMBOL(usb_hub_tt_clear_buffer);EXPORT_SYMBOL(usb_lock_device_for_reset);EXPORT_SYMBOL(usb_find_interface);EXPORT_SYMBOL(usb_ifnum_to_if);EXPORT_SYMBOL(usb_altnum_to_altsetting);EXPORT_SYMBOL(__usb_get_extra_descriptor);EXPORT_SYMBOL(usb_find_device);EXPORT_SYMBOL(usb_get_current_frame_number);EXPORT_SYMBOL(usb_buffer_alloc);EXPORT_SYMBOL(usb_buffer_free);EXPORT_SYMBOL(usb_buffer_map_sg);EXPORT_SYMBOL(usb_buffer_unmap_sg);MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -