uvcvideo.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. .. SPDX-License-Identifier: GPL-2.0
  2. The Linux USB Video Class (UVC) driver
  3. ======================================
  4. This file documents some driver-specific aspects of the UVC driver, such as
  5. driver-specific ioctls and implementation notes.
  6. Questions and remarks can be sent to the Linux UVC development mailing list at
  7. linux-media@vger.kernel.org.
  8. Extension Unit (XU) support
  9. ---------------------------
  10. Introduction
  11. ~~~~~~~~~~~~
  12. The UVC specification allows for vendor-specific extensions through extension
  13. units (XUs). The Linux UVC driver supports extension unit controls (XU controls)
  14. through two separate mechanisms:
  15. - through mappings of XU controls to V4L2 controls
  16. - through a driver-specific ioctl interface
  17. The first one allows generic V4L2 applications to use XU controls by mapping
  18. certain XU controls onto V4L2 controls, which then show up during ordinary
  19. control enumeration.
  20. The second mechanism requires uvcvideo-specific knowledge for the application to
  21. access XU controls but exposes the entire UVC XU concept to user space for
  22. maximum flexibility.
  23. Both mechanisms complement each other and are described in more detail below.
  24. Control mappings
  25. ~~~~~~~~~~~~~~~~
  26. The UVC driver provides an API for user space applications to define so-called
  27. control mappings at runtime. These allow for individual XU controls or byte
  28. ranges thereof to be mapped to new V4L2 controls. Such controls appear and
  29. function exactly like normal V4L2 controls (i.e. the stock controls, such as
  30. brightness, contrast, etc.). However, reading or writing of such a V4L2 controls
  31. triggers a read or write of the associated XU control.
  32. The ioctl used to create these control mappings is called UVCIOC_CTRL_MAP.
  33. Previous driver versions (before 0.2.0) required another ioctl to be used
  34. beforehand (UVCIOC_CTRL_ADD) to pass XU control information to the UVC driver.
  35. This is no longer necessary as newer uvcvideo versions query the information
  36. directly from the device.
  37. For details on the UVCIOC_CTRL_MAP ioctl please refer to the section titled
  38. "IOCTL reference" below.
  39. 3. Driver specific XU control interface
  40. For applications that need to access XU controls directly, e.g. for testing
  41. purposes, firmware upload, or accessing binary controls, a second mechanism to
  42. access XU controls is provided in the form of a driver-specific ioctl, namely
  43. UVCIOC_CTRL_QUERY.
  44. A call to this ioctl allows applications to send queries to the UVC driver that
  45. directly map to the low-level UVC control requests.
  46. In order to make such a request the UVC unit ID of the control's extension unit
  47. and the control selector need to be known. This information either needs to be
  48. hardcoded in the application or queried using other ways such as by parsing the
  49. UVC descriptor or, if available, using the media controller API to enumerate a
  50. device's entities.
  51. Unless the control size is already known it is necessary to first make a
  52. UVC_GET_LEN requests in order to be able to allocate a sufficiently large buffer
  53. and set the buffer size to the correct value. Similarly, to find out whether
  54. UVC_GET_CUR or UVC_SET_CUR are valid requests for a given control, a
  55. UVC_GET_INFO request should be made. The bits 0 (GET supported) and 1 (SET
  56. supported) of the resulting byte indicate which requests are valid.
  57. With the addition of the UVCIOC_CTRL_QUERY ioctl the UVCIOC_CTRL_GET and
  58. UVCIOC_CTRL_SET ioctls have become obsolete since their functionality is a
  59. subset of the former ioctl. For the time being they are still supported but
  60. application developers are encouraged to use UVCIOC_CTRL_QUERY instead.
  61. For details on the UVCIOC_CTRL_QUERY ioctl please refer to the section titled
  62. "IOCTL reference" below.
  63. Security
  64. ~~~~~~~~
  65. The API doesn't currently provide a fine-grained access control facility. The
  66. UVCIOC_CTRL_ADD and UVCIOC_CTRL_MAP ioctls require super user permissions.
  67. Suggestions on how to improve this are welcome.
  68. Debugging
  69. ~~~~~~~~~
  70. In order to debug problems related to XU controls or controls in general it is
  71. recommended to enable the UVC_TRACE_CONTROL bit in the module parameter 'trace'.
  72. This causes extra output to be written into the system log.
  73. IOCTL reference
  74. ~~~~~~~~~~~~~~~
  75. UVCIOC_CTRL_MAP - Map a UVC control to a V4L2 control
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. Argument: struct uvc_xu_control_mapping
  78. **Description**:
  79. This ioctl creates a mapping between a UVC control or part of a UVC
  80. control and a V4L2 control. Once mappings are defined, userspace
  81. applications can access vendor-defined UVC control through the V4L2
  82. control API.
  83. To create a mapping, applications fill the uvc_xu_control_mapping
  84. structure with information about an existing UVC control defined with
  85. UVCIOC_CTRL_ADD and a new V4L2 control.
  86. A UVC control can be mapped to several V4L2 controls. For instance,
  87. a UVC pan/tilt control could be mapped to separate pan and tilt V4L2
  88. controls. The UVC control is divided into non overlapping fields using
  89. the 'size' and 'offset' fields and are then independently mapped to
  90. V4L2 control.
  91. For signed integer V4L2 controls the data_type field should be set to
  92. UVC_CTRL_DATA_TYPE_SIGNED. Other values are currently ignored.
  93. **Return value**:
  94. On success 0 is returned. On error -1 is returned and errno is set
  95. appropriately.
  96. ENOMEM
  97. Not enough memory to perform the operation.
  98. EPERM
  99. Insufficient privileges (super user privileges are required).
  100. EINVAL
  101. No such UVC control.
  102. EOVERFLOW
  103. The requested offset and size would overflow the UVC control.
  104. EEXIST
  105. Mapping already exists.
  106. **Data types**:
  107. .. code-block:: none
  108. * struct uvc_xu_control_mapping
  109. __u32 id V4L2 control identifier
  110. __u8 name[32] V4L2 control name
  111. __u8 entity[16] UVC extension unit GUID
  112. __u8 selector UVC control selector
  113. __u8 size V4L2 control size (in bits)
  114. __u8 offset V4L2 control offset (in bits)
  115. enum v4l2_ctrl_type
  116. v4l2_type V4L2 control type
  117. enum uvc_control_data_type
  118. data_type UVC control data type
  119. struct uvc_menu_info
  120. *menu_info Array of menu entries (for menu controls only)
  121. __u32 menu_count Number of menu entries (for menu controls only)
  122. * struct uvc_menu_info
  123. __u32 value Menu entry value used by the device
  124. __u8 name[32] Menu entry name
  125. * enum uvc_control_data_type
  126. UVC_CTRL_DATA_TYPE_RAW Raw control (byte array)
  127. UVC_CTRL_DATA_TYPE_SIGNED Signed integer
  128. UVC_CTRL_DATA_TYPE_UNSIGNED Unsigned integer
  129. UVC_CTRL_DATA_TYPE_BOOLEAN Boolean
  130. UVC_CTRL_DATA_TYPE_ENUM Enumeration
  131. UVC_CTRL_DATA_TYPE_BITMASK Bitmask
  132. UVC_CTRL_DATA_TYPE_RECT Rectangular area
  133. UVCIOC_CTRL_QUERY - Query a UVC XU control
  134. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  135. Argument: struct uvc_xu_control_query
  136. **Description**:
  137. This ioctl queries a UVC XU control identified by its extension unit ID
  138. and control selector.
  139. There are a number of different queries available that closely
  140. correspond to the low-level control requests described in the UVC
  141. specification. These requests are:
  142. UVC_GET_CUR
  143. Obtain the current value of the control.
  144. UVC_GET_MIN
  145. Obtain the minimum value of the control.
  146. UVC_GET_MAX
  147. Obtain the maximum value of the control.
  148. UVC_GET_DEF
  149. Obtain the default value of the control.
  150. UVC_GET_RES
  151. Query the resolution of the control, i.e. the step size of the
  152. allowed control values.
  153. UVC_GET_LEN
  154. Query the size of the control in bytes.
  155. UVC_GET_INFO
  156. Query the control information bitmap, which indicates whether
  157. get/set requests are supported.
  158. UVC_SET_CUR
  159. Update the value of the control.
  160. Applications must set the 'size' field to the correct length for the
  161. control. Exceptions are the UVC_GET_LEN and UVC_GET_INFO queries, for
  162. which the size must be set to 2 and 1, respectively. The 'data' field
  163. must point to a valid writable buffer big enough to hold the indicated
  164. number of data bytes.
  165. Data is copied directly from the device without any driver-side
  166. processing. Applications are responsible for data buffer formatting,
  167. including little-endian/big-endian conversion. This is particularly
  168. important for the result of the UVC_GET_LEN requests, which is always
  169. returned as a little-endian 16-bit integer by the device.
  170. **Return value**:
  171. On success 0 is returned. On error -1 is returned and errno is set
  172. appropriately.
  173. ENOENT
  174. The device does not support the given control or the specified
  175. extension unit could not be found.
  176. ENOBUFS
  177. The specified buffer size is incorrect (too big or too small).
  178. EINVAL
  179. An invalid request code was passed.
  180. EBADRQC
  181. The given request is not supported by the given control.
  182. EFAULT
  183. The data pointer references an inaccessible memory area.
  184. **Data types**:
  185. .. code-block:: none
  186. * struct uvc_xu_control_query
  187. __u8 unit Extension unit ID
  188. __u8 selector Control selector
  189. __u8 query Request code to send to the device
  190. __u16 size Control data size (in bytes)
  191. __u8 *data Control value
  192. Driver-specific V4L2 controls
  193. -----------------------------
  194. The uvcvideo driver implements the following UVC-specific controls:
  195. ``V4L2_CID_UVC_REGION_OF_INTEREST_RECT (struct)``
  196. This control determines the region of interest (ROI). ROI is a
  197. rectangular area represented by a struct :c:type:`v4l2_rect`. The
  198. rectangle is in global sensor coordinates using pixel units. It is
  199. independent of the field of view, not impacted by any cropping or
  200. scaling.
  201. Use ``V4L2_CTRL_WHICH_MIN_VAL`` and ``V4L2_CTRL_WHICH_MAX_VAL`` to query
  202. the range of rectangle sizes.
  203. Setting a ROI allows the camera to optimize the capture for the region.
  204. The value of ``V4L2_CID_REGION_OF_INTEREST_AUTO`` control determines
  205. the detailed behavior.
  206. An example of use of this control, can be found in the:
  207. `Chrome OS USB camera HAL.
  208. <https://chromium.googlesource.com/chromiumos/platform2/+/refs/heads/release-R121-15699.B/camera/hal/usb/>`
  209. ``V4L2_CID_UVC_REGION_OF_INTEREST_AUTO (bitmask)``
  210. This determines which, if any, on-board features should track to the
  211. Region of Interest specified by the current value of
  212. ``V4L2_CID_UVD__REGION_OF_INTEREST_RECT``.
  213. Max value is a mask indicating all supported Auto Controls.
  214. .. flat-table::
  215. :header-rows: 0
  216. :stub-columns: 0
  217. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_EXPOSURE``
  218. - Setting this bit causes automatic exposure to track the region of
  219. interest instead of the whole image.
  220. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_IRIS``
  221. - Setting this bit causes automatic iris to track the region of interest
  222. instead of the whole image.
  223. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_WHITE_BALANCE``
  224. - Setting this bit causes automatic white balance to track the region
  225. of interest instead of the whole image.
  226. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_FOCUS``
  227. - Setting this bit causes automatic focus adjustment to track the region
  228. of interest instead of the whole image.
  229. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_FACE_DETECT``
  230. - Setting this bit causes automatic face detection to track the region of
  231. interest instead of the whole image.
  232. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_DETECT_AND_TRACK``
  233. - Setting this bit enables automatic face detection and tracking. The
  234. current value of ``V4L2_CID_REGION_OF_INTEREST_RECT`` may be updated by
  235. the driver.
  236. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_IMAGE_STABILIZATION``
  237. - Setting this bit enables automatic image stabilization. The
  238. current value of ``V4L2_CID_REGION_OF_INTEREST_RECT`` may be updated by
  239. the driver.
  240. * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_HIGHER_QUALITY``
  241. - Setting this bit enables automatically capture the specified region
  242. with higher quality if possible.