完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
概念
#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 failedn ",numdev_major); } if(!ret){ printk(KERN_EMERG,"t register_chrdev_region req %d is failedn ",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.data = kmalloc(REGDEV_SIZE,GFP_KERNEL); memset(my_devices.data,0,REGDEV_SIZE); /*设备注册到系统*/ ret_init_cdev(&my_devices,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:%dn ",module_arg2); /*除去字符设备*/ for(i = 0;i < DEVICE_MINOR_NUM;i++){ cdev_del(&(my_devices->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 |
|
|
|
只有小组成员才能发言,加入小组>>
771 浏览 0 评论
1146 浏览 1 评论
2527 浏览 5 评论
2858 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2709 浏览 6 评论
keil5中manage run-time environment怎么是灰色,不可以操作吗?
1056浏览 3评论
189浏览 2评论
453浏览 2评论
364浏览 2评论
M0518 PWM的电压输出只有2V左右,没有3.3V是怎么回事?
449浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-19 01:17 , Processed in 1.082530 second(s), Total 80, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号