linux-kernellinux-device-driverspi

How to get device enumeration from struct device in Linux kernel module code


I am working with a clock driver for a device which resides on the SPI bus, device is already probed and attached to SPI bus, spidev is struct spidev pointer and spidev->dev points to device and spidev pointer passed in to a function where I print stuff:

dev_dbg(&spidev->dev, "Requested rate: %u", rate); 

and I get

myClockDriver spi1.0: myClock_output_clk_set_rate: Requested rate: 10000000

How is the word "spi1.0" costructed/printed, is it thru one of the elements in struct device? Is it a string or put together as a string from an enumerated value? I get spi1.0 and spi3.0 in my prints. I want to use such an enumeration in my function.

I have tried to print spi->dev->init_name but it seems to be NULL.


Solution

  • dev_dbg() ends up (in the cases when it's not a no-op) in

    dev_printk_emit(..., "...%s %s: ...", dev_driver_string(dev), dev_name(dev), ...);
    

    In your case the first string is myClockDriver, which comes from the driver (it's obvious), and the second one is spiX.Y. This as you may notice from the function name comes from the device name. That name for the device comes from the controller name and chip select number (see spi_dev_set_name() implementation:

    dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), spi_get_chipselect(spi, 0));
    

    where controller name comes from this line (see implementation of spi_register_controller()):

    /*
     * Register the device, then userspace will see it.
     * Registration fails if the bus ID is in use.
     */
    dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);