内核文件:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include "myled.h"
int major, minor = 0;
struct cdev *cdev;
struct class *cls;
struct device *dev;
//定义一个指向设备节点的指针
struct device_node *node;
struct device_node *node_led;
struct property *pr; //属性结构体指针
int gpiono1;
struct gpio_desc *desc_led1;
struct gpio_desc *desc_led2;
struct gpio_desc *desc_led3;
int chdev_open(struct inode *inode, struct file *file)
{
return 0;
}
int chdev_close(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t chdev_read(struct file *file, char __user *ubuf, size_t szie, loff_t *loff)
{
return 0;
}
ssize_t chdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
return 0;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret, which;
switch (cmd)
{
case LedOFF:
ret = copy_from_user(&which, (void *)arg, sizeof(int));
if (ret)
{
printk("用户向内核拷贝数据失败\n");
return -EIO;
}
switch (which)
{
case 1:
gpiod_set_value(desc_led1, 0);
break;
case 2:
gpiod_set_value(desc_led2, 0);
break;
case 3:
gpiod_set_value(desc_led3, 0);
break;
}
break;
case LedON:
ret = copy_from_user(&which, (void *)arg, sizeof(int));
if (ret)
{
printk("用户向内核拷贝数据失败\n");
return -EIO;
}
switch (which)
{
case 1:
gpiod_set_value(desc_led1, 1);
break;
case 2:
gpiod_set_value(desc_led2, 1);
break;
case 3:
gpiod_set_value(desc_led3, 1);
break;
}
break;
default:
printk("功能码错误\n");
break;
}
return 0;
}
struct file_operations fops = {
.open = chdev_open,
.read = chdev_read,
.write = chdev_write,
.release = chdev_close,
.unlocked_ioctl = ioctl,
};
static int __init mycdev_init(void)
{
int ret;
dev_t devno;
cdev = cdev_alloc();
if (NULL == cdev)
{
printk("初始化驱动设备对象失败\n");
ret = -ENOMEM;
goto ERR1;
}
cdev_init(cdev, &fops);
ret = alloc_chrdev_region(&devno, minor, 1, "my_led");
if (ret)
{
printk("申请设备号失败\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
ret = cdev_add(cdev, MKDEV(major, minor), 1);
if (ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
cls = class_create(THIS_MODULE, "chdev_led");
if (IS_ERR(cls))
{
printk("向上提交节点失败\n");
goto ERR4;
}
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");
if (IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
//根据设备节点路径获取设备节点信息
node = of_find_node_by_path("/leds");
if (node == NULL)
{
printk("获取节点信息失败\n");
return ENODATA;
}
node_led = of_get_child_by_name(node, "extend-leds");
if (NULL == node_led)
{
printk("获取子节点失败\n");
}
//获取gpio编号
desc_led1 = gpiod_get_from_of_node(node_led, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(desc_led1))
{
printk("获取申请gpio编号失败\n");
return PTR_ERR(desc_led1);
}
desc_led2 = gpiod_get_from_of_node(node_led, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(desc_led1))
{
printk("获取申请gpio编号失败\n");
return PTR_ERR(desc_led1);
}
desc_led3 = gpiod_get_from_of_node(node_led, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(desc_led1))
{
printk("获取申请gpio编号失败\n");
return PTR_ERR(desc_led1);
}
return 0;
ERR5:
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), 1);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
//销毁设备节点
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
// 注销字符设备驱动
cdev_del(cdev);
// 释放设备号
unregister_chrdev_region(MKDEV(major, minor), 1);
// 释放动态申请的空间
kfree(cdev);
gpiod_set_value(desc_led1, 0);
gpiod_put(desc_led1);
gpiod_set_value(desc_led2, 0);
gpiod_put(desc_led2);
gpiod_set_value(desc_led3, 0);
gpiod_put(desc_led3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用文件:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include "myled.h"
int main(int argc, char const *argv[])
{
int fd;
char buf[128] = {};
int which;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
printf("打开文件失败\n");
exit(-1);
}
while (1)
{
printf("请输入控制命令1:开灯1 0:关灯\n");
fgets(buf, sizeof(buf), stdin);
printf("请输入要操作的灯:1-》LED1 2->LED2 3->LED3\n");
scanf("%d", &which);
getchar();
//吃掉换行符
buf[strlen(buf) - 1] = '\0';
if (buf[0] == '1')
ioctl(fd, LedON, &which);
else
ioctl(fd, LedOFF, &which);
}
close(fd);
return 0;
}