linux device driver 3 简单的字符驱动

p { margin-bottom: 0.08in; }

ldd 第三章

鉴于书中附带代码是综合性的,虽然很简单,但是对于初学者来说还是有点复杂,

我把作者的例子进行了一些简化,更有利于看清最重要的东西。也有利于一步步的学习,循序渐进。

首先,了解一下涉及到数据结构。

 

 

我们简化一下 ldd中的设计,如下,

下面,我简要的介绍一下,例子中的函数的作用:

function

mydev_init_module :

申请设备号 register_chrdev_region or alloc_chrdev_region

分配,初始化设备,并添加设备

分配: 静态或者 cdev_alloc

初始化: cdev_init

添加: cdev_add

mydev_clearup_module :

释放分配的用于存放数据的内存

删除设备 cdev_del

释放“设备结构体”所占内存

释放申请的设备号

 

mydev_llseek:

修改文件的当前的读写位置 (f_pos值 )

mydev_open :

保存”设备“指针到文件结构体的 private_data域

mydev_release :

open 的逆操作

mydev_read :

将写入的数据按照一定规则读出

mydev_write

讲用户数据保存到某个位置(内存或者硬件设备)

下面是简化后的代码结构

macro

MYDEBUG

ELEMENT_SIZE

 

struct

dataelem

mydev

 

variable

mydev0

mydev_major

mydev_minor

element_size

mydev_ops

mydev_init_module

mydev_clearup_module

 

function

mydev_init_module

mydev_setup_cdev

mydev_clearup_module

mydev_free_mem

mydev_llseek

mydev_open

mydev_release

mydev_read

mydev_write

 

说明:本例子为了更突出重点,而很简陋,重复打开设备的结果是不可预料的

 

 

源码:

 

操作过程:

 

# make //生成 mydev0.ko

# insmod mydev0.ko //插入生成的模块

# cat /proc/devices //查看分配的主设备号

...

250 mydev <-----我们的模块

251 hidraw

252 usbmon

...

# mknod /dev/mydev0 c 250 0 //创建设备结点

# echo “” > /var/log/messages //清空记录方便查看

# echo “ I love you Linux” > /dev/mydev0 //向设备中写入数据

#cat /dev/mydev0 //从设备中读出数据

#cat /var/log/messages //查看打印出来的调试信息

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值