Unix (Linux) Device Drivers

Similar documents
Virtual File System (VFS) Implementation in Linux. Tushar B. Kute,

CS5460/6460: Operating Systems. Lecture 24: Device drivers. Anton Burtsev April, 2014

Linux Device Drivers. 3. Char Drivers. 1. Introduction 2. Kernel Modules 3. Char Drivers 4. Advanced Char Drivers 5. Interrupts

CS 378 (Spring 2003)

Step Motor. Step Motor Device Driver. Step Motor. Step Motor (2) Step Motor. Step Motor. source. open loop,

Designing and developing device drivers. Coding drivers

Linux drivers - Exercise

Operating System Labs. Yuanbin Wu

Linux Loadable Kernel Modules (LKM)

USB. Development of a USB device driver working on Linux and Control Interface. Takeshi Fukutani, Shoji Kodani and Tomokazu Takahashi

Finish up OS topics Group plans

Kernel Modules. Kartik Gopalan

Character Device Drivers

RF-IDs in the Kernel -- Episode III: I want to File Away

NPTEL Course Jan K. Gopinath Indian Institute of Science

Operating Systems II BS degree in Computer Engineering Sapienza University of Rome Lecturer: Francesco Quaglia. Topics: 1.

Device Drivers. CS449 Fall 2017

Introduction Reading Writing scull. Linux Device Drivers - char driver

Linux Device Driver. Analog/Digital Signal Interfacing

Loadable Kernel Module

Linux Device Drivers. 3. Char Drivers cont. 3. Char Drivers. 1. Introduction 2. Kernel Modules 3. Char Drivers 4. Advanced Char Drivers 5.

Linux Kernel Module Programming. Tushar B. Kute,

Linux Device Drivers

toorcon 2004 Hooking the Linux ELF Loader Richard Johnson

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Introduction to Device Drivers Part-1

FAME Operatinf Systems - Modules

The Virtual Filesystem

The device driver (DD) implements these user functions, which translate system calls into device-specific operations that act on real hardware

Workspace for '5-linux' Page 1 (row 1, column 1)

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Disk and File System

Linux Kernel Modules & Device Drivers April 9, 2012

/dev/hello_world: A Simple Introduction to Device Drivers under Linux

7.4 Simple example of Linux drivers

NPTEL Course Jan K. Gopinath Indian Institute of Science

CS 423 Operating System Design: Introduction to Linux Kernel Programming (MP1 Q&A)

3 Character Drivers. 3.1 The Design of scullc. Linux Device Drivers in Assembly

Device Drivers Demystified ESC 117. Doug Abbott, Principal Consultant Intellimetrix. Why device drivers? What s a device driver?

Make Your Own Linux Module CS 444/544

Embedded Systems. Prof. Myung-Eui Lee (A-405) 1-1. Embedded Systems KUT

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Linux modules 1. Support system calls and services 2. Programming facilities 3. Kernel probing

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Virtual Machine Introspection Bhushan Jain

Operating System Concepts Ch. 11: File System Implementation

A brief introduction to C programming for Java programmers

CS 378 (Spring 2003)

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

7.3 Simplest module for embedded Linux drivers

CS 0449 Project 4: /dev/rps Due: Friday, December 8, 2017, at 11:59pm

CSE 333 SECTION 3. POSIX I/O Functions

Android 多核心嵌入式多媒體系統設計與實作

ECEN 449: Microprocessor System Design Department of Electrical and Computer Engineering Texas A&M University

PFStat. Global notes

I/O MANAGEMENT CATEGORIES OF I/O DEVICES 1. Slide 1. Slide 3. Controller: Three classes of devices (by usage):

Software Layers. Device Drivers 4/15/2013. User

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Abstraction via the OS. Device Drivers. Software Layers. Device Drivers. Types of Devices. Mechanism vs. Policy. Jonathan Misurda

CS 453: Operating Systems Programming Project 5 (100 points) Linux Device Driver

Files. Eric McCreath

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

What is a Linux Device Driver? Kevin Dankwardt, Ph.D. VP Technology Open Source Careers

Computer Network Terms Common functions and devices in computer networks Structure of computer networks Many examples with some detailed information

Introduction to the Linux Kernel. Hao-Ran Liu

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Linux Device Driver in Action (LDDiA)

Developing Real-Time Applications

CPSC Tutorial 17

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

Programming. Data Structure

Networking Subsystem in Linux. Manoj Naik IBM Almaden Research Center

Chapter 12 IoT Projects Case Studies. Lesson-01: Introduction

CPSC 8220 FINAL EXAM, SPRING 2016

Motivation Process Need. Distributed Computing Systems. Motivation Disk Functionality (1 of 2) Motivation Disk Functionality (2 of 2)

CSE 333 SECTION 3. POSIX I/O Functions

MaRTE OS Misc utilities

AutoISES: Automatically Inferring Security Specifications and Detecting Violations

PMON Module An Example of Writing Kernel Module Code for Debian 2.6 on Genesi Pegasos II

Input & Output 1: File systems

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

The course that gives CMU its Zip! I/O Nov 15, 2001

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Section 3: File I/O, JSON, Generics. Meghan Cowan

The Linux Kernel Module Programming Guide

File I/O and File Systems

Introduction to Linux Device Drivers Recreating Life One Driver At a Time

Design and Implementation of an Asymmetric Multiprocessor Environment Within an SMP System. Roberto Mijat, ARM Santa Clara, October 2, 2007

T4-Input/Output Management

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

Character Device Drivers One Module - Multiple Devices

Linux Device Drivers Interrupt Requests

Concurrency Aspects of Project 2

Introduction to Operating Systems. Device Drivers. John Franco. Dept. of Electrical Engineering and Computing Systems University of Cincinnati

FILE SYSTEMS. Jo, Heeseung

Tutorial 2. Linux networking, sk_buff and stateless packet filtering. Roei Ben-Harush Check Point Software Technologies Ltd.

Embedded System Software I

Systems Programming. 09. Filesystem in USErspace (FUSE) Alexander Holupirek

Chapter 4. File Systems. Part 1

Distribution Kernel Security Hardening with ftrace

ECEN 449 Microprocessor System Design. Review of C Programming

Transcription:

Unix (Linux) Device Drivers Kernel module that handles the interaction with an specific hardware device, hiding its operational details behind a common interface Three basic categories Character Block Network December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 115

Kernel Overview: LINUX December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 116

Hardware Devices Accessible via /dev pseudo-files Kernel redirect pseudo-file operations to proper device driver services considering major and minor numbers Major Identifies a driver within the kernel (8 bits) Minor Identifies a device (unit) within the driver December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 117

Char Devices Byte streams (e.g. /dev/console, /dev/ttys0, /dev/st0) Operate mostly like ordinary files No backward seeks December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 118

Block Devices Block-accessible devices at I/O level File system related devices (e.g. disks) Share a common interface with char devices, but distinct semantics Block oriented (accessing single bytes is a waste) Seekable Additional operations to support file systems December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 119

Net Devices Do not fit properly under the pseudo-file interface Usually not a node in a file system Integration with a protocol stack Generic network interface instead Communication related operations (e.g. sending, receiving, package marshaling, time-out handling, statistic collection) Optimized for TCP/IP integration December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 120

Hello World Module [root]#cat > hello.c #define MODULE #include <linux/module.h> int init_module(){printk( Hello World! ); return 0;} void cleanup_module(){printk( Good Bye! );} ^D [root]# gcc -c hello.c [root]# insmod hello.o [root]# dmesg [root]# rmmod hello [root]# dmesg December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 121

Module Initialization Initialization int init_module(void) Module's entry point Called at loading (by insmod) Performs module registration Finalization void cleanup_module(void) Module's exit point Called at unloading (by rmmod) Performs module unregistration December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 122

Module Registration Binds a module to the kernel's syscall interface Registration int register_chrdev(unsigned int major, const char *name, struct file_operations *fops) Unregistration int unregister_chrdev(unsigned int major, const char *name) Pseudo file mknod /dev/devname0 c major minor December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 123

Module Parameters Externally accessible module-global variables Declared via MODULE_PARM macro int irq = 10; char * name = Unknown ; MODULE_PARM(irq, i ); /* declare irq as int */ MODULE_PARM(name, s ); /* declare name as string */ Defined at load time insmod mod.o irq=9 name= The Server December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 124

Module Info Externally visible module declarations used to supply clients with some useful information Macros MODULE_AUTHOR("Somebody"); MODULE_DESCRIPTION("This module doesn't do anything"); MODULE_PARM_DESC(irq, "Device IRQ (3/4)" December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 125

struct file_operations struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char *, size_t,... ssize_t (*write) (struct file *, const char *,... int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct... int (*ioctl) (struct inode *, struct file *,... int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *,... int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); : : }; December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 126

struct file struct file { struct list_head f_list; struct dentry *f_dentry; struct vfsmount *f_vfsmnt; struct file_operations *f_op; atomic_t f_count; unsigned int f_flags; mode_t f_mode; loff_t f_pos; unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin; struct fown_struct f_owner; unsigned int f_uid, f_gid; int f_error; : }; MINOR(f_dentry->d_inode->i_rdev) December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 127

Module's Reference Counter Automatically tracks how many clients a module has at a moment Avoids unloading a module that is being used by a client Manipulated by macros MOD_INC_USE_COUNT MOD_DEC_USE_COUNT MOD_IN_USE December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 128

Programming Hits No standard libraries (and headers) printk instead of printf #include <linux/x.h> #include <asm/y.h> Signalize kernel code #define KERNEL Avoid name-clashes Local symbols (static) Prefixed symbols (mod_sym) EXPORT_NO_SYMBOLS; December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 129

More Programming Hits Kernel code runs within the context of calling user process #include <asm/uaccess.h> unsigned long copy_to_user(to, from, count); unsigned long copy_from_user(to, from, count); In-kernel memory allocation #include <linux/malloc.h> void *kmalloc(unsigned int size, int priority); void kfree(void *obj); December 20, 2008 Antônio Augusto Fröhlich (http://www.lisha.ufsc.br) 130