gadget_uvc.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. =======================
  2. Linux UVC Gadget Driver
  3. =======================
  4. Overview
  5. --------
  6. The UVC Gadget driver is a driver for hardware on the *device* side of a USB
  7. connection. It is intended to run on a Linux system that has USB device-side
  8. hardware such as boards with an OTG port.
  9. On the device system, once the driver is bound it appears as a V4L2 device with
  10. the output capability.
  11. On the host side (once connected via USB cable), a device running the UVC Gadget
  12. driver *and controlled by an appropriate userspace program* should appear as a UVC
  13. specification compliant camera, and function appropriately with any program
  14. designed to handle them. The userspace program running on the device system can
  15. queue image buffers from a variety of sources to be transmitted via the USB
  16. connection. Typically this would mean forwarding the buffers from a camera sensor
  17. peripheral, but the source of the buffer is entirely dependent on the userspace
  18. companion program.
  19. Configuring the device kernel
  20. -----------------------------
  21. The Kconfig options USB_CONFIGFS, USB_LIBCOMPOSITE, USB_CONFIGFS_F_UVC and
  22. USB_F_UVC must be selected to enable support for the UVC gadget.
  23. Configuring the gadget through configfs
  24. ---------------------------------------
  25. The UVC Gadget expects to be configured through configfs using the UVC function.
  26. This allows a significant degree of flexibility, as many of a UVC device's
  27. settings can be controlled this way.
  28. Not all of the available attributes are described here. For a complete enumeration
  29. see Documentation/ABI/testing/configfs-usb-gadget-uvc
  30. Assumptions
  31. ~~~~~~~~~~~
  32. This section assumes that you have mounted configfs at `/sys/kernel/config` and
  33. created a gadget as `/sys/kernel/config/usb_gadget/g1`.
  34. The UVC Function
  35. ~~~~~~~~~~~~~~~~
  36. The first step is to create the UVC function:
  37. .. code-block:: bash
  38. # These variables will be assumed throughout the rest of the document
  39. CONFIGFS="/sys/kernel/config"
  40. GADGET="$CONFIGFS/usb_gadget/g1"
  41. FUNCTION="$GADGET/functions/uvc.0"
  42. mkdir -p $FUNCTION
  43. Formats and Frames
  44. ~~~~~~~~~~~~~~~~~~
  45. You must configure the gadget by telling it which formats you support, as well
  46. as the frame sizes and frame intervals that are supported for each format. In
  47. the current implementation there is no way for the gadget to refuse to set a
  48. format that the host instructs it to set, so it is important that this step is
  49. completed *accurately* to ensure that the host never asks for a format that
  50. can't be provided.
  51. Formats are created under the streaming/uncompressed and streaming/mjpeg configfs
  52. groups, with the framesizes created under the formats in the following
  53. structure:
  54. ::
  55. uvc.0 +
  56. |
  57. + streaming +
  58. |
  59. + mjpeg +
  60. | |
  61. | + mjpeg +
  62. | |
  63. | + 720p
  64. | |
  65. | + 1080p
  66. |
  67. + uncompressed +
  68. |
  69. + yuyv +
  70. |
  71. + 720p
  72. |
  73. + 1080p
  74. Each frame can then be configured with a width and height, plus the maximum
  75. buffer size required to store a single frame, and finally with the supported
  76. frame intervals for that format and framesize. Width and height are enumerated in
  77. units of pixels, frame interval in units of 100ns. To create the structure
  78. above with 2, 15 and 100 fps frameintervals for each framesize for example you
  79. might do:
  80. .. code-block:: bash
  81. create_frame() {
  82. # Example usage:
  83. # create_frame <width> <height> <group> <format name>
  84. WIDTH=$1
  85. HEIGHT=$2
  86. FORMAT=$3
  87. NAME=$4
  88. wdir=$FUNCTION/streaming/$FORMAT/$NAME/${HEIGHT}p
  89. mkdir -p $wdir
  90. echo $WIDTH > $wdir/wWidth
  91. echo $HEIGHT > $wdir/wHeight
  92. echo $(( $WIDTH * $HEIGHT * 2 )) > $wdir/dwMaxVideoFrameBufferSize
  93. cat <<EOF > $wdir/dwFrameInterval
  94. 666666
  95. 100000
  96. 5000000
  97. EOF
  98. }
  99. create_frame 1280 720 mjpeg mjpeg
  100. create_frame 1920 1080 mjpeg mjpeg
  101. create_frame 1280 720 uncompressed yuyv
  102. create_frame 1920 1080 uncompressed yuyv
  103. The only uncompressed format currently supported is YUYV, which is detailed at
  104. Documentation/userspace-api/media/v4l/pixfmt-packed-yuv.rst.
  105. Color Matching Descriptors
  106. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. It's possible to specify some colometry information for each format you create.
  108. This step is optional, and default information will be included if this step is
  109. skipped; those default values follow those defined in the Color Matching Descriptor
  110. section of the UVC specification.
  111. To create a Color Matching Descriptor, create a configfs item and set its three
  112. attributes to your desired settings and then link to it from the format you wish
  113. it to be associated with:
  114. .. code-block:: bash
  115. # Create a new Color Matching Descriptor
  116. mkdir $FUNCTION/streaming/color_matching/yuyv
  117. pushd $FUNCTION/streaming/color_matching/yuyv
  118. echo 1 > bColorPrimaries
  119. echo 1 > bTransferCharacteristics
  120. echo 4 > bMatrixCoefficients
  121. popd
  122. # Create a symlink to the Color Matching Descriptor from the format's config item
  123. ln -s $FUNCTION/streaming/color_matching/yuyv $FUNCTION/streaming/uncompressed/yuyv
  124. For details about the valid values, consult the UVC specification. Note that a
  125. default color matching descriptor exists and is used by any format which does
  126. not have a link to a different Color Matching Descriptor. It's possible to
  127. change the attribute settings for the default descriptor, so bear in mind that if
  128. you do that you are altering the defaults for any format that does not link to
  129. a different one.
  130. Header linking
  131. ~~~~~~~~~~~~~~
  132. The UVC specification requires that Format and Frame descriptors be preceded by
  133. Headers detailing things such as the number and cumulative size of the different
  134. Format descriptors that follow. This and similar operations are achieved in
  135. configfs by linking between the configfs item representing the header and the
  136. config items representing those other descriptors, in this manner:
  137. .. code-block:: bash
  138. mkdir $FUNCTION/streaming/header/h
  139. # This section links the format descriptors and their associated frames
  140. # to the header
  141. cd $FUNCTION/streaming/header/h
  142. ln -s ../../uncompressed/yuyv
  143. ln -s ../../mjpeg/mjpeg
  144. # This section ensures that the header will be transmitted for each
  145. # speed's set of descriptors. If support for a particular speed is not
  146. # needed then it can be skipped here.
  147. cd ../../class/fs
  148. ln -s ../../header/h
  149. cd ../../class/hs
  150. ln -s ../../header/h
  151. cd ../../class/ss
  152. ln -s ../../header/h
  153. cd ../../../control
  154. mkdir header/h
  155. ln -s header/h class/fs
  156. ln -s header/h class/ss
  157. Extension Unit Support
  158. ~~~~~~~~~~~~~~~~~~~~~~
  159. A UVC Extension Unit (XU) basically provides a distinct unit to which control set
  160. and get requests can be addressed. The meaning of those control requests is
  161. entirely implementation dependent, but may be used to control settings outside
  162. of the UVC specification (for example enabling or disabling video effects). An
  163. XU can be inserted into the UVC unit chain or left free-hanging.
  164. Configuring an extension unit involves creating an entry in the appropriate
  165. directory and setting its attributes appropriately, like so:
  166. .. code-block:: bash
  167. mkdir $FUNCTION/control/extensions/xu.0
  168. pushd $FUNCTION/control/extensions/xu.0
  169. # Set the bUnitID of the Processing Unit as the source for this
  170. # Extension Unit
  171. echo 2 > baSourceID
  172. # Set this XU as the source of the default output terminal. This inserts
  173. # the XU into the UVC chain between the PU and OT such that the final
  174. # chain is IT > PU > XU.0 > OT
  175. cat bUnitID > ../../terminal/output/default/baSourceID
  176. # Flag some controls as being available for use. The bmControl field is
  177. # a bitmap with each bit denoting the availability of a particular
  178. # control. For example to flag the 0th, 2nd and 3rd controls available:
  179. echo 0x0d > bmControls
  180. # Set the GUID; this is a vendor-specific code identifying the XU.
  181. echo -e -n "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10" > guidExtensionCode
  182. popd
  183. The bmControls attribute and the baSourceID attribute are multi-value attributes.
  184. This means that you may write multiple newline separated values to them. For
  185. example to flag the 1st, 2nd, 9th and 10th controls as being available you would
  186. need to write two values to bmControls, like so:
  187. .. code-block:: bash
  188. cat << EOF > bmControls
  189. 0x03
  190. 0x03
  191. EOF
  192. The multi-value nature of the baSourceID attribute belies the fact that XUs can
  193. be multiple-input, though note that this currently has no significant effect.
  194. The bControlSize attribute reflects the size of the bmControls attribute, and
  195. similarly bNrInPins reflects the size of the baSourceID attributes. Both
  196. attributes are automatically increased / decreased as you set bmControls and
  197. baSourceID. It is also possible to manually increase or decrease bControlSize
  198. which has the effect of truncating entries to the new size, or padding entries
  199. out with 0x00, for example:
  200. ::
  201. $ cat bmControls
  202. 0x03
  203. 0x05
  204. $ cat bControlSize
  205. 2
  206. $ echo 1 > bControlSize
  207. $ cat bmControls
  208. 0x03
  209. $ echo 2 > bControlSize
  210. $ cat bmControls
  211. 0x03
  212. 0x00
  213. bNrInPins and baSourceID function in the same way.
  214. Configuring Supported Controls for Camera Terminal and Processing Unit
  215. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  216. The Camera Terminal and Processing Units in the UVC chain also have bmControls
  217. attributes which function similarly to the same field in an Extension Unit.
  218. Unlike XUs however, the meaning of the bitflag for these units is defined in
  219. the UVC specification; you should consult the "Camera Terminal Descriptor" and
  220. "Processing Unit Descriptor" sections for an enumeration of the flags.
  221. .. code-block:: bash
  222. # Set the Processing Unit's bmControls, flagging Brightness, Contrast
  223. # and Hue as available controls:
  224. echo 0x05 > $FUNCTION/control/processing/default/bmControls
  225. # Set the Camera Terminal's bmControls, flagging Focus Absolute and
  226. # Focus Relative as available controls:
  227. echo 0x60 > $FUNCTION/control/terminal/camera/default/bmControls
  228. If you do not set these fields then by default the Auto-Exposure Mode control
  229. for the Camera Terminal and the Brightness control for the Processing Unit will
  230. be flagged as available; if they are not supported you should set the field to
  231. 0x00.
  232. Note that the size of the bmControls field for a Camera Terminal or Processing
  233. Unit is fixed by the UVC specification, and so the bControlSize attribute is
  234. read-only here.
  235. Custom Strings Support
  236. ~~~~~~~~~~~~~~~~~~~~~~
  237. String descriptors that provide a textual description for various parts of a
  238. USB device can be defined in the usual place within USB configfs, and may then
  239. be linked to from the UVC function root or from Extension Unit directories to
  240. assign those strings as descriptors:
  241. .. code-block:: bash
  242. # Create a string descriptor in us-EN and link to it from the function
  243. # root. The name of the link is significant here, as it declares this
  244. # descriptor to be intended for the Interface Association Descriptor.
  245. # Other significant link names at function root are vs0_desc and vs1_desc
  246. # For the VideoStreaming Interface 0/1 Descriptors.
  247. mkdir -p $GADGET/strings/0x409/iad_desc
  248. echo -n "Interface Associaton Descriptor" > $GADGET/strings/0x409/iad_desc/s
  249. ln -s $GADGET/strings/0x409/iad_desc $FUNCTION/iad_desc
  250. # Because the link to a String Descriptor from an Extension Unit clearly
  251. # associates the two, the name of this link is not significant and may
  252. # be set freely.
  253. mkdir -p $GADGET/strings/0x409/xu.0
  254. echo -n "A Very Useful Extension Unit" > $GADGET/strings/0x409/xu.0/s
  255. ln -s $GADGET/strings/0x409/xu.0 $FUNCTION/control/extensions/xu.0
  256. The interrupt endpoint
  257. ~~~~~~~~~~~~~~~~~~~~~~
  258. The VideoControl interface has an optional interrupt endpoint which is by default
  259. disabled. This is intended to support delayed response control set requests for
  260. UVC (which should respond through the interrupt endpoint rather than tying up
  261. endpoint 0). At present support for sending data through this endpoint is missing
  262. and so it is left disabled to avoid confusion. If you wish to enable it you can
  263. do so through the configfs attribute:
  264. .. code-block:: bash
  265. echo 1 > $FUNCTION/control/enable_interrupt_ep
  266. Bandwidth configuration
  267. ~~~~~~~~~~~~~~~~~~~~~~~
  268. There are three attributes which control the bandwidth of the USB connection.
  269. These live in the function root and can be set within limits:
  270. .. code-block:: bash
  271. # streaming_interval sets bInterval. Values range from 1..255
  272. echo 1 > $FUNCTION/streaming_interval
  273. # streaming_maxpacket sets wMaxPacketSize. Valid values are 1024/2048/3072
  274. echo 3072 > $FUNCTION/streaming_maxpacket
  275. # streaming_maxburst sets bMaxBurst. Valid values are 1..15
  276. echo 1 > $FUNCTION/streaming_maxburst
  277. The values passed here will be clamped to valid values according to the UVC
  278. specification (which depend on the speed of the USB connection). To understand
  279. how the settings influence bandwidth you should consult the UVC specifications,
  280. but a rule of thumb is that increasing the streaming_maxpacket setting will
  281. improve bandwidth (and thus the maximum possible framerate), whilst the same is
  282. true for streaming_maxburst provided the USB connection is running at SuperSpeed.
  283. Increasing streaming_interval will reduce bandwidth and framerate.
  284. The userspace application
  285. -------------------------
  286. By itself, the UVC Gadget driver cannot do anything particularly interesting. It
  287. must be paired with a userspace program that responds to UVC control requests and
  288. fills buffers to be queued to the V4L2 device that the driver creates. How those
  289. things are achieved is implementation dependent and beyond the scope of this
  290. document, but a reference application can be found at https://gitlab.freedesktop.org/camera/uvc-gadget