blob: 410a321682e6589405c2fd5f11956e15b44b22e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * block2mtd.c - create an mtd from a block device
3 *
4 * Copyright (C) 2001,2002 Simon Evans <spse@secret.org.uk>
Joern Engel2b54aae2008-02-06 01:38:02 -08005 * Copyright (C) 2004-2006 Joern Engel <joern@wh.fh-wedel.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Licence: GPL
8 */
Joe Perchesa1c06602013-04-19 10:59:35 -07009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Felix Fietkaud6a3f012014-11-09 07:21:13 -050012/*
13 * When the first attempt at device initialization fails, we may need to
14 * wait a little bit and retry. This timeout, by default 3 seconds, gives
15 * device time to start up. Required on BCM2708 and a few other chipsets.
16 */
17#define MTD_DEFAULT_TIMEOUT 3
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
Felix Fietkaud6a3f012014-11-09 07:21:13 -050020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fs.h>
22#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040023#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/bio.h>
25#include <linux/pagemap.h>
26#include <linux/list.h>
27#include <linux/init.h>
28#include <linux/mtd/mtd.h>
Ingo Molnar48b19262006-03-31 02:29:41 -080029#include <linux/mutex.h>
Ville Hervac4e7fb32006-07-14 00:31:16 +030030#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Ezequiel Garciaf83c3832013-10-13 18:05:23 -030032#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Info for the block device */
35struct block2mtd_dev {
36 struct list_head list;
37 struct block_device *blkdev;
38 struct mtd_info mtd;
Ingo Molnar48b19262006-03-31 02:29:41 -080039 struct mutex write_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
42
43/* Static info about the MTD, used in cleanup_module */
44static LIST_HEAD(blkmtd_device_list);
45
46
Nick Piggin6fe69002007-05-06 14:49:04 -070047static struct page *page_read(struct address_space *mapping, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Nick Piggin6fe69002007-05-06 14:49:04 -070049 return read_mapping_page(mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* erase a specified part of the device */
53static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
54{
55 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
56 struct page *page;
57 int index = to >> PAGE_SHIFT; // page index
58 int pages = len >> PAGE_SHIFT;
59 u_long *p;
60 u_long *max;
61
62 while (pages) {
Joern Engel21d31f12007-02-20 20:22:22 +010063 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (IS_ERR(page))
65 return PTR_ERR(page);
66
Joern Engel0ffb74c2007-02-20 20:20:58 +010067 max = page_address(page) + PAGE_SIZE;
68 for (p=page_address(page); p<max; p++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (*p != -1UL) {
70 lock_page(page);
71 memset(page_address(page), 0xff, PAGE_SIZE);
72 set_page_dirty(page);
73 unlock_page(page);
NeilBrown031da732012-12-12 19:32:15 +110074 balance_dirty_pages_ratelimited(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
76 }
77
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030078 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 pages--;
80 index++;
81 }
82 return 0;
83}
84static int block2mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
85{
86 struct block2mtd_dev *dev = mtd->priv;
87 size_t from = instr->addr;
88 size_t len = instr->len;
89 int err;
90
Ingo Molnar48b19262006-03-31 02:29:41 -080091 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 err = _block2mtd_erase(dev, from, len);
Ingo Molnar48b19262006-03-31 02:29:41 -080093 mutex_unlock(&dev->write_mutex);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +010094 if (err)
Joe Perchesa1c06602013-04-19 10:59:35 -070095 pr_err("erase failed err = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return err;
98}
99
100
101static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
102 size_t *retlen, u_char *buf)
103{
104 struct block2mtd_dev *dev = mtd->priv;
105 struct page *page;
106 int index = from >> PAGE_SHIFT;
Joern Engel711c11b2005-03-07 20:29:09 +0000107 int offset = from & (PAGE_SIZE-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int cpylen;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 while (len) {
111 if ((offset + len) > PAGE_SIZE)
112 cpylen = PAGE_SIZE - offset; // multiple pages
113 else
114 cpylen = len; // this page
115 len = len - cpylen;
116
Joern Engel21d31f12007-02-20 20:22:22 +0100117 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (IS_ERR(page))
119 return PTR_ERR(page);
120
121 memcpy(buf, page_address(page) + offset, cpylen);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300122 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (retlen)
125 *retlen += cpylen;
126 buf += cpylen;
127 offset = 0;
128 index++;
129 }
130 return 0;
131}
132
133
134/* write data to the underlying device */
135static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
136 loff_t to, size_t len, size_t *retlen)
137{
138 struct page *page;
139 struct address_space *mapping = dev->blkdev->bd_inode->i_mapping;
140 int index = to >> PAGE_SHIFT; // page index
141 int offset = to & ~PAGE_MASK; // page offset
142 int cpylen;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 while (len) {
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000145 if ((offset+len) > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 cpylen = PAGE_SIZE - offset; // multiple pages
147 else
148 cpylen = len; // this page
149 len = len - cpylen;
150
Joern Engel21d31f12007-02-20 20:22:22 +0100151 page = page_read(mapping, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (IS_ERR(page))
153 return PTR_ERR(page);
154
155 if (memcmp(page_address(page)+offset, buf, cpylen)) {
156 lock_page(page);
157 memcpy(page_address(page) + offset, buf, cpylen);
158 set_page_dirty(page);
159 unlock_page(page);
NeilBrown031da732012-12-12 19:32:15 +1100160 balance_dirty_pages_ratelimited(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300162 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (retlen)
165 *retlen += cpylen;
166
167 buf += cpylen;
168 offset = 0;
169 index++;
170 }
171 return 0;
172}
Ville Hervac4e7fb32006-07-14 00:31:16 +0300173
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static 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 Molnar48b19262006-03-31 02:29:41 -0800181 mutex_lock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 err = _block2mtd_write(dev, buf, to, len, retlen);
Ingo Molnar48b19262006-03-31 02:29:41 -0800183 mutex_unlock(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (err > 0)
185 err = 0;
186 return err;
187}
188
189
190/* sync the device - wait until the write queue is empty */
191static 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
199static 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 Mortonfc0ecff2007-02-10 01:45:39 -0800207 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
208 0, -1);
Tejun Heoe525fd82010-11-13 11:55:17 +0100209 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 kfree(dev);
213}
214
215
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500216static struct block2mtd_dev *add_device(char *devname, int erase_size,
217 int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500219#ifndef MODULE
220 int i;
221#endif
Tejun Heoe525fd82010-11-13 11:55:17 +0100222 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
Colin Ian Kingd40a18f2018-01-20 22:09:34 +0000223 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct block2mtd_dev *dev;
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700225 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (!devname)
228 return NULL;
229
Burman Yan95b93a02006-11-15 21:10:29 +0200230 dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (!dev)
232 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* Get a handle on the device */
Tejun Heod4d77622010-11-13 11:55:18 +0100235 bdev = blkdev_get_by_path(devname, mode, dev);
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500236
Ville Hervac4e7fb32006-07-14 00:31:16 +0300237#ifndef MODULE
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500238 /*
239 * We might not have the root device mounted at this point.
240 * Try to resolve the device name by other means.
241 */
242 for (i = 0; IS_ERR(bdev) && i <= timeout; i++) {
243 dev_t devt;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300244
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500245 if (i)
246 /*
247 * Calling wait_for_device_probe in the first loop
248 * was not enough, sleep for a bit in subsequent
249 * go-arounds.
250 */
251 msleep(1000);
252 wait_for_device_probe();
Ville Hervac4e7fb32006-07-14 00:31:16 +0300253
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500254 devt = name_to_dev_t(devname);
255 if (!devt)
256 continue;
257 bdev = blkdev_get_by_dev(devt, mode, dev);
Ville Hervac4e7fb32006-07-14 00:31:16 +0300258 }
259#endif
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (IS_ERR(bdev)) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700262 pr_err("error: cannot open device %s\n", devname);
Fabian Frederick4bed2072014-01-25 10:45:08 +0800263 goto err_free_block2mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 dev->blkdev = bdev;
266
267 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700268 pr_err("attempting to use an MTD device as a block device\n");
Fabian Frederick4bed2072014-01-25 10:45:08 +0800269 goto err_free_block2mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
Fabian Frederickea6d8332014-03-06 18:04:22 +0800272 if ((long)dev->blkdev->bd_inode->i_size % erase_size) {
273 pr_err("erasesize must be a divisor of device size\n");
274 goto err_free_block2mtd;
275 }
276
Ingo Molnar48b19262006-03-31 02:29:41 -0800277 mutex_init(&dev->write_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Setup the MTD structure */
280 /* make the name contain the block device in */
Julia Lawall4d682422010-03-10 22:15:19 +0100281 name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700282 if (!name)
Fabian Frederick4bed2072014-01-25 10:45:08 +0800283 goto err_destroy_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Greg Kroah-Hartmaneadcf0d2008-07-02 12:46:22 -0700285 dev->mtd.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK;
288 dev->mtd.erasesize = erase_size;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400289 dev->mtd.writesize = 1;
Artem Bityutskiyb6043872012-02-03 09:32:44 +0200290 dev->mtd.writebufsize = PAGE_SIZE;
David Woodhouse21c8db92006-06-14 21:39:48 +0100291 dev->mtd.type = MTD_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 dev->mtd.flags = MTD_CAP_RAM;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200293 dev->mtd._erase = block2mtd_erase;
294 dev->mtd._write = block2mtd_write;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200295 dev->mtd._sync = block2mtd_sync;
296 dev->mtd._read = block2mtd_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dev->mtd.priv = dev;
298 dev->mtd.owner = THIS_MODULE;
299
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100300 if (mtd_device_register(&dev->mtd, NULL, 0)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300301 /* Device didn't get added, so free the entry */
Fabian Frederick4bed2072014-01-25 10:45:08 +0800302 goto err_destroy_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 list_add(&dev->list, &blkmtd_device_list);
Joe Perchesa1c06602013-04-19 10:59:35 -0700306 pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
307 dev->mtd.index,
308 dev->mtd.name + strlen("block2mtd: "),
309 dev->mtd.erasesize >> 10, dev->mtd.erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return dev;
311
Fabian Frederick4bed2072014-01-25 10:45:08 +0800312err_destroy_mutex:
313 mutex_destroy(&dev->write_mutex);
314err_free_block2mtd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 block2mtd_free_device(dev);
316 return NULL;
317}
318
319
Joern Engel954c2422006-04-18 21:03:08 -0700320/* This function works similar to reguler strtoul. In addition, it
321 * allows some suffixes for a more human-readable number format:
322 * ki, Ki, kiB, KiB - multiply result with 1024
323 * Mi, MiB - multiply result with 1024^2
324 * Gi, GiB - multiply result with 1024^3
325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int ustrtoul(const char *cp, char **endp, unsigned int base)
327{
328 unsigned long result = simple_strtoul(cp, endp, base);
329 switch (**endp) {
330 case 'G' :
331 result *= 1024;
Gustavo A. R. Silva98473f52018-08-09 11:05:13 -0500332 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case 'M':
334 result *= 1024;
Gustavo A. R. Silva98473f52018-08-09 11:05:13 -0500335 /* fall through */
Joern Engel954c2422006-04-18 21:03:08 -0700336 case 'K':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 case 'k':
338 result *= 1024;
339 /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
Joern Engel954c2422006-04-18 21:03:08 -0700340 if ((*endp)[1] == 'i') {
341 if ((*endp)[2] == 'B')
342 (*endp) += 3;
343 else
344 (*endp) += 2;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 return result;
348}
349
350
Thomas Gleixnercc712292005-03-19 22:40:47 +0000351static int parse_num(size_t *num, const char *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 char *endp;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000354 size_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Thomas Gleixnercc712292005-03-19 22:40:47 +0000356 n = (size_t) ustrtoul(token, &endp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (*endp)
358 return -EINVAL;
359
Thomas Gleixnercc712292005-03-19 22:40:47 +0000360 *num = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
363
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static inline void kill_final_newline(char *str)
366{
367 char *newline = strrchr(str, '\n');
368 if (newline && !newline[1])
369 *newline = 0;
370}
371
372
Ville Hervac4e7fb32006-07-14 00:31:16 +0300373#ifndef MODULE
374static int block2mtd_init_called = 0;
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500375/* 80 for device, 12 for erase size */
376static char block2mtd_paramline[80 + 12];
Ville Hervac4e7fb32006-07-14 00:31:16 +0300377#endif
378
Ville Hervac4e7fb32006-07-14 00:31:16 +0300379static int block2mtd_setup2(const char *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500381 /* 80 for device, 12 for erase size, 80 for name, 8 for timeout */
382 char buf[80 + 12 + 80 + 8];
Jesper Juhla6550e52006-05-14 01:42:25 +0200383 char *str = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 char *token[2];
385 char *name;
Thomas Gleixnercc712292005-03-19 22:40:47 +0000386 size_t erase_size = PAGE_SIZE;
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500387 unsigned long timeout = MTD_DEFAULT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int i, ret;
389
Joe Perchesa1c06602013-04-19 10:59:35 -0700390 if (strnlen(val, sizeof(buf)) >= sizeof(buf)) {
391 pr_err("parameter too long\n");
392 return 0;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 strcpy(str, val);
396 kill_final_newline(str);
397
Jesper Juhla6550e52006-05-14 01:42:25 +0200398 for (i = 0; i < 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 token[i] = strsep(&str, ",");
400
Joe Perchesa1c06602013-04-19 10:59:35 -0700401 if (str) {
402 pr_err("too many arguments\n");
403 return 0;
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Joe Perchesa1c06602013-04-19 10:59:35 -0700406 if (!token[0]) {
407 pr_err("no argument\n");
408 return 0;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Ville Hervac4e7fb32006-07-14 00:31:16 +0300411 name = token[0];
Joe Perchesa1c06602013-04-19 10:59:35 -0700412 if (strlen(name) + 1 > 80) {
413 pr_err("device name too long\n");
414 return 0;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (token[1]) {
Thomas Gleixnercc712292005-03-19 22:40:47 +0000418 ret = parse_num(&erase_size, token[1]);
Jesper Juhla6550e52006-05-14 01:42:25 +0200419 if (ret) {
Joe Perchesa1c06602013-04-19 10:59:35 -0700420 pr_err("illegal erase size\n");
421 return 0;
Jesper Juhla6550e52006-05-14 01:42:25 +0200422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500425 add_device(name, erase_size, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return 0;
428}
429
430
Kees Cooke4dca7b2017-10-17 19:04:42 -0700431static int block2mtd_setup(const char *val, const struct kernel_param *kp)
Ville Hervac4e7fb32006-07-14 00:31:16 +0300432{
433#ifdef MODULE
434 return block2mtd_setup2(val);
435#else
436 /* If more parameters are later passed in via
437 /sys/module/block2mtd/parameters/block2mtd
438 and block2mtd_init() has already been called,
439 we can parse the argument now. */
440
441 if (block2mtd_init_called)
442 return block2mtd_setup2(val);
443
444 /* During early boot stage, we only save the parameters
445 here. We must parse them later: if the param passed
446 from kernel boot command line, block2mtd_setup() is
447 called so early that it is not possible to resolve
448 the device (even kmalloc() fails). Deter that work to
449 block2mtd_setup2(). */
450
451 strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
452
453 return 0;
454#endif
455}
456
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
459MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
460
461static int __init block2mtd_init(void)
462{
Ville Hervac4e7fb32006-07-14 00:31:16 +0300463 int ret = 0;
Ville Hervac4e7fb32006-07-14 00:31:16 +0300464
465#ifndef MODULE
466 if (strlen(block2mtd_paramline))
467 ret = block2mtd_setup2(block2mtd_paramline);
468 block2mtd_init_called = 1;
469#endif
470
471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474
Bill Pemberton810b7e02012-11-19 13:26:04 -0500475static void block2mtd_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct list_head *pos, *next;
478
479 /* Remove the MTD devices */
480 list_for_each_safe(pos, next, &blkmtd_device_list) {
481 struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list);
482 block2mtd_sync(&dev->mtd);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100483 mtd_device_unregister(&dev->mtd);
Fabian Frederick4bed2072014-01-25 10:45:08 +0800484 mutex_destroy(&dev->write_mutex);
Joe Perchesa1c06602013-04-19 10:59:35 -0700485 pr_info("mtd%d: [%s] removed\n",
486 dev->mtd.index,
487 dev->mtd.name + strlen("block2mtd: "));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 list_del(&dev->list);
489 block2mtd_free_device(dev);
490 }
491}
492
Felix Fietkaud6a3f012014-11-09 07:21:13 -0500493late_initcall(block2mtd_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494module_exit(block2mtd_exit);
495
496MODULE_LICENSE("GPL");
Joern Engel2b54aae2008-02-06 01:38:02 -0800497MODULE_AUTHOR("Joern Engel <joern@lazybastard.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498MODULE_DESCRIPTION("Emulate an MTD using a block device");