led.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
char number = 0;
struct device_node *dnode;
struct gpio_desc *gpiono;
unsigned int irqno[3] = {0};
char key[3][6] = {"key1", "key2", "key3"};
char kbuf[128] = {0};
// 定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
// 分配tasklet对象
struct tasklet_struct tasklet;
// tasklet低半部处理函数
void tasklet_callback(struct tasklet_struct *tasklet)
{
int i;
for (i = 1; i < 101; i++)
{
printk("i=%d\n", i);
}
}
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
unsigned int arg = (unsigned int)dev_id;
switch (arg)
{
case 0:
number = number ^ 1;
gpiod_set_value(gpiono, number);
printk("key1 interrupt\n");
condition = 1;
wake_up_interruptible(&wq_head);
break;
case 1:
printk("key2 interrupt\n");
break;
case 2:
printk("key3 interrupt\n");
break;
}
tasklet_schedule(&tasklet);
return IRQ_HANDLED;
}
int mycdev_open(struct inode *inode, struct file *file)
{
// unsigned int led_minor = MINOR(inode->i_rdev);
// file->private_data = (void *)led_minor;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
kbuf[0] = number;
if (sizeof(kbuf) < size)
size = sizeof(kbuf);
wait_event_interruptible(wq_head, condition);
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user failed");
return -EIO;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
condition = 0;
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int ret, i;
// 初始化等待队列头
init_waitqueue_head(&wq_head);
dnode = of_find_node_by_path("/myleds");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
// 根据设备树节点解析led1 gpio结构体并向内核注册
gpiono = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono);
}
gpiod_set_value(gpiono, 0);
// 分配字符设备驱动对象空间
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备对象空间失败\n");
ret = -EFAULT;
goto out1;
}
// 字符设备对驱动象部分初始化
cdev_init(cdev, &fops);
// 申请设备号
if (major > 0)
{
ret = register_chrdev_region(MKDEV(major, minor), 1, "myled");
if (ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else
{
ret = alloc_chrdev_region(&devno, minor, 1, "myled");
if (ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
// 注册字符设备驱动对象
ret = cdev_add(cdev, MKDEV(major, minor), 1);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
// 向上提交目录
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
for (i = 0; i < 1; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret = -PTR_ERR(dev);
goto out5;
}
printk("向上提交设备节点信息成功\n");
}
// 解析设备树节点
dnode = of_find_node_by_name(NULL, "mykeys");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
for (i = 0; i < 3; i++)
{
// 获取软中断号
irqno[i] = irq_of_parse_and_map(dnode, i);
if (!irqno[i])
{
printk("软中断号获取失败\n");
return -ENOMEM;
}
printk("软中断获取成功irqno%d=%d\n", i, irqno[i]);
// 注册中断
ret = request_irq(irqno[i], myirq_handler, IRQF_TRIGGER_FALLING, key[i], (void *)i);
if (ret)
{
printk("注册驱动失败");
return ret;
}
printk("key%d中断注册成功\n", i);
}
// 初始化tasklet对象
tasklet_setup(&tasklet, tasklet_callback);
return 0;
out5:
for (--i; i < 1; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major, minor), 1);
out2:
kfree(&cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
gpiod_set_value(gpiono, 0);
gpiod_put(gpiono);
// 注销中断
for (i = 0; i < 3; i++)
{
free_irq(irqno[i], (void *)i);
}
// 1.销毁设备信息
for (i = 0; i < 1; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 2.销毁目录
class_destroy(cls);
// 3.注销对象
cdev_del(cdev);
// 4.释放设备号
unregister_chrdev_region(MKDEV(major, minor), 1);
// 5.释放对象空间
kfree(&cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/myled0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
memset(buf, 0, sizeof(buf));
read(fd, buf, sizeof(buf));
printf("buf:%s\n", buf);
}
close(fd);
return 0;
}