Chinaunix首页 | 论坛 | 博客
  • 博客访问: 526327
  • 博文数量: 68
  • 博客积分: 2501
  • 博客等级: 大尉
  • 技术积分: 713
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-07 17:49
个人简介

文章分类

全部博文(68)

分类: LINUX

2010-05-20 11:58:19

#include
#include
#include

#include
#include
#include
#include

#include
#include

#define MINOR_DEV_NAME "minordev"
#define MINOR_DEV_MAJOR 240
#define MINOR_WRITE_ADDR 0x0378
#define MINOR_READ_ADDR 0x0379

int minor0_open(struct inode *inode,struct file *file)
{
printk("call minor0 open!\n");
return 0;
}

ssize_t minor0_write(struct file *file,const char *buf,size_t count,loff_t *f_ops)
{
unsigned char status;
int loop;
printk("device file-write operation!\n");
for (loop = 0;loop get_user(status,(char *) buf);
outb(status,MINOR_WRITE_ADDR);
}
return count;
}

int minor0_close(struct inode *inode,struct file *file)
{
printk("call minor0 close\n");
return 0;
}

int minor1_open(struct inode *inode,struct file *file)
{
printk("call minor1 open!\n");
return 0;
}

ssize_t minor1_read(struct file *file,char *buf,size_t count,loff_t *f_ops)
{
unsigned char status;
int loop;
printk("device file-read operation!\n");
for (loop = 0;loop status = inb(MINOR_READ_ADDR);
put_user(status,(char *) &buf[loop]);
}
return count;
}

int minor1_close(struct inode *inode,struct file *file)
{
printk("call minor1 close\n");
return 0;
}

struct file_operations minor0_fops={
.owner = THIS_MODULE,
.write = minor0_write,
.open = minor0_open,
.release = minor0_close
};

struct file_operations minor1_fops={
.owner = THIS_MODULE,
.read = minor1_read,
.open = minor1_open,
.release = minor1_close
};

int minor_open(struct inode *inode,struct file *filp)
{
printk("call minor open\n");
switch(MINOR(inode->i_rdev))
{
case 1: filp->f_op=&minor0_fops;break;
case 2: filp->f_op=&minor1_fops;break;
default: return -ENXIO;
}
if (filp->f_op && filp->f_op->open)
return filp->f_op->open(inode,filp);
return 0;
}

struct file_operations minor_fops={
.owner =THIS_MODULE,
.open =minor_open,
};

int __init int_init(void)
{
int ret;
ret=register_chrdev(MINOR_DEV_MAJOR,MINOR_DEV_NAME,&minor_fops);
if (ret < 0) {
printk(KERN_INFO"minor device register failed!\n");
return ret;
}
else {
printk(KERN_INFO"minor device register successed!\n");
}
return 0;
}
void __exit void_exit(void)
{
unregister_chrdev(MINOR_DEV_MAJOR,MINOR_DEV_NAME);
printk(KERN_INFO"minor device unregister successed!\n");
}


module_init(int_init);
module_exit(void_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Savant Pan");
MODULE_DESCRIPTION("minor device driver example");
阅读(1075) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~