Revert last change. Bug noticed by Linus.
[linux-2.6/linux-mips.git] / drivers / mtd / mtdram.c
blob674435272ceecb8dec609e66f8165daa54613621
1 /*
2 * mtdram - a test mtd device
3 * $Id: mtdram.c,v 1.13 2000/07/03 10:01:38 dwmw2 Exp $
4 * Author: Alexander Larsson <alex@cendio.se>
6 * Copyright (c) 1999 Alexander Larsson <alex@cendio.se>
8 * This code is GPL
12 #include <linux/module.h>
14 #include <linux/malloc.h>
15 #include <linux/ioport.h>
16 #include <linux/mtd/compatmac.h>
17 #include <linux/mtd/mtd.h>
20 #define MTDRAM_TOTAL_SIZE 1024*1024*8
21 #define MTDRAM_ERASE_SIZE 4*1024
24 // We could store these in the mtd structure, but we only support 1 device..
25 static struct mtd_info *mtd_info;
28 static int
29 ram_erase(struct mtd_info *mtd, struct erase_info *instr)
31 if (instr->addr + instr->len > mtd->size)
32 return -EINVAL;
34 memset((char *)mtd->priv + instr->addr, 0xff, instr->len);
36 instr->state = MTD_ERASE_DONE;
38 if (instr->callback)
39 (*(instr->callback))(instr);
40 return 0;
43 static int ram_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
45 if (from + len > mtd->size)
46 return -EINVAL;
48 *mtdbuf = mtd->priv + from;
49 *retlen = len;
50 return 0;
53 static void ram_unpoint (struct mtd_info *mtd, u_char *addr)
57 static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
58 size_t *retlen, u_char *buf)
60 if (from + len > mtd->size)
61 return -EINVAL;
63 memcpy(buf, mtd->priv + from, len);
65 *retlen=len;
66 return 0;
69 static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
70 size_t *retlen, const u_char *buf)
72 if (to + len > mtd->size)
73 return -EINVAL;
75 memcpy ((char *)mtd->priv + to, buf, len);
77 *retlen=len;
78 return 0;
81 #if LINUX_VERSION_CODE < 0x20300
82 #ifdef MODULE
83 #define init_mtdram init_module
84 #define cleanup_mtdram cleanup_module
85 #endif
86 #endif
88 //static void __exit cleanup_mtdram(void)
89 mod_exit_t cleanup_mtdram(void)
91 if (mtd_info) {
92 del_mtd_device(mtd_info);
93 if (mtd_info->priv)
94 vfree(mtd_info->priv);
95 kfree(mtd_info);
99 extern struct module __this_module;
101 mod_init_t init_mtdram(void)
103 // Allocate some memory
104 mtd_info = (struct mtd_info *)kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
105 if (mtd_info == 0)
106 return 0;
108 memset(mtd_info, 0, sizeof(*mtd_info));
110 // Setup the MTD structure
111 mtd_info->name = "mtdram test device";
112 mtd_info->type = MTD_RAM;
113 mtd_info->flags = MTD_CAP_RAM;
114 mtd_info->size = MTDRAM_TOTAL_SIZE;
115 mtd_info->erasesize = MTDRAM_ERASE_SIZE;
116 mtd_info->priv = vmalloc(MTDRAM_TOTAL_SIZE);
117 memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
119 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
120 mtd_info->module = THIS_MODULE;
121 #endif
123 if (!mtd_info->priv) {
124 kfree(mtd_info);
125 mtd_info = NULL;
126 return -ENOMEM;
128 mtd_info->erase = ram_erase;
129 mtd_info->point = ram_point;
130 mtd_info->unpoint = ram_unpoint;
131 mtd_info->read = ram_read;
132 mtd_info->write = ram_write;
134 if (add_mtd_device(mtd_info)) {
135 vfree(mtd_info->priv);
136 kfree(mtd_info);
137 mtd_info = NULL;
138 return -EIO;
141 return 0;
144 #if LINUX_VERSION_CODE > 0x20300
145 module_init(init_mtdram);
146 module_exit(cleanup_mtdram);
147 #endif