1 /* 2 * V4L2 sub-device 3 * 4 * Copyright (C) 2010 Nokia Corporation 5 * 6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> 7 * Sakari Ailus <sakari.ailus@iki.fi> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 */ 18 19 #include <linux/ioctl.h> 20 #include <linux/mm.h> 21 #include <linux/slab.h> 22 #include <linux/types.h> 23 #include <linux/videodev2.h> 24 #include <linux/export.h> 25 26 #include <media/v4l2-ctrls.h> 27 #include <media/v4l2-device.h> 28 #include <media/v4l2-ioctl.h> 29 #include <media/v4l2-fh.h> 30 #include <media/v4l2-event.h> 31 32 static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd) 33 { 34 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 35 if (sd->entity.num_pads) { 36 fh->pad = v4l2_subdev_alloc_pad_config(sd); 37 if (fh->pad == NULL) 38 return -ENOMEM; 39 } 40 #endif 41 return 0; 42 } 43 44 static void subdev_fh_free(struct v4l2_subdev_fh *fh) 45 { 46 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 47 v4l2_subdev_free_pad_config(fh->pad); 48 fh->pad = NULL; 49 #endif 50 } 51 52 static int subdev_open(struct file *file) 53 { 54 struct video_device *vdev = video_devdata(file); 55 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 56 struct v4l2_subdev_fh *subdev_fh; 57 #if defined(CONFIG_MEDIA_CONTROLLER) 58 struct media_entity *entity = NULL; 59 #endif 60 int ret; 61 62 subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL); 63 if (subdev_fh == NULL) 64 return -ENOMEM; 65 66 ret = subdev_fh_init(subdev_fh, sd); 67 if (ret) { 68 kfree(subdev_fh); 69 return ret; 70 } 71 72 v4l2_fh_init(&subdev_fh->vfh, vdev); 73 v4l2_fh_add(&subdev_fh->vfh); 74 file->private_data = &subdev_fh->vfh; 75 #if defined(CONFIG_MEDIA_CONTROLLER) 76 if (sd->v4l2_dev->mdev) { 77 entity = media_entity_get(&sd->entity); 78 if (!entity) { 79 ret = -EBUSY; 80 goto err; 81 } 82 } 83 #endif 84 85 if (sd->internal_ops && sd->internal_ops->open) { 86 ret = sd->internal_ops->open(sd, subdev_fh); 87 if (ret < 0) 88 goto err; 89 } 90 91 return 0; 92 93 err: 94 #if defined(CONFIG_MEDIA_CONTROLLER) 95 media_entity_put(entity); 96 #endif 97 v4l2_fh_del(&subdev_fh->vfh); 98 v4l2_fh_exit(&subdev_fh->vfh); 99 subdev_fh_free(subdev_fh); 100 kfree(subdev_fh); 101 102 return ret; 103 } 104 105 static int subdev_close(struct file *file) 106 { 107 struct video_device *vdev = video_devdata(file); 108 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 109 struct v4l2_fh *vfh = file->private_data; 110 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 111 112 if (sd->internal_ops && sd->internal_ops->close) 113 sd->internal_ops->close(sd, subdev_fh); 114 #if defined(CONFIG_MEDIA_CONTROLLER) 115 if (sd->v4l2_dev->mdev) 116 media_entity_put(&sd->entity); 117 #endif 118 v4l2_fh_del(vfh); 119 v4l2_fh_exit(vfh); 120 subdev_fh_free(subdev_fh); 121 kfree(subdev_fh); 122 file->private_data = NULL; 123 124 return 0; 125 } 126 127 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 128 static int check_format(struct v4l2_subdev *sd, 129 struct v4l2_subdev_format *format) 130 { 131 if (format->which != V4L2_SUBDEV_FORMAT_TRY && 132 format->which != V4L2_SUBDEV_FORMAT_ACTIVE) 133 return -EINVAL; 134 135 if (format->pad >= sd->entity.num_pads) 136 return -EINVAL; 137 138 return 0; 139 } 140 141 static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop) 142 { 143 if (crop->which != V4L2_SUBDEV_FORMAT_TRY && 144 crop->which != V4L2_SUBDEV_FORMAT_ACTIVE) 145 return -EINVAL; 146 147 if (crop->pad >= sd->entity.num_pads) 148 return -EINVAL; 149 150 return 0; 151 } 152 153 static int check_selection(struct v4l2_subdev *sd, 154 struct v4l2_subdev_selection *sel) 155 { 156 if (sel->which != V4L2_SUBDEV_FORMAT_TRY && 157 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) 158 return -EINVAL; 159 160 if (sel->pad >= sd->entity.num_pads) 161 return -EINVAL; 162 163 return 0; 164 } 165 166 static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid) 167 { 168 if (edid->pad >= sd->entity.num_pads) 169 return -EINVAL; 170 171 if (edid->blocks && edid->edid == NULL) 172 return -EINVAL; 173 174 return 0; 175 } 176 #endif 177 178 static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg) 179 { 180 struct video_device *vdev = video_devdata(file); 181 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 182 struct v4l2_fh *vfh = file->private_data; 183 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 184 struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh); 185 int rval; 186 #endif 187 188 switch (cmd) { 189 case VIDIOC_QUERYCTRL: 190 return v4l2_queryctrl(vfh->ctrl_handler, arg); 191 192 case VIDIOC_QUERY_EXT_CTRL: 193 return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg); 194 195 case VIDIOC_QUERYMENU: 196 return v4l2_querymenu(vfh->ctrl_handler, arg); 197 198 case VIDIOC_G_CTRL: 199 return v4l2_g_ctrl(vfh->ctrl_handler, arg); 200 201 case VIDIOC_S_CTRL: 202 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg); 203 204 case VIDIOC_G_EXT_CTRLS: 205 return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg); 206 207 case VIDIOC_S_EXT_CTRLS: 208 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg); 209 210 case VIDIOC_TRY_EXT_CTRLS: 211 return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg); 212 213 case VIDIOC_DQEVENT: 214 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 215 return -ENOIOCTLCMD; 216 217 return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK); 218 219 case VIDIOC_SUBSCRIBE_EVENT: 220 return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg); 221 222 case VIDIOC_UNSUBSCRIBE_EVENT: 223 return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg); 224 225 #ifdef CONFIG_VIDEO_ADV_DEBUG 226 case VIDIOC_DBG_G_REGISTER: 227 { 228 struct v4l2_dbg_register *p = arg; 229 230 if (!capable(CAP_SYS_ADMIN)) 231 return -EPERM; 232 return v4l2_subdev_call(sd, core, g_register, p); 233 } 234 case VIDIOC_DBG_S_REGISTER: 235 { 236 struct v4l2_dbg_register *p = arg; 237 238 if (!capable(CAP_SYS_ADMIN)) 239 return -EPERM; 240 return v4l2_subdev_call(sd, core, s_register, p); 241 } 242 #endif 243 244 case VIDIOC_LOG_STATUS: { 245 int ret; 246 247 pr_info("%s: ================= START STATUS =================\n", 248 sd->name); 249 ret = v4l2_subdev_call(sd, core, log_status); 250 pr_info("%s: ================== END STATUS ==================\n", 251 sd->name); 252 return ret; 253 } 254 255 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API) 256 case VIDIOC_SUBDEV_G_FMT: { 257 struct v4l2_subdev_format *format = arg; 258 259 rval = check_format(sd, format); 260 if (rval) 261 return rval; 262 263 memset(format->reserved, 0, sizeof(format->reserved)); 264 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 265 return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format); 266 } 267 268 case VIDIOC_SUBDEV_S_FMT: { 269 struct v4l2_subdev_format *format = arg; 270 271 rval = check_format(sd, format); 272 if (rval) 273 return rval; 274 275 memset(format->reserved, 0, sizeof(format->reserved)); 276 memset(format->format.reserved, 0, sizeof(format->format.reserved)); 277 return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format); 278 } 279 280 case VIDIOC_SUBDEV_G_CROP: { 281 struct v4l2_subdev_crop *crop = arg; 282 struct v4l2_subdev_selection sel; 283 284 rval = check_crop(sd, crop); 285 if (rval) 286 return rval; 287 288 memset(crop->reserved, 0, sizeof(crop->reserved)); 289 memset(&sel, 0, sizeof(sel)); 290 sel.which = crop->which; 291 sel.pad = crop->pad; 292 sel.target = V4L2_SEL_TGT_CROP; 293 294 rval = v4l2_subdev_call( 295 sd, pad, get_selection, subdev_fh->pad, &sel); 296 297 crop->rect = sel.r; 298 299 return rval; 300 } 301 302 case VIDIOC_SUBDEV_S_CROP: { 303 struct v4l2_subdev_crop *crop = arg; 304 struct v4l2_subdev_selection sel; 305 306 memset(crop->reserved, 0, sizeof(crop->reserved)); 307 rval = check_crop(sd, crop); 308 if (rval) 309 return rval; 310 311 memset(&sel, 0, sizeof(sel)); 312 sel.which = crop->which; 313 sel.pad = crop->pad; 314 sel.target = V4L2_SEL_TGT_CROP; 315 sel.r = crop->rect; 316 317 rval = v4l2_subdev_call( 318 sd, pad, set_selection, subdev_fh->pad, &sel); 319 320 crop->rect = sel.r; 321 322 return rval; 323 } 324 325 case VIDIOC_SUBDEV_ENUM_MBUS_CODE: { 326 struct v4l2_subdev_mbus_code_enum *code = arg; 327 328 if (code->which != V4L2_SUBDEV_FORMAT_TRY && 329 code->which != V4L2_SUBDEV_FORMAT_ACTIVE) 330 return -EINVAL; 331 332 if (code->pad >= sd->entity.num_pads) 333 return -EINVAL; 334 335 memset(code->reserved, 0, sizeof(code->reserved)); 336 return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad, 337 code); 338 } 339 340 case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: { 341 struct v4l2_subdev_frame_size_enum *fse = arg; 342 343 if (fse->which != V4L2_SUBDEV_FORMAT_TRY && 344 fse->which != V4L2_SUBDEV_FORMAT_ACTIVE) 345 return -EINVAL; 346 347 if (fse->pad >= sd->entity.num_pads) 348 return -EINVAL; 349 350 memset(fse->reserved, 0, sizeof(fse->reserved)); 351 return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad, 352 fse); 353 } 354 355 case VIDIOC_SUBDEV_G_FRAME_INTERVAL: { 356 struct v4l2_subdev_frame_interval *fi = arg; 357 358 if (fi->pad >= sd->entity.num_pads) 359 return -EINVAL; 360 361 memset(fi->reserved, 0, sizeof(fi->reserved)); 362 return v4l2_subdev_call(sd, video, g_frame_interval, arg); 363 } 364 365 case VIDIOC_SUBDEV_S_FRAME_INTERVAL: { 366 struct v4l2_subdev_frame_interval *fi = arg; 367 368 if (fi->pad >= sd->entity.num_pads) 369 return -EINVAL; 370 371 memset(fi->reserved, 0, sizeof(fi->reserved)); 372 return v4l2_subdev_call(sd, video, s_frame_interval, arg); 373 } 374 375 case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: { 376 struct v4l2_subdev_frame_interval_enum *fie = arg; 377 378 if (fie->which != V4L2_SUBDEV_FORMAT_TRY && 379 fie->which != V4L2_SUBDEV_FORMAT_ACTIVE) 380 return -EINVAL; 381 382 if (fie->pad >= sd->entity.num_pads) 383 return -EINVAL; 384 385 memset(fie->reserved, 0, sizeof(fie->reserved)); 386 return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad, 387 fie); 388 } 389 390 case VIDIOC_SUBDEV_G_SELECTION: { 391 struct v4l2_subdev_selection *sel = arg; 392 393 rval = check_selection(sd, sel); 394 if (rval) 395 return rval; 396 397 memset(sel->reserved, 0, sizeof(sel->reserved)); 398 return v4l2_subdev_call( 399 sd, pad, get_selection, subdev_fh->pad, sel); 400 } 401 402 case VIDIOC_SUBDEV_S_SELECTION: { 403 struct v4l2_subdev_selection *sel = arg; 404 405 rval = check_selection(sd, sel); 406 if (rval) 407 return rval; 408 409 memset(sel->reserved, 0, sizeof(sel->reserved)); 410 return v4l2_subdev_call( 411 sd, pad, set_selection, subdev_fh->pad, sel); 412 } 413 414 case VIDIOC_G_EDID: { 415 struct v4l2_subdev_edid *edid = arg; 416 417 rval = check_edid(sd, edid); 418 if (rval) 419 return rval; 420 421 return v4l2_subdev_call(sd, pad, get_edid, edid); 422 } 423 424 case VIDIOC_S_EDID: { 425 struct v4l2_subdev_edid *edid = arg; 426 427 rval = check_edid(sd, edid); 428 if (rval) 429 return rval; 430 431 return v4l2_subdev_call(sd, pad, set_edid, edid); 432 } 433 434 case VIDIOC_SUBDEV_DV_TIMINGS_CAP: { 435 struct v4l2_dv_timings_cap *cap = arg; 436 437 if (cap->pad >= sd->entity.num_pads) 438 return -EINVAL; 439 440 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 441 } 442 443 case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: { 444 struct v4l2_enum_dv_timings *dvt = arg; 445 446 if (dvt->pad >= sd->entity.num_pads) 447 return -EINVAL; 448 449 return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt); 450 } 451 452 case VIDIOC_SUBDEV_QUERY_DV_TIMINGS: 453 return v4l2_subdev_call(sd, video, query_dv_timings, arg); 454 455 case VIDIOC_SUBDEV_G_DV_TIMINGS: 456 return v4l2_subdev_call(sd, video, g_dv_timings, arg); 457 458 case VIDIOC_SUBDEV_S_DV_TIMINGS: 459 return v4l2_subdev_call(sd, video, s_dv_timings, arg); 460 #endif 461 default: 462 return v4l2_subdev_call(sd, core, ioctl, cmd, arg); 463 } 464 465 return 0; 466 } 467 468 static long subdev_ioctl(struct file *file, unsigned int cmd, 469 unsigned long arg) 470 { 471 return video_usercopy(file, cmd, arg, subdev_do_ioctl); 472 } 473 474 #ifdef CONFIG_COMPAT 475 static long subdev_compat_ioctl32(struct file *file, unsigned int cmd, 476 unsigned long arg) 477 { 478 struct video_device *vdev = video_devdata(file); 479 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 480 481 return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg); 482 } 483 #endif 484 485 static __poll_t subdev_poll(struct file *file, poll_table *wait) 486 { 487 struct video_device *vdev = video_devdata(file); 488 struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev); 489 struct v4l2_fh *fh = file->private_data; 490 491 if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS)) 492 return EPOLLERR; 493 494 poll_wait(file, &fh->wait, wait); 495 496 if (v4l2_event_pending(fh)) 497 return EPOLLPRI; 498 499 return 0; 500 } 501 502 const struct v4l2_file_operations v4l2_subdev_fops = { 503 .owner = THIS_MODULE, 504 .open = subdev_open, 505 .unlocked_ioctl = subdev_ioctl, 506 #ifdef CONFIG_COMPAT 507 .compat_ioctl32 = subdev_compat_ioctl32, 508 #endif 509 .release = subdev_close, 510 .poll = subdev_poll, 511 }; 512 513 #ifdef CONFIG_MEDIA_CONTROLLER 514 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd, 515 struct media_link *link, 516 struct v4l2_subdev_format *source_fmt, 517 struct v4l2_subdev_format *sink_fmt) 518 { 519 /* The width, height and code must match. */ 520 if (source_fmt->format.width != sink_fmt->format.width 521 || source_fmt->format.height != sink_fmt->format.height 522 || source_fmt->format.code != sink_fmt->format.code) 523 return -EPIPE; 524 525 /* The field order must match, or the sink field order must be NONE 526 * to support interlaced hardware connected to bridges that support 527 * progressive formats only. 528 */ 529 if (source_fmt->format.field != sink_fmt->format.field && 530 sink_fmt->format.field != V4L2_FIELD_NONE) 531 return -EPIPE; 532 533 return 0; 534 } 535 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default); 536 537 static int 538 v4l2_subdev_link_validate_get_format(struct media_pad *pad, 539 struct v4l2_subdev_format *fmt) 540 { 541 if (is_media_entity_v4l2_subdev(pad->entity)) { 542 struct v4l2_subdev *sd = 543 media_entity_to_v4l2_subdev(pad->entity); 544 545 fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE; 546 fmt->pad = pad->index; 547 return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt); 548 } 549 550 WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L, 551 "Driver bug! Wrong media entity type 0x%08x, entity %s\n", 552 pad->entity->function, pad->entity->name); 553 554 return -EINVAL; 555 } 556 557 int v4l2_subdev_link_validate(struct media_link *link) 558 { 559 struct v4l2_subdev *sink; 560 struct v4l2_subdev_format sink_fmt, source_fmt; 561 int rval; 562 563 rval = v4l2_subdev_link_validate_get_format( 564 link->source, &source_fmt); 565 if (rval < 0) 566 return 0; 567 568 rval = v4l2_subdev_link_validate_get_format( 569 link->sink, &sink_fmt); 570 if (rval < 0) 571 return 0; 572 573 sink = media_entity_to_v4l2_subdev(link->sink->entity); 574 575 rval = v4l2_subdev_call(sink, pad, link_validate, link, 576 &source_fmt, &sink_fmt); 577 if (rval != -ENOIOCTLCMD) 578 return rval; 579 580 return v4l2_subdev_link_validate_default( 581 sink, link, &source_fmt, &sink_fmt); 582 } 583 EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate); 584 585 struct v4l2_subdev_pad_config * 586 v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd) 587 { 588 struct v4l2_subdev_pad_config *cfg; 589 int ret; 590 591 if (!sd->entity.num_pads) 592 return NULL; 593 594 cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg), 595 GFP_KERNEL | __GFP_ZERO); 596 if (!cfg) 597 return NULL; 598 599 ret = v4l2_subdev_call(sd, pad, init_cfg, cfg); 600 if (ret < 0 && ret != -ENOIOCTLCMD) { 601 kvfree(cfg); 602 return NULL; 603 } 604 605 return cfg; 606 } 607 EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config); 608 609 void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg) 610 { 611 kvfree(cfg); 612 } 613 EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config); 614 #endif /* CONFIG_MEDIA_CONTROLLER */ 615 616 void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops) 617 { 618 INIT_LIST_HEAD(&sd->list); 619 BUG_ON(!ops); 620 sd->ops = ops; 621 sd->v4l2_dev = NULL; 622 sd->flags = 0; 623 sd->name[0] = '\0'; 624 sd->grp_id = 0; 625 sd->dev_priv = NULL; 626 sd->host_priv = NULL; 627 #if defined(CONFIG_MEDIA_CONTROLLER) 628 sd->entity.name = sd->name; 629 sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV; 630 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN; 631 #endif 632 } 633 EXPORT_SYMBOL(v4l2_subdev_init); 634 635 void v4l2_subdev_notify_event(struct v4l2_subdev *sd, 636 const struct v4l2_event *ev) 637 { 638 v4l2_event_queue(sd->devnode, ev); 639 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev); 640 } 641 EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event); 642