第一步usb主控器设备的分配
usb_create_hcd
struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,struct device *dev, const char *bus_name)
{
struct usb_hcd *hcd;
hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); //分配usb_hcd和私有数据内存
if (!hcd) {
dev_dbg (dev, "hcd alloc failed\n");
return NULL;
}
dev_set_drvdata(dev, hcd); //dev->p->driver_data=hcd
kref_init(&hcd->kref); //参考计数+1
usb_bus_init(&hcd->self); //初始化usb总线
hcd->self.controller = dev; //设置总线控制器设备
hcd->self.bus_name = bus_name; //设置总线名
hcd->self.uses_dma = (dev->dma_mask != NULL); //设置DMA标志
init_timer(&hcd->rh_timer); //初始化定时器
hcd->rh_timer.function = rh_timer_func; //定时器回调函数
hcd->rh_timer.data = (unsigned long) hcd; //回调函数参数
#ifdef CONFIG_USB_SUSPEND
INIT_WORK(&hcd->wakeup_work, hcd_resume_work); //唤醒执行hcd_resume_work函数
#endif
mutex_init(&hcd->bandwidth_mutex);
hcd->driver = driver; //设置主控器驱动
hcd->product_desc = (driver->product_desc) ? driver->product_desc :"USB Host Controller"; //设置厂商字串
if (hcd->driver->recover_hcd) //若主控器驱动存在recover_hcd方法
INIT_WORK(&hcd->ehci_omap_work, hcd->driver->recover_hcd); //则调用其方法
return hcd;
}
这里定时器的回调函数
static void rh_timer_func (unsigned long _hcd)
{
usb_hcd_poll_rh_status((struct usb_hcd *) _hcd); //调用hcd轮询检测状态的函数
}
第二步usb主控器设备添加
usb_add_hcd
int usb_add_hcd(struct usb_hcd *hcd,unsigned int irqnum, unsigned long irqflags)
{
int retval;
struct usb_device *rhdev; //usb设备(root hub)
dev_info(hcd->self.controller, "%s\n", hcd->product_desc); //设置usb主控器设备设备文件名称
hcd->authorized_default = hcd->wireless? 0 : 1; //检测无线受权信息
set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); //置位usb主控器标志 (全功耗)
if ((retval = hcd_buffer_create(hcd)) != 0) { //分配usb主控器设备DMA缓冲池
dev_dbg(hcd->self.controller, "pool alloc failed\n");
return retval;
}
if ((retval = usb_register_bus(&hcd->self)) < 0) //usb注册总线
goto err_register_bus;
if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) { //分配usb设备(根hub)
dev_err(hcd->self.