2 * Direct MTD block device access
4 * $Id: mtdblock.c,v 1.66 2004/11/25 13:52:52 joern Exp $
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
10 #include <linux/config.h>
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/sched.h> /* TASK_* */
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 set_current_state(TASK_INTERRUPTIBLE
);
69 add_wait_queue(&wait_q
, &wait
);
71 ret
= MTD_ERASE(mtd
, &erase
);
73 set_current_state(TASK_RUNNING
);
74 remove_wait_queue(&wait_q
, &wait
);
75 printk (KERN_WARNING
"mtdblock: erase of region [0x%lx, 0x%x] "
81 schedule(); /* Wait for erase to finish. */
82 remove_wait_queue(&wait_q
, &wait
);
85 * Next, writhe data to flash.
88 ret
= MTD_WRITE (mtd
, pos
, len
, &retlen
, buf
);
97 static int write_cached_data (struct mtdblk_dev
*mtdblk
)
99 struct mtd_info
*mtd
= mtdblk
->mtd
;
102 if (mtdblk
->cache_state
!= STATE_DIRTY
)
105 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: writing cached data for \"%s\" "
106 "at 0x%lx, size 0x%x\n", mtd
->name
,
107 mtdblk
->cache_offset
, mtdblk
->cache_size
);
109 ret
= erase_write (mtd
, mtdblk
->cache_offset
,
110 mtdblk
->cache_size
, mtdblk
->cache_data
);
115 * Here we could argubly set the cache state to STATE_CLEAN.
116 * However this could lead to inconsistency since we will not
117 * be notified if this content is altered on the flash by other
118 * means. Let's declare it empty and leave buffering tasks to
119 * the buffer cache instead.
121 mtdblk
->cache_state
= STATE_EMPTY
;
126 static int do_cached_write (struct mtdblk_dev
*mtdblk
, unsigned long pos
,
127 int len
, const char *buf
)
129 struct mtd_info
*mtd
= mtdblk
->mtd
;
130 unsigned int sect_size
= mtdblk
->cache_size
;
134 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
135 mtd
->name
, pos
, len
);
138 return MTD_WRITE (mtd
, pos
, len
, &retlen
, buf
);
141 unsigned long sect_start
= (pos
/sect_size
)*sect_size
;
142 unsigned int offset
= pos
- sect_start
;
143 unsigned int size
= sect_size
- offset
;
147 if (size
== sect_size
) {
149 * We are covering a whole sector. Thus there is no
150 * need to bother with the cache while it may still be
151 * useful for other partial writes.
153 ret
= erase_write (mtd
, pos
, size
, buf
);
157 /* Partial sector: need to use the cache */
159 if (mtdblk
->cache_state
== STATE_DIRTY
&&
160 mtdblk
->cache_offset
!= sect_start
) {
161 ret
= write_cached_data(mtdblk
);
166 if (mtdblk
->cache_state
== STATE_EMPTY
||
167 mtdblk
->cache_offset
!= sect_start
) {
168 /* fill the cache with the current sector */
169 mtdblk
->cache_state
= STATE_EMPTY
;
170 ret
= MTD_READ(mtd
, sect_start
, sect_size
, &retlen
, mtdblk
->cache_data
);
173 if (retlen
!= sect_size
)
176 mtdblk
->cache_offset
= sect_start
;
177 mtdblk
->cache_size
= sect_size
;
178 mtdblk
->cache_state
= STATE_CLEAN
;
181 /* write data to our local cache */
182 memcpy (mtdblk
->cache_data
+ offset
, buf
, size
);
183 mtdblk
->cache_state
= STATE_DIRTY
;
195 static int do_cached_read (struct mtdblk_dev
*mtdblk
, unsigned long pos
,
198 struct mtd_info
*mtd
= mtdblk
->mtd
;
199 unsigned int sect_size
= mtdblk
->cache_size
;
203 DEBUG(MTD_DEBUG_LEVEL2
, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
204 mtd
->name
, pos
, len
);
207 return MTD_READ (mtd
, pos
, len
, &retlen
, buf
);
210 unsigned long sect_start
= (pos
/sect_size
)*sect_size
;
211 unsigned int offset
= pos
- sect_start
;
212 unsigned int size
= sect_size
- offset
;
217 * Check if the requested data is already cached
218 * Read the requested amount of data from our internal cache if it
219 * contains what we want, otherwise we read the data directly
222 if (mtdblk
->cache_state
!= STATE_EMPTY
&&
223 mtdblk
->cache_offset
== sect_start
) {
224 memcpy (buf
, mtdblk
->cache_data
+ offset
, size
);
226 ret
= MTD_READ (mtd
, pos
, size
, &retlen
, buf
);
241 static int mtdblock_readsect(struct mtd_blktrans_dev
*dev
,
242 unsigned long block
, char *buf
)
244 struct mtdblk_dev
*mtdblk
= mtdblks
[dev
->devnum
];
245 return do_cached_read(mtdblk
, block
<<9, 512, buf
);
248 static int mtdblock_writesect(struct mtd_blktrans_dev
*dev
,
249 unsigned long block
, char *buf
)
251 struct mtdblk_dev
*mtdblk
= mtdblks
[dev
->devnum
];
252 if (unlikely(!mtdblk
->cache_data
&& mtdblk
->cache_size
)) {
253 mtdblk
->cache_data
= vmalloc(mtdblk
->mtd
->erasesize
);
254 if (!mtdblk
->cache_data
)
256 /* -EINTR is not really correct, but it is the best match
257 * documented in man 2 write for all cases. We could also
258 * return -EAGAIN sometimes, but why bother?
261 return do_cached_write(mtdblk
, block
<<9, 512, buf
);
264 static int mtdblock_open(struct mtd_blktrans_dev
*mbd
)
266 struct mtdblk_dev
*mtdblk
;
267 struct mtd_info
*mtd
= mbd
->mtd
;
268 int dev
= mbd
->devnum
;
270 DEBUG(MTD_DEBUG_LEVEL1
,"mtdblock_open\n");
273 mtdblks
[dev
]->count
++;
277 /* OK, it's not open. Create cache info for it */
278 mtdblk
= kmalloc(sizeof(struct mtdblk_dev
), GFP_KERNEL
);
282 memset(mtdblk
, 0, sizeof(*mtdblk
));
286 init_MUTEX (&mtdblk
->cache_sem
);
287 mtdblk
->cache_state
= STATE_EMPTY
;
288 if ((mtdblk
->mtd
->flags
& MTD_CAP_RAM
) != MTD_CAP_RAM
&&
289 mtdblk
->mtd
->erasesize
) {
290 mtdblk
->cache_size
= mtdblk
->mtd
->erasesize
;
291 mtdblk
->cache_data
= NULL
;
294 mtdblks
[dev
] = mtdblk
;
296 DEBUG(MTD_DEBUG_LEVEL1
, "ok\n");
301 static int mtdblock_release(struct mtd_blktrans_dev
*mbd
)
303 int dev
= mbd
->devnum
;
304 struct mtdblk_dev
*mtdblk
= mtdblks
[dev
];
306 DEBUG(MTD_DEBUG_LEVEL1
, "mtdblock_release\n");
308 down(&mtdblk
->cache_sem
);
309 write_cached_data(mtdblk
);
310 up(&mtdblk
->cache_sem
);
312 if (!--mtdblk
->count
) {
313 /* It was the last usage. Free the device */
315 if (mtdblk
->mtd
->sync
)
316 mtdblk
->mtd
->sync(mtdblk
->mtd
);
317 vfree(mtdblk
->cache_data
);
320 DEBUG(MTD_DEBUG_LEVEL1
, "ok\n");
325 static int mtdblock_flush(struct mtd_blktrans_dev
*dev
)
327 struct mtdblk_dev
*mtdblk
= mtdblks
[dev
->devnum
];
329 down(&mtdblk
->cache_sem
);
330 write_cached_data(mtdblk
);
331 up(&mtdblk
->cache_sem
);
333 if (mtdblk
->mtd
->sync
)
334 mtdblk
->mtd
->sync(mtdblk
->mtd
);
338 static void mtdblock_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
340 struct mtd_blktrans_dev
*dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
345 memset(dev
, 0, sizeof(*dev
));
348 dev
->devnum
= mtd
->index
;
350 dev
->size
= mtd
->size
>> 9;
353 if (!(mtd
->flags
& MTD_WRITEABLE
))
356 add_mtd_blktrans_dev(dev
);
359 static void mtdblock_remove_dev(struct mtd_blktrans_dev
*dev
)
361 del_mtd_blktrans_dev(dev
);
365 static struct mtd_blktrans_ops mtdblock_tr
= {
369 .open
= mtdblock_open
,
370 .flush
= mtdblock_flush
,
371 .release
= mtdblock_release
,
372 .readsect
= mtdblock_readsect
,
373 .writesect
= mtdblock_writesect
,
374 .add_mtd
= mtdblock_add_mtd
,
375 .remove_dev
= mtdblock_remove_dev
,
376 .owner
= THIS_MODULE
,
379 static int __init
init_mtdblock(void)
381 return register_mtd_blktrans(&mtdblock_tr
);
384 static void __exit
cleanup_mtdblock(void)
386 deregister_mtd_blktrans(&mtdblock_tr
);
389 module_init(init_mtdblock
);
390 module_exit(cleanup_mtdblock
);
393 MODULE_LICENSE("GPL");
394 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
395 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");