linux驱动系列学习之helloworld驱动(一)


前言

这个系列文章,是我个人linux驱动学习之余的记录,以免后面忘记。


一、Linux驱动

linux系统今天已经运行在数十亿设备上面,兼容30多个体系。面对数不尽的驱动,linux抽象成字符设备、块设备、网络设备。
字符设备可以将设备以字符流的形式,进行读写,大多数硬件设备均可以抽象成字符设备,如常见的串口、spi、i2c、led、按键等等,这个也是开发中主要遇到的,块设备相比较而言会难很多,块设备按照块为单位进行读写,需要相关调度算法调度设备进行读写。网络设备则和网络通信相关。
本文主要接受最简单的字符驱动—hello world驱动。

二、hello world驱动

1.字符驱动构成

一个字符设备构成主要由以下部分:

static int hello_init(void)//模块加载时调用的函数
static void hello_exit(void)//模块卸载时调用的函数
module_init(hello_init);       //告诉内核这是一个模块的入口函数
module_exit(hello_exit);       //出口函数 
MODULE_LICENSE("GPL");         //模块使用的协议
MODULE_AUTHOR("xxx");          //模块的作者xxx

其中,hello_init、hello_exit、module_init(hello_init)module_exit(hello_exit)是必须要有的。

2.Makefile

内容如下:

KERNELDIR := /home/xxx/imx6ull/linux_source
CURRENT_PATH := $(shell pwd)
obj-m := hello.o
build: kernel_modules
kernel_modules:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean

其中,KERNELDIR指内核的路径, obj-m := hello.o表示模块编译出来的名字为hello.o。
编译是直接make即可得到hello.ko文件,使用insmod hello.ko即可加载到内核中,使用lsmod,可以看到内核中加载了hello驱动模块。
该处使用的url网络请求的数据。


3.文件操作接口

static struct fb_ops结构体如下:

static const struct file_operations hello_fops = {
	.open = hello_open,
	.read =hello_read,
	.write = hello_write,
	.unlocked_ioctl = hello_unlocked_ioctl,
	.release = hello_release,
};
static int hello_open (struct inode *node, struct file *filp);
static ssize_t hello_read(struct file *filp, char __user * buf, size_t size, loff_t * off);
static ssize_t hello_write (struct file *filp, const char __user *buf, size_t size, loff_t * off)
static long hello_unlocked_ioctl(struct file * filp, unsigned int cmd, unsigned long arg);
static int hello_release(struct inode *node, struct file *size) ;

hello_fops 一般填充由open、read、write、ioctl、release函数,其他的如poll用到再去学习。

总结

linux系统已经对驱动设备进行了必要抽象,并提供相应的API函数,写linux驱动就是填充相应的结构体,调用相应的API函数告诉内核,并在相应的函数里面完成相应的功能。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你好!要编写一个简单的Linux驱动程序来打印"Hello World",可以按照以下步骤进行操作: 1. 创建一个文件夹来存放驱动程序的源代码。可以使用以下命令创建: ``` mkdir helloworld_driver cd helloworld_driver ``` 2. 创建一个名为`helloworld.c`的C语言源代码文件,并使用文本编辑器打开它: ``` touch helloworld.c nano helloworld.c ``` 3. 在打开的文件中,输入以下驱动程序的基本结构: ```c #include <linux/module.h> #include <linux/kernel.h> static int __init hello_init(void) { printk(KERN_INFO "Hello World!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye World!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple hello world driver"); ``` 上述代码包含了两个函数:`hello_init()`和`hello_exit()`。`hello_init()`函数在加载驱动程序时调用,用于打印"Hello World"。`hello_exit()`函数在卸载驱动程序时调用,用于打印"Goodbye World"。 4. 保存并关闭文件。 5. 创建一个名为`Makefile`的文件,并使用文本编辑器打开它: ``` touch Makefile nano Makefile ``` 6. 在打开的文件中,输入以下内容: ``` obj-m += helloworld.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ``` 7. 保存并关闭文件。 8. 打开终端,进入到存放驱动程序源代码的文件夹。 9. 使用以下命令编译和构建驱动程序: ``` make ``` 10. 如果一切顺利,没有错误发生,将会生成一个名为`helloworld.ko`的驱动程序模块文件。 11. 使用以下命令加载驱动程序: ``` sudo insmod helloworld.ko ``` 这将会在终端上打印"Hello World"。 12. 使用以下命令卸载驱动程序: ``` sudo rmmod helloworld ``` 这将会在终端上打印"Goodbye World"。 这就是一个简单的Linux驱动程序的"helloworld"编写过程。请注意,编写和加载驱动程序可能需要管理员权限。如果有任何问题,请随时向我提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫川宁520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值