驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include "head.h"
#include <linux/compiler.h>
#include <linux/io.h>
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
// 封装操作方法
struct device_node *dnode;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct cdev *cdev;
char kbuf[128]={0};
dev_t devno;
module_param(major,uint,0664);//方便在命令行传递major的值
struct class*cls;
struct device *dev;
unsigned int condition=0;
//定义一个等待队列头
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
int num=MINOR(inode->i_rdev);
file->private_data=(void*)num;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
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 *lof)
{
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 myled_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int num=(int)file->private_data;
switch(num)
{
case 0://控制LED1
switch(cmd)
{
case LED_ON:
gpiod_set_value(gpiono1, 1);
break;
case LED_OF:
gpiod_set_value(gpiono1, 0);
}
break;
case 1://控制LED2
switch(cmd)
{
case LED_ON:
gpiod_set_value(gpiono2, 1);
break;
case LED_OF:
gpiod_set_value(gpiono2, 0);
}
break;
case 2://控制LED3
switch(cmd)
{
case LED_ON:
gpiod_set_value(gpiono3, 1);
break;
case LED_OF:
gpiod_set_value(gpiono3, 0);
}
}
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.unlocked_ioctl=myled_ioctl,
};
static int __init mycdev_init(void)
{
dnode = of_find_node_by_path("/myled");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("设备树解析成功\n");
gpiono1 = gpiod_get_from_of_node(dnode, "led-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono1))
{
printk("申请gpio1对象失败\n");
return -ENXIO;
}
printk("申请gpio1信息对象成功\n");
gpiono2 = gpiod_get_from_of_node(dnode, "led-gpios", 1, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
printk("申请gpio2对象失败\n");
return -ENXIO;
}
printk("申请gpio2信息对象成功\n");
gpiono3 = gpiod_get_from_of_node(dnode, "led-gpios", 2, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
printk("申请gpio3对象失败\n");
return -ENXIO;
}
printk("申请gpio3信息对象成功\n");
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");
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)
{
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
int i;
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");
用户层代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, const char *argv[])
{
char buf[128] = "";
int a;
int fd;
while (1)
{
printf("请选择要打开的灯(1,2,3)\n");
scanf(" %d", &a);
switch (a)
{
case 1:
fd = open("/dev/myled0", O_RDWR);
if (fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
printf("打开文件%d成功\n",a-1);
break;
case 2:
fd = open("/dev/myled1", O_RDWR);
if (fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
printf("打开文件%d成功\n",a-1);
break;
case 3:
fd = open("/dev/myled2", O_RDWR);
if (fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
printf("打开文件%d成功\n",a-1);
break;
default:
printf("请输入范围内的数\n");
}
int b;
printf("请开灯关灯(0/1)\n");
scanf(" %d",&b);
switch(b)
{
case 1:
ioctl(fd,LED_ON);
break;
case 0:
ioctl(fd,LED_OF);
break;
default:
printf("请输入'0'或'1'\n");
}
close(fd);
printf("关闭文件%d\n",a-1);
}
close(fd);
return 0;
}