+ All Categories
Home > Education > Device drivers tsp

Device drivers tsp

Date post: 07-Nov-2014
Category:
Upload: pradeep-kumar-ts
View: 3,331 times
Download: 2 times
Share this document with a friend
Description:
 
Popular Tags:
24
Device Drivers in Linux T S PRADEEP KUMAR Lecturer, SCS, VIT
Transcript
Page 1: Device drivers tsp

Device Drivers in Linux T S PRADEEP KUMAR

Lecturer, SCS, VIT

Page 2: Device drivers tsp

2

Key points

• History• Why Linux• Linux Kernel• Device Drivers

Page 3: Device drivers tsp

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.

Page 4: Device drivers tsp

4

What Linux in Embedded?

Page 5: Device drivers tsp

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.

Page 6: Device drivers tsp

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

/

Page 8: Device drivers tsp

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.

Page 9: Device drivers tsp

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.

Page 10: Device drivers tsp

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

Page 11: Device drivers tsp

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, ...

Page 12: Device drivers tsp

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.

Page 13: Device drivers tsp

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.

Page 14: Device drivers tsp

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.

Page 15: Device drivers tsp

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

Page 16: Device drivers tsp

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()

Page 17: Device drivers tsp

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.

Page 18: Device drivers tsp

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()

Page 19: Device drivers tsp

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

Page 20: Device drivers tsp

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; }

Page 21: Device drivers tsp

21

static void cleanup_module(void){

unregister_chrdev(major,”mychr”);printk(“Bye”);

}

Page 22: Device drivers tsp

22

Makefile (2.6 Kernel)

• objm := hellokm.o• KDIR := /lib/modules/$(shell uname r)• PWD := $(shell pwd)• default:

make C $(KDIR) SUBDIRS=$(PWD) modules

Page 23: Device drivers tsp

23

Compilation (2.4 KERNEL)

root# gcc -c hello.croot# insmod ./hello.oHello, worldroot# rmmod helloGoodbye cruel worldroot#

Page 24: Device drivers tsp

24

Thank you


Recommended