USB NCM usbnet 枚舉流程代碼分析
一、cdc_ncm.c
在linux 驅動中,CDC NCM的驅動位于: ??kerneldrivers
etusbcdc_ncm.c?
?
static struct usb_driver cdc_ncm_driver = {
.name = "cdc_ncm",
.id_table = cdc_devs,
.probe = usbnet_probe,
.disconnect = usbnet_disconnect,
.suspend = usbnet_suspend,
.resume = usbnet_resume,
.reset_resume = usbnet_resume,
.supports_autosuspend = 1,
.disable_hub_initiated_lpm = 1,
};
module_usb_driver(cdc_ncm_driver);
在結構體??cdc_ncm_driver?
?中,比較重要的是??id_table?
?,??probe?
?這兩個,
在??id_table?
? 中定義了所有符合??cdc_ncm?
?設備的 ??idVendor?
?號,當??linux?
? 枚舉時檢測到USB設備的??idVendor?
?在這個??id_table?
?中時,就會調用??usbnet_probe?
?進行起網操作。
1.1 struct usb_device_id結構體
在分析??cdc_ncm?
?的??id_table?
?之前,我們先來看下??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; //供應商ID
__u16 idProduct; //產品ID
__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; //接口協議
/* Used for vendor-specific interface matches */
__u8 bInterfaceNumber;
/* not matched against */
kernel_ulong_t driver_info
__attribute__((aligned(sizeof(kernel_ulong_t)))); // NCM 驅動結構體,其中包含了收發(fā)包的函數等
};
結合結構體定義,我們來看下??cdc_ncm?
?的??id_table?
?:
static const struct usb_device_id cdc_devs[] = {
// 愛立信 F5521gw USB網卡
/* Ericsson MBM devices like F5521gw */
{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | USB_DEVICE_ID_MATCH_VENDOR,
//匹配方式:CLASS(設備類)、SUBCLASS(子類)、PROTOCOL(設備協議)
.idVendor = 0x0bdb, //供應商ID: 0x0bdb
.bInterfaceClass = USB_CLASS_COMM, //設備類型: 網卡類
.bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, //設備子類型:NCM
.bInterfaceProtocol = USB_CDC_PROTO_NONE, //設備協議: 無
.driver_info = (unsigned long) &wwan_info, // NCM 驅動結構體,其中包含了收發(fā)包的函數等
},
/* DW5812 LTE Verizon Mobile Broadband Card Unlike DW5550 this device requires FLAG_NOARP */
{ USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bb,
USB_CLASS_COMM,
USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
.driver_info = (unsigned long)&wwan_noarp_info,
},
/* DW5813 LTE AT&T Mobile Broadband Card Unlike DW5550 this device requires FLAG_NOARP */
{ USB_DEVICE_AND_INTERFACE_INFO(0x413c, 0x81bc,
USB_CLASS_COMM,
USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
.driver_info = (unsigned long)&wwan_noarp_info,
},
/* Dell branded MBM devices like DW5550 */
{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | USB_DEVICE_ID_MATCH_VENDOR,
.idVendor = 0x413c,
.bInterfaceClass = USB_CLASS_COMM,
.bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
.bInterfaceProtocol = USB_CDC_PROTO_NONE,
.driver_info = (unsigned long) &wwan_info,
},
/* Toshiba branded MBM devices */
{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | USB_DEVICE_ID_MATCH_VENDOR,
.idVendor = 0x0930,
.bInterfaceClass = USB_CLASS_COMM,
.bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
.bInterfaceProtocol = USB_CDC_PROTO_NONE,
.driver_info = (unsigned long) &wwan_info,
},
/* tag Huawei devices as wwan */
{ USB_VENDOR_AND_INTERFACE_INFO(0x12d1,
USB_CLASS_COMM,
USB_CDC_SUBCLASS_NCM,
USB_CDC_PROTO_NONE),
.driver_info = (unsigned long)&wwan_info,
},
/* Infineon(now Intel) HSPA Modem platform */
{ USB_DEVICE_AND_INTERFACE_INFO(0x1519, 0x0443,
USB_CLASS_COMM,
USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
.driver_info = (unsigned long)&wwan_noarp_info,
},
/* Generic CDC-NCM devices */
{ USB_INTERFACE_INFO(USB_CLASS_COMM,
USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),
.driver_info = (unsigned long)&cdc_ncm_info,
}
};
MODULE_DEVICE_TABLE(usb, cdc_devs);
1.1.1 match_flags 設備類型
所有的??match_flags?
?定義在??mod_devicetable.h?
? 中:
/* Some useful macros to use to create struct usb_device_id */
1.1.2 bInterfaceClass 接口類
/* Device and/or Interface Class codes
* as found in bDeviceClass or bInterfaceClass
* and defined by www.usb.org documents */
/* for DeviceClass */
//聲音設備
//調制解調器,網卡,ISDN連接
//HID設備,如鼠標,鍵盤
//物理設備
//靜止圖像捕捉設備
//打印機
//批量存儲設備
//USB HUBS
/* chip+ smart card */ //智能卡
/* content security */
//視頻設備,如網絡攝像頭
//網絡控制設備
//雜項設備
//廠商自定義的設備
wait…
本文摘自 :https://blog.51cto.com/c