您的位置:首页 > 运维架构 > Linux

Linux设备模型(下)之Platform

2013-08-26 15:54 330 查看
从Linux2.6内核起,引入一套新的驱动管理和注册机制:platform_device和 platform_driver 。Linux 中大部分的设备驱动,都可以使用这套机制,设备用 platform_device 表示;驱动用platform_driver 进行注册。 platform是一个虚拟的地址总线,相比pci,usb,它主要用于描述SOC上的片上资源,比如s3c2440上集成的控制器(lcd,watchdog,rtc等), platform所描述的资源有一个共同点,就是在cpu的总线上直接取址。平台设备会分到一个名称(用在驱动绑定中)以及一系列诸如地址和中断请求号(IRQ)之类的资源.

[cpp]
view plaincopyprint?

struct platform_device { const char * name; intid; struct device dev; u32num_resources; struct resource * resource; };

struct platform_device {
const char * name;
intid;
struct device dev;
u32num_resources;
struct resource * resource;
};


平台驱动遵循标准驱动模型的规范, 平台驱动按标准规范对电源管理和关机通告提供支 持

[cpp]
view plaincopyprint?

struct platform_driver { int (*probe)(struct platform_device *); int (*remove)(struct platform_device *); void (*shutdown)(struct platform_device *); int (*suspend)(struct platform_device *, pm_message_t state); int (*suspend_late)(struct platform_device *, pm_message_t state); int (*resume_early)(struct platform_device *); int (*resume)(struct platform_device *); struct device_driver driver; };
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*suspend_late)(struct platform_device *, pm_message_t state);
int (*resume_early)(struct platform_device *);
int (*resume)(struct platform_device *);
struct device_driver driver;
};


probe()总应该核实指定的设备硬件确实存在;平台设置代码有时不能确定这一点

系统为platform总线定义一个bus_type的实例platform_bus_type,通过其成员函数match(),确定device和driver如何匹配。

匹配platform_device和platform_driver主要看二者的name字段是否相同。(name必须要相同才能匹配)

用platform_device_register(platform_device*)函数注册单个的平台设备。

一般是在平台的BSP文件中定义platform_device,通过platform_add_devices(platform_device*)函数将平台设备注册到系统中platform_driver 的注册与注销:

platform_driver_register(platform_driver*)

platform_driver_unregister(platform_driver*)

设备资源(struct resource)

关于资源的操作(平台设备会分到一系列诸如地址和中断请求号(IRQ)之类的资源)

struct resource {

resource_size_t start;

resource_size_t end;

const char *name;

unsigned long flags;

// IORESOURCE_IO

//IORESOURCE_MEM

//IORESOURCE_IRQ

//IORESOURCE_DMA

struct resource *parent, *sibling, *child;

};

基于资源的分类(flags)有I/O端口、IRQ、DMA等等,而I/O端口又分为2种类型,IORESOURCE_IO(I/O映射) IORESOURCE_MEM(内存映射)CPU对外设IO端口物理地址的编址方式有2种:一种是IO映射方式(IO-mapped), 另一种是内存映射方式(Memory-mapped)。具体采用哪一种方式则取决于CPU的体系结构。 像X86体系对外设就专门实现了一个单独地址空间,并且有专门的I/O指令来访问I/O端口,像ARM体系结构通常只是实现一个物理地址空间,I/O端口就被映射到CPU的单一物理地址空间中,而成为内存的一部分,所以一般资源都采用(IORESOURCE_MEM)。linux中对设备的资源按照资源树的结构来组织(其实就是一个链表结构的插入、删除、查找等操作),上面再添加设备(platform_device_add)的同时对相应的资源在资源树上进行插入操作

int insert_resource(struct resource *parent, struct resource *new)

关于platform resource有相关的函数进行对资源的操作。

struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);

int platform_get_irq(struct platform_device *, unsigned int);

struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);

int platform_get_irq_byname(struct platform_device *, char *);

例如s3c2440的watchdog资源分配实例:

watchdog寄存器的基地址为0x5300000

#define S3C2410_PA_WATCHDOG (0x53000000)

#define S3C24XX_SZ_WATCHDOG SZ_1M

static struct resource s3c_wdt_resource[] = {

[0] = {

.start = S3C24XX_PA_WATCHDOG,

.end = S3C24XX_PA_WATCHDOG + S3C24XX_SZ_WATCHDOG - 1,

.flags = IORESOURCE_MEM, //内存映射

},

[1] = {

.start = IRQ_WDT,

.end = IRQ_WDT,

.flags = IORESOURCE_IRQ, //IRQ

}

};

plat_button_device.c:

[cpp]
view plaincopyprint?

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <mach/regs-gpio.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/io.h>

/*平台资源的定义*/
static struct resource s3c_buttons_resource[] = {
[0]={
.start = S3C24XX_PA_GPIO,// (0x56000000)
.end = S3C24XX_PA_GPIO + S3C24XX_SZ_GPIO - 1,//(0x56000000) + 0x00100000 -1
.flags = IORESOURCE_MEM,
},

[1]={
.start = IRQ_EINT7,
.end = IRQ_EINT7,
.flags = IORESOURCE_IRQ,
},
[2]={
.start = IRQ_EINT1,
.end = IRQ_EINT1,
.flags = IORESOURCE_IRQ,
},
[3]={
.start = IRQ_EINT2,
.end = IRQ_EINT2,
.flags = IORESOURCE_IRQ,
},
[4]={
.start = IRQ_EINT3,
.end = IRQ_EINT3,
.flags = IORESOURCE_IRQ,
},
[5]={
.start = IRQ_EINT4,
.end = IRQ_EINT4,
.flags = IORESOURCE_IRQ,
},
[6]={
.start = IRQ_EINT5,
.end = IRQ_EINT5,
.flags = IORESOURCE_IRQ,
}
};

static struct platform_device *s3c_buttons;

static int __init platform_init(void)
{

s3c_buttons = platform_device_alloc("gec2440-buttons",-1);

platform_device_add_resources(s3c_buttons,&s3c_buttons_resource,7);

/*平台设备的注册*/
platform_device_add(s3c_buttons);
}

static void __exit platform_exit(void)
{
platform_device_unregister(s3c_buttons);
}

module_init(platform_init);
module_exit(platform_exit);

MODULE_AUTHOR("Steven Bai");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:gec2440buttons");

#include <linux/module.h>

#include <linux/types.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <mach/regs-gpio.h>
#include <linux/interrupt.h>

#include <linux/device.h>
#include <linux/io.h>

/*平台资源的定义*/
static struct resource s3c_buttons_resource[] = {
[0]={
.start = S3C24XX_PA_GPIO,//                       (0x56000000)
.end   = S3C24XX_PA_GPIO + S3C24XX_SZ_GPIO - 1,//(0x56000000) + 0x00100000 -1
.flags = IORESOURCE_MEM,
},

[1]={
.start = IRQ_EINT7,
.end   = IRQ_EINT7,
.flags = IORESOURCE_IRQ,
},
[2]={
.start = IRQ_EINT1,
.end   = IRQ_EINT1,
.flags = IORESOURCE_IRQ,
},
[3]={
.start = IRQ_EINT2,
.end   = IRQ_EINT2,
.flags = IORESOURCE_IRQ,
},
[4]={
.start = IRQ_EINT3,
.end   = IRQ_EINT3,
.flags = IORESOURCE_IRQ,
},
[5]={
.start = IRQ_EINT4,
.end   = IRQ_EINT4,
.flags = IORESOURCE_IRQ,
},
[6]={
.start = IRQ_EINT5,
.end   = IRQ_EINT5,
.flags = IORESOURCE_IRQ,
}
};

static struct platform_device *s3c_buttons;

static int __init platform_init(void)
{

s3c_buttons = platform_device_alloc("gec2440-buttons",-1);

platform_device_add_resources(s3c_buttons,&s3c_buttons_resource,7);

/*平台设备的注册*/
platform_device_add(s3c_buttons);
}

static void __exit platform_exit(void)
{
platform_device_unregister(s3c_buttons);
}

module_init(platform_init);
module_exit(platform_exit);

MODULE_AUTHOR("Steven Bai");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:gec2440buttons");

plat_button_driver.c:

[cpp]
view plaincopyprint?

#include <linux/module.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <mach/map.h>
#include <mach/regs-gpio.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/unistd.h>
#include <linux/device.h>

#define DEVICE_NAME "buttons"

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;

static int key_value;
static struct device *buttons_dev; /* platform device attached to */
static struct resource *buttons_mem;
static struct resource *buttons_irq;
static void __iomem *buttons_base;

static int button_irqs[6];

static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
int i;
for(i=0; i<6; i++){
if(irq == button_irqs[i]){
printk("==>interrput number:%d\n",irq);
key_value = i;
ev_press =1;
wake_up_interruptible(&button_waitq);
}
}

return IRQ_RETVAL(IRQ_HANDLED);

}

static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
int i;
int err = 0;
/*注册中断*/
for(i=0; i<6; i++){
if (button_irqs[i] < 0)
continue;

/*中断触发方式:上升沿触发*/
err = request_irq(button_irqs[i],buttons_interrupt,IRQ_TYPE_EDGE_RISING,NULL,NULL);
if(err)
break;
}

if (err) {
i--;
for (; i >= 0; i--) {
if (button_irqs[i] < 0) {
continue;
}
disable_irq(button_irqs[i]);
free_irq(button_irqs[i], NULL);
}
return -EBUSY;
}

ev_press = 0;
return 0;
}

static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
int i;
for (i=0; i<6; i++) {
if (button_irqs[i] < 0) {
continue;
}
free_irq(button_irqs[i],NULL);
}
return 0;
}

static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;
if (!ev_press) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user(buff, &key_value, sizeof(key_value));
return sizeof(key_value);
}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press){
mask |= POLLIN | POLLRDNORM;
}
return mask;
}

static struct file_operations gec2440buttons_fops = {
.owner = THIS_MODULE,
.open = s3c24xx_buttons_open,
.release = s3c24xx_buttons_close,
.read = s3c24xx_buttons_read,
.poll = s3c24xx_buttons_poll,
};

static struct miscdevice gec2440_miscdev = {

.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gec2440buttons_fops,
};

/* device interface */
static int gec2440_buttons_probe(struct platform_device *pdev)
{
struct resource *res;
struct device *dev;
int ret;
int size;
int i;

printk("probe:%s\n", __func__);
dev = &pdev->dev;
buttons_dev = &pdev->dev;

/*平台资源获取*/
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(dev, "no memory resource specified\n");
return -ENOENT;
}

size = (res->end - res->start) + 1;
buttons_mem = request_mem_region(res->start, size, pdev->name);
if (buttons_mem == NULL) {
dev_err(dev, "failed to get memory region\n");
ret = -ENOENT;
goto err_req;
}

buttons_base = ioremap(res->start, size);
if (buttons_base == NULL) {
dev_err(dev, "failed to ioremap() region\n");
ret = -EINVAL;
goto err_req;
}
printk(KERN_DEBUG"probe: mapped buttons_base=%p\n", buttons_base);

/*get irq number*/
for(i=0; i<6; i++)
{
buttons_irq = platform_get_resource(pdev,IORESOURCE_IRQ,i);
if(buttons_irq == NULL)
{
dev_err(dev,"no irq resource specified\n");
ret = -ENOENT;
goto err_map;
}
button_irqs[i] = buttons_irq->start;
printk("button_irqs[%d]=%d\n",i,button_irqs[i]);
}
ret = misc_register(&gec2440_miscdev);

return 0;

err_map:
iounmap(buttons_base);

err_req:
release_resource(buttons_mem);
kfree(buttons_mem);

return ret;
}

static int gec2440_buttons_remove(struct platform_device *dev)
{
release_resource(buttons_mem);
kfree(buttons_mem);
buttons_mem = NULL;

iounmap(buttons_base);
misc_deregister(&gec2440_miscdev);
return 0;
}

/*平台驱动定义*/
static struct platform_driver gec2440buttons_driver = {
.probe = gec2440_buttons_probe,
.remove = gec2440_buttons_remove,
.driver = {
.owner = THIS_MODULE,
.name = "gec2440-buttons",
},
};

static char banner[] __initdata =
"gec2440 Buttons Driver\n";

static int __init buttons_init(void)
{
printk(banner);
/*平台驱动注册*/
platform_driver_register(&gec2440buttons_driver);
return 0;
}

static void __exit buttons_exit(void)
{
platform_driver_unregister(&gec2440buttons_driver);
}

module_init(buttons_init);
module_exit(buttons_exit);

MODULE_AUTHOR("Steven Bai");
MODULE_DESCRIPTION("gec2440 Buttons Driver");
MODULE_LICENSE("GPL");

#include <linux/module.h>

#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>

#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <mach/map.h>
#include <mach/regs-gpio.h>
#include <linux/poll.h>

#include <linux/irq.h>
#include <asm/unistd.h>
#include <linux/device.h>

#define DEVICE_NAME "buttons"

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;

static int key_value;
static struct device     *buttons_dev;	/* platform device attached to */
static struct resource	*buttons_mem;
static struct resource   *buttons_irq;
static void __iomem	    *buttons_base;

static int button_irqs[6];

static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
int i;
for(i=0; i<6; i++){
if(irq == button_irqs[i]){
printk("==>interrput number:%d\n",irq);
key_value = i;
ev_press =1;
wake_up_interruptible(&button_waitq);
}
}

return IRQ_RETVAL(IRQ_HANDLED);

}

static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
int i;
int err = 0;
/*注册中断*/
for(i=0; i<6; i++){
if (button_irqs[i] < 0)
continue;

/*中断触发方式:上升沿触发*/
err = request_irq(button_irqs[i],buttons_interrupt,IRQ_TYPE_EDGE_RISING,NULL,NULL);
if(err)
break;
}

if (err) {
i--;
for (; i >= 0; i--) {
if (button_irqs[i] < 0) {
continue;
}
disable_irq(button_irqs[i]);
free_irq(button_irqs[i], NULL);
}
return -EBUSY;
}

ev_press = 0;
return 0;
}

static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
int i;
for (i=0; i<6; i++) {
if (button_irqs[i] < 0) {
continue;
}
free_irq(button_irqs[i],NULL);
}
return 0;
}

static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;
if (!ev_press) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user(buff, &key_value, sizeof(key_value));
return sizeof(key_value);
}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press){
mask |= POLLIN | POLLRDNORM;
}
return mask;
}

static struct file_operations gec2440buttons_fops = {
.owner   =   THIS_MODULE,
.open    =   s3c24xx_buttons_open,
.release =   s3c24xx_buttons_close,
.read    =   s3c24xx_buttons_read,
.poll    =   s3c24xx_buttons_poll,
};

static struct miscdevice gec2440_miscdev = {

.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gec2440buttons_fops,
};

/* device interface */
static int gec2440_buttons_probe(struct platform_device *pdev)
{
struct resource *res;
struct device *dev;
int ret;
int size;
int i;

printk("probe:%s\n", __func__);
dev = &pdev->dev;
buttons_dev = &pdev->dev;

/*平台资源获取*/
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
dev_err(dev, "no memory resource specified\n");
return -ENOENT;
}

size = (res->end - res->start) + 1;
buttons_mem = request_mem_region(res->start, size, pdev->name);
if (buttons_mem == NULL) {
dev_err(dev, "failed to get memory region\n");
ret = -ENOENT;
goto err_req;
}

buttons_base = ioremap(res->start, size);
if (buttons_base == NULL) {
dev_err(dev, "failed to ioremap() region\n");
ret = -EINVAL;
goto err_req;
}
printk(KERN_DEBUG"probe: mapped buttons_base=%p\n", buttons_base);

/*get irq number*/
for(i=0; i<6; i++)
{
buttons_irq = platform_get_resource(pdev,IORESOURCE_IRQ,i);
if(buttons_irq == NULL)
{
dev_err(dev,"no irq resource specified\n");
ret = -ENOENT;
goto err_map;
}
button_irqs[i] = buttons_irq->start;
printk("button_irqs[%d]=%d\n",i,button_irqs[i]);
}
ret = misc_register(&gec2440_miscdev);

return 0;

err_map:
iounmap(buttons_base);

err_req:
release_resource(buttons_mem);
kfree(buttons_mem);

return ret;
}

static int gec2440_buttons_remove(struct platform_device *dev)
{
release_resource(buttons_mem);
kfree(buttons_mem);
buttons_mem = NULL;

iounmap(buttons_base);
misc_deregister(&gec2440_miscdev);
return 0;
}

/*平台驱动定义*/
static struct platform_driver gec2440buttons_driver = {
.probe		= gec2440_buttons_probe,
.remove		= gec2440_buttons_remove,
.driver		= {
.owner	= THIS_MODULE,
.name	= "gec2440-buttons",
},
};

static char banner[] __initdata =
"gec2440 Buttons Driver\n";

static int __init buttons_init(void)
{
printk(banner);
/*平台驱动注册*/
platform_driver_register(&gec2440buttons_driver);
return 0;
}

static void __exit buttons_exit(void)
{
platform_driver_unregister(&gec2440buttons_driver);
}

module_init(buttons_init);
module_exit(buttons_exit);

MODULE_AUTHOR("Steven Bai");
MODULE_DESCRIPTION("gec2440 Buttons Driver");
MODULE_LICENSE("GPL");


开发板一上电后,可以/sys/bus/platform/下会挂上这个设备和对应的驱动
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: