11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * block2mtd.c - create an mtd from a block device 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk> 52b54aaefSJoern Engel * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Licence: GPL 81da177e4SLinus Torvalds */ 9a1c06609SJoe Perches 10a1c06609SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11a1c06609SJoe Perches 12d6a3f017SFelix Fietkau /* 13d6a3f017SFelix Fietkau * When the first attempt at device initialization fails, we may need to 14d6a3f017SFelix Fietkau * wait a little bit and retry. This timeout, by default 3 seconds, gives 15d6a3f017SFelix Fietkau * device time to start up. Required on BCM2708 and a few other chipsets. 16d6a3f017SFelix Fietkau */ 17d6a3f017SFelix Fietkau #define MTD_DEFAULT_TIMEOUT 3 18d6a3f017SFelix Fietkau 191da177e4SLinus Torvalds #include <linux/module.h> 20d6a3f017SFelix Fietkau #include <linux/delay.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/blkdev.h> 2366114cadSTejun Heo #include <linux/backing-dev.h> 241da177e4SLinus Torvalds #include <linux/bio.h> 251da177e4SLinus Torvalds #include <linux/pagemap.h> 261da177e4SLinus Torvalds #include <linux/list.h> 271da177e4SLinus Torvalds #include <linux/init.h> 281da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 2948b19268SIngo Molnar #include <linux/mutex.h> 30c4e7fb31SVille Herva #include <linux/mount.h> 315a0e3ad6STejun Heo #include <linux/slab.h> 32f83c3838SEzequiel Garcia #include <linux/major.h> 331da177e4SLinus Torvalds 34*a04e9653SJoachim Wiberg /* Maximum number of comma-separated items in the 'block2mtd=' parameter */ 35*a04e9653SJoachim Wiberg #define BLOCK2MTD_PARAM_MAX_COUNT 2 36*a04e9653SJoachim Wiberg 371da177e4SLinus Torvalds /* Info for the block device */ 381da177e4SLinus Torvalds struct block2mtd_dev { 391da177e4SLinus Torvalds struct list_head list; 401da177e4SLinus Torvalds struct block_device *blkdev; 411da177e4SLinus Torvalds struct mtd_info mtd; 4248b19268SIngo Molnar struct mutex write_mutex; 431da177e4SLinus Torvalds }; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* Static info about the MTD, used in cleanup_module */ 471da177e4SLinus Torvalds static LIST_HEAD(blkmtd_device_list); 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds 50c3917a04SLiu Song static struct page *page_read(struct address_space *mapping, pgoff_t index) 511da177e4SLinus Torvalds { 526fe6900eSNick Piggin return read_mapping_page(mapping, index, NULL); 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* erase a specified part of the device */ 561da177e4SLinus Torvalds static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len) 571da177e4SLinus Torvalds { 581da177e4SLinus Torvalds struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; 591da177e4SLinus Torvalds struct page *page; 60c3917a04SLiu Song pgoff_t index = to >> PAGE_SHIFT; // page index 611da177e4SLinus Torvalds int pages = len >> PAGE_SHIFT; 621da177e4SLinus Torvalds u_long *p; 631da177e4SLinus Torvalds u_long *max; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds while (pages) { 6621d31f1fSJoern Engel page = page_read(mapping, index); 671da177e4SLinus Torvalds if (IS_ERR(page)) 681da177e4SLinus Torvalds return PTR_ERR(page); 691da177e4SLinus Torvalds 700ffb74ccSJoern Engel max = page_address(page) + PAGE_SIZE; 710ffb74ccSJoern Engel for (p=page_address(page); p<max; p++) 721da177e4SLinus Torvalds if (*p != -1UL) { 731da177e4SLinus Torvalds lock_page(page); 741da177e4SLinus Torvalds memset(page_address(page), 0xff, PAGE_SIZE); 751da177e4SLinus Torvalds set_page_dirty(page); 761da177e4SLinus Torvalds unlock_page(page); 77031da73fSNeilBrown balance_dirty_pages_ratelimited(mapping); 781da177e4SLinus Torvalds break; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 8109cbfeafSKirill A. Shutemov put_page(page); 821da177e4SLinus Torvalds pages--; 831da177e4SLinus Torvalds index++; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds return 0; 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 901da177e4SLinus Torvalds size_t from = instr->addr; 911da177e4SLinus Torvalds size_t len = instr->len; 921da177e4SLinus Torvalds int err; 931da177e4SLinus Torvalds 9448b19268SIngo Molnar mutex_lock(&dev->write_mutex); 951da177e4SLinus Torvalds err = _block2mtd_erase(dev, from, len); 9648b19268SIngo Molnar mutex_unlock(&dev->write_mutex); 97e7bfb3fdSBoris Brezillon if (err) 98a1c06609SJoe Perches pr_err("erase failed err = %d\n", err); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds return err; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len, 1051da177e4SLinus Torvalds size_t *retlen, u_char *buf) 1061da177e4SLinus Torvalds { 1071da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 1081da177e4SLinus Torvalds struct page *page; 109c3917a04SLiu Song pgoff_t index = from >> PAGE_SHIFT; 110711c11b7SJoern Engel int offset = from & (PAGE_SIZE-1); 1111da177e4SLinus Torvalds int cpylen; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds while (len) { 1141da177e4SLinus Torvalds if ((offset + len) > PAGE_SIZE) 1151da177e4SLinus Torvalds cpylen = PAGE_SIZE - offset; // multiple pages 1161da177e4SLinus Torvalds else 1171da177e4SLinus Torvalds cpylen = len; // this page 1181da177e4SLinus Torvalds len = len - cpylen; 1191da177e4SLinus Torvalds 12021d31f1fSJoern Engel page = page_read(dev->blkdev->bd_inode->i_mapping, index); 1211da177e4SLinus Torvalds if (IS_ERR(page)) 1221da177e4SLinus Torvalds return PTR_ERR(page); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds memcpy(buf, page_address(page) + offset, cpylen); 12509cbfeafSKirill A. Shutemov put_page(page); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds if (retlen) 1281da177e4SLinus Torvalds *retlen += cpylen; 1291da177e4SLinus Torvalds buf += cpylen; 1301da177e4SLinus Torvalds offset = 0; 1311da177e4SLinus Torvalds index++; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* write data to the underlying device */ 1381da177e4SLinus Torvalds static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf, 1391da177e4SLinus Torvalds loff_t to, size_t len, size_t *retlen) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds struct page *page; 1421da177e4SLinus Torvalds struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; 143c3917a04SLiu Song pgoff_t index = to >> PAGE_SHIFT; // page index 1441da177e4SLinus Torvalds int offset = to & ~PAGE_MASK; // page offset 1451da177e4SLinus Torvalds int cpylen; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds while (len) { 1481da177e4SLinus Torvalds if ((offset+len) > PAGE_SIZE) 1491da177e4SLinus Torvalds cpylen = PAGE_SIZE - offset; // multiple pages 1501da177e4SLinus Torvalds else 1511da177e4SLinus Torvalds cpylen = len; // this page 1521da177e4SLinus Torvalds len = len - cpylen; 1531da177e4SLinus Torvalds 15421d31f1fSJoern Engel page = page_read(mapping, index); 1551da177e4SLinus Torvalds if (IS_ERR(page)) 1561da177e4SLinus Torvalds return PTR_ERR(page); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds if (memcmp(page_address(page)+offset, buf, cpylen)) { 1591da177e4SLinus Torvalds lock_page(page); 1601da177e4SLinus Torvalds memcpy(page_address(page) + offset, buf, cpylen); 1611da177e4SLinus Torvalds set_page_dirty(page); 1621da177e4SLinus Torvalds unlock_page(page); 163031da73fSNeilBrown balance_dirty_pages_ratelimited(mapping); 1641da177e4SLinus Torvalds } 16509cbfeafSKirill A. Shutemov put_page(page); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds if (retlen) 1681da177e4SLinus Torvalds *retlen += cpylen; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds buf += cpylen; 1711da177e4SLinus Torvalds offset = 0; 1721da177e4SLinus Torvalds index++; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds return 0; 1751da177e4SLinus Torvalds } 176c4e7fb31SVille Herva 177c4e7fb31SVille Herva 1781da177e4SLinus Torvalds static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len, 1791da177e4SLinus Torvalds size_t *retlen, const u_char *buf) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 1821da177e4SLinus Torvalds int err; 1831da177e4SLinus Torvalds 18448b19268SIngo Molnar mutex_lock(&dev->write_mutex); 1851da177e4SLinus Torvalds err = _block2mtd_write(dev, buf, to, len, retlen); 18648b19268SIngo Molnar mutex_unlock(&dev->write_mutex); 1871da177e4SLinus Torvalds if (err > 0) 1881da177e4SLinus Torvalds err = 0; 1891da177e4SLinus Torvalds return err; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* sync the device - wait until the write queue is empty */ 1941da177e4SLinus Torvalds static void block2mtd_sync(struct mtd_info *mtd) 1951da177e4SLinus Torvalds { 1961da177e4SLinus Torvalds struct block2mtd_dev *dev = mtd->priv; 1971da177e4SLinus Torvalds sync_blockdev(dev->blkdev); 1981da177e4SLinus Torvalds return; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static void block2mtd_free_device(struct block2mtd_dev *dev) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds if (!dev) 2051da177e4SLinus Torvalds return; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds kfree(dev->mtd.name); 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds if (dev->blkdev) { 210fc0ecff6SAndrew Morton invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 211fc0ecff6SAndrew Morton 0, -1); 212e525fd89STejun Heo blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds kfree(dev); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds 219d6a3f017SFelix Fietkau static struct block2mtd_dev *add_device(char *devname, int erase_size, 220d6a3f017SFelix Fietkau int timeout) 2211da177e4SLinus Torvalds { 222d6a3f017SFelix Fietkau #ifndef MODULE 223d6a3f017SFelix Fietkau int i; 224d6a3f017SFelix Fietkau #endif 225e525fd89STejun Heo const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL; 226d40a18feSColin Ian King struct block_device *bdev; 2271da177e4SLinus Torvalds struct block2mtd_dev *dev; 228eadcf0d7SGreg Kroah-Hartman char *name; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds if (!devname) 2311da177e4SLinus Torvalds return NULL; 2321da177e4SLinus Torvalds 23395b93a0cSBurman Yan dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL); 2341da177e4SLinus Torvalds if (!dev) 2351da177e4SLinus Torvalds return NULL; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds /* Get a handle on the device */ 238d4d77629STejun Heo bdev = blkdev_get_by_path(devname, mode, dev); 239d6a3f017SFelix Fietkau 240c4e7fb31SVille Herva #ifndef MODULE 241d6a3f017SFelix Fietkau /* 242d6a3f017SFelix Fietkau * We might not have the root device mounted at this point. 243d6a3f017SFelix Fietkau * Try to resolve the device name by other means. 244d6a3f017SFelix Fietkau */ 245d6a3f017SFelix Fietkau for (i = 0; IS_ERR(bdev) && i <= timeout; i++) { 246d6a3f017SFelix Fietkau dev_t devt; 247c4e7fb31SVille Herva 248d6a3f017SFelix Fietkau if (i) 249d6a3f017SFelix Fietkau /* 250d6a3f017SFelix Fietkau * Calling wait_for_device_probe in the first loop 251d6a3f017SFelix Fietkau * was not enough, sleep for a bit in subsequent 252d6a3f017SFelix Fietkau * go-arounds. 253d6a3f017SFelix Fietkau */ 254d6a3f017SFelix Fietkau msleep(1000); 255d6a3f017SFelix Fietkau wait_for_device_probe(); 256c4e7fb31SVille Herva 257d6a3f017SFelix Fietkau devt = name_to_dev_t(devname); 258d6a3f017SFelix Fietkau if (!devt) 259d6a3f017SFelix Fietkau continue; 260d4d77629STejun Heo bdev = blkdev_get_by_dev(devt, mode, dev); 261c4e7fb31SVille Herva } 262c4e7fb31SVille Herva #endif 263c4e7fb31SVille Herva 2641da177e4SLinus Torvalds if (IS_ERR(bdev)) { 265a1c06609SJoe Perches pr_err("error: cannot open device %s\n", devname); 2664bed207cSFabian Frederick goto err_free_block2mtd; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds dev->blkdev = bdev; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { 271a1c06609SJoe Perches pr_err("attempting to use an MTD device as a block device\n"); 2724bed207cSFabian Frederick goto err_free_block2mtd; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 275ea6d833aSFabian Frederick if ((long)dev->blkdev->bd_inode->i_size % erase_size) { 276ea6d833aSFabian Frederick pr_err("erasesize must be a divisor of device size\n"); 277ea6d833aSFabian Frederick goto err_free_block2mtd; 278ea6d833aSFabian Frederick } 279ea6d833aSFabian Frederick 28048b19268SIngo Molnar mutex_init(&dev->write_mutex); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* Setup the MTD structure */ 2831da177e4SLinus Torvalds /* make the name contain the block device in */ 2844d682420SJulia Lawall name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname); 285eadcf0d7SGreg Kroah-Hartman if (!name) 2864bed207cSFabian Frederick goto err_destroy_mutex; 2871da177e4SLinus Torvalds 288eadcf0d7SGreg Kroah-Hartman dev->mtd.name = name; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; 2911da177e4SLinus Torvalds dev->mtd.erasesize = erase_size; 29217ffc7baSArtem B. Bityutskiy dev->mtd.writesize = 1; 293b6043874SArtem Bityutskiy dev->mtd.writebufsize = PAGE_SIZE; 29421c8db9eSDavid Woodhouse dev->mtd.type = MTD_RAM; 2951da177e4SLinus Torvalds dev->mtd.flags = MTD_CAP_RAM; 2963c3c10bbSArtem Bityutskiy dev->mtd._erase = block2mtd_erase; 2973c3c10bbSArtem Bityutskiy dev->mtd._write = block2mtd_write; 2983c3c10bbSArtem Bityutskiy dev->mtd._sync = block2mtd_sync; 2993c3c10bbSArtem Bityutskiy dev->mtd._read = block2mtd_read; 3001da177e4SLinus Torvalds dev->mtd.priv = dev; 3011da177e4SLinus Torvalds dev->mtd.owner = THIS_MODULE; 3021da177e4SLinus Torvalds 303ee0e87b1SJamie Iles if (mtd_device_register(&dev->mtd, NULL, 0)) { 30425985edcSLucas De Marchi /* Device didn't get added, so free the entry */ 3054bed207cSFabian Frederick goto err_destroy_mutex; 3061da177e4SLinus Torvalds } 307d6a3f017SFelix Fietkau 3081da177e4SLinus Torvalds list_add(&dev->list, &blkmtd_device_list); 309a1c06609SJoe Perches pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n", 310a1c06609SJoe Perches dev->mtd.index, 3110bc88c59SStephane Chazelas dev->mtd.name + strlen("block2mtd: "), 3121da177e4SLinus Torvalds dev->mtd.erasesize >> 10, dev->mtd.erasesize); 3131da177e4SLinus Torvalds return dev; 3141da177e4SLinus Torvalds 3154bed207cSFabian Frederick err_destroy_mutex: 3164bed207cSFabian Frederick mutex_destroy(&dev->write_mutex); 3174bed207cSFabian Frederick err_free_block2mtd: 3181da177e4SLinus Torvalds block2mtd_free_device(dev); 3191da177e4SLinus Torvalds return NULL; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds 323954c2422SJoern Engel /* This function works similar to reguler strtoul. In addition, it 324954c2422SJoern Engel * allows some suffixes for a more human-readable number format: 325954c2422SJoern Engel * ki, Ki, kiB, KiB - multiply result with 1024 326954c2422SJoern Engel * Mi, MiB - multiply result with 1024^2 327954c2422SJoern Engel * Gi, GiB - multiply result with 1024^3 328954c2422SJoern Engel */ 3291da177e4SLinus Torvalds static int ustrtoul(const char *cp, char **endp, unsigned int base) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds unsigned long result = simple_strtoul(cp, endp, base); 3321da177e4SLinus Torvalds switch (**endp) { 3331da177e4SLinus Torvalds case 'G' : 3341da177e4SLinus Torvalds result *= 1024; 335025a06c1SMiquel Raynal fallthrough; 3361da177e4SLinus Torvalds case 'M': 3371da177e4SLinus Torvalds result *= 1024; 338025a06c1SMiquel Raynal fallthrough; 339954c2422SJoern Engel case 'K': 3401da177e4SLinus Torvalds case 'k': 3411da177e4SLinus Torvalds result *= 1024; 3421da177e4SLinus Torvalds /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */ 343954c2422SJoern Engel if ((*endp)[1] == 'i') { 344954c2422SJoern Engel if ((*endp)[2] == 'B') 345954c2422SJoern Engel (*endp) += 3; 346954c2422SJoern Engel else 3471da177e4SLinus Torvalds (*endp) += 2; 3481da177e4SLinus Torvalds } 349954c2422SJoern Engel } 3501da177e4SLinus Torvalds return result; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds 354cc71229fSThomas Gleixner static int parse_num(size_t *num, const char *token) 3551da177e4SLinus Torvalds { 3561da177e4SLinus Torvalds char *endp; 357cc71229fSThomas Gleixner size_t n; 3581da177e4SLinus Torvalds 359cc71229fSThomas Gleixner n = (size_t) ustrtoul(token, &endp, 0); 3601da177e4SLinus Torvalds if (*endp) 3611da177e4SLinus Torvalds return -EINVAL; 3621da177e4SLinus Torvalds 363cc71229fSThomas Gleixner *num = n; 3641da177e4SLinus Torvalds return 0; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds static inline void kill_final_newline(char *str) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds char *newline = strrchr(str, '\n'); 3711da177e4SLinus Torvalds if (newline && !newline[1]) 3721da177e4SLinus Torvalds *newline = 0; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds 376c4e7fb31SVille Herva #ifndef MODULE 377c4e7fb31SVille Herva static int block2mtd_init_called = 0; 378d6a3f017SFelix Fietkau /* 80 for device, 12 for erase size */ 379d6a3f017SFelix Fietkau static char block2mtd_paramline[80 + 12]; 380c4e7fb31SVille Herva #endif 381c4e7fb31SVille Herva 382c4e7fb31SVille Herva static int block2mtd_setup2(const char *val) 3831da177e4SLinus Torvalds { 384d6a3f017SFelix Fietkau /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */ 385d6a3f017SFelix Fietkau char buf[80 + 12 + 80 + 8]; 386a6550e57SJesper Juhl char *str = buf; 387*a04e9653SJoachim Wiberg char *token[BLOCK2MTD_PARAM_MAX_COUNT]; 3881da177e4SLinus Torvalds char *name; 389cc71229fSThomas Gleixner size_t erase_size = PAGE_SIZE; 390d6a3f017SFelix Fietkau unsigned long timeout = MTD_DEFAULT_TIMEOUT; 3911da177e4SLinus Torvalds int i, ret; 3921da177e4SLinus Torvalds 393a1c06609SJoe Perches if (strnlen(val, sizeof(buf)) >= sizeof(buf)) { 394a1c06609SJoe Perches pr_err("parameter too long\n"); 395a1c06609SJoe Perches return 0; 396a1c06609SJoe Perches } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds strcpy(str, val); 3991da177e4SLinus Torvalds kill_final_newline(str); 4001da177e4SLinus Torvalds 401*a04e9653SJoachim Wiberg for (i = 0; i < BLOCK2MTD_PARAM_MAX_COUNT; i++) 4021da177e4SLinus Torvalds token[i] = strsep(&str, ","); 4031da177e4SLinus Torvalds 404a1c06609SJoe Perches if (str) { 405a1c06609SJoe Perches pr_err("too many arguments\n"); 406a1c06609SJoe Perches return 0; 407a1c06609SJoe Perches } 4081da177e4SLinus Torvalds 409a1c06609SJoe Perches if (!token[0]) { 410a1c06609SJoe Perches pr_err("no argument\n"); 411a1c06609SJoe Perches return 0; 412a1c06609SJoe Perches } 4131da177e4SLinus Torvalds 414c4e7fb31SVille Herva name = token[0]; 415a1c06609SJoe Perches if (strlen(name) + 1 > 80) { 416a1c06609SJoe Perches pr_err("device name too long\n"); 417a1c06609SJoe Perches return 0; 418a1c06609SJoe Perches } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds if (token[1]) { 421cc71229fSThomas Gleixner ret = parse_num(&erase_size, token[1]); 422a6550e57SJesper Juhl if (ret) { 423a1c06609SJoe Perches pr_err("illegal erase size\n"); 424a1c06609SJoe Perches return 0; 4251da177e4SLinus Torvalds } 426a6550e57SJesper Juhl } 4271da177e4SLinus Torvalds 428d6a3f017SFelix Fietkau add_device(name, erase_size, timeout); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds return 0; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds 434e4dca7b7SKees Cook static int block2mtd_setup(const char *val, const struct kernel_param *kp) 435c4e7fb31SVille Herva { 436c4e7fb31SVille Herva #ifdef MODULE 437c4e7fb31SVille Herva return block2mtd_setup2(val); 438c4e7fb31SVille Herva #else 439c4e7fb31SVille Herva /* If more parameters are later passed in via 440c4e7fb31SVille Herva /sys/module/block2mtd/parameters/block2mtd 441c4e7fb31SVille Herva and block2mtd_init() has already been called, 442c4e7fb31SVille Herva we can parse the argument now. */ 443c4e7fb31SVille Herva 444c4e7fb31SVille Herva if (block2mtd_init_called) 445c4e7fb31SVille Herva return block2mtd_setup2(val); 446c4e7fb31SVille Herva 447c4e7fb31SVille Herva /* During early boot stage, we only save the parameters 448c4e7fb31SVille Herva here. We must parse them later: if the param passed 449c4e7fb31SVille Herva from kernel boot command line, block2mtd_setup() is 450c4e7fb31SVille Herva called so early that it is not possible to resolve 451c4e7fb31SVille Herva the device (even kmalloc() fails). Deter that work to 452c4e7fb31SVille Herva block2mtd_setup2(). */ 453c4e7fb31SVille Herva 454c4e7fb31SVille Herva strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline)); 455c4e7fb31SVille Herva 456c4e7fb31SVille Herva return 0; 457c4e7fb31SVille Herva #endif 458c4e7fb31SVille Herva } 459c4e7fb31SVille Herva 460c4e7fb31SVille Herva 4611da177e4SLinus Torvalds module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200); 4621da177e4SLinus Torvalds MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\""); 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds static int __init block2mtd_init(void) 4651da177e4SLinus Torvalds { 466c4e7fb31SVille Herva int ret = 0; 467c4e7fb31SVille Herva 468c4e7fb31SVille Herva #ifndef MODULE 469c4e7fb31SVille Herva if (strlen(block2mtd_paramline)) 470c4e7fb31SVille Herva ret = block2mtd_setup2(block2mtd_paramline); 471c4e7fb31SVille Herva block2mtd_init_called = 1; 472c4e7fb31SVille Herva #endif 473c4e7fb31SVille Herva 474c4e7fb31SVille Herva return ret; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds 478810b7e06SBill Pemberton static void block2mtd_exit(void) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds struct list_head *pos, *next; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* Remove the MTD devices */ 4831da177e4SLinus Torvalds list_for_each_safe(pos, next, &blkmtd_device_list) { 4841da177e4SLinus Torvalds struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); 4851da177e4SLinus Torvalds block2mtd_sync(&dev->mtd); 486ee0e87b1SJamie Iles mtd_device_unregister(&dev->mtd); 4874bed207cSFabian Frederick mutex_destroy(&dev->write_mutex); 488a1c06609SJoe Perches pr_info("mtd%d: [%s] removed\n", 489a1c06609SJoe Perches dev->mtd.index, 4900bc88c59SStephane Chazelas dev->mtd.name + strlen("block2mtd: ")); 4911da177e4SLinus Torvalds list_del(&dev->list); 4921da177e4SLinus Torvalds block2mtd_free_device(dev); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 496d6a3f017SFelix Fietkau late_initcall(block2mtd_init); 4971da177e4SLinus Torvalds module_exit(block2mtd_exit); 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 5002b54aaefSJoern Engel MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>"); 5011da177e4SLinus Torvalds MODULE_DESCRIPTION("Emulate an MTD using a block device"); 502