USB NCM usbnet 枚舉流程代碼分析
一、cdc_ncm.c
在linux 驅(qū)動中,CDC NCM的驅(qū)動位于: ??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);
在結(jié)構(gòu)體??cdc_ncm_driver?
?中,比較重要的是??id_table?
?,??probe?
?這兩個,
在??id_table?
? 中定義了所有符合??cdc_ncm?
?設(shè)備的 ??idVendor?
?號,當??linux?
? 枚舉時檢測到USB設(shè)備的??idVendor?
?在這個??id_table?
?中時,就會調(diào)用??usbnet_probe?
?進行起網(wǎng)操作。
1.1 struct usb_device_id結(jié)構(gòu)體
在分析??cdc_ncm?
?的??id_table?
?之前,我們先來看下??usb_device_id?
? 結(jié)構(gòu)體是如何定義的:
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; //產(chǎn)品ID
__u16 bcdDevice_lo;
__u16 bcdDevice_hi;
/* Used for device class matches */
__u8 bDeviceClass; //設(shè)備類型
__u8 bDeviceSubClass; //設(shè)備子類型
__u8 bDeviceProtocol; //協(xié)議
/* Used for interface class matches */
__u8 bInterfaceClass; //接口類
__u8 bInterfaceSubClass; //接口子類
__u8 bInterfaceProtocol; //接口協(xié)議
/* Used for vendor-specific interface matches */
__u8 bInterfaceNumber;
/* not matched against */
kernel_ulong_t driver_info
__attribute__((aligned(sizeof(kernel_ulong_t)))); // NCM 驅(qū)動結(jié)構(gòu)體,其中包含了收發(fā)包的函數(shù)等
};
結(jié)合結(jié)構(gòu)體定義,我們來看下??cdc_ncm?
?的??id_table?
?:
static const struct usb_device_id cdc_devs[] = {
// 愛立信 F5521gw USB網(wǎng)卡
/* Ericsson MBM devices like F5521gw */
{ .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | USB_DEVICE_ID_MATCH_VENDOR,
//匹配方式:CLASS(設(shè)備類)、SUBCLASS(子類)、PROTOCOL(設(shè)備協(xié)議)
.idVendor = 0x0bdb, //供應商ID: 0x0bdb
.bInterfaceClass = USB_CLASS_COMM, //設(shè)備類型: 網(wǎng)卡類
.bInterfaceSubClass = USB_CDC_SUBCLASS_NCM, //設(shè)備子類型:NCM
.bInterfaceProtocol = USB_CDC_PROTO_NONE, //設(shè)備協(xié)議: 無
.driver_info = (unsigned long) &wwan_info, // NCM 驅(qū)動結(jié)構(gòu)體,其中包含了收發(fā)包的函數(shù)等
},
/* 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 設(shè)備類型
所有的??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 */
//聲音設(shè)備
//調(diào)制解調(diào)器,網(wǎng)卡,ISDN連接
//HID設(shè)備,如鼠標,鍵盤
//物理設(shè)備
//靜止圖像捕捉設(shè)備
//打印機
//批量存儲設(shè)備
//USB HUBS
/* chip+ smart card */ //智能卡
/* content security */
//視頻設(shè)備,如網(wǎng)絡攝像頭
//網(wǎng)絡控制設(shè)備
//雜項設(shè)備
//廠商自定義的設(shè)備
wait…
本文摘自 :https://blog.51cto.com/c