分析内核源码 -- mass_storage.c


分析 mass_storage.c

struct usb_composite_driver {
	const char				*name;
	const struct usb_device_descriptor	*dev;
	struct usb_gadget_strings		**strings;
	enum usb_device_speed			max_speed;
	unsigned		needs_serial:1;

	int			(*bind)(struct usb_composite_dev *cdev);
	int			(*unbind)(struct usb_composite_dev *);

	void			(*disconnect)(struct usb_composite_dev *);

	/* global suspend hooks */
	void			(*suspend)(struct usb_composite_dev *);
	void			(*resume)(struct usb_composite_dev *);
	struct usb_gadget_driver		gadget_driver;
};

static struct usb_composite_driver msg_driver = {
	.name		= "g_mass_storage",
	.dev		= &msg_device_desc,
	.max_speed	= USB_SPEED_SUPER_PLUS,
	.needs_serial	= 1,
	.strings	= dev_strings,
	.bind		= msg_bind,
	.unbind		= msg_unbind,
};




msg_init
	usb_composite_probe(&msg_driver)
			//构造 struct usb_gadget_driver  结构体。 关联 usb_composite_driver 和 usb_gadget_driver 结构体
			driver->gadget_driver = composite_driver_template;
			gadget_driver = &driver->gadget_driver;
			gadget_driver->function =  (char *) driver->name;
			gadget_driver->driver.name = driver->name;
			gadget_driver->max_speed = driver->max_speed;
			//最终会调用 usb_gadget_probe_driver 函数
			usb_gadget_probe_driver(gadget_driver)
				list_for_each_entry(udc, &udc_list, list) //查找 struct usb_udc	(即udc),如果找到,则调用 udc_bind_to_driver 
					/* For now we take the first one */
					if (!udc->driver)
						udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
							//构造 usb_udc 结构体,  关联 usb_udc 与 usb_gadget_driver
							udc->driver = driver;
							udc->dev.driver = &driver->driver;
							udc->gadget->dev.driver = &driver->driver;
							//绑定
							driver->bind(udc->gadget, driver)  //实际调用的是 composite_driver_template 里的 composite_bind 函数
												composite_bind  //这个是最重要的函数
													composite_dev_prepare
														usb_ep_alloc_request
														cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL); //请求发送的内容保存在此
														cdev->req->complete = composite_setup_complete;
														
														
													composite->bind(cdev)  //也就是调用  msg_bind 函数 ( mass_storage.c)
													
							usb_gadget_udc_start(udc)
								udc->gadget->ops->udc_start(udc->gadget, udc->driver);
								
							usb_udc_connect_control(udc)
								usb_gadget_connect(udc->gadget)
									gadget->ops->pullup(gadget, 1) // Change Data+ pullup status



 

你可能感兴趣的:(嵌入式驱动,USB)