驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
unsigned int major; // 设备号
char kbuf[128] = {0};
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;
struct resource *res;
unsigned int irqno;
//struct gpio_desc *gpiono;
// 封装各个操作方法
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 *iof)
{
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;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *iof)
{
int ret;
// 从用户拷贝
if (size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy from user filed\n");
return -EIO;
}
switch (kbuf[0])
{
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;
case '4': gpiod_set_value(gpiono_1,0); break;
case '5': gpiod_set_value(gpiono_2,0); break;
case '6': gpiod_set_value(gpiono_3,0); break;
}
return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
//probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev)//当和设备匹配成功之后执行probe
{
//字符设备驱动的注册
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");
//解析MEM类型的资源
res=platform_get_resource(pdev,IORESOURCE_MEM,0);
if(res == NULL)
{
printk("解析资源失败%d\n",__LINE__);
return -ENOMEM;
}
//解析中断类型资源
irqno=platform_get_irq(pdev,0);
if(irqno <0)
{
printk("解析资源成功%d\n",__LINE__);
return -ENOMEM;
}
printk("设备匹配成功%s:%s:%d\n",__FILE__,__func__,__LINE__);
//解析出GPIO编号并申请
gpiono_1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
gpiono_2=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
gpiono_3=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono_1) |IS_ERR(gpiono_2) |IS_ERR(gpiono_3))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono_1);
}
//亮灯
//gpiod_set_value(gpiono,1);
printk("GPIO信息解析成功\n");
return 0;
}
//remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev)//当设备和驱动分离时执行remove
{
//灭灯
gpiod_set_value(gpiono_1,0);
gpiod_set_value(gpiono_2,0);
gpiod_set_value(gpiono_3,0);
//释放GPIO编号
gpiod_put(gpiono_1);
gpiod_put(gpiono_2);
gpiod_put(gpiono_3);
// 字符设备驱动的注销
unregister_chrdev(major, "mycdev");
printk("设备匹配失败%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[]={
{.compatible="hqyj,myplatform",},
{.compatible="hqyj,myplatform1",},
{.compatible="hqyj,myplatform2",},
{},
};
//构建名字表
struct platform_device_id idtable[]={
{"aaaaa",0},
{"bbbbb",1},
{"ccccc",2},
{},
};
//创建平台对象结构体变量并初始化
struct platform_driver pdrv={
.probe=pdrv_probe, //用于匹配设备后执行
.remove=pdrv_remove, //用于分离设备后执行
.driver={
.name="aaaaa",
.of_match_table=oftable, //指定设备树匹配表的首地址
},
};
//一键注册宏包含__init,__exit,及两者的调用
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
c程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mycdev", O_RDWR); // 为了调用驱动中的mycdev_open
if (fd < 0)
{
printf("打开设备文件失败");
exit(-1);
}
while (1)
{
printf("请输入控制码选择灯控制开关)\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0'; // 用'\0'替代\n
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}