Linux Device Drivers

Similar documents
Character Device Drivers

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

Character Device Drivers One Module - Multiple Devices

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Kernel Modules. Kartik Gopalan

Introduction Reading Writing scull. Linux Device Drivers - char driver

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

Linux Kernel Modules & Device Drivers April 9, 2012

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

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

Device Drivers. CS449 Fall 2017

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

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

Finish up OS topics Group plans

Designing and developing device drivers. Coding drivers

MP3: VIRTUAL MEMORY PAGE FAULT MEASUREMENT

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

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

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

7.4 Simple example of Linux drivers

Linux Kernel Module Programming. Tushar B. Kute,

Unix (Linux) Device Drivers

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

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

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

Linux drivers - Exercise

Concurrency and Race Conditions (Linux Device Drivers, 3rd Edition (

CS 378 (Spring 2003)

Interrupt handling. Interrupt handling. Deferred work. Interrupt handling. Remove an interrupt handler. 1. Link a struct work to a function

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

Simple char driver. for Linux. my_first.c: headers. my_first.c: file structure. Calcolatori Elettronici e Sistemi Operativi.

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

University of Texas at Arlington. CSE Spring 2018 Operating Systems Project 4a - The /Proc File Systems and mmap. Instructor: Jia Rao

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

ASE++ : Linux Kernel Programming

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

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

Programming the GPMC driver

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

Loadable Kernel Module

Make Your Own Linux Module CS 444/544

Operating systems for embedded systems. Embedded Operating Systems

CPSC Tutorial 17

Introduction to Device Drivers Part-1

Operating systems for embedded systems

Linux Kernel Development (LKD)

Linux Device Driver. Analog/Digital Signal Interfacing

Kernel. Kernel = computer program that connects the user applications to the system hardware Handles:

Advanced Operating Systems #13

BLOCK DEVICES. Philipp Dollst

Kthreads, Mutexes, and Debugging. Sarah Diesburg CS 3430 Operating Systems

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

Hunting Down Data Races in the Linux Kernel

7.3 Simplest module for embedded Linux drivers

The Note/1 Driver Design

Developing Real-Time Applications

Embedded data structures and lifetime management

ECE 650 Systems Programming & Engineering. Spring 2018

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

In Java we have the keyword null, which is the value of an uninitialized reference type

File Systems 1. File Systems

File Systems 1. File Systems

Concurrency Aspects of Project 2

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Linux Synchronization Mechanisms in Driver Development. Dr. B. Thangaraju & S. Parimala

CPSC 8220 FINAL EXAM, SPRING 2016

Table of Contents. Preface... xi

Formal Verification of Linux Device Drivers

Operating System Labs. Yuanbin Wu

Loadable Kernel Modules

Sample Mid-Term Exam 2

Driving Me Nuts Device Classes

PRINCIPLES OF OPERATING SYSTEMS

Final Step #7. Memory mapping For Sunday 15/05 23h59

Processes. Johan Montelius KTH

File Systems: Consistency Issues

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

VFS, Continued. Don Porter CSE 506

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

A process. the stack

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

Linux Device Driver in Action (LDDiA)

UNIX File System. UNIX File System. The UNIX file system has a hierarchical tree structure with the top in root.

NPTEL Course Jan K. Gopinath Indian Institute of Science

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

Input & Output 1: File systems

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

MaRTE OS Misc utilities

Files and File Systems

CSC 1600 Memory Layout for Unix Processes"

Short Notes of CS201

Finding User/Kernel Pointer Bugs with Type Inference p.1

Basic OS Progamming Abstrac7ons

Linux Kernel Development (LKD)

CS201 - Introduction to Programming Glossary By

Files and Directories Filesystems from a user s perspective

Basic OS Progamming Abstrac2ons

Disk divided into one or more partitions

Transcription:

Linux Device Drivers

Modules A piece of code that can be added to the kernel at runtime is called a module A device driver is one kind of module Each module is made up of object code that can be dynamically linked to the running kernel Dynamic linking done using insmod program Unlinking done using rmmod program Keep kernel small

Character Devices Char device drivers stream of bytes (sequential access) open, close, read, write E.g. console, serial ports Block device drivers buffering

Character Device Drivers Char devices are accessed through nodes of the filesystem tree located in the /dev directory Special files for char drivers are identified by c in the first character of the ls -l listing in /dev crw--w---- 1 root tty 4, tty40

Example Character Device Scull: Simple Character Utility for Loading Localities (scull) A memory based device Does not connect to any real device

Character Device Driver: Scull User User-level Programs System Calls Kernel Read Scull Device Driver Write Device: Memory Area Scull devices are persistent; can be shared

Device Numbers Major number Identifies the driver associated with the device Available in: /proc/devices Minor number Used by the Kernel to determine exactly which device is being referred to Idea: many devices can share the same driver e.g. many terminals might share the same driver

dev_t type Device Numbers Used to hold device numbers Major and minor parts 32 bit (12 bits for major number, 20 bits for minor number) Macros To obtain the major or minor parts of a dev_t MAJOR(dev_t dev); MINOR(dev_t dev); To convert major and minor numbers into dev_t MKDEV(int major, int minor);

Device Major Number: Static Allocation int register_chrdev_region (dev_t first, unsigned int count, char *name) first: beginning device number of the range you would like to allocate count: total device numbers (minor) you are requesting (will be 1 for us) name: name of the device that should be associated with this range

Device Major Number: Dynamic Allocation ** int alloc_chrdev_region (dev_t *dev, unsigned int firstminor, unsigned int count, char *name) dev: output parameter; on successful completion, holds the first number in your allocated range firstminor: requested first minor number to use; usually 0 count: total number of contiguous device numbers (minor) you are requesting name: name of the device that should be associated with this number range

Example of Device Number Allocation extern int scull_major; // auto allocation => 0 extern int scull_minor; // assume this is 0 if (scull_major) { dev = MKDEV(scull_major, scull_minor); result = register_chrdev_region (dev, scull_nr_devs, scull ); } else { result = alloc_chrdev_region (&dev, scull_minor, scull_nr_devs, scull ); scull_major = MAJOR(dev); }

Device Driver Life-cycle Stage 1: Registration and Initialization module_init (called when insmod is invoked) Stage 2: Serving requests from user-space programs open, read, write, close, lseek Stage 3: De-registration and clean-up module_exit (called when rmmod is invoked) Hello World

#include <linux/init.h> #include <linux/module.h> static char *chararg = "foo"; static int intarg = 25; /* declare that intarg and chararg are args to the module and list their types and permissions */ module_param (intarg, int, S_IRUGO); module_param (chararg, charp, S_IRUGO); /* module initialize function */ static int hello_init(void) { printk (KERN_INFO "HelloWorld: You passed: %d and %s\n", intarg, chararg); } /* module remove function */ static void hello_exit(void) { printk (KERN_INFO "HelloWorld: So long and thanks for all the fish..\n"); } /* specify the module init and remove functions */ module_init(hello_init); module_exit(hello_exit); root# insmod./hello.ko HelloWorld: You passed 25 and foo root# rmmod hello HelloWorld: So long and thanks for all the fish..

Important Data Structures struct file This structure is created every time a file/dev is opened. It is maintained while the file is open struct inode An inode is maintained for each file/dev; contains pointers to the device structure (cdev) struct cdev the char device; contains a pointer to the file operations structure struct file_operations contains pointers to functions for device interface functions struct your_device contains state, storage, and cdev

struct file_operations struct file_operations scull_fops = {.llseek = scull_llseek,.read = scull_read,.write = scull_write,.ioctl = scull_ioctl,.open = scull_open,.release = scull_release, } User code: fd = open ( /dev/scull0, ); read (fd, );

struct file Some important fields: open file struct file_operations *fops The operations associated with the file void *private_data (~ device-specific data) Useful resource for preserving state information across system calls struct file: filp *f_op *private_data struct file_operations struct your_device

Scull Device struct scull_dev { // up to you (i.e.. struct your_device) data, bookkeeping, buffers, struct semaphore sem; struct cdev cdev; } struct cdev is Kernel s internal structure that represents char devices The scull device driver needs to initialize this structure, initialize the cdev structure and register cdev with the Kernel

Passed to open function struct inode Some important fields dev_t i_rdev For inodes of device files, this field contains the actual device number struct cdev *i_cdev struct cdev is Kernel s internal structure that represents char devices container_of: from i_cdev => *struct your_device

Char Device Registration Kernel uses structures of type struct cdev to represent char devices internally Before Kernel can invoke device s operations, we must do the following 1. Set the file_operations pointer inside this structure 2. Allocate and register one or more such structures

Char Device Registration void cdev_init (struct cdev *cdev, struct file_operations *fops) int cdev_add (struct cdev *dev, dev_t num, unsigned int count); count: #of device numbers (usually, this is 1) Device is now live

Status after Char Device Registration User Space File System Kernel Space struct inode cdev_add scull_dev cdev Scull Device Driver

Handling of open call User Space open File System Kernel Space open struct inode scull_open (struct inode *inode, struct file *filp) Scull Device Driver scull_dev cdev

Conceptual View Process A after registration scull module scull_devices scull0 scull1 scull2 inode Buffer Mutex Semaphores cdev inode Buffer afmutex Semaphores cdev inode Buffer Mutex Semaphores cdev open file sets private_data file_operations

Open and release open (*inode, *filp) setup filp->private_data for subsequent methods device-specific initialization release (*inode, *filp) // close device-specific dealloc / release resources

Read and write read (*filp, *buff, count, *offp) write (*filp, *buff, count, *offp) returns: <0 on error; >= 0 is bytes transferred buff user space pointer copy_to_user (toaddr, fromaddr) copy_from_user (toaddr, fromaddr)

Closer look at read...

Allocating memory in the kernel kmalloc (size, GFP_KERNEL) similar to malloc memory is not cleared kfree (memptr) allocate buffers within your device

Synchronization Block processes calling your device Semaphores sema_init (*sem, val) down (*sem), down_interruptible, down_trylock up (*sem) WaitQueues init_waitqueue_head() wait_event(), wait_event_interruptible()... wake_up(), wake_up_interruptible()