开发一个驱动程序时,不免涉及到对硬件的相关操作,例如读取寄存器和引脚,在不利用任何框架的基础上,硬件代码总是和其他操作耦合到一块,这样做的坏处是代码耦合性太强,例如有三盏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");
硬件信息代码和操作代码完全混在一块,修改硬件信息就可能对操作代码产生影响,如果代码比较简单还可以忍受,一旦代码量非常大的时候,这种修改是非常痛苦的。