Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp631971ybz; Fri, 24 Apr 2020 06:44:29 -0700 (PDT) X-Google-Smtp-Source: APiQypJ6QiJLJ51nDJIktJybOwxGpbNpcBBBhJgiCINZ4S5w4YI9+oO+URHDfoFKFL14zujbU4lX X-Received: by 2002:a17:906:3796:: with SMTP id n22mr6818150ejc.225.1587735869657; Fri, 24 Apr 2020 06:44:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587735869; cv=none; d=google.com; s=arc-20160816; b=i5YgHUixrJWtZ1Q3yjqLKxwIqP/IQGR4r5MB5v6r8jkFlCgsqai3GvHj5UfB7eamMt mY3AJD59r+jkEVZ5ezo/pkxQVphx8C9nP+fFZs2+/fUSl1wbbAO+HL56ZJ9gvMmKYjzs Lozy2W7ROoAkcL6q/E8xJ+jnNei51WJyyNr9IMqpgwkp0e6T9eZrx4iw9i7SX+BR2epb ++Vm4bM0Nt+L0iVSBCHuTOaufWba9evNzzSVujXVnuMaJH7VzNHmVVkNqB0TQcspw5h6 8u9MQDoUpZP70LmV4Rdtsns9v1bABK5aAqUYx+Nh3NiK58t9Fp13WzP4lafCUQxe4HEc CGdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=6bzGPbBR5INYjAbEOgGMPYc9YHJ427hb01U+XkmIM2w=; b=ryDIxxUShtjs9tHWb93FKjhXKfKmO0vTLnPUGicgizWKx64x0GCc085ck6tKuXxoyV M/6pXXIUW1tNFWwi2WfPyFDKHKRuWTfxKSYhW/26miZbSRw2V3Z5kxSdIlllIQJJAskL i5fP8v4fEoLdLXwMgaH9BvXDjp+JZOsx4N/LnAEnhJHRmhLBBINVEjy3lASGSWsKvjY5 gD69wxPrCVqIyoMq9rZeOTK6tnTCqsBe/aFmQxW5rFerOYLU29iC4DhGy16r9d+v6gmV tj1obh4myKsuEodnoQwB5xLRwDC8Qa3oi0fbnxPxRkf+Gv/XmVxl8+hJWRFtZgoseObf /0lQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ragnatech-se.20150623.gappssmtp.com header.s=20150623 header.b=sWsdKnmu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id di15si2728325edb.423.2020.04.24.06.44.05; Fri, 24 Apr 2020 06:44:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ragnatech-se.20150623.gappssmtp.com header.s=20150623 header.b=sWsdKnmu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727980AbgDXNl4 (ORCPT + 99 others); Fri, 24 Apr 2020 09:41:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726791AbgDXNlz (ORCPT ); Fri, 24 Apr 2020 09:41:55 -0400 Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BA2EC09B045 for ; Fri, 24 Apr 2020 06:41:55 -0700 (PDT) Received: by mail-lj1-x241.google.com with SMTP id n6so9947686ljg.12 for ; Fri, 24 Apr 2020 06:41:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ragnatech-se.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=6bzGPbBR5INYjAbEOgGMPYc9YHJ427hb01U+XkmIM2w=; b=sWsdKnmuOFjeeA/Ws/vlAHRXawQreqqdgcW35iCwHM2+/VBbptLWTFraQ6HONd1a3V IR3yJ+iHTro8gT4ssMoXJRpemdXoNLT7bUFUcVHtn6dKkJ4ITuunwv/NtOW2AiwHjVUJ ISLvu+vGmSkp1xl5MjLviloIH2Rq/giA/lhnJ7Ga4BoyasPHUYnCn9iXlEheBNHSsSTl XOdrzz94Oi8G0oCz/WEd5EIL5XuyFsIHI6sSgUkC1IVuuVh/E1juTqWSdFXw/VWXO9x5 p8jFxyUcC4HNdXJEhailJmxyixlkwEyIsY+HK08/mE0OAenvQO2IOyo2APMSRejYip4r CP9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=6bzGPbBR5INYjAbEOgGMPYc9YHJ427hb01U+XkmIM2w=; b=JO1GW1b38USuMurAUguRHZXnZJBf0LkBGfGLS7gTni0k6P0hOSnFiXux2c733aq70X WYyr9lHQWkRMBs5Z0mFu0NKA591F9hX4RynTZSQ2EW5bgrtdgRYN/3OUcak1b3VOvfEV 2TSMpV7QN20laeJ9KQJDoem341F+oKuHCjg5eFbmKmAfYCjhyeHOoj83TXIuQ9GIYBQ7 bCDchfS12+Iug1/Mjhzo8RUD6WAnwiKDxnGpROEqo81vWf6TxTBcf6HcA9ZiPfXu1xht o6NHAyccwY3VU8RUet4LLIcUua74XNaSJXp4XilPQmeygb1iLINC9Kn/GaG8F8NiP2aq FPPQ== X-Gm-Message-State: AGi0PuYv59W4mQ7C1mZNUyTf7x6mvactUsxKpjjY0V3Ji2P02z5PrXvo NNXt3zNtuGXCcRyhadu2efE72g== X-Received: by 2002:a2e:9a82:: with SMTP id p2mr6021525lji.279.1587735713510; Fri, 24 Apr 2020 06:41:53 -0700 (PDT) Received: from localhost (h-209-203.A463.priv.bahnhof.se. [155.4.209.203]) by smtp.gmail.com with ESMTPSA id z21sm4248693ljh.42.2020.04.24.06.41.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2020 06:41:52 -0700 (PDT) Date: Fri, 24 Apr 2020 15:41:52 +0200 From: Niklas =?iso-8859-1?Q?S=F6derlund?= To: Helen Koike Cc: linux-media@vger.kernel.org, kernel@collabora.com, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, hans.verkuil@cisco.com, skhan@linuxfoundation.org, mchehab@kernel.org Subject: Re: [PATCH v3 2/4] media: v4l2-common: add helper functions to call s_stream() callbacks Message-ID: <20200424134152.GB4040416@oden.dyn.berto.se> References: <20200415013044.1778572-1-helen.koike@collabora.com> <20200415013044.1778572-3-helen.koike@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200415013044.1778572-3-helen.koike@collabora.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Helen, Thanks for your work. On 2020-04-14 22:30:42 -0300, Helen Koike wrote: > Add v4l2_pipeline_stream_{enable,disable} helper functions to iterate > through the subdevices in a given stream (i.e following links from sink > to source) and call .s_stream() callback. > > Add stream_count on the subdevice object for simultaneous streaming from > different video devices which shares subdevices. > > Prevent calling .s_stream(true) if it was already called previously by > another stream. > > Prevent calling .s_stream(false) from one stream when there are still > others active. > > If .s_stream(true) fails, call .s_stream(false) in the reverse order. > > Signed-off-by: Helen Koike > --- > > Changes in v3: > - re-write helpers to use media walkers as in v1, but with > v4l2_pipeline_subdevs_get() to reverse the order we call s_stream(true) > in subdevices. > - rename size to max_size (and update docs) in v4l2_pipeline_subdevs_get() to > reflect the meaning of the argument. > - add if defined(CONFIG_MEDIA_CONTROLLER) around helpers > > Changes in v2: > - re-write helpers to not use media walkers > > drivers/media/v4l2-core/v4l2-common.c | 125 ++++++++++++++++++++++++++ > include/media/v4l2-common.h | 43 +++++++++ > include/media/v4l2-subdev.h | 2 + > 3 files changed, 170 insertions(+) > > diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c > index 9e8eb45a5b03c..2f991a1a57d7c 100644 > --- a/drivers/media/v4l2-core/v4l2-common.c > +++ b/drivers/media/v4l2-core/v4l2-common.c > @@ -442,3 +442,128 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat, > return 0; > } > EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt); > + > +#if defined(CONFIG_MEDIA_CONTROLLER) > + > +/* > + * v4l2_pipeline_subdevs_get - Assemble a list of subdevices in a pipeline > + * @subdevs: the array to be filled. > + * @max_size: max number of elements that can fit in subdevs array. > + * > + * Walk from a video node, following links from sink to source and fill the > + * array with subdevices in the path. > + * The walker performs a depth-first traversal, which means that, in a topology > + * sd1->sd2->sd3->vdev, sd1 will be the first element placed in the array. > + * > + * Return the number of subdevices filled in the array. > + */ > +static int v4l2_pipeline_subdevs_get(struct video_device *vdev, > + struct media_pipeline *pipe, > + struct v4l2_subdev **subdevs, > + unsigned int max_size) > +{ > + struct media_entity *entity = &vdev->entity; > + struct media_device *mdev = entity->graph_obj.mdev; > + int idx = 0; > + int ret; > + > + mutex_lock(&mdev->graph_mutex); > + > + if (!pipe->streaming_count) { > + ret = media_graph_walk_init(&pipe->graph, mdev); > + if (ret) { > + mutex_unlock(&mdev->graph_mutex); > + return ret; > + } > + } This confuses me a bit. Why is this conditional on streaming_count ? Looking how it's used in mc-entity.c the count is also increased/decreased with this pattern. Would it make sens to create a local 'struct media_graph graph' here and always init and clean it up ? > + > + media_graph_walk_start(&pipe->graph, entity); > + > + while ((entity = media_graph_walk_next_stream(&pipe->graph))) { > + if (!is_media_entity_v4l2_subdev(entity)) > + continue; > + if (WARN_ON(idx >= max_size)) { > + mutex_unlock(&mdev->graph_mutex); > + return -EINVAL; > + } > + subdevs[idx++] = media_entity_to_v4l2_subdev(entity); > + } > + > + if (!pipe->streaming_count) > + media_graph_walk_cleanup(&pipe->graph); > + > + mutex_unlock(&mdev->graph_mutex); > + > + return idx; > +} > + > +__must_check int v4l2_pipeline_stream_enable(struct video_device *vdev, > + struct media_pipeline *pipe) > +{ > + struct media_device *mdev = vdev->entity.graph_obj.mdev; > + struct v4l2_subdev *subdevs[MEDIA_ENTITY_ENUM_MAX_DEPTH]; > + struct v4l2_subdev *sd; > + int i, size, ret; > + > + size = v4l2_pipeline_subdevs_get(vdev, pipe, > + subdevs, ARRAY_SIZE(subdevs)); > + if (size <= 0) > + return size; > + > + /* Call s_stream() in reverse order to enable sensors last */ > + for (i = size - 1; i >= 0; i--) { > + sd = subdevs[i]; + if (sd->stream_count++) > + continue; > + dev_dbg(mdev->dev, > + "enabling stream for '%s'\n", sd->entity.name); > + ret = v4l2_subdev_call(sd, video, s_stream, true); > + if (ret && ret != -ENOIOCTLCMD) { > + sd->stream_count = 0; > + goto err_stream_disable; > + } > + } > + > + return 0; > + > +err_stream_disable: > + for (i++; i < size; i++) { > + sd = subdevs[i]; > + if (--sd->stream_count) > + continue; > + dev_dbg(mdev->dev, > + "disabling stream for '%s'\n", sd->entity.name); > + v4l2_subdev_call(sd, video, s_stream, false); > + }; > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(v4l2_pipeline_stream_enable); > + > +void v4l2_pipeline_stream_disable(struct video_device *vdev, > + struct media_pipeline *pipe) > +{ > + struct media_device *mdev = vdev->entity.graph_obj.mdev; > + struct v4l2_subdev *subdevs[MEDIA_ENTITY_ENUM_MAX_DEPTH]; > + unsigned int i; > + int size; > + > + size = v4l2_pipeline_subdevs_get(vdev, pipe, > + subdevs, ARRAY_SIZE(subdevs)); > + if (WARN_ON(size < 0)) > + return; > + > + /* Call s_stream() in order to disable sensors first */ > + for (i = 0; i < size; i++) { > + struct v4l2_subdev *sd = subdevs[i]; > + > + if (--sd->stream_count) > + continue; > + dev_dbg(mdev->dev, > + "disabling stream for '%s'\n", sd->entity.name); > + v4l2_subdev_call(sd, video, s_stream, false); small nit, maybe this can be extracted to a helper as the code is duplicated here and in the error path v4l2_pipeline_stream_enable ? > + } > +} > +EXPORT_SYMBOL_GPL(v4l2_pipeline_stream_disable); > + > +#endif /* CONFIG_MEDIA_CONTROLLER */ > diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h > index 150ee16ebd811..dc46812120cdc 100644 > --- a/include/media/v4l2-common.h > +++ b/include/media/v4l2-common.h > @@ -539,4 +539,47 @@ static inline void v4l2_buffer_set_timestamp(struct v4l2_buffer *buf, > buf->timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC; > } > > +#if defined(CONFIG_MEDIA_CONTROLLER) > + > +/** > + * v4l2_pipeline_stream_enable - Call .s_stream(true) callbacks in the stream > + * @vdev: Starting video device. > + * @pipe: Pipeline this entity belongs to. > + * > + * Call .s_stream(true) callback in all the subdevices participating in the > + * stream, i.e. following links from sink to source. > + * > + * .s_stream(true) is also called from sink to source, i.e. in a topology > + * sd1->sd2->sd3->vdev, .s_stream(true) of sd3 is called first. > + * > + * Calls to this function can be nested, in which case the same number of > + * v4l2_pipeline_stream_disable() calls will be required to disable streaming > + * through subdevices in the pipeline. > + * The pipeline pointer must be identical for all nested calls to > + * v4l2_pipeline_stream_enable(). > + */ > +__must_check int v4l2_pipeline_stream_enable(struct video_device *vdev, > + struct media_pipeline *pipe); > + > +/** > + * v4l2_pipeline_stream_disable - Call .s_stream(false) callbacks in the stream > + * @vdev: Starting video device. > + * @pipe: Pipeline this entity belongs to. > + * > + * Call .s_stream(false) callback in all the subdevices participating in the > + * stream, i.e. following links from sink to source. > + * > + * s_stream(false) is called in a reverse order from > + * v4l2_pipeline_stream_enable(), i.e. in a topology sd1->sd2->sd3->vdev, > + * .s_stream(false) of sd1 is called first. > + * > + * If multiple calls to v4l2_pipeline_stream_enable() have been made, the same > + * number of calls to this function are required to disable streaming through > + * subdevices in the pipeline. > + */ > +void v4l2_pipeline_stream_disable(struct video_device *vdev, > + struct media_pipeline *pipe); > + > +#endif /* CONFIG_MEDIA_CONTROLLER */ > + > #endif /* V4L2_COMMON_H_ */ > diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h > index a4848de598521..20f913a9f70c5 100644 > --- a/include/media/v4l2-subdev.h > +++ b/include/media/v4l2-subdev.h > @@ -838,6 +838,7 @@ struct v4l2_subdev_platform_data { > * @subdev_notifier: A sub-device notifier implicitly registered for the sub- > * device using v4l2_device_register_sensor_subdev(). > * @pdata: common part of subdevice platform data > + * @stream_count: Stream count for the subdevice. > * > * Each instance of a subdev driver should create this struct, either > * stand-alone or embedded in a larger struct. > @@ -869,6 +870,7 @@ struct v4l2_subdev { > struct v4l2_async_notifier *notifier; > struct v4l2_async_notifier *subdev_notifier; > struct v4l2_subdev_platform_data *pdata; > + unsigned int stream_count; > }; > > > -- > 2.26.0 > -- Regards, Niklas S?derlund