cdc_mbim.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. cdc_mbim - Driver for CDC MBIM Mobile Broadband modems
  2. ========================================================
  3. The cdc_mbim driver supports USB devices conforming to the "Universal
  4. Serial Bus Communications Class Subclass Specification for Mobile
  5. Broadband Interface Model" [1], which is a further development of
  6. "Universal Serial Bus Communications Class Subclass Specifications for
  7. Network Control Model Devices" [2] optimized for Mobile Broadband
  8. devices, aka "3G/LTE modems".
  9. Command Line Parameters
  10. =======================
  11. The cdc_mbim driver has no parameters of its own. But the probing
  12. behaviour for NCM 1.0 backwards compatible MBIM functions (an
  13. "NCM/MBIM function" as defined in section 3.2 of [1]) is affected
  14. by a cdc_ncm driver parameter:
  15. prefer_mbim
  16. -----------
  17. Type: Boolean
  18. Valid Range: N/Y (0-1)
  19. Default Value: Y (MBIM is preferred)
  20. This parameter sets the system policy for NCM/MBIM functions. Such
  21. functions will be handled by either the cdc_ncm driver or the cdc_mbim
  22. driver depending on the prefer_mbim setting. Setting prefer_mbim=N
  23. makes the cdc_mbim driver ignore these functions and lets the cdc_ncm
  24. driver handle them instead.
  25. The parameter is writable, and can be changed at any time. A manual
  26. unbind/bind is required to make the change effective for NCM/MBIM
  27. functions bound to the "wrong" driver
  28. Basic usage
  29. ===========
  30. MBIM functions are inactive when unmanaged. The cdc_mbim driver only
  31. provides an userspace interface to the MBIM control channel, and will
  32. not participate in the management of the function. This implies that a
  33. userspace MBIM management application always is required to enable a
  34. MBIM function.
  35. Such userspace applications includes, but are not limited to:
  36. - mbimcli (included with the libmbim [3] library), and
  37. - ModemManager [4]
  38. Establishing a MBIM IP session reequires at least these actions by the
  39. management application:
  40. - open the control channel
  41. - configure network connection settings
  42. - connect to network
  43. - configure IP interface
  44. Management application development
  45. ----------------------------------
  46. The driver <-> userspace interfaces are described below. The MBIM
  47. control channel protocol is described in [1].
  48. MBIM control channel userspace ABI
  49. ==================================
  50. /dev/cdc-wdmX character device
  51. ------------------------------
  52. The driver creates a two-way pipe to the MBIM function control channel
  53. using the cdc-wdm driver as a subdriver. The userspace end of the
  54. control channel pipe is a /dev/cdc-wdmX character device.
  55. The cdc_mbim driver does not process or police messages on the control
  56. channel. The channel is fully delegated to the userspace management
  57. application. It is therefore up to this application to ensure that it
  58. complies with all the control channel requirements in [1].
  59. The cdc-wdmX device is created as a child of the MBIM control
  60. interface USB device. The character device associated with a specific
  61. MBIM function can be looked up using sysfs. For example:
  62. bjorn@nemi:~$ ls /sys/bus/usb/drivers/cdc_mbim/2-4:2.12/usbmisc
  63. cdc-wdm0
  64. bjorn@nemi:~$ grep . /sys/bus/usb/drivers/cdc_mbim/2-4:2.12/usbmisc/cdc-wdm0/dev
  65. 180:0
  66. USB configuration descriptors
  67. -----------------------------
  68. The wMaxControlMessage field of the CDC MBIM functional descriptor
  69. limits the maximum control message size. The managament application is
  70. responsible for negotiating a control message size complying with the
  71. requirements in section 9.3.1 of [1], taking this descriptor field
  72. into consideration.
  73. The userspace application can access the CDC MBIM functional
  74. descriptor of a MBIM function using either of the two USB
  75. configuration descriptor kernel interfaces described in [6] or [7].
  76. See also the ioctl documentation below.
  77. Fragmentation
  78. -------------
  79. The userspace application is responsible for all control message
  80. fragmentation and defragmentaion, as described in section 9.5 of [1].
  81. /dev/cdc-wdmX write()
  82. ---------------------
  83. The MBIM control messages from the management application *must not*
  84. exceed the negotiated control message size.
  85. /dev/cdc-wdmX read()
  86. --------------------
  87. The management application *must* accept control messages of up the
  88. negotiated control message size.
  89. /dev/cdc-wdmX ioctl()
  90. --------------------
  91. IOCTL_WDM_MAX_COMMAND: Get Maximum Command Size
  92. This ioctl returns the wMaxControlMessage field of the CDC MBIM
  93. functional descriptor for MBIM devices. This is intended as a
  94. convenience, eliminating the need to parse the USB descriptors from
  95. userspace.
  96. #include <stdio.h>
  97. #include <fcntl.h>
  98. #include <sys/ioctl.h>
  99. #include <linux/types.h>
  100. #include <linux/usb/cdc-wdm.h>
  101. int main()
  102. {
  103. __u16 max;
  104. int fd = open("/dev/cdc-wdm0", O_RDWR);
  105. if (!ioctl(fd, IOCTL_WDM_MAX_COMMAND, &max))
  106. printf("wMaxControlMessage is %d\n", max);
  107. }
  108. Custom device services
  109. ----------------------
  110. The MBIM specification allows vendors to freely define additional
  111. services. This is fully supported by the cdc_mbim driver.
  112. Support for new MBIM services, including vendor specified services, is
  113. implemented entirely in userspace, like the rest of the MBIM control
  114. protocol
  115. New services should be registered in the MBIM Registry [5].
  116. MBIM data channel userspace ABI
  117. ===============================
  118. wwanY network device
  119. --------------------
  120. The cdc_mbim driver represents the MBIM data channel as a single
  121. network device of the "wwan" type. This network device is initially
  122. mapped to MBIM IP session 0.
  123. Multiplexed IP sessions (IPS)
  124. -----------------------------
  125. MBIM allows multiplexing up to 256 IP sessions over a single USB data
  126. channel. The cdc_mbim driver models such IP sessions as 802.1q VLAN
  127. subdevices of the master wwanY device, mapping MBIM IP session Z to
  128. VLAN ID Z for all values of Z greater than 0.
  129. The device maximum Z is given in the MBIM_DEVICE_CAPS_INFO structure
  130. described in section 10.5.1 of [1].
  131. The userspace management application is responsible for adding new
  132. VLAN links prior to establishing MBIM IP sessions where the SessionId
  133. is greater than 0. These links can be added by using the normal VLAN
  134. kernel interfaces, either ioctl or netlink.
  135. For example, adding a link for a MBIM IP session with SessionId 3:
  136. ip link add link wwan0 name wwan0.3 type vlan id 3
  137. The driver will automatically map the "wwan0.3" network device to MBIM
  138. IP session 3.
  139. Device Service Streams (DSS)
  140. ----------------------------
  141. MBIM also allows up to 256 non-IP data streams to be multiplexed over
  142. the same shared USB data channel. The cdc_mbim driver models these
  143. sessions as another set of 802.1q VLAN subdevices of the master wwanY
  144. device, mapping MBIM DSS session A to VLAN ID (256 + A) for all values
  145. of A.
  146. The device maximum A is given in the MBIM_DEVICE_SERVICES_INFO
  147. structure described in section 10.5.29 of [1].
  148. The DSS VLAN subdevices are used as a practical interface between the
  149. shared MBIM data channel and a MBIM DSS aware userspace application.
  150. It is not intended to be presented as-is to an end user. The
  151. assumption is that an userspace application initiating a DSS session
  152. also takes care of the necessary framing of the DSS data, presenting
  153. the stream to the end user in an appropriate way for the stream type.
  154. The network device ABI requires a dummy ethernet header for every DSS
  155. data frame being transported. The contents of this header is
  156. arbitrary, with the following exceptions:
  157. - TX frames using an IP protocol (0x0800 or 0x86dd) will be dropped
  158. - RX frames will have the protocol field set to ETH_P_802_3 (but will
  159. not be properly formatted 802.3 frames)
  160. - RX frames will have the destination address set to the hardware
  161. address of the master device
  162. The DSS supporting userspace management application is responsible for
  163. adding the dummy ethernet header on TX and stripping it on RX.
  164. This is a simple example using tools commonly available, exporting
  165. DssSessionId 5 as a pty character device pointed to by a /dev/nmea
  166. symlink:
  167. ip link add link wwan0 name wwan0.dss5 type vlan id 261
  168. ip link set dev wwan0.dss5 up
  169. socat INTERFACE:wwan0.dss5,type=2 PTY:,echo=0,link=/dev/nmea
  170. This is only an example, most suitable for testing out a DSS
  171. service. Userspace applications supporting specific MBIM DSS services
  172. are expected to use the tools and programming interfaces required by
  173. that service.
  174. Note that adding VLAN links for DSS sessions is entirely optional. A
  175. management application may instead choose to bind a packet socket
  176. directly to the master network device, using the received VLAN tags to
  177. map frames to the correct DSS session and adding 18 byte VLAN ethernet
  178. headers with the appropriate tag on TX. In this case using a socket
  179. filter is recommended, matching only the DSS VLAN subset. This avoid
  180. unnecessary copying of unrelated IP session data to userspace. For
  181. example:
  182. static struct sock_filter dssfilter[] = {
  183. /* use special negative offsets to get VLAN tag */
  184. BPF_STMT(BPF_LD|BPF_B|BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT),
  185. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 1, 0, 6), /* true */
  186. /* verify DSS VLAN range */
  187. BPF_STMT(BPF_LD|BPF_H|BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG),
  188. BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 256, 0, 4), /* 256 is first DSS VLAN */
  189. BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, 512, 3, 0), /* 511 is last DSS VLAN */
  190. /* verify ethertype */
  191. BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 2 * ETH_ALEN),
  192. BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ETH_P_802_3, 0, 1),
  193. BPF_STMT(BPF_RET|BPF_K, (u_int)-1), /* accept */
  194. BPF_STMT(BPF_RET|BPF_K, 0), /* ignore */
  195. };
  196. Tagged IP session 0 VLAN
  197. ------------------------
  198. As described above, MBIM IP session 0 is treated as special by the
  199. driver. It is initially mapped to untagged frames on the wwanY
  200. network device.
  201. This mapping implies a few restrictions on multiplexed IPS and DSS
  202. sessions, which may not always be practical:
  203. - no IPS or DSS session can use a frame size greater than the MTU on
  204. IP session 0
  205. - no IPS or DSS session can be in the up state unless the network
  206. device representing IP session 0 also is up
  207. These problems can be avoided by optionally making the driver map IP
  208. session 0 to a VLAN subdevice, similar to all other IP sessions. This
  209. behaviour is triggered by adding a VLAN link for the magic VLAN ID
  210. 4094. The driver will then immediately start mapping MBIM IP session
  211. 0 to this VLAN, and will drop untagged frames on the master wwanY
  212. device.
  213. Tip: It might be less confusing to the end user to name this VLAN
  214. subdevice after the MBIM SessionID instead of the VLAN ID. For
  215. example:
  216. ip link add link wwan0 name wwan0.0 type vlan id 4094
  217. VLAN mapping
  218. ------------
  219. Summarizing the cdc_mbim driver mapping described above, we have this
  220. relationship between VLAN tags on the wwanY network device and MBIM
  221. sessions on the shared USB data channel:
  222. VLAN ID MBIM type MBIM SessionID Notes
  223. ---------------------------------------------------------
  224. untagged IPS 0 a)
  225. 1 - 255 IPS 1 - 255 <VLANID>
  226. 256 - 511 DSS 0 - 255 <VLANID - 256>
  227. 512 - 4093 b)
  228. 4094 IPS 0 c)
  229. a) if no VLAN ID 4094 link exists, else dropped
  230. b) unsupported VLAN range, unconditionally dropped
  231. c) if a VLAN ID 4094 link exists, else dropped
  232. References
  233. ==========
  234. [1] USB Implementers Forum, Inc. - "Universal Serial Bus
  235. Communications Class Subclass Specification for Mobile Broadband
  236. Interface Model", Revision 1.0 (Errata 1), May 1, 2013
  237. - http://www.usb.org/developers/docs/devclass_docs/
  238. [2] USB Implementers Forum, Inc. - "Universal Serial Bus
  239. Communications Class Subclass Specifications for Network Control
  240. Model Devices", Revision 1.0 (Errata 1), November 24, 2010
  241. - http://www.usb.org/developers/docs/devclass_docs/
  242. [3] libmbim - "a glib-based library for talking to WWAN modems and
  243. devices which speak the Mobile Interface Broadband Model (MBIM)
  244. protocol"
  245. - http://www.freedesktop.org/wiki/Software/libmbim/
  246. [4] ModemManager - "a DBus-activated daemon which controls mobile
  247. broadband (2G/3G/4G) devices and connections"
  248. - http://www.freedesktop.org/wiki/Software/ModemManager/
  249. [5] "MBIM (Mobile Broadband Interface Model) Registry"
  250. - http://compliance.usb.org/mbim/
  251. [6] "/proc/bus/usb filesystem output"
  252. - Documentation/usb/proc_usb_info.txt
  253. [7] "/sys/bus/usb/devices/.../descriptors"
  254. - Documentation/ABI/stable/sysfs-bus-usb