Linux usb子系统(一)(4)

初始化并注册一个中断urb。函数原型如下:

static inline void usb_fill_int_urb(struct urb *urb,struct usb_device *dev,unsigned int pipe, void *transfer_buffer,int buffer_length,usb_complete_t complete_fn,void *context,int interval)

这个函数参数比较多, urb表示我们要注册的urb对象; dev表示这个urb对象的目的设备; pipe表示读写管道, 使用usb_sndintpipe()和usb_rcvintpipe()获取; transfer_buffer表示传递数据的缓冲区首地址;buffer_length表示缓冲区长度;complete_fn表示如果我们发出的urb有了回应, 就回调这个函数; context是回调函数的参数, 由用户定义, 相当于request_irq中的void *dev; interval就是发送周期, 核心层会以这个参数为周期通过usb控制器驱动轮询设备,

usb_alloc()

urb和xxx一样,要用内核分配函数,其中会做一些初始化的工作

usb_fill_bulk_urb

初始化并注册一个海量存储urb

usb_fill_control_urb

初始化并注册一个控制urb

usb_submit_urb

通知内核发送urb对象

usb_driver 1048 struct usb_driver { 1049 const char *name; 1051 int (*probe) (struct usb_interface *intf, 1052 const struct usb_device_id *id); 1054 void (*disconnect) (struct usb_interface *intf); 1056 int (*unlocked_ioctl) (struct usb_interface *intf, unsigned int code, 1057 void *buf); 1059 int (*suspend) (struct usb_interface *intf, pm_message_t message); 1060 int (*resume) (struct usb_interface *intf); 1061 int (*reset_resume)(struct usb_interface *intf); 1063 int (*pre_reset)(struct usb_interface *intf); 1064 int (*post_reset)(struct usb_interface *intf); 1066 const struct usb_device_id *id_table; 1068 struct usb_dynids dynids; 1069 struct usbdrv_wrap drvwrap; 1070 unsigned int no_dynamic_id:1; 1071 unsigned int supports_autosuspend:1; 1072 unsigned int disable_hub_initiated_lpm:1; 1073 unsigned int soft_unbind:1; 1074 };

struct usb_driver
--1049-->usb设备的名字
--1051-->探测函数, 当usb_driver的id_table和usb设备信息匹配的时候会执行, 主要的工作是申请资源, 初始化, 提供接口
--1054-->当驱动模块被卸载时或设备被拔出时会被执行
--1066-->功能依然是匹配一样, 只是usb的设备信息由4个维度描述, 所以id_table可以填充的内容也多种多样

usb_register

注册一个usb_driver到内核

usb_deregister

注销一个usb_driver

id_table

内核提供了如下的宏来构造一个usb_device_id对象, 其实也就是对usb_device_id中的不同域进行了填充, 由于设备的差异性, 不同的USB设备会上报不同的设备信息, 但无论上报哪些信息, 一定属于下面这些宏的一种封装. 可以先使用lsusb -v查看设备的硬件信息, 再根据其提供的硬件信息确定id_table编写相应的驱动

USB_DEVICE 811 #define USB_DEVICE(vend, prod) \ 812 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, \ 813 .idVendor = (vend), \ 814 .idProduct = (prod) USB_DEVICE_VER 825 #define USB_DEVICE_VER(vend, prod, lo, hi) \ 826 .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION, \ 827 .idVendor = (vend), \ 828 .idProduct = (prod), \ 829 .bcdDevice_lo = (lo), \ 830 .bcdDevice_hi = (hi) USB_DEVICE_INTERFACE_CLASS 841 #define USB_DEVICE_INTERFACE_CLASS(vend, prod, cl) \ 842 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 843 USB_DEVICE_ID_MATCH_INT_CLASS, \ 844 .idVendor = (vend), \ 845 .idProduct = (prod), \ 846 .bInterfaceClass = (cl) USB_DEVICE_INTERFACE_PROTOCOL 857 #define USB_DEVICE_INTERFACE_PROTOCOL(vend, prod, pr) \ 858 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 859 USB_DEVICE_ID_MATCH_INT_PROTOCOL, \ 860 .idVendor = (vend), \ 861 .idProduct = (prod), \ 862 .bInterfaceProtocol = (pr) USB_DEVICE_INTERFACE_NUMBER 873 #define USB_DEVICE_INTERFACE_NUMBER(vend, prod, num) \ 874 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 875 USB_DEVICE_ID_MATCH_INT_NUMBER, \ 876 .idVendor = (vend), \ 877 .idProduct = (prod), \ 878 .bInterfaceNumber = (num) USB_DEVICE_INFO 889 #define USB_DEVICE_INFO(cl, sc, pr) \ 890 .match_flags = USB_DEVICE_ID_MATCH_DEV_INFO, \ 891 .bDeviceClass = (cl), \ 892 .bDeviceSubClass = (sc), \ 893 .bDeviceProtocol = (pr) 894 USB_INTERFACE_INFO 904 #define USB_INTERFACE_INFO(cl, sc, pr) \ 905 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, \ 906 .bInterfaceClass = (cl), \ 907 .bInterfaceSubClass = (sc), \ 908 .bInterfaceProtocol = (pr) USB_DEVICE_AND_INTERFACE_INFO 924 #define USB_DEVICE_AND_INTERFACE_INFO(vend, prod, cl, sc, pr) \ 925 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ 926 | USB_DEVICE_ID_MATCH_DEVICE, \ 927 .idVendor = (vend), \ 928 .idProduct = (prod), \ 929 .bInterfaceClass = (cl), \ 930 .bInterfaceSubClass = (sc), \ 931 .bInterfaceProtocol = (pr) USB_VENDOR_AND_INTERFACE_INFO 946 #define USB_VENDOR_AND_INTERFACE_INFO(vend, cl, sc, pr) \ 947 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO \ 948 | USB_DEVICE_ID_MATCH_VENDOR, \ 949 .idVendor = (vend), \ 950 .bInterfaceClass = (cl), \ 951 .bInterfaceSubClass = (sc), \ 952 .bInterfaceProtocol = (pr) 953 id_table实例

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bb9ababfed16e313172621482d0b1db4.html