在Linux内核中你很难看到驱动+应用的程序设计方法,而是使用的是Linux分层框架分层分工,标准化接口+标准化功能代码:
interface - 接口层,标准化接口
device - 设备层,实现功能函数
include - 接口层及设备层共用的头文件目录
modules - 编译输出的模块目录
test - 应用程序

接口层在模块初始化函数中就注册了一个字符设备,操作方法集file_operations是一个纯接口集合
file_operations中的每一个接口调用底层设备层功能函数。操作方法集是一个纯虚函数集合在open的时候替换为底层的设备层的功能函数,如果底层没有实现功能函数,则执行接口层的标准代码。
1、interface代码
/*
* 分层分工设计演示
* cdev为接口层
* mydev.c
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include "../include/mydev.h"
#define MYDEVMAX 256
static struct mydev *mydevarr[MYDEVMAX];
static struct class *mydevclass = NULL;
static const char mydevname[] = "mydev";
static int __init mydev_init(void)
{
int i;
for(i = 0; i < MYDEVMAX; i++){
mydevarr[i] = NULL;
}