这里简单总结下platform总线的设备驱动代码的框架。 1、建立文件夹platform 2、在文件夹下编写设备文件device.c #include <linux/device.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/string.h> #include <linux/platform_device.h> MODULE_AUTHOR("WJB"); MODULE_LICENSE("Dual BSD/GPL"); static struct platform_device *my_device; static int __init my_device_init(void) { int ret = 0; my_device = platform_device_alloc("my_dev", -1); ret = platform_device_add(my_device); if(ret) platform_device_put(my_device); return ret; } static void my_device_exit(void) { platform_device_unregister(my_device); } module_init(my_device_init); module_exit(my_device_exit); 3、在文件夹下编写驱动文件driver.c #include <linux/device.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/string.h> #include <linux/platform_device.h> MODULE_AUTHOR("WJB"); MODULE_LICENSE("Dual BSD/GPL"); static int my_probe(struct device* dev) { printk("driver found device which my driver can handle!/n"); return 0; } static int my_remove(struct device* dev) { printk("drvier found device unpluged!/n"); } static struct platform_driver my_driver = { .probe = my_probe, .remove = my_remove, .driver = { .owner = THIS_MODULE, .name = "my_dev", }, }; static int __init my_driver_init(void) { return platform_driver_register(&my_driver); } static void my_driver_exit(void) { platform_driver_unregister(&my_driver); } module_init(my_driver_init); module_exit(my_driver_exit); 4、Makefile文件 ifneq ($(KERNELRELEASE),) obj-m := device.o driver.o else KDIR := /lib/modules/2.6.30-1/build all: make -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.0 *.mod.o *.mod.c *.symvers endif 5、make 执行make命令,将得到device.ko, driver.ko。 6、添加设备 使用insmod device.ko和insmod driver.ko命令添加设备。 这时候,在/sys/bus/platform/devices下,我们发现我们添加的设备my_dev已经存在了。 另外,在/sys/bus/platform/drivers下,我们发现我们添加的驱动my_dev也已经存在了。 在/sys/bus/platform/devices/my_dev/driver下,有设备my_dev,这说明my_dev设备和my_dev驱动已经联系起来了。 另外,在/sys/bus/platform/drivers/my_dev/device下,有驱动my_dev,这也说明my_dev设备和my_dev驱动已经联系起来了。
浙公网安备 33010602011771号