1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * V4L2 sub-device 4 * 5 * Copyright (C) 2010 Nokia Corporation 6 * 7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 8 * Sakari Ailus <sakari.ailus@iki.fi> 9 */ 10 11 #include <linux/export.h> 12 #include <linux/ioctl.h> 13 #include <linux/leds.h> 14 #include <linux/mm.h> 15 #include <linux/module.h> 16 #include <linux/overflow.h> 17 #include <linux/slab.h> 18 #include <linux/string.h> 19 #include <linux/types.h> 20 #include <linux/version.h> 21 #include <linux/videodev2.h> 22 23 #include <media/v4l2-ctrls.h> 24 #include <media/v4l2-device.h> 25 #include <media/v4l2-event.h> 26 #include <media/v4l2-fh.h> 27 #include <media/v4l2-ioctl.h> 28 29 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 30 /* 31 * The Streams API is an experimental feature. To use the Streams API, set 32 * 'v4l2_subdev_enable_streams_api' to 1 below. 33 */ 34 35 static bool v4l2_subdev_enable_streams_api; 36 #endif 37 38 /* 39 * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set 40 * of streams. 41 * 42 * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX 43 * restricts the total number of streams in a pad, although the stream ID is 44 * not restricted. 45 */ 46 #define V4L2_SUBDEV_MAX_STREAM_ID 63 47 48 #include "v4l2-subdev-priv.h" 49 50 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 51 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) 52 { 53 struct v4l2_subdev_state *state; 54 static struct lock_class_key key; 55 56 state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key); 57 if (IS_ERR(state)) 58 return PTR_ERR(state); 59 60 fh->state = state; 61 62 return 0; 63 } 64 65 static void subdev_fh_free(struct v4l2_subdev_fh *fh) 66 { 67 __v4l2_subdev_state_free(fh->state); 68 fh->state = NULL; 69 } 70 71 static int subdev_open(struct file *file) 72 { 73 struct video_device *vdev = video_devdata(file); 74 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 75 struct v4l2_subdev_fh *subdev_fh; 76 int ret; 77 78 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); 79 if (subdev_fh == NULL) 80 return -ENOMEM; 81 82 ret = subdev_fh_init(subdev_fh, sd); 83 if (ret) { 84 kfree(subdev_fh); 85 return ret; 86 } 87 88 v4l2_fh_init(&subdev_fh->vfh, vdev); 89 v4l2_fh_add(&subdev_fh->vfh); 90 file->private_data = &subdev_fh->vfh; 91 92 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) { 93 struct module *owner; 94 95 owner = sd->entity.graph_obj.mdev->dev->driver->owner; 96 if (!try_module_get(owner)) { 97 ret = -EBUSY; 98 goto err; 99 } 100 subdev_fh->owner = owner; 101 } 102 103 if (sd->internal_ops && sd->internal_ops->open) { 104 ret = sd->internal_ops->open(sd, subdev_fh); 105 if (ret < 0) 106 goto err; 107 } 108 109 return 0; 110 111 err: 112 module_put(subdev_fh->owner); 113 v4l2_fh_del(&subdev_fh->vfh); 114 v4l2_fh_exit(&subdev_fh->vfh); 115 subdev_fh_free(subdev_fh); 116 kfree(subdev_fh); 117 118 return ret; 119 } 120 121 static int subdev_close(struct file *file) 122 { 123 struct video_device *vdev = video_devdata(file); 124 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 125 struct v4l2_fh *vfh = file->private_data; 126 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 127 128 if (sd->internal_ops && sd->internal_ops->close) 129 sd->internal_ops->close(sd, subdev_fh); 130 module_put(subdev_fh->owner); 131 v4l2_fh_del(vfh); 132 v4l2_fh_exit(vfh); 133 subdev_fh_free(subdev_fh); 134 kfree(subdev_fh); 135 file->private_data = NULL; 136 137 return 0; 138 } 139 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 140 static int subdev_open(struct file *file) 141 { 142 return -ENODEV; 143 } 144 145 static int subdev_close(struct file *file) 146 { 147 return -ENODEV; 148 } 149 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 150 151 static inline int check_which(u32 which) 152 { 153 if (which != V4L2_SUBDEV_FORMAT_TRY && 154 which != V4L2_SUBDEV_FORMAT_ACTIVE) 155 return -EINVAL; 156 157 return 0; 158 } 159 160 static inline int check_pad(struct v4l2_subdev *sd, u32 pad) 161 { 162 #if defined(CONFIG_MEDIA_CONTROLLER) 163 if (sd->entity.num_pads) { 164 if (pad >= sd->entity.num_pads) 165 return -EINVAL; 166 return 0; 167 } 168 #endif 169 /* allow pad 0 on subdevices not registered as media entities */ 170 if (pad > 0) 171 return -EINVAL; 172 return 0; 173 } 174 175 static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state, 176 u32 which, u32 pad, u32 stream) 177 { 178 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) { 179 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 180 if (!v4l2_subdev_state_get_stream_format(state, pad, stream)) 181 return -EINVAL; 182 return 0; 183 #else 184 return -EINVAL; 185 #endif 186 } 187 188 if (stream != 0) 189 return -EINVAL; 190 191 if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads)) 192 return -EINVAL; 193 194 return 0; 195 } 196 197 static inline int check_format(struct v4l2_subdev *sd, 198 struct v4l2_subdev_state *state, 199 struct v4l2_subdev_format *format) 200 { 201 if (!format) 202 return -EINVAL; 203 204 return check_which(format->which) ? : check_pad(sd, format->pad) ? : 205 check_state(sd, state, format->which, format->pad, format->stream); 206 } 207 208 static int call_get_fmt(struct v4l2_subdev *sd, 209 struct v4l2_subdev_state *state, 210 struct v4l2_subdev_format *format) 211 { 212 return check_format(sd, state, format) ? : 213 sd->ops->pad->get_fmt(sd, state, format); 214 } 215 216 static int call_set_fmt(struct v4l2_subdev *sd, 217 struct v4l2_subdev_state *state, 218 struct v4l2_subdev_format *format) 219 { 220 return check_format(sd, state, format) ? : 221 sd->ops->pad->set_fmt(sd, state, format); 222 } 223 224 static int call_enum_mbus_code(struct v4l2_subdev *sd, 225 struct v4l2_subdev_state *state, 226 struct v4l2_subdev_mbus_code_enum *code) 227 { 228 if (!code) 229 return -EINVAL; 230 231 return check_which(code->which) ? : check_pad(sd, code->pad) ? : 232 check_state(sd, state, code->which, code->pad, code->stream) ? : 233 sd->ops->pad->enum_mbus_code(sd, state, code); 234 } 235 236 static int call_enum_frame_size(struct v4l2_subdev *sd, 237 struct v4l2_subdev_state *state, 238 struct v4l2_subdev_frame_size_enum *fse) 239 { 240 if (!fse) 241 return -EINVAL; 242 243 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? : 244 check_state(sd, state, fse->which, fse->pad, fse->stream) ? : 245 sd->ops->pad->enum_frame_size(sd, state, fse); 246 } 247 248 static inline int check_frame_interval(struct v4l2_subdev *sd, 249 struct v4l2_subdev_frame_interval *fi) 250 { 251 if (!fi) 252 return -EINVAL; 253 254 return check_pad(sd, fi->pad); 255 } 256 257 static int call_g_frame_interval(struct v4l2_subdev *sd, 258 struct v4l2_subdev_frame_interval *fi) 259 { 260 return check_frame_interval(sd, fi) ? : 261 sd->ops->video->g_frame_interval(sd, fi); 262 } 263 264 static int call_s_frame_interval(struct v4l2_subdev *sd, 265 struct v4l2_subdev_frame_interval *fi) 266 { 267 return check_frame_interval(sd, fi) ? : 268 sd->ops->video->s_frame_interval(sd, fi); 269 } 270 271 static int call_enum_frame_interval(struct v4l2_subdev *sd, 272 struct v4l2_subdev_state *state, 273 struct v4l2_subdev_frame_interval_enum *fie) 274 { 275 if (!fie) 276 return -EINVAL; 277 278 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? : 279 check_state(sd, state, fie->which, fie->pad, fie->stream) ? : 280 sd->ops->pad->enum_frame_interval(sd, state, fie); 281 } 282 283 static inline int check_selection(struct v4l2_subdev *sd, 284 struct v4l2_subdev_state *state, 285 struct v4l2_subdev_selection *sel) 286 { 287 if (!sel) 288 return -EINVAL; 289 290 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? : 291 check_state(sd, state, sel->which, sel->pad, sel->stream); 292 } 293 294 static int call_get_selection(struct v4l2_subdev *sd, 295 struct v4l2_subdev_state *state, 296 struct v4l2_subdev_selection *sel) 297 { 298 return check_selection(sd, state, sel) ? : 299 sd->ops->pad->get_selection(sd, state, sel); 300 } 301 302 static int call_set_selection(struct v4l2_subdev *sd, 303 struct v4l2_subdev_state *state, 304 struct v4l2_subdev_selection *sel) 305 { 306 return check_selection(sd, state, sel) ? : 307 sd->ops->pad->set_selection(sd, state, sel); 308 } 309 310 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, 311 struct v4l2_mbus_frame_desc *fd) 312 { 313 unsigned int i; 314 int ret; 315 316 memset(fd, 0, sizeof(*fd)); 317 318 ret = sd->ops->pad->get_frame_desc(sd, pad, fd); 319 if (ret) 320 return ret; 321 322 dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad, 323 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" : 324 fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : 325 "unknown"); 326 327 for (i = 0; i < fd->num_entries; i++) { 328 struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i]; 329 char buf[20] = ""; 330 331 if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) 332 WARN_ON(snprintf(buf, sizeof(buf), 333 ", vc %u, dt 0x%02x", 334 entry->bus.csi2.vc, 335 entry->bus.csi2.dt) >= sizeof(buf)); 336 337 dev_dbg(sd->dev, 338 "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n", 339 entry->stream, entry->pixelcode, entry->length, 340 entry->flags, buf); 341 } 342 343 return 0; 344 } 345 346 static inline int check_edid(struct v4l2_subdev *sd, 347 struct v4l2_subdev_edid *edid) 348 { 349 if (!edid) 350 return -EINVAL; 351 352 if (edid->blocks && edid->edid == NULL) 353 return -EINVAL; 354 355 return check_pad(sd, edid->pad); 356 } 357 358 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 359 { 360 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid); 361 } 362 363 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 364 { 365 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid); 366 } 367 368 static int call_dv_timings_cap(struct v4l2_subdev *sd, 369 struct v4l2_dv_timings_cap *cap) 370 { 371 if (!cap) 372 return -EINVAL; 373 374 return check_pad(sd, cap->pad) ? : 375 sd->ops->pad->dv_timings_cap(sd, cap); 376 } 377 378 static int call_enum_dv_timings(struct v4l2_subdev *sd, 379 struct v4l2_enum_dv_timings *dvt) 380 { 381 if (!dvt) 382 return -EINVAL; 383 384 return check_pad(sd, dvt->pad) ? : 385 sd->ops->pad->enum_dv_timings(sd, dvt); 386 } 387 388 static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad, 389 struct v4l2_mbus_config *config) 390 { 391 return check_pad(sd, pad) ? : 392 sd->ops->pad->get_mbus_config(sd, pad, config); 393 } 394 395 static int call_s_stream(struct v4l2_subdev *sd, int enable) 396 { 397 int ret; 398 399 /* 400 * The .s_stream() operation must never be called to start or stop an 401 * already started or stopped subdev. Catch offenders but don't return 402 * an error yet to avoid regressions. 403 * 404 * As .s_stream() is mutually exclusive with the .enable_streams() and 405 * .disable_streams() operation, we can use the enabled_streams field 406 * to store the subdev streaming state. 407 */ 408 if (WARN_ON(!!sd->enabled_streams == !!enable)) 409 return 0; 410 411 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 412 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 413 if (enable) 414 led_set_brightness(sd->privacy_led, 415 sd->privacy_led->max_brightness); 416 else 417 led_set_brightness(sd->privacy_led, 0); 418 } 419 #endif 420 ret = sd->ops->video->s_stream(sd, enable); 421 422 if (!enable && ret < 0) { 423 dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret); 424 ret = 0; 425 } 426 427 if (!ret) 428 sd->enabled_streams = enable ? BIT(0) : 0; 429 430 return ret; 431 } 432 433 #ifdef CONFIG_MEDIA_CONTROLLER 434 /* 435 * Create state-management wrapper for pad ops dealing with subdev state. The 436 * wrapper handles the case where the caller does not provide the called 437 * subdev's state. This should be removed when all the callers are fixed. 438 */ 439 #define DEFINE_STATE_WRAPPER(f, arg_type) \ 440 static int call_##f##_state(struct v4l2_subdev *sd, \ 441 struct v4l2_subdev_state *_state, \ 442 arg_type *arg) \ 443 { \ 444 struct v4l2_subdev_state *state = _state; \ 445 int ret; \ 446 if (!_state) \ 447 state = v4l2_subdev_lock_and_get_active_state(sd); \ 448 ret = call_##f(sd, state, arg); \ 449 if (!_state && state) \ 450 v4l2_subdev_unlock_state(state); \ 451 return ret; \ 452 } 453 454 #else /* CONFIG_MEDIA_CONTROLLER */ 455 456 #define DEFINE_STATE_WRAPPER(f, arg_type) \ 457 static int call_##f##_state(struct v4l2_subdev *sd, \ 458 struct v4l2_subdev_state *state, \ 459 arg_type *arg) \ 460 { \ 461 return call_##f(sd, state, arg); \ 462 } 463 464 #endif /* CONFIG_MEDIA_CONTROLLER */ 465 466 DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format); 467 DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format); 468 DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum); 469 DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum); 470 DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum); 471 DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection); 472 DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection); 473 474 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = { 475 .get_fmt = call_get_fmt_state, 476 .set_fmt = call_set_fmt_state, 477 .enum_mbus_code = call_enum_mbus_code_state, 478 .enum_frame_size = call_enum_frame_size_state, 479 .enum_frame_interval = call_enum_frame_interval_state, 480 .get_selection = call_get_selection_state, 481 .set_selection = call_set_selection_state, 482 .get_edid = call_get_edid, 483 .set_edid = call_set_edid, 484 .dv_timings_cap = call_dv_timings_cap, 485 .enum_dv_timings = call_enum_dv_timings, 486 .get_frame_desc = call_get_frame_desc, 487 .get_mbus_config = call_get_mbus_config, 488 }; 489 490 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = { 491 .g_frame_interval = call_g_frame_interval, 492 .s_frame_interval = call_s_frame_interval, 493 .s_stream = call_s_stream, 494 }; 495 496 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = { 497 .pad = &v4l2_subdev_call_pad_wrappers, 498 .video = &v4l2_subdev_call_video_wrappers, 499 }; 500 EXPORT_SYMBOL(v4l2_subdev_call_wrappers); 501 502 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 503 504 static struct v4l2_subdev_state * 505 subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh, 506 unsigned int cmd, void *arg) 507 { 508 u32 which; 509 510 switch (cmd) { 511 default: 512 return NULL; 513 case VIDIOC_SUBDEV_G_FMT: 514 case VIDIOC_SUBDEV_S_FMT: 515 which = ((struct v4l2_subdev_format *)arg)->which; 516 break; 517 case VIDIOC_SUBDEV_G_CROP: 518 case VIDIOC_SUBDEV_S_CROP: 519 which = ((struct v4l2_subdev_crop *)arg)->which; 520 break; 521 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: 522 which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which; 523 break; 524 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: 525 which = ((struct v4l2_subdev_frame_size_enum *)arg)->which; 526 break; 527 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: 528 which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which; 529 break; 530 case VIDIOC_SUBDEV_G_SELECTION: 531 case VIDIOC_SUBDEV_S_SELECTION: 532 which = ((struct v4l2_subdev_selection *)arg)->which; 533 break; 534 case VIDIOC_SUBDEV_G_ROUTING: 535 case VIDIOC_SUBDEV_S_ROUTING: 536 which = ((struct v4l2_subdev_routing *)arg)->which; 537 break; 538 } 539 540 return which == V4L2_SUBDEV_FORMAT_TRY ? 541 subdev_fh->state : 542 v4l2_subdev_get_unlocked_active_state(sd); 543 } 544 545 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg, 546 struct v4l2_subdev_state *state) 547 { 548 struct video_device *vdev = video_devdata(file); 549 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 550 struct v4l2_fh *vfh = file->private_data; 551 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 552 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags); 553 bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS; 554 bool client_supports_streams = subdev_fh->client_caps & 555 V4L2_SUBDEV_CLIENT_CAP_STREAMS; 556 int rval; 557 558 switch (cmd) { 559 case VIDIOC_SUBDEV_QUERYCAP: { 560 struct v4l2_subdev_capability *cap = arg; 561 562 memset(cap->reserved, 0, sizeof(cap->reserved)); 563 cap->version = LINUX_VERSION_CODE; 564 cap->capabilities = 565 (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) | 566 (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0); 567 568 return 0; 569 } 570 571 case VIDIOC_QUERYCTRL: 572 /* 573 * TODO: this really should be folded into v4l2_queryctrl (this 574 * currently returns -EINVAL for NULL control handlers). 575 * However, v4l2_queryctrl() is still called directly by 576 * drivers as well and until that has been addressed I believe 577 * it is safer to do the check here. The same is true for the 578 * other control ioctls below. 579 */ 580 if (!vfh->ctrl_handler) 581 return -ENOTTY; 582 return v4l2_queryctrl(vfh->ctrl_handler, arg); 583 584 case VIDIOC_QUERY_EXT_CTRL: 585 if (!vfh->ctrl_handler) 586 return -ENOTTY; 587 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); 588 589 case VIDIOC_QUERYMENU: 590 if (!vfh->ctrl_handler) 591 return -ENOTTY; 592 return v4l2_querymenu(vfh->ctrl_handler, arg); 593 594 case VIDIOC_G_CTRL: 595 if (!vfh->ctrl_handler) 596 return -ENOTTY; 597 return v4l2_g_ctrl(vfh->ctrl_handler, arg); 598 599 case VIDIOC_S_CTRL: 600 if (!vfh->ctrl_handler) 601 return -ENOTTY; 602 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); 603 604 case VIDIOC_G_EXT_CTRLS: 605 if (!vfh->ctrl_handler) 606 return -ENOTTY; 607 return v4l2_g_ext_ctrls(vfh->ctrl_handler, 608 vdev, sd->v4l2_dev->mdev, arg); 609 610 case VIDIOC_S_EXT_CTRLS: 611 if (!vfh->ctrl_handler) 612 return -ENOTTY; 613 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, 614 vdev, sd->v4l2_dev->mdev, arg); 615 616 case VIDIOC_TRY_EXT_CTRLS: 617 if (!vfh->ctrl_handler) 618 return -ENOTTY; 619 return v4l2_try_ext_ctrls(vfh->ctrl_handler, 620 vdev, sd->v4l2_dev->mdev, arg); 621 622 case VIDIOC_DQEVENT: 623 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 624 return -ENOIOCTLCMD; 625 626 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); 627 628 case VIDIOC_SUBSCRIBE_EVENT: 629 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); 630 631 case VIDIOC_UNSUBSCRIBE_EVENT: 632 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); 633 634 #ifdef CONFIG_VIDEO_ADV_DEBUG 635 case VIDIOC_DBG_G_REGISTER: 636 { 637 struct v4l2_dbg_register *p = arg; 638 639 if (!capable(CAP_SYS_ADMIN)) 640 return -EPERM; 641 return v4l2_subdev_call(sd, core, g_register, p); 642 } 643 case VIDIOC_DBG_S_REGISTER: 644 { 645 struct v4l2_dbg_register *p = arg; 646 647 if (!capable(CAP_SYS_ADMIN)) 648 return -EPERM; 649 return v4l2_subdev_call(sd, core, s_register, p); 650 } 651 case VIDIOC_DBG_G_CHIP_INFO: 652 { 653 struct v4l2_dbg_chip_info *p = arg; 654 655 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr) 656 return -EINVAL; 657 if (sd->ops->core && sd->ops->core->s_register) 658 p->flags |= V4L2_CHIP_FL_WRITABLE; 659 if (sd->ops->core && sd->ops->core->g_register) 660 p->flags |= V4L2_CHIP_FL_READABLE; 661 strscpy(p->name, sd->name, sizeof(p->name)); 662 return 0; 663 } 664 #endif 665 666 case VIDIOC_LOG_STATUS: { 667 int ret; 668 669 pr_info("%s: ================= START STATUS =================\n", 670 sd->name); 671 ret = v4l2_subdev_call(sd, core, log_status); 672 pr_info("%s: ================== END STATUS ==================\n", 673 sd->name); 674 return ret; 675 } 676 677 case VIDIOC_SUBDEV_G_FMT: { 678 struct v4l2_subdev_format *format = arg; 679 680 if (!client_supports_streams) 681 format->stream = 0; 682 683 memset(format->reserved, 0, sizeof(format->reserved)); 684 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 685 return v4l2_subdev_call(sd, pad, get_fmt, state, format); 686 } 687 688 case VIDIOC_SUBDEV_S_FMT: { 689 struct v4l2_subdev_format *format = arg; 690 691 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 692 return -EPERM; 693 694 if (!client_supports_streams) 695 format->stream = 0; 696 697 memset(format->reserved, 0, sizeof(format->reserved)); 698 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 699 return v4l2_subdev_call(sd, pad, set_fmt, state, format); 700 } 701 702 case VIDIOC_SUBDEV_G_CROP: { 703 struct v4l2_subdev_crop *crop = arg; 704 struct v4l2_subdev_selection sel; 705 706 if (!client_supports_streams) 707 crop->stream = 0; 708 709 memset(crop->reserved, 0, sizeof(crop->reserved)); 710 memset(&sel, 0, sizeof(sel)); 711 sel.which = crop->which; 712 sel.pad = crop->pad; 713 sel.target = V4L2_SEL_TGT_CROP; 714 715 rval = v4l2_subdev_call( 716 sd, pad, get_selection, state, &sel); 717 718 crop->rect = sel.r; 719 720 return rval; 721 } 722 723 case VIDIOC_SUBDEV_S_CROP: { 724 struct v4l2_subdev_crop *crop = arg; 725 struct v4l2_subdev_selection sel; 726 727 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 728 return -EPERM; 729 730 if (!client_supports_streams) 731 crop->stream = 0; 732 733 memset(crop->reserved, 0, sizeof(crop->reserved)); 734 memset(&sel, 0, sizeof(sel)); 735 sel.which = crop->which; 736 sel.pad = crop->pad; 737 sel.target = V4L2_SEL_TGT_CROP; 738 sel.r = crop->rect; 739 740 rval = v4l2_subdev_call( 741 sd, pad, set_selection, state, &sel); 742 743 crop->rect = sel.r; 744 745 return rval; 746 } 747 748 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { 749 struct v4l2_subdev_mbus_code_enum *code = arg; 750 751 if (!client_supports_streams) 752 code->stream = 0; 753 754 memset(code->reserved, 0, sizeof(code->reserved)); 755 return v4l2_subdev_call(sd, pad, enum_mbus_code, state, 756 code); 757 } 758 759 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { 760 struct v4l2_subdev_frame_size_enum *fse = arg; 761 762 if (!client_supports_streams) 763 fse->stream = 0; 764 765 memset(fse->reserved, 0, sizeof(fse->reserved)); 766 return v4l2_subdev_call(sd, pad, enum_frame_size, state, 767 fse); 768 } 769 770 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { 771 struct v4l2_subdev_frame_interval *fi = arg; 772 773 if (!client_supports_streams) 774 fi->stream = 0; 775 776 memset(fi->reserved, 0, sizeof(fi->reserved)); 777 return v4l2_subdev_call(sd, video, g_frame_interval, arg); 778 } 779 780 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { 781 struct v4l2_subdev_frame_interval *fi = arg; 782 783 if (ro_subdev) 784 return -EPERM; 785 786 if (!client_supports_streams) 787 fi->stream = 0; 788 789 memset(fi->reserved, 0, sizeof(fi->reserved)); 790 return v4l2_subdev_call(sd, video, s_frame_interval, arg); 791 } 792 793 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { 794 struct v4l2_subdev_frame_interval_enum *fie = arg; 795 796 if (!client_supports_streams) 797 fie->stream = 0; 798 799 memset(fie->reserved, 0, sizeof(fie->reserved)); 800 return v4l2_subdev_call(sd, pad, enum_frame_interval, state, 801 fie); 802 } 803 804 case VIDIOC_SUBDEV_G_SELECTION: { 805 struct v4l2_subdev_selection *sel = arg; 806 807 if (!client_supports_streams) 808 sel->stream = 0; 809 810 memset(sel->reserved, 0, sizeof(sel->reserved)); 811 return v4l2_subdev_call( 812 sd, pad, get_selection, state, sel); 813 } 814 815 case VIDIOC_SUBDEV_S_SELECTION: { 816 struct v4l2_subdev_selection *sel = arg; 817 818 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 819 return -EPERM; 820 821 if (!client_supports_streams) 822 sel->stream = 0; 823 824 memset(sel->reserved, 0, sizeof(sel->reserved)); 825 return v4l2_subdev_call( 826 sd, pad, set_selection, state, sel); 827 } 828 829 case VIDIOC_G_EDID: { 830 struct v4l2_subdev_edid *edid = arg; 831 832 return v4l2_subdev_call(sd, pad, get_edid, edid); 833 } 834 835 case VIDIOC_S_EDID: { 836 struct v4l2_subdev_edid *edid = arg; 837 838 return v4l2_subdev_call(sd, pad, set_edid, edid); 839 } 840 841 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { 842 struct v4l2_dv_timings_cap *cap = arg; 843 844 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 845 } 846 847 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { 848 struct v4l2_enum_dv_timings *dvt = arg; 849 850 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); 851 } 852 853 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: 854 return v4l2_subdev_call(sd, video, query_dv_timings, arg); 855 856 case VIDIOC_SUBDEV_G_DV_TIMINGS: 857 return v4l2_subdev_call(sd, video, g_dv_timings, arg); 858 859 case VIDIOC_SUBDEV_S_DV_TIMINGS: 860 if (ro_subdev) 861 return -EPERM; 862 863 return v4l2_subdev_call(sd, video, s_dv_timings, arg); 864 865 case VIDIOC_SUBDEV_G_STD: 866 return v4l2_subdev_call(sd, video, g_std, arg); 867 868 case VIDIOC_SUBDEV_S_STD: { 869 v4l2_std_id *std = arg; 870 871 if (ro_subdev) 872 return -EPERM; 873 874 return v4l2_subdev_call(sd, video, s_std, *std); 875 } 876 877 case VIDIOC_SUBDEV_ENUMSTD: { 878 struct v4l2_standard *p = arg; 879 v4l2_std_id id; 880 881 if (v4l2_subdev_call(sd, video, g_tvnorms, &id)) 882 return -EINVAL; 883 884 return v4l_video_std_enumstd(p, id); 885 } 886 887 case VIDIOC_SUBDEV_QUERYSTD: 888 return v4l2_subdev_call(sd, video, querystd, arg); 889 890 case VIDIOC_SUBDEV_G_ROUTING: { 891 struct v4l2_subdev_routing *routing = arg; 892 struct v4l2_subdev_krouting *krouting; 893 894 if (!v4l2_subdev_enable_streams_api) 895 return -ENOIOCTLCMD; 896 897 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) 898 return -ENOIOCTLCMD; 899 900 memset(routing->reserved, 0, sizeof(routing->reserved)); 901 902 krouting = &state->routing; 903 904 if (routing->num_routes < krouting->num_routes) { 905 routing->num_routes = krouting->num_routes; 906 return -ENOSPC; 907 } 908 909 memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes, 910 krouting->routes, 911 krouting->num_routes * sizeof(*krouting->routes)); 912 routing->num_routes = krouting->num_routes; 913 914 return 0; 915 } 916 917 case VIDIOC_SUBDEV_S_ROUTING: { 918 struct v4l2_subdev_routing *routing = arg; 919 struct v4l2_subdev_route *routes = 920 (struct v4l2_subdev_route *)(uintptr_t)routing->routes; 921 struct v4l2_subdev_krouting krouting = {}; 922 unsigned int i; 923 924 if (!v4l2_subdev_enable_streams_api) 925 return -ENOIOCTLCMD; 926 927 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) 928 return -ENOIOCTLCMD; 929 930 if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 931 return -EPERM; 932 933 memset(routing->reserved, 0, sizeof(routing->reserved)); 934 935 for (i = 0; i < routing->num_routes; ++i) { 936 const struct v4l2_subdev_route *route = &routes[i]; 937 const struct media_pad *pads = sd->entity.pads; 938 939 if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID || 940 route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID) 941 return -EINVAL; 942 943 if (route->sink_pad >= sd->entity.num_pads) 944 return -EINVAL; 945 946 if (!(pads[route->sink_pad].flags & 947 MEDIA_PAD_FL_SINK)) 948 return -EINVAL; 949 950 if (route->source_pad >= sd->entity.num_pads) 951 return -EINVAL; 952 953 if (!(pads[route->source_pad].flags & 954 MEDIA_PAD_FL_SOURCE)) 955 return -EINVAL; 956 } 957 958 krouting.num_routes = routing->num_routes; 959 krouting.routes = routes; 960 961 return v4l2_subdev_call(sd, pad, set_routing, state, 962 routing->which, &krouting); 963 } 964 965 case VIDIOC_SUBDEV_G_CLIENT_CAP: { 966 struct v4l2_subdev_client_capability *client_cap = arg; 967 968 client_cap->capabilities = subdev_fh->client_caps; 969 970 return 0; 971 } 972 973 case VIDIOC_SUBDEV_S_CLIENT_CAP: { 974 struct v4l2_subdev_client_capability *client_cap = arg; 975 976 /* 977 * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not 978 * enabled. Remove this when streams API is no longer 979 * experimental. 980 */ 981 if (!v4l2_subdev_enable_streams_api) 982 client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS; 983 984 /* Filter out unsupported capabilities */ 985 client_cap->capabilities &= V4L2_SUBDEV_CLIENT_CAP_STREAMS; 986 987 subdev_fh->client_caps = client_cap->capabilities; 988 989 return 0; 990 } 991 992 default: 993 return v4l2_subdev_call(sd, core, ioctl, cmd, arg); 994 } 995 996 return 0; 997 } 998 999 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg) 1000 { 1001 struct video_device *vdev = video_devdata(file); 1002 struct mutex *lock = vdev->lock; 1003 long ret = -ENODEV; 1004 1005 if (lock && mutex_lock_interruptible(lock)) 1006 return -ERESTARTSYS; 1007 1008 if (video_is_registered(vdev)) { 1009 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 1010 struct v4l2_fh *vfh = file->private_data; 1011 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 1012 struct v4l2_subdev_state *state; 1013 1014 state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg); 1015 1016 if (state) 1017 v4l2_subdev_lock_state(state); 1018 1019 ret = subdev_do_ioctl(file, cmd, arg, state); 1020 1021 if (state) 1022 v4l2_subdev_unlock_state(state); 1023 } 1024 1025 if (lock) 1026 mutex_unlock(lock); 1027 return ret; 1028 } 1029 1030 static long subdev_ioctl(struct file *file, unsigned int cmd, 1031 unsigned long arg) 1032 { 1033 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock); 1034 } 1035 1036 #ifdef CONFIG_COMPAT 1037 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 1038 unsigned long arg) 1039 { 1040 struct video_device *vdev = video_devdata(file); 1041 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 1042 1043 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); 1044 } 1045 #endif 1046 1047 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1048 static long subdev_ioctl(struct file *file, unsigned int cmd, 1049 unsigned long arg) 1050 { 1051 return -ENODEV; 1052 } 1053 1054 #ifdef CONFIG_COMPAT 1055 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 1056 unsigned long arg) 1057 { 1058 return -ENODEV; 1059 } 1060 #endif 1061 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1062 1063 static __poll_t subdev_poll(struct file *file, poll_table *wait) 1064 { 1065 struct video_device *vdev = video_devdata(file); 1066 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 1067 struct v4l2_fh *fh = file->private_data; 1068 1069 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 1070 return EPOLLERR; 1071 1072 poll_wait(file, &fh->wait, wait); 1073 1074 if (v4l2_event_pending(fh)) 1075 return EPOLLPRI; 1076 1077 return 0; 1078 } 1079 1080 const struct v4l2_file_operations v4l2_subdev_fops = { 1081 .owner = THIS_MODULE, 1082 .open = subdev_open, 1083 .unlocked_ioctl = subdev_ioctl, 1084 #ifdef CONFIG_COMPAT 1085 .compat_ioctl32 = subdev_compat_ioctl32, 1086 #endif 1087 .release = subdev_close, 1088 .poll = subdev_poll, 1089 }; 1090 1091 #ifdef CONFIG_MEDIA_CONTROLLER 1092 1093 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, 1094 struct fwnode_endpoint *endpoint) 1095 { 1096 struct fwnode_handle *fwnode; 1097 struct v4l2_subdev *sd; 1098 1099 if (!is_media_entity_v4l2_subdev(entity)) 1100 return -EINVAL; 1101 1102 sd = media_entity_to_v4l2_subdev(entity); 1103 1104 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode); 1105 fwnode_handle_put(fwnode); 1106 1107 if (device_match_fwnode(sd->dev, fwnode)) 1108 return endpoint->port; 1109 1110 return -ENXIO; 1111 } 1112 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1); 1113 1114 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, 1115 struct media_link *link, 1116 struct v4l2_subdev_format *source_fmt, 1117 struct v4l2_subdev_format *sink_fmt) 1118 { 1119 bool pass = true; 1120 1121 /* The width, height and code must match. */ 1122 if (source_fmt->format.width != sink_fmt->format.width) { 1123 dev_dbg(sd->entity.graph_obj.mdev->dev, 1124 "%s: width does not match (source %u, sink %u)\n", 1125 __func__, 1126 source_fmt->format.width, sink_fmt->format.width); 1127 pass = false; 1128 } 1129 1130 if (source_fmt->format.height != sink_fmt->format.height) { 1131 dev_dbg(sd->entity.graph_obj.mdev->dev, 1132 "%s: height does not match (source %u, sink %u)\n", 1133 __func__, 1134 source_fmt->format.height, sink_fmt->format.height); 1135 pass = false; 1136 } 1137 1138 if (source_fmt->format.code != sink_fmt->format.code) { 1139 dev_dbg(sd->entity.graph_obj.mdev->dev, 1140 "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n", 1141 __func__, 1142 source_fmt->format.code, sink_fmt->format.code); 1143 pass = false; 1144 } 1145 1146 /* The field order must match, or the sink field order must be NONE 1147 * to support interlaced hardware connected to bridges that support 1148 * progressive formats only. 1149 */ 1150 if (source_fmt->format.field != sink_fmt->format.field && 1151 sink_fmt->format.field != V4L2_FIELD_NONE) { 1152 dev_dbg(sd->entity.graph_obj.mdev->dev, 1153 "%s: field does not match (source %u, sink %u)\n", 1154 __func__, 1155 source_fmt->format.field, sink_fmt->format.field); 1156 pass = false; 1157 } 1158 1159 if (pass) 1160 return 0; 1161 1162 dev_dbg(sd->entity.graph_obj.mdev->dev, 1163 "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__, 1164 link->source->entity->name, link->source->index, 1165 link->sink->entity->name, link->sink->index); 1166 1167 return -EPIPE; 1168 } 1169 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); 1170 1171 static int 1172 v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream, 1173 struct v4l2_subdev_format *fmt, 1174 bool states_locked) 1175 { 1176 struct v4l2_subdev_state *state; 1177 struct v4l2_subdev *sd; 1178 int ret; 1179 1180 if (!is_media_entity_v4l2_subdev(pad->entity)) { 1181 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L, 1182 "Driver bug! Wrong media entity type 0x%08x, entity %s\n", 1183 pad->entity->function, pad->entity->name); 1184 1185 return -EINVAL; 1186 } 1187 1188 sd = media_entity_to_v4l2_subdev(pad->entity); 1189 1190 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; 1191 fmt->pad = pad->index; 1192 fmt->stream = stream; 1193 1194 if (states_locked) 1195 state = v4l2_subdev_get_locked_active_state(sd); 1196 else 1197 state = v4l2_subdev_lock_and_get_active_state(sd); 1198 1199 ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt); 1200 1201 if (!states_locked && state) 1202 v4l2_subdev_unlock_state(state); 1203 1204 return ret; 1205 } 1206 1207 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1208 1209 static void __v4l2_link_validate_get_streams(struct media_pad *pad, 1210 u64 *streams_mask, 1211 bool states_locked) 1212 { 1213 struct v4l2_subdev_route *route; 1214 struct v4l2_subdev_state *state; 1215 struct v4l2_subdev *subdev; 1216 1217 subdev = media_entity_to_v4l2_subdev(pad->entity); 1218 1219 *streams_mask = 0; 1220 1221 if (states_locked) 1222 state = v4l2_subdev_get_locked_active_state(subdev); 1223 else 1224 state = v4l2_subdev_lock_and_get_active_state(subdev); 1225 1226 if (WARN_ON(!state)) 1227 return; 1228 1229 for_each_active_route(&state->routing, route) { 1230 u32 route_pad; 1231 u32 route_stream; 1232 1233 if (pad->flags & MEDIA_PAD_FL_SOURCE) { 1234 route_pad = route->source_pad; 1235 route_stream = route->source_stream; 1236 } else { 1237 route_pad = route->sink_pad; 1238 route_stream = route->sink_stream; 1239 } 1240 1241 if (route_pad != pad->index) 1242 continue; 1243 1244 *streams_mask |= BIT_ULL(route_stream); 1245 } 1246 1247 if (!states_locked) 1248 v4l2_subdev_unlock_state(state); 1249 } 1250 1251 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 1252 1253 static void v4l2_link_validate_get_streams(struct media_pad *pad, 1254 u64 *streams_mask, 1255 bool states_locked) 1256 { 1257 struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity); 1258 1259 if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) { 1260 /* Non-streams subdevs have an implicit stream 0 */ 1261 *streams_mask = BIT_ULL(0); 1262 return; 1263 } 1264 1265 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1266 __v4l2_link_validate_get_streams(pad, streams_mask, states_locked); 1267 #else 1268 /* This shouldn't happen */ 1269 *streams_mask = 0; 1270 #endif 1271 } 1272 1273 static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked) 1274 { 1275 struct v4l2_subdev *sink_subdev = 1276 media_entity_to_v4l2_subdev(link->sink->entity); 1277 struct device *dev = sink_subdev->entity.graph_obj.mdev->dev; 1278 u64 source_streams_mask; 1279 u64 sink_streams_mask; 1280 u64 dangling_sink_streams; 1281 u32 stream; 1282 int ret; 1283 1284 dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n", 1285 link->source->entity->name, link->source->index, 1286 link->sink->entity->name, link->sink->index); 1287 1288 v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked); 1289 v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked); 1290 1291 /* 1292 * It is ok to have more source streams than sink streams as extra 1293 * source streams can just be ignored by the receiver, but having extra 1294 * sink streams is an error as streams must have a source. 1295 */ 1296 dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) & 1297 sink_streams_mask; 1298 if (dangling_sink_streams) { 1299 dev_err(dev, "Dangling sink streams: mask %#llx\n", 1300 dangling_sink_streams); 1301 return -EINVAL; 1302 } 1303 1304 /* Validate source and sink stream formats */ 1305 1306 for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) { 1307 struct v4l2_subdev_format sink_fmt, source_fmt; 1308 1309 if (!(sink_streams_mask & BIT_ULL(stream))) 1310 continue; 1311 1312 dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n", 1313 link->source->entity->name, link->source->index, stream, 1314 link->sink->entity->name, link->sink->index, stream); 1315 1316 ret = v4l2_subdev_link_validate_get_format(link->source, stream, 1317 &source_fmt, states_locked); 1318 if (ret < 0) { 1319 dev_dbg(dev, 1320 "Failed to get format for \"%s\":%u:%u (but that's ok)\n", 1321 link->source->entity->name, link->source->index, 1322 stream); 1323 continue; 1324 } 1325 1326 ret = v4l2_subdev_link_validate_get_format(link->sink, stream, 1327 &sink_fmt, states_locked); 1328 if (ret < 0) { 1329 dev_dbg(dev, 1330 "Failed to get format for \"%s\":%u:%u (but that's ok)\n", 1331 link->sink->entity->name, link->sink->index, 1332 stream); 1333 continue; 1334 } 1335 1336 /* TODO: add stream number to link_validate() */ 1337 ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link, 1338 &source_fmt, &sink_fmt); 1339 if (!ret) 1340 continue; 1341 1342 if (ret != -ENOIOCTLCMD) 1343 return ret; 1344 1345 ret = v4l2_subdev_link_validate_default(sink_subdev, link, 1346 &source_fmt, &sink_fmt); 1347 1348 if (ret) 1349 return ret; 1350 } 1351 1352 return 0; 1353 } 1354 1355 int v4l2_subdev_link_validate(struct media_link *link) 1356 { 1357 struct v4l2_subdev *source_sd, *sink_sd; 1358 struct v4l2_subdev_state *source_state, *sink_state; 1359 bool states_locked; 1360 int ret; 1361 1362 if (!is_media_entity_v4l2_subdev(link->sink->entity) || 1363 !is_media_entity_v4l2_subdev(link->source->entity)) { 1364 pr_warn_once("%s of link '%s':%u->'%s':%u is not a V4L2 sub-device, driver bug!\n", 1365 !is_media_entity_v4l2_subdev(link->sink->entity) ? 1366 "sink" : "source", 1367 link->source->entity->name, link->source->index, 1368 link->sink->entity->name, link->sink->index); 1369 return 0; 1370 } 1371 1372 sink_sd = media_entity_to_v4l2_subdev(link->sink->entity); 1373 source_sd = media_entity_to_v4l2_subdev(link->source->entity); 1374 1375 sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd); 1376 source_state = v4l2_subdev_get_unlocked_active_state(source_sd); 1377 1378 states_locked = sink_state && source_state; 1379 1380 if (states_locked) { 1381 v4l2_subdev_lock_state(sink_state); 1382 v4l2_subdev_lock_state(source_state); 1383 } 1384 1385 ret = v4l2_subdev_link_validate_locked(link, states_locked); 1386 1387 if (states_locked) { 1388 v4l2_subdev_unlock_state(sink_state); 1389 v4l2_subdev_unlock_state(source_state); 1390 } 1391 1392 return ret; 1393 } 1394 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); 1395 1396 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity, 1397 unsigned int pad0, unsigned int pad1) 1398 { 1399 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); 1400 struct v4l2_subdev_krouting *routing; 1401 struct v4l2_subdev_state *state; 1402 unsigned int i; 1403 1404 state = v4l2_subdev_lock_and_get_active_state(sd); 1405 1406 routing = &state->routing; 1407 1408 for (i = 0; i < routing->num_routes; ++i) { 1409 struct v4l2_subdev_route *route = &routing->routes[i]; 1410 1411 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)) 1412 continue; 1413 1414 if ((route->sink_pad == pad0 && route->source_pad == pad1) || 1415 (route->source_pad == pad0 && route->sink_pad == pad1)) { 1416 v4l2_subdev_unlock_state(state); 1417 return true; 1418 } 1419 } 1420 1421 v4l2_subdev_unlock_state(state); 1422 1423 return false; 1424 } 1425 EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep); 1426 1427 struct v4l2_subdev_state * 1428 __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name, 1429 struct lock_class_key *lock_key) 1430 { 1431 struct v4l2_subdev_state *state; 1432 int ret; 1433 1434 state = kzalloc(sizeof(*state), GFP_KERNEL); 1435 if (!state) 1436 return ERR_PTR(-ENOMEM); 1437 1438 __mutex_init(&state->_lock, lock_name, lock_key); 1439 if (sd->state_lock) 1440 state->lock = sd->state_lock; 1441 else 1442 state->lock = &state->_lock; 1443 1444 /* Drivers that support streams do not need the legacy pad config */ 1445 if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) { 1446 state->pads = kvcalloc(sd->entity.num_pads, 1447 sizeof(*state->pads), GFP_KERNEL); 1448 if (!state->pads) { 1449 ret = -ENOMEM; 1450 goto err; 1451 } 1452 } 1453 1454 /* 1455 * There can be no race at this point, but we lock the state anyway to 1456 * satisfy lockdep checks. 1457 */ 1458 v4l2_subdev_lock_state(state); 1459 ret = v4l2_subdev_call(sd, pad, init_cfg, state); 1460 v4l2_subdev_unlock_state(state); 1461 1462 if (ret < 0 && ret != -ENOIOCTLCMD) 1463 goto err; 1464 1465 return state; 1466 1467 err: 1468 if (state && state->pads) 1469 kvfree(state->pads); 1470 1471 kfree(state); 1472 1473 return ERR_PTR(ret); 1474 } 1475 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc); 1476 1477 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state) 1478 { 1479 if (!state) 1480 return; 1481 1482 mutex_destroy(&state->_lock); 1483 1484 kfree(state->routing.routes); 1485 kvfree(state->stream_configs.configs); 1486 kvfree(state->pads); 1487 kfree(state); 1488 } 1489 EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free); 1490 1491 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name, 1492 struct lock_class_key *key) 1493 { 1494 struct v4l2_subdev_state *state; 1495 1496 state = __v4l2_subdev_state_alloc(sd, name, key); 1497 if (IS_ERR(state)) 1498 return PTR_ERR(state); 1499 1500 sd->active_state = state; 1501 1502 return 0; 1503 } 1504 EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize); 1505 1506 void v4l2_subdev_cleanup(struct v4l2_subdev *sd) 1507 { 1508 struct v4l2_async_subdev_endpoint *ase, *ase_tmp; 1509 1510 __v4l2_subdev_state_free(sd->active_state); 1511 sd->active_state = NULL; 1512 1513 if (list_empty(&sd->async_subdev_endpoint_list)) 1514 return; 1515 1516 list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list, 1517 async_subdev_endpoint_entry) { 1518 list_del(&ase->async_subdev_endpoint_entry); 1519 1520 kfree(ase); 1521 } 1522 } 1523 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup); 1524 1525 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 1526 1527 static int 1528 v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs, 1529 const struct v4l2_subdev_krouting *routing) 1530 { 1531 struct v4l2_subdev_stream_configs new_configs = { 0 }; 1532 struct v4l2_subdev_route *route; 1533 u32 idx; 1534 1535 /* Count number of formats needed */ 1536 for_each_active_route(routing, route) { 1537 /* 1538 * Each route needs a format on both ends of the route. 1539 */ 1540 new_configs.num_configs += 2; 1541 } 1542 1543 if (new_configs.num_configs) { 1544 new_configs.configs = kvcalloc(new_configs.num_configs, 1545 sizeof(*new_configs.configs), 1546 GFP_KERNEL); 1547 1548 if (!new_configs.configs) 1549 return -ENOMEM; 1550 } 1551 1552 /* 1553 * Fill in the 'pad' and stream' value for each item in the array from 1554 * the routing table 1555 */ 1556 idx = 0; 1557 1558 for_each_active_route(routing, route) { 1559 new_configs.configs[idx].pad = route->sink_pad; 1560 new_configs.configs[idx].stream = route->sink_stream; 1561 1562 idx++; 1563 1564 new_configs.configs[idx].pad = route->source_pad; 1565 new_configs.configs[idx].stream = route->source_stream; 1566 1567 idx++; 1568 } 1569 1570 kvfree(stream_configs->configs); 1571 *stream_configs = new_configs; 1572 1573 return 0; 1574 } 1575 1576 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state, 1577 struct v4l2_subdev_format *format) 1578 { 1579 struct v4l2_mbus_framefmt *fmt; 1580 1581 if (sd->flags & V4L2_SUBDEV_FL_STREAMS) 1582 fmt = v4l2_subdev_state_get_stream_format(state, format->pad, 1583 format->stream); 1584 else if (format->pad < sd->entity.num_pads && format->stream == 0) 1585 fmt = v4l2_subdev_get_pad_format(sd, state, format->pad); 1586 else 1587 fmt = NULL; 1588 1589 if (!fmt) 1590 return -EINVAL; 1591 1592 format->format = *fmt; 1593 1594 return 0; 1595 } 1596 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt); 1597 1598 int v4l2_subdev_set_routing(struct v4l2_subdev *sd, 1599 struct v4l2_subdev_state *state, 1600 const struct v4l2_subdev_krouting *routing) 1601 { 1602 struct v4l2_subdev_krouting *dst = &state->routing; 1603 const struct v4l2_subdev_krouting *src = routing; 1604 struct v4l2_subdev_krouting new_routing = { 0 }; 1605 size_t bytes; 1606 int r; 1607 1608 if (unlikely(check_mul_overflow((size_t)src->num_routes, 1609 sizeof(*src->routes), &bytes))) 1610 return -EOVERFLOW; 1611 1612 lockdep_assert_held(state->lock); 1613 1614 if (src->num_routes > 0) { 1615 new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL); 1616 if (!new_routing.routes) 1617 return -ENOMEM; 1618 } 1619 1620 new_routing.num_routes = src->num_routes; 1621 1622 r = v4l2_subdev_init_stream_configs(&state->stream_configs, 1623 &new_routing); 1624 if (r) { 1625 kfree(new_routing.routes); 1626 return r; 1627 } 1628 1629 kfree(dst->routes); 1630 *dst = new_routing; 1631 1632 return 0; 1633 } 1634 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing); 1635 1636 struct v4l2_subdev_route * 1637 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing, 1638 struct v4l2_subdev_route *route) 1639 { 1640 if (route) 1641 ++route; 1642 else 1643 route = &routing->routes[0]; 1644 1645 for (; route < routing->routes + routing->num_routes; ++route) { 1646 if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)) 1647 continue; 1648 1649 return route; 1650 } 1651 1652 return NULL; 1653 } 1654 EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route); 1655 1656 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd, 1657 struct v4l2_subdev_state *state, 1658 const struct v4l2_subdev_krouting *routing, 1659 const struct v4l2_mbus_framefmt *fmt) 1660 { 1661 struct v4l2_subdev_stream_configs *stream_configs; 1662 unsigned int i; 1663 int ret; 1664 1665 ret = v4l2_subdev_set_routing(sd, state, routing); 1666 if (ret) 1667 return ret; 1668 1669 stream_configs = &state->stream_configs; 1670 1671 for (i = 0; i < stream_configs->num_configs; ++i) 1672 stream_configs->configs[i].fmt = *fmt; 1673 1674 return 0; 1675 } 1676 EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt); 1677 1678 struct v4l2_mbus_framefmt * 1679 v4l2_subdev_state_get_stream_format(struct v4l2_subdev_state *state, 1680 unsigned int pad, u32 stream) 1681 { 1682 struct v4l2_subdev_stream_configs *stream_configs; 1683 unsigned int i; 1684 1685 lockdep_assert_held(state->lock); 1686 1687 stream_configs = &state->stream_configs; 1688 1689 for (i = 0; i < stream_configs->num_configs; ++i) { 1690 if (stream_configs->configs[i].pad == pad && 1691 stream_configs->configs[i].stream == stream) 1692 return &stream_configs->configs[i].fmt; 1693 } 1694 1695 return NULL; 1696 } 1697 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_format); 1698 1699 struct v4l2_rect * 1700 v4l2_subdev_state_get_stream_crop(struct v4l2_subdev_state *state, 1701 unsigned int pad, u32 stream) 1702 { 1703 struct v4l2_subdev_stream_configs *stream_configs; 1704 unsigned int i; 1705 1706 lockdep_assert_held(state->lock); 1707 1708 stream_configs = &state->stream_configs; 1709 1710 for (i = 0; i < stream_configs->num_configs; ++i) { 1711 if (stream_configs->configs[i].pad == pad && 1712 stream_configs->configs[i].stream == stream) 1713 return &stream_configs->configs[i].crop; 1714 } 1715 1716 return NULL; 1717 } 1718 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_crop); 1719 1720 struct v4l2_rect * 1721 v4l2_subdev_state_get_stream_compose(struct v4l2_subdev_state *state, 1722 unsigned int pad, u32 stream) 1723 { 1724 struct v4l2_subdev_stream_configs *stream_configs; 1725 unsigned int i; 1726 1727 lockdep_assert_held(state->lock); 1728 1729 stream_configs = &state->stream_configs; 1730 1731 for (i = 0; i < stream_configs->num_configs; ++i) { 1732 if (stream_configs->configs[i].pad == pad && 1733 stream_configs->configs[i].stream == stream) 1734 return &stream_configs->configs[i].compose; 1735 } 1736 1737 return NULL; 1738 } 1739 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_stream_compose); 1740 1741 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing, 1742 u32 pad, u32 stream, u32 *other_pad, 1743 u32 *other_stream) 1744 { 1745 unsigned int i; 1746 1747 for (i = 0; i < routing->num_routes; ++i) { 1748 struct v4l2_subdev_route *route = &routing->routes[i]; 1749 1750 if (route->source_pad == pad && 1751 route->source_stream == stream) { 1752 if (other_pad) 1753 *other_pad = route->sink_pad; 1754 if (other_stream) 1755 *other_stream = route->sink_stream; 1756 return 0; 1757 } 1758 1759 if (route->sink_pad == pad && route->sink_stream == stream) { 1760 if (other_pad) 1761 *other_pad = route->source_pad; 1762 if (other_stream) 1763 *other_stream = route->source_stream; 1764 return 0; 1765 } 1766 } 1767 1768 return -EINVAL; 1769 } 1770 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end); 1771 1772 struct v4l2_mbus_framefmt * 1773 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state, 1774 u32 pad, u32 stream) 1775 { 1776 u32 other_pad, other_stream; 1777 int ret; 1778 1779 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, 1780 pad, stream, 1781 &other_pad, &other_stream); 1782 if (ret) 1783 return NULL; 1784 1785 return v4l2_subdev_state_get_stream_format(state, other_pad, 1786 other_stream); 1787 } 1788 EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format); 1789 1790 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state, 1791 u32 pad0, u32 pad1, u64 *streams) 1792 { 1793 const struct v4l2_subdev_krouting *routing = &state->routing; 1794 struct v4l2_subdev_route *route; 1795 u64 streams0 = 0; 1796 u64 streams1 = 0; 1797 1798 for_each_active_route(routing, route) { 1799 if (route->sink_pad == pad0 && route->source_pad == pad1 && 1800 (*streams & BIT_ULL(route->sink_stream))) { 1801 streams0 |= BIT_ULL(route->sink_stream); 1802 streams1 |= BIT_ULL(route->source_stream); 1803 } 1804 if (route->source_pad == pad0 && route->sink_pad == pad1 && 1805 (*streams & BIT_ULL(route->source_stream))) { 1806 streams0 |= BIT_ULL(route->source_stream); 1807 streams1 |= BIT_ULL(route->sink_stream); 1808 } 1809 } 1810 1811 *streams = streams0; 1812 return streams1; 1813 } 1814 EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams); 1815 1816 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd, 1817 const struct v4l2_subdev_krouting *routing, 1818 enum v4l2_subdev_routing_restriction disallow) 1819 { 1820 u32 *remote_pads = NULL; 1821 unsigned int i, j; 1822 int ret = -EINVAL; 1823 1824 if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX | 1825 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) { 1826 remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads), 1827 GFP_KERNEL); 1828 if (!remote_pads) 1829 return -ENOMEM; 1830 1831 for (i = 0; i < sd->entity.num_pads; ++i) 1832 remote_pads[i] = U32_MAX; 1833 } 1834 1835 for (i = 0; i < routing->num_routes; ++i) { 1836 const struct v4l2_subdev_route *route = &routing->routes[i]; 1837 1838 /* Validate the sink and source pad numbers. */ 1839 if (route->sink_pad >= sd->entity.num_pads || 1840 !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) { 1841 dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n", 1842 i, route->sink_pad); 1843 goto out; 1844 } 1845 1846 if (route->source_pad >= sd->entity.num_pads || 1847 !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) { 1848 dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n", 1849 i, route->source_pad); 1850 goto out; 1851 } 1852 1853 /* 1854 * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a 1855 * sink pad must be routed to a single source pad. 1856 */ 1857 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) { 1858 if (remote_pads[route->sink_pad] != U32_MAX && 1859 remote_pads[route->sink_pad] != route->source_pad) { 1860 dev_dbg(sd->dev, 1861 "route %u attempts to mix %s streams\n", 1862 i, "sink"); 1863 goto out; 1864 } 1865 } 1866 1867 /* 1868 * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a 1869 * source pad must originate from a single sink pad. 1870 */ 1871 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) { 1872 if (remote_pads[route->source_pad] != U32_MAX && 1873 remote_pads[route->source_pad] != route->sink_pad) { 1874 dev_dbg(sd->dev, 1875 "route %u attempts to mix %s streams\n", 1876 i, "source"); 1877 goto out; 1878 } 1879 } 1880 1881 /* 1882 * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink 1883 * side can not do stream multiplexing, i.e. there can be only 1884 * a single stream in a sink pad. 1885 */ 1886 if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) { 1887 if (remote_pads[route->sink_pad] != U32_MAX) { 1888 dev_dbg(sd->dev, 1889 "route %u attempts to multiplex on %s pad %u\n", 1890 i, "sink", route->sink_pad); 1891 goto out; 1892 } 1893 } 1894 1895 /* 1896 * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the 1897 * source side can not do stream multiplexing, i.e. there can 1898 * be only a single stream in a source pad. 1899 */ 1900 if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) { 1901 if (remote_pads[route->source_pad] != U32_MAX) { 1902 dev_dbg(sd->dev, 1903 "route %u attempts to multiplex on %s pad %u\n", 1904 i, "source", route->source_pad); 1905 goto out; 1906 } 1907 } 1908 1909 if (remote_pads) { 1910 remote_pads[route->sink_pad] = route->source_pad; 1911 remote_pads[route->source_pad] = route->sink_pad; 1912 } 1913 1914 for (j = i + 1; j < routing->num_routes; ++j) { 1915 const struct v4l2_subdev_route *r = &routing->routes[j]; 1916 1917 /* 1918 * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can 1919 * originate from the same (sink) stream. 1920 */ 1921 if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) && 1922 route->sink_pad == r->sink_pad && 1923 route->sink_stream == r->sink_stream) { 1924 dev_dbg(sd->dev, 1925 "routes %u and %u originate from same sink (%u/%u)\n", 1926 i, j, route->sink_pad, 1927 route->sink_stream); 1928 goto out; 1929 } 1930 1931 /* 1932 * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end 1933 * at the same (source) stream. 1934 */ 1935 if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) && 1936 route->source_pad == r->source_pad && 1937 route->source_stream == r->source_stream) { 1938 dev_dbg(sd->dev, 1939 "routes %u and %u end at same source (%u/%u)\n", 1940 i, j, route->source_pad, 1941 route->source_stream); 1942 goto out; 1943 } 1944 } 1945 } 1946 1947 ret = 0; 1948 1949 out: 1950 kfree(remote_pads); 1951 return ret; 1952 } 1953 EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate); 1954 1955 static int v4l2_subdev_enable_streams_fallback(struct v4l2_subdev *sd, u32 pad, 1956 u64 streams_mask) 1957 { 1958 struct device *dev = sd->entity.graph_obj.mdev->dev; 1959 unsigned int i; 1960 int ret; 1961 1962 /* 1963 * The subdev doesn't implement pad-based stream enable, fall back 1964 * on the .s_stream() operation. This can only be done for subdevs that 1965 * have a single source pad, as sd->enabled_streams is global to the 1966 * subdev. 1967 */ 1968 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE)) 1969 return -EOPNOTSUPP; 1970 1971 for (i = 0; i < sd->entity.num_pads; ++i) { 1972 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE) 1973 return -EOPNOTSUPP; 1974 } 1975 1976 if (sd->enabled_streams & streams_mask) { 1977 dev_dbg(dev, "set of streams %#llx already enabled on %s:%u\n", 1978 streams_mask, sd->entity.name, pad); 1979 return -EALREADY; 1980 } 1981 1982 /* Start streaming when the first streams are enabled. */ 1983 if (!sd->enabled_streams) { 1984 ret = v4l2_subdev_call(sd, video, s_stream, 1); 1985 if (ret) 1986 return ret; 1987 } 1988 1989 sd->enabled_streams |= streams_mask; 1990 1991 return 0; 1992 } 1993 1994 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad, 1995 u64 streams_mask) 1996 { 1997 struct device *dev = sd->entity.graph_obj.mdev->dev; 1998 struct v4l2_subdev_state *state; 1999 u64 found_streams = 0; 2000 unsigned int i; 2001 int ret; 2002 2003 /* A few basic sanity checks first. */ 2004 if (pad >= sd->entity.num_pads) 2005 return -EINVAL; 2006 2007 if (!streams_mask) 2008 return 0; 2009 2010 /* Fallback on .s_stream() if .enable_streams() isn't available. */ 2011 if (!sd->ops->pad || !sd->ops->pad->enable_streams) 2012 return v4l2_subdev_enable_streams_fallback(sd, pad, 2013 streams_mask); 2014 2015 state = v4l2_subdev_lock_and_get_active_state(sd); 2016 2017 /* 2018 * Verify that the requested streams exist and that they are not 2019 * already enabled. 2020 */ 2021 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2022 struct v4l2_subdev_stream_config *cfg = 2023 &state->stream_configs.configs[i]; 2024 2025 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream))) 2026 continue; 2027 2028 found_streams |= BIT_ULL(cfg->stream); 2029 2030 if (cfg->enabled) { 2031 dev_dbg(dev, "stream %u already enabled on %s:%u\n", 2032 cfg->stream, sd->entity.name, pad); 2033 ret = -EALREADY; 2034 goto done; 2035 } 2036 } 2037 2038 if (found_streams != streams_mask) { 2039 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n", 2040 streams_mask & ~found_streams, sd->entity.name, pad); 2041 ret = -EINVAL; 2042 goto done; 2043 } 2044 2045 dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask); 2046 2047 /* Call the .enable_streams() operation. */ 2048 ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad, 2049 streams_mask); 2050 if (ret) { 2051 dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad, 2052 streams_mask, ret); 2053 goto done; 2054 } 2055 2056 /* Mark the streams as enabled. */ 2057 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2058 struct v4l2_subdev_stream_config *cfg = 2059 &state->stream_configs.configs[i]; 2060 2061 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream))) 2062 cfg->enabled = true; 2063 } 2064 2065 done: 2066 v4l2_subdev_unlock_state(state); 2067 2068 return ret; 2069 } 2070 EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams); 2071 2072 static int v4l2_subdev_disable_streams_fallback(struct v4l2_subdev *sd, u32 pad, 2073 u64 streams_mask) 2074 { 2075 struct device *dev = sd->entity.graph_obj.mdev->dev; 2076 unsigned int i; 2077 int ret; 2078 2079 /* 2080 * If the subdev doesn't implement pad-based stream enable, fall back 2081 * on the .s_stream() operation. This can only be done for subdevs that 2082 * have a single source pad, as sd->enabled_streams is global to the 2083 * subdev. 2084 */ 2085 if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE)) 2086 return -EOPNOTSUPP; 2087 2088 for (i = 0; i < sd->entity.num_pads; ++i) { 2089 if (i != pad && sd->entity.pads[i].flags & MEDIA_PAD_FL_SOURCE) 2090 return -EOPNOTSUPP; 2091 } 2092 2093 if ((sd->enabled_streams & streams_mask) != streams_mask) { 2094 dev_dbg(dev, "set of streams %#llx already disabled on %s:%u\n", 2095 streams_mask, sd->entity.name, pad); 2096 return -EALREADY; 2097 } 2098 2099 /* Stop streaming when the last streams are disabled. */ 2100 if (!(sd->enabled_streams & ~streams_mask)) { 2101 ret = v4l2_subdev_call(sd, video, s_stream, 0); 2102 if (ret) 2103 return ret; 2104 } 2105 2106 sd->enabled_streams &= ~streams_mask; 2107 2108 return 0; 2109 } 2110 2111 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad, 2112 u64 streams_mask) 2113 { 2114 struct device *dev = sd->entity.graph_obj.mdev->dev; 2115 struct v4l2_subdev_state *state; 2116 u64 found_streams = 0; 2117 unsigned int i; 2118 int ret; 2119 2120 /* A few basic sanity checks first. */ 2121 if (pad >= sd->entity.num_pads) 2122 return -EINVAL; 2123 2124 if (!streams_mask) 2125 return 0; 2126 2127 /* Fallback on .s_stream() if .disable_streams() isn't available. */ 2128 if (!sd->ops->pad || !sd->ops->pad->disable_streams) 2129 return v4l2_subdev_disable_streams_fallback(sd, pad, 2130 streams_mask); 2131 2132 state = v4l2_subdev_lock_and_get_active_state(sd); 2133 2134 /* 2135 * Verify that the requested streams exist and that they are not 2136 * already disabled. 2137 */ 2138 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2139 struct v4l2_subdev_stream_config *cfg = 2140 &state->stream_configs.configs[i]; 2141 2142 if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream))) 2143 continue; 2144 2145 found_streams |= BIT_ULL(cfg->stream); 2146 2147 if (!cfg->enabled) { 2148 dev_dbg(dev, "stream %u already disabled on %s:%u\n", 2149 cfg->stream, sd->entity.name, pad); 2150 ret = -EALREADY; 2151 goto done; 2152 } 2153 } 2154 2155 if (found_streams != streams_mask) { 2156 dev_dbg(dev, "streams 0x%llx not found on %s:%u\n", 2157 streams_mask & ~found_streams, sd->entity.name, pad); 2158 ret = -EINVAL; 2159 goto done; 2160 } 2161 2162 dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask); 2163 2164 /* Call the .disable_streams() operation. */ 2165 ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad, 2166 streams_mask); 2167 if (ret) { 2168 dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad, 2169 streams_mask, ret); 2170 goto done; 2171 } 2172 2173 /* Mark the streams as disabled. */ 2174 for (i = 0; i < state->stream_configs.num_configs; ++i) { 2175 struct v4l2_subdev_stream_config *cfg = 2176 &state->stream_configs.configs[i]; 2177 2178 if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream))) 2179 cfg->enabled = false; 2180 } 2181 2182 done: 2183 v4l2_subdev_unlock_state(state); 2184 2185 return ret; 2186 } 2187 EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams); 2188 2189 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable) 2190 { 2191 struct v4l2_subdev_state *state; 2192 struct v4l2_subdev_route *route; 2193 struct media_pad *pad; 2194 u64 source_mask = 0; 2195 int pad_index = -1; 2196 2197 /* 2198 * Find the source pad. This helper is meant for subdevs that have a 2199 * single source pad, so failures shouldn't happen, but catch them 2200 * loudly nonetheless as they indicate a driver bug. 2201 */ 2202 media_entity_for_each_pad(&sd->entity, pad) { 2203 if (pad->flags & MEDIA_PAD_FL_SOURCE) { 2204 pad_index = pad->index; 2205 break; 2206 } 2207 } 2208 2209 if (WARN_ON(pad_index == -1)) 2210 return -EINVAL; 2211 2212 /* 2213 * As there's a single source pad, just collect all the source streams. 2214 */ 2215 state = v4l2_subdev_lock_and_get_active_state(sd); 2216 2217 for_each_active_route(&state->routing, route) 2218 source_mask |= BIT_ULL(route->source_stream); 2219 2220 v4l2_subdev_unlock_state(state); 2221 2222 if (enable) 2223 return v4l2_subdev_enable_streams(sd, pad_index, source_mask); 2224 else 2225 return v4l2_subdev_disable_streams(sd, pad_index, source_mask); 2226 } 2227 EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper); 2228 2229 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 2230 2231 #endif /* CONFIG_MEDIA_CONTROLLER */ 2232 2233 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) 2234 { 2235 INIT_LIST_HEAD(&sd->list); 2236 BUG_ON(!ops); 2237 sd->ops = ops; 2238 sd->v4l2_dev = NULL; 2239 sd->flags = 0; 2240 sd->name[0] = '\0'; 2241 sd->grp_id = 0; 2242 sd->dev_priv = NULL; 2243 sd->host_priv = NULL; 2244 sd->privacy_led = NULL; 2245 INIT_LIST_HEAD(&sd->async_subdev_endpoint_list); 2246 #if defined(CONFIG_MEDIA_CONTROLLER) 2247 sd->entity.name = sd->name; 2248 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 2249 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; 2250 #endif 2251 } 2252 EXPORT_SYMBOL(v4l2_subdev_init); 2253 2254 void v4l2_subdev_notify_event(struct v4l2_subdev *sd, 2255 const struct v4l2_event *ev) 2256 { 2257 v4l2_event_queue(sd->devnode, ev); 2258 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev); 2259 } 2260 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event); 2261 2262 int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd) 2263 { 2264 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 2265 sd->privacy_led = led_get(sd->dev, "privacy-led"); 2266 if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT) 2267 return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led), 2268 "getting privacy LED\n"); 2269 2270 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 2271 mutex_lock(&sd->privacy_led->led_access); 2272 led_sysfs_disable(sd->privacy_led); 2273 led_trigger_remove(sd->privacy_led); 2274 led_set_brightness(sd->privacy_led, 0); 2275 mutex_unlock(&sd->privacy_led->led_access); 2276 } 2277 #endif 2278 return 0; 2279 } 2280 EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led); 2281 2282 void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd) 2283 { 2284 #if IS_REACHABLE(CONFIG_LEDS_CLASS) 2285 if (!IS_ERR_OR_NULL(sd->privacy_led)) { 2286 mutex_lock(&sd->privacy_led->led_access); 2287 led_sysfs_enable(sd->privacy_led); 2288 mutex_unlock(&sd->privacy_led->led_access); 2289 led_put(sd->privacy_led); 2290 } 2291 #endif 2292 } 2293 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led); 2294