1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * V4L2 controls framework core implementation. 4 * 5 * Copyright (C) 2010-2021 Hans Verkuil <hverkuil-cisco@xs4all.nl> 6 */ 7 8 #include <linux/export.h> 9 #include <linux/mm.h> 10 #include <linux/slab.h> 11 #include <media/v4l2-ctrls.h> 12 #include <media/v4l2-event.h> 13 #include <media/v4l2-fwnode.h> 14 15 #include "v4l2-ctrls-priv.h" 16 17 static const union v4l2_ctrl_ptr ptr_null; 18 19 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, 20 u32 changes) 21 { 22 memset(ev, 0, sizeof(*ev)); 23 ev->type = V4L2_EVENT_CTRL; 24 ev->id = ctrl->id; 25 ev->u.ctrl.changes = changes; 26 ev->u.ctrl.type = ctrl->type; 27 ev->u.ctrl.flags = user_flags(ctrl); 28 if (ctrl->is_ptr) 29 ev->u.ctrl.value64 = 0; 30 else 31 ev->u.ctrl.value64 = *ctrl->p_cur.p_s64; 32 ev->u.ctrl.minimum = ctrl->minimum; 33 ev->u.ctrl.maximum = ctrl->maximum; 34 if (ctrl->type == V4L2_CTRL_TYPE_MENU 35 || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU) 36 ev->u.ctrl.step = 1; 37 else 38 ev->u.ctrl.step = ctrl->step; 39 ev->u.ctrl.default_value = ctrl->default_value; 40 } 41 42 void send_initial_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl) 43 { 44 struct v4l2_event ev; 45 u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; 46 47 if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)) 48 changes |= V4L2_EVENT_CTRL_CH_VALUE; 49 fill_event(&ev, ctrl, changes); 50 v4l2_event_queue_fh(fh, &ev); 51 } 52 53 void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes) 54 { 55 struct v4l2_event ev; 56 struct v4l2_subscribed_event *sev; 57 58 if (list_empty(&ctrl->ev_subs)) 59 return; 60 fill_event(&ev, ctrl, changes); 61 62 list_for_each_entry(sev, &ctrl->ev_subs, node) 63 if (sev->fh != fh || 64 (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK)) 65 v4l2_event_queue_fh(sev->fh, &ev); 66 } 67 68 static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx, 69 union v4l2_ctrl_ptr ptr1, 70 union v4l2_ctrl_ptr ptr2) 71 { 72 switch (ctrl->type) { 73 case V4L2_CTRL_TYPE_BUTTON: 74 return false; 75 case V4L2_CTRL_TYPE_STRING: 76 idx *= ctrl->elem_size; 77 /* strings are always 0-terminated */ 78 return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx); 79 case V4L2_CTRL_TYPE_INTEGER64: 80 return ptr1.p_s64[idx] == ptr2.p_s64[idx]; 81 case V4L2_CTRL_TYPE_U8: 82 return ptr1.p_u8[idx] == ptr2.p_u8[idx]; 83 case V4L2_CTRL_TYPE_U16: 84 return ptr1.p_u16[idx] == ptr2.p_u16[idx]; 85 case V4L2_CTRL_TYPE_U32: 86 return ptr1.p_u32[idx] == ptr2.p_u32[idx]; 87 default: 88 if (ctrl->is_int) 89 return ptr1.p_s32[idx] == ptr2.p_s32[idx]; 90 idx *= ctrl->elem_size; 91 return !memcmp(ptr1.p_const + idx, ptr2.p_const + idx, 92 ctrl->elem_size); 93 } 94 } 95 96 /* Default intra MPEG-2 quantisation coefficients, from the specification. */ 97 static const u8 mpeg2_intra_quant_matrix[64] = { 98 8, 16, 16, 19, 16, 19, 22, 22, 99 22, 22, 22, 22, 26, 24, 26, 27, 100 27, 27, 26, 26, 26, 26, 27, 27, 101 27, 29, 29, 29, 34, 34, 34, 29, 102 29, 29, 27, 27, 29, 29, 32, 32, 103 34, 34, 37, 38, 37, 35, 35, 34, 104 35, 38, 38, 40, 40, 40, 48, 48, 105 46, 46, 56, 56, 58, 69, 69, 83 106 }; 107 108 static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx, 109 union v4l2_ctrl_ptr ptr) 110 { 111 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence; 112 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture; 113 struct v4l2_ctrl_mpeg2_quantisation *p_mpeg2_quant; 114 struct v4l2_ctrl_vp8_frame *p_vp8_frame; 115 struct v4l2_ctrl_fwht_params *p_fwht_params; 116 void *p = ptr.p + idx * ctrl->elem_size; 117 118 if (ctrl->p_def.p_const) 119 memcpy(p, ctrl->p_def.p_const, ctrl->elem_size); 120 else 121 memset(p, 0, ctrl->elem_size); 122 123 switch ((u32)ctrl->type) { 124 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 125 p_mpeg2_sequence = p; 126 127 /* 4:2:0 */ 128 p_mpeg2_sequence->chroma_format = 1; 129 break; 130 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 131 p_mpeg2_picture = p; 132 133 /* interlaced top field */ 134 p_mpeg2_picture->picture_structure = V4L2_MPEG2_PIC_TOP_FIELD; 135 p_mpeg2_picture->picture_coding_type = 136 V4L2_MPEG2_PIC_CODING_TYPE_I; 137 break; 138 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 139 p_mpeg2_quant = p; 140 141 memcpy(p_mpeg2_quant->intra_quantiser_matrix, 142 mpeg2_intra_quant_matrix, 143 ARRAY_SIZE(mpeg2_intra_quant_matrix)); 144 /* 145 * The default non-intra MPEG-2 quantisation 146 * coefficients are all 16, as per the specification. 147 */ 148 memset(p_mpeg2_quant->non_intra_quantiser_matrix, 16, 149 sizeof(p_mpeg2_quant->non_intra_quantiser_matrix)); 150 break; 151 case V4L2_CTRL_TYPE_VP8_FRAME: 152 p_vp8_frame = p; 153 p_vp8_frame->num_dct_parts = 1; 154 break; 155 case V4L2_CTRL_TYPE_FWHT_PARAMS: 156 p_fwht_params = p; 157 p_fwht_params->version = V4L2_FWHT_VERSION; 158 p_fwht_params->width = 1280; 159 p_fwht_params->height = 720; 160 p_fwht_params->flags = V4L2_FWHT_FL_PIXENC_YUV | 161 (2 << V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET); 162 break; 163 } 164 } 165 166 static void std_init(const struct v4l2_ctrl *ctrl, u32 idx, 167 union v4l2_ctrl_ptr ptr) 168 { 169 switch (ctrl->type) { 170 case V4L2_CTRL_TYPE_STRING: 171 idx *= ctrl->elem_size; 172 memset(ptr.p_char + idx, ' ', ctrl->minimum); 173 ptr.p_char[idx + ctrl->minimum] = '\0'; 174 break; 175 case V4L2_CTRL_TYPE_INTEGER64: 176 ptr.p_s64[idx] = ctrl->default_value; 177 break; 178 case V4L2_CTRL_TYPE_INTEGER: 179 case V4L2_CTRL_TYPE_INTEGER_MENU: 180 case V4L2_CTRL_TYPE_MENU: 181 case V4L2_CTRL_TYPE_BITMASK: 182 case V4L2_CTRL_TYPE_BOOLEAN: 183 ptr.p_s32[idx] = ctrl->default_value; 184 break; 185 case V4L2_CTRL_TYPE_BUTTON: 186 case V4L2_CTRL_TYPE_CTRL_CLASS: 187 ptr.p_s32[idx] = 0; 188 break; 189 case V4L2_CTRL_TYPE_U8: 190 ptr.p_u8[idx] = ctrl->default_value; 191 break; 192 case V4L2_CTRL_TYPE_U16: 193 ptr.p_u16[idx] = ctrl->default_value; 194 break; 195 case V4L2_CTRL_TYPE_U32: 196 ptr.p_u32[idx] = ctrl->default_value; 197 break; 198 default: 199 std_init_compound(ctrl, idx, ptr); 200 break; 201 } 202 } 203 204 static void std_log(const struct v4l2_ctrl *ctrl) 205 { 206 union v4l2_ctrl_ptr ptr = ctrl->p_cur; 207 208 if (ctrl->is_array) { 209 unsigned i; 210 211 for (i = 0; i < ctrl->nr_of_dims; i++) 212 pr_cont("[%u]", ctrl->dims[i]); 213 pr_cont(" "); 214 } 215 216 switch (ctrl->type) { 217 case V4L2_CTRL_TYPE_INTEGER: 218 pr_cont("%d", *ptr.p_s32); 219 break; 220 case V4L2_CTRL_TYPE_BOOLEAN: 221 pr_cont("%s", *ptr.p_s32 ? "true" : "false"); 222 break; 223 case V4L2_CTRL_TYPE_MENU: 224 pr_cont("%s", ctrl->qmenu[*ptr.p_s32]); 225 break; 226 case V4L2_CTRL_TYPE_INTEGER_MENU: 227 pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]); 228 break; 229 case V4L2_CTRL_TYPE_BITMASK: 230 pr_cont("0x%08x", *ptr.p_s32); 231 break; 232 case V4L2_CTRL_TYPE_INTEGER64: 233 pr_cont("%lld", *ptr.p_s64); 234 break; 235 case V4L2_CTRL_TYPE_STRING: 236 pr_cont("%s", ptr.p_char); 237 break; 238 case V4L2_CTRL_TYPE_U8: 239 pr_cont("%u", (unsigned)*ptr.p_u8); 240 break; 241 case V4L2_CTRL_TYPE_U16: 242 pr_cont("%u", (unsigned)*ptr.p_u16); 243 break; 244 case V4L2_CTRL_TYPE_U32: 245 pr_cont("%u", (unsigned)*ptr.p_u32); 246 break; 247 case V4L2_CTRL_TYPE_H264_SPS: 248 pr_cont("H264_SPS"); 249 break; 250 case V4L2_CTRL_TYPE_H264_PPS: 251 pr_cont("H264_PPS"); 252 break; 253 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 254 pr_cont("H264_SCALING_MATRIX"); 255 break; 256 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS: 257 pr_cont("H264_SLICE_PARAMS"); 258 break; 259 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS: 260 pr_cont("H264_DECODE_PARAMS"); 261 break; 262 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS: 263 pr_cont("H264_PRED_WEIGHTS"); 264 break; 265 case V4L2_CTRL_TYPE_FWHT_PARAMS: 266 pr_cont("FWHT_PARAMS"); 267 break; 268 case V4L2_CTRL_TYPE_VP8_FRAME: 269 pr_cont("VP8_FRAME"); 270 break; 271 case V4L2_CTRL_TYPE_HDR10_CLL_INFO: 272 pr_cont("HDR10_CLL_INFO"); 273 break; 274 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY: 275 pr_cont("HDR10_MASTERING_DISPLAY"); 276 break; 277 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 278 pr_cont("MPEG2_QUANTISATION"); 279 break; 280 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 281 pr_cont("MPEG2_SEQUENCE"); 282 break; 283 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 284 pr_cont("MPEG2_PICTURE"); 285 break; 286 default: 287 pr_cont("unknown type %d", ctrl->type); 288 break; 289 } 290 } 291 292 /* 293 * Round towards the closest legal value. Be careful when we are 294 * close to the maximum range of the control type to prevent 295 * wrap-arounds. 296 */ 297 #define ROUND_TO_RANGE(val, offset_type, ctrl) \ 298 ({ \ 299 offset_type offset; \ 300 if ((ctrl)->maximum >= 0 && \ 301 val >= (ctrl)->maximum - (s32)((ctrl)->step / 2)) \ 302 val = (ctrl)->maximum; \ 303 else \ 304 val += (s32)((ctrl)->step / 2); \ 305 val = clamp_t(typeof(val), val, \ 306 (ctrl)->minimum, (ctrl)->maximum); \ 307 offset = (val) - (ctrl)->minimum; \ 308 offset = (ctrl)->step * (offset / (u32)(ctrl)->step); \ 309 val = (ctrl)->minimum + offset; \ 310 0; \ 311 }) 312 313 /* Validate a new control */ 314 315 #define zero_padding(s) \ 316 memset(&(s).padding, 0, sizeof((s).padding)) 317 #define zero_reserved(s) \ 318 memset(&(s).reserved, 0, sizeof((s).reserved)) 319 320 /* 321 * Compound controls validation requires setting unused fields/flags to zero 322 * in order to properly detect unchanged controls with std_equal's memcmp. 323 */ 324 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx, 325 union v4l2_ctrl_ptr ptr) 326 { 327 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence; 328 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture; 329 struct v4l2_ctrl_vp8_frame *p_vp8_frame; 330 struct v4l2_ctrl_fwht_params *p_fwht_params; 331 struct v4l2_ctrl_h264_sps *p_h264_sps; 332 struct v4l2_ctrl_h264_pps *p_h264_pps; 333 struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights; 334 struct v4l2_ctrl_h264_slice_params *p_h264_slice_params; 335 struct v4l2_ctrl_h264_decode_params *p_h264_dec_params; 336 struct v4l2_ctrl_hevc_sps *p_hevc_sps; 337 struct v4l2_ctrl_hevc_pps *p_hevc_pps; 338 struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params; 339 struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering; 340 struct v4l2_area *area; 341 void *p = ptr.p + idx * ctrl->elem_size; 342 unsigned int i; 343 344 switch ((u32)ctrl->type) { 345 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 346 p_mpeg2_sequence = p; 347 348 switch (p_mpeg2_sequence->chroma_format) { 349 case 1: /* 4:2:0 */ 350 case 2: /* 4:2:2 */ 351 case 3: /* 4:4:4 */ 352 break; 353 default: 354 return -EINVAL; 355 } 356 break; 357 358 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 359 p_mpeg2_picture = p; 360 361 switch (p_mpeg2_picture->intra_dc_precision) { 362 case 0: /* 8 bits */ 363 case 1: /* 9 bits */ 364 case 2: /* 10 bits */ 365 case 3: /* 11 bits */ 366 break; 367 default: 368 return -EINVAL; 369 } 370 371 switch (p_mpeg2_picture->picture_structure) { 372 case V4L2_MPEG2_PIC_TOP_FIELD: 373 case V4L2_MPEG2_PIC_BOTTOM_FIELD: 374 case V4L2_MPEG2_PIC_FRAME: 375 break; 376 default: 377 return -EINVAL; 378 } 379 380 switch (p_mpeg2_picture->picture_coding_type) { 381 case V4L2_MPEG2_PIC_CODING_TYPE_I: 382 case V4L2_MPEG2_PIC_CODING_TYPE_P: 383 case V4L2_MPEG2_PIC_CODING_TYPE_B: 384 break; 385 default: 386 return -EINVAL; 387 } 388 zero_reserved(*p_mpeg2_picture); 389 break; 390 391 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 392 break; 393 394 case V4L2_CTRL_TYPE_FWHT_PARAMS: 395 p_fwht_params = p; 396 if (p_fwht_params->version < V4L2_FWHT_VERSION) 397 return -EINVAL; 398 if (!p_fwht_params->width || !p_fwht_params->height) 399 return -EINVAL; 400 break; 401 402 case V4L2_CTRL_TYPE_H264_SPS: 403 p_h264_sps = p; 404 405 /* Some syntax elements are only conditionally valid */ 406 if (p_h264_sps->pic_order_cnt_type != 0) { 407 p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0; 408 } else if (p_h264_sps->pic_order_cnt_type != 1) { 409 p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0; 410 p_h264_sps->offset_for_non_ref_pic = 0; 411 p_h264_sps->offset_for_top_to_bottom_field = 0; 412 memset(&p_h264_sps->offset_for_ref_frame, 0, 413 sizeof(p_h264_sps->offset_for_ref_frame)); 414 } 415 416 if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) { 417 p_h264_sps->chroma_format_idc = 1; 418 p_h264_sps->bit_depth_luma_minus8 = 0; 419 p_h264_sps->bit_depth_chroma_minus8 = 0; 420 421 p_h264_sps->flags &= 422 ~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS; 423 424 if (p_h264_sps->chroma_format_idc < 3) 425 p_h264_sps->flags &= 426 ~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE; 427 } 428 429 if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY) 430 p_h264_sps->flags &= 431 ~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD; 432 433 /* 434 * Chroma 4:2:2 format require at least High 4:2:2 profile. 435 * 436 * The H264 specification and well-known parser implementations 437 * use profile-idc values directly, as that is clearer and 438 * less ambiguous. We do the same here. 439 */ 440 if (p_h264_sps->profile_idc < 122 && 441 p_h264_sps->chroma_format_idc > 1) 442 return -EINVAL; 443 /* Chroma 4:4:4 format require at least High 4:2:2 profile */ 444 if (p_h264_sps->profile_idc < 244 && 445 p_h264_sps->chroma_format_idc > 2) 446 return -EINVAL; 447 if (p_h264_sps->chroma_format_idc > 3) 448 return -EINVAL; 449 450 if (p_h264_sps->bit_depth_luma_minus8 > 6) 451 return -EINVAL; 452 if (p_h264_sps->bit_depth_chroma_minus8 > 6) 453 return -EINVAL; 454 if (p_h264_sps->log2_max_frame_num_minus4 > 12) 455 return -EINVAL; 456 if (p_h264_sps->pic_order_cnt_type > 2) 457 return -EINVAL; 458 if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12) 459 return -EINVAL; 460 if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN) 461 return -EINVAL; 462 break; 463 464 case V4L2_CTRL_TYPE_H264_PPS: 465 p_h264_pps = p; 466 467 if (p_h264_pps->num_slice_groups_minus1 > 7) 468 return -EINVAL; 469 if (p_h264_pps->num_ref_idx_l0_default_active_minus1 > 470 (V4L2_H264_REF_LIST_LEN - 1)) 471 return -EINVAL; 472 if (p_h264_pps->num_ref_idx_l1_default_active_minus1 > 473 (V4L2_H264_REF_LIST_LEN - 1)) 474 return -EINVAL; 475 if (p_h264_pps->weighted_bipred_idc > 2) 476 return -EINVAL; 477 /* 478 * pic_init_qp_minus26 shall be in the range of 479 * -(26 + QpBdOffset_y) to +25, inclusive, 480 * where QpBdOffset_y is 6 * bit_depth_luma_minus8 481 */ 482 if (p_h264_pps->pic_init_qp_minus26 < -62 || 483 p_h264_pps->pic_init_qp_minus26 > 25) 484 return -EINVAL; 485 if (p_h264_pps->pic_init_qs_minus26 < -26 || 486 p_h264_pps->pic_init_qs_minus26 > 25) 487 return -EINVAL; 488 if (p_h264_pps->chroma_qp_index_offset < -12 || 489 p_h264_pps->chroma_qp_index_offset > 12) 490 return -EINVAL; 491 if (p_h264_pps->second_chroma_qp_index_offset < -12 || 492 p_h264_pps->second_chroma_qp_index_offset > 12) 493 return -EINVAL; 494 break; 495 496 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 497 break; 498 499 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS: 500 p_h264_pred_weights = p; 501 502 if (p_h264_pred_weights->luma_log2_weight_denom > 7) 503 return -EINVAL; 504 if (p_h264_pred_weights->chroma_log2_weight_denom > 7) 505 return -EINVAL; 506 break; 507 508 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS: 509 p_h264_slice_params = p; 510 511 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B) 512 p_h264_slice_params->flags &= 513 ~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED; 514 515 if (p_h264_slice_params->colour_plane_id > 2) 516 return -EINVAL; 517 if (p_h264_slice_params->cabac_init_idc > 2) 518 return -EINVAL; 519 if (p_h264_slice_params->disable_deblocking_filter_idc > 2) 520 return -EINVAL; 521 if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 || 522 p_h264_slice_params->slice_alpha_c0_offset_div2 > 6) 523 return -EINVAL; 524 if (p_h264_slice_params->slice_beta_offset_div2 < -6 || 525 p_h264_slice_params->slice_beta_offset_div2 > 6) 526 return -EINVAL; 527 528 if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I || 529 p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI) 530 p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0; 531 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B) 532 p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0; 533 534 if (p_h264_slice_params->num_ref_idx_l0_active_minus1 > 535 (V4L2_H264_REF_LIST_LEN - 1)) 536 return -EINVAL; 537 if (p_h264_slice_params->num_ref_idx_l1_active_minus1 > 538 (V4L2_H264_REF_LIST_LEN - 1)) 539 return -EINVAL; 540 zero_reserved(*p_h264_slice_params); 541 break; 542 543 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS: 544 p_h264_dec_params = p; 545 546 if (p_h264_dec_params->nal_ref_idc > 3) 547 return -EINVAL; 548 for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) { 549 struct v4l2_h264_dpb_entry *dpb_entry = 550 &p_h264_dec_params->dpb[i]; 551 552 zero_reserved(*dpb_entry); 553 } 554 zero_reserved(*p_h264_dec_params); 555 break; 556 557 case V4L2_CTRL_TYPE_VP8_FRAME: 558 p_vp8_frame = p; 559 560 switch (p_vp8_frame->num_dct_parts) { 561 case 1: 562 case 2: 563 case 4: 564 case 8: 565 break; 566 default: 567 return -EINVAL; 568 } 569 zero_padding(p_vp8_frame->segment); 570 zero_padding(p_vp8_frame->lf); 571 zero_padding(p_vp8_frame->quant); 572 zero_padding(p_vp8_frame->entropy); 573 zero_padding(p_vp8_frame->coder_state); 574 break; 575 576 case V4L2_CTRL_TYPE_HEVC_SPS: 577 p_hevc_sps = p; 578 579 if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) { 580 p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0; 581 p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0; 582 p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0; 583 p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0; 584 } 585 586 if (!(p_hevc_sps->flags & 587 V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT)) 588 p_hevc_sps->num_long_term_ref_pics_sps = 0; 589 break; 590 591 case V4L2_CTRL_TYPE_HEVC_PPS: 592 p_hevc_pps = p; 593 594 if (!(p_hevc_pps->flags & 595 V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED)) 596 p_hevc_pps->diff_cu_qp_delta_depth = 0; 597 598 if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) { 599 p_hevc_pps->num_tile_columns_minus1 = 0; 600 p_hevc_pps->num_tile_rows_minus1 = 0; 601 memset(&p_hevc_pps->column_width_minus1, 0, 602 sizeof(p_hevc_pps->column_width_minus1)); 603 memset(&p_hevc_pps->row_height_minus1, 0, 604 sizeof(p_hevc_pps->row_height_minus1)); 605 606 p_hevc_pps->flags &= 607 ~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED; 608 } 609 610 if (p_hevc_pps->flags & 611 V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) { 612 p_hevc_pps->pps_beta_offset_div2 = 0; 613 p_hevc_pps->pps_tc_offset_div2 = 0; 614 } 615 616 zero_padding(*p_hevc_pps); 617 break; 618 619 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS: 620 p_hevc_slice_params = p; 621 622 if (p_hevc_slice_params->num_active_dpb_entries > 623 V4L2_HEVC_DPB_ENTRIES_NUM_MAX) 624 return -EINVAL; 625 626 zero_padding(p_hevc_slice_params->pred_weight_table); 627 628 for (i = 0; i < p_hevc_slice_params->num_active_dpb_entries; 629 i++) { 630 struct v4l2_hevc_dpb_entry *dpb_entry = 631 &p_hevc_slice_params->dpb[i]; 632 633 zero_padding(*dpb_entry); 634 } 635 636 zero_padding(*p_hevc_slice_params); 637 break; 638 639 case V4L2_CTRL_TYPE_HDR10_CLL_INFO: 640 break; 641 642 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY: 643 p_hdr10_mastering = p; 644 645 for (i = 0; i < 3; ++i) { 646 if (p_hdr10_mastering->display_primaries_x[i] < 647 V4L2_HDR10_MASTERING_PRIMARIES_X_LOW || 648 p_hdr10_mastering->display_primaries_x[i] > 649 V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH || 650 p_hdr10_mastering->display_primaries_y[i] < 651 V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW || 652 p_hdr10_mastering->display_primaries_y[i] > 653 V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH) 654 return -EINVAL; 655 } 656 657 if (p_hdr10_mastering->white_point_x < 658 V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW || 659 p_hdr10_mastering->white_point_x > 660 V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH || 661 p_hdr10_mastering->white_point_y < 662 V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW || 663 p_hdr10_mastering->white_point_y > 664 V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH) 665 return -EINVAL; 666 667 if (p_hdr10_mastering->max_display_mastering_luminance < 668 V4L2_HDR10_MASTERING_MAX_LUMA_LOW || 669 p_hdr10_mastering->max_display_mastering_luminance > 670 V4L2_HDR10_MASTERING_MAX_LUMA_HIGH || 671 p_hdr10_mastering->min_display_mastering_luminance < 672 V4L2_HDR10_MASTERING_MIN_LUMA_LOW || 673 p_hdr10_mastering->min_display_mastering_luminance > 674 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH) 675 return -EINVAL; 676 677 /* The following restriction comes from ITU-T Rec. H.265 spec */ 678 if (p_hdr10_mastering->max_display_mastering_luminance == 679 V4L2_HDR10_MASTERING_MAX_LUMA_LOW && 680 p_hdr10_mastering->min_display_mastering_luminance == 681 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH) 682 return -EINVAL; 683 684 break; 685 686 case V4L2_CTRL_TYPE_AREA: 687 area = p; 688 if (!area->width || !area->height) 689 return -EINVAL; 690 break; 691 692 default: 693 return -EINVAL; 694 } 695 696 return 0; 697 } 698 699 static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx, 700 union v4l2_ctrl_ptr ptr) 701 { 702 size_t len; 703 u64 offset; 704 s64 val; 705 706 switch ((u32)ctrl->type) { 707 case V4L2_CTRL_TYPE_INTEGER: 708 return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl); 709 case V4L2_CTRL_TYPE_INTEGER64: 710 /* 711 * We can't use the ROUND_TO_RANGE define here due to 712 * the u64 divide that needs special care. 713 */ 714 val = ptr.p_s64[idx]; 715 if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2)) 716 val = ctrl->maximum; 717 else 718 val += (s64)(ctrl->step / 2); 719 val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum); 720 offset = val - ctrl->minimum; 721 do_div(offset, ctrl->step); 722 ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step; 723 return 0; 724 case V4L2_CTRL_TYPE_U8: 725 return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl); 726 case V4L2_CTRL_TYPE_U16: 727 return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl); 728 case V4L2_CTRL_TYPE_U32: 729 return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl); 730 731 case V4L2_CTRL_TYPE_BOOLEAN: 732 ptr.p_s32[idx] = !!ptr.p_s32[idx]; 733 return 0; 734 735 case V4L2_CTRL_TYPE_MENU: 736 case V4L2_CTRL_TYPE_INTEGER_MENU: 737 if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum) 738 return -ERANGE; 739 if (ptr.p_s32[idx] < BITS_PER_LONG_LONG && 740 (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx]))) 741 return -EINVAL; 742 if (ctrl->type == V4L2_CTRL_TYPE_MENU && 743 ctrl->qmenu[ptr.p_s32[idx]][0] == '\0') 744 return -EINVAL; 745 return 0; 746 747 case V4L2_CTRL_TYPE_BITMASK: 748 ptr.p_s32[idx] &= ctrl->maximum; 749 return 0; 750 751 case V4L2_CTRL_TYPE_BUTTON: 752 case V4L2_CTRL_TYPE_CTRL_CLASS: 753 ptr.p_s32[idx] = 0; 754 return 0; 755 756 case V4L2_CTRL_TYPE_STRING: 757 idx *= ctrl->elem_size; 758 len = strlen(ptr.p_char + idx); 759 if (len < ctrl->minimum) 760 return -ERANGE; 761 if ((len - (u32)ctrl->minimum) % (u32)ctrl->step) 762 return -ERANGE; 763 return 0; 764 765 default: 766 return std_validate_compound(ctrl, idx, ptr); 767 } 768 } 769 770 static const struct v4l2_ctrl_type_ops std_type_ops = { 771 .equal = std_equal, 772 .init = std_init, 773 .log = std_log, 774 .validate = std_validate, 775 }; 776 777 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv) 778 { 779 if (!ctrl) 780 return; 781 if (!notify) { 782 ctrl->call_notify = 0; 783 return; 784 } 785 if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify)) 786 return; 787 ctrl->handler->notify = notify; 788 ctrl->handler->notify_priv = priv; 789 ctrl->call_notify = 1; 790 } 791 EXPORT_SYMBOL(v4l2_ctrl_notify); 792 793 /* Copy the one value to another. */ 794 static void ptr_to_ptr(struct v4l2_ctrl *ctrl, 795 union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to) 796 { 797 if (ctrl == NULL) 798 return; 799 memcpy(to.p, from.p_const, ctrl->elems * ctrl->elem_size); 800 } 801 802 /* Copy the new value to the current value. */ 803 void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags) 804 { 805 bool changed; 806 807 if (ctrl == NULL) 808 return; 809 810 /* has_changed is set by cluster_changed */ 811 changed = ctrl->has_changed; 812 if (changed) 813 ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur); 814 815 if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) { 816 /* Note: CH_FLAGS is only set for auto clusters. */ 817 ctrl->flags &= 818 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE); 819 if (!is_cur_manual(ctrl->cluster[0])) { 820 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 821 if (ctrl->cluster[0]->has_volatiles) 822 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 823 } 824 fh = NULL; 825 } 826 if (changed || ch_flags) { 827 /* If a control was changed that was not one of the controls 828 modified by the application, then send the event to all. */ 829 if (!ctrl->is_new) 830 fh = NULL; 831 send_event(fh, ctrl, 832 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags); 833 if (ctrl->call_notify && changed && ctrl->handler->notify) 834 ctrl->handler->notify(ctrl, ctrl->handler->notify_priv); 835 } 836 } 837 838 /* Copy the current value to the new value */ 839 void cur_to_new(struct v4l2_ctrl *ctrl) 840 { 841 if (ctrl == NULL) 842 return; 843 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new); 844 } 845 846 /* Copy the new value to the request value */ 847 void new_to_req(struct v4l2_ctrl_ref *ref) 848 { 849 if (!ref) 850 return; 851 ptr_to_ptr(ref->ctrl, ref->ctrl->p_new, ref->p_req); 852 ref->valid_p_req = true; 853 } 854 855 /* Copy the current value to the request value */ 856 void cur_to_req(struct v4l2_ctrl_ref *ref) 857 { 858 if (!ref) 859 return; 860 ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->p_req); 861 ref->valid_p_req = true; 862 } 863 864 /* Copy the request value to the new value */ 865 void req_to_new(struct v4l2_ctrl_ref *ref) 866 { 867 if (!ref) 868 return; 869 if (ref->valid_p_req) 870 ptr_to_ptr(ref->ctrl, ref->p_req, ref->ctrl->p_new); 871 else 872 ptr_to_ptr(ref->ctrl, ref->ctrl->p_cur, ref->ctrl->p_new); 873 } 874 875 /* Control range checking */ 876 int check_range(enum v4l2_ctrl_type type, 877 s64 min, s64 max, u64 step, s64 def) 878 { 879 switch (type) { 880 case V4L2_CTRL_TYPE_BOOLEAN: 881 if (step != 1 || max > 1 || min < 0) 882 return -ERANGE; 883 fallthrough; 884 case V4L2_CTRL_TYPE_U8: 885 case V4L2_CTRL_TYPE_U16: 886 case V4L2_CTRL_TYPE_U32: 887 case V4L2_CTRL_TYPE_INTEGER: 888 case V4L2_CTRL_TYPE_INTEGER64: 889 if (step == 0 || min > max || def < min || def > max) 890 return -ERANGE; 891 return 0; 892 case V4L2_CTRL_TYPE_BITMASK: 893 if (step || min || !max || (def & ~max)) 894 return -ERANGE; 895 return 0; 896 case V4L2_CTRL_TYPE_MENU: 897 case V4L2_CTRL_TYPE_INTEGER_MENU: 898 if (min > max || def < min || def > max) 899 return -ERANGE; 900 /* Note: step == menu_skip_mask for menu controls. 901 So here we check if the default value is masked out. */ 902 if (step && ((1 << def) & step)) 903 return -EINVAL; 904 return 0; 905 case V4L2_CTRL_TYPE_STRING: 906 if (min > max || min < 0 || step < 1 || def) 907 return -ERANGE; 908 return 0; 909 default: 910 return 0; 911 } 912 } 913 914 /* Validate a new control */ 915 int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new) 916 { 917 unsigned idx; 918 int err = 0; 919 920 for (idx = 0; !err && idx < ctrl->elems; idx++) 921 err = ctrl->type_ops->validate(ctrl, idx, p_new); 922 return err; 923 } 924 925 /* Set the handler's error code if it wasn't set earlier already */ 926 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err) 927 { 928 if (hdl->error == 0) 929 hdl->error = err; 930 return err; 931 } 932 933 /* Initialize the handler */ 934 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, 935 unsigned nr_of_controls_hint, 936 struct lock_class_key *key, const char *name) 937 { 938 mutex_init(&hdl->_lock); 939 hdl->lock = &hdl->_lock; 940 lockdep_set_class_and_name(hdl->lock, key, name); 941 INIT_LIST_HEAD(&hdl->ctrls); 942 INIT_LIST_HEAD(&hdl->ctrl_refs); 943 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8; 944 hdl->buckets = kvmalloc_array(hdl->nr_of_buckets, 945 sizeof(hdl->buckets[0]), 946 GFP_KERNEL | __GFP_ZERO); 947 hdl->error = hdl->buckets ? 0 : -ENOMEM; 948 v4l2_ctrl_handler_init_request(hdl); 949 return hdl->error; 950 } 951 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class); 952 953 /* Free all controls and control refs */ 954 void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) 955 { 956 struct v4l2_ctrl_ref *ref, *next_ref; 957 struct v4l2_ctrl *ctrl, *next_ctrl; 958 struct v4l2_subscribed_event *sev, *next_sev; 959 960 if (hdl == NULL || hdl->buckets == NULL) 961 return; 962 963 v4l2_ctrl_handler_free_request(hdl); 964 965 mutex_lock(hdl->lock); 966 /* Free all nodes */ 967 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) { 968 list_del(&ref->node); 969 kfree(ref); 970 } 971 /* Free all controls owned by the handler */ 972 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) { 973 list_del(&ctrl->node); 974 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node) 975 list_del(&sev->node); 976 kvfree(ctrl); 977 } 978 kvfree(hdl->buckets); 979 hdl->buckets = NULL; 980 hdl->cached = NULL; 981 hdl->error = 0; 982 mutex_unlock(hdl->lock); 983 mutex_destroy(&hdl->_lock); 984 } 985 EXPORT_SYMBOL(v4l2_ctrl_handler_free); 986 987 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer 988 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing 989 with applications that do not use the NEXT_CTRL flag. 990 991 We just find the n-th private user control. It's O(N), but that should not 992 be an issue in this particular case. */ 993 static struct v4l2_ctrl_ref *find_private_ref( 994 struct v4l2_ctrl_handler *hdl, u32 id) 995 { 996 struct v4l2_ctrl_ref *ref; 997 998 id -= V4L2_CID_PRIVATE_BASE; 999 list_for_each_entry(ref, &hdl->ctrl_refs, node) { 1000 /* Search for private user controls that are compatible with 1001 VIDIOC_G/S_CTRL. */ 1002 if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER && 1003 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) { 1004 if (!ref->ctrl->is_int) 1005 continue; 1006 if (id == 0) 1007 return ref; 1008 id--; 1009 } 1010 } 1011 return NULL; 1012 } 1013 1014 /* Find a control with the given ID. */ 1015 struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id) 1016 { 1017 struct v4l2_ctrl_ref *ref; 1018 int bucket; 1019 1020 id &= V4L2_CTRL_ID_MASK; 1021 1022 /* Old-style private controls need special handling */ 1023 if (id >= V4L2_CID_PRIVATE_BASE) 1024 return find_private_ref(hdl, id); 1025 bucket = id % hdl->nr_of_buckets; 1026 1027 /* Simple optimization: cache the last control found */ 1028 if (hdl->cached && hdl->cached->ctrl->id == id) 1029 return hdl->cached; 1030 1031 /* Not in cache, search the hash */ 1032 ref = hdl->buckets ? hdl->buckets[bucket] : NULL; 1033 while (ref && ref->ctrl->id != id) 1034 ref = ref->next; 1035 1036 if (ref) 1037 hdl->cached = ref; /* cache it! */ 1038 return ref; 1039 } 1040 1041 /* Find a control with the given ID. Take the handler's lock first. */ 1042 struct v4l2_ctrl_ref *find_ref_lock(struct v4l2_ctrl_handler *hdl, u32 id) 1043 { 1044 struct v4l2_ctrl_ref *ref = NULL; 1045 1046 if (hdl) { 1047 mutex_lock(hdl->lock); 1048 ref = find_ref(hdl, id); 1049 mutex_unlock(hdl->lock); 1050 } 1051 return ref; 1052 } 1053 1054 /* Find a control with the given ID. */ 1055 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id) 1056 { 1057 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id); 1058 1059 return ref ? ref->ctrl : NULL; 1060 } 1061 EXPORT_SYMBOL(v4l2_ctrl_find); 1062 1063 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */ 1064 int handler_new_ref(struct v4l2_ctrl_handler *hdl, 1065 struct v4l2_ctrl *ctrl, 1066 struct v4l2_ctrl_ref **ctrl_ref, 1067 bool from_other_dev, bool allocate_req) 1068 { 1069 struct v4l2_ctrl_ref *ref; 1070 struct v4l2_ctrl_ref *new_ref; 1071 u32 id = ctrl->id; 1072 u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1; 1073 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */ 1074 unsigned int size_extra_req = 0; 1075 1076 if (ctrl_ref) 1077 *ctrl_ref = NULL; 1078 1079 /* 1080 * Automatically add the control class if it is not yet present and 1081 * the new control is not a compound control. 1082 */ 1083 if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES && 1084 id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL) 1085 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0)) 1086 return hdl->error; 1087 1088 if (hdl->error) 1089 return hdl->error; 1090 1091 if (allocate_req) 1092 size_extra_req = ctrl->elems * ctrl->elem_size; 1093 new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL); 1094 if (!new_ref) 1095 return handler_set_err(hdl, -ENOMEM); 1096 new_ref->ctrl = ctrl; 1097 new_ref->from_other_dev = from_other_dev; 1098 if (size_extra_req) 1099 new_ref->p_req.p = &new_ref[1]; 1100 1101 INIT_LIST_HEAD(&new_ref->node); 1102 1103 mutex_lock(hdl->lock); 1104 1105 /* Add immediately at the end of the list if the list is empty, or if 1106 the last element in the list has a lower ID. 1107 This ensures that when elements are added in ascending order the 1108 insertion is an O(1) operation. */ 1109 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) { 1110 list_add_tail(&new_ref->node, &hdl->ctrl_refs); 1111 goto insert_in_hash; 1112 } 1113 1114 /* Find insert position in sorted list */ 1115 list_for_each_entry(ref, &hdl->ctrl_refs, node) { 1116 if (ref->ctrl->id < id) 1117 continue; 1118 /* Don't add duplicates */ 1119 if (ref->ctrl->id == id) { 1120 kfree(new_ref); 1121 goto unlock; 1122 } 1123 list_add(&new_ref->node, ref->node.prev); 1124 break; 1125 } 1126 1127 insert_in_hash: 1128 /* Insert the control node in the hash */ 1129 new_ref->next = hdl->buckets[bucket]; 1130 hdl->buckets[bucket] = new_ref; 1131 if (ctrl_ref) 1132 *ctrl_ref = new_ref; 1133 if (ctrl->handler == hdl) { 1134 /* By default each control starts in a cluster of its own. 1135 * new_ref->ctrl is basically a cluster array with one 1136 * element, so that's perfect to use as the cluster pointer. 1137 * But only do this for the handler that owns the control. 1138 */ 1139 ctrl->cluster = &new_ref->ctrl; 1140 ctrl->ncontrols = 1; 1141 } 1142 1143 unlock: 1144 mutex_unlock(hdl->lock); 1145 return 0; 1146 } 1147 1148 /* Add a new control */ 1149 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, 1150 const struct v4l2_ctrl_ops *ops, 1151 const struct v4l2_ctrl_type_ops *type_ops, 1152 u32 id, const char *name, enum v4l2_ctrl_type type, 1153 s64 min, s64 max, u64 step, s64 def, 1154 const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size, 1155 u32 flags, const char * const *qmenu, 1156 const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def, 1157 void *priv) 1158 { 1159 struct v4l2_ctrl *ctrl; 1160 unsigned sz_extra; 1161 unsigned nr_of_dims = 0; 1162 unsigned elems = 1; 1163 bool is_array; 1164 unsigned tot_ctrl_size; 1165 unsigned idx; 1166 void *data; 1167 int err; 1168 1169 if (hdl->error) 1170 return NULL; 1171 1172 while (dims && dims[nr_of_dims]) { 1173 elems *= dims[nr_of_dims]; 1174 nr_of_dims++; 1175 if (nr_of_dims == V4L2_CTRL_MAX_DIMS) 1176 break; 1177 } 1178 is_array = nr_of_dims > 0; 1179 1180 /* Prefill elem_size for all types handled by std_type_ops */ 1181 switch ((u32)type) { 1182 case V4L2_CTRL_TYPE_INTEGER64: 1183 elem_size = sizeof(s64); 1184 break; 1185 case V4L2_CTRL_TYPE_STRING: 1186 elem_size = max + 1; 1187 break; 1188 case V4L2_CTRL_TYPE_U8: 1189 elem_size = sizeof(u8); 1190 break; 1191 case V4L2_CTRL_TYPE_U16: 1192 elem_size = sizeof(u16); 1193 break; 1194 case V4L2_CTRL_TYPE_U32: 1195 elem_size = sizeof(u32); 1196 break; 1197 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 1198 elem_size = sizeof(struct v4l2_ctrl_mpeg2_sequence); 1199 break; 1200 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 1201 elem_size = sizeof(struct v4l2_ctrl_mpeg2_picture); 1202 break; 1203 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 1204 elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantisation); 1205 break; 1206 case V4L2_CTRL_TYPE_FWHT_PARAMS: 1207 elem_size = sizeof(struct v4l2_ctrl_fwht_params); 1208 break; 1209 case V4L2_CTRL_TYPE_H264_SPS: 1210 elem_size = sizeof(struct v4l2_ctrl_h264_sps); 1211 break; 1212 case V4L2_CTRL_TYPE_H264_PPS: 1213 elem_size = sizeof(struct v4l2_ctrl_h264_pps); 1214 break; 1215 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 1216 elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix); 1217 break; 1218 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS: 1219 elem_size = sizeof(struct v4l2_ctrl_h264_slice_params); 1220 break; 1221 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS: 1222 elem_size = sizeof(struct v4l2_ctrl_h264_decode_params); 1223 break; 1224 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS: 1225 elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights); 1226 break; 1227 case V4L2_CTRL_TYPE_VP8_FRAME: 1228 elem_size = sizeof(struct v4l2_ctrl_vp8_frame); 1229 break; 1230 case V4L2_CTRL_TYPE_HEVC_SPS: 1231 elem_size = sizeof(struct v4l2_ctrl_hevc_sps); 1232 break; 1233 case V4L2_CTRL_TYPE_HEVC_PPS: 1234 elem_size = sizeof(struct v4l2_ctrl_hevc_pps); 1235 break; 1236 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS: 1237 elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params); 1238 break; 1239 case V4L2_CTRL_TYPE_HDR10_CLL_INFO: 1240 elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info); 1241 break; 1242 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY: 1243 elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display); 1244 break; 1245 case V4L2_CTRL_TYPE_AREA: 1246 elem_size = sizeof(struct v4l2_area); 1247 break; 1248 default: 1249 if (type < V4L2_CTRL_COMPOUND_TYPES) 1250 elem_size = sizeof(s32); 1251 break; 1252 } 1253 tot_ctrl_size = elem_size * elems; 1254 1255 /* Sanity checks */ 1256 if (id == 0 || name == NULL || !elem_size || 1257 id >= V4L2_CID_PRIVATE_BASE || 1258 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) || 1259 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) { 1260 handler_set_err(hdl, -ERANGE); 1261 return NULL; 1262 } 1263 err = check_range(type, min, max, step, def); 1264 if (err) { 1265 handler_set_err(hdl, err); 1266 return NULL; 1267 } 1268 if (is_array && 1269 (type == V4L2_CTRL_TYPE_BUTTON || 1270 type == V4L2_CTRL_TYPE_CTRL_CLASS)) { 1271 handler_set_err(hdl, -EINVAL); 1272 return NULL; 1273 } 1274 1275 sz_extra = 0; 1276 if (type == V4L2_CTRL_TYPE_BUTTON) 1277 flags |= V4L2_CTRL_FLAG_WRITE_ONLY | 1278 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; 1279 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS) 1280 flags |= V4L2_CTRL_FLAG_READ_ONLY; 1281 else if (type == V4L2_CTRL_TYPE_INTEGER64 || 1282 type == V4L2_CTRL_TYPE_STRING || 1283 type >= V4L2_CTRL_COMPOUND_TYPES || 1284 is_array) 1285 sz_extra += 2 * tot_ctrl_size; 1286 1287 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) 1288 sz_extra += elem_size; 1289 1290 ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL); 1291 if (ctrl == NULL) { 1292 handler_set_err(hdl, -ENOMEM); 1293 return NULL; 1294 } 1295 1296 INIT_LIST_HEAD(&ctrl->node); 1297 INIT_LIST_HEAD(&ctrl->ev_subs); 1298 ctrl->handler = hdl; 1299 ctrl->ops = ops; 1300 ctrl->type_ops = type_ops ? type_ops : &std_type_ops; 1301 ctrl->id = id; 1302 ctrl->name = name; 1303 ctrl->type = type; 1304 ctrl->flags = flags; 1305 ctrl->minimum = min; 1306 ctrl->maximum = max; 1307 ctrl->step = step; 1308 ctrl->default_value = def; 1309 ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING; 1310 ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string; 1311 ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64; 1312 ctrl->is_array = is_array; 1313 ctrl->elems = elems; 1314 ctrl->nr_of_dims = nr_of_dims; 1315 if (nr_of_dims) 1316 memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0])); 1317 ctrl->elem_size = elem_size; 1318 if (type == V4L2_CTRL_TYPE_MENU) 1319 ctrl->qmenu = qmenu; 1320 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU) 1321 ctrl->qmenu_int = qmenu_int; 1322 ctrl->priv = priv; 1323 ctrl->cur.val = ctrl->val = def; 1324 data = &ctrl[1]; 1325 1326 if (!ctrl->is_int) { 1327 ctrl->p_new.p = data; 1328 ctrl->p_cur.p = data + tot_ctrl_size; 1329 } else { 1330 ctrl->p_new.p = &ctrl->val; 1331 ctrl->p_cur.p = &ctrl->cur.val; 1332 } 1333 1334 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) { 1335 ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size; 1336 memcpy(ctrl->p_def.p, p_def.p_const, elem_size); 1337 } 1338 1339 for (idx = 0; idx < elems; idx++) { 1340 ctrl->type_ops->init(ctrl, idx, ctrl->p_cur); 1341 ctrl->type_ops->init(ctrl, idx, ctrl->p_new); 1342 } 1343 1344 if (handler_new_ref(hdl, ctrl, NULL, false, false)) { 1345 kvfree(ctrl); 1346 return NULL; 1347 } 1348 mutex_lock(hdl->lock); 1349 list_add_tail(&ctrl->node, &hdl->ctrls); 1350 mutex_unlock(hdl->lock); 1351 return ctrl; 1352 } 1353 1354 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl, 1355 const struct v4l2_ctrl_config *cfg, void *priv) 1356 { 1357 bool is_menu; 1358 struct v4l2_ctrl *ctrl; 1359 const char *name = cfg->name; 1360 const char * const *qmenu = cfg->qmenu; 1361 const s64 *qmenu_int = cfg->qmenu_int; 1362 enum v4l2_ctrl_type type = cfg->type; 1363 u32 flags = cfg->flags; 1364 s64 min = cfg->min; 1365 s64 max = cfg->max; 1366 u64 step = cfg->step; 1367 s64 def = cfg->def; 1368 1369 if (name == NULL) 1370 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step, 1371 &def, &flags); 1372 1373 is_menu = (type == V4L2_CTRL_TYPE_MENU || 1374 type == V4L2_CTRL_TYPE_INTEGER_MENU); 1375 if (is_menu) 1376 WARN_ON(step); 1377 else 1378 WARN_ON(cfg->menu_skip_mask); 1379 if (type == V4L2_CTRL_TYPE_MENU && !qmenu) { 1380 qmenu = v4l2_ctrl_get_menu(cfg->id); 1381 } else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) { 1382 handler_set_err(hdl, -EINVAL); 1383 return NULL; 1384 } 1385 1386 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name, 1387 type, min, max, 1388 is_menu ? cfg->menu_skip_mask : step, def, 1389 cfg->dims, cfg->elem_size, 1390 flags, qmenu, qmenu_int, cfg->p_def, priv); 1391 if (ctrl) 1392 ctrl->is_private = cfg->is_private; 1393 return ctrl; 1394 } 1395 EXPORT_SYMBOL(v4l2_ctrl_new_custom); 1396 1397 /* Helper function for standard non-menu controls */ 1398 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl, 1399 const struct v4l2_ctrl_ops *ops, 1400 u32 id, s64 min, s64 max, u64 step, s64 def) 1401 { 1402 const char *name; 1403 enum v4l2_ctrl_type type; 1404 u32 flags; 1405 1406 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 1407 if (type == V4L2_CTRL_TYPE_MENU || 1408 type == V4L2_CTRL_TYPE_INTEGER_MENU || 1409 type >= V4L2_CTRL_COMPOUND_TYPES) { 1410 handler_set_err(hdl, -EINVAL); 1411 return NULL; 1412 } 1413 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 1414 min, max, step, def, NULL, 0, 1415 flags, NULL, NULL, ptr_null, NULL); 1416 } 1417 EXPORT_SYMBOL(v4l2_ctrl_new_std); 1418 1419 /* Helper function for standard menu controls */ 1420 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl, 1421 const struct v4l2_ctrl_ops *ops, 1422 u32 id, u8 _max, u64 mask, u8 _def) 1423 { 1424 const char * const *qmenu = NULL; 1425 const s64 *qmenu_int = NULL; 1426 unsigned int qmenu_int_len = 0; 1427 const char *name; 1428 enum v4l2_ctrl_type type; 1429 s64 min; 1430 s64 max = _max; 1431 s64 def = _def; 1432 u64 step; 1433 u32 flags; 1434 1435 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 1436 1437 if (type == V4L2_CTRL_TYPE_MENU) 1438 qmenu = v4l2_ctrl_get_menu(id); 1439 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU) 1440 qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len); 1441 1442 if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) { 1443 handler_set_err(hdl, -EINVAL); 1444 return NULL; 1445 } 1446 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 1447 0, max, mask, def, NULL, 0, 1448 flags, qmenu, qmenu_int, ptr_null, NULL); 1449 } 1450 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu); 1451 1452 /* Helper function for standard menu controls with driver defined menu */ 1453 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl, 1454 const struct v4l2_ctrl_ops *ops, u32 id, u8 _max, 1455 u64 mask, u8 _def, const char * const *qmenu) 1456 { 1457 enum v4l2_ctrl_type type; 1458 const char *name; 1459 u32 flags; 1460 u64 step; 1461 s64 min; 1462 s64 max = _max; 1463 s64 def = _def; 1464 1465 /* v4l2_ctrl_new_std_menu_items() should only be called for 1466 * standard controls without a standard menu. 1467 */ 1468 if (v4l2_ctrl_get_menu(id)) { 1469 handler_set_err(hdl, -EINVAL); 1470 return NULL; 1471 } 1472 1473 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 1474 if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) { 1475 handler_set_err(hdl, -EINVAL); 1476 return NULL; 1477 } 1478 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 1479 0, max, mask, def, NULL, 0, 1480 flags, qmenu, NULL, ptr_null, NULL); 1481 1482 } 1483 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items); 1484 1485 /* Helper function for standard compound controls */ 1486 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl, 1487 const struct v4l2_ctrl_ops *ops, u32 id, 1488 const union v4l2_ctrl_ptr p_def) 1489 { 1490 const char *name; 1491 enum v4l2_ctrl_type type; 1492 u32 flags; 1493 s64 min, max, step, def; 1494 1495 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 1496 if (type < V4L2_CTRL_COMPOUND_TYPES) { 1497 handler_set_err(hdl, -EINVAL); 1498 return NULL; 1499 } 1500 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 1501 min, max, step, def, NULL, 0, 1502 flags, NULL, NULL, p_def, NULL); 1503 } 1504 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound); 1505 1506 /* Helper function for standard integer menu controls */ 1507 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl, 1508 const struct v4l2_ctrl_ops *ops, 1509 u32 id, u8 _max, u8 _def, const s64 *qmenu_int) 1510 { 1511 const char *name; 1512 enum v4l2_ctrl_type type; 1513 s64 min; 1514 u64 step; 1515 s64 max = _max; 1516 s64 def = _def; 1517 u32 flags; 1518 1519 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 1520 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) { 1521 handler_set_err(hdl, -EINVAL); 1522 return NULL; 1523 } 1524 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 1525 0, max, 0, def, NULL, 0, 1526 flags, NULL, qmenu_int, ptr_null, NULL); 1527 } 1528 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu); 1529 1530 /* Add the controls from another handler to our own. */ 1531 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl, 1532 struct v4l2_ctrl_handler *add, 1533 bool (*filter)(const struct v4l2_ctrl *ctrl), 1534 bool from_other_dev) 1535 { 1536 struct v4l2_ctrl_ref *ref; 1537 int ret = 0; 1538 1539 /* Do nothing if either handler is NULL or if they are the same */ 1540 if (!hdl || !add || hdl == add) 1541 return 0; 1542 if (hdl->error) 1543 return hdl->error; 1544 mutex_lock(add->lock); 1545 list_for_each_entry(ref, &add->ctrl_refs, node) { 1546 struct v4l2_ctrl *ctrl = ref->ctrl; 1547 1548 /* Skip handler-private controls. */ 1549 if (ctrl->is_private) 1550 continue; 1551 /* And control classes */ 1552 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS) 1553 continue; 1554 /* Filter any unwanted controls */ 1555 if (filter && !filter(ctrl)) 1556 continue; 1557 ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false); 1558 if (ret) 1559 break; 1560 } 1561 mutex_unlock(add->lock); 1562 return ret; 1563 } 1564 EXPORT_SYMBOL(v4l2_ctrl_add_handler); 1565 1566 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl) 1567 { 1568 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX) 1569 return true; 1570 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX) 1571 return true; 1572 switch (ctrl->id) { 1573 case V4L2_CID_AUDIO_MUTE: 1574 case V4L2_CID_AUDIO_VOLUME: 1575 case V4L2_CID_AUDIO_BALANCE: 1576 case V4L2_CID_AUDIO_BASS: 1577 case V4L2_CID_AUDIO_TREBLE: 1578 case V4L2_CID_AUDIO_LOUDNESS: 1579 return true; 1580 default: 1581 break; 1582 } 1583 return false; 1584 } 1585 EXPORT_SYMBOL(v4l2_ctrl_radio_filter); 1586 1587 /* Cluster controls */ 1588 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls) 1589 { 1590 bool has_volatiles = false; 1591 int i; 1592 1593 /* The first control is the master control and it must not be NULL */ 1594 if (WARN_ON(ncontrols == 0 || controls[0] == NULL)) 1595 return; 1596 1597 for (i = 0; i < ncontrols; i++) { 1598 if (controls[i]) { 1599 controls[i]->cluster = controls; 1600 controls[i]->ncontrols = ncontrols; 1601 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE) 1602 has_volatiles = true; 1603 } 1604 } 1605 controls[0]->has_volatiles = has_volatiles; 1606 } 1607 EXPORT_SYMBOL(v4l2_ctrl_cluster); 1608 1609 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls, 1610 u8 manual_val, bool set_volatile) 1611 { 1612 struct v4l2_ctrl *master = controls[0]; 1613 u32 flag = 0; 1614 int i; 1615 1616 v4l2_ctrl_cluster(ncontrols, controls); 1617 WARN_ON(ncontrols <= 1); 1618 WARN_ON(manual_val < master->minimum || manual_val > master->maximum); 1619 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl)); 1620 master->is_auto = true; 1621 master->has_volatiles = set_volatile; 1622 master->manual_mode_value = manual_val; 1623 master->flags |= V4L2_CTRL_FLAG_UPDATE; 1624 1625 if (!is_cur_manual(master)) 1626 flag = V4L2_CTRL_FLAG_INACTIVE | 1627 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0); 1628 1629 for (i = 1; i < ncontrols; i++) 1630 if (controls[i]) 1631 controls[i]->flags |= flag; 1632 } 1633 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster); 1634 1635 /* 1636 * Obtain the current volatile values of an autocluster and mark them 1637 * as new. 1638 */ 1639 void update_from_auto_cluster(struct v4l2_ctrl *master) 1640 { 1641 int i; 1642 1643 for (i = 1; i < master->ncontrols; i++) 1644 cur_to_new(master->cluster[i]); 1645 if (!call_op(master, g_volatile_ctrl)) 1646 for (i = 1; i < master->ncontrols; i++) 1647 if (master->cluster[i]) 1648 master->cluster[i]->is_new = 1; 1649 } 1650 1651 /* 1652 * Return non-zero if one or more of the controls in the cluster has a new 1653 * value that differs from the current value. 1654 */ 1655 static int cluster_changed(struct v4l2_ctrl *master) 1656 { 1657 bool changed = false; 1658 unsigned int idx; 1659 int i; 1660 1661 for (i = 0; i < master->ncontrols; i++) { 1662 struct v4l2_ctrl *ctrl = master->cluster[i]; 1663 bool ctrl_changed = false; 1664 1665 if (!ctrl) 1666 continue; 1667 1668 if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE) { 1669 changed = true; 1670 ctrl_changed = true; 1671 } 1672 1673 /* 1674 * Set has_changed to false to avoid generating 1675 * the event V4L2_EVENT_CTRL_CH_VALUE 1676 */ 1677 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) { 1678 ctrl->has_changed = false; 1679 continue; 1680 } 1681 1682 for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++) 1683 ctrl_changed = !ctrl->type_ops->equal(ctrl, idx, 1684 ctrl->p_cur, ctrl->p_new); 1685 ctrl->has_changed = ctrl_changed; 1686 changed |= ctrl->has_changed; 1687 } 1688 return changed; 1689 } 1690 1691 /* 1692 * Core function that calls try/s_ctrl and ensures that the new value is 1693 * copied to the current value on a set. 1694 * Must be called with ctrl->handler->lock held. 1695 */ 1696 int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master, 1697 bool set, u32 ch_flags) 1698 { 1699 bool update_flag; 1700 int ret; 1701 int i; 1702 1703 /* 1704 * Go through the cluster and either validate the new value or 1705 * (if no new value was set), copy the current value to the new 1706 * value, ensuring a consistent view for the control ops when 1707 * called. 1708 */ 1709 for (i = 0; i < master->ncontrols; i++) { 1710 struct v4l2_ctrl *ctrl = master->cluster[i]; 1711 1712 if (!ctrl) 1713 continue; 1714 1715 if (!ctrl->is_new) { 1716 cur_to_new(ctrl); 1717 continue; 1718 } 1719 /* 1720 * Check again: it may have changed since the 1721 * previous check in try_or_set_ext_ctrls(). 1722 */ 1723 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) 1724 return -EBUSY; 1725 } 1726 1727 ret = call_op(master, try_ctrl); 1728 1729 /* Don't set if there is no change */ 1730 if (ret || !set || !cluster_changed(master)) 1731 return ret; 1732 ret = call_op(master, s_ctrl); 1733 if (ret) 1734 return ret; 1735 1736 /* If OK, then make the new values permanent. */ 1737 update_flag = is_cur_manual(master) != is_new_manual(master); 1738 1739 for (i = 0; i < master->ncontrols; i++) { 1740 /* 1741 * If we switch from auto to manual mode, and this cluster 1742 * contains volatile controls, then all non-master controls 1743 * have to be marked as changed. The 'new' value contains 1744 * the volatile value (obtained by update_from_auto_cluster), 1745 * which now has to become the current value. 1746 */ 1747 if (i && update_flag && is_new_manual(master) && 1748 master->has_volatiles && master->cluster[i]) 1749 master->cluster[i]->has_changed = true; 1750 1751 new_to_cur(fh, master->cluster[i], ch_flags | 1752 ((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0)); 1753 } 1754 return 0; 1755 } 1756 1757 /* Activate/deactivate a control. */ 1758 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active) 1759 { 1760 /* invert since the actual flag is called 'inactive' */ 1761 bool inactive = !active; 1762 bool old; 1763 1764 if (ctrl == NULL) 1765 return; 1766 1767 if (inactive) 1768 /* set V4L2_CTRL_FLAG_INACTIVE */ 1769 old = test_and_set_bit(4, &ctrl->flags); 1770 else 1771 /* clear V4L2_CTRL_FLAG_INACTIVE */ 1772 old = test_and_clear_bit(4, &ctrl->flags); 1773 if (old != inactive) 1774 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS); 1775 } 1776 EXPORT_SYMBOL(v4l2_ctrl_activate); 1777 1778 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed) 1779 { 1780 bool old; 1781 1782 if (ctrl == NULL) 1783 return; 1784 1785 lockdep_assert_held(ctrl->handler->lock); 1786 1787 if (grabbed) 1788 /* set V4L2_CTRL_FLAG_GRABBED */ 1789 old = test_and_set_bit(1, &ctrl->flags); 1790 else 1791 /* clear V4L2_CTRL_FLAG_GRABBED */ 1792 old = test_and_clear_bit(1, &ctrl->flags); 1793 if (old != grabbed) 1794 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS); 1795 } 1796 EXPORT_SYMBOL(__v4l2_ctrl_grab); 1797 1798 /* Call s_ctrl for all controls owned by the handler */ 1799 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl) 1800 { 1801 struct v4l2_ctrl *ctrl; 1802 int ret = 0; 1803 1804 if (hdl == NULL) 1805 return 0; 1806 1807 lockdep_assert_held(hdl->lock); 1808 1809 list_for_each_entry(ctrl, &hdl->ctrls, node) 1810 ctrl->done = false; 1811 1812 list_for_each_entry(ctrl, &hdl->ctrls, node) { 1813 struct v4l2_ctrl *master = ctrl->cluster[0]; 1814 int i; 1815 1816 /* Skip if this control was already handled by a cluster. */ 1817 /* Skip button controls and read-only controls. */ 1818 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON || 1819 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)) 1820 continue; 1821 1822 for (i = 0; i < master->ncontrols; i++) { 1823 if (master->cluster[i]) { 1824 cur_to_new(master->cluster[i]); 1825 master->cluster[i]->is_new = 1; 1826 master->cluster[i]->done = true; 1827 } 1828 } 1829 ret = call_op(master, s_ctrl); 1830 if (ret) 1831 break; 1832 } 1833 1834 return ret; 1835 } 1836 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup); 1837 1838 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl) 1839 { 1840 int ret; 1841 1842 if (hdl == NULL) 1843 return 0; 1844 1845 mutex_lock(hdl->lock); 1846 ret = __v4l2_ctrl_handler_setup(hdl); 1847 mutex_unlock(hdl->lock); 1848 1849 return ret; 1850 } 1851 EXPORT_SYMBOL(v4l2_ctrl_handler_setup); 1852 1853 /* Log the control name and value */ 1854 static void log_ctrl(const struct v4l2_ctrl *ctrl, 1855 const char *prefix, const char *colon) 1856 { 1857 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY)) 1858 return; 1859 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS) 1860 return; 1861 1862 pr_info("%s%s%s: ", prefix, colon, ctrl->name); 1863 1864 ctrl->type_ops->log(ctrl); 1865 1866 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE | 1867 V4L2_CTRL_FLAG_GRABBED | 1868 V4L2_CTRL_FLAG_VOLATILE)) { 1869 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE) 1870 pr_cont(" inactive"); 1871 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED) 1872 pr_cont(" grabbed"); 1873 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) 1874 pr_cont(" volatile"); 1875 } 1876 pr_cont("\n"); 1877 } 1878 1879 /* Log all controls owned by the handler */ 1880 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl, 1881 const char *prefix) 1882 { 1883 struct v4l2_ctrl *ctrl; 1884 const char *colon = ""; 1885 int len; 1886 1887 if (!hdl) 1888 return; 1889 if (!prefix) 1890 prefix = ""; 1891 len = strlen(prefix); 1892 if (len && prefix[len - 1] != ' ') 1893 colon = ": "; 1894 mutex_lock(hdl->lock); 1895 list_for_each_entry(ctrl, &hdl->ctrls, node) 1896 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED)) 1897 log_ctrl(ctrl, prefix, colon); 1898 mutex_unlock(hdl->lock); 1899 } 1900 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status); 1901 1902 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl, 1903 const struct v4l2_ctrl_ops *ctrl_ops, 1904 const struct v4l2_fwnode_device_properties *p) 1905 { 1906 if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) { 1907 u32 orientation_ctrl; 1908 1909 switch (p->orientation) { 1910 case V4L2_FWNODE_ORIENTATION_FRONT: 1911 orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT; 1912 break; 1913 case V4L2_FWNODE_ORIENTATION_BACK: 1914 orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK; 1915 break; 1916 case V4L2_FWNODE_ORIENTATION_EXTERNAL: 1917 orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL; 1918 break; 1919 default: 1920 return -EINVAL; 1921 } 1922 if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops, 1923 V4L2_CID_CAMERA_ORIENTATION, 1924 V4L2_CAMERA_ORIENTATION_EXTERNAL, 0, 1925 orientation_ctrl)) 1926 return hdl->error; 1927 } 1928 1929 if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) { 1930 if (!v4l2_ctrl_new_std(hdl, ctrl_ops, 1931 V4L2_CID_CAMERA_SENSOR_ROTATION, 1932 p->rotation, p->rotation, 1, 1933 p->rotation)) 1934 return hdl->error; 1935 } 1936 1937 return hdl->error; 1938 } 1939 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties); 1940