Linux Device Driver Self Study
1. 목적 ¶이 문서는 유영창님의 리눅스 디바이스 드라이버란 책을 토대로 제가 공부한 결과를 정리한 문서입니다. 책의 요약보다는 각 장별 제가 배우고 느낀점을 정리하는 데 초점을 둘 것입니다.
2.2.1.1. 커널에 대한 개괄적인 설명 ¶
2.2.1.2. 디바이스 드라이버 ¶
2.2.2.3. 디바이스 파일 관련 함수 ¶
2.2.2.4.1. port.c ¶
/*
* Test /dev/port device file.
* Output data to print port that is located 0x378
* It connected 8-LED.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd; // File Descripter
int lp; // Loop Counter
unsigned char buff[1]; // Buffer for write
fd = open("/dev/port", O_RDWR); // open /dev/port in read & write mode
if(fd < 0) // open error, fd must be unsigned.
{
perror("/dev/port open error");
}
while(1) // Infinite loop. It must be terminated manually.
{
for(lp = 0; lp < 8; lp++) // loop 8th times.
{
lseek(fd, 0x378, SEEK_SET); // Define file pointer is 0x378 that is parallel port.
buff[0] = (0x01 << lp); // shift lp times. LED will be shifted.
write(fd, buff, 1); // Out to print port.
sleep(1); // delay some time. Maybe 1 second?
}
}
close(fd);
return 0;
}
2.2.2.4.2. lp.c ¶
/*
* test ioctl() by using /dev/lp0.
* /dev/lp0 can control printer port. It is used to check printer status.
* 13 Pin -> LP_PSELECD
* 12 Pin -> LP_POUTPA
* 10 Pin -> LP_PACK
* If button is pushed, they are connected to ground.
* So logic value is change to zero.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/lp.h>
int main(int argc, char **argv)
{
int fd; // file descripter.
int prnstate; // save state of print port.
int lp; // loop counter.
fd = open("/dev/lp0", O_RDWR | O_NDELAY); // open read & write & no_delay.
if(fd < 0)
{
perror("open error");
exit(1);
}
while(1)
{
ioctl(fd, LPGETSTATUS, &prnstate); // load print status to prnstate.
/* To get status of printer,
* We define parameter of ioctl() is LPGETSTATUS. */
if((prnstate & LP_PSELECD) == 0) // LP_PSELECD is 13 Pin. It is connected GND.
printf("LP_PSELECK\n");
else if((prnstate & LP_POUTPA) == 0)
printf("LP_POUTPA\n");
else if((prnstate & LP_PACK) == 0)
printf("LP_PACK\n");
else
printf("ON\n");
usleep(50000);
}
close(fd);
return 0;
}
2.2.3.1. 모듈 ¶
2.2.3.2. 모듈 소스 형식 ¶
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h>
module_init(hello_init); // 사용자가 임의로 지정할 수 있다. module_exit(hello_exit); 2.2.3.3. 모듈 매개변수 지정 ¶
#define <linux/moduelparam.h> static int onevalue = 1; static char *twostring = NULL; module_param(onevalue, int, 0); module_param(twosrting, charp, 0);
2.2.3.4. 커널 메시지 출력 ¶
2.3. 2006-03-25 ¶
2.4.1.1. 변수 ¶
2.4.2.1. 디바이스 드라이버 제어 방식 ¶
2.4.3.1. 초기화와 종료처리 ¶
2.4.4.1. Makefile ¶
obj-m := test.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
test:
echo $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
2.4.4.2. kmalloc() test ¶
|
The wise shepherd never trusts his flock to a smiling wolf. |
||||||||||||||||||||||||||||||||||||||||||||||||||










