xref: /linux/drivers/mtd/devices/block2mtd.c (revision 6fe6900e1e5b6fa9e5c59aa5061f244fe3f467e2)
11da177e4SLinus Torvalds /*
22b9175c1SAdrian Bunk  * $Id: block2mtd.c,v 1.30 2005/11/29 14:48:32 gleixner Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * block2mtd.c - create an mtd from a block device
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 2001,2002	Simon Evans <spse@secret.org.uk>
7151e7659SDavid Woodhouse  * Copyright (C) 2004-2006	Jörn Engel <joern@wh.fh-wedel.de>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Licence: GPL
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/fs.h>
131da177e4SLinus Torvalds #include <linux/blkdev.h>
141da177e4SLinus Torvalds #include <linux/bio.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/list.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/mtd/mtd.h>
191da177e4SLinus Torvalds #include <linux/buffer_head.h>
2048b19268SIngo Molnar #include <linux/mutex.h>
21c4e7fb31SVille Herva #include <linux/mount.h>
221da177e4SLinus Torvalds 
232b9175c1SAdrian Bunk #define VERSION "$Revision: 1.30 $"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define ERROR(fmt, args...) printk(KERN_ERR "block2mtd: " fmt "\n" , ## args)
271da177e4SLinus Torvalds #define INFO(fmt, args...) printk(KERN_INFO "block2mtd: " fmt "\n" , ## args)
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /* Info for the block device */
311da177e4SLinus Torvalds struct block2mtd_dev {
321da177e4SLinus Torvalds 	struct list_head list;
331da177e4SLinus Torvalds 	struct block_device *blkdev;
341da177e4SLinus Torvalds 	struct mtd_info mtd;
3548b19268SIngo Molnar 	struct mutex write_mutex;
361da177e4SLinus Torvalds };
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /* Static info about the MTD, used in cleanup_module */
401da177e4SLinus Torvalds static LIST_HEAD(blkmtd_device_list);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 
4321d31f1fSJoern Engel static struct page *page_read(struct address_space *mapping, int index)
441da177e4SLinus Torvalds {
45*6fe6900eSNick Piggin 	return read_mapping_page(mapping, index, NULL);
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* erase a specified part of the device */
491da177e4SLinus Torvalds static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
521da177e4SLinus Torvalds 	struct page *page;
531da177e4SLinus Torvalds 	int index = to >> PAGE_SHIFT;	// page index
541da177e4SLinus Torvalds 	int pages = len >> PAGE_SHIFT;
551da177e4SLinus Torvalds 	u_long *p;
561da177e4SLinus Torvalds 	u_long *max;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	while (pages) {
5921d31f1fSJoern Engel 		page = page_read(mapping, index);
601da177e4SLinus Torvalds 		if (!page)
611da177e4SLinus Torvalds 			return -ENOMEM;
621da177e4SLinus Torvalds 		if (IS_ERR(page))
631da177e4SLinus Torvalds 			return PTR_ERR(page);
641da177e4SLinus Torvalds 
650ffb74ccSJoern Engel 		max = page_address(page) + PAGE_SIZE;
660ffb74ccSJoern Engel 		for (p=page_address(page); p<max; p++)
671da177e4SLinus Torvalds 			if (*p != -1UL) {
681da177e4SLinus Torvalds 				lock_page(page);
691da177e4SLinus Torvalds 				memset(page_address(page), 0xff, PAGE_SIZE);
701da177e4SLinus Torvalds 				set_page_dirty(page);
711da177e4SLinus Torvalds 				unlock_page(page);
721da177e4SLinus Torvalds 				break;
731da177e4SLinus Torvalds 			}
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 		page_cache_release(page);
761da177e4SLinus Torvalds 		pages--;
771da177e4SLinus Torvalds 		index++;
781da177e4SLinus Torvalds 	}
791da177e4SLinus Torvalds 	return 0;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
841da177e4SLinus Torvalds 	size_t from = instr->addr;
851da177e4SLinus Torvalds 	size_t len = instr->len;
861da177e4SLinus Torvalds 	int err;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	instr->state = MTD_ERASING;
8948b19268SIngo Molnar 	mutex_lock(&dev->write_mutex);
901da177e4SLinus Torvalds 	err = _block2mtd_erase(dev, from, len);
9148b19268SIngo Molnar 	mutex_unlock(&dev->write_mutex);
921da177e4SLinus Torvalds 	if (err) {
931da177e4SLinus Torvalds 		ERROR("erase failed err = %d", err);
941da177e4SLinus Torvalds 		instr->state = MTD_ERASE_FAILED;
951da177e4SLinus Torvalds 	} else
961da177e4SLinus Torvalds 		instr->state = MTD_ERASE_DONE;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	instr->state = MTD_ERASE_DONE;
991da177e4SLinus Torvalds 	mtd_erase_callback(instr);
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;
1091da177e4SLinus Torvalds 	int index = from >> PAGE_SHIFT;
110711c11b7SJoern Engel 	int offset = from & (PAGE_SIZE-1);
1111da177e4SLinus Torvalds 	int cpylen;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (from > mtd->size)
1141da177e4SLinus Torvalds 		return -EINVAL;
1151da177e4SLinus Torvalds 	if (from + len > mtd->size)
1161da177e4SLinus Torvalds 		len = mtd->size - from;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (retlen)
1191da177e4SLinus Torvalds 		*retlen = 0;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	while (len) {
1221da177e4SLinus Torvalds 		if ((offset + len) > PAGE_SIZE)
1231da177e4SLinus Torvalds 			cpylen = PAGE_SIZE - offset;	// multiple pages
1241da177e4SLinus Torvalds 		else
1251da177e4SLinus Torvalds 			cpylen = len;	// this page
1261da177e4SLinus Torvalds 		len = len - cpylen;
1271da177e4SLinus Torvalds 
12821d31f1fSJoern Engel 		page = page_read(dev->blkdev->bd_inode->i_mapping, index);
1291da177e4SLinus Torvalds 		if (!page)
1301da177e4SLinus Torvalds 			return -ENOMEM;
1311da177e4SLinus Torvalds 		if (IS_ERR(page))
1321da177e4SLinus Torvalds 			return PTR_ERR(page);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 		memcpy(buf, page_address(page) + offset, cpylen);
1351da177e4SLinus Torvalds 		page_cache_release(page);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 		if (retlen)
1381da177e4SLinus Torvalds 			*retlen += cpylen;
1391da177e4SLinus Torvalds 		buf += cpylen;
1401da177e4SLinus Torvalds 		offset = 0;
1411da177e4SLinus Torvalds 		index++;
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 	return 0;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /* write data to the underlying device */
1481da177e4SLinus Torvalds static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
1491da177e4SLinus Torvalds 		loff_t to, size_t len, size_t *retlen)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct page *page;
1521da177e4SLinus Torvalds 	struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
1531da177e4SLinus Torvalds 	int index = to >> PAGE_SHIFT;	// page index
1541da177e4SLinus Torvalds 	int offset = to & ~PAGE_MASK;	// page offset
1551da177e4SLinus Torvalds 	int cpylen;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	if (retlen)
1581da177e4SLinus Torvalds 		*retlen = 0;
1591da177e4SLinus Torvalds 	while (len) {
1601da177e4SLinus Torvalds 		if ((offset+len) > PAGE_SIZE)
1611da177e4SLinus Torvalds 			cpylen = PAGE_SIZE - offset;	// multiple pages
1621da177e4SLinus Torvalds 		else
1631da177e4SLinus Torvalds 			cpylen = len;			// this page
1641da177e4SLinus Torvalds 		len = len - cpylen;
1651da177e4SLinus Torvalds 
16621d31f1fSJoern Engel 		page = page_read(mapping, index);
1671da177e4SLinus Torvalds 		if (!page)
1681da177e4SLinus Torvalds 			return -ENOMEM;
1691da177e4SLinus Torvalds 		if (IS_ERR(page))
1701da177e4SLinus Torvalds 			return PTR_ERR(page);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 		if (memcmp(page_address(page)+offset, buf, cpylen)) {
1731da177e4SLinus Torvalds 			lock_page(page);
1741da177e4SLinus Torvalds 			memcpy(page_address(page) + offset, buf, cpylen);
1751da177e4SLinus Torvalds 			set_page_dirty(page);
1761da177e4SLinus Torvalds 			unlock_page(page);
1771da177e4SLinus Torvalds 		}
1781da177e4SLinus Torvalds 		page_cache_release(page);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		if (retlen)
1811da177e4SLinus Torvalds 			*retlen += cpylen;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 		buf += cpylen;
1841da177e4SLinus Torvalds 		offset = 0;
1851da177e4SLinus Torvalds 		index++;
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 	return 0;
1881da177e4SLinus Torvalds }
189c4e7fb31SVille Herva 
190c4e7fb31SVille Herva 
1911da177e4SLinus Torvalds static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1921da177e4SLinus Torvalds 		size_t *retlen, const u_char *buf)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
1951da177e4SLinus Torvalds 	int err;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if (!len)
1981da177e4SLinus Torvalds 		return 0;
1991da177e4SLinus Torvalds 	if (to >= mtd->size)
2001da177e4SLinus Torvalds 		return -ENOSPC;
2011da177e4SLinus Torvalds 	if (to + len > mtd->size)
2021da177e4SLinus Torvalds 		len = mtd->size - to;
2031da177e4SLinus Torvalds 
20448b19268SIngo Molnar 	mutex_lock(&dev->write_mutex);
2051da177e4SLinus Torvalds 	err = _block2mtd_write(dev, buf, to, len, retlen);
20648b19268SIngo Molnar 	mutex_unlock(&dev->write_mutex);
2071da177e4SLinus Torvalds 	if (err > 0)
2081da177e4SLinus Torvalds 		err = 0;
2091da177e4SLinus Torvalds 	return err;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /* sync the device - wait until the write queue is empty */
2141da177e4SLinus Torvalds static void block2mtd_sync(struct mtd_info *mtd)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct block2mtd_dev *dev = mtd->priv;
2171da177e4SLinus Torvalds 	sync_blockdev(dev->blkdev);
2181da177e4SLinus Torvalds 	return;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static void block2mtd_free_device(struct block2mtd_dev *dev)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	if (!dev)
2251da177e4SLinus Torvalds 		return;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	kfree(dev->mtd.name);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	if (dev->blkdev) {
230fc0ecff6SAndrew Morton 		invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
231fc0ecff6SAndrew Morton 					0, -1);
2321da177e4SLinus Torvalds 		close_bdev_excl(dev->blkdev);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	kfree(dev);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /* FIXME: ensure that mtd->size % erase_size == 0 */
2401da177e4SLinus Torvalds static struct block2mtd_dev *add_device(char *devname, int erase_size)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct block_device *bdev;
2431da177e4SLinus Torvalds 	struct block2mtd_dev *dev;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	if (!devname)
2461da177e4SLinus Torvalds 		return NULL;
2471da177e4SLinus Torvalds 
24895b93a0cSBurman Yan 	dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
2491da177e4SLinus Torvalds 	if (!dev)
2501da177e4SLinus Torvalds 		return NULL;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/* Get a handle on the device */
2531da177e4SLinus Torvalds 	bdev = open_bdev_excl(devname, O_RDWR, NULL);
254c4e7fb31SVille Herva #ifndef MODULE
255c4e7fb31SVille Herva 	if (IS_ERR(bdev)) {
256c4e7fb31SVille Herva 
257c4e7fb31SVille Herva 		/* We might not have rootfs mounted at this point. Try
258c4e7fb31SVille Herva 		   to resolve the device name by other means. */
259c4e7fb31SVille Herva 
2608870530aSJoern Engel 		dev_t devt = name_to_dev_t(devname);
2618870530aSJoern Engel 		if (devt) {
2628870530aSJoern Engel 			bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
263c4e7fb31SVille Herva 		}
264c4e7fb31SVille Herva 	}
265c4e7fb31SVille Herva #endif
266c4e7fb31SVille Herva 
2671da177e4SLinus Torvalds 	if (IS_ERR(bdev)) {
2681da177e4SLinus Torvalds 		ERROR("error: cannot open device %s", devname);
2691da177e4SLinus Torvalds 		goto devinit_err;
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds 	dev->blkdev = bdev;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
2741da177e4SLinus Torvalds 		ERROR("attempting to use an MTD device as a block device");
2751da177e4SLinus Torvalds 		goto devinit_err;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
27848b19268SIngo Molnar 	mutex_init(&dev->write_mutex);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* Setup the MTD structure */
2811da177e4SLinus Torvalds 	/* make the name contain the block device in */
2821da177e4SLinus Torvalds 	dev->mtd.name = kmalloc(sizeof("block2mtd: ") + strlen(devname),
2831da177e4SLinus Torvalds 			GFP_KERNEL);
2841da177e4SLinus Torvalds 	if (!dev->mtd.name)
2851da177e4SLinus Torvalds 		goto devinit_err;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	sprintf(dev->mtd.name, "block2mtd: %s", devname);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
2901da177e4SLinus Torvalds 	dev->mtd.erasesize = erase_size;
29117ffc7baSArtem B. Bityutskiy 	dev->mtd.writesize = 1;
29221c8db9eSDavid Woodhouse 	dev->mtd.type = MTD_RAM;
2931da177e4SLinus Torvalds 	dev->mtd.flags = MTD_CAP_RAM;
2941da177e4SLinus Torvalds 	dev->mtd.erase = block2mtd_erase;
2951da177e4SLinus Torvalds 	dev->mtd.write = block2mtd_write;
2961da177e4SLinus Torvalds 	dev->mtd.writev = default_mtd_writev;
2971da177e4SLinus Torvalds 	dev->mtd.sync = block2mtd_sync;
2981da177e4SLinus Torvalds 	dev->mtd.read = block2mtd_read;
2991da177e4SLinus Torvalds 	dev->mtd.priv = dev;
3001da177e4SLinus Torvalds 	dev->mtd.owner = THIS_MODULE;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (add_mtd_device(&dev->mtd)) {
3031da177e4SLinus Torvalds 		/* Device didnt get added, so free the entry */
3041da177e4SLinus Torvalds 		goto devinit_err;
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 	list_add(&dev->list, &blkmtd_device_list);
3071da177e4SLinus Torvalds 	INFO("mtd%d: [%s] erase_size = %dKiB [%d]", dev->mtd.index,
3081da177e4SLinus Torvalds 			dev->mtd.name + strlen("blkmtd: "),
3091da177e4SLinus Torvalds 			dev->mtd.erasesize >> 10, dev->mtd.erasesize);
3101da177e4SLinus Torvalds 	return dev;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds devinit_err:
3131da177e4SLinus Torvalds 	block2mtd_free_device(dev);
3141da177e4SLinus Torvalds 	return NULL;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 
318954c2422SJoern Engel /* This function works similar to reguler strtoul.  In addition, it
319954c2422SJoern Engel  * allows some suffixes for a more human-readable number format:
320954c2422SJoern Engel  * ki, Ki, kiB, KiB	- multiply result with 1024
321954c2422SJoern Engel  * Mi, MiB		- multiply result with 1024^2
322954c2422SJoern Engel  * Gi, GiB		- multiply result with 1024^3
323954c2422SJoern Engel  */
3241da177e4SLinus Torvalds static int ustrtoul(const char *cp, char **endp, unsigned int base)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	unsigned long result = simple_strtoul(cp, endp, base);
3271da177e4SLinus Torvalds 	switch (**endp) {
3281da177e4SLinus Torvalds 	case 'G' :
3291da177e4SLinus Torvalds 		result *= 1024;
3301da177e4SLinus Torvalds 	case 'M':
3311da177e4SLinus Torvalds 		result *= 1024;
332954c2422SJoern Engel 	case 'K':
3331da177e4SLinus Torvalds 	case 'k':
3341da177e4SLinus Torvalds 		result *= 1024;
3351da177e4SLinus Torvalds 	/* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
336954c2422SJoern Engel 		if ((*endp)[1] == 'i') {
337954c2422SJoern Engel 			if ((*endp)[2] == 'B')
338954c2422SJoern Engel 				(*endp) += 3;
339954c2422SJoern Engel 			else
3401da177e4SLinus Torvalds 				(*endp) += 2;
3411da177e4SLinus Torvalds 		}
342954c2422SJoern Engel 	}
3431da177e4SLinus Torvalds 	return result;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 
347cc71229fSThomas Gleixner static int parse_num(size_t *num, const char *token)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	char *endp;
350cc71229fSThomas Gleixner 	size_t n;
3511da177e4SLinus Torvalds 
352cc71229fSThomas Gleixner 	n = (size_t) ustrtoul(token, &endp, 0);
3531da177e4SLinus Torvalds 	if (*endp)
3541da177e4SLinus Torvalds 		return -EINVAL;
3551da177e4SLinus Torvalds 
356cc71229fSThomas Gleixner 	*num = n;
3571da177e4SLinus Torvalds 	return 0;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds static inline void kill_final_newline(char *str)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	char *newline = strrchr(str, '\n');
3641da177e4SLinus Torvalds 	if (newline && !newline[1])
3651da177e4SLinus Torvalds 		*newline = 0;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds #define parse_err(fmt, args...) do {		\
3701da177e4SLinus Torvalds 	ERROR("block2mtd: " fmt "\n", ## args);	\
3711da177e4SLinus Torvalds 	return 0;				\
3721da177e4SLinus Torvalds } while (0)
3731da177e4SLinus Torvalds 
374c4e7fb31SVille Herva #ifndef MODULE
375c4e7fb31SVille Herva static int block2mtd_init_called = 0;
376c4e7fb31SVille Herva static __initdata char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
377c4e7fb31SVille Herva #endif
378c4e7fb31SVille Herva 
379c4e7fb31SVille Herva 
380c4e7fb31SVille Herva static int block2mtd_setup2(const char *val)
3811da177e4SLinus Torvalds {
382a6550e57SJesper Juhl 	char buf[80 + 12]; /* 80 for device, 12 for erase size */
383a6550e57SJesper Juhl 	char *str = buf;
3841da177e4SLinus Torvalds 	char *token[2];
3851da177e4SLinus Torvalds 	char *name;
386cc71229fSThomas Gleixner 	size_t erase_size = PAGE_SIZE;
3871da177e4SLinus Torvalds 	int i, ret;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (strnlen(val, sizeof(buf)) >= sizeof(buf))
3901da177e4SLinus Torvalds 		parse_err("parameter too long");
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	strcpy(str, val);
3931da177e4SLinus Torvalds 	kill_final_newline(str);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
3961da177e4SLinus Torvalds 		token[i] = strsep(&str, ",");
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (str)
3991da177e4SLinus Torvalds 		parse_err("too many arguments");
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	if (!token[0])
4021da177e4SLinus Torvalds 		parse_err("no argument");
4031da177e4SLinus Torvalds 
404c4e7fb31SVille Herva 	name = token[0];
405c4e7fb31SVille Herva 	if (strlen(name) + 1 > 80)
406c4e7fb31SVille Herva 		parse_err("device name too long");
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	if (token[1]) {
409cc71229fSThomas Gleixner 		ret = parse_num(&erase_size, token[1]);
410a6550e57SJesper Juhl 		if (ret) {
411a6550e57SJesper Juhl 			kfree(name);
4121da177e4SLinus Torvalds 			parse_err("illegal erase size");
4131da177e4SLinus Torvalds 		}
414a6550e57SJesper Juhl 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	add_device(name, erase_size);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	return 0;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 
422c4e7fb31SVille Herva static int block2mtd_setup(const char *val, struct kernel_param *kp)
423c4e7fb31SVille Herva {
424c4e7fb31SVille Herva #ifdef MODULE
425c4e7fb31SVille Herva 	return block2mtd_setup2(val);
426c4e7fb31SVille Herva #else
427c4e7fb31SVille Herva 	/* If more parameters are later passed in via
428c4e7fb31SVille Herva 	   /sys/module/block2mtd/parameters/block2mtd
429c4e7fb31SVille Herva 	   and block2mtd_init() has already been called,
430c4e7fb31SVille Herva 	   we can parse the argument now. */
431c4e7fb31SVille Herva 
432c4e7fb31SVille Herva 	if (block2mtd_init_called)
433c4e7fb31SVille Herva 		return block2mtd_setup2(val);
434c4e7fb31SVille Herva 
435c4e7fb31SVille Herva 	/* During early boot stage, we only save the parameters
436c4e7fb31SVille Herva 	   here. We must parse them later: if the param passed
437c4e7fb31SVille Herva 	   from kernel boot command line, block2mtd_setup() is
438c4e7fb31SVille Herva 	   called so early that it is not possible to resolve
439c4e7fb31SVille Herva 	   the device (even kmalloc() fails). Deter that work to
440c4e7fb31SVille Herva 	   block2mtd_setup2(). */
441c4e7fb31SVille Herva 
442c4e7fb31SVille Herva 	strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
443c4e7fb31SVille Herva 
444c4e7fb31SVille Herva 	return 0;
445c4e7fb31SVille Herva #endif
446c4e7fb31SVille Herva }
447c4e7fb31SVille Herva 
448c4e7fb31SVille Herva 
4491da177e4SLinus Torvalds module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
4501da177e4SLinus Torvalds MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds static int __init block2mtd_init(void)
4531da177e4SLinus Torvalds {
454c4e7fb31SVille Herva 	int ret = 0;
4551da177e4SLinus Torvalds 	INFO("version " VERSION);
456c4e7fb31SVille Herva 
457c4e7fb31SVille Herva #ifndef MODULE
458c4e7fb31SVille Herva 	if (strlen(block2mtd_paramline))
459c4e7fb31SVille Herva 		ret = block2mtd_setup2(block2mtd_paramline);
460c4e7fb31SVille Herva 	block2mtd_init_called = 1;
461c4e7fb31SVille Herva #endif
462c4e7fb31SVille Herva 
463c4e7fb31SVille Herva 	return ret;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static void __devexit block2mtd_exit(void)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	struct list_head *pos, *next;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	/* Remove the MTD devices */
4721da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &blkmtd_device_list) {
4731da177e4SLinus Torvalds 		struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
4741da177e4SLinus Torvalds 		block2mtd_sync(&dev->mtd);
4751da177e4SLinus Torvalds 		del_mtd_device(&dev->mtd);
4761da177e4SLinus Torvalds 		INFO("mtd%d: [%s] removed", dev->mtd.index,
4771da177e4SLinus Torvalds 				dev->mtd.name + strlen("blkmtd: "));
4781da177e4SLinus Torvalds 		list_del(&dev->list);
4791da177e4SLinus Torvalds 		block2mtd_free_device(dev);
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds module_init(block2mtd_init);
4851da177e4SLinus Torvalds module_exit(block2mtd_exit);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds MODULE_LICENSE("GPL");
4881da177e4SLinus Torvalds MODULE_AUTHOR("Simon Evans <spse@secret.org.uk> and others");
4891da177e4SLinus Torvalds MODULE_DESCRIPTION("Emulate an MTD using a block device");
490