rfid5-写成platform驱动

上文platform驱动虽然可用,但内层要要写misc驱动,所以不使用设备提供的资源会更加简便。可以如下改一下
Makefile,同上文
ifneq ($(KERNELRELEASE),)  
obj-m :=  platform_dev.o  platform_drv.o 
else  
KDIR := /opt/FriendlyARM/mini2440/linux-2.6.32.2  
#KDIR := /lib/modules/`uname -r`/build
all:  
	make -C $(KDIR) M=$(PWD) modules  
clean:  
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers  
endif  
设备中去掉资源项,并使用内核alloc的设备在将其add到平台总线,不必registe设备了
/******************platfrom_dev.c***************************/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/kernel.h>

#define DEVICE_NAME "song_rfid"
static struct platform_device *my_device;

static int __init my_init(void)
{
	int ret=0;

	my_device= platform_device_alloc(DEVICE_NAME,-1);//this name is matched for driver,song_rfid
	ret=platform_device_add(my_device);

	if (ret == 0) {
		printk("Register %s\n",DEVICE_NAME);
	} else {
		printk("Register  error.\n");
		platform_device_put(my_device);
	}
	return ret;
}

static void __exit my_exit(void)
{
	platform_device_unregister(my_device);
	printk("Unregister %s\n",DEVICE_NAME);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_VERSION("1.5");

/******************platfrom_drv.c***************************/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/io.h>

#define DRIVER_NAME "song_rfid"


static int  my_probe(struct platform_device *pdev)
{
	printk("driver find device : %s which can handle\n",DRIVER_NAME);
	return 0;
}

static int  my_remove(struct platform_device *pdev)
{
	printk("driver found device : %s unpluged\n",DRIVER_NAME);
	return 0;
}

static struct platform_driver my_driver = {
	.probe 		= my_probe,
	.remove 	= my_remove,
	.driver 	= {
		.owner 	= THIS_MODULE,
		.name 	= DRIVER_NAME,//this name is matched for driver,song_rfid
	},
};

static int __init my_init(void)
{
	printk("Register my_driver.\n");
	return platform_driver_register(&my_driver);
}

static void __exit my_exit(void)
{
	printk("Unregister my_driver.\n");
	platform_driver_unregister(&my_driver);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");

[root@FriendlyARM plg]# lsmod 
[root@FriendlyARM plg]# insmod platform_dev.ko 
Register song_rfid
[root@FriendlyARM plg]# insmod platform_drv.ko 
Register my_driver.
driver find device : song_rfid which can handle
[root@FriendlyARM plg]# ls /sys/bus/platform/devices/
dm9000             s3c2410-rtc        s3c2440-sdi        s3c24xx_uda134x.0
regulatory.0       s3c2410-spi.0      s3c2440-uart.0     soc-audio
s3c2410-iis        s3c2410-wdt        s3c2440-uart.1     song_rfid
s3c2410-lcd        s3c2440-i2c        s3c2440-uart.2
s3c2410-ohci       s3c2440-nand       s3c2440-usbgadget
[root@FriendlyARM plg]# ls /sys/bus/platform/drivers
dm9000           s3c2410-ohci     s3c2440-uart     song_rfid
s3c-i2c          s3c2410-rtc      s3c24xx-nand
s3c-sdi          s3c2410-spi      s3c24xx_uda134x
s3c2410-lcd      s3c2412-lcd      soc-audio
[root@FriendlyARM plg]# rmmod platform_dev 
driver found device : song_rfid unpluged
Unregister song_rfid
rmmod: module 'platform_dev' not found
[root@FriendlyARM plg]# rmmod platform_drv
Unregister my_driver.
rmmod: module 'platform_drv' not found
[root@FriendlyARM plg]# lsmod 
[root@FriendlyARM plg]# 



posted on 2011-10-25 10:55  _song  阅读(232)  评论(0编辑  收藏  举报