Designing and developing device drivers. Coding drivers

Similar documents
Introduction Reading Writing scull. Linux Device Drivers - char driver

Character Device Drivers

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

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

Unix (Linux) Device Drivers

MP3: VIRTUAL MEMORY PAGE FAULT MEASUREMENT

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Linux Device Drivers

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

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

CS 378 (Spring 2003)

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

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

Introduction to Device Drivers Part-1

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

Character Device Drivers One Module - Multiple Devices

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

Linux drivers - Exercise

Loadable Kernel Module

Finish up OS topics Group plans

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

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

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

Kernel Modules. Kartik Gopalan

Device Drivers. CS449 Fall 2017

7.4 Simple example of Linux drivers

BLOCK DEVICES. Philipp Dollst

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

FAME Operatinf Systems - Modules

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

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

CPSC 8220 FINAL EXAM, SPRING 2016

1 #include <linux/module.h> 2 #include <linux/sched.h> 3 #include <linux/interrupt.h> 4 #include <linux/init.h> 5 #include <linux/kernel.

Developing Audio Applications on a StrongArm platform. Craig Duffy Bristol UWE, UK

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

Operating systems for embedded systems. Embedded Operating Systems

TPMC680-SW-82. Linux Device Driver. 64 Digital Input/Output Version 1.1.x. User Manual. Issue April 2010

Operating systems for embedded systems

Operating System Labs. Yuanbin Wu

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

Operating System Concepts Ch. 11: File System Implementation

Linux Device Driver. Analog/Digital Signal Interfacing

Linux Device Driver in Action (LDDiA)

Linux Kernel Modules & Device Drivers April 9, 2012

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

NPTEL Course Jan K. Gopinath Indian Institute of Science

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

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

NPTEL Course Jan K. Gopinath Indian Institute of Science

Files. Eric McCreath

Driving Me Nuts Device Classes

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

Linux Loadable Kernel Modules (LKM)

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

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

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

Linux Device Drivers

Input & Output 1: File systems

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

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

TIP675-SW-82. Linux Device Driver. 48 TTL I/O Lines with Interrupts Version 1.2.x. User Manual. Issue November 2013

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

Operating System Labs. Yuanbin Wu

Overview. Daemon processes and advanced I/O. Source: Chapters 13&14 of Stevens book

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

CSE 333 SECTION 3. POSIX I/O Functions

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

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

I/O Management Intro. Chapter 5

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

CARRIER-SW-82. Linux Device Driver. IPAC Carrier Version 2.2.x. User Manual. Issue November 2017

The Linux driver for the Rome PCI-AER board

CS 378 (Spring 2003)

The Embedded I/O Company TIP700-SW-82 Linux Device Driver User Manual TEWS TECHNOLOGIES GmbH TEWS TECHNOLOGIES LLC

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

CS , Spring Sample Exam 3

MaRTE OS Misc utilities

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

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

Building Ethernet Drivers on RTLinux-GPL 1

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

UNIX System Calls. Sys Calls versus Library Func

Run time environments

Introduc)on to I/O. CSE 421/521: Opera)ng Systems Karthik Dantu

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Table of Contents. Preface... xi

Programming the GPMC driver

Lecture 10 Overview!

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

ECE 650 Systems Programming & Engineering. Spring 2018

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

Experiment 6 The Real World Interface

OPAE Intel FPGA Linux Device Driver Architecture Guide

INSTRUMENTATION. using FREE/OPEN CODE

Linux Device Drivers Interrupt Requests

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 333 SECTION 3. POSIX I/O Functions

Coccinelle: Killing Driver Bugs Before They Hatch

Introduction USB Pisanje gonilnika USB. USB drivers

Transcription:

Designing and developing device drivers Coding drivers

Registering a driver 2 calls to register a driver defined in <linux/fs.h> int register_chrdev_region(dev_t first, unsigned int count, char *name); int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name); Use alloc_chrddev_region They allocate major numbers and register the device Show up in /proc/devices and /sysfs

/proc/devices Character devices: 1 mem 2 pty 3 ttyp 4 ttys 6 lp 7 vcs 10 misc 13 input 14 sound 21 sg 180 usb Block devices: 2 fd 8 sd 11 sr 65 sd 66 sd

Dynamic allocation Eventually all major numbers will be dynamically allocated A script should be provided to initialise the device Can be in /etc/init.d/ A lot depends upon whether and how the driver is to be distributed

A init script #!/bin/sh module="scull" device="scull" mode="664" #invoke insmod with all arguments we got #and use a pathname, as newer modutils don't look in. by default /sbin/insmod./$module.ko $* exit 1 #remove stale nodes rm -f /dev/${device}[0-3] major=$(awk "\\$2==\"$module\" {print \\$1}" /proc/devices) mknod /dev/${device}0 c $major 0 mknod /dev/${device}1 c $major 1 mknod /dev/${device}2 c $major 2 mknod /dev/${device}3 c $major 3 #give appropriate group/permissions, and change the group. #Not all distributions have staff, some have "wheel" instead. group="staff" grep -q '^staff:' /etc/group group="wheel" chgrp $group /dev/${device}[0-3] chmod $mode /dev/${device}[0-3]

Char device de registration To de register or release a device call void unregister_chrdev_region(dev_t first, unsigned int count);

Important kernel data structures There are 3 important kernel data structures for device drvier writers File operations fops A field in File The File struct Not to be confused with stdio FILE The Inode struct

fops file operations In <linux/fs.h> All of the common calls that can be made on a file loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char user *, size_t, loff_t *); ssize_t (*aio_read)(struct kiocb *, char user *, size_t, loff_t); ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*release) (struct inode *, struct file *);

The FILE structure In <linux/fs.h> Used for every open file. Not specific to device drivers mode_t f_mode; loff_t f_pos; unsigned int f_flags; struct file_operations *f_op; void *private_data;

The inode structure Has a large amount of disc, file system and kernel information There are 2 fields important for drivers dev_t i_rdev; Use the macros iminor and imajor to access this struct cdev *i_cdev; The data structure for character devices

struct cdev Can be initialised directly from <linux/cdev.h> struct cdev *my_cdev = cdev_alloc( ); my_cdev >ops = &my_fops; Or embedded in driver s main data structure void cdev_init(struct cdev *cdev, struct file_operations *fops); Finally the kernel has to add the new cdev int cdev_add(struct cdev *dev, dev_t num, unsigned int count); The cdev can be removed with void cdev_del(struct cdev *dev);

The old method In most drivers you work with the older method of character device registration will be used int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); To unregister int unregister_chrdev(unsigned int major, const char *name);

sa1100 audio.h typedef struct { char *id; /* identification string */ struct device *dev; /* device */ audio_buf_t *buffers; /* pointer to audio buffer structures */ u_int usr_head; /* user fragment index */ u_int dma_head; /* DMA fragment index to go */ u_int dma_tail; /* DMA fragment index to complete */ u_int fragsize; /* fragment i.e. buffer size */ u_int nbfrags; /* nbr of fragments i.e. buffers */ u_int pending_frags; /* Fragments sent to DMA */ dma_device_t dma_dev; /* device identifier for DMA */ dma_regs_t *dma_regs; /* points to our DMA registers */ int bytecount; /* nbr of processed bytes */ int fragcount; /* nbr of fragment transitions */ struct semaphore sem; /* account for fragment usage */ wait_queue_head_t wq; /* for poll */ int dma_spinref; /* DMA is spinning */ int mapped:1; /* mmap()'ed buffers */ int active:1; /* actually in progress */ int stopped:1; /* might be active but stopped */ int spin_idle:1; /* have DMA spin on zeros when idle */ int sa1111_dma:1; /* DMA handled by SA1111 */ } audio_stream_t; /* * State structure for one instance */ typedef struct { audio_stream_t *output_stream; audio_stream_t *input_stream; int rd_ref:1; /* open reference for recording */ int wr_ref:1; /* open reference for playback */ int need_tx_for_rx:1; /* if data must be sent while receiving */ void *data; void (*hw_init)(void *); void (*hw_shutdown)(void *); int (*client_ioctl)(struct inode *, struct file *, uint, ulong); struct semaphore sem; /* to protect against races in attach() */ } audio_state_t;

Audio functions and fileops extern int sa1100_audio_attach( struct inode *inode, struct file *file, audio_state_t *state); extern ssize_t audio_write(struct file *file, const char *buffer, size_t count, loff_t * ppos); int sa1100_audio_suspend(audio_state_t *s, pm_message_t state); int sa1100_audio_resume(audio_state_t *s); extern int audio_read(struct file *file, char *buffer, size_t count, loff_t * ppos); static struct file_operations dev_fileops = {.owner = THIS_MODULE,.read = audio_read,.write = audio_write,.ioctl = audio_ioctl,.release = audio_release,.mmap = audio_mmap,.poll = audio_poll,.llseek = audio_llseek, };

sa1100_audio_attach int sa1100_audio_attach(struct inode *inode, struct file *file, audio_state_t *state) { audio_stream_t *os = state >output_stream; audio_stream_t *is = state >input_stream; int err, need_tx_dma; int seq1,seq2; DPRINTK("sa1100_audio_attach fmode %d\n",file >f_mode); file >private_data=state; os >dev >coherent_dma_mask=0xffffffff; down(&state >sem); /* access control */ err = ENODEV; if ((file >f_mode & FMODE_WRITE) &&!os) goto out; if ((file >f_mode & FMODE_READ) &&!is) goto out; err = EBUSY; if ((file >f_mode & FMODE_WRITE) && state >wr_ref) goto out; if ((file >f_mode & FMODE_READ) && state >rd_ref) goto out;

ucb1x00 core.c static int ucb1x00_probe(struct mcp *mcp) { struct ucb1x00 *ucb; struct ucb1x00_driver *drv; unsigned int id; int ret = ENODEV;...... mcp_enable(mcp); id = mcp_reg_read(mcp, UCB_ID); printk("ucb1x00_probe is is %x\n",id); if (id!= UCB_ID_1200 && id!= UCB_ID_1300 && id!= UCB_ID_TC35143) { printk(kern_warning "UCB1x00 ID not found: %04x\n", id); goto err_disable; } Major = register_chrdev(0,device_name,&fops); printk("device Number %d\n",major); goto out;