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
1
int main(int argc, char *argv[]){
2
  int devfd;
3
  int num = 0;
4
5
  if (argc > 1) num = atoi(argv[1]);
6
  if (num < 0) num = 0xff;
7
8
  devfd = open("/dev/debug", O_RDONLY);
9
  if (devfd == -1) {
10
    printf("Can't open /dev/debug\n");
11
    return -1;
12
  }
13
14
  printf("Write 0x%02x...\n", num);
15
  ioctl(devfd, IOCTL_WRITE, num);
16
  printf("Done. Wait 5 seconds...\n");
17
  sleep(5);
18
  close(devfd);
19
20
  return 0;
21
}
Copied!
Copy link