Search code examples
linuxlinux-kernellinux-device-drivergpiocat

How can I use the cat command to read from a character device with my own character device driver?


I am trying to write a simple read/write character driver for a beaglebone. One of the tasks is to be able to use the cat command to read from the device, but I am unable to do so.

Here is the code for my read function for my character device driver located inside char_drvr.c:

ssize_t lkm_read (struct file *pfile,
                  char __user *buffer,
                  size_t length,
                  loff_t *offset)
{
    int device_buffer = (int)gpio_get_value(gpioIn); //gets value from gpio pin (HI or LO)
    return simple_read_from_buffer(buffer,length, offset, &device_buffer, sizeof(int));     
}

Read executes successfully (the correct value is returned) when I run my test application, so I know that device is able to communicate with the device driver, but nothing is returned (right away) when I run

$cat /dev/simple_char_driver

My full code: https://github.com/gteeger/character_driver1

Attempted solutions:

cat function calling read() infinite times

C - infinite read from cat in Device file

and a few others.

Any help is greatly appreciated


Solution

  • Answer: they are called character device drivers, not boolean device drivers.

    Fixed code, now cat works:

    ssize_t lkm_read (struct file *pfile,
                      char __user *buffer,
                      size_t length,
                      loff_t *offset) 
    {
    char out = '0';
    bool gpioPin = gpio_get_value(gpioIn);
    if (gpioPin == 0) out = '0';
    if (gpioPin == 1) out = '1';
    printk(KERN_ALERT "Inside the %s function\n", __FUNCTION__);
    return simple_read_from_buffer(buffer, length, offset, &out, sizeof(char));
    }