MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mtd / mtdblock_ro.c
blob226dc7352b0b3960d36013ce3848cfd7fd813bb9
1 /*
2 * $Id: mtdblock_ro.c,v 1.18 2003/06/23 12:00:08 dwmw2 Exp $
4 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
6 * Simple read-only (writable only for RAM) mtdblock driver
8 * (C) 2002 David McCullough, Added MAGIC_ROM_PTR support
9 */
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/blktrans.h>
16 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
17 unsigned long block, char *buf)
19 size_t retlen;
21 if (dev->mtd->read(dev->mtd, (block * 512), 512, &retlen, buf))
22 return 1;
23 return 0;
26 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
27 unsigned long block, char *buf)
29 size_t retlen;
31 if (dev->mtd->write(dev->mtd, (block * 512), 512, &retlen, buf))
32 return 1;
33 return 0;
36 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
38 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
40 if (!dev)
41 return;
43 memset(dev, 0, sizeof(*dev));
45 dev->mtd = mtd;
46 dev->devnum = mtd->index;
47 dev->blksize = 512;
48 dev->size = mtd->size >> 9;
49 dev->tr = tr;
50 if ((mtd->flags & (MTD_CLEAR_BITS|MTD_SET_BITS|MTD_WRITEABLE)) !=
51 (MTD_CLEAR_BITS|MTD_SET_BITS|MTD_WRITEABLE))
52 dev->readonly = 1;
54 add_mtd_blktrans_dev(dev);
57 #ifdef CONFIG_MAGIC_ROM_PTR
58 #include <linux/mm.h>
59 static int
60 mtdblock_romptr(struct mtd_blktrans_dev *dev, struct vm_area_struct * vma)
62 struct mtd_info *mtd = dev->mtd;
63 u_char *ptr;
64 size_t len;
66 if (!mtd->point /* Can't do it, No function to point to correct addr */
68 || (*mtd->point)(mtd,vma->vm_pgoff,vma->vm_end-vma->vm_start,&len,&ptr) != 0)
69 return -ENOSYS;
71 vma->vm_start = (unsigned long) ptr;
72 vma->vm_end = vma->vm_start + len;
73 return 0;
75 #endif
77 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
79 del_mtd_blktrans_dev(dev);
80 kfree(dev);
83 struct mtd_blktrans_ops mtdblock_tr = {
84 .name = "mtdblock",
85 .major = 31,
86 .part_bits = 0,
87 .readsect = mtdblock_readsect,
88 .writesect = mtdblock_writesect,
89 .add_mtd = mtdblock_add_mtd,
90 .remove_dev = mtdblock_remove_dev,
91 .owner = THIS_MODULE,
92 #ifdef CONFIG_MAGIC_ROM_PTR
93 .romptr = mtdblock_romptr,
94 #endif
97 static int __init mtdblock_init(void)
99 return register_mtd_blktrans(&mtdblock_tr);
102 static void __exit mtdblock_exit(void)
104 deregister_mtd_blktrans(&mtdblock_tr);
107 module_init(mtdblock_init);
108 module_exit(mtdblock_exit);
110 MODULE_LICENSE("GPL");
111 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
112 MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");