1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunThe Linux USB Video Class (UVC) driver 4*4882a593Smuzhiyun====================================== 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunThis file documents some driver-specific aspects of the UVC driver, such as 7*4882a593Smuzhiyundriver-specific ioctls and implementation notes. 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunQuestions and remarks can be sent to the Linux UVC development mailing list at 10*4882a593Smuzhiyunlinux-uvc-devel@lists.berlios.de. 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunExtension Unit (XU) support 14*4882a593Smuzhiyun--------------------------- 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunIntroduction 17*4882a593Smuzhiyun~~~~~~~~~~~~ 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunThe UVC specification allows for vendor-specific extensions through extension 20*4882a593Smuzhiyununits (XUs). The Linux UVC driver supports extension unit controls (XU controls) 21*4882a593Smuzhiyunthrough two separate mechanisms: 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun - through mappings of XU controls to V4L2 controls 24*4882a593Smuzhiyun - through a driver-specific ioctl interface 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunThe first one allows generic V4L2 applications to use XU controls by mapping 27*4882a593Smuzhiyuncertain XU controls onto V4L2 controls, which then show up during ordinary 28*4882a593Smuzhiyuncontrol enumeration. 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunThe second mechanism requires uvcvideo-specific knowledge for the application to 31*4882a593Smuzhiyunaccess XU controls but exposes the entire UVC XU concept to user space for 32*4882a593Smuzhiyunmaximum flexibility. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunBoth mechanisms complement each other and are described in more detail below. 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunControl mappings 38*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunThe UVC driver provides an API for user space applications to define so-called 41*4882a593Smuzhiyuncontrol mappings at runtime. These allow for individual XU controls or byte 42*4882a593Smuzhiyunranges thereof to be mapped to new V4L2 controls. Such controls appear and 43*4882a593Smuzhiyunfunction exactly like normal V4L2 controls (i.e. the stock controls, such as 44*4882a593Smuzhiyunbrightness, contrast, etc.). However, reading or writing of such a V4L2 controls 45*4882a593Smuzhiyuntriggers a read or write of the associated XU control. 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunThe ioctl used to create these control mappings is called UVCIOC_CTRL_MAP. 48*4882a593SmuzhiyunPrevious driver versions (before 0.2.0) required another ioctl to be used 49*4882a593Smuzhiyunbeforehand (UVCIOC_CTRL_ADD) to pass XU control information to the UVC driver. 50*4882a593SmuzhiyunThis is no longer necessary as newer uvcvideo versions query the information 51*4882a593Smuzhiyundirectly from the device. 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunFor details on the UVCIOC_CTRL_MAP ioctl please refer to the section titled 54*4882a593Smuzhiyun"IOCTL reference" below. 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun3. Driver specific XU control interface 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunFor applications that need to access XU controls directly, e.g. for testing 60*4882a593Smuzhiyunpurposes, firmware upload, or accessing binary controls, a second mechanism to 61*4882a593Smuzhiyunaccess XU controls is provided in the form of a driver-specific ioctl, namely 62*4882a593SmuzhiyunUVCIOC_CTRL_QUERY. 63*4882a593Smuzhiyun 64*4882a593SmuzhiyunA call to this ioctl allows applications to send queries to the UVC driver that 65*4882a593Smuzhiyundirectly map to the low-level UVC control requests. 66*4882a593Smuzhiyun 67*4882a593SmuzhiyunIn order to make such a request the UVC unit ID of the control's extension unit 68*4882a593Smuzhiyunand the control selector need to be known. This information either needs to be 69*4882a593Smuzhiyunhardcoded in the application or queried using other ways such as by parsing the 70*4882a593SmuzhiyunUVC descriptor or, if available, using the media controller API to enumerate a 71*4882a593Smuzhiyundevice's entities. 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunUnless the control size is already known it is necessary to first make a 74*4882a593SmuzhiyunUVC_GET_LEN requests in order to be able to allocate a sufficiently large buffer 75*4882a593Smuzhiyunand set the buffer size to the correct value. Similarly, to find out whether 76*4882a593SmuzhiyunUVC_GET_CUR or UVC_SET_CUR are valid requests for a given control, a 77*4882a593SmuzhiyunUVC_GET_INFO request should be made. The bits 0 (GET supported) and 1 (SET 78*4882a593Smuzhiyunsupported) of the resulting byte indicate which requests are valid. 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunWith the addition of the UVCIOC_CTRL_QUERY ioctl the UVCIOC_CTRL_GET and 81*4882a593SmuzhiyunUVCIOC_CTRL_SET ioctls have become obsolete since their functionality is a 82*4882a593Smuzhiyunsubset of the former ioctl. For the time being they are still supported but 83*4882a593Smuzhiyunapplication developers are encouraged to use UVCIOC_CTRL_QUERY instead. 84*4882a593Smuzhiyun 85*4882a593SmuzhiyunFor details on the UVCIOC_CTRL_QUERY ioctl please refer to the section titled 86*4882a593Smuzhiyun"IOCTL reference" below. 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunSecurity 90*4882a593Smuzhiyun~~~~~~~~ 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunThe API doesn't currently provide a fine-grained access control facility. The 93*4882a593SmuzhiyunUVCIOC_CTRL_ADD and UVCIOC_CTRL_MAP ioctls require super user permissions. 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunSuggestions on how to improve this are welcome. 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunDebugging 99*4882a593Smuzhiyun~~~~~~~~~ 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunIn order to debug problems related to XU controls or controls in general it is 102*4882a593Smuzhiyunrecommended to enable the UVC_TRACE_CONTROL bit in the module parameter 'trace'. 103*4882a593SmuzhiyunThis causes extra output to be written into the system log. 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunIOCTL reference 107*4882a593Smuzhiyun~~~~~~~~~~~~~~~ 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunUVCIOC_CTRL_MAP - Map a UVC control to a V4L2 control 110*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunArgument: struct uvc_xu_control_mapping 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun**Description**: 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun This ioctl creates a mapping between a UVC control or part of a UVC 117*4882a593Smuzhiyun control and a V4L2 control. Once mappings are defined, userspace 118*4882a593Smuzhiyun applications can access vendor-defined UVC control through the V4L2 119*4882a593Smuzhiyun control API. 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun To create a mapping, applications fill the uvc_xu_control_mapping 122*4882a593Smuzhiyun structure with information about an existing UVC control defined with 123*4882a593Smuzhiyun UVCIOC_CTRL_ADD and a new V4L2 control. 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun A UVC control can be mapped to several V4L2 controls. For instance, 126*4882a593Smuzhiyun a UVC pan/tilt control could be mapped to separate pan and tilt V4L2 127*4882a593Smuzhiyun controls. The UVC control is divided into non overlapping fields using 128*4882a593Smuzhiyun the 'size' and 'offset' fields and are then independently mapped to 129*4882a593Smuzhiyun V4L2 control. 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun For signed integer V4L2 controls the data_type field should be set to 132*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_SIGNED. Other values are currently ignored. 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun**Return value**: 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun On success 0 is returned. On error -1 is returned and errno is set 137*4882a593Smuzhiyun appropriately. 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun ENOMEM 140*4882a593Smuzhiyun Not enough memory to perform the operation. 141*4882a593Smuzhiyun EPERM 142*4882a593Smuzhiyun Insufficient privileges (super user privileges are required). 143*4882a593Smuzhiyun EINVAL 144*4882a593Smuzhiyun No such UVC control. 145*4882a593Smuzhiyun EOVERFLOW 146*4882a593Smuzhiyun The requested offset and size would overflow the UVC control. 147*4882a593Smuzhiyun EEXIST 148*4882a593Smuzhiyun Mapping already exists. 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun**Data types**: 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun.. code-block:: none 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun * struct uvc_xu_control_mapping 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun __u32 id V4L2 control identifier 157*4882a593Smuzhiyun __u8 name[32] V4L2 control name 158*4882a593Smuzhiyun __u8 entity[16] UVC extension unit GUID 159*4882a593Smuzhiyun __u8 selector UVC control selector 160*4882a593Smuzhiyun __u8 size V4L2 control size (in bits) 161*4882a593Smuzhiyun __u8 offset V4L2 control offset (in bits) 162*4882a593Smuzhiyun enum v4l2_ctrl_type 163*4882a593Smuzhiyun v4l2_type V4L2 control type 164*4882a593Smuzhiyun enum uvc_control_data_type 165*4882a593Smuzhiyun data_type UVC control data type 166*4882a593Smuzhiyun struct uvc_menu_info 167*4882a593Smuzhiyun *menu_info Array of menu entries (for menu controls only) 168*4882a593Smuzhiyun __u32 menu_count Number of menu entries (for menu controls only) 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun * struct uvc_menu_info 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun __u32 value Menu entry value used by the device 173*4882a593Smuzhiyun __u8 name[32] Menu entry name 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun * enum uvc_control_data_type 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_RAW Raw control (byte array) 179*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_SIGNED Signed integer 180*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_UNSIGNED Unsigned integer 181*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_BOOLEAN Boolean 182*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_ENUM Enumeration 183*4882a593Smuzhiyun UVC_CTRL_DATA_TYPE_BITMASK Bitmask 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunUVCIOC_CTRL_QUERY - Query a UVC XU control 187*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 188*4882a593SmuzhiyunArgument: struct uvc_xu_control_query 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun**Description**: 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun This ioctl queries a UVC XU control identified by its extension unit ID 193*4882a593Smuzhiyun and control selector. 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun There are a number of different queries available that closely 196*4882a593Smuzhiyun correspond to the low-level control requests described in the UVC 197*4882a593Smuzhiyun specification. These requests are: 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun UVC_GET_CUR 200*4882a593Smuzhiyun Obtain the current value of the control. 201*4882a593Smuzhiyun UVC_GET_MIN 202*4882a593Smuzhiyun Obtain the minimum value of the control. 203*4882a593Smuzhiyun UVC_GET_MAX 204*4882a593Smuzhiyun Obtain the maximum value of the control. 205*4882a593Smuzhiyun UVC_GET_DEF 206*4882a593Smuzhiyun Obtain the default value of the control. 207*4882a593Smuzhiyun UVC_GET_RES 208*4882a593Smuzhiyun Query the resolution of the control, i.e. the step size of the 209*4882a593Smuzhiyun allowed control values. 210*4882a593Smuzhiyun UVC_GET_LEN 211*4882a593Smuzhiyun Query the size of the control in bytes. 212*4882a593Smuzhiyun UVC_GET_INFO 213*4882a593Smuzhiyun Query the control information bitmap, which indicates whether 214*4882a593Smuzhiyun get/set requests are supported. 215*4882a593Smuzhiyun UVC_SET_CUR 216*4882a593Smuzhiyun Update the value of the control. 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun Applications must set the 'size' field to the correct length for the 219*4882a593Smuzhiyun control. Exceptions are the UVC_GET_LEN and UVC_GET_INFO queries, for 220*4882a593Smuzhiyun which the size must be set to 2 and 1, respectively. The 'data' field 221*4882a593Smuzhiyun must point to a valid writable buffer big enough to hold the indicated 222*4882a593Smuzhiyun number of data bytes. 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun Data is copied directly from the device without any driver-side 225*4882a593Smuzhiyun processing. Applications are responsible for data buffer formatting, 226*4882a593Smuzhiyun including little-endian/big-endian conversion. This is particularly 227*4882a593Smuzhiyun important for the result of the UVC_GET_LEN requests, which is always 228*4882a593Smuzhiyun returned as a little-endian 16-bit integer by the device. 229*4882a593Smuzhiyun 230*4882a593Smuzhiyun**Return value**: 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun On success 0 is returned. On error -1 is returned and errno is set 233*4882a593Smuzhiyun appropriately. 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun ENOENT 236*4882a593Smuzhiyun The device does not support the given control or the specified 237*4882a593Smuzhiyun extension unit could not be found. 238*4882a593Smuzhiyun ENOBUFS 239*4882a593Smuzhiyun The specified buffer size is incorrect (too big or too small). 240*4882a593Smuzhiyun EINVAL 241*4882a593Smuzhiyun An invalid request code was passed. 242*4882a593Smuzhiyun EBADRQC 243*4882a593Smuzhiyun The given request is not supported by the given control. 244*4882a593Smuzhiyun EFAULT 245*4882a593Smuzhiyun The data pointer references an inaccessible memory area. 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun**Data types**: 248*4882a593Smuzhiyun 249*4882a593Smuzhiyun.. code-block:: none 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun * struct uvc_xu_control_query 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun __u8 unit Extension unit ID 254*4882a593Smuzhiyun __u8 selector Control selector 255*4882a593Smuzhiyun __u8 query Request code to send to the device 256*4882a593Smuzhiyun __u16 size Control data size (in bytes) 257*4882a593Smuzhiyun __u8 *data Control value 258