免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 527 | 回复: 0
打印 上一主题 下一主题

读linux2.6驱动的一点收获 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-24 21:59 |只看该作者 |倒序浏览

                ================================
[color="#ff0000"]Author: taoyuetao
Email: tao_yuetao@yahoo.com.cn
Blog: http://www.eetop.cn/blog/?11145
2006-11-21
================================
从2.6版本开始引入了platform这个概念,在开发底层驱动程序时,首先要确认的就是设备的资源信息,例如设备的地址,
在2.6内核中将每个设备的资源用结构platform_device来描述,该结构体定义在kernel\include\linux\platform_device.h中,
struct platform_device {
const char * name;
u32  id;
struct device dev;
u32  num_resources;
struct resource * resource;
};
该结构一个重要的元素是resource,该元素存入了最为重要的设备资源信息,定义在kernel\include\linux\ioport.h中,
struct resource {
const char *name;
unsigned long start, end;         //一般会是硬件寄存器的地址 或者 IRQ中断号
unsigned long flags;
struct resource *parent, *sibling, *child;
};
下面举个例子来说明一下:
在kernel\arch\arm\mach-pxa\pxa27x.c定义了
tatic struct resource pxa27x_ohci_resources[] = {
[0] = {
  .start  = 0x4C000000,  .end    = 0x4C00ff6f,  .flags  = IORESOURCE_MEM,
},
[1] = {
  .start  = IRQ_USBH1,  .end    = IRQ_USBH1,  .flags  = IORESOURCE_IRQ,
},
};
这里定义了两组resource,它描述了一个usb host设备的资源,第1组描述了这个usb host设备所占用的
总线地址范围,IORESOURCE_MEM表示第1组描述的是内存类型的资源信息,第2组描述了这个usb host设备
的中断号,IORESOURCE_IRQ表示第2组描述的是中断资源信息。设备驱动会根据flags来获取相应的资源信息。
有了resource信息,就可以定义platform_device了:
static struct platform_device ohci_device = {
.name  = "pxa27x-ohci",
.id  = -1,
.dev  = {
  .dma_mask = &pxa27x_dmamask,
  .coherent_dma_mask = 0xffffffff,
},
.num_resources  = ARRAY_SIZE(pxa27x_ohci_resources),
.resource       = pxa27x_ohci_resources,
};
有了platform_device就可以调用函数platform_add_devices向系统中添加该设备了,这里的实现是
static int __init pxa27x_init(void)
{
return platform_add_devices(devices, ARRAY_SIZE(devices));
}
这里的pxa27x_init必须在设备驱动加载之前被调用,可以把它放到
subsys_initcall(pxa27x_init);
驱动程序需要实现结构体struct platform_driver,参考kernel\driver\usb\host\ohci-pxa27.c,
static struct platform_driver ohci_hcd_pxa27x_driver = {
.probe  = ohci_hcd_pxa27x_drv_probe,   //被调用来查询一个特定设备的存在
.remove  = ohci_hcd_pxa27x_drv_remove,
#ifdef CONFIG_PM
.suspend = ohci_hcd_pxa27x_drv_suspend,
.resume  = ohci_hcd_pxa27x_drv_resume,
#endif
.driver  = {
  .name = "pxa27x-ohci",
},
};
在驱动初始化函数中调用函数platform_driver_register()注册platform_driver,需要注意的是ohci_device结构中name元素和ohci_hcd_pxa27x_driver结构中driver.name必须是相同的,这样在platform_driver_register()注册时会对所有已注册的所有platform_device中的name和当前注册的platform_driver的driver.name进行比较,只有找到相同的名称的platfomr_device才能注册成功,当注册成功时会调用platform_driver结构元素probe函数指针,这里就是ohci_hcd_pxa27x_drv_probe。
当进入probe函数后,需要获取设备的资源信息,获取资源的函数有:
struct resource * platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);
根据参数type所指定类型,例如IORESOURCE_MEM,来获取指定的资源。
struct int platform_get_irq(struct platform_device *dev, unsigned int num);
获取资源中的中断号。
struct resource * platform_get_resource_byname(struct platform_device *dev, unsigned int type, char *name);
根据参数name所指定的名称,来获取指定的资源。
int platform_get_irq_byname(struct platform_device *dev, char *name);
根据参数name所指定的名称,来获取资源中的中断号。
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40912/showart_406958.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP