MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mtd / mtdblock.c
blobfc74687c470bcca2fd6b72aecc4f582222c0e80a
1 /*
2 * Direct MTD block device access
4 * $Id: mtdblock.c,v 1.64 2003/10/04 17:14:14 dwmw2 Exp $
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
8 * (C) 2002 David McCullough (Added MAGIC_ROM_PTR support)
9 */
11 #include <linux/config.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/fs.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/blktrans.h>
22 static struct mtdblk_dev {
23 struct mtd_info *mtd;
24 int count;
25 struct semaphore cache_sem;
26 unsigned char *cache_data;
27 unsigned long cache_offset;
28 unsigned int cache_size;
29 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
30 } *mtdblks[MAX_MTD_DEVICES];
33 * Cache stuff...
35 * Since typical flash erasable sectors are much larger than what Linux's
36 * buffer cache can handle, we must implement read-modify-write on flash
37 * sectors for each block write requests. To avoid over-erasing flash sectors
38 * and to speed things up, we locally cache a whole flash sector while it is
39 * being written to until a different sector is required.
42 static void erase_callback(struct erase_info *done)
44 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
45 wake_up(wait_q);
48 static int erase_write (struct mtd_info *mtd, unsigned long pos,
49 int len, const char *buf)
51 struct erase_info erase;
52 DECLARE_WAITQUEUE(wait, current);
53 wait_queue_head_t wait_q;
54 size_t retlen;
55 int ret;
58 * First, let's erase the flash block.
61 init_waitqueue_head(&wait_q);
62 erase.mtd = mtd;
63 erase.callback = erase_callback;
64 erase.addr = pos;
65 erase.len = len;
66 erase.priv = (u_long)&wait_q;
68 #if 1 // mask by Victor Yu. 05-14-2007
69 set_current_state(TASK_INTERRUPTIBLE);
70 #endif
71 add_wait_queue(&wait_q, &wait);
73 ret = MTD_ERASE(mtd, &erase);
74 if (ret) {
75 #if 1 // mask by Victor Yu. 05-14-2007
76 set_current_state(TASK_RUNNING);
77 #endif
78 remove_wait_queue(&wait_q, &wait);
79 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
80 "on \"%s\" failed\n",
81 pos, len, mtd->name);
82 return ret;
85 schedule(); /* Wait for erase to finish. */
86 remove_wait_queue(&wait_q, &wait);
89 * Next, writhe data to flash.
92 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
93 if (ret)
94 return ret;
95 if (retlen != len)
96 return -EIO;
97 return 0;
101 static int write_cached_data (struct mtdblk_dev *mtdblk)
103 struct mtd_info *mtd = mtdblk->mtd;
104 int ret;
106 if (mtdblk->cache_state != STATE_DIRTY)
107 return 0;
109 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
110 "at 0x%lx, size 0x%x\n", mtd->name,
111 mtdblk->cache_offset, mtdblk->cache_size);
113 ret = erase_write (mtd, mtdblk->cache_offset,
114 mtdblk->cache_size, mtdblk->cache_data);
115 if (ret)
116 return ret;
119 * Here we could argubly set the cache state to STATE_CLEAN.
120 * However this could lead to inconsistency since we will not
121 * be notified if this content is altered on the flash by other
122 * means. Let's declare it empty and leave buffering tasks to
123 * the buffer cache instead.
125 mtdblk->cache_state = STATE_EMPTY;
126 return 0;
130 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
131 int len, const char *buf)
133 struct mtd_info *mtd = mtdblk->mtd;
134 unsigned int sect_size = mtdblk->cache_size;
135 size_t retlen;
136 int ret;
138 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
139 mtd->name, pos, len);
141 if (!sect_size)
142 return MTD_WRITE (mtd, pos, len, &retlen, buf);
144 while (len > 0) {
145 unsigned long sect_start = (pos/sect_size)*sect_size;
146 unsigned int offset = pos - sect_start;
147 unsigned int size = sect_size - offset;
148 if( size > len )
149 size = len;
151 if (size == sect_size) {
153 * We are covering a whole sector. Thus there is no
154 * need to bother with the cache while it may still be
155 * useful for other partial writes.
157 ret = erase_write (mtd, pos, size, buf);
158 if (ret)
159 return ret;
160 } else {
161 /* Partial sector: need to use the cache */
163 if (mtdblk->cache_state == STATE_DIRTY &&
164 mtdblk->cache_offset != sect_start) {
165 ret = write_cached_data(mtdblk);
166 if (ret)
167 return ret;
170 if (mtdblk->cache_state == STATE_EMPTY ||
171 mtdblk->cache_offset != sect_start) {
172 /* fill the cache with the current sector */
173 mtdblk->cache_state = STATE_EMPTY;
174 ret = MTD_READ(mtd, sect_start, sect_size, &retlen, mtdblk->cache_data);
175 if (ret)
176 return ret;
177 if (retlen != sect_size)
178 return -EIO;
180 mtdblk->cache_offset = sect_start;
181 mtdblk->cache_size = sect_size;
182 mtdblk->cache_state = STATE_CLEAN;
185 /* write data to our local cache */
186 memcpy (mtdblk->cache_data + offset, buf, size);
187 mtdblk->cache_state = STATE_DIRTY;
190 buf += size;
191 pos += size;
192 len -= size;
195 return 0;
199 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
200 int len, char *buf)
202 struct mtd_info *mtd = mtdblk->mtd;
203 unsigned int sect_size = mtdblk->cache_size;
204 size_t retlen;
205 int ret;
207 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
208 mtd->name, pos, len);
210 if (!sect_size)
211 return MTD_READ (mtd, pos, len, &retlen, buf);
213 while (len > 0) {
214 unsigned long sect_start = (pos/sect_size)*sect_size;
215 unsigned int offset = pos - sect_start;
216 unsigned int size = sect_size - offset;
217 if (size > len)
218 size = len;
221 * Check if the requested data is already cached
222 * Read the requested amount of data from our internal cache if it
223 * contains what we want, otherwise we read the data directly
224 * from flash.
226 if (mtdblk->cache_state != STATE_EMPTY &&
227 mtdblk->cache_offset == sect_start) {
228 memcpy (buf, mtdblk->cache_data + offset, size);
229 } else {
230 ret = MTD_READ (mtd, pos, size, &retlen, buf);
231 if (ret)
232 return ret;
233 if (retlen != size)
234 return -EIO;
237 buf += size;
238 pos += size;
239 len -= size;
242 return 0;
245 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
246 unsigned long block, char *buf)
248 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
249 return do_cached_read(mtdblk, block<<9, 512, buf);
252 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
253 unsigned long block, char *buf)
255 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
256 if (unlikely(!mtdblk->cache_data)) {
257 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
258 if (!mtdblk->cache_data)
259 return -EINTR;
260 /* -EINTR is not really correct, but it is the best match
261 * documented in man 2 write for all cases. We could also
262 * return -EAGAIN sometimes, but why bother?
265 return do_cached_write(mtdblk, block<<9, 512, buf);
268 static int mtdblock_open(struct mtd_blktrans_dev *mbd)
270 struct mtdblk_dev *mtdblk;
271 struct mtd_info *mtd = mbd->mtd;
272 int dev = mbd->devnum;
274 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
276 if (mtdblks[dev]) {
277 mtdblks[dev]->count++;
278 return 0;
281 /* OK, it's not open. Create cache info for it */
282 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
283 if (!mtdblk)
284 return -ENOMEM;
286 memset(mtdblk, 0, sizeof(*mtdblk));
287 mtdblk->count = 1;
288 mtdblk->mtd = mtd;
290 init_MUTEX (&mtdblk->cache_sem);
291 mtdblk->cache_state = STATE_EMPTY;
292 if ((mtdblk->mtd->flags & MTD_CAP_RAM) != MTD_CAP_RAM &&
293 mtdblk->mtd->erasesize) {
294 mtdblk->cache_size = mtdblk->mtd->erasesize;
295 mtdblk->cache_data = NULL;
298 mtdblks[dev] = mtdblk;
300 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
302 return 0;
305 static int mtdblock_release(struct mtd_blktrans_dev *mbd)
307 int dev = mbd->devnum;
308 struct mtdblk_dev *mtdblk = mtdblks[dev];
310 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
312 down(&mtdblk->cache_sem);
313 write_cached_data(mtdblk);
314 up(&mtdblk->cache_sem);
316 if (!--mtdblk->count) {
317 /* It was the last usage. Free the device */
318 mtdblks[dev] = NULL;
319 if (mtdblk->mtd->sync)
320 mtdblk->mtd->sync(mtdblk->mtd);
321 vfree(mtdblk->cache_data);
322 kfree(mtdblk);
324 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
326 return 0;
329 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
331 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
333 down(&mtdblk->cache_sem);
334 write_cached_data(mtdblk);
335 up(&mtdblk->cache_sem);
337 if (mtdblk->mtd->sync)
338 mtdblk->mtd->sync(mtdblk->mtd);
339 return 0;
342 #ifdef CONFIG_MAGIC_ROM_PTR
343 #include <linux/mm.h>
344 static int
345 mtdblock_romptr(struct mtd_blktrans_dev *dev, struct vm_area_struct * vma)
347 struct mtd_info *mtd = dev->mtd;
348 u_char *ptr;
349 size_t len;
351 if (!mtd->point /* Can't do it, No function to point to correct addr */
353 || (*mtd->point)(mtd,vma->vm_pgoff,vma->vm_end-vma->vm_start,&len,&ptr) != 0)
354 return -ENOSYS;
356 vma->vm_start = (unsigned long) ptr;
357 vma->vm_end = vma->vm_start + len;
358 return 0;
360 #endif
362 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
364 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
366 if (!dev)
367 return;
369 memset(dev, 0, sizeof(*dev));
371 dev->mtd = mtd;
372 dev->devnum = mtd->index;
373 dev->blksize = 512;
374 dev->size = mtd->size >> 9;
375 dev->tr = tr;
377 if (!(mtd->flags & MTD_WRITEABLE))
378 dev->readonly = 1;
380 add_mtd_blktrans_dev(dev);
383 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
385 del_mtd_blktrans_dev(dev);
386 kfree(dev);
389 struct mtd_blktrans_ops mtdblock_tr = {
390 .name = "mtdblock",
391 .major = 31,
392 .part_bits = 0,
393 .open = mtdblock_open,
394 .flush = mtdblock_flush,
395 .release = mtdblock_release,
396 .readsect = mtdblock_readsect,
397 .writesect = mtdblock_writesect,
398 .add_mtd = mtdblock_add_mtd,
399 .remove_dev = mtdblock_remove_dev,
400 .owner = THIS_MODULE,
401 #ifdef CONFIG_MAGIC_ROM_PTR
402 .romptr = mtdblock_romptr,
403 #endif
406 int __init init_mtdblock(void)
408 return register_mtd_blktrans(&mtdblock_tr);
411 static void __exit cleanup_mtdblock(void)
413 deregister_mtd_blktrans(&mtdblock_tr);
416 module_init(init_mtdblock);
417 module_exit(cleanup_mtdblock);
420 MODULE_LICENSE("GPL");
421 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
422 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");