1. mydm1.c:
<span style="font-size:14px;">#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/uaccess.h>
MODULE_LICENSE("Dual BSD/GPL");
int devMajor = 224; // 主设备号用于内核把文件和它的驱动链接在一起
static unsigned char simple_inc = 0;
static unsigned char demoBuffer[256]; // 用于存储该驱动的数据
int simple_open( struct inode *inode, struct file *filp )
{
printk(" : open Success!\n");
if(simple_inc>0)
{
return -1;
}
simple_inc = simple_inc + 1;
return 0;
}
int simple_release(struct inode *inode, struct file *filp)
{
printk(": release Success%d!\n",devMajor);
simple_inc = simple_inc - 1;
return 0;
}
ssize_t simple_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
printk(": read Success%d!\n", devMajor);
/*把数据复制到应用程序空间
从内核空间demoBuffer中拷贝数据到用户空间buf中*/
if( copy_to_user(buf,demoBuffer,count) )
{
count=-EFAULT;
}
return count;
}
ssize_t simple_write(struct file *filp, const char __user *buf,size_t count,loff_t *f_pos)
{
printk(": write Success%d!\n",devMajor);
/*把数据复制到内核空间*/
if(copy_from_user(demoBuffer + *f_pos,buf,count))
{
count=-EFAULT;
}
return count;
}
struct file_operations simple_fops={
.owner=THIS_MODULE,
.read=simple_read,
.write=simple_write,
.open=simple_open,
.release=simple_release,
};
/*******************************************************
MODULEROUTINE
*******************************************************/
static void __exit simple_cleanup_module(void)
{
unregister_chrdev(devMajor,"mydm1");
printk(" : simple_cleanup_module!\n");
//return 0;
}
static int __init simple_init_module(void)
{
int ret;
//根据设备号与设备名注册字符设备,此处设备名不一定要与模块名相同,
/*register_chrdev函数用于在内核空间,把驱动
和/dev下设备文件链接在一起*/
ret=register_chrdev( devMajor,"mydm1",&simple_fops);
if(ret<0)
{
printk(" : Unabletoregistercharacterdevice%d!\n",devMajor);
return ret;
}
else
{
printk(" : Success%d!\n",devMajor );
}
return 0;
}
module_init(simple_init_module);
module_exit(simple_cleanup_module);</span>
2. Makefile:
<span style="font-size:14px;">obj-m := mydm1.o
#KERNELDIR := /lib/modules/3.13.0-32-generic/build
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm *.o *.ko *.mod.c *.order *.symvers</span>
<span style="font-size:14px;">#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
int fd;
int i;
char data[256];
int retval;
fd = open("/dev/fgj",O_RDWR);
if(fd==-1)
{
perror("erroropen\n");
exit(-1);
}
printf("open/dev/fgjsuccessfully\n");
//写数据
retval = write(fd,"fgj",3);
if(retval==-1)
{
perror("writeerror\n");
exit(-1);
}
printf("write successfully\n");
//读数据
retval=read(fd,data,3);
if(retval==-1)
{
perror("readerror\n");
exit(-1);
}
data[retval]=0;
printf("readsuccessfully:%s\n",data);
//关闭设备
close(fd);
}
</span>
4. 运行:
1. 到包含Makefile和mydm1.c的目录下执行make,生成mydm1.ko;
2. 执行sudo insmod mydm1.ko;
3. 验证:lsmod | grep mydm1
4. 需要创建一个文件(该设备文件用于和设备驱动操作)
mknod /dev/fgj c 224 0 c代表字符设备 224为主设备号,0为从设备号
5. gcc test.c,然后执行:sudo ./a.out 输出如下:
open/dev/fgjsuccessfully
write successfully
readsuccessfully:fgj
用户函数和内核函数对应关系:
注意:
1. 保证/dev/fgj有读写权限 chmod 666 /dev/fgj;
2. printk打印的消息若灭有在控制台上显示,可以同构dmesg命令或查看系统的日志文件cat /var/log/syslog命令看到;
3. 驱动编译:gcc必须用系统自带的版本,gcc -v查看当前版本, 否则insmod后,lsmod会没反应,系统直接卡死,注销也不行,只能重启;
ref:
http://read.pudn.com/downloads119/ebook/506573/Linuxdevicedriver.pdf