head.h
- #define LED_ON _IOW('l',1,int *)
- #define LED_OFF _IOW('l',0,int *)
text.c
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include "head.h"
-
- int main(int argc,const char * argv[])
- {
- int a;
- int b;
-
-
- while(1)
- {
- int fd;
- //从中断获取我们想实现的功能功能
- printf("请选择要控制的灯:0-LED1,1-LED2,2-LED3\n");
- scanf("%d",&a);
- switch(a)
- {
- case 1:
- fd = open("/dev/mycdev0",O_RDWR);
- break;
- case 2:
- fd = open("/dev/mycdev1",O_RDWR);
- break;
- case 3:
- fd = open("/dev/mycdev2",O_RDWR);
- break;
- }
- if(fd<0)
- {
- printf("打开设备文件失败\n");
- return -1;
- }
- printf("打开设备文件成功\n");
-
- printf("请输入想要实现的功能 ");
- printf("0(关灯)1(开灯)\n");
- printf("请输入:");
- scanf("%d",&b);
-
- switch (b)
- {
- case 1:
- ioctl(fd,LED_ON,&b);
- break;
- case 0:
- ioctl(fd,LED_OFF,&b);
- break;
- }
- close(fd);
- }
-
-
- return 0;
- }
demo.c
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/uaccess.h>
- #include <linux/io.h>
- #include <linux/device.h>
- #include <linux/cdev.h>
- #include <linux/slab.h>
- #include <linux/kdev_t.h>
- #include "head.h"
- #include <linux/of.h>
- #include <linux/of_gpio.h>
- #include <linux/gpio.h>
- struct cdev* cdev;
- unsigned int major=0;
- unsigned int minor=0;
- dev_t devno;
- struct class* cls;
- struct device *dev;
-
- struct device_node *dnode;
- struct gpio_desc *gpiono1;
- struct gpio_desc *gpiono2;
- struct gpio_desc *gpiono3;
-
- int mycdev_open(struct inode * inode,struct file* file)
- {
- unsigned int min=MINOR(inode->i_rdev);//获取打开的文件的次设备号
- file->private_data=(void *)min;
- 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 * indode,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 min=(int)file->private_data;//获取文件的次设备号
-
- switch(min)
- {
- case 0://LED1
- switch(cmd)
- {
- case LED_ON://开灯
- gpiod_set_value(gpiono1,1);
- break;
- case LED_OFF://关灯
- gpiod_set_value(gpiono1,0);
- break;
- }
- break;
- case 1://LED2
- switch(cmd)
- {
- case LED_ON://开灯
- gpiod_set_value(gpiono2,1);
- break;
- case LED_OFF://关灯
- gpiod_set_value(gpiono2,0);
- break;
- }
- break;
- case 2://LED3
- switch(cmd)
- {
- case LED_ON://开灯
- gpiod_set_value(gpiono3,1);
- break;
- case LED_OFF://关灯
- gpiod_set_value(gpiono3,0);
- break;
- }
- break;
- }
- printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
- return 0;
-
- }
-
- struct file_operations fops = {
- .open=mycdev_open,
- .read=mycdev_read,
- .write=mycdev_write,
- .unlocked_ioctl = mycdev_ioctl,
- .release=mycdev_close,
- };
-
- static int __init mycdev_init(void)
- {
- //1.申请一个对象空间cdev_alloc
- int ret;
- cdev=cdev_alloc();
- if(cdev==NULL)
- {
- printk("申请字符设备驱动对象空间失败\n");
- ret =-EFAULT;
- goto out1;
- }
- printk("申请字符设备驱动对象空间成功\n");
- //2.初始化对象cdev_init
- cdev_init(cdev,&fops);
- //3.申请设备号 register_chrdev_region()/alloc_chrdev_region()
- if(major==0)//动态申请
- {
- ret=alloc_chrdev_region(&devno,minor,3,"mychrdev");
- if(ret)
- {
- printk("动态申请设备号失败\n");
- goto out2;
- }
- major=MAJOR(devno);//根据设备号获取主设备号
- minor=MINOR(devno);//根据设备号获取次设备号
- }
- else//静态申请
- {
- ret=register_chrdev_region(MKDEV(major,minor),3,"mychrdev");
- if(ret)
- {
- printk("静态申请设备号失败\n");
- goto out2;
- }
- }
- printk("设备号申请成功\n");
- //4.注册驱动对象 cdev_add
- ret=cdev_add(cdev,MKDEV(major,minor),3);
- if(ret)
- {
- printk("注册字符设备驱动对象失败\n");
- goto out3;
- }
- printk("注册字符设备驱动对象成功\n");
- //5.向上提交目录 class_create
- cls=class_create(THIS_MODULE,"mychrdev");
- if(IS_ERR(cls))
- {
- printk("想上提交目录失败\n");
- goto out4;
- }
- printk("想上提交目录成功\n");
- //6.向上提交设备节点信息 device_create
- int i;
- for(i=0;i<3;i++)
- {
- dev=device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
- if(IS_ERR(dev))
- {
- printk("想上提交设备节点失败\n");
- goto out5;
- }
- }
- printk("想上提交设备节点成功\n");
-
- //使用gpio驱动
- dnode=of_find_node_by_path("/myled");
- if(dnode==NULL)
- {
- printk("解析设备树节点失败\n");
- return -ENXIO;
- }
- //获取led1 gpio编号
- gpiono1=gpiod_get_from_of_node(dnode,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono1))
- {
- printk("申请gpio信息失败\n");
- return -PTR_ERR(gpiono1);
- }
- //获取led2 gpio编号
- gpiono2=gpiod_get_from_of_node(dnode,"led2-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono2))
- {
- printk("申请gpio信息失败\n");
- return -PTR_ERR(gpiono2);
- }
- //获取led3 gpio编号
- gpiono3=gpiod_get_from_of_node(dnode,"led3-gpio",0,GPIOD_OUT_LOW,NULL);
- if(IS_ERR(gpiono3))
- {
- printk("申请gpio信息失败\n");
- return -PTR_ERR(gpiono3);
- }
- //初始化为0
- gpiod_set_value(gpiono1,0);
- gpiod_set_value(gpiono2,0);
- gpiod_set_value(gpiono3,0);
-
- 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)
- {
- int i;
- //1.销毁设备节点信息
- for(i=0;i<3;i++)
- {
- device_destroy(cls,MKDEV(major,i));
- }
- //2.销毁目录
- class_destroy(cls);
- //3.注销字符设备驱动对象
- cdev_del(cdev);
- //4.释放设备号
- unregister_chrdev_region(MKDEV(major,minor),3);
- //5.释放申请到的字符设备驱动对象空间
- kfree(cdev);
- //释放GPIO编号
- gpiod_put(gpiono1);
- gpiod_put(gpiono2);
- gpiod_put(gpiono3);
-
- }
- module_init(mycdev_init);
- module_exit(mycdev_exit);
- MODULE_LICENSE("GPL");