linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Question] How to check whether or not a device is an USB device?
@ 2022-11-24 15:40 Vincent Mailhol
  2022-11-24 16:02 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Vincent Mailhol @ 2022-11-24 15:40 UTC (permalink / raw)
  To: linux-usb; +Cc: Greg Kroah-Hartman, Alan Stern

Hello,

I am trying to have devlink report default information at the core
level. One of the attributes reported by devlink is the serial number
which is available in usb_device::serial (details: [1]).

This code would work:

        if (!strcmp(dev->parent->type->name, "usb_device")) {
                /* is indeed an USB device */

but the string comparison looks odd.

There is a is_usb_device() which does the check:

  https://elixir.bootlin.com/linux/v6.1-rc1/source/drivers/usb/core/usb.h#L152

but this function is only visible at the USB core level.

Thus my questions:

  * what is the correct way (outside of USB core) to check if a device
is an USB device? Is the string comparaison OK or is there a smarter
way?

  * would it make sense to export the is_usb_device() function?


[1] https://lore.kernel.org/netdev/20221122154934.13937-1-mailhol.vincent@wanadoo.fr/

Thank you for your help,


Yours sincerely,
Vincent Mailhol

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Question] How to check whether or not a device is an USB device?
  2022-11-24 15:40 [Question] How to check whether or not a device is an USB device? Vincent Mailhol
@ 2022-11-24 16:02 ` Greg Kroah-Hartman
  2022-11-24 16:08   ` Vincent Mailhol
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2022-11-24 16:02 UTC (permalink / raw)
  To: Vincent Mailhol; +Cc: linux-usb, Alan Stern

On Fri, Nov 25, 2022 at 12:40:34AM +0900, Vincent Mailhol wrote:
> Hello,
> 
> I am trying to have devlink report default information at the core
> level. One of the attributes reported by devlink is the serial number
> which is available in usb_device::serial (details: [1]).
> 
> This code would work:
> 
>         if (!strcmp(dev->parent->type->name, "usb_device")) {
>                 /* is indeed an USB device */
> 
> but the string comparison looks odd.
> 
> There is a is_usb_device() which does the check:
> 
>   https://elixir.bootlin.com/linux/v6.1-rc1/source/drivers/usb/core/usb.h#L152
> 
> but this function is only visible at the USB core level.
> 
> Thus my questions:
> 
>   * what is the correct way (outside of USB core) to check if a device
> is an USB device?

There is none, you should never do this.  There is a reason the driver
model does not have "types" for all devices that are allowed to be
checked.

It is up to the driver that controls this device to know what type of
device this is.

Where in the kernel are you trying to do this type of thing?

> Is the string comparaison OK or is there a smarter way?

This should not be done at all, you can not rely on it.

>   * would it make sense to export the is_usb_device() function?

Nope!

> [1] https://lore.kernel.org/netdev/20221122154934.13937-1-mailhol.vincent@wanadoo.fr/

Have the netdev driver provide a way to get the serial number of a
device.  Then in the driver, it can do the correct call as it "knows"
that this device really is a USB device.

Don't let class code like this depend on random bus types, that's not
the correct way to do things.

hope this helps,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Question] How to check whether or not a device is an USB device?
  2022-11-24 16:02 ` Greg Kroah-Hartman
@ 2022-11-24 16:08   ` Vincent Mailhol
  2022-11-24 16:15     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Vincent Mailhol @ 2022-11-24 16:08 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-usb, Alan Stern

On Fri. 25 Nov. 2022 at 01:02, Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
> On Fri, Nov 25, 2022 at 12:40:34AM +0900, Vincent Mailhol wrote:
> > Hello,
> >
> > I am trying to have devlink report default information at the core
> > level. One of the attributes reported by devlink is the serial number
> > which is available in usb_device::serial (details: [1]).
> >
> > This code would work:
> >
> >         if (!strcmp(dev->parent->type->name, "usb_device")) {
> >                 /* is indeed an USB device */
> >
> > but the string comparison looks odd.
> >
> > There is a is_usb_device() which does the check:
> >
> >   https://elixir.bootlin.com/linux/v6.1-rc1/source/drivers/usb/core/usb.h#L152
> >
> > but this function is only visible at the USB core level.
> >
> > Thus my questions:
> >
> >   * what is the correct way (outside of USB core) to check if a device
> > is an USB device?
>
> There is none, you should never do this.  There is a reason the driver
> model does not have "types" for all devices that are allowed to be
> checked.
>
> It is up to the driver that controls this device to know what type of
> device this is.
>
> Where in the kernel are you trying to do this type of thing?
>
> > Is the string comparaison OK or is there a smarter way?
>
> This should not be done at all, you can not rely on it.
>
> >   * would it make sense to export the is_usb_device() function?
>
> Nope!
>
> > [1] https://lore.kernel.org/netdev/20221122154934.13937-1-mailhol.vincent@wanadoo.fr/
>
> Have the netdev driver provide a way to get the serial number of a
> device.  Then in the driver, it can do the correct call as it "knows"
> that this device really is a USB device.

The driver already has the devlink_info_serial_number_put() to do
that. So according to your comments, no need to change anything.

> Don't let class code like this depend on random bus types, that's not
> the correct way to do things.
>
> hope this helps,

Yes, thank you for the quick answer!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Question] How to check whether or not a device is an USB device?
  2022-11-24 16:08   ` Vincent Mailhol
@ 2022-11-24 16:15     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2022-11-24 16:15 UTC (permalink / raw)
  To: Vincent Mailhol; +Cc: linux-usb, Alan Stern

On Fri, Nov 25, 2022 at 01:08:07AM +0900, Vincent Mailhol wrote:
> On Fri. 25 Nov. 2022 at 01:02, Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> > On Fri, Nov 25, 2022 at 12:40:34AM +0900, Vincent Mailhol wrote:
> > > Hello,
> > >
> > > I am trying to have devlink report default information at the core
> > > level. One of the attributes reported by devlink is the serial number
> > > which is available in usb_device::serial (details: [1]).
> > >
> > > This code would work:
> > >
> > >         if (!strcmp(dev->parent->type->name, "usb_device")) {
> > >                 /* is indeed an USB device */
> > >
> > > but the string comparison looks odd.
> > >
> > > There is a is_usb_device() which does the check:
> > >
> > >   https://elixir.bootlin.com/linux/v6.1-rc1/source/drivers/usb/core/usb.h#L152
> > >
> > > but this function is only visible at the USB core level.
> > >
> > > Thus my questions:
> > >
> > >   * what is the correct way (outside of USB core) to check if a device
> > > is an USB device?
> >
> > There is none, you should never do this.  There is a reason the driver
> > model does not have "types" for all devices that are allowed to be
> > checked.
> >
> > It is up to the driver that controls this device to know what type of
> > device this is.
> >
> > Where in the kernel are you trying to do this type of thing?
> >
> > > Is the string comparaison OK or is there a smarter way?
> >
> > This should not be done at all, you can not rely on it.
> >
> > >   * would it make sense to export the is_usb_device() function?
> >
> > Nope!
> >
> > > [1] https://lore.kernel.org/netdev/20221122154934.13937-1-mailhol.vincent@wanadoo.fr/
> >
> > Have the netdev driver provide a way to get the serial number of a
> > device.  Then in the driver, it can do the correct call as it "knows"
> > that this device really is a USB device.
> 
> The driver already has the devlink_info_serial_number_put() to do
> that. So according to your comments, no need to change anything.

Great, that's even easier!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-24 16:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-24 15:40 [Question] How to check whether or not a device is an USB device? Vincent Mailhol
2022-11-24 16:02 ` Greg Kroah-Hartman
2022-11-24 16:08   ` Vincent Mailhol
2022-11-24 16:15     ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).