Linux Driver
Linux Driver
DRIVERS IN LINUX
HCMUTE – Faculty of Mechanical Engineering
Lecturer: PhD. Bui Ha Duc
Outline
• Introduction to Device driver
Driver
HARDWARE
Device Driver
• Device driver is a computer program
that operates or controls a
particular type of device that is
attached to a computer
• Driver provides a software interface
to hardware devices, enabling
operating systems and other
computer programs to access
hardware functions.
Functions of Driver
• Isolate the user program from the complexity of
the hardware device
• E.g. open, copy a file in hard disk
GPIO 17
Device files
Device driver
Hardware
• Device Name:
/dev/gpio “gpio” command
• /dev directory contains the special device files for all the
devices
10
Notes:
• No main() function
• No built-in C function
• 2 fundamental function
• module_init()
• module_exit()
• printk : print messeage to kernel log files
Compile a Driver
• A device driver must be compiled against the kernel on
which it will execute
• Method 1: Create a Make file and put it in the same folder
with file hello.c
obj-m := hello1.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
Gọi driver
buttons_fd = open(“/dev/buttons”,0)
Đọc trạng thái nút nhấn
read(buttons_fd,current_buttons,sizeof(current_buttons)
Đóng driver
close(buttons_fd)
Đóng file driver
linux-2.6.32.2/drivers/char/mini2440_buttons.c
31