1.1第一种字符驱动模板:
#include <linux/init.h>
#include <linux/kdev_t.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
MODULE_LICENSE ("GPL");
struct cdev myuart;
struct file_operations var;
#define CHRDEV_MAJOR 201
#define CHRDEV_MINOR 2
#define CHRDEV_NAME "chrdev"
dev_t dev_id;
static int chrdev_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "chrdev_open run");
return 0;
}
static int chrdev_close(struct inode *inode, struct file *file)
{
printk(KERN_INFO "chrdev_close run");
return 0;
}
static int __init hello_init (void)
{
int ret = 0;
int ret1 = 0;
dev_id = MKDEV(CHRDEV_MAJOR, CHRDEV_MINOR);
ret = register_chrdev_region(dev_id, 1 , CHRDEV_NAME);
if(ret == -1)
{
printk(KERN_INFO "dev_id = %d can't use \n",CHRDEV_MAJOR);
ret1 = alloc_chrdev_region(&dev_id, CHRDEV_MINOR, 1, CHRDEV_NAME);
if(ret1 == -1)
{
printk(KERN_INFO "dev_id error \n");
return -1;
}
else
{
printk(KERN_INFO "major = %d \n", MAJOR(dev_id));
}
}
else
{
printk(KERN_INFO "major = %d can use \n",CHRDEV_MAJOR);
}
var.open = chrdev_open;
var.release = chrdev_close;
cdev_init(&myuart,&var);
cdev_add(&myuart,dev_id,1);
return 0;
}
static void __exit hello_exit (void)
{
unregister_chrdev_region(dev_id, 1);
cdev_del(&myuart);
}
module_init (hello_init);
module_exit (hello_exit);
1.2第二种字符驱动模板
#include <linux/init.h>
#include <linux/kdev_t.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
MODULE_LICENSE ("GPL");
#define CHRDEV_MAJOR 201
#define CHRDEV_MINOR 2
#define CHRDEV_NAME "chrdev"
struct cdev mycdev;
dev_t dev_id;
static int chrdev_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "chrdev_open run");
return 0;
}
static int chrdev_close(struct inode *inode, struct file *file)
{
printk(KERN_INFO "chrdev_close run");
return 0;
}
static struct file_operations ops =
{
.owner = THIS_MODULE,
.open = chrdev_open,
.release = chrdev_close,
};
static int __init hello_init (void)
{
int ret = 0;
int ret1 = 0;
mycdev.owner = THIS_MODULE;
cdev_init(&mycdev,&ops);
dev_id = MKDEV(CHRDEV_MAJOR, CHRDEV_MINOR);
ret = register_chrdev_region(dev_id, 1 , CHRDEV_NAME);
if(ret == -1)
{
printk(KERN_INFO "dev_id = %d can't use \n",CHRDEV_MAJOR);
ret1 = alloc_chrdev_region(&dev_id, CHRDEV_MINOR, 1, CHRDEV_NAME);
if(ret1 == -1)
{
printk(KERN_INFO "dev_id error \n");
return -1;
}
else
{
printk(KERN_INFO "major = %d \n", MAJOR(dev_id));
}
}
else
{
printk(KERN_INFO "major = %d can use \n",CHRDEV_MAJOR);
cdev_add(&mycdev, dev_id, 1);
}
return 0;
}
static void __exit hello_exit (void)
{
unregister_chrdev_region(dev_id, 1);
cdev_del(&mycdev);
}
module_init (hello_init);
module_exit (hello_exit);
2.测试文件
#include "stdio.h"
#include "fcntl.h"
#include "stdlib.h"
#include "unistd.h"
int main()
{
int fd;
loop:
fd = open("./dev",O_RDWR,0777);
if(fd == -1)
{
printf("open error\n");
return -1;
}
printf("open success\n");
sleep(5);
close(fd);
printf("close success\n");
sleep(5);
goto loop;
return 0;
}
3.makefile文件
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /home/lht/kernel2.6/linux-2.6.14
KERNELDIR ?= /lib/modules/$(shell uname -r)/build M=$(PWD) modules
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* module*
.PHONY: modules modules_install clean
else
obj-m := hello.o
endif
1.先执行一下make,可以在设置设备号之前先用cat /proc/devices查看已经被使用的设备号
2.使用insmod hello.ko将驱动程序加载到内核中
3.使用mknod ./dev c 201 2创建一个设备文件
3.执行测试文件并使用demeg查看内核输出的结果验证是否正确
4.注意切换到root用户下进行
4.如果显示文件打开失败注意查看设备号是否匹配
4.函数详解:
1. 驱动程序相关的shell命令:
insmod *.ko 将驱动程序加载到内核中
rmmod *.ko 将驱动程序移除出内核
lsmod 查看内核中的模块信息
dmesg 查看内核的打印信息(printk打印的信息)
2.驱动程序框架函数:
module_init() 驱动模块加载函数,insmod 命令会调用驱动程序的module_init
module_exit() 驱动模块卸载函数,rmmod 命令会调用驱动程序的module_exit
3.管理字符设备的结构体:
struct cdev
{
struct kobject kobj;
struct module *owner; //表示驱动设备的属主
const struct file_operations *ops; //包含字符设备的主体代码,含有很多的函数指针
struct list_head list;
dev_t dev; //字符设备的设备号
unsigned int count;
};
4.struct file_operations 结构体的主要内容:
如果报错尝试在*后面添加一个变量名
struct file_operations
{
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long,
unsigned long, unsigned long);
int (*check_flags)(int); int (*dir_notify)(struct file *filp, unsigned long arg);
}
5.设备号相关函数:
(1) MKDEV:将一个主设备号和一个次设备号合并成一个32数,MKDEV(major,minor);
(2) MAJOR:将一个32 位的数提取高12 位,获得主设备号,MAJOR(dev_id);
(3) MINOR:将一个32 位的数提取低20 位,获得次设备号,MINOR(dev_id);
(4) int register_chrdev_region(dev_t dev, unsigned int count, char *name)。
功能:向内核注册设备号。
参数:dev 为要检测的设备号;count 为要检测的设备数量;name 为要检测的设备名称。
返回值:成功返回0,出错返回-1。
(5) unregister_chrdev_region (dev_t dev, unsigned int number_of_devices)。
功能:向内核注销设备号。
参数:dev 为要注销的设备号;number_of_devices 为要注销的设备数量。
返回值:成功返回0,出错返回-1。
(5) alloc_chrdev_region (dev_t *dev, unsigned int minor, unsigned intnumber_of_devices, char*name)。
功能:向内核申请动态分配设备号。
参数:dev 接收内核动态分配设备号指针;minor 为指定分配设备的次设备号值;number_of_devices 为设备数量;name 为要检测的设备名称。
返回值:成功返回0,出错返回-1。
6.cdev结构体操作函数:
(1) void cdev_init(struct cdev *cdev, const struct file_operations *op)
功能:初始化cdev 结构体成员ops,等价于:mycdev.ops=myfile_operation。
参数:cdev 为要加载的cdev 结构体对象指针;op 为struct file_operations 对象指针。
(2) struct cdev *cdev_alloc(void)
功能:动态分配空间,在定义struct cdev 指针对象时使用,相当于应用程序空间的malloc函数。
返回值:成功返回cdev 结构体对象指针,出错返回NULL。
(3) int cdev_add(struct cdev *cdev, dev_t dev, unsigned int number_of_devices)。
功能:初始化cdev 结构体成员:dev,同时将struct cdev 对象加载到内核中。
参数:cdev 为要加载的cdev 结构体对象指针;dev 为设备号;number_of_devices 为设备的数量
返回值:成功返回0,出错返回-1。
(4) void cdev_del(struct cdev *cdev)。
功能:将struct cdev 对象从内核中卸载。
参数:cdev 为要加载的cdev 结构体对象指针。