Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.

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
Lecture 101 Lecture 10: Kernel Modules and Device Drivers ECE 412: Microcomputer Laboratory.
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
The UNIX File System Harry Chen Department of CSEE University of MD Baltimore County.
USERSPACE I/O Reporter: R 張凱富.
Building and Running Modules Sarah Diesburg COP 5641.
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
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.
Embedded System Programming Introduction to Device Drivers.
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
Tutorial and Demos on Linux Virtual Machine
Data Structures in the Kernel Sarah Diesburg COP 5641.
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.
Therac-25 Computer-controlled radiation therapy machine
Operating System Program 5 I/O System DMA Device Driver.
Linux Installation and Administration – Lesson 5 Tutor: George Papamarkos Topic: Devices in Linux.
CS 6560 Operating System Design Lecture 13 Finish File Systems Block I/O Layer.
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
C questions A great programmer codes excellent code in C and Java. The code does video decoding. Java code works faster then C on my computer. how come?
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Lecture 2: Linux devices Roei Ben-Harush Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder.
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.
Kyu Ho Park Sept. 22, Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
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.
1 1 Nov. 24, 2015 Kyu Ho Park Lecture 11 Time Handling,GPIO and I/O Systems.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
Linux Kernel Programming (LKP). LKP New sub-course New sub-course We will learn together We will learn together Evaluation of this part of course will.
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.
File Systems - Part I CS Introduction to Operating Systems.
I/O Software CS 537 – Introduction to Operating Systems.
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 4: Managing File System State Dr. Xiao Qin Auburn University.
 LED 를 직접 제어하는 디바이스 드라이버를 작성해 보자  Reminder: LED 는 SPI 를 통해 ARM7 과 연결된다. ◦ 그렇다면 고쳐야 하는 코드는 어디에 ?  linux-2.6.x/arch/arm/mach-nds/arm7 ◦ Hardware spec.
Linux Device Model A device model after 2.5
Device Driver_Skeleton
Introduction to Developing Embedded Linux Device Drivers
Lecture 3 Module Programming and Device Driver (Homework#1 included)
Linux Kernel Module Programming
Computer System Laboratory
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
Operation System Program 4
chapter 3-Char Device Driver
CS 6560 Operating System Design
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Lab 4 Kernel Module Operating System Lab.
CS 6560 Operating System Design Kernel Loadable Modules
Implementation of Embedded OS
Computer System Laboratory
Loadable Kernel Modules
Presentation transcript:

Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015

Why module programming?  ‘modules’: relocatable objects that can be loaded or removed dynamically into or from the kernel.  Most of the system functions such as file systems, device drivers, communication protocol can be implemented by modules in Linux!!!  To add or modify some functions of the Kernel, no need to recompile the whole kernel.

Monolithic Kernel and Micro kernel  Monolithic kernel: All functions are provided by a single kernel space ->Linux  Micro kernel: Kernel functions are minimized and the other functions are implemented in user space  Mach, VxWorks, Window NT.

A simple module program Ref: The Linux Kernel Module Programming Guide, Peter Jay Salzman et al.

Module ‘Makefile’ Line1: module name to be created. Line3: uname –r -> current kernel version, kernel directory for this module. Line4: current module programming directory

insmod-dmesg-rmmod #insmod simpleModule.ko #dmesg #rmmod simpleModule.ko #dmesg

Module utility  insmod, rmmod  lsmod - program to show the status of modules  depmod - program to generate modules.dep and map files.  modprobe – program to add and remove modules from the kernel

Kernel 2.4 and _header_file2.6_header_file #include MODULE#include #include <linux/module.h>#include <linux/kernel.h> 2.4_module_functions2.6_module_functions int init_module(void);module_init(xxx_init); void cleanup_module(void);module_exit(xxx_exit);

Passing parameters charp == char *

Module_param( ) module_param(para_name, para_type, attribute) Para_type: short, ushort, int, uint, long, ulong, charp, bool, invbool, intarray

printk( )  Typical usage: printk(KERN_INFO “message\n”);  loglevel  Remark: the execution time of printk( ) is very long!!!

Lecture for Homework 1 (Developing Device Driver) CoreLab.

Introduction to Device Driver  What is Device Driver?

Introduction to Device Driver  What is Device Driver? Software Hardware

User Level Hardware Level Kernel Level

Device files -File Types in Linux: Regular Files,Directory, Character Device Files,Block Device File,Link,Pipe, Socket -Device Files(Special Files) Characer Devices Block Devices

struct file_opertions The operations users can do on Linux file system are defined at the

Device Driver Device driver is a program to access devices through functions defined in file_operations.

Major and Minor Number Major number  Each device driver is identified by a unique major number. This number is the index into an array that contains the information about the driver (the device_struct) Minor number  This uniquely identifies a particular instance of a device. For example, a system may have multiple IDE hard disks each would have a major number of 3, but a different minor number.

ls –al (-,d,c,l,p,s,b) Major Number Minor Number

ls -al - (regular file)

register_chrdev( ), unregister_chrdev_region( )

register_chrdev, unregister_chrdev and register_chrdev_region,alloc_chrdev_region, unregister_chedev_region -The old way to regiater a char device driver: int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); int unregister_chrdev(unsigned int major, const char *name);

-int register_chrdev_region(dev_t first, unsigned int count, char *name); /* If the device major number is known */ -int alloc_chrdev_region(dev_t *dev, unsigned int minor, unsigned int count, char *name)l -void unregister_chrdev_region(dev_t first, unsigned int count);

cdev /usr/src/linux/include/linux/cdev.h struct cdev { struct kobjects kobj; struct module *owner; struct file_operations *ops; dev_t dev; unsigned int count; };

Functions for character devices #include struct cdev *cdev_alloc(void); void cdev_init(struct cdev +p, struct file_operations *fop); int cdev_add(struct cdev *p, dev_t first, unsigned count); void cdev_del(struct cdev *p);

- To obtain a standalone cdev structure at at runtime; struct cdev *my_cdev= cdev_alloc( ); dummy_cdev->ops - &my_fops; - To embed the cdev structure within a device specific structure; void cdev_init(struct cdev *cdev, struct file_operations *fops); -Finally to inform the kernel; int cdev_add(struct cdev *dev, dev_t first_dev_num,unsigned int count);

fops -Initialization of file operations struct file_operations fops = {.open = dummy_open,.release= dummy_close,.read= dummy_read,.write= dummy_write, };

dummy_driver.c

#dmesg after running an application program

Device Driver Internel  Basically, it is usually generated by C code.  In other word, it can be simple as much as ‘hello world’ in C programming.  You can freely imagine your device driver structure.  Even though it is not linked with real HW, it works.  Closely related with kernel. (use of system calls)

Device Drivers  Character Device  Bypass Buffer Cache  Serialized Data  User receives Row Data of the device  Ex. Terminal, video card, sound card…  Block Device  Through Buffer Cache  Random Accessible  Ex. HDD, CDROM …  Network Device  Ex. Ethernet

How to access the Device Driver  How can we access to the device driver?  Through Device File (special file)  Char device and Block device have this file.  ‘mknod /dev/dummy c 250 0’ – creation of the device file.  Real Usage  fd = open("/ dev/dummy ", O_RDWR);  write(fd,buf,1);  read(fd,buf,1);  close(fd);

Implementation of Device Driver  Specification of Dummy Device Driver  No linkage with Real HW.  Using printk, only printout the name of the device driver operation called by a user.  Implementations  Module init and cleanup  4 Operations in Dummy Device Driver  dummy_read : read system call  dummy_write : write system call  dummy_open : open system call  dummy_release : close system call

Implementation of Device Driver  How can we implement it?  Initialization (in init_module function)  Mapping a device file with the device driver  Registration of a Char Device : offered by OS  Needs Major Number, Name, and File_operation Structure of the device driver. int register_chrdev( unsigned int major, const * name, struct file_operations * fops); struct file_operations dummy_fops = {.open = dummy_open,// open.read = dummy_read, // read.write = dummy_write, // write.release = dummy_release, // release }; Mapping system calls with the internal functions of the device drivers

How can we implement it? dummy_driver.c #include #define DUMMY_MAJOR_NUMBER 250 static struct file_operations dummy_fops= { open: dummy_open, read: dummy_read, write: dummy_write, release: dummy_release, }; char devicename[20]; char value=' '; static struct cdev my_cdev; static int __init dummy_init(void) { dev_t dev=MKDEV(DUMMY_MAJOR_NUMBER,0); printk("Dummy Driver: init module\n"); strcpy(devicename, "Dummy_Driver"); register_chrdev(DUMMY_MAJOR_NUMBER, devicename, &dummy_fops); cdev_init(&my_cdev, &dummy_fops); cdev_add(&my_cdev,dev,128); return 0; } static void __exit dummy_exit(void) { printk("Dummy Driver : Clean Up Module\n"); cdev_del(&my_cdev); unregister_chrdev_region(MKDEV(DUMMY_MAJOR_NUMBER,0),128); }

ssize_t dummy_read(struct file *file, char *buffer, size_t length, loff_t *offset) { printk("Dummy Driver : Here is REad Call[%x]\n", value); if (copy_to_user(buffer, &value, sizeof(char))) return -EFAULT; return 0; } ssize_t dummy_write(struct file *file, const char *buffer, size_t length, loff_t *offset) { char vaule; if( copy_from_user(&value, buffer, sizeof(char))) return -EFAULT; printk("Dummy Driver: Here is Write Call [%x]\n", value); return 0; } int dummy_open(struct inode *inode, struct file *file) { printk("Dummy Driver : Open Call\n"); return 0; } int dummy_release(struct inode *inode, struct file *file) { printk("Dummy Driver : Release Call\n"); return 0; } module_init(dummy_init); module_exit(dummy_exit); MODULE_AUTHOR("KHPARK"); MODULE_DESCRIPTION("Dummy_Driver"); MODULE_LICENSE("GPL"); Continued from the previous page

How to build? Makefile

Module Instructions  mknod /dev/DUMMY_DEVICE c  insmod dummy_driver.ko  dmesg  rmmod dummy_driver.ko

test.c  Test: #include int main(void) { char buf[20]; int fd = open("/dev/DUMMY_DEVICE", O_RDWR); if(fd <= 0) return -1; buf[0] = 0x07; write(fd, buf, 1); read(fd, buf, 1); printf("data from the device : %x\n", buf[0]); close(fd); return 0; } #dmesg //after the execution of test program.

Problem1  1. device driver with FIFO queue  FIFO Queue structure In (write) & Creation of Entry in the Queue Out (read) & Deletion of Entry APP1 APP2 Check the written data sequence and the read data sequence are same or not.

Problem2  2. make your own ‘flush function’ that makes queue empty. APP1 APP2 APP3 Flush OP Thrash all the entries in the queue

Environment & Policies  Environment  Same as the Project1  Policies  Same as the Project1  Due : Sept. 24, 11:59 PM  Submission :  (hard copy) :  (soft copy) :

Appendix  Queue Example

Appendix  Queue Example #define QUEUE_SIZE 1024 typedef { int head, tail; int item[QUEUE_SIZE]; } CQ_t; static inline void InitCQ(CQ_t *q) { q->head = q->tail = 0; } static inline int IsFull(CQ_t *q) { return ( (q->head+1)%QUEUE_SIZE == q->tail ); } static inline int IsEmpty(CQ_t *q) { return (q->head == q->tail); } #define QUEUE_SIZE 1024 typedef { int head, tail; int item[QUEUE_SIZE]; } CQ_t; static inline void InitCQ(CQ_t *q) { q->head = q->tail = 0; } static inline int IsFull(CQ_t *q) { return ( (q->head+1)%QUEUE_SIZE == q->tail ); } static inline int IsEmpty(CQ_t *q) { return (q->head == q->tail); } int Queue(CQ_t *q, int value) { if (q==NULL) return -1; if (IsFull(q)) return -2; q->item[q->head] = value; q->head = (q->head + 1)%QUEUE_SIZE; return 0; } int Dequeue(CQ_t *q, int *value) { if (q==NULL || value==NULL) return -1; if (IsEmpty(q)) return -2; *value = q->item[q->tail]; q->tail = (q->tail + 1)%QUEUE_SIZE; return 0; } int Queue(CQ_t *q, int value) { if (q==NULL) return -1; if (IsFull(q)) return -2; q->item[q->head] = value; q->head = (q->head + 1)%QUEUE_SIZE; return 0; } int Dequeue(CQ_t *q, int *value) { if (q==NULL || value==NULL) return -1; if (IsEmpty(q)) return -2; *value = q->item[q->tail]; q->tail = (q->tail + 1)%QUEUE_SIZE; return 0; }