1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunV4L2 Controls 4*4882a593Smuzhiyun============= 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunIntroduction 7*4882a593Smuzhiyun------------ 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunThe V4L2 control API seems simple enough, but quickly becomes very hard to 10*4882a593Smuzhiyunimplement correctly in drivers. But much of the code needed to handle controls 11*4882a593Smuzhiyunis actually not driver specific and can be moved to the V4L core framework. 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunAfter all, the only part that a driver developer is interested in is: 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun1) How do I add a control? 16*4882a593Smuzhiyun2) How do I set the control's value? (i.e. s_ctrl) 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunAnd occasionally: 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun3) How do I get the control's value? (i.e. g_volatile_ctrl) 21*4882a593Smuzhiyun4) How do I validate the user's proposed control value? (i.e. try_ctrl) 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunAll the rest is something that can be done centrally. 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunThe control framework was created in order to implement all the rules of the 26*4882a593SmuzhiyunV4L2 specification with respect to controls in a central place. And to make 27*4882a593Smuzhiyunlife as easy as possible for the driver developer. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunNote that the control framework relies on the presence of a struct 30*4882a593Smuzhiyun:c:type:`v4l2_device` for V4L2 drivers and struct v4l2_subdev for 31*4882a593Smuzhiyunsub-device drivers. 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunObjects in the framework 35*4882a593Smuzhiyun------------------------ 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunThere are two main objects: 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunThe :c:type:`v4l2_ctrl` object describes the control properties and keeps 40*4882a593Smuzhiyuntrack of the control's value (both the current value and the proposed new 41*4882a593Smuzhiyunvalue). 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun:c:type:`v4l2_ctrl_handler` is the object that keeps track of controls. It 44*4882a593Smuzhiyunmaintains a list of v4l2_ctrl objects that it owns and another list of 45*4882a593Smuzhiyunreferences to controls, possibly to controls owned by other handlers. 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunBasic usage for V4L2 and sub-device drivers 49*4882a593Smuzhiyun------------------------------------------- 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun1) Prepare the driver: 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun.. code-block:: c 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun #include <media/v4l2-ctrls.h> 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun1.1) Add the handler to your driver's top-level struct: 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunFor V4L2 drivers: 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun.. code-block:: c 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun struct foo_dev { 64*4882a593Smuzhiyun ... 65*4882a593Smuzhiyun struct v4l2_device v4l2_dev; 66*4882a593Smuzhiyun ... 67*4882a593Smuzhiyun struct v4l2_ctrl_handler ctrl_handler; 68*4882a593Smuzhiyun ... 69*4882a593Smuzhiyun }; 70*4882a593Smuzhiyun 71*4882a593SmuzhiyunFor sub-device drivers: 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun.. code-block:: c 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun struct foo_dev { 76*4882a593Smuzhiyun ... 77*4882a593Smuzhiyun struct v4l2_subdev sd; 78*4882a593Smuzhiyun ... 79*4882a593Smuzhiyun struct v4l2_ctrl_handler ctrl_handler; 80*4882a593Smuzhiyun ... 81*4882a593Smuzhiyun }; 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun1.2) Initialize the handler: 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun.. code-block:: c 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun v4l2_ctrl_handler_init(&foo->ctrl_handler, nr_of_controls); 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunThe second argument is a hint telling the function how many controls this 90*4882a593Smuzhiyunhandler is expected to handle. It will allocate a hashtable based on this 91*4882a593Smuzhiyuninformation. It is a hint only. 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun1.3) Hook the control handler into the driver: 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunFor V4L2 drivers: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun.. code-block:: c 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun foo->v4l2_dev.ctrl_handler = &foo->ctrl_handler; 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunFor sub-device drivers: 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun.. code-block:: c 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun foo->sd.ctrl_handler = &foo->ctrl_handler; 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun1.4) Clean up the handler at the end: 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun.. code-block:: c 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun v4l2_ctrl_handler_free(&foo->ctrl_handler); 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun2) Add controls: 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunYou add non-menu controls by calling :c:func:`v4l2_ctrl_new_std`: 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun.. code-block:: c 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl, 121*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops, 122*4882a593Smuzhiyun u32 id, s32 min, s32 max, u32 step, s32 def); 123*4882a593Smuzhiyun 124*4882a593SmuzhiyunMenu and integer menu controls are added by calling 125*4882a593Smuzhiyun:c:func:`v4l2_ctrl_new_std_menu`: 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun.. code-block:: c 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl, 130*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops, 131*4882a593Smuzhiyun u32 id, s32 max, s32 skip_mask, s32 def); 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunMenu controls with a driver specific menu are added by calling 134*4882a593Smuzhiyun:c:func:`v4l2_ctrl_new_std_menu_items`: 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun.. code-block:: c 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items( 139*4882a593Smuzhiyun struct v4l2_ctrl_handler *hdl, 140*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops, u32 id, s32 max, 141*4882a593Smuzhiyun s32 skip_mask, s32 def, const char * const *qmenu); 142*4882a593Smuzhiyun 143*4882a593SmuzhiyunStandard compound controls can be added by calling 144*4882a593Smuzhiyun:c:func:`v4l2_ctrl_new_std_compound`: 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun.. code-block:: c 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl, 149*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops, u32 id, 150*4882a593Smuzhiyun const union v4l2_ctrl_ptr p_def); 151*4882a593Smuzhiyun 152*4882a593SmuzhiyunInteger menu controls with a driver specific menu can be added by calling 153*4882a593Smuzhiyun:c:func:`v4l2_ctrl_new_int_menu`: 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun.. code-block:: c 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl, 158*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops, 159*4882a593Smuzhiyun u32 id, s32 max, s32 def, const s64 *qmenu_int); 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunThese functions are typically called right after the 162*4882a593Smuzhiyun:c:func:`v4l2_ctrl_handler_init`: 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun.. code-block:: c 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun static const s64 exp_bias_qmenu[] = { 167*4882a593Smuzhiyun -2, -1, 0, 1, 2 168*4882a593Smuzhiyun }; 169*4882a593Smuzhiyun static const char * const test_pattern[] = { 170*4882a593Smuzhiyun "Disabled", 171*4882a593Smuzhiyun "Vertical Bars", 172*4882a593Smuzhiyun "Solid Black", 173*4882a593Smuzhiyun "Solid White", 174*4882a593Smuzhiyun }; 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun v4l2_ctrl_handler_init(&foo->ctrl_handler, nr_of_controls); 177*4882a593Smuzhiyun v4l2_ctrl_new_std(&foo->ctrl_handler, &foo_ctrl_ops, 178*4882a593Smuzhiyun V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); 179*4882a593Smuzhiyun v4l2_ctrl_new_std(&foo->ctrl_handler, &foo_ctrl_ops, 180*4882a593Smuzhiyun V4L2_CID_CONTRAST, 0, 255, 1, 128); 181*4882a593Smuzhiyun v4l2_ctrl_new_std_menu(&foo->ctrl_handler, &foo_ctrl_ops, 182*4882a593Smuzhiyun V4L2_CID_POWER_LINE_FREQUENCY, 183*4882a593Smuzhiyun V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 184*4882a593Smuzhiyun V4L2_CID_POWER_LINE_FREQUENCY_DISABLED); 185*4882a593Smuzhiyun v4l2_ctrl_new_int_menu(&foo->ctrl_handler, &foo_ctrl_ops, 186*4882a593Smuzhiyun V4L2_CID_EXPOSURE_BIAS, 187*4882a593Smuzhiyun ARRAY_SIZE(exp_bias_qmenu) - 1, 188*4882a593Smuzhiyun ARRAY_SIZE(exp_bias_qmenu) / 2 - 1, 189*4882a593Smuzhiyun exp_bias_qmenu); 190*4882a593Smuzhiyun v4l2_ctrl_new_std_menu_items(&foo->ctrl_handler, &foo_ctrl_ops, 191*4882a593Smuzhiyun V4L2_CID_TEST_PATTERN, ARRAY_SIZE(test_pattern) - 1, 0, 192*4882a593Smuzhiyun 0, test_pattern); 193*4882a593Smuzhiyun ... 194*4882a593Smuzhiyun if (foo->ctrl_handler.error) { 195*4882a593Smuzhiyun int err = foo->ctrl_handler.error; 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun v4l2_ctrl_handler_free(&foo->ctrl_handler); 198*4882a593Smuzhiyun return err; 199*4882a593Smuzhiyun } 200*4882a593Smuzhiyun 201*4882a593SmuzhiyunThe :c:func:`v4l2_ctrl_new_std` function returns the v4l2_ctrl pointer to 202*4882a593Smuzhiyunthe new control, but if you do not need to access the pointer outside the 203*4882a593Smuzhiyuncontrol ops, then there is no need to store it. 204*4882a593Smuzhiyun 205*4882a593SmuzhiyunThe :c:func:`v4l2_ctrl_new_std` function will fill in most fields based on 206*4882a593Smuzhiyunthe control ID except for the min, max, step and default values. These are 207*4882a593Smuzhiyunpassed in the last four arguments. These values are driver specific while 208*4882a593Smuzhiyuncontrol attributes like type, name, flags are all global. The control's 209*4882a593Smuzhiyuncurrent value will be set to the default value. 210*4882a593Smuzhiyun 211*4882a593SmuzhiyunThe :c:func:`v4l2_ctrl_new_std_menu` function is very similar but it is 212*4882a593Smuzhiyunused for menu controls. There is no min argument since that is always 0 for 213*4882a593Smuzhiyunmenu controls, and instead of a step there is a skip_mask argument: if bit 214*4882a593SmuzhiyunX is 1, then menu item X is skipped. 215*4882a593Smuzhiyun 216*4882a593SmuzhiyunThe :c:func:`v4l2_ctrl_new_int_menu` function creates a new standard 217*4882a593Smuzhiyuninteger menu control with driver-specific items in the menu. It differs 218*4882a593Smuzhiyunfrom v4l2_ctrl_new_std_menu in that it doesn't have the mask argument and 219*4882a593Smuzhiyuntakes as the last argument an array of signed 64-bit integers that form an 220*4882a593Smuzhiyunexact menu item list. 221*4882a593Smuzhiyun 222*4882a593SmuzhiyunThe :c:func:`v4l2_ctrl_new_std_menu_items` function is very similar to 223*4882a593Smuzhiyunv4l2_ctrl_new_std_menu but takes an extra parameter qmenu, which is the 224*4882a593Smuzhiyundriver specific menu for an otherwise standard menu control. A good example 225*4882a593Smuzhiyunfor this control is the test pattern control for capture/display/sensors 226*4882a593Smuzhiyundevices that have the capability to generate test patterns. These test 227*4882a593Smuzhiyunpatterns are hardware specific, so the contents of the menu will vary from 228*4882a593Smuzhiyundevice to device. 229*4882a593Smuzhiyun 230*4882a593SmuzhiyunNote that if something fails, the function will return NULL or an error and 231*4882a593Smuzhiyunset ctrl_handler->error to the error code. If ctrl_handler->error was already 232*4882a593Smuzhiyunset, then it will just return and do nothing. This is also true for 233*4882a593Smuzhiyunv4l2_ctrl_handler_init if it cannot allocate the internal data structure. 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunThis makes it easy to init the handler and just add all controls and only check 236*4882a593Smuzhiyunthe error code at the end. Saves a lot of repetitive error checking. 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunIt is recommended to add controls in ascending control ID order: it will be 239*4882a593Smuzhiyuna bit faster that way. 240*4882a593Smuzhiyun 241*4882a593Smuzhiyun3) Optionally force initial control setup: 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun.. code-block:: c 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun v4l2_ctrl_handler_setup(&foo->ctrl_handler); 246*4882a593Smuzhiyun 247*4882a593SmuzhiyunThis will call s_ctrl for all controls unconditionally. Effectively this 248*4882a593Smuzhiyuninitializes the hardware to the default control values. It is recommended 249*4882a593Smuzhiyunthat you do this as this ensures that both the internal data structures and 250*4882a593Smuzhiyunthe hardware are in sync. 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun4) Finally: implement the :c:type:`v4l2_ctrl_ops` 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun.. code-block:: c 255*4882a593Smuzhiyun 256*4882a593Smuzhiyun static const struct v4l2_ctrl_ops foo_ctrl_ops = { 257*4882a593Smuzhiyun .s_ctrl = foo_s_ctrl, 258*4882a593Smuzhiyun }; 259*4882a593Smuzhiyun 260*4882a593SmuzhiyunUsually all you need is s_ctrl: 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun.. code-block:: c 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun static int foo_s_ctrl(struct v4l2_ctrl *ctrl) 265*4882a593Smuzhiyun { 266*4882a593Smuzhiyun struct foo *state = container_of(ctrl->handler, struct foo, ctrl_handler); 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun switch (ctrl->id) { 269*4882a593Smuzhiyun case V4L2_CID_BRIGHTNESS: 270*4882a593Smuzhiyun write_reg(0x123, ctrl->val); 271*4882a593Smuzhiyun break; 272*4882a593Smuzhiyun case V4L2_CID_CONTRAST: 273*4882a593Smuzhiyun write_reg(0x456, ctrl->val); 274*4882a593Smuzhiyun break; 275*4882a593Smuzhiyun } 276*4882a593Smuzhiyun return 0; 277*4882a593Smuzhiyun } 278*4882a593Smuzhiyun 279*4882a593SmuzhiyunThe control ops are called with the v4l2_ctrl pointer as argument. 280*4882a593SmuzhiyunThe new control value has already been validated, so all you need to do is 281*4882a593Smuzhiyunto actually update the hardware registers. 282*4882a593Smuzhiyun 283*4882a593SmuzhiyunYou're done! And this is sufficient for most of the drivers we have. No need 284*4882a593Smuzhiyunto do any validation of control values, or implement QUERYCTRL, QUERY_EXT_CTRL 285*4882a593Smuzhiyunand QUERYMENU. And G/S_CTRL as well as G/TRY/S_EXT_CTRLS are automatically supported. 286*4882a593Smuzhiyun 287*4882a593Smuzhiyun 288*4882a593Smuzhiyun.. note:: 289*4882a593Smuzhiyun 290*4882a593Smuzhiyun The remainder sections deal with more advanced controls topics and scenarios. 291*4882a593Smuzhiyun In practice the basic usage as described above is sufficient for most drivers. 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun 294*4882a593SmuzhiyunInheriting Sub-device Controls 295*4882a593Smuzhiyun------------------------------ 296*4882a593Smuzhiyun 297*4882a593SmuzhiyunWhen a sub-device is registered with a V4L2 driver by calling 298*4882a593Smuzhiyunv4l2_device_register_subdev() and the ctrl_handler fields of both v4l2_subdev 299*4882a593Smuzhiyunand v4l2_device are set, then the controls of the subdev will become 300*4882a593Smuzhiyunautomatically available in the V4L2 driver as well. If the subdev driver 301*4882a593Smuzhiyuncontains controls that already exist in the V4L2 driver, then those will be 302*4882a593Smuzhiyunskipped (so a V4L2 driver can always override a subdev control). 303*4882a593Smuzhiyun 304*4882a593SmuzhiyunWhat happens here is that v4l2_device_register_subdev() calls 305*4882a593Smuzhiyunv4l2_ctrl_add_handler() adding the controls of the subdev to the controls 306*4882a593Smuzhiyunof v4l2_device. 307*4882a593Smuzhiyun 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunAccessing Control Values 310*4882a593Smuzhiyun------------------------ 311*4882a593Smuzhiyun 312*4882a593SmuzhiyunThe following union is used inside the control framework to access control 313*4882a593Smuzhiyunvalues: 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun.. code-block:: c 316*4882a593Smuzhiyun 317*4882a593Smuzhiyun union v4l2_ctrl_ptr { 318*4882a593Smuzhiyun s32 *p_s32; 319*4882a593Smuzhiyun s64 *p_s64; 320*4882a593Smuzhiyun char *p_char; 321*4882a593Smuzhiyun void *p; 322*4882a593Smuzhiyun }; 323*4882a593Smuzhiyun 324*4882a593SmuzhiyunThe v4l2_ctrl struct contains these fields that can be used to access both 325*4882a593Smuzhiyuncurrent and new values: 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun.. code-block:: c 328*4882a593Smuzhiyun 329*4882a593Smuzhiyun s32 val; 330*4882a593Smuzhiyun struct { 331*4882a593Smuzhiyun s32 val; 332*4882a593Smuzhiyun } cur; 333*4882a593Smuzhiyun 334*4882a593Smuzhiyun 335*4882a593Smuzhiyun union v4l2_ctrl_ptr p_new; 336*4882a593Smuzhiyun union v4l2_ctrl_ptr p_cur; 337*4882a593Smuzhiyun 338*4882a593SmuzhiyunIf the control has a simple s32 type type, then: 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun.. code-block:: c 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun &ctrl->val == ctrl->p_new.p_s32 343*4882a593Smuzhiyun &ctrl->cur.val == ctrl->p_cur.p_s32 344*4882a593Smuzhiyun 345*4882a593SmuzhiyunFor all other types use ctrl->p_cur.p<something>. Basically the val 346*4882a593Smuzhiyunand cur.val fields can be considered an alias since these are used so often. 347*4882a593Smuzhiyun 348*4882a593SmuzhiyunWithin the control ops you can freely use these. The val and cur.val speak for 349*4882a593Smuzhiyunthemselves. The p_char pointers point to character buffers of length 350*4882a593Smuzhiyunctrl->maximum + 1, and are always 0-terminated. 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunUnless the control is marked volatile the p_cur field points to the the 353*4882a593Smuzhiyuncurrent cached control value. When you create a new control this value is made 354*4882a593Smuzhiyunidentical to the default value. After calling v4l2_ctrl_handler_setup() this 355*4882a593Smuzhiyunvalue is passed to the hardware. It is generally a good idea to call this 356*4882a593Smuzhiyunfunction. 357*4882a593Smuzhiyun 358*4882a593SmuzhiyunWhenever a new value is set that new value is automatically cached. This means 359*4882a593Smuzhiyunthat most drivers do not need to implement the g_volatile_ctrl() op. The 360*4882a593Smuzhiyunexception is for controls that return a volatile register such as a signal 361*4882a593Smuzhiyunstrength read-out that changes continuously. In that case you will need to 362*4882a593Smuzhiyunimplement g_volatile_ctrl like this: 363*4882a593Smuzhiyun 364*4882a593Smuzhiyun.. code-block:: c 365*4882a593Smuzhiyun 366*4882a593Smuzhiyun static int foo_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 367*4882a593Smuzhiyun { 368*4882a593Smuzhiyun switch (ctrl->id) { 369*4882a593Smuzhiyun case V4L2_CID_BRIGHTNESS: 370*4882a593Smuzhiyun ctrl->val = read_reg(0x123); 371*4882a593Smuzhiyun break; 372*4882a593Smuzhiyun } 373*4882a593Smuzhiyun } 374*4882a593Smuzhiyun 375*4882a593SmuzhiyunNote that you use the 'new value' union as well in g_volatile_ctrl. In general 376*4882a593Smuzhiyuncontrols that need to implement g_volatile_ctrl are read-only controls. If they 377*4882a593Smuzhiyunare not, a V4L2_EVENT_CTRL_CH_VALUE will not be generated when the control 378*4882a593Smuzhiyunchanges. 379*4882a593Smuzhiyun 380*4882a593SmuzhiyunTo mark a control as volatile you have to set V4L2_CTRL_FLAG_VOLATILE: 381*4882a593Smuzhiyun 382*4882a593Smuzhiyun.. code-block:: c 383*4882a593Smuzhiyun 384*4882a593Smuzhiyun ctrl = v4l2_ctrl_new_std(&sd->ctrl_handler, ...); 385*4882a593Smuzhiyun if (ctrl) 386*4882a593Smuzhiyun ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 387*4882a593Smuzhiyun 388*4882a593SmuzhiyunFor try/s_ctrl the new values (i.e. as passed by the user) are filled in and 389*4882a593Smuzhiyunyou can modify them in try_ctrl or set them in s_ctrl. The 'cur' union 390*4882a593Smuzhiyuncontains the current value, which you can use (but not change!) as well. 391*4882a593Smuzhiyun 392*4882a593SmuzhiyunIf s_ctrl returns 0 (OK), then the control framework will copy the new final 393*4882a593Smuzhiyunvalues to the 'cur' union. 394*4882a593Smuzhiyun 395*4882a593SmuzhiyunWhile in g_volatile/s/try_ctrl you can access the value of all controls owned 396*4882a593Smuzhiyunby the same handler since the handler's lock is held. If you need to access 397*4882a593Smuzhiyunthe value of controls owned by other handlers, then you have to be very careful 398*4882a593Smuzhiyunnot to introduce deadlocks. 399*4882a593Smuzhiyun 400*4882a593SmuzhiyunOutside of the control ops you have to go through to helper functions to get 401*4882a593Smuzhiyunor set a single control value safely in your driver: 402*4882a593Smuzhiyun 403*4882a593Smuzhiyun.. code-block:: c 404*4882a593Smuzhiyun 405*4882a593Smuzhiyun s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl); 406*4882a593Smuzhiyun int v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val); 407*4882a593Smuzhiyun 408*4882a593SmuzhiyunThese functions go through the control framework just as VIDIOC_G/S_CTRL ioctls 409*4882a593Smuzhiyundo. Don't use these inside the control ops g_volatile/s/try_ctrl, though, that 410*4882a593Smuzhiyunwill result in a deadlock since these helpers lock the handler as well. 411*4882a593Smuzhiyun 412*4882a593SmuzhiyunYou can also take the handler lock yourself: 413*4882a593Smuzhiyun 414*4882a593Smuzhiyun.. code-block:: c 415*4882a593Smuzhiyun 416*4882a593Smuzhiyun mutex_lock(&state->ctrl_handler.lock); 417*4882a593Smuzhiyun pr_info("String value is '%s'\n", ctrl1->p_cur.p_char); 418*4882a593Smuzhiyun pr_info("Integer value is '%s'\n", ctrl2->cur.val); 419*4882a593Smuzhiyun mutex_unlock(&state->ctrl_handler.lock); 420*4882a593Smuzhiyun 421*4882a593Smuzhiyun 422*4882a593SmuzhiyunMenu Controls 423*4882a593Smuzhiyun------------- 424*4882a593Smuzhiyun 425*4882a593SmuzhiyunThe v4l2_ctrl struct contains this union: 426*4882a593Smuzhiyun 427*4882a593Smuzhiyun.. code-block:: c 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun union { 430*4882a593Smuzhiyun u32 step; 431*4882a593Smuzhiyun u32 menu_skip_mask; 432*4882a593Smuzhiyun }; 433*4882a593Smuzhiyun 434*4882a593SmuzhiyunFor menu controls menu_skip_mask is used. What it does is that it allows you 435*4882a593Smuzhiyunto easily exclude certain menu items. This is used in the VIDIOC_QUERYMENU 436*4882a593Smuzhiyunimplementation where you can return -EINVAL if a certain menu item is not 437*4882a593Smuzhiyunpresent. Note that VIDIOC_QUERYCTRL always returns a step value of 1 for 438*4882a593Smuzhiyunmenu controls. 439*4882a593Smuzhiyun 440*4882a593SmuzhiyunA good example is the MPEG Audio Layer II Bitrate menu control where the 441*4882a593Smuzhiyunmenu is a list of standardized possible bitrates. But in practice hardware 442*4882a593Smuzhiyunimplementations will only support a subset of those. By setting the skip 443*4882a593Smuzhiyunmask you can tell the framework which menu items should be skipped. Setting 444*4882a593Smuzhiyunit to 0 means that all menu items are supported. 445*4882a593Smuzhiyun 446*4882a593SmuzhiyunYou set this mask either through the v4l2_ctrl_config struct for a custom 447*4882a593Smuzhiyuncontrol, or by calling v4l2_ctrl_new_std_menu(). 448*4882a593Smuzhiyun 449*4882a593Smuzhiyun 450*4882a593SmuzhiyunCustom Controls 451*4882a593Smuzhiyun--------------- 452*4882a593Smuzhiyun 453*4882a593SmuzhiyunDriver specific controls can be created using v4l2_ctrl_new_custom(): 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun.. code-block:: c 456*4882a593Smuzhiyun 457*4882a593Smuzhiyun static const struct v4l2_ctrl_config ctrl_filter = { 458*4882a593Smuzhiyun .ops = &ctrl_custom_ops, 459*4882a593Smuzhiyun .id = V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER, 460*4882a593Smuzhiyun .name = "Spatial Filter", 461*4882a593Smuzhiyun .type = V4L2_CTRL_TYPE_INTEGER, 462*4882a593Smuzhiyun .flags = V4L2_CTRL_FLAG_SLIDER, 463*4882a593Smuzhiyun .max = 15, 464*4882a593Smuzhiyun .step = 1, 465*4882a593Smuzhiyun }; 466*4882a593Smuzhiyun 467*4882a593Smuzhiyun ctrl = v4l2_ctrl_new_custom(&foo->ctrl_handler, &ctrl_filter, NULL); 468*4882a593Smuzhiyun 469*4882a593SmuzhiyunThe last argument is the priv pointer which can be set to driver-specific 470*4882a593Smuzhiyunprivate data. 471*4882a593Smuzhiyun 472*4882a593SmuzhiyunThe v4l2_ctrl_config struct also has a field to set the is_private flag. 473*4882a593Smuzhiyun 474*4882a593SmuzhiyunIf the name field is not set, then the framework will assume this is a standard 475*4882a593Smuzhiyuncontrol and will fill in the name, type and flags fields accordingly. 476*4882a593Smuzhiyun 477*4882a593Smuzhiyun 478*4882a593SmuzhiyunActive and Grabbed Controls 479*4882a593Smuzhiyun--------------------------- 480*4882a593Smuzhiyun 481*4882a593SmuzhiyunIf you get more complex relationships between controls, then you may have to 482*4882a593Smuzhiyunactivate and deactivate controls. For example, if the Chroma AGC control is 483*4882a593Smuzhiyunon, then the Chroma Gain control is inactive. That is, you may set it, but 484*4882a593Smuzhiyunthe value will not be used by the hardware as long as the automatic gain 485*4882a593Smuzhiyuncontrol is on. Typically user interfaces can disable such input fields. 486*4882a593Smuzhiyun 487*4882a593SmuzhiyunYou can set the 'active' status using v4l2_ctrl_activate(). By default all 488*4882a593Smuzhiyuncontrols are active. Note that the framework does not check for this flag. 489*4882a593SmuzhiyunIt is meant purely for GUIs. The function is typically called from within 490*4882a593Smuzhiyuns_ctrl. 491*4882a593Smuzhiyun 492*4882a593SmuzhiyunThe other flag is the 'grabbed' flag. A grabbed control means that you cannot 493*4882a593Smuzhiyunchange it because it is in use by some resource. Typical examples are MPEG 494*4882a593Smuzhiyunbitrate controls that cannot be changed while capturing is in progress. 495*4882a593Smuzhiyun 496*4882a593SmuzhiyunIf a control is set to 'grabbed' using v4l2_ctrl_grab(), then the framework 497*4882a593Smuzhiyunwill return -EBUSY if an attempt is made to set this control. The 498*4882a593Smuzhiyunv4l2_ctrl_grab() function is typically called from the driver when it 499*4882a593Smuzhiyunstarts or stops streaming. 500*4882a593Smuzhiyun 501*4882a593Smuzhiyun 502*4882a593SmuzhiyunControl Clusters 503*4882a593Smuzhiyun---------------- 504*4882a593Smuzhiyun 505*4882a593SmuzhiyunBy default all controls are independent from the others. But in more 506*4882a593Smuzhiyuncomplex scenarios you can get dependencies from one control to another. 507*4882a593SmuzhiyunIn that case you need to 'cluster' them: 508*4882a593Smuzhiyun 509*4882a593Smuzhiyun.. code-block:: c 510*4882a593Smuzhiyun 511*4882a593Smuzhiyun struct foo { 512*4882a593Smuzhiyun struct v4l2_ctrl_handler ctrl_handler; 513*4882a593Smuzhiyun #define AUDIO_CL_VOLUME (0) 514*4882a593Smuzhiyun #define AUDIO_CL_MUTE (1) 515*4882a593Smuzhiyun struct v4l2_ctrl *audio_cluster[2]; 516*4882a593Smuzhiyun ... 517*4882a593Smuzhiyun }; 518*4882a593Smuzhiyun 519*4882a593Smuzhiyun state->audio_cluster[AUDIO_CL_VOLUME] = 520*4882a593Smuzhiyun v4l2_ctrl_new_std(&state->ctrl_handler, ...); 521*4882a593Smuzhiyun state->audio_cluster[AUDIO_CL_MUTE] = 522*4882a593Smuzhiyun v4l2_ctrl_new_std(&state->ctrl_handler, ...); 523*4882a593Smuzhiyun v4l2_ctrl_cluster(ARRAY_SIZE(state->audio_cluster), state->audio_cluster); 524*4882a593Smuzhiyun 525*4882a593SmuzhiyunFrom now on whenever one or more of the controls belonging to the same 526*4882a593Smuzhiyuncluster is set (or 'gotten', or 'tried'), only the control ops of the first 527*4882a593Smuzhiyuncontrol ('volume' in this example) is called. You effectively create a new 528*4882a593Smuzhiyuncomposite control. Similar to how a 'struct' works in C. 529*4882a593Smuzhiyun 530*4882a593SmuzhiyunSo when s_ctrl is called with V4L2_CID_AUDIO_VOLUME as argument, you should set 531*4882a593Smuzhiyunall two controls belonging to the audio_cluster: 532*4882a593Smuzhiyun 533*4882a593Smuzhiyun.. code-block:: c 534*4882a593Smuzhiyun 535*4882a593Smuzhiyun static int foo_s_ctrl(struct v4l2_ctrl *ctrl) 536*4882a593Smuzhiyun { 537*4882a593Smuzhiyun struct foo *state = container_of(ctrl->handler, struct foo, ctrl_handler); 538*4882a593Smuzhiyun 539*4882a593Smuzhiyun switch (ctrl->id) { 540*4882a593Smuzhiyun case V4L2_CID_AUDIO_VOLUME: { 541*4882a593Smuzhiyun struct v4l2_ctrl *mute = ctrl->cluster[AUDIO_CL_MUTE]; 542*4882a593Smuzhiyun 543*4882a593Smuzhiyun write_reg(0x123, mute->val ? 0 : ctrl->val); 544*4882a593Smuzhiyun break; 545*4882a593Smuzhiyun } 546*4882a593Smuzhiyun case V4L2_CID_CONTRAST: 547*4882a593Smuzhiyun write_reg(0x456, ctrl->val); 548*4882a593Smuzhiyun break; 549*4882a593Smuzhiyun } 550*4882a593Smuzhiyun return 0; 551*4882a593Smuzhiyun } 552*4882a593Smuzhiyun 553*4882a593SmuzhiyunIn the example above the following are equivalent for the VOLUME case: 554*4882a593Smuzhiyun 555*4882a593Smuzhiyun.. code-block:: c 556*4882a593Smuzhiyun 557*4882a593Smuzhiyun ctrl == ctrl->cluster[AUDIO_CL_VOLUME] == state->audio_cluster[AUDIO_CL_VOLUME] 558*4882a593Smuzhiyun ctrl->cluster[AUDIO_CL_MUTE] == state->audio_cluster[AUDIO_CL_MUTE] 559*4882a593Smuzhiyun 560*4882a593SmuzhiyunIn practice using cluster arrays like this becomes very tiresome. So instead 561*4882a593Smuzhiyunthe following equivalent method is used: 562*4882a593Smuzhiyun 563*4882a593Smuzhiyun.. code-block:: c 564*4882a593Smuzhiyun 565*4882a593Smuzhiyun struct { 566*4882a593Smuzhiyun /* audio cluster */ 567*4882a593Smuzhiyun struct v4l2_ctrl *volume; 568*4882a593Smuzhiyun struct v4l2_ctrl *mute; 569*4882a593Smuzhiyun }; 570*4882a593Smuzhiyun 571*4882a593SmuzhiyunThe anonymous struct is used to clearly 'cluster' these two control pointers, 572*4882a593Smuzhiyunbut it serves no other purpose. The effect is the same as creating an 573*4882a593Smuzhiyunarray with two control pointers. So you can just do: 574*4882a593Smuzhiyun 575*4882a593Smuzhiyun.. code-block:: c 576*4882a593Smuzhiyun 577*4882a593Smuzhiyun state->volume = v4l2_ctrl_new_std(&state->ctrl_handler, ...); 578*4882a593Smuzhiyun state->mute = v4l2_ctrl_new_std(&state->ctrl_handler, ...); 579*4882a593Smuzhiyun v4l2_ctrl_cluster(2, &state->volume); 580*4882a593Smuzhiyun 581*4882a593SmuzhiyunAnd in foo_s_ctrl you can use these pointers directly: state->mute->val. 582*4882a593Smuzhiyun 583*4882a593SmuzhiyunNote that controls in a cluster may be NULL. For example, if for some 584*4882a593Smuzhiyunreason mute was never added (because the hardware doesn't support that 585*4882a593Smuzhiyunparticular feature), then mute will be NULL. So in that case we have a 586*4882a593Smuzhiyuncluster of 2 controls, of which only 1 is actually instantiated. The 587*4882a593Smuzhiyunonly restriction is that the first control of the cluster must always be 588*4882a593Smuzhiyunpresent, since that is the 'master' control of the cluster. The master 589*4882a593Smuzhiyuncontrol is the one that identifies the cluster and that provides the 590*4882a593Smuzhiyunpointer to the v4l2_ctrl_ops struct that is used for that cluster. 591*4882a593Smuzhiyun 592*4882a593SmuzhiyunObviously, all controls in the cluster array must be initialized to either 593*4882a593Smuzhiyuna valid control or to NULL. 594*4882a593Smuzhiyun 595*4882a593SmuzhiyunIn rare cases you might want to know which controls of a cluster actually 596*4882a593Smuzhiyunwere set explicitly by the user. For this you can check the 'is_new' flag of 597*4882a593Smuzhiyuneach control. For example, in the case of a volume/mute cluster the 'is_new' 598*4882a593Smuzhiyunflag of the mute control would be set if the user called VIDIOC_S_CTRL for 599*4882a593Smuzhiyunmute only. If the user would call VIDIOC_S_EXT_CTRLS for both mute and volume 600*4882a593Smuzhiyuncontrols, then the 'is_new' flag would be 1 for both controls. 601*4882a593Smuzhiyun 602*4882a593SmuzhiyunThe 'is_new' flag is always 1 when called from v4l2_ctrl_handler_setup(). 603*4882a593Smuzhiyun 604*4882a593Smuzhiyun 605*4882a593SmuzhiyunHandling autogain/gain-type Controls with Auto Clusters 606*4882a593Smuzhiyun------------------------------------------------------- 607*4882a593Smuzhiyun 608*4882a593SmuzhiyunA common type of control cluster is one that handles 'auto-foo/foo'-type 609*4882a593Smuzhiyuncontrols. Typical examples are autogain/gain, autoexposure/exposure, 610*4882a593Smuzhiyunautowhitebalance/red balance/blue balance. In all cases you have one control 611*4882a593Smuzhiyunthat determines whether another control is handled automatically by the hardware, 612*4882a593Smuzhiyunor whether it is under manual control from the user. 613*4882a593Smuzhiyun 614*4882a593SmuzhiyunIf the cluster is in automatic mode, then the manual controls should be 615*4882a593Smuzhiyunmarked inactive and volatile. When the volatile controls are read the 616*4882a593Smuzhiyung_volatile_ctrl operation should return the value that the hardware's automatic 617*4882a593Smuzhiyunmode set up automatically. 618*4882a593Smuzhiyun 619*4882a593SmuzhiyunIf the cluster is put in manual mode, then the manual controls should become 620*4882a593Smuzhiyunactive again and the volatile flag is cleared (so g_volatile_ctrl is no longer 621*4882a593Smuzhiyuncalled while in manual mode). In addition just before switching to manual mode 622*4882a593Smuzhiyunthe current values as determined by the auto mode are copied as the new manual 623*4882a593Smuzhiyunvalues. 624*4882a593Smuzhiyun 625*4882a593SmuzhiyunFinally the V4L2_CTRL_FLAG_UPDATE should be set for the auto control since 626*4882a593Smuzhiyunchanging that control affects the control flags of the manual controls. 627*4882a593Smuzhiyun 628*4882a593SmuzhiyunIn order to simplify this a special variation of v4l2_ctrl_cluster was 629*4882a593Smuzhiyunintroduced: 630*4882a593Smuzhiyun 631*4882a593Smuzhiyun.. code-block:: c 632*4882a593Smuzhiyun 633*4882a593Smuzhiyun void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls, 634*4882a593Smuzhiyun u8 manual_val, bool set_volatile); 635*4882a593Smuzhiyun 636*4882a593SmuzhiyunThe first two arguments are identical to v4l2_ctrl_cluster. The third argument 637*4882a593Smuzhiyuntells the framework which value switches the cluster into manual mode. The 638*4882a593Smuzhiyunlast argument will optionally set V4L2_CTRL_FLAG_VOLATILE for the non-auto controls. 639*4882a593SmuzhiyunIf it is false, then the manual controls are never volatile. You would typically 640*4882a593Smuzhiyunuse that if the hardware does not give you the option to read back to values as 641*4882a593Smuzhiyundetermined by the auto mode (e.g. if autogain is on, the hardware doesn't allow 642*4882a593Smuzhiyunyou to obtain the current gain value). 643*4882a593Smuzhiyun 644*4882a593SmuzhiyunThe first control of the cluster is assumed to be the 'auto' control. 645*4882a593Smuzhiyun 646*4882a593SmuzhiyunUsing this function will ensure that you don't need to handle all the complex 647*4882a593Smuzhiyunflag and volatile handling. 648*4882a593Smuzhiyun 649*4882a593Smuzhiyun 650*4882a593SmuzhiyunVIDIOC_LOG_STATUS Support 651*4882a593Smuzhiyun------------------------- 652*4882a593Smuzhiyun 653*4882a593SmuzhiyunThis ioctl allow you to dump the current status of a driver to the kernel log. 654*4882a593SmuzhiyunThe v4l2_ctrl_handler_log_status(ctrl_handler, prefix) can be used to dump the 655*4882a593Smuzhiyunvalue of the controls owned by the given handler to the log. You can supply a 656*4882a593Smuzhiyunprefix as well. If the prefix didn't end with a space, then ': ' will be added 657*4882a593Smuzhiyunfor you. 658*4882a593Smuzhiyun 659*4882a593Smuzhiyun 660*4882a593SmuzhiyunDifferent Handlers for Different Video Nodes 661*4882a593Smuzhiyun-------------------------------------------- 662*4882a593Smuzhiyun 663*4882a593SmuzhiyunUsually the V4L2 driver has just one control handler that is global for 664*4882a593Smuzhiyunall video nodes. But you can also specify different control handlers for 665*4882a593Smuzhiyundifferent video nodes. You can do that by manually setting the ctrl_handler 666*4882a593Smuzhiyunfield of struct video_device. 667*4882a593Smuzhiyun 668*4882a593SmuzhiyunThat is no problem if there are no subdevs involved but if there are, then 669*4882a593Smuzhiyunyou need to block the automatic merging of subdev controls to the global 670*4882a593Smuzhiyuncontrol handler. You do that by simply setting the ctrl_handler field in 671*4882a593Smuzhiyunstruct v4l2_device to NULL. Now v4l2_device_register_subdev() will no longer 672*4882a593Smuzhiyunmerge subdev controls. 673*4882a593Smuzhiyun 674*4882a593SmuzhiyunAfter each subdev was added, you will then have to call v4l2_ctrl_add_handler 675*4882a593Smuzhiyunmanually to add the subdev's control handler (sd->ctrl_handler) to the desired 676*4882a593Smuzhiyuncontrol handler. This control handler may be specific to the video_device or 677*4882a593Smuzhiyunfor a subset of video_device's. For example: the radio device nodes only have 678*4882a593Smuzhiyunaudio controls, while the video and vbi device nodes share the same control 679*4882a593Smuzhiyunhandler for the audio and video controls. 680*4882a593Smuzhiyun 681*4882a593SmuzhiyunIf you want to have one handler (e.g. for a radio device node) have a subset 682*4882a593Smuzhiyunof another handler (e.g. for a video device node), then you should first add 683*4882a593Smuzhiyunthe controls to the first handler, add the other controls to the second 684*4882a593Smuzhiyunhandler and finally add the first handler to the second. For example: 685*4882a593Smuzhiyun 686*4882a593Smuzhiyun.. code-block:: c 687*4882a593Smuzhiyun 688*4882a593Smuzhiyun v4l2_ctrl_new_std(&radio_ctrl_handler, &radio_ops, V4L2_CID_AUDIO_VOLUME, ...); 689*4882a593Smuzhiyun v4l2_ctrl_new_std(&radio_ctrl_handler, &radio_ops, V4L2_CID_AUDIO_MUTE, ...); 690*4882a593Smuzhiyun v4l2_ctrl_new_std(&video_ctrl_handler, &video_ops, V4L2_CID_BRIGHTNESS, ...); 691*4882a593Smuzhiyun v4l2_ctrl_new_std(&video_ctrl_handler, &video_ops, V4L2_CID_CONTRAST, ...); 692*4882a593Smuzhiyun v4l2_ctrl_add_handler(&video_ctrl_handler, &radio_ctrl_handler, NULL); 693*4882a593Smuzhiyun 694*4882a593SmuzhiyunThe last argument to v4l2_ctrl_add_handler() is a filter function that allows 695*4882a593Smuzhiyunyou to filter which controls will be added. Set it to NULL if you want to add 696*4882a593Smuzhiyunall controls. 697*4882a593Smuzhiyun 698*4882a593SmuzhiyunOr you can add specific controls to a handler: 699*4882a593Smuzhiyun 700*4882a593Smuzhiyun.. code-block:: c 701*4882a593Smuzhiyun 702*4882a593Smuzhiyun volume = v4l2_ctrl_new_std(&video_ctrl_handler, &ops, V4L2_CID_AUDIO_VOLUME, ...); 703*4882a593Smuzhiyun v4l2_ctrl_new_std(&video_ctrl_handler, &ops, V4L2_CID_BRIGHTNESS, ...); 704*4882a593Smuzhiyun v4l2_ctrl_new_std(&video_ctrl_handler, &ops, V4L2_CID_CONTRAST, ...); 705*4882a593Smuzhiyun 706*4882a593SmuzhiyunWhat you should not do is make two identical controls for two handlers. 707*4882a593SmuzhiyunFor example: 708*4882a593Smuzhiyun 709*4882a593Smuzhiyun.. code-block:: c 710*4882a593Smuzhiyun 711*4882a593Smuzhiyun v4l2_ctrl_new_std(&radio_ctrl_handler, &radio_ops, V4L2_CID_AUDIO_MUTE, ...); 712*4882a593Smuzhiyun v4l2_ctrl_new_std(&video_ctrl_handler, &video_ops, V4L2_CID_AUDIO_MUTE, ...); 713*4882a593Smuzhiyun 714*4882a593SmuzhiyunThis would be bad since muting the radio would not change the video mute 715*4882a593Smuzhiyuncontrol. The rule is to have one control for each hardware 'knob' that you 716*4882a593Smuzhiyuncan twiddle. 717*4882a593Smuzhiyun 718*4882a593Smuzhiyun 719*4882a593SmuzhiyunFinding Controls 720*4882a593Smuzhiyun---------------- 721*4882a593Smuzhiyun 722*4882a593SmuzhiyunNormally you have created the controls yourself and you can store the struct 723*4882a593Smuzhiyunv4l2_ctrl pointer into your own struct. 724*4882a593Smuzhiyun 725*4882a593SmuzhiyunBut sometimes you need to find a control from another handler that you do 726*4882a593Smuzhiyunnot own. For example, if you have to find a volume control from a subdev. 727*4882a593Smuzhiyun 728*4882a593SmuzhiyunYou can do that by calling v4l2_ctrl_find: 729*4882a593Smuzhiyun 730*4882a593Smuzhiyun.. code-block:: c 731*4882a593Smuzhiyun 732*4882a593Smuzhiyun struct v4l2_ctrl *volume; 733*4882a593Smuzhiyun 734*4882a593Smuzhiyun volume = v4l2_ctrl_find(sd->ctrl_handler, V4L2_CID_AUDIO_VOLUME); 735*4882a593Smuzhiyun 736*4882a593SmuzhiyunSince v4l2_ctrl_find will lock the handler you have to be careful where you 737*4882a593Smuzhiyunuse it. For example, this is not a good idea: 738*4882a593Smuzhiyun 739*4882a593Smuzhiyun.. code-block:: c 740*4882a593Smuzhiyun 741*4882a593Smuzhiyun struct v4l2_ctrl_handler ctrl_handler; 742*4882a593Smuzhiyun 743*4882a593Smuzhiyun v4l2_ctrl_new_std(&ctrl_handler, &video_ops, V4L2_CID_BRIGHTNESS, ...); 744*4882a593Smuzhiyun v4l2_ctrl_new_std(&ctrl_handler, &video_ops, V4L2_CID_CONTRAST, ...); 745*4882a593Smuzhiyun 746*4882a593Smuzhiyun...and in video_ops.s_ctrl: 747*4882a593Smuzhiyun 748*4882a593Smuzhiyun.. code-block:: c 749*4882a593Smuzhiyun 750*4882a593Smuzhiyun case V4L2_CID_BRIGHTNESS: 751*4882a593Smuzhiyun contrast = v4l2_find_ctrl(&ctrl_handler, V4L2_CID_CONTRAST); 752*4882a593Smuzhiyun ... 753*4882a593Smuzhiyun 754*4882a593SmuzhiyunWhen s_ctrl is called by the framework the ctrl_handler.lock is already taken, so 755*4882a593Smuzhiyunattempting to find another control from the same handler will deadlock. 756*4882a593Smuzhiyun 757*4882a593SmuzhiyunIt is recommended not to use this function from inside the control ops. 758*4882a593Smuzhiyun 759*4882a593Smuzhiyun 760*4882a593SmuzhiyunPreventing Controls inheritance 761*4882a593Smuzhiyun------------------------------- 762*4882a593Smuzhiyun 763*4882a593SmuzhiyunWhen one control handler is added to another using v4l2_ctrl_add_handler, then 764*4882a593Smuzhiyunby default all controls from one are merged to the other. But a subdev might 765*4882a593Smuzhiyunhave low-level controls that make sense for some advanced embedded system, but 766*4882a593Smuzhiyunnot when it is used in consumer-level hardware. In that case you want to keep 767*4882a593Smuzhiyunthose low-level controls local to the subdev. You can do this by simply 768*4882a593Smuzhiyunsetting the 'is_private' flag of the control to 1: 769*4882a593Smuzhiyun 770*4882a593Smuzhiyun.. code-block:: c 771*4882a593Smuzhiyun 772*4882a593Smuzhiyun static const struct v4l2_ctrl_config ctrl_private = { 773*4882a593Smuzhiyun .ops = &ctrl_custom_ops, 774*4882a593Smuzhiyun .id = V4L2_CID_..., 775*4882a593Smuzhiyun .name = "Some Private Control", 776*4882a593Smuzhiyun .type = V4L2_CTRL_TYPE_INTEGER, 777*4882a593Smuzhiyun .max = 15, 778*4882a593Smuzhiyun .step = 1, 779*4882a593Smuzhiyun .is_private = 1, 780*4882a593Smuzhiyun }; 781*4882a593Smuzhiyun 782*4882a593Smuzhiyun ctrl = v4l2_ctrl_new_custom(&foo->ctrl_handler, &ctrl_private, NULL); 783*4882a593Smuzhiyun 784*4882a593SmuzhiyunThese controls will now be skipped when v4l2_ctrl_add_handler is called. 785*4882a593Smuzhiyun 786*4882a593Smuzhiyun 787*4882a593SmuzhiyunV4L2_CTRL_TYPE_CTRL_CLASS Controls 788*4882a593Smuzhiyun---------------------------------- 789*4882a593Smuzhiyun 790*4882a593SmuzhiyunControls of this type can be used by GUIs to get the name of the control class. 791*4882a593SmuzhiyunA fully featured GUI can make a dialog with multiple tabs with each tab 792*4882a593Smuzhiyuncontaining the controls belonging to a particular control class. The name of 793*4882a593Smuzhiyuneach tab can be found by querying a special control with ID <control class | 1>. 794*4882a593Smuzhiyun 795*4882a593SmuzhiyunDrivers do not have to care about this. The framework will automatically add 796*4882a593Smuzhiyuna control of this type whenever the first control belonging to a new control 797*4882a593Smuzhiyunclass is added. 798*4882a593Smuzhiyun 799*4882a593Smuzhiyun 800*4882a593SmuzhiyunAdding Notify Callbacks 801*4882a593Smuzhiyun----------------------- 802*4882a593Smuzhiyun 803*4882a593SmuzhiyunSometimes the platform or bridge driver needs to be notified when a control 804*4882a593Smuzhiyunfrom a sub-device driver changes. You can set a notify callback by calling 805*4882a593Smuzhiyunthis function: 806*4882a593Smuzhiyun 807*4882a593Smuzhiyun.. code-block:: c 808*4882a593Smuzhiyun 809*4882a593Smuzhiyun void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, 810*4882a593Smuzhiyun void (*notify)(struct v4l2_ctrl *ctrl, void *priv), void *priv); 811*4882a593Smuzhiyun 812*4882a593SmuzhiyunWhenever the give control changes value the notify callback will be called 813*4882a593Smuzhiyunwith a pointer to the control and the priv pointer that was passed with 814*4882a593Smuzhiyunv4l2_ctrl_notify. Note that the control's handler lock is held when the 815*4882a593Smuzhiyunnotify function is called. 816*4882a593Smuzhiyun 817*4882a593SmuzhiyunThere can be only one notify function per control handler. Any attempt 818*4882a593Smuzhiyunto set another notify function will cause a WARN_ON. 819*4882a593Smuzhiyun 820*4882a593Smuzhiyunv4l2_ctrl functions and data structures 821*4882a593Smuzhiyun--------------------------------------- 822*4882a593Smuzhiyun 823*4882a593Smuzhiyun.. kernel-doc:: include/media/v4l2-ctrls.h 824