Computer System Laboratory

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

Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Computer System Laboratory
Lecture 12 Page 1 CS 111 Online Devices and Device Drivers CS 111 On-Line MS Program Operating Systems Peter Reiher.
Embedded Systems Programming Writing Device Drivers.
Embedded Real-time Systems The Linux kernel. The Operating System Kernel Resident in memory, privileged mode System calls offer general purpose services.
Tutorial and Demos on Linux Virtual Machine
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Computer System Laboratory
Copyright Arshi Khan1 System Programming Instructor Arshi Khan.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
COMPUTER SYSTEM LABORATORY Lab4 - Bootloader. Lab 4 Experimental Goal Learn how to build U-Boot bootloader for PXA /10/8/ 142.
Operating System Program 5 I/O System DMA Device Driver.
hardware and operating systems basics.
Interrupts and DMA CSCI The Role of the Operating System in Performing I/O Two main jobs of a computer are: –Processing –Performing I/O manage and.
OS provide a user-friendly environment and manage resources of the computer system. Operating systems manage: –Processes –Memory –Storage –I/O subsystem.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Lab 10 Department of Computer Science and Information Engineering National Taiwan University Lab10 – Debugging II 2014/12/2 1 /16.
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.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Processes Introduction to Operating Systems: Module 3.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
UNIX Unit 1- Architecture of Unix - By Pratima.
A. Frank - P. Weisberg Operating Systems Structure of Operating Systems.
Chapter 13 – I/O Systems (Pgs ). Devices  Two conflicting properties A. Growing uniformity in interfaces (both h/w and s/w): e.g., USB, TWAIN.
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.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
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.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
CSCE451/851 Introduction to Operating Systems
Introduction to Operating Systems Concepts
Computer System Structures
Input/Output (I/O) Important OS function – control I/O
By Ganesan Alagu Ganesh Feb 26, 2008
Computer System Laboratory
Device Driver_Skeleton
Operating System Overview
Computer System Laboratory
Implementation of Embedded OS
Computer System Laboratory
Chapter 13: I/O Systems Modified by Dr. Neerja Mhaskar for CS 3SH3.
Computer System Laboratory
By Ganesan Alagu Ganesh Feb 21, 2008
Linux Kernel Module Programming
Computer System Laboratory
Implementation of Embedded OS
Implementation of Embedded OS
Computer System Laboratory
Operating System Structure
Want to play a game? – Linux Kernel Modules
The PCI bus (Peripheral Component Interconnect ) is the most commonly used peripheral bus on desktops and bigger computers. higher-level bus architectures.
Implementation of Embedded OS
CS703 - Advanced Operating Systems
Chapter 2: System Structures
CS 6560 Operating System Design
Lecture Topics: 11/1 General Operating System Concepts Processes
Lab 4 Kernel Module Operating System Lab.
Implementation of Embedded OS
Computer System Laboratory
Computer System Laboratory
System Calls System calls are the user API to the OS
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Computer System Laboratory Lab7 - Driver / 19

Experimental Goal Understand the architecture of Linux device driver and kernel module. / 19

Environment Host Machine Target Machine Build Machine OS: Windows Raspberry Pi (2 or 3) Build Machine A computer with a SD card slot OS: Ubuntu 15.10 (or above) 64-bit / 19

Software Required Host Machine Build Machine PL2303 Driver PuTTY Build Machine A built kernel with its configuration and header files GCC Cross-Compiler targeting ARM DHT11 sensor driver sample code You may find all software on the CSL Course Software. / 19

Hardware Checklist Raspberry Pi Power supply Micro SD card and card reader USB-TTL cable / Network cable DHT11 sendor and 3 wires / 19

Introduction to Device Drivers What are device drivers? Make a particular piece of hardware respond to a well-defined internal programming interface. Hide completely the details of how the device works. User activities are performed by means of a set of standardized calls that are independent of the specific driver. Mapping those calls to device-specific operations that act on real hardware is then the role of a device driver. / 19

Device Driver Architecture user program /dev/XXX device driver physical device / 19

Physical vs. Logical A physical device usually has a corresponding logical device file (e.g. /dev/XXX). User programs operate the logical device file via system calls such as open(), read(), write(), and close(). Operations to the logical device file will cause a set of corresponding handlers (e.g. fops.open(), fops.read(), fops.write(), fops.release()) provided by the device driver being invoked to perform real operations to the physical device. A device driver has two layers: physical device driver (PDD) and logical device driver (LDD). PDD performs real operations to physical devices, and LDD implements the set of handlers using PDD. / 19

Writing a Logical Device Driver (fops) Define file_operations and implement handlers of system calls. #include <linux/fs.h> struct file_operations dev_fops = { .open = dev_open, .read = dev_read, .write = dev_write, .release = dev_close, }; Implement these functions / 19

Writing a Logical Device Driver (device) Define miscdevice and register a device file. #include <linux/miscdevice.h> struct miscdevice dev = { .minor = MISC_DYNAMIC_MINOR, .name = "dht11", .fops = &dev_fops, .mode = S_IRUGO | S_IWUGO, }; misc_register(&dev); Name of your device file System call handlers Permission of your device file / 19

Writing a Physical Device Driver This part of work depends on the specification of the physical device you are targeting to. Typically you need the datasheet of the physical device to learn how to interact with it via low level I/O operations. If some people already implement that PDD, usually you would like to copy-and- paste their work rather than reading the datasheet and implementing the PDD by yourself. At the lowest software layer, there are only two ways to perform I/O: port I/O and memory mapped I/O. Port I/O: Use special CPU instructions to access I/O ports, which are not memory address. Memory mapped I/O: Access special memory addresses, which are associated with I/O system rather than real RAM. / 19

Data Transfer between Kernel and User Arguments passing read(fd, buf, size); dev_read(file, buf, size, pos); Your dev_read() has to fetch data from physical device, and write the fetched data to user buffer “buf”. The fetched data exists in kernel space since the driver is in kernel space, but “buf” is a pointer points to user space. As the result, we need copy_to_user() to transfer data between kernel and user. #include <linux/uaccess.h> copy_to_user(user_pointer, kernel_pointer, size_to_copy); / 19

Introduction to Kernel Module A kernel module contains code that runs in kernel mode to extend the running kernel. It can be loaded/unloaded during runtime of the kernel. Most Linux drivers are implemented as kernel modules. / 19

Related Commands Load/unload a kernel module module.ko $ sudo insmod module.ko $ sudo rmmod module.ko Dump the message from kernel’s printk() $ dmesg / 19

Write a Kernel Module Called after insmod Called after rmmod #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> static int init_demo(void) { printk("Hello World!\n"); return 0; } static void cleanup_demo(void) { printk("Goodbye World!\n"); } module_init(init_demo); module_exit(cleanup_demo); MODULE_LICENSE("GPL"); Called after insmod Called after rmmod / 19

Build a Kernel Module We cross compile kernel modules in the build machine. A built kernel with its configuration and header files is required. In the sample Makefile, set the KDIR variable to the path of your built kernel. After the above steps, you can compile you kernel module. For more information of building kernel modules, see kbuild in the reference page. % # in the kernel module directory % export ARCH=arm % export CROSS_COMPILE=arm-linux-gnueabihf- % make / 19

DHT11 sensor DHT11 sensor is a combined humidity and temperature sensor. We will write a device driver that requests humidity and temperature from DHT11. Connect DHT11 pins and RPi GPIO pins. DHT11 DATA  GPIO GPIO02 DHT11 VCC  GPIO 3.3V DHT11 GND  GPIO GND / 19

Lab Requirement The physical part of the DHT11 sensor driver is already contained in the sample code. After inserting the module built from the sample code, the humidity and temperature sensed by DHT11 will be displayed in dmesg. Your job is to complete the logical part of the driver, and write a user space program to read humidity and temperature from the logical device. / 19

Reference The Linux Kernel Module Programming Guide Linux/Documentation/kbuild/modules.txt DHT11 Datasheet / 19