@(linux 编程)[led, 驱动]
在学习platform驱动之前,我写过最原始的 led 驱动程序,其包含了硬件初始化和设备注册的所有工作。这种模型理解起来直观易懂,但是在实际的应用当中却显得不那么理想。设备信息和驱动代码冗余在一起,一旦设备信息发生一点变化将会导致整个驱动程序的大幅度修改甚至重写,十分的麻烦。因此,linux引入了platform总线的概念,即使用虚拟总线技术将设备信息和驱动代码进行分离,驱动代码只负责处理驱动的 逻辑,而关于具体的设备信息则放到单独的设备文件数当中。这样一来,当硬件信息发生改变的时候,我们只需要修改设备信息的源代码无需关注驱动的逻辑,反之,如果驱动的逻辑需要修改我们无需修改设备文件。这样有利于程序员对驱动代码的维护和省级,大大减少了工作量。
如下图所示,platform 总线会维护两条链表,分别管理设备和驱动,当一个设备被注册到总线上的时候,总线会根据其名字搜索对应的驱动,如果找到就将设备信息导入驱动程序并执行驱动;当一个驱动被注册到平台总线的时候,总线也会搜索设备。总之,平台总线负责将设备信息和驱动代码匹配,这样就可以做到驱动和设备信息的分离。
接下来看plat_led驱动实例
/********************************************************************************
* Copyright: (C) 2011 Guo Wenxue
* All rights reserved.
*
* Filename: s3c_led.c
* Description: This is the common LED driver runs on S3C24XX.
*
* Version: 1.0.0(10/27/2011~)
* Author: Guo Wenxue
* ChangeLog: 1, Release initial version on "10/27/2011 11:39:10 AM"
*
********************************************************************************/
#include "s3c_driver.h"
#define DRV_AUTHOR "Guo Wenxue "
#define DRV_DESC "S3C24XX LED driver"
/* Driver version*/
#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0
#define DEV_NAME DEV_LED_NAME
//#define DEV_MAJOR DEV_LED_MAJOR
#ifndef DEV_MAJOR
#define DEV_MAJOR 0 /* dynamic major by default */
#endif
#define TIMER_TIMEOUT 40
static int debug = DISABLE;
static int dev_major = DEV_MAJOR;
static int dev_minor = 0;
/* ============================ Platform Device part ===============================*/
/* LED hardware informtation structure*/
struct s3c_led_info
{
unsigned char num; /* The LED number */
unsigned int gpio; /* Which GPIO the LED used */
unsigned char active_level; /* The GPIO pin level(HIGHLEVEL or LOWLEVEL) to turn on or off */
unsigned char status; /* Current LED status: OFF/ON */
unsigned char blink; /* Blink or not */
};
/* The LED platform device private data structure */
struct s3c_led_platform_data
{
struct s3c_led_info *leds;
int nleds;
};
/* LED hardware informtation data*/
static struct s3c_led_info s3c_leds[] = {
[0] = {
.num = 1,
.gpio = S3C2410_GPB(5),
.active_level = LOWLEVEL,
.status = OFF,
.blink = ENABLE,
},
[1] = {
.num = 2,
.gpio = S3C2410_GPB(6),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[2] = {
.num = 3,
.gpio = S3C2410_GPB(8),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
[3] = {
.num = 4,
.gpio = S3C2410_GPB(10),
.active_level = LOWLEVEL,
.status = OFF,
.blink = DISABLE,
},
};
/* The LED platform device private data */
static struct s3c_led_platform_data s3c_led_data = {
.leds = s3c_leds,
.nleds = ARRAY_SIZE(s3c_leds),
};
struct led_device
{
struct s3c_led_platform_data *data;
struct cdev cdev;
struct class *dev_class;
struct timer_list blink_timer;
} led_device;
static void platform_led_release(struct device * dev)
{
int i;
struct s3c_led_platform_data *pdata = dev->platform_data;
dbg_print("%s():%d\n", __FUNCTION__,__LINE__);
/* Turn all LED off */
for(i=0; inleds; i++)
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
}
/*该结构体为最终要注册到总线上的设备对象*/
static struct platform_device s3c_led_device = {
.name = "s3c_led",
.id = 1,
.dev =
{
.platform_data = &s3c_led_data,
.release = platform_led_release,
},
};
/* ===================== led device driver part ===========================*/
void led_timer_handler(unsigned long data)
{
int i;
struct s3c_led_platform_data *pdata = (struct s3c_led_platform_data *)data;
for(i=0; inleds; i++)
{
if(ON == pdata->leds[i].status)
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, pdata->leds[i].active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
if(ENABLE == pdata->leds[i].blink ) /* LED should blink */
{
/* Switch status between 0 and 1 to turn LED ON or off */
pdata->leds[i].status = pdata->leds[i].status ^ 0x01;
}
mod_timer(&(led_device.blink_timer), jiffies + TIMER_TIMEOUT);
}
}
static int led_open(struct inode *inode, struct file *file)
{
struct led_device *pdev ;
struct s3c_led_platform_data *pdata;
pdev = container_of(inode->i_cdev,struct led_device, cdev);
pdata = pdev->data;
file->private_data = pdata;
return 0;
}
static int led_release(struct inode *inode, struct file *file)
{
return 0;
}
static void print_led_help(void)
{
printk("Follow is the ioctl() command for LED driver:\n");
printk("Enable Driver debug command: %u\n", SET_DRV_DEBUG);
printk("Get Driver verion command : %u\n", GET_DRV_VER);
printk("Turn LED on command : %u\n", LED_ON);
printk("Turn LED off command : %u\n", LED_OFF);
printk("Turn LED blink command : %u\n", LED_BLINK);
}
/* compatible with kernel version >=2.6.38*/
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct s3c_led_platform_data *pdata = file->private_data;
switch (cmd)
{
case SET_DRV_DEBUG:
dbg_print("%s driver debug now.\n", DISABLE == arg ? "Disable" : "Enable");
debug = (0==arg) ? DISABLE : ENABLE;
break;
case GET_DRV_VER:
print_version(DRV_VERSION);
return DRV_VERSION;
case LED_OFF:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = OFF;
pdata->leds[arg].blink = DISABLE;
break;
case LED_ON:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = ON;
pdata->leds[arg].blink = DISABLE;
break;
case LED_BLINK:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].blink = ENABLE;
pdata->leds[arg].status = ON;
break;
default:
dbg_print("%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);
print_led_help();
return -EINVAL;
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl, /* compatible with kernel version >=2.6.38*/
};
/*一旦驱动和设备都注册在总线上,进入probe函数进行匹配*/
static int s3c_led_probe(struct platform_device *dev)
{
struct s3c_led_platform_data *pdata = dev->dev.platform_data;
int result = 0;
int i;
dev_t devno;//设备号
/* Initialize the LED status */
for(i=0; inleds; i++)
{
s3c2410_gpio_cfgpin(pdata->leds[i].gpio, S3C2410_GPIO_OUTPUT);
if(ON == pdata->leds[i].status)
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, pdata->leds[i].active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
}
/* Alloc the device for driver */
if (0 != dev_major) //如果已经有主设备号
{
devno = MKDEV(dev_major, dev_minor); //生成devno值
result = register_chrdev_region(devno, 1, DEV_NAME); //分配次设备号
}
else //如果没有主设备号
{
result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME); //动态获取主设备号
dev_major = MAJOR(devno);
}
/* Alloc for device major failure */
if (result < 0)
{
printk("%s driver can't get major %d\n", DEV_NAME, dev_major);
return result;
}
/* Initialize button structure and register cdev*/
memset(&led_device, 0, sizeof(led_device));//清空led_device结构体空间
led_device.data = dev->dev.platform_data;
cdev_init (&(led_device.cdev), &led_fops);
led_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(led_device.cdev), devno , 1); //添加设备到内核
if (result)
{
printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME);
goto ERROR;
}
led_device.dev_class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(led_device.dev_class))
{
printk("%s driver create class failture\n",DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
device_create(led_device.dev_class, NULL, devno, NULL, DEV_NAME);
#else
device_create (led_device.dev_class, NULL, devno, DEV_NAME);
#endif
/* Initial the LED blink timer */
init_timer(&(led_device.blink_timer));
led_device.blink_timer.function = led_timer_handler;
led_device.blink_timer.data = (unsigned long)pdata;
led_device.blink_timer.expires = jiffies + TIMER_TIMEOUT;
add_timer(&(led_device.blink_timer));
printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
return 0;
ERROR:
printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
cdev_del(&(led_device.cdev));
unregister_chrdev_region(devno, 1);
return result;
}
static int s3c_led_remove(struct platform_device *dev)
{
dev_t devno = MKDEV(dev_major, dev_minor);
del_timer(&(led_device.blink_timer));
cdev_del(&(led_device.cdev));
device_destroy(led_device.dev_class, devno);
class_destroy(led_device.dev_class);
unregister_chrdev_region(devno, 1);
printk("S3C %s driver removed\n", DEV_NAME);
return 0;
}
static struct platform_driver s3c_led_driver = {
.probe = s3c_led_probe,
.remove = s3c_led_remove,
.driver = {
.name = "s3c_led",
.owner = THIS_MODULE,
},
};
static int __init s3c_led_init(void)
{
int ret = 0;
/*设备注册:把指定设备添加到内核中平台总线的设备列表,等待匹配,匹配成功则回调驱动中probe;*/
ret = platform_device_register(&s3c_led_device);//将建立好的设备对象注册到内核,返回0表示注册成功
if(ret)//如果注册失败
{
printk(KERN_ERR "%s:%d: Can't register platform device %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_dev;
}
dbg_print("Regist S3C LED Platform Device successfully.\n");//如果注册成功
/*驱动注册:把指定驱动添加到内核中平台总线的驱动列表,等待匹配,匹配成功则回调驱动中probe;*/
ret = platform_driver_register(&s3c_led_driver);
if(ret)//如果注册失败
{
printk(KERN_ERR "%s:%d: Can't register platform driver %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_drv;
}
dbg_print("Regist S3C LED Platform Driver successfully.\n");//如果注册成功
return 0;
fail_reg_plat_drv:
platform_driver_unregister(&s3c_led_driver);
fail_reg_plat_dev:
return ret;
}
static void s3c_led_exit(void)
{
dbg_print("%s():%d remove LED platform drvier\n", __FUNCTION__,__LINE__);
platform_driver_unregister(&s3c_led_driver);
dbg_print("%s():%d remove LED platform device\n", __FUNCTION__,__LINE__);
/**注销:把指定设备从设备列表中删除,如果驱动已匹配则回调驱动方法和设备信息中的release;*/
platform_device_unregister(&s3c_led_device);
}
module_init(s3c_led_init);
module_exit(s3c_led_exit);
module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);
module_param(dev_minor, int, S_IRUGO);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:S3C24XX_led");
毫无疑问,拿到驱动代码的第一件事当然是看初始化函数,即本历程中的
static int __init s3c_led_init(void)
{
int ret = 0;
/*设备注册:把指定设备添加到内核中平台总线的设备列表,等待匹配,匹配成功则回调驱动中probe函数;*/
ret = platform_device_register(&s3c_led_device);//将建立好的设备对象注册到内核,返回0表示注册成功
if(ret)//如果设备注册失败,返回错误信息并注销之前驱动的注册
{
printk(KERN_ERR "%s:%d: Can't register platform device %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_dev;
}
dbg_print("Regist S3C LED Platform Device successfully.\n");//如果注册成功打印成功信息
/*驱动注册:把指定驱动添加到内核中平台总线的驱动列表,等待匹配,匹配成功则回调驱动中probe;*/
ret = platform_driver_register(&s3c_led_driver);
if(ret)//同上面一样,如果注册失败返回错误信息
{
printk(KERN_ERR "%s:%d: Can't register platform driver %d\n", __FUNCTION__,__LINE__, ret);
goto fail_reg_plat_drv;
}
dbg_print("Regist S3C LED Platform Driver successfully.\n");//如果注册成功
return 0;
fail_reg_plat_drv:
platform_driver_unregister(&s3c_led_driver);
fail_reg_plat_dev:
return ret;
}
在初始化函数 int __init s3c_led_init(void) 中所做的工作为设备和驱动在platform总线的注册,根据platform驱动原理,如果设备和驱动都注册成功,程序将跳转到probe函数中去执行。
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl, /* compatible with kernel version >=2.6.38*/
};
在驱动程序中我们保留了一些设备编号,file_operation结构体就是驱动程序的这些编号连接到内核。这个结构中的每一个成员变量都必须指向驱动程序中实现特定操作的函数,对于不支持的操作,设置为NULL。
/*一旦驱动和设备都注册在总线上,进入probe函数进行匹配*/
static int s3c_led_probe(struct platform_device *dev)
{
struct s3c_led_platform_data *pdata = dev->dev.platform_data;
int result = 0;
int i;
dev_t devno;//设备编号
/* 初始化LED的状态*/
for(i=0; inleds; i++)
{
s3c2410_gpio_cfgpin(pdata->leds[i].gpio, S3C2410_GPIO_OUTPUT);/*设置相应gpio为输出模式*/
if(ON == pdata->leds[i].status)
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, pdata->leds[i].active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
}
/* Alloc the device for driver */
if (0 != dev_major) //如果已经有主设备号
{
devno = MKDEV(dev_major, dev_minor); //生成devno值
result = register_chrdev_region(devno, 1, DEV_NAME); //分配次设备号
}
else //如果没有主设备号
{
result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME); //动态获取主设备号
dev_major = MAJOR(devno);
}
if (result < 0) //如果设备号分配失败打印错误信息
{
printk("%s driver can't get major %d\n", DEV_NAME, dev_major);
return result;
}
memset(&led_device, 0, sizeof(led_device));//清空led_device结构体空间
led_device.data = dev->dev.platform_data;
cdev_init (&(led_device.cdev), &led_fops);//注册cdev结构体
led_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(led_device.cdev), devno , 1); //添加设备到内核
if (result) //如果添加失败,打印错误信息
{
printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME);
goto ERROR;
}
/*为设备自动创建节点*/
led_device.dev_class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(led_device.dev_class)) //如果设备节点创建失败打印以下错误信息
{
printk("%s driver create class failture\n",DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
device_create(led_device.dev_class, NULL, devno, NULL, DEV_NAME);
#else
device_create (led_device.dev_class, NULL, devno, DEV_NAME);
#endif
/* 启用内核定时器,初始 timer_list 的结构的一些变量 */
init_timer(&(led_device.blink_timer));
led_device.blink_timer.function = led_timer_handler;
led_device.blink_timer.data = (unsigned long)pdata;
led_device.blink_timer.expires = jiffies + TIMER_TIMEOUT;
add_timer(&(led_device.blink_timer)); //将blink_timer 加入内核timer列表中,等待处理
printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
return 0;
ERROR:
printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
cdev_del(&(led_device.cdev));
unregister_chrdev_region(devno, 1);
return result;
}
从上面程序可以看出,在probe函数中主要做的事情为
1.分配设备编号
2. 初始化led_device结构体并注册cdev结构体
3. 自动创建设备节点
4. 初始化定时器
static int led_open(struct inode *inode, struct file *file)
{
struct led_device *pdev ;
struct s3c_led_platform_data *pdata;
pdev = container_of(inode->i_cdev,struct led_device, cdev);
pdata = pdev->data;
file->private_data = pdata;
return 0;
}
关于container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址。
在本函数中即获得结构体led_device的地址
关于inode结构
inode结构体在内部表示文件,其包含了大量有关文件的信息,
struct inode {
dev_t i_rdev;
struct cdev * i_cdev;
}
其中i_rdev表示真正的设备编号,cdev表示字符设备的内核内部结构,当inode表示一个字符设备时,cdev包含了指向struct cdev的结构体指针
与file结构体不同,file表示打开的文件描述符,只有打开文件后才生效。对于同一个文件,可能有多个file结构,但它们都指向单个的inode结构体。
ioctl函数主要实现用户空间和内核空间命令的传递,从而实现对LED等亮灭以及闪烁的控制
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct s3c_led_platform_data *pdata = file->private_data;
switch (cmd)//cmd为内核空间传过来的命令
{
case SET_DRV_DEBUG:
dbg_print("%s driver debug now.\n", DISABLE == arg ? "Disable" : "Enable");
debug = (0==arg) ? DISABLE : ENABLE;
break;
case GET_DRV_VER://查看版本信息
print_version(DRV_VERSION);
return DRV_VERSION;
/*如果传入LED_OFF命令,则关闭LED灯*/
case LED_OFF:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = OFF;
pdata->leds[arg].blink = DISABLE;
break;
/*如果传入LED_OFF命令,则开启LED灯*/
case LED_ON:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].status = ON;
pdata->leds[arg].blink = DISABLE;
break;
/*如果传入LED_BLINK命令,则闪烁LED灯*/
case LED_BLINK:
if(pdata->nleds <= arg)
{
printk("LED%ld doesn't exist\n", arg);
return -ENOTTY;
}
pdata->leds[arg].blink = ENABLE;
pdata->leds[arg].status = ON;
break;
default:
dbg_print("%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);
print_led_help();
return -EINVAL;
}
return 0;
}
定时器中断服务函数实现的主要功能是控制LED等的闪烁
void led_timer_handler(unsigned long data)
{
int i;
struct s3c_led_platform_data *pdata = (struct s3c_led_platform_data *)data;
/*如果LED灯状态为开,则关闭LED灯,否则则关闭LED灯*/
for(i=0; inleds; i++)
{
if(ON == pdata->leds[i].status)
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, pdata->leds[i].active_level);
}
else
{
s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level);
}
if(ENABLE == pdata->leds[i].blink ) /* LED should blink */
{
/* Switch status between 0 and 1 to turn LED ON or off */
pdata->leds[i].status = pdata->leds[i].status ^ 0x01;
}
/*修改定时器的超时时间*/
mod_timer(&(led_device.blink_timer), jiffies + TIMER_TIMEOUT);
}
}
到此,plat_led驱动的重要函数的分析到此为止。。