标签: linux-device-driver

为什么 file_operations 中的 unlocked_ioctl 返回 long,而 sys/ioctl.h 中的 ioctl() 返回 int?

struct file_operations 中的unlocked_ioctl 的签名是

long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
Run Code Online (Sandbox Code Playgroud)

而 man 2 ioctl 说 ioctl(2) 的签名是:

int ioctl(int d, int request, ...);
Run Code Online (Sandbox Code Playgroud)

我知道参数在内核内部是如何被破坏的,但是为什么内核空间中的返回类型是long,而用户空间中的返回类型却是int?当我想返回一个负值作为错误时,这会产生一个问题:由于二补码编码,我返回的所有负值都会变成 -1。

c unix ioctl linux-device-driver linux-kernel

2
推荐指数
1
解决办法
2267
查看次数

Linux 网络驱动程序 --- net_device_ops

到目前为止,我只在设备驱动程序中使用了文件操作结构。使用系统调用 open & read 、 write 。

如何使用 net_device_ops 打开设备驱动程序并传输数据?网上有用户程序与之交互的参考示例吗?

http://lnxpps.de/rpie/mcp2515_mod.c

linux linux-device-driver

2
推荐指数
1
解决办法
6636
查看次数

如何在块设备驱动程序中禁用请求合并?

如何在块设备驱动程序中禁用此功能?我的意思是:正如下面的文档中提到的,我想将该“标志”的值设置为 2。我在哪里可以做到这一点?最好在块设备驱动程序代码中。

What:       /sys/block/<disk>/queue/nomerges
Date:       January 2010
Contact:
Description:
        Standard I/O elevator operations include attempts to
        merge contiguous I/Os. For known random I/O loads these
        attempts will always fail and result in extra cycles
        being spent in the kernel. This allows one to turn off
        this behavior on one of two ways: When set to 1, complex
        merge checks are disabled, but the simple one-shot merges
        with the previous I/O request are enabled. When set to 2,
        all merge tries …
Run Code Online (Sandbox Code Playgroud)

linux linux-device-driver linux-kernel

2
推荐指数
1
解决办法
4785
查看次数

device_register 和 driver_register 的区别

我正在写一个UART驱动程序。我在第 14.Linux 设备模型一章中遇到了这两个函数。

int device_register(struct device *dev);
int driver_register(struct device_driver *drv);
Run Code Online (Sandbox Code Playgroud)

由于 UART 是一个字符驱动程序,我使用 ( 动态创建了主alloc_chrdev_region)设备号,并使用cdevadd().

我在 omap-serial.c 中遇到了uart_register_driver()和。platform_driver_register()

我可以映射driver_register与 ,platform_driver_register()uart_register_driver映射与 tty 相关的函数。因为我是初学者,所以我不想使用 tty 相关的函数。

uart_register_driver与?有关device_driver()

请解释。

c serial-port linux-device-driver embedded-linux uart

2
推荐指数
1
解决办法
3753
查看次数

系统调用如何从用户空间到内核空间并返回用户空间?

我研究了一些文章,其中我得到的信息是像 open() 这样的系统调用调用 glibc 中的包装函数,然后引发一个陷阱,将上下文从用户空间切换到内核空间,然后使用 cpu 寄存器来调用系统调用内核空间中的参数/参数。

但我仍然想我缺少系统调用调用的分步过程或详细序列。如果人们能够提供考虑 ARM arch 作为参考的步骤,那就太好了。提前致谢。

linux linux-device-driver linux-kernel embedded-linux

2
推荐指数
1
解决办法
4785
查看次数

为什么USB设备在成功探测后立即断开连接

我正在学习如何为 USB 设备编写内核模块,我根据 USB 设备的 VID/PID 更改了“drivers/usb_sculptor.c”中找到的 usb_sculpture 示例。我能够使用 insmod 成功插入模块。插入后,设备探测函数被调用并成功返回,但在断开连接函数被调用后立即返回。

当我尝试 lsmod(设备仍处于插入状态)时,它显示驱动程序未被任何设备使用。

insmod 后的 dmesg:

[207.206082] usb_sculpture:模块验证失败:签名和/或所需密钥丢失 - 污染内核

[207.206451]usbcore:注册了新的接口驱动程序骨架

设备插入后 dmesg:

[275.794675]骨架1-1.2:1.0:USB骨架设备现在连接到USBSkel-1

[275.946207]usb 1-1.2:usbfs:骨架声明接口0,而“brltty”设置配置#1

[275.946924]骨架1-1.2:1.0:USB骨架#1现已断开连接

请指导我什么问题导致设备断开连接。

linux linux-device-driver linux-kernel

2
推荐指数
1
解决办法
3242
查看次数

应该如何调用 init_module (在用户空间中)

我试图弄清楚如何从“C”应用程序加载内核模块,我不想使用,finit_module因为我的系统上没有用于此系统调用的 glibc 包装器。

这是我尝试过的:

#include <fcntl.h>
#include <sys/mman.h>

int init_module(void *module_image, unsigned long len,
                const char *param_values);

int main() {
    int res = 0;
    void *buf = 0;
    struct stat sb;
    int rc = 0;

    int fd = open("/tmp/my-test.ko", O_RDONLY|O_CLOEXEC);
    if (fd < 0) {
        rc = -1;
        goto EXIT;
    }

    res = fstat(fd, &sb);
    if (res == -1) {
        rc = -2;
        goto EXIT_CLOSE;
    }

    buf = mmap(0, sb.st_size, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
    if (buf == 0) …
Run Code Online (Sandbox Code Playgroud)

linux linux-device-driver embedded-linux

2
推荐指数
1
解决办法
1763
查看次数

如何通过 make menuconfig 或 make nconfig 禁用 CONFIG_STRICT_DEVMEM

我需要禁用CONFIG_STRICT_DEVMEMLinux 内核中的一个选项并重新编译它。为什么?我被告知要这样做。我知道我可以通过简单地在文件中注释掉它来禁用它.config,但我需要看看它是如何通过make menuconfigor完成的make nconfig。为什么?这是一个演示,我需要能够展示它。有谁知道该怎么做?

任何帮助表示赞赏。

linux linux-device-driver linux-kernel

2
推荐指数
1
解决办法
7274
查看次数

标志 PF_MEMALLOC 有什么用

当我在Linux中的一个设备驱动程序中浏览一些代码时,我发现该标志PF_MEMALLOC正在线程(进程)中设置。我在头文件中找到了这个标志的定义,它说“分配内存”

#define PF_MEMALLOC     0x00000800      /* Allocating memory */
Run Code Online (Sandbox Code Playgroud)

所以,我的疑问是,当在进程/线程之类的代码中设置这个标志时,它到底有什么用途current->flags |= PF_MEMALLOC;

scheduler linux-device-driver linux-kernel

2
推荐指数
1
解决办法
2265
查看次数

我们需要在调用 schedule 之前调用 set_current_state(TASK_INTERRUPTIBLE) 吗?

我是否需要在调用set_current_state()之前调用schedule()以安排运行队列中的下一个进程?

我看过很多代码,其中内核线程函数set_current_state()用于将状态设置为TASK_RUNNINGTASK_UNINTERRUPTIBLE

set_current_state(TASK_INTERRUPTIBLE);

while (!kthread_should_stop()) {
        ....
        schedule();
        .....
}
__set_current_state(TASK_RUNNING);
Run Code Online (Sandbox Code Playgroud)

是否有必要调用set_current_state()TASK_INTERRUPTIBLE调用之前schedule()

在这个示例代码中,将状态设置为TASK_RUNNING退出kthread_should_stop()循环后有什么用?

c scheduling linux-device-driver linux-kernel

2
推荐指数
1
解决办法
1054
查看次数