zoukankan      html  css  js  c++  java
  • 字符设备的另一种注册方法

    代码如下:

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <linux/init.h>
    #include <linux/delay.h>
    #include <linux/irq.h>
    #include <asm/uaccess.h>
    #include <asm/irq.h>
    #include <asm/io.h>
    #include <asm/arch/regs-gpio.h>
    #include <asm/hardware.h>
    #include <linux/poll.h>
    #include <linux/cdev.h>
    
    /* 确定主设备号 */
    static int major;
    static struct class *cls;
    static struct cdev my_cdev;
    
    static int open(struct inode *inode, struct file *file)
    {
    	printk(" In 'open' 
    ");
    	return 0;
    }
    
    
    /* 构造file_operations */
    static struct file_operations fops = {
    	.owner = THIS_MODULE,
    	.open  = open,
    };
    
    static int cdev_init(void)
    {
    	dev_t devid;
    	
    	if (major) {
    		devid = MKDEV(major, 0);
    		register_chrdev_region(devid, 1, "cdev");  
    	} else {
    		/* 动态申请设备编号 */
    		alloc_chrdev_region(&devid, 0, 1, "cdev"); 
    		major = MAJOR(devid);                     
    	}
    	
    	cdev_init(&my_cdev, &fops);
    	cdev_add(&my_cdev, devid, 1);
    
    	/* 创建类 */
    	cls = class_create(THIS_MODULE, "cdev");
    	/* 创建节点 /dev/cdev0 */
    	class_device_create(cls, NULL, MKDEV(major, 0), NULL, "cdev0"); 
    	
    	return 0;
    }
    
    static void cdev_exit(void)
    {
    	class_device_destroy(cls, MKDEV(major, 0));
    	class_destroy(cls);
    
    	cdev_del(&my_cdev);
    	unregister_chrdev_region(MKDEV(major, 0), 1);
    }
    
    module_init(cdev_init);
    module_exit(cdev_exit);
    
    MODULE_LICENSE("GPL");
    
  • 相关阅读:
    关于动态的添加iview admin路由以及刷新侧边栏按钮
    Qt配置,载入html,Echart, 交互
    junit、hamcrest和eclemma安装、使用
    程序中关于浮点数计算的问题
    洛谷P1164->小A点菜
    一道简单的题目
    P1101:单词方阵
    第一个博客
    php生成条形码
    php 上传音频(MP3、MP4等)文件 获取播放时间长度
  • 原文地址:https://www.cnblogs.com/GyForever1004/p/8353674.html
Copyright © 2011-2022 走看看