歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 設備模型(device-model)之平台總線(bus),驅動(driver),設備(device)

設備模型(device-model)之平台總線(bus),驅動(driver),設備(device)

日期:2017/3/1 9:10:55   编辑:Linux編程

關於關於驅動設備模型相關概念請參考《Linux Device Drivers》等相關書籍,和內核源碼目錄...\Documentation\driver-model

Linux Device Drivers [PDF] 下載見 http://www.linuxidc.com/Linux/2011-06/37778.htm

簡單來說總線(bus),驅動(driver),設備(device)這三者之間的關系就是:驅動開發者可以通過總線(bus)來將驅動(driver)和設備(device)進行隔離,這樣的好處就是開發者可以將相對穩定不變的驅動(driver)獨立起來,可以通過總線(bus)來橋接與之匹配的設備(device)。設備(device)只需要提供與硬件相關的底層硬件的配置,如io,中斷等。

platform.c 提供了一個平台總線(platform_bus),和注冊平台設備(platform_device)和平台驅動(platform_driver)的相關接口,其中平台總線(platform_bus)已經編進內核,開發者只需要提供平台設備(platform_device)和平台驅動(platform_driver)的相關代碼就行了。

在linux源碼目錄\drivers\input\keyboard下,提供了一個gpio_keys.c的平台驅動(platform_driver),這個就是一個簡單地按鍵驅動,檢測到按鍵狀態,上報給輸入子系統。

因此,開發者需要做的就是提供一個平台設備(platform_device),以向平台驅動(platform_driver)提供相關的硬件配置,如按鍵IO,中斷號,按鍵碼等等。

gpio_keys.c (不用任何改動)

/*
* Driver for keys on GPIO lines capable of generating interrupts.
*
* Copyright 2005 Phil Blundell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#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 <linux/gpio_keys.h>

#include <asm/gpio.h>

static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
{
int i;
/* [cgw]: 在gpio_keys_probe()中調用了request_irq(..., pdev),
* 因此dev_id指向了platform_device *pdev,通過dev_id間接傳遞
* platform_device *指針
*/
struct platform_device *pdev = dev_id;
/* [cgw]: 當platform_device 和 platform_driver匹配時,會通過
* probe()傳遞platform_device進來。在注冊platform_device時,
* platform_device.dev.platform_data必須指向gpio_keys_platform_data *
*/
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
/* [cgw]: probe()中已分配了一個struct input_dev,並通過platform_set_drvdata()
* 設置platform_device->dev->driver_data = input;
*/
struct input_dev *input = platform_get_drvdata(pdev);

/* [cgw]: 輪詢pdata->nbuttons個按鍵 */
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
int gpio = button->gpio;

/* [cgw]: 某個gpio發生了中斷 */
if (irq == gpio_to_irq(gpio)) {
/* [cgw]: 獲得按鍵類型 */
unsigned int type = button->type ?: EV_KEY;
/* [cgw]: 獲取按鍵狀態 */
int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
/* [cgw]: 發送按鍵事件 */
input_event(input, type, button->code, !!state);
/* [cgw]: 發送同步事件 */
input_sync(input);
}
}

return IRQ_HANDLED;
}

static int __devinit gpio_keys_probe(struct platform_device *pdev)
{
/* [cgw]: 在gpio_keys_probe()中調用了request_irq(..., pdev),
* 因此dev_id指向了platform_device *pdev,通過dev_id間接傳遞
* platform_device *指針
*/
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
struct input_dev *input;
int i, error;

/* [cgw]: 分配一個輸入設備 */
input = input_allocate_device();
/* [cgw]: 分配失敗 */
if (!input)
return -ENOMEM;

/* [cgw]: 設置platform_device->dev->driver_data = input */
platform_set_drvdata(pdev, input);

/* [cgw]: 設置evdev.c支持的事件類型 */
input->evbit[0] = BIT(EV_KEY);
/* [cgw]: 設置輸入設備名,同platform_device的名字 */
input->name = pdev->name;
/* [cgw]: */
input->phys = "gpio-keys/input0";
/* [cgw]: 設置輸入設備dev的父節點為platform_device->dev */
input->dev.parent = &pdev->dev;

/* [cgw]: 設置輸入設備總線類型,供應商,產品,版本 */
input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;

/* [cgw]: 為pdata->nbuttons個按鍵申請中斷 */
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
/* [cgw]: 獲得gpio對應的中斷號 */
int irq = gpio_to_irq(button->gpio);
/* [cgw]: 獲得按鍵類型 */
unsigned int type = button->type ?: EV_KEY;

/* [cgw]: 設置中斷類型為邊沿中斷 */
set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
/* [cgw]: 申請中斷,設置中斷服務程序 */
error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
button->desc ? button->desc : "gpio_keys",
pdev);
if (error) {
printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n",
irq, error);
goto fail;
}

/* [cgw]: 設置evdev.c支持的按鍵碼 */
input_set_capability(input, type, button->code);
}

/* [cgw]: 注冊一個輸入設備 */
error = input_register_device(input);
/* [cgw]: 注冊失敗 */
if (error) {
printk(KERN_ERR "Unable to register gpio-keys input device\n");
goto fail;
}

return 0;

fail:
/* [cgw]: 釋放中斷 */
for (i = i - 1; i >= 0; i--)
free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
/* [cgw]: 釋放輸入設備占用的內存 */
input_free_device(input);

return error;
}

static int __devexit gpio_keys_remove(struct platform_device *pdev)
{
/* [cgw]: 在gpio_keys_probe()中調用了request_irq(..., pdev),
* 因此dev_id指向了platform_device *pdev,通過dev_id間接傳遞
* platform_device *指針
*/
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
/* [cgw]: probe()中已分配了一個struct input_dev,並通過platform_set_drvdata()
* 設置platform_device->dev->driver_data = input;
*/
struct input_dev *input = platform_get_drvdata(pdev);
int i;

/* [cgw]: 釋放中斷 */
for (i = 0; i < pdata->nbuttons; i++) {
int irq = gpio_to_irq(pdata->buttons[i].gpio);
free_irq(irq, pdev);
}

/* [cgw]: 注銷輸入設備 */
input_unregister_device(input);

return 0;
}

struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.remove = __devexit_p(gpio_keys_remove),
.driver = {
.name = "gpio-keys",
}
};

static int __init gpio_keys_init(void)
{
/* [cgw]: 注冊gpio_keys_device_driver平台驅動 */
return platform_driver_register(&gpio_keys_device_driver);
}

static void __exit gpio_keys_exit(void)
{
/* [cgw]: 注銷gpio_keys_device_driver平台驅動 */
platform_driver_unregister(&gpio_keys_device_driver);
}

module_init(gpio_keys_init);
module_exit(gpio_keys_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Phil Blundell <[email protected]>");
MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");

平台設備(platform_device):
keys_dev.c

#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/gpio_keys.h>
#include <linux/input.h>
#include <linux/irq.h>

#include <asm/gpio.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>

/* [cgw]: 設置四個按鍵的鍵碼,按鍵io,激活狀態,按鍵名,按鍵類型 */
static struct gpio_keys_button keys_buff[4] = {
{
KEY_A,
S3C2410_GPF0,
1,
"A",
EV_KEY
},

{
KEY_B,
S3C2410_GPF2,
1,
"B",
EV_KEY
},

{
KEY_C,
S3C2410_GPG3,
1,
"C",
EV_KEY
},

{
KEY_D,
S3C2410_GPG11,
1,
"D",
EV_KEY
},
};


static struct gpio_keys_platform_data keys_dev = {
.buttons = &keys_buff[0],
.nbuttons = ARRAY_SIZE(keys_buff)
};

static void keys_dev_release(struct device * dev)
{
printk("keys_dev_release! \n");
}

/* [cgw]: 分配一個平台設備 */
static struct platform_device keys_platform_dev = {
.name = "gpio-keys",
.id = -1,
.dev = {
.release = keys_dev_release,
.platform_data = (void *)&keys_dev,
},
};


static int keys_dev_init(void)
{
/* [cgw]: 注冊keys_platform_dev平台設備 */
platform_device_register(&keys_platform_dev);
return 0;
}

static void keys_dev_exit(void)
{
/* [cgw]: 注銷keys_platform_dev平台設備 */
platform_device_unregister(&keys_platform_dev);
}

module_init(keys_dev_init);
module_exit(keys_dev_exit);

MODULE_LICENSE("GPL");

應用測試程序:

platform_test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include <linux/input.h>

int fd;

void my_signal_fun(int signum)
{
struct input_event buttons_event;

/* [cgw]: 異步通知產生時返回的數據 */
read(fd, &buttons_event, sizeof(struct input_event));

/* [cgw]: 打印事件類型,事件碼,事件值 */
printf("type: 0x%x code: 0x%x value: 0x%x\n",
buttons_event.type,
buttons_event.code,
buttons_event.value);
}

int main(int argc, char **argv)
{
int ret, arg;
struct pollfd fds[1];
unsigned long ver = 0;

fd = open("/dev/event1", O_RDWR | O_NONBLOCK);

if (fd < 0)
{
printf("can't open!\n");
}

ioctl(fd, EVIOCGVERSION, &ver);
printf("Ver:0x%x \n", ver);

/* [cgw]: 設置文件標識符 */
fds[0].fd = fd;
/* [cgw]: 設置應用程序要響應的事件 */
fds[0].events = POLLIN;

while (1)
{
/* [cgw]: 休眠5S */
ret = poll(fds, 1, 5000);

/* [cgw]: 喚醒或超時 */
//printf("wake up!\n");
if (ret == 0)
{
printf("time out\n");
}
else
{
my_signal_fun(arg);
}
}

close(fd);

return 0;
}

平台設備(platform_device) keys_platform_dev 是怎樣找到與之匹配的平台驅動(platform_driver) gpio_keys_device_driver的呢?

因為他們都注冊到了平台總線(platform_bus)上。平台驅動(platform_driver)和平台設備(platform_device)會相互查找彼此是否匹配,匹配的條件就是,

1 static int platform_match(struct device * dev, struct device_driver * drv)
2 {
3     struct platform_device *pdev = container_of(dev, struct platform_device, dev);
4 
5     return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
6 }

platform_device->name == platform_driver->driver->name 即他們的名字就是:“gpio-keys”

一旦匹配,就會調用

static int platform_drv_probe(struct device *_dev)
{
struct platform_driver *drv = to_platform_driver(_dev->driver);
struct platform_device *dev = to_platform_device(_dev);

return drv->probe(dev);
}

drv->probe() = platform_driver->probe() = gpio_keys_probe()

這樣平台驅動(platform_driver)就可以獲得平台設備(platform_device)的 struct platform_device結構的數據了,即keys_platform_dev,從這個結構就可以獲得相關配置,以使能平台驅動(platform_driver) gpio_keys.c中相應的IO和中斷。

實驗現象:

# insmod gpio_keys.ko                     //安裝平台驅動(platform_driver)
# insmod keys_dev.ko                      //安裝平台設備(platform_device)
input: gpio-keys as /class/input/input1
# ./platform_test                         //運行應用測試程序
Ver:0x10000                               //用ioctl獲取輸入子系統的版本號
type: 0x1 code: 0x2e value: 0x1           //按下"C"鍵
type: 0x0 code: 0x0 value: 0x0            //因為調用了input_sync()
type: 0x1 code: 0x2e value: 0x0           //松開(彈起)"C"鍵
type: 0x0 code: 0x0 value: 0x0            //因為調用了input_sync()
type: 0x1 code: 0x30 value: 0x1           //... ...
type: 0x0 code: 0x0 value: 0x0
type: 0x1 code: 0x30 value: 0x0
type: 0x0 code: 0x0 value: 0x0
type: 0x1 code: 0x1e value: 0x1
type: 0x0 code: 0x0 value: 0x0
type: 0x1 code: 0x1e value: 0x0
type: 0x0 code: 0x0 value: 0x0
type: 0x1 code: 0x20 value: 0x1
type: 0x0 code: 0x0 value: 0x0
type: 0x1 code: 0x20 value: 0x0
type: 0x0 code: 0x0 value: 0x0
time out
time out
time out

Copyright © Linux教程網 All Rights Reserved