从驱动程序代码设置设备权限失败 [英] Setting device permission from driver code fails

查看:211
本文介绍了从驱动程序代码设置设备权限失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Linux内核3.10.14中的用户空间访问I2C设备驱动程序节点. 我在内核配置中添加了i2c-dev,并获得了/dev/i2c- *设备节点.但是他们有权限

I want to access I2C device driver nodes from user space in a linux kernel 3.10.14. I added i2c-dev in the kernel configuration and got the /dev/i2c-* device nodes. However they have permission

$ ls -l /dev/i2c-*
crw------- root     root      89,   1 2014-08-21 20:00 i2c-1

在drivers/i2c/i2c-dev.c中,我添加了回调

In drivers/i2c/i2c-dev.c I added the callback

static char *i2c_dev_devnode(struct device *dev, umode_t *mode)
{
    if (!mode)
            return NULL;
    if (MAJOR(dev->devt) == I2C_MAJOR)
            *mode = 0666;
    return NULL;
}

在同一文件中,我将回调添加到设备类struct中:

and in the same file I added the callback to the device class struct:

static int __init i2c_dev_init(void)
{
   ...
   i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
   ...

    /* set access rights */
    i2c_dev_class->devnode = i2c_dev_devnode;
   ...
}

但是设备节点的访问权限仍然保留

However the access rights of the device node remain

crw------- root     root      89,   1 2014-08-21 20:00 i2c-1

没有/lib/udev/rules.d或/etc/udev/rules.d

There is no /lib/udev/rules.d or /etc/udev/rules.d

如果有任何建议,我将不胜感激.

I would appreciate any suggestions what might go wrong here.

我也对如何测试此问题的想法感兴趣.

I am also interested in ideas how to test this issue.

推荐答案

您可以尝试以下方法.至少在内核4.9.56上有效.

You might try the following. This works at least with kernel 4.9.56.

static int my_uevent(struct device *dev, struct kobj_uevent_env *env)
{
    add_uevent_var(env, "DEVMODE=%#o", 0666);
    return 0;
}

static int __init i2c_dev_init(void)
{
   ...
   i2c_dev_class = class_create(THIS_MODULE, "i2c-dev");
   ...

   /* set access rights */
   i2c_dev_class->dev_uevent = my_uevent;
   ...
}

这篇关于从驱动程序代码设置设备权限失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆