blob: d172195fbd15fcbd947a2bc0780d0f9044cba2fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2006 Red Hat UK Limited
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/ptrace.h>
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +030027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/major.h>
31#include <linux/fs.h>
Artem Bityutskiy77993082006-10-11 14:52:44 +030032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ioctl.h>
34#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/proc_fs.h>
Ben Hutchingsb520e412010-01-29 20:59:42 +000036#include <linux/idr.h>
Jörn Engela33eb6b2010-04-27 09:40:52 +020037#include <linux/backing-dev.h>
Tejun Heo05d71b462010-03-30 02:52:39 +090038#include <linux/gfp.h>
David Howells0d01ff22013-04-11 23:51:01 +010039#include <linux/slab.h>
Brian Norris3efe41b2014-11-26 01:01:08 -080040#include <linux/reboot.h>
Dan Ehrenberg727dc612015-04-02 15:15:10 -070041#include <linux/kconfig.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/mtd/mtd.h>
Jamie Ilesf5671ab2011-05-23 17:15:46 +010044#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ben Dooks356d70f2007-05-28 20:28:34 +010046#include "mtdcore.h"
Artem Bityutskiy660685d2013-03-14 13:27:40 +020047
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010048static struct backing_dev_info mtd_bdi = {
Jörn Engela33eb6b2010-04-27 09:40:52 +020049};
Ben Dooks356d70f2007-05-28 20:28:34 +010050
David Woodhouse15bce402009-04-05 07:40:58 -070051static int mtd_cls_suspend(struct device *dev, pm_message_t state);
52static int mtd_cls_resume(struct device *dev);
David Brownell1f24b5a2009-03-26 00:42:41 -070053
David Woodhouse15bce402009-04-05 07:40:58 -070054static struct class mtd_class = {
55 .name = "mtd",
56 .owner = THIS_MODULE,
57 .suspend = mtd_cls_suspend,
58 .resume = mtd_cls_resume,
59};
David Brownell1f24b5a2009-03-26 00:42:41 -070060
Ben Hutchingsb520e412010-01-29 20:59:42 +000061static DEFINE_IDR(mtd_idr);
62
Thomas Gleixner97894cd2005-11-07 11:15:26 +000063/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080065DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066EXPORT_SYMBOL_GPL(mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +000067
68struct mtd_info *__mtd_next_device(int i)
69{
70 return idr_get_next(&mtd_idr, &i);
71}
72EXPORT_SYMBOL_GPL(__mtd_next_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static LIST_HEAD(mtd_notifiers);
75
David Brownell1f24b5a2009-03-26 00:42:41 -070076
David Brownell1f24b5a2009-03-26 00:42:41 -070077#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
David Brownell1f24b5a2009-03-26 00:42:41 -070078
79/* REVISIT once MTD uses the driver model better, whoever allocates
80 * the mtd_info will probably want to use the release() hook...
81 */
82static void mtd_release(struct device *dev)
83{
Brian Norris5e472122014-07-21 19:06:47 -070084 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020085 dev_t index = MTD_DEVT(mtd->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070086
Brian Norris5e472122014-07-21 19:06:47 -070087 /* remove /dev/mtdXro node */
88 device_destroy(&mtd_class, index + 1);
David Woodhouse15bce402009-04-05 07:40:58 -070089}
90
91static int mtd_cls_suspend(struct device *dev, pm_message_t state)
92{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020093 struct mtd_info *mtd = dev_get_drvdata(dev);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070094
Artem Bityutskiy1a308712012-01-16 11:07:16 +020095 return mtd ? mtd_suspend(mtd) : 0;
David Woodhouse15bce402009-04-05 07:40:58 -070096}
97
98static int mtd_cls_resume(struct device *dev)
99{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200100 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200101
Brian Norris3ee5014185b2012-01-27 12:39:32 -0800102 if (mtd)
Artem Bityutskiyead995f2011-12-23 19:31:25 +0200103 mtd_resume(mtd);
David Woodhouse15bce402009-04-05 07:40:58 -0700104 return 0;
David Brownell1f24b5a2009-03-26 00:42:41 -0700105}
106
107static ssize_t mtd_type_show(struct device *dev,
108 struct device_attribute *attr, char *buf)
109{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200110 struct mtd_info *mtd = dev_get_drvdata(dev);
David Brownell1f24b5a2009-03-26 00:42:41 -0700111 char *type;
112
113 switch (mtd->type) {
114 case MTD_ABSENT:
115 type = "absent";
116 break;
117 case MTD_RAM:
118 type = "ram";
119 break;
120 case MTD_ROM:
121 type = "rom";
122 break;
123 case MTD_NORFLASH:
124 type = "nor";
125 break;
126 case MTD_NANDFLASH:
127 type = "nand";
128 break;
129 case MTD_DATAFLASH:
130 type = "dataflash";
131 break;
132 case MTD_UBIVOLUME:
133 type = "ubi";
134 break;
Huang Shijief4837242013-09-25 14:58:19 +0800135 case MTD_MLCNANDFLASH:
136 type = "mlc-nand";
137 break;
David Brownell1f24b5a2009-03-26 00:42:41 -0700138 default:
139 type = "unknown";
140 }
141
142 return snprintf(buf, PAGE_SIZE, "%s\n", type);
143}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700144static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
145
146static ssize_t mtd_flags_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200149 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700150
151 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
152
153}
154static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
155
156static ssize_t mtd_size_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200159 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700160
161 return snprintf(buf, PAGE_SIZE, "%llu\n",
162 (unsigned long long)mtd->size);
163
164}
165static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
166
167static ssize_t mtd_erasesize_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
169{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200170 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700171
172 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
173
174}
175static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
176
177static ssize_t mtd_writesize_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
179{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200180 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700181
182 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
183
184}
185static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
186
Artem Bityutskiye7693542009-04-18 12:29:42 +0300187static ssize_t mtd_subpagesize_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200190 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300191 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
192
193 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
194
195}
196static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
197
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700198static ssize_t mtd_oobsize_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
200{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200201 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700202
203 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
204
205}
206static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
207
208static ssize_t mtd_numeraseregions_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200211 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700212
213 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
214
215}
216static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
217 NULL);
218
219static ssize_t mtd_name_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200222 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700223
224 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
225
226}
227static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700228
Mike Dunna9b672e2012-04-25 12:06:07 -0700229static ssize_t mtd_ecc_strength_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct mtd_info *mtd = dev_get_drvdata(dev);
233
234 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
235}
236static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
237
Mike Dunnd062d4e2012-04-25 12:06:08 -0700238static ssize_t mtd_bitflip_threshold_show(struct device *dev,
239 struct device_attribute *attr,
240 char *buf)
241{
242 struct mtd_info *mtd = dev_get_drvdata(dev);
243
244 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
245}
246
247static ssize_t mtd_bitflip_threshold_store(struct device *dev,
248 struct device_attribute *attr,
249 const char *buf, size_t count)
250{
251 struct mtd_info *mtd = dev_get_drvdata(dev);
252 unsigned int bitflip_threshold;
253 int retval;
254
255 retval = kstrtouint(buf, 0, &bitflip_threshold);
256 if (retval)
257 return retval;
258
259 mtd->bitflip_threshold = bitflip_threshold;
260 return count;
261}
262static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
263 mtd_bitflip_threshold_show,
264 mtd_bitflip_threshold_store);
265
Huang Shijiebf977e32013-08-16 10:10:05 +0800266static ssize_t mtd_ecc_step_size_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 struct mtd_info *mtd = dev_get_drvdata(dev);
270
271 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
272
273}
274static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
275
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300276static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct mtd_info *mtd = dev_get_drvdata(dev);
280 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
281
282 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
283}
284static DEVICE_ATTR(corrected_bits, S_IRUGO,
285 mtd_ecc_stats_corrected_show, NULL);
286
287static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
289{
290 struct mtd_info *mtd = dev_get_drvdata(dev);
291 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
292
293 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
294}
295static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
296
297static ssize_t mtd_badblocks_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
299{
300 struct mtd_info *mtd = dev_get_drvdata(dev);
301 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
302
303 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
304}
305static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
306
307static ssize_t mtd_bbtblocks_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
309{
310 struct mtd_info *mtd = dev_get_drvdata(dev);
311 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
312
313 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
314}
315static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
316
David Brownell1f24b5a2009-03-26 00:42:41 -0700317static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700318 &dev_attr_type.attr,
319 &dev_attr_flags.attr,
320 &dev_attr_size.attr,
321 &dev_attr_erasesize.attr,
322 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300323 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700324 &dev_attr_oobsize.attr,
325 &dev_attr_numeraseregions.attr,
326 &dev_attr_name.attr,
Mike Dunna9b672e2012-04-25 12:06:07 -0700327 &dev_attr_ecc_strength.attr,
Huang Shijiebf977e32013-08-16 10:10:05 +0800328 &dev_attr_ecc_step_size.attr,
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300329 &dev_attr_corrected_bits.attr,
330 &dev_attr_ecc_failures.attr,
331 &dev_attr_bad_blocks.attr,
332 &dev_attr_bbt_blocks.attr,
Mike Dunnd062d4e2012-04-25 12:06:08 -0700333 &dev_attr_bitflip_threshold.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700334 NULL,
335};
Axel Lin54c738f2013-12-02 10:12:25 +0800336ATTRIBUTE_GROUPS(mtd);
David Brownell1f24b5a2009-03-26 00:42:41 -0700337
338static struct device_type mtd_devtype = {
339 .name = "mtd",
340 .groups = mtd_groups,
341 .release = mtd_release,
342};
343
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100344#ifndef CONFIG_MMU
345unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
346{
347 switch (mtd->type) {
348 case MTD_RAM:
349 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
350 NOMMU_MAP_READ | NOMMU_MAP_WRITE;
351 case MTD_ROM:
352 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
353 NOMMU_MAP_READ;
354 default:
355 return NOMMU_MAP_COPY;
356 }
357}
Arnd Bergmann706a4e52015-01-28 11:09:20 -0700358EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100359#endif
360
Brian Norris3efe41b2014-11-26 01:01:08 -0800361static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
362 void *cmd)
363{
364 struct mtd_info *mtd;
365
366 mtd = container_of(n, struct mtd_info, reboot_notifier);
367 mtd->_reboot(mtd);
368
369 return NOTIFY_DONE;
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/**
373 * add_mtd_device - register an MTD device
374 * @mtd: pointer to new MTD device info structure
375 *
376 * Add a device to the list of MTD devices present in the system, and
377 * notify each currently active MTD 'user' of its arrival. Returns
378 * zero on success or 1 on failure, which currently will only happen
Ben Hutchingsb520e412010-01-29 20:59:42 +0000379 * if there is insufficient memory or a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
381
382int add_mtd_device(struct mtd_info *mtd)
383{
Ben Hutchingsb520e412010-01-29 20:59:42 +0000384 struct mtd_notifier *not;
385 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100387 mtd->backing_dev_info = &mtd_bdi;
David Howells402d3262009-02-12 10:40:00 +0000388
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400389 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800390 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Tejun Heo589e9c42013-02-27 17:04:33 -0800392 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
393 if (i < 0)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000394 goto fail_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ben Hutchingsb520e412010-01-29 20:59:42 +0000396 mtd->index = i;
397 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000398
Mike Dunnd062d4e2012-04-25 12:06:08 -0700399 /* default value if not set by driver */
400 if (mtd->bitflip_threshold == 0)
401 mtd->bitflip_threshold = mtd->ecc_strength;
402
Ben Hutchingsb520e412010-01-29 20:59:42 +0000403 if (is_power_of_2(mtd->erasesize))
404 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
405 else
406 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000407
Ben Hutchingsb520e412010-01-29 20:59:42 +0000408 if (is_power_of_2(mtd->writesize))
409 mtd->writesize_shift = ffs(mtd->writesize) - 1;
410 else
411 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000412
Ben Hutchingsb520e412010-01-29 20:59:42 +0000413 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
414 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
Håvard Skinnemoen187ef152006-09-22 10:07:08 +0100415
Ben Hutchingsb520e412010-01-29 20:59:42 +0000416 /* Some chips always power up locked. Unlock them now */
Artem Bityutskiy38134562011-12-30 17:00:35 +0200417 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
418 error = mtd_unlock(mtd, 0, mtd->size);
419 if (error && error != -EOPNOTSUPP)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000420 printk(KERN_WARNING
421 "%s: unlock failed, writes may not work\n",
422 mtd->name);
423 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700424
Ben Hutchingsb520e412010-01-29 20:59:42 +0000425 /* Caller should have set dev.parent to match the
426 * physical device.
427 */
428 mtd->dev.type = &mtd_devtype;
429 mtd->dev.class = &mtd_class;
430 mtd->dev.devt = MTD_DEVT(i);
431 dev_set_name(&mtd->dev, "mtd%d", i);
432 dev_set_drvdata(&mtd->dev, mtd);
433 if (device_register(&mtd->dev) != 0)
434 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700435
Brian Norris5e472122014-07-21 19:06:47 -0700436 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
437 "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000438
Brian Norris289c0522011-07-19 10:06:09 -0700439 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000440 /* No need to get a refcount on the module containing
441 the notifier, since we hold the mtd_table_mutex */
442 list_for_each_entry(not, &mtd_notifiers, list)
443 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000444
Ingo Molnar48b19262006-03-31 02:29:41 -0800445 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000446 /* We _know_ we aren't being removed, because
447 our caller is still holding us here. So none
448 of this try_ nonsense, and no bitching about it
449 either. :) */
450 __module_get(THIS_MODULE);
451 return 0;
452
453fail_added:
454 idr_remove(&mtd_idr, i);
455fail_locked:
456 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 1;
458}
459
460/**
461 * del_mtd_device - unregister an MTD device
462 * @mtd: pointer to MTD device info structure
463 *
464 * Remove a device from the list of MTD devices present in the system,
465 * and notify each currently active MTD 'user' of its departure.
466 * Returns zero on success or 1 on failure, which currently will happen
467 * if the requested device does not appear to be present in the list.
468 */
469
Jamie Ileseea72d52011-05-23 10:23:42 +0100470int del_mtd_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int ret;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200473 struct mtd_notifier *not;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000474
Ingo Molnar48b19262006-03-31 02:29:41 -0800475 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Ben Hutchingsb520e412010-01-29 20:59:42 +0000477 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ret = -ENODEV;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200479 goto out_error;
480 }
481
482 /* No need to get a refcount on the module containing
483 the notifier, since we hold the mtd_table_mutex */
484 list_for_each_entry(not, &mtd_notifiers, list)
485 not->remove(mtd);
486
487 if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000488 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 mtd->index, mtd->name, mtd->usecount);
490 ret = -EBUSY;
491 } else {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700492 device_unregister(&mtd->dev);
493
Ben Hutchingsb520e412010-01-29 20:59:42 +0000494 idr_remove(&mtd_idr, mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 module_put(THIS_MODULE);
497 ret = 0;
498 }
499
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200500out_error:
Ingo Molnar48b19262006-03-31 02:29:41 -0800501 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return ret;
503}
504
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700505static int mtd_add_device_partitions(struct mtd_info *mtd,
506 struct mtd_partition *real_parts,
507 int nbparts)
508{
509 int ret;
510
511 if (nbparts == 0 || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
512 ret = add_mtd_device(mtd);
513 if (ret == 1)
514 return -ENODEV;
515 }
516
517 if (nbparts > 0) {
518 ret = add_mtd_partitions(mtd, real_parts, nbparts);
519 if (ret && IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
520 del_mtd_device(mtd);
521 return ret;
522 }
523
524 return 0;
525}
526
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/**
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300529 * mtd_device_parse_register - parse partitions and register an MTD device.
530 *
531 * @mtd: the MTD device to register
532 * @types: the list of MTD partition probes to try, see
533 * 'parse_mtd_partitions()' for more information
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400534 * @parser_data: MTD partition parser-specific data
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300535 * @parts: fallback partition information to register, if parsing fails;
536 * only valid if %nr_parts > %0
537 * @nr_parts: the number of partitions in parts, if zero then the full
538 * MTD device is registered if no partition info is found
539 *
540 * This function aggregates MTD partitions parsing (done by
541 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
542 * basically follows the most common pattern found in many MTD drivers:
543 *
544 * * It first tries to probe partitions on MTD device @mtd using parsers
545 * specified in @types (if @types is %NULL, then the default list of parsers
546 * is used, see 'parse_mtd_partitions()' for more information). If none are
547 * found this functions tries to fallback to information specified in
548 * @parts/@nr_parts.
Brian Norris92394b5c2011-07-20 09:53:42 -0700549 * * If any partitioning info was found, this function registers the found
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700550 * partitions. If the MTD_PARTITIONED_MASTER option is set, then the device
551 * as a whole is registered first.
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300552 * * If no partitions were found this function just registers the MTD device
553 * @mtd and exits.
554 *
555 * Returns zero in case of success and a negative error code in case of failure.
556 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200557int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400558 struct mtd_part_parser_data *parser_data,
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300559 const struct mtd_partition *parts,
560 int nr_parts)
561{
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700562 int ret;
563 struct mtd_partition *real_parts = NULL;
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300564
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700565 ret = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
566 if (ret <= 0 && nr_parts && parts) {
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300567 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
568 GFP_KERNEL);
Jason Liu4d523b602011-08-24 19:26:28 +0800569 if (!real_parts)
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700570 ret = -ENOMEM;
Jason Liu4d523b602011-08-24 19:26:28 +0800571 else
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700572 ret = nr_parts;
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300573 }
574
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700575 if (ret >= 0)
576 ret = mtd_add_device_partitions(mtd, real_parts, ret);
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300577
Niklas Cassele1dd8642015-02-01 02:08:50 +0100578 /*
579 * FIXME: some drivers unfortunately call this function more than once.
580 * So we have to check if we've already assigned the reboot notifier.
581 *
582 * Generally, we can make multiple calls work for most cases, but it
583 * does cause problems with parse_mtd_partitions() above (e.g.,
584 * cmdlineparts will register partitions more than once).
585 */
586 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
Brian Norris3efe41b2014-11-26 01:01:08 -0800587 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
588 register_reboot_notifier(&mtd->reboot_notifier);
589 }
590
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700591 kfree(real_parts);
592 return ret;
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300593}
594EXPORT_SYMBOL_GPL(mtd_device_parse_register);
595
596/**
Jamie Ilesf5671ab2011-05-23 17:15:46 +0100597 * mtd_device_unregister - unregister an existing MTD device.
598 *
599 * @master: the MTD device to unregister. This will unregister both the master
600 * and any partitions if registered.
601 */
602int mtd_device_unregister(struct mtd_info *master)
603{
604 int err;
605
Brian Norris3efe41b2014-11-26 01:01:08 -0800606 if (master->_reboot)
607 unregister_reboot_notifier(&master->reboot_notifier);
608
Jamie Ilesf5671ab2011-05-23 17:15:46 +0100609 err = del_mtd_partitions(master);
610 if (err)
611 return err;
612
613 if (!device_is_registered(&master->dev))
614 return 0;
615
616 return del_mtd_device(master);
617}
618EXPORT_SYMBOL_GPL(mtd_device_unregister);
619
620/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * register_mtd_user - register a 'user' of MTD devices.
622 * @new: pointer to notifier info structure
623 *
624 * Registers a pair of callbacks function to be called upon addition
625 * or removal of MTD devices. Causes the 'add' callback to be immediately
626 * invoked for each MTD device currently present in the system.
627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628void register_mtd_user (struct mtd_notifier *new)
629{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000630 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Ingo Molnar48b19262006-03-31 02:29:41 -0800632 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 list_add(&new->list, &mtd_notifiers);
635
Wanlong Gaod5ca5122011-05-20 21:14:30 +0800636 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000637
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000638 mtd_for_each_device(mtd)
639 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Ingo Molnar48b19262006-03-31 02:29:41 -0800641 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200643EXPORT_SYMBOL_GPL(register_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000646 * unregister_mtd_user - unregister a 'user' of MTD devices.
647 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 *
649 * Removes a callback function pair from the list of 'users' to be
650 * notified upon addition or removal of MTD devices. Causes the
651 * 'remove' callback to be immediately invoked for each MTD device
652 * currently present in the system.
653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654int unregister_mtd_user (struct mtd_notifier *old)
655{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000656 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Ingo Molnar48b19262006-03-31 02:29:41 -0800658 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 module_put(THIS_MODULE);
661
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000662 mtd_for_each_device(mtd)
663 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800666 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 0;
668}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200669EXPORT_SYMBOL_GPL(unregister_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/**
672 * get_mtd_device - obtain a validated handle for an MTD device
673 * @mtd: last known address of the required MTD device
674 * @num: internal device number of the required MTD device
675 *
676 * Given a number and NULL address, return the num'th entry in the device
677 * table, if any. Given an address and num == -1, search the device table
678 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300679 * both, return the num'th driver only if its address matches. Return
680 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
683{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000684 struct mtd_info *ret = NULL, *other;
685 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Ingo Molnar48b19262006-03-31 02:29:41 -0800687 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000690 mtd_for_each_device(other) {
691 if (other == mtd) {
692 ret = mtd;
693 break;
694 }
695 }
Ben Hutchingsb520e412010-01-29 20:59:42 +0000696 } else if (num >= 0) {
697 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (mtd && mtd != ret)
699 ret = NULL;
700 }
701
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200702 if (!ret) {
703 ret = ERR_PTR(err);
704 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200707 err = __get_mtd_device(ret);
708 if (err)
709 ret = ERR_PTR(err);
710out:
Ingo Molnar48b19262006-03-31 02:29:41 -0800711 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200713}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200714EXPORT_SYMBOL_GPL(get_mtd_device);
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300715
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200716
717int __get_mtd_device(struct mtd_info *mtd)
718{
719 int err;
720
721 if (!try_module_get(mtd->owner))
722 return -ENODEV;
723
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200724 if (mtd->_get_device) {
725 err = mtd->_get_device(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200726
727 if (err) {
728 module_put(mtd->owner);
729 return err;
730 }
731 }
732 mtd->usecount++;
733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200735EXPORT_SYMBOL_GPL(__get_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Artem Bityutskiy77993082006-10-11 14:52:44 +0300737/**
738 * get_mtd_device_nm - obtain a validated handle for an MTD device by
739 * device name
740 * @name: MTD device name to open
741 *
742 * This function returns MTD device description structure in case of
743 * success and an error code in case of failure.
744 */
Artem Bityutskiy77993082006-10-11 14:52:44 +0300745struct mtd_info *get_mtd_device_nm(const char *name)
746{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000747 int err = -ENODEV;
748 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300749
750 mutex_lock(&mtd_table_mutex);
751
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000752 mtd_for_each_device(other) {
753 if (!strcmp(name, other->name)) {
754 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300755 break;
756 }
757 }
758
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300759 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300760 goto out_unlock;
761
Wanlong Gao52534f22011-05-17 22:36:18 +0800762 err = __get_mtd_device(mtd);
763 if (err)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300764 goto out_unlock;
765
Artem Bityutskiy77993082006-10-11 14:52:44 +0300766 mutex_unlock(&mtd_table_mutex);
767 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300768
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300769out_unlock:
770 mutex_unlock(&mtd_table_mutex);
771 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300772}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200773EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775void put_mtd_device(struct mtd_info *mtd)
776{
Ingo Molnar48b19262006-03-31 02:29:41 -0800777 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200778 __put_mtd_device(mtd);
779 mutex_unlock(&mtd_table_mutex);
780
781}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200782EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200783
784void __put_mtd_device(struct mtd_info *mtd)
785{
786 --mtd->usecount;
787 BUG_ON(mtd->usecount < 0);
788
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200789 if (mtd->_put_device)
790 mtd->_put_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 module_put(mtd->owner);
793}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200794EXPORT_SYMBOL_GPL(__put_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Artem Bityutskiy52b02032011-12-30 15:57:25 +0200796/*
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200797 * Erase is an asynchronous operation. Device drivers are supposed
798 * to call instr->callback() whenever the operation completes, even
799 * if it completes with a failure.
800 * Callers are supposed to pass a callback function and wait for it
801 * to be called before writing to the block.
802 */
803int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
804{
Brian Norris0c2b4e22014-07-21 19:06:27 -0700805 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200806 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200807 if (!(mtd->flags & MTD_WRITEABLE))
808 return -EROFS;
Shmulik Ladkani3b27dac2012-02-09 15:36:29 +0200809 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200810 if (!instr->len) {
811 instr->state = MTD_ERASE_DONE;
812 mtd_erase_callback(instr);
813 return 0;
814 }
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200815 return mtd->_erase(mtd, instr);
816}
817EXPORT_SYMBOL_GPL(mtd_erase);
818
819/*
820 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
821 */
822int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
823 void **virt, resource_size_t *phys)
824{
825 *retlen = 0;
Artem Bityutskiy0dd52352012-02-08 15:13:26 +0200826 *virt = NULL;
827 if (phys)
828 *phys = 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200829 if (!mtd->_point)
830 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700831 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200832 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200833 if (!len)
834 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200835 return mtd->_point(mtd, from, len, retlen, virt, phys);
836}
837EXPORT_SYMBOL_GPL(mtd_point);
838
839/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
840int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
841{
842 if (!mtd->_point)
843 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700844 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200845 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200846 if (!len)
847 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200848 return mtd->_unpoint(mtd, from, len);
849}
850EXPORT_SYMBOL_GPL(mtd_unpoint);
851
852/*
853 * Allow NOMMU mmap() to directly map the device (if not NULL)
854 * - return the address to which the offset maps
855 * - return -ENOSYS to indicate refusal to do the mapping
856 */
857unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
858 unsigned long offset, unsigned long flags)
859{
860 if (!mtd->_get_unmapped_area)
861 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700862 if (offset >= mtd->size || len > mtd->size - offset)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200863 return -EINVAL;
864 return mtd->_get_unmapped_area(mtd, len, offset, flags);
865}
866EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
867
868int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
869 u_char *buf)
870{
Mike Dunnedbc45402012-04-25 12:06:11 -0700871 int ret_code;
Artem Bityutskiy834247e2012-02-06 12:39:07 +0200872 *retlen = 0;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700873 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200874 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200875 if (!len)
876 return 0;
Mike Dunnedbc45402012-04-25 12:06:11 -0700877
878 /*
879 * In the absence of an error, drivers return a non-negative integer
880 * representing the maximum number of bitflips that were corrected on
881 * any one ecc region (if applicable; zero otherwise).
882 */
883 ret_code = mtd->_read(mtd, from, len, retlen, buf);
884 if (unlikely(ret_code < 0))
885 return ret_code;
886 if (mtd->ecc_strength == 0)
887 return 0; /* device lacks ecc */
888 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200889}
890EXPORT_SYMBOL_GPL(mtd_read);
891
892int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
893 const u_char *buf)
894{
895 *retlen = 0;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700896 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200897 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200898 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
899 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200900 if (!len)
901 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200902 return mtd->_write(mtd, to, len, retlen, buf);
903}
904EXPORT_SYMBOL_GPL(mtd_write);
905
906/*
907 * In blackbox flight recorder like scenarios we want to make successful writes
908 * in interrupt context. panic_write() is only intended to be called when its
909 * known the kernel is about to panic and we need the write to succeed. Since
910 * the kernel is not going to be running for much longer, this function can
911 * break locks and delay to ensure the write succeeds (but not sleep).
912 */
913int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
914 const u_char *buf)
915{
916 *retlen = 0;
917 if (!mtd->_panic_write)
918 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700919 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200920 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200921 if (!(mtd->flags & MTD_WRITEABLE))
922 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200923 if (!len)
924 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200925 return mtd->_panic_write(mtd, to, len, retlen, buf);
926}
927EXPORT_SYMBOL_GPL(mtd_panic_write);
928
Brian Norrisd2d48482012-06-22 16:35:38 -0700929int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
930{
Brian Norrise47f6852012-06-22 16:35:39 -0700931 int ret_code;
Brian Norrisd2d48482012-06-22 16:35:38 -0700932 ops->retlen = ops->oobretlen = 0;
933 if (!mtd->_read_oob)
934 return -EOPNOTSUPP;
Brian Norrise47f6852012-06-22 16:35:39 -0700935 /*
936 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
937 * similar to mtd->_read(), returning a non-negative integer
938 * representing max bitflips. In other cases, mtd->_read_oob() may
939 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
940 */
941 ret_code = mtd->_read_oob(mtd, from, ops);
942 if (unlikely(ret_code < 0))
943 return ret_code;
944 if (mtd->ecc_strength == 0)
945 return 0; /* device lacks ecc */
946 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Brian Norrisd2d48482012-06-22 16:35:38 -0700947}
948EXPORT_SYMBOL_GPL(mtd_read_oob);
949
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200950/*
951 * Method to access the protection register area, present in some flash
952 * devices. The user data is one time programmable but the factory data is read
953 * only.
954 */
Christian Riesch4b78fc42014-01-28 09:29:44 +0100955int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
956 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200957{
958 if (!mtd->_get_fact_prot_info)
959 return -EOPNOTSUPP;
960 if (!len)
961 return 0;
Christian Riesch4b78fc42014-01-28 09:29:44 +0100962 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200963}
964EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
965
966int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
967 size_t *retlen, u_char *buf)
968{
969 *retlen = 0;
970 if (!mtd->_read_fact_prot_reg)
971 return -EOPNOTSUPP;
972 if (!len)
973 return 0;
974 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
975}
976EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
977
Christian Riesch4b78fc42014-01-28 09:29:44 +0100978int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
979 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200980{
981 if (!mtd->_get_user_prot_info)
982 return -EOPNOTSUPP;
983 if (!len)
984 return 0;
Christian Riesch4b78fc42014-01-28 09:29:44 +0100985 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200986}
987EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
988
989int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
990 size_t *retlen, u_char *buf)
991{
992 *retlen = 0;
993 if (!mtd->_read_user_prot_reg)
994 return -EOPNOTSUPP;
995 if (!len)
996 return 0;
997 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
998}
999EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1000
1001int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1002 size_t *retlen, u_char *buf)
1003{
Christian Riesch9a78bc82014-03-06 12:42:37 +01001004 int ret;
1005
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001006 *retlen = 0;
1007 if (!mtd->_write_user_prot_reg)
1008 return -EOPNOTSUPP;
1009 if (!len)
1010 return 0;
Christian Riesch9a78bc82014-03-06 12:42:37 +01001011 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1012 if (ret)
1013 return ret;
1014
1015 /*
1016 * If no data could be written at all, we are out of memory and
1017 * must return -ENOSPC.
1018 */
1019 return (*retlen) ? 0 : -ENOSPC;
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001020}
1021EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1022
1023int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1024{
1025 if (!mtd->_lock_user_prot_reg)
1026 return -EOPNOTSUPP;
1027 if (!len)
1028 return 0;
1029 return mtd->_lock_user_prot_reg(mtd, from, len);
1030}
1031EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1032
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001033/* Chip-supported device locking */
1034int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1035{
1036 if (!mtd->_lock)
1037 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001038 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001039 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001040 if (!len)
1041 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001042 return mtd->_lock(mtd, ofs, len);
1043}
1044EXPORT_SYMBOL_GPL(mtd_lock);
1045
1046int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1047{
1048 if (!mtd->_unlock)
1049 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001050 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001051 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001052 if (!len)
1053 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001054 return mtd->_unlock(mtd, ofs, len);
1055}
1056EXPORT_SYMBOL_GPL(mtd_unlock);
1057
1058int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1059{
1060 if (!mtd->_is_locked)
1061 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001062 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001063 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001064 if (!len)
1065 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001066 return mtd->_is_locked(mtd, ofs, len);
1067}
1068EXPORT_SYMBOL_GPL(mtd_is_locked);
1069
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001070int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001071{
Brian Norris0c2b4e22014-07-21 19:06:27 -07001072 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001073 return -EINVAL;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001074 if (!mtd->_block_isreserved)
1075 return 0;
1076 return mtd->_block_isreserved(mtd, ofs);
1077}
1078EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1079
1080int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1081{
Brian Norris0c2b4e22014-07-21 19:06:27 -07001082 if (ofs < 0 || ofs >= mtd->size)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001083 return -EINVAL;
1084 if (!mtd->_block_isbad)
1085 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001086 return mtd->_block_isbad(mtd, ofs);
1087}
1088EXPORT_SYMBOL_GPL(mtd_block_isbad);
1089
1090int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1091{
1092 if (!mtd->_block_markbad)
1093 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001094 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001095 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001096 if (!(mtd->flags & MTD_WRITEABLE))
1097 return -EROFS;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001098 return mtd->_block_markbad(mtd, ofs);
1099}
1100EXPORT_SYMBOL_GPL(mtd_block_markbad);
1101
1102/*
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001103 * default_mtd_writev - the default writev method
1104 * @mtd: mtd device description object pointer
1105 * @vecs: the vectors to write
1106 * @count: count of vectors in @vecs
1107 * @to: the MTD device offset to write to
1108 * @retlen: on exit contains the count of bytes written to the MTD device.
1109 *
1110 * This function returns zero in case of success and a negative error code in
1111 * case of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 */
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001113static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1114 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 unsigned long i;
1117 size_t totlen = 0, thislen;
1118 int ret = 0;
1119
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001120 for (i = 0; i < count; i++) {
1121 if (!vecs[i].iov_len)
1122 continue;
1123 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1124 vecs[i].iov_base);
1125 totlen += thislen;
1126 if (ret || thislen != vecs[i].iov_len)
1127 break;
1128 to += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001130 *retlen = totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return ret;
1132}
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001133
1134/*
1135 * mtd_writev - the vector-based MTD write method
1136 * @mtd: mtd device description object pointer
1137 * @vecs: the vectors to write
1138 * @count: count of vectors in @vecs
1139 * @to: the MTD device offset to write to
1140 * @retlen: on exit contains the count of bytes written to the MTD device.
1141 *
1142 * This function returns zero in case of success and a negative error code in
1143 * case of failure.
1144 */
1145int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1146 unsigned long count, loff_t to, size_t *retlen)
1147{
1148 *retlen = 0;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001149 if (!(mtd->flags & MTD_WRITEABLE))
1150 return -EROFS;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001151 if (!mtd->_writev)
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001152 return default_mtd_writev(mtd, vecs, count, to, retlen);
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001153 return mtd->_writev(mtd, vecs, count, to, retlen);
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001154}
1155EXPORT_SYMBOL_GPL(mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Grant Erickson33b53712011-04-08 08:51:32 -07001157/**
1158 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001159 * @mtd: mtd device description object pointer
1160 * @size: a pointer to the ideal or maximum size of the allocation, points
Grant Erickson33b53712011-04-08 08:51:32 -07001161 * to the actual allocation size on success.
1162 *
1163 * This routine attempts to allocate a contiguous kernel buffer up to
1164 * the specified size, backing off the size of the request exponentially
1165 * until the request succeeds or until the allocation size falls below
1166 * the system page size. This attempts to make sure it does not adversely
1167 * impact system performance, so when allocating more than one page, we
Linus Torvaldscaf49192012-12-10 10:51:16 -08001168 * ask the memory allocator to avoid re-trying, swapping, writing back
1169 * or performing I/O.
Grant Erickson33b53712011-04-08 08:51:32 -07001170 *
1171 * Note, this function also makes sure that the allocated buffer is aligned to
1172 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1173 *
1174 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1175 * to handle smaller (i.e. degraded) buffer allocations under low- or
1176 * fragmented-memory situations where such reduced allocations, from a
1177 * requested ideal, are allowed.
1178 *
1179 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1180 */
1181void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1182{
Linus Torvaldscaf49192012-12-10 10:51:16 -08001183 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1184 __GFP_NORETRY | __GFP_NO_KSWAPD;
Grant Erickson33b53712011-04-08 08:51:32 -07001185 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1186 void *kbuf;
1187
1188 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1189
1190 while (*size > min_alloc) {
1191 kbuf = kmalloc(*size, flags);
1192 if (kbuf)
1193 return kbuf;
1194
1195 *size >>= 1;
1196 *size = ALIGN(*size, mtd->writesize);
1197 }
1198
1199 /*
1200 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1201 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1202 */
1203 return kmalloc(*size, GFP_KERNEL);
1204}
Grant Erickson33b53712011-04-08 08:51:32 -07001205EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Pavel Machek2d2dce02006-03-31 02:29:49 -08001207#ifdef CONFIG_PROC_FS
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/* Support for /proc/mtd */
1211
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001212static int mtd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001214 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001216 seq_puts(m, "dev: size erasesize name\n");
Ingo Molnar48b19262006-03-31 02:29:41 -08001217 mutex_lock(&mtd_table_mutex);
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001218 mtd_for_each_device(mtd) {
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001219 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1220 mtd->index, (unsigned long long)mtd->size,
1221 mtd->erasesize, mtd->name);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001222 }
Ingo Molnar48b19262006-03-31 02:29:41 -08001223 mutex_unlock(&mtd_table_mutex);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001227static int mtd_proc_open(struct inode *inode, struct file *file)
1228{
1229 return single_open(file, mtd_proc_show, NULL);
1230}
1231
1232static const struct file_operations mtd_proc_ops = {
1233 .open = mtd_proc_open,
1234 .read = seq_read,
1235 .llseek = seq_lseek,
1236 .release = single_release,
1237};
Kevin Cernekee45b09072009-04-04 11:03:04 -07001238#endif /* CONFIG_PROC_FS */
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/*====================================================================*/
1241/* Init code */
1242
Jens Axboe0661b1a2010-04-27 09:49:47 +02001243static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1244{
1245 int ret;
1246
1247 ret = bdi_init(bdi);
1248 if (!ret)
Kees Cook02aa2a32013-07-03 15:04:56 -07001249 ret = bdi_register(bdi, NULL, "%s", name);
Jens Axboe0661b1a2010-04-27 09:49:47 +02001250
1251 if (ret)
1252 bdi_destroy(bdi);
1253
1254 return ret;
1255}
1256
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001257static struct proc_dir_entry *proc_mtd;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static int __init init_mtd(void)
1260{
David Woodhouse15bce402009-04-05 07:40:58 -07001261 int ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -07001262
Jens Axboe0661b1a2010-04-27 09:49:47 +02001263 ret = class_register(&mtd_class);
1264 if (ret)
1265 goto err_reg;
1266
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001267 ret = mtd_bdi_init(&mtd_bdi, "mtd");
Jens Axboe0661b1a2010-04-27 09:49:47 +02001268 if (ret)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001269 goto err_bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02001270
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001271 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001272
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001273 ret = init_mtdchar();
1274 if (ret)
1275 goto out_procfs;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02001278
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001279out_procfs:
1280 if (proc_mtd)
1281 remove_proc_entry("mtd", NULL);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001282err_bdi:
Jens Axboe0661b1a2010-04-27 09:49:47 +02001283 class_unregister(&mtd_class);
1284err_reg:
1285 pr_err("Error registering mtd class or bdi: %d\n", ret);
1286 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289static void __exit cleanup_mtd(void)
1290{
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001291 cleanup_mtdchar();
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001292 if (proc_mtd)
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001293 remove_proc_entry("mtd", NULL);
David Woodhouse15bce402009-04-05 07:40:58 -07001294 class_unregister(&mtd_class);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001295 bdi_destroy(&mtd_bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298module_init(init_mtd);
1299module_exit(cleanup_mtd);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301MODULE_LICENSE("GPL");
1302MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1303MODULE_DESCRIPTION("Core MTD registration and access routines");