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/ioctl.h> 12 #include <linux/mm.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 #include <linux/types.h> 16 #include <linux/videodev2.h> 17 #include <linux/export.h> 18 #include <linux/version.h> 19 20 #include <media/v4l2-ctrls.h> 21 #include <media/v4l2-device.h> 22 #include <media/v4l2-ioctl.h> 23 #include <media/v4l2-fh.h> 24 #include <media/v4l2-event.h> 25 26 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 27 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) 28 { 29 if (sd->entity.num_pads) { 30 fh->pad = v4l2_subdev_alloc_pad_config(sd); 31 if (fh->pad == NULL) 32 return -ENOMEM; 33 } 34 35 return 0; 36 } 37 38 static void subdev_fh_free(struct v4l2_subdev_fh *fh) 39 { 40 v4l2_subdev_free_pad_config(fh->pad); 41 fh->pad = NULL; 42 } 43 44 static int subdev_open(struct file *file) 45 { 46 struct video_device *vdev = video_devdata(file); 47 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 48 struct v4l2_subdev_fh *subdev_fh; 49 int ret; 50 51 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); 52 if (subdev_fh == NULL) 53 return -ENOMEM; 54 55 ret = subdev_fh_init(subdev_fh, sd); 56 if (ret) { 57 kfree(subdev_fh); 58 return ret; 59 } 60 61 v4l2_fh_init(&subdev_fh->vfh, vdev); 62 v4l2_fh_add(&subdev_fh->vfh); 63 file->private_data = &subdev_fh->vfh; 64 #if defined(CONFIG_MEDIA_CONTROLLER) 65 if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) { 66 struct module *owner; 67 68 owner = sd->entity.graph_obj.mdev->dev->driver->owner; 69 if (!try_module_get(owner)) { 70 ret = -EBUSY; 71 goto err; 72 } 73 subdev_fh->owner = owner; 74 } 75 #endif 76 77 if (sd->internal_ops && sd->internal_ops->open) { 78 ret = sd->internal_ops->open(sd, subdev_fh); 79 if (ret < 0) 80 goto err; 81 } 82 83 return 0; 84 85 err: 86 module_put(subdev_fh->owner); 87 v4l2_fh_del(&subdev_fh->vfh); 88 v4l2_fh_exit(&subdev_fh->vfh); 89 subdev_fh_free(subdev_fh); 90 kfree(subdev_fh); 91 92 return ret; 93 } 94 95 static int subdev_close(struct file *file) 96 { 97 struct video_device *vdev = video_devdata(file); 98 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 99 struct v4l2_fh *vfh = file->private_data; 100 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 101 102 if (sd->internal_ops && sd->internal_ops->close) 103 sd->internal_ops->close(sd, subdev_fh); 104 module_put(subdev_fh->owner); 105 v4l2_fh_del(vfh); 106 v4l2_fh_exit(vfh); 107 subdev_fh_free(subdev_fh); 108 kfree(subdev_fh); 109 file->private_data = NULL; 110 111 return 0; 112 } 113 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 114 static int subdev_open(struct file *file) 115 { 116 return -ENODEV; 117 } 118 119 static int subdev_close(struct file *file) 120 { 121 return -ENODEV; 122 } 123 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 124 125 static inline int check_which(u32 which) 126 { 127 if (which != V4L2_SUBDEV_FORMAT_TRY && 128 which != V4L2_SUBDEV_FORMAT_ACTIVE) 129 return -EINVAL; 130 131 return 0; 132 } 133 134 static inline int check_pad(struct v4l2_subdev *sd, u32 pad) 135 { 136 #if defined(CONFIG_MEDIA_CONTROLLER) 137 if (sd->entity.num_pads) { 138 if (pad >= sd->entity.num_pads) 139 return -EINVAL; 140 return 0; 141 } 142 #endif 143 /* allow pad 0 on subdevices not registered as media entities */ 144 if (pad > 0) 145 return -EINVAL; 146 return 0; 147 } 148 149 static int check_cfg(u32 which, struct v4l2_subdev_pad_config *cfg) 150 { 151 if (which == V4L2_SUBDEV_FORMAT_TRY && !cfg) 152 return -EINVAL; 153 154 return 0; 155 } 156 157 static inline int check_format(struct v4l2_subdev *sd, 158 struct v4l2_subdev_pad_config *cfg, 159 struct v4l2_subdev_format *format) 160 { 161 if (!format) 162 return -EINVAL; 163 164 return check_which(format->which) ? : check_pad(sd, format->pad) ? : 165 check_cfg(format->which, cfg); 166 } 167 168 static int call_get_fmt(struct v4l2_subdev *sd, 169 struct v4l2_subdev_pad_config *cfg, 170 struct v4l2_subdev_format *format) 171 { 172 return check_format(sd, cfg, format) ? : 173 sd->ops->pad->get_fmt(sd, cfg, format); 174 } 175 176 static int call_set_fmt(struct v4l2_subdev *sd, 177 struct v4l2_subdev_pad_config *cfg, 178 struct v4l2_subdev_format *format) 179 { 180 return check_format(sd, cfg, format) ? : 181 sd->ops->pad->set_fmt(sd, cfg, format); 182 } 183 184 static int call_enum_mbus_code(struct v4l2_subdev *sd, 185 struct v4l2_subdev_pad_config *cfg, 186 struct v4l2_subdev_mbus_code_enum *code) 187 { 188 if (!code) 189 return -EINVAL; 190 191 return check_which(code->which) ? : check_pad(sd, code->pad) ? : 192 check_cfg(code->which, cfg) ? : 193 sd->ops->pad->enum_mbus_code(sd, cfg, code); 194 } 195 196 static int call_enum_frame_size(struct v4l2_subdev *sd, 197 struct v4l2_subdev_pad_config *cfg, 198 struct v4l2_subdev_frame_size_enum *fse) 199 { 200 if (!fse) 201 return -EINVAL; 202 203 return check_which(fse->which) ? : check_pad(sd, fse->pad) ? : 204 check_cfg(fse->which, cfg) ? : 205 sd->ops->pad->enum_frame_size(sd, cfg, fse); 206 } 207 208 static inline int check_frame_interval(struct v4l2_subdev *sd, 209 struct v4l2_subdev_frame_interval *fi) 210 { 211 if (!fi) 212 return -EINVAL; 213 214 return check_pad(sd, fi->pad); 215 } 216 217 static int call_g_frame_interval(struct v4l2_subdev *sd, 218 struct v4l2_subdev_frame_interval *fi) 219 { 220 return check_frame_interval(sd, fi) ? : 221 sd->ops->video->g_frame_interval(sd, fi); 222 } 223 224 static int call_s_frame_interval(struct v4l2_subdev *sd, 225 struct v4l2_subdev_frame_interval *fi) 226 { 227 return check_frame_interval(sd, fi) ? : 228 sd->ops->video->s_frame_interval(sd, fi); 229 } 230 231 static int call_enum_frame_interval(struct v4l2_subdev *sd, 232 struct v4l2_subdev_pad_config *cfg, 233 struct v4l2_subdev_frame_interval_enum *fie) 234 { 235 if (!fie) 236 return -EINVAL; 237 238 return check_which(fie->which) ? : check_pad(sd, fie->pad) ? : 239 check_cfg(fie->which, cfg) ? : 240 sd->ops->pad->enum_frame_interval(sd, cfg, fie); 241 } 242 243 static inline int check_selection(struct v4l2_subdev *sd, 244 struct v4l2_subdev_pad_config *cfg, 245 struct v4l2_subdev_selection *sel) 246 { 247 if (!sel) 248 return -EINVAL; 249 250 return check_which(sel->which) ? : check_pad(sd, sel->pad) ? : 251 check_cfg(sel->which, cfg); 252 } 253 254 static int call_get_selection(struct v4l2_subdev *sd, 255 struct v4l2_subdev_pad_config *cfg, 256 struct v4l2_subdev_selection *sel) 257 { 258 return check_selection(sd, cfg, sel) ? : 259 sd->ops->pad->get_selection(sd, cfg, sel); 260 } 261 262 static int call_set_selection(struct v4l2_subdev *sd, 263 struct v4l2_subdev_pad_config *cfg, 264 struct v4l2_subdev_selection *sel) 265 { 266 return check_selection(sd, cfg, sel) ? : 267 sd->ops->pad->set_selection(sd, cfg, sel); 268 } 269 270 static inline int check_edid(struct v4l2_subdev *sd, 271 struct v4l2_subdev_edid *edid) 272 { 273 if (!edid) 274 return -EINVAL; 275 276 if (edid->blocks && edid->edid == NULL) 277 return -EINVAL; 278 279 return check_pad(sd, edid->pad); 280 } 281 282 static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 283 { 284 return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid); 285 } 286 287 static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 288 { 289 return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid); 290 } 291 292 static int call_dv_timings_cap(struct v4l2_subdev *sd, 293 struct v4l2_dv_timings_cap *cap) 294 { 295 if (!cap) 296 return -EINVAL; 297 298 return check_pad(sd, cap->pad) ? : 299 sd->ops->pad->dv_timings_cap(sd, cap); 300 } 301 302 static int call_enum_dv_timings(struct v4l2_subdev *sd, 303 struct v4l2_enum_dv_timings *dvt) 304 { 305 if (!dvt) 306 return -EINVAL; 307 308 return check_pad(sd, dvt->pad) ? : 309 sd->ops->pad->enum_dv_timings(sd, dvt); 310 } 311 312 static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = { 313 .get_fmt = call_get_fmt, 314 .set_fmt = call_set_fmt, 315 .enum_mbus_code = call_enum_mbus_code, 316 .enum_frame_size = call_enum_frame_size, 317 .enum_frame_interval = call_enum_frame_interval, 318 .get_selection = call_get_selection, 319 .set_selection = call_set_selection, 320 .get_edid = call_get_edid, 321 .set_edid = call_set_edid, 322 .dv_timings_cap = call_dv_timings_cap, 323 .enum_dv_timings = call_enum_dv_timings, 324 }; 325 326 static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = { 327 .g_frame_interval = call_g_frame_interval, 328 .s_frame_interval = call_s_frame_interval, 329 }; 330 331 const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = { 332 .pad = &v4l2_subdev_call_pad_wrappers, 333 .video = &v4l2_subdev_call_video_wrappers, 334 }; 335 EXPORT_SYMBOL(v4l2_subdev_call_wrappers); 336 337 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 338 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) 339 { 340 struct video_device *vdev = video_devdata(file); 341 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 342 struct v4l2_fh *vfh = file->private_data; 343 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 344 bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags); 345 int rval; 346 347 switch (cmd) { 348 case VIDIOC_SUBDEV_QUERYCAP: { 349 struct v4l2_subdev_capability *cap = arg; 350 351 memset(cap->reserved, 0, sizeof(cap->reserved)); 352 cap->version = LINUX_VERSION_CODE; 353 cap->capabilities = ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0; 354 355 return 0; 356 } 357 358 case VIDIOC_QUERYCTRL: 359 /* 360 * TODO: this really should be folded into v4l2_queryctrl (this 361 * currently returns -EINVAL for NULL control handlers). 362 * However, v4l2_queryctrl() is still called directly by 363 * drivers as well and until that has been addressed I believe 364 * it is safer to do the check here. The same is true for the 365 * other control ioctls below. 366 */ 367 if (!vfh->ctrl_handler) 368 return -ENOTTY; 369 return v4l2_queryctrl(vfh->ctrl_handler, arg); 370 371 case VIDIOC_QUERY_EXT_CTRL: 372 if (!vfh->ctrl_handler) 373 return -ENOTTY; 374 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); 375 376 case VIDIOC_QUERYMENU: 377 if (!vfh->ctrl_handler) 378 return -ENOTTY; 379 return v4l2_querymenu(vfh->ctrl_handler, arg); 380 381 case VIDIOC_G_CTRL: 382 if (!vfh->ctrl_handler) 383 return -ENOTTY; 384 return v4l2_g_ctrl(vfh->ctrl_handler, arg); 385 386 case VIDIOC_S_CTRL: 387 if (!vfh->ctrl_handler) 388 return -ENOTTY; 389 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); 390 391 case VIDIOC_G_EXT_CTRLS: 392 if (!vfh->ctrl_handler) 393 return -ENOTTY; 394 return v4l2_g_ext_ctrls(vfh->ctrl_handler, 395 vdev, sd->v4l2_dev->mdev, arg); 396 397 case VIDIOC_S_EXT_CTRLS: 398 if (!vfh->ctrl_handler) 399 return -ENOTTY; 400 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, 401 vdev, sd->v4l2_dev->mdev, arg); 402 403 case VIDIOC_TRY_EXT_CTRLS: 404 if (!vfh->ctrl_handler) 405 return -ENOTTY; 406 return v4l2_try_ext_ctrls(vfh->ctrl_handler, 407 vdev, sd->v4l2_dev->mdev, arg); 408 409 case VIDIOC_DQEVENT: 410 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 411 return -ENOIOCTLCMD; 412 413 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); 414 415 case VIDIOC_DQEVENT_TIME32: { 416 struct v4l2_event_time32 *ev32 = arg; 417 struct v4l2_event ev = { }; 418 419 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 420 return -ENOIOCTLCMD; 421 422 rval = v4l2_event_dequeue(vfh, &ev, file->f_flags & O_NONBLOCK); 423 424 *ev32 = (struct v4l2_event_time32) { 425 .type = ev.type, 426 .pending = ev.pending, 427 .sequence = ev.sequence, 428 .timestamp.tv_sec = ev.timestamp.tv_sec, 429 .timestamp.tv_nsec = ev.timestamp.tv_nsec, 430 .id = ev.id, 431 }; 432 433 memcpy(&ev32->u, &ev.u, sizeof(ev.u)); 434 memcpy(&ev32->reserved, &ev.reserved, sizeof(ev.reserved)); 435 436 return rval; 437 } 438 439 case VIDIOC_SUBSCRIBE_EVENT: 440 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); 441 442 case VIDIOC_UNSUBSCRIBE_EVENT: 443 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); 444 445 #ifdef CONFIG_VIDEO_ADV_DEBUG 446 case VIDIOC_DBG_G_REGISTER: 447 { 448 struct v4l2_dbg_register *p = arg; 449 450 if (!capable(CAP_SYS_ADMIN)) 451 return -EPERM; 452 return v4l2_subdev_call(sd, core, g_register, p); 453 } 454 case VIDIOC_DBG_S_REGISTER: 455 { 456 struct v4l2_dbg_register *p = arg; 457 458 if (!capable(CAP_SYS_ADMIN)) 459 return -EPERM; 460 return v4l2_subdev_call(sd, core, s_register, p); 461 } 462 case VIDIOC_DBG_G_CHIP_INFO: 463 { 464 struct v4l2_dbg_chip_info *p = arg; 465 466 if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr) 467 return -EINVAL; 468 if (sd->ops->core && sd->ops->core->s_register) 469 p->flags |= V4L2_CHIP_FL_WRITABLE; 470 if (sd->ops->core && sd->ops->core->g_register) 471 p->flags |= V4L2_CHIP_FL_READABLE; 472 strscpy(p->name, sd->name, sizeof(p->name)); 473 return 0; 474 } 475 #endif 476 477 case VIDIOC_LOG_STATUS: { 478 int ret; 479 480 pr_info("%s: ================= START STATUS =================\n", 481 sd->name); 482 ret = v4l2_subdev_call(sd, core, log_status); 483 pr_info("%s: ================== END STATUS ==================\n", 484 sd->name); 485 return ret; 486 } 487 488 case VIDIOC_SUBDEV_G_FMT: { 489 struct v4l2_subdev_format *format = arg; 490 491 memset(format->reserved, 0, sizeof(format->reserved)); 492 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 493 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format); 494 } 495 496 case VIDIOC_SUBDEV_S_FMT: { 497 struct v4l2_subdev_format *format = arg; 498 499 if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 500 return -EPERM; 501 502 memset(format->reserved, 0, sizeof(format->reserved)); 503 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 504 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format); 505 } 506 507 case VIDIOC_SUBDEV_G_CROP: { 508 struct v4l2_subdev_crop *crop = arg; 509 struct v4l2_subdev_selection sel; 510 511 memset(crop->reserved, 0, sizeof(crop->reserved)); 512 memset(&sel, 0, sizeof(sel)); 513 sel.which = crop->which; 514 sel.pad = crop->pad; 515 sel.target = V4L2_SEL_TGT_CROP; 516 517 rval = v4l2_subdev_call( 518 sd, pad, get_selection, subdev_fh->pad, &sel); 519 520 crop->rect = sel.r; 521 522 return rval; 523 } 524 525 case VIDIOC_SUBDEV_S_CROP: { 526 struct v4l2_subdev_crop *crop = arg; 527 struct v4l2_subdev_selection sel; 528 529 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 530 return -EPERM; 531 532 memset(crop->reserved, 0, sizeof(crop->reserved)); 533 memset(&sel, 0, sizeof(sel)); 534 sel.which = crop->which; 535 sel.pad = crop->pad; 536 sel.target = V4L2_SEL_TGT_CROP; 537 sel.r = crop->rect; 538 539 rval = v4l2_subdev_call( 540 sd, pad, set_selection, subdev_fh->pad, &sel); 541 542 crop->rect = sel.r; 543 544 return rval; 545 } 546 547 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { 548 struct v4l2_subdev_mbus_code_enum *code = arg; 549 550 memset(code->reserved, 0, sizeof(code->reserved)); 551 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad, 552 code); 553 } 554 555 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { 556 struct v4l2_subdev_frame_size_enum *fse = arg; 557 558 memset(fse->reserved, 0, sizeof(fse->reserved)); 559 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad, 560 fse); 561 } 562 563 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { 564 struct v4l2_subdev_frame_interval *fi = arg; 565 566 memset(fi->reserved, 0, sizeof(fi->reserved)); 567 return v4l2_subdev_call(sd, video, g_frame_interval, arg); 568 } 569 570 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { 571 struct v4l2_subdev_frame_interval *fi = arg; 572 573 if (ro_subdev) 574 return -EPERM; 575 576 memset(fi->reserved, 0, sizeof(fi->reserved)); 577 return v4l2_subdev_call(sd, video, s_frame_interval, arg); 578 } 579 580 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { 581 struct v4l2_subdev_frame_interval_enum *fie = arg; 582 583 memset(fie->reserved, 0, sizeof(fie->reserved)); 584 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad, 585 fie); 586 } 587 588 case VIDIOC_SUBDEV_G_SELECTION: { 589 struct v4l2_subdev_selection *sel = arg; 590 591 memset(sel->reserved, 0, sizeof(sel->reserved)); 592 return v4l2_subdev_call( 593 sd, pad, get_selection, subdev_fh->pad, sel); 594 } 595 596 case VIDIOC_SUBDEV_S_SELECTION: { 597 struct v4l2_subdev_selection *sel = arg; 598 599 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev) 600 return -EPERM; 601 602 memset(sel->reserved, 0, sizeof(sel->reserved)); 603 return v4l2_subdev_call( 604 sd, pad, set_selection, subdev_fh->pad, sel); 605 } 606 607 case VIDIOC_G_EDID: { 608 struct v4l2_subdev_edid *edid = arg; 609 610 return v4l2_subdev_call(sd, pad, get_edid, edid); 611 } 612 613 case VIDIOC_S_EDID: { 614 struct v4l2_subdev_edid *edid = arg; 615 616 return v4l2_subdev_call(sd, pad, set_edid, edid); 617 } 618 619 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { 620 struct v4l2_dv_timings_cap *cap = arg; 621 622 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 623 } 624 625 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { 626 struct v4l2_enum_dv_timings *dvt = arg; 627 628 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); 629 } 630 631 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: 632 return v4l2_subdev_call(sd, video, query_dv_timings, arg); 633 634 case VIDIOC_SUBDEV_G_DV_TIMINGS: 635 return v4l2_subdev_call(sd, video, g_dv_timings, arg); 636 637 case VIDIOC_SUBDEV_S_DV_TIMINGS: 638 if (ro_subdev) 639 return -EPERM; 640 641 return v4l2_subdev_call(sd, video, s_dv_timings, arg); 642 643 case VIDIOC_SUBDEV_G_STD: 644 return v4l2_subdev_call(sd, video, g_std, arg); 645 646 case VIDIOC_SUBDEV_S_STD: { 647 v4l2_std_id *std = arg; 648 649 if (ro_subdev) 650 return -EPERM; 651 652 return v4l2_subdev_call(sd, video, s_std, *std); 653 } 654 655 case VIDIOC_SUBDEV_ENUMSTD: { 656 struct v4l2_standard *p = arg; 657 v4l2_std_id id; 658 659 if (v4l2_subdev_call(sd, video, g_tvnorms, &id)) 660 return -EINVAL; 661 662 return v4l_video_std_enumstd(p, id); 663 } 664 665 case VIDIOC_SUBDEV_QUERYSTD: 666 return v4l2_subdev_call(sd, video, querystd, arg); 667 668 default: 669 return v4l2_subdev_call(sd, core, ioctl, cmd, arg); 670 } 671 672 return 0; 673 } 674 675 static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg) 676 { 677 struct video_device *vdev = video_devdata(file); 678 struct mutex *lock = vdev->lock; 679 long ret = -ENODEV; 680 681 if (lock && mutex_lock_interruptible(lock)) 682 return -ERESTARTSYS; 683 if (video_is_registered(vdev)) 684 ret = subdev_do_ioctl(file, cmd, arg); 685 if (lock) 686 mutex_unlock(lock); 687 return ret; 688 } 689 690 static long subdev_ioctl(struct file *file, unsigned int cmd, 691 unsigned long arg) 692 { 693 return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock); 694 } 695 696 #ifdef CONFIG_COMPAT 697 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 698 unsigned long arg) 699 { 700 struct video_device *vdev = video_devdata(file); 701 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 702 703 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); 704 } 705 #endif 706 707 #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 708 static long subdev_ioctl(struct file *file, unsigned int cmd, 709 unsigned long arg) 710 { 711 return -ENODEV; 712 } 713 714 #ifdef CONFIG_COMPAT 715 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 716 unsigned long arg) 717 { 718 return -ENODEV; 719 } 720 #endif 721 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */ 722 723 static __poll_t subdev_poll(struct file *file, poll_table *wait) 724 { 725 struct video_device *vdev = video_devdata(file); 726 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 727 struct v4l2_fh *fh = file->private_data; 728 729 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 730 return EPOLLERR; 731 732 poll_wait(file, &fh->wait, wait); 733 734 if (v4l2_event_pending(fh)) 735 return EPOLLPRI; 736 737 return 0; 738 } 739 740 const struct v4l2_file_operations v4l2_subdev_fops = { 741 .owner = THIS_MODULE, 742 .open = subdev_open, 743 .unlocked_ioctl = subdev_ioctl, 744 #ifdef CONFIG_COMPAT 745 .compat_ioctl32 = subdev_compat_ioctl32, 746 #endif 747 .release = subdev_close, 748 .poll = subdev_poll, 749 }; 750 751 #ifdef CONFIG_MEDIA_CONTROLLER 752 753 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity, 754 struct fwnode_endpoint *endpoint) 755 { 756 struct fwnode_handle *fwnode; 757 struct v4l2_subdev *sd; 758 759 if (!is_media_entity_v4l2_subdev(entity)) 760 return -EINVAL; 761 762 sd = media_entity_to_v4l2_subdev(entity); 763 764 fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode); 765 fwnode_handle_put(fwnode); 766 767 if (dev_fwnode(sd->dev) == fwnode) 768 return endpoint->port; 769 770 return -ENXIO; 771 } 772 EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1); 773 774 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, 775 struct media_link *link, 776 struct v4l2_subdev_format *source_fmt, 777 struct v4l2_subdev_format *sink_fmt) 778 { 779 /* The width, height and code must match. */ 780 if (source_fmt->format.width != sink_fmt->format.width 781 || source_fmt->format.height != sink_fmt->format.height 782 || source_fmt->format.code != sink_fmt->format.code) 783 return -EPIPE; 784 785 /* The field order must match, or the sink field order must be NONE 786 * to support interlaced hardware connected to bridges that support 787 * progressive formats only. 788 */ 789 if (source_fmt->format.field != sink_fmt->format.field && 790 sink_fmt->format.field != V4L2_FIELD_NONE) 791 return -EPIPE; 792 793 return 0; 794 } 795 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); 796 797 static int 798 v4l2_subdev_link_validate_get_format(struct media_pad *pad, 799 struct v4l2_subdev_format *fmt) 800 { 801 if (is_media_entity_v4l2_subdev(pad->entity)) { 802 struct v4l2_subdev *sd = 803 media_entity_to_v4l2_subdev(pad->entity); 804 805 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; 806 fmt->pad = pad->index; 807 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt); 808 } 809 810 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L, 811 "Driver bug! Wrong media entity type 0x%08x, entity %s\n", 812 pad->entity->function, pad->entity->name); 813 814 return -EINVAL; 815 } 816 817 int v4l2_subdev_link_validate(struct media_link *link) 818 { 819 struct v4l2_subdev *sink; 820 struct v4l2_subdev_format sink_fmt, source_fmt; 821 int rval; 822 823 rval = v4l2_subdev_link_validate_get_format( 824 link->source, &source_fmt); 825 if (rval < 0) 826 return 0; 827 828 rval = v4l2_subdev_link_validate_get_format( 829 link->sink, &sink_fmt); 830 if (rval < 0) 831 return 0; 832 833 sink = media_entity_to_v4l2_subdev(link->sink->entity); 834 835 rval = v4l2_subdev_call(sink, pad, link_validate, link, 836 &source_fmt, &sink_fmt); 837 if (rval != -ENOIOCTLCMD) 838 return rval; 839 840 return v4l2_subdev_link_validate_default( 841 sink, link, &source_fmt, &sink_fmt); 842 } 843 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); 844 845 struct v4l2_subdev_pad_config * 846 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd) 847 { 848 struct v4l2_subdev_pad_config *cfg; 849 int ret; 850 851 if (!sd->entity.num_pads) 852 return NULL; 853 854 cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg), 855 GFP_KERNEL | __GFP_ZERO); 856 if (!cfg) 857 return NULL; 858 859 ret = v4l2_subdev_call(sd, pad, init_cfg, cfg); 860 if (ret < 0 && ret != -ENOIOCTLCMD) { 861 kvfree(cfg); 862 return NULL; 863 } 864 865 return cfg; 866 } 867 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config); 868 869 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg) 870 { 871 kvfree(cfg); 872 } 873 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config); 874 #endif /* CONFIG_MEDIA_CONTROLLER */ 875 876 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) 877 { 878 INIT_LIST_HEAD(&sd->list); 879 BUG_ON(!ops); 880 sd->ops = ops; 881 sd->v4l2_dev = NULL; 882 sd->flags = 0; 883 sd->name[0] = '\0'; 884 sd->grp_id = 0; 885 sd->dev_priv = NULL; 886 sd->host_priv = NULL; 887 #if defined(CONFIG_MEDIA_CONTROLLER) 888 sd->entity.name = sd->name; 889 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 890 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; 891 #endif 892 } 893 EXPORT_SYMBOL(v4l2_subdev_init); 894 895 void v4l2_subdev_notify_event(struct v4l2_subdev *sd, 896 const struct v4l2_event *ev) 897 { 898 v4l2_event_queue(sd->devnode, ev); 899 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev); 900 } 901 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event); 902