前言
相信经过hello程序的编译,大家对内核模块已经有所了解。下面要介绍的是如何实现一个简单的字符设备。
首先在用户目录下创建文件夹scull,其中包含三个文件:scull.c、Makefile、build
scull.c
#include
#include
#include /* everything... */
#include
#include /* copy_*_user */
MODULE_LICENSE("GPL");
int scull_major = 0;
int scull_minor = 0;
struct cdev cdev; /* Char device structure */
#define MAX_SIZE 10
size_t size = 0;
char store[MAX_SIZE];
/*
* Open and close
*/
int scull_open(struct inode *inode , struct file *filp)
{
/* trim to 0 the length of the device if open was write -only
*/
if ( (filp ->f_flags & O_ACCMODE) == O_WRONLY) {
size = 0;
}
return 0; /* success */
}
int scull_release(struct inode *inode , struct file *filp)
{
return 0;
}
/*
* Data management: read and write
*/
ssize_t scull_read(struct file *filp , char __user *buf, size_t count, loff_t *f_pos)
{
ssize_t retval = 0;
if (*f_pos >= size)
goto out;
if (*f_pos + count > size)
count = size - *f_pos;
if (copy_to_user(buf, store + *f_pos , count)) {
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
out:
return retval;
}
ssize_t scull_write(struct file *filp , const char __user *buf,size_t count ,loff_t *f_pos)
{
ssize_t retval = -ENOMEM; /* value used in "goto out"
statements */
if (*f_pos >= MAX_SIZE)
goto out;
if (*f_pos + count > MAX_SIZE)
count = MAX_SIZE - *f_pos;
if (copy_from_user(store + *f_pos , buf, count)) {
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
/* update the size */
if (size < *f_pos)
size = *f_pos;
out:
return retval;
}
struct file_operations scull_fops = {
.owner = THIS_MODULE ,
.read = scull_read ,
.write = scull_write ,
.open = scull_open ,
.release = scull_release ,
};
int scull_init_module(void)
{
int result;
dev_t dev = 0;//在linux中是unsigned int 类型,32位,用于在驱动程序中定义设备编号,高12位为主设备号,低20位为次设备号
/*
* Get a range of minor numbers to work with , asking for a
dynamic major
*/
result = alloc_chrdev_region(&dev, scull_minor , 1, "scull");//动态申请设备号,设备名称"scull",设备个数1,次设备号scull_minor,申请到的设备号存储在dev中。该函数返回值小于0表示申请失败。
scull_major = MAJOR(dev);
if (result < 0) {
printk(KERN_WARNING "scull:?can't?get?major?%d\n",
scull_major);
return result;
}
/* register our device */
cdev_init(&cdev , &scull_fops);//用上面声明的scull_fops初始化cdev。
cdev.owner = THIS_MODULE;
cdev.ops = &scull_fops;
result = cdev_add (&cdev , dev, 1);//这个是在字符设备中添加一个设备。
if (result) {
printk(KERN_WARNING "Error?%d?adding?scull", result)
;
unregister_chrdev_region(dev, 1);
return result;
}
return 0; /* succeed */
}
void scull_cleanup_module(void)
{
/* cleanup_module is never called if registering failed */
dev_t dev;
cdev_del(&cdev);
dev = MKDEV(scull_major , scull_minor);
unregister_chrdev_region(dev, 1);
}
module_init(scull_init_module);
module_exit(scull_cleanup_module);
cdev结构的说明
struct cdev {
struct kobject kobj;
struct module *owner;//所属模块
const struct file_operations *ops;//文件操作结构
struct list_head list;
dev_t dev; //设备号,int 类型,高12位为主设备号,低20位为次设备号
unsigned int count;
};
cdev_add (&cdev , dev, 1)
这个是在字符设备中添加一个设备。具体实现中的kobj_map有些复杂,感兴趣的可以参考steven_miao的ChinaUnix博客或者搜索相关问题深入研究。
read()与write()函数。
scull_read/scull_write(struct file *filp , char __user *buf, size_t count,loff_t *f_pos)
filp文件类型,一般在open函数中都会使filp指向本模块的文件结构,但是本文采用全局变量实现,没有用到filp。buf是要读到的地方,count是要读取的个数,f_ops是偏移量。
copy_from_user和copy_to_user的函数原型都是strncpy,因为涉及到内核与用户权限问题对其进行封装。
build
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
Makefile
obj-m := scull.o
编译装载模块
编译:打开命令行,执行以下命令:
cd scull
sh build
安装:
sudo insmod scull.ko //装载模块scull.ko
查看注册设备:
cat /proc/devices | grep scull
如果有类似下面的信息(数字可能不一样),则说明一切顺利。否则说明模块初始化有错误,可查看/var/log/kern.log 了解具体情况。
250 scull
建立设备文件
sudo mknod /dev/scull c 250 0//按照查看的设备号更改250
测试
sudo su
echo 123 > /dev/scull
cat /dev/scull
如果看到输出123证明设备正常工作。
设备移除
sudo rm /dev/scull
sudo rmmod scull
希望大家认真对待这次的字符设备,阅读相关资料,了解每个函数的功能,参数的作用,这会在以后的实验中多次用到。