将自定义标志传递给“打开"窗口.在设备驱动程序中 [英] Passing custom flags to "open" in a device driver

查看:63
本文介绍了将自定义标志传递给“打开"窗口.在设备驱动程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些自定义标志传递给设备驱动程序的open()调用.

I need to pass some custom flags to the open() call of my device driver.

我在LDD3中找到了这个例子:

I found this example in LDD3:

int dev_open(struct inode *inode, struct file *filp)
{
    if ((filp->f_flags & O_ACCMODE) == O_WRONLY) {
        ...
    }
}

我的问题是:是否可以定义其他标志(例如O_ACCMODEO_WRONLY)而不会与其他任何标志冲突?

My question is: is it possibile to define other flags (like O_ACCMODE and O_WRONLY) without conflicts with any others?

推荐答案

是的,有可能.看看 include/uapi/asm-generic/fcntl.h .请注意下一条评论:

Yes, it's possible. Take a look at include/uapi/asm-generic/fcntl.h. Pay attention to next comment:

/*
 * When introducing new O_* bits, please check its uniqueness in fcntl_init().
 */

现在查看fcntl_init()函数(在 fs/fcntl中定义.c ):

/*
 * Please add new bits here to ensure allocation uniqueness.
 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
 * is defined as O_NONBLOCK on some platforms and not on others.
 */
BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
    O_RDONLY        | O_WRONLY      | O_RDWR        |
    O_CREAT         | O_EXCL        | O_NOCTTY      |
    O_TRUNC         | O_APPEND      | /* O_NONBLOCK | */
    __O_SYNC        | O_DSYNC       | FASYNC        |
    O_DIRECT        | O_LARGEFILE   | O_DIRECTORY   |
    O_NOFOLLOW      | O_NOATIME     | O_CLOEXEC     |
    __FMODE_EXEC    | O_PATH        | __O_TMPFILE
    ));

因此,首先您需要为新定义查找唯一值,以便可以按fcntl_init()中列出的标志按位进行或"运算.接下来,您需要将新定义添加到include/uapi/asm-generic/fcntl.h.最后,将您的新定义添加到fcntl_init(),以便在编译时对其进行检查.

So first you need to find unique value for your new definition, so it can be bitwise-or'd with flags listed in fcntl_init(). Next you need to add your new definition to include/uapi/asm-generic/fcntl.h. And finally add your new define to fcntl_init(), so it will be checked at compile time.

最后,归结为找到与现有定义不冲突的值.例如.如我所见,所有10、100、1000、10000、100000、1000000和10000000都被使用了.因此,对于新标记,您可以使用100000000、200000000、400000000和800000000值.

In the end it boils down to finding the value that doesn't conflict with existing definitions. E.g. as I can see all 10, 100, 1000, 10000, 100000, 1000000 and 10000000 are used. So for your new flags you can use 100000000, 200000000, 400000000 and 800000000 values.

更新:正如正确提及的 SailorCaire 一样,您还需要在BUILD_BUG_ON()宏.例如,如果最初是BUILD_BUG_ON(20 - 1,并且要向此列表中添加一个元素,则应将其设置为BUILD_BUG_ON(21 - 1.

UPDATE: As SailorCaire correctly mentioned, you also need to increment first number in BUILD_BUG_ON() macro. For example, if it originally was BUILD_BUG_ON(20 - 1, and you are to add one element to this list, you should make it BUILD_BUG_ON(21 - 1.

更新2 : SailorCaire 中的另一个有价值的补充:

UPDATE 2: Another valuable addition from SailorCaire:

顺便说一句,您需要执行make install_headers,复制新的标头,并且看起来您需要重新编译glibc,以便它可以知道API的更改.

By the way, you'll need to do make install_headers, copy the new headers, and it looks like you'll need to recompile glibc so it becomes aware of the API change.

这篇关于将自定义标志传递给“打开"窗口.在设备驱动程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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