Linux Device Driver Self Study
Contents
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. 2006-04-06 ¶
2.4.1.1. º¯¼ö ¶
2.4.1.3. ¸Þ¸ð¸® Ç® ¶
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 ¶
|
Words must be weighed, not counted. |