linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Vincent Mailhol <vincent.mailhol@gmail.com>
Cc: linux-usb@vger.kernel.org, Alan Stern <stern@rowland.harvard.edu>
Subject: Re: [Question] How to check whether or not a device is an USB device?
Date: Thu, 24 Nov 2022 17:02:04 +0100	[thread overview]
Message-ID: <Y3+VfNdt/K7UtRcw@kroah.com> (raw)
In-Reply-To: <CAMZ6RqKB70YohOEeUmYWthbi8N3ADVLQUg-=j6enf5cDQC_eSg@mail.gmail.com>

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

  reply	other threads:[~2022-11-24 16:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2022-11-24 16:08   ` Vincent Mailhol
2022-11-24 16:15     ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y3+VfNdt/K7UtRcw@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=vincent.mailhol@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).