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)
11 #include <linux/config.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/kernel.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
{
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
];
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
;
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
;
58 * First, let's erase the flash block.
61 init_waitqueue_head(&wait_q
);
63 erase
.callback
= erase_callback
;
66 erase
.priv
= (u_long
)&wait_q
;
68 #if 1 // mask by Victor Yu. 05-14-2007
69 set_current_state(TASK_INTERRUPTIBLE
);
71 add_wait_queue(&wait_q
, &wait
);
73 ret
= MTD_ERASE(mtd
, &erase
);
75 #if 1 // mask by Victor Yu. 05-14-2007
76 set_current_state(TASK_RUNNING
);
78 remove_wait_queue(&wait_q
, &wait
);
79 printk (KERN_WARNING
"mtdblock: erase of region [0x%lx, 0x%x] "
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
);
101 static int write_cached_data (struct mtdblk_dev
*mtdblk
)
103 struct mtd_info
*mtd
= mtdblk
->mtd
;
106 if (mtdblk
->cache_state
!= STATE_DIRTY
)
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
);
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
;
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
;
138 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
139 mtd
->name
, pos
, len
);
142 return MTD_WRITE (mtd
, pos
, len
, &retlen
, buf
);
145 unsigned long sect_start
= (pos
/sect_size
)*sect_size
;
146 unsigned int offset
= pos
- sect_start
;
147 unsigned int size
= sect_size
- offset
;
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
);
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
);
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
);
177 if (retlen
!= sect_size
)
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
;
199 static int do_cached_read (struct mtdblk_dev
*mtdblk
, unsigned long pos
,
202 struct mtd_info
*mtd
= mtdblk
->mtd
;
203 unsigned int sect_size
= mtdblk
->cache_size
;
207 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
208 mtd
->name
, pos
, len
);
211 return MTD_READ (mtd
, pos
, len
, &retlen
, buf
);
214 unsigned long sect_start
= (pos
/sect_size
)*sect_size
;
215 unsigned int offset
= pos
- sect_start
;
216 unsigned int size
= sect_size
- offset
;
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
226 if (mtdblk
->cache_state
!= STATE_EMPTY
&&
227 mtdblk
->cache_offset
== sect_start
) {
228 memcpy (buf
, mtdblk
->cache_data
+ offset
, size
);
230 ret
= MTD_READ (mtd
, pos
, size
, &retlen
, buf
);
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
)
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");
277 mtdblks
[dev
]->count
++;
281 /* OK, it's not open. Create cache info for it */
282 mtdblk
= kmalloc(sizeof(struct mtdblk_dev
), GFP_KERNEL
);
286 memset(mtdblk
, 0, sizeof(*mtdblk
));
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");
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 */
319 if (mtdblk
->mtd
->sync
)
320 mtdblk
->mtd
->sync(mtdblk
->mtd
);
321 vfree(mtdblk
->cache_data
);
324 DEBUG(MTD_DEBUG_LEVEL1
, "ok\n");
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
);
342 #ifdef CONFIG_MAGIC_ROM_PTR
343 #include <linux/mm.h>
345 mtdblock_romptr(struct mtd_blktrans_dev
*dev
, struct vm_area_struct
* vma
)
347 struct mtd_info
*mtd
= dev
->mtd
;
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)
356 vma
->vm_start
= (unsigned long) ptr
;
357 vma
->vm_end
= vma
->vm_start
+ len
;
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
);
369 memset(dev
, 0, sizeof(*dev
));
372 dev
->devnum
= mtd
->index
;
374 dev
->size
= mtd
->size
>> 9;
377 if (!(mtd
->flags
& MTD_WRITEABLE
))
380 add_mtd_blktrans_dev(dev
);
383 static void mtdblock_remove_dev(struct mtd_blktrans_dev
*dev
)
385 del_mtd_blktrans_dev(dev
);
389 struct mtd_blktrans_ops mtdblock_tr
= {
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
,
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");