#include
#include
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int count = 10;
static char *init_mesg = "hello,world\n";
static char *exit_mesg = "goodbye\n";
static int major = 252;
static int minor = 0;
dev_t devnum;
int static hello_dev_open(struct inode *inode, struct file *file)
{
printk("file open in hello_dev_open......finished!\n");
return 0;
}
int static hello_dev_release(struct inode *inode, struct file *file)
{
printk("file release in hello_dev_release......finished!\n");
return 0;
}
ssize_t hello_dev_read(struct file *file, char __user *buf,size_t count, loff_t *offset)
{
char alpha[27];
int i, cnt;
memset(alpha, 0, 27);
for(i = 0; i < 26; i++)
alpha[i] = 'a' + i;
if(count > 26)
cnt = 26;
else
cnt = count;
//使用copy_to_user ()函数从driver读数据到user
if(!copy_to_user((char *)buf, alpha, cnt))
return cnt;
else
return -1;
}
ssize_t hello_dev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
{
char alpha[27];
int cnt;
memset(alpha, 0, 27);
if(count > 26)
cnt = 26;
else
cnt = count;
//使用copy_from_user()函数从user写数据到driver
if(!copy_from_user((char *)alpha, buf, cnt))
{
printk(alpha);
printk("\n");
return cnt;
}
else
return -1;
}
static struct cdev hello_dev;
static struct file_operations fops ={
.owner = THIS_MODULE,
.open = hello_dev_open,
.release = hello_dev_release,
.read = hello_dev_read,
.write = hello_dev_write,
};
static int __init hello_init(void)
{
int i;
int ret;
for(i = 0; i < count; i++)
printk(init_mesg);
//ret = register_chrdev_region(MKDEV(major,minor), 1, "hello_dev");
ret = alloc_chrdev_region(&devnum, 10, 1, "hello_dev");
if(!ret)
{
major = MAJOR(devnum);
minor = MINOR(devnum);
printk("major = %d; minor = %d\n", major, minor);
}
cdev_init(&hello_dev, &fops);
ret = cdev_add(&hello_dev, devnum, 1);
return ret;
}
void hello_exit(void)
{
printk(exit_mesg);
cdev_del(&hello_dev);
unregister_chrdev_region(MKDEV(major, minor),1);
return;
}
void hello(void)
{
printk("good mornig1\n");
}
module_param(count, int, S_IRUGO);
module_param(init_mesg, charp, S_IRUGO);
module_param(exit_mesg, charp, S_IRUGO);
module_init(hello_init);
module_exit(hello_exit);
测试程序:
main.c:
#include
#include
#include
#include
#include
#define DEVNAME "/dev/hello"
int main()
{
char alpha[27];
int fd,i;
memset(alpha, 0, 27);
for(i = 0; i < 26; i++)
alpha[i] = 'A' + i;
fd = open(DEVNAME, O_RDWR);
if(fd == -1)
("file %s is opening......failure!", DEVNAME);
else
printf("file %s is opening......successfully!\nits fd is %d\n", DEVNAME, fd);
getchar();
printf("write A-Z to kernel......\n");
write(fd, alpha, 26);
getchar();
printf("read datas from kernel.......\n");
read(fd, alpha, 26);
printf("%s\n", alpha);
getchar();
close(fd);
return 0;
}
file /dev/hello is opening……successfully!
Its fd is 3
(输入回车)
write A-Z to kernel……
(输入回车)
read datas from kernel……
abcdefghijklmnopqrstuvwxyz
(输入回车)
输入dmesg命令,结果如下:
file open in hello_dev_open……finished!
ABCDEFGHIJKLMNOPQRSTUVWXYZ
file release in hello_dev_open……finished!