编译进内核还是编译为模块
#ifdef MODULE module_init(fbmem_init); static void __exit fbmem_exit(void) { remove_proc_entry("fb", NULL); class_destroy(fb_class); unregister_chrdev(FB_MAJOR, "fb"); } module_exit(fbmem_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Framebuffer base"); #else subsys_initcall(fbmem_init); #endif
#ifdef MODULE。当定义了MODULE宏的时候,使用module_init,否则用subsys_initcall
fbmem_init函数
static int __init fbmem_init(void) { proc_create("fb", 0, NULL, &fb_proc_fops); if (register_chrdev(FB_MAJOR,"fb",&fb_fops)) printk("unable to get major %d for fb devs\n", FB_MAJOR); fb_class = class_create(THIS_MODULE, "graphics"); if (IS_ERR(fb_class)) { printk(KERN_WARNING "Unable to create fb class; errno = %ld\n", PTR_ERR(fb_class)); fb_class = NULL; } return 0; }
- proc_create创建fb设备在proc文件系统下的相关的东西
- register_chrdev注册fb设备,cat /proc/devices可以看到所有已经注册了的设备
- class_create创建graphics类,在/sys/class目录下会生成graphics文件夹
fb_proc_fops
static const struct file_operations fb_proc_fops = { .owner = THIS_MODULE, .open = proc_fb_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release, };
static int proc_fb_open(struct inode *inode, struct file *file) { return seq_open(file, &proc_fb_seq_ops); }
static const struct seq_operations proc_fb_seq_ops = { .start = fb_seq_start, .next = fb_seq_next, .stop = fb_se