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