実験(10)で作成したdio_drv.cを書き直すと次のようになる。
#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/cdev.h> #define DRIVERNAME "dio_drv" #define DEV_COUNT 1 MODULE_LICENSE("GPL"); static dev_t dev_num; static struct cdev char_dev; static struct file_operations dio_fops = { .owner = NULL, .read = NULL, .write = NULL, .ioctl = NULL, .open = NULL, .release = NULL, }; int __init dio_init(void) { int ret; int baseminor = 0; printk("initialize the dio_device\n"); ret = alloc_chrdev_region(&dev_num, baseminor, DEV_COUNT, DRIVERNAME); if (ret < 0) { printk(DRIVERNAME ": Touroku Shippai\n"); goto err1; } cdev_init(&char_dev, &dio_fops); char_dev.owner = THIS_MODULE; ret = cdev_add(&char_dev, dev_num, DEV_COUNT); if (ret < 0) { goto err2; } printk(DRIVERNAME ": Touroku Seikou. Major Number=%d\n", MAJOR(dev_num)); return 0; err2: cdev_del(&char_dev); unregister_chrdev_region(dev_num, DEV_COUNT); err1: return ret; } void __exit dio_cleanup(void) { printk("cleanup the dio_device\n"); cdev_del(&char_dev); unregister_chrdev_region(dev_num, DEV_COUNT); printk(DRIVERNAME ": Sakujo Seikou. Major Number=%d\n", MAJOR(dev_num)); } module_init(dio_init); module_exit(dio_cleanup);