Linux设备模型实例

led_dev.c

/*
	分配/设置/注册一个platform_device
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

static struct resource led_resource[] = {
//	[0] = DEFINE_RES_MEM(0x56000050, 8),
//	[1] = DEFINE_RES_IRQ(4),	
};
struct platform_device led_dev = {
	.name		= "myled",
	.id		= 0,
	.num_resources	= ARRAY_SIZE(led_resource),
	.resource	= led_resource,
};

//入口函数
static int led_dev_init(void)
{
	//注册一个平台设备
	platform_device_register(&led_dev);
	return 0;
}

//出口函数
static void led_dev_exit(void)
{
	platform_device_unregister(&led_dev);
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");




led_drv.c

/*
	分配/设置/注册一个platform_driver
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//probe函数
static int led_probe(struct platform_device *pdev)
{
	//根据platform_device的资源进行ioremap
	
	//注册字符设备驱动程序
	printk(KERN_ALERT "led_probe: find led!\n");
	return 0;
}


static int led_remove(struct platform_device *pdev)
{
	//卸载字符设备驱动程序
	//iounmap
	printk(KERN_ALERT "led_remove: remove led!\n");
	return 0;
}

//注册一个platform driver
static struct platform_driver led_drv = {
	.probe		= led_probe,
	.remove		= led_remove,
	.driver		= {
		.name	= "myled",
		.owner	= THIS_MODULE,
	}
};


//入口函数
static int led_drv_init(void)
{
	platform_driver_register(&led_drv);
	return 0;
}

//出口函数
static void led_drv_exit(void)
{
	platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit)
MODULE_LICENSE("GPL");


你可能感兴趣的:(Linux设备模型)