linux总线设备驱动模型

转载Linux总线、设备、驱动模型

platform_device侧
分配/设置/注册一个platform_device
static struct platform_device led_dev={
.name =“myled”,
.id =-1,
.num_resources=ARRAY_SIZE(led_resource),
.resource =led_resource,
.dev = {
//.release = led_release,
},
};

platform_device_register(&led_dev);
platform_device_unregister(&led_dev);

platform_drive侧
分配/设置/注册一个drv平台
struct platform_driver led_drv = {
.probe = led_probe,//获取资源,注册字符设备驱动程序
.remove = led_remove,
.driver = {
.name = “myled”,
}
};

platform_driver_register(&led_drv);
platform_driver_unregister(&led_drv);

通过命令点亮led

led-dev

#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>

static struct resource led_resource[] = {
		[0] = {
			.start = 0x56000050,//寄存器起始物理地址gpfcon
			.end   = 0x56000050 + 8 - 1,//
			.flags = IORESOURCE_MEM,//资源类
		},
		[1] = {
			.start =4,	//gpf4-led1,gpf5-led2,gpf6-led4修改即可改变点亮的led
			.end   = 4,	//		
			.flags = IORESOURCE_IRQ,
		}
};

	/*分配/设置/注册一个platform_device*/
static struct platform_device led_dev={
	.name	="myled",
	.id		=-1,
	.num_resources=ARRAY_SIZE(led_resource),
	.resource	  =led_resource,
	.dev = { 
    	//.release = led_release, 
	 },
};

static int led_dev_init()
{
	platform_device_register(&led_dev);
	return 0;
}

static int led_dev_exit()
{
	platform_device_unregister(&led_dev);
	return 0;
}

module_init(led_dev_init);//入口函数
module_exit(led_dev_exit);//出口函数
MODULE_LICENSE("GPL");

led-drv

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>

#include <linux/device.h>
#include <mach/gpio.h>

#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/gpio_keys.h>
#include <asm/mach/map.h>

static int major;
static struct class *cls;
static volatile unsigned long *gpio_con;//gpfcon
static volatile unsigned long *gpio_dat;//gpfdat
static int pin;

static int led_open(struct inode *inode, struct file *file)
{
	//printk("first_drv_open\n");
	/* 配置为输出 */
	*gpio_con &= ~(0x3<<(pin*2));
	*gpio_con |= (0x1<<(pin*2));
	return 0;	
}

static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	int val;
	//printk("first_drv_write\n");
	copy_from_user(&val, buf, count); //	copy_to_user();

	if (val == 1)
	{
		*gpio_dat &= ~(1<<pin);// 点亮
	}
	else
	{
		*gpio_dat |= (1<<pin);// 熄灭
	}
	return 0;
}

static struct file_operations led_fops = {
    .owner  =   THIS_MODULE,   
    .open   =   led_open,     
	.write	=	led_write,	   
};

static int led_probe(struct platform_device *pdev)
{
	struct resource *res;

	/*根据platform-device的资源进行ioremap*/
	res=platform_get_resource(pdev,IORESOURCE_MEM,0);//获得这类资源的第一个
	gpio_con=ioremap(res->start,res->end-res->start+1);
	gpio_dat=gpio_con+1;

	res=platform_get_resource(pdev,IORESOURCE_IRQ,0);//获得需要控制的gpio
	pin=res->start;
	
	/*注册字符设备驱动程序*/
	major=register_chrdev(0,"myled",&led_fops);
	cls = class_create(THIS_MODULE, "myled");
	device_create(cls, NULL, MKDEV(major, 0), NULL, "led");
	return 0;
}

static int led_remove(struct platform_device *pdev)
{
	/* 卸载字符设备驱动程序 */
	printk("led_remove, remove led\n");
	device_destroy(cls, MKDEV(major, 0));
	class_destroy(cls);
	unregister_chrdev(major, "myled");
	iounmap(gpio_con);
	return 0;
}

/*定义一个drv平台*/
struct platform_driver led_drv = {
	.probe		= led_probe,
	.remove		= led_remove,
	.driver		= {
		.name	= "myled",
	}
};


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");

测试程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* led_test on/off */
int main(int argc, char **argv)
{
	int fd;
	int val = 1;
	fd = open("/dev/led", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!\n");
	}
	if (argc != 2)
	{
		printf("Usage :\n");
		printf("%s <on|off>\n", argv[0]);
		return 0;
	}

	if (strcmp(argv[1], "on") == 0)
	{
		val  = 1;
	}
	else
	{
		val = 0;
	}
	write(fd, &val, 4);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值