linux平台设备驱动简单模板

驱动模板:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>


#define DEVICE_NAME    "hello"    

static int MYDRIVER_Major = 0;
static struct class *hello_class;

static int hello_open(struct inode *inode, struct file *file)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_release(struct inode *inode, struct file *file)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_write(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
	printk("%s enter\n", __func__);
	return 0;
}

static int hello_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	return 0;
}

static struct file_operations hello_fops =
{
	.owner  =   THIS_MODULE,
	.open   =   hello_open, 
	.release =  hello_release,
	.read   =   hello_read,
	.write  =   hello_write,
	.unlocked_ioctl  =   hello_ioctl,
};

static int hello_probe(struct platform_device *pdev)
{
	int ret = 0;
	printk("%s enter\n", __func__);

	MYDRIVER_Major = register_chrdev(0, DEVICE_NAME, &hello_fops);
	if (MYDRIVER_Major < 0)
	{
		printk(DEVICE_NAME " can't register major number\n");
		return MYDRIVER_Major;
	}
	printk("register My Driver OK! Major = %d\n", MYDRIVER_Major);

	//注册一个类,使mdev可以在"/dev/"目录下面建立设备节点
	hello_class = class_create(THIS_MODULE, DEVICE_NAME);
	if(IS_ERR(hello_class))
	{
		printk("Err: failed in My Driver class. \n");
		return -1;
	}
	//创建一个设备节点,节点名为DEVICE_NAME
	device_create(hello_class, NULL, MKDEV(MYDRIVER_Major, 0), NULL, DEVICE_NAME);

	printk(DEVICE_NAME " initialized\n");

	return 0;
}

static int hello_remove(struct platform_device *pdev)
{
	printk("%s enter\n", __func__);

	free_irq(IRQ_EXT0, NULL);
	device_destroy(hello_class, MKDEV(MYDRIVER_Major, 0));
	class_destroy(hello_class);
	unregister_chrdev(MYDRIVER_Major, DEVICE_NAME);

	return 0;
}


static struct platform_driver hello_platform_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.driver         = {
		.name   = "hello",
		.owner  = THIS_MODULE,
	},
};

module_platform_driver(hello_platform_driver);

MODULE_AUTHOR("zwy");        
MODULE_DESCRIPTION("platform driver template");    
MODULE_LICENSE("GPL");

添加一个设备

static struct platform_device hello_device = {
	.name = "hello",
};

Makefile:

PWD = $(shell pwd)  
KERNEL_SRC = /home/wuyu/linuxbsp/kernel

MODULE_NAME = hello.o
   
obj-m := $(MODULE_NAME)  
module-objs := $(MODULE_NAME)   
   
all:  
       $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules  

clean:  
       rm *.ko  
       rm *.o  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值