COMP 3438 – Part I - Lecture 5 Character Device Drivers

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Linux device-driver issues
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
RT_FIFO, Device driver.
Sogang University Advanced Operating Systems (Linux Device Drivers) Advanced Operating Systems (Linux Device Drivers) Sang Gue Oh, Ph.D.
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 - /
Computer System Laboratory
USERSPACE I/O Reporter: R 張凱富.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
52 Advanced Operating Systems Writing Device Drivers.
Embedded Systems Programming Writing Device Drivers.
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
Device Drivers In Linux © Gregory Kesden Fall 2000.
M. Muztaba Fuad Advanced Operating System Project Device Drivers.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
Therac-25 Computer-controlled radiation therapy machine
Operating System Program 5 I/O System DMA Device Driver.
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Modules and Device Drivers (ESP – Fall 2014) Computer Science & Engineering Department.
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
COMP 3438 System Programming
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture 2: Linux devices Roei Ben-Harush Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
UNIX Files File organization and a few primitives.
Dynamic memory allocation and Pointers Lecture 4.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
OS2014 PROJECT 2 Supplemental Information. Outline Sequence Diagram of Project 2 Kernel Modules Kernel Sockets Work Queues Synchronization.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
K ERNEL D EVELOPMENT CSC585 Class Project Dawn Nelson December 2009.
LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate.
Linux Device Driver 2009/04/08. Reference Book Another Reference Book Embedded Linux Primer: A Practical, Real-World Approach By Christopher Hallinan.
Interfacing Device Drivers with the Kernel
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 The File System. 2 Linux File System Linux supports 15 file systems –ext, ext2, xia, minix, umsdos, msdos, vfat, proc, smb, ncp, iso9660, sysv, hpfs,
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
Finish up OS topics Group plans. Today Finish up and review Linux device driver stuff – Walk example again – See how it all goes together – Discuss talking.
Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 4: Managing File System State Dr. Xiao Qin Auburn University.
COMP 3438 – Part II - Lecture 3 Lexical Analysis II Par III: Finite Automata Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Linux Device Model A device model after 2.5
Device Driver_Skeleton
Lecture 3 Module Programming and Device Driver (Homework#1 included)
Linux Kernel Module Programming
Computer System Laboratory
Linking & Loading.
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
Memory Allocation CS 217.
chapter 3-Char Device Driver
Linking & Loading CS-502 Operating Systems
Implementation of Embedded OS
Computer System Laboratory
Loadable Kernel Modules
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Presentation transcript:

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

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)

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)

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

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 251 0 Char device minor number major number File Name

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

Task 1: Implement functionalities based on the generic interface

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.

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

“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 *); }

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

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

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

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

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.

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

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

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

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

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

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

“Hello World” Device Driver (Cont.)

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

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.

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 251 1 User programs access devices through file operation system calls int fd; fd = open(“/dev/lab1”, O_RDONLY);

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

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.