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