Linux 內核啟動信息的打印 --- dev_driver_string函數/dev_name函數


內核啟動時,常會打印出一些信息:開頭是 "驅動模塊的名字: + 具體的信息"

如:在運行的linux系統設備上,插入鼠標,就會打印出鼠標的相關信息;

[ 402.134068] input: USB Optical Mouse as /devices/soc0/soc/2100000.aips-bus/2184000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/0003:0461:4D81.0003/input/input3
[ 402.149618] hid-generic 0003:0461:4D81.0003: input: USB HID v1.11 Mouse [USB Optical Mouse] on usb-ci_hdrc.0-1/input0
紅色標注的 "input: " 是鼠標插入式,內核檢測到,鼠標是輸入設備,於是就把將鼠標與輸入模塊的驅動。

紅色標注的"hid-generic 0003:0461:4D81.0003" 是鼠標匹配的驅動是輸入模塊的hid-generic驅動。

這樣的信息輸出通常都是有 dev_info/dev_err/dev_notice/...., 具體可參考:linux/include/linux/device.h文件中這些函數的定義。

這些函數的定義格式都是相同的,只是輸出等級不同,即print_level不同。

dev_info(dev, “%s[%d] \n”, __func__, __LINE__);

這是第一個參數不同,后兩個就是printk的參數相同,格式也相同。

第一個參數dev: 就是將這個設備的模塊的名字等信息相同, 最終還是調用printk打印出來.

通常,dev第一個參數的打信息,是用兩個函數從dev中獲取信息。

const char *dev_driver_string(const struct device *dev)
{
        struct device_driver *drv;


        /* dev->driver can change to NULL underneath us because of unbinding,
         * so be careful about accessing it.  dev->bus and dev->class should
         * never change once they are set, so they don't need special care.
         */
        drv = ACCESS_ONCE(dev->driver);
        return drv ? drv->name :
                        (dev->bus ? dev->bus->name :
                        (dev->class ? dev->class->name : ""));

}

調用這個函數可以輸出 "hid-generic"

static inline const char *dev_name(const struct device *dev)
{
        /* Use the init name until the kobject becomes available */
        if (dev->init_name)
                return dev->init_name;


        return kobject_name(&dev->kobj);

}

調用這個函數,可以輸出" 0003: ......"

dev_driver_string() / dev_name() 是內核調試打印信息中常用的輸出函數
---------------------
作者:香雨亭榭
來源:CSDN
原文:https://blog.csdn.net/hpu11/article/details/80688565
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 

struct usb_interface {
/* array of alternate settings for this interface,
* stored in no particular order */
struct usb_host_interface *altsetting;

struct usb_host_interface *cur_altsetting; /* the currently
* active alternate setting */
unsigned num_altsetting; /* number of alternate settings */

/* If there is an interface association descriptor then it will list
* the associated interfaces */
struct usb_interface_assoc_descriptor *intf_assoc;

int minor; /* minor number this interface is
* bound to */
enum usb_interface_condition condition; /* state of binding */
unsigned sysfs_files_created:1; /* the sysfs attributes exist */
unsigned ep_devs_created:1; /* endpoint "devices" exist */
unsigned unregistering:1; /* unregistration is in progress */
unsigned needs_remote_wakeup:1; /* driver requires remote wakeup */
unsigned needs_altsetting0:1; /* switch to altsetting 0 is pending */
unsigned needs_binding:1; /* needs delayed unbind/rebind */
unsigned resetting_device:1; /* true: bandwidth alloc after reset */
unsigned authorized:1; /* used for interface authorization */

struct device dev; /* interface specific device info */
struct device *usb_dev;
atomic_t pm_usage_cnt; /* usage counter for autosuspend */
struct work_struct reset_ws; /* for resets in atomic context */
};

dev_name(&intf->dev)對應着USB的四元組信息,可以用這個信息,來判斷設備的具體位置,然后根據該信息,給設備固定名字

cdc_acm 3-1:1.0: ttyACM0: USB ACM device


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM