Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* drivers/char/memdev.c
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- */
- #include <asm/uaccess.h>
- #include <linux/module.h>
- #include <linux/cdev.h>
- #include <linux/semaphore.h>
- #include <linux/sched.h>
- #include <linux/wait.h>
- #include <linux/fs.h>
- #include <linux/mm.h>
- #include <linux/device.h>
- #define DEVICE_NAME "memdev"
- static int memdev_mmap(struct file *filp, struct vm_area_struct *vma)
- {
- int err;
- err = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, vma->vm_end - vma->vm_start, vma->vm_page_prot);
- if (err) {
- printk(KERN_ALERT "%s :io_remap_pfn_range failed.: \n", __func__);
- return -EAGAIN;
- }
- return 0;
- }
- static int memdev_open(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- static int memdev_release(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- static struct file_operations memdev_fops = {
- .owner = THIS_MODULE,
- .mmap = memdev_mmap,
- .open = memdev_open,
- .release = memdev_release
- };
- static int alloc_chrdev_region_ok = 0; /* false */
- static int cdev_add_ok = 0; /* false */
- static int class_create_ok = 0; /* false */
- static int device_create_ok = 0; /* false */
- static struct cdev memdev_cdev;
- static dev_t memdev_dev;
- static struct class *memdev_class;
- static void cleanup(void)
- {
- if (device_create_ok) {
- device_destroy(memdev_class, memdev_dev);
- }
- if (class_create_ok) {
- class_destroy(memdev_class);
- }
- if (cdev_add_ok) {
- cdev_del(&memdev_cdev);
- }
- if (alloc_chrdev_region_ok) {
- unregister_chrdev_region(memdev_dev, 1);
- }
- return;
- }
- static int __init memdev_init(void)
- {
- int err = -1;
- err = alloc_chrdev_region(&memdev_dev, 0, 1, DEVICE_NAME);
- if (err) {
- printk(KERN_ALERT "%s :alloc_chrdev_region failed.: %d\n", __func__, err);
- goto fail;
- } else {
- alloc_chrdev_region_ok = 1; /* true */
- }
- cdev_init(&memdev_cdev, &memdev_fops);
- memdev_cdev.owner = THIS_MODULE;
- err = cdev_add(&memdev_cdev, memdev_dev, 1);
- if (err) {
- printk(KERN_ALERT "%s :cdev_add failed.: %d\n", __func__, err);
- goto fail;
- } else {
- cdev_add_ok = 1; /* true */
- }
- /* make device file */
- memdev_class = class_create(THIS_MODULE, DEVICE_NAME);
- if (IS_ERR(memdev_class)) {
- printk(KERN_ALERT "%s :class_create failed.: \n", __func__);
- goto fail;
- } else {
- class_create_ok = 1; /* true */
- }
- if (device_create(memdev_class, NULL, memdev_dev,
- &memdev_cdev, DEVICE_NAME) < 0) {
- printk(KERN_ALERT "%s :device_create failed.: \n", __func__);
- goto fail;
- } else {
- device_create_ok = 1; /* true */
- }
- return 0;
- fail:
- cleanup();
- return err;
- }
- module_init(memdev_init);
- static void __exit memdev_exit(void)
- {
- cleanup();
- return;
- }
- module_exit(memdev_exit);
- MODULE_DESCRIPTION("MEMDEV DRIVER MODULE");
- MODULE_LICENSE("GPL v2");
- MODULE_AUTHOR("goroh_kun");
- MODULE_VERSION("1.00");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement