简体   繁体   English

如何使用函数并将其变量传递给linux驱动程序LM70中定义的用户应用程序?

[英]How to use a function and pass its variables to the user app defined in the linux driver LM70?

Hi i would like to know how is it possible to call/run the following function from user space. 您好我想知道如何从用户空间调用/运行以下功能。

static ssize_t lm70_sense_temp(struct device *dev,
        struct device_attribute *attr, char *buf)

{ //some code . {//一些代码。 . status = sprintf(buf, "%d\\n", val); status = sprintf(buf,“%d \\ n”,val); /* millidegrees Celsius */ . / * millidegrees Celsius * /。 . //some code } //一些代码}

This function is defined in lm70.c driver located in the kernel/drivers/hwmon folder of the linux source? 这个函数是在lm70.c驱动程序中定义的,位于linux源代码的kernel / drivers / hwmon文件夹中? Is it possible to pass the values of this functions internal variables to the user application? 是否可以将此函数内部变量的值传递给用户应用程序? I would like to retrieve the value of val variable in the above function... 我想在上面的函数中检索val变量的值...

I don't know well the kernel internals. 我不太了解内核的内部情况。 However, I grepped for lm70_sense_temp in the entire kernel source tree, and it appears only in the file linux-3.7.1/drivers/hwmon/lm70.c , first as a static function, then as the argument to DEVICE_ATTR . 但是,我在整个内核源代码树中使用了lm70_sense_temp ,它只出现在文件linux-3.7.1/drivers/hwmon/lm70.c ,首先作为静态函数,然后作为DEVICE_ATTR的参数。

Then I googled for linux kernel DEVICE_ATTR and found immediately device.txt which shows that you probably should read that thru the sysfs, ie under /sys ; 然后我用Google搜索了linux kernel DEVICE_ATTR并立即找到了device.txt ,它显示你可能应该通过sysfs读取,即在/sys ; read sysfs-rules.txt ; read sysfs-rules.txt ; so a user application could very probably read something relevant under /sys/ 所以用户应用程序很可能会读取/sys/下的相关内容

I'm downvoting your question because I feel that you could have searched a few minutes like I did (and I am not a kernel expert). 我正在低估你的问题,因为我觉得你可以像我一样搜索几分钟(而且我不是内核专家)。

You don't need to call this function from user space to get that value - it is already exported to you via sysfs. 您不需要从用户空间调用此函数来获取该值 - 它已经通过sysfs导出给您。

You could use grep to find which hwmon device it is: 您可以使用grep查找它是哪个hwmon设备:

grep -rl "lm70" /sys/class/hwmon/*/name /sys/class/hwmon/*/*/name

Then you can read the temperature input from your user space program, eg: 然后,您可以从用户空间程序中读取温度输入,例如:

#include <stdio.h>
#include <fcntl.h>

#define SENSOR_FILE "/sys/class/hwmon/hwmon0/temp1_input"

int readSensor(void)
{
    int fd, val = -1;
    char buf[32];
    fd = open(SENSOR_FILE, O_RDONLY);
    if (fd < 0) {
        printf("Failed to open %s\n", SENSOR_FILE);
        return val;
    }
    if (read(fd, &buf, sizeof(buf)) > 0) {
        val = atoi(buf);
        printf("Sensor value = %d\n", val);
    } else {
        printf("Failed to read %s\n", SENSOR_FILE);
    }
    close(fd);
    return val;
}

As others have already stated - you can't call kernel code from user space, thems the breaks. 正如其他人已经说过的那样 - 你不能从用户空间调用内核代码,那就是休息时间。

You cannot call a driver function directly from user space. 您无法直接从用户空间调用驱动程序功能。

If that function is exported with EXPORT_SYMBOL or EXPORT_SYMBOL_GPL then we can write a simple kernel module and call that function directly. 如果使用EXPORT_SYMBOLEXPORT_SYMBOL_GPL导出该函数,那么我们可以编写一个简单的内核模块并直接调用该函数。 The result can be sent to user space through FIFO or shared memory. 结果可以通过FIFO或共享内存发送到用户空间。

But in your case, this function is not exported. 但在您的情况下,不会导出此功能。 so you should not do in this way. 所以你不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM