[MTD] core: Clean up trailing white spaces
[linux-2.6-block.git] / drivers / mtd / mtdblock.c
CommitLineData
97894cda 1/*
1da177e4
LT
2 * Direct MTD block device access
3 *
97894cda 4 * $Id: mtdblock.c,v 1.68 2005/11/07 11:14:20 gleixner Exp $
1da177e4
LT
5 *
6 * (C) 2000-2003 Nicolas Pitre <nico@cam.org>
7 * (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
8 */
9
10#include <linux/config.h>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/init.h>
15fdc52f
TG
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
1da177e4 16#include <linux/slab.h>
15fdc52f 17#include <linux/types.h>
1da177e4 18#include <linux/vmalloc.h>
15fdc52f 19
1da177e4
LT
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/blktrans.h>
22
23static struct mtdblk_dev {
24 struct mtd_info *mtd;
25 int count;
26 struct semaphore cache_sem;
27 unsigned char *cache_data;
28 unsigned long cache_offset;
29 unsigned int cache_size;
30 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
31} *mtdblks[MAX_MTD_DEVICES];
32
33/*
34 * Cache stuff...
97894cda 35 *
1da177e4
LT
36 * Since typical flash erasable sectors are much larger than what Linux's
37 * buffer cache can handle, we must implement read-modify-write on flash
38 * sectors for each block write requests. To avoid over-erasing flash sectors
39 * and to speed things up, we locally cache a whole flash sector while it is
40 * being written to until a different sector is required.
41 */
42
43static void erase_callback(struct erase_info *done)
44{
45 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
46 wake_up(wait_q);
47}
48
97894cda 49static int erase_write (struct mtd_info *mtd, unsigned long pos,
1da177e4
LT
50 int len, const char *buf)
51{
52 struct erase_info erase;
53 DECLARE_WAITQUEUE(wait, current);
54 wait_queue_head_t wait_q;
55 size_t retlen;
56 int ret;
57
58 /*
59 * First, let's erase the flash block.
60 */
61
62 init_waitqueue_head(&wait_q);
63 erase.mtd = mtd;
64 erase.callback = erase_callback;
65 erase.addr = pos;
66 erase.len = len;
67 erase.priv = (u_long)&wait_q;
68
69 set_current_state(TASK_INTERRUPTIBLE);
70 add_wait_queue(&wait_q, &wait);
71
72 ret = MTD_ERASE(mtd, &erase);
73 if (ret) {
74 set_current_state(TASK_RUNNING);
75 remove_wait_queue(&wait_q, &wait);
76 printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
77 "on \"%s\" failed\n",
78 pos, len, mtd->name);
79 return ret;
80 }
81
82 schedule(); /* Wait for erase to finish. */
83 remove_wait_queue(&wait_q, &wait);
84
85 /*
86 * Next, writhe data to flash.
87 */
88
89 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
90 if (ret)
91 return ret;
92 if (retlen != len)
93 return -EIO;
94 return 0;
95}
96
97
98static int write_cached_data (struct mtdblk_dev *mtdblk)
99{
100 struct mtd_info *mtd = mtdblk->mtd;
101 int ret;
102
103 if (mtdblk->cache_state != STATE_DIRTY)
104 return 0;
105
106 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: writing cached data for \"%s\" "
97894cda 107 "at 0x%lx, size 0x%x\n", mtd->name,
1da177e4 108 mtdblk->cache_offset, mtdblk->cache_size);
97894cda
TG
109
110 ret = erase_write (mtd, mtdblk->cache_offset,
1da177e4
LT
111 mtdblk->cache_size, mtdblk->cache_data);
112 if (ret)
113 return ret;
114
115 /*
116 * Here we could argubly set the cache state to STATE_CLEAN.
97894cda
TG
117 * However this could lead to inconsistency since we will not
118 * be notified if this content is altered on the flash by other
1da177e4
LT
119 * means. Let's declare it empty and leave buffering tasks to
120 * the buffer cache instead.
121 */
122 mtdblk->cache_state = STATE_EMPTY;
123 return 0;
124}
125
126
97894cda 127static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
1da177e4
LT
128 int len, const char *buf)
129{
130 struct mtd_info *mtd = mtdblk->mtd;
131 unsigned int sect_size = mtdblk->cache_size;
132 size_t retlen;
133 int ret;
134
135 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
136 mtd->name, pos, len);
97894cda 137
1da177e4
LT
138 if (!sect_size)
139 return MTD_WRITE (mtd, pos, len, &retlen, buf);
140
141 while (len > 0) {
142 unsigned long sect_start = (pos/sect_size)*sect_size;
143 unsigned int offset = pos - sect_start;
144 unsigned int size = sect_size - offset;
97894cda 145 if( size > len )
1da177e4
LT
146 size = len;
147
148 if (size == sect_size) {
97894cda 149 /*
1da177e4
LT
150 * We are covering a whole sector. Thus there is no
151 * need to bother with the cache while it may still be
152 * useful for other partial writes.
153 */
154 ret = erase_write (mtd, pos, size, buf);
155 if (ret)
156 return ret;
157 } else {
158 /* Partial sector: need to use the cache */
159
160 if (mtdblk->cache_state == STATE_DIRTY &&
161 mtdblk->cache_offset != sect_start) {
162 ret = write_cached_data(mtdblk);
97894cda 163 if (ret)
1da177e4
LT
164 return ret;
165 }
166
167 if (mtdblk->cache_state == STATE_EMPTY ||
168 mtdblk->cache_offset != sect_start) {
169 /* fill the cache with the current sector */
170 mtdblk->cache_state = STATE_EMPTY;
171 ret = MTD_READ(mtd, sect_start, sect_size, &retlen, mtdblk->cache_data);
172 if (ret)
173 return ret;
174 if (retlen != sect_size)
175 return -EIO;
176
177 mtdblk->cache_offset = sect_start;
178 mtdblk->cache_size = sect_size;
179 mtdblk->cache_state = STATE_CLEAN;
180 }
181
182 /* write data to our local cache */
183 memcpy (mtdblk->cache_data + offset, buf, size);
184 mtdblk->cache_state = STATE_DIRTY;
185 }
186
187 buf += size;
188 pos += size;
189 len -= size;
190 }
191
192 return 0;
193}
194
195
97894cda 196static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
1da177e4
LT
197 int len, char *buf)
198{
199 struct mtd_info *mtd = mtdblk->mtd;
200 unsigned int sect_size = mtdblk->cache_size;
201 size_t retlen;
202 int ret;
203
97894cda 204 DEBUG(MTD_DEBUG_LEVEL2, "mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
1da177e4 205 mtd->name, pos, len);
97894cda 206
1da177e4
LT
207 if (!sect_size)
208 return MTD_READ (mtd, pos, len, &retlen, buf);
209
210 while (len > 0) {
211 unsigned long sect_start = (pos/sect_size)*sect_size;
212 unsigned int offset = pos - sect_start;
213 unsigned int size = sect_size - offset;
97894cda 214 if (size > len)
1da177e4
LT
215 size = len;
216
217 /*
218 * Check if the requested data is already cached
219 * Read the requested amount of data from our internal cache if it
220 * contains what we want, otherwise we read the data directly
221 * from flash.
222 */
223 if (mtdblk->cache_state != STATE_EMPTY &&
224 mtdblk->cache_offset == sect_start) {
225 memcpy (buf, mtdblk->cache_data + offset, size);
226 } else {
227 ret = MTD_READ (mtd, pos, size, &retlen, buf);
228 if (ret)
229 return ret;
230 if (retlen != size)
231 return -EIO;
232 }
233
234 buf += size;
235 pos += size;
236 len -= size;
237 }
238
239 return 0;
240}
241
242static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
243 unsigned long block, char *buf)
244{
245 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
246 return do_cached_read(mtdblk, block<<9, 512, buf);
247}
248
249static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
250 unsigned long block, char *buf)
251{
252 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
253 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
254 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize);
255 if (!mtdblk->cache_data)
256 return -EINTR;
257 /* -EINTR is not really correct, but it is the best match
258 * documented in man 2 write for all cases. We could also
259 * return -EAGAIN sometimes, but why bother?
260 */
261 }
262 return do_cached_write(mtdblk, block<<9, 512, buf);
263}
264
265static int mtdblock_open(struct mtd_blktrans_dev *mbd)
266{
267 struct mtdblk_dev *mtdblk;
268 struct mtd_info *mtd = mbd->mtd;
269 int dev = mbd->devnum;
270
271 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
97894cda 272
1da177e4
LT
273 if (mtdblks[dev]) {
274 mtdblks[dev]->count++;
275 return 0;
276 }
97894cda 277
1da177e4
LT
278 /* OK, it's not open. Create cache info for it */
279 mtdblk = kmalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
280 if (!mtdblk)
281 return -ENOMEM;
282
283 memset(mtdblk, 0, sizeof(*mtdblk));
284 mtdblk->count = 1;
285 mtdblk->mtd = mtd;
286
287 init_MUTEX (&mtdblk->cache_sem);
288 mtdblk->cache_state = STATE_EMPTY;
289 if ((mtdblk->mtd->flags & MTD_CAP_RAM) != MTD_CAP_RAM &&
290 mtdblk->mtd->erasesize) {
291 mtdblk->cache_size = mtdblk->mtd->erasesize;
292 mtdblk->cache_data = NULL;
293 }
294
295 mtdblks[dev] = mtdblk;
97894cda 296
1da177e4
LT
297 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
298
299 return 0;
300}
301
302static int mtdblock_release(struct mtd_blktrans_dev *mbd)
303{
304 int dev = mbd->devnum;
305 struct mtdblk_dev *mtdblk = mtdblks[dev];
306
307 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
308
309 down(&mtdblk->cache_sem);
310 write_cached_data(mtdblk);
311 up(&mtdblk->cache_sem);
312
313 if (!--mtdblk->count) {
314 /* It was the last usage. Free the device */
315 mtdblks[dev] = NULL;
316 if (mtdblk->mtd->sync)
317 mtdblk->mtd->sync(mtdblk->mtd);
318 vfree(mtdblk->cache_data);
319 kfree(mtdblk);
320 }
321 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
322
323 return 0;
97894cda 324}
1da177e4
LT
325
326static int mtdblock_flush(struct mtd_blktrans_dev *dev)
327{
328 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum];
329
330 down(&mtdblk->cache_sem);
331 write_cached_data(mtdblk);
332 up(&mtdblk->cache_sem);
333
334 if (mtdblk->mtd->sync)
335 mtdblk->mtd->sync(mtdblk->mtd);
336 return 0;
337}
338
339static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
340{
341 struct mtd_blktrans_dev *dev = kmalloc(sizeof(*dev), GFP_KERNEL);
342
343 if (!dev)
344 return;
345
346 memset(dev, 0, sizeof(*dev));
347
348 dev->mtd = mtd;
349 dev->devnum = mtd->index;
350 dev->blksize = 512;
351 dev->size = mtd->size >> 9;
352 dev->tr = tr;
353
354 if (!(mtd->flags & MTD_WRITEABLE))
355 dev->readonly = 1;
356
357 add_mtd_blktrans_dev(dev);
358}
359
360static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
361{
362 del_mtd_blktrans_dev(dev);
363 kfree(dev);
364}
365
366static struct mtd_blktrans_ops mtdblock_tr = {
367 .name = "mtdblock",
368 .major = 31,
369 .part_bits = 0,
370 .open = mtdblock_open,
371 .flush = mtdblock_flush,
372 .release = mtdblock_release,
373 .readsect = mtdblock_readsect,
374 .writesect = mtdblock_writesect,
375 .add_mtd = mtdblock_add_mtd,
376 .remove_dev = mtdblock_remove_dev,
377 .owner = THIS_MODULE,
378};
379
380static int __init init_mtdblock(void)
381{
382 return register_mtd_blktrans(&mtdblock_tr);
383}
384
385static void __exit cleanup_mtdblock(void)
386{
387 deregister_mtd_blktrans(&mtdblock_tr);
388}
389
390module_init(init_mtdblock);
391module_exit(cleanup_mtdblock);
392
393
394MODULE_LICENSE("GPL");
395MODULE_AUTHOR("Nicolas Pitre <nico@cam.org> et al.");
396MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");