一个简单的platform设备驱动
硬/软件环境:s3c2440/linux-2.6.36/busybox-1.18.4/arm-linux-gcc 4.4.3
下面是模块的c代码:
#include
<linux/module.h>
#include
<linux/init.h>
#include
<linux/fs.h>
#include
<linux/cdev.h>
#include
<linux/mm.h>
#include
<linux/ioport.h>
#include
<linux/interrupt.h>
#include
<linux/delay.h>
#include
<linux/device.h>
#include
<linux/platform_device.h>
#include
<asm/io.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zl");
int zl_probe(struct device
* dev)
{
struct platform_device
*pdev = container_of(dev,
struct platform_device, dev);
struct resource
*res = pdev->resource;
printk("driver zl_probe.\n");
return 0;
}
int zl_remove(struct device
* dev)
{
printk("driver zl_remove.\n");
return 0;
}
struct device_driver zl_driver_struct
= {
.name
= "zl_device",
//this string will be show as /sys/bus/platform/drivers/zl_device
.bus
= &platform_bus_type,
.probe
= zl_probe,
.remove
= zl_remove,
};
void zl_release(struct device
*dev)
{
printk("device release.\n");
}
struct resource zl_res[]
= {
{
.name
= "zl_res_name",
.start
= 0x56000000,
.end
= 0x57000000,
.flags
= IORESOURCE_IO,
},
{
.start
= IRQ_EINT2,
.end
= IRQ_EINT2,
.flags
= IORESOURCE_IRQ,
},
};
struct platform_device zl_dev
= {
.name
= "zl_device", //this string will be show as /sys/devices/platform/zl_device.0
.dev
= {
.release
= zl_release,
},
.num_resources
= ARRAY_SIZE(zl_res),
.resource
= zl_res,
};
int test_init(void)
{
int ret
= 0;
platform_device_register(&zl_dev);
driver_register(&zl_driver_struct);
return ret;
}
void test_exit(void)
{
driver_unregister(&zl_driver_struct);
platform_device_unregister(&zl_dev);
}
module_init(test_init);
module_exit(test_exit);
|
下面是Makefile文件内容:
#KERNEL = /home/zl/my2440-2.6.13
KERNEL = /media/STUDY/linux/kernel/my2440-2.6.36
#KERNEL = /lib/modules/$(shell uname
-r)/build
default:
make -C $(KERNEL) M=$(shell
pwd) modules
clean:
make -C $(KERNEL) M=$(shell
pwd) modules clean
modules_install:
make -C $(KERNEL) M=$(shell
pwd) modules_install INSTALL_MOD_PATH=/home/zl/s3c2440_nfs
depmod -a
-b /home/zl/s3c2440_nfs 2.6.36-MY2440
obj-m += driver.o
|
执行make命令后,“insmod driver.ko”插入模块后
/sys/devices/platform/zl_device.0文件生成
/sys/bus/platform/devices/目录下有名为zl_device.0的链接文件指向../../../devices/platform/zl_device.0
/sys/bus/platform/drivers/zl_device/目录下有zl_device.0软链接指向../../../../devices/platform/zl_device.0
如果platform_device结构体中name与device_driver结构中的name值相同就会执行probe指向的函数zl_probe函数