本文共 3474 字,大约阅读时间需要 11 分钟。
#include#include // 定义module_param module_param_arrary 函数的头文件#include // 定义参数函数中perm 参数的枚举量头文件#include // MKDEV 转换设备号数据类型的宏定义#include // 三个字符设备号函数#include // 定义字符设备的结构体#include // 分配内存空间的函数#include #defind DEVICE_NAME "dcdev";#defind DEVICE_MINOR_NUM 2;#defind DEVICE_MAJOR 0;// 主设备号#defind DEVICE_MINOR 0;#defind REGDEV_SIZE 3000;#include ;static struct class *myClass;module_init(sdev_init);module_exit(sdev_exit);static int module_arg1,module_arg2;static int int_array[50];static int int_num;int numdev_major = DEVICE_MAJOR;int numdev_minor = DEVICE_MINOR;/**/// 输入主设备号module_param(numdev_major,int,S_IRUSR);// 输入次设备号module_param(numdev_minor,int,S_IRUSR);struct reg_dev{ char* data; unsigned long size; struct cdev cdev;};struct file_operations my_fops = { .owner = THIS_MODULE,};static void ret_init_cdev(struct reg_dev * dev,int index){ int error; /*数据初始化*/ int devno = MKDEV(numdev_major,numdev_minor+index); cdev_init(&dev->cdev,&my_fops); dev->cdev.owner = THIS_MODULE; dev->cdev.ops = &my_fops; err = cdev_add(&dev->cdev,devno,1); if(err){ printk(KERN_EMERG,"\t cdev_add is fail err is %d \n ",err); }else{ printk(KERN_EMERG,"\t cdev_add success! \n" ); }}struct reg_dev* my_devices;static int sdev_init(void){ int ret = -1; dev_t num_dev; if(numdev_major){ // 如果0 则动态申请 num_dev = MKDEV(numdev_major,numdev_minor); ret = register_chrdev_region(num_dev,DEVICE_MINOR_NUM,DEVICE_NAME); }else{ ret = allo_chrdev_region(&num_dev,numdev_minor,DEVICE_MINOR_NUM,DEVICE_NAME); numdev_major = MAJOR(num_dev); printk(KERN_EMERG,"\t numdev_major %d is failed\n ",numdev_major); } if(!ret){ printk(KERN_EMERG,"\t register_chrdev_region req %d is failed\n ",numdev_major); } myClass = class_create(THIS_MODULE,DEVICE_NAME); my_devices = kmalloc(DEVICE_MINOR_NUM*sizeof(struct reg_dev),GPF_KERNEL); if(!my_devices){ ret = -ENOMEM; goto fail; return 0; } /*初始化设备*/ for(int i = 0 ;i < DEVICE_MINOR_NUM;i++){ my_devices[i].data = kmalloc(REGDEV_SIZE,GFP_KERNEL); memset(my_devices[i].data,0,REGDEV_SIZE); /*设备注册到系统*/ ret_init_cdev(&my_devices[i],i); /*创建设备节点*/ device_create(myClass,NULL,MKDEV(numdev_major,numdev_minor+i),NULL,DEVICE_NAME"%d",i); } memset(my_devices,0,DEVICE_MINOR_NUM * sizeof(struct reg_dev)); printk(KERN_EMERG,"\ dcdev enter!\n "); return 0;fail: /*注销设备号*/ unregister_chrdev_region(MKDEV(numdev_major,numdev_minor),DEVICE_MINOR_NUM); printk(KERN_EMERG,"\kmalloc is fail!\n "); return 0;}static int sdev_exit(void){ int i = 0; printk(KERN_EMERG,"\t cdev is:%d\n ",module_arg2); /*除去字符设备*/ for(i = 0;i < DEVICE_MINOR_NUM;i++){ cdev_del(&(my_devices[i]->cdev)); device_destory(myClass,MKDEV(numdev_major,numdev_minor+i)); } class_destory(myClass); kfree(my_devices); unregister_chrdev_region(MKDEV(numdev_major,numdev_minor),DEVICE_MINOR_NUM); return 0;}
ls /sys/class 能看到新的设备类节点ls /dev/ 看到设备节点
mknod dev/test0 c 249 0mknod dev/test0 c 249 1
转载地址:http://vhoin.baihongyu.com/