VEDA 55일차 - 리눅스 커널 프로그래밍
driver model : 드라이버는 여러 형태가 있다. elixir 참조
1. old fashioned : module(insmod, rmmod) + device file(mknod(/dev 0 ~ 255, ~ 512)) + file_operations(open, close, read, write)
2. new fashioned : object oriented concept(,but not c++)
복잡해지는 장치 위상 구조(허브의 허브의 허브의....)
전원 관리 기능
유저 영역에서 접근(/sys, sysfs)
구체적인 형태(bus(버스), devices(장치), driver(장치의 입출력), class(디바이스의 기능적 종류의 구분 방법, 일반 유저의 구분 기준))
코드 중복 최소화(kobject_get/put)
직관적인 장치 구분
kobject : 드라이버 모델의 가장 기본적인 요소, 다른 구조체 내부에서 내장되어 사용, sysfs 파일 시스템에서 하나의 디렉토리로 나타나고, attribute는 하나의 파일로 표현된다.
참조 카운트 : 객체가 누군가에게 참조되고 있다면 그 객체를 계속 유지해야 한다. 더 이상 사용하지 않는다면 자원을 해제해야 한다. => kobject는 이 기능을 제공한다.
kset : kobject + 더 많은 기능
https://blog.naver.com/PostView.nhn?blogId=msnayana&logNo=80165906129
디바이스 드라이버 와 모듈
디바이스 드라이버 와 모듈 ~~ for Linux 리눅스에서 디바이스 드라이버는 상당히 중...
blog.naver.com
드라이버에 대한 설명에 앞서서 /proc, /sys, /dev에 대해서 알고 가는 것이 좋다.
항목 | /proc | /sys | /dev |
유형 | 가상 파일 시스템 | 가상 파일 시스템 | 실제 디바이스 파일 (udev 생성) |
주 용도 | 프로세스/커널 정보 제공 | 커널 객체(디바이스/드라이버) 표현 | 디바이스 노드를 통한 실제 I/O |
사용자 공간과의 인터페이스 | 정보 제공 위주 (읽기 중심) | sysfs 속성 읽기/쓰기 | open/read/write/ioctl 등 |
주요 커널 API | proc_create(), seq_file | class_create(), device_create() | register_chrdev(), cdev_*() |
장치 드라이버 관련 | 디버그/통계 정보 제공 | 장치/드라이버 모델, 속성 관리 | 사용자와 실제 데이터 교환 통로 |
드라이버 모델
bus (버스) | 디바이스와 드라이버를 연결해주는 논리적/물리적 통로 | struct bus_type |
device (디바이스) | 실제 장치(센서, USB, 블록 디바이스 등) | struct device |
driver (드라이버) | 디바이스를 제어하기 위한 커널 코드 | struct device_driver |
class (클래스) | 기능적으로 유사한 장치를 그룹화 (예: 입력 장치, 저장 장치) | struct class |
interface (인터페이스) | 디바이스가 특정 프로토콜 기능을 구현하는 경우 (예: USB 인터페이스) | 프로토콜별 구조체 (예: usb_interface) |
struct bus_type {
const char *name;
struct device *dev_root;
struct kset subsys;
struct kset drivers;
struct kset devices;
int (*match)(struct device *dev, struct device_driver *drv);
int (*probe)(struct device *dev);
int (*remove)(struct device *dev);
...
};
디바이스화 드라이버의 매칭 기준을 제공하는 핵심 구조, USB, I2C, PCI 등의 버스를 추상화 ex) USB-A 잭을 사용하는 키보드는 USB에 매치된다.
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};
플랫폼 버스에 등록된 장치를 위한 드라이버, 일반적으로 디바이스 트리 또는 하드코딩으로 장치 정보가 정해짐 => 키보드 같은 장치가 아니라 내부 RCC, GPIO 등 peripheral device 의미, platform_device와 platform_driver는 1:1로 매칭되어 사용된다. probe, of_match_table이 중요하다.
struct device {
const char *init_name;
struct device *parent;
struct bus_type *bus;
struct device_driver *driver;
struct class *class;
dev_t devt;
void *platform_data;
struct kobject kobj;
...
};
장치 자체를 표현하는 구조체, 드라이버, 부모 디바이스, 버스 정보 등이 포함된다. ex) USB-A 타입 키보드를 표현하는 device 구조체
struct device_driver {
const char *name;
struct bus_type *bus;
int (*probe)(struct device *dev);
int (*remove)(struct device *dev);
...
};
드라이버 자체를 표현한다. 드라이버가 처리할 수 있는 디바이스 타입을 지정한다. ex) 잭 타입에 상관없이 일정 데이터 포맷을 송수신하는 키보드를 조작하기 위한 드라이버 구조체
struct class {
const char *name;
struct module *owner;
struct kobject *dev_kobj;
int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
...
};
비슷한 기능을 가진 디바이스를 그룹화 한다. /sys/class/ 생성에 사용
struct kobject {
const char *name;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
};
위에서 설명한 구조체들이 내부적으로 갖추고 있는 객체, 커널 오브젝트 계층 구조 구성(sysfs 지원)
+----------------+
| bus_type | ← 각 장치와 드라이버를 연결
+----------------+
▲ ▲
| |
+-------+ +-------------------+
| device | | device_driver |
+--------+ +------------------+
|
▼
+--------+
| class | ← 장치의 논리적 분류
+--------+
|
▼
+--------+
| /dev | ← udev가 장치 파일 생성
+--------+
MISC 계열 디바이스
기타 장치, 기본적으로 character device로 분류되기는 한다.(애매하다.)
경량 character device driver를 만들기 위한 struct 제공
input device driver
USB 버스 등장 후 입력장치의 인터페이스의 표주화 요구 대두 => 입력 장치들을 input 장치로 통합, 드라이버도 통합, 이벤트 처리 기반, 더 많은 학습 필요
usb transfer mode
1. control : usb 연결 여부 확인 등에 사용
2. interrupt transfer : 인터럽트는 아니지만 인터럽트와 유사하게 동작한다.(key press -> interrupt(EVENT) -> PC), 이벤트 처리기반
3. bulk transfer : USB stick, usb 메모리 장치 등등에 사용되는 통신 모드, 데이터 점검 있음
4. iso-synchronous transfer : usb 스피커 등에 사용되는 통신 모드, 벌크와 비슷하지만 데이터 점검이 없음
touch screen 관련 드라이버 확인