Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * block2mtd.c - create an mtd from a block device |
| 3 | * |
| 4 | * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk> |
Joern Engel | 2b54aae | 2008-02-06 01:38:02 -0800 | [diff] [blame] | 5 | * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Licence: GPL |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/bio.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/mtd/mtd.h> |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 18 | #include <linux/mount.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args) |
| 22 | #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args) |
| 23 | |
| 24 | |
| 25 | /* Info for the block device */ |
| 26 | struct block2mtd_dev { |
| 27 | struct list_head list; |
| 28 | struct block_device *blkdev; |
| 29 | struct mtd_info mtd; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 30 | struct mutex write_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | |
| 34 | /* Static info about the MTD, used in cleanup_module */ |
| 35 | static LIST_HEAD(blkmtd_device_list); |
| 36 | |
| 37 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 38 | static struct page *page_read(struct address_space *mapping, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 40 | return read_mapping_page(mapping, index, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* erase a specified part of the device */ |
| 44 | static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len) |
| 45 | { |
| 46 | struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; |
| 47 | struct page *page; |
| 48 | int index = to >> PAGE_SHIFT; // page index |
| 49 | int pages = len >> PAGE_SHIFT; |
| 50 | u_long *p; |
| 51 | u_long *max; |
| 52 | |
| 53 | while (pages) { |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 54 | page = page_read(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | if (IS_ERR(page)) |
| 56 | return PTR_ERR(page); |
| 57 | |
Joern Engel | 0ffb74c | 2007-02-20 20:20:58 +0100 | [diff] [blame] | 58 | max = page_address(page) + PAGE_SIZE; |
| 59 | for (p=page_address(page); p<max; p++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | if (*p != -1UL) { |
| 61 | lock_page(page); |
| 62 | memset(page_address(page), 0xff, PAGE_SIZE); |
| 63 | set_page_dirty(page); |
| 64 | unlock_page(page); |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | page_cache_release(page); |
| 69 | pages--; |
| 70 | index++; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 75 | { |
| 76 | struct block2mtd_dev *dev = mtd->priv; |
| 77 | size_t from = instr->addr; |
| 78 | size_t len = instr->len; |
| 79 | int err; |
| 80 | |
| 81 | instr->state = MTD_ERASING; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 82 | mutex_lock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | err = _block2mtd_erase(dev, from, len); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 84 | mutex_unlock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (err) { |
| 86 | ERROR("erase failed err = %d", err); |
| 87 | instr->state = MTD_ERASE_FAILED; |
| 88 | } else |
| 89 | instr->state = MTD_ERASE_DONE; |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | mtd_erase_callback(instr); |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 97 | size_t *retlen, u_char *buf) |
| 98 | { |
| 99 | struct block2mtd_dev *dev = mtd->priv; |
| 100 | struct page *page; |
| 101 | int index = from >> PAGE_SHIFT; |
Joern Engel | 711c11b | 2005-03-07 20:29:09 +0000 | [diff] [blame] | 102 | int offset = from & (PAGE_SIZE-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | int cpylen; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | while (len) { |
| 106 | if ((offset + len) > PAGE_SIZE) |
| 107 | cpylen = PAGE_SIZE - offset; // multiple pages |
| 108 | else |
| 109 | cpylen = len; // this page |
| 110 | len = len - cpylen; |
| 111 | |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 112 | page = page_read(dev->blkdev->bd_inode->i_mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (IS_ERR(page)) |
| 114 | return PTR_ERR(page); |
| 115 | |
| 116 | memcpy(buf, page_address(page) + offset, cpylen); |
| 117 | page_cache_release(page); |
| 118 | |
| 119 | if (retlen) |
| 120 | *retlen += cpylen; |
| 121 | buf += cpylen; |
| 122 | offset = 0; |
| 123 | index++; |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* write data to the underlying device */ |
| 130 | static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf, |
| 131 | loff_t to, size_t len, size_t *retlen) |
| 132 | { |
| 133 | struct page *page; |
| 134 | struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; |
| 135 | int index = to >> PAGE_SHIFT; // page index |
| 136 | int offset = to & ~PAGE_MASK; // page offset |
| 137 | int cpylen; |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | while (len) { |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 140 | if ((offset+len) > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | cpylen = PAGE_SIZE - offset; // multiple pages |
| 142 | else |
| 143 | cpylen = len; // this page |
| 144 | len = len - cpylen; |
| 145 | |
Joern Engel | 21d31f1 | 2007-02-20 20:22:22 +0100 | [diff] [blame] | 146 | page = page_read(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (IS_ERR(page)) |
| 148 | return PTR_ERR(page); |
| 149 | |
| 150 | if (memcmp(page_address(page)+offset, buf, cpylen)) { |
| 151 | lock_page(page); |
| 152 | memcpy(page_address(page) + offset, buf, cpylen); |
| 153 | set_page_dirty(page); |
| 154 | unlock_page(page); |
| 155 | } |
| 156 | page_cache_release(page); |
| 157 | |
| 158 | if (retlen) |
| 159 | *retlen += cpylen; |
| 160 | |
| 161 | buf += cpylen; |
| 162 | offset = 0; |
| 163 | index++; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 167 | |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 170 | size_t *retlen, const u_char *buf) |
| 171 | { |
| 172 | struct block2mtd_dev *dev = mtd->priv; |
| 173 | int err; |
| 174 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 175 | mutex_lock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | err = _block2mtd_write(dev, buf, to, len, retlen); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 177 | mutex_unlock(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | if (err > 0) |
| 179 | err = 0; |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* sync the device - wait until the write queue is empty */ |
| 185 | static void block2mtd_sync(struct mtd_info *mtd) |
| 186 | { |
| 187 | struct block2mtd_dev *dev = mtd->priv; |
| 188 | sync_blockdev(dev->blkdev); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | static void block2mtd_free_device(struct block2mtd_dev *dev) |
| 194 | { |
| 195 | if (!dev) |
| 196 | return; |
| 197 | |
| 198 | kfree(dev->mtd.name); |
| 199 | |
| 200 | if (dev->blkdev) { |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 201 | invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, |
| 202 | 0, -1); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 203 | blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | kfree(dev); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* FIXME: ensure that mtd->size % erase_size == 0 */ |
| 211 | static struct block2mtd_dev *add_device(char *devname, int erase_size) |
| 212 | { |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 213 | const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | struct block_device *bdev; |
| 215 | struct block2mtd_dev *dev; |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 216 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | if (!devname) |
| 219 | return NULL; |
| 220 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 221 | dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (!dev) |
| 223 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* Get a handle on the device */ |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 226 | bdev = blkdev_get_by_path(devname, mode, dev); |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 227 | #ifndef MODULE |
| 228 | if (IS_ERR(bdev)) { |
| 229 | |
| 230 | /* We might not have rootfs mounted at this point. Try |
| 231 | to resolve the device name by other means. */ |
| 232 | |
Joern Engel | 8870530 | 2007-02-20 20:21:41 +0100 | [diff] [blame] | 233 | dev_t devt = name_to_dev_t(devname); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 234 | if (devt) |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 235 | bdev = blkdev_get_by_dev(devt, mode, dev); |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 236 | } |
| 237 | #endif |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (IS_ERR(bdev)) { |
| 240 | ERROR("error: cannot open device %s", devname); |
| 241 | goto devinit_err; |
| 242 | } |
| 243 | dev->blkdev = bdev; |
| 244 | |
| 245 | if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { |
| 246 | ERROR("attempting to use an MTD device as a block device"); |
| 247 | goto devinit_err; |
| 248 | } |
| 249 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 250 | mutex_init(&dev->write_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | /* Setup the MTD structure */ |
| 253 | /* make the name contain the block device in */ |
Julia Lawall | 4d68242 | 2010-03-10 22:15:19 +0100 | [diff] [blame] | 254 | name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname); |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 255 | if (!name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | goto devinit_err; |
| 257 | |
Greg Kroah-Hartman | eadcf0d | 2008-07-02 12:46:22 -0700 | [diff] [blame] | 258 | dev->mtd.name = name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; |
| 261 | dev->mtd.erasesize = erase_size; |
Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 262 | dev->mtd.writesize = 1; |
Artem Bityutskiy | b604387 | 2012-02-03 09:32:44 +0200 | [diff] [blame] | 263 | dev->mtd.writebufsize = PAGE_SIZE; |
David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 264 | dev->mtd.type = MTD_RAM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | dev->mtd.flags = MTD_CAP_RAM; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 266 | dev->mtd._erase = block2mtd_erase; |
| 267 | dev->mtd._write = block2mtd_write; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 268 | dev->mtd._sync = block2mtd_sync; |
| 269 | dev->mtd._read = block2mtd_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | dev->mtd.priv = dev; |
| 271 | dev->mtd.owner = THIS_MODULE; |
| 272 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 273 | if (mtd_device_register(&dev->mtd, NULL, 0)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 274 | /* Device didn't get added, so free the entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | goto devinit_err; |
| 276 | } |
| 277 | list_add(&dev->list, &blkmtd_device_list); |
| 278 | INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index, |
Stephane Chazelas | 0bc88c5 | 2008-04-18 13:44:15 -0700 | [diff] [blame] | 279 | dev->mtd.name + strlen("block2mtd: "), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | dev->mtd.erasesize >> 10, dev->mtd.erasesize); |
| 281 | return dev; |
| 282 | |
| 283 | devinit_err: |
| 284 | block2mtd_free_device(dev); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 289 | /* This function works similar to reguler strtoul. In addition, it |
| 290 | * allows some suffixes for a more human-readable number format: |
| 291 | * ki, Ki, kiB, KiB - multiply result with 1024 |
| 292 | * Mi, MiB - multiply result with 1024^2 |
| 293 | * Gi, GiB - multiply result with 1024^3 |
| 294 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static int ustrtoul(const char *cp, char **endp, unsigned int base) |
| 296 | { |
| 297 | unsigned long result = simple_strtoul(cp, endp, base); |
| 298 | switch (**endp) { |
| 299 | case 'G' : |
| 300 | result *= 1024; |
| 301 | case 'M': |
| 302 | result *= 1024; |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 303 | case 'K': |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | case 'k': |
| 305 | result *= 1024; |
| 306 | /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ |
Joern Engel | 954c242 | 2006-04-18 21:03:08 -0700 | [diff] [blame] | 307 | if ((*endp)[1] == 'i') { |
| 308 | if ((*endp)[2] == 'B') |
| 309 | (*endp) += 3; |
| 310 | else |
| 311 | (*endp) += 2; |
| 312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 318 | static int parse_num(size_t *num, const char *token) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
| 320 | char *endp; |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 321 | size_t n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 323 | n = (size_t) ustrtoul(token, &endp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | if (*endp) |
| 325 | return -EINVAL; |
| 326 | |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 327 | *num = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | static inline void kill_final_newline(char *str) |
| 333 | { |
| 334 | char *newline = strrchr(str, '\n'); |
| 335 | if (newline && !newline[1]) |
| 336 | *newline = 0; |
| 337 | } |
| 338 | |
| 339 | |
Stephane Chazelas | 0bc88c5 | 2008-04-18 13:44:15 -0700 | [diff] [blame] | 340 | #define parse_err(fmt, args...) do { \ |
| 341 | ERROR(fmt, ## args); \ |
| 342 | return 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } while (0) |
| 344 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 345 | #ifndef MODULE |
| 346 | static int block2mtd_init_called = 0; |
Adrian Bunk | 4839f04 | 2007-05-02 12:33:17 +0100 | [diff] [blame] | 347 | static char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */ |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 348 | #endif |
| 349 | |
| 350 | |
| 351 | static int block2mtd_setup2(const char *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 353 | char buf[80 + 12]; /* 80 for device, 12 for erase size */ |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 354 | char *str = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | char *token[2]; |
| 356 | char *name; |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 357 | size_t erase_size = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | int i, ret; |
| 359 | |
| 360 | if (strnlen(val, sizeof(buf)) >= sizeof(buf)) |
| 361 | parse_err("parameter too long"); |
| 362 | |
| 363 | strcpy(str, val); |
| 364 | kill_final_newline(str); |
| 365 | |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 366 | for (i = 0; i < 2; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | token[i] = strsep(&str, ","); |
| 368 | |
| 369 | if (str) |
| 370 | parse_err("too many arguments"); |
| 371 | |
| 372 | if (!token[0]) |
| 373 | parse_err("no argument"); |
| 374 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 375 | name = token[0]; |
| 376 | if (strlen(name) + 1 > 80) |
| 377 | parse_err("device name too long"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | if (token[1]) { |
Thomas Gleixner | cc71229 | 2005-03-19 22:40:47 +0000 | [diff] [blame] | 380 | ret = parse_num(&erase_size, token[1]); |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 381 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | parse_err("illegal erase size"); |
Jesper Juhl | a6550e5 | 2006-05-14 01:42:25 +0200 | [diff] [blame] | 383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | add_device(name, erase_size); |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 392 | static int block2mtd_setup(const char *val, struct kernel_param *kp) |
| 393 | { |
| 394 | #ifdef MODULE |
| 395 | return block2mtd_setup2(val); |
| 396 | #else |
| 397 | /* If more parameters are later passed in via |
| 398 | /sys/module/block2mtd/parameters/block2mtd |
| 399 | and block2mtd_init() has already been called, |
| 400 | we can parse the argument now. */ |
| 401 | |
| 402 | if (block2mtd_init_called) |
| 403 | return block2mtd_setup2(val); |
| 404 | |
| 405 | /* During early boot stage, we only save the parameters |
| 406 | here. We must parse them later: if the param passed |
| 407 | from kernel boot command line, block2mtd_setup() is |
| 408 | called so early that it is not possible to resolve |
| 409 | the device (even kmalloc() fails). Deter that work to |
| 410 | block2mtd_setup2(). */ |
| 411 | |
| 412 | strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline)); |
| 413 | |
| 414 | return 0; |
| 415 | #endif |
| 416 | } |
| 417 | |
| 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200); |
| 420 | MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\""); |
| 421 | |
| 422 | static int __init block2mtd_init(void) |
| 423 | { |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 424 | int ret = 0; |
Ville Herva | c4e7fb3 | 2006-07-14 00:31:16 +0300 | [diff] [blame] | 425 | |
| 426 | #ifndef MODULE |
| 427 | if (strlen(block2mtd_paramline)) |
| 428 | ret = block2mtd_setup2(block2mtd_paramline); |
| 429 | block2mtd_init_called = 1; |
| 430 | #endif |
| 431 | |
| 432 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame^] | 436 | static void block2mtd_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
| 438 | struct list_head *pos, *next; |
| 439 | |
| 440 | /* Remove the MTD devices */ |
| 441 | list_for_each_safe(pos, next, &blkmtd_device_list) { |
| 442 | struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); |
| 443 | block2mtd_sync(&dev->mtd); |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 444 | mtd_device_unregister(&dev->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | INFO("mtd%d: [%s] removed", dev->mtd.index, |
Stephane Chazelas | 0bc88c5 | 2008-04-18 13:44:15 -0700 | [diff] [blame] | 446 | dev->mtd.name + strlen("block2mtd: ")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | list_del(&dev->list); |
| 448 | block2mtd_free_device(dev); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | module_init(block2mtd_init); |
| 454 | module_exit(block2mtd_exit); |
| 455 | |
| 456 | MODULE_LICENSE("GPL"); |
Joern Engel | 2b54aae | 2008-02-06 01:38:02 -0800 | [diff] [blame] | 457 | MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | MODULE_DESCRIPTION("Emulate an MTD using a block device"); |