linux下的platform模型【device & driver】

                                                                      linux下的platform模型【device & driver】

一、platform模型

     在PC设备中,对于USB和PCI(Peripheral Component Interconnect)设备,例如鼠标,都是通过厂商ID和设备ID匹配,从而实现driver& device分离的效果。

    在linux中,LED等设备没有ID对应,因此需要通过虚拟驱动链和设备链,实现platform模型。

二、linux下platform模型实现【例:plat_led.c】      

      由于没有ID,在linux下,设备与驱动是用设备名进行配对,所以,在添加设备的时候,一定要确认设备与驱动同名。

   Linux下的虚拟总线platform对应设备platform_device,对应的驱动为platform_driver。

/*********************************************************************************
 *      Copyright:  (C) 2011 Guo Wenxue<guowenxue@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  s3c_led.c
 *    Description:  This is the common LED driver runs on S3C24XX.
 *                 
 *        Version:  1.0.0(10/27/2011~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/27/2011 11:39:10 AM"
 *                 
 ********************************************************************************/
#include "s3c_driver.h"

#define DRV_AUTHOR                "Guo Wenxue <guowenxue@gmail.com>"
#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,
    }, 
};
/*
P1:是设置LED的硬件信息。每个LED的信息(struct s3c_led_info中GPIO,LED号等),LED信息
(s3c_led_platform_data中LED个数),录入每个LED的状态。至此,每一个LED的信息都知道了。
*/
/*  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, //void*类型指针可以被赋值所有类型的地址//
        .release = platform_led_release,
    },
};
/*
P2:通过struct platform_device:s3c_led_device,
将所有LED的信息都汇总到这一个结构体当中,从而可以
通过访问这个结构体一步一步的获取到每一个LED的信息。
*/

/* ===================== 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;
}
/*P1:定义LED的功能函数*/

/*FOPS结构体*/
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*/
};
/*
P2:FOPS汇总功能
*/

static int s3c_led_probe(struct platform_device *dev)/*通过Platform Device 中的platform_device结构
                                                         体访问所有LED信息*/
    struct s3c_led_platform_data *pdata = dev->dev.platform_data; 
    int result = 0;
    int i;
    dev_t devno;

     /* Initialize the LED status */
	 /*gpio口初始化*/
    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*/
	/*初始化cdev,加入FOPS*/
    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;

}
/*
  P1:probe函数是将device和driver联合起来的重要函数。在probe之后会注册设备:
  ret = platform_device_register(&s3c_led_device);linux内核自动将s3c_led_device
  传入*dev。
  P2:由于获取到了设备信息,所以可以进行驱动的一般步骤:初始化硬件,申请主次设备号,
  初始化cdev,加入FOPS[因为在驱动部分已经完成了定义FOPS,所以这里只需要添加】。
*/

/*移除设备*/
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,    //当设备和驱动匹配之后,通过s3c_led_probe注册
    .remove     = s3c_led_remove, 
    .driver     = { 
        .name       = "s3c_led",    // 通过name匹配设备和驱动
        .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");

<span style="font-size:18px;">/*
<strong>总结:</strong>
linux下的platform分为四步:设备部分,驱动部分,probe联合部分以及注册部分。
*/</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值