首页 技术 正文
技术 2022年11月10日
0 收藏 708 点赞 4,639 浏览 2525 个字

转自:http://blog.csdn.net/uruita/article/details/7278313

從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;
 unsigned long flags;
 struct resource *parent, *sibling, *child;
};

下面舉個例子來說明一下:

在kernel\arch\arm\mach-pxa\pxa27x.c定義了
static 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所指定的名称来获取資源中的中断号。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902