头文件
#ifndef __HEAD_H__
#define __HEAD_H__
//构建LED开关的功能码,添加ioctl第三个参数
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#endif
驱动程序代码
mychrdev.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
struct cdev *cdev;
char kbuf[128]={0};
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
module_param(major,uint,0664);//方便在命令行传递major的值
struct class*cls;
struct device *dev;
struct device_node *dnode;
struct gpio_desc *gpiono[3];
int mycdev_open(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 which;
//获取arg对应的用户空间中到的值
int ret = copy_from_user(&which,(void *)arg,4);
if(ret){
printk("从用户空间数据失败\n");
return -EIO;
}
switch (cmd) {
case LED_ON:
switch (which) {
case 1: // LED1
gpiod_set_value(gpiono[0], 1); // LED1开灯
break;
case 2: // LED2
gpiod_set_value(gpiono[1], 1); // LED2开灯
break;
case 3: // LED3
gpiod_set_value(gpiono[2], 1); // LED3开灯
break;
}
break;
case LED_OFF:
switch (which) {
case 1: // LED1
gpiod_set_value(gpiono[0], 0); // LED1开灯
break;
case 2: // LED2
gpiod_set_value(gpiono[1], 0); // LED2开灯
break;
case 3: // LED3
gpiod_set_value(gpiono[2], 0); // LED3开灯
break;
}
break;
break;
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
/*
// 字符设备驱动注册
major = register_chrdev(0, "mychrdev", &fops);
if(major < 0){
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE,"myled");
if(IS_ERR(cls)){
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功\n");
// 向上提交设备节点信息
devs = device_create(cls, NULL, MKDEV(major,0), NULL, "myled");
if(IS_ERR(devs)){
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(devs);
}
printk("向上提交设备节点成功\n");
*/
//MARK 设备驱动相关
int ret;
//为字符设备驱动对象申请空间
cdev=cdev_alloc();
if(cdev==NULL)
{
printk("字符设备驱动对象申请空间失败\n");
ret=-EFAULT;
goto out1;
}
printk("申请对象空间成功\n");
//初始化字符设备驱动对象
cdev_init(cdev,&fops);
//申请设备号
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");
//注册字符设备驱动对象
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");
//向上提交设备节点信息
int i;
for(i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret=-PTR_ERR(dev);
goto out5;
}
}
printk("向上提交设备信息成功\n");
//MARK 设备树相关
// 解析LED的设备树节点
dnode = of_find_node_by_path("/myled");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析GPIO信息成功\n");
// 申请gpio对象
gpiono[0] = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("申请gpio对象失败\n");
return -ENXIO;
}
gpiono[1] = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("申请gpio对象失败\n");
return -ENXIO;
}
gpiono[2] = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("申请gpio对象失败\n");
return -ENXIO;
}
printk("申请gpio信息对象成功\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;
}
static void __exit mycdev_exit(void)
{
//循环灭灯和释放gpio编号
int i;
for(i=0;i<3;i++){
// 灭灯
gpiod_set_value(gpiono[i], 0);
// 释放gpio编号
gpiod_put(gpiono[i]);
}
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用端程序代码
pro1.c
#include
#include
#include
#include
#include
#include "head.h"
int main(int argc, const char *argv[])
{
int a,b;
char buf[128]={0};
int fd=open("/dev/myled0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1){
//从终端读取
printf("请输入对LED灯的控制: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;
}
}
return 0;
}