From patchwork Thu Nov 9 21:02:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 163585 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp705194vqs; Thu, 9 Nov 2023 13:03:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCAsPV5LMos0usrFzpj9FaK9B+fUNQu5LFBaQfY7cXLIGklYnTc/gINebzVBrTyqFieRHk X-Received: by 2002:a05:6808:1887:b0:3b2:bc91:cbca with SMTP id bi7-20020a056808188700b003b2bc91cbcamr3244867oib.20.1699563827210; Thu, 09 Nov 2023 13:03:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699563827; cv=none; d=google.com; s=arc-20160816; b=IzdcMwgjzUVQqDxBQ5CSWXueA0RAn3tcGIoAYMLkUEHTEl5+wlUdxLsHaZxS5UZbgR lfGuvLFfhp/NwMfHzfq8yam5cXd6NKQ3bsQ4B0ngEKMg6C89YAnSdDrSznH/Q0TPVHUW 7j1sVDoVDn0s+68AV/OXL3bNQ+wDURodhaAyr5H1+UhU5iopCIXeZvyMyjlN7LErKmmt P4+nEuLOJK1L6dBzVvjuVwnt+90hbXo93M2JOhGSaHAgyqJhYLV8bBKZGTY1ll4EjcQF J+0Icfm7ORUHdsF5dFJelWD4JFkP6dsVjOYFRlsB3gJzIjb1rCXiAMyR7aHi0s2AlCbR Lm5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JCTRxGHgwhK84h/ZzI1NgoxjCoaQojD1iTt87t+p91Y=; fh=3fOBkHgfYXZxXJLNWeMhIReWwutwCiJkoU/oq1nHxIw=; b=I+WXylOdvso48amNw/OdQzocTcbjhwoexFuUBY5jZBr8L3CYlxQXR0XvHWSF2vXEAO +cR8uZ3YjbxQwLM42gzc0/bF4oUcviDl9NwWfbG468TYANSRJrl6w4UJXqgPNBC/OTOC hy9Pjivun50SNvFVri8D+HP/oBpjV4Yy6d0TGdpX/nAzk54CRLE6OXucKoL1Afog2pbO nIxsl5/b2cgTZQ8KFRnD0OIX56dA+Fiv8eR6yGyCkfJ2yaKMXjzGoCHvihNExlX/kyrR ialH7O/XAknr30Ols//C/l1qmjK7ZjbNQDnQMbqd8N22IwrIwJe3Ew7VQx/2gDvp7thV HCnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=puu+jPwP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n18-20020a05680803b200b003ae50da9a6bsi6210038oie.212.2023.11.09.13.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 13:03:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=puu+jPwP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0C2A2839C872; Thu, 9 Nov 2023 13:03:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345266AbjKIVDf (ORCPT + 30 others); Thu, 9 Nov 2023 16:03:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345247AbjKIVD3 (ORCPT ); Thu, 9 Nov 2023 16:03:29 -0500 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4ECC24239; Thu, 9 Nov 2023 13:03:27 -0800 (PST) Received: from umang.jain (unknown [103.251.226.64]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 55B4416F9; Thu, 9 Nov 2023 22:03:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1699563783; bh=oK58tgcoyg38J0Dq4OGc76mXK/hbRlzuovhuQYajoWg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=puu+jPwPfZFvKJNfQICRdZQm0DdIaZZlPMzqvOXstUdy3EydTpu/sOmMZqTp1/q1P cYLpsotW8oMll7X7oWV20x7T/hM07tm3mCgsOqlDsTb/gotV/YB9dulNgeuUrGENoL JBFfEey3DytCml2huYbLGvvMmoa/aFpjxOO/QepM= From: Umang Jain To: linux-media@vger.kernel.org, kernel-list@raspberrypi.com, linux-kernel@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-staging@lists.linux.dev Cc: Dave Stevenson , Kieran Bingham , Laurent Pinchart , "Ricardo B . Marliere" , Dan Carpenter , Stefan Wahren , Dave Stevenson , Umang Jain Subject: [PATCH v2 03/15] media: videobuf2: Allow exporting of a struct dmabuf Date: Thu, 9 Nov 2023 16:02:55 -0500 Message-ID: <20231109210309.638594-4-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231109210309.638594-1-umang.jain@ideasonboard.com> References: <20231109210309.638594-1-umang.jain@ideasonboard.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 09 Nov 2023 13:03:44 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782121839329147004 X-GMAIL-MSGID: 1782121839329147004 From: Dave Stevenson videobuf2 only allowed exporting a dmabuf as a file descriptor, but there are instances where having the struct dma_buf is useful within the kernel. Split the current implementation into two, one step which exports a struct dma_buf, and the second which converts that into an fd. Signed-off-by: Dave Stevenson Signed-off-by: Umang Jain --- .../media/common/videobuf2/videobuf2-core.c | 36 +++++++++++++------ include/media/videobuf2-core.h | 15 ++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 27aee92f3eea..8ba85c2db375 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -2229,49 +2229,49 @@ static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, return -EINVAL; } -int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, - unsigned int index, unsigned int plane, unsigned int flags) +struct dma_buf *vb2_core_expbuf_dmabuf(struct vb2_queue *q, unsigned int type, + unsigned int index, unsigned int plane, + unsigned int flags) { struct vb2_buffer *vb = NULL; struct vb2_plane *vb_plane; - int ret; struct dma_buf *dbuf; if (q->memory != VB2_MEMORY_MMAP) { dprintk(q, 1, "queue is not currently set up for mmap\n"); - return -EINVAL; + return ERR_PTR(-EINVAL); } if (!q->mem_ops->get_dmabuf) { dprintk(q, 1, "queue does not support DMA buffer exporting\n"); - return -EINVAL; + return ERR_PTR(-EINVAL); } if (flags & ~(O_CLOEXEC | O_ACCMODE)) { dprintk(q, 1, "queue does support only O_CLOEXEC and access mode flags\n"); - return -EINVAL; + return ERR_PTR(-EINVAL); } if (type != q->type) { dprintk(q, 1, "invalid buffer type\n"); - return -EINVAL; + return ERR_PTR(-EINVAL); } if (index >= q->num_buffers) { dprintk(q, 1, "buffer index out of range\n"); - return -EINVAL; + return ERR_PTR(-EINVAL); } vb = q->bufs[index]; if (plane >= vb->num_planes) { dprintk(q, 1, "buffer plane out of range\n"); - return -EINVAL; + return ERR_PTR(-EINVAL); } if (vb2_fileio_is_active(q)) { dprintk(q, 1, "expbuf: file io in progress\n"); - return -EBUSY; + return ERR_PTR(-EBUSY); } vb_plane = &vb->planes[plane]; @@ -2283,9 +2283,23 @@ int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, if (IS_ERR_OR_NULL(dbuf)) { dprintk(q, 1, "failed to export buffer %d, plane %d\n", index, plane); - return -EINVAL; + return ERR_PTR(-EINVAL); } + return dbuf; +} +EXPORT_SYMBOL_GPL(vb2_core_expbuf_dmabuf); + +int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, + unsigned int index, unsigned int plane, unsigned int flags) +{ + struct dma_buf *dbuf; + int ret; + + dbuf = vb2_core_expbuf_dmabuf(q, type, index, plane, flags); + if (IS_ERR(dbuf)) + return PTR_ERR(dbuf); + ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE); if (ret < 0) { dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n", diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 4b6a9d2ea372..cba4e495f6a2 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -925,6 +925,21 @@ int vb2_core_streamon(struct vb2_queue *q, unsigned int type); */ int vb2_core_streamoff(struct vb2_queue *q, unsigned int type); +/** + * vb2_core_expbuf_dmabuf() - Export a buffer as a dma_buf structure + * @q: videobuf2 queue + * @type: buffer type + * @index: id number of the buffer + * @plane: index of the plane to be exported, 0 for single plane queues + * @flags: flags for newly created file, currently only O_CLOEXEC is + * supported, refer to manual of open syscall for more details + * + * Return: Returns the dmabuf pointer + */ +struct dma_buf *vb2_core_expbuf_dmabuf(struct vb2_queue *q, unsigned int type, + unsigned int index, unsigned int plane, + unsigned int flags); + /** * vb2_core_expbuf() - Export a buffer as a file descriptor. * @q: pointer to &struct vb2_queue with videobuf2 queue.