第一个驱动程序(在Ubuntu系统下运行)

您所在的位置:网站首页 linux驱动程序运行在哪里 第一个驱动程序(在Ubuntu系统下运行)

第一个驱动程序(在Ubuntu系统下运行)

2023-05-03 03:38| 来源: 网络整理| 查看: 265

一、构造内核源码树

# apt-cache search linux-source # apt-get install linux-source-4.4.0(下载的源码在目录/usr/src下) # 解压内核源码tar xjf ..... 进入源码目录 # make oldconfig # make # make modules make modules_install

ref:http://blog.chinaunix.net/uid-24782829-id-3211008.html

由于我主机本身内核版本就为4.4.0-21-generic,所以/lib/modules/4.4.0-21-generic/本身就存在,所以上述过程就不需要执行了。至此,构造内核源码树完成。

make时报错:

scripts/sign-file.c:23:30: fatal error: openssl/opensslv.h: 没有那个文件或目录 compilation terminated. scripts/Makefile.host:91: recipe for target 'scripts/sign-file' failed make[1]: *** [scripts/sign-file] Error 1 Makefile:566: recipe for target 'scripts

解决方法:ubuntu下缺少了如下的组件,安装一下即可

sudo apt-get install libssl-dev

二、在Linux下写驱动程序

源代码firstdrv.c:

#include #include #include #include #include static int first_drv_open(struct inode *inode, struct file *file) {printk("first_drv_open\n");return 0; }static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) {printk("first_drv_write\n");return 0; }static struct file_operations first_drv_fops = {.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */.open = first_drv_open, .write = first_drv_write, };static int first_drv_init(void) {register_chrdev(246, "first_drv", &first_drv_fops); // 注册, 告诉内核return 0; }static void first_drv_exit(void) {unregister_chrdev(246, "first_drv"); // 卸载 }module_init(first_drv_init); module_exit(first_drv_exit);MODULE_LICENSE("GPL");

makefile:

obj-m:=firstdrv.o #注意.o文件名要和驱动程序文件名一致 CURRENT_PATH :=$(shell pwd) LINUX_PATH :=/lib/modules/4.4.0-21-generic/buildall: make -C $(LINUX_PATH) M=$(CURRENT_PATH) modules clean: make -C $(LINUX_PATH) M=$(CURRENT_PATH) clean # make 编译模块 # insmod firstdrv.ko #lsmod #cat /proc/devices

测试程序firstdrvtest.c:

#include #include #include #include int main() {int fd;int val=1;fd=open("/dev/xyz",O_RDWR);if(fd


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3