标签:linux驱动
<span style="font-size:12px;">extern struct device *device_create(struct class *cls, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...) __attribute__((format(printf, 5, 6)));</span>
#include <linux/module.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/poll.h>
#include <linux/device.h>
static struct class *Myleddrv_class; //自动注册驱动主设备
static struct device *Myleddrv_dev;
volatile unsigned long *gpbcon = NULL; //控制寄存器
volatile unsigned long *gpbdat = NULL; //数据寄存器
static int Myleddrv_open(void)
{
printk("Hello Linux World!\n");
return 0;
}
static int Myleddrv_write(void)
{
return 0;
}
static struct file_operations Myleddrv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = Myleddrv_open,
.write = Myleddrv_write,
};
static int major; //全局变量
static int Myleddrv_init(void)
{
major = register_chrdev(0, "Myleddrv", &Myleddrv_fops); // 注册, 告诉内核
Myleddrv_class = class_create(THIS_MODULE, "Myleddrv");
Myleddrv_dev = device_create(Myleddrv_class, NULL, MKDEV(major, 0), NULL, "Myleddrv"); /* /dev/Myleddrv */
return 0;
}
static void Myleddrv_exit(void)
{
unregister_chrdev(major, "Myleddrv"); // 卸载
printk("Myleddrv has been unregistered!\n");
device_unregister(Myleddrv_dev);
device_destroy(Myleddrv_class,major);
}
module_init(Myleddrv_init);
module_exit(Myleddrv_exit);
MODULE_LICENSE("GPL");
标签:linux驱动
原文地址:http://blog.csdn.net/xy010902100449/article/details/45394981