#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/ioctl.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/fcntl.h>
#include <linux/cdev.h>
#define LED_MAJOR 100
#define LED_MINOR 0
#define SET_GPIO (*(volatile unsigned *)0xbfd010C4)
#define SET_OUT (*(volatile unsigned *)0xbfd010D4)
#define SET_LED (*(volatile unsigned *)0xbfd010F4)
struct led_dev
{
struct cdev cdev;
};
static int my_start (struct inode *, struct file *);
//static ssize_t left (struct file *, char __user *, size_t, loff_t *);
struct class *led_class;
struct led_dev *led_devices;
static struct file_operations led_fops =
{
.owner = THIS_MODULE,
.open = my_start,
// .read = left,
};
static int my_start(struct inode * node, struct file * fil)
{
SET_GPIO = (SET_GPIO | 0x000000C0);
SET_OUT = (SET_OUT & ~(0x000000C0));
SET_LED = (SET_LED & ~(0x000000c0));
printk("Here is open\n");
return 0;
}
static int __init led_init(void)
{
int ret;
dev_t dev = 0;
dev = MKDEV(LED_MAJOR, LED_MINOR); //合并主设备号和次设备号,板上主设备号只能是0-255
ret = register_chrdev_region(dev, 1, "led"); //为驱动注册一个设备号
if(ret)
{
printk("register_fail\n");
}else{
printk("register_succeed\n");
}
led_devices = kmalloc(sizeof(struct led_dev), GFP_KERNEL); //GFP_KERNEL:linux/gfp.h中定义的一个宏,是分配内核空间的内存时的一个标志位
if(led_devices == NULL)
{
printk("malloc_fail\n");
}else{
printk("malloc_succeed\n");
}
memset(led_devices, 0, sizeof(struct led_dev));
cdev_init(&led_devices->cdev, &led_fops); //绑定cdev和自己的函数操作
led_devices->cdev.owner = THIS_MODULE; //指向提供驱动程序的模块
led_devices->cdev.ops = &led_fops; //函数操作提供的API
ret = cdev_add(&led_devices->cdev, dev, 1);//把cdev,设备号,设备号范围添加到系统中
if(ret)
{
printk("add_fail\n");
}else{
printk("add_succeed\n");
}
led_class = class_create(THIS_MODULE, "led_class");//创建一个类
device_create(led_class, NULL, dev, NULL, "led");//在/dev目录下创建设备节点
return ret;
}
static void __exit led_exit(void)
{
dev_t devno = MKDEV(LED_MAJOR, LED_MINOR);
unregister_chrdev_region(devno,1);//删除注册的设备号
device_destroy(led_class, devno);//删除/dev目录下的设备节点
printk("exit_succeed\n");
class_destroy(led_class);//删除类
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");//模块许可证声明
记录:LED灯驱动
最新推荐文章于 2022-05-07 10:27:17 发布