📄 usb.c
字号:
dev->ep_in[0] = dev->ep_out[0] = &dev->ep0; /* Save readable and stable topology id, distinguishing devices * by location for diagnostics, tools, driver model, etc. The * string is a path along hub ports, from the root. Each device's * dev->devpath will be stable until USB is re-cabled, and hubs * are often labeled with these port numbers. The bus_id isn't * as stable: bus->busnum changes easily from modprobe order, * cardbus or pci hotplugging, and so on. */ if (unlikely (!parent)) { dev->devpath [0] = '0'; dev->dev.parent = bus->controller; sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum); } else { /* match any labeling on the hubs; it's one-based */ if (parent->devpath [0] == '0') snprintf (dev->devpath, sizeof dev->devpath, "%d", port1); else snprintf (dev->devpath, sizeof dev->devpath, "%s.%d", parent->devpath, port1); dev->dev.parent = &parent->dev; sprintf (&dev->dev.bus_id[0], "%d-%s", bus->busnum, dev->devpath); /* hub driver sets up TT records */ } dev->portnum = port1; dev->bus = bus; dev->parent = parent; INIT_LIST_HEAD(&dev->filelist); return dev;}/** * usb_get_dev - increments the reference count of the usb device structure * @dev: the device being referenced * * Each live reference to a device should be refcounted. * * Drivers for USB interfaces should normally record such references in * their probe() methods, when they bind to an interface, and release * them by calling usb_put_dev(), in their disconnect() methods. * * A pointer to the device with the incremented reference counter is returned. */struct usb_device *usb_get_dev(struct usb_device *dev){ if (dev) get_device(&dev->dev); return dev;}/** * usb_put_dev - release a use of the usb device structure * @dev: device that's been disconnected * * Must be called when a user of a device is finished with it. When the last * user of the device calls this function, the memory of the device is freed. */void usb_put_dev(struct usb_device *dev){ if (dev) put_device(&dev->dev);}/** * usb_get_intf - increments the reference count of the usb interface structure * @intf: the interface being referenced * * Each live reference to a interface must be refcounted. * * Drivers for USB interfaces should normally record such references in * their probe() methods, when they bind to an interface, and release * them by calling usb_put_intf(), in their disconnect() methods. * * A pointer to the interface with the incremented reference counter is * returned. */struct usb_interface *usb_get_intf(struct usb_interface *intf){ if (intf) get_device(&intf->dev); return intf;}/** * usb_put_intf - release a use of the usb interface structure * @intf: interface that's been decremented * * Must be called when a user of an interface is finished with it. When the * last user of the interface calls this function, the memory of the interface * is freed. */void usb_put_intf(struct usb_interface *intf){ if (intf) put_device(&intf->dev);}/* USB device locking * * USB devices and interfaces are locked using the semaphore in their * embedded struct device. The hub driver guarantees that whenever a * device is connected or disconnected, drivers are called with the * USB device locked as well as their particular interface. * * Complications arise when several devices are to be locked at the same * time. Only hub-aware drivers that are part of usbcore ever have to * do this; nobody else needs to worry about it. The rule for locking * is simple: * * When locking both a device and its parent, always lock the * the parent first. *//** * usb_lock_device_for_reset - cautiously acquire the lock for a * usb device structure * @udev: device that's being locked * @iface: interface bound to the driver making the request (optional) * * Attempts to acquire the device lock, but fails if the device is * NOTATTACHED or SUSPENDED, or if iface is specified and the interface * is neither BINDING nor BOUND. Rather than sleeping to wait for the * lock, the routine polls repeatedly. This is to prevent deadlock with * disconnect; in some drivers (such as usb-storage) the disconnect() * or suspend() method will block waiting for a device reset to complete. * * Returns a negative error code for failure, otherwise 1 or 0 to indicate * that the device will or will not have to be unlocked. (0 can be * returned when an interface is given and is BINDING, because in that * case the driver already owns the device lock.) */int usb_lock_device_for_reset(struct usb_device *udev, struct usb_interface *iface){ unsigned long jiffies_expire = jiffies + HZ; if (udev->state == USB_STATE_NOTATTACHED) return -ENODEV; if (udev->state == USB_STATE_SUSPENDED) return -EHOSTUNREACH; if (iface) { switch (iface->condition) { case USB_INTERFACE_BINDING: return 0; case USB_INTERFACE_BOUND: break; default: return -EINTR; } } while (usb_trylock_device(udev) != 0) { /* If we can't acquire the lock after waiting one second, * we're probably deadlocked */ if (time_after(jiffies, jiffies_expire)) return -EBUSY; msleep(15); if (udev->state == USB_STATE_NOTATTACHED) 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 dev->bus->op->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 || !dev->bus->op || !dev->bus->op->buffer_alloc) return NULL; return dev->bus->op->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 || !dev->bus->op || !dev->bus->op->buffer_free) return; dev->bus->op->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(). */#if 0struct urb *usb_buffer_map (struct urb *urb){ struct usb_bus *bus; struct device *controller; if (!urb || !urb->dev || !(bus = urb->dev->bus) || !(controller = bus->controller)) return NULL; if (controller->dma_mask) { urb->transfer_dma = dma_map_single (controller, urb->transfer_buffer, urb->transfer_buffer_length, usb_pipein (urb->pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); if (usb_pipecontrol (urb->pipe)) urb->setup_dma = dma_map_single (controller, urb->setup_packet, sizeof (struct usb_ctrlrequest), DMA_TO_DEVICE); // FIXME generic api broken like pci, can't report errors // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; } else urb->transfer_dma = ~0; urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); return urb;}#endif /* 0 *//* 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -