Linux驱动开发之平台驱动实例分析

1、驱动源代码

#include <linux/module.h> 
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>

MODULE_LICENSE("Dual BSD/GPL");


static int driver_probe(struct platform_device *dev)
{
printk("platform: match ok!\n");
return 0;
}


static int driver_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}


static void device_release(struct device *dev)
{
printk("platform: device remove\n");
}


struct platform_device test_device = {
.id = -1,
.name = "test_device",
.dev.release = device_release, 
};


struct platform_driver test_driver = {
.probe = driver_probe,
.remove = __devexit_p(driver_remove),
.driver = {
.name = "test_device",
},
};


static int __init platform_init(void)
{
platform_device_register(&test_device);
platform_driver_register(&test_driver);
printk("platform: driver installed\n");
return 0;
}


static void __exit platform_exit(void)
{
platform_driver_unregister(&test_driver);
platform_device_unregister(&test_device);
printk("platform: driver uninstalled!\n");
}

module_init(platform_init);

module_exit(platform_exit);

2、Makefile


ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:                               
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:                                             
        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
else
    obj-m := plat.o

endif

猜你喜欢

转载自blog.csdn.net/xiezhi123456/article/details/81040254