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 fill_event(struct v4l2_event * ev,struct v4l2_ctrl * ctrl,u32 changes)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 send_initial_event(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl)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 send_event(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 changes)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 v4l2_ctrl_type_op_equal(const struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr ptr1,union v4l2_ctrl_ptr ptr2)68 bool v4l2_ctrl_type_op_equal(const struct v4l2_ctrl *ctrl, 69 union v4l2_ctrl_ptr ptr1, union v4l2_ctrl_ptr ptr2) 70 { 71 unsigned int i; 72 73 switch (ctrl->type) { 74 case V4L2_CTRL_TYPE_BUTTON: 75 return false; 76 case V4L2_CTRL_TYPE_STRING: 77 for (i = 0; i < ctrl->elems; i++) { 78 unsigned int idx = i * ctrl->elem_size; 79 80 /* strings are always 0-terminated */ 81 if (strcmp(ptr1.p_char + idx, ptr2.p_char + idx)) 82 return false; 83 } 84 return true; 85 default: 86 return !memcmp(ptr1.p_const, ptr2.p_const, 87 ctrl->elems * ctrl->elem_size); 88 } 89 } 90 EXPORT_SYMBOL(v4l2_ctrl_type_op_equal); 91 92 /* Default intra MPEG-2 quantisation coefficients, from the specification. */ 93 static const u8 mpeg2_intra_quant_matrix[64] = { 94 8, 16, 16, 19, 16, 19, 22, 22, 95 22, 22, 22, 22, 26, 24, 26, 27, 96 27, 27, 26, 26, 26, 26, 27, 27, 97 27, 29, 29, 29, 34, 34, 34, 29, 98 29, 29, 27, 27, 29, 29, 32, 32, 99 34, 34, 37, 38, 37, 35, 35, 34, 100 35, 38, 38, 40, 40, 40, 48, 48, 101 46, 46, 56, 56, 58, 69, 69, 83 102 }; 103 std_init_compound(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)104 static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx, 105 union v4l2_ctrl_ptr ptr) 106 { 107 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence; 108 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture; 109 struct v4l2_ctrl_mpeg2_quantisation *p_mpeg2_quant; 110 struct v4l2_ctrl_vp8_frame *p_vp8_frame; 111 struct v4l2_ctrl_vp9_frame *p_vp9_frame; 112 struct v4l2_ctrl_fwht_params *p_fwht_params; 113 struct v4l2_ctrl_h264_scaling_matrix *p_h264_scaling_matrix; 114 struct v4l2_ctrl_av1_sequence *p_av1_sequence; 115 void *p = ptr.p + idx * ctrl->elem_size; 116 117 if (ctrl->p_def.p_const) 118 memcpy(p, ctrl->p_def.p_const, ctrl->elem_size); 119 else 120 memset(p, 0, ctrl->elem_size); 121 122 switch ((u32)ctrl->type) { 123 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 124 p_mpeg2_sequence = p; 125 126 /* 4:2:0 */ 127 p_mpeg2_sequence->chroma_format = 1; 128 break; 129 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 130 p_mpeg2_picture = p; 131 132 /* interlaced top field */ 133 p_mpeg2_picture->picture_structure = V4L2_MPEG2_PIC_TOP_FIELD; 134 p_mpeg2_picture->picture_coding_type = 135 V4L2_MPEG2_PIC_CODING_TYPE_I; 136 break; 137 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 138 p_mpeg2_quant = p; 139 140 memcpy(p_mpeg2_quant->intra_quantiser_matrix, 141 mpeg2_intra_quant_matrix, 142 ARRAY_SIZE(mpeg2_intra_quant_matrix)); 143 /* 144 * The default non-intra MPEG-2 quantisation 145 * coefficients are all 16, as per the specification. 146 */ 147 memset(p_mpeg2_quant->non_intra_quantiser_matrix, 16, 148 sizeof(p_mpeg2_quant->non_intra_quantiser_matrix)); 149 break; 150 case V4L2_CTRL_TYPE_VP8_FRAME: 151 p_vp8_frame = p; 152 p_vp8_frame->num_dct_parts = 1; 153 break; 154 case V4L2_CTRL_TYPE_VP9_FRAME: 155 p_vp9_frame = p; 156 p_vp9_frame->profile = 0; 157 p_vp9_frame->bit_depth = 8; 158 p_vp9_frame->flags |= V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING | 159 V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING; 160 break; 161 case V4L2_CTRL_TYPE_AV1_SEQUENCE: 162 p_av1_sequence = p; 163 p_av1_sequence->bit_depth = 8; 164 break; 165 case V4L2_CTRL_TYPE_FWHT_PARAMS: 166 p_fwht_params = p; 167 p_fwht_params->version = V4L2_FWHT_VERSION; 168 p_fwht_params->width = 1280; 169 p_fwht_params->height = 720; 170 p_fwht_params->flags = V4L2_FWHT_FL_PIXENC_YUV | 171 (2 << V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET); 172 break; 173 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 174 p_h264_scaling_matrix = p; 175 /* 176 * The default (flat) H.264 scaling matrix when none are 177 * specified in the bitstream, this is according to formulas 178 * (7-8) and (7-9) of the specification. 179 */ 180 memset(p_h264_scaling_matrix, 16, sizeof(*p_h264_scaling_matrix)); 181 break; 182 } 183 } 184 std_min_compound(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)185 static void std_min_compound(const struct v4l2_ctrl *ctrl, u32 idx, 186 union v4l2_ctrl_ptr ptr) 187 { 188 void *p = ptr.p + idx * ctrl->elem_size; 189 190 if (ctrl->p_min.p_const) 191 memcpy(p, ctrl->p_min.p_const, ctrl->elem_size); 192 else 193 memset(p, 0, ctrl->elem_size); 194 } 195 std_max_compound(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)196 static void std_max_compound(const struct v4l2_ctrl *ctrl, u32 idx, 197 union v4l2_ctrl_ptr ptr) 198 { 199 void *p = ptr.p + idx * ctrl->elem_size; 200 201 if (ctrl->p_max.p_const) 202 memcpy(p, ctrl->p_max.p_const, ctrl->elem_size); 203 else 204 memset(p, 0, ctrl->elem_size); 205 } 206 __v4l2_ctrl_type_op_init(const struct v4l2_ctrl * ctrl,u32 from_idx,u32 which,union v4l2_ctrl_ptr ptr)207 static void __v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, 208 u32 which, union v4l2_ctrl_ptr ptr) 209 { 210 unsigned int i; 211 u32 tot_elems = ctrl->elems; 212 u32 elems = tot_elems - from_idx; 213 s64 value; 214 215 switch (which) { 216 case V4L2_CTRL_WHICH_DEF_VAL: 217 value = ctrl->default_value; 218 break; 219 case V4L2_CTRL_WHICH_MAX_VAL: 220 value = ctrl->maximum; 221 break; 222 case V4L2_CTRL_WHICH_MIN_VAL: 223 value = ctrl->minimum; 224 break; 225 default: 226 return; 227 } 228 229 switch (ctrl->type) { 230 case V4L2_CTRL_TYPE_STRING: 231 if (which == V4L2_CTRL_WHICH_DEF_VAL) 232 value = ctrl->minimum; 233 234 for (i = from_idx; i < tot_elems; i++) { 235 unsigned int offset = i * ctrl->elem_size; 236 237 memset(ptr.p_char + offset, ' ', value); 238 ptr.p_char[offset + value] = '\0'; 239 } 240 break; 241 case V4L2_CTRL_TYPE_INTEGER64: 242 if (value) { 243 for (i = from_idx; i < tot_elems; i++) 244 ptr.p_s64[i] = value; 245 } else { 246 memset(ptr.p_s64 + from_idx, 0, elems * sizeof(s64)); 247 } 248 break; 249 case V4L2_CTRL_TYPE_INTEGER: 250 case V4L2_CTRL_TYPE_INTEGER_MENU: 251 case V4L2_CTRL_TYPE_MENU: 252 case V4L2_CTRL_TYPE_BITMASK: 253 case V4L2_CTRL_TYPE_BOOLEAN: 254 if (value) { 255 for (i = from_idx; i < tot_elems; i++) 256 ptr.p_s32[i] = value; 257 } else { 258 memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32)); 259 } 260 break; 261 case V4L2_CTRL_TYPE_BUTTON: 262 case V4L2_CTRL_TYPE_CTRL_CLASS: 263 memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32)); 264 break; 265 case V4L2_CTRL_TYPE_U8: 266 memset(ptr.p_u8 + from_idx, value, elems); 267 break; 268 case V4L2_CTRL_TYPE_U16: 269 if (value) { 270 for (i = from_idx; i < tot_elems; i++) 271 ptr.p_u16[i] = value; 272 } else { 273 memset(ptr.p_u16 + from_idx, 0, elems * sizeof(u16)); 274 } 275 break; 276 case V4L2_CTRL_TYPE_U32: 277 if (value) { 278 for (i = from_idx; i < tot_elems; i++) 279 ptr.p_u32[i] = value; 280 } else { 281 memset(ptr.p_u32 + from_idx, 0, elems * sizeof(u32)); 282 } 283 break; 284 default: 285 for (i = from_idx; i < tot_elems; i++) { 286 switch (which) { 287 case V4L2_CTRL_WHICH_DEF_VAL: 288 std_init_compound(ctrl, i, ptr); 289 break; 290 case V4L2_CTRL_WHICH_MAX_VAL: 291 std_max_compound(ctrl, i, ptr); 292 break; 293 case V4L2_CTRL_WHICH_MIN_VAL: 294 std_min_compound(ctrl, i, ptr); 295 break; 296 } 297 } 298 break; 299 } 300 } 301 v4l2_ctrl_type_op_init(const struct v4l2_ctrl * ctrl,u32 from_idx,union v4l2_ctrl_ptr ptr)302 void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, 303 union v4l2_ctrl_ptr ptr) 304 { 305 __v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_DEF_VAL, ptr); 306 } 307 EXPORT_SYMBOL(v4l2_ctrl_type_op_init); 308 v4l2_ctrl_type_op_minimum(const struct v4l2_ctrl * ctrl,u32 from_idx,union v4l2_ctrl_ptr ptr)309 static void v4l2_ctrl_type_op_minimum(const struct v4l2_ctrl *ctrl, 310 u32 from_idx, union v4l2_ctrl_ptr ptr) 311 { 312 __v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_MIN_VAL, ptr); 313 } 314 v4l2_ctrl_type_op_maximum(const struct v4l2_ctrl * ctrl,u32 from_idx,union v4l2_ctrl_ptr ptr)315 static void v4l2_ctrl_type_op_maximum(const struct v4l2_ctrl *ctrl, 316 u32 from_idx, union v4l2_ctrl_ptr ptr) 317 { 318 __v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_MAX_VAL, ptr); 319 } 320 v4l2_ctrl_type_op_log(const struct v4l2_ctrl * ctrl)321 void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl) 322 { 323 union v4l2_ctrl_ptr ptr = ctrl->p_cur; 324 325 if (ctrl->is_array) { 326 unsigned i; 327 328 for (i = 0; i < ctrl->nr_of_dims; i++) 329 pr_cont("[%u]", ctrl->dims[i]); 330 pr_cont(" "); 331 } 332 333 switch (ctrl->type) { 334 case V4L2_CTRL_TYPE_INTEGER: 335 pr_cont("%d", *ptr.p_s32); 336 break; 337 case V4L2_CTRL_TYPE_BOOLEAN: 338 pr_cont("%s", *ptr.p_s32 ? "true" : "false"); 339 break; 340 case V4L2_CTRL_TYPE_MENU: 341 pr_cont("%s", ctrl->qmenu[*ptr.p_s32]); 342 break; 343 case V4L2_CTRL_TYPE_INTEGER_MENU: 344 pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]); 345 break; 346 case V4L2_CTRL_TYPE_BITMASK: 347 pr_cont("0x%08x", *ptr.p_s32); 348 break; 349 case V4L2_CTRL_TYPE_INTEGER64: 350 pr_cont("%lld", *ptr.p_s64); 351 break; 352 case V4L2_CTRL_TYPE_STRING: 353 pr_cont("%s", ptr.p_char); 354 break; 355 case V4L2_CTRL_TYPE_U8: 356 pr_cont("%u", (unsigned)*ptr.p_u8); 357 break; 358 case V4L2_CTRL_TYPE_U16: 359 pr_cont("%u", (unsigned)*ptr.p_u16); 360 break; 361 case V4L2_CTRL_TYPE_U32: 362 pr_cont("%u", (unsigned)*ptr.p_u32); 363 break; 364 case V4L2_CTRL_TYPE_AREA: 365 pr_cont("%ux%u", ptr.p_area->width, ptr.p_area->height); 366 break; 367 case V4L2_CTRL_TYPE_H264_SPS: 368 pr_cont("H264_SPS"); 369 break; 370 case V4L2_CTRL_TYPE_H264_PPS: 371 pr_cont("H264_PPS"); 372 break; 373 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 374 pr_cont("H264_SCALING_MATRIX"); 375 break; 376 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS: 377 pr_cont("H264_SLICE_PARAMS"); 378 break; 379 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS: 380 pr_cont("H264_DECODE_PARAMS"); 381 break; 382 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS: 383 pr_cont("H264_PRED_WEIGHTS"); 384 break; 385 case V4L2_CTRL_TYPE_FWHT_PARAMS: 386 pr_cont("FWHT_PARAMS"); 387 break; 388 case V4L2_CTRL_TYPE_VP8_FRAME: 389 pr_cont("VP8_FRAME"); 390 break; 391 case V4L2_CTRL_TYPE_HDR10_CLL_INFO: 392 pr_cont("HDR10_CLL_INFO"); 393 break; 394 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY: 395 pr_cont("HDR10_MASTERING_DISPLAY"); 396 break; 397 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 398 pr_cont("MPEG2_QUANTISATION"); 399 break; 400 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 401 pr_cont("MPEG2_SEQUENCE"); 402 break; 403 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 404 pr_cont("MPEG2_PICTURE"); 405 break; 406 case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR: 407 pr_cont("VP9_COMPRESSED_HDR"); 408 break; 409 case V4L2_CTRL_TYPE_VP9_FRAME: 410 pr_cont("VP9_FRAME"); 411 break; 412 case V4L2_CTRL_TYPE_HEVC_SPS: 413 pr_cont("HEVC_SPS"); 414 break; 415 case V4L2_CTRL_TYPE_HEVC_PPS: 416 pr_cont("HEVC_PPS"); 417 break; 418 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS: 419 pr_cont("HEVC_SLICE_PARAMS"); 420 break; 421 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX: 422 pr_cont("HEVC_SCALING_MATRIX"); 423 break; 424 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS: 425 pr_cont("HEVC_DECODE_PARAMS"); 426 break; 427 case V4L2_CTRL_TYPE_AV1_SEQUENCE: 428 pr_cont("AV1_SEQUENCE"); 429 break; 430 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY: 431 pr_cont("AV1_TILE_GROUP_ENTRY"); 432 break; 433 case V4L2_CTRL_TYPE_AV1_FRAME: 434 pr_cont("AV1_FRAME"); 435 break; 436 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN: 437 pr_cont("AV1_FILM_GRAIN"); 438 break; 439 case V4L2_CTRL_TYPE_RECT: 440 pr_cont("(%d,%d)/%ux%u", 441 ptr.p_rect->left, ptr.p_rect->top, 442 ptr.p_rect->width, ptr.p_rect->height); 443 break; 444 default: 445 pr_cont("unknown type %d", ctrl->type); 446 break; 447 } 448 } 449 EXPORT_SYMBOL(v4l2_ctrl_type_op_log); 450 451 /* 452 * Round towards the closest legal value. Be careful when we are 453 * close to the maximum range of the control type to prevent 454 * wrap-arounds. 455 */ 456 #define ROUND_TO_RANGE(val, offset_type, ctrl) \ 457 ({ \ 458 offset_type offset; \ 459 if ((ctrl)->maximum >= 0 && \ 460 val >= (ctrl)->maximum - (s32)((ctrl)->step / 2)) \ 461 val = (ctrl)->maximum; \ 462 else \ 463 val += (s32)((ctrl)->step / 2); \ 464 val = clamp_t(typeof(val), val, \ 465 (ctrl)->minimum, (ctrl)->maximum); \ 466 offset = (val) - (ctrl)->minimum; \ 467 offset = (ctrl)->step * (offset / (u32)(ctrl)->step); \ 468 val = (ctrl)->minimum + offset; \ 469 0; \ 470 }) 471 472 /* Validate a new control */ 473 474 #define zero_padding(s) \ 475 memset(&(s).padding, 0, sizeof((s).padding)) 476 #define zero_reserved(s) \ 477 memset(&(s).reserved, 0, sizeof((s).reserved)) 478 479 static int validate_vp9_lf_params(struct v4l2_vp9_loop_filter * lf)480 validate_vp9_lf_params(struct v4l2_vp9_loop_filter *lf) 481 { 482 unsigned int i; 483 484 if (lf->flags & ~(V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED | 485 V4L2_VP9_LOOP_FILTER_FLAG_DELTA_UPDATE)) 486 return -EINVAL; 487 488 /* That all values are in the accepted range. */ 489 if (lf->level > GENMASK(5, 0)) 490 return -EINVAL; 491 492 if (lf->sharpness > GENMASK(2, 0)) 493 return -EINVAL; 494 495 for (i = 0; i < ARRAY_SIZE(lf->ref_deltas); i++) 496 if (lf->ref_deltas[i] < -63 || lf->ref_deltas[i] > 63) 497 return -EINVAL; 498 499 for (i = 0; i < ARRAY_SIZE(lf->mode_deltas); i++) 500 if (lf->mode_deltas[i] < -63 || lf->mode_deltas[i] > 63) 501 return -EINVAL; 502 503 zero_reserved(*lf); 504 return 0; 505 } 506 507 static int validate_vp9_quant_params(struct v4l2_vp9_quantization * quant)508 validate_vp9_quant_params(struct v4l2_vp9_quantization *quant) 509 { 510 if (quant->delta_q_y_dc < -15 || quant->delta_q_y_dc > 15 || 511 quant->delta_q_uv_dc < -15 || quant->delta_q_uv_dc > 15 || 512 quant->delta_q_uv_ac < -15 || quant->delta_q_uv_ac > 15) 513 return -EINVAL; 514 515 zero_reserved(*quant); 516 return 0; 517 } 518 519 static int validate_vp9_seg_params(struct v4l2_vp9_segmentation * seg)520 validate_vp9_seg_params(struct v4l2_vp9_segmentation *seg) 521 { 522 unsigned int i, j; 523 524 if (seg->flags & ~(V4L2_VP9_SEGMENTATION_FLAG_ENABLED | 525 V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP | 526 V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE | 527 V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA | 528 V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE)) 529 return -EINVAL; 530 531 for (i = 0; i < ARRAY_SIZE(seg->feature_enabled); i++) { 532 if (seg->feature_enabled[i] & 533 ~V4L2_VP9_SEGMENT_FEATURE_ENABLED_MASK) 534 return -EINVAL; 535 } 536 537 for (i = 0; i < ARRAY_SIZE(seg->feature_data); i++) { 538 static const int range[] = { 255, 63, 3, 0 }; 539 540 for (j = 0; j < ARRAY_SIZE(seg->feature_data[j]); j++) { 541 if (seg->feature_data[i][j] < -range[j] || 542 seg->feature_data[i][j] > range[j]) 543 return -EINVAL; 544 } 545 } 546 547 zero_reserved(*seg); 548 return 0; 549 } 550 551 static int validate_vp9_compressed_hdr(struct v4l2_ctrl_vp9_compressed_hdr * hdr)552 validate_vp9_compressed_hdr(struct v4l2_ctrl_vp9_compressed_hdr *hdr) 553 { 554 if (hdr->tx_mode > V4L2_VP9_TX_MODE_SELECT) 555 return -EINVAL; 556 557 return 0; 558 } 559 560 static int validate_vp9_frame(struct v4l2_ctrl_vp9_frame * frame)561 validate_vp9_frame(struct v4l2_ctrl_vp9_frame *frame) 562 { 563 int ret; 564 565 /* Make sure we're not passed invalid flags. */ 566 if (frame->flags & ~(V4L2_VP9_FRAME_FLAG_KEY_FRAME | 567 V4L2_VP9_FRAME_FLAG_SHOW_FRAME | 568 V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT | 569 V4L2_VP9_FRAME_FLAG_INTRA_ONLY | 570 V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV | 571 V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX | 572 V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE | 573 V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING | 574 V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING | 575 V4L2_VP9_FRAME_FLAG_COLOR_RANGE_FULL_SWING)) 576 return -EINVAL; 577 578 if (frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT && 579 frame->flags & V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX) 580 return -EINVAL; 581 582 if (frame->profile > V4L2_VP9_PROFILE_MAX) 583 return -EINVAL; 584 585 if (frame->reset_frame_context > V4L2_VP9_RESET_FRAME_CTX_ALL) 586 return -EINVAL; 587 588 if (frame->frame_context_idx >= V4L2_VP9_NUM_FRAME_CTX) 589 return -EINVAL; 590 591 /* 592 * Profiles 0 and 1 only support 8-bit depth, profiles 2 and 3 only 10 593 * and 12 bit depths. 594 */ 595 if ((frame->profile < 2 && frame->bit_depth != 8) || 596 (frame->profile >= 2 && 597 (frame->bit_depth != 10 && frame->bit_depth != 12))) 598 return -EINVAL; 599 600 /* Profile 0 and 2 only accept YUV 4:2:0. */ 601 if ((frame->profile == 0 || frame->profile == 2) && 602 (!(frame->flags & V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING) || 603 !(frame->flags & V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING))) 604 return -EINVAL; 605 606 /* Profile 1 and 3 only accept YUV 4:2:2, 4:4:0 and 4:4:4. */ 607 if ((frame->profile == 1 || frame->profile == 3) && 608 ((frame->flags & V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING) && 609 (frame->flags & V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING))) 610 return -EINVAL; 611 612 if (frame->interpolation_filter > V4L2_VP9_INTERP_FILTER_SWITCHABLE) 613 return -EINVAL; 614 615 /* 616 * According to the spec, tile_cols_log2 shall be less than or equal 617 * to 6. 618 */ 619 if (frame->tile_cols_log2 > 6) 620 return -EINVAL; 621 622 if (frame->reference_mode > V4L2_VP9_REFERENCE_MODE_SELECT) 623 return -EINVAL; 624 625 ret = validate_vp9_lf_params(&frame->lf); 626 if (ret) 627 return ret; 628 629 ret = validate_vp9_quant_params(&frame->quant); 630 if (ret) 631 return ret; 632 633 ret = validate_vp9_seg_params(&frame->seg); 634 if (ret) 635 return ret; 636 637 zero_reserved(*frame); 638 return 0; 639 } 640 validate_av1_quantization(struct v4l2_av1_quantization * q)641 static int validate_av1_quantization(struct v4l2_av1_quantization *q) 642 { 643 if (q->flags > GENMASK(2, 0)) 644 return -EINVAL; 645 646 if (q->delta_q_y_dc < -64 || q->delta_q_y_dc > 63 || 647 q->delta_q_u_dc < -64 || q->delta_q_u_dc > 63 || 648 q->delta_q_v_dc < -64 || q->delta_q_v_dc > 63 || 649 q->delta_q_u_ac < -64 || q->delta_q_u_ac > 63 || 650 q->delta_q_v_ac < -64 || q->delta_q_v_ac > 63 || 651 q->delta_q_res > GENMASK(1, 0)) 652 return -EINVAL; 653 654 if (q->qm_y > GENMASK(3, 0) || 655 q->qm_u > GENMASK(3, 0) || 656 q->qm_v > GENMASK(3, 0)) 657 return -EINVAL; 658 659 return 0; 660 } 661 validate_av1_segmentation(struct v4l2_av1_segmentation * s)662 static int validate_av1_segmentation(struct v4l2_av1_segmentation *s) 663 { 664 u32 i; 665 u32 j; 666 667 if (s->flags > GENMASK(4, 0)) 668 return -EINVAL; 669 670 for (i = 0; i < ARRAY_SIZE(s->feature_data); i++) { 671 static const int segmentation_feature_signed[] = { 1, 1, 1, 1, 1, 0, 0, 0 }; 672 static const int segmentation_feature_max[] = { 255, 63, 63, 63, 63, 7, 0, 0}; 673 674 for (j = 0; j < ARRAY_SIZE(s->feature_data[j]); j++) { 675 s32 limit = segmentation_feature_max[j]; 676 677 if (segmentation_feature_signed[j]) { 678 if (s->feature_data[i][j] < -limit || 679 s->feature_data[i][j] > limit) 680 return -EINVAL; 681 } else { 682 if (s->feature_data[i][j] < 0 || s->feature_data[i][j] > limit) 683 return -EINVAL; 684 } 685 } 686 } 687 688 return 0; 689 } 690 validate_av1_loop_filter(struct v4l2_av1_loop_filter * lf)691 static int validate_av1_loop_filter(struct v4l2_av1_loop_filter *lf) 692 { 693 u32 i; 694 695 if (lf->flags > GENMASK(3, 0)) 696 return -EINVAL; 697 698 for (i = 0; i < ARRAY_SIZE(lf->level); i++) { 699 if (lf->level[i] > GENMASK(5, 0)) 700 return -EINVAL; 701 } 702 703 if (lf->sharpness > GENMASK(2, 0)) 704 return -EINVAL; 705 706 for (i = 0; i < ARRAY_SIZE(lf->ref_deltas); i++) { 707 if (lf->ref_deltas[i] < -64 || lf->ref_deltas[i] > 63) 708 return -EINVAL; 709 } 710 711 for (i = 0; i < ARRAY_SIZE(lf->mode_deltas); i++) { 712 if (lf->mode_deltas[i] < -64 || lf->mode_deltas[i] > 63) 713 return -EINVAL; 714 } 715 716 return 0; 717 } 718 validate_av1_cdef(struct v4l2_av1_cdef * cdef)719 static int validate_av1_cdef(struct v4l2_av1_cdef *cdef) 720 { 721 u32 i; 722 723 if (cdef->damping_minus_3 > GENMASK(1, 0) || 724 cdef->bits > GENMASK(1, 0)) 725 return -EINVAL; 726 727 for (i = 0; i < 1 << cdef->bits; i++) { 728 if (cdef->y_pri_strength[i] > GENMASK(3, 0) || 729 cdef->y_sec_strength[i] > 4 || 730 cdef->uv_pri_strength[i] > GENMASK(3, 0) || 731 cdef->uv_sec_strength[i] > 4) 732 return -EINVAL; 733 } 734 735 return 0; 736 } 737 validate_av1_loop_restauration(struct v4l2_av1_loop_restoration * lr)738 static int validate_av1_loop_restauration(struct v4l2_av1_loop_restoration *lr) 739 { 740 if (lr->lr_unit_shift > 3 || lr->lr_uv_shift > 1) 741 return -EINVAL; 742 743 return 0; 744 } 745 validate_av1_film_grain(struct v4l2_ctrl_av1_film_grain * fg)746 static int validate_av1_film_grain(struct v4l2_ctrl_av1_film_grain *fg) 747 { 748 u32 i; 749 750 if (fg->flags > GENMASK(4, 0)) 751 return -EINVAL; 752 753 if (fg->film_grain_params_ref_idx > GENMASK(2, 0) || 754 fg->num_y_points > 14 || 755 fg->num_cb_points > 10 || 756 fg->num_cr_points > GENMASK(3, 0) || 757 fg->grain_scaling_minus_8 > GENMASK(1, 0) || 758 fg->ar_coeff_lag > GENMASK(1, 0) || 759 fg->ar_coeff_shift_minus_6 > GENMASK(1, 0) || 760 fg->grain_scale_shift > GENMASK(1, 0)) 761 return -EINVAL; 762 763 if (!(fg->flags & V4L2_AV1_FILM_GRAIN_FLAG_APPLY_GRAIN)) 764 return 0; 765 766 for (i = 1; i < fg->num_y_points; i++) 767 if (fg->point_y_value[i] <= fg->point_y_value[i - 1]) 768 return -EINVAL; 769 770 for (i = 1; i < fg->num_cb_points; i++) 771 if (fg->point_cb_value[i] <= fg->point_cb_value[i - 1]) 772 return -EINVAL; 773 774 for (i = 1; i < fg->num_cr_points; i++) 775 if (fg->point_cr_value[i] <= fg->point_cr_value[i - 1]) 776 return -EINVAL; 777 778 return 0; 779 } 780 validate_av1_frame(struct v4l2_ctrl_av1_frame * f)781 static int validate_av1_frame(struct v4l2_ctrl_av1_frame *f) 782 { 783 int ret = 0; 784 785 ret = validate_av1_quantization(&f->quantization); 786 if (ret) 787 return ret; 788 ret = validate_av1_segmentation(&f->segmentation); 789 if (ret) 790 return ret; 791 ret = validate_av1_loop_filter(&f->loop_filter); 792 if (ret) 793 return ret; 794 ret = validate_av1_cdef(&f->cdef); 795 if (ret) 796 return ret; 797 ret = validate_av1_loop_restauration(&f->loop_restoration); 798 if (ret) 799 return ret; 800 801 if (f->flags & 802 ~(V4L2_AV1_FRAME_FLAG_SHOW_FRAME | 803 V4L2_AV1_FRAME_FLAG_SHOWABLE_FRAME | 804 V4L2_AV1_FRAME_FLAG_ERROR_RESILIENT_MODE | 805 V4L2_AV1_FRAME_FLAG_DISABLE_CDF_UPDATE | 806 V4L2_AV1_FRAME_FLAG_ALLOW_SCREEN_CONTENT_TOOLS | 807 V4L2_AV1_FRAME_FLAG_FORCE_INTEGER_MV | 808 V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC | 809 V4L2_AV1_FRAME_FLAG_USE_SUPERRES | 810 V4L2_AV1_FRAME_FLAG_ALLOW_HIGH_PRECISION_MV | 811 V4L2_AV1_FRAME_FLAG_IS_MOTION_MODE_SWITCHABLE | 812 V4L2_AV1_FRAME_FLAG_USE_REF_FRAME_MVS | 813 V4L2_AV1_FRAME_FLAG_DISABLE_FRAME_END_UPDATE_CDF | 814 V4L2_AV1_FRAME_FLAG_ALLOW_WARPED_MOTION | 815 V4L2_AV1_FRAME_FLAG_REFERENCE_SELECT | 816 V4L2_AV1_FRAME_FLAG_REDUCED_TX_SET | 817 V4L2_AV1_FRAME_FLAG_SKIP_MODE_ALLOWED | 818 V4L2_AV1_FRAME_FLAG_SKIP_MODE_PRESENT | 819 V4L2_AV1_FRAME_FLAG_FRAME_SIZE_OVERRIDE | 820 V4L2_AV1_FRAME_FLAG_BUFFER_REMOVAL_TIME_PRESENT | 821 V4L2_AV1_FRAME_FLAG_FRAME_REFS_SHORT_SIGNALING)) 822 return -EINVAL; 823 824 if (f->superres_denom > GENMASK(2, 0) + 9) 825 return -EINVAL; 826 827 return 0; 828 } 829 validate_av1_sequence(struct v4l2_ctrl_av1_sequence * s)830 static int validate_av1_sequence(struct v4l2_ctrl_av1_sequence *s) 831 { 832 if (s->flags & 833 ~(V4L2_AV1_SEQUENCE_FLAG_STILL_PICTURE | 834 V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK | 835 V4L2_AV1_SEQUENCE_FLAG_ENABLE_FILTER_INTRA | 836 V4L2_AV1_SEQUENCE_FLAG_ENABLE_INTRA_EDGE_FILTER | 837 V4L2_AV1_SEQUENCE_FLAG_ENABLE_INTERINTRA_COMPOUND | 838 V4L2_AV1_SEQUENCE_FLAG_ENABLE_MASKED_COMPOUND | 839 V4L2_AV1_SEQUENCE_FLAG_ENABLE_WARPED_MOTION | 840 V4L2_AV1_SEQUENCE_FLAG_ENABLE_DUAL_FILTER | 841 V4L2_AV1_SEQUENCE_FLAG_ENABLE_ORDER_HINT | 842 V4L2_AV1_SEQUENCE_FLAG_ENABLE_JNT_COMP | 843 V4L2_AV1_SEQUENCE_FLAG_ENABLE_REF_FRAME_MVS | 844 V4L2_AV1_SEQUENCE_FLAG_ENABLE_SUPERRES | 845 V4L2_AV1_SEQUENCE_FLAG_ENABLE_CDEF | 846 V4L2_AV1_SEQUENCE_FLAG_ENABLE_RESTORATION | 847 V4L2_AV1_SEQUENCE_FLAG_MONO_CHROME | 848 V4L2_AV1_SEQUENCE_FLAG_COLOR_RANGE | 849 V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X | 850 V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y | 851 V4L2_AV1_SEQUENCE_FLAG_FILM_GRAIN_PARAMS_PRESENT | 852 V4L2_AV1_SEQUENCE_FLAG_SEPARATE_UV_DELTA_Q)) 853 return -EINVAL; 854 855 if (s->seq_profile == 1 && s->flags & V4L2_AV1_SEQUENCE_FLAG_MONO_CHROME) 856 return -EINVAL; 857 858 /* reserved */ 859 if (s->seq_profile > 2) 860 return -EINVAL; 861 862 /* TODO: PROFILES */ 863 return 0; 864 } 865 866 /* 867 * Compound controls validation requires setting unused fields/flags to zero 868 * in order to properly detect unchanged controls with v4l2_ctrl_type_op_equal's 869 * memcmp. 870 */ std_validate_compound(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)871 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx, 872 union v4l2_ctrl_ptr ptr) 873 { 874 struct v4l2_ctrl_mpeg2_sequence *p_mpeg2_sequence; 875 struct v4l2_ctrl_mpeg2_picture *p_mpeg2_picture; 876 struct v4l2_ctrl_vp8_frame *p_vp8_frame; 877 struct v4l2_ctrl_fwht_params *p_fwht_params; 878 struct v4l2_ctrl_h264_sps *p_h264_sps; 879 struct v4l2_ctrl_h264_pps *p_h264_pps; 880 struct v4l2_ctrl_h264_pred_weights *p_h264_pred_weights; 881 struct v4l2_ctrl_h264_slice_params *p_h264_slice_params; 882 struct v4l2_ctrl_h264_decode_params *p_h264_dec_params; 883 struct v4l2_ctrl_hevc_sps *p_hevc_sps; 884 struct v4l2_ctrl_hevc_pps *p_hevc_pps; 885 struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering; 886 struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params; 887 struct v4l2_area *area; 888 struct v4l2_rect *rect; 889 void *p = ptr.p + idx * ctrl->elem_size; 890 unsigned int i; 891 892 switch ((u32)ctrl->type) { 893 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 894 p_mpeg2_sequence = p; 895 896 switch (p_mpeg2_sequence->chroma_format) { 897 case 1: /* 4:2:0 */ 898 case 2: /* 4:2:2 */ 899 case 3: /* 4:4:4 */ 900 break; 901 default: 902 return -EINVAL; 903 } 904 break; 905 906 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 907 p_mpeg2_picture = p; 908 909 switch (p_mpeg2_picture->intra_dc_precision) { 910 case 0: /* 8 bits */ 911 case 1: /* 9 bits */ 912 case 2: /* 10 bits */ 913 case 3: /* 11 bits */ 914 break; 915 default: 916 return -EINVAL; 917 } 918 919 switch (p_mpeg2_picture->picture_structure) { 920 case V4L2_MPEG2_PIC_TOP_FIELD: 921 case V4L2_MPEG2_PIC_BOTTOM_FIELD: 922 case V4L2_MPEG2_PIC_FRAME: 923 break; 924 default: 925 return -EINVAL; 926 } 927 928 switch (p_mpeg2_picture->picture_coding_type) { 929 case V4L2_MPEG2_PIC_CODING_TYPE_I: 930 case V4L2_MPEG2_PIC_CODING_TYPE_P: 931 case V4L2_MPEG2_PIC_CODING_TYPE_B: 932 break; 933 default: 934 return -EINVAL; 935 } 936 zero_reserved(*p_mpeg2_picture); 937 break; 938 939 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 940 break; 941 942 case V4L2_CTRL_TYPE_FWHT_PARAMS: 943 p_fwht_params = p; 944 if (p_fwht_params->version < V4L2_FWHT_VERSION) 945 return -EINVAL; 946 if (!p_fwht_params->width || !p_fwht_params->height) 947 return -EINVAL; 948 break; 949 950 case V4L2_CTRL_TYPE_H264_SPS: 951 p_h264_sps = p; 952 953 /* Some syntax elements are only conditionally valid */ 954 if (p_h264_sps->pic_order_cnt_type != 0) { 955 p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 = 0; 956 } else if (p_h264_sps->pic_order_cnt_type != 1) { 957 p_h264_sps->num_ref_frames_in_pic_order_cnt_cycle = 0; 958 p_h264_sps->offset_for_non_ref_pic = 0; 959 p_h264_sps->offset_for_top_to_bottom_field = 0; 960 memset(&p_h264_sps->offset_for_ref_frame, 0, 961 sizeof(p_h264_sps->offset_for_ref_frame)); 962 } 963 964 if (!V4L2_H264_SPS_HAS_CHROMA_FORMAT(p_h264_sps)) { 965 p_h264_sps->chroma_format_idc = 1; 966 p_h264_sps->bit_depth_luma_minus8 = 0; 967 p_h264_sps->bit_depth_chroma_minus8 = 0; 968 969 p_h264_sps->flags &= 970 ~V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS; 971 } 972 973 if (p_h264_sps->chroma_format_idc < 3) 974 p_h264_sps->flags &= 975 ~V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE; 976 977 if (p_h264_sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY) 978 p_h264_sps->flags &= 979 ~V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD; 980 981 /* 982 * Chroma 4:2:2 format require at least High 4:2:2 profile. 983 * 984 * The H264 specification and well-known parser implementations 985 * use profile-idc values directly, as that is clearer and 986 * less ambiguous. We do the same here. 987 */ 988 if (p_h264_sps->profile_idc < 122 && 989 p_h264_sps->chroma_format_idc > 1) 990 return -EINVAL; 991 /* Chroma 4:4:4 format require at least High 4:2:2 profile */ 992 if (p_h264_sps->profile_idc < 244 && 993 p_h264_sps->chroma_format_idc > 2) 994 return -EINVAL; 995 if (p_h264_sps->chroma_format_idc > 3) 996 return -EINVAL; 997 998 if (p_h264_sps->bit_depth_luma_minus8 > 6) 999 return -EINVAL; 1000 if (p_h264_sps->bit_depth_chroma_minus8 > 6) 1001 return -EINVAL; 1002 if (p_h264_sps->log2_max_frame_num_minus4 > 12) 1003 return -EINVAL; 1004 if (p_h264_sps->pic_order_cnt_type > 2) 1005 return -EINVAL; 1006 if (p_h264_sps->log2_max_pic_order_cnt_lsb_minus4 > 12) 1007 return -EINVAL; 1008 if (p_h264_sps->max_num_ref_frames > V4L2_H264_REF_LIST_LEN) 1009 return -EINVAL; 1010 break; 1011 1012 case V4L2_CTRL_TYPE_H264_PPS: 1013 p_h264_pps = p; 1014 1015 if (p_h264_pps->num_slice_groups_minus1 > 7) 1016 return -EINVAL; 1017 if (p_h264_pps->num_ref_idx_l0_default_active_minus1 > 1018 (V4L2_H264_REF_LIST_LEN - 1)) 1019 return -EINVAL; 1020 if (p_h264_pps->num_ref_idx_l1_default_active_minus1 > 1021 (V4L2_H264_REF_LIST_LEN - 1)) 1022 return -EINVAL; 1023 if (p_h264_pps->weighted_bipred_idc > 2) 1024 return -EINVAL; 1025 /* 1026 * pic_init_qp_minus26 shall be in the range of 1027 * -(26 + QpBdOffset_y) to +25, inclusive, 1028 * where QpBdOffset_y is 6 * bit_depth_luma_minus8 1029 */ 1030 if (p_h264_pps->pic_init_qp_minus26 < -62 || 1031 p_h264_pps->pic_init_qp_minus26 > 25) 1032 return -EINVAL; 1033 if (p_h264_pps->pic_init_qs_minus26 < -26 || 1034 p_h264_pps->pic_init_qs_minus26 > 25) 1035 return -EINVAL; 1036 if (p_h264_pps->chroma_qp_index_offset < -12 || 1037 p_h264_pps->chroma_qp_index_offset > 12) 1038 return -EINVAL; 1039 if (p_h264_pps->second_chroma_qp_index_offset < -12 || 1040 p_h264_pps->second_chroma_qp_index_offset > 12) 1041 return -EINVAL; 1042 break; 1043 1044 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 1045 break; 1046 1047 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS: 1048 p_h264_pred_weights = p; 1049 1050 if (p_h264_pred_weights->luma_log2_weight_denom > 7) 1051 return -EINVAL; 1052 if (p_h264_pred_weights->chroma_log2_weight_denom > 7) 1053 return -EINVAL; 1054 break; 1055 1056 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS: 1057 p_h264_slice_params = p; 1058 1059 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B) 1060 p_h264_slice_params->flags &= 1061 ~V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED; 1062 1063 if (p_h264_slice_params->colour_plane_id > 2) 1064 return -EINVAL; 1065 if (p_h264_slice_params->cabac_init_idc > 2) 1066 return -EINVAL; 1067 if (p_h264_slice_params->disable_deblocking_filter_idc > 2) 1068 return -EINVAL; 1069 if (p_h264_slice_params->slice_alpha_c0_offset_div2 < -6 || 1070 p_h264_slice_params->slice_alpha_c0_offset_div2 > 6) 1071 return -EINVAL; 1072 if (p_h264_slice_params->slice_beta_offset_div2 < -6 || 1073 p_h264_slice_params->slice_beta_offset_div2 > 6) 1074 return -EINVAL; 1075 1076 if (p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_I || 1077 p_h264_slice_params->slice_type == V4L2_H264_SLICE_TYPE_SI) 1078 p_h264_slice_params->num_ref_idx_l0_active_minus1 = 0; 1079 if (p_h264_slice_params->slice_type != V4L2_H264_SLICE_TYPE_B) 1080 p_h264_slice_params->num_ref_idx_l1_active_minus1 = 0; 1081 1082 if (p_h264_slice_params->num_ref_idx_l0_active_minus1 > 1083 (V4L2_H264_REF_LIST_LEN - 1)) 1084 return -EINVAL; 1085 if (p_h264_slice_params->num_ref_idx_l1_active_minus1 > 1086 (V4L2_H264_REF_LIST_LEN - 1)) 1087 return -EINVAL; 1088 zero_reserved(*p_h264_slice_params); 1089 break; 1090 1091 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS: 1092 p_h264_dec_params = p; 1093 1094 if (p_h264_dec_params->nal_ref_idc > 3) 1095 return -EINVAL; 1096 for (i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) { 1097 struct v4l2_h264_dpb_entry *dpb_entry = 1098 &p_h264_dec_params->dpb[i]; 1099 1100 zero_reserved(*dpb_entry); 1101 } 1102 zero_reserved(*p_h264_dec_params); 1103 break; 1104 1105 case V4L2_CTRL_TYPE_VP8_FRAME: 1106 p_vp8_frame = p; 1107 1108 switch (p_vp8_frame->num_dct_parts) { 1109 case 1: 1110 case 2: 1111 case 4: 1112 case 8: 1113 break; 1114 default: 1115 return -EINVAL; 1116 } 1117 zero_padding(p_vp8_frame->segment); 1118 zero_padding(p_vp8_frame->lf); 1119 zero_padding(p_vp8_frame->quant); 1120 zero_padding(p_vp8_frame->entropy); 1121 zero_padding(p_vp8_frame->coder_state); 1122 break; 1123 1124 case V4L2_CTRL_TYPE_HEVC_SPS: 1125 p_hevc_sps = p; 1126 1127 if (!(p_hevc_sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) { 1128 p_hevc_sps->pcm_sample_bit_depth_luma_minus1 = 0; 1129 p_hevc_sps->pcm_sample_bit_depth_chroma_minus1 = 0; 1130 p_hevc_sps->log2_min_pcm_luma_coding_block_size_minus3 = 0; 1131 p_hevc_sps->log2_diff_max_min_pcm_luma_coding_block_size = 0; 1132 } 1133 1134 if (!(p_hevc_sps->flags & 1135 V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT)) 1136 p_hevc_sps->num_long_term_ref_pics_sps = 0; 1137 break; 1138 1139 case V4L2_CTRL_TYPE_HEVC_PPS: 1140 p_hevc_pps = p; 1141 1142 if (!(p_hevc_pps->flags & 1143 V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED)) 1144 p_hevc_pps->diff_cu_qp_delta_depth = 0; 1145 1146 if (!(p_hevc_pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) { 1147 p_hevc_pps->num_tile_columns_minus1 = 0; 1148 p_hevc_pps->num_tile_rows_minus1 = 0; 1149 memset(&p_hevc_pps->column_width_minus1, 0, 1150 sizeof(p_hevc_pps->column_width_minus1)); 1151 memset(&p_hevc_pps->row_height_minus1, 0, 1152 sizeof(p_hevc_pps->row_height_minus1)); 1153 1154 p_hevc_pps->flags &= 1155 ~V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED; 1156 } 1157 1158 if (p_hevc_pps->flags & 1159 V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER) { 1160 p_hevc_pps->pps_beta_offset_div2 = 0; 1161 p_hevc_pps->pps_tc_offset_div2 = 0; 1162 } 1163 break; 1164 1165 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS: 1166 p_hevc_decode_params = p; 1167 1168 if (p_hevc_decode_params->num_active_dpb_entries > 1169 V4L2_HEVC_DPB_ENTRIES_NUM_MAX) 1170 return -EINVAL; 1171 break; 1172 1173 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS: 1174 break; 1175 1176 case V4L2_CTRL_TYPE_HDR10_CLL_INFO: 1177 break; 1178 1179 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY: 1180 p_hdr10_mastering = p; 1181 1182 for (i = 0; i < 3; ++i) { 1183 if (p_hdr10_mastering->display_primaries_x[i] < 1184 V4L2_HDR10_MASTERING_PRIMARIES_X_LOW || 1185 p_hdr10_mastering->display_primaries_x[i] > 1186 V4L2_HDR10_MASTERING_PRIMARIES_X_HIGH || 1187 p_hdr10_mastering->display_primaries_y[i] < 1188 V4L2_HDR10_MASTERING_PRIMARIES_Y_LOW || 1189 p_hdr10_mastering->display_primaries_y[i] > 1190 V4L2_HDR10_MASTERING_PRIMARIES_Y_HIGH) 1191 return -EINVAL; 1192 } 1193 1194 if (p_hdr10_mastering->white_point_x < 1195 V4L2_HDR10_MASTERING_WHITE_POINT_X_LOW || 1196 p_hdr10_mastering->white_point_x > 1197 V4L2_HDR10_MASTERING_WHITE_POINT_X_HIGH || 1198 p_hdr10_mastering->white_point_y < 1199 V4L2_HDR10_MASTERING_WHITE_POINT_Y_LOW || 1200 p_hdr10_mastering->white_point_y > 1201 V4L2_HDR10_MASTERING_WHITE_POINT_Y_HIGH) 1202 return -EINVAL; 1203 1204 if (p_hdr10_mastering->max_display_mastering_luminance < 1205 V4L2_HDR10_MASTERING_MAX_LUMA_LOW || 1206 p_hdr10_mastering->max_display_mastering_luminance > 1207 V4L2_HDR10_MASTERING_MAX_LUMA_HIGH || 1208 p_hdr10_mastering->min_display_mastering_luminance < 1209 V4L2_HDR10_MASTERING_MIN_LUMA_LOW || 1210 p_hdr10_mastering->min_display_mastering_luminance > 1211 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH) 1212 return -EINVAL; 1213 1214 /* The following restriction comes from ITU-T Rec. H.265 spec */ 1215 if (p_hdr10_mastering->max_display_mastering_luminance == 1216 V4L2_HDR10_MASTERING_MAX_LUMA_LOW && 1217 p_hdr10_mastering->min_display_mastering_luminance == 1218 V4L2_HDR10_MASTERING_MIN_LUMA_HIGH) 1219 return -EINVAL; 1220 1221 break; 1222 1223 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX: 1224 break; 1225 1226 case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR: 1227 return validate_vp9_compressed_hdr(p); 1228 1229 case V4L2_CTRL_TYPE_VP9_FRAME: 1230 return validate_vp9_frame(p); 1231 case V4L2_CTRL_TYPE_AV1_FRAME: 1232 return validate_av1_frame(p); 1233 case V4L2_CTRL_TYPE_AV1_SEQUENCE: 1234 return validate_av1_sequence(p); 1235 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY: 1236 break; 1237 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN: 1238 return validate_av1_film_grain(p); 1239 1240 case V4L2_CTRL_TYPE_AREA: 1241 area = p; 1242 if (!area->width || !area->height) 1243 return -EINVAL; 1244 break; 1245 1246 case V4L2_CTRL_TYPE_RECT: 1247 rect = p; 1248 if (!rect->width || !rect->height) 1249 return -EINVAL; 1250 break; 1251 1252 default: 1253 return -EINVAL; 1254 } 1255 1256 return 0; 1257 } 1258 std_validate_elem(const struct v4l2_ctrl * ctrl,u32 idx,union v4l2_ctrl_ptr ptr)1259 static int std_validate_elem(const struct v4l2_ctrl *ctrl, u32 idx, 1260 union v4l2_ctrl_ptr ptr) 1261 { 1262 size_t len; 1263 u64 offset; 1264 s64 val; 1265 1266 switch ((u32)ctrl->type) { 1267 case V4L2_CTRL_TYPE_INTEGER: 1268 return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl); 1269 case V4L2_CTRL_TYPE_INTEGER64: 1270 /* 1271 * We can't use the ROUND_TO_RANGE define here due to 1272 * the u64 divide that needs special care. 1273 */ 1274 val = ptr.p_s64[idx]; 1275 if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2)) 1276 val = ctrl->maximum; 1277 else 1278 val += (s64)(ctrl->step / 2); 1279 val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum); 1280 offset = val - ctrl->minimum; 1281 do_div(offset, ctrl->step); 1282 ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step; 1283 return 0; 1284 case V4L2_CTRL_TYPE_U8: 1285 return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl); 1286 case V4L2_CTRL_TYPE_U16: 1287 return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl); 1288 case V4L2_CTRL_TYPE_U32: 1289 return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl); 1290 1291 case V4L2_CTRL_TYPE_BOOLEAN: 1292 ptr.p_s32[idx] = !!ptr.p_s32[idx]; 1293 return 0; 1294 1295 case V4L2_CTRL_TYPE_MENU: 1296 case V4L2_CTRL_TYPE_INTEGER_MENU: 1297 if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum) 1298 return -ERANGE; 1299 if (ptr.p_s32[idx] < BITS_PER_LONG_LONG && 1300 (ctrl->menu_skip_mask & BIT_ULL(ptr.p_s32[idx]))) 1301 return -EINVAL; 1302 if (ctrl->type == V4L2_CTRL_TYPE_MENU && 1303 ctrl->qmenu[ptr.p_s32[idx]][0] == '\0') 1304 return -EINVAL; 1305 return 0; 1306 1307 case V4L2_CTRL_TYPE_BITMASK: 1308 ptr.p_s32[idx] &= ctrl->maximum; 1309 return 0; 1310 1311 case V4L2_CTRL_TYPE_BUTTON: 1312 case V4L2_CTRL_TYPE_CTRL_CLASS: 1313 ptr.p_s32[idx] = 0; 1314 return 0; 1315 1316 case V4L2_CTRL_TYPE_STRING: 1317 idx *= ctrl->elem_size; 1318 len = strlen(ptr.p_char + idx); 1319 if (len < ctrl->minimum) 1320 return -ERANGE; 1321 if ((len - (u32)ctrl->minimum) % (u32)ctrl->step) 1322 return -ERANGE; 1323 return 0; 1324 1325 default: 1326 return std_validate_compound(ctrl, idx, ptr); 1327 } 1328 } 1329 v4l2_ctrl_type_op_validate(const struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr ptr)1330 int v4l2_ctrl_type_op_validate(const struct v4l2_ctrl *ctrl, 1331 union v4l2_ctrl_ptr ptr) 1332 { 1333 unsigned int i; 1334 int ret = 0; 1335 1336 switch ((u32)ctrl->type) { 1337 case V4L2_CTRL_TYPE_U8: 1338 if (ctrl->maximum == 0xff && ctrl->minimum == 0 && ctrl->step == 1) 1339 return 0; 1340 break; 1341 case V4L2_CTRL_TYPE_U16: 1342 if (ctrl->maximum == 0xffff && ctrl->minimum == 0 && ctrl->step == 1) 1343 return 0; 1344 break; 1345 case V4L2_CTRL_TYPE_U32: 1346 if (ctrl->maximum == 0xffffffff && ctrl->minimum == 0 && ctrl->step == 1) 1347 return 0; 1348 break; 1349 1350 case V4L2_CTRL_TYPE_BUTTON: 1351 case V4L2_CTRL_TYPE_CTRL_CLASS: 1352 memset(ptr.p_s32, 0, ctrl->new_elems * sizeof(s32)); 1353 return 0; 1354 } 1355 1356 for (i = 0; !ret && i < ctrl->new_elems; i++) 1357 ret = std_validate_elem(ctrl, i, ptr); 1358 return ret; 1359 } 1360 EXPORT_SYMBOL(v4l2_ctrl_type_op_validate); 1361 1362 static const struct v4l2_ctrl_type_ops std_type_ops = { 1363 .equal = v4l2_ctrl_type_op_equal, 1364 .init = v4l2_ctrl_type_op_init, 1365 .minimum = v4l2_ctrl_type_op_minimum, 1366 .maximum = v4l2_ctrl_type_op_maximum, 1367 .log = v4l2_ctrl_type_op_log, 1368 .validate = v4l2_ctrl_type_op_validate, 1369 }; 1370 v4l2_ctrl_notify(struct v4l2_ctrl * ctrl,v4l2_ctrl_notify_fnc notify,void * priv)1371 void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv) 1372 { 1373 if (!ctrl) 1374 return; 1375 if (!notify) { 1376 ctrl->call_notify = 0; 1377 return; 1378 } 1379 if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify)) 1380 return; 1381 ctrl->handler->notify = notify; 1382 ctrl->handler->notify_priv = priv; 1383 ctrl->call_notify = 1; 1384 } 1385 EXPORT_SYMBOL(v4l2_ctrl_notify); 1386 1387 /* Copy the one value to another. */ ptr_to_ptr(struct v4l2_ctrl * ctrl,union v4l2_ctrl_ptr from,union v4l2_ctrl_ptr to,unsigned int elems)1388 static void ptr_to_ptr(struct v4l2_ctrl *ctrl, 1389 union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to, 1390 unsigned int elems) 1391 { 1392 if (ctrl == NULL) 1393 return; 1394 memcpy(to.p, from.p_const, elems * ctrl->elem_size); 1395 } 1396 1397 /* Copy the new value to the current value. */ new_to_cur(struct v4l2_fh * fh,struct v4l2_ctrl * ctrl,u32 ch_flags)1398 void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags) 1399 { 1400 bool changed; 1401 1402 if (ctrl == NULL) 1403 return; 1404 1405 /* has_changed is set by cluster_changed */ 1406 changed = ctrl->has_changed; 1407 if (changed) { 1408 if (ctrl->is_dyn_array) 1409 ctrl->elems = ctrl->new_elems; 1410 ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur, ctrl->elems); 1411 } 1412 1413 if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) { 1414 /* Note: CH_FLAGS is only set for auto clusters. */ 1415 ctrl->flags &= 1416 ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE); 1417 if (!is_cur_manual(ctrl->cluster[0])) { 1418 ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; 1419 if (ctrl->cluster[0]->has_volatiles) 1420 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 1421 } 1422 fh = NULL; 1423 } 1424 if (changed || ch_flags) { 1425 /* If a control was changed that was not one of the controls 1426 modified by the application, then send the event to all. */ 1427 if (!ctrl->is_new) 1428 fh = NULL; 1429 send_event(fh, ctrl, 1430 (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags); 1431 if (ctrl->call_notify && changed && ctrl->handler->notify) 1432 ctrl->handler->notify(ctrl, ctrl->handler->notify_priv); 1433 } 1434 } 1435 1436 /* Copy the current value to the new value */ cur_to_new(struct v4l2_ctrl * ctrl)1437 void cur_to_new(struct v4l2_ctrl *ctrl) 1438 { 1439 if (ctrl == NULL) 1440 return; 1441 if (ctrl->is_dyn_array) 1442 ctrl->new_elems = ctrl->elems; 1443 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems); 1444 } 1445 req_alloc_array(struct v4l2_ctrl_ref * ref,u32 elems)1446 static bool req_alloc_array(struct v4l2_ctrl_ref *ref, u32 elems) 1447 { 1448 void *tmp; 1449 1450 if (elems == ref->p_req_array_alloc_elems) 1451 return true; 1452 if (ref->ctrl->is_dyn_array && 1453 elems < ref->p_req_array_alloc_elems) 1454 return true; 1455 1456 tmp = kvmalloc(elems * ref->ctrl->elem_size, GFP_KERNEL); 1457 1458 if (!tmp) { 1459 ref->p_req_array_enomem = true; 1460 return false; 1461 } 1462 ref->p_req_array_enomem = false; 1463 kvfree(ref->p_req.p); 1464 ref->p_req.p = tmp; 1465 ref->p_req_array_alloc_elems = elems; 1466 return true; 1467 } 1468 1469 /* Copy the new value to the request value */ new_to_req(struct v4l2_ctrl_ref * ref)1470 void new_to_req(struct v4l2_ctrl_ref *ref) 1471 { 1472 struct v4l2_ctrl *ctrl; 1473 1474 if (!ref) 1475 return; 1476 1477 ctrl = ref->ctrl; 1478 if (ctrl->is_array && !req_alloc_array(ref, ctrl->new_elems)) 1479 return; 1480 1481 ref->p_req_elems = ctrl->new_elems; 1482 ptr_to_ptr(ctrl, ctrl->p_new, ref->p_req, ref->p_req_elems); 1483 ref->p_req_valid = true; 1484 } 1485 1486 /* Copy the current value to the request value */ cur_to_req(struct v4l2_ctrl_ref * ref)1487 void cur_to_req(struct v4l2_ctrl_ref *ref) 1488 { 1489 struct v4l2_ctrl *ctrl; 1490 1491 if (!ref) 1492 return; 1493 1494 ctrl = ref->ctrl; 1495 if (ctrl->is_array && !req_alloc_array(ref, ctrl->elems)) 1496 return; 1497 1498 ref->p_req_elems = ctrl->elems; 1499 ptr_to_ptr(ctrl, ctrl->p_cur, ref->p_req, ctrl->elems); 1500 ref->p_req_valid = true; 1501 } 1502 1503 /* Copy the request value to the new value */ req_to_new(struct v4l2_ctrl_ref * ref)1504 int req_to_new(struct v4l2_ctrl_ref *ref) 1505 { 1506 struct v4l2_ctrl *ctrl; 1507 1508 if (!ref) 1509 return 0; 1510 1511 ctrl = ref->ctrl; 1512 1513 /* 1514 * This control was never set in the request, so just use the current 1515 * value. 1516 */ 1517 if (!ref->p_req_valid) { 1518 if (ctrl->is_dyn_array) 1519 ctrl->new_elems = ctrl->elems; 1520 ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new, ctrl->new_elems); 1521 return 0; 1522 } 1523 1524 /* Not an array, so just copy the request value */ 1525 if (!ctrl->is_array) { 1526 ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems); 1527 return 0; 1528 } 1529 1530 /* Sanity check, should never happen */ 1531 if (WARN_ON(!ref->p_req_array_alloc_elems)) 1532 return -ENOMEM; 1533 1534 if (!ctrl->is_dyn_array && 1535 ref->p_req_elems != ctrl->p_array_alloc_elems) 1536 return -ENOMEM; 1537 1538 /* 1539 * Check if the number of elements in the request is more than the 1540 * elements in ctrl->p_array. If so, attempt to realloc ctrl->p_array. 1541 * Note that p_array is allocated with twice the number of elements 1542 * in the dynamic array since it has to store both the current and 1543 * new value of such a control. 1544 */ 1545 if (ref->p_req_elems > ctrl->p_array_alloc_elems) { 1546 unsigned int sz = ref->p_req_elems * ctrl->elem_size; 1547 void *old = ctrl->p_array; 1548 void *tmp = kvzalloc(2 * sz, GFP_KERNEL); 1549 1550 if (!tmp) 1551 return -ENOMEM; 1552 memcpy(tmp, ctrl->p_new.p, ctrl->elems * ctrl->elem_size); 1553 memcpy(tmp + sz, ctrl->p_cur.p, ctrl->elems * ctrl->elem_size); 1554 ctrl->p_new.p = tmp; 1555 ctrl->p_cur.p = tmp + sz; 1556 ctrl->p_array = tmp; 1557 ctrl->p_array_alloc_elems = ref->p_req_elems; 1558 kvfree(old); 1559 } 1560 1561 ctrl->new_elems = ref->p_req_elems; 1562 ptr_to_ptr(ctrl, ref->p_req, ctrl->p_new, ctrl->new_elems); 1563 return 0; 1564 } 1565 1566 /* Control range checking */ check_range(enum v4l2_ctrl_type type,s64 min,s64 max,u64 step,s64 def)1567 int check_range(enum v4l2_ctrl_type type, 1568 s64 min, s64 max, u64 step, s64 def) 1569 { 1570 switch (type) { 1571 case V4L2_CTRL_TYPE_BOOLEAN: 1572 if (step != 1 || max > 1 || min < 0) 1573 return -ERANGE; 1574 fallthrough; 1575 case V4L2_CTRL_TYPE_U8: 1576 case V4L2_CTRL_TYPE_U16: 1577 case V4L2_CTRL_TYPE_U32: 1578 case V4L2_CTRL_TYPE_INTEGER: 1579 case V4L2_CTRL_TYPE_INTEGER64: 1580 if (step == 0 || min > max || def < min || def > max) 1581 return -ERANGE; 1582 return 0; 1583 case V4L2_CTRL_TYPE_BITMASK: 1584 if (step || min || !max || (def & ~max)) 1585 return -ERANGE; 1586 return 0; 1587 case V4L2_CTRL_TYPE_MENU: 1588 case V4L2_CTRL_TYPE_INTEGER_MENU: 1589 if (min > max || def < min || def > max || 1590 min < 0 || (step && max >= BITS_PER_LONG_LONG)) 1591 return -ERANGE; 1592 /* Note: step == menu_skip_mask for menu controls. 1593 So here we check if the default value is masked out. */ 1594 if (def < BITS_PER_LONG_LONG && (step & BIT_ULL(def))) 1595 return -EINVAL; 1596 return 0; 1597 case V4L2_CTRL_TYPE_STRING: 1598 if (min > max || min < 0 || step < 1 || def) 1599 return -ERANGE; 1600 return 0; 1601 default: 1602 return 0; 1603 } 1604 } 1605 1606 /* Set the handler's error code if it wasn't set earlier already */ handler_set_err(struct v4l2_ctrl_handler * hdl,int err)1607 static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err) 1608 { 1609 if (hdl->error == 0) 1610 hdl->error = err; 1611 return err; 1612 } 1613 1614 /* Initialize the handler */ v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler * hdl,unsigned nr_of_controls_hint,struct lock_class_key * key,const char * name)1615 int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl, 1616 unsigned nr_of_controls_hint, 1617 struct lock_class_key *key, const char *name) 1618 { 1619 mutex_init(&hdl->_lock); 1620 hdl->lock = &hdl->_lock; 1621 lockdep_set_class_and_name(hdl->lock, key, name); 1622 INIT_LIST_HEAD(&hdl->ctrls); 1623 INIT_LIST_HEAD(&hdl->ctrl_refs); 1624 hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8; 1625 hdl->buckets = kvcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]), 1626 GFP_KERNEL); 1627 hdl->error = hdl->buckets ? 0 : -ENOMEM; 1628 v4l2_ctrl_handler_init_request(hdl); 1629 return hdl->error; 1630 } 1631 EXPORT_SYMBOL(v4l2_ctrl_handler_init_class); 1632 1633 /* Free all controls and control refs */ v4l2_ctrl_handler_free(struct v4l2_ctrl_handler * hdl)1634 int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl) 1635 { 1636 struct v4l2_ctrl_ref *ref, *next_ref; 1637 struct v4l2_ctrl *ctrl, *next_ctrl; 1638 struct v4l2_subscribed_event *sev, *next_sev; 1639 1640 if (!hdl) 1641 return 0; 1642 1643 if (!hdl->buckets) 1644 return hdl->error; 1645 1646 v4l2_ctrl_handler_free_request(hdl); 1647 1648 mutex_lock(hdl->lock); 1649 /* Free all nodes */ 1650 list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) { 1651 list_del(&ref->node); 1652 if (ref->p_req_array_alloc_elems) 1653 kvfree(ref->p_req.p); 1654 kfree(ref); 1655 } 1656 /* Free all controls owned by the handler */ 1657 list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) { 1658 list_del(&ctrl->node); 1659 list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node) 1660 list_del(&sev->node); 1661 kvfree(ctrl->p_array); 1662 kvfree(ctrl); 1663 } 1664 kvfree(hdl->buckets); 1665 hdl->buckets = NULL; 1666 hdl->cached = NULL; 1667 mutex_unlock(hdl->lock); 1668 mutex_destroy(&hdl->_lock); 1669 1670 return hdl->error; 1671 } 1672 EXPORT_SYMBOL(v4l2_ctrl_handler_free); 1673 1674 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer 1675 be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing 1676 with applications that do not use the NEXT_CTRL flag. 1677 1678 We just find the n-th private user control. It's O(N), but that should not 1679 be an issue in this particular case. */ find_private_ref(struct v4l2_ctrl_handler * hdl,u32 id)1680 static struct v4l2_ctrl_ref *find_private_ref( 1681 struct v4l2_ctrl_handler *hdl, u32 id) 1682 { 1683 struct v4l2_ctrl_ref *ref; 1684 1685 id -= V4L2_CID_PRIVATE_BASE; 1686 list_for_each_entry(ref, &hdl->ctrl_refs, node) { 1687 /* Search for private user controls that are compatible with 1688 VIDIOC_G/S_CTRL. */ 1689 if (V4L2_CTRL_ID2WHICH(ref->ctrl->id) == V4L2_CTRL_CLASS_USER && 1690 V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) { 1691 if (!ref->ctrl->is_int) 1692 continue; 1693 if (id == 0) 1694 return ref; 1695 id--; 1696 } 1697 } 1698 return NULL; 1699 } 1700 1701 /* Find a control with the given ID. */ find_ref(struct v4l2_ctrl_handler * hdl,u32 id)1702 struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id) 1703 { 1704 struct v4l2_ctrl_ref *ref; 1705 int bucket; 1706 1707 id &= V4L2_CTRL_ID_MASK; 1708 1709 /* Old-style private controls need special handling */ 1710 if (id >= V4L2_CID_PRIVATE_BASE) 1711 return find_private_ref(hdl, id); 1712 bucket = id % hdl->nr_of_buckets; 1713 1714 /* Simple optimization: cache the last control found */ 1715 if (hdl->cached && hdl->cached->ctrl->id == id) 1716 return hdl->cached; 1717 1718 /* Not in cache, search the hash */ 1719 ref = hdl->buckets ? hdl->buckets[bucket] : NULL; 1720 while (ref && ref->ctrl->id != id) 1721 ref = ref->next; 1722 1723 if (ref) 1724 hdl->cached = ref; /* cache it! */ 1725 return ref; 1726 } 1727 1728 /* Find a control with the given ID. Take the handler's lock first. */ find_ref_lock(struct v4l2_ctrl_handler * hdl,u32 id)1729 struct v4l2_ctrl_ref *find_ref_lock(struct v4l2_ctrl_handler *hdl, u32 id) 1730 { 1731 struct v4l2_ctrl_ref *ref = NULL; 1732 1733 if (hdl) { 1734 mutex_lock(hdl->lock); 1735 ref = find_ref(hdl, id); 1736 mutex_unlock(hdl->lock); 1737 } 1738 return ref; 1739 } 1740 1741 /* Find a control with the given ID. */ v4l2_ctrl_find(struct v4l2_ctrl_handler * hdl,u32 id)1742 struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id) 1743 { 1744 struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id); 1745 1746 return ref ? ref->ctrl : NULL; 1747 } 1748 EXPORT_SYMBOL(v4l2_ctrl_find); 1749 1750 /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */ handler_new_ref(struct v4l2_ctrl_handler * hdl,struct v4l2_ctrl * ctrl,struct v4l2_ctrl_ref ** ctrl_ref,bool from_other_dev,bool allocate_req)1751 int handler_new_ref(struct v4l2_ctrl_handler *hdl, 1752 struct v4l2_ctrl *ctrl, 1753 struct v4l2_ctrl_ref **ctrl_ref, 1754 bool from_other_dev, bool allocate_req) 1755 { 1756 struct v4l2_ctrl_ref *ref; 1757 struct v4l2_ctrl_ref *new_ref; 1758 u32 id = ctrl->id; 1759 u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1; 1760 int bucket = id % hdl->nr_of_buckets; /* which bucket to use */ 1761 unsigned int size_extra_req = 0; 1762 1763 if (ctrl_ref) 1764 *ctrl_ref = NULL; 1765 1766 /* 1767 * Automatically add the control class if it is not yet present and 1768 * the new control is not a compound control. 1769 */ 1770 if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES && 1771 id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL) 1772 if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0)) 1773 return hdl->error; 1774 1775 if (hdl->error) 1776 return hdl->error; 1777 1778 if (allocate_req && !ctrl->is_array) 1779 size_extra_req = ctrl->elems * ctrl->elem_size; 1780 new_ref = kzalloc(sizeof(*new_ref) + size_extra_req, GFP_KERNEL); 1781 if (!new_ref) 1782 return handler_set_err(hdl, -ENOMEM); 1783 new_ref->ctrl = ctrl; 1784 new_ref->from_other_dev = from_other_dev; 1785 if (size_extra_req) 1786 new_ref->p_req.p = &new_ref[1]; 1787 1788 INIT_LIST_HEAD(&new_ref->node); 1789 1790 mutex_lock(hdl->lock); 1791 1792 /* Add immediately at the end of the list if the list is empty, or if 1793 the last element in the list has a lower ID. 1794 This ensures that when elements are added in ascending order the 1795 insertion is an O(1) operation. */ 1796 if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) { 1797 list_add_tail(&new_ref->node, &hdl->ctrl_refs); 1798 goto insert_in_hash; 1799 } 1800 1801 /* Find insert position in sorted list */ 1802 list_for_each_entry(ref, &hdl->ctrl_refs, node) { 1803 if (ref->ctrl->id < id) 1804 continue; 1805 /* Don't add duplicates */ 1806 if (ref->ctrl->id == id) { 1807 kfree(new_ref); 1808 goto unlock; 1809 } 1810 list_add(&new_ref->node, ref->node.prev); 1811 break; 1812 } 1813 1814 insert_in_hash: 1815 /* Insert the control node in the hash */ 1816 new_ref->next = hdl->buckets[bucket]; 1817 hdl->buckets[bucket] = new_ref; 1818 if (ctrl_ref) 1819 *ctrl_ref = new_ref; 1820 if (ctrl->handler == hdl) { 1821 /* By default each control starts in a cluster of its own. 1822 * new_ref->ctrl is basically a cluster array with one 1823 * element, so that's perfect to use as the cluster pointer. 1824 * But only do this for the handler that owns the control. 1825 */ 1826 ctrl->cluster = &new_ref->ctrl; 1827 ctrl->ncontrols = 1; 1828 } 1829 1830 unlock: 1831 mutex_unlock(hdl->lock); 1832 return 0; 1833 } 1834 1835 /* Add a new control */ v4l2_ctrl_new(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,const struct v4l2_ctrl_type_ops * type_ops,u32 id,const char * name,enum v4l2_ctrl_type type,s64 min,s64 max,u64 step,s64 def,const u32 dims[V4L2_CTRL_MAX_DIMS],u32 elem_size,u32 flags,const char * const * qmenu,const s64 * qmenu_int,const union v4l2_ctrl_ptr p_def,const union v4l2_ctrl_ptr p_min,const union v4l2_ctrl_ptr p_max,void * priv)1836 static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, 1837 const struct v4l2_ctrl_ops *ops, 1838 const struct v4l2_ctrl_type_ops *type_ops, 1839 u32 id, const char *name, enum v4l2_ctrl_type type, 1840 s64 min, s64 max, u64 step, s64 def, 1841 const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size, 1842 u32 flags, const char * const *qmenu, 1843 const s64 *qmenu_int, 1844 const union v4l2_ctrl_ptr p_def, 1845 const union v4l2_ctrl_ptr p_min, 1846 const union v4l2_ctrl_ptr p_max, 1847 void *priv) 1848 { 1849 struct v4l2_ctrl *ctrl; 1850 unsigned sz_extra; 1851 unsigned nr_of_dims = 0; 1852 unsigned elems = 1; 1853 bool is_array; 1854 unsigned tot_ctrl_size; 1855 void *data; 1856 int err; 1857 1858 if (hdl->error) 1859 return NULL; 1860 1861 while (dims && dims[nr_of_dims]) { 1862 elems *= dims[nr_of_dims]; 1863 nr_of_dims++; 1864 if (nr_of_dims == V4L2_CTRL_MAX_DIMS) 1865 break; 1866 } 1867 is_array = nr_of_dims > 0; 1868 1869 /* Prefill elem_size for all types handled by std_type_ops */ 1870 switch ((u32)type) { 1871 case V4L2_CTRL_TYPE_INTEGER64: 1872 elem_size = sizeof(s64); 1873 break; 1874 case V4L2_CTRL_TYPE_STRING: 1875 elem_size = max + 1; 1876 break; 1877 case V4L2_CTRL_TYPE_U8: 1878 elem_size = sizeof(u8); 1879 break; 1880 case V4L2_CTRL_TYPE_U16: 1881 elem_size = sizeof(u16); 1882 break; 1883 case V4L2_CTRL_TYPE_U32: 1884 elem_size = sizeof(u32); 1885 break; 1886 case V4L2_CTRL_TYPE_MPEG2_SEQUENCE: 1887 elem_size = sizeof(struct v4l2_ctrl_mpeg2_sequence); 1888 break; 1889 case V4L2_CTRL_TYPE_MPEG2_PICTURE: 1890 elem_size = sizeof(struct v4l2_ctrl_mpeg2_picture); 1891 break; 1892 case V4L2_CTRL_TYPE_MPEG2_QUANTISATION: 1893 elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantisation); 1894 break; 1895 case V4L2_CTRL_TYPE_FWHT_PARAMS: 1896 elem_size = sizeof(struct v4l2_ctrl_fwht_params); 1897 break; 1898 case V4L2_CTRL_TYPE_H264_SPS: 1899 elem_size = sizeof(struct v4l2_ctrl_h264_sps); 1900 break; 1901 case V4L2_CTRL_TYPE_H264_PPS: 1902 elem_size = sizeof(struct v4l2_ctrl_h264_pps); 1903 break; 1904 case V4L2_CTRL_TYPE_H264_SCALING_MATRIX: 1905 elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix); 1906 break; 1907 case V4L2_CTRL_TYPE_H264_SLICE_PARAMS: 1908 elem_size = sizeof(struct v4l2_ctrl_h264_slice_params); 1909 break; 1910 case V4L2_CTRL_TYPE_H264_DECODE_PARAMS: 1911 elem_size = sizeof(struct v4l2_ctrl_h264_decode_params); 1912 break; 1913 case V4L2_CTRL_TYPE_H264_PRED_WEIGHTS: 1914 elem_size = sizeof(struct v4l2_ctrl_h264_pred_weights); 1915 break; 1916 case V4L2_CTRL_TYPE_VP8_FRAME: 1917 elem_size = sizeof(struct v4l2_ctrl_vp8_frame); 1918 break; 1919 case V4L2_CTRL_TYPE_HEVC_SPS: 1920 elem_size = sizeof(struct v4l2_ctrl_hevc_sps); 1921 break; 1922 case V4L2_CTRL_TYPE_HEVC_PPS: 1923 elem_size = sizeof(struct v4l2_ctrl_hevc_pps); 1924 break; 1925 case V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS: 1926 elem_size = sizeof(struct v4l2_ctrl_hevc_slice_params); 1927 break; 1928 case V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX: 1929 elem_size = sizeof(struct v4l2_ctrl_hevc_scaling_matrix); 1930 break; 1931 case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS: 1932 elem_size = sizeof(struct v4l2_ctrl_hevc_decode_params); 1933 break; 1934 case V4L2_CTRL_TYPE_HDR10_CLL_INFO: 1935 elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info); 1936 break; 1937 case V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY: 1938 elem_size = sizeof(struct v4l2_ctrl_hdr10_mastering_display); 1939 break; 1940 case V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR: 1941 elem_size = sizeof(struct v4l2_ctrl_vp9_compressed_hdr); 1942 break; 1943 case V4L2_CTRL_TYPE_VP9_FRAME: 1944 elem_size = sizeof(struct v4l2_ctrl_vp9_frame); 1945 break; 1946 case V4L2_CTRL_TYPE_AV1_SEQUENCE: 1947 elem_size = sizeof(struct v4l2_ctrl_av1_sequence); 1948 break; 1949 case V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY: 1950 elem_size = sizeof(struct v4l2_ctrl_av1_tile_group_entry); 1951 break; 1952 case V4L2_CTRL_TYPE_AV1_FRAME: 1953 elem_size = sizeof(struct v4l2_ctrl_av1_frame); 1954 break; 1955 case V4L2_CTRL_TYPE_AV1_FILM_GRAIN: 1956 elem_size = sizeof(struct v4l2_ctrl_av1_film_grain); 1957 break; 1958 case V4L2_CTRL_TYPE_AREA: 1959 elem_size = sizeof(struct v4l2_area); 1960 break; 1961 case V4L2_CTRL_TYPE_RECT: 1962 elem_size = sizeof(struct v4l2_rect); 1963 break; 1964 default: 1965 if (type < V4L2_CTRL_COMPOUND_TYPES) 1966 elem_size = sizeof(s32); 1967 break; 1968 } 1969 1970 if (type < V4L2_CTRL_COMPOUND_TYPES && 1971 type != V4L2_CTRL_TYPE_BUTTON && 1972 type != V4L2_CTRL_TYPE_CTRL_CLASS && 1973 type != V4L2_CTRL_TYPE_STRING) 1974 flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX; 1975 1976 /* Sanity checks */ 1977 if (id == 0 || name == NULL || !elem_size || 1978 id >= V4L2_CID_PRIVATE_BASE || 1979 (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) || 1980 (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) { 1981 handler_set_err(hdl, -ERANGE); 1982 return NULL; 1983 } 1984 1985 err = check_range(type, min, max, step, def); 1986 if (err) { 1987 handler_set_err(hdl, err); 1988 return NULL; 1989 } 1990 if (is_array && 1991 (type == V4L2_CTRL_TYPE_BUTTON || 1992 type == V4L2_CTRL_TYPE_CTRL_CLASS)) { 1993 handler_set_err(hdl, -EINVAL); 1994 return NULL; 1995 } 1996 if (flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY) { 1997 /* 1998 * For now only support this for one-dimensional arrays only. 1999 * 2000 * This can be relaxed in the future, but this will 2001 * require more effort. 2002 */ 2003 if (nr_of_dims != 1) { 2004 handler_set_err(hdl, -EINVAL); 2005 return NULL; 2006 } 2007 /* Start with just 1 element */ 2008 elems = 1; 2009 } 2010 2011 tot_ctrl_size = elem_size * elems; 2012 sz_extra = 0; 2013 if (type == V4L2_CTRL_TYPE_BUTTON) 2014 flags |= V4L2_CTRL_FLAG_WRITE_ONLY | 2015 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; 2016 else if (type == V4L2_CTRL_TYPE_CTRL_CLASS) 2017 flags |= V4L2_CTRL_FLAG_READ_ONLY; 2018 else if (!is_array && 2019 (type == V4L2_CTRL_TYPE_INTEGER64 || 2020 type == V4L2_CTRL_TYPE_STRING || 2021 type >= V4L2_CTRL_COMPOUND_TYPES)) 2022 sz_extra += 2 * tot_ctrl_size; 2023 2024 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) 2025 sz_extra += elem_size; 2026 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_min.p_const) 2027 sz_extra += elem_size; 2028 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_max.p_const) 2029 sz_extra += elem_size; 2030 2031 ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL); 2032 if (ctrl == NULL) { 2033 handler_set_err(hdl, -ENOMEM); 2034 return NULL; 2035 } 2036 2037 INIT_LIST_HEAD(&ctrl->node); 2038 INIT_LIST_HEAD(&ctrl->ev_subs); 2039 ctrl->handler = hdl; 2040 ctrl->ops = ops; 2041 ctrl->type_ops = type_ops ? type_ops : &std_type_ops; 2042 ctrl->id = id; 2043 ctrl->name = name; 2044 ctrl->type = type; 2045 ctrl->flags = flags; 2046 ctrl->minimum = min; 2047 ctrl->maximum = max; 2048 ctrl->step = step; 2049 ctrl->default_value = def; 2050 ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING; 2051 ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string; 2052 ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64; 2053 ctrl->is_array = is_array; 2054 ctrl->is_dyn_array = !!(flags & V4L2_CTRL_FLAG_DYNAMIC_ARRAY); 2055 ctrl->elems = elems; 2056 ctrl->new_elems = elems; 2057 ctrl->nr_of_dims = nr_of_dims; 2058 if (nr_of_dims) 2059 memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0])); 2060 ctrl->elem_size = elem_size; 2061 if (type == V4L2_CTRL_TYPE_MENU) 2062 ctrl->qmenu = qmenu; 2063 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU) 2064 ctrl->qmenu_int = qmenu_int; 2065 ctrl->priv = priv; 2066 ctrl->cur.val = ctrl->val = def; 2067 data = &ctrl[1]; 2068 2069 if (ctrl->is_array) { 2070 ctrl->p_array_alloc_elems = elems; 2071 ctrl->p_array = kvzalloc(2 * elems * elem_size, GFP_KERNEL); 2072 if (!ctrl->p_array) { 2073 kvfree(ctrl); 2074 return NULL; 2075 } 2076 data = ctrl->p_array; 2077 } 2078 2079 if (!ctrl->is_int) { 2080 ctrl->p_new.p = data; 2081 ctrl->p_cur.p = data + tot_ctrl_size; 2082 } else { 2083 ctrl->p_new.p = &ctrl->val; 2084 ctrl->p_cur.p = &ctrl->cur.val; 2085 } 2086 2087 if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) { 2088 if (ctrl->is_array) 2089 ctrl->p_def.p = &ctrl[1]; 2090 else 2091 ctrl->p_def.p = ctrl->p_cur.p + tot_ctrl_size; 2092 memcpy(ctrl->p_def.p, p_def.p_const, elem_size); 2093 } 2094 2095 if (flags & V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX) { 2096 void *ptr = ctrl->p_def.p; 2097 2098 if (p_min.p_const) { 2099 ptr += elem_size; 2100 ctrl->p_min.p = ptr; 2101 memcpy(ctrl->p_min.p, p_min.p_const, elem_size); 2102 } 2103 2104 if (p_max.p_const) { 2105 ptr += elem_size; 2106 ctrl->p_max.p = ptr; 2107 memcpy(ctrl->p_max.p, p_max.p_const, elem_size); 2108 } 2109 } 2110 2111 ctrl->type_ops->init(ctrl, 0, ctrl->p_cur); 2112 cur_to_new(ctrl); 2113 2114 if (handler_new_ref(hdl, ctrl, NULL, false, false)) { 2115 kvfree(ctrl->p_array); 2116 kvfree(ctrl); 2117 return NULL; 2118 } 2119 mutex_lock(hdl->lock); 2120 list_add_tail(&ctrl->node, &hdl->ctrls); 2121 mutex_unlock(hdl->lock); 2122 return ctrl; 2123 } 2124 v4l2_ctrl_new_custom(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_config * cfg,void * priv)2125 struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl, 2126 const struct v4l2_ctrl_config *cfg, void *priv) 2127 { 2128 bool is_menu; 2129 struct v4l2_ctrl *ctrl; 2130 const char *name = cfg->name; 2131 const char * const *qmenu = cfg->qmenu; 2132 const s64 *qmenu_int = cfg->qmenu_int; 2133 enum v4l2_ctrl_type type = cfg->type; 2134 u32 flags = cfg->flags; 2135 s64 min = cfg->min; 2136 s64 max = cfg->max; 2137 u64 step = cfg->step; 2138 s64 def = cfg->def; 2139 2140 if (name == NULL) 2141 v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step, 2142 &def, &flags); 2143 2144 is_menu = (type == V4L2_CTRL_TYPE_MENU || 2145 type == V4L2_CTRL_TYPE_INTEGER_MENU); 2146 if (is_menu) 2147 WARN_ON(step); 2148 else 2149 WARN_ON(cfg->menu_skip_mask); 2150 if (type == V4L2_CTRL_TYPE_MENU && !qmenu) { 2151 qmenu = v4l2_ctrl_get_menu(cfg->id); 2152 } else if (type == V4L2_CTRL_TYPE_INTEGER_MENU && !qmenu_int) { 2153 handler_set_err(hdl, -EINVAL); 2154 return NULL; 2155 } 2156 2157 ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name, 2158 type, min, max, 2159 is_menu ? cfg->menu_skip_mask : step, def, 2160 cfg->dims, cfg->elem_size, 2161 flags, qmenu, qmenu_int, cfg->p_def, cfg->p_min, 2162 cfg->p_max, priv); 2163 if (ctrl) 2164 ctrl->is_private = cfg->is_private; 2165 return ctrl; 2166 } 2167 EXPORT_SYMBOL(v4l2_ctrl_new_custom); 2168 2169 /* Helper function for standard non-menu controls */ v4l2_ctrl_new_std(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,s64 min,s64 max,u64 step,s64 def)2170 struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl, 2171 const struct v4l2_ctrl_ops *ops, 2172 u32 id, s64 min, s64 max, u64 step, s64 def) 2173 { 2174 const char *name; 2175 enum v4l2_ctrl_type type; 2176 u32 flags; 2177 2178 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 2179 if (type == V4L2_CTRL_TYPE_MENU || 2180 type == V4L2_CTRL_TYPE_INTEGER_MENU || 2181 type >= V4L2_CTRL_COMPOUND_TYPES) { 2182 handler_set_err(hdl, -EINVAL); 2183 return NULL; 2184 } 2185 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 2186 min, max, step, def, NULL, 0, 2187 flags, NULL, NULL, ptr_null, ptr_null, 2188 ptr_null, NULL); 2189 } 2190 EXPORT_SYMBOL(v4l2_ctrl_new_std); 2191 2192 /* Helper function for standard menu controls */ v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,u8 _max,u64 mask,u8 _def)2193 struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl, 2194 const struct v4l2_ctrl_ops *ops, 2195 u32 id, u8 _max, u64 mask, u8 _def) 2196 { 2197 const char * const *qmenu = NULL; 2198 const s64 *qmenu_int = NULL; 2199 unsigned int qmenu_int_len = 0; 2200 const char *name; 2201 enum v4l2_ctrl_type type; 2202 s64 min; 2203 s64 max = _max; 2204 s64 def = _def; 2205 u64 step; 2206 u32 flags; 2207 2208 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 2209 2210 if (type == V4L2_CTRL_TYPE_MENU) 2211 qmenu = v4l2_ctrl_get_menu(id); 2212 else if (type == V4L2_CTRL_TYPE_INTEGER_MENU) 2213 qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len); 2214 2215 if ((!qmenu && !qmenu_int) || (qmenu_int && max >= qmenu_int_len)) { 2216 handler_set_err(hdl, -EINVAL); 2217 return NULL; 2218 } 2219 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 2220 0, max, mask, def, NULL, 0, 2221 flags, qmenu, qmenu_int, ptr_null, ptr_null, 2222 ptr_null, NULL); 2223 } 2224 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu); 2225 2226 /* Helper function for standard menu controls with driver defined menu */ v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,u8 _max,u64 mask,u8 _def,const char * const * qmenu)2227 struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl, 2228 const struct v4l2_ctrl_ops *ops, u32 id, u8 _max, 2229 u64 mask, u8 _def, const char * const *qmenu) 2230 { 2231 enum v4l2_ctrl_type type; 2232 const char *name; 2233 u32 flags; 2234 u64 step; 2235 s64 min; 2236 s64 max = _max; 2237 s64 def = _def; 2238 2239 /* v4l2_ctrl_new_std_menu_items() should only be called for 2240 * standard controls without a standard menu. 2241 */ 2242 if (v4l2_ctrl_get_menu(id)) { 2243 handler_set_err(hdl, -EINVAL); 2244 return NULL; 2245 } 2246 2247 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 2248 if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) { 2249 handler_set_err(hdl, -EINVAL); 2250 return NULL; 2251 } 2252 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 2253 0, max, mask, def, NULL, 0, 2254 flags, qmenu, NULL, ptr_null, ptr_null, 2255 ptr_null, NULL); 2256 2257 } 2258 EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items); 2259 2260 /* Helper function for standard compound controls */ v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,const union v4l2_ctrl_ptr p_def,const union v4l2_ctrl_ptr p_min,const union v4l2_ctrl_ptr p_max)2261 struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl, 2262 const struct v4l2_ctrl_ops *ops, u32 id, 2263 const union v4l2_ctrl_ptr p_def, 2264 const union v4l2_ctrl_ptr p_min, 2265 const union v4l2_ctrl_ptr p_max) 2266 { 2267 const char *name; 2268 enum v4l2_ctrl_type type; 2269 u32 flags; 2270 s64 min, max, step, def; 2271 2272 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 2273 if (type < V4L2_CTRL_COMPOUND_TYPES) { 2274 handler_set_err(hdl, -EINVAL); 2275 return NULL; 2276 } 2277 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 2278 min, max, step, def, NULL, 0, 2279 flags, NULL, NULL, p_def, p_min, p_max, NULL); 2280 } 2281 EXPORT_SYMBOL(v4l2_ctrl_new_std_compound); 2282 2283 /* Helper function for standard integer menu controls */ v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ops,u32 id,u8 _max,u8 _def,const s64 * qmenu_int)2284 struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl, 2285 const struct v4l2_ctrl_ops *ops, 2286 u32 id, u8 _max, u8 _def, const s64 *qmenu_int) 2287 { 2288 const char *name; 2289 enum v4l2_ctrl_type type; 2290 s64 min; 2291 u64 step; 2292 s64 max = _max; 2293 s64 def = _def; 2294 u32 flags; 2295 2296 v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags); 2297 if (type != V4L2_CTRL_TYPE_INTEGER_MENU) { 2298 handler_set_err(hdl, -EINVAL); 2299 return NULL; 2300 } 2301 return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 2302 0, max, 0, def, NULL, 0, 2303 flags, NULL, qmenu_int, ptr_null, ptr_null, 2304 ptr_null, NULL); 2305 } 2306 EXPORT_SYMBOL(v4l2_ctrl_new_int_menu); 2307 2308 /* Add the controls from another handler to our own. */ v4l2_ctrl_add_handler(struct v4l2_ctrl_handler * hdl,struct v4l2_ctrl_handler * add,bool (* filter)(const struct v4l2_ctrl * ctrl),bool from_other_dev)2309 int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl, 2310 struct v4l2_ctrl_handler *add, 2311 bool (*filter)(const struct v4l2_ctrl *ctrl), 2312 bool from_other_dev) 2313 { 2314 struct v4l2_ctrl_ref *ref; 2315 int ret = 0; 2316 2317 /* Do nothing if either handler is NULL or if they are the same */ 2318 if (!hdl || !add || hdl == add) 2319 return 0; 2320 if (hdl->error) 2321 return hdl->error; 2322 mutex_lock(add->lock); 2323 list_for_each_entry(ref, &add->ctrl_refs, node) { 2324 struct v4l2_ctrl *ctrl = ref->ctrl; 2325 2326 /* Skip handler-private controls. */ 2327 if (ctrl->is_private) 2328 continue; 2329 /* And control classes */ 2330 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS) 2331 continue; 2332 /* Filter any unwanted controls */ 2333 if (filter && !filter(ctrl)) 2334 continue; 2335 ret = handler_new_ref(hdl, ctrl, NULL, from_other_dev, false); 2336 if (ret) 2337 break; 2338 } 2339 mutex_unlock(add->lock); 2340 return ret; 2341 } 2342 EXPORT_SYMBOL(v4l2_ctrl_add_handler); 2343 v4l2_ctrl_radio_filter(const struct v4l2_ctrl * ctrl)2344 bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl) 2345 { 2346 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_TX) 2347 return true; 2348 if (V4L2_CTRL_ID2WHICH(ctrl->id) == V4L2_CTRL_CLASS_FM_RX) 2349 return true; 2350 switch (ctrl->id) { 2351 case V4L2_CID_AUDIO_MUTE: 2352 case V4L2_CID_AUDIO_VOLUME: 2353 case V4L2_CID_AUDIO_BALANCE: 2354 case V4L2_CID_AUDIO_BASS: 2355 case V4L2_CID_AUDIO_TREBLE: 2356 case V4L2_CID_AUDIO_LOUDNESS: 2357 return true; 2358 default: 2359 break; 2360 } 2361 return false; 2362 } 2363 EXPORT_SYMBOL(v4l2_ctrl_radio_filter); 2364 2365 /* Cluster controls */ v4l2_ctrl_cluster(unsigned ncontrols,struct v4l2_ctrl ** controls)2366 void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls) 2367 { 2368 bool has_volatiles = false; 2369 int i; 2370 2371 /* The first control is the master control and it must not be NULL */ 2372 if (WARN_ON(ncontrols == 0 || controls[0] == NULL)) 2373 return; 2374 2375 for (i = 0; i < ncontrols; i++) { 2376 if (controls[i]) { 2377 controls[i]->cluster = controls; 2378 controls[i]->ncontrols = ncontrols; 2379 if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE) 2380 has_volatiles = true; 2381 } 2382 } 2383 controls[0]->has_volatiles = has_volatiles; 2384 } 2385 EXPORT_SYMBOL(v4l2_ctrl_cluster); 2386 v4l2_ctrl_auto_cluster(unsigned ncontrols,struct v4l2_ctrl ** controls,u8 manual_val,bool set_volatile)2387 void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls, 2388 u8 manual_val, bool set_volatile) 2389 { 2390 struct v4l2_ctrl *master = controls[0]; 2391 u32 flag = 0; 2392 int i; 2393 2394 v4l2_ctrl_cluster(ncontrols, controls); 2395 WARN_ON(ncontrols <= 1); 2396 WARN_ON(manual_val < master->minimum || manual_val > master->maximum); 2397 WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl)); 2398 master->is_auto = true; 2399 master->has_volatiles = set_volatile; 2400 master->manual_mode_value = manual_val; 2401 master->flags |= V4L2_CTRL_FLAG_UPDATE; 2402 2403 if (!is_cur_manual(master)) 2404 flag = V4L2_CTRL_FLAG_INACTIVE | 2405 (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0); 2406 2407 for (i = 1; i < ncontrols; i++) 2408 if (controls[i]) 2409 controls[i]->flags |= flag; 2410 } 2411 EXPORT_SYMBOL(v4l2_ctrl_auto_cluster); 2412 2413 /* 2414 * Obtain the current volatile values of an autocluster and mark them 2415 * as new. 2416 */ update_from_auto_cluster(struct v4l2_ctrl * master)2417 void update_from_auto_cluster(struct v4l2_ctrl *master) 2418 { 2419 int i; 2420 2421 for (i = 1; i < master->ncontrols; i++) 2422 cur_to_new(master->cluster[i]); 2423 if (!call_op(master, g_volatile_ctrl)) 2424 for (i = 1; i < master->ncontrols; i++) 2425 if (master->cluster[i]) 2426 master->cluster[i]->is_new = 1; 2427 } 2428 2429 /* 2430 * Return non-zero if one or more of the controls in the cluster has a new 2431 * value that differs from the current value. 2432 */ cluster_changed(struct v4l2_ctrl * master)2433 static int cluster_changed(struct v4l2_ctrl *master) 2434 { 2435 bool changed = false; 2436 int i; 2437 2438 for (i = 0; i < master->ncontrols; i++) { 2439 struct v4l2_ctrl *ctrl = master->cluster[i]; 2440 bool ctrl_changed = false; 2441 2442 if (!ctrl) 2443 continue; 2444 2445 if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE) { 2446 changed = true; 2447 ctrl_changed = true; 2448 } 2449 2450 /* 2451 * Set has_changed to false to avoid generating 2452 * the event V4L2_EVENT_CTRL_CH_VALUE 2453 */ 2454 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) { 2455 ctrl->has_changed = false; 2456 continue; 2457 } 2458 2459 if (ctrl->elems != ctrl->new_elems) 2460 ctrl_changed = true; 2461 if (!ctrl_changed) 2462 ctrl_changed = !ctrl->type_ops->equal(ctrl, 2463 ctrl->p_cur, ctrl->p_new); 2464 ctrl->has_changed = ctrl_changed; 2465 changed |= ctrl->has_changed; 2466 } 2467 return changed; 2468 } 2469 2470 /* 2471 * Core function that calls try/s_ctrl and ensures that the new value is 2472 * copied to the current value on a set. 2473 * Must be called with ctrl->handler->lock held. 2474 */ try_or_set_cluster(struct v4l2_fh * fh,struct v4l2_ctrl * master,bool set,u32 ch_flags)2475 int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master, 2476 bool set, u32 ch_flags) 2477 { 2478 bool update_flag; 2479 int ret; 2480 int i; 2481 2482 /* 2483 * Go through the cluster and either validate the new value or 2484 * (if no new value was set), copy the current value to the new 2485 * value, ensuring a consistent view for the control ops when 2486 * called. 2487 */ 2488 for (i = 0; i < master->ncontrols; i++) { 2489 struct v4l2_ctrl *ctrl = master->cluster[i]; 2490 2491 if (!ctrl) 2492 continue; 2493 2494 if (!ctrl->is_new) { 2495 cur_to_new(ctrl); 2496 continue; 2497 } 2498 /* 2499 * Check again: it may have changed since the 2500 * previous check in try_or_set_ext_ctrls(). 2501 */ 2502 if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)) 2503 return -EBUSY; 2504 } 2505 2506 ret = call_op(master, try_ctrl); 2507 2508 /* Don't set if there is no change */ 2509 if (ret || !set || !cluster_changed(master)) 2510 return ret; 2511 ret = call_op(master, s_ctrl); 2512 if (ret) 2513 return ret; 2514 2515 /* If OK, then make the new values permanent. */ 2516 update_flag = is_cur_manual(master) != is_new_manual(master); 2517 2518 for (i = 0; i < master->ncontrols; i++) { 2519 /* 2520 * If we switch from auto to manual mode, and this cluster 2521 * contains volatile controls, then all non-master controls 2522 * have to be marked as changed. The 'new' value contains 2523 * the volatile value (obtained by update_from_auto_cluster), 2524 * which now has to become the current value. 2525 */ 2526 if (i && update_flag && is_new_manual(master) && 2527 master->has_volatiles && master->cluster[i]) 2528 master->cluster[i]->has_changed = true; 2529 2530 new_to_cur(fh, master->cluster[i], ch_flags | 2531 ((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0)); 2532 } 2533 return 0; 2534 } 2535 2536 /* Activate/deactivate a control. */ v4l2_ctrl_activate(struct v4l2_ctrl * ctrl,bool active)2537 void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active) 2538 { 2539 /* invert since the actual flag is called 'inactive' */ 2540 bool inactive = !active; 2541 bool old; 2542 2543 if (ctrl == NULL) 2544 return; 2545 2546 if (inactive) 2547 /* set V4L2_CTRL_FLAG_INACTIVE */ 2548 old = test_and_set_bit(4, &ctrl->flags); 2549 else 2550 /* clear V4L2_CTRL_FLAG_INACTIVE */ 2551 old = test_and_clear_bit(4, &ctrl->flags); 2552 if (old != inactive) 2553 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS); 2554 } 2555 EXPORT_SYMBOL(v4l2_ctrl_activate); 2556 __v4l2_ctrl_grab(struct v4l2_ctrl * ctrl,bool grabbed)2557 void __v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed) 2558 { 2559 bool old; 2560 2561 if (ctrl == NULL) 2562 return; 2563 2564 lockdep_assert_held(ctrl->handler->lock); 2565 2566 if (grabbed) 2567 /* set V4L2_CTRL_FLAG_GRABBED */ 2568 old = test_and_set_bit(1, &ctrl->flags); 2569 else 2570 /* clear V4L2_CTRL_FLAG_GRABBED */ 2571 old = test_and_clear_bit(1, &ctrl->flags); 2572 if (old != grabbed) 2573 send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS); 2574 } 2575 EXPORT_SYMBOL(__v4l2_ctrl_grab); 2576 2577 /* Call s_ctrl for all controls owned by the handler */ __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler * hdl)2578 int __v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl) 2579 { 2580 struct v4l2_ctrl *ctrl; 2581 int ret = 0; 2582 2583 if (hdl == NULL) 2584 return 0; 2585 2586 lockdep_assert_held(hdl->lock); 2587 2588 list_for_each_entry(ctrl, &hdl->ctrls, node) 2589 ctrl->done = false; 2590 2591 list_for_each_entry(ctrl, &hdl->ctrls, node) { 2592 struct v4l2_ctrl *master = ctrl->cluster[0]; 2593 int i; 2594 2595 /* Skip if this control was already handled by a cluster. */ 2596 /* Skip button controls and read-only controls. */ 2597 if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON || 2598 (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)) 2599 continue; 2600 2601 for (i = 0; i < master->ncontrols; i++) { 2602 if (master->cluster[i]) { 2603 cur_to_new(master->cluster[i]); 2604 master->cluster[i]->is_new = 1; 2605 master->cluster[i]->done = true; 2606 } 2607 } 2608 ret = call_op(master, s_ctrl); 2609 if (ret) 2610 break; 2611 } 2612 2613 return ret; 2614 } 2615 EXPORT_SYMBOL_GPL(__v4l2_ctrl_handler_setup); 2616 v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler * hdl)2617 int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl) 2618 { 2619 int ret; 2620 2621 if (hdl == NULL) 2622 return 0; 2623 2624 mutex_lock(hdl->lock); 2625 ret = __v4l2_ctrl_handler_setup(hdl); 2626 mutex_unlock(hdl->lock); 2627 2628 return ret; 2629 } 2630 EXPORT_SYMBOL(v4l2_ctrl_handler_setup); 2631 2632 /* Log the control name and value */ log_ctrl(const struct v4l2_ctrl * ctrl,const char * prefix,const char * colon)2633 static void log_ctrl(const struct v4l2_ctrl *ctrl, 2634 const char *prefix, const char *colon) 2635 { 2636 if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY)) 2637 return; 2638 if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS) 2639 return; 2640 2641 pr_info("%s%s%s: ", prefix, colon, ctrl->name); 2642 2643 ctrl->type_ops->log(ctrl); 2644 2645 if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE | 2646 V4L2_CTRL_FLAG_GRABBED | 2647 V4L2_CTRL_FLAG_VOLATILE)) { 2648 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE) 2649 pr_cont(" inactive"); 2650 if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED) 2651 pr_cont(" grabbed"); 2652 if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) 2653 pr_cont(" volatile"); 2654 } 2655 pr_cont("\n"); 2656 } 2657 2658 /* Log all controls owned by the handler */ v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler * hdl,const char * prefix)2659 void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl, 2660 const char *prefix) 2661 { 2662 struct v4l2_ctrl *ctrl; 2663 const char *colon = ""; 2664 int len; 2665 2666 if (!hdl) 2667 return; 2668 if (!prefix) 2669 prefix = ""; 2670 len = strlen(prefix); 2671 if (len && prefix[len - 1] != ' ') 2672 colon = ": "; 2673 mutex_lock(hdl->lock); 2674 list_for_each_entry(ctrl, &hdl->ctrls, node) 2675 if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED)) 2676 log_ctrl(ctrl, prefix, colon); 2677 mutex_unlock(hdl->lock); 2678 } 2679 EXPORT_SYMBOL(v4l2_ctrl_handler_log_status); 2680 v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler * hdl,const struct v4l2_ctrl_ops * ctrl_ops,const struct v4l2_fwnode_device_properties * p)2681 int v4l2_ctrl_new_fwnode_properties(struct v4l2_ctrl_handler *hdl, 2682 const struct v4l2_ctrl_ops *ctrl_ops, 2683 const struct v4l2_fwnode_device_properties *p) 2684 { 2685 if (hdl->error) 2686 return hdl->error; 2687 2688 if (p->orientation != V4L2_FWNODE_PROPERTY_UNSET) { 2689 u32 orientation_ctrl; 2690 2691 switch (p->orientation) { 2692 case V4L2_FWNODE_ORIENTATION_FRONT: 2693 orientation_ctrl = V4L2_CAMERA_ORIENTATION_FRONT; 2694 break; 2695 case V4L2_FWNODE_ORIENTATION_BACK: 2696 orientation_ctrl = V4L2_CAMERA_ORIENTATION_BACK; 2697 break; 2698 case V4L2_FWNODE_ORIENTATION_EXTERNAL: 2699 orientation_ctrl = V4L2_CAMERA_ORIENTATION_EXTERNAL; 2700 break; 2701 default: 2702 return -EINVAL; 2703 } 2704 if (!v4l2_ctrl_new_std_menu(hdl, ctrl_ops, 2705 V4L2_CID_CAMERA_ORIENTATION, 2706 V4L2_CAMERA_ORIENTATION_EXTERNAL, 0, 2707 orientation_ctrl)) 2708 return hdl->error; 2709 } 2710 2711 if (p->rotation != V4L2_FWNODE_PROPERTY_UNSET) { 2712 if (!v4l2_ctrl_new_std(hdl, ctrl_ops, 2713 V4L2_CID_CAMERA_SENSOR_ROTATION, 2714 p->rotation, p->rotation, 1, 2715 p->rotation)) 2716 return hdl->error; 2717 } 2718 2719 return hdl->error; 2720 } 2721 EXPORT_SYMBOL(v4l2_ctrl_new_fwnode_properties); 2722