Linux 驅動之 platform 驅動模型總結 (基於tiny210 平台 LED 驅動)

來源:互聯網
上載者:User

標籤:linux驅動

1、概述

在一般情況下,2.6核心中已經初始化並掛載了一條platform匯流排在sysfs檔案系統中。那麼我們編寫platform模型驅動時,需要完成兩個工作:1:實現platform驅動 2:實現platform裝置,然而在實現這兩個工作的過程中還需要實現其他的很多小工作,在後面介紹。platform模型驅動的實現過程核心架構就很簡單,如下所示。

platform驅動模型三個對象:platform匯流排、platform裝置、platform驅動。platform匯流排對應的核心結構:struct bus_type-->它包含的最關鍵的函數:match()platform裝置對應的核心結構:struct platform_device-->註冊:platform_device_register(unregiste)platform驅動對應的核心結構:struct platform_driver-->註冊:platform_driver_register(unregiste)

簡單介紹下platform驅動的工作過程:裝置(或驅動)註冊的時候,都會引發匯流排調用自己的match函數來尋找目前platform匯流排是否掛載有與該裝置(或驅動)名字匹配的驅動(或裝置)( http://blog.csdn.net/xy010902100449/article/details/45700523 前面我們講了platform裝置驅動的match自動匹配過程), 如果存在則將雙方綁定;如果先註冊裝置,驅動還沒有註冊,那麼裝置在被註冊到匯流排上時,將不會匹配到與自己同名的驅動,然後在驅動註冊到匯流排上時,因為裝置登入,那麼匯流排會立即匹配與綁定這時的同名的裝置與驅動,再調用驅動中的probe函數等;如果是驅動先註冊,同裝置驅動一樣先會匹配失敗,匹配失敗將導致它的probe函數暫不調用,而是要等到裝置註冊成功並與自己匹配綁定後才會調用。

2、代碼

/* * Author: ZP1015  * * Copyright 2015 SCUT. * * platform  device for led  */#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <asm/io.h>#include <asm/uaccess.h>#include <linux/device.h>#include <linux/cdev.h>#include <linux/platform_device.h>static void Myled_platform_device_release(struct device * dev){    return ;}static struct resource Myled_resource[] = {        [0] = {                .start = 0xe0200280,                .end   = 0xe0200280 + 12,                .flags = IORESOURCE_MEM        },};static struct platform_device Myledplatform_device_led = {        .name           = "Myled_platform_device_driver",        .id             = -1,        .num_resources  = ARRAY_SIZE(Myled_resource),        .resource       = Myled_resource,        .dev            = {              .release  = Myled_platform_device_release,        },};static int __init Myled_platform_device_init(void){    printk("Myled_platform_device add ok!\n");return platform_device_register(&Myledplatform_device_led);}static void __exit Myled_platform_device_exit(void){    printk("Myled_platform_device remove ok!\n");platform_device_unregister(&Myledplatform_device_led);}MODULE_AUTHOR("ZP1015");MODULE_LICENSE("GPL");module_init(Myled_platform_device_init);module_exit(Myled_platform_device_exit);

/* * Author: ZP1015  * * Copyright 2015 SCUT. * * platform  driver for led  */#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <asm/io.h>#include <asm/uaccess.h>#include <linux/device.h>#include <linux/cdev.h>#include <linux/platform_device.h>#define GLOBAL_LED_MAJOR  250static unsigned int global_led_major = GLOBAL_LED_MAJOR; static struct cdev *led_cdev = NULL; static struct class *led_class = NULL;volatile unsigned long *gpbcon = NULL;volatile unsigned long *gpbdat = NULL; volatile unsigned long *gpbup = NULL; static int Myled_open(struct inode * inode,struct file * file){return 0;}static ssize_t Myled_read(struct file * file,const char __user * in,size_t size,loff_t * off){return 0;}static ssize_t Myled_write(struct file * file,const char __user * in,size_t size,loff_t * off){return 0;}static void myled_configure(void){*gpbcon &= ~((0x3<<(0*4)) | (0x3<<(1*4)) | (0x3<<(2*4)) | (0x3<<(3*4)));*gpbcon |=  ((0x1<<(0*4)) | (0x1<<(1*4)) | (0x1<<(2*4)) | (0x1<<(3*4)));}static void myled_on(void){*gpbdat &= ~((1<<0) | (1<<1) | (1<<2) | (1<<3));// 點燈}static void myled_off(void){*gpbdat |= (1 << 0)|(1 << 1)|(1 << 2)|(1 << 3);// 滅燈}struct file_operations led_fops = {.owner = THIS_MODULE,.open  = Myled_open,.read  = Myled_read,.write = Myled_write,};static int __devinit Myled_probe(struct platform_device *pdev){int ret;int err;dev_t devno;struct resource *pIORESOURCE_MEM;devno = MKDEV(global_led_major,0);printk(KERN_ALERT"Myled_probe!\n");if (devno) {ret = register_chrdev_region(devno,1,"Myled_platfor_driver");} else {ret = alloc_chrdev_region(&devno,0,1,"Myled_platfor_driver");global_led_major = MAJOR(devno);}if (ret < 0) {return ret;}led_cdev = cdev_alloc();cdev_init(led_cdev,&led_fops);led_cdev->owner = THIS_MODULE;err = cdev_add(led_cdev,devno,1);led_class = class_create(THIS_MODULE,"Myled_platfor_driver");device_create(led_class,NULL,MKDEV(global_led_major,0),NULL,"platfor_driver_for_Myled");pIORESOURCE_MEM = platform_get_resource(pdev,IORESOURCE_MEM,0);gpbcon = ioremap(pIORESOURCE_MEM->start,pIORESOURCE_MEM->end - pIORESOURCE_MEM->start);gpbdat = gpbcon + 1;gpbup  = gpbcon + 2;myled_configure();myled_on();if (err) {printk(KERN_NOTICE"Error %d adding led_cdev",err);return -1;} else {printk(KERN_NOTICE"platform_driver_for_Myled init ok!\n");return 0;}}static int __devexit Myled_remove(struct platform_device *pdev){printk("Myled_remove!\n");cdev_del(led_cdev);iounmap(gpbcon);unregister_chrdev_region(MKDEV(global_led_major,0),1);device_destroy(led_class, MKDEV(global_led_major,0));class_destroy(led_class);myled_off();return 0;}static struct platform_driver Myled_platform_driver = {.probe  = Myled_probe,.remove = __devexit_p(Myled_remove),.driver = {.name = "Myled_platform_device_driver",.owner = THIS_MODULE,}};static int __init Myled_platform_driver_init(void){    printk("platform_driver_for_Myled init\n");return platform_driver_register(&Myled_platform_driver);}static void __exit Myled_platform_driver_exit(void){    printk("platform_driver_for_Myled exit\n");platform_driver_unregister(&Myled_platform_driver);}MODULE_AUTHOR("ZP1015");MODULE_LICENSE("GPL");module_param(global_led_major,int,S_IRUGO);module_init(Myled_platform_driver_init);module_exit(Myled_platform_driver_exit);

3、 實現結果先註冊裝置,然後註冊裝置驅動
insmod 一下燈全亮,rmmod 一下等全滅。

Linux 驅動之 platform 驅動模型總結 (基於tiny210 平台 LED 驅動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.