linux 查看驱动模块(lsmod)和 驱动设备(ls /dev)
lsmod 命令,用于列出当前 linux 系统中加载的模块。当驱动开发人员编写好驱动代码,并生成驱动代码对应的驱动模块后,可以通过 insmod xxx.ko 将驱动模块(.ko)加载到 linux 操作系统中。最后,通过 lsmod 命令就可以看到 xxx.ko 已经加载到 linux 系统当中了。
一、lsmod
lsmod 命令,用于列出当前 linux 系统中加载的模块。当驱动开发人员编写好驱动代码,并生成驱动代码对应的驱动模块后,可以通过 insmod xxx.ko 将驱动模块(.ko)加载到 linux 操作系统中。最后,通过 lsmod 命令就可以看到 xxx.ko 已经加载到 linux 系统当中了。
驱动代码:
back@ubuntu2205:~$ cat driver.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
int hello_probe(struct platform_device *pdev)
{
printk("[%s] match ok\n", __FILE__);
return 0;
}
int hello_remove(struct platform_device *pdev)
{
printk("[%s] hello_remove\n", __FILE__);
return 0 ;
}
struct platform_driver hello_driver = {
.probe = hello_probe,
.remove = hello_remove,
.driver.name = "yikoulinux",
};
static int hello_init(void)
{
printk("[%s] hello_init\n", __FILE__);
return platform_driver_register(&hello_driver);
}
static void hello_exit(void)
{
printk("[%s] hello_exit\n", __FILE__);
platform_driver_unregister(&hello_driver);
return;
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
back@ubuntu2205:~$ cat device.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
void hello_release(struct device *dev)
{
printk("[%s] hello_release\n",__FILE__);
return;
}
struct platform_device hello_device ={
.name = "yikoulinux",
.id = -1,
.dev.release = hello_release,
//hardware TBD
};
static int hello_init(void)
{
printk("[%s] hello_init\n", __FILE__);
return platform_device_register(&hello_device);
}
static void hello_exit(void)
{
printk("[%s] hello_exit\n", __FILE__);
platform_device_unregister(&hello_device);
return;
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
back@ubuntu2205:~$ ls -lh
total 1.1M
-rw-r--r-- 1 back back 709 8月 3 12:13 device.c
-rw-rw-r-- 1 back back 5.6K 8月 3 12:18 device.ko
-rw-r--r-- 1 back back 829 8月 3 12:13 driver.c
-rw-rw-r-- 1 back back 5.3K 8月 3 12:18 driver.ko
back@ubuntu2205:~$
可以看到,我们通过 insmod 命令将 1.1 中的 device.ko 和 driver.ko加载到系统后,lsmod 就会发现我们 insmod 加载的 .ko 模块。
二、其他常见信息的查看
查看CPU信息: cat /proc/cpuinfo
查看板卡信息:cat /proc/pci
查看PCI信息: lspci
例子: lspci |grep Ethernet 查看网卡型号
查看内存信息:cat /proc/meminfo
查看USB设备: cat /proc/bus/usb/devices
查看键盘和鼠标:cat /proc/bus/input/devices
查看系统硬盘信息和使用情况:fdisk & disk - l & df
查看各设备的中断请求(IRQ): cat /proc/interrupts
查看系统体系结构:uname -a
dmidecode查看硬件信息,包括bios、cpu、内存等信息
dmesg | more 查看硬件信息
三、ls /dev
ls /dev 命令用于查看系统中的驱动设备,包括字符设备、块设备。一个驱动模块可以注册多个设备文件。
如字符设备 i2c-0 ~ i2c-4,这5个都是字符设备,它们的主设备号都是 89,此设备号为 0 ~ 4。
同理,块设备 loop0 ~ loop7,它们的主设备号都是 7,此设备号为 0 ~ 7。

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)