在Linux中,你如何使用现有的类中device_create? [英] In Linux, how do you use device_create within an existing class?

查看:458
本文介绍了在Linux中,你如何使用现有的类中device_create?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我今天上市,因为它是这个问题,我的的反对改变实现(移动类的创建一个公共区例如),如果它使事情更容易...我只是不知道如何去做。 :结束注意

Note: I'm listing this problem as it is today, I'm not opposed to changing the implementation (moving the creation of the class to a common area for example) if it makes things easier... I'm just not sure how to do it. :End Note

我有两个Linux内核模块和我试图更新/ sys条目都为他们。谷歌和其他搜索的来源的时候,我见过很多code的线沿线的:

I've got two linux kernel modules and I'm trying to update the /sys entries for them. Searching around on google and other sources, I've seen lots of code along the lines of:

static dev_t MyDev;
static struct class *c1;

static int __init start_func(void)
{
    ...
    MyDev = MKDEV(nMajor, MINOR_VERSION);
    register_chrdev_region(MyDev, 1, MODULE_NAME);
    c1 = class_create(THIS_MODULE, "chardrv");
    device_create(c1, NULL, MyDev, NULL, MODULE_NAME);
    ....

和我已经验证了这个code ++工程,我的第一个模块,并且它正确地创建一个:

And I've verified for my first module this code works, and that it correctly creates a:

/sys/class/chardrv/<MODULE_NAME>

条目。我想知道的是你如何在现有类创建设备。换句话说,我的模块中的一个创建了这个新chardrv类,现在我想我的其他模块,能够在同一类下也注册它的设备。

entry. What I'd like to know is how do you create a device in an existing class. In other words, one of my modules created this new chardrv class, now I want my other module to be able to also register its devices under the same class.

我不能叫class_create()再次(第二个模块),因为chardrv类已经存在...

I can't call class_create() again (in the second module), because that "chardrv" class already exists...

所以,我可以运行检查,看是否/ SYS /班/ chardrv存在,这可以帮助我决定,如果我需要调用class_create()或没有,这不是一个问题。让我们把一些伪code在这里澄清:

So I can run a check to see if /sys/class/chardrv exists, and this can help me decide if I need to call class_create() or not, that's not a problem. Lets put some pseudo code in here to clarify:

if ( path "/sys/class/chardrv" does not exist)
    new_class = class_create("chardrv")
else
    new_class = some how get class "chardrv" handle, or properties, or whatever
device_create(new_class, ...)

所以,按照这个例子,如果我的类已经存在,我只是想我的新设备添加到它的从第二模块我想我需要创建一个类结构,并以某种方式填充它用正确的chardrv级属性,然后像以前一样叫device_create,但我不知道该怎么做。

So as per this example, if my class already exists, and I just want to add my new device into it from a second module I assume I need to create a class structure and somehow populate it with the correct "chardrv class" attributes then call device_create as before, but I'm not sure how to do that.

推荐答案

要与同一类使用 device_create 功能,只需一个指针传递同一类。

To use the device_create function with the same class, just pass it a pointer to the same class.

由于要在不同的模块比在其中创建类的一个叫 device_create ,你需要为指针导出符号类。您可以使用 EXPORT_SYMBOL 宏来做到这一点。

Since you want to call device_create in a different module than the one in which you create the class, you'll need to export the symbol for the pointer to the class. You can use the EXPORT_SYMBOL macro to do this.

例如:

module1.c

extern struct class *c1;    /* declare as extern */
EXPORT_SYMBOL(c1);          /* use EXPORT_SYMBOL to export c1 */

static dev_t mod1_dev;
static int __init start_func(void)
{
        ...
        /* define class here */
        c1 = class_create(THIS_MODULE, "chardrv");

        /* create first device */
        device_create(c1, NULL, mod1_dev, NULL, "mod1_dev");
        ....
}

module2.c

extern struct class *c1;    /* declare as extern */

static dev_t mod2_dev;
static int __init start_func(void)
{
        ...
        /* c1 is defined in module 1 */

        /* create second device */
        device_create(c1, NULL, mod2_dev, NULL, "mod2_dev");
        ....
}

请注意:您需要之前插入模块1 模块2 因为类指针定义和出口的模块1

Note: You'll need to insert module1 before module2 since the class pointer is defined and exported in module1.

这应该创建你所期望的目录:

That should create the directories you are expecting:


  • / SYS /班/ chardrv / mod1_dev

  • / SYS /班/ chardrv / mod2_dev

  • /sys/class/chardrv/mod1_dev
  • /sys/class/chardrv/mod2_dev

顺便说一句,如果你是当您试图加载第二个模块得到一个参数无效的错误,你可能需要<一个href=\"http://stackoverflow.com/questions/16360689/invalid-parameters-error-when-trying-to-insert-module-that-accesses-exported-s\">add一个 KBUILD_EXTRA_SYMBOLS 行你的Makefile 。

By the way, if you are getting an Invalid parameters error when you try to load the second module, you might have to add a KBUILD_EXTRA_SYMBOLS line to your Makefile.

这篇关于在Linux中,你如何使用现有的类中device_create?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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