1. 程式人生 > >驅動學習回顧——platform匯流排驅動-結合LED例項分析

驅動學習回顧——platform匯流排驅動-結合LED例項分析

前一篇文章總結了Linux下的bus裝置模型,實際上,我們在程式設計過程中,並不需要自己來建立匯流排,Linux已經為我們實現了諸如platform匯流排、USB匯流排、I2C匯流排等等。
而接下來要學習總結的platform匯流排有點“特別”,與USB、I2C、SPI等物理匯流排不同,它是一條虛擬匯流排。像USB裝置、I2C裝置都會有自己對應型別的匯流排進行掛載,從而與CPU通訊。但是在嵌入式系統中,並不是所有的裝置都能歸屬於這些常見的匯流排,所以Linux為了保證裝置驅動的統一和完整,“虛擬”出了這麼一條匯流排。
platform只是Linux匯流排模型下的一個子類。

下面以以前做過的在platform上實現的LED驅動程式來進行總結。
和之前一樣先從__init

入手:

一、

static int __init s3c_led_init(void)
{
   int       ret = 0;

   ret = platform_device_register(&s3c_led_device);
   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"
); 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; }

1、可以看到在這個s3c_led_init 函式中s3c_led_device 是通過platform_device_register 進行了裝置的註冊。

//drivers/base/platform.c
int platform_device_register(struct platform_device *pdev)
{
    device_initialize(&pdev->dev);
    return platform_device_add(pdev);
}
EXPORT_SYMBOL_GPL(platform_device_register);

platform_device_register 進而呼叫platform_device_add
,在這個函式裡會將父裝置設定為 platform_bus(即:/sys/devices/platform),然後設定將裝置掛在platform 匯流排下(即:platform_bus_type),接著對平臺裝置資源做處理,最後將平臺裝置新增入系統中(即:/sys/devices/platform/xxx)。

2、s3c_led_init 中接著進行驅動的註冊platform_driver_register。同樣看到這個函式:

//drivers/base/platform.c
 /**
 * platform_driver_register - register a driver for platform-level devices
 * @drv: platform driver structure
 */
int platform_driver_register(struct platform_driver *drv)
{
    drv->driver.bus = &platform_bus_type;  //裝置驅動掛在platform下
    /*對 drv 中的函式指標進行填充*/
    if (drv->probe)
        drv->driver.probe = platform_drv_probe;
    if (drv->remove)
        drv->driver.remove = platform_drv_remove;
    if (drv->shutdown)
        drv->driver.shutdown = platform_drv_shutdown;

    return driver_register(&drv->driver);  //註冊裝置驅動
}
EXPORT_SYMBOL_GPL(platform_driver_register);

二、

接下來進入s3c_led_device 看看:

static struct platform_device s3c_led_device = {
    .name    = "s3c_led",
    .id      = 1,
    .dev     = 
    {
        .platform_data = &s3c_led_data, 
        .release = platform_led_release,
    },
};

裡面的name id 是為後面的match 驅動和裝置的匹配,platform_data 存放的是設個裝置(led)的資源。繼續進入s3c_led_data

static struct s3c_led_platform_data s3c_led_data = {
    .leds = s3c_leds,
    .nleds = ARRAY_SIZE(s3c_leds),
};

進入s3c_leds

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,
    }, 
};

這是一個結構體型別的陣列s3c_leds[] ,裡面的成員就儲存的是每一個裝置的物理屬性。

三、

再來看驅動是怎麼運作的,s3c_led_driver

static struct platform_driver s3c_led_driver = { 
    .probe      = s3c_led_probe, 
    .remove     = s3c_led_remove, 
    .driver     = { 
        .name       = "s3c_led", 
        .owner      = THIS_MODULE, 
    },
};

看到了熟悉的身影: probe ,前一篇博文講到,probe 在驅動匹配完成後執行,用以驅動的初始化。remove 也提到了,是用作驅動和裝置分離、釋放的。只要驅動或裝置有一方離開了匯流排,就會執行這個函式。

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的初始化,nleds 前面有,是一共4個led燈。全部熄滅作為初始狀態。

    /* Initialize the LED status */
    for(i=0; i<pdata->nleds; 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); 
         }
    }

下面的程式碼部分就很熟悉了,跟普通字元裝置的流程相似。1.分配主、次裝置號(手動分配 or 自動分配);2.cdev_init 建立字元裝置結構體變數,並與led_fops (file operation)關聯起來,cdev_add 將該裝置新增進核心

    /*  Alloc the device for driver */
    if (0 != dev_major) 
    { 
        devno = MKDEV(dev_major, dev_minor); 
        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 led structure and register cdev*/
    memset(&led_device, 0, sizeof(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; 
    } 

下面這段程式碼是自動建立裝置節點,省掉了手動mknode 的過程。


    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

下面一段是跟定時器有關,等下再看led_timer_handler 做了什麼。

    /*  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;

}

下面是有關定時器的函式:

void led_timer_handler(unsigned long data)
{ 
    int  i; 
    struct s3c_led_platform_data *pdata = (struct s3c_led_platform_data *)data;

    for(i=0; i<pdata->nleds; 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);
    }
}

這段程式碼作用就是:定時讀取每一個LED的引腳狀態到active_level 裡,若裝置有開啟blink 則在每個迴圈內反轉一次狀態。
請留意這裡有一個mod_timermod_timer()會重新註冊定時器到核心,而不管定時器函式是否被執行過。
注意!一個重要的知識點:jiffies。 關於jiffies考慮單獨做一個總結,這個機制很有參考意義,比如可以借鑑到微控制器的開發上。這裡先留一個引子。

remove

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;
}

這也是有一套流程的:回收主裝置號;登出字元裝置變數;這裡新增了清除定時器和銷燬dev_class

四、

登出平臺裝置:

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__);
    platform_device_unregister(&s3c_led_device);
}

很簡單,platform_driver_unregister 登出驅動,platform_device_unregister 登出裝置。

整個led例項的過程以圖示說明:(比較簡陋,僅供參考。。)
這裡寫圖片描述
關於platform在核心中是如何一步步新增到bus上的就不深究了。從網上摘錄一個圖:
這裡寫圖片描述

最後給出完整程式碼:

#include "s3c_driver.h"

#define DRV_AUTHOR                "Tangbin"
#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; i<pdata->nleds; 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; i<pdata->nleds; 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*/
};


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; i<pdata->nleds; 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); 
        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.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;

   ret = platform_device_register(&s3c_led_device);
   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");

   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__);
    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");

相關推薦

no