18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ciThe Linux USB Video Class (UVC) driver
48c2ecf20Sopenharmony_ci======================================
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ciThis file documents some driver-specific aspects of the UVC driver, such as
78c2ecf20Sopenharmony_cidriver-specific ioctls and implementation notes.
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciQuestions and remarks can be sent to the Linux UVC development mailing list at
108c2ecf20Sopenharmony_cilinux-uvc-devel@lists.berlios.de.
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciExtension Unit (XU) support
148c2ecf20Sopenharmony_ci---------------------------
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciIntroduction
178c2ecf20Sopenharmony_ci~~~~~~~~~~~~
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciThe UVC specification allows for vendor-specific extensions through extension
208c2ecf20Sopenharmony_ciunits (XUs). The Linux UVC driver supports extension unit controls (XU controls)
218c2ecf20Sopenharmony_cithrough two separate mechanisms:
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci  - through mappings of XU controls to V4L2 controls
248c2ecf20Sopenharmony_ci  - through a driver-specific ioctl interface
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciThe first one allows generic V4L2 applications to use XU controls by mapping
278c2ecf20Sopenharmony_cicertain XU controls onto V4L2 controls, which then show up during ordinary
288c2ecf20Sopenharmony_cicontrol enumeration.
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciThe second mechanism requires uvcvideo-specific knowledge for the application to
318c2ecf20Sopenharmony_ciaccess XU controls but exposes the entire UVC XU concept to user space for
328c2ecf20Sopenharmony_cimaximum flexibility.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciBoth mechanisms complement each other and are described in more detail below.
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciControl mappings
388c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciThe UVC driver provides an API for user space applications to define so-called
418c2ecf20Sopenharmony_cicontrol mappings at runtime. These allow for individual XU controls or byte
428c2ecf20Sopenharmony_ciranges thereof to be mapped to new V4L2 controls. Such controls appear and
438c2ecf20Sopenharmony_cifunction exactly like normal V4L2 controls (i.e. the stock controls, such as
448c2ecf20Sopenharmony_cibrightness, contrast, etc.). However, reading or writing of such a V4L2 controls
458c2ecf20Sopenharmony_citriggers a read or write of the associated XU control.
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciThe ioctl used to create these control mappings is called UVCIOC_CTRL_MAP.
488c2ecf20Sopenharmony_ciPrevious driver versions (before 0.2.0) required another ioctl to be used
498c2ecf20Sopenharmony_cibeforehand (UVCIOC_CTRL_ADD) to pass XU control information to the UVC driver.
508c2ecf20Sopenharmony_ciThis is no longer necessary as newer uvcvideo versions query the information
518c2ecf20Sopenharmony_cidirectly from the device.
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciFor details on the UVCIOC_CTRL_MAP ioctl please refer to the section titled
548c2ecf20Sopenharmony_ci"IOCTL reference" below.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci3. Driver specific XU control interface
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciFor applications that need to access XU controls directly, e.g. for testing
608c2ecf20Sopenharmony_cipurposes, firmware upload, or accessing binary controls, a second mechanism to
618c2ecf20Sopenharmony_ciaccess XU controls is provided in the form of a driver-specific ioctl, namely
628c2ecf20Sopenharmony_ciUVCIOC_CTRL_QUERY.
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciA call to this ioctl allows applications to send queries to the UVC driver that
658c2ecf20Sopenharmony_cidirectly map to the low-level UVC control requests.
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciIn order to make such a request the UVC unit ID of the control's extension unit
688c2ecf20Sopenharmony_ciand the control selector need to be known. This information either needs to be
698c2ecf20Sopenharmony_cihardcoded in the application or queried using other ways such as by parsing the
708c2ecf20Sopenharmony_ciUVC descriptor or, if available, using the media controller API to enumerate a
718c2ecf20Sopenharmony_cidevice's entities.
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciUnless the control size is already known it is necessary to first make a
748c2ecf20Sopenharmony_ciUVC_GET_LEN requests in order to be able to allocate a sufficiently large buffer
758c2ecf20Sopenharmony_ciand set the buffer size to the correct value. Similarly, to find out whether
768c2ecf20Sopenharmony_ciUVC_GET_CUR or UVC_SET_CUR are valid requests for a given control, a
778c2ecf20Sopenharmony_ciUVC_GET_INFO request should be made. The bits 0 (GET supported) and 1 (SET
788c2ecf20Sopenharmony_cisupported) of the resulting byte indicate which requests are valid.
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciWith the addition of the UVCIOC_CTRL_QUERY ioctl the UVCIOC_CTRL_GET and
818c2ecf20Sopenharmony_ciUVCIOC_CTRL_SET ioctls have become obsolete since their functionality is a
828c2ecf20Sopenharmony_cisubset of the former ioctl. For the time being they are still supported but
838c2ecf20Sopenharmony_ciapplication developers are encouraged to use UVCIOC_CTRL_QUERY instead.
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciFor details on the UVCIOC_CTRL_QUERY ioctl please refer to the section titled
868c2ecf20Sopenharmony_ci"IOCTL reference" below.
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciSecurity
908c2ecf20Sopenharmony_ci~~~~~~~~
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciThe API doesn't currently provide a fine-grained access control facility. The
938c2ecf20Sopenharmony_ciUVCIOC_CTRL_ADD and UVCIOC_CTRL_MAP ioctls require super user permissions.
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciSuggestions on how to improve this are welcome.
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciDebugging
998c2ecf20Sopenharmony_ci~~~~~~~~~
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciIn order to debug problems related to XU controls or controls in general it is
1028c2ecf20Sopenharmony_cirecommended to enable the UVC_TRACE_CONTROL bit in the module parameter 'trace'.
1038c2ecf20Sopenharmony_ciThis causes extra output to be written into the system log.
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciIOCTL reference
1078c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciUVCIOC_CTRL_MAP - Map a UVC control to a V4L2 control
1108c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciArgument: struct uvc_xu_control_mapping
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci**Description**:
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	This ioctl creates a mapping between a UVC control or part of a UVC
1178c2ecf20Sopenharmony_ci	control and a V4L2 control. Once mappings are defined, userspace
1188c2ecf20Sopenharmony_ci	applications can access vendor-defined UVC control through the V4L2
1198c2ecf20Sopenharmony_ci	control API.
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	To create a mapping, applications fill the uvc_xu_control_mapping
1228c2ecf20Sopenharmony_ci	structure with information about an existing UVC control defined with
1238c2ecf20Sopenharmony_ci	UVCIOC_CTRL_ADD and a new V4L2 control.
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	A UVC control can be mapped to several V4L2 controls. For instance,
1268c2ecf20Sopenharmony_ci	a UVC pan/tilt control could be mapped to separate pan and tilt V4L2
1278c2ecf20Sopenharmony_ci	controls. The UVC control is divided into non overlapping fields using
1288c2ecf20Sopenharmony_ci	the 'size' and 'offset' fields and are then independently mapped to
1298c2ecf20Sopenharmony_ci	V4L2 control.
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	For signed integer V4L2 controls the data_type field should be set to
1328c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_SIGNED. Other values are currently ignored.
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci**Return value**:
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	On success 0 is returned. On error -1 is returned and errno is set
1378c2ecf20Sopenharmony_ci	appropriately.
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ENOMEM
1408c2ecf20Sopenharmony_ci		Not enough memory to perform the operation.
1418c2ecf20Sopenharmony_ci	EPERM
1428c2ecf20Sopenharmony_ci		Insufficient privileges (super user privileges are required).
1438c2ecf20Sopenharmony_ci	EINVAL
1448c2ecf20Sopenharmony_ci		No such UVC control.
1458c2ecf20Sopenharmony_ci	EOVERFLOW
1468c2ecf20Sopenharmony_ci		The requested offset and size would overflow the UVC control.
1478c2ecf20Sopenharmony_ci	EEXIST
1488c2ecf20Sopenharmony_ci		Mapping already exists.
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci**Data types**:
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci.. code-block:: none
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	* struct uvc_xu_control_mapping
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	__u32	id		V4L2 control identifier
1578c2ecf20Sopenharmony_ci	__u8	name[32]	V4L2 control name
1588c2ecf20Sopenharmony_ci	__u8	entity[16]	UVC extension unit GUID
1598c2ecf20Sopenharmony_ci	__u8	selector	UVC control selector
1608c2ecf20Sopenharmony_ci	__u8	size		V4L2 control size (in bits)
1618c2ecf20Sopenharmony_ci	__u8	offset		V4L2 control offset (in bits)
1628c2ecf20Sopenharmony_ci	enum v4l2_ctrl_type
1638c2ecf20Sopenharmony_ci		v4l2_type	V4L2 control type
1648c2ecf20Sopenharmony_ci	enum uvc_control_data_type
1658c2ecf20Sopenharmony_ci		data_type	UVC control data type
1668c2ecf20Sopenharmony_ci	struct uvc_menu_info
1678c2ecf20Sopenharmony_ci		*menu_info	Array of menu entries (for menu controls only)
1688c2ecf20Sopenharmony_ci	__u32	menu_count	Number of menu entries (for menu controls only)
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	* struct uvc_menu_info
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	__u32	value		Menu entry value used by the device
1738c2ecf20Sopenharmony_ci	__u8	name[32]	Menu entry name
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	* enum uvc_control_data_type
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_RAW		Raw control (byte array)
1798c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_SIGNED	Signed integer
1808c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_UNSIGNED	Unsigned integer
1818c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_BOOLEAN	Boolean
1828c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_ENUM		Enumeration
1838c2ecf20Sopenharmony_ci	UVC_CTRL_DATA_TYPE_BITMASK	Bitmask
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ciUVCIOC_CTRL_QUERY - Query a UVC XU control
1878c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1888c2ecf20Sopenharmony_ciArgument: struct uvc_xu_control_query
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci**Description**:
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	This ioctl queries a UVC XU control identified by its extension unit ID
1938c2ecf20Sopenharmony_ci	and control selector.
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	There are a number of different queries available that closely
1968c2ecf20Sopenharmony_ci	correspond to the low-level control requests described in the UVC
1978c2ecf20Sopenharmony_ci	specification. These requests are:
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	UVC_GET_CUR
2008c2ecf20Sopenharmony_ci		Obtain the current value of the control.
2018c2ecf20Sopenharmony_ci	UVC_GET_MIN
2028c2ecf20Sopenharmony_ci		Obtain the minimum value of the control.
2038c2ecf20Sopenharmony_ci	UVC_GET_MAX
2048c2ecf20Sopenharmony_ci		Obtain the maximum value of the control.
2058c2ecf20Sopenharmony_ci	UVC_GET_DEF
2068c2ecf20Sopenharmony_ci		Obtain the default value of the control.
2078c2ecf20Sopenharmony_ci	UVC_GET_RES
2088c2ecf20Sopenharmony_ci		Query the resolution of the control, i.e. the step size of the
2098c2ecf20Sopenharmony_ci		allowed control values.
2108c2ecf20Sopenharmony_ci	UVC_GET_LEN
2118c2ecf20Sopenharmony_ci		Query the size of the control in bytes.
2128c2ecf20Sopenharmony_ci	UVC_GET_INFO
2138c2ecf20Sopenharmony_ci		Query the control information bitmap, which indicates whether
2148c2ecf20Sopenharmony_ci		get/set requests are supported.
2158c2ecf20Sopenharmony_ci	UVC_SET_CUR
2168c2ecf20Sopenharmony_ci		Update the value of the control.
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	Applications must set the 'size' field to the correct length for the
2198c2ecf20Sopenharmony_ci	control. Exceptions are the UVC_GET_LEN and UVC_GET_INFO queries, for
2208c2ecf20Sopenharmony_ci	which the size must be set to 2 and 1, respectively. The 'data' field
2218c2ecf20Sopenharmony_ci	must point to a valid writable buffer big enough to hold the indicated
2228c2ecf20Sopenharmony_ci	number of data bytes.
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	Data is copied directly from the device without any driver-side
2258c2ecf20Sopenharmony_ci	processing. Applications are responsible for data buffer formatting,
2268c2ecf20Sopenharmony_ci	including little-endian/big-endian conversion. This is particularly
2278c2ecf20Sopenharmony_ci	important for the result of the UVC_GET_LEN requests, which is always
2288c2ecf20Sopenharmony_ci	returned as a little-endian 16-bit integer by the device.
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci**Return value**:
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	On success 0 is returned. On error -1 is returned and errno is set
2338c2ecf20Sopenharmony_ci	appropriately.
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	ENOENT
2368c2ecf20Sopenharmony_ci		The device does not support the given control or the specified
2378c2ecf20Sopenharmony_ci		extension unit could not be found.
2388c2ecf20Sopenharmony_ci	ENOBUFS
2398c2ecf20Sopenharmony_ci		The specified buffer size is incorrect (too big or too small).
2408c2ecf20Sopenharmony_ci	EINVAL
2418c2ecf20Sopenharmony_ci		An invalid request code was passed.
2428c2ecf20Sopenharmony_ci	EBADRQC
2438c2ecf20Sopenharmony_ci		The given request is not supported by the given control.
2448c2ecf20Sopenharmony_ci	EFAULT
2458c2ecf20Sopenharmony_ci		The data pointer references an inaccessible memory area.
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci**Data types**:
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci.. code-block:: none
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	* struct uvc_xu_control_query
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	__u8	unit		Extension unit ID
2548c2ecf20Sopenharmony_ci	__u8	selector	Control selector
2558c2ecf20Sopenharmony_ci	__u8	query		Request code to send to the device
2568c2ecf20Sopenharmony_ci	__u16	size		Control data size (in bytes)
2578c2ecf20Sopenharmony_ci	__u8	*data		Control value
258