linux 下 usb子系统中的宏转换的疑问
最近在学习usb驱动,对其中的一个宏定义,在应用上感觉无法理解:
#define to_usb_interface(d) container_of(d, struct usb_interface, dev)
这个宏本身是返回d(struct device d)这个结构体的首地址,如果参数d是
struct usb_interface {
struct usb_host_interface *altsetting;
struct usb_host_interface *cur_altsetting;
unsigned num_altsetting;
int minor;
enum usb_interface_condition condition;
struct device dev;
struct class_device *class_dev;
};
中的dev,那就很自然的返回这个dev所对应的struct usb_interface的结构体的首地址,但是,在查阅代码中发现,有时候这个d所对应的struct device dev是usb_device中的dev成员,这时,请问如何理解?比如,我跟踪了一下从hub检测到usb插入到匹配设备驱动的整个流程,大致如下:
1.hub发现有usb设备插入后,通过中断hub_irq()函数调用hub_event来检查hub的端口
2.确定发现设备后,调用struct usb_device *udev = usb_alloc_dev(hdev, hdev->bus, port1);分配一个struct usb_device 空间。
3.调用usb_new_device(udev);把udev设备加入设备模型。
4.int usb_new_device(struct usb_device *udev)又会调用device_add继续执行加入设备模型的动作
5.int device_add(struct device *dev)函数又调用bus_attach_device(dev);继续向下走。
6.void bus_attach_device(struct device * dev)又调用device_attach继续向下走
7.int device_attach(struct device * dev)又调用bus_for_each_drv继续向下走
8.bus_for_each_drv函数又调用__device_attach函数继续向下走
9.static int __device_attach(struct device_driver * drv, void * data)调用driver_probe_device继续向下走。
10.driver_probe_device函数会调用drv->probe(dev)继续往下走
11.drv->probe(dev)实际上就是static int usb_probe_interface(struct device *dev)函数:
static int usb_probe_interface(struct device *dev)
{
struct usb_interface * intf = to_usb_interface(dev);
struct usb_driver * driver = to_usb_driver(dev->driver);
......
}
这里的struct usb_interface * intf = to_usb_interface(dev);
是返回一个usb_interface的结构体,但是,这个宏的参数dev是hub发现usb设备创立的usb_device,即参数是struct usb_device,并非struct usb_interface结构体,如此,虽然这两个结构体中都有struct device dev这个成语,但是代表的含义明显不同,一个代表的是usb设备,一个代表的是usb接口,那么这个宏返回,是否有问题。
我想返回肯定是正确的,但是,我不知道我在哪里地方理解错了。
各位达人,请问错在哪里?