Usb/core/driver.c 中提供的一些例程
先说一个结构体
Usb_device_id
struct usb_device_id {
/* which fields to match against? */
__u16 match_flags;
/* Used for product specific matches; range is inclusive */
__u16 idVendor;
__u16 idProduct;
__u16 bcdDevice_lo;
__u16 bcdDevice_hi;
/* Used for device class matches */
__u8 bDeviceClass;
__u8 bDeviceSubClass;
__u8 bDeviceProtocol;
/* Used for interface class matches */
__u8 bInterfaceClass;
__u8 bInterfaceSubClass;
__u8 bInterfaceProtocol;
/* not matched against */
kernel_ulong_t driver_info;
};
这个结构体用来在探测和热插拔过程中来标识一个usb device
usb_store_new_id
存储一个usb 设备ID,然后使用指定驱动来探测所有设备
Usb 驱动和设备驱动的关系如下
struct usbdrv_wrap {
struct device_driver driver;
int for_devices;
};
注意 for_devices 非0表示是设备驱动,为0 表示为接口驱动
struct usb_driver {
const char *name;
int (*probe) (struct usb_interface *intf,
const struct usb_device_id *id);
void (*disconnect) (struct usb_interface *intf);
int (*ioctl) (struct usb_interface *intf, unsigned int code,
void *buf);
int (*suspend) (struct usb_interface *intf, pm_message_t message);
int (*resume) (struct usb_interface *intf);
void (*pre_reset) (struct usb_interface *intf);
void (*post_reset) (struct usb_interface *intf);
const struct usb_device_id *id_table;
struct usb_dynids dynids;
struct usbdrv_wrap drvwrap;
unsigned int no_dynamic_id:1;
unsigned int supports_autosuspend:1;
};
在usb_driver 中有设备id结构体链表的指针
从设备驱动也可以方便找到usb_driver
所以添加usb设备id,不需要传入设备id链表
也就有了下面这个函数
store_new_id
struct driver_attribute {
struct attribute attr;
ssize_t (*show)(struct device_driver *, char * buf);
ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
};
驱动属性实际上也就是在属性基础上封装了show和store 方法
struct attribute {
const char * name;
struct module * owner;
mode_t mode;
};
名字,所有者,模式
#define DRIVER_ATTR(_name,_mode,_show,_store) /
struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
定义了一个驱动属性的宏
static int usb_create_newid_file(struct usb_driver *usb_drv)
{
int error = 0;
if (usb_drv->no_dynamic_id)
goto exit;
if (usb_drv->probe != NULL)
error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj,
&driver_attr_new_id.attr);
exit:
return error;
}
为一个usb驱动添加一个属性?
实际上也就是目录实体下创建了一个子目录实体
有添加就有移除
usb_remove_newid_file
usb_free_dynids
移除一个设备ID
usb_match_dynamic_id
为指定的usb_interface 找一个匹配的设备ID
usb_probe_device
探测设备
usb_unbind_device
直接调用usb驱动的disconnect
usb_probe_interface
首先得在id_table 中查找,如果找不到就得在动态id表中进行查找