linux中的platform设备

转自:http://blog.163.com/huawei_d/blog/static/2116102572013631114028201/

linux中的platform设备 

本文转载自边缘之火 《linux中的platform设备》

linux中有一类设备,platform 。平台设备,或者说是总线,在将linux移植到mini2440上时,在mach-mini2440.c中有这样一段代码

static struct platform_device *mini2440_devices[] __initdata = {
 &s3c_device_usb,
 &s3c_device_rtc,
 &s3c_device_lcd,
 &s3c_device_wdt,
 &s3c_device_i2c0,
 &s3c_device_iis,
 &mini2440_device_eth,
 &s3c24xx_uda134x,
 &s3c_device_nand,
 &s3c_device_sdi,
 &s3c_device_usbgadget,
};

这个结构体中的每一个设备都是一个platform device, 这些设备通过 platform_add_devices(mini2440_devices, ARRAY_SIZE(mini2440_devices)); 加到内核中,或者说挂接到platform总线上,例如移植rtc时,似乎只需要在上边那个结构体中增加&s3c_device_rtc,就可以了,但这些platform设备怎么工作呢?

linux驱动的思想是这样的,它把所有的硬件都抽象成一个设备文件,在户空间只要操作这些文件,就可以操作硬件了,接下来看下这些平台设备,怎么对它们操作,例如s3c_device_rtc

static struct resource s3c_rtc_resource[] = {
 [0] = {
  .start = S3C24XX_PA_RTC,
  .end   = S3C24XX_PA_RTC + 0xff,
  .flags = IORESOURCE_MEM,
 },
 [1] = {
  .start = IRQ_RTC,
  .end   = IRQ_RTC,
  .flags = IORESOURCE_IRQ,
 },
 [2] = {
  .start = IRQ_TICK,
  .end   = IRQ_TICK,
  .flags = IORESOURCE_IRQ
 }
};

struct platform_device s3c_device_rtc = {
 .name    = "s3c2410-rtc",
 .id    = -1,
 .num_resources   = ARRAY_SIZE(s3c_rtc_resource),
 .resource   = s3c_rtc_resource,
};

这是时s3c_device_rtc 的定义,从这里可以看到这个设备的名称"s3c2410-rtc"以及s3c_rtc_resource,然后用platform_device_register(s3c_device_rtc );将设备注册,就是告诉内核,存在了这么一个设备。就像在/dev/下创建了一个节点一样,但现在并不能对它操作,比如open ,因为它还没有驱动程序。

接下来在看/driver/rtc/rtc-s3c.c,这里有s3c_device_rtc这个设备的驱动,

static struct platform_driver s3c2410_rtc_driver = {
 .probe  = s3c_rtc_probe,
 .remove  = __devexit_p(s3c_rtc_remove),
 .suspend = s3c_rtc_suspend,
 .resume  = s3c_rtc_resume,
 .driver  = {
  .name = "s3c2410-rtc",
  .owner = THIS_MODULE,
 },
};

static int __init s3c_rtc_init(void)
{
 printk(banner);
 return platform_driver_register(&s3c2410_rtc_driver);
}

static void __exit s3c_rtc_exit(void)
{
 platform_driver_unregister(&s3c2410_rtc_driver);
}

module_init(s3c_rtc_init);
module_exit(s3c_rtc_exit);

MODULE_DESCRIPTION("Samsung S3C RTC Driver");
MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:s3c2410-rtc");

这代码应该很熟悉了,甚至可以看到了module_init(s3c_rtc_init);和module_exit(s3c_rtc_exit);,可以肯定这就是驱动,看这个结构体

static struct platform_driver s3c2410_rtc_driver = {
 .probe  = s3c_rtc_probe,
 .remove  = __devexit_p(s3c_rtc_remove),
 .suspend = s3c_rtc_suspend,
 .resume  = s3c_rtc_resume,
 .driver  = {
  .name = "s3c2410-rtc",
  .owner = THIS_MODULE,
 },

它是platform_driver ,从名字可以看到它和platform_device 是对应的,再注意另一句 platform_driver_register(&s3c2410_rtc_driver);
这里注册了一个驱动,而且这个驱动的名字叫“s3c2410-rtc”这里很关键,这个名字一定要与platform_device的名字一样, 到这里似乎可以了,对比led的驱动,我们可以想,在insmod 时,就会调用module_init(s3c_rtc_init);   ,但是led的module_init一般会注册一个字符设备,然后我们通过设备号创建设备文件,这后就可以操作led了,但对于这个rtc似乎不是这样,在他的module_init(s3c_rtc_init);   时好像只是告知内核,现在存在这么一个driver,其实远不止这些。

           首先,先来找到和用户层系统调用直接相关的文件操作函数结构体,依然在/driver/rtc/rtc-s3c.c中,有
          
  static const struct rtc_class_ops s3c_rtcops = {
 .open  = s3c_rtc_open,
 .release = s3c_rtc_release,
 .read_time = s3c_rtc_gettime,
 .set_time = s3c_rtc_settime,
 .read_alarm = s3c_rtc_getalarm,
 .set_alarm = s3c_rtc_setalarm,
 .irq_set_freq = s3c_rtc_setfreq,
 .irq_set_state = s3c_rtc_setpie,
 .proc         = s3c_rtc_proc,
};

这个结构体很关键了,其实linux中 ,这种ops字段的结构体都属于这类,以后看驱动也是要看这个为用户层提供了什么系统调用。

例如,在用户层,使用open打开s3c_divece_rtc,就会调用s3c_rtc_open 这个函数。接下来分析s3c_rtcops 结构体

怎么和s3c_divece_rtc这个platform设备绑在一起的。


对比leds的驱动,register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);这一条语句就把ops结构体绑定到了设备号为

LED_MAJOR,设备名为DEVICE_NAME的设备上,那么platform设备怎么做呢?

接下来看下这个结构体

static struct platform_driver s3c2410_rtc_driver = {
 .probe  = s3c_rtc_probe,
 .remove  = __devexit_p(s3c_rtc_remove),
 .suspend = s3c_rtc_suspend,
 .resume  = s3c_rtc_resume,
 .driver  = {
  .name = "s3c2410-rtc",
  .owner = THIS_MODULE,
 },

这个platform_driver结构体,没有像ops结构体向用户空间提够系统调用函数,似乎也没有直接操作硬件,看起来相当神秘,其实只要分析一下probe这个函数就会清楚他在做什么,

static int __devinit s3c_rtc_probe(struct platform_device *pdev)
{
 struct rtc_device *rtc;
 struct resource *res;
 int ret;

 pr_debug("%s: probe=%p\n", __func__, pdev);

 /* find the IRQs */

 s3c_rtc_tickno = platform_get_irq(pdev, 1);
 if (s3c_rtc_tickno < 0) {
  dev_err(&pdev->dev, "no irq for rtc tick\n");
  return -ENOENT;
 }

 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
 if (s3c_rtc_alarmno < 0) {
  dev_err(&pdev->dev, "no irq for alarm\n");
  return -ENOENT;
 }

 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
   s3c_rtc_tickno, s3c_rtc_alarmno);

 /* get the memory region */

 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 if (res == NULL) {
  dev_err(&pdev->dev, "failed to get memory region resource\n");
  return -ENOENT;
 }

 s3c_rtc_mem = request_mem_region(res->start,
      res->end-res->start+1,
      pdev->name);

 if (s3c_rtc_mem == NULL) {
  dev_err(&pdev->dev, "failed to reserve memory region\n");
  ret = -ENOENT;
  goto err_nores;
 }

 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
 if (s3c_rtc_base == NULL) {
  dev_err(&pdev->dev, "failed ioremap()\n");
  ret = -EINVAL;
  goto err_nomap;
 }

 /* check to see if everything is setup correctly */

 s3c_rtc_enable(pdev, 1);

  pr_debug("s3c2410_rtc: RTCCON=%02x\n",
   readb(s3c_rtc_base + S3C2410_RTCCON));

 s3c_rtc_setfreq(&pdev->dev, 1);

 device_init_wakeup(&pdev->dev, 1);

 /* register RTC and exit */

 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
      THIS_MODULE);

 if (IS_ERR(rtc)) {
  dev_err(&pdev->dev, "cannot attach rtc\n");
  ret = PTR_ERR(rtc);
  goto err_nortc;
 }

 rtc->max_user_freq = 128;

 platform_set_drvdata(pdev, rtc);
 return 0;

 err_nortc:
 s3c_rtc_enable(pdev, 0);
 iounmap(s3c_rtc_base);

 err_nomap:
 release_resource(s3c_rtc_mem);

 err_nores:
 return ret;
}
在这个函数中有两处加红的地方,是关键,第一处向内核注册RTC设备,同时这个RTC设备绑定了s3c_rtcops结构体,第二处,platform_set_drvdata(pdev, rtc);这个函数可以这样认为,就是将RTC设备插到s3c_device_rtc中,或者这样认为,s3c_device_rtc这个平台设备有很多资源,RTC设备连带它的文件函数操作集只不过是这个平台设备的一部分资源。到这里还有最后一个问题,这个platform设备要想使用,也就意味着module_init(s3c_rtc_init);一定在调用了probe函数之后才能使用这个设备,这个probe是在platform_driver_register(&s3c2410_rtc_driver);中调用的,可以用source insight追溯到。


在来总结一下platform divece 运行的整个流程:

首先module_init(s3c_rtc_init)通过platform_driver_register(&s3c2410_rtc_driver);注册s3c2410_rtc_driver,在注册过程中,内核通过名称"s3c2410-rtc"将驱动和s3c_device_rtc设备对应,接着调用probe函数,注册RTC设备,并插入到s3c_device_rtc设备中,通过这一步就连接了文件操作函数s3c_rtcops s3c_device_rtc设备。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值