Re: [Question] How to check whether or not a device is an USB device?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri. 25 Nov. 2022 at 01:02, Greg Kroah-Hartman
<gregkh@xxxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxx/
>
> 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!



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux