xref: /openbmc/linux/drivers/mtd/mtdcore.c (revision 5a170e9e)
1 /*
2  * Core registration and callback routines for MTD
3  * drivers and users.
4  *
5  * 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  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/ptrace.h>
27 #include <linux/seq_file.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/major.h>
31 #include <linux/fs.h>
32 #include <linux/err.h>
33 #include <linux/ioctl.h>
34 #include <linux/init.h>
35 #include <linux/of.h>
36 #include <linux/proc_fs.h>
37 #include <linux/idr.h>
38 #include <linux/backing-dev.h>
39 #include <linux/gfp.h>
40 #include <linux/slab.h>
41 #include <linux/reboot.h>
42 #include <linux/leds.h>
43 #include <linux/debugfs.h>
44 #include <linux/nvmem-provider.h>
45 
46 #include <linux/mtd/mtd.h>
47 #include <linux/mtd/partitions.h>
48 
49 #include "mtdcore.h"
50 
51 struct backing_dev_info *mtd_bdi;
52 
53 #ifdef CONFIG_PM_SLEEP
54 
55 static int mtd_cls_suspend(struct device *dev)
56 {
57 	struct mtd_info *mtd = dev_get_drvdata(dev);
58 
59 	return mtd ? mtd_suspend(mtd) : 0;
60 }
61 
62 static int mtd_cls_resume(struct device *dev)
63 {
64 	struct mtd_info *mtd = dev_get_drvdata(dev);
65 
66 	if (mtd)
67 		mtd_resume(mtd);
68 	return 0;
69 }
70 
71 static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume);
72 #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops)
73 #else
74 #define MTD_CLS_PM_OPS NULL
75 #endif
76 
77 static struct class mtd_class = {
78 	.name = "mtd",
79 	.owner = THIS_MODULE,
80 	.pm = MTD_CLS_PM_OPS,
81 };
82 
83 static DEFINE_IDR(mtd_idr);
84 
85 /* These are exported solely for the purpose of mtd_blkdevs.c. You
86    should not use them for _anything_ else */
87 DEFINE_MUTEX(mtd_table_mutex);
88 EXPORT_SYMBOL_GPL(mtd_table_mutex);
89 
90 struct mtd_info *__mtd_next_device(int i)
91 {
92 	return idr_get_next(&mtd_idr, &i);
93 }
94 EXPORT_SYMBOL_GPL(__mtd_next_device);
95 
96 static LIST_HEAD(mtd_notifiers);
97 
98 
99 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
100 
101 /* REVISIT once MTD uses the driver model better, whoever allocates
102  * the mtd_info will probably want to use the release() hook...
103  */
104 static void mtd_release(struct device *dev)
105 {
106 	struct mtd_info *mtd = dev_get_drvdata(dev);
107 	dev_t index = MTD_DEVT(mtd->index);
108 
109 	/* remove /dev/mtdXro node */
110 	device_destroy(&mtd_class, index + 1);
111 }
112 
113 static ssize_t mtd_type_show(struct device *dev,
114 		struct device_attribute *attr, char *buf)
115 {
116 	struct mtd_info *mtd = dev_get_drvdata(dev);
117 	char *type;
118 
119 	switch (mtd->type) {
120 	case MTD_ABSENT:
121 		type = "absent";
122 		break;
123 	case MTD_RAM:
124 		type = "ram";
125 		break;
126 	case MTD_ROM:
127 		type = "rom";
128 		break;
129 	case MTD_NORFLASH:
130 		type = "nor";
131 		break;
132 	case MTD_NANDFLASH:
133 		type = "nand";
134 		break;
135 	case MTD_DATAFLASH:
136 		type = "dataflash";
137 		break;
138 	case MTD_UBIVOLUME:
139 		type = "ubi";
140 		break;
141 	case MTD_MLCNANDFLASH:
142 		type = "mlc-nand";
143 		break;
144 	default:
145 		type = "unknown";
146 	}
147 
148 	return snprintf(buf, PAGE_SIZE, "%s\n", type);
149 }
150 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
151 
152 static ssize_t mtd_flags_show(struct device *dev,
153 		struct device_attribute *attr, char *buf)
154 {
155 	struct mtd_info *mtd = dev_get_drvdata(dev);
156 
157 	return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
158 
159 }
160 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
161 
162 static ssize_t mtd_size_show(struct device *dev,
163 		struct device_attribute *attr, char *buf)
164 {
165 	struct mtd_info *mtd = dev_get_drvdata(dev);
166 
167 	return snprintf(buf, PAGE_SIZE, "%llu\n",
168 		(unsigned long long)mtd->size);
169 
170 }
171 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
172 
173 static ssize_t mtd_erasesize_show(struct device *dev,
174 		struct device_attribute *attr, char *buf)
175 {
176 	struct mtd_info *mtd = dev_get_drvdata(dev);
177 
178 	return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
179 
180 }
181 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
182 
183 static ssize_t mtd_writesize_show(struct device *dev,
184 		struct device_attribute *attr, char *buf)
185 {
186 	struct mtd_info *mtd = dev_get_drvdata(dev);
187 
188 	return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
189 
190 }
191 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
192 
193 static ssize_t mtd_subpagesize_show(struct device *dev,
194 		struct device_attribute *attr, char *buf)
195 {
196 	struct mtd_info *mtd = dev_get_drvdata(dev);
197 	unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
198 
199 	return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
200 
201 }
202 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
203 
204 static ssize_t mtd_oobsize_show(struct device *dev,
205 		struct device_attribute *attr, char *buf)
206 {
207 	struct mtd_info *mtd = dev_get_drvdata(dev);
208 
209 	return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
210 
211 }
212 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
213 
214 static ssize_t mtd_oobavail_show(struct device *dev,
215 				 struct device_attribute *attr, char *buf)
216 {
217 	struct mtd_info *mtd = dev_get_drvdata(dev);
218 
219 	return snprintf(buf, PAGE_SIZE, "%u\n", mtd->oobavail);
220 }
221 static DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL);
222 
223 static ssize_t mtd_numeraseregions_show(struct device *dev,
224 		struct device_attribute *attr, char *buf)
225 {
226 	struct mtd_info *mtd = dev_get_drvdata(dev);
227 
228 	return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
229 
230 }
231 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
232 	NULL);
233 
234 static ssize_t mtd_name_show(struct device *dev,
235 		struct device_attribute *attr, char *buf)
236 {
237 	struct mtd_info *mtd = dev_get_drvdata(dev);
238 
239 	return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
240 
241 }
242 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
243 
244 static ssize_t mtd_ecc_strength_show(struct device *dev,
245 				     struct device_attribute *attr, char *buf)
246 {
247 	struct mtd_info *mtd = dev_get_drvdata(dev);
248 
249 	return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
250 }
251 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
252 
253 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
254 					  struct device_attribute *attr,
255 					  char *buf)
256 {
257 	struct mtd_info *mtd = dev_get_drvdata(dev);
258 
259 	return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
260 }
261 
262 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
263 					   struct device_attribute *attr,
264 					   const char *buf, size_t count)
265 {
266 	struct mtd_info *mtd = dev_get_drvdata(dev);
267 	unsigned int bitflip_threshold;
268 	int retval;
269 
270 	retval = kstrtouint(buf, 0, &bitflip_threshold);
271 	if (retval)
272 		return retval;
273 
274 	mtd->bitflip_threshold = bitflip_threshold;
275 	return count;
276 }
277 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
278 		   mtd_bitflip_threshold_show,
279 		   mtd_bitflip_threshold_store);
280 
281 static ssize_t mtd_ecc_step_size_show(struct device *dev,
282 		struct device_attribute *attr, char *buf)
283 {
284 	struct mtd_info *mtd = dev_get_drvdata(dev);
285 
286 	return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
287 
288 }
289 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
290 
291 static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
292 		struct device_attribute *attr, char *buf)
293 {
294 	struct mtd_info *mtd = dev_get_drvdata(dev);
295 	struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
296 
297 	return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
298 }
299 static DEVICE_ATTR(corrected_bits, S_IRUGO,
300 		   mtd_ecc_stats_corrected_show, NULL);
301 
302 static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
303 		struct device_attribute *attr, char *buf)
304 {
305 	struct mtd_info *mtd = dev_get_drvdata(dev);
306 	struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
307 
308 	return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
309 }
310 static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
311 
312 static ssize_t mtd_badblocks_show(struct device *dev,
313 		struct device_attribute *attr, char *buf)
314 {
315 	struct mtd_info *mtd = dev_get_drvdata(dev);
316 	struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
317 
318 	return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
319 }
320 static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
321 
322 static ssize_t mtd_bbtblocks_show(struct device *dev,
323 		struct device_attribute *attr, char *buf)
324 {
325 	struct mtd_info *mtd = dev_get_drvdata(dev);
326 	struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
327 
328 	return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
329 }
330 static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
331 
332 static struct attribute *mtd_attrs[] = {
333 	&dev_attr_type.attr,
334 	&dev_attr_flags.attr,
335 	&dev_attr_size.attr,
336 	&dev_attr_erasesize.attr,
337 	&dev_attr_writesize.attr,
338 	&dev_attr_subpagesize.attr,
339 	&dev_attr_oobsize.attr,
340 	&dev_attr_oobavail.attr,
341 	&dev_attr_numeraseregions.attr,
342 	&dev_attr_name.attr,
343 	&dev_attr_ecc_strength.attr,
344 	&dev_attr_ecc_step_size.attr,
345 	&dev_attr_corrected_bits.attr,
346 	&dev_attr_ecc_failures.attr,
347 	&dev_attr_bad_blocks.attr,
348 	&dev_attr_bbt_blocks.attr,
349 	&dev_attr_bitflip_threshold.attr,
350 	NULL,
351 };
352 ATTRIBUTE_GROUPS(mtd);
353 
354 static const struct device_type mtd_devtype = {
355 	.name		= "mtd",
356 	.groups		= mtd_groups,
357 	.release	= mtd_release,
358 };
359 
360 #ifndef CONFIG_MMU
361 unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
362 {
363 	switch (mtd->type) {
364 	case MTD_RAM:
365 		return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
366 			NOMMU_MAP_READ | NOMMU_MAP_WRITE;
367 	case MTD_ROM:
368 		return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
369 			NOMMU_MAP_READ;
370 	default:
371 		return NOMMU_MAP_COPY;
372 	}
373 }
374 EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
375 #endif
376 
377 static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
378 			       void *cmd)
379 {
380 	struct mtd_info *mtd;
381 
382 	mtd = container_of(n, struct mtd_info, reboot_notifier);
383 	mtd->_reboot(mtd);
384 
385 	return NOTIFY_DONE;
386 }
387 
388 /**
389  * mtd_wunit_to_pairing_info - get pairing information of a wunit
390  * @mtd: pointer to new MTD device info structure
391  * @wunit: write unit we are interested in
392  * @info: returned pairing information
393  *
394  * Retrieve pairing information associated to the wunit.
395  * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
396  * paired together, and where programming a page may influence the page it is
397  * paired with.
398  * The notion of page is replaced by the term wunit (write-unit) to stay
399  * consistent with the ->writesize field.
400  *
401  * The @wunit argument can be extracted from an absolute offset using
402  * mtd_offset_to_wunit(). @info is filled with the pairing information attached
403  * to @wunit.
404  *
405  * From the pairing info the MTD user can find all the wunits paired with
406  * @wunit using the following loop:
407  *
408  * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
409  *	info.pair = i;
410  *	mtd_pairing_info_to_wunit(mtd, &info);
411  *	...
412  * }
413  */
414 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
415 			      struct mtd_pairing_info *info)
416 {
417 	int npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
418 
419 	if (wunit < 0 || wunit >= npairs)
420 		return -EINVAL;
421 
422 	if (mtd->pairing && mtd->pairing->get_info)
423 		return mtd->pairing->get_info(mtd, wunit, info);
424 
425 	info->group = 0;
426 	info->pair = wunit;
427 
428 	return 0;
429 }
430 EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info);
431 
432 /**
433  * mtd_pairing_info_to_wunit - get wunit from pairing information
434  * @mtd: pointer to new MTD device info structure
435  * @info: pairing information struct
436  *
437  * Returns a positive number representing the wunit associated to the info
438  * struct, or a negative error code.
439  *
440  * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
441  * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
442  * doc).
443  *
444  * It can also be used to only program the first page of each pair (i.e.
445  * page attached to group 0), which allows one to use an MLC NAND in
446  * software-emulated SLC mode:
447  *
448  * info.group = 0;
449  * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
450  * for (info.pair = 0; info.pair < npairs; info.pair++) {
451  *	wunit = mtd_pairing_info_to_wunit(mtd, &info);
452  *	mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
453  *		  mtd->writesize, &retlen, buf + (i * mtd->writesize));
454  * }
455  */
456 int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
457 			      const struct mtd_pairing_info *info)
458 {
459 	int ngroups = mtd_pairing_groups(mtd);
460 	int npairs = mtd_wunit_per_eb(mtd) / ngroups;
461 
462 	if (!info || info->pair < 0 || info->pair >= npairs ||
463 	    info->group < 0 || info->group >= ngroups)
464 		return -EINVAL;
465 
466 	if (mtd->pairing && mtd->pairing->get_wunit)
467 		return mtd->pairing->get_wunit(mtd, info);
468 
469 	return info->pair;
470 }
471 EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit);
472 
473 /**
474  * mtd_pairing_groups - get the number of pairing groups
475  * @mtd: pointer to new MTD device info structure
476  *
477  * Returns the number of pairing groups.
478  *
479  * This number is usually equal to the number of bits exposed by a single
480  * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
481  * to iterate over all pages of a given pair.
482  */
483 int mtd_pairing_groups(struct mtd_info *mtd)
484 {
485 	if (!mtd->pairing || !mtd->pairing->ngroups)
486 		return 1;
487 
488 	return mtd->pairing->ngroups;
489 }
490 EXPORT_SYMBOL_GPL(mtd_pairing_groups);
491 
492 static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
493 			      void *val, size_t bytes)
494 {
495 	struct mtd_info *mtd = priv;
496 	size_t retlen;
497 	int err;
498 
499 	err = mtd_read(mtd, offset, bytes, &retlen, val);
500 	if (err && err != -EUCLEAN)
501 		return err;
502 
503 	return retlen == bytes ? 0 : -EIO;
504 }
505 
506 static int mtd_nvmem_add(struct mtd_info *mtd)
507 {
508 	struct nvmem_config config = {};
509 
510 	config.dev = &mtd->dev;
511 	config.name = mtd->name;
512 	config.owner = THIS_MODULE;
513 	config.reg_read = mtd_nvmem_reg_read;
514 	config.size = mtd->size;
515 	config.word_size = 1;
516 	config.stride = 1;
517 	config.read_only = true;
518 	config.root_only = true;
519 	config.no_of_node = true;
520 	config.priv = mtd;
521 
522 	mtd->nvmem = nvmem_register(&config);
523 	if (IS_ERR(mtd->nvmem)) {
524 		/* Just ignore if there is no NVMEM support in the kernel */
525 		if (PTR_ERR(mtd->nvmem) == -ENOSYS) {
526 			mtd->nvmem = NULL;
527 		} else {
528 			dev_err(&mtd->dev, "Failed to register NVMEM device\n");
529 			return PTR_ERR(mtd->nvmem);
530 		}
531 	}
532 
533 	return 0;
534 }
535 
536 static struct dentry *dfs_dir_mtd;
537 
538 /**
539  *	add_mtd_device - register an MTD device
540  *	@mtd: pointer to new MTD device info structure
541  *
542  *	Add a device to the list of MTD devices present in the system, and
543  *	notify each currently active MTD 'user' of its arrival. Returns
544  *	zero on success or non-zero on failure.
545  */
546 
547 int add_mtd_device(struct mtd_info *mtd)
548 {
549 	struct mtd_notifier *not;
550 	int i, error;
551 
552 	/*
553 	 * May occur, for instance, on buggy drivers which call
554 	 * mtd_device_parse_register() multiple times on the same master MTD,
555 	 * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
556 	 */
557 	if (WARN_ONCE(mtd->dev.type, "MTD already registered\n"))
558 		return -EEXIST;
559 
560 	BUG_ON(mtd->writesize == 0);
561 
562 	if (WARN_ON((!mtd->erasesize || !mtd->_erase) &&
563 		    !(mtd->flags & MTD_NO_ERASE)))
564 		return -EINVAL;
565 
566 	mutex_lock(&mtd_table_mutex);
567 
568 	i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
569 	if (i < 0) {
570 		error = i;
571 		goto fail_locked;
572 	}
573 
574 	mtd->index = i;
575 	mtd->usecount = 0;
576 
577 	/* default value if not set by driver */
578 	if (mtd->bitflip_threshold == 0)
579 		mtd->bitflip_threshold = mtd->ecc_strength;
580 
581 	if (is_power_of_2(mtd->erasesize))
582 		mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
583 	else
584 		mtd->erasesize_shift = 0;
585 
586 	if (is_power_of_2(mtd->writesize))
587 		mtd->writesize_shift = ffs(mtd->writesize) - 1;
588 	else
589 		mtd->writesize_shift = 0;
590 
591 	mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
592 	mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
593 
594 	/* Some chips always power up locked. Unlock them now */
595 	if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
596 		error = mtd_unlock(mtd, 0, mtd->size);
597 		if (error && error != -EOPNOTSUPP)
598 			printk(KERN_WARNING
599 			       "%s: unlock failed, writes may not work\n",
600 			       mtd->name);
601 		/* Ignore unlock failures? */
602 		error = 0;
603 	}
604 
605 	/* Caller should have set dev.parent to match the
606 	 * physical device, if appropriate.
607 	 */
608 	mtd->dev.type = &mtd_devtype;
609 	mtd->dev.class = &mtd_class;
610 	mtd->dev.devt = MTD_DEVT(i);
611 	dev_set_name(&mtd->dev, "mtd%d", i);
612 	dev_set_drvdata(&mtd->dev, mtd);
613 	of_node_get(mtd_get_of_node(mtd));
614 	error = device_register(&mtd->dev);
615 	if (error)
616 		goto fail_added;
617 
618 	/* Add the nvmem provider */
619 	error = mtd_nvmem_add(mtd);
620 	if (error)
621 		goto fail_nvmem_add;
622 
623 	if (!IS_ERR_OR_NULL(dfs_dir_mtd)) {
624 		mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(&mtd->dev), dfs_dir_mtd);
625 		if (IS_ERR_OR_NULL(mtd->dbg.dfs_dir)) {
626 			pr_debug("mtd device %s won't show data in debugfs\n",
627 				 dev_name(&mtd->dev));
628 		}
629 	}
630 
631 	device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
632 		      "mtd%dro", i);
633 
634 	pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
635 	/* No need to get a refcount on the module containing
636 	   the notifier, since we hold the mtd_table_mutex */
637 	list_for_each_entry(not, &mtd_notifiers, list)
638 		not->add(mtd);
639 
640 	mutex_unlock(&mtd_table_mutex);
641 	/* We _know_ we aren't being removed, because
642 	   our caller is still holding us here. So none
643 	   of this try_ nonsense, and no bitching about it
644 	   either. :) */
645 	__module_get(THIS_MODULE);
646 	return 0;
647 
648 fail_nvmem_add:
649 	device_unregister(&mtd->dev);
650 fail_added:
651 	of_node_put(mtd_get_of_node(mtd));
652 	idr_remove(&mtd_idr, i);
653 fail_locked:
654 	mutex_unlock(&mtd_table_mutex);
655 	return error;
656 }
657 
658 /**
659  *	del_mtd_device - unregister an MTD device
660  *	@mtd: pointer to MTD device info structure
661  *
662  *	Remove a device from the list of MTD devices present in the system,
663  *	and notify each currently active MTD 'user' of its departure.
664  *	Returns zero on success or 1 on failure, which currently will happen
665  *	if the requested device does not appear to be present in the list.
666  */
667 
668 int del_mtd_device(struct mtd_info *mtd)
669 {
670 	int ret;
671 	struct mtd_notifier *not;
672 
673 	mutex_lock(&mtd_table_mutex);
674 
675 	debugfs_remove_recursive(mtd->dbg.dfs_dir);
676 
677 	if (idr_find(&mtd_idr, mtd->index) != mtd) {
678 		ret = -ENODEV;
679 		goto out_error;
680 	}
681 
682 	/* No need to get a refcount on the module containing
683 		the notifier, since we hold the mtd_table_mutex */
684 	list_for_each_entry(not, &mtd_notifiers, list)
685 		not->remove(mtd);
686 
687 	if (mtd->usecount) {
688 		printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
689 		       mtd->index, mtd->name, mtd->usecount);
690 		ret = -EBUSY;
691 	} else {
692 		/* Try to remove the NVMEM provider */
693 		if (mtd->nvmem)
694 			nvmem_unregister(mtd->nvmem);
695 
696 		device_unregister(&mtd->dev);
697 
698 		idr_remove(&mtd_idr, mtd->index);
699 		of_node_put(mtd_get_of_node(mtd));
700 
701 		module_put(THIS_MODULE);
702 		ret = 0;
703 	}
704 
705 out_error:
706 	mutex_unlock(&mtd_table_mutex);
707 	return ret;
708 }
709 
710 /*
711  * Set a few defaults based on the parent devices, if not provided by the
712  * driver
713  */
714 static void mtd_set_dev_defaults(struct mtd_info *mtd)
715 {
716 	if (mtd->dev.parent) {
717 		if (!mtd->owner && mtd->dev.parent->driver)
718 			mtd->owner = mtd->dev.parent->driver->owner;
719 		if (!mtd->name)
720 			mtd->name = dev_name(mtd->dev.parent);
721 	} else {
722 		pr_debug("mtd device won't show a device symlink in sysfs\n");
723 	}
724 
725 	mtd->orig_flags = mtd->flags;
726 }
727 
728 /**
729  * mtd_device_parse_register - parse partitions and register an MTD device.
730  *
731  * @mtd: the MTD device to register
732  * @types: the list of MTD partition probes to try, see
733  *         'parse_mtd_partitions()' for more information
734  * @parser_data: MTD partition parser-specific data
735  * @parts: fallback partition information to register, if parsing fails;
736  *         only valid if %nr_parts > %0
737  * @nr_parts: the number of partitions in parts, if zero then the full
738  *            MTD device is registered if no partition info is found
739  *
740  * This function aggregates MTD partitions parsing (done by
741  * 'parse_mtd_partitions()') and MTD device and partitions registering. It
742  * basically follows the most common pattern found in many MTD drivers:
743  *
744  * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
745  *   registered first.
746  * * Then It tries to probe partitions on MTD device @mtd using parsers
747  *   specified in @types (if @types is %NULL, then the default list of parsers
748  *   is used, see 'parse_mtd_partitions()' for more information). If none are
749  *   found this functions tries to fallback to information specified in
750  *   @parts/@nr_parts.
751  * * If no partitions were found this function just registers the MTD device
752  *   @mtd and exits.
753  *
754  * Returns zero in case of success and a negative error code in case of failure.
755  */
756 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
757 			      struct mtd_part_parser_data *parser_data,
758 			      const struct mtd_partition *parts,
759 			      int nr_parts)
760 {
761 	int ret;
762 
763 	mtd_set_dev_defaults(mtd);
764 
765 	if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
766 		ret = add_mtd_device(mtd);
767 		if (ret)
768 			return ret;
769 	}
770 
771 	/* Prefer parsed partitions over driver-provided fallback */
772 	ret = parse_mtd_partitions(mtd, types, parser_data);
773 	if (ret > 0)
774 		ret = 0;
775 	else if (nr_parts)
776 		ret = add_mtd_partitions(mtd, parts, nr_parts);
777 	else if (!device_is_registered(&mtd->dev))
778 		ret = add_mtd_device(mtd);
779 	else
780 		ret = 0;
781 
782 	if (ret)
783 		goto out;
784 
785 	/*
786 	 * FIXME: some drivers unfortunately call this function more than once.
787 	 * So we have to check if we've already assigned the reboot notifier.
788 	 *
789 	 * Generally, we can make multiple calls work for most cases, but it
790 	 * does cause problems with parse_mtd_partitions() above (e.g.,
791 	 * cmdlineparts will register partitions more than once).
792 	 */
793 	WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
794 		  "MTD already registered\n");
795 	if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
796 		mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
797 		register_reboot_notifier(&mtd->reboot_notifier);
798 	}
799 
800 out:
801 	if (ret && device_is_registered(&mtd->dev))
802 		del_mtd_device(mtd);
803 
804 	return ret;
805 }
806 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
807 
808 /**
809  * mtd_device_unregister - unregister an existing MTD device.
810  *
811  * @master: the MTD device to unregister.  This will unregister both the master
812  *          and any partitions if registered.
813  */
814 int mtd_device_unregister(struct mtd_info *master)
815 {
816 	int err;
817 
818 	if (master->_reboot)
819 		unregister_reboot_notifier(&master->reboot_notifier);
820 
821 	err = del_mtd_partitions(master);
822 	if (err)
823 		return err;
824 
825 	if (!device_is_registered(&master->dev))
826 		return 0;
827 
828 	return del_mtd_device(master);
829 }
830 EXPORT_SYMBOL_GPL(mtd_device_unregister);
831 
832 /**
833  *	register_mtd_user - register a 'user' of MTD devices.
834  *	@new: pointer to notifier info structure
835  *
836  *	Registers a pair of callbacks function to be called upon addition
837  *	or removal of MTD devices. Causes the 'add' callback to be immediately
838  *	invoked for each MTD device currently present in the system.
839  */
840 void register_mtd_user (struct mtd_notifier *new)
841 {
842 	struct mtd_info *mtd;
843 
844 	mutex_lock(&mtd_table_mutex);
845 
846 	list_add(&new->list, &mtd_notifiers);
847 
848 	__module_get(THIS_MODULE);
849 
850 	mtd_for_each_device(mtd)
851 		new->add(mtd);
852 
853 	mutex_unlock(&mtd_table_mutex);
854 }
855 EXPORT_SYMBOL_GPL(register_mtd_user);
856 
857 /**
858  *	unregister_mtd_user - unregister a 'user' of MTD devices.
859  *	@old: pointer to notifier info structure
860  *
861  *	Removes a callback function pair from the list of 'users' to be
862  *	notified upon addition or removal of MTD devices. Causes the
863  *	'remove' callback to be immediately invoked for each MTD device
864  *	currently present in the system.
865  */
866 int unregister_mtd_user (struct mtd_notifier *old)
867 {
868 	struct mtd_info *mtd;
869 
870 	mutex_lock(&mtd_table_mutex);
871 
872 	module_put(THIS_MODULE);
873 
874 	mtd_for_each_device(mtd)
875 		old->remove(mtd);
876 
877 	list_del(&old->list);
878 	mutex_unlock(&mtd_table_mutex);
879 	return 0;
880 }
881 EXPORT_SYMBOL_GPL(unregister_mtd_user);
882 
883 /**
884  *	get_mtd_device - obtain a validated handle for an MTD device
885  *	@mtd: last known address of the required MTD device
886  *	@num: internal device number of the required MTD device
887  *
888  *	Given a number and NULL address, return the num'th entry in the device
889  *	table, if any.	Given an address and num == -1, search the device table
890  *	for a device with that address and return if it's still present. Given
891  *	both, return the num'th driver only if its address matches. Return
892  *	error code if not.
893  */
894 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
895 {
896 	struct mtd_info *ret = NULL, *other;
897 	int err = -ENODEV;
898 
899 	mutex_lock(&mtd_table_mutex);
900 
901 	if (num == -1) {
902 		mtd_for_each_device(other) {
903 			if (other == mtd) {
904 				ret = mtd;
905 				break;
906 			}
907 		}
908 	} else if (num >= 0) {
909 		ret = idr_find(&mtd_idr, num);
910 		if (mtd && mtd != ret)
911 			ret = NULL;
912 	}
913 
914 	if (!ret) {
915 		ret = ERR_PTR(err);
916 		goto out;
917 	}
918 
919 	err = __get_mtd_device(ret);
920 	if (err)
921 		ret = ERR_PTR(err);
922 out:
923 	mutex_unlock(&mtd_table_mutex);
924 	return ret;
925 }
926 EXPORT_SYMBOL_GPL(get_mtd_device);
927 
928 
929 int __get_mtd_device(struct mtd_info *mtd)
930 {
931 	int err;
932 
933 	if (!try_module_get(mtd->owner))
934 		return -ENODEV;
935 
936 	if (mtd->_get_device) {
937 		err = mtd->_get_device(mtd);
938 
939 		if (err) {
940 			module_put(mtd->owner);
941 			return err;
942 		}
943 	}
944 	mtd->usecount++;
945 	return 0;
946 }
947 EXPORT_SYMBOL_GPL(__get_mtd_device);
948 
949 /**
950  *	get_mtd_device_nm - obtain a validated handle for an MTD device by
951  *	device name
952  *	@name: MTD device name to open
953  *
954  * 	This function returns MTD device description structure in case of
955  * 	success and an error code in case of failure.
956  */
957 struct mtd_info *get_mtd_device_nm(const char *name)
958 {
959 	int err = -ENODEV;
960 	struct mtd_info *mtd = NULL, *other;
961 
962 	mutex_lock(&mtd_table_mutex);
963 
964 	mtd_for_each_device(other) {
965 		if (!strcmp(name, other->name)) {
966 			mtd = other;
967 			break;
968 		}
969 	}
970 
971 	if (!mtd)
972 		goto out_unlock;
973 
974 	err = __get_mtd_device(mtd);
975 	if (err)
976 		goto out_unlock;
977 
978 	mutex_unlock(&mtd_table_mutex);
979 	return mtd;
980 
981 out_unlock:
982 	mutex_unlock(&mtd_table_mutex);
983 	return ERR_PTR(err);
984 }
985 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
986 
987 void put_mtd_device(struct mtd_info *mtd)
988 {
989 	mutex_lock(&mtd_table_mutex);
990 	__put_mtd_device(mtd);
991 	mutex_unlock(&mtd_table_mutex);
992 
993 }
994 EXPORT_SYMBOL_GPL(put_mtd_device);
995 
996 void __put_mtd_device(struct mtd_info *mtd)
997 {
998 	--mtd->usecount;
999 	BUG_ON(mtd->usecount < 0);
1000 
1001 	if (mtd->_put_device)
1002 		mtd->_put_device(mtd);
1003 
1004 	module_put(mtd->owner);
1005 }
1006 EXPORT_SYMBOL_GPL(__put_mtd_device);
1007 
1008 /*
1009  * Erase is an synchronous operation. Device drivers are epected to return a
1010  * negative error code if the operation failed and update instr->fail_addr
1011  * to point the portion that was not properly erased.
1012  */
1013 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1014 {
1015 	instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
1016 
1017 	if (!mtd->erasesize || !mtd->_erase)
1018 		return -ENOTSUPP;
1019 
1020 	if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
1021 		return -EINVAL;
1022 	if (!(mtd->flags & MTD_WRITEABLE))
1023 		return -EROFS;
1024 
1025 	if (!instr->len)
1026 		return 0;
1027 
1028 	ledtrig_mtd_activity();
1029 	return mtd->_erase(mtd, instr);
1030 }
1031 EXPORT_SYMBOL_GPL(mtd_erase);
1032 
1033 /*
1034  * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
1035  */
1036 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1037 	      void **virt, resource_size_t *phys)
1038 {
1039 	*retlen = 0;
1040 	*virt = NULL;
1041 	if (phys)
1042 		*phys = 0;
1043 	if (!mtd->_point)
1044 		return -EOPNOTSUPP;
1045 	if (from < 0 || from >= mtd->size || len > mtd->size - from)
1046 		return -EINVAL;
1047 	if (!len)
1048 		return 0;
1049 	return mtd->_point(mtd, from, len, retlen, virt, phys);
1050 }
1051 EXPORT_SYMBOL_GPL(mtd_point);
1052 
1053 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
1054 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
1055 {
1056 	if (!mtd->_unpoint)
1057 		return -EOPNOTSUPP;
1058 	if (from < 0 || from >= mtd->size || len > mtd->size - from)
1059 		return -EINVAL;
1060 	if (!len)
1061 		return 0;
1062 	return mtd->_unpoint(mtd, from, len);
1063 }
1064 EXPORT_SYMBOL_GPL(mtd_unpoint);
1065 
1066 /*
1067  * Allow NOMMU mmap() to directly map the device (if not NULL)
1068  * - return the address to which the offset maps
1069  * - return -ENOSYS to indicate refusal to do the mapping
1070  */
1071 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
1072 				    unsigned long offset, unsigned long flags)
1073 {
1074 	size_t retlen;
1075 	void *virt;
1076 	int ret;
1077 
1078 	ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL);
1079 	if (ret)
1080 		return ret;
1081 	if (retlen != len) {
1082 		mtd_unpoint(mtd, offset, retlen);
1083 		return -ENOSYS;
1084 	}
1085 	return (unsigned long)virt;
1086 }
1087 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
1088 
1089 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
1090 	     u_char *buf)
1091 {
1092 	int ret_code;
1093 	*retlen = 0;
1094 	if (from < 0 || from >= mtd->size || len > mtd->size - from)
1095 		return -EINVAL;
1096 	if (!len)
1097 		return 0;
1098 
1099 	ledtrig_mtd_activity();
1100 	/*
1101 	 * In the absence of an error, drivers return a non-negative integer
1102 	 * representing the maximum number of bitflips that were corrected on
1103 	 * any one ecc region (if applicable; zero otherwise).
1104 	 */
1105 	if (mtd->_read) {
1106 		ret_code = mtd->_read(mtd, from, len, retlen, buf);
1107 	} else if (mtd->_read_oob) {
1108 		struct mtd_oob_ops ops = {
1109 			.len = len,
1110 			.datbuf = buf,
1111 		};
1112 
1113 		ret_code = mtd->_read_oob(mtd, from, &ops);
1114 		*retlen = ops.retlen;
1115 	} else {
1116 		return -ENOTSUPP;
1117 	}
1118 
1119 	if (unlikely(ret_code < 0))
1120 		return ret_code;
1121 	if (mtd->ecc_strength == 0)
1122 		return 0;	/* device lacks ecc */
1123 	return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1124 }
1125 EXPORT_SYMBOL_GPL(mtd_read);
1126 
1127 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1128 	      const u_char *buf)
1129 {
1130 	*retlen = 0;
1131 	if (to < 0 || to >= mtd->size || len > mtd->size - to)
1132 		return -EINVAL;
1133 	if ((!mtd->_write && !mtd->_write_oob) ||
1134 	    !(mtd->flags & MTD_WRITEABLE))
1135 		return -EROFS;
1136 	if (!len)
1137 		return 0;
1138 	ledtrig_mtd_activity();
1139 
1140 	if (!mtd->_write) {
1141 		struct mtd_oob_ops ops = {
1142 			.len = len,
1143 			.datbuf = (u8 *)buf,
1144 		};
1145 		int ret;
1146 
1147 		ret = mtd->_write_oob(mtd, to, &ops);
1148 		*retlen = ops.retlen;
1149 		return ret;
1150 	}
1151 
1152 	return mtd->_write(mtd, to, len, retlen, buf);
1153 }
1154 EXPORT_SYMBOL_GPL(mtd_write);
1155 
1156 /*
1157  * In blackbox flight recorder like scenarios we want to make successful writes
1158  * in interrupt context. panic_write() is only intended to be called when its
1159  * known the kernel is about to panic and we need the write to succeed. Since
1160  * the kernel is not going to be running for much longer, this function can
1161  * break locks and delay to ensure the write succeeds (but not sleep).
1162  */
1163 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1164 		    const u_char *buf)
1165 {
1166 	*retlen = 0;
1167 	if (!mtd->_panic_write)
1168 		return -EOPNOTSUPP;
1169 	if (to < 0 || to >= mtd->size || len > mtd->size - to)
1170 		return -EINVAL;
1171 	if (!(mtd->flags & MTD_WRITEABLE))
1172 		return -EROFS;
1173 	if (!len)
1174 		return 0;
1175 	return mtd->_panic_write(mtd, to, len, retlen, buf);
1176 }
1177 EXPORT_SYMBOL_GPL(mtd_panic_write);
1178 
1179 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1180 			     struct mtd_oob_ops *ops)
1181 {
1182 	/*
1183 	 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1184 	 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1185 	 *  this case.
1186 	 */
1187 	if (!ops->datbuf)
1188 		ops->len = 0;
1189 
1190 	if (!ops->oobbuf)
1191 		ops->ooblen = 0;
1192 
1193 	if (offs < 0 || offs + ops->len > mtd->size)
1194 		return -EINVAL;
1195 
1196 	if (ops->ooblen) {
1197 		size_t maxooblen;
1198 
1199 		if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1200 			return -EINVAL;
1201 
1202 		maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1203 				      mtd_div_by_ws(offs, mtd)) *
1204 			     mtd_oobavail(mtd, ops)) - ops->ooboffs;
1205 		if (ops->ooblen > maxooblen)
1206 			return -EINVAL;
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1213 {
1214 	int ret_code;
1215 	ops->retlen = ops->oobretlen = 0;
1216 
1217 	ret_code = mtd_check_oob_ops(mtd, from, ops);
1218 	if (ret_code)
1219 		return ret_code;
1220 
1221 	ledtrig_mtd_activity();
1222 
1223 	/* Check the validity of a potential fallback on mtd->_read */
1224 	if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1225 		return -EOPNOTSUPP;
1226 
1227 	if (mtd->_read_oob)
1228 		ret_code = mtd->_read_oob(mtd, from, ops);
1229 	else
1230 		ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1231 				      ops->datbuf);
1232 
1233 	/*
1234 	 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1235 	 * similar to mtd->_read(), returning a non-negative integer
1236 	 * representing max bitflips. In other cases, mtd->_read_oob() may
1237 	 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1238 	 */
1239 	if (unlikely(ret_code < 0))
1240 		return ret_code;
1241 	if (mtd->ecc_strength == 0)
1242 		return 0;	/* device lacks ecc */
1243 	return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1244 }
1245 EXPORT_SYMBOL_GPL(mtd_read_oob);
1246 
1247 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1248 				struct mtd_oob_ops *ops)
1249 {
1250 	int ret;
1251 
1252 	ops->retlen = ops->oobretlen = 0;
1253 
1254 	if (!(mtd->flags & MTD_WRITEABLE))
1255 		return -EROFS;
1256 
1257 	ret = mtd_check_oob_ops(mtd, to, ops);
1258 	if (ret)
1259 		return ret;
1260 
1261 	ledtrig_mtd_activity();
1262 
1263 	/* Check the validity of a potential fallback on mtd->_write */
1264 	if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1265 		return -EOPNOTSUPP;
1266 
1267 	if (mtd->_write_oob)
1268 		return mtd->_write_oob(mtd, to, ops);
1269 	else
1270 		return mtd->_write(mtd, to, ops->len, &ops->retlen,
1271 				   ops->datbuf);
1272 }
1273 EXPORT_SYMBOL_GPL(mtd_write_oob);
1274 
1275 /**
1276  * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1277  * @mtd: MTD device structure
1278  * @section: ECC section. Depending on the layout you may have all the ECC
1279  *	     bytes stored in a single contiguous section, or one section
1280  *	     per ECC chunk (and sometime several sections for a single ECC
1281  *	     ECC chunk)
1282  * @oobecc: OOB region struct filled with the appropriate ECC position
1283  *	    information
1284  *
1285  * This function returns ECC section information in the OOB area. If you want
1286  * to get all the ECC bytes information, then you should call
1287  * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1288  *
1289  * Returns zero on success, a negative error code otherwise.
1290  */
1291 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1292 		      struct mtd_oob_region *oobecc)
1293 {
1294 	memset(oobecc, 0, sizeof(*oobecc));
1295 
1296 	if (!mtd || section < 0)
1297 		return -EINVAL;
1298 
1299 	if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1300 		return -ENOTSUPP;
1301 
1302 	return mtd->ooblayout->ecc(mtd, section, oobecc);
1303 }
1304 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1305 
1306 /**
1307  * mtd_ooblayout_free - Get the OOB region definition of a specific free
1308  *			section
1309  * @mtd: MTD device structure
1310  * @section: Free section you are interested in. Depending on the layout
1311  *	     you may have all the free bytes stored in a single contiguous
1312  *	     section, or one section per ECC chunk plus an extra section
1313  *	     for the remaining bytes (or other funky layout).
1314  * @oobfree: OOB region struct filled with the appropriate free position
1315  *	     information
1316  *
1317  * This function returns free bytes position in the OOB area. If you want
1318  * to get all the free bytes information, then you should call
1319  * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1320  *
1321  * Returns zero on success, a negative error code otherwise.
1322  */
1323 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1324 		       struct mtd_oob_region *oobfree)
1325 {
1326 	memset(oobfree, 0, sizeof(*oobfree));
1327 
1328 	if (!mtd || section < 0)
1329 		return -EINVAL;
1330 
1331 	if (!mtd->ooblayout || !mtd->ooblayout->free)
1332 		return -ENOTSUPP;
1333 
1334 	return mtd->ooblayout->free(mtd, section, oobfree);
1335 }
1336 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1337 
1338 /**
1339  * mtd_ooblayout_find_region - Find the region attached to a specific byte
1340  * @mtd: mtd info structure
1341  * @byte: the byte we are searching for
1342  * @sectionp: pointer where the section id will be stored
1343  * @oobregion: used to retrieve the ECC position
1344  * @iter: iterator function. Should be either mtd_ooblayout_free or
1345  *	  mtd_ooblayout_ecc depending on the region type you're searching for
1346  *
1347  * This function returns the section id and oobregion information of a
1348  * specific byte. For example, say you want to know where the 4th ECC byte is
1349  * stored, you'll use:
1350  *
1351  * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1352  *
1353  * Returns zero on success, a negative error code otherwise.
1354  */
1355 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1356 				int *sectionp, struct mtd_oob_region *oobregion,
1357 				int (*iter)(struct mtd_info *,
1358 					    int section,
1359 					    struct mtd_oob_region *oobregion))
1360 {
1361 	int pos = 0, ret, section = 0;
1362 
1363 	memset(oobregion, 0, sizeof(*oobregion));
1364 
1365 	while (1) {
1366 		ret = iter(mtd, section, oobregion);
1367 		if (ret)
1368 			return ret;
1369 
1370 		if (pos + oobregion->length > byte)
1371 			break;
1372 
1373 		pos += oobregion->length;
1374 		section++;
1375 	}
1376 
1377 	/*
1378 	 * Adjust region info to make it start at the beginning at the
1379 	 * 'start' ECC byte.
1380 	 */
1381 	oobregion->offset += byte - pos;
1382 	oobregion->length -= byte - pos;
1383 	*sectionp = section;
1384 
1385 	return 0;
1386 }
1387 
1388 /**
1389  * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1390  *				  ECC byte
1391  * @mtd: mtd info structure
1392  * @eccbyte: the byte we are searching for
1393  * @sectionp: pointer where the section id will be stored
1394  * @oobregion: OOB region information
1395  *
1396  * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1397  * byte.
1398  *
1399  * Returns zero on success, a negative error code otherwise.
1400  */
1401 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1402 				 int *section,
1403 				 struct mtd_oob_region *oobregion)
1404 {
1405 	return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1406 					 mtd_ooblayout_ecc);
1407 }
1408 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1409 
1410 /**
1411  * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1412  * @mtd: mtd info structure
1413  * @buf: destination buffer to store OOB bytes
1414  * @oobbuf: OOB buffer
1415  * @start: first byte to retrieve
1416  * @nbytes: number of bytes to retrieve
1417  * @iter: section iterator
1418  *
1419  * Extract bytes attached to a specific category (ECC or free)
1420  * from the OOB buffer and copy them into buf.
1421  *
1422  * Returns zero on success, a negative error code otherwise.
1423  */
1424 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1425 				const u8 *oobbuf, int start, int nbytes,
1426 				int (*iter)(struct mtd_info *,
1427 					    int section,
1428 					    struct mtd_oob_region *oobregion))
1429 {
1430 	struct mtd_oob_region oobregion;
1431 	int section, ret;
1432 
1433 	ret = mtd_ooblayout_find_region(mtd, start, &section,
1434 					&oobregion, iter);
1435 
1436 	while (!ret) {
1437 		int cnt;
1438 
1439 		cnt = min_t(int, nbytes, oobregion.length);
1440 		memcpy(buf, oobbuf + oobregion.offset, cnt);
1441 		buf += cnt;
1442 		nbytes -= cnt;
1443 
1444 		if (!nbytes)
1445 			break;
1446 
1447 		ret = iter(mtd, ++section, &oobregion);
1448 	}
1449 
1450 	return ret;
1451 }
1452 
1453 /**
1454  * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1455  * @mtd: mtd info structure
1456  * @buf: source buffer to get OOB bytes from
1457  * @oobbuf: OOB buffer
1458  * @start: first OOB byte to set
1459  * @nbytes: number of OOB bytes to set
1460  * @iter: section iterator
1461  *
1462  * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1463  * is selected by passing the appropriate iterator.
1464  *
1465  * Returns zero on success, a negative error code otherwise.
1466  */
1467 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1468 				u8 *oobbuf, int start, int nbytes,
1469 				int (*iter)(struct mtd_info *,
1470 					    int section,
1471 					    struct mtd_oob_region *oobregion))
1472 {
1473 	struct mtd_oob_region oobregion;
1474 	int section, ret;
1475 
1476 	ret = mtd_ooblayout_find_region(mtd, start, &section,
1477 					&oobregion, iter);
1478 
1479 	while (!ret) {
1480 		int cnt;
1481 
1482 		cnt = min_t(int, nbytes, oobregion.length);
1483 		memcpy(oobbuf + oobregion.offset, buf, cnt);
1484 		buf += cnt;
1485 		nbytes -= cnt;
1486 
1487 		if (!nbytes)
1488 			break;
1489 
1490 		ret = iter(mtd, ++section, &oobregion);
1491 	}
1492 
1493 	return ret;
1494 }
1495 
1496 /**
1497  * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1498  * @mtd: mtd info structure
1499  * @iter: category iterator
1500  *
1501  * Count the number of bytes in a given category.
1502  *
1503  * Returns a positive value on success, a negative error code otherwise.
1504  */
1505 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1506 				int (*iter)(struct mtd_info *,
1507 					    int section,
1508 					    struct mtd_oob_region *oobregion))
1509 {
1510 	struct mtd_oob_region oobregion;
1511 	int section = 0, ret, nbytes = 0;
1512 
1513 	while (1) {
1514 		ret = iter(mtd, section++, &oobregion);
1515 		if (ret) {
1516 			if (ret == -ERANGE)
1517 				ret = nbytes;
1518 			break;
1519 		}
1520 
1521 		nbytes += oobregion.length;
1522 	}
1523 
1524 	return ret;
1525 }
1526 
1527 /**
1528  * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1529  * @mtd: mtd info structure
1530  * @eccbuf: destination buffer to store ECC bytes
1531  * @oobbuf: OOB buffer
1532  * @start: first ECC byte to retrieve
1533  * @nbytes: number of ECC bytes to retrieve
1534  *
1535  * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1536  *
1537  * Returns zero on success, a negative error code otherwise.
1538  */
1539 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1540 			       const u8 *oobbuf, int start, int nbytes)
1541 {
1542 	return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1543 				       mtd_ooblayout_ecc);
1544 }
1545 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1546 
1547 /**
1548  * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1549  * @mtd: mtd info structure
1550  * @eccbuf: source buffer to get ECC bytes from
1551  * @oobbuf: OOB buffer
1552  * @start: first ECC byte to set
1553  * @nbytes: number of ECC bytes to set
1554  *
1555  * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1556  *
1557  * Returns zero on success, a negative error code otherwise.
1558  */
1559 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1560 			       u8 *oobbuf, int start, int nbytes)
1561 {
1562 	return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1563 				       mtd_ooblayout_ecc);
1564 }
1565 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1566 
1567 /**
1568  * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1569  * @mtd: mtd info structure
1570  * @databuf: destination buffer to store ECC bytes
1571  * @oobbuf: OOB buffer
1572  * @start: first ECC byte to retrieve
1573  * @nbytes: number of ECC bytes to retrieve
1574  *
1575  * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1576  *
1577  * Returns zero on success, a negative error code otherwise.
1578  */
1579 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1580 				const u8 *oobbuf, int start, int nbytes)
1581 {
1582 	return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1583 				       mtd_ooblayout_free);
1584 }
1585 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1586 
1587 /**
1588  * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
1589  * @mtd: mtd info structure
1590  * @databuf: source buffer to get data bytes from
1591  * @oobbuf: OOB buffer
1592  * @start: first ECC byte to set
1593  * @nbytes: number of ECC bytes to set
1594  *
1595  * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1596  *
1597  * Returns zero on success, a negative error code otherwise.
1598  */
1599 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1600 				u8 *oobbuf, int start, int nbytes)
1601 {
1602 	return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1603 				       mtd_ooblayout_free);
1604 }
1605 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1606 
1607 /**
1608  * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1609  * @mtd: mtd info structure
1610  *
1611  * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1612  *
1613  * Returns zero on success, a negative error code otherwise.
1614  */
1615 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1616 {
1617 	return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1618 }
1619 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1620 
1621 /**
1622  * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
1623  * @mtd: mtd info structure
1624  *
1625  * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1626  *
1627  * Returns zero on success, a negative error code otherwise.
1628  */
1629 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1630 {
1631 	return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1632 }
1633 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1634 
1635 /*
1636  * Method to access the protection register area, present in some flash
1637  * devices. The user data is one time programmable but the factory data is read
1638  * only.
1639  */
1640 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1641 			   struct otp_info *buf)
1642 {
1643 	if (!mtd->_get_fact_prot_info)
1644 		return -EOPNOTSUPP;
1645 	if (!len)
1646 		return 0;
1647 	return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1648 }
1649 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1650 
1651 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1652 			   size_t *retlen, u_char *buf)
1653 {
1654 	*retlen = 0;
1655 	if (!mtd->_read_fact_prot_reg)
1656 		return -EOPNOTSUPP;
1657 	if (!len)
1658 		return 0;
1659 	return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1660 }
1661 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1662 
1663 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1664 			   struct otp_info *buf)
1665 {
1666 	if (!mtd->_get_user_prot_info)
1667 		return -EOPNOTSUPP;
1668 	if (!len)
1669 		return 0;
1670 	return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1671 }
1672 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1673 
1674 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1675 			   size_t *retlen, u_char *buf)
1676 {
1677 	*retlen = 0;
1678 	if (!mtd->_read_user_prot_reg)
1679 		return -EOPNOTSUPP;
1680 	if (!len)
1681 		return 0;
1682 	return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1683 }
1684 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1685 
1686 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1687 			    size_t *retlen, u_char *buf)
1688 {
1689 	int ret;
1690 
1691 	*retlen = 0;
1692 	if (!mtd->_write_user_prot_reg)
1693 		return -EOPNOTSUPP;
1694 	if (!len)
1695 		return 0;
1696 	ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1697 	if (ret)
1698 		return ret;
1699 
1700 	/*
1701 	 * If no data could be written at all, we are out of memory and
1702 	 * must return -ENOSPC.
1703 	 */
1704 	return (*retlen) ? 0 : -ENOSPC;
1705 }
1706 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1707 
1708 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1709 {
1710 	if (!mtd->_lock_user_prot_reg)
1711 		return -EOPNOTSUPP;
1712 	if (!len)
1713 		return 0;
1714 	return mtd->_lock_user_prot_reg(mtd, from, len);
1715 }
1716 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1717 
1718 /* Chip-supported device locking */
1719 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1720 {
1721 	if (!mtd->_lock)
1722 		return -EOPNOTSUPP;
1723 	if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
1724 		return -EINVAL;
1725 	if (!len)
1726 		return 0;
1727 	return mtd->_lock(mtd, ofs, len);
1728 }
1729 EXPORT_SYMBOL_GPL(mtd_lock);
1730 
1731 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1732 {
1733 	if (!mtd->_unlock)
1734 		return -EOPNOTSUPP;
1735 	if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
1736 		return -EINVAL;
1737 	if (!len)
1738 		return 0;
1739 	return mtd->_unlock(mtd, ofs, len);
1740 }
1741 EXPORT_SYMBOL_GPL(mtd_unlock);
1742 
1743 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1744 {
1745 	if (!mtd->_is_locked)
1746 		return -EOPNOTSUPP;
1747 	if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
1748 		return -EINVAL;
1749 	if (!len)
1750 		return 0;
1751 	return mtd->_is_locked(mtd, ofs, len);
1752 }
1753 EXPORT_SYMBOL_GPL(mtd_is_locked);
1754 
1755 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1756 {
1757 	if (ofs < 0 || ofs >= mtd->size)
1758 		return -EINVAL;
1759 	if (!mtd->_block_isreserved)
1760 		return 0;
1761 	return mtd->_block_isreserved(mtd, ofs);
1762 }
1763 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1764 
1765 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1766 {
1767 	if (ofs < 0 || ofs >= mtd->size)
1768 		return -EINVAL;
1769 	if (!mtd->_block_isbad)
1770 		return 0;
1771 	return mtd->_block_isbad(mtd, ofs);
1772 }
1773 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1774 
1775 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1776 {
1777 	if (!mtd->_block_markbad)
1778 		return -EOPNOTSUPP;
1779 	if (ofs < 0 || ofs >= mtd->size)
1780 		return -EINVAL;
1781 	if (!(mtd->flags & MTD_WRITEABLE))
1782 		return -EROFS;
1783 	return mtd->_block_markbad(mtd, ofs);
1784 }
1785 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1786 
1787 /*
1788  * default_mtd_writev - the default writev method
1789  * @mtd: mtd device description object pointer
1790  * @vecs: the vectors to write
1791  * @count: count of vectors in @vecs
1792  * @to: the MTD device offset to write to
1793  * @retlen: on exit contains the count of bytes written to the MTD device.
1794  *
1795  * This function returns zero in case of success and a negative error code in
1796  * case of failure.
1797  */
1798 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1799 			      unsigned long count, loff_t to, size_t *retlen)
1800 {
1801 	unsigned long i;
1802 	size_t totlen = 0, thislen;
1803 	int ret = 0;
1804 
1805 	for (i = 0; i < count; i++) {
1806 		if (!vecs[i].iov_len)
1807 			continue;
1808 		ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1809 				vecs[i].iov_base);
1810 		totlen += thislen;
1811 		if (ret || thislen != vecs[i].iov_len)
1812 			break;
1813 		to += vecs[i].iov_len;
1814 	}
1815 	*retlen = totlen;
1816 	return ret;
1817 }
1818 
1819 /*
1820  * mtd_writev - the vector-based MTD write method
1821  * @mtd: mtd device description object pointer
1822  * @vecs: the vectors to write
1823  * @count: count of vectors in @vecs
1824  * @to: the MTD device offset to write to
1825  * @retlen: on exit contains the count of bytes written to the MTD device.
1826  *
1827  * This function returns zero in case of success and a negative error code in
1828  * case of failure.
1829  */
1830 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1831 	       unsigned long count, loff_t to, size_t *retlen)
1832 {
1833 	*retlen = 0;
1834 	if (!(mtd->flags & MTD_WRITEABLE))
1835 		return -EROFS;
1836 	if (!mtd->_writev)
1837 		return default_mtd_writev(mtd, vecs, count, to, retlen);
1838 	return mtd->_writev(mtd, vecs, count, to, retlen);
1839 }
1840 EXPORT_SYMBOL_GPL(mtd_writev);
1841 
1842 /**
1843  * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1844  * @mtd: mtd device description object pointer
1845  * @size: a pointer to the ideal or maximum size of the allocation, points
1846  *        to the actual allocation size on success.
1847  *
1848  * This routine attempts to allocate a contiguous kernel buffer up to
1849  * the specified size, backing off the size of the request exponentially
1850  * until the request succeeds or until the allocation size falls below
1851  * the system page size. This attempts to make sure it does not adversely
1852  * impact system performance, so when allocating more than one page, we
1853  * ask the memory allocator to avoid re-trying, swapping, writing back
1854  * or performing I/O.
1855  *
1856  * Note, this function also makes sure that the allocated buffer is aligned to
1857  * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1858  *
1859  * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1860  * to handle smaller (i.e. degraded) buffer allocations under low- or
1861  * fragmented-memory situations where such reduced allocations, from a
1862  * requested ideal, are allowed.
1863  *
1864  * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1865  */
1866 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1867 {
1868 	gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
1869 	size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1870 	void *kbuf;
1871 
1872 	*size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1873 
1874 	while (*size > min_alloc) {
1875 		kbuf = kmalloc(*size, flags);
1876 		if (kbuf)
1877 			return kbuf;
1878 
1879 		*size >>= 1;
1880 		*size = ALIGN(*size, mtd->writesize);
1881 	}
1882 
1883 	/*
1884 	 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1885 	 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1886 	 */
1887 	return kmalloc(*size, GFP_KERNEL);
1888 }
1889 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1890 
1891 #ifdef CONFIG_PROC_FS
1892 
1893 /*====================================================================*/
1894 /* Support for /proc/mtd */
1895 
1896 static int mtd_proc_show(struct seq_file *m, void *v)
1897 {
1898 	struct mtd_info *mtd;
1899 
1900 	seq_puts(m, "dev:    size   erasesize  name\n");
1901 	mutex_lock(&mtd_table_mutex);
1902 	mtd_for_each_device(mtd) {
1903 		seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1904 			   mtd->index, (unsigned long long)mtd->size,
1905 			   mtd->erasesize, mtd->name);
1906 	}
1907 	mutex_unlock(&mtd_table_mutex);
1908 	return 0;
1909 }
1910 #endif /* CONFIG_PROC_FS */
1911 
1912 /*====================================================================*/
1913 /* Init code */
1914 
1915 static struct backing_dev_info * __init mtd_bdi_init(char *name)
1916 {
1917 	struct backing_dev_info *bdi;
1918 	int ret;
1919 
1920 	bdi = bdi_alloc(GFP_KERNEL);
1921 	if (!bdi)
1922 		return ERR_PTR(-ENOMEM);
1923 
1924 	bdi->name = name;
1925 	/*
1926 	 * We put '-0' suffix to the name to get the same name format as we
1927 	 * used to get. Since this is called only once, we get a unique name.
1928 	 */
1929 	ret = bdi_register(bdi, "%.28s-0", name);
1930 	if (ret)
1931 		bdi_put(bdi);
1932 
1933 	return ret ? ERR_PTR(ret) : bdi;
1934 }
1935 
1936 static struct proc_dir_entry *proc_mtd;
1937 
1938 static int __init init_mtd(void)
1939 {
1940 	int ret;
1941 
1942 	ret = class_register(&mtd_class);
1943 	if (ret)
1944 		goto err_reg;
1945 
1946 	mtd_bdi = mtd_bdi_init("mtd");
1947 	if (IS_ERR(mtd_bdi)) {
1948 		ret = PTR_ERR(mtd_bdi);
1949 		goto err_bdi;
1950 	}
1951 
1952 	proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show);
1953 
1954 	ret = init_mtdchar();
1955 	if (ret)
1956 		goto out_procfs;
1957 
1958 	dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
1959 
1960 	return 0;
1961 
1962 out_procfs:
1963 	if (proc_mtd)
1964 		remove_proc_entry("mtd", NULL);
1965 	bdi_put(mtd_bdi);
1966 err_bdi:
1967 	class_unregister(&mtd_class);
1968 err_reg:
1969 	pr_err("Error registering mtd class or bdi: %d\n", ret);
1970 	return ret;
1971 }
1972 
1973 static void __exit cleanup_mtd(void)
1974 {
1975 	debugfs_remove_recursive(dfs_dir_mtd);
1976 	cleanup_mtdchar();
1977 	if (proc_mtd)
1978 		remove_proc_entry("mtd", NULL);
1979 	class_unregister(&mtd_class);
1980 	bdi_put(mtd_bdi);
1981 	idr_destroy(&mtd_idr);
1982 }
1983 
1984 module_init(init_mtd);
1985 module_exit(cleanup_mtd);
1986 
1987 MODULE_LICENSE("GPL");
1988 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1989 MODULE_DESCRIPTION("Core MTD registration and access routines");
1990