ioctl实现功能的控制风扇、蜂鸣器、马达、LED灯,编写四个硬件驱动,一个应用程序控制4个硬件

头文件

#ifndef __HEAD_H__
#define __HEAD_H__ 

typedef struct{
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;	
}gpio_t;

//地址
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_FMQ_ADDR 0X50003000
#define PHY_FS_ADDR 0X50006000
#define PHY_MD_ADDR 0X50007000
#define PHY_RCC_ADDR 0X50000A28

//开关灯功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
#define FMQ_ON _IO('l', 1)
#define FMQ_OFF _IO('l', 0)
#define FS_ON _IO('l', 1)
#define FS_OFF _IO('l', 0)
#define MD_ON _IO('l', 1)
#define MD_OFF _IO('l', 0)

#endif 

应用程序

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

int main(int argc, char const *argv[])
{
    int a, b, c;
    char buf[128] = {0};
    int fdm = open("/dev/mycdev_fmq", O_RDWR);
    int fds = open("/dev/mycdev_fs", O_RDWR);
    int fdl = open("/dev/mycdev_led", O_RDWR);
    int fdd = open("/dev/mycdev_md", O_RDWR);
    if (fdd < 0 | fdl < 0 | fds < 0 | fdm < 0){
        printf("设备文件打开失败\n");
        exit(-1);
    }
    while (1)
    {
    A:
        printf("请输入驱动的程序>>>\n 1(风扇)\t 2(蜂鸣器)\t 3(马达)\t 4(LED灯)\t 其他(退出)\n");
        scanf("%d", &a);
        switch (a)
        {
        case 1:
            while (1)
            {
                printf("请输入操作码>>>\n 1(风扇转动)\t 0(风扇停)\t 其他(返回上一级菜单)\n");
                scanf("%d", &b);
                switch (b)
                {
                case 1:
                    ioctl(fds, FS_ON); // 转
                    break;
                case 0:
                    ioctl(fds, FS_OFF); // 停
                    break;
                default:
                    goto A;
                }
            }
            break;
        case 2:
            while (1)
            {
                printf("请输入操作码>>>\n 1(蜂鸣器响)\t 0(蜂鸣器停)\t 其他(返回上一级菜单)\n");
                scanf("%d", &b);
                switch (b)
                {
                case 1:
                    ioctl(fdm, FMQ_ON); // 响
                    break;
                case 0:
                    ioctl(fdm, FMQ_OFF); // 停
                    break;
                default:
                    goto A;
                }
            }
            break;
        case 3:
            while (1)
            {
                printf("请输入操作码>>>\n 1(震动马达启动)\t 0(震动马达停)\t 其他(返回上一级菜单)\n");
                scanf("%d", &b);
                switch (b)
                {
                case 1:
                    ioctl(fdd, MD_ON); // 响
                    break;
                case 0:
                    ioctl(fdd, MD_OFF); // 停
                    break;
                default:
                    goto A;
                }
            }
            break;
        case 4:
            while (1)
            {
            B:
                printf("请输入要进行的操作>>>\n 1(开灯)\t 0(关灯)\t 其他(返回上一级菜单)\n");
                scanf("%d", &b);
                switch (b)
                {
                case 1:
                    printf("请输入要操作的灯>>>\n 1(LED1)\t 2(LED2)\t \n 3(LED3)\n");
                    scanf("%d", &c);
                    if (c > 3 & c < 1)
                    {
                        printf("输入错误请重新输入\n");
                        goto B;
                    }
                    ioctl(fdl, LED_ON, &c); // 开灯
                    break;
                case 0:
                    printf("请输入要操作的灯>>>\n 1(LED1)\t 2(LED2)\t \n 3(LED3)\n");
                    scanf("%d", &c);
                    if (c > 3 & c < 1)
                    {
                        printf("输入错误请重新输入\n");
                        goto B;
                    }
                    ioctl(fdl, LED_OFF, &c); // 关灯
                    break;
                default:
                    goto A;
                }
            }
            break;
        default:
            close(fdm);
            close(fds);
            close(fdl);
            close(fdd);
            return 0;
        }
    }
    close(fdm);
    close(fds);
    close(fdl);
    close(fdd);
    return 0;
}

灯驱动

#include 
#include 
#include 
#include 
#include 
#include "head.h"
 
int major;
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//封装驱动中的操作方法
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 *lof){
    unsigned int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);//内核向用户拷贝数据
    if(ret){
        printk("copy_to_user filed%d\n",__LINE__);
        return ret;
    }
    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__);
    unsigned int ret;
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);//从用户拷贝数据
    if(ret){
        printk("copy_from_user filed%d\n",__LINE__);
        return ret;
    }
   if(kbuf[0] == '1'){
    (vir_led1->ODR) |= (1<<10);
   }else if(kbuf[0] == '2'){
    (vir_led1->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '3'){
    (vir_led2->ODR) |= (1<<10);
   }else if(kbuf[0] == '4'){
    (vir_led2->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '5'){
    (vir_led3->ODR) |= (1<<8);
   }else if(kbuf[0] == '6'){
    (vir_led3->ODR) &= (~(1<<8));
   }
    return 0;
}*/
int mycdev_close(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;
    int ret = copy_from_user(&which, (void *)arg, 4);
    if(ret){
        printk("copy_from_user filed\n");
        return ret;
    }
    switch(cmd){
        case LED_ON:
            switch(which){
                case 1:
                    (vir_led1->ODR) |= (1<<10);
                    break;
                case 2:
                    (vir_led2->ODR) |= (1<<10);
                    break;
                case 3:
                    (vir_led3->ODR) |= (1<<8);
                    break;
                default:return 0;
            }
            break;
        case LED_OFF:
            switch(which){
                case 1:
                    (vir_led1->ODR) &= (~(1<<10));
                    break;
                case 2:
                    (vir_led2->ODR) &= (~(1<<10));
                    break;
                case 3:
                    (vir_led3->ODR) &= (~(1<<8));
                    break;
                default:return 0;
            }
            break;
    }
    return 0;
}
//定义操作方法结构体变量
struct file_operations fops = {
    .open = mycdev_open,
    /*.read = mycdev_read,
    .write = mycdev_write,*/
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};
int all_led_init(void){
    //寄存器地址映射
    vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    if(vir_led1 == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    if(vir_led2 == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    vir_led3 = vir_led1;
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //RCC层寄存器的初始化
    (*vir_rcc) |= (1<<4);
    (*vir_rcc) |= (1<<5);
    //LED1寄存器的初始化
    (vir_led1->MODER) &= (~(3<<20));
    (vir_led1->MODER) |= (1<<20);
    (vir_led1->ODR) &= (~(1<<10));
    //LED2寄存器的初始化
    (vir_led2->MODER) &= (~(3<<20));
    (vir_led2->MODER) |= (1<<20);
    (vir_led2->ODR) &= (~(1<<10));
    //LED3寄存器的初始化
    (vir_led3->MODER) &= (~(3<<16));
    (vir_led3->MODER) |= (1<<16);
    (vir_led3->ODR) &= (~(1<<8));
    printk("寄存器初始化成功\n");
    return 0;
}
static int __init mycdev_init(void){
    //字符设备驱动的注册
    major = register_chrdev(0,"mycdev_led", &fops);
    if(major < 0){
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);
    //向上提交目录
    cls = class_create(THIS_MODULE, "mycdev_led");
    if(IS_ERR(cls)){
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major,0), NULL, "mycdev_led");
    if(IS_ERR(dev)){
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功\n");
    //寄存器映射初始化
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void){
    //取消内存映射
    iounmap(vir_led1);
    iounmap(vir_led2);
    iounmap(vir_rcc);
    //字符设备驱动的注销
    unregister_chrdev(cls, MKDEV(major, 0));
    //销毁目录空间
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major, "mychrdev_led");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

风扇驱动

#include 
#include 
#include 
#include 
#include 
#include "head.h"
 
int major;
char kbuf[128] = {0};
gpio_t *vir_fs;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//封装驱动中的操作方法
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 *lof){
    unsigned int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);//内核向用户拷贝数据
    if(ret){
        printk("copy_to_user filed%d\n",__LINE__);
        return ret;
    }
    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__);
    unsigned int ret;
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);//从用户拷贝数据
    if(ret){
        printk("copy_from_user filed%d\n",__LINE__);
        return ret;
    }
   if(kbuf[0] == '1'){
    (vir_led1->ODR) |= (1<<10);
   }else if(kbuf[0] == '2'){
    (vir_led1->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '3'){
    (vir_led2->ODR) |= (1<<10);
   }else if(kbuf[0] == '4'){
    (vir_led2->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '5'){
    (vir_led3->ODR) |= (1<<8);
   }else if(kbuf[0] == '6'){
    (vir_led3->ODR) &= (~(1<<8));
   }
    return 0;
}*/
int mycdev_close(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){
    switch(cmd){
        case FS_ON:
            (vir_fs->ODR) |= (1 << 9);
            break;
        case FS_OFF:
            (vir_fs->ODR) &= (~(1 << 9));
            break;
    }
    return 0;
}
//定义操作方法结构体变量
struct file_operations fops = {
    .open = mycdev_open,
    /*.read = mycdev_read,
    .write = mycdev_write,*/
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};
int all_led_init(void){
    //寄存器地址映射
    vir_fs = ioremap(PHY_FS_ADDR, sizeof(gpio_t));
    if(vir_fs == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //RCC层寄存器的初始化
    (*vir_rcc) |= (1 << 4);
    //GPIO层寄存器初始化
    (vir_fs->MODER) &= (~(0x3 << 18));
    (vir_fs->MODER) |= (0x1 << 18);
    (vir_fs->ODR ) &= (~(0xf << 9));
    printk("寄存器初始化成功\n");
    return 0;
}
static int __init mycdev_init(void){
    //字符设备驱动的注册
    major = register_chrdev(0,"mycdev_fs", &fops);
    if(major < 0){
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);
    //向上提交目录
    cls = class_create(THIS_MODULE, "mycdev_fs");
    if(IS_ERR(cls)){
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major,0), NULL, "mycdev_fs");
    if(IS_ERR(dev)){
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功\n");
    //寄存器映射初始化
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void){
    //取消内存映射
    iounmap(vir_fs);
    iounmap(vir_rcc);
    //销毁设备节点信息
    unregister_chrdev(cls, MKDEV(major, 0));
    //销毁目录空间
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major, "mycdev_fs");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

马达驱动

#include 
#include 
#include 
#include 
#include 
#include "head.h"
 
int major;
char kbuf[128] = {0};
gpio_t *vir_md;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//封装驱动中的操作方法
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 *lof){
    unsigned int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);//内核向用户拷贝数据
    if(ret){
        printk("copy_to_user filed%d\n",__LINE__);
        return ret;
    }
    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__);
    unsigned int ret;
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);//从用户拷贝数据
    if(ret){
        printk("copy_from_user filed%d\n",__LINE__);
        return ret;
    }
   if(kbuf[0] == '1'){
    (vir_led1->ODR) |= (1<<10);
   }else if(kbuf[0] == '2'){
    (vir_led1->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '3'){
    (vir_led2->ODR) |= (1<<10);
   }else if(kbuf[0] == '4'){
    (vir_led2->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '5'){
    (vir_led3->ODR) |= (1<<8);
   }else if(kbuf[0] == '6'){
    (vir_led3->ODR) &= (~(1<<8));
   }
    return 0;
}*/
int mycdev_close(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){
    switch(cmd){
        case MD_ON:
            (vir_md->ODR) |= (1 << 6);
            break;
        case MD_OFF:
            (vir_md->ODR) &= (~(1 << 6));
            break;
    }
    return 0;
}
//定义操作方法结构体变量
struct file_operations fops = {
    .open = mycdev_open,
    /*.read = mycdev_read,
    .write = mycdev_write,*/
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};
int all_led_init(void){
    //寄存器地址映射
    vir_md = ioremap(PHY_MD_ADDR, sizeof(gpio_t));
    if(vir_md == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //RCC层寄存器的初始化
    (*vir_rcc) |= (1 << 5);
    //GPIO层寄存器初始化
    (vir_md->MODER) &= (~(0x3 << 12));
    (vir_md->MODER) |= (0x1 << 12);
    (vir_md->ODR ) &= (~(0xf << 6));
    printk("寄存器初始化成功\n");
    return 0;
}
static int __init mycdev_init(void){
    //字符设备驱动的注册
    major = register_chrdev(0,"mycdev_md", &fops);
    if(major < 0){
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);
    //向上提交目录
    cls = class_create(THIS_MODULE, "mycdev_md");
    if(IS_ERR(cls)){
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major,0), NULL, "mycdev_md");
    if(IS_ERR(dev)){
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功\n");
    //寄存器映射初始化
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void){
    //取消内存映射
    iounmap(vir_md);
    iounmap(vir_rcc);
    //字符设备驱动的注销
    unregister_chrdev(cls, MKDEV(major, 0));
    //销毁目录空间
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major, "mycdev_md");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

蜂鸣器驱动

#include 
#include 
#include 
#include 
#include 
#include "head.h"
 
int major;
char kbuf[128] = {0};
gpio_t *vir_fmq;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//封装驱动中的操作方法
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 *lof){
    unsigned int ret;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);//内核向用户拷贝数据
    if(ret){
        printk("copy_to_user filed%d\n",__LINE__);
        return ret;
    }
    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__);
    unsigned int ret;
    if(size > sizeof(kbuf))//判断用户的需求,如果内核满足不了,给内核能满足的最大值
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);//从用户拷贝数据
    if(ret){
        printk("copy_from_user filed%d\n",__LINE__);
        return ret;
    }
   if(kbuf[0] == '1'){
    (vir_led1->ODR) |= (1<<10);
   }else if(kbuf[0] == '2'){
    (vir_led1->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '3'){
    (vir_led2->ODR) |= (1<<10);
   }else if(kbuf[0] == '4'){
    (vir_led2->ODR) &= (~(1<<10));
   }else if(kbuf[0] == '5'){
    (vir_led3->ODR) |= (1<<8);
   }else if(kbuf[0] == '6'){
    (vir_led3->ODR) &= (~(1<<8));
   }
    return 0;
}*/
int mycdev_close(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){
    switch(cmd){
        case FMQ_ON:
            (vir_fmq->ODR) |= (1 << 6);
            break;
        case FMQ_OFF:
            (vir_fmq->ODR) &= (~(1 << 6));
            break;
    }
    return 0;
}
//定义操作方法结构体变量
struct file_operations fops = {
    .open = mycdev_open,
    /*.read = mycdev_read,
    .write = mycdev_write,*/
    .release = mycdev_close,
    .unlocked_ioctl = mycdev_ioctl,
};
int all_led_init(void){
    //寄存器地址映射
    vir_fmq = ioremap(PHY_FMQ_ADDR, sizeof(gpio_t));
    if(vir_fmq == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    if(vir_rcc == NULL){
        printk("ioremap filed: %d\n", __LINE__);
        return -ENOMEM;
    }
    printk("物理地址映射成功\n");
    //RCC层寄存器的初始化
    (*vir_rcc) |= (1 << 1);
    //GPIO层寄存器初始化
    (vir_fmq->MODER) &= (~(0x3 << 12));
    (vir_fmq->MODER) |= (0x1 << 12);
    (vir_fmq->ODR ) &= (~(0xf << 6));
    printk("寄存器初始化成功\n");
    return 0;
}
static int __init mycdev_init(void){
    //字符设备驱动的注册
    major = register_chrdev(0,"mycdev_fmq", &fops);
    if(major < 0){
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n", major);
    //向上提交目录
    cls = class_create(THIS_MODULE, "mycdev_fmq");
    if(IS_ERR(cls)){
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major,0), NULL, "mycdev_fmq");
    if(IS_ERR(dev)){
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功\n");
    //寄存器映射初始化
    all_led_init();
    return 0;
}
static void __exit mycdev_exit(void){
    //取消内存映射
    iounmap(vir_fmq);
    iounmap(vir_rcc);
    //字符设备驱动的注销
    unregister_chrdev(cls, MKDEV(major, 0));
    //销毁目录空间
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major, "mycdev_fmq");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

你可能感兴趣的:(c语言,开发语言)