diff mbox series

[v8.1,3/6] media: v4l2: Extend VIDIOC_ENUM_FMT to support MC-centric devices

Message ID 20200424134331.22271-1-laurent.pinchart@ideasonboard.com (mailing list archive)
State New
Delegated to: Kieran Bingham
Headers show
Series None | expand

Commit Message

Laurent Pinchart April 24, 2020, 1:43 p.m. UTC
The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
node. For MC-centric devices, its behaviour has always been ill-defined,
with drivers implementing one of the following behaviours:

- No support for VIDIOC_ENUM_FMT at all
- Enumerating all formats supported by the video node, regardless of the
  configuration of the pipeline
- Enumerating formats supported by the video node for the active
  configuration of the connected subdevice

The first behaviour is obviously useless for applications. The second
behaviour provides the most information, but doesn't offer a way to find
what formats are compatible with a given pipeline configuration. The
third behaviour fixes that, but with the drawback that applications
can't enumerate all supported formats anymore, and have to modify the
active configuration of the pipeline to enumerate formats.

The situation is messy as none of the implemented behaviours are ideal,
and userspace can't predict what will happen as the behaviour is
driver-specific.

To fix this, let's extend the VIDIOC_ENUM_FMT with a missing capability:
enumerating pixel formats for a given media bus code. The media bus code
is passed through the v4l2_fmtdesc structure in a new mbus_code field
(repurposed from the reserved fields). With this capability in place,
applications can enumerate pixel formats for a given media bus code
without modifying the active configuration of the device.

The current behaviour of the ioctl is preserved when the new mbus_code
field is set to 0, ensuring compatibility with existing userspace. The
API extension is documented as mandatory for MC-centric devices (as
advertised through the V4L2_CAP_IO_MC capability), allowing applications
and compliance tools to easily determine the availability of the
VIDIOC_ENUM_FMT extension.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
---
Changes since v8:

- Zero the mbus_code field if V4L2_CAP_IO_MC isn't set
- Document the new mbus_code field
- Fix ref tags in documentation
---
 .../media/uapi/v4l/vidioc-enum-fmt.rst        | 25 ++++++++++++++++---
 drivers/media/v4l2-core/v4l2-ioctl.c          | 13 ++++++++--
 include/uapi/linux/videodev2.h                |  3 ++-
 3 files changed, 34 insertions(+), 7 deletions(-)

Comments

Mauro Carvalho Chehab April 29, 2020, 4:27 p.m. UTC | #1
Em Fri, 24 Apr 2020 16:43:31 +0300
Laurent Pinchart <laurent.pinchart@ideasonboard.com> escreveu:

> The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
> node. For MC-centric devices, its behaviour has always been ill-defined,
> with drivers implementing one of the following behaviours:
> 

...

The patch itself is OK. However, there's a change you did at the
documentation that it is unrelated. 

See below.

> diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> index 8ca6ab701e4a..a987debc7654 100644
> --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
>  formats in preference order, where preferred formats are returned before
>  (that is, with lower ``index`` value) less-preferred formats.
>  
> -.. note::
> -   After switching input or output the list of enumerated image
> -   formats may be different.

Why are you dropping this note?

This basically reverts this changeset:

  commit 93828d6438081649e81b8681df9bf6ad5d691650
  Author: Hans Verkuil <hans.verkuil@cisco.com>
  Date:   Mon Sep 3 09:37:18 2012 -0300

    [media] DocBook: make the G/S/TRY_FMT specification more strict
    
    - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
    - TRY_FMT should give the same result as S_FMT, all other things being equal.
    - ENUMFMT may return different formats for different inputs or outputs.
    This was decided during the 2012 Media Workshop.
    
    Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
    Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
    Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
    Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
    Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>

As far as I remember, from our 2012 discussions, some drivers may change 
the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
degrees rotation would equally affect it.

Perhaps, the removal was just some mistake. If so, please re-submit
another patch without it.

Otherwise, if are there any good reasons for such change, and it won't
cause any API regressions, please place it on a separate patch, clearly
that.

... Or, maybe you felt that it won't make sense for MC-centric devices.
On such case, please improve the note stating it on a way that it would
be understandable on both MC-centric and bridge-centrid drivers. 

Thanks,
Mauro
Laurent Pinchart April 29, 2020, 4:38 p.m. UTC | #2
Hi Mauro,

On Wed, Apr 29, 2020 at 06:27:39PM +0200, Mauro Carvalho Chehab wrote:
> Em Fri, 24 Apr 2020 16:43:31 +0300 Laurent Pinchart escreveu:
> 
> > The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
> > node. For MC-centric devices, its behaviour has always been ill-defined,
> > with drivers implementing one of the following behaviours:
> > 
> 
> ...
> 
> The patch itself is OK. However, there's a change you did at the
> documentation that it is unrelated. 
> 
> See below.
> 
> > diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > index 8ca6ab701e4a..a987debc7654 100644
> > --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
> >  formats in preference order, where preferred formats are returned before
> >  (that is, with lower ``index`` value) less-preferred formats.
> >  
> > -.. note::
> > -   After switching input or output the list of enumerated image
> > -   formats may be different.
> 
> Why are you dropping this note?
> 
> This basically reverts this changeset:
> 
>   commit 93828d6438081649e81b8681df9bf6ad5d691650
>   Author: Hans Verkuil <hans.verkuil@cisco.com>
>   Date:   Mon Sep 3 09:37:18 2012 -0300
> 
>     [media] DocBook: make the G/S/TRY_FMT specification more strict
>     
>     - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
>     - TRY_FMT should give the same result as S_FMT, all other things being equal.
>     - ENUMFMT may return different formats for different inputs or outputs.
>     This was decided during the 2012 Media Workshop.
>     
>     Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
>     Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
>     Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>     Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
>     Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
> 
> As far as I remember, from our 2012 discussions, some drivers may change 
> the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
> VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
> degrees rotation would equally affect it.
> 
> Perhaps, the removal was just some mistake. If so, please re-submit
> another patch without it.
> 
> Otherwise, if are there any good reasons for such change, and it won't
> cause any API regressions, please place it on a separate patch, clearly
> that.
> 
> ... Or, maybe you felt that it won't make sense for MC-centric devices.
> On such case, please improve the note stating it on a way that it would
> be understandable on both MC-centric and bridge-centrid drivers.

I'm not dropping the requirement, I'm rewriting it :-) The patch indeed
removes the above, but adds the following

----
If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
<device-capabilities>`, applications shall initialize the ``mbus_code`` field
to zero and drivers shall ignore the value of the field.  Drivers shall
enumerate all image formats. The enumerated formats may depend on the active
input or output of the device.

If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
<device-capabilities>`, applications may initialize the ``mbus_code`` field to
a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
image formats that can produce (for video output devices) or be produced from
(for video capture devices) that media bus code.  Regardless of the value of
the ``mbus_code`` field, the enumerated image formats shall not depend on the
active configuration of the video device or device pipeline. Enumeration shall
otherwise operate as previously described.
----

Note the last sentence for the !V4L2_CAP_IO_MC case:

"The enumerated formats may depend on the active input or output of the
device."

We can extend it with

"The enumerated formats may depend on the active input or output of the
device, switching the input or output may thus produce different lists
of enumerated formats."

I think it's a bit overkill as it's saying the same thing twice, but if
you prefer that, I'm fine with it.

For the V4L2_CAP_IO_MC case there's no .s_input() or .s_output(), so the
note isn't applicable.
Mauro Carvalho Chehab April 29, 2020, 6:01 p.m. UTC | #3
Em Wed, 29 Apr 2020 19:38:49 +0300
Laurent Pinchart <laurent.pinchart@ideasonboard.com> escreveu:

> Hi Mauro,
> 
> On Wed, Apr 29, 2020 at 06:27:39PM +0200, Mauro Carvalho Chehab wrote:
> > Em Fri, 24 Apr 2020 16:43:31 +0300 Laurent Pinchart escreveu:
> >   
> > > The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
> > > node. For MC-centric devices, its behaviour has always been ill-defined,
> > > with drivers implementing one of the following behaviours:
> > >   
> > 
> > ...
> > 
> > The patch itself is OK. However, there's a change you did at the
> > documentation that it is unrelated. 
> > 
> > See below.
> >   
> > > diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > index 8ca6ab701e4a..a987debc7654 100644
> > > --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
> > >  formats in preference order, where preferred formats are returned before
> > >  (that is, with lower ``index`` value) less-preferred formats.
> > >  
> > > -.. note::
> > > -   After switching input or output the list of enumerated image
> > > -   formats may be different.  
> > 
> > Why are you dropping this note?
> > 
> > This basically reverts this changeset:
> > 
> >   commit 93828d6438081649e81b8681df9bf6ad5d691650
> >   Author: Hans Verkuil <hans.verkuil@cisco.com>
> >   Date:   Mon Sep 3 09:37:18 2012 -0300
> > 
> >     [media] DocBook: make the G/S/TRY_FMT specification more strict
> >     
> >     - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
> >     - TRY_FMT should give the same result as S_FMT, all other things being equal.
> >     - ENUMFMT may return different formats for different inputs or outputs.
> >     This was decided during the 2012 Media Workshop.
> >     
> >     Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
> >     Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> >     Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> >     Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
> >     Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
> > 
> > As far as I remember, from our 2012 discussions, some drivers may change 
> > the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
> > VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
> > degrees rotation would equally affect it.
> > 
> > Perhaps, the removal was just some mistake. If so, please re-submit
> > another patch without it.
> > 
> > Otherwise, if are there any good reasons for such change, and it won't
> > cause any API regressions, please place it on a separate patch, clearly
> > that.
> > 
> > ... Or, maybe you felt that it won't make sense for MC-centric devices.
> > On such case, please improve the note stating it on a way that it would
> > be understandable on both MC-centric and bridge-centrid drivers.  
> 
> I'm not dropping the requirement, I'm rewriting it :-) The patch indeed
> removes the above, but adds the following
> 
> ----
> If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> <device-capabilities>`, applications shall initialize the ``mbus_code`` field
> to zero and drivers shall ignore the value of the field.  Drivers shall
> enumerate all image formats. The enumerated formats may depend on the active
> input or output of the device.
> 
> If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
> <device-capabilities>`, applications may initialize the ``mbus_code`` field to
> a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
> ``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
> image formats that can produce (for video output devices) or be produced from
> (for video capture devices) that media bus code.  Regardless of the value of
> the ``mbus_code`` field, the enumerated image formats shall not depend on the
> active configuration of the video device or device pipeline. Enumeration shall
> otherwise operate as previously described.

Hmm... it took me re-reading this text 4 or 5 times, in order to understand
that you're actually meaning bridge-centric devices here :-)

Also, you placed two independent and unrelated information at the same
paragraph.

IMHO, you should let it more clear, like, for example adding a numerated
list, like:

1. Bridge-centric devices

   As such devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
   <device-capabilities>`, applications shall initialize the ``mbus_code`` field
   to zero and drivers shall ignore the value of the field.  

   Drivers shall enumerate all image formats. The enumerated formats may depend
   on the active input or output of the device.

2. MC-centric devices

  As such devices advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
  <device-capabilities>`, applications may initialize the ``mbus_code`` field to
  a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. 

  If the ``mbus_code`` field is not zero, drivers shall restrict enumeration to 
  only the image formats that can produce (for video output devices) or be produced 
  from (for video capture devices) that media bus code.  

  Regardless of the value of the ``mbus_code`` field, the enumerated image formats
  shall not depend on the active configuration of the video device or device
  pipeline. Enumeration shall otherwise operate as previously described.

- 

On a side note: can a MC-centric device fill ``mbus_code`` field with zero?
The second paragraph seems to contradict the first one with mandates that
``mbus_code`` should be a valid format.

> ----
> 
> Note the last sentence for the !V4L2_CAP_IO_MC case:
> 
> "The enumerated formats may depend on the active input or output of the
> device."
> 
> We can extend it with
> 
> "The enumerated formats may depend on the active input or output of the
> device, switching the input or output may thus produce different lists
> of enumerated formats."

That sounds better, but "may" seems to weak for my taste. So, I would 
also add:

  Applications should (re)call VIDIOC_ENUM_FMT after changing the ative
  input or output of the device.

> 
> I think it's a bit overkill as it's saying the same thing twice, but if
> you prefer that, I'm fine with it.
> 
> For the V4L2_CAP_IO_MC case there's no .s_input() or .s_output(), so the
> note isn't applicable.

Ok.

Thanks,
Mauro
Laurent Pinchart April 29, 2020, 6:50 p.m. UTC | #4
Hi Mauro,

On Wed, Apr 29, 2020 at 08:01:40PM +0200, Mauro Carvalho Chehab wrote:
> Em Wed, 29 Apr 2020 19:38:49 +0300 Laurent Pinchart escreveu:
> > On Wed, Apr 29, 2020 at 06:27:39PM +0200, Mauro Carvalho Chehab wrote:
> > > Em Fri, 24 Apr 2020 16:43:31 +0300 Laurent Pinchart escreveu:
> > >   
> > > > The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
> > > > node. For MC-centric devices, its behaviour has always been ill-defined,
> > > > with drivers implementing one of the following behaviours:
> > > 
> > > ...
> > > 
> > > The patch itself is OK. However, there's a change you did at the
> > > documentation that it is unrelated. 
> > > 
> > > See below.
> > >   
> > > > diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > index 8ca6ab701e4a..a987debc7654 100644
> > > > --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
> > > >  formats in preference order, where preferred formats are returned before
> > > >  (that is, with lower ``index`` value) less-preferred formats.
> > > >  
> > > > -.. note::
> > > > -   After switching input or output the list of enumerated image
> > > > -   formats may be different.  
> > > 
> > > Why are you dropping this note?
> > > 
> > > This basically reverts this changeset:
> > > 
> > >   commit 93828d6438081649e81b8681df9bf6ad5d691650
> > >   Author: Hans Verkuil <hans.verkuil@cisco.com>
> > >   Date:   Mon Sep 3 09:37:18 2012 -0300
> > > 
> > >     [media] DocBook: make the G/S/TRY_FMT specification more strict
> > >     
> > >     - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
> > >     - TRY_FMT should give the same result as S_FMT, all other things being equal.
> > >     - ENUMFMT may return different formats for different inputs or outputs.
> > >     This was decided during the 2012 Media Workshop.
> > >     
> > >     Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
> > >     Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> > >     Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > >     Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
> > >     Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
> > > 
> > > As far as I remember, from our 2012 discussions, some drivers may change 
> > > the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
> > > VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
> > > degrees rotation would equally affect it.
> > > 
> > > Perhaps, the removal was just some mistake. If so, please re-submit
> > > another patch without it.
> > > 
> > > Otherwise, if are there any good reasons for such change, and it won't
> > > cause any API regressions, please place it on a separate patch, clearly
> > > that.
> > > 
> > > ... Or, maybe you felt that it won't make sense for MC-centric devices.
> > > On such case, please improve the note stating it on a way that it would
> > > be understandable on both MC-centric and bridge-centrid drivers.  
> > 
> > I'm not dropping the requirement, I'm rewriting it :-) The patch indeed
> > removes the above, but adds the following
> > 
> > ----
> > If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> > <device-capabilities>`, applications shall initialize the ``mbus_code`` field
> > to zero and drivers shall ignore the value of the field.  Drivers shall
> > enumerate all image formats. The enumerated formats may depend on the active
> > input or output of the device.
> > 
> > If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
> > <device-capabilities>`, applications may initialize the ``mbus_code`` field to
> > a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
> > ``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
> > image formats that can produce (for video output devices) or be produced from
> > (for video capture devices) that media bus code.  Regardless of the value of
> > the ``mbus_code`` field, the enumerated image formats shall not depend on the
> > active configuration of the video device or device pipeline. Enumeration shall
> > otherwise operate as previously described.
> 
> Hmm... it took me re-reading this text 4 or 5 times, in order to understand
> that you're actually meaning bridge-centric devices here :-)
> 
> Also, you placed two independent and unrelated information at the same
> paragraph.
> 
> IMHO, you should let it more clear, like, for example adding a numerated
> list, like:
> 
> 1. Bridge-centric devices
> 
>    As such devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>    <device-capabilities>`, applications shall initialize the ``mbus_code`` field
>    to zero and drivers shall ignore the value of the field.

I could settle for

   These devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
   <device-capabilities>`. Applications shall initialize the ``mbus_code`` field
   to zero and drivers shall ignore the value of the field.

and similarly below. It bothers me though, as "bridge" isn't formally
defined anywhere in the userspace API documentation. It's more formal to
explain the behaviour of individual ioctls based solely on the
V4L2_CAP_IO_MC flag, and gather all the explanation of what
bridge-centric vs. MC-centric means in a single place, an introductory
section, instead of spreading it through documentation of individual
ioctls. V4L2 has more UAPI documentation than most other kernel APIs,
but there are lots of places where details are not very clear.
Standardizing ioctl documentation on the use of common vocabulary
("may", "shall", ...) and using clearly defined concepts (e.g.
V4L2_CAP_IO_MC) instead of losely defined ones (e.g. Bridge-centric
devices) that are explained in non-normative sections would increase
clarity. I thus prefer the wording in v8.1 of this patch, or possibly
with the small extension I've proposed in my previous e-mail.

Hans, Sakari, what do you think ?

> 
>    Drivers shall enumerate all image formats. The enumerated formats may depend
>    on the active input or output of the device.
> 
> 2. MC-centric devices
> 
>   As such devices advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>   <device-capabilities>`, applications may initialize the ``mbus_code`` field to
>   a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. 
> 
>   If the ``mbus_code`` field is not zero, drivers shall restrict enumeration to 
>   only the image formats that can produce (for video output devices) or be produced 
>   from (for video capture devices) that media bus code.  
> 
>   Regardless of the value of the ``mbus_code`` field, the enumerated image formats
>   shall not depend on the active configuration of the video device or device
>   pipeline. Enumeration shall otherwise operate as previously described.
> 
> - 
> 
> On a side note: can a MC-centric device fill ``mbus_code`` field with zero?

I assume you mean application here ? mbus_code is set by applications.

> The second paragraph seems to contradict the first one with mandates that
> ``mbus_code`` should be a valid format.

The first paragraph says that applications "may" set the field. The
second paragraph explains what happens if they do.

> > ----
> > 
> > Note the last sentence for the !V4L2_CAP_IO_MC case:
> > 
> > "The enumerated formats may depend on the active input or output of the
> > device."
> > 
> > We can extend it with
> > 
> > "The enumerated formats may depend on the active input or output of the
> > device, switching the input or output may thus produce different lists
> > of enumerated formats."
> 
> That sounds better, but "may" seems to weak for my taste. So, I would 
> also add:
> 
>   Applications should (re)call VIDIOC_ENUM_FMT after changing the ative
>   input or output of the device.

That only applies if they need to enumerate formats in the first place.
I'd prefer avoiding making this more complex. "may" is standard
vocabulary in specifications to indicate a permitted behaviour. How
applications react to that is based on their needs, and I don't think
here is the right place to try and imagine what those needs are.

> > I think it's a bit overkill as it's saying the same thing twice, but if
> > you prefer that, I'm fine with it.
> > 
> > For the V4L2_CAP_IO_MC case there's no .s_input() or .s_output(), so the
> > note isn't applicable.
Mauro Carvalho Chehab April 29, 2020, 7:46 p.m. UTC | #5
Em Wed, 29 Apr 2020 21:50:33 +0300
Laurent Pinchart <laurent.pinchart@ideasonboard.com> escreveu:

> Hi Mauro,
> 
> On Wed, Apr 29, 2020 at 08:01:40PM +0200, Mauro Carvalho Chehab wrote:
> > Em Wed, 29 Apr 2020 19:38:49 +0300 Laurent Pinchart escreveu:  
> > > On Wed, Apr 29, 2020 at 06:27:39PM +0200, Mauro Carvalho Chehab wrote:  
> > > > Em Fri, 24 Apr 2020 16:43:31 +0300 Laurent Pinchart escreveu:
> > > >     
> > > > > The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
> > > > > node. For MC-centric devices, its behaviour has always been ill-defined,
> > > > > with drivers implementing one of the following behaviours:  
> > > > 
> > > > ...
> > > > 
> > > > The patch itself is OK. However, there's a change you did at the
> > > > documentation that it is unrelated. 
> > > > 
> > > > See below.
> > > >     
> > > > > diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > > index 8ca6ab701e4a..a987debc7654 100644
> > > > > --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > > +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > > @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
> > > > >  formats in preference order, where preferred formats are returned before
> > > > >  (that is, with lower ``index`` value) less-preferred formats.
> > > > >  
> > > > > -.. note::
> > > > > -   After switching input or output the list of enumerated image
> > > > > -   formats may be different.    
> > > > 
> > > > Why are you dropping this note?
> > > > 
> > > > This basically reverts this changeset:
> > > > 
> > > >   commit 93828d6438081649e81b8681df9bf6ad5d691650
> > > >   Author: Hans Verkuil <hans.verkuil@cisco.com>
> > > >   Date:   Mon Sep 3 09:37:18 2012 -0300
> > > > 
> > > >     [media] DocBook: make the G/S/TRY_FMT specification more strict
> > > >     
> > > >     - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
> > > >     - TRY_FMT should give the same result as S_FMT, all other things being equal.
> > > >     - ENUMFMT may return different formats for different inputs or outputs.
> > > >     This was decided during the 2012 Media Workshop.
> > > >     
> > > >     Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
> > > >     Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> > > >     Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > >     Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
> > > >     Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
> > > > 
> > > > As far as I remember, from our 2012 discussions, some drivers may change 
> > > > the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
> > > > VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
> > > > degrees rotation would equally affect it.
> > > > 
> > > > Perhaps, the removal was just some mistake. If so, please re-submit
> > > > another patch without it.
> > > > 
> > > > Otherwise, if are there any good reasons for such change, and it won't
> > > > cause any API regressions, please place it on a separate patch, clearly
> > > > that.
> > > > 
> > > > ... Or, maybe you felt that it won't make sense for MC-centric devices.
> > > > On such case, please improve the note stating it on a way that it would
> > > > be understandable on both MC-centric and bridge-centrid drivers.    
> > > 
> > > I'm not dropping the requirement, I'm rewriting it :-) The patch indeed
> > > removes the above, but adds the following
> > > 
> > > ----
> > > If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> > > <device-capabilities>`, applications shall initialize the ``mbus_code`` field
> > > to zero and drivers shall ignore the value of the field.  Drivers shall
> > > enumerate all image formats. The enumerated formats may depend on the active
> > > input or output of the device.
> > > 
> > > If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
> > > <device-capabilities>`, applications may initialize the ``mbus_code`` field to
> > > a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
> > > ``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
> > > image formats that can produce (for video output devices) or be produced from
> > > (for video capture devices) that media bus code.  Regardless of the value of
> > > the ``mbus_code`` field, the enumerated image formats shall not depend on the
> > > active configuration of the video device or device pipeline. Enumeration shall
> > > otherwise operate as previously described.  
> > 
> > Hmm... it took me re-reading this text 4 or 5 times, in order to understand
> > that you're actually meaning bridge-centric devices here :-)
> > 
> > Also, you placed two independent and unrelated information at the same
> > paragraph.
> > 
> > IMHO, you should let it more clear, like, for example adding a numerated
> > list, like:
> > 
> > 1. Bridge-centric devices
> > 
> >    As such devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> >    <device-capabilities>`, applications shall initialize the ``mbus_code`` field
> >    to zero and drivers shall ignore the value of the field.  
> 
> I could settle for
> 
>    These devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>    <device-capabilities>`. Applications shall initialize the ``mbus_code`` field
>    to zero and drivers shall ignore the value of the field.
> 
> and similarly below. 

Works for me.

> It bothers me though, as "bridge" isn't formally
> defined anywhere in the userspace API documentation.

Actually, I submitted some patches some time ago related to documenting
it. They are the "v8, x/7" at patchwork:

	https://patchwork.linuxtv.org/project/linux-media/list/?series=&submitter=&state=&q=&archive=&delegate=1

> It's more formal to
> explain the behaviour of individual ioctls based solely on the
> V4L2_CAP_IO_MC flag, and gather all the explanation of what
> bridge-centric vs. MC-centric means in a single place, an introductory
> section, instead of spreading it through documentation of individual
> ioctls. 

This patch in particular (from the above series) does that:

	https://patchwork.linuxtv.org/patch/44904/

-

That's said, that series is outdated. IMO, we should do another spin 
on that, in the light of the V4L2_CAP_IO_MC changes (making the 
introductory chapter point to it).

I'll seek for some time and re-submit such patchset.

> V4L2 has more UAPI documentation than most other kernel APIs,
> but there are lots of places where details are not very clear.
> Standardizing ioctl documentation on the use of common vocabulary
> ("may", "shall", ...) and using clearly defined concepts (e.g.
> V4L2_CAP_IO_MC) instead of losely defined ones (e.g. Bridge-centric
> devices) that are explained in non-normative sections would increase
> clarity. I thus prefer the wording in v8.1 of this patch, or possibly
> with the small extension I've proposed in my previous e-mail.

See, my proposal is to have both ;-)

The list have the "less formal" definition (MC-centric x bridge-centric),
with is how media developers usually refer to those (as you did at
the subject of this patch series), while the contents preserve a more 
technical definition, based on V4L2_CAP_IO_MC field.

> 
> Hans, Sakari, what do you think ?
> 
> > 
> >    Drivers shall enumerate all image formats. The enumerated formats may depend
> >    on the active input or output of the device.
> > 
> > 2. MC-centric devices
> > 
> >   As such devices advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> >   <device-capabilities>`, applications may initialize the ``mbus_code`` field to
> >   a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. 
> > 
> >   If the ``mbus_code`` field is not zero, drivers shall restrict enumeration to 
> >   only the image formats that can produce (for video output devices) or be produced 
> >   from (for video capture devices) that media bus code.  
> > 
> >   Regardless of the value of the ``mbus_code`` field, the enumerated image formats
> >   shall not depend on the active configuration of the video device or device
> >   pipeline. Enumeration shall otherwise operate as previously described.
> > 
> > - 
> > 
> > On a side note: can a MC-centric device fill ``mbus_code`` field with zero?  
> 
> I assume you mean application here ? mbus_code is set by applications.
> 
> > The second paragraph seems to contradict the first one with mandates that
> > ``mbus_code`` should be a valid format.  
> 
> The first paragraph says that applications "may" set the field. The
> second paragraph explains what happens if they do.

Ok. I would then write it as:

 2. MC-centric devices
 
   These devices advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
   <device-capabilities>`.

   Applications shall initialize the ``mbus_code`` field to zero or to
   a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. 
 
   If the ``mbus_code`` field is zero, the driver shall return all
   media formats supported by the driver. Otherwise, drivers shall
   restrict enumeration to only the image formats that can produce 
   (for video output devices) or be produced from (for video capture
   devices) that media bus code.

   If the ``mbus_code`` is not valid or if it is not supported, the
   driver shall return -EINVAL.
 
   Regardless of the value of the ``mbus_code`` field, the enumerated image
   formats shall not depend on the active configuration of the video device
   or device pipeline. Enumeration shall otherwise operate as previously
   described.


> 
> > > ----
> > > 
> > > Note the last sentence for the !V4L2_CAP_IO_MC case:
> > > 
> > > "The enumerated formats may depend on the active input or output of the
> > > device."
> > > 
> > > We can extend it with
> > > 
> > > "The enumerated formats may depend on the active input or output of the
> > > device, switching the input or output may thus produce different lists
> > > of enumerated formats."  
> > 
> > That sounds better, but "may" seems to weak for my taste. So, I would 
> > also add:
> > 
> >   Applications should (re)call VIDIOC_ENUM_FMT after changing the ative
> >   input or output of the device.  
> 
> That only applies if they need to enumerate formats in the first place.
> I'd prefer avoiding making this more complex. "may" is standard
> vocabulary in specifications to indicate a permitted behaviour. How
> applications react to that is based on their needs, and I don't think
> here is the right place to try and imagine what those needs are.

Works for me.

Thanks,
Mauro
Sakari Ailus April 29, 2020, 8:08 p.m. UTC | #6
Hi Laurent, Mauro,

On Wed, Apr 29, 2020 at 09:50:33PM +0300, Laurent Pinchart wrote:
> Hi Mauro,
> 
> On Wed, Apr 29, 2020 at 08:01:40PM +0200, Mauro Carvalho Chehab wrote:
> > Em Wed, 29 Apr 2020 19:38:49 +0300 Laurent Pinchart escreveu:
> > > On Wed, Apr 29, 2020 at 06:27:39PM +0200, Mauro Carvalho Chehab wrote:
> > > > Em Fri, 24 Apr 2020 16:43:31 +0300 Laurent Pinchart escreveu:
> > > >   
> > > > > The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
> > > > > node. For MC-centric devices, its behaviour has always been ill-defined,
> > > > > with drivers implementing one of the following behaviours:
> > > > 
> > > > ...
> > > > 
> > > > The patch itself is OK. However, there's a change you did at the
> > > > documentation that it is unrelated. 
> > > > 
> > > > See below.
> > > >   
> > > > > diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > > index 8ca6ab701e4a..a987debc7654 100644
> > > > > --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > > +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
> > > > > @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
> > > > >  formats in preference order, where preferred formats are returned before
> > > > >  (that is, with lower ``index`` value) less-preferred formats.
> > > > >  
> > > > > -.. note::
> > > > > -   After switching input or output the list of enumerated image
> > > > > -   formats may be different.  
> > > > 
> > > > Why are you dropping this note?
> > > > 
> > > > This basically reverts this changeset:
> > > > 
> > > >   commit 93828d6438081649e81b8681df9bf6ad5d691650
> > > >   Author: Hans Verkuil <hans.verkuil@cisco.com>
> > > >   Date:   Mon Sep 3 09:37:18 2012 -0300
> > > > 
> > > >     [media] DocBook: make the G/S/TRY_FMT specification more strict
> > > >     
> > > >     - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
> > > >     - TRY_FMT should give the same result as S_FMT, all other things being equal.
> > > >     - ENUMFMT may return different formats for different inputs or outputs.
> > > >     This was decided during the 2012 Media Workshop.
> > > >     
> > > >     Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
> > > >     Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
> > > >     Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > >     Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
> > > >     Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
> > > > 
> > > > As far as I remember, from our 2012 discussions, some drivers may change 
> > > > the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
> > > > VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
> > > > degrees rotation would equally affect it.
> > > > 
> > > > Perhaps, the removal was just some mistake. If so, please re-submit
> > > > another patch without it.
> > > > 
> > > > Otherwise, if are there any good reasons for such change, and it won't
> > > > cause any API regressions, please place it on a separate patch, clearly
> > > > that.
> > > > 
> > > > ... Or, maybe you felt that it won't make sense for MC-centric devices.
> > > > On such case, please improve the note stating it on a way that it would
> > > > be understandable on both MC-centric and bridge-centrid drivers.  
> > > 
> > > I'm not dropping the requirement, I'm rewriting it :-) The patch indeed
> > > removes the above, but adds the following
> > > 
> > > ----
> > > If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> > > <device-capabilities>`, applications shall initialize the ``mbus_code`` field
> > > to zero and drivers shall ignore the value of the field.  Drivers shall
> > > enumerate all image formats. The enumerated formats may depend on the active
> > > input or output of the device.
> > > 
> > > If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
> > > <device-capabilities>`, applications may initialize the ``mbus_code`` field to
> > > a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
> > > ``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
> > > image formats that can produce (for video output devices) or be produced from
> > > (for video capture devices) that media bus code.  Regardless of the value of
> > > the ``mbus_code`` field, the enumerated image formats shall not depend on the
> > > active configuration of the video device or device pipeline. Enumeration shall
> > > otherwise operate as previously described.
> > 
> > Hmm... it took me re-reading this text 4 or 5 times, in order to understand
> > that you're actually meaning bridge-centric devices here :-)
> > 
> > Also, you placed two independent and unrelated information at the same
> > paragraph.
> > 
> > IMHO, you should let it more clear, like, for example adding a numerated
> > list, like:
> > 
> > 1. Bridge-centric devices
> > 
> >    As such devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> >    <device-capabilities>`, applications shall initialize the ``mbus_code`` field
> >    to zero and drivers shall ignore the value of the field.
> 
> I could settle for
> 
>    These devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>    <device-capabilities>`. Applications shall initialize the ``mbus_code`` field
>    to zero and drivers shall ignore the value of the field.
> 
> and similarly below. It bothers me though, as "bridge" isn't formally
> defined anywhere in the userspace API documentation. It's more formal to
> explain the behaviour of individual ioctls based solely on the
> V4L2_CAP_IO_MC flag, and gather all the explanation of what
> bridge-centric vs. MC-centric means in a single place, an introductory

How about "video node centric"? That's what I recall has been used
previously to differentiate regular V4L2 uAPI drivers from MC-centric
drivers. Bridge refers to hardware whereas MC-centric is software, just as
video node centric would be.

> section, instead of spreading it through documentation of individual
> ioctls. V4L2 has more UAPI documentation than most other kernel APIs,
> but there are lots of places where details are not very clear.
> Standardizing ioctl documentation on the use of common vocabulary
> ("may", "shall", ...) and using clearly defined concepts (e.g.
> V4L2_CAP_IO_MC) instead of losely defined ones (e.g. Bridge-centric
> devices) that are explained in non-normative sections would increase
> clarity. I thus prefer the wording in v8.1 of this patch, or possibly
> with the small extension I've proposed in my previous e-mail.
> 
> Hans, Sakari, what do you think ?

My preference is with v8.1 wording, with bridge-centric replaced by video
node centric. This is because it differentiates between the flag what
actually defines device behaviour. That's what applications see, they don't
necessarily know what kind of devices they're working with when they open
the device node.
Hans Verkuil May 5, 2020, 2:27 p.m. UTC | #7
On 29/04/2020 22:08, Sakari Ailus wrote:
> Hi Laurent, Mauro,
> 
> On Wed, Apr 29, 2020 at 09:50:33PM +0300, Laurent Pinchart wrote:
>> Hi Mauro,
>>
>> On Wed, Apr 29, 2020 at 08:01:40PM +0200, Mauro Carvalho Chehab wrote:
>>> Em Wed, 29 Apr 2020 19:38:49 +0300 Laurent Pinchart escreveu:
>>>> On Wed, Apr 29, 2020 at 06:27:39PM +0200, Mauro Carvalho Chehab wrote:
>>>>> Em Fri, 24 Apr 2020 16:43:31 +0300 Laurent Pinchart escreveu:
>>>>>   
>>>>>> The VIDIOC_ENUM_FMT ioctl enumerates all formats supported by a video
>>>>>> node. For MC-centric devices, its behaviour has always been ill-defined,
>>>>>> with drivers implementing one of the following behaviours:
>>>>>
>>>>> ...
>>>>>
>>>>> The patch itself is OK. However, there's a change you did at the
>>>>> documentation that it is unrelated. 
>>>>>
>>>>> See below.
>>>>>   
>>>>>> diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
>>>>>> index 8ca6ab701e4a..a987debc7654 100644
>>>>>> --- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
>>>>>> +++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
>>>>>> @@ -48,10 +48,21 @@ one until ``EINVAL`` is returned. If applicable, drivers shall return
>>>>>>  formats in preference order, where preferred formats are returned before
>>>>>>  (that is, with lower ``index`` value) less-preferred formats.
>>>>>>  
>>>>>> -.. note::
>>>>>> -   After switching input or output the list of enumerated image
>>>>>> -   formats may be different.  
>>>>>
>>>>> Why are you dropping this note?
>>>>>
>>>>> This basically reverts this changeset:
>>>>>
>>>>>   commit 93828d6438081649e81b8681df9bf6ad5d691650
>>>>>   Author: Hans Verkuil <hans.verkuil@cisco.com>
>>>>>   Date:   Mon Sep 3 09:37:18 2012 -0300
>>>>>
>>>>>     [media] DocBook: make the G/S/TRY_FMT specification more strict
>>>>>     
>>>>>     - S/TRY_FMT should always succeed, unless an invalid type field is passed in.
>>>>>     - TRY_FMT should give the same result as S_FMT, all other things being equal.
>>>>>     - ENUMFMT may return different formats for different inputs or outputs.
>>>>>     This was decided during the 2012 Media Workshop.
>>>>>     
>>>>>     Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
>>>>>     Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
>>>>>     Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>>>>>     Acked-by: Sakari Ailus <sakari.ailus@iki.fi>
>>>>>     Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
>>>>>
>>>>> As far as I remember, from our 2012 discussions, some drivers may change 
>>>>> the enumerated image formats when some ioctls like VIDIOC_S_INPUT and
>>>>> VIDIOC_S_OUTPUT ioctls are used. I also vaguely remember that 90 and 270
>>>>> degrees rotation would equally affect it.
>>>>>
>>>>> Perhaps, the removal was just some mistake. If so, please re-submit
>>>>> another patch without it.
>>>>>
>>>>> Otherwise, if are there any good reasons for such change, and it won't
>>>>> cause any API regressions, please place it on a separate patch, clearly
>>>>> that.
>>>>>
>>>>> ... Or, maybe you felt that it won't make sense for MC-centric devices.
>>>>> On such case, please improve the note stating it on a way that it would
>>>>> be understandable on both MC-centric and bridge-centrid drivers.  
>>>>
>>>> I'm not dropping the requirement, I'm rewriting it :-) The patch indeed
>>>> removes the above, but adds the following
>>>>
>>>> ----
>>>> If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>>>> <device-capabilities>`, applications shall initialize the ``mbus_code`` field
>>>> to zero and drivers shall ignore the value of the field.  Drivers shall
>>>> enumerate all image formats. The enumerated formats may depend on the active
>>>> input or output of the device.
>>>>
>>>> If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
>>>> <device-capabilities>`, applications may initialize the ``mbus_code`` field to
>>>> a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
>>>> ``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
>>>> image formats that can produce (for video output devices) or be produced from
>>>> (for video capture devices) that media bus code.  Regardless of the value of
>>>> the ``mbus_code`` field, the enumerated image formats shall not depend on the
>>>> active configuration of the video device or device pipeline. Enumeration shall
>>>> otherwise operate as previously described.
>>>
>>> Hmm... it took me re-reading this text 4 or 5 times, in order to understand
>>> that you're actually meaning bridge-centric devices here :-)
>>>
>>> Also, you placed two independent and unrelated information at the same
>>> paragraph.
>>>
>>> IMHO, you should let it more clear, like, for example adding a numerated
>>> list, like:
>>>
>>> 1. Bridge-centric devices
>>>
>>>    As such devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>>>    <device-capabilities>`, applications shall initialize the ``mbus_code`` field
>>>    to zero and drivers shall ignore the value of the field.
>>
>> I could settle for
>>
>>    These devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
>>    <device-capabilities>`. Applications shall initialize the ``mbus_code`` field
>>    to zero and drivers shall ignore the value of the field.
>>
>> and similarly below. It bothers me though, as "bridge" isn't formally
>> defined anywhere in the userspace API documentation. It's more formal to
>> explain the behaviour of individual ioctls based solely on the
>> V4L2_CAP_IO_MC flag, and gather all the explanation of what
>> bridge-centric vs. MC-centric means in a single place, an introductory
> 
> How about "video node centric"? That's what I recall has been used
> previously to differentiate regular V4L2 uAPI drivers from MC-centric
> drivers. Bridge refers to hardware whereas MC-centric is software, just as
> video node centric would be.

I like that. Video node centric vs MC-centric. Although I think we had
this discussion before. We never really managed to come up with
satisfying names for this.

> 
>> section, instead of spreading it through documentation of individual
>> ioctls. V4L2 has more UAPI documentation than most other kernel APIs,
>> but there are lots of places where details are not very clear.
>> Standardizing ioctl documentation on the use of common vocabulary
>> ("may", "shall", ...) and using clearly defined concepts (e.g.
>> V4L2_CAP_IO_MC) instead of losely defined ones (e.g. Bridge-centric
>> devices) that are explained in non-normative sections would increase
>> clarity. I thus prefer the wording in v8.1 of this patch, or possibly
>> with the small extension I've proposed in my previous e-mail.
>>
>> Hans, Sakari, what do you think ?
> 
> My preference is with v8.1 wording, with bridge-centric replaced by video
> node centric. This is because it differentiates between the flag what
> actually defines device behaviour. That's what applications see, they don't
> necessarily know what kind of devices they're working with when they open
> the device node.
> 

You can easily say: 'If the driver doesn't advertise the ``V4L2_CAP_IO_MC``
capability (also known as a 'video node centric' driver)'. That way you
associate the absence of the capability with the type of driver.

I would really like to keep the old text of the note, so replace:

"The enumerated formats may depend on the active input or output of the device."

with:

"After switching to another input or output the list of enumerated formats
 may be different."

I know it says the same, but the original text clearly indicates that it is
the act of switching inputs/outputs that can cause a change of formats.

Just referring to the "active" input/output leaves it to the reader to infer
that the list can change when you select a new active input/output. It's better
(less work for the reader) to be explicit about that.

Regards,

	Hans
Mauro Carvalho Chehab May 6, 2020, 9:51 a.m. UTC | #8
Em Wed, 29 Apr 2020 23:08:08 +0300
Sakari Ailus <sakari.ailus@linux.intel.com> escreveu:

> > I could settle for
> > 
> >    These devices don't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
> >    <device-capabilities>`. Applications shall initialize the ``mbus_code`` field
> >    to zero and drivers shall ignore the value of the field.
> > 
> > and similarly below. It bothers me though, as "bridge" isn't formally
> > defined anywhere in the userspace API documentation. It's more formal to
> > explain the behaviour of individual ioctls based solely on the
> > V4L2_CAP_IO_MC flag, and gather all the explanation of what
> > bridge-centric vs. MC-centric means in a single place, an introductory  
> 
> How about "video node centric"? 

Works for me.

Thanks,
Mauro
Mauro Carvalho Chehab May 6, 2020, 9:53 a.m. UTC | #9
Em Tue, 5 May 2020 16:27:25 +0200
Hans Verkuil <hverkuil@xs4all.nl> escreveu:

> "The enumerated formats may depend on the active input or output of the device."
> 
> with:
> 
> "After switching to another input or output the list of enumerated formats
>  may be different."
>

This change sounds OK to me as well. It should be clear that this applies
to video node centric devices, though.

Thanks,
Mauro
diff mbox series

Patch

diff --git a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
index 8ca6ab701e4a..a987debc7654 100644
--- a/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
+++ b/Documentation/media/uapi/v4l/vidioc-enum-fmt.rst
@@ -48,10 +48,21 @@  one until ``EINVAL`` is returned. If applicable, drivers shall return
 formats in preference order, where preferred formats are returned before
 (that is, with lower ``index`` value) less-preferred formats.
 
-.. note::
+If the driver doesn't advertise the ``V4L2_CAP_IO_MC`` :ref:`capability
+<device-capabilities>`, applications shall initialize the ``mbus_code`` field
+to zero and drivers shall ignore the value of the field.  Drivers shall
+enumerate all image formats. The enumerated formats may depend on the active
+input or output of the device.
 
-   After switching input or output the list of enumerated image
-   formats may be different.
+If the driver advertises the ``V4L2_CAP_IO_MC`` :ref:`capability
+<device-capabilities>`, applications may initialize the ``mbus_code`` field to
+a valid :ref:`media bus format code <v4l2-mbus-pixelcode>`. If the
+``mbus_code`` field is not zero, drivers shall restrict enumeration to only the
+image formats that can produce (for video output devices) or be produced from
+(for video capture devices) that media bus code.  Regardless of the value of
+the ``mbus_code`` field, the enumerated image formats shall not depend on the
+active configuration of the video device or device pipeline. Enumeration shall
+otherwise operate as previously described.
 
 
 .. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.7cm}|
@@ -106,7 +117,13 @@  formats in preference order, where preferred formats are returned before
 	   These codes are not the same as those used
 	   in the Windows world.
     * - __u32
-      - ``reserved``\ [4]
+      - ``mbus_code``
+      - Media bus code restricting the enumerated formats, set by the
+        application. Only applicable to drivers that advertise the
+        ``V4L2_CAP_IO_MC`` :ref:`capability <device-capabilities>`, shall be 0
+        otherwise.
+    * - __u32
+      - ``reserved``\ [3]
       - Reserved for future extensions. Drivers must set the array to
 	zero.
 
diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c
index 3545a8adf844..0550f20d7177 100644
--- a/drivers/media/v4l2-core/v4l2-ioctl.c
+++ b/drivers/media/v4l2-core/v4l2-ioctl.c
@@ -264,12 +264,13 @@  static void v4l_print_fmtdesc(const void *arg, bool write_only)
 {
 	const struct v4l2_fmtdesc *p = arg;
 
-	pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
+	pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, mbus_code=0x%04x, description='%.*s'\n",
 		p->index, prt_names(p->type, v4l2_type_names),
 		p->flags, (p->pixelformat & 0xff),
 		(p->pixelformat >>  8) & 0xff,
 		(p->pixelformat >> 16) & 0xff,
 		(p->pixelformat >> 24) & 0xff,
+		p->mbus_code,
 		(int)sizeof(p->description), p->description);
 }
 
@@ -1467,12 +1468,20 @@  static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
 	struct video_device *vdev = video_devdata(file);
 	struct v4l2_fmtdesc *p = arg;
 	int ret = check_fmt(file, p->type);
+	u32 mbus_code;
 	u32 cap_mask;
 
 	if (ret)
 		return ret;
 	ret = -EINVAL;
 
+	if (!(vdev->device_caps & V4L2_CAP_IO_MC))
+		p->mbus_code = 0;
+
+	mbus_code = p->mbus_code;
+	CLEAR_AFTER_FIELD(p, type);
+	p->mbus_code = mbus_code;
+
 	switch (p->type) {
 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
@@ -2752,7 +2761,7 @@  DEFINE_V4L_STUB_FUNC(dv_timings_cap)
 
 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
 	IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
-	IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
+	IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, 0),
 	IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
 	IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
 	IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index c793263a3705..0ece960844a5 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -779,7 +779,8 @@  struct v4l2_fmtdesc {
 	__u32               flags;
 	__u8		    description[32];   /* Description string */
 	__u32		    pixelformat;       /* Format fourcc      */
-	__u32		    reserved[4];
+	__u32		    mbus_code;		/* Media bus code    */
+	__u32		    reserved[3];
 };
 
 #define V4L2_FMT_FLAG_COMPRESSED		0x0001