Communicate with Kernel driver

User space can not exchange data with kernel space directly. They need to use system call. e.g. fopen, ioctl, write, read …… etc. In Kernel Space …… 1. Register your device with “register_chrdev”, defined in linux/fs.h .  https://ithelp.ithome.com.tw/articles/10159749 2. Implement driver functions, ioctl, open,…  struct file_operations fops = {   .owner = THIS_MODULE,   .read = device1_read,   .write = device1_write,   .ioctl = device1_ioctl,   .open = device1_open,   .release = device1_release,  };  int ioctl(struct inode *, struct file *, unsigned int, unsigned long);  is in linux/ioctl.h  http://ccckmit.wikidot.com/lk:io  開發 driver 需要的基礎知識  user space/kernel space 的IO觀念及實作 Copy data ……  Kernel Space to User Space: copy_to_user()  User Space to Kernel Space: copy_from_user() In User Space …… Use system call to control kernel driver.  fopen (open)  write  read  close  seek  poll / select  ioctl  mmap  fcntl

int main(int argc, char *argv[]){
  int devfd;
  int num = 0;

  if (argc > 1) num = atoi(argv[1]);
  if (num < 0) num = 0xff;

  devfd = open("/dev/debug", O_RDONLY);
  if (devfd == -1) {
    printf("Can't open /dev/debug\n");
    return -1;
  }

  printf("Write 0x%02x...\n", num);
  ioctl(devfd, IOCTL_WRITE, num);
  printf("Done. Wait 5 seconds...\n");
  sleep(5);
  close(devfd);

  return 0;
}

Last updated