在《debian下使用dynamic printk分析usb转串口驱动执行流程》中使用了usb转串口,当前例子使用usb网卡分析驱动(dm9601芯片)。
仍然需要使能dynamic printk,可以参考《debian下配置dynamic printk以及重新编译内核》配置并重新编译内核。
此处使用的usb网卡是从京东购买的沐阳 Jp1081。
未插入usb网卡时,查看usb信息:
$ lsusb
Bus Device : ID 1d6b: Linux Foundation 2.0 root hub
Bus Device : ID 1d6b: Linux Foundation 3.0 root hub
Bus Device : ID 1d6b: Linux Foundation 2.0 root hub
Bus Device : ID 1d6b: Linux Foundation 2.0 root hub
Bus Device : ID 174f:114f Syntek
Bus Device : ID : Intel Corp. Integrated Rate Matching Hub
Bus Device : ID : Intel Corp. Integrated Rate Matching Hub
Bus Device : ID 105b:e065
Bus Device : ID 0bda: Realtek Semiconductor Corp.
插入usb网卡,查看usb信息:
$ lsusb
Bus Device : ID 1d6b: Linux Foundation 2.0 root hub
Bus Device : ID 1d6b: Linux Foundation 3.0 root hub
Bus Device : ID 1d6b: Linux Foundation 2.0 root hub
Bus Device : ID 1d6b: Linux Foundation 2.0 root hub
Bus Device : ID 174f:114f Syntek
Bus Device : ID : Intel Corp. Integrated Rate Matching Hub
Bus Device : ID : Intel Corp. Integrated Rate Matching Hub
Bus Device : ID 105b:e065
Bus Device : ID 0bda: Realtek Semiconductor Corp.
Bus 001 Device 074: ID 0fe6:9700 Kontron (Industrial Computer Source / ICS Advent) DM9601 Fast Ethernet Adapter
可以看到多出一行输出信息,其核心芯片是DM9601。
查看dm9601驱动信息:
$ sudo modinfo dm9601
[sudo] password for host:
filename: /lib/modules/3.2./kernel/drivers/net/usb/dm9601.ko
license: GPL
description: Davicom DM9601 USB 1.1 ethernet devices
author: Peter Korsgaard <jacmet@sunsite.dk>
alias: usb:v0A46p9000d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0FE6p9700d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0FE6p8101d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0A47p9601d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0A46p8515d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0A46p0268d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0A46p6688d*dc*dsc*dp*ic*isc*ip*
alias: usb:v0A46p9601d*dc*dsc*dp*ic*isc*ip*
alias: usb:v07AAp9601d*dc*dsc*dp*ic*isc*ip*
depends: usbnet,usbcore,mii
intree: Y
vermagic: 3.2. SMP mod_unload modversions
其中depends:一行后面的内容表示dm9601依赖于usbnet和usbcore和mii这三个驱动模块,
可以继续用modinfo查看其他模块的依赖关系,还有usbcore依赖于usb-common模块。
所以dm9601芯片依赖于dm9601、usbnet、mii、usbcore和usb-common这五个驱动程序。
查看drivers/net/usbMakefile,可以知道:
dm9601驱动由drivers/net/usb/dm9601.c生成的
usbnet驱动是由drivers/net/usb/usbnet.c生成的。
查看drivers/net/Makefile,可以知道:
mii驱动是由drivers/net/mii.c生成的。
查看drivers/usb/Makefile,可以知道:
usb-common驱动是由drivers/usb/usb-common.c生成的。
查看drivers/usb/core/Makefile,可以知道:
usbcore是由由drivers/usb/core/下面的usb.c hub.c hcd.c urb.c message.c driver.c config.c file.c buffer.c sysfs.c endpoint.c devio.c
notify.c generic.c quirks.c devices.c hcd-pci.c inode.c这些文件生成的。
编译一个配置脚本来配置dynamic printk,内容如下:
#!/bin/sh DYNAMIC_CONTROL=/sys/kernel/debug/dynamic_debug/control #usbcore
echo 'file drivers/usb/core/usb.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/hub.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/hcd.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/urb.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/message.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/driver.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/config.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/file.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/buffer.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/sysfs.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/endpoint.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/device.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/notify.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/generic.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/quirks.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/devices.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/hci-pci.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/usb/core/inode.c +flmpt' > $DYNAMIC_CONTROL #drivers/base files
echo 'file drivers/base/core.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/sys.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/bus.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/dd.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/syscore.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/driver.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/class.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/platform.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/cpu.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/firmware.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/init.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/map.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/devres.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/attribute_container.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/transport_class.c +flmpt' > $DYNAMIC_CONTROL
echo 'file drivers/base/topology.c +flmpt' > $DYNAMIC_CONTROL #usb-common
echo 'file drivers/usb/usb-common.c +flmpt' > $DYNAMIC_CONTROL #mii
echo 'file drivers/net/mii.c +flmpt' > $DYNAMIC_CONTROL #usbnet
echo 'file drivers/net/usbnet.c +flmpt' > $DYNAMIC_CONTROL #dm9601
echo 'file drivers/net/dm9601.c +flmpt' > $DYNAMIC_CONTROL
切换到root账户:
su
下面命令都是使用root账户执行:
挂载debugfs:
mount -t debugfs none /sys/kernel/debug
拔掉usb网卡,删除dm9601、usbnet和mii驱动(usbcore和usb_common无法删除,也应该删除):
# modprobe -r dm9601 usbnet mii
然后执行上面配置dynamic printk的脚本。
然后插入usb网卡,得到下面信息(使用dmesg):
[38737.680241] [] usbcore:usb_remote_wakeup:: usb usb1: usb wakeup-resume
[38737.680254] [] usbcore:hcd_bus_resume:: usb usb1: usb auto-resume
[38737.693461] [] usbcore:hub_resume:: hub -:1.0: hub_resume
[38737.693488] [] usbcore:hub_activate:: hub -:1.0: port : status change
[38737.693504] [] usbcore:hub_activate:: hub -:1.0: port : status change
[38737.797310] [] usbcore:hub_events:: hub -:1.0: state ports chg evt
[38737.797343] [] usbcore:hub_port_connect_change:: hub -:1.0: port , status , change , Mb/s
[38737.925150] [] usbcore:hub_port_debounce:: hub -:1.0: debounce: port : total 100ms stable 100ms status 0x101
[38738.036986] usb -: new full-speed USB device number using xhci_hcd
[38738.053846] [] usbcore:usb_get_langid:: usb -: default language 0x0409
[38738.054100] [] usbcore:usb_new_device:: usb -: udev , busnum , minor =
[38738.054108] usb -: New USB device found, idVendor=0fe6, idProduct=
[38738.054112] usb -: New USB device strings: Mfr=, Product=, SerialNumber=
[38738.054116] usb -: Product: USB 2.0 /100M Ethernet Adaptor
[38738.054125] [] core:device_add:: device: '1-2': device_add
[38738.054287] [] bus:bus_add_device:: bus: 'usb': add device -
[38738.054364] [] dd:driver_probe_device:: bus: 'usb': driver_probe_device: matched device - with driver usb
[38738.054375] [] dd:really_probe:: bus: 'usb': really_probe: probing driver usb with device -
[38738.054395] [] usbcore:usb_probe_device:: usb -: usb_probe_device
[38738.054407] [] usbcore:usb_choose_configuration:: usb -: configuration # chosen from choice
[38738.054669] [] usbcore:usb_set_configuration:: usb -: adding -:1.0 (config #, interface )
[38738.054684] [] core:device_add:: device: '1-2:1.0': device_add
[38738.054720] [] bus:bus_add_device:: bus: 'usb': add device -:1.0
[38738.054802] [] dd:driver_probe_device:: bus: 'usb': driver_probe_device: matched device -:1.0 with driver usbserial_generic
[38738.054814] [] dd:really_probe:: bus: 'usb': really_probe: probing driver usbserial_generic with device -:1.0
[38738.054837] [] usbcore:usb_probe_interface:: usbserial_generic -:1.0: usb_probe_interface
[38738.054851] [] usbcore:usb_probe_interface:: usbserial_generic -:1.0: usb_probe_interface - got id
[38738.054877] [] dd:really_probe:: usbserial_generic: probe of -:1.0 rejects match -
[38738.054916] [] core:device_add:: device: 'ep_81': device_add
[38738.054975] [] core:device_add:: device: 'ep_02': device_add
[38738.055022] [] core:device_add:: device: 'ep_83': device_add
[38738.055079] [] dd:driver_bound:: driver: '1-2': driver_bound: bound to device 'usb'
[38738.055088] [] dd:really_probe:: bus: 'usb': really_probe: bound device - to driver usb
[38738.055101] [] core:device_add:: device: 'ep_00': device_add
[38738.079351] [] bus:bus_add_driver:: bus: 'usb': add driver dm9601
[38738.079374] [] dd:driver_probe_device:: bus: 'usb': driver_probe_device: matched device -:1.0 with driver dm9601
[38738.079382] [] dd:really_probe:: bus: 'usb': really_probe: probing driver dm9601 with device -:1.0
[38738.079392] [] usbcore:usb_probe_interface:: dm9601 -:1.0: usb_probe_interface
[38738.079399] [] usbcore:usb_probe_interface:: dm9601 -:1.0: usb_probe_interface - got id
[38738.092443] [] core:device_add:: device: 'eth0': device_add
[38738.093442] dm9601 -:1.0: eth0: register 'dm9601' at usb-::14.0-, Davicom DM9601 USB Ethernet, :e0:4c:::
[38738.093482] [] dd:driver_bound:: driver: '1-2:1.0': driver_bound: bound to device 'dm9601'
[38738.093497] [] dd:really_probe:: bus: 'usb': really_probe: bound device -:1.0 to driver dm9601
[38738.093586] usbcore: registered new interface driver dm9601
[38738.101169] [] core:device_rename:: device: 'eth0': device_rename: renaming to 'eth1'
[38738.137651] udevd[]: renamed network interface eth0 to eth1
[38738.188557] dm9601 -:1.0: eth1: link up, 100Mbps, full-duplex, lpa 0xFFFF
[38748.657111] eth1: no IPv6 routers present
前面大部分输出信息跟使用usb转串口类似,都是使用usbcore检测设备信息以及设备驱动,最终检测到dm9601驱动
usb中有个remote wakeup功能,usb hcd(host controller driver)中默认remote wakeup功能就是调用hcd_resume_work实现,
该函数会调用usb_remote_wakeup,就产生了输出信息:
[38737.680241] [] usbcore:usb_remote_wakeup:: usb usb1: usb wakeup-resume
在usb子系统初始化之时,执行usb_init函数,该函数调用了usb_register_device_driver(&usb_generic_driver, THIS_MODULE).
默认情况下找到usb设备都是先调用usb_generic_driver的相关函数。此处执行resume功能就调用usb.generic_driver的resume功能,
即generic_resume函数。
在执行generic_resume时,调用了hcd_bus_resume函数,输出了下面信息:
[38737.680254] [] usbcore:hcd_bus_resume:: usb usb1: usb auto-resume
接着调用hub的resume功能,即hub_resume函数,输出下面信息:
[38737.693461] [] usbcore:hub_resume:: hub -:1.0: hub_resume
hub_resume中还执行hub_activate(hub,HUB_RESUME),在hub_activate中检查每一个port是否状态发生了改变,并输出信息,
产生下面两行输出:
[38737.693488] [] usbcore:hub_activate:: hub -:1.0: port : status change
[38737.693504] [] usbcore:hub_activate:: hub -:1.0: port : status change
在usb hub初始化时,创建了一个内核线程(名为khubd),线程执行函数是hub_thread.
在hub_thread函数中循环执行hub_events函数,hub_events中循环检测是否port状态发生了改变,并根据其改变状态执行功能。
hub_events函数中如果hub_event_list不空,那么就获取从hub_event_list获取对应的事件,根据该事件得到hub,再根据hub得到对应的hub_dev。
然后输出事件信息:
[38737.797310] [164] usbcore:hub_events:3592: hub 1-0:1.0: state 7 ports 4 chg 0004 evt
hub_events中检测到port连接状态发生了改变,会调用hub_port_connect_change函数,该函数开头输出信息:
[38737.797343] [] usbcore:hub_port_connect_change:: hub -:1.0: port , status , change , Mb/s
在hub_port_connect_change函数中执行hub_port_debounce函数,输出下面信息:
[38737.925150] [] usbcore:hub_port_debounce:: hub -:1.0: debounce: port : total 100ms stable 100ms status 0x101
接着执行hub_port_init函数,在hub_port_init中输出下面信息:
[38738.036986] usb -: new full-speed USB device number using xhci_hcd
接着执行usb_new_device函数,调用usb_enumerate_device函数,usb_enumerate_device调用usb_cache_string,
usb_cache_string调用usb_string,usb_string调用usb_get_langid,输出下面信息:
[38738.053846] [] usbcore:usb_get_langid:: usb -: default language 0x0409
之后usb_new_device输出下面信息:
[38738.054100] [] usbcore:usb_new_device:: usb -: udev , busnum , minor =
在usb_new_device中调用announce_device函数,输出下面信息:
[38738.054108] usb -: New USB device found, idVendor=0fe6, idProduct=
[38738.054112] usb -: New USB device strings: Mfr=, Product=, SerialNumber=
[38738.054116] usb -: Product: USB 2.0 /100M Ethernet Adaptor
usb_new_device接着调用device_add。
在device_add中输出信息:
[38738.054125] [] core:device_add:: device: '1-2': device_add
device_add中接着调用bus_add_device。
bus_add_device中输出信息:
[38738.054287] [] bus:bus_add_device:: bus: 'usb': add device -
device_add中接着调用bus_probe_device,bus_probe_device中执行device_attach.
在device_attach中对其bus上的每一个驱动都调用__device_attach函数。
在__device_attach中对调用driver_match_device来对驱动和设备来进行匹配,如果匹配成功,则调用driver_probe_device,否则直接返回0.
此处将设备和驱动匹配成功,所以执行driver_probe_device函数。
在执行driver_probe_device时输出下面信息:
[38738.054364] [] dd:driver_probe_device:: bus: 'usb': driver_probe_device: matched device - with driver usb
driver_probe_device继续执行really_probe函数,输出下面信息:
[38738.054375] [] dd:really_probe:: bus: 'usb': really_probe: probing driver usb with device -
driver_probe_device会执行其驱动的probe函数。之前在对usb subsys初始化时调用了usb_init,usb_init调用了
usb_register_device_driver(&usb_generic_driver, THIS_MODULE),然后usb_register_device_driver中设置了驱动的probe函数是usb_probe_device.
所以这里执行usb_probe_device函数,在此函数中输出下面信息:
[38738.054395] [] usbcore:usb_probe_device:: usb -: usb_probe_device
usb_probe_device还会执行usb_device_driver的probe函数,而usb_generic_driver定义时就将其probe设置成了generic_probe.
所以这里会调用generic_probe函数。在generic_probe中会调用usb_choose_configuration,从而输出下面信息:
[38738.054407] [] usbcore:usb_choose_configuration:: usb -: configuration # chosen from choice
generic_probe中接下来调用usb_set_configuration.
usb_set_configuration中对usb接口输出下面信息:
[38738.054669] [] usbcore:usb_set_configuration:: usb -: adding -:1.0 (config #, interface )
在usb_set_configuration中调用device_add函数添加该usb接口设备。
device_add中输出下面信息:
[38738.054684] [] core:device_add:: device: '1-2:1.0': device_add
device_add中调用bus_add_device,输出下面信息:
[38738.054720] [] bus:bus_add_device:: bus: 'usb': add device -:1.0
device_add接下来调用bus_probe_device,跟前面类似,仍然对总线上所有驱动和设备来进行匹配,如果匹配成功,
就执行这个驱动。此处匹配usbserial-generic驱动成功,执行driver_probe_device,输出下面信息:
[38738.054802] [] dd:driver_probe_device:: bus: 'usb': driver_probe_device: matched device -:1.0 with driver usbserial_generic
driver_probe_device中调用really_probe,在really_probe中输出信息:
[38738.054814] [] dd:really_probe:: bus: 'usb': really_probe: probing driver usbserial_generic with device -:1.0
跟前面类似,调用驱动的probe函数,即usb_probe_interface函数。
usb_probe_interface中输出下面信息:
[38738.054837] [] usbcore:usb_probe_interface:: usbserial_generic -:1.0: usb_probe_interface
[38738.054851] [] usbcore:usb_probe_interface:: usbserial_generic -:1.0: usb_probe_interface - got id
该函数执行失败,在回到really_probe函数后输出下面信息:
[38738.054877] [] dd:really_probe:: usbserial_generic: probe of -:1.0 rejects match -
接下来三行输出信息不知道是哪部分代码产生的:
[38738.054916] [] core:device_add:: device: 'ep_81': device_add
[38738.054975] [] core:device_add:: device: 'ep_02': device_add
[38738.055022] [] core:device_add:: device: 'ep_83': device_add
此时返回到上一个执行的really_probe,执行driver_bound,输出信息:
[38738.055079] [] dd:driver_bound:: driver: '1-2': driver_bound: bound to device 'usb'
然后在really_probe中输出下面信息:
[38738.055079] [] dd:driver_bound:: driver: '1-2': driver_bound: bound to device 'usb'
下面一行输出信息页不知道是那部分代码产生的(可能和当前代码执行流程无关?):
[38738.055101] [] core:device_add:: device: 'ep_00': device_add
接下来加载dm9601驱动,调用dm9601_init,dm9601_init调用usb_register(&dm9601_driver).
usb_register中调用bus_add_driver。
在bus_add_driver中输出下面信息:
[38738.079351] [] bus:bus_add_driver:: bus: 'usb': add driver dm9601
bus_add_driver中调用driver_attach,driver_attach中对于总线上每个设备执行__driver_attach.
__driver_attach中对设备和驱动进行匹配,如果匹配成功,则执行driver_probe_device.
此处匹配成功,执行driver_probe_device函数,输出下面信息:
[38738.079374] [] dd:driver_probe_device:: bus: 'usb': driver_probe_device: matched device -:1.0 with driver dm9601
driver_probe_device中执行really_probe函数,然后调用驱动的probe函数,即usb_probe_interface函数,
usb_probe_interface中输出下面信息:
[38738.079392] [] usbcore:usb_probe_interface:: dm9601 -:1.0: usb_probe_interface
[38738.079399] [] usbcore:usb_probe_interface:: dm9601 -:1.0: usb_probe_interface - got id
usb_probe_interface中调用driver->probe,dm9601_driver定义中其probe函数即usbnet_probe函数。
usbnet_probe函数中调用register_netdev,register_netdev调用register_netdevice,register_netdevice调用netdev_register_kobject,
netdev_register_kobject将设备名称设置成eth0,然后调用device_add,在device_add中输出下面信息:
[38738.092443] [] core:device_add:: device: 'eth0': device_add
usbnet_probe继续执行,输出下面信息:
[38738.093442] dm9601 -:1.0: eth0: register 'dm9601' at usb-::14.0-, Davicom DM9601 USB Ethernet, :e0:4c:::
回到really_probe继续执行,调用driver_bound,在driver_bound中输出下面信息:
[38738.093482] [] dd:driver_bound:: driver: '1-2:1.0': driver_bound: bound to device 'dm9601'
然后在really_probe中输出下面信息:
[38738.093497] [] dd:really_probe:: bus: 'usb': really_probe: bound device -:1.0 to driver dm9601
回到前面的usb_register_driver,输出下面信息:
[38738.093586] usbcore: registered new interface driver dm9601
我的系统中udev规则(/etc/udev/rules.d/70-persistent-net.rules)中有下面内容:
# USB device 0x:0x (dm9601)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:e0:4c:53:44:58", ATTR{dev_id}=="0x0", ATTR{type}=="", KERNEL== "eth*", NAME="eth1"
所以在添加usb网卡时会将eth0改成eth1,产生下面输出:
[38738.101169] [] core:device_rename:: device: 'eth0': device_rename: renaming to 'eth1'
[38738.137651] udevd[]: renamed network interface eth0 to eth1
dm9601驱动还会调用dm9601_link_reset,dm9601_link_reset调用mii_check_media,mii_check_media中产生下面输出:
[38738.188557] dm9601 -:1.0: eth1: link up, 100Mbps, full-duplex, lpa 0xFFFF
最后一行信息是在addr_rs_timer函数中输出的,该函数被配置成ipv6接口的定时器函数,过一段时间都会调用该函数。
到这里整个usb网卡检测过程就完成了,其他的网卡相关函数还需要看看书后再来分析。