📄 usb.c
字号:
dma_sync_single (controller, urb->transfer_dma, urb->transfer_buffer_length, usb_pipein (urb->pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); if (usb_pipecontrol (urb->pipe)) dma_sync_single (controller, urb->setup_dma, sizeof (struct usb_ctrlrequest), DMA_TO_DEVICE); }}#endif/** * 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(). */#if 0void usb_buffer_unmap (struct urb *urb){ struct usb_bus *bus; struct device *controller; if (!urb || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) || !urb->dev || !(bus = urb->dev->bus) || !(controller = bus->controller)) return; if (controller->dma_mask) { dma_unmap_single (controller, urb->transfer_dma, urb->transfer_buffer_length, usb_pipein (urb->pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); if (usb_pipecontrol (urb->pipe)) dma_unmap_single (controller, urb->setup_dma, sizeof (struct usb_ctrlrequest), DMA_TO_DEVICE); } urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);}#endif /* 0 *//** * 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 (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 */#if 0/** * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s) * @dev: device to which the scatterlist will be mapped * @pipe: endpoint defining the mapping direction * @sg: the scatterlist to synchronize * @n_hw_ents: the positive return value from usb_buffer_map_sg * * Use this when you are re-using a scatterlist's data buffers for * another USB request. */void usb_buffer_dmasync_sg (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_sync_sg (controller, sg, n_hw_ents, usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);}#endif/** * 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 (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);}static int verify_suspended(struct device *dev, void *unused){ return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;}static int usb_generic_suspend(struct device *dev, pm_message_t message){ struct usb_interface *intf; struct usb_driver *driver; int status; /* USB devices enter SUSPEND state through their hubs, but can be * marked for FREEZE as soon as their children are already idled. * But those semantics are useless, so we equate the two (sigh). */ if (dev->driver == &usb_generic_driver) { if (dev->power.power_state.event == message.event) return 0; /* we need to rule out bogus requests through sysfs */ status = device_for_each_child(dev, NULL, verify_suspended); if (status) return status; return usb_suspend_device (to_usb_device(dev)); } if ((dev->driver == NULL) || (dev->driver_data == &usb_generic_driver_data)) return 0; intf = to_usb_interface(dev); driver = to_usb_driver(dev->driver); /* with no hardware, USB interfaces only use FREEZE and ON states */ if (!is_active(intf)) return 0; if (driver->suspend && driver->resume) { status = driver->suspend(intf, message); if (status) dev_err(dev, "%s error %d\n", "suspend", status); else mark_quiesced(intf); } else { // FIXME else if there's no suspend method, disconnect... dev_warn(dev, "no suspend for driver %s?\n", driver->name); mark_quiesced(intf); status = 0; } return status;}static int usb_generic_resume(struct device *dev){ struct usb_interface *intf; struct usb_driver *driver; struct usb_device *udev; int status; if (dev->power.power_state.event == PM_EVENT_ON) return 0; /* mark things as "on" immediately, no matter what errors crop up */ dev->power.power_state.event = PM_EVENT_ON; /* devices resume through their hubs */ if (dev->driver == &usb_generic_driver) { udev = to_usb_device(dev); if (udev->state == USB_STATE_NOTATTACHED) return 0; return usb_resume_device (to_usb_device(dev)); } if ((dev->driver == NULL) || (dev->driver_data == &usb_generic_driver_data)) { dev->power.power_state.event = PM_EVENT_FREEZE; return 0; } intf = to_usb_interface(dev); driver = to_usb_driver(dev->driver); udev = interface_to_usbdev(intf); if (udev->state == USB_STATE_NOTATTACHED) return 0; /* if driver was suspended, it has a resume method; * however, sysfs can wrongly mark things as suspended * (on the "no suspend method" FIXME path above) */ if (driver->resume) { status = driver->resume(intf); if (status) { dev_err(dev, "%s error %d\n", "resume", status); mark_quiesced(intf); } } else dev_warn(dev, "no resume for driver %s?\n", driver->name); return 0;}struct bus_type usb_bus_type = { .name = "usb", .match = usb_device_match, .hotplug = usb_hotplug, .suspend = usb_generic_suspend, .resume = usb_generic_resume,};#ifndef MODULEstatic int __init usb_setup_disable(char *str){ nousb = 1; return 1;}/* format to disable USB on kernel command line is: nousb */__setup("nousb", usb_setup_disable);#endif/* * 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 = bus_register(&usb_bus_type); if (retval) goto out; 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 = usbdev_init(); if (retval) goto usbdevice_init_failed; retval = usbfs_init(); if (retval) goto fs_init_failed; retval = usb_hub_init(); if (retval) goto hub_init_failed; retval = driver_register(&usb_generic_driver); if (!retval) goto out; usb_hub_cleanup();hub_init_failed: usbfs_cleanup();fs_init_failed: usbdev_cleanup();usbdevice_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);out: return retval;}/* * Cleanup */static void __exit usb_exit(void){ /* This will matter if shutdown/reboot does exitcalls. */ if (nousb) return; driver_unregister(&usb_generic_driver); usb_major_cleanup(); usbfs_cleanup(); usb_deregister(&usbfs_driver); usbdev_cleanup(); usb_hub_cleanup(); usb_host_cleanup(); bus_unregister(&usb_bus_type);}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_register);EXPORT_SYMBOL(usb_deregister);EXPORT_SYMBOL(usb_disabled);EXPORT_SYMBOL_GPL(usb_get_intf);EXPORT_SYMBOL_GPL(usb_put_intf);EXPORT_SYMBOL(usb_alloc_dev);EXPORT_SYMBOL(usb_put_dev);EXPORT_SYMBOL(usb_get_dev);EXPORT_SYMBOL(usb_hub_tt_clear_buffer);EXPORT_SYMBOL(usb_lock_device);EXPORT_SYMBOL(usb_trylock_device);EXPORT_SYMBOL(usb_lock_device_for_reset);EXPORT_SYMBOL(usb_unlock_device);EXPORT_SYMBOL(usb_driver_claim_interface);EXPORT_SYMBOL(usb_driver_release_interface);EXPORT_SYMBOL(usb_match_id);EXPORT_SYMBOL(usb_find_interface);EXPORT_SYMBOL(usb_ifnum_to_if);EXPORT_SYMBOL(usb_altnum_to_altsetting);EXPORT_SYMBOL(usb_reset_device);EXPORT_SYMBOL(usb_disconnect);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);#if 0EXPORT_SYMBOL (usb_buffer_map);EXPORT_SYMBOL (usb_buffer_dmasync);EXPORT_SYMBOL (usb_buffer_unmap);#endifEXPORT_SYMBOL (usb_buffer_map_sg);#if 0EXPORT_SYMBOL (usb_buffer_dmasync_sg);#endifEXPORT_SYMBOL (usb_buffer_unmap_sg);MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -