usb_alloc_dev

来源:互联网 发布:c语言缺省参数 编辑:程序博客网 时间:2024/06/11 20:06
/** * usb_alloc_dev - usb device constructor (usbcore-internal) * @parent: hub to which device is connected; null to allocate a root hub * @bus: bus used to access the device * @port1: one-based index of port; ignored for root hubs * Context: !in_interrupt() * * Only hub drivers (including virtual root hub drivers for host * controllers) should ever call this. * * This call may not be used in a non-sleeping context. */struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1){struct usb_device *dev;struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);unsigned root_hub = 0;dev = kzalloc(sizeof(*dev), GFP_KERNEL);if (!dev)return NULL;if (!usb_get_hcd(bus_to_hcd(bus))) {kfree(dev);return NULL;}/* Root hubs aren't true devices, so don't allocate HCD resources */if (usb_hcd->driver->alloc_dev && parent &&!usb_hcd->driver->alloc_dev(usb_hcd, dev)) {usb_put_hcd(bus_to_hcd(bus));kfree(dev);return NULL;}device_initialize(&dev->dev);dev->dev.bus = &usb_bus_type;dev->dev.type = &usb_device_type;dev->dev.groups = usb_device_groups;dev->dev.dma_mask = bus->controller->dma_mask;set_dev_node(&dev->dev, dev_to_node(bus->controller));dev->state = USB_STATE_ATTACHED;/*设置usb设备状态已连接*/atomic_set(&dev->urbnum, 0);    /*初始化usb_device结构体里面的端点0的urb_list*/INIT_LIST_HEAD(&dev->ep0.urb_list);dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;/* ep0 maxpacket comes later, from device descriptor */usb_enable_endpoint(dev, &dev->ep0, false);/*打开端点0*/dev->can_submit = 1;/*可以提交*//* 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 name isn't * as stable:  bus->busnum changes easily from modprobe order, * cardbus or pci hotplugging, and so on. */     /*判断你的usb设备是不是连接到Root Hub上面*/if (unlikely(!parent)) {dev->devpath[0] = '0';dev->route = 0;dev->dev.parent = bus->controller;dev_set_name(&dev->dev, "usb%d", bus->busnum);root_hub = 1;} else {/* match any labeling on the hubs; it's one-based */if (parent->devpath[0] == '0') {snprintf(dev->devpath, sizeof dev->devpath,"%d", port1);/* Root ports are not counted in route string */dev->route = 0;} else {snprintf(dev->devpath, sizeof dev->devpath,"%s.%d", parent->devpath, port1);/* Route string assumes hubs have less than 16 ports */if (port1 < 15)dev->route = parent->route +(port1 << ((parent->level - 1)*4));elsedev->route = parent->route +(15 << ((parent->level - 1)*4));}dev->dev.parent = &parent->dev;dev_set_name(&dev->dev, "%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);/*初始化一个队列*/#ifdefCONFIG_PMpm_runtime_set_autosuspend_delay(&dev->dev,usb_autosuspend_delay * 1000);dev->connect_time = jiffies;dev->active_duration = -jiffies;#endifif (root_hub)/* Root hub always ok [and always wired] */dev->authorized = 1;else {dev->authorized = usb_hcd->authorized_default;dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;}return dev;}

当有usb设备连接到主机都会调用这个函数;为usb设备分配内存;并初始化相应的值;
原创粉丝点击