linux-2.6.22.6内核总线设备驱动模型

news/2024/7/5 3:36:25

开发一个驱动程序时,不免涉及到对硬件的相关操作,例如读取寄存器和引脚,在不利用任何框架的基础上,硬件代码总是和其他操作耦合到一块,这样做的坏处是代码耦合性太强,例如有三盏led灯,驱动程序每次只打开一盏灯,就如想换盏灯打开,那么每次都得修改寄存器和引脚等硬件相关的信息,由于对硬件的操作是通用性的,而这些操作和硬件本身的信息封装在了一块,所以这种修改可能会对这些通用操作产生影响。

为了消除这种影响,最好的方法是把硬件通用操作和硬件本身的信息分开,这样如果硬件发生了改变,只需修改硬件信息部分代码即可,不会对操作部分产生影响,总线设备驱动模型可以认为就是这样一种实现。

总线设备驱动模型可以分为总线(bus)、设备(device)、驱动(driver)三个部分,设备可以认为就是硬件本身信息,驱动是对硬件的操作,总线则把驱动和设备联系起来。device和driver需要向bus注册,注册的时候bus会根据name对device和driver进行匹配,如果匹配上则会调用driver对应结构体的proble函数(device和driver都有自己的结构体描述),在proble函数里,我们可以处理自己想要的操作,同理,driver结构体也有一个remove函数,当卸载device或driver被调用。

例如一个打开led某盏灯的驱动用总线设备驱动模型开发代码如下:

device端:

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>
#include <linux/uaccess.h>


/* 分配/设置/注册一个platform_device */

static struct resource led_resource[] = {
    [0] = {
        .start = 0x56000050,
        .end   = 0x56000050 + 8 - 1,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 5,
        .end   = 5,
        .flags = IORESOURCE_IRQ,
    }

};

static void led_release(struct device * dev)
{
}


static struct platform_device led_dev = {
    .name         = "myled",
    .id       = -1,
    .num_resources    = ARRAY_SIZE(led_resource),
    .resource     = led_resource,
    .dev = { 
    	.release = led_release, 
	},
};

static int led_dev_init(void)
{
	platform_device_register(&led_dev);
	return 0;
}

static void led_dev_exit(void)
{
	platform_device_unregister(&led_dev);
}

module_init(led_dev_init);
module_exit(led_dev_exit);

MODULE_LICENSE("GPL");

driver端:

/* 分配/设置/注册一个platform_driver */

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/uaccess.h>

static int major;


static struct class *cls;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file)
{
	//printk("first_drv_open\n");
	/* 配置为输出 */
	*gpio_con &= ~(0x3<<(pin*2));
	*gpio_con |= (0x1<<(pin*2));
	return 0;	
}

static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int val;

	//printk("first_drv_write\n");

	copy_from_user(&val, buf, count); //	copy_to_user();

	if (val == 1)
	{
		// 点灯
		*gpio_dat &= ~(1<<pin);
	}
	else
	{
		// 灭灯
		*gpio_dat |= (1<<pin);
	}
	
	return 0;
}


static struct file_operations led_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   led_open,     
	.write	=	led_write,	   
};

static int led_probe(struct platform_device *pdev)
{
	struct resource		*res;

	/* 根据platform_device的资源进行ioremap */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	gpio_con = ioremap(res->start, res->end - res->start + 1);
	gpio_dat = gpio_con + 1;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	pin = res->start;

	/* 注册字符设备驱动程序 */

	printk("led_probe, found led\n");

	//major = register_chrdev(0, "myled", &led_fops);
	 major = register_chrdev(0, "myled", &led_fops); // 注册, 告诉内核

	cls = class_create(THIS_MODULE, "myled");
	
	//class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
	device_create(cls, NULL, MKDEV(major, 0), NULL, "led");

	
	return 0;
}

static int led_remove(struct platform_device *pdev)
{
	/* 卸载字符设备驱动程序 */
	/* iounmap */
	printk("led_remove, remove led\n");

	device_destroy(cls, MKDEV(major, 0));
	unregister_chrdev(major, "myled");
	class_destroy(cls);
	iounmap(gpio_con);
	
	return 0;
}


struct platform_driver led_drv = {
	.probe		= led_probe,
	.remove		= led_remove,
	.driver		= {
		.name	= "myled",
	}
};


static int led_drv_init(void)
{
	platform_driver_register(&led_drv);
	return 0;
}

static void led_drv_exit(void)
{
	platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

分析:当装载led_drv.ko和led_dev.ko后,由于两边的name都是myled,所以匹配成功就会触发driver里的led_probe执行。在led_probe里获取了device信息,并创建了设备信息,应用程序就可依此进行相关操作。如果要修改设备信息,只需要修改device部分代码即可,driver代码不受影响,可见这明显降低了代码的耦合性,是一种分层和分离的代码设计思想。

如果不分离,代码是这样的:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

#define DEVICE_NAME     "first_driver"  /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_MAJOR       231     /* 主设备号 */

static struct class *firstdrv_class;
static struct class_device	*firstdrv_class_dev;

volatile unsigned long *pGPFCON = NULL;
volatile unsigned long *pGPFDAT = NULL;

/* 应用程序对设备文件/dev/leds执行open(...)时,
 * 就会调用s3c24xx_leds_open函数
 */
static int s3c24xx_open(struct inode *inode, struct file *file)
{
    *pGPFCON &= ~((3<<8)|(3<<10)|(3<<12));
    *pGPFCON |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
    return 0;
}


static ssize_t s3c24xx_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){

 
    int val;
    copy_from_user(&val, buf, count);
     printk("s3c24xx_write  : %s \n",val);
    if(val == 1){
    *pGPFDAT &= ~((1<<4) | (1<<5) | (1<<6));

    }else{

    *pGPFDAT |= (1<<4) | (1<<5) | (1<<6);
    }
    return 0;
    
}

/* 这个结构是字符设备驱动程序的核心
 * 当应用程序操作设备文件时所调用的open、read、write等函数,
 * 最终会调用这个结构中指定的对应函数
 */
static struct file_operations s3c24xx_leds_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c24xx_open,     
    .write  =   s3c24xx_write,
};

/*
 * 执行insmod命令时就会调用这个函数 
 */
 
int major;
static int __init s3c24xx_init(void)
{

     printk(DEVICE_NAME " s3c24xx_init\n");
   

    /* 注册字符设备
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数
     * LED_MAJOR可以设为0,表示由内核自动分配主设备号
     */
   major = register_chrdev(0, DEVICE_NAME, &s3c24xx_leds_fops); // 注册, 告诉内核

	firstdrv_class = class_create(THIS_MODULE, DEVICE_NAME);

	firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/xyz */
   
   pGPFCON = (volatile unsigned long *)ioremap(0x56000050, 16);
   pGPFDAT = pGPFCON + 1;

    if (major < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return major;
    }
    
    printk(DEVICE_NAME " initialized\n");
    return 0;
}

/*
 * 执行rmmod命令时就会调用这个函数 
 */
static void __exit s3c24xx_exit(void)
{
    /* 卸载驱动程序 */
    unregister_chrdev(major, DEVICE_NAME); // 卸载

	class_device_unregister(firstdrv_class_dev);
	class_destroy(firstdrv_class);
}

/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_init);
module_exit(s3c24xx_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("hcc");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");

硬件信息代码和操作代码完全混在一块,修改硬件信息就可能对操作代码产生影响,如果代码比较简单还可以忍受,一旦代码量非常大的时候,这种修改是非常痛苦的。


http://lihuaxi.xjx100.cn/news/1288462.html

相关文章

Python中的for循环语句及其应用举例(等差数列求和、阶乘、寻找最大值)

Python中的for循环语句及其应用举例(等差数列求和、阶乘、寻找最大值) 在学习任何编程语言的时候&#xff0c;不熟悉判断选择结构和循环结构&#xff0c;就难以发挥计算机优秀的计算能力和提高学习工作效率。本文将重点讲解Python中的for循环语句&#xff0c;并举例等差数列求…

easyConnect 报本地环境异常错误

一、检查任务管理器中发现ecagent.exe进程是禁用状态。如图&#xff1a; 二、在异常客户端上&#xff0c;找到easyconnect的安装目录&#xff08;默认路径&#xff1a;C:\Program Files (x86)\Sangfor\SSL\ECAgent&#xff09;&#xff0c;找到ecagent.exe应用程序尝试手动执行…

H3C交换机在地址池下如何进行IP和MAC地址绑定

环境&#xff1a; H3C S6520-26Q-SI version 7.1.070, Release 6326 问题描述&#xff1a; H3C交换机在地址池下如何进行IP和MAC地址绑定 将MAC地址为0000-e03f-0305的PC机与IP地址10.1.1.1绑定&#xff0c;掩码为255.255.255.0 解决方案&#xff1a; 1.进入地址池视图 …

C# async await使用方法

运行界面&#xff1a; 代码部分&#xff1a; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using …

北上广深共享办公室市场概览

在北上广深这四个城市&#xff0c;共享办公室已经成为一种日益流行的办公模式。随着经济的快速发展和科技创新的不断推进&#xff0c;越来越多的企业开始青睐共享办公室&#xff0c;以适应灵活办公和协作办公的需求。那么&#xff0c;2023年共享办公室市场的前景如何呢&#xf…

DDR5这么快,为啥还能那么稳?

高速先生成员--姜杰 大家都在关注DDR5跑的有多快&#xff0c;高速先生却在关心它为什么能够跑的稳…… 内存的稳定性&#xff0c;离不开RAS功能。提起RAS&#xff0c;熟悉DDR的小伙伴们一定记得行地址选通信号&#xff08;Row Address Strobe, RAS&#xff09;&#xff0c;不…

Fiddler如何进行弱网测试

目录 前言 1、Fiddler设置 2、手机设置 3.设置模拟调制解调器的速度&#xff1a; 4.设置手机代理 前言 使用Fiddler对手机App应用进行抓包&#xff0c;可以对App接口进行测试&#xff0c;也可以了解App传输中流量使用及请求响应情况&#xff0c;从而测试数据传输过程中流量…

数据结构与算法--堆

最小堆C实现&#xff1a;&#xff08;可以插入一个数、删除最小值&#xff09; #include <iostream> using namespace std;const int DefaultSize 1000005;template<class T> class MinHeap{T* heap;int currentSize;int maxHeapSize;void siftDown(int start);v…