驱动模板:
#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