Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 3438 – Part I - Lecture 5 Character Device Drivers

Similar presentations


Presentation on theme: "COMP 3438 – Part I - Lecture 5 Character Device Drivers"— Presentation transcript:

1 COMP 3438 – Part I - Lecture 5 Character Device Drivers
Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.

2 Course Organization (This lecture is in red)
Part I: Unix System Programming (Device Driver Development) Part II: Compiler Design Character Device Driver Development Introduction to Block Device Driver Syntax Analysis Overview of Device Driver Development Lexical Analysis Process File System Overview of Complier Design Overview of Unix Sys. Prog. Overview of the Subject (COMP 3438)

3 Outline Lecture 4: Introduction to Device Drivers
1. What is a device driver? 2. Related kernel data structures from a generic view 3. Devices and Files 4. Major design issues 5. Types of device drivers Lecture 5-I: Char device driver I A simple device driver – “Hello world” Lecture 5-II: Char device driver II A device driver for LED Lecture 6: Block Device Driver A device driver for RAM Disk Lecture 5-II: Char device driver III Interrupt-driven driver (Button)

4 The Overview of Device Driver Development
In Unix, two generic interfaces are provided for device driver development for user programs and kernel User programs: devices are treated as special files and users can only access devices through file operation system call (such as open, close, read, write, etc.) just like accessing regular files Kernel: provide a generic interface and kernel routines for all device drivers to implement functionalities and register functions into the kernel data structures (such as char/block device driver tables).

5 The Interface for User Programs
Devices are treated as files User programs access devices through file operation system calls Each file has an inode, and each device driver has a major number. When using a device driver in user programs, we need to create a special file by associating its major number with an inode Command: mknod file_name c/b major_number minor_number e.g. mknod /dev/lab1 c Char device minor number major number File Name

6 The Device Driver Development (Kernel)
Two tasks Implement functionalities based on the generic interface Register the device driver into the kernel data structure (the char/block device driver table) The major number is the ID of a device driver

7 Task 1: Implement functionalities based on the generic interface

8 The Generic Interface for Char Device Driver in Linux
A data structure called file_operations defined in <linux/fs.h> which is an array of function points struct file_operations { struct module *owner; int (* open) (struct inode *, struct file *); ssize_t (* read) (struct file *, char *, size_t, loff_t *); ssize_t (* write) (struct file *, const char *, size_t, loff_t *); int (* release) (struct inode *, struct file *); } To develop a char driver, we need to implement corresponding functions for a device based on the above generic interface.

9 “Hello World” Device Driver
Header files include the prototypes of kernel routines and data structures needed in the program. Use “static”, the effective range of a variable is limited in the file containing it so we can avoid name pollution.

10 “Hello World” Device Driver (Cont.)
Implement functionalities of a device based on the generic interface defined in file_operations. struct file_operations { struct module *owner; int (* open) (struct inode *, struct file *); ssize_t (* read) (struct file *, char *, size_t, loff_t *); int (* release) (struct inode *, struct file *); }

11 “Hello World” Device Driver (Cont.)
copy_to_user( ) and copy_from_user( ): Implement data transfer between kernel space and user space

12 Kernel Space and User Space
Kernel space and user space are separated in Unix. Linux kernel 2.4 Virtual Address

13 Task 2: Register the device driver into the kernel data structure .

14 Dynamic Allocation of Major Numbers
First, obtain device numbers Dynamic allocation is preferred int alloc_chrdev_region (dev_t * dev, unsigned baseminor, unsigned count, const char * name); dev output parameter for first assigned number baseminor first of the requested range of minor numbers Count the number of minor numbers required Name the name of the associated device or driver

15 Dynamic Allocation of Device Numbers
int alloc_chrdev_region (dev_t * dev, unsigned baseminor, unsigned count, const char * name); dev output parameter for first assigned number baseminor first of the requested range of minor numbers Count the number of minor numbers required Name the name of the associated device or driver Descriptions: Allocates a range of char device numbers. The major number will be chosen dynamically, and returned (along with the first minor number) in dev. Returns zero or a negative error code.

16 Major and Minor From a device number (dev_t), we can obtain the major number and minor number. MAJOR (dev_t dev); MINOR (dev_t dev); e.g. static dev_t devno; static int major; alloc_chrdev_region(&devno, 0, 1, “dev” ); major = MAJOR (devno);

17 Register Char Device Driver
The functions we implement for a device can be registered into the kernel by the kernel routines below: /*cdev_init — initialize a cdev structure*/ void cdev_init (struct cdev * cdev, const struct file_operations * fops); /*cdev_add — add a char device to the system*/ int cdev_add (struct cdev * p, dev_t dev, unsigned count);

18 cdev_init() – Initialize a cdev structure
Initialize a char device (link with fops) void cdev_init (struct cdev * cdev, const struct file_operations * fops); Arguments: cdev – the structure to initialize fops – the file_operations for this device

19 cdev_add() – Add a char device to the sys.
Add a char device to the system int cdev_add ( struct cdev * p, dev_t dev, unsigned count); Arguments: p – the cdev structure for the device dev – the first device number for which this device is responsible count – the number of consecutive minor numbers corresponding to this device

20 Remove Device/Release Device Numbers
/* cdev_del — remove a cdev from the system*/ void cdev_del (struct cdev * p); p – the cdev structure to be removed /* unregister_chrdev_region — return a range of device numbers*/ void unregister_chrdev_region (dev_t from, unsigned count); from – the first in the range of numbers to unregister count – the number of device numbers to unregister

21 “Hello World” Device Driver (Cont.)
The functions implemented based on the data structure file_operations The variable is used in chrdev_init( )

22 “Hello World” Device Driver (Cont.)

23 “Hello World” Device Driver (Cont.)
Load driver with corresponding function Unload driver with corresponding function

24 Compile and Load/Unload
Linux provides a dynamic module load/unload method for device driver management. Compile: Add module dependency relations into Makefile and Config.h Run command “make menuconfig” to set up the module as “dynamically load/unload” Run command “make dep” Run command “make modules” Load/Unload: insmod xxx.o rmmod xxx where xxx is the module name.

25 Test Devices are treated as files so we need to create a file for a device driver using command “mknod”: e.g. mknod /dev/lab1 c User programs access devices through file operation system calls int fd; fd = open(“/dev/lab1”, O_RDONLY);

26 Application Program #include <linux/types.h>
#include <linux/stat.h> #include <linux/fcntl.h> main() { int fd, count, i; char buf[100]; fd = open("/dev/lab1", O_RDONLY); count = read(fd, buf, 100); printf("count-%d\n", count); buf[count]='\0'; printf("%s", buf); }

27 Summary To generic interfaces are provided for using devices in Unix:
In the kernel, a generic interface is provided For user programs, devices are treated as files. To develop a device driver, basically we need to finish two tasks: Implement functionalities based on the generic interface Register the device driver into the kernel data structure (the char/block device driver table) A Simple device driver “Hello World” has been provided as an example for understanding the procedure of a char device driver.


Download ppt "COMP 3438 – Part I - Lecture 5 Character Device Drivers"

Similar presentations


Ads by Google