#include <linux/module.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include "myled.h"
#define CNAME "myled"
/*mynode@0x12345678{
astring="hello 21091";
uint =<0xaabbccdd 0x11223344>;
binarry=[00 0c 29 7b f9 be];
mixed ="hello",[11 22],<0x12345678>;
};*/
//定义指向设备树节点结构体空间的指针
struct device_node *node;
//控制led1
struct gpio_desc *nodo;
//控制led2
struct gpio_desc *nodp;
//控制led3
struct gpio_desc *nodq;
//控制led4
struct gpio_desc *nodr;
//控制led5
struct gpio_desc *nods;
//控制led6
struct gpio_desc *nodt;
//int gpio;
struct cdev *cdev;
struct class * cls;
struct device * dev;
#if 1
unsigned int major = 0;//动态申请设备号
#else
unsigned int major = 500; //静态指定设备号
#endif
int minor = 0;
const int count = 7;
int ret;
int mycdev_open(struct inode *inode,struct file *file)
{
return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
int whitch;
int ret;
//判断命令码
switch (cmd)
{
case LED_ON:
//2.将用户空间的数据传递给内核空间
ret = copy_from_user(&whitch,(void*)arg,sizeof(int));
if(ret)
{
printk("copy from user led on!!!!\n");
return -EIO;
}
switch(whitch) //3.判断whitch的值
{
case LED1:
gpiod_set_value(nodo,1);
break;
case LED2:
gpiod_set_value(nodp,1);
break;
case LED3:
gpiod_set_value(nodq,1);
break;
case LED4:
gpiod_set_value(nodr,1);
break;
case LED5:
gpiod_set_value(nods,1);
break;
case LED6:
gpiod_set_value(nodt,1);
break;
}
break;
case LED_OFF:
ret = copy_from_user(&whitch,(void*)arg,sizeof(int));
if(ret)
{
printk("copy from user led on!!!!\n");
return -EIO;
}
switch(whitch) //3.判断whitch的值
{
case LED1:
gpiod_set_value(nodo,0);
break;
case LED2:
gpiod_set_value(nodp,0);
break;
case LED3:
gpiod_set_value(nodq,0);
break;
case LED4:
gpiod_set_value(nodr,0);
break;
case LED5:
gpiod_set_value(nods,0);
break;
case LED6:
gpiod_set_value(nodt,0);
break;
}
break;
default:
break;
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
return 0;
}
const struct file_operations fops={
.open=mycdev_open,
.unlocked_ioctl=mycdev_ioctl,
.release=mycdev_close,
};
static __init int demo_init(void)
{
dev_t devdev;
int i;
//1.分配cdev结构体
cdev = cdev_alloc();
if(NULL == cdev)
{
printk("cdev alloc is error\n");
ret = -EIO;
goto ERR1;
}
//2.初始化结构体
cdev_init(cdev,&fops);
//3.申请设备号
if(major > 0)
{
//静态指定设备号
ret = register_chrdev_region(MKDEV(major,minor),count,CNAME);
if(ret)
{
printk("register chrdev region is error\n");
ret = -ENOMEM;
goto ERR2;
}
}
else{
//动态申请设备号
ret = alloc_chrdev_region(&devdev,0,count,CNAME);
if(ret)
{
printk("alloc chrdev region is error\n");
ret = -ENOMEM;
goto ERR2;
}
//获取主设备号
major = MAJOR(devdev);
//获取次设备号
minor = MINOR(devdev);
}
//4.驱动的注册
ret = cdev_add(cdev,MKDEV(major,minor),count);
if(ret)
{
printk("cdev add is error\n");
ret = -EIO;
goto ERR3;
}
//5.自动创建设备节点
//1)向上层提交目录信息
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls))
{
printk("class create is error\n");
ret = PTR_ERR(cls);
goto ERR4;
}
//2)向上层提交设备节点信息
for(i=0;i<count;i++)
{
dev = device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("device create is error\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
//通过路径获取设备树节点信息
node=of_find_node_by_name(NULL,"myleds");
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
nodo=gpiod_get_from_of_node(node,"myled1",0,GPIOD_OUT_LOW,NULL);
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
nodp=gpiod_get_from_of_node(node,"myled2",0,GPIOD_OUT_LOW,NULL);
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
nodq=gpiod_get_from_of_node(node,"myled3",0,GPIOD_OUT_LOW,NULL);
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
nodr=gpiod_get_from_of_node(node,"myled4",0,GPIOD_OUT_LOW,NULL);
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
nods=gpiod_get_from_of_node(node,"myled5",0,GPIOD_OUT_LOW,NULL);
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
nodt=gpiod_get_from_of_node(node,"myled6",0,GPIOD_OUT_LOW,NULL);
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
ret=gpiod_direction_output(nodo,0);
ret=gpiod_direction_output(nodp,0);
ret=gpiod_direction_output(nodq,0);
ret=gpiod_direction_output(nodr,0);
ret=gpiod_direction_output(nods,0);
ret=gpiod_direction_output(nodt,0);
ret=gpiod_get_value(nodo);
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
kfree(cdev);
ERR1:
return -EIO;
}
static __exit void demo_exit(void)
{
int i;
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//2.销毁目录信息
class_destroy(cls);
//3.驱动的注销
cdev_del(cdev);
//4.销毁设备号
unregister_chrdev_region(MKDEV(major,minor),count);
//5.释放cdev结构体
kfree(cdev);
gpiod_put(nodo);
gpiod_put(nodp);
gpiod_put(nodq);
gpiod_put(nodr);
gpiod_put(nods);
gpiod_put(nodt);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
text.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"
int main(int argc, char const *argv[])
{
int fd1 = -1;
int fd2 = -1;
int fd3 = -1;
int fd4 = -1;
int fd5 = -1;
int fd6 = -1;
int whitch;
fd1 = open("/dev/myled0",O_RDWR);
if(-1==fd1)
{
perror("open fd1 is error");
exit(1);
}
fd2 = open("/dev/myled1",O_RDWR);
if(-1==fd2)
{
perror("open fd2 is error");
exit(1);
}
fd3 = open("/dev/myled2",O_RDWR);
if(-1==fd3)
{
perror("open fd3 is error");
exit(1);
}
fd4 = open("/dev/myled3",O_RDWR);
if(-1==fd4)
{
perror("open fd4 is error");
exit(1);
}
fd5 = open("/dev/myled4",O_RDWR);
if(-1==fd5)
{
perror("open fd5 is error");
exit(1);
}
fd6 = open("/dev/myled5",O_RDWR);
if(-1==fd6)
{
perror("open fd6 is error");
exit(1);
}
while(1)
{
whitch=LED1;
ioctl(fd1,LED_ON,&whitch);
sleep(1);
ioctl(fd1,LED_OFF,&whitch);
sleep(1);
whitch=LED2;
ioctl(fd2,LED_ON,&whitch);
sleep(1);
ioctl(fd2,LED_OFF,&whitch);
sleep(1);
whitch=LED3;
ioctl(fd3,LED_ON,&whitch);
sleep(1);
ioctl(fd3,LED_OFF,&whitch);
sleep(1);
whitch=LED4;
ioctl(fd4,LED_ON,&whitch);
sleep(1);
ioctl(fd4,LED_OFF,&whitch);
sleep(1);
whitch=LED5;
ioctl(fd5,LED_ON,&whitch);
sleep(1);
ioctl(fd5,LED_OFF,&whitch);
sleep(1);
whitch=LED6;
ioctl(fd6,LED_ON,&whitch);
sleep(1);
ioctl(fd6,LED_OFF,&whitch);
sleep(1);
}
close(fd1);
close(fd2);
close(fd3);
close(fd4);
close(fd5);
close(fd6);
return 0;
}