# Platform Driver 平台驅動
Linux中的所有裝置驅動都需要註冊到系統平台下,這此操作由platform_device.h中定義的一組函數完成.我們先來看看struct platform_driver這個結構體:
view plaincopy to clipboardprint?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 (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};
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 (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};
該結構中包含了一組操作函數和一個struct device_driver的對像. 在我們自己的驅動中首先要做的就是定義platform_driver中的函數,並建立這個結構的一個對象執行個體, 然後在init()函數中調用platform_driver_register()向系統註冊我們的驅動.
如kernel-3.0的leds-s3c24xx.c 中的platform_driver的定義為:
view plaincopy to clipboardprint?static struct platform_driver s3c24xx_led_driver = {
.probe = s3c24xx_led_probe,
.remove = s3c24xx_led_remove,
.driver = {
.name = "s3c24xx_led",
.owner = THIS_MODULE,
},
};
static struct platform_driver s3c24xx_led_driver = {
.probe = s3c24xx_led_probe,
.remove = s3c24xx_led_remove,
.driver = {
.name = "s3c24xx_led",
.owner = THIS_MODULE,
},
};
其它中指定了.driver的名字和所有者. 在調用platform_driver_register()註冊驅動時,platform首先指定該裝置是在系統中platform_bus主匯流排下,這隻是一個虛擬匯流排驅動.然後platform驅動在調用driver_register(), driver_register在調用bus_add_driver()向系統匯流排管理器中註冊驅動.如果成功會進行裝置掃描,在dd.c中的driver_attach()中進行.實際會調用__driver_attach(),該函數又調用driver_probe_device(), driver_probe_device()分別會調用驅動註冊時指定的匯流排probe和驅動自己的probe函數, 即我們上面註冊的s3c24xx_led_probe()函數, 匯流排probe函數由platform_driver_register()中設定, 還設定了預設的Driver的操作函數.
在我們的probe函數中主要是進行裝置的探測和初始化,這裡2410的led是串連到gpio上的,所以總是存在,但是如果是一些像usb那樣的外設,我們就需要先探測對外的外設是否存在.在sc324xx_led_probe中主要做了以下3件事.
1.建立一個私人資料對象, 並調用platform_set_drvdata()來附著到裝置對象上.傳給probe函數的參數是一個struct platform_device的平台裝置對像(在那建立的呢?),
struct platform_device在定義如下:
view plaincopy to clipboardprint?struct platform_device {
const char * name;
int id;
struct device dev;
u32 num_resources;
struct resource * resource;
const struct platform_device_id *id_entry;
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
struct platform_device {
const char * name;
int id;
struct device dev;
u32 num_resources;
struct resource * resource;
const struct platform_device_id *id_entry;
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
其中有一個struct device對象,這個對象儲存了系統中一個具體的裝置的相關資訊. 其中driver_data一般用於儲存和裝置具體相關的私人資訊,如這裡用於儲存struct s3c24xx_gpio_led執行個體,但是我們不要直接操作它,而應該使用platform_set_drvdata()函數.
2. 初始化具體的gpio
3. 調用led_classdev_register向核心註冊一個led類驅動
小節: 這這個過程我們可以看到,在一個驅動程式中需要做那些事情,及基本的流程.
1.需要定義一個platform_driver的對象來描述驅動相關資訊.
2.實現platform_driver中定義的必要的函數
3.向平台驅動管理器註冊驅動.
4.驅動管理器會調用.probe函數來探測裝置,需要在這裡做裝置初始化.
5.核心根據操作請求調用驅動的相應函數,如open, close等.
# Platform Device 平台裝置
在platform driver中核心回調用我們註冊的.probe函數,參數為一個platform_device的對象指標,但是這個從那來的呢, 以s3c2410 smdk板為例,對應的mach為mach-s3c2410,具體用的那個要看我們在配置核心的時候選擇的什麼平台和mach了,具體怎麼配置或增加mach,不在本文的範圍. 現在開啟arch/arm/mach-s3c2410/mach-smdk2410.c 在檔案的最後有個MACHINE_START的結構體:
view plaincopy to clipboardprint?MACHINE_START(SMDK2410, "SMDK2410")
.phys_io = S3C2410_PA_UART,
.io_pg_offst = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
.boot_params = S3C2410_SDRAM_PA + 0x100,
.map_io = smdk2410_map_io,
.init_irq = s3c24xx_init_irq,
.init_machine = smdk_machine_init,
.timer = &s3c24xx_timer,
MACHINE_END
MACHINE_START(SMDK2410, "SMDK2410")
.phys_io = S3C2410_PA_UART,
.io_pg_offst = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
.boot_params = S3C2410_SDRAM_PA + 0x100,
.map_io = smdk2410_map_io,
.init_irq = s3c24xx_init_irq,
.init_machine = smdk_machine_init,
.timer = &s3c24xx_timer,
MACHINE_END
該結構體描述了該mach相關的系統資源,核心在初始化時,根據該結構體的內容,進行初始化操作.找到其中指定的smdk_machine_init函數, 該函數的內容不多,現在我們只關心:
platform_add_devices(smdk_devs, ARRAY_SIZE(smdk_devs));
這個調用, 進入platform_add_devices可以看到在該函數中有一個for迴圈來遍曆devs參數中的platform_device,然後調用platform_device_register註冊裝置.從而產生前面我們的s3c24xx_led_probe收到的platform_device.
那主個devs又是那來的呢,回到上一層,可以看到傳入的是一個靜態定義的platform_device的數組,這裡面就是根據我們的mach定義那的一些設定的配置資訊,有點好pc的bios.所以要修改硬體設定就需要從這裡開始.
作者“歡迎來到牛棚”