Linux Device Driver
for Dummies
2016/2
• What’s device driver?
• Setup
• Hello, device driver world!
• The next steps to driver developer
What is device driver?
• 





OS
Application
Hardware
device driver
•
•
•
• 

…
• API
• printf() API only C
•
• ➡
Setup
•
• α
•
•
yum kernel-devel, kernel-headers
apt-get linux-headers-generic, linux-source
Hello, device driver world!
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void) {
printk(KERN_ALERT "driver loaded.n");
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT "driver unloaded.n");
}
module_init(hello_init);
module_exit(hello_exit);
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void) {
printk(KERN_ALERT "driver loaded.n");
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT "driver unloaded.n");
}
module_init(hello_init);
module_exit(hello_exit);
• stdio.h, iostream
#include <linux/module.h>
#include <linux/init.h>
•


MODULE_LICENSE("Dual BSD/GPL");
$ insmod hello.ko
$ dmesg | tail -1
[ XX.XXX] hello: module verification failed: signatureand/or required key missing -
tainting kernel
module_init()/exit()
•
• insmod/rmmod
• main()
•
module_init(hello_init);
module_exit(hello_exit);
.ko insmod, rmmod
.ko Makefile
• .ko Makefile
• -C dir 

= make
obj-m := hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) clean
•
• KBuild/
KConfig
$ insmod hello.ko
$ dmesg | tail -1
[ XX.XXX] driver loaded
$ rmmod hello.ko
$ dmesg | tail -1
[ XX.XXX] driver unloaded
The next steps to driver
developer
…
mknod
struct file_operations
copy_{from/to}_user()
•
• mknod
$ ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 2 29 16:39 /dev/ttyS0
• Linux
• MMU(Memory Management Unit)
•
• struct map_desc
•
Primula arch/arm/mach-epson13/epson13.c
•
•
ARM memcpy()
unsigned long copy_from_user(void *to, const void __user *from,
unsigned long n)
unsigned long copy_to_user(void __user *to, const void *from,
unsigned long n)
try! linux device driver

Linux device driver for dummies