驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#define LED_ON _IOW('l', 1, int) // 开灯
#define LED_OFF _IOW('l', 0, int) // 关灯
int major;
char kbuf[128] = {0};
// 定义设备类和设备变量
struct class *cls;
struct device *dev;
// 定义设备操作函数
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct device_node *dnode;
int mycdev_open(struct inode *inode, struct file *file)
{
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;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,就给内核支持的最大大小,需要更正大小
if (size > sizeof(kbuf))
size = sizeof(kbuf);
// 将数据从内核空间拷贝到用户空间
ret = copy_to_user(ubuf, kbuf, size);
if (ret < 0)
{
printk("copy_to_user failed!\n");
return -1;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,就给内核支持的最大大小,需要更正大小
if (size > sizeof(kbuf))
size = sizeof(kbuf);
// 将数据从用户空间拷贝到内核空间
ret = copy_from_user(kbuf, ubuf, size);
if (ret < 0)
{
printk("copy_from_user failed!\n");
return -1;
}
return 0;
}
// ioctl
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which, ret;
// 将ioctl第三个参数的数值拷贝给which
ret = copy_from_user(&which, (int *)arg, sizeof(int));
if (ret < 0)
{
printk("copy_from_user failed!\n");
return -EIO;
}
// 根据功能码进行硬件功能控制
switch (cmd)
{
case LED_ON:
switch (which)
{
case 1:
gpiod_set_value(gpiono1, 1);
break;
case 2:
gpiod_set_value(gpiono2, 1);
break;
case 3:
gpiod_set_value(gpiono3, 1);
break;
default:
break;
}
break;
case LED_OFF:
switch (which)
{
case 1:
gpiod_set_value(gpiono1, 0);
break;
case 2:
gpiod_set_value(gpiono2, 0);
break;
case 3:
gpiod_set_value(gpiono3, 0);
break;
default:
break;
}
break;
}
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,
.write = mycdev_write,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
// 入口函数
static int __init mycdev_init(void)
{
// 字符设备驱动注册
major = register_chrdev(0, "myled", &fops);
// 判断返回值
if (major < 0)
{
printk("register_chrdev failed!\n");
return -1;
}
// 打印主设备号
printk("major = %d\n", major);
// 向上提交目录,创建设备类
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("class_create failed!\n");
return -1;
}
printk("class_create success\n");
// 向上提交设备信息,创建设备节点
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myled");
if (IS_ERR(dev))
{
printk("device_create failed!\n");
return -1;
}
printk("device_create success\n");
// 解析设备树节点
dnode = of_find_node_by_name(NULL, "myleds");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -EIO;
}
printk("解析设备树节点成功\n");
// 根据设备树节点解析出LED1的GPIO编号
gpiono1 = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("解析gpio1编号失败\n");
return PTR_ERR(gpiono1);
}
printk("解析gpio1编号成功\n");
// 根据设备树节点解析出LED2的GPIO编号
gpiono2 = gpiod_get_from_of_node(dnode, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("解析gpio2编号失败\n");
return PTR_ERR(gpiono2);
}
printk("解析gpio2编号成功\n");
// 根据设备树节点解析出LED3的GPIO编号
gpiono3 = gpiod_get_from_of_node(dnode, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("解析gpio3编号失败\n");
return PTR_ERR(gpiono3);
}
printk("解析gpio3编号成功\n");
return 0;
}
// 出口函数
static void __exit mycdev_exit(void)
{
// 销毁设备节点(信息)
device_destroy(cls, MKDEV(major, 0));
// 销毁设备类(目录)
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "myled");
// 关灯
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
// 释放gpio编号
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
}
// 声明模块入口和出口
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/ioctl.h>
#define LED_ON _IOW('l', 1, int) // 开灯
#define LED_OFF _IOW('l', 0, int) // 关灯
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int which, a;
// 打开设备文件
int fd = open("/dev/myled", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
// 在终端输入数据
while (1)
{
printf("请输入要进行的操作:1(开灯) 0(关灯)>>>>>>");
scanf("%d", &a);
if (a == 1)
{
printf("请输入要进行操作的灯:1(LED1) 2(LED2) 3(LED3)>>>>>>");
scanf("%d", &which);
ioctl(fd, LED_ON, &which);
}
else
{
printf("请输入要进行操作的灯:1(LED1) 2(LED2) 3(LED3)>>>>>>");
scanf("%d", &which);
ioctl(fd, LED_OFF, &which);
}
}
// 关闭设备文件
close(fd);
return 0;
}
测试效果