xref: /linux/drivers/mtd/devices/block2mtd.c (revision d40a18fec04ae6dc6f6760923aa5f487d1af2444)
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 
341da177e4SLinus Torvalds /* Info for the block device */
351da177e4SLinus Torvalds struct block2mtd_dev {
361da177e4SLinus Torvalds 	struct list_head list;
371da177e4SLinus Torvalds 	struct block_device *blkdev;
381da177e4SLinus Torvalds 	struct mtd_info mtd;
3948b19268SIngo Molnar 	struct mutex write_mutex;
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* Static info about the MTD, used in cleanup_module */
441da177e4SLinus Torvalds static LIST_HEAD(blkmtd_device_list);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 
4721d31f1fSJoern Engel static struct page *page_read(struct address_space *mapping, int index)
481da177e4SLinus Torvalds {
496fe6900eSNick Piggin 	return read_mapping_page(mapping, index, NULL);
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /* erase a specified part of the device */
531da177e4SLinus Torvalds static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
561da177e4SLinus Torvalds 	struct page *page;
571da177e4SLinus Torvalds 	int index = to >> PAGE_SHIFT;	// page index
581da177e4SLinus Torvalds 	int pages = len >> PAGE_SHIFT;
591da177e4SLinus Torvalds 	u_long *p;
601da177e4SLinus Torvalds 	u_long *max;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	while (pages) {
6321d31f1fSJoern Engel 		page = page_read(mapping, index);
641da177e4SLinus Torvalds 		if (IS_ERR(page))
651da177e4SLinus Torvalds 			return PTR_ERR(page);
661da177e4SLinus Torvalds 
670ffb74ccSJoern Engel 		max = page_address(page) + PAGE_SIZE;
680ffb74ccSJoern Engel 		for (p=page_address(page); p<max; p++)
691da177e4SLinus Torvalds 			if (*p != -1UL) {
701da177e4SLinus Torvalds 				lock_page(page);
711da177e4SLinus Torvalds 				memset(page_address(page), 0xff, PAGE_SIZE);
721da177e4SLinus Torvalds 				set_page_dirty(page);
731da177e4SLinus Torvalds 				unlock_page(page);
74031da73fSNeilBrown 				balance_dirty_pages_ratelimited(mapping);
751da177e4SLinus Torvalds 				break;
761da177e4SLinus Torvalds 			}
771da177e4SLinus Torvalds 
7809cbfeafSKirill A. Shutemov 		put_page(page);
791da177e4SLinus Torvalds 		pages--;
801da177e4SLinus Torvalds 		index++;
811da177e4SLinus Torvalds 	}
821da177e4SLinus Torvalds 	return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
871da177e4SLinus Torvalds 	size_t from = instr->addr;
881da177e4SLinus Torvalds 	size_t len = instr->len;
891da177e4SLinus Torvalds 	int err;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	instr->state = MTD_ERASING;
9248b19268SIngo Molnar 	mutex_lock(&dev->write_mutex);
931da177e4SLinus Torvalds 	err = _block2mtd_erase(dev, from, len);
9448b19268SIngo Molnar 	mutex_unlock(&dev->write_mutex);
951da177e4SLinus Torvalds 	if (err) {
96a1c06609SJoe Perches 		pr_err("erase failed err = %d\n", err);
971da177e4SLinus Torvalds 		instr->state = MTD_ERASE_FAILED;
981da177e4SLinus Torvalds 	} else
991da177e4SLinus Torvalds 		instr->state = MTD_ERASE_DONE;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	mtd_erase_callback(instr);
1021da177e4SLinus Torvalds 	return err;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1071da177e4SLinus Torvalds 		size_t *retlen, u_char *buf)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
1101da177e4SLinus Torvalds 	struct page *page;
1111da177e4SLinus Torvalds 	int index = from >> PAGE_SHIFT;
112711c11b7SJoern Engel 	int offset = from & (PAGE_SIZE-1);
1131da177e4SLinus Torvalds 	int cpylen;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	while (len) {
1161da177e4SLinus Torvalds 		if ((offset + len) > PAGE_SIZE)
1171da177e4SLinus Torvalds 			cpylen = PAGE_SIZE - offset;	// multiple pages
1181da177e4SLinus Torvalds 		else
1191da177e4SLinus Torvalds 			cpylen = len;	// this page
1201da177e4SLinus Torvalds 		len = len - cpylen;
1211da177e4SLinus Torvalds 
12221d31f1fSJoern Engel 		page = page_read(dev->blkdev->bd_inode->i_mapping, index);
1231da177e4SLinus Torvalds 		if (IS_ERR(page))
1241da177e4SLinus Torvalds 			return PTR_ERR(page);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 		memcpy(buf, page_address(page) + offset, cpylen);
12709cbfeafSKirill A. Shutemov 		put_page(page);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 		if (retlen)
1301da177e4SLinus Torvalds 			*retlen += cpylen;
1311da177e4SLinus Torvalds 		buf += cpylen;
1321da177e4SLinus Torvalds 		offset = 0;
1331da177e4SLinus Torvalds 		index++;
1341da177e4SLinus Torvalds 	}
1351da177e4SLinus Torvalds 	return 0;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /* write data to the underlying device */
1401da177e4SLinus Torvalds static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
1411da177e4SLinus Torvalds 		loff_t to, size_t len, size_t *retlen)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	struct page *page;
1441da177e4SLinus Torvalds 	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
1451da177e4SLinus Torvalds 	int index = to >> PAGE_SHIFT;	// page index
1461da177e4SLinus Torvalds 	int offset = to & ~PAGE_MASK;	// page offset
1471da177e4SLinus Torvalds 	int cpylen;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	while (len) {
1501da177e4SLinus Torvalds 		if ((offset+len) > PAGE_SIZE)
1511da177e4SLinus Torvalds 			cpylen = PAGE_SIZE - offset;	// multiple pages
1521da177e4SLinus Torvalds 		else
1531da177e4SLinus Torvalds 			cpylen = len;			// this page
1541da177e4SLinus Torvalds 		len = len - cpylen;
1551da177e4SLinus Torvalds 
15621d31f1fSJoern Engel 		page = page_read(mapping, index);
1571da177e4SLinus Torvalds 		if (IS_ERR(page))
1581da177e4SLinus Torvalds 			return PTR_ERR(page);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 		if (memcmp(page_address(page)+offset, buf, cpylen)) {
1611da177e4SLinus Torvalds 			lock_page(page);
1621da177e4SLinus Torvalds 			memcpy(page_address(page) + offset, buf, cpylen);
1631da177e4SLinus Torvalds 			set_page_dirty(page);
1641da177e4SLinus Torvalds 			unlock_page(page);
165031da73fSNeilBrown 			balance_dirty_pages_ratelimited(mapping);
1661da177e4SLinus Torvalds 		}
16709cbfeafSKirill A. Shutemov 		put_page(page);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 		if (retlen)
1701da177e4SLinus Torvalds 			*retlen += cpylen;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 		buf += cpylen;
1731da177e4SLinus Torvalds 		offset = 0;
1741da177e4SLinus Torvalds 		index++;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 	return 0;
1771da177e4SLinus Torvalds }
178c4e7fb31SVille Herva 
179c4e7fb31SVille Herva 
1801da177e4SLinus Torvalds static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1811da177e4SLinus Torvalds 		size_t *retlen, const u_char *buf)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
1841da177e4SLinus Torvalds 	int err;
1851da177e4SLinus Torvalds 
18648b19268SIngo Molnar 	mutex_lock(&dev->write_mutex);
1871da177e4SLinus Torvalds 	err = _block2mtd_write(dev, buf, to, len, retlen);
18848b19268SIngo Molnar 	mutex_unlock(&dev->write_mutex);
1891da177e4SLinus Torvalds 	if (err > 0)
1901da177e4SLinus Torvalds 		err = 0;
1911da177e4SLinus Torvalds 	return err;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /* sync the device - wait until the write queue is empty */
1961da177e4SLinus Torvalds static void block2mtd_sync(struct mtd_info *mtd)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
1991da177e4SLinus Torvalds 	sync_blockdev(dev->blkdev);
2001da177e4SLinus Torvalds 	return;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static void block2mtd_free_device(struct block2mtd_dev *dev)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	if (!dev)
2071da177e4SLinus Torvalds 		return;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	kfree(dev->mtd.name);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (dev->blkdev) {
212fc0ecff6SAndrew Morton 		invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
213fc0ecff6SAndrew Morton 					0, -1);
214e525fd89STejun Heo 		blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	kfree(dev);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 
221d6a3f017SFelix Fietkau static struct block2mtd_dev *add_device(char *devname, int erase_size,
222d6a3f017SFelix Fietkau 		int timeout)
2231da177e4SLinus Torvalds {
224d6a3f017SFelix Fietkau #ifndef MODULE
225d6a3f017SFelix Fietkau 	int i;
226d6a3f017SFelix Fietkau #endif
227e525fd89STejun Heo 	const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
228*d40a18feSColin Ian King 	struct block_device *bdev;
2291da177e4SLinus Torvalds 	struct block2mtd_dev *dev;
230eadcf0d7SGreg Kroah-Hartman 	char *name;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	if (!devname)
2331da177e4SLinus Torvalds 		return NULL;
2341da177e4SLinus Torvalds 
23595b93a0cSBurman Yan 	dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
2361da177e4SLinus Torvalds 	if (!dev)
2371da177e4SLinus Torvalds 		return NULL;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* Get a handle on the device */
240d4d77629STejun Heo 	bdev = blkdev_get_by_path(devname, mode, dev);
241d6a3f017SFelix Fietkau 
242c4e7fb31SVille Herva #ifndef MODULE
243d6a3f017SFelix Fietkau 	/*
244d6a3f017SFelix Fietkau 	 * We might not have the root device mounted at this point.
245d6a3f017SFelix Fietkau 	 * Try to resolve the device name by other means.
246d6a3f017SFelix Fietkau 	 */
247d6a3f017SFelix Fietkau 	for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
248d6a3f017SFelix Fietkau 		dev_t devt;
249c4e7fb31SVille Herva 
250d6a3f017SFelix Fietkau 		if (i)
251d6a3f017SFelix Fietkau 			/*
252d6a3f017SFelix Fietkau 			 * Calling wait_for_device_probe in the first loop
253d6a3f017SFelix Fietkau 			 * was not enough, sleep for a bit in subsequent
254d6a3f017SFelix Fietkau 			 * go-arounds.
255d6a3f017SFelix Fietkau 			 */
256d6a3f017SFelix Fietkau 			msleep(1000);
257d6a3f017SFelix Fietkau 		wait_for_device_probe();
258c4e7fb31SVille Herva 
259d6a3f017SFelix Fietkau 		devt = name_to_dev_t(devname);
260d6a3f017SFelix Fietkau 		if (!devt)
261d6a3f017SFelix Fietkau 			continue;
262d4d77629STejun Heo 		bdev = blkdev_get_by_dev(devt, mode, dev);
263c4e7fb31SVille Herva 	}
264c4e7fb31SVille Herva #endif
265c4e7fb31SVille Herva 
2661da177e4SLinus Torvalds 	if (IS_ERR(bdev)) {
267a1c06609SJoe Perches 		pr_err("error: cannot open device %s\n", devname);
2684bed207cSFabian Frederick 		goto err_free_block2mtd;
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 	dev->blkdev = bdev;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
273a1c06609SJoe Perches 		pr_err("attempting to use an MTD device as a block device\n");
2744bed207cSFabian Frederick 		goto err_free_block2mtd;
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 
277ea6d833aSFabian Frederick 	if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
278ea6d833aSFabian Frederick 		pr_err("erasesize must be a divisor of device size\n");
279ea6d833aSFabian Frederick 		goto err_free_block2mtd;
280ea6d833aSFabian Frederick 	}
281ea6d833aSFabian Frederick 
28248b19268SIngo Molnar 	mutex_init(&dev->write_mutex);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* Setup the MTD structure */
2851da177e4SLinus Torvalds 	/* make the name contain the block device in */
2864d682420SJulia Lawall 	name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
287eadcf0d7SGreg Kroah-Hartman 	if (!name)
2884bed207cSFabian Frederick 		goto err_destroy_mutex;
2891da177e4SLinus Torvalds 
290eadcf0d7SGreg Kroah-Hartman 	dev->mtd.name = name;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
2931da177e4SLinus Torvalds 	dev->mtd.erasesize = erase_size;
29417ffc7baSArtem B. Bityutskiy 	dev->mtd.writesize = 1;
295b6043874SArtem Bityutskiy 	dev->mtd.writebufsize = PAGE_SIZE;
29621c8db9eSDavid Woodhouse 	dev->mtd.type = MTD_RAM;
2971da177e4SLinus Torvalds 	dev->mtd.flags = MTD_CAP_RAM;
2983c3c10bbSArtem Bityutskiy 	dev->mtd._erase = block2mtd_erase;
2993c3c10bbSArtem Bityutskiy 	dev->mtd._write = block2mtd_write;
3003c3c10bbSArtem Bityutskiy 	dev->mtd._sync = block2mtd_sync;
3013c3c10bbSArtem Bityutskiy 	dev->mtd._read = block2mtd_read;
3021da177e4SLinus Torvalds 	dev->mtd.priv = dev;
3031da177e4SLinus Torvalds 	dev->mtd.owner = THIS_MODULE;
3041da177e4SLinus Torvalds 
305ee0e87b1SJamie Iles 	if (mtd_device_register(&dev->mtd, NULL, 0)) {
30625985edcSLucas De Marchi 		/* Device didn't get added, so free the entry */
3074bed207cSFabian Frederick 		goto err_destroy_mutex;
3081da177e4SLinus Torvalds 	}
309d6a3f017SFelix Fietkau 
3101da177e4SLinus Torvalds 	list_add(&dev->list, &blkmtd_device_list);
311a1c06609SJoe Perches 	pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
312a1c06609SJoe Perches 		dev->mtd.index,
3130bc88c59SStephane Chazelas 		dev->mtd.name + strlen("block2mtd: "),
3141da177e4SLinus Torvalds 		dev->mtd.erasesize >> 10, dev->mtd.erasesize);
3151da177e4SLinus Torvalds 	return dev;
3161da177e4SLinus Torvalds 
3174bed207cSFabian Frederick err_destroy_mutex:
3184bed207cSFabian Frederick 	mutex_destroy(&dev->write_mutex);
3194bed207cSFabian Frederick err_free_block2mtd:
3201da177e4SLinus Torvalds 	block2mtd_free_device(dev);
3211da177e4SLinus Torvalds 	return NULL;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 
325954c2422SJoern Engel /* This function works similar to reguler strtoul.  In addition, it
326954c2422SJoern Engel  * allows some suffixes for a more human-readable number format:
327954c2422SJoern Engel  * ki, Ki, kiB, KiB	- multiply result with 1024
328954c2422SJoern Engel  * Mi, MiB		- multiply result with 1024^2
329954c2422SJoern Engel  * Gi, GiB		- multiply result with 1024^3
330954c2422SJoern Engel  */
3311da177e4SLinus Torvalds static int ustrtoul(const char *cp, char **endp, unsigned int base)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	unsigned long result = simple_strtoul(cp, endp, base);
3341da177e4SLinus Torvalds 	switch (**endp) {
3351da177e4SLinus Torvalds 	case 'G' :
3361da177e4SLinus Torvalds 		result *= 1024;
3371da177e4SLinus Torvalds 	case 'M':
3381da177e4SLinus Torvalds 		result *= 1024;
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;
3871da177e4SLinus Torvalds 	char *token[2];
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 
4011da177e4SLinus Torvalds 	for (i = 0; i < 2; 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