반응형
일단..
Linux 의 device 드라이버를 작성할때 minor 번호에 따라서 동작을 틀리게 해야할 경우가 있다.
구글을 검색한자료를 정리한다.
major / minor 얻기
일반적으로 device driver 의 open / close(release) operation 함수에서 얻을수있다.
예를들면...
static int ex_open(struct inode *inode, struct file *filp) { int mnr = iminor(inode); .... return 0; /* success */ } |
위와같이 인자로 넘어온 inode 구조체를 이용하면 쉽게 얻어올수있다.
write / read operation 함수에서..
minor / major 번호를 얻어올수는 없을까?
일단 write 함수의 type 을보면..
static ssize_t ex_write(struct file *filp, const char *buff, size_t len, loff_t * off { } |
위의 인자에서 minor 나 major 번호등을 얻어오긴 힘든것 같다.
(file 구조체에서 얻어오려고 구글링을 이리저리 해봤지만 방법은 없는듯 ㅠㅠ)
그럼 방법은 없는가? : private_data 를 활용하자.
file 구조체에는 private_data 라는 void 형태의 포인터를 저장할수있다.
예를들면...
static int ex_open(struct inode *inode, struct file *filp) { int mnr = iminor(inode); struct fo *fodp = &fodevs[mnr]; fodevs[mnr]->minor = mnr; /* store which fanout device in the file's private data */ filp->private_data = (void *) fodp; return 0; /* success */ } |
위와같이 활용이 가능하다.
즉, private_data 에 연결된 fo 구조체에 open 시 넘어온 major / minor 를 저장한다는 것이다.
read / write 시에...minor / major 를 얻어오기
간단하게 예를 들면 다음과같다.
static ssize_t ex_write(struct file *filp, const char *buff, size_t len, loff_t * off) { int mnr = ((struct fo *)filp->private_data)->minor; copy_from_user(ex_buff, buff, len); // ... 생략... return len; } |
open 시에 private_data 쪽에 저장한 minor 번호를 갖고올수있다는 것이다.
소스 분석을 하다보니...
open 할때마다 (minor 가 다른 file description.. ) 커널에서 device driver 를 위한 context 를 새로 만들기 때문에...
위와같은 방식 으로 사용가능한 것 이다.
반응형