驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include<linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include "led.h"
#include <linux/cdev.h>
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct class *cls;
struct device *dev;
int i; // 循环变量
int ret = 0; // 返回值
struct cdev* cdev; // 字符设备驱动结构体
unsigned int major = 499; // 主设备号
unsigned int minor = 0; // 次设备号
unsigned int count = 3; // 申请设备资源个数
dev_t devno; // 设备号
int my_open(struct inode *inode, struct file *file)
{
return 0;
}
int my_close(struct inode *inode, struct file *file)
{
return 0;
}
long my_ioctl(struct file *file, unsigned int cmd, unsigned long size)
{
int num;
copy_from_user(&num, (void *)size, 4);
switch (cmd)
{
case LED_ON:
{
switch (num)
{
case 1:
gpiod_set_value(gpiono1, 1);
printk("11111\n");
//while(1);
break;
case 2:
gpiod_set_value(gpiono2, 1);
printk("22222\n");
break;
case 3:
gpiod_set_value(gpiono3, 1);
printk("33333\n");
break;
default:
break;
}
}
break;
case LED_OFF:
{
switch (num)
{
case 1:
gpiod_set_value(gpiono1, 0);
printk("11111\n");
break;
case 2:
gpiod_set_value(gpiono2, 0);
printk("222222\n");
break;
case 3:
gpiod_set_value(gpiono3, 0);
printk("33333\n");
break;
default:
break;
}
}
break;
default:
break;
}
return 0;
}
struct file_operations fops = {
.open = my_open,
.release = my_close,
.unlocked_ioctl = my_ioctl,
};
//定义相关成员函数
//probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev)//当和设备匹配成功之后执行probe
{
// 1.分配对象空间
cdev = cdev_alloc();
if (cdev == NULL) {
printk("分配字符设备驱动对象失败\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
ret = -EFAULT;
goto OUT1;
}
printk("分配对象空间成功\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
// 2.初始化对象
cdev_init(cdev, &fops);
// 3.申请设备号
if (major > 0) // 静态指定设备号
{
ret = register_chrdev_region(MKDEV(major, minor), count, "myled");
if (ret) {
printk("静态指定设备号失败\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
goto OUT2;
}
} else if (major == 0) // 动态申请设备号
{
ret = alloc_chrdev_region(&devno, minor, count, "myled");
if (ret) {
printk("动态申请设备号失败\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
goto OUT2;
}
// 获取主设备号和次设备号
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
// 4.注册字符设备驱动对象
ret = cdev_add(cdev, MKDEV(major, minor), count);
if (ret) {
printk("注册字符设备驱动对象失败\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
goto OUT3;
}
printk("注册字符设备驱动对象成功\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
// 向上提交目录
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls)) {
printk("向上提交目录失败\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
// 向上提交设备节点信息
for (i = 0; i < count; 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");
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
//解析出GPIO编号并申请
gpiono1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono1);
}
printk("GPIO信息解析成功1\n");
gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("of node error\n");
return -PTR_ERR(gpiono2);
}
printk("GPIO信息解析成功2\n");
gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("of node error\n");
return -PTR_ERR(gpiono3);
}
printk("GPIO信息解析成功3\n");
return 0;
printk("GPIO信息解析成功4\n");
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), count);
OUT2:
// 释放对象空间
kfree(cdev);
OUT1:
return ret;
}
//remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev)//当设备和驱动分离时执行remove
{
int i;
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
// 销毁设备节点
for (i = 0; i < count; i++) {
device_destroy(cls, MKDEV(major, i));
}
// 释放目录空间
class_destroy(cls);
// 1.注销字符设备驱动对象
cdev_del(cdev);
// 2.释放设备号
unregister_chrdev_region(MKDEV(major, minor), count);
// 3.释放对象空间
kfree(cdev);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
//在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[]={
{.compatible="platform.myleds",},
{},
};
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable, //指定设备树匹配表的首地址
},
};
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
head.h
#ifndef __LED_H__
#define __LED_H__
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
led.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;
}