interface_to_usbdev 函数
根据probe函数的参数intf获取usb_device接口。
static inline struct usb_device *interface_to_usbdev(struct usb_interface *intf)
{
return to_usb_device(intf->dev.parent);
}
#define to_usb_device(d) container_of(d, struct usb_device, dev)
最终通过container_of宏获得所需要的结构体本身,该宏是通过结构体成员来获取结构体本身,常用。
原型如下:
container_of(ptr, type, member)
ptr:表示结构体中member的地址
type:表示结构体类型
member:表示结构体中的成员
通过ptr的地址可以返回结构体的首地址
container_of的实现:
define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type )( (char )__mptr - offsetof(type,member) );})
该宏实现过程:
首先定义一个临时的数据类型(通过typeof( ((type *)0)->member )获得)与ptr相同的指针变量__mptr,然后用它来保存ptr的值。
用(char *)__mptr减去member在结构体中的偏移量,得到的值就是整个结构体变量的首地址(整个宏的返回值就是这个首地址)。
注:typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型。