0

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); /* millidegrees Celsius */ . . //some code }

This function is defined in lm70.c driver located in the kernel/drivers/hwmon folder of the linux source? 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...

user1877992
  • 67
  • 1
  • 5

3 Answers3

1

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.

Then I googled for linux kernel DEVICE_ATTR and found immediately device.txt which shows that you probably should read that thru the sysfs, i.e. under /sys; read sysfs-rules.txt; so a user application could very probably read something relevant under /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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

You don't need to call this function from user space to get that value - it is already exported to you via sysfs.

You could use grep to find which hwmon device it is:

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

Then you can read the temperature input from your user space program, e.g:

#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.

austinmarton
  • 2,278
  • 3
  • 20
  • 23
0

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. The result can be sent to user space through FIFO or shared memory.

But in your case, this function is not exported. so you should not do in this way.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63