Warm tip: This article is reproduced from stackoverflow.com, please click
linux linux-kernel linux-device-driver

How to access input device driver from userspace

发布于 2020-03-27 10:30:00

I'm currently developing an input subsystem driver for touchscreen. What I don't know is how to access the device from userspace, e.g. how to open a file that should be created in filesystem. What I've done so far is this: After I insmod the driver, I get the following message in dmesg:

input: driver_name as /devices/platform/soc/3f804000.i2c/i2c-1/1-0038/input/input0

Now when I go at this location, I find input0, which is a directory. In this directory, I can find files such as name, properties, uevent, but none of the files here contains touch data.

My question here is, where does input subsystem puts touch data after I call

input_report_abs(data.input, ABS_X, coord_x);
input_report_abs(data.input, ABS_Y, coord_y);
input_sync(data.input);
Questioner
smiljanic997
Viewed
91
smiljanic997 2019-07-05 04:16

SOLVED: Once you do insmod, new file is created under /dev/input, in my case it was event0 file. In order to test the functionality, you can do evtest input0. This file can be used from a userspace program in the following way:

struct input_event ev;
FILE* fd = open("/dev/input/event0", O_RDWR);
while(1)
{
    int count = read(fd, &ev, sizeof(struct input_event);
    for(int i = 0; i < (int)count / sizeof(struct input_event); i++)
    {
        if(EV_KEY == ev.type) // printf ...
        if(EV_ABS == ev.type) // printf ...
    }
}

Hope this will help somebody because I feel like this isn't covered enough in Documentation.