
| 字符设备
字符设备是能够像字节流一样被访问的设备,当对字符掘薯设备发出读写请求,相应的IO *** 作立即发生。Linux系统中很多设备都是字符设备,如字符终端、串口、键盘、鼠标等。在嵌入式Linux开发中,接触最多的就是字符设备以及驱动。
| 块设备
块设备是Linux系统中进行TO *** 作时必判激者须以块为单位进行访问的设备,块设备能够安装文件系统。块设备驱动会利用一块系统内存作为缓冲区,因此对块设备发出读写访问,并不一定立即产生硬件I/O *** 作。Linux系统中常见的块设备有如硬盘、软驱等等。
| 网络设备
网络设备既可以是网卡这样的硬件设备,也可以是一个纯软件设备如回环设备。网络设备由Linux的网络子系统驱动,负责数据包的发送和接收,而不是铅凯面向流设备,因此在Linux系统文件系统中网络设备没有节点。对网络设备的访问是通过socket调用产生,而不是普通的文件 *** 作如
open/closc和 read/write等。
第一部分 字符设备驱动程序1.1 函差扮亩数scull_open()
int scull_open(struct inode *inode,struct file *filp) {
MOD_INC_USE_COUNT; // 增加该模块缺闭的用虚森户数目
printk(“This chrdev is in open\n”);
return 0;
}
1.2 函数scull_write()
int scull_write(struct inode *inode,struct file *filp,const char *buffer,int count) {
if(count <0)
return –EINVAL;
if(scull.usage || scull.new_msg)
return –EBUSY;
scull.usage = 1;
kfree(scull.data);
data = kmalloc(sizeof(char)*(count+1),GFP_KERNEL);
if(!scull.data) {
return –ENOMEM;
}
copy_from_user(scull.data,buffer,count + 1);
scull.usage = 0;
scull.new_msg = 1;
return count;
}
1.3 函数scull_read()
int scull_read(struct inode *inode,struct file *filp,char *buffer,int count) {
int length;
if(count <0)
return –EINVAL;
if(scull.usage)
return –EBUSY;
scull.usage = 1;
if(scull.data == 0)
return 0;
length = strlen(scull.data);
if(length <count)
count = length;
copy_to_user(buf,scull.data,count + 1);
scull.new_msg = 0;
scull.usage = 0;
return count;
}
1.4 函数scull_ioctl()
#include <linux/ioctl.h>
#define SCULL_MAJOR 0
#define SCULL_MAGIC SCULL_MAJOR
#define SCULL_RESET _IO(SCULL_MAGIC,0) // reset the data
#define SCULL_QUERY_NEW_MSG _IO(SCULL_MAGIC,1) // check for new message
#define SCULL_QUERY_MSG_LENGTH _IO(SCULL_MAGIC,2) //get message length
#define IOC_NEW_MSG 1
static int usage,new_msg; // control flags
static char *data;
int scull_ioctl(struct inode *inode,struct file *filp,unsigned long int cmd,unsigned long arg) {
int ret=0;
switch(cmd) {
case SCULL_RESET:
kfree(data);
data = NULL;
usage = 0;
new_msg = 0;
break;
case SCULL_QUERY_NEW_MSG:
if(new_msg)
return IOC_NEW_MSG;
break;
case SCULL_QUERY_MSG_LENGTH:
if(data == NULL){
return 0;
}
else {
return strlen(data);
}
break;
default:
return –ENOTTY;
}
return ret;
}
1.5 函数scull_release()
void scull_release(struct inode *inode,struct file *filp) {
MOD_DEC_USE_COUNT; // 该模块的用户数目减1
printk(“This chrdev is in release\n”);
return 0;
#ifdef DEBUG
printk(“scull_release(%p,%p)\n”,inode,filp);
#endif
}
1.6 测试函数
在该字符设备驱动程序编译加载后,再在/dev目录下创建字符设备文件chrdev,使用命令: #mknod /dev/chrdev c major minor ,其中“c”表示chrdev是字符设备,“major”是chrdev的主设备号。(该字符设备驱动程序编译加载后,可在/proc/devices文件中获得主设备号,或者使用命令: #cat /proc/devices | awk ”\\$2==”chrdev\”{ print\\$1}” 获得主设备号)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include “chardev.h”// 见后面定义
void write_proc(void);
void read_proc(void);
main(int argc,char **argv) {
if(argc == 1) {
puts(“syntax: testprog[write|read]\n”);
exit(0);
}
if(!strcmp(argv[1],“write”)) {
write_porc();
}
else if(!strcmp(argv[1],“read”)) {
read_proc();
}
else {
puts(“testprog: invalid command!\n”);
}
return 0;
}
void write_proc() {
int fd,len,quit = 0;
char buf[100];
fd = open(“/dev/chrdev”,O_WRONLY);
if(fd <= 0) {
printf(“Error opening device for writing!\n”);
exit(1);
}
while(!quit) {
printf(“\n Please write into:”);
gets(buf);
if(!strcmp(buf,“exit”))
quit = 1;
while(ioctl(fd,DYNCHAR_QUERY_NEW_MSG))
usleep(100);
len = write(fd,buf,strlen(buf));
if(len <0) {
printf(“Error writing to device!\n”);
close(fd);
exit(1);
}
printf(“\n There are %d bytes written to device!\n”,len);
}
close(fd);
}
void read_proc() {
int fd,len,quit = 0;
char *buf = NULL;
fd=open(“/dev/chrdev”,O_RDONLY);
if(fd <0) {
printf(“Error opening device for reading!\n”);
exit(1);
}
while(!quit) {
printf(“\n Please read out:”);
while(!ioctl(fd,DYNCHAR_QUERY_NEW_MSG))
usleep(100);
// get the msg length
len = ioctl(fd,DYNCHAR_QUERY_MSG_LENGTH,NULL);
if(len) {
if(buf != NULL)
free(buf);
buf = malloc(sizeof(char)*(len+1));
len = read(fd,buf,len);
if(len <0) {
printf(“Error reading from device!\n”);
}
else {
if(!strcmp(buf,“exit”) {
ioctl(fd,DYNCHAR_RESET); // reset
quit = 1;
}
else
printf(“%s\n”,buf);
}
}
}
free(buf);
close(fd);
}
// 以下为chrdev.h定义
#ifndef _DYNCHAR_DEVICE_H
#define _DYNCHAR_DEVICE_H
#include <linux/ioctl.h>
#define DYNCHAR_MAJOR 42
#define DYNCHAR_MAGIC DYNCHAR_MAJOR
#define DYNCHAR_RESET _IO(DYNCHAR_MAGIC,0) // reset the data
#define DYNCHAR_QUERY_NEW_MSG _IO(DYNCHAR_MAGIC,1) // check for new message
#define DYNCHAR_QUERY_MSG_LENGTH _IO(DYNCHAR_MAGIC,2) // get message length
#define IOC_NEW_MSG 1
#endif
一、Linux device driver 的概念系统调用是 *** 作系统内核和应用程序之间的接口,设备驱动程序是 *** 作系统内核和机器硬件之间的接口.设备驱动程序为应用程序屏蔽了硬件的细节,这样在应用程判梁序看来,硬件设备只是一个设备文件,应用程序可以象 *** 作普通文件一样对硬件设备进行 *** 作.设备驱动程序是内核的一部分,它完成以下的功能:1、对设备初始化和释放;
2、把数据从内核传送到硬件和从硬件读取数据;
3、读取应用程序传送给设备文件的数据和回送应用程序请求的数据;
4、检测和处理设备出现的错误.
在Linux *** 作系统下枯闹有三类主要的设备文件类型,一是字符设备,二是块设备,三是网络设备.字符设备和块设备的主要区别是:在对字符设备发出读/写请求时,实际的硬件I/O一般就紧接着发生了,块设备则不然,它利用一块系统内存作缓冲区,当用户进程对设备请求能满足用户的要求,就返回请求的数据,如果不能,就调用请求函数来进行实际的I/O *** 作.块设备是主要针对磁盘等慢速设备设计的,以免耗费过多的CPU时间来等待.
已经提到,用户进程是通过设备文件来与实际的硬件打交道.每个设备文件都都有其文件属性(c/b),表示是字符设备还是块设备?另外每个文件都有两个设备号,第一个是主设备号,标识驱动程序,第二个是从设备号,标识使用同一个设备驱动程序的不同的硬件设备,比如有两个软盘,就可以用从设备号来区分他们.设备文件的的主设备号必须与设备驱动程序在登记时申请的主设备号一致,否则用户进程将无法访问到驱动程序.
最后必须提到的是,在用户进程调用驱动程序时,系统进入核心态,这时不再是抢先式调度.也就是说,系统必须在你的驱动程序的子函数返回后才能进行其他的工作.如果你的驱动程序陷入死循环,不幸的是你只有重新启动机器了,然后就是漫长的fsck.
二、实例剖析
我们来写一个最简单的字符设备驱动掘败运程序.虽然它什么也不做,但是通过它可以了解Linux的设备驱动程序的工作原理.把下面的C代码输入机器,你就会获得一个真正的设备驱动程序.
由于用户进程是通过设备文件同硬件打交道,对设备文件的 *** 作方式不外乎就是一些系统调用,如 open,read,write,close…, 注意,不是fopen, fread,但是如何把系统调用和驱动程序关联起来呢?这需要了解一个非常关键的数据结构:
STruct file_operatiONs {
int (*seek) (struct inode * ,struct file *, off_t ,int)
int (*read) (struct inode * ,struct file *, char ,int)
int (*write) (struct inode * ,struct file *, off_t ,int)
int (*readdir) (struct inode * ,struct file *, struct dirent * ,int)
int (*select) (struct inode * ,struct file *, int ,select_table *)
int (*ioctl) (struct inode * ,struct file *, unsined int ,unsigned long)
int (*mmap) (struct inode * ,struct file *, struct vm_area_struct *)
int (*open) (struct inode * ,struct file *)
int (*release) (struct inode * ,struct file *)
int (*fsync) (struct inode * ,struct file *)
int (*fasync) (struct inode * ,struct file *,int)
int (*check_media_change) (struct inode * ,struct file *)
int (*revalidate) (dev_t dev)
}
这个结构的每一个成员的名字都对应着一个系统调用.用户进程利用系统调用在对设备文件进行诸如read/write *** 作时,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数.这是linux的设备驱动程序工作的基本原理.既然是这样,则编写设备驱动程序的主要工作就是编写子函数,并填充file_operations的各个域.
下面就开始写子程序.
#include
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)