Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11072
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-18 14:06
文章分类

全部博文(12)

文章存档

2013年(12)

我的朋友

分类: LINUX

2013-05-18 14:11:12

原文地址:ioctl 简单例子 作者:xingzuzi

main 函数:
#include
#include
#include
#include

#define filename "/dev/dubo_c"

#define SET_A 1

#define GET_A 2

struct info{
    char a;
    int b;
};


int test_ioctl(int fd)
{
    printf("%s \n",__FUNCTION__);
    struct info seta;
    struct info geta;
    int ret ;

    seta.a = 'a';
    seta.b = 15 ;

    ret = ioctl(fd ,SET_A,&seta);
    printf("ret SET_A = %d\n",ret);
    ret = ioctl(fd ,GET_A,&geta);
    printf("ret GET_A = %d\n",ret);


    //ioctl(fd,GET_A,&geta);
    printf("geta geta.a = %c ,geta.b = %d\n",geta.a,geta.b);

    return 0;
}


int main(void )
{
    printf("%s \n",__FUNCTION__);
    int fd ;
    char buf[10];
    char * string = "driver_write";
    int ret ;

    fd = open(filename ,O_RDWR);

    if (fd < 0)
    {
        printf("Open %s file error,please use sudo ?",filename);
        return -1;
    }
#if 1
    ret = read(fd ,buf , 10);
    buf[9]='\0';
    printf("%d dubodu \n",ret);

    if( ret != 10);
    {
        printf("NOTICE: read %s file ,get %d Bytes\n",filename ,ret);

    }

    printf("read from driver %s \n",buf);


    ret = write(fd ,string ,10);

    if (ret != 10)
    {
        printf("NOTICE:write %s file ,get %d Bytes \n",filename ,ret);
    }
#endif
    test_ioctl(fd);
    sleep(20);
    close(fd);

    return 0;
}

drive.c :
#include
#include
#include
#include
#include

#define DUBO_MAJOR 240

MODULE_AUTHOR("dubo");
MODULE_DESCRIPTION("module example");
MODULE_LICENSE("GPL");

#define SET_A 1
#define GET_A 2

struct kinfo{
    char a;
    int b;
};


int dubo_open( struct inode * inode, struct file *filp)
{
    int major ,minor ;

    printk("%s  aka \n",__FUNCTION__);
    minor = MINOR(inode->i_rdev);// 获得次设备号
    major = MAJOR(inode->i_rdev);// 获得主设备号

    printk("dubo_open ok major =%d ,minor =%d \n",major ,minor);

    return 0;
}


ssize_t dubo_read(struct file * filp ,char __user * buf , size_t count,loff_t * f_ops)
{

        struct inode * inode = filp->f_path.dentry->d_inode ;
       
        int major ,minor;

        char * rbuf = "dubo read";

        printk("%s aka \n",__FUNCTION__);
        minor = MINOR (inode ->i_rdev);
        major =  MAJOR (inode ->i_rdev);

        printk("dubo_read ok major =%d ,minor =%d \n",major ,minor);

        if (count > 10)
            count = 10 ;

        copy_to_user(buf,rbuf,count);
       
        current->state = TASK_UNINTERRUPTIBLE;
         printk ("task name = %s,pid=%d call me!\n",current->comm,current->pid);

         schedule ();
            return count;
}




ssize_t dubo_write (struct file * filp ,const char __user * buf ,size_t count ,loff_t *f_pos)
{

    printk("%s  aka \n",__FUNCTION__);
        struct inode * inode = filp->f_path.dentry->d_inode ;

        int major ,minor ;

        char wbuf[10];

        minor = MINOR(inode ->i_rdev);
        major = MAJOR(inode ->i_rdev);

        printk("dubo_write ok major =%d ,minor =%d \n",major ,minor );


        if(count >10)
            count = 10;


            copy_from_user(wbuf,buf,count);


            wbuf[9] = 0;

            printk("user write:%s \n",wbuf);

            return count ;

}

int dubo_ioctl(struct inode * inode ,struct file * filp,unsigned int cmd ,unsigned long arg)
{
    printk("%s dubo_ioctl \n",__FUNCTION__);
    struct kinfo geta,seta ;

    geta.a ='k';

    geta.b = 33;

    switch(cmd)
    {
        case SET_A:
                copy_from_user(&seta,(void *) arg,sizeof(struct kinfo));
                printk("seta.a = %c ,seta.b = %d\n",seta.a,seta.b);
                break;

        case GET_A:
                printk("geta.a = %c ,geta.b = %d\n",geta.a,geta.b);
                copy_to_user((void *)arg,&geta,sizeof(struct kinfo));
                break;
        default:
                break;
    }

    return 0;

}



int dubo_release(struct inode * inode ,struct file *filp)
{
    printk("%s aka \n",__FUNCTION__);
        return 0;
}


struct file_operations dubo_fops = {
        .owner = THIS_MODULE,
        .open = dubo_open,
        .read = dubo_read,
        .write = dubo_write,
        .release = dubo_release,
        .ioctl = dubo_ioctl,

};


int __init
dubo_init(void )
{
    printk("%s aka \n",__FUNCTION__);
    int rc;

    printk("Test char dev \n");

    rc = register_chrdev(DUBO_MAJOR , "dubo_c",&dubo_fops);

    if( rc < 0)
    {
            printk("register %s char dev error \n","dubo_c");
            return -1;
    }

    printk("ok!\n");
    return 0;
}


void __exit
dubo_exit(void)
{
    printk("%s  aka \n",__FUNCTION__);
    unregister_chrdev(DUBO_MAJOR ,"dubo_c");
    printk("module exit\n");
    return ;
}

module_init(dubo_init);
module_exit(dubo_exit);


阅读(215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~