linux中OTG识别到一个U盘后产生一个sg节点的全过程

注:本篇文章暂时不做流程图,如果有需求后续补做。

1. 需要准备的源码文件列表:

base部分:

kernel\base\core.c

kernel\base\bus.c

kernel\base\dd.c

kernel\base\class.c

kernel\base\driver.c

 

头文件部分:

kernel\include\linux\device.h

kernel\include\linux\usb.h

kernel\include\scsi\scsi_host.h

 

usb核心部分:

kernel\driver\usb\core\usb.c

kernel\driverusb\core\driver.c

kernel\driverusb\core\hub.c 

kernel\driverusb\core\driver.c

kernel\drivers\usb\core\message.c

kernel\drivers\usb\core\generic.c

 

大容量设备部分:

kernel\driverusb\storage\usb.c

 

scsi部分:

kernel\driverscsi\scsi_scan.c

kernel\driverscsi\scsi_sysfs.c

kernel\driverscsi\sg.c

 

2. 当一个U盘插入linux设备前发生的事情:

a. 最开始注册hub部分:

  需要关注注册驱动的有hub, usb, usb-storage。hub中用来做检测usb口是否有OTG的东东接入,usb是所有usb接入设备的老大哥,usb-storage只是usb的一个小老弟。

翻到 kernel\driver\usb\core\usb.c 源码,这里先注册了hub驱动,再注册了usb驱动。

注:代码中“...”表示忽略这部分的代码,只需要关注贴出来的代码即可。

static int __init usb_init(void)
{
...
	retval = usb_hub_init();//注册hub驱动
...
}  

先看hub注册过程,打开kernel\driverusb\core\hub.c,

static struct usb_driver hub_driver = {
	.name =		"hub",
...
};

int usb_hub_init(void)
{
	if (usb_register(&hub_driver) < 0) {
		printk(KERN_ERR "%s: can't register hub driver\n",
			usbcore_name);
		return -1;
	}
...
}

先关注usb_register,省略的部分后面再关注,打开kernel\include\linux\usb.h,

/* use a define to avoid include chaining to get THIS_MODULE & friends */
#define usb_register(driver) \
	usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)

然后进入kernel\driverusb\core\driver.c中的

int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
			const char *mod_name)
{
...
	new_driver->drvwrap.driver.name = (char *) new_driver->name;
	new_driver->drvwrap.driver.bus = &usb_bus_type;
	new_driver->drvwrap.driver.probe = usb_probe_interface;
...
	retval = driver_register(&new_driver->drvwrap.driver);
	if (retval)
		goto out;
...
}
EXPORT_SYMBOL_GPL(usb_register_driver); 
driver_register的实现在kernel\base\driver.c中,
int driver_register(struct device_driver *drv)
{
...
	ret = bus_add_driver(drv);
...
}
bus_add_driver的实现在kernel\base\bus.c中,
int bus_add_driver(struct device_driver *drv)
{
...
	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
				     "%s", drv->name);
...

	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
...	
     module_add_driver(drv->owner, drv);
...
}

这段处理大概就是把hub驱动加入到了一个链表中,因为链表就是拿来做数据操作,基本就是增加,删除,修改,遍历查找的,后续用到的时候再讲即可,hub注册部分就是这样了。

  

b. 注册usb部分:

打开kernel\driver\usb\core\usb.c,就在注册hub驱动的下3行,注册了usb设备驱动,

static int __init usb_init(void)
{
...
	retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
...
}

然后进入kernel\driverusb\core\driver.c中的,

int usb_register_device_driver(struct usb_device_driver *new_udriver,
		struct module *owner)
{
...
	new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
	new_udriver->drvwrap.driver.bus = &usb_bus_type;
	new_udriver->drvwrap.driver.probe = usb_probe_device;
...
	retval = driver_register(&new_udriver->drvwrap.driver);

...
}
EXPORT_SYMBOL_GPL(usb_register_device_driver);

 又到了driver_register,最后也就是把usb设备驱动添加到一个链表中,等待着遍历执行的时刻。

 

c. 注册usb-storage部分:

打开usb\storage\usb.c,这里注册了usb-storage的驱动,这个驱动就是与U盘节点有关的。

static struct usb_driver usb_storage_driver = {
	.name =		"usb-storage",
...
};

module_usb_driver(usb_storage_driver);

可以看看它的实现,打开kernel\include\linux\usb.h,

#define module_usb_driver(__usb_driver) \
	module_driver(__usb_driver, usb_register, \
		       usb_deregister)

可以在kernel\include\linux\device.h查看module_driver的实现,

#define module_driver(__driver, __register, __unregister, ...) \
static int __init __driver##_init(void) \
{ \
	return __register(&(__driver) , ##__VA_ARGS__); \
} \
module_init(__driver##_init); \
static void __exit __driver##_exit(void) \
{ \
	__unregister(&(__driver) , ##__VA_ARGS__); \
} \
module_exit(__driver##_exit);  

 就是一个宏,注册用usb_register,反向注册用usb_deregister,然后再module_init它,就会在开机的时候执行了。至于usb_register,最后也就是把usb-storage驱动添加到一个链表中,等待着遍历执行的时刻。

 

3. 当一个U盘插入linux设备后:

a. 需要有一个线程等待检测U盘插入,重新回到kernel\driverusb\core\hub.c,

int usb_hub_init(void)
{
...
	khubd_task = kthread_run(hub_thread, NULL, "khubd");
...
}

  

static int hub_thread(void *__unused)
{
...
	do {
		hub_events();
		wait_event_freezable(khubd_wait,
				!list_empty(&hub_event_list) ||
				kthread_should_stop());
	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
...
}

  

static void hub_events(void)
{
...
	while (1) {
...            
             hdev = hub->hdev; //这里有一段获取usb驱动设备过程,忽略,因为我还没仔细研究过
... if (connect_change) hub_port_connect_change(hub, i, portstatus, portchange); } ... }

 

static void hub_port_connect_change(struct usb_hub *hub, int port1,
					u16 portstatus, u16 portchange)
{
...
		/* Run it through the hoops (find a driver, etc) */
		if (!status) {
			status = usb_new_device(udev);
...
}

  

int usb_new_device(struct usb_device *udev)
{
...
	err = device_add(&udev->dev);
...
}

 

进入到kernel\base\core.c中,

int device_add(struct device *dev)
{
...
	bus_probe_device(dev);
...
}

  

 进入到kernel\base\bus.c中,

void bus_probe_device(struct device *dev)
{
...
		ret = device_attach(dev);
...
}

  

 进入到kernel\base\dd.c中,

int device_attach(struct device *dev)
{
... ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); ...
}

  

int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
		     void *data, int (*fn)(struct device_driver *, void *))
{
... error = fn(drv, data); ... }

  

static int __device_attach(struct device_driver *drv, void *data)
{
...
	return driver_probe_device(drv, dev);
}

  

int driver_probe_device(struct device_driver *drv, struct device *dev)
{
...
	ret = really_probe(dev, drv);
....
}

  

static int really_probe(struct device *dev, struct device_driver *drv)
{
...
	} else if (drv->probe) {
		ret = drv->probe(dev);
		if (ret)
			goto probe_failed;
	}
...
}

  之前链表插入的usb设备驱动的probe就在此刻被遍历出来,然后调用。

回顾插入的函数指针,打开kernel\driverusb\core\driver.c,

int usb_register_device_driver(struct usb_device_driver *new_udriver,
		struct module *owner)
{
...
	new_udriver->drvwrap.driver.probe = usb_probe_device;
...
}

进入 

static int usb_probe_device(struct device *dev)
{
	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
...
		error = udriver->probe(udev);
...
}

 

由kernel\include\linux\usb.h中:

#define	to_usb_device_driver(d) container_of(d, struct usb_device_driver, \
		drvwrap.driver)

和kernel\driver\usb\core\usb.c中:

retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);

可知

这里的probe会调用usb_generic_driver的probe,因为container_of的作用就是把指向d的指针返回,返回的指针即为&usb_generic_driver。

 

打开kernel\drivers\usb\core\generic.c,

struct usb_device_driver usb_generic_driver = {
	.name =	"usb",
	.probe = generic_probe,
...
};

  

static int generic_probe(struct usb_device *udev)
{
...
			err = usb_set_configuration(udev, c);
...
}

 

打开kernel\drivers\usb\core\message.c,

int usb_set_configuration(struct usb_device *dev, int configuration)
{
...
		ret = device_add(&intf->dev);
...
}

之前提到过device_add->bus_probe_device->device_attach->__device_attach->driver_probe_device->really_probe->传入的设备对应的驱动probe。

在usb_set_configuration或者之前,肯定有一个获取usb-storage驱动信息的过程,总之这次的probe会进入usb_probe_interface,驱动就是之前注册的usb-storage。

打开kernel\driverusb\core\driver.c,

static int usb_probe_interface(struct device *dev)
{
   struct usb_driver *driver = to_usb_driver(dev->driver); ... error = driver->probe(intf, id); ... }

同之前container_of返回指向p的指针分析的一样,这次返回的指针是&usb_storage_driver。

打开kernel\driverusb\storage\usb.c,

static int storage_probe(struct usb_interface *intf,
			 const struct usb_device_id *id)
{
...
     result = usb_stor_probe1(&us, intf, id, unusual_dev);
... result = usb_stor_probe2(us); ... } static struct usb_driver usb_storage_driver = { .name = "usb-storage",
     .probe = storage_probe, ... };

  

int usb_stor_probe1(struct us_data **pus,
		struct usb_interface *intf,
		const struct usb_device_id *id,
		struct us_unusual_dev *unusual_dev)
{
...
	INIT_DELAYED_WORK(&us->scan_dwork, usb_stor_scan_dwork);
...
}

 

int usb_stor_probe2(struct us_data *us)
{
...
	queue_delayed_work(system_freezable_wq, &us->scan_dwork,
			delay_use * HZ);
...
}

 这段就是usb_stor_probe1中注册了一个延时的工作队列,然后usb_stor_probe2唤醒这个工作队列注册的函数usb_stor_scan_dwork工作。

  

static void usb_stor_scan_dwork(struct work_struct *work)
{
...
	scsi_scan_host(us_to_host(us));
...
}

  

下一步就是scsi子系统的工作了。

 

b. sg节点的创建。

打开kernel\driverscsi\scsi_scan.c,

void scsi_scan_host(struct Scsi_Host *shost)
{
...
	async_schedule(do_scan_async, data);
...
}

  

static void do_scan_async(void *_data, async_cookie_t c)
{
...
	scsi_finish_async_scan(data);
}

  

static void scsi_finish_async_scan(struct async_scan_data *data)
{
...
	scsi_sysfs_add_devices(shost);
...
}

  

static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
{
...
		if (!scsi_host_scan_allowed(shost) ||
		    scsi_sysfs_add_sdev(sdev) != 0)
			__scsi_remove_device(sdev);
	}
}

 

打开kernel\driverscsi\scsi_sysfs.c,

int scsi_sysfs_add_sdev(struct scsi_device *sdev)
{
...
	error = device_add(&sdev->sdev_dev);
...
} 

注:这里传的是&sdev->sdev_dev,而不是&sdev->sdev_gendev

又到了device_add,这次可不是走really_probe那么简单了,直接show出关键代码,

打开kernel\base\core.c,

int device_add(struct device *dev)
{
...
			if (class_intf->add_dev)
				class_intf->add_dev(dev, class_intf);
...
}  

add_dev会调用哪个class_interface?

打开kernel\driverscsi\sg.c

static int __init
init_sg(void)
{
...
	rc = scsi_register_interface(&sg_interface);
...
}

  

static struct class_interface sg_interface = {
	.add_dev	= sg_add,
	.remove_dev	= sg_remove,
};

可知调用的add_dev就是sg_add,所以节点sg就是以下代码创建的。

static int
sg_add(struct device *cl_dev, struct class_interface *cl_intf)
{
...
	sdp = sg_alloc(disk, scsidp);
...
}

  

static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
{
...
	sprintf(disk->disk_name, "sg%d", k);
...
}

  

源码太多,花了我大把时间才捋清。

大体就是,注册一堆东东,总线(usb)啊,驱动设备(usb)啊,驱动(hub,usb-storage)啊,class(sg_interface)啊等等,然后跑一个线程,检测到需要的东东后,比对注册到特定链表的数据,然后就调用各种probe和注册的接口如add_dev等。

 

posted @ 2019-11-26 12:02  啊源股  阅读(1403)  评论(0编辑  收藏  举报