mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include "led.h"
struct gpio_desc *gpiono_1;
struct gpio_desc *gpiono_2;
struct gpio_desc *gpiono_3;
struct device_node *dnode;
struct class *cls;
struct device *dev;
int major;
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which;
int ret = copy_from_user(&which, (void *)arg, 4);
switch (cmd)
{
case LED_ON :
switch(which)
{
case 1:
gpiod_set_value(gpiono_1, 1);
break;
case 2:
gpiod_set_value(gpiono_2, 1);
break;
case 3:
gpiod_set_value(gpiono_3, 1);
break;
default:
printk("输入错误\n");
}
break;
case LED_OFF:
switch(which)
{
case 1:
gpiod_set_value(gpiono_1, 0);
break;
case 2:
gpiod_set_value(gpiono_2, 0);
break;
case 3:
gpiod_set_value(gpiono_3, 0);
break;
default:
printk("输入错误\n");
}
}
return 0;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.unlocked_ioctl = mycdev_ioctl,
};
static int __init mycdev_init(void)
{
// 字符设备驱动注册
major = register_chrdev(0, "mycdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点成功\n");
// 解析设备树节点
dnode = of_find_node_by_name(NULL, "myleds");
if (dnode == NULL)
{
printk("解析设备树数失败\n");
return -ENOMEM;
}
printk("解析设备树节点成功\n");
// 根据设备树节点解析出gpio编号并申请
gpiono_1 = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono_1))
{
printk("解析设备号1失败\n");
return -PTR_ERR(gpiono_1);
}
printk("申请gpio_led1编号成功\n");
// 根据设备树节点解析出gpio编号并申请
gpiono_2 = gpiod_get_from_of_node(dnode, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono_2))
{
printk("解析设备号2失败\n");
return -PTR_ERR(gpiono_2);
}
printk("申请gpio_led2编号成功\n");
// 根据设备树节点解析出gpio编号并申请
gpiono_3 = gpiod_get_from_of_node(dnode, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono_3))
{
printk("解析设备号3失败\n");
return -PTR_ERR(gpiono_3);
}
printk("申请gpio_led3编号成功\n");
// 开灯
// gpiod_set_value(gpiono, 1);
return 0;
}
static void __exit mycdev_exit(void)
{
// 灭灯
gpiod_set_value(gpiono_1, 0);
// gpiod_set_value(gpiono_2, 0);
// gpiod_set_value(gpiono_3, 0);
// 释放gpio编号
gpiod_put(gpiono_1);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录空间
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
text.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "led.h"
int main(int argc, char const *argv[])
{
int a, b;
char buf[128] = {0};
int fd = open("/dev/mycdev", O_RDONLY);
if(fd < 0)
{
printf("打开设备文件失败%s\n", __FILE__);
exit(-1);
}
while(1)
{
printf("请输入:1(开灯),0(关灯)>>>");
scanf("%d", &a);
printf("请输入控制的灯:1(LED1),2(LED2),3(LED3)>>>");
scanf("%d", &b);
switch(a)
{
case 1:
ioctl(fd, LED_ON, &b);//开灯
break;
case 0:
ioctl(fd, LED_OFF, &b);//关灯
break;
default:
printf("输入错误\n");
}
}
close(fd);
return 0;
}
led,h
#ifndef __HEAD_H__
#define __HEAD_H__
//封装开关灯功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('1', 0, int)
#endif