标签:pre ons ops node return asm span lock 占用
原本的字符设备只能有255个驱动程序,原因是一个主设备号占用了0~255的次设备号
把register_chrdev展开可得到一下几个部分:register_chrdev_region/alloc_chrdev_region,cdev_init,cdev_add
参照register_chrdev的写法完成hello.c的代码
1 #include <linux/module.h> 2 #include <linux/kernel.h> 3 #include <linux/fs.h> 4 #include <linux/init.h> 5 #include <linux/delay.h> 6 #include <linux/irq.h> 7 #include <asm/uaccess.h> 8 #include <asm/irq.h> 9 #include <asm/io.h> 10 #include <asm/arch/regs-gpio.h> 11 #include <asm/hardware.h> 12 #include <linux/poll.h> 13 #include <linux/cdev.h> 14 15 static int major; 16 17 static int hello_open(struct inode *inode, struct file *file) 18 { 19 printk("hello_open\n"); 20 return 0; 21 } 22 23 static int hello2_open(struct inode *inode, struct file *file) 24 { 25 printk("hello2_open\n"); 26 return 0; 27 } 28 29 30 31 static struct file_operations hello_fops = { 32 .owner = THIS_MODULE, 33 .open = hello_open, 34 }; 35 36 static struct file_operations hello2_fops = { 37 .owner = THIS_MODULE, 38 .open = hello2_open, 39 }; 40 41 #define HELLO_CNT 2 42 43 static struct cdev hello_cdev; 44 static struct cdev hello2_cdev; 45 static struct class *cls; 46 47 48 static int hello_init(void) 49 { 50 dev_t devid; 51 52 if (major) { 53 devid = MKDEV(major, 0); 54 register_chrdev_region(devid, HELLO_CNT, "hello"); 55 } else { 56 alloc_chrdev_region(&devid, 0, HELLO_CNT, "hello"); 57 major = MAJOR(devid); 58 } 59 cdev_init(&hello_cdev, &hello_fops); 60 cdev_add(&hello_cdev, devid, HELLO_CNT); 61 62 devid = MKDEV(major, 2); 63 register_chrdev_region(devid, 1, "hello2"); 64 cdev_init(&hello2_cdev, &hello2_fops); 65 cdev_add(&hello2_cdev, devid, 1); 66 67 cls = class_create(THIS_MODULE, "hello"); 68 class_device_create(cls, NULL, MKDEV(major, 0), NULL, "hello0"); /* /dev/hello0 */ 69 class_device_create(cls, NULL, MKDEV(major, 1), NULL, "hello1"); /* /dev/hello1 */ 70 class_device_create(cls, NULL, MKDEV(major, 2), NULL, "hello2"); /* /dev/hello2 */ 71 class_device_create(cls, NULL, MKDEV(major, 3), NULL, "hello3"); /* /dev/hello3 */ 72 73 74 return 0; 75 76 } 77 78 static void hello_exit(void) 79 { 80 class_device_destroy(cls, MKDEV(major, 0)); 81 class_device_destroy(cls, MKDEV(major, 1)); 82 class_device_destroy(cls, MKDEV(major, 2)); 83 class_device_destroy(cls, MKDEV(major, 3)); 84 class_destroy(cls); 85 86 cdev_del(&hello_cdev); 87 unregister_chrdev_region(MKDEV(major, 0), HELLO_CNT); 88 89 cdev_del(&hello2_cdev); 90 unregister_chrdev_region(MKDEV(major, 2), 1); 91 } 92 93 module_init(hello_init); 94 module_exit(hello_exit); 95 96 97 MODULE_LICENSE("GPL");
(major,0~1)对应于hello,即/dev/hello0,/dev/hello1
(major,2)对应于hello2,即/dev/hello2
/dev/hello3打不开
标签:pre ons ops node return asm span lock 占用
原文地址:http://www.cnblogs.com/Lwd-linux/p/6358227.html