#include #include #include #include #include #include #include #include #include #include #include #include #include #define CAMERA_BUF_QUEUE_SIZE 3 #define MAX_CAMERA 9 using namespace std; using namespace kms; enum class BufferProvider { DRM, V4L2, }; class CameraPipeline { public: CameraPipeline(int cam_fd, Card& card, Crtc* crtc, Plane* plane, uint32_t x, uint32_t y, uint32_t iw, uint32_t ih, PixelFormat pixfmt, BufferProvider buffer_provider); ~CameraPipeline(); CameraPipeline(const CameraPipeline& other) = delete; CameraPipeline& operator=(const CameraPipeline& other) = delete; void show_next_frame(AtomicReq &req); int fd() const { return m_fd; } void start_streaming(); private: ExtFramebuffer* GetExtFrameBuffer(Card& card, uint32_t i, PixelFormat pixfmt); int m_fd; /* camera file descriptor */ Crtc* m_crtc; Plane* m_plane; BufferProvider m_buffer_provider; vector m_fb; /* framebuffers for DRM buffers */ vector m_extfb; /* framebuffers for V4L2 buffers */ int m_prev_fb_index; uint32_t m_in_width, m_in_height; /* camera capture resolution */ /* image properties for display */ uint32_t m_out_width, m_out_height; uint32_t m_out_x, m_out_y; }; static int buffer_export(int v4lfd, enum v4l2_buf_type bt, uint32_t index, int *dmafd) { struct v4l2_exportbuffer expbuf; memset(&expbuf, 0, sizeof(expbuf)); expbuf.type = bt; expbuf.index = index; if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) { perror("VIDIOC_EXPBUF"); return -1; } *dmafd = expbuf.fd; return 0; } ExtFramebuffer* CameraPipeline::GetExtFrameBuffer(Card& card, uint32_t i, PixelFormat pixfmt) { int r, dmafd; r = buffer_export(m_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, i, &dmafd); ASSERT(r == 0); uint32_t handle; r = drmPrimeFDToHandle(card.fd(), dmafd, &handle); ASSERT(r == 0); const PixelFormatInfo& format_info = get_pixel_format_info(pixfmt); ASSERT(format_info.num_planes == 1); uint32_t handles[4] { handle }; uint32_t pitches[4] { m_in_width * (format_info.planes[0].bitspp / 8) }; uint32_t offsets[4] { }; return new ExtFramebuffer(card, m_in_width, m_in_height, pixfmt, handles, pitches, offsets); } bool inline better_size(struct v4l2_frmsize_discrete* v4ldisc, uint32_t iw, uint32_t ih, uint32_t best_w, uint32_t best_h) { if (v4ldisc->width <= iw && v4ldisc->height <= ih && (v4ldisc->width >= best_w || v4ldisc->height >= best_h)) return true; return false; } CameraPipeline::CameraPipeline(int cam_fd, Card& card, Crtc *crtc, Plane* plane, uint32_t x, uint32_t y, uint32_t iw, uint32_t ih, PixelFormat pixfmt, BufferProvider buffer_provider) : m_fd(cam_fd), m_crtc(crtc), m_buffer_provider(buffer_provider), m_prev_fb_index(-1) { int r; uint32_t best_w = 320; uint32_t best_h = 240; struct v4l2_frmsizeenum v4lfrms = { }; v4lfrms.pixel_format = (uint32_t)pixfmt; while (ioctl(m_fd, VIDIOC_ENUM_FRAMESIZES, &v4lfrms) == 0) { if (v4lfrms.type == V4L2_FRMSIZE_TYPE_DISCRETE) { if (better_size(&v4lfrms.discrete, iw, ih, best_w, best_h)) { best_w = v4lfrms.discrete.width; best_h = v4lfrms.discrete.height; } } else { break; } v4lfrms.index++; }; m_out_width = m_in_width = best_w; m_out_height = m_in_height = best_h; /* Move it to the middle of the requested area */ m_out_x = x + iw / 2 - m_out_width / 2; m_out_y = y + ih / 2 - m_out_height / 2; struct v4l2_format v4lfmt = { }; v4lfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; r = ioctl(m_fd, VIDIOC_G_FMT, &v4lfmt); ASSERT(r == 0); v4lfmt.fmt.pix.pixelformat = (uint32_t)pixfmt; v4lfmt.fmt.pix.width = m_in_width; v4lfmt.fmt.pix.height = m_in_height; r = ioctl(m_fd, VIDIOC_S_FMT, &v4lfmt); ASSERT(r == 0); uint32_t v4l_mem; if (m_buffer_provider == BufferProvider::V4L2) v4l_mem = V4L2_MEMORY_MMAP; else v4l_mem = V4L2_MEMORY_DMABUF; struct v4l2_requestbuffers v4lreqbuf = { }; v4lreqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4lreqbuf.memory = v4l_mem; v4lreqbuf.count = CAMERA_BUF_QUEUE_SIZE; r = ioctl(m_fd, VIDIOC_REQBUFS, &v4lreqbuf); ASSERT(r == 0); ASSERT(v4lreqbuf.count == CAMERA_BUF_QUEUE_SIZE); struct v4l2_buffer v4lbuf = { }; v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4lbuf.memory = v4l_mem; for (unsigned i = 0; i < CAMERA_BUF_QUEUE_SIZE; i++) { DumbFramebuffer *fb = NULL; ExtFramebuffer *extfb = NULL; if (m_buffer_provider == BufferProvider::V4L2) extfb = GetExtFrameBuffer(card, i, pixfmt); else fb = new DumbFramebuffer(card, m_in_width, m_in_height, pixfmt); v4lbuf.index = i; if (m_buffer_provider == BufferProvider::DRM) v4lbuf.m.fd = fb->prime_fd(0); r = ioctl(m_fd, VIDIOC_QBUF, &v4lbuf); ASSERT(r == 0); if (m_buffer_provider == BufferProvider::V4L2) m_extfb.push_back(extfb); else m_fb.push_back(fb); } m_plane = plane; // Do initial plane setup with first fb, so that we only need to // set the FB when page flipping AtomicReq req(card); Framebuffer *fb; if (m_buffer_provider == BufferProvider::V4L2) fb = m_extfb[0]; else fb = m_fb[0]; req.add(m_plane, "CRTC_ID", m_crtc->id()); req.add(m_plane, "FB_ID", fb->id()); req.add(m_plane, "CRTC_X", m_out_x); req.add(m_plane, "CRTC_Y", m_out_y); req.add(m_plane, "CRTC_W", m_out_width); req.add(m_plane, "CRTC_H", m_out_height); req.add(m_plane, "SRC_X", 0); req.add(m_plane, "SRC_Y", 0); req.add(m_plane, "SRC_W", m_in_width << 16); req.add(m_plane, "SRC_H", m_in_height << 16); r = req.commit_sync(); FAIL_IF(r, "initial plane setup failed"); } CameraPipeline::~CameraPipeline() { for (unsigned i = 0; i < m_fb.size(); i++) delete m_fb[i]; for (unsigned i = 0; i < m_extfb.size(); i++) delete m_extfb[i]; ::close(m_fd); } void CameraPipeline::start_streaming() { enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; int r = ioctl(m_fd, VIDIOC_STREAMON, &type); FAIL_IF(r, "Failed to enable camera stream: %d", r); } void CameraPipeline::show_next_frame(AtomicReq& req) { int r; uint32_t v4l_mem; if (m_buffer_provider == BufferProvider::V4L2) v4l_mem = V4L2_MEMORY_MMAP; else v4l_mem = V4L2_MEMORY_DMABUF; struct v4l2_buffer v4l2buf = { }; v4l2buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4l2buf.memory = v4l_mem; r = ioctl(m_fd, VIDIOC_DQBUF, &v4l2buf); if (r != 0) { printf("VIDIOC_DQBUF ioctl failed with %d\n", errno); return; } unsigned fb_index = v4l2buf.index; Framebuffer *fb; if (m_buffer_provider == BufferProvider::V4L2) fb = m_extfb[fb_index]; else fb = m_fb[fb_index]; req.add(m_plane, "FB_ID", fb->id()); if (m_prev_fb_index >= 0) { memset(&v4l2buf, 0, sizeof(v4l2buf)); v4l2buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; v4l2buf.memory = v4l_mem; v4l2buf.index = m_prev_fb_index; if (m_buffer_provider == BufferProvider::DRM) v4l2buf.m.fd = m_fb[m_prev_fb_index]->prime_fd(0); r = ioctl(m_fd, VIDIOC_QBUF, &v4l2buf); ASSERT(r == 0); } m_prev_fb_index = fb_index; } static bool is_capture_dev(int fd) { struct v4l2_capability cap = { }; int r = ioctl(fd, VIDIOC_QUERYCAP, &cap); ASSERT(r == 0); return cap.capabilities & V4L2_CAP_VIDEO_CAPTURE; } std::vector glob(const std::string& pat) { glob_t glob_result; glob(pat.c_str(), 0, NULL, &glob_result); vector ret; for(unsigned i = 0; i < glob_result.gl_pathc; ++i) ret.push_back(string(glob_result.gl_pathv[i])); globfree(&glob_result); return ret; } static const char* usage_str = "Usage: kmscapture [OPTIONS]\n\n" "Options:\n" " -s, --single Single camera mode. Open only /dev/video0\n" " --buffer-type= Use DRM or V4L provided buffers. Default: DRM\n" " -h, --help Print this help\n" ; int main(int argc, char** argv) { BufferProvider buffer_provider = BufferProvider::DRM; bool single_cam = false; OptionSet optionset = { Option("s|single", [&]() { single_cam = true; }), Option("|buffer-type=", [&](string s) { if (s == "v4l") buffer_provider = BufferProvider::V4L2; else if (s == "drm") buffer_provider = BufferProvider::DRM; else FAIL("Invalid buffer provider: %s", s.c_str()); }), Option("h|help", [&]() { puts(usage_str); exit(-1); }), }; optionset.parse(argc, argv); if (optionset.params().size() > 0) { puts(usage_str); exit(-1); } auto pixfmt = PixelFormat::YUYV; Card card; auto conn = card.get_first_connected_connector(); auto crtc = conn->get_current_crtc(); printf("Display: %dx%d\n", crtc->width(), crtc->height()); printf("Buffer provider: %s\n", buffer_provider == BufferProvider::V4L2? "V4L" : "DRM"); vector camera_fds; for (string vidpath : glob("/dev/video*")) { int fd = ::open(vidpath.c_str(), O_RDWR | O_NONBLOCK); if (fd < 0) continue; if (!is_capture_dev(fd)) { close(fd); continue; } camera_fds.push_back(fd); if (single_cam) break; } FAIL_IF(camera_fds.size() == 0, "No cameras found"); vector available_planes; for (Plane* p : crtc->get_possible_planes()) { if (p->plane_type() != PlaneType::Overlay) continue; if (!p->supports_format(pixfmt)) continue; available_planes.push_back(p); } FAIL_IF(available_planes.size() < camera_fds.size(), "Not enough video planes for cameras"); uint32_t plane_w = crtc->width() / camera_fds.size(); vector cameras; for (unsigned i = 0; i < camera_fds.size(); ++i) { int cam_fd = camera_fds[i]; Plane* plane = available_planes[i]; auto cam = new CameraPipeline(cam_fd, card, crtc, plane, i * plane_w, 0, plane_w, crtc->height(), pixfmt, buffer_provider); cameras.push_back(cam); } unsigned nr_cameras = cameras.size(); vector fds(nr_cameras + 1); for (unsigned i = 0; i < nr_cameras; i++) { fds[i].fd = cameras[i]->fd(); fds[i].events = POLLIN; } fds[nr_cameras].fd = 0; fds[nr_cameras].events = POLLIN; for (auto cam : cameras) cam->start_streaming(); while (true) { int r = poll(fds.data(), nr_cameras + 1, -1); ASSERT(r > 0); if (fds[nr_cameras].revents != 0) break; AtomicReq req(card); for (unsigned i = 0; i < nr_cameras; i++) { if (!fds[i].revents) continue; cameras[i]->show_next_frame(req); fds[i].revents = 0; } r = req.test(); FAIL_IF(r, "Atomic commit failed: %d", r); req.commit_sync(); } for (auto cam : cameras) delete cam; } '#n233'>233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
/**
 * \file drm_irq.c
 * IRQ support
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
 *
 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "drmP.h"

#include <linux/interrupt.h>	/* For task queue support */

/**
 * Get interrupt from bus id.
 *
 * \param inode device inode.
 * \param file_priv DRM file private.
 * \param cmd command.
 * \param arg user argument, pointing to a drm_irq_busid structure.
 * \return zero on success or a negative number on failure.
 *
 * Finds the PCI device with the specified bus id and gets its IRQ number.
 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
 * to that of the device that this DRM instance attached to.
 */
int drm_irq_by_busid(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
{
	struct drm_irq_busid *p = data;

	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
		return -EINVAL;

	if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
	    (p->busnum & 0xff) != dev->pdev->bus->number ||
	    p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
		return -EINVAL;

	p->irq = dev->pdev->irq;

	DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
		  p->irq);

	return 0;
}

static void vblank_disable_fn(unsigned long arg)
{
	struct drm_device *dev = (struct drm_device *)arg;
	unsigned long irqflags;
	int i;

	if (!dev->vblank_disable_allowed)
		return;

	for (i = 0; i < dev->num_crtcs; i++) {
		spin_lock_irqsave(&dev->vbl_lock, irqflags);
		if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
		    dev->vblank_enabled[i]) {
			DRM_DEBUG("disabling vblank on crtc %d\n", i);
			dev->last_vblank[i] =
				dev->driver->get_vblank_counter(dev, i);