1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun====================================================== 4*4882a593Smuzhiyuncdc_mbim - Driver for CDC MBIM Mobile Broadband modems 5*4882a593Smuzhiyun====================================================== 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunThe cdc_mbim driver supports USB devices conforming to the "Universal 8*4882a593SmuzhiyunSerial Bus Communications Class Subclass Specification for Mobile 9*4882a593SmuzhiyunBroadband Interface Model" [1], which is a further development of 10*4882a593Smuzhiyun"Universal Serial Bus Communications Class Subclass Specifications for 11*4882a593SmuzhiyunNetwork Control Model Devices" [2] optimized for Mobile Broadband 12*4882a593Smuzhiyundevices, aka "3G/LTE modems". 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunCommand Line Parameters 16*4882a593Smuzhiyun======================= 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunThe cdc_mbim driver has no parameters of its own. But the probing 19*4882a593Smuzhiyunbehaviour for NCM 1.0 backwards compatible MBIM functions (an 20*4882a593Smuzhiyun"NCM/MBIM function" as defined in section 3.2 of [1]) is affected 21*4882a593Smuzhiyunby a cdc_ncm driver parameter: 22*4882a593Smuzhiyun 23*4882a593Smuzhiyunprefer_mbim 24*4882a593Smuzhiyun----------- 25*4882a593Smuzhiyun:Type: Boolean 26*4882a593Smuzhiyun:Valid Range: N/Y (0-1) 27*4882a593Smuzhiyun:Default Value: Y (MBIM is preferred) 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunThis parameter sets the system policy for NCM/MBIM functions. Such 30*4882a593Smuzhiyunfunctions will be handled by either the cdc_ncm driver or the cdc_mbim 31*4882a593Smuzhiyundriver depending on the prefer_mbim setting. Setting prefer_mbim=N 32*4882a593Smuzhiyunmakes the cdc_mbim driver ignore these functions and lets the cdc_ncm 33*4882a593Smuzhiyundriver handle them instead. 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunThe parameter is writable, and can be changed at any time. A manual 36*4882a593Smuzhiyununbind/bind is required to make the change effective for NCM/MBIM 37*4882a593Smuzhiyunfunctions bound to the "wrong" driver 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunBasic usage 41*4882a593Smuzhiyun=========== 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunMBIM functions are inactive when unmanaged. The cdc_mbim driver only 44*4882a593Smuzhiyunprovides a userspace interface to the MBIM control channel, and will 45*4882a593Smuzhiyunnot participate in the management of the function. This implies that a 46*4882a593Smuzhiyunuserspace MBIM management application always is required to enable a 47*4882a593SmuzhiyunMBIM function. 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunSuch userspace applications includes, but are not limited to: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun - mbimcli (included with the libmbim [3] library), and 52*4882a593Smuzhiyun - ModemManager [4] 53*4882a593Smuzhiyun 54*4882a593SmuzhiyunEstablishing a MBIM IP session reequires at least these actions by the 55*4882a593Smuzhiyunmanagement application: 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun - open the control channel 58*4882a593Smuzhiyun - configure network connection settings 59*4882a593Smuzhiyun - connect to network 60*4882a593Smuzhiyun - configure IP interface 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunManagement application development 63*4882a593Smuzhiyun---------------------------------- 64*4882a593SmuzhiyunThe driver <-> userspace interfaces are described below. The MBIM 65*4882a593Smuzhiyuncontrol channel protocol is described in [1]. 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunMBIM control channel userspace ABI 69*4882a593Smuzhiyun================================== 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun/dev/cdc-wdmX character device 72*4882a593Smuzhiyun------------------------------ 73*4882a593SmuzhiyunThe driver creates a two-way pipe to the MBIM function control channel 74*4882a593Smuzhiyunusing the cdc-wdm driver as a subdriver. The userspace end of the 75*4882a593Smuzhiyuncontrol channel pipe is a /dev/cdc-wdmX character device. 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunThe cdc_mbim driver does not process or police messages on the control 78*4882a593Smuzhiyunchannel. The channel is fully delegated to the userspace management 79*4882a593Smuzhiyunapplication. It is therefore up to this application to ensure that it 80*4882a593Smuzhiyuncomplies with all the control channel requirements in [1]. 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunThe cdc-wdmX device is created as a child of the MBIM control 83*4882a593Smuzhiyuninterface USB device. The character device associated with a specific 84*4882a593SmuzhiyunMBIM function can be looked up using sysfs. For example:: 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun bjorn@nemi:~$ ls /sys/bus/usb/drivers/cdc_mbim/2-4:2.12/usbmisc 87*4882a593Smuzhiyun cdc-wdm0 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun bjorn@nemi:~$ grep . /sys/bus/usb/drivers/cdc_mbim/2-4:2.12/usbmisc/cdc-wdm0/dev 90*4882a593Smuzhiyun 180:0 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun 93*4882a593SmuzhiyunUSB configuration descriptors 94*4882a593Smuzhiyun----------------------------- 95*4882a593SmuzhiyunThe wMaxControlMessage field of the CDC MBIM functional descriptor 96*4882a593Smuzhiyunlimits the maximum control message size. The managament application is 97*4882a593Smuzhiyunresponsible for negotiating a control message size complying with the 98*4882a593Smuzhiyunrequirements in section 9.3.1 of [1], taking this descriptor field 99*4882a593Smuzhiyuninto consideration. 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunThe userspace application can access the CDC MBIM functional 102*4882a593Smuzhiyundescriptor of a MBIM function using either of the two USB 103*4882a593Smuzhiyunconfiguration descriptor kernel interfaces described in [6] or [7]. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunSee also the ioctl documentation below. 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunFragmentation 109*4882a593Smuzhiyun------------- 110*4882a593SmuzhiyunThe userspace application is responsible for all control message 111*4882a593Smuzhiyunfragmentation and defragmentaion, as described in section 9.5 of [1]. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun/dev/cdc-wdmX write() 115*4882a593Smuzhiyun--------------------- 116*4882a593SmuzhiyunThe MBIM control messages from the management application *must not* 117*4882a593Smuzhiyunexceed the negotiated control message size. 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun/dev/cdc-wdmX read() 121*4882a593Smuzhiyun-------------------- 122*4882a593SmuzhiyunThe management application *must* accept control messages of up the 123*4882a593Smuzhiyunnegotiated control message size. 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun/dev/cdc-wdmX ioctl() 127*4882a593Smuzhiyun--------------------- 128*4882a593SmuzhiyunIOCTL_WDM_MAX_COMMAND: Get Maximum Command Size 129*4882a593SmuzhiyunThis ioctl returns the wMaxControlMessage field of the CDC MBIM 130*4882a593Smuzhiyunfunctional descriptor for MBIM devices. This is intended as a 131*4882a593Smuzhiyunconvenience, eliminating the need to parse the USB descriptors from 132*4882a593Smuzhiyunuserspace. 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun:: 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun #include <stdio.h> 137*4882a593Smuzhiyun #include <fcntl.h> 138*4882a593Smuzhiyun #include <sys/ioctl.h> 139*4882a593Smuzhiyun #include <linux/types.h> 140*4882a593Smuzhiyun #include <linux/usb/cdc-wdm.h> 141*4882a593Smuzhiyun int main() 142*4882a593Smuzhiyun { 143*4882a593Smuzhiyun __u16 max; 144*4882a593Smuzhiyun int fd = open("/dev/cdc-wdm0", O_RDWR); 145*4882a593Smuzhiyun if (!ioctl(fd, IOCTL_WDM_MAX_COMMAND, &max)) 146*4882a593Smuzhiyun printf("wMaxControlMessage is %d\n", max); 147*4882a593Smuzhiyun } 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunCustom device services 151*4882a593Smuzhiyun---------------------- 152*4882a593SmuzhiyunThe MBIM specification allows vendors to freely define additional 153*4882a593Smuzhiyunservices. This is fully supported by the cdc_mbim driver. 154*4882a593Smuzhiyun 155*4882a593SmuzhiyunSupport for new MBIM services, including vendor specified services, is 156*4882a593Smuzhiyunimplemented entirely in userspace, like the rest of the MBIM control 157*4882a593Smuzhiyunprotocol 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunNew services should be registered in the MBIM Registry [5]. 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunMBIM data channel userspace ABI 164*4882a593Smuzhiyun=============================== 165*4882a593Smuzhiyun 166*4882a593SmuzhiyunwwanY network device 167*4882a593Smuzhiyun-------------------- 168*4882a593SmuzhiyunThe cdc_mbim driver represents the MBIM data channel as a single 169*4882a593Smuzhiyunnetwork device of the "wwan" type. This network device is initially 170*4882a593Smuzhiyunmapped to MBIM IP session 0. 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun 173*4882a593SmuzhiyunMultiplexed IP sessions (IPS) 174*4882a593Smuzhiyun----------------------------- 175*4882a593SmuzhiyunMBIM allows multiplexing up to 256 IP sessions over a single USB data 176*4882a593Smuzhiyunchannel. The cdc_mbim driver models such IP sessions as 802.1q VLAN 177*4882a593Smuzhiyunsubdevices of the master wwanY device, mapping MBIM IP session Z to 178*4882a593SmuzhiyunVLAN ID Z for all values of Z greater than 0. 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunThe device maximum Z is given in the MBIM_DEVICE_CAPS_INFO structure 181*4882a593Smuzhiyundescribed in section 10.5.1 of [1]. 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunThe userspace management application is responsible for adding new 184*4882a593SmuzhiyunVLAN links prior to establishing MBIM IP sessions where the SessionId 185*4882a593Smuzhiyunis greater than 0. These links can be added by using the normal VLAN 186*4882a593Smuzhiyunkernel interfaces, either ioctl or netlink. 187*4882a593Smuzhiyun 188*4882a593SmuzhiyunFor example, adding a link for a MBIM IP session with SessionId 3:: 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun ip link add link wwan0 name wwan0.3 type vlan id 3 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunThe driver will automatically map the "wwan0.3" network device to MBIM 193*4882a593SmuzhiyunIP session 3. 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun 196*4882a593SmuzhiyunDevice Service Streams (DSS) 197*4882a593Smuzhiyun---------------------------- 198*4882a593SmuzhiyunMBIM also allows up to 256 non-IP data streams to be multiplexed over 199*4882a593Smuzhiyunthe same shared USB data channel. The cdc_mbim driver models these 200*4882a593Smuzhiyunsessions as another set of 802.1q VLAN subdevices of the master wwanY 201*4882a593Smuzhiyundevice, mapping MBIM DSS session A to VLAN ID (256 + A) for all values 202*4882a593Smuzhiyunof A. 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunThe device maximum A is given in the MBIM_DEVICE_SERVICES_INFO 205*4882a593Smuzhiyunstructure described in section 10.5.29 of [1]. 206*4882a593Smuzhiyun 207*4882a593SmuzhiyunThe DSS VLAN subdevices are used as a practical interface between the 208*4882a593Smuzhiyunshared MBIM data channel and a MBIM DSS aware userspace application. 209*4882a593SmuzhiyunIt is not intended to be presented as-is to an end user. The 210*4882a593Smuzhiyunassumption is that a userspace application initiating a DSS session 211*4882a593Smuzhiyunalso takes care of the necessary framing of the DSS data, presenting 212*4882a593Smuzhiyunthe stream to the end user in an appropriate way for the stream type. 213*4882a593Smuzhiyun 214*4882a593SmuzhiyunThe network device ABI requires a dummy ethernet header for every DSS 215*4882a593Smuzhiyundata frame being transported. The contents of this header is 216*4882a593Smuzhiyunarbitrary, with the following exceptions: 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun - TX frames using an IP protocol (0x0800 or 0x86dd) will be dropped 219*4882a593Smuzhiyun - RX frames will have the protocol field set to ETH_P_802_3 (but will 220*4882a593Smuzhiyun not be properly formatted 802.3 frames) 221*4882a593Smuzhiyun - RX frames will have the destination address set to the hardware 222*4882a593Smuzhiyun address of the master device 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunThe DSS supporting userspace management application is responsible for 225*4882a593Smuzhiyunadding the dummy ethernet header on TX and stripping it on RX. 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunThis is a simple example using tools commonly available, exporting 228*4882a593SmuzhiyunDssSessionId 5 as a pty character device pointed to by a /dev/nmea 229*4882a593Smuzhiyunsymlink:: 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun ip link add link wwan0 name wwan0.dss5 type vlan id 261 232*4882a593Smuzhiyun ip link set dev wwan0.dss5 up 233*4882a593Smuzhiyun socat INTERFACE:wwan0.dss5,type=2 PTY:,echo=0,link=/dev/nmea 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunThis is only an example, most suitable for testing out a DSS 236*4882a593Smuzhiyunservice. Userspace applications supporting specific MBIM DSS services 237*4882a593Smuzhiyunare expected to use the tools and programming interfaces required by 238*4882a593Smuzhiyunthat service. 239*4882a593Smuzhiyun 240*4882a593SmuzhiyunNote that adding VLAN links for DSS sessions is entirely optional. A 241*4882a593Smuzhiyunmanagement application may instead choose to bind a packet socket 242*4882a593Smuzhiyundirectly to the master network device, using the received VLAN tags to 243*4882a593Smuzhiyunmap frames to the correct DSS session and adding 18 byte VLAN ethernet 244*4882a593Smuzhiyunheaders with the appropriate tag on TX. In this case using a socket 245*4882a593Smuzhiyunfilter is recommended, matching only the DSS VLAN subset. This avoid 246*4882a593Smuzhiyununnecessary copying of unrelated IP session data to userspace. For 247*4882a593Smuzhiyunexample:: 248*4882a593Smuzhiyun 249*4882a593Smuzhiyun static struct sock_filter dssfilter[] = { 250*4882a593Smuzhiyun /* use special negative offsets to get VLAN tag */ 251*4882a593Smuzhiyun BPF_STMT(BPF_LD|BPF_B|BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT), 252*4882a593Smuzhiyun BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 1, 0, 6), /* true */ 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun /* verify DSS VLAN range */ 255*4882a593Smuzhiyun BPF_STMT(BPF_LD|BPF_H|BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG), 256*4882a593Smuzhiyun BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 256, 0, 4), /* 256 is first DSS VLAN */ 257*4882a593Smuzhiyun BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 512, 3, 0), /* 511 is last DSS VLAN */ 258*4882a593Smuzhiyun 259*4882a593Smuzhiyun /* verify ethertype */ 260*4882a593Smuzhiyun BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 2 * ETH_ALEN), 261*4882a593Smuzhiyun BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETH_P_802_3, 0, 1), 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun BPF_STMT(BPF_RET|BPF_K, (u_int)-1), /* accept */ 264*4882a593Smuzhiyun BPF_STMT(BPF_RET|BPF_K, 0), /* ignore */ 265*4882a593Smuzhiyun }; 266*4882a593Smuzhiyun 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun 269*4882a593SmuzhiyunTagged IP session 0 VLAN 270*4882a593Smuzhiyun------------------------ 271*4882a593SmuzhiyunAs described above, MBIM IP session 0 is treated as special by the 272*4882a593Smuzhiyundriver. It is initially mapped to untagged frames on the wwanY 273*4882a593Smuzhiyunnetwork device. 274*4882a593Smuzhiyun 275*4882a593SmuzhiyunThis mapping implies a few restrictions on multiplexed IPS and DSS 276*4882a593Smuzhiyunsessions, which may not always be practical: 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun - no IPS or DSS session can use a frame size greater than the MTU on 279*4882a593Smuzhiyun IP session 0 280*4882a593Smuzhiyun - no IPS or DSS session can be in the up state unless the network 281*4882a593Smuzhiyun device representing IP session 0 also is up 282*4882a593Smuzhiyun 283*4882a593SmuzhiyunThese problems can be avoided by optionally making the driver map IP 284*4882a593Smuzhiyunsession 0 to a VLAN subdevice, similar to all other IP sessions. This 285*4882a593Smuzhiyunbehaviour is triggered by adding a VLAN link for the magic VLAN ID 286*4882a593Smuzhiyun4094. The driver will then immediately start mapping MBIM IP session 287*4882a593Smuzhiyun0 to this VLAN, and will drop untagged frames on the master wwanY 288*4882a593Smuzhiyundevice. 289*4882a593Smuzhiyun 290*4882a593SmuzhiyunTip: It might be less confusing to the end user to name this VLAN 291*4882a593Smuzhiyunsubdevice after the MBIM SessionID instead of the VLAN ID. For 292*4882a593Smuzhiyunexample:: 293*4882a593Smuzhiyun 294*4882a593Smuzhiyun ip link add link wwan0 name wwan0.0 type vlan id 4094 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun 297*4882a593SmuzhiyunVLAN mapping 298*4882a593Smuzhiyun------------ 299*4882a593Smuzhiyun 300*4882a593SmuzhiyunSummarizing the cdc_mbim driver mapping described above, we have this 301*4882a593Smuzhiyunrelationship between VLAN tags on the wwanY network device and MBIM 302*4882a593Smuzhiyunsessions on the shared USB data channel:: 303*4882a593Smuzhiyun 304*4882a593Smuzhiyun VLAN ID MBIM type MBIM SessionID Notes 305*4882a593Smuzhiyun --------------------------------------------------------- 306*4882a593Smuzhiyun untagged IPS 0 a) 307*4882a593Smuzhiyun 1 - 255 IPS 1 - 255 <VLANID> 308*4882a593Smuzhiyun 256 - 511 DSS 0 - 255 <VLANID - 256> 309*4882a593Smuzhiyun 512 - 4093 b) 310*4882a593Smuzhiyun 4094 IPS 0 c) 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun a) if no VLAN ID 4094 link exists, else dropped 313*4882a593Smuzhiyun b) unsupported VLAN range, unconditionally dropped 314*4882a593Smuzhiyun c) if a VLAN ID 4094 link exists, else dropped 315*4882a593Smuzhiyun 316*4882a593Smuzhiyun 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun 319*4882a593SmuzhiyunReferences 320*4882a593Smuzhiyun========== 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun 1) USB Implementers Forum, Inc. - "Universal Serial Bus 323*4882a593Smuzhiyun Communications Class Subclass Specification for Mobile Broadband 324*4882a593Smuzhiyun Interface Model", Revision 1.0 (Errata 1), May 1, 2013 325*4882a593Smuzhiyun 326*4882a593Smuzhiyun - http://www.usb.org/developers/docs/devclass_docs/ 327*4882a593Smuzhiyun 328*4882a593Smuzhiyun 2) USB Implementers Forum, Inc. - "Universal Serial Bus 329*4882a593Smuzhiyun Communications Class Subclass Specifications for Network Control 330*4882a593Smuzhiyun Model Devices", Revision 1.0 (Errata 1), November 24, 2010 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun - http://www.usb.org/developers/docs/devclass_docs/ 333*4882a593Smuzhiyun 334*4882a593Smuzhiyun 3) libmbim - "a glib-based library for talking to WWAN modems and 335*4882a593Smuzhiyun devices which speak the Mobile Interface Broadband Model (MBIM) 336*4882a593Smuzhiyun protocol" 337*4882a593Smuzhiyun 338*4882a593Smuzhiyun - http://www.freedesktop.org/wiki/Software/libmbim/ 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun 4) ModemManager - "a DBus-activated daemon which controls mobile 341*4882a593Smuzhiyun broadband (2G/3G/4G) devices and connections" 342*4882a593Smuzhiyun 343*4882a593Smuzhiyun - http://www.freedesktop.org/wiki/Software/ModemManager/ 344*4882a593Smuzhiyun 345*4882a593Smuzhiyun 5) "MBIM (Mobile Broadband Interface Model) Registry" 346*4882a593Smuzhiyun 347*4882a593Smuzhiyun - http://compliance.usb.org/mbim/ 348*4882a593Smuzhiyun 349*4882a593Smuzhiyun 6) "/sys/kernel/debug/usb/devices output format" 350*4882a593Smuzhiyun 351*4882a593Smuzhiyun - Documentation/driver-api/usb/usb.rst 352*4882a593Smuzhiyun 353*4882a593Smuzhiyun 7) "/sys/bus/usb/devices/.../descriptors" 354*4882a593Smuzhiyun 355*4882a593Smuzhiyun - Documentation/ABI/stable/sysfs-bus-usb 356