Application code is very simple. We are opening our device which is in the path "/dev/myled". Now there is an infinite loop. First we are reading one byte from device and LSB bit is indicating the status of the LED. Now we are toggling the status and writing back the status to the device. Again we are doing the same using ioctl(). Here reading and writing happends in the device driver read()/write()/ioctl() call back and calls are blocked till interrupt is triggred.

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

#define IO_READ 0
#define IO_WRITE 1

int main(int argc, char *argv[])
{
  int fd;
  unsigned char status;
  fd = open ("/dev/myled", O_RDWR);
  if (fd < 0) {
    return (-1);
  }
  while (1) {
    
    read(fd, &status, 1);
    printf("LED Status %s using read()\n", (status & 1) ? "on" :"off");
    sleep(1);
    printf("Turning LED %s using write()\n", (status & 1) ? "off" :"on");
    status ^= 1;
    write(fd, &status, 1);
    sleep(1);
    
    ioctl(fd, IO_READ, &status);
    printf("LED Status %s using ioctl()\n", (status & 1) ? "on" :"off");
    sleep(1);
    printf("Turning LED %s using ioctl()\n", (status & 1) ? "off" :"on");
    status ^= 1;
    ioctl(fd, IO_WRITE, &status);
    sleep(1);
  }
  close(fd);
  return 0;
}

#