Linux驱动-platform总线设备驱动
扫描二维码
随时随地手机看文章
platform_bus_type
:
struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs = platform_dev_attrs,
.match = platform_match, //设备和驱动使用match函数来判断是否匹配
.uevent = platform_uevent,
.pm = PLATFORM_PM_OPS_PTR,
};
/* platform_match函数用于匹配总线中的驱动和设备 */
static int platform_match(struct device *dev, struct device_driver *drv)
{
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv);
/* match against the id table first */
if (pdrv->id_table)
return platform_match_id(pdrv->id_table, pdev) != NULL;
/* fall-back to driver name match */
return (strcmp(pdev->name, drv->name) == 0);
}
platform_match函数首先判断是否platform_driver中定义了id_table,如果有则使用id_table来进行匹配。
否则,判断platform_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的probe函数。
② platform_driver 结构体:
struct device_driver {
const char *name;
struct bus_type *bus;
struct module *owner;
const char *mod_name; /* used for built-in modules */
bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
const struct of_device_id *of_match_table;
const struct acpi_device_id *acpi_match_table;
int (*probe) (struct device *dev);
int (*remove) (struct device *dev);
void (*shutdown) (struct device *dev);
int (*suspend) (struct device *dev, pm_message_t state);
int (*resume) (struct device *dev);
const struct attribute_group **groups;
const struct dev_pm_ops *pm;
struct driver_private *p;
};
platform_driver结构体有device_driver成员,该成员的各自字段如上所示,device_driver也有probe、remove、shutdown等函数,在平台驱动注册的时候被初始化。
前面说过,当系统中存在有平台设备和平台驱动通过总线的match函数匹配后则会调用platform_driver的probe函数,参数为platform_device,有时候也通过id_table来判断是否匹配。
struct platform_device_id {
char name[PLATFORM_NAME_SIZE];
kernel_ulong_t driver_data
__attribute__((aligned(sizeof(kernel_ulong_t))));
};
②-Ⅰ 平台驱动的注册使用platform_driver_register函数:
int platform_driver_register(struct platform_driver *drv)
{
drv->driver.bus = &platform_bus_type;
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;
if (drv->suspend)
drv->driver.suspend = platform_drv_suspend;
if (drv->resume)
drv->driver.resume = platform_drv_resume;
return driver_register(&drv->driver);
}
先初始化platform_driver里的driver,该driver的类型为device_driver,设置driver的bus为platform_bus_type;
设置driver的probe为platform_drv_probe;
设置driver的remove为platform_drv_remove;
设置driver的shutdown为platform_drv_shutdown;
设置driver的suspend为platform_drv_suspend;
设置driver的resume为platform_drv_resume,最后调用driver_register函数来注册平台驱动。
②-Ⅱ 平台驱动的注销使用platform_driver_unregister函数:
void platform_driver_unregister(struct platform_driver *drv)
{
driver_unregister(&drv->driver);
}
③ platform_device 结构体:
struct platform_device {
const char * name; /* 名字 */
int id;
struct device dev;
u32 num_resources; /* 资源总数 */
struct resource * resource; /* 资源 */
struct platform_device_id *id_entry;
};
其中有个重要的成员是resource,是设备的资源信息,如IO地址,中断号等。
struct resource {
resource_size_t start; //资源的起始值
resource_size_t end; //资源的结束值
const char *name;
unsigned long flags; //资源的类型,如IORESOURCE_IO,IORESOURCE_MEM,IORESOURCE_IRQ,IORESOURCE_DMA
struct resource *parent, *sibling, *child;
};
有的设备可能有多个资源,通常使用platform_get_resource函数来获取资源
/**
* platform_get_resource - get a resource for a device
* @dev: platform device
* @type: resource type
* @num: resource index
*/
struct resource *platform_get_resource(struct platform_device *dev,
unsigned int type, unsigned int num)
{
int i;
for (i = 0; i < dev->num_resources; i++) {
struct resource *r = &dev->resource[i];
if (type == resource_type(r) && num-- == 0)
return r;
}
return NULL;
}
③-Ⅰ 平台设备的注册,使用platform_device_register函数:
int platform_device_register(struct platform_device *pdev)
{
device_initialize(&pdev->dev);
return platform_device_add(pdev);
}
platform_device_register函数先通过device_initialize函数初始化platform_device的device成员,然后调用platform_device_add向内核添加一个平台设备。
int platform_device_add(struct platform_device *pdev)
{
int i, ret = 0;
if (!pdev) /* 如果pdev为空则返回EINVAL */
return -EINVAL;
/* 如果pdev->dev.parent为空则将pdev->dev.parent设置为platform_bus */
if (!pdev->dev.parent)
pdev->dev.parent = &platform_bus;
pdev->dev.bus = &platform_bus_type; /* 设置总线类型 */
if (pdev->id != -1) /* 如果id = -1则表示自动分配name */
dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
else
dev_set_name(&pdev->dev, pdev->name);
for (i = 0; i < pdev->num_resources; i++) {
struct resource *p, *r = &pdev->resource[i]; /* 获取资源 */
if (r->name == NULL)
r->name = dev_name(&pdev->dev);
p = r->parent;
if (!p) {
if (resource_type(r) == IORESOURCE_MEM) /* 设置资源类型 */
p = &iomem_resource;
else if (resource_type(r) == IORESOURCE_IO)
p = &ioport_resource;
}
if (p && insert_resource(p, r)) {
printk(KERN_ERR
"%s: failed to claim resource %dn",
dev_name(&pdev->dev), i);
ret = -EBUSY;
goto failed;
}
}
pr_debug("Registering platform device '%s'. Parent at %sn",
dev_name(&pdev->dev), dev_name(pdev->dev.parent));
/* 向内核添加一个device */
ret = device_add(&pdev->dev);
if (ret == 0)
return ret;
failed:
while (--i >= 0) {
struct resource *r = &pdev->resource[i];
unsigned long type = resource_type(r);
if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
release_resource(r);
}
return ret;
}
platform_device_add最终调用device_add来完成平台设备的注册。
③-Ⅱ 平台设备的注销,使用platform_device_unregister函数:
void platform_device_unregister(struct platform_device *pdev)
{
platform_device_del(pdev);
platform_device_put(pdev);
}
platform_device_unregister函数调用platform_device_del函数来注销平台设备
void platform_device_del(struct platform_device *pdev)
{
int i;
if (pdev) {
device_del(&pdev->dev);
for (i = 0; i < pdev->num_resources; i++) {
struct resource *r = &pdev->resource[i];
unsigned long type = resource_type(r);
if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
release_resource(r);
}
}
}
platform_device_del函数调用device_del函数来删除平台设备。
相应地,要释放资源应调用release_resource函数,前提是资源的类型为IORESOURCE_MEM或者IORESOURCE_IO
三、用platform总线设备驱动来点亮led
定义平台设备驱动:led_drv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int major;
static struct class *cls;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;
static int led_open(struct inode *inode, struct file *file)
{
//printk("first_drv_openn");
/* 配置为输出 */
*gpio_con &= ~(0x3<<(pin*2));
*gpio_con |= (0x1<<(pin*2));
return 0;
}
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
//printk("first_drv_writen");
copy_from_user(&val, buf, count); // copy_to_user();
if (val == 1)
{
// 点灯
*gpio_dat &= ~(1<start,res->end - res->start +1);
gpio_dat = gpio_con + 1;
res = platform_get_resource(pdev,IORESOURCE_IRQ,0);
pin = res->start;
/* 注册字符设备驱动程序 */
printk("led_probe, found ledn");
major = register_chrdev(0,"czgled",&led_fops); // 注册字符设备驱动程序 , 0表示系统自动分配主设备号
cls = class_create(THIS_MODULE,"czgled");// 创建类"czg_led"
class_device_create(cls,NULL,MKDEV(major,0),NULL,"led");/* /dev/led */
return 0;
}
static int led_remove(struct platform_device *pdev)
{
/* 卸载字符设备驱动程序 */
/* iounmap */
printk("led_remove, remove ledn");
class_device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
unregister_chrdev(major,"czgled");
iounmap(gpio_con);
return 0;
}
/* 分配/设置platform_driver */
struct platform_driver led_drv = {
.probe = led_probe, // 匹配成功后将调用的函数
.remove = led_remove, // 与pobe功能相关,做清理工作
.driver = {
.name = "czgled", //必须和platform_device的name一样
}
};
/* 注册一个platform_driver */
static int led_drv_init(void)
{
platform_driver_register(&led_drv);
return 0;
}
/* 卸载platform_driver */
static void led_drv_exit(void)
{
platform_driver_unregister(&led_drv);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
定义平台设备:led_dev.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct resource led_resource[] = {
[0] = {
.start = 0x56000050,// gpfcon寄存器开始地址
.end = 0x56000050 + 8 - 1, // gpfcon寄存器结束地址
.flags = IORESOURCE_MEM, // 标志
},
[1] = {
.start = 4, // gpfdat 4
.end = 4,
.flags = IORESOURCE_IRQ, // 标记
}
};
static void led_release(struct device * dev)
{
}// do nothing
/* 分配/设置platform_device */
static struct platform_device led_dev = {
.name = "czgled", // 必须和platform_driver内嵌的driver.name一样
.id = -1,
.num_resources = ARRAY_SIZE(led_resource), // 资源大小
.resource = led_resource, // 前面的资源数组
.dev = {
.release = led_release, // 必须设置,空的也可以
},
};
/* 注册一个platform_device */
static int led_dev_init(void)
{
platform_device_register(&led_dev);
return 0;
}
/* 卸载platform_device */
static void led_dev_exit(void)
{
platform_device_unregister(&led_dev);
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
Makefile:
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += led_drv.o
obj-m += led_dev.o
测试程序:led_test.c
#include
#include
#include
#include
/* led_test on
* led_test off
*/
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
printf("can't open!n");
}
if (argc != 2)
{
printf("Usage :n");
printf("%s n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 1;
}