linux 下 usb子系统中的宏转换的疑问

image_eye 2012-02-07 04:46:21
最近在学习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接口,那么这个宏返回,是否有问题。

我想返回肯定是正确的,但是,我不知道我在哪里地方理解错了。

各位达人,请问错在哪里?
...全文
86 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy126163 2013-02-26
  • 打赏
  • 举报
回复
在这里出的错,是这个函数:err = device_add(&udev->dev); 在这里,它传递的参数为:struct device *dev这个结构体!!
image_eye 2012-02-07
  • 打赏
  • 举报
回复
没人指点啊,好困惑!
image_eye 2012-02-07
  • 打赏
  • 举报
回复
我看的代码版本是2.6.18的

4,466

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧