主设备号和file_operations结构关系; ioctl和unlocked_ioctl, ioctl简单测试例子

open一个字符设备的过程是从
VFS层open系统调用 ===> def_chr_fops-->open ===> cdev->fops->open ===> device specific operation

如果是同一类设备,比如系统中有多个framebuffer,那么就把共有的操作放在cdev->fops->open ,这时向系统注册的仅仅是这一个驱动程序;
将这类主设备号相同的一类设备(如/dev/fb0 /dev/fb1等)放在一个数组中,然后在cdev->fops->open 根据次设备号作为Index来调用体现不同设备差异的那些操作,即 device specific operation , 这就是为什么framebuffer/block 等硬件驱动中实现的都不是file_operations这种操作,而是跟具体设备相关的那些操作。
这就是所谓的一类设备都应一种驱动。


可以认为大多数简单的设备,仅仅需要实现到cdev->fops->open 这个位置,等他们变得复杂,能够提炼出共性的时候就 需要实现到[红色device specific operation 这个位置。

 

对于ioctl操作,优先执行f_op->unlocked_ioctl,如果没有unlocked_ioctl,那么执行f_op->ioctl


在学习ARM开发中.ioctl在内2.6.32中。file_operatioins中还有ioctl.自己升级到2.6.36时,使用vim 查错源代码时,发现没有ioctl只有unlocked_ioctl与compat_ioctl函数了。

自己改写这个unlocked_ioctl函数,但是,不知如何用应用程序测试,
当在内核使用ioctl函数时,我在应用程序中使用ioctl(fd,"on"来测试是否调用了ioctl函数。
可是改成unlocked_ioctl函数时,使用unlocked_ioctl()函数时,提示没有这个函数,请问怎么办?
long  test_ioctl(struct file *filp,unsigned int cmd,unsigned long value){
        struct newdev *p = filp->private_data;
        printk("ioctl %X \n",p);
        switch(cmd){
                case 0:
                        p->on();break;
                case 1:
                        p->off();break;
                default:
                        printk("Unknow cmd\n";
                        return -EINVAL;
        }
        return 0;
}

struct file_operations fops = {
        .open = test_open,
        .unlocked_ioctl = test_ioctl,
};

void test_on(void){
        printk("test_on test\n";
}

void test_off(void){
        printk("test_off test \n";
}

void initial_device(struct newdev *d){
        d->on = test_on;
        d->off = test_off;
}


应用程序如下:

int main(int argc,char *argv[]){

        int fd;
        char buf[10];
        printf("1\n";
        printf("argc[%d]\n",argc);
        if (argc != 2){
                printf("Usage %s string \n",argv[0]);
                return -1;
        }
        printf("2\n";

        fd = open("/dev/test",O_RDWR);
        printf("3\n";

        if (fd < 0 ){
                printf("fd open errror\n";
                return -1;

        }
        printf("4\n";
        if (!strncasecmp(argv[1],"on",2))
                unlocked_ioctl(fd,0);
        if (!strncasecmp(argv[1],"off",3))
                unlocked_ioctl(fd,1);

        return 0;
}

root@xu-laptop:/usr/src/linux-2.6.36/drivers/new/10th# gcc -o app app.c
/tmp/cc4PWyCc.o: In function `main':
app.c.text+0xf1): undefined reference to `unlocked_ioctl'
app.c.text+0x12a): undefined reference to `unlocked_ioctl'
collect2: ld returned 1 exit status

root@xu-laptop:/usr/src/linux-2.6.36/drivers/new/10th# 为了把BKL从内核中慢慢去掉,加入了unlocked_ioctl,但是有很多地方都用到ioctl,所以一直保留着,直到b19dd42f的时候,最后移除了所有对ioctl的使用,才把ioctl从file_operations里面去掉。


所以你在内核里用unlocked_ioctl是没有问题的。

但是你应用程序里面不能用unlocked_ioctl,因为系统调用ioctl是没有改变的,还是原来的系统调用接口,只是系统调用的实现中,vfs_ioctl()变成了unlocked_ioctl,在应用层你根本不用关注内核中的这些实现上的改变,你只需要按照系统调用的用法用就可以了。

所以,在内核module里,你应该用.unlocked_ioctl = test_ioctl;

而在应用层测试的时候,应用的系统调用仍然是ioctl()。

也就时VFS层这个中间虚拟文件层调用我写的unlocked_ioctl,而应用层还是通过ioctl调用VFS里面的函数,我要做的工作由VFS完成了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <linux/module.h> #include <linux/fs.h> #include <linux/gpio.h> // 各种gpio的数据结构及函数 #include <linux/cdev.h> #include <linux/init.h> //__init __exit 宏定义声明 #include <linux/device.h> //class devise声明 #include <linux/uaccess.h> //copy_from_user 的头文件 #include <linux/types.h> //设备 dev_t 类型声明 #include <linux/ioctl.h> MODULE_LICENSE("Dual BSD/GPL"); #define IOCTL_GPIO_OFF 0 /*灭*/ #define IOCTL_GPIO_ON 1 /*亮*/ #define DEVICE_NAME "beepctrl_caiyuxin" static struct class *ioctrl_class; #define BEEP_MAJOR 0 /*预设的主设备*/ static int BEEP_major = BEEP_MAJOR; /*BEEP设备结构体*/ struct BEEP_dev { struct cdev cdev; /*cdev结构体*/ }; struct BEEP_dev *BEEP_devp; /*设备结构体指针*/ // 定义三色BEEP的GPIO引脚 static const struct gpio beeps[] = { // { 2, GPIOF_OUT_INIT_HIGH, "BEEP_RED" }, // { 3, GPIOF_OUT_INIT_HIGH, "BEEP_GREEN" }, { 25, GPIOF_OUT_INIT_HIGH, "BEEP" }, }; int BEEP_open(struct inode *inode, struct file *filp)//打开设备节点 { // int i; // printk(KERN_INFO " beeps opened\n"); // for(i=0;i<3;i++) // { // gpio_set_value(beeps[i].gpio, 0); // } return 0; } static long int BEEP_ioctl(struct file *filp,unsigned int cmd, unsigned long arg) { //ioctl函数接口 if (arg > sizeof(beeps)/sizeof(unsigned long)) { return -EINVAL; } printk("arg,cmd: %ld %d\n", arg, cmd); switch(cmd) { case IOCTL_GPIO_OFF:// 设置指定引脚的输出电平为0,由电路图可知,输出0时为灭 gpio_set_value(beeps[arg].gpio, 0); break; case IOCTL_GPIO_ON: gpio_set_value(beeps[arg].gpio, 1); break; default: return -EINVAL; } return 0; } int BEEP_release(struct inode *inode, struct file *filp)//释放设备节点 { int i; printk(KERN_INFO "BEEPs driver successfully close\n"); for(i=0;i<3;i++) { gpio_set_value(beeps[i].gpio, 0); } return 0; } static const struct file_operations BEEP_fops = { .owner = THIS_MODULE, .open = BEEP_open, .release = BEEP_release, .unlocked_ioctl = BEEP_ioctl, /* 实现主要控制功能*/ }; /*初始化并注册cdev*/ static void BEEP_setup
最新发布
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值