代码如下:
chardev.c
#include<linux/module.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
MODULE_LICENSE("GPL");
#define MAJOR_NUM 111
static ssize_t chardev_read(struct file *,char *,size_t,loff_t *);
static ssize_t chardev_write(struct file *,const char *,size_t, loff_t *);
struct file_operations chardev_fops=
{
read: chardev_read,
write: chardev_write,
};
static int chardev_var = 0;
static int chardev_init(void)
{
int ret;
ret=register_chrdev(MAJOR_NUM,"chardev",&chardev_fops);
if(ret)
{
printk("chardev register failure");
}
else
{
printk("chardev register success");
}
return ret;
}
static void chardev_exit(void)
{
unregister_chrdev(MAJOR_NUM,"chardev");
}
static ssize_t chardev_read(struct file *filp,char *buf,size_t len,loff_t *off)
{
if(copy_to_user(buf,&chardev_var,sizeof(int)))
{
return -EFAULT;
}
return sizeof(int);
}
static ssize_t chardev_write(struct file *filp,const char *buf,size_t len,loff_t *off)
{
if(copy_from_user(&chardev_var,buf,sizeof(int)))
{
return -EFAULT;
}
return sizeof(int);
}
module_init(chardev_init);
module_exit(chardev_exit);
Makefile 代码如下:
KERNELDIR?= /lib/modules/2.6.25-14.fc9.i686/build/
PWD := $(shell pwd)
CC=$(CROSS_COMPILE)gcc
obj-m := chardev.o
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
make编译
在/dev 目录下建立相应的设备节点,方法参照前两篇博客。
测试程序代码:
chardev_test.c
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main()
{
int fd,num;
fd=open("/dev/chardev",O_RDWR,S_IRUSR|S_IWUSR);
if(fd!=-1)
{
read(fd,&num,sizeof(int));
printf("The chardev is %d\n",num);
printf("Please input the num written to chardev\n");
scanf("%d",&num);
write(fd,&num,sizeof(int));
read(fd,&num,sizeof(int));
printf("The chardev is %d\n",num);
close(fd);
}
else
{
printf("Device open failure\n");
}
}
运行测试程序,会在终端里让你输入一个字符,同时返回一个你输入的字符。
完成应用程序与驱动程序的数据交互。
但是这个不同于上一个是直接进行的数据传递。这个示例用到了两个关键的函数
copy_from_user
.函数原型在[arch/i386/lib/usercopy.c]中
这个是完成从用户空间拷贝数据到内核空间,完成应用程序到驱动程序的数据传递
unsigned long
copy_from_user(void *to, const void __user *from, unsigned long n)
{
might_sleep();
if (access_ok(VERIFY_READ, from, n))
n = __copy_from_user(to, from, n);
else
memset(to, 0, n);
return n;
}
详见参考:
http://linux.chinaunix.net/techdoc/develop/2007/10/28/970816.shtml
copy_to_user
这个函数是完成从内核空间到用户空间拷贝数据的作用,完成从驱动程序到应用程序的传递
函数原型为
835 /**
836 * copy_to_user: - Copy a block of data into user space.
837 * @to: Destination address, in user space.
838 * @from: Source address, in kernel space.
839 * @n: Number of bytes to copy.
840 *
841 * Context: User context only. This function may sleep.
842 *
843 * Copy data from kernel space to user space.
844 *
845 * Returns number of bytes that could not be copied.
846 * On success, this will be zero.
847 */
848 unsigned long
849 copy_to_user(void __user *to, const void *from, unsigned long n)
850 {
851 if (access_ok(VERIFY_WRITE, to, n))
852 n = __copy_to_user(to, from, n);
853 return n;
854 }
详见参考
http://blog.chinaunix.net/space.php?uid=21289517&do=blog&id=1828184