分类:
2009-11-26 09:11:37
obj-m += mymodule.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) cleanmymodule.c
#include#include #include #include #include #include struct class *mymodule_class; EXPORT_SYMBOL(mymodule_class); MODULE_DESCRIPTION("mymodule module"); MODULE_AUTHOR("Chris Malley"); MODULE_LICENSE("GPL"); #define MYMODULE_MAJOR 248 static const struct file_operations mymodule_fops = { .owner = THIS_MODULE, }; static void __exit cleanup_mymodule(void) { device_destroy(mymodule_class, MKDEV(MYMODULE_MAJOR, 0)); class_destroy(mymodule_class); unregister_chrdev(MYMODULE_MAJOR, "mymodule"); } static int __init init_mymodule(void) { int err = 0; if (register_chrdev(MYMODULE_MAJOR, "mymodule", &mymodule_fops)) { printk(KERN_ERR "mymodule: unable to get major %d\n", MYMODULE_MAJOR); err = -EIO; goto out; } mymodule_class = class_create(THIS_MODULE, "mymodule"); if (IS_ERR(mymodule_class)) { err = PTR_ERR(mymodule_class); goto out_chrdev; } device_create(mymodule_class, NULL, MKDEV(MYMODULE_MAJOR, 0), "mymod0"); err = 0; goto out; out_chrdev: unregister_chrdev(MYMODULE_MAJOR, "mymodule"); out: return err; } module_init(init_mymodule); module_exit(cleanup_mymodule);