<<linux device driver,third edition>> Chapter 3:Char Drivers

The Internal Representation of Device Numbers

Within the kernel,the dev_t type(defined in linux/types.h ) is used to hold device numbers---both the major and minor parts.

it should,instead,make use of a set of macros found in linux/kdev_t.h.To obtain the major or minor parts of a dev_t,use:

MAJOR(dev_t dev);
MINOR(dev_t dev);

If,instead,you have the major and minor numbers and need to turn them into a dev_t,use:

MKDEV(int major,int minor);

Allocating and Freeing Device Numbers

one of the first things your driver will need to do when setting up a char device is to obtain one or more device numbers to work with.

declared in linux/fs.h:

//static alloc device number
int register_chrdev_region(dev_t first,unsigned int count,char *name);


//dynamic alloc
int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int count,char *name);

//free device number
void unregister_chrdev_region(dev_t first,unsigned int count);

Some Important Data Structures

File Operations

Each open file(represented internally by a file structrue)is associated with its own set of functions(by including a field called f_op that points to a file_operations structure)

We can consider the file to be an "object" and the functions operating on it to be its "method"(object-oriented programming)

The file Structure

The file structure represents an open file.

The inode Structure

The inode structure is used by the kernel internally to represent files.

There can be numerous file structures representing multiple open descriptors on a single file,but all point to a single inode structure.

Char Device Registration

Before the kernel invokes your device's operations,you must allocate and register one or more of type struct cdev(linux/cdev.h).

There are two ways of allocating and initializing one of these structures.

struct cdev *my_cdev = cdev_alloc();
my_cdev->ops = &my_fops;
//or
void cdev_init(struct cdev *cdev,struct file_operations *fops);
int cdev_add(struct cdev *dev,dev_t num,unsigned int count);
void cdev_del(struct cdev *dev);

The open Method

int (*open)(struct inode * inode,struct file *filp);

The release Method

int (*release)(struct inode * inode,struct file *filp);

read and write

prototype:

ssize_t read(struct file *filp,char __user *buf,size_t count,loff_t *offp);

ssize_t write(struct file *filp,const char __user *buf,size_t count,loff_t *offp);

copy a whole segment of data to or from the user address space.(linux/uaccess.h)

unsigned long copy_to_user(void __user *to,const void *from,unsigned long count);

unsigned long copy_from_user(void *to,const void __user * from,unsigend long count);

转载于:https://www.cnblogs.com/r1ng0/p/9570113.html

Linux Device Driver (3edtion)原版 1. An Introduction to Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Role of the Device Driver 2 Splitting the Kernel 4 Classes of Devices and Modules 5 Security Issues 8 Version Numbering 10 License Terms 11 Joining the Kernel Development Community 12 Overview of the Book 12 2. Building and Running Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Setting Up Your Test System 15 The Hello World Module 16 Kernel Modules Versus Applications 18 Compiling and Loading 22 The Kernel Symbol Table 28 Preliminaries 30 Initialization and Shutdown 31 Module Parameters 35 Doing It in User Space 37 Quick Reference 39 3. Char Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Design of scull 42 Major and Minor Numbers 43 Some Important Data Structures 49 ,ldr3TOC.fm.4587 Page v Thursday, January 20, 2005 9:30 AMvi | Table of Contents Char Device Registration 55 open and release 58 scull’s Memory Usage 60 read and write 63 Playing with the New Devices 70 Quick Reference 70 4. Debugging Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Debugging Support in the Kernel 73 Debugging by Printing 75 Debugging by Querying 82 Debugging by Watching 91 Debugging System Faults 93 Debuggers and Related Tools 99 5. Concurrency and Race Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Pitfalls in scull 107 Concurrency and Its Management 107 Semaphores and Mutexes 109 Completions 114 Spinlocks 116 Locking Traps 121 Alternatives to Locking 123 Quick Reference 130 6. Advanced Char Driver Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 ioctl 135 Blocking I/O 147 poll and select 163 Asynchronous Notification 169 Seeking a Device 171 Access Control on a Device File 173 Quick Reference 179
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值