[임베디드 시스템] Device drivers

이번에는 device driver에 대해 공부해보자.

  • Device Driver
    • device와 kernel 사이의 interface로 커널과 디바이스사이의 data전달
    • Device driver은 hardware controller을 관리하거나 message를 보내는 Software이다.
      • Hardware Controller
        • device를 직접 제어하는 녀석
        • Start, Stop, Initialize, Control, Diagnose the device
          • device에는 character device , block device, network device가 있다.
            • character device : 저속의 I/O 한번에 1byte씩 접근
            • block device : Random access / 한번에 왕창 가져옴 / SSD
            • Network Device : /dev에 list되지 않는다.

  • kernel space에 존재한다.
  • module 구조이다.
  • major number & minor number로 identify된다.
    • Major number : driver을 구분하는 number이다.
    • device interface table인 chdevs[]의 index가 된다.
      • chdev[major#].(*read)()
    • 그런데 한 driver는 여러 device를 제어할 수 있기 때문에
    • driver 내 sub device 를 구분하는 minor number이 존재한다.
    • major#은 드라이버 모듈 내에서 init시 register_chrdev, unregister_chrdev 함수를 통해 device interface table에 등록 및 해제한다.
    • 여기서 parameter로 0을 넣으면 자동할당이 되지만, static하게 할당하고 싶다면 /proc/devices(device name/major#)을 보고 사용하지 않은 major#을 mknod해줘야 한다.
  • 리눅스에서는 디바이스가 마치 파일과 같이 동작한다.(open, read/write, close)
    • Device flie
      • user과 kernel 사이의 인터페이스로, systemcall을 통해 device에 접근할 수 있다.
      • /dev 에 존재한다.
      • mknod를 통해 생성한다.
        • mknod filename type(c,b) major minor

그림 1 device driver

그림2 device driver
  • 그림 2를 보자
    • 우선적으로 mknod를 통해 device file이 생성되며, 우리가 major#, minor#을 여기에 할당했기 때문에 device file은 major#을 알고 있다.
    • insmod를 하면 해당 디바이스드라이버가 init된다. 이 과정에서 register_chrdev()를 통해 해당 드라이버의 major#가 device interface table(chrdevs[])에 등록된다.
    • user에서 device file을 open()시 해당 시스템콜은 device interface table을 갖고 있는 chdevs[] 배열에 접근하여, major#을 index로 가지는 변수를 open()한다.
    • 이 외 read, write, ioctl등의 함수를 device file을 통해 수행한다. 얘네는 hardware controller을 컨트롤한다.
    • rmmod 시 device driver 이 exit되면서 unregister_chrdev() 함수를 통해 device interface table(chrdevs[])로부터 해당 driver이 해제된다.
  • ioctl
    • int ioctl(int fd, int cmd, ...)의 형태
      • ...는 실제 argument가 들어간다.
      • cmd는 integer로, [type(major#), number(cmd 번호), direction(r/w), size(transfer 할 data 의 크기)]의 정보를 가진다.
      • 그림3와 같은 명령어로 encoding, decoding한다.
        • 여기서 datatype이 size를 의미한다. (integer -> sizeof(integer)
그림3 ioctl 명령어
      • 즉, ioctl함수 내에서는 cmd의 종류에 따라 if문을 이용하여 알맞은 수행을 얼마든지 할 수 있다. 다시말해, 여러 기능(cmd)을 이 안에서 수행 가능하다.







댓글

가장 많이 본 글