led.h
#ifndef __LED_H__
#define __LED_H__
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
#endif
test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include "led.h"
#include <unistd.h>
int main(int argc, const char *argv[])
{
int fd1, fd2, fd3, num1, num2;
fd1 = open("/dev/myled0", O_RDWR);
if (fd1 < 0)
{
printf("open error\n");
return -1;
}
fd2 = open("/dev/myled1", O_RDWR);
if (fd2 < 0)
{
printf("open error\n");
return -1;
}
fd3 = open("/dev/myled2", O_RDWR);
if (fd3 < 0)
{
printf("open error\n");
return -1;
}
while (1)
{
printf("请输入要执行哪个灯的功能>>1(LED1)**2(LED2)**3(LED3)>>>>>>");
scanf("%d", &num1);
switch (num1)
{
case 1:
{
printf("请输入要执行的功能>>1(LED1亮)**0(LED1灭)>>>>");
scanf("%d", &num2);
switch (num2)
{
case 1:
ioctl(fd1, LED_ON,&num1);
break;
case 0:
ioctl(fd1, LED_OFF,&num1);
break;
default:
break;
}
}
break;
case 2:
{
printf("请输入要执行的功能>>1(LED2亮)**0(LED2灭)");
scanf("%d", &num2);
switch (num2)
{
case 1:
ioctl(fd2, LED_ON,&num1);
break;
case 0:
ioctl(fd2, LED_OFF,&num1);
break;
default:
break;
}
}
break;
case 3:
{
printf("请输入要执行的功能>>1(LED3亮)**0(LED3灭)");
scanf("%d", &num2);
switch (num2)
{
case 1:
ioctl(fd3, LED_ON,&num1);
break;
case 0:
ioctl(fd3, LED_OFF,&num1);
break;
default:
break;
}
}
break;
default:
break;
}
}
close(fd1);
close(fd2);
close(fd3);
return 0;
}
mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include "led.h"
#include <linux/gpio.h>
unsigned int major = 500; // 设备号
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
struct cdev *cdev;
struct gpio_desc *gpio_num1;
struct gpio_desc *gpio_num2;
struct gpio_desc *gpio_num3;
struct device_node *dnode;
// 封装各个操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int num;
copy_from_user(&num, (void *)arg, 4);
switch (cmd)
{
case LED_ON:
{
switch (num)
{
case 1:
gpiod_set_value(gpio_num1, 1);
printk("11111\n");
//while(1);
break;
case 2:
gpiod_set_value(gpio_num2, 1);
break;
case 3:
gpiod_set_value(gpio_num3, 1);
break;
default:
break;
}
}
break;
case LED_OFF:
{
switch (num)
{
case 1:
printk("222222\n");
gpiod_set_value(gpio_num1, 0);
break;
case 2:
gpiod_set_value(gpio_num2, 0);
break;
case 3:
gpiod_set_value(gpio_num3, 0);
break;
default:
break;
}
}
break;
default:
break;
}
return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {
.open = mycdev_open,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
static int __init mycdev_init(void)
{
int ret, i;
// 1.分配字符设备驱动对象空间
cdev = cdev_alloc();
if (NULL == cdev)
{
printk("分配字符设备驱动对象失败\n");
ret = -ENOMEM;
goto OUT1;
}
printk("分配字符设备驱动对象成功\n");
// 2.初始化字符设备对象
cdev_init(cdev, &fops);
// 3.申请设备号
if (major > 0)
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
if (ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
else if (0 == major)
{
ret = alloc_chrdev_region(&devno, minor, 3, "myled");
if (ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
// 获取主设备号和次设备号
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
// 4.注册字符设备驱动对象
ret = cdev_add(cdev, MKDEV(major, minor), 3);
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 < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
ret = -PTR_ERR(dev);
goto OUT5;
}
}
printk("向上提交设备节点成功\n");
// 完成硬件寄存器地址的映射及初始化
dnode = of_find_node_by_name(NULL, "myleds"); // 解析设备树节点 得到设备数节点首地址
if (dnode == NULL)
{
printk("of find node by name error\n");
return -EIO;
}
printk("of find node by name 1\n");
gpio_num1 = gpiod_get_from_of_node(dnode, "led1", 0, GPIOD_OUT_HIGH, NULL); // 根据解析到的设备数节点首地址,来解析出对应的gpio编号
if (IS_ERR(gpio_num1))
{
printk("error\n");
return -PTR_ERR(gpio_num1);
}
printk("gpio request 1\n");
gpio_num2 = gpiod_get_from_of_node(dnode, "led2", 0, GPIOD_OUT_HIGH, NULL); // 根据解析到的设备数节点首地址,来解析出对应的gpio编号
if (IS_ERR(gpio_num2))
{
printk("error\n");
return -PTR_ERR(gpio_num2);
}
printk("gpio request 2\n");
gpio_num3 = gpiod_get_from_of_node(dnode, "led3", 0, GPIOD_OUT_HIGH, NULL); // 根据解析到的设备数节点首地址,来解析出对应的gpio编号
if (IS_ERR(gpio_num3))
{
printk("error\n");
return -PTR_ERR(gpio_num3);
}
printk("gpio request 3\n");
// printk ("%d\n",gpiod_get_value(gpio_num1));
return 0;
OUT5:
// 释放已经申请的设备节点信息
for (--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
// 释放目录空间
class_destroy(cls);
OUT4:
// 注销字符设备驱动对象
cdev_del(cdev);
OUT3:
// 释放设备号
unregister_chrdev_region(MKDEV(major, minor), 3);
OUT2:
// 释放对象空间
kfree(cdev);
OUT1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
gpiod_set_value(gpio_num1, 0); // 关灯
gpiod_set_value(gpio_num2, 0); // 关灯
gpiod_set_value(gpio_num3, 0); // 关灯
gpiod_put(gpio_num1);
gpiod_put(gpio_num2);
gpiod_put(gpio_num3);
// 销毁设备节点
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 释放目录空间
class_destroy(cls);
// 注销字符设备驱动对象
cdev_del(cdev);
// 释放设备号
unregister_chrdev_region(MKDEV(major, minor), 3);
// 释放对象空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");