pdrv.c
#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"
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono[3];
struct class *cls;
struct device *dev;
struct cdev *cdev;
unsigned int major = 500;
unsigned int minor = 0;
dev_t devno;
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[0], 1);
break;
case 2:
gpiod_set_value(gpiono[1], 1);
break;
case 3:
gpiod_set_value(gpiono[2], 1);
break;
}
break;
case LED_OFF: // 关灯
switch (which)
{
case 1:
gpiod_set_value(gpiono[0], 0);
break;
case 2:
gpiod_set_value(gpiono[1], 0);
break;
case 3:
gpiod_set_value(gpiono[2], 0);
break;
}
break;
}
return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops={
.unlocked_ioctl=mycdev_ioctl,
};
// 定义想换成员函数
// probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev)
{
int ret, i;
// 1.分配对象空间
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("分配字符设备驱动对象失败\n");
ret = -EFAULT;
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 (major == 0) // 动态申请设备号
{
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");
// 完成硬件寄存器地址的映射以及初始化
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;
// 解析MEM类型的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (NULL == res)
{
printk("解析资源失败%d\n", __LINE__);
return -ENOMEM;
}
// 解析中断类型的资源
irqno = platform_get_irq(pdev, 0);
if (irqno < 0)
{
printk("解析资源失败%d\n", __LINE__);
return -ENOMEM;
}
printk("mem:%11llx irq:%d\n", res->start, irqno);
// 解析出GPIO编号并申请
gpiono[0] = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
gpiono[1] = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
gpiono[2] = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
for (int i = 0; i < 3; i++)
{
if (IS_ERR(gpiono[i]))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono[i]);
}
}
printk("GPIO解析成功\n");
return 0;
}
// remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev)
{
for (int i = 0; i < 3; i++)
{
// 灭灯
gpiod_set_value(gpiono[i], 0);
// 释放GPIO编号
gpiod_put(gpiono[i]);
}
// 销毁设备节点
int i;
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 释放目录空间
class_destroy(cls);
// 1.注销字符设备驱动对象
cdev_del(cdev);
// 2.释放设备号
unregister_chrdev_region(MKDEV(major, minor), 3);
// 3.释放对象空间
kfree(cdev);
}
// 在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[] = {
{
.compatible = "hello, myplatform",
},
{
.compatible = "hello, myplatform1",
},
{
.compatible = "hello, myplatform2",
},
{},
};
// 分配对象并初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "hello",
// 指定设备树匹配表的首地址
.of_match_table = oftable,
},
};
// 一键注册宏,代替入口出口
module_platform_driver(pdrv);
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/myled", 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;
}
}
close(fd);
return 0;
}