device drivers tsp
DESCRIPTION
TRANSCRIPT
Device Drivers in Linux T S PRADEEP KUMAR
Lecturer, SCS, VIT
2
Key points
• History• Why Linux• Linux Kernel• Device Drivers
3
History• Linux is a modern, free operating system
based on UNIX standards.
• First developed as a small but self-contained kernel in 1991 by Linus Torvalds, with the major design goal of UNIX compatibility.
• Collaboration by many users all around the world, corresponding almost exclusively over the Internet.
4
What Linux in Embedded?
5
Linux Kernel/System/Distribution
• Kernel– The OS Code that runs on privileged mode
• System– Essential system components, but runs in
user mode– Compilers, system libraries
• Linux Distribution– Precompiled and ready to install tools and
packages– Popular distributions: redHat, SuSE, Debain,
Mandrake,etc.
6
Red Hat Directory Tree
/b oot
/b in
/in c lu de
/lib ex e c
/lib
/lo ca l
/sb in
/sh are
/X 1 1 R 6
/u sr
/u ser1
/u ser2....
/h o m e /d ev /b in
/in it.d
/rc .d
/h ttpd
/x in e t.d
/e tc /sb in
/lo ck
/log
/lib
/m a il
/w w w
/var /tm p /root /p roc
/
8
User Space
• At any given time, the CPU executes ineither User Mode or Kernel Mode• User Space
– Cannot execute privileged instructions– Cannot access kernel code and data– Cannot directly access hardware resources– Shell or other applications like Open
Office,etc.
9
Kernel Space
• Full privileges, Full access• Any subroutines or functions
forming part of the kernel (modules and device drivers, etc) are considered to be part of kernel space.
10
Kernel Modules or Device Drivers
• Multiple Processes trying to access a particular hardware
• Executed as part of the system kernel
• Drivers are either part of the kernel or loaded as a module
11
Everything is File• You have probably heard the saying
that in UNIX, everything is a file.
• This is true, because ALL devices are provided as a file in the UNIX OS.
• The user can then perform basic file
operations on the device – read, write, open, close, ...
12
Prerequisites
• Basic knowledge of kernel compilation
• A good deal of programmingexperience in C under Linux
• the right techniques of data structures, like linked list is essential along with their data types.
13
Major & Minor Numbers
• Devices require unique identification in the system
• The major number identifies the type of device
• The minor number identifies any mode or subunit of the device.
14
Blocks & Characters• Devices within the kernel can be separated
between character and block devices.
• A block device is something that can host a filesystem such as a disk. A block device can only be accessed as multiples of a block, where a block is usually 1kB of data .
• A character device is one that can be accessed like a file. – This driver implements the open, close, read and
write system calls.– Examples: Console, Parallel Ports, Serial Ports, etc.
15
Device Drivers
Option 1• Build device driver into the kernel
– Advantage – Driver available at boottime– Disadvantage – My need to load drivers that
are rarely used
Option 2• Build device driver as a kernel module
– Advantage – Load When Needed– Advantage – Unload when not longer needed– Disadvantage – Potential attempts to load
“bad” modules into the kernel
16
Device Drivers as a Module• If a device is provided as a module, then
it must be registered and unregistered with the kernel.
• To register a device driver– register_chrdev()– register_blkdev()
• To unregister a device driver – unregister_chrdev() or– unregister_blkdev()
17
Accessing the Device
• Once created, the device is accessible through a standard interface.– Function pointers in a structure.
• Two types of structures typically exist,
one for character devices and one for block devices.
18
Device Driver Events
Events User Functions Kernel Functions
Load Module insmod init_module()
Open Device fopen file_operations:open
Read Device fread file_operations:read
Write Device fwrite file_operations:write
Close Device fclose file_operations:release
Remove Device rmmod Cleanup_module()
19
Demos• To list currently loaded kernel modules
– /sbin/lsmod• Example character device name
– ls l /dev/lp0• Example block device name
– ls l /dev/hda• Show list of registered character/block
devices– cat /proc/devices
• Show list of network interfaces– /sbin/ifconfig a
20
Hello World Device Driver#include <linux/module.h>#include <linux/kernel.h>MODULE_LICENSE(“GPL”);static int major=0;static struct file_operations fops={};
static int init_module(void) { printk("hello, world\n"); major=register_chrdev(major,”mychr”,&fops);printk(“major=%d\n”,major);return 0; }
21
static void cleanup_module(void){
unregister_chrdev(major,”mychr”);printk(“Bye”);
}
22
Makefile (2.6 Kernel)
• objm := hellokm.o• KDIR := /lib/modules/$(shell uname r)• PWD := $(shell pwd)• default:
make C $(KDIR) SUBDIRS=$(PWD) modules
23
Compilation (2.4 KERNEL)
root# gcc -c hello.croot# insmod ./hello.oHello, worldroot# rmmod helloGoodbye cruel worldroot#
24
Thank you