blob: fb1d68d5e2e563c40060e5c0dc9186e6e68b163f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkf6d5e252008-11-19 16:20:36 +01002/*
3 * Core registration and callback routines for MTD
4 * drivers and users.
5 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02006 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7 * Copyright © 2006 Red Hat UK Limited
8 *
Kyungmin Parkf6d5e252008-11-19 16:20:36 +01009 */
10
Heiko Schocherf5895d12014-06-24 10:10:04 +020011#ifndef __UBOOT__
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/ptrace.h>
15#include <linux/seq_file.h>
16#include <linux/string.h>
17#include <linux/timer.h>
18#include <linux/major.h>
19#include <linux/fs.h>
20#include <linux/err.h>
21#include <linux/ioctl.h>
22#include <linux/init.h>
23#include <linux/proc_fs.h>
24#include <linux/idr.h>
25#include <linux/backing-dev.h>
26#include <linux/gfp.h>
27#include <linux/slab.h>
28#else
Heiko Schocherf5895d12014-06-24 10:10:04 +020029#include <linux/err.h>
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010030#include <ubi_uboot.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020031#endif
32
Fabio Estevam0297d1e2015-11-05 12:43:39 -020033#include <linux/log2.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020034#include <linux/mtd/mtd.h>
35#include <linux/mtd/partitions.h>
36
37#include "mtdcore.h"
38
39#ifndef __UBOOT__
40/*
41 * backing device capabilities for non-mappable devices (such as NAND flash)
42 * - permits private mappings, copies are taken of the data
43 */
44static struct backing_dev_info mtd_bdi_unmappable = {
45 .capabilities = BDI_CAP_MAP_COPY,
46};
47
48/*
49 * backing device capabilities for R/O mappable devices (such as ROM)
50 * - permits private mappings, copies are taken of the data
51 * - permits non-writable shared mappings
52 */
53static struct backing_dev_info mtd_bdi_ro_mappable = {
54 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
55 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
56};
57
58/*
59 * backing device capabilities for writable mappable devices (such as RAM)
60 * - permits private mappings, copies are taken of the data
61 * - permits non-writable shared mappings
62 */
63static struct backing_dev_info mtd_bdi_rw_mappable = {
64 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
65 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
66 BDI_CAP_WRITE_MAP),
67};
68
69static int mtd_cls_suspend(struct device *dev, pm_message_t state);
70static int mtd_cls_resume(struct device *dev);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010071
Heiko Schocherf5895d12014-06-24 10:10:04 +020072static struct class mtd_class = {
73 .name = "mtd",
74 .owner = THIS_MODULE,
75 .suspend = mtd_cls_suspend,
76 .resume = mtd_cls_resume,
77};
78#else
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010079struct mtd_info *mtd_table[MAX_MTD_DEVICES];
80
Heiko Schocherf5895d12014-06-24 10:10:04 +020081#define MAX_IDR_ID 64
82
83struct idr_layer {
84 int used;
85 void *ptr;
86};
87
88struct idr {
89 struct idr_layer id[MAX_IDR_ID];
90};
91
92#define DEFINE_IDR(name) struct idr name;
93
94void idr_remove(struct idr *idp, int id)
95{
96 if (idp->id[id].used)
97 idp->id[id].used = 0;
98
99 return;
100}
101void *idr_find(struct idr *idp, int id)
102{
103 if (idp->id[id].used)
104 return idp->id[id].ptr;
105
106 return NULL;
107}
108
109void *idr_get_next(struct idr *idp, int *next)
110{
111 void *ret;
112 int id = *next;
113
114 ret = idr_find(idp, id);
115 if (ret) {
116 id ++;
117 if (!idp->id[id].used)
118 id = 0;
119 *next = id;
120 } else {
121 *next = 0;
122 }
123
124 return ret;
125}
126
127int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
128{
129 struct idr_layer *idl;
130 int i = 0;
131
132 while (i < MAX_IDR_ID) {
133 idl = &idp->id[i];
134 if (idl->used == 0) {
135 idl->used = 1;
136 idl->ptr = ptr;
137 return i;
138 }
139 i++;
140 }
141 return -ENOSPC;
142}
143#endif
144
145static DEFINE_IDR(mtd_idr);
146
147/* These are exported solely for the purpose of mtd_blkdevs.c. You
148 should not use them for _anything_ else */
149DEFINE_MUTEX(mtd_table_mutex);
150EXPORT_SYMBOL_GPL(mtd_table_mutex);
151
152struct mtd_info *__mtd_next_device(int i)
153{
154 return idr_get_next(&mtd_idr, &i);
155}
156EXPORT_SYMBOL_GPL(__mtd_next_device);
157
158#ifndef __UBOOT__
159static LIST_HEAD(mtd_notifiers);
160
161
162#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
163
164/* REVISIT once MTD uses the driver model better, whoever allocates
165 * the mtd_info will probably want to use the release() hook...
166 */
167static void mtd_release(struct device *dev)
168{
169 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
170 dev_t index = MTD_DEVT(mtd->index);
171
172 /* remove /dev/mtdXro node if needed */
173 if (index)
174 device_destroy(&mtd_class, index + 1);
175}
176
177static int mtd_cls_suspend(struct device *dev, pm_message_t state)
178{
179 struct mtd_info *mtd = dev_get_drvdata(dev);
180
181 return mtd ? mtd_suspend(mtd) : 0;
182}
183
184static int mtd_cls_resume(struct device *dev)
185{
186 struct mtd_info *mtd = dev_get_drvdata(dev);
187
188 if (mtd)
189 mtd_resume(mtd);
190 return 0;
191}
192
193static ssize_t mtd_type_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 struct mtd_info *mtd = dev_get_drvdata(dev);
197 char *type;
198
199 switch (mtd->type) {
200 case MTD_ABSENT:
201 type = "absent";
202 break;
203 case MTD_RAM:
204 type = "ram";
205 break;
206 case MTD_ROM:
207 type = "rom";
208 break;
209 case MTD_NORFLASH:
210 type = "nor";
211 break;
212 case MTD_NANDFLASH:
213 type = "nand";
214 break;
215 case MTD_DATAFLASH:
216 type = "dataflash";
217 break;
218 case MTD_UBIVOLUME:
219 type = "ubi";
220 break;
221 case MTD_MLCNANDFLASH:
222 type = "mlc-nand";
223 break;
224 default:
225 type = "unknown";
226 }
227
228 return snprintf(buf, PAGE_SIZE, "%s\n", type);
229}
230static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
231
232static ssize_t mtd_flags_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234{
235 struct mtd_info *mtd = dev_get_drvdata(dev);
236
237 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
238
239}
240static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
241
242static ssize_t mtd_size_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244{
245 struct mtd_info *mtd = dev_get_drvdata(dev);
246
247 return snprintf(buf, PAGE_SIZE, "%llu\n",
248 (unsigned long long)mtd->size);
249
250}
251static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
252
253static ssize_t mtd_erasesize_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
256 struct mtd_info *mtd = dev_get_drvdata(dev);
257
258 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
259
260}
261static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
262
263static ssize_t mtd_writesize_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
265{
266 struct mtd_info *mtd = dev_get_drvdata(dev);
267
268 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
269
270}
271static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
272
273static ssize_t mtd_subpagesize_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
275{
276 struct mtd_info *mtd = dev_get_drvdata(dev);
277 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
278
279 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
280
281}
282static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
283
284static ssize_t mtd_oobsize_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct mtd_info *mtd = dev_get_drvdata(dev);
288
289 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
290
291}
292static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
293
294static ssize_t mtd_numeraseregions_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct mtd_info *mtd = dev_get_drvdata(dev);
298
299 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
300
301}
302static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
303 NULL);
304
305static ssize_t mtd_name_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct mtd_info *mtd = dev_get_drvdata(dev);
309
310 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
311
312}
313static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
314
315static ssize_t mtd_ecc_strength_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct mtd_info *mtd = dev_get_drvdata(dev);
319
320 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
321}
322static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
323
324static ssize_t mtd_bitflip_threshold_show(struct device *dev,
325 struct device_attribute *attr,
326 char *buf)
327{
328 struct mtd_info *mtd = dev_get_drvdata(dev);
329
330 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
331}
332
333static ssize_t mtd_bitflip_threshold_store(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
336{
337 struct mtd_info *mtd = dev_get_drvdata(dev);
338 unsigned int bitflip_threshold;
339 int retval;
340
341 retval = kstrtouint(buf, 0, &bitflip_threshold);
342 if (retval)
343 return retval;
344
345 mtd->bitflip_threshold = bitflip_threshold;
346 return count;
347}
348static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
349 mtd_bitflip_threshold_show,
350 mtd_bitflip_threshold_store);
351
352static ssize_t mtd_ecc_step_size_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
354{
355 struct mtd_info *mtd = dev_get_drvdata(dev);
356
357 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
358
359}
360static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
361
362static struct attribute *mtd_attrs[] = {
363 &dev_attr_type.attr,
364 &dev_attr_flags.attr,
365 &dev_attr_size.attr,
366 &dev_attr_erasesize.attr,
367 &dev_attr_writesize.attr,
368 &dev_attr_subpagesize.attr,
369 &dev_attr_oobsize.attr,
370 &dev_attr_numeraseregions.attr,
371 &dev_attr_name.attr,
372 &dev_attr_ecc_strength.attr,
373 &dev_attr_ecc_step_size.attr,
374 &dev_attr_bitflip_threshold.attr,
375 NULL,
376};
377ATTRIBUTE_GROUPS(mtd);
378
379static struct device_type mtd_devtype = {
380 .name = "mtd",
381 .groups = mtd_groups,
382 .release = mtd_release,
383};
384#endif
385
386/**
387 * add_mtd_device - register an MTD device
388 * @mtd: pointer to new MTD device info structure
389 *
390 * Add a device to the list of MTD devices present in the system, and
391 * notify each currently active MTD 'user' of its arrival. Returns
392 * zero on success or 1 on failure, which currently will only happen
393 * if there is insufficient memory or a sysfs error.
394 */
395
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100396int add_mtd_device(struct mtd_info *mtd)
397{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200398#ifndef __UBOOT__
399 struct mtd_notifier *not;
400#endif
401 int i, error;
402
403#ifndef __UBOOT__
404 if (!mtd->backing_dev_info) {
405 switch (mtd->type) {
406 case MTD_RAM:
407 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
408 break;
409 case MTD_ROM:
410 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
411 break;
412 default:
413 mtd->backing_dev_info = &mtd_bdi_unmappable;
414 break;
415 }
416 }
417#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100418
419 BUG_ON(mtd->writesize == 0);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200420 mutex_lock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100421
Heiko Schocherf5895d12014-06-24 10:10:04 +0200422 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
423 if (i < 0)
424 goto fail_locked;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100425
Heiko Schocherf5895d12014-06-24 10:10:04 +0200426 mtd->index = i;
427 mtd->usecount = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000428
Heiko Schocherf5895d12014-06-24 10:10:04 +0200429 /* default value if not set by driver */
430 if (mtd->bitflip_threshold == 0)
431 mtd->bitflip_threshold = mtd->ecc_strength;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000432
Heiko Schocherf5895d12014-06-24 10:10:04 +0200433 if (is_power_of_2(mtd->erasesize))
434 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
435 else
436 mtd->erasesize_shift = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100437
Heiko Schocherf5895d12014-06-24 10:10:04 +0200438 if (is_power_of_2(mtd->writesize))
439 mtd->writesize_shift = ffs(mtd->writesize) - 1;
440 else
441 mtd->writesize_shift = 0;
442
443 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
444 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
445
446 /* Some chips always power up locked. Unlock them now */
447 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
448 error = mtd_unlock(mtd, 0, mtd->size);
449 if (error && error != -EOPNOTSUPP)
450 printk(KERN_WARNING
451 "%s: unlock failed, writes may not work\n",
452 mtd->name);
453 }
454
455#ifndef __UBOOT__
456 /* Caller should have set dev.parent to match the
457 * physical device.
458 */
459 mtd->dev.type = &mtd_devtype;
460 mtd->dev.class = &mtd_class;
461 mtd->dev.devt = MTD_DEVT(i);
462 dev_set_name(&mtd->dev, "mtd%d", i);
463 dev_set_drvdata(&mtd->dev, mtd);
464 if (device_register(&mtd->dev) != 0)
465 goto fail_added;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100466
Heiko Schocherf5895d12014-06-24 10:10:04 +0200467 if (MTD_DEVT(i))
468 device_create(&mtd_class, mtd->dev.parent,
469 MTD_DEVT(i) + 1,
470 NULL, "mtd%dro", i);
471
472 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
473 /* No need to get a refcount on the module containing
474 the notifier, since we hold the mtd_table_mutex */
475 list_for_each_entry(not, &mtd_notifiers, list)
476 not->add(mtd);
Heiko Schocherb24c4272014-07-15 16:08:42 +0200477#else
478 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200479#endif
480
481 mutex_unlock(&mtd_table_mutex);
482 /* We _know_ we aren't being removed, because
483 our caller is still holding us here. So none
484 of this try_ nonsense, and no bitching about it
485 either. :) */
486 __module_get(THIS_MODULE);
487 return 0;
488
489#ifndef __UBOOT__
490fail_added:
491 idr_remove(&mtd_idr, i);
492#endif
493fail_locked:
494 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100495 return 1;
496}
497
498/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200499 * del_mtd_device - unregister an MTD device
500 * @mtd: pointer to MTD device info structure
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100501 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200502 * Remove a device from the list of MTD devices present in the system,
503 * and notify each currently active MTD 'user' of its departure.
504 * Returns zero on success or 1 on failure, which currently will happen
505 * if the requested device does not appear to be present in the list.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100506 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200507
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100508int del_mtd_device(struct mtd_info *mtd)
509{
510 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200511#ifndef __UBOOT__
512 struct mtd_notifier *not;
513#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100514
Heiko Schocherf5895d12014-06-24 10:10:04 +0200515 mutex_lock(&mtd_table_mutex);
516
517 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100518 ret = -ENODEV;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200519 goto out_error;
520 }
521
522#ifndef __UBOOT__
523 /* No need to get a refcount on the module containing
524 the notifier, since we hold the mtd_table_mutex */
525 list_for_each_entry(not, &mtd_notifiers, list)
526 not->remove(mtd);
527#endif
528
529 if (mtd->usecount) {
530 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
531 mtd->index, mtd->name, mtd->usecount);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100532 ret = -EBUSY;
533 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200534#ifndef __UBOOT__
535 device_unregister(&mtd->dev);
536#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100537
Heiko Schocherf5895d12014-06-24 10:10:04 +0200538 idr_remove(&mtd_idr, mtd->index);
539
540 module_put(THIS_MODULE);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100541 ret = 0;
542 }
543
Heiko Schocherf5895d12014-06-24 10:10:04 +0200544out_error:
545 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100546 return ret;
547}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200548
549#ifndef __UBOOT__
550/**
551 * mtd_device_parse_register - parse partitions and register an MTD device.
552 *
553 * @mtd: the MTD device to register
554 * @types: the list of MTD partition probes to try, see
555 * 'parse_mtd_partitions()' for more information
556 * @parser_data: MTD partition parser-specific data
557 * @parts: fallback partition information to register, if parsing fails;
558 * only valid if %nr_parts > %0
559 * @nr_parts: the number of partitions in parts, if zero then the full
560 * MTD device is registered if no partition info is found
561 *
562 * This function aggregates MTD partitions parsing (done by
563 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
564 * basically follows the most common pattern found in many MTD drivers:
565 *
566 * * It first tries to probe partitions on MTD device @mtd using parsers
567 * specified in @types (if @types is %NULL, then the default list of parsers
568 * is used, see 'parse_mtd_partitions()' for more information). If none are
569 * found this functions tries to fallback to information specified in
570 * @parts/@nr_parts.
571 * * If any partitioning info was found, this function registers the found
572 * partitions.
573 * * If no partitions were found this function just registers the MTD device
574 * @mtd and exits.
575 *
576 * Returns zero in case of success and a negative error code in case of failure.
577 */
578int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
579 struct mtd_part_parser_data *parser_data,
580 const struct mtd_partition *parts,
581 int nr_parts)
582{
583 int err;
584 struct mtd_partition *real_parts;
585
586 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
587 if (err <= 0 && nr_parts && parts) {
588 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
589 GFP_KERNEL);
590 if (!real_parts)
591 err = -ENOMEM;
592 else
593 err = nr_parts;
594 }
595
596 if (err > 0) {
597 err = add_mtd_partitions(mtd, real_parts, err);
598 kfree(real_parts);
599 } else if (err == 0) {
600 err = add_mtd_device(mtd);
601 if (err == 1)
602 err = -ENODEV;
603 }
604
605 return err;
606}
607EXPORT_SYMBOL_GPL(mtd_device_parse_register);
608
609/**
610 * mtd_device_unregister - unregister an existing MTD device.
611 *
612 * @master: the MTD device to unregister. This will unregister both the master
613 * and any partitions if registered.
614 */
615int mtd_device_unregister(struct mtd_info *master)
616{
617 int err;
618
619 err = del_mtd_partitions(master);
620 if (err)
621 return err;
622
623 if (!device_is_registered(&master->dev))
624 return 0;
625
626 return del_mtd_device(master);
627}
628EXPORT_SYMBOL_GPL(mtd_device_unregister);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100629
630/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200631 * register_mtd_user - register a 'user' of MTD devices.
632 * @new: pointer to notifier info structure
633 *
634 * Registers a pair of callbacks function to be called upon addition
635 * or removal of MTD devices. Causes the 'add' callback to be immediately
636 * invoked for each MTD device currently present in the system.
637 */
638void register_mtd_user (struct mtd_notifier *new)
639{
640 struct mtd_info *mtd;
641
642 mutex_lock(&mtd_table_mutex);
643
644 list_add(&new->list, &mtd_notifiers);
645
646 __module_get(THIS_MODULE);
647
648 mtd_for_each_device(mtd)
649 new->add(mtd);
650
651 mutex_unlock(&mtd_table_mutex);
652}
653EXPORT_SYMBOL_GPL(register_mtd_user);
654
655/**
656 * unregister_mtd_user - unregister a 'user' of MTD devices.
657 * @old: pointer to notifier info structure
658 *
659 * Removes a callback function pair from the list of 'users' to be
660 * notified upon addition or removal of MTD devices. Causes the
661 * 'remove' callback to be immediately invoked for each MTD device
662 * currently present in the system.
663 */
664int unregister_mtd_user (struct mtd_notifier *old)
665{
666 struct mtd_info *mtd;
667
668 mutex_lock(&mtd_table_mutex);
669
670 module_put(THIS_MODULE);
671
672 mtd_for_each_device(mtd)
673 old->remove(mtd);
674
675 list_del(&old->list);
676 mutex_unlock(&mtd_table_mutex);
677 return 0;
678}
679EXPORT_SYMBOL_GPL(unregister_mtd_user);
680#endif
681
682/**
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100683 * get_mtd_device - obtain a validated handle for an MTD device
684 * @mtd: last known address of the required MTD device
685 * @num: internal device number of the required MTD device
686 *
687 * Given a number and NULL address, return the num'th entry in the device
Heiko Schocherf5895d12014-06-24 10:10:04 +0200688 * table, if any. Given an address and num == -1, search the device table
689 * for a device with that address and return if it's still present. Given
690 * both, return the num'th driver only if its address matches. Return
691 * error code if not.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100692 */
693struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
694{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200695 struct mtd_info *ret = NULL, *other;
696 int err = -ENODEV;
697
698 mutex_lock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100699
700 if (num == -1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200701 mtd_for_each_device(other) {
702 if (other == mtd) {
703 ret = mtd;
704 break;
705 }
706 }
707 } else if (num >= 0) {
708 ret = idr_find(&mtd_idr, num);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100709 if (mtd && mtd != ret)
710 ret = NULL;
711 }
712
Heiko Schocherf5895d12014-06-24 10:10:04 +0200713 if (!ret) {
714 ret = ERR_PTR(err);
715 goto out;
716 }
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100717
Heiko Schocherf5895d12014-06-24 10:10:04 +0200718 err = __get_mtd_device(ret);
719 if (err)
720 ret = ERR_PTR(err);
721out:
722 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100723 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200724}
725EXPORT_SYMBOL_GPL(get_mtd_device);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100726
Heiko Schocherf5895d12014-06-24 10:10:04 +0200727
728int __get_mtd_device(struct mtd_info *mtd)
729{
730 int err;
731
732 if (!try_module_get(mtd->owner))
733 return -ENODEV;
734
735 if (mtd->_get_device) {
736 err = mtd->_get_device(mtd);
737
738 if (err) {
739 module_put(mtd->owner);
740 return err;
741 }
742 }
743 mtd->usecount++;
744 return 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100745}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200746EXPORT_SYMBOL_GPL(__get_mtd_device);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100747
748/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200749 * get_mtd_device_nm - obtain a validated handle for an MTD device by
750 * device name
751 * @name: MTD device name to open
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100752 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200753 * This function returns MTD device description structure in case of
754 * success and an error code in case of failure.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100755 */
756struct mtd_info *get_mtd_device_nm(const char *name)
757{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200758 int err = -ENODEV;
759 struct mtd_info *mtd = NULL, *other;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100760
Heiko Schocherf5895d12014-06-24 10:10:04 +0200761 mutex_lock(&mtd_table_mutex);
762
763 mtd_for_each_device(other) {
764 if (!strcmp(name, other->name)) {
765 mtd = other;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100766 break;
767 }
768 }
769
770 if (!mtd)
771 goto out_unlock;
772
Heiko Schocherf5895d12014-06-24 10:10:04 +0200773 err = __get_mtd_device(mtd);
774 if (err)
775 goto out_unlock;
776
777 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100778 return mtd;
779
780out_unlock:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200781 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100782 return ERR_PTR(err);
783}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200784EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Ben Gardiner50bae732010-08-31 17:48:01 -0400785
786#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
787/**
788 * mtd_get_len_incl_bad
789 *
790 * Check if length including bad blocks fits into device.
791 *
792 * @param mtd an MTD device
793 * @param offset offset in flash
794 * @param length image length
795 * @return image length including bad blocks in *len_incl_bad and whether or not
796 * the length returned was truncated in *truncated
797 */
798void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
799 const uint64_t length, uint64_t *len_incl_bad,
800 int *truncated)
801{
802 *truncated = 0;
803 *len_incl_bad = 0;
804
maxin.john@enea.comb5ee6e22014-09-08 19:04:16 +0200805 if (!mtd->_block_isbad) {
Ben Gardiner50bae732010-08-31 17:48:01 -0400806 *len_incl_bad = length;
807 return;
808 }
809
810 uint64_t len_excl_bad = 0;
811 uint64_t block_len;
812
813 while (len_excl_bad < length) {
Scott Wood10390ce2010-09-09 15:40:03 -0500814 if (offset >= mtd->size) {
815 *truncated = 1;
816 return;
817 }
818
Ben Gardiner50bae732010-08-31 17:48:01 -0400819 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
820
maxin.john@enea.comb5ee6e22014-09-08 19:04:16 +0200821 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
Ben Gardiner50bae732010-08-31 17:48:01 -0400822 len_excl_bad += block_len;
823
824 *len_incl_bad += block_len;
825 offset += block_len;
Ben Gardiner50bae732010-08-31 17:48:01 -0400826 }
827}
828#endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000829
Heiko Schocherf5895d12014-06-24 10:10:04 +0200830void put_mtd_device(struct mtd_info *mtd)
831{
832 mutex_lock(&mtd_table_mutex);
833 __put_mtd_device(mtd);
834 mutex_unlock(&mtd_table_mutex);
835
836}
837EXPORT_SYMBOL_GPL(put_mtd_device);
838
839void __put_mtd_device(struct mtd_info *mtd)
840{
841 --mtd->usecount;
842 BUG_ON(mtd->usecount < 0);
843
844 if (mtd->_put_device)
845 mtd->_put_device(mtd);
846
847 module_put(mtd->owner);
848}
849EXPORT_SYMBOL_GPL(__put_mtd_device);
850
851/*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000852 * Erase is an asynchronous operation. Device drivers are supposed
853 * to call instr->callback() whenever the operation completes, even
854 * if it completes with a failure.
855 * Callers are supposed to pass a callback function and wait for it
856 * to be called before writing to the block.
857 */
858int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
859{
860 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
861 return -EINVAL;
862 if (!(mtd->flags & MTD_WRITEABLE))
863 return -EROFS;
864 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
865 if (!instr->len) {
866 instr->state = MTD_ERASE_DONE;
867 mtd_erase_callback(instr);
868 return 0;
869 }
870 return mtd->_erase(mtd, instr);
871}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200872EXPORT_SYMBOL_GPL(mtd_erase);
873
874#ifndef __UBOOT__
875/*
876 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
877 */
878int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
879 void **virt, resource_size_t *phys)
880{
881 *retlen = 0;
882 *virt = NULL;
883 if (phys)
884 *phys = 0;
885 if (!mtd->_point)
886 return -EOPNOTSUPP;
887 if (from < 0 || from > mtd->size || len > mtd->size - from)
888 return -EINVAL;
889 if (!len)
890 return 0;
891 return mtd->_point(mtd, from, len, retlen, virt, phys);
892}
893EXPORT_SYMBOL_GPL(mtd_point);
894
895/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
896int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
897{
898 if (!mtd->_point)
899 return -EOPNOTSUPP;
900 if (from < 0 || from > mtd->size || len > mtd->size - from)
901 return -EINVAL;
902 if (!len)
903 return 0;
904 return mtd->_unpoint(mtd, from, len);
905}
906EXPORT_SYMBOL_GPL(mtd_unpoint);
907#endif
908
909/*
910 * Allow NOMMU mmap() to directly map the device (if not NULL)
911 * - return the address to which the offset maps
912 * - return -ENOSYS to indicate refusal to do the mapping
913 */
914unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
915 unsigned long offset, unsigned long flags)
916{
917 if (!mtd->_get_unmapped_area)
918 return -EOPNOTSUPP;
919 if (offset > mtd->size || len > mtd->size - offset)
920 return -EINVAL;
921 return mtd->_get_unmapped_area(mtd, len, offset, flags);
922}
923EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000924
925int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
926 u_char *buf)
927{
Paul Burton700a76c2013-09-04 15:16:56 +0100928 int ret_code;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200929 *retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000930 if (from < 0 || from > mtd->size || len > mtd->size - from)
931 return -EINVAL;
932 if (!len)
933 return 0;
Paul Burton700a76c2013-09-04 15:16:56 +0100934
935 /*
936 * In the absence of an error, drivers return a non-negative integer
937 * representing the maximum number of bitflips that were corrected on
938 * any one ecc region (if applicable; zero otherwise).
939 */
Boris Brezillon6c20df72018-08-16 17:29:59 +0200940 if (mtd->_read) {
941 ret_code = mtd->_read(mtd, from, len, retlen, buf);
942 } else if (mtd->_read_oob) {
943 struct mtd_oob_ops ops = {
944 .len = len,
945 .datbuf = buf,
946 };
947
948 ret_code = mtd->_read_oob(mtd, from, &ops);
949 *retlen = ops.retlen;
950 } else {
951 return -ENOTSUPP;
952 }
953
Paul Burton700a76c2013-09-04 15:16:56 +0100954 if (unlikely(ret_code < 0))
955 return ret_code;
956 if (mtd->ecc_strength == 0)
957 return 0; /* device lacks ecc */
958 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000959}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200960EXPORT_SYMBOL_GPL(mtd_read);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000961
962int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
963 const u_char *buf)
964{
965 *retlen = 0;
966 if (to < 0 || to > mtd->size || len > mtd->size - to)
967 return -EINVAL;
Boris Brezillon6c20df72018-08-16 17:29:59 +0200968 if ((!mtd->_write && !mtd->_write_oob) ||
969 !(mtd->flags & MTD_WRITEABLE))
Sergey Lapin3a38a552013-01-14 03:46:50 +0000970 return -EROFS;
971 if (!len)
972 return 0;
Boris Brezillon6c20df72018-08-16 17:29:59 +0200973
974 if (!mtd->_write) {
975 struct mtd_oob_ops ops = {
976 .len = len,
977 .datbuf = (u8 *)buf,
978 };
979 int ret;
980
981 ret = mtd->_write_oob(mtd, to, &ops);
982 *retlen = ops.retlen;
983 return ret;
984 }
985
Sergey Lapin3a38a552013-01-14 03:46:50 +0000986 return mtd->_write(mtd, to, len, retlen, buf);
987}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200988EXPORT_SYMBOL_GPL(mtd_write);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000989
990/*
991 * In blackbox flight recorder like scenarios we want to make successful writes
992 * in interrupt context. panic_write() is only intended to be called when its
993 * known the kernel is about to panic and we need the write to succeed. Since
994 * the kernel is not going to be running for much longer, this function can
995 * break locks and delay to ensure the write succeeds (but not sleep).
996 */
997int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
998 const u_char *buf)
999{
1000 *retlen = 0;
1001 if (!mtd->_panic_write)
1002 return -EOPNOTSUPP;
1003 if (to < 0 || to > mtd->size || len > mtd->size - to)
1004 return -EINVAL;
1005 if (!(mtd->flags & MTD_WRITEABLE))
1006 return -EROFS;
1007 if (!len)
1008 return 0;
1009 return mtd->_panic_write(mtd, to, len, retlen, buf);
1010}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001011EXPORT_SYMBOL_GPL(mtd_panic_write);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001012
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001013static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1014 struct mtd_oob_ops *ops)
1015{
1016 /*
1017 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1018 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1019 * this case.
1020 */
1021 if (!ops->datbuf)
1022 ops->len = 0;
1023
1024 if (!ops->oobbuf)
1025 ops->ooblen = 0;
1026
1027 if (offs < 0 || offs + ops->len > mtd->size)
1028 return -EINVAL;
1029
1030 if (ops->ooblen) {
1031 u64 maxooblen;
1032
1033 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1034 return -EINVAL;
1035
1036 maxooblen = ((mtd_div_by_ws(mtd->size, mtd) -
1037 mtd_div_by_ws(offs, mtd)) *
1038 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1039 if (ops->ooblen > maxooblen)
1040 return -EINVAL;
1041 }
1042
1043 return 0;
1044}
1045
Sergey Lapin3a38a552013-01-14 03:46:50 +00001046int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1047{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001048 int ret_code;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001049 ops->retlen = ops->oobretlen = 0;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001050
1051 ret_code = mtd_check_oob_ops(mtd, from, ops);
1052 if (ret_code)
1053 return ret_code;
1054
Miquel Raynal19ea9252018-08-16 17:30:02 +02001055 /* Check the validity of a potential fallback on mtd->_read */
1056 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1057 return -EOPNOTSUPP;
1058
1059 if (mtd->_read_oob)
1060 ret_code = mtd->_read_oob(mtd, from, ops);
1061 else
1062 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1063 ops->datbuf);
1064
Heiko Schocherf5895d12014-06-24 10:10:04 +02001065 /*
1066 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1067 * similar to mtd->_read(), returning a non-negative integer
1068 * representing max bitflips. In other cases, mtd->_read_oob() may
1069 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1070 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001071 if (unlikely(ret_code < 0))
1072 return ret_code;
1073 if (mtd->ecc_strength == 0)
1074 return 0; /* device lacks ecc */
1075 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001076}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001077EXPORT_SYMBOL_GPL(mtd_read_oob);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001078
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001079int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1080 struct mtd_oob_ops *ops)
1081{
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001082 int ret;
1083
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001084 ops->retlen = ops->oobretlen = 0;
Miquel Raynal19ea9252018-08-16 17:30:02 +02001085
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001086 if (!(mtd->flags & MTD_WRITEABLE))
1087 return -EROFS;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001088
1089 ret = mtd_check_oob_ops(mtd, to, ops);
1090 if (ret)
1091 return ret;
1092
Miquel Raynal19ea9252018-08-16 17:30:02 +02001093 /* Check the validity of a potential fallback on mtd->_write */
1094 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1095 return -EOPNOTSUPP;
1096
1097 if (mtd->_write_oob)
1098 return mtd->_write_oob(mtd, to, ops);
1099 else
1100 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1101 ops->datbuf);
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001102}
1103EXPORT_SYMBOL_GPL(mtd_write_oob);
1104
Boris Brezillone1b1e3a2017-11-22 02:38:23 +09001105/**
1106 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1107 * @mtd: MTD device structure
1108 * @section: ECC section. Depending on the layout you may have all the ECC
1109 * bytes stored in a single contiguous section, or one section
1110 * per ECC chunk (and sometime several sections for a single ECC
1111 * ECC chunk)
1112 * @oobecc: OOB region struct filled with the appropriate ECC position
1113 * information
1114 *
1115 * This function returns ECC section information in the OOB area. If you want
1116 * to get all the ECC bytes information, then you should call
1117 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1118 *
1119 * Returns zero on success, a negative error code otherwise.
1120 */
1121int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1122 struct mtd_oob_region *oobecc)
1123{
1124 memset(oobecc, 0, sizeof(*oobecc));
1125
1126 if (!mtd || section < 0)
1127 return -EINVAL;
1128
1129 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1130 return -ENOTSUPP;
1131
1132 return mtd->ooblayout->ecc(mtd, section, oobecc);
1133}
1134EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1135
1136/**
1137 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1138 * section
1139 * @mtd: MTD device structure
1140 * @section: Free section you are interested in. Depending on the layout
1141 * you may have all the free bytes stored in a single contiguous
1142 * section, or one section per ECC chunk plus an extra section
1143 * for the remaining bytes (or other funky layout).
1144 * @oobfree: OOB region struct filled with the appropriate free position
1145 * information
1146 *
1147 * This function returns free bytes position in the OOB area. If you want
1148 * to get all the free bytes information, then you should call
1149 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1150 *
1151 * Returns zero on success, a negative error code otherwise.
1152 */
1153int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1154 struct mtd_oob_region *oobfree)
1155{
1156 memset(oobfree, 0, sizeof(*oobfree));
1157
1158 if (!mtd || section < 0)
1159 return -EINVAL;
1160
1161 if (!mtd->ooblayout || !mtd->ooblayout->free)
1162 return -ENOTSUPP;
1163
1164 return mtd->ooblayout->free(mtd, section, oobfree);
1165}
1166EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1167
1168/**
1169 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1170 * @mtd: mtd info structure
1171 * @byte: the byte we are searching for
1172 * @sectionp: pointer where the section id will be stored
1173 * @oobregion: used to retrieve the ECC position
1174 * @iter: iterator function. Should be either mtd_ooblayout_free or
1175 * mtd_ooblayout_ecc depending on the region type you're searching for
1176 *
1177 * This function returns the section id and oobregion information of a
1178 * specific byte. For example, say you want to know where the 4th ECC byte is
1179 * stored, you'll use:
1180 *
1181 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1182 *
1183 * Returns zero on success, a negative error code otherwise.
1184 */
1185static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1186 int *sectionp, struct mtd_oob_region *oobregion,
1187 int (*iter)(struct mtd_info *,
1188 int section,
1189 struct mtd_oob_region *oobregion))
1190{
1191 int pos = 0, ret, section = 0;
1192
1193 memset(oobregion, 0, sizeof(*oobregion));
1194
1195 while (1) {
1196 ret = iter(mtd, section, oobregion);
1197 if (ret)
1198 return ret;
1199
1200 if (pos + oobregion->length > byte)
1201 break;
1202
1203 pos += oobregion->length;
1204 section++;
1205 }
1206
1207 /*
1208 * Adjust region info to make it start at the beginning at the
1209 * 'start' ECC byte.
1210 */
1211 oobregion->offset += byte - pos;
1212 oobregion->length -= byte - pos;
1213 *sectionp = section;
1214
1215 return 0;
1216}
1217
1218/**
1219 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1220 * ECC byte
1221 * @mtd: mtd info structure
1222 * @eccbyte: the byte we are searching for
1223 * @sectionp: pointer where the section id will be stored
1224 * @oobregion: OOB region information
1225 *
1226 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1227 * byte.
1228 *
1229 * Returns zero on success, a negative error code otherwise.
1230 */
1231int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1232 int *section,
1233 struct mtd_oob_region *oobregion)
1234{
1235 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1236 mtd_ooblayout_ecc);
1237}
1238EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1239
1240/**
1241 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1242 * @mtd: mtd info structure
1243 * @buf: destination buffer to store OOB bytes
1244 * @oobbuf: OOB buffer
1245 * @start: first byte to retrieve
1246 * @nbytes: number of bytes to retrieve
1247 * @iter: section iterator
1248 *
1249 * Extract bytes attached to a specific category (ECC or free)
1250 * from the OOB buffer and copy them into buf.
1251 *
1252 * Returns zero on success, a negative error code otherwise.
1253 */
1254static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1255 const u8 *oobbuf, int start, int nbytes,
1256 int (*iter)(struct mtd_info *,
1257 int section,
1258 struct mtd_oob_region *oobregion))
1259{
1260 struct mtd_oob_region oobregion;
1261 int section, ret;
1262
1263 ret = mtd_ooblayout_find_region(mtd, start, &section,
1264 &oobregion, iter);
1265
1266 while (!ret) {
1267 int cnt;
1268
1269 cnt = min_t(int, nbytes, oobregion.length);
1270 memcpy(buf, oobbuf + oobregion.offset, cnt);
1271 buf += cnt;
1272 nbytes -= cnt;
1273
1274 if (!nbytes)
1275 break;
1276
1277 ret = iter(mtd, ++section, &oobregion);
1278 }
1279
1280 return ret;
1281}
1282
1283/**
1284 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1285 * @mtd: mtd info structure
1286 * @buf: source buffer to get OOB bytes from
1287 * @oobbuf: OOB buffer
1288 * @start: first OOB byte to set
1289 * @nbytes: number of OOB bytes to set
1290 * @iter: section iterator
1291 *
1292 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1293 * is selected by passing the appropriate iterator.
1294 *
1295 * Returns zero on success, a negative error code otherwise.
1296 */
1297static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1298 u8 *oobbuf, int start, int nbytes,
1299 int (*iter)(struct mtd_info *,
1300 int section,
1301 struct mtd_oob_region *oobregion))
1302{
1303 struct mtd_oob_region oobregion;
1304 int section, ret;
1305
1306 ret = mtd_ooblayout_find_region(mtd, start, &section,
1307 &oobregion, iter);
1308
1309 while (!ret) {
1310 int cnt;
1311
1312 cnt = min_t(int, nbytes, oobregion.length);
1313 memcpy(oobbuf + oobregion.offset, buf, cnt);
1314 buf += cnt;
1315 nbytes -= cnt;
1316
1317 if (!nbytes)
1318 break;
1319
1320 ret = iter(mtd, ++section, &oobregion);
1321 }
1322
1323 return ret;
1324}
1325
1326/**
1327 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1328 * @mtd: mtd info structure
1329 * @iter: category iterator
1330 *
1331 * Count the number of bytes in a given category.
1332 *
1333 * Returns a positive value on success, a negative error code otherwise.
1334 */
1335static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1336 int (*iter)(struct mtd_info *,
1337 int section,
1338 struct mtd_oob_region *oobregion))
1339{
1340 struct mtd_oob_region oobregion;
1341 int section = 0, ret, nbytes = 0;
1342
1343 while (1) {
1344 ret = iter(mtd, section++, &oobregion);
1345 if (ret) {
1346 if (ret == -ERANGE)
1347 ret = nbytes;
1348 break;
1349 }
1350
1351 nbytes += oobregion.length;
1352 }
1353
1354 return ret;
1355}
1356
1357/**
1358 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1359 * @mtd: mtd info structure
1360 * @eccbuf: destination buffer to store ECC bytes
1361 * @oobbuf: OOB buffer
1362 * @start: first ECC byte to retrieve
1363 * @nbytes: number of ECC bytes to retrieve
1364 *
1365 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1366 *
1367 * Returns zero on success, a negative error code otherwise.
1368 */
1369int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1370 const u8 *oobbuf, int start, int nbytes)
1371{
1372 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1373 mtd_ooblayout_ecc);
1374}
1375EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1376
1377/**
1378 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1379 * @mtd: mtd info structure
1380 * @eccbuf: source buffer to get ECC bytes from
1381 * @oobbuf: OOB buffer
1382 * @start: first ECC byte to set
1383 * @nbytes: number of ECC bytes to set
1384 *
1385 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1386 *
1387 * Returns zero on success, a negative error code otherwise.
1388 */
1389int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1390 u8 *oobbuf, int start, int nbytes)
1391{
1392 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1393 mtd_ooblayout_ecc);
1394}
1395EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1396
1397/**
1398 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1399 * @mtd: mtd info structure
1400 * @databuf: destination buffer to store ECC bytes
1401 * @oobbuf: OOB buffer
1402 * @start: first ECC byte to retrieve
1403 * @nbytes: number of ECC bytes to retrieve
1404 *
1405 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1406 *
1407 * Returns zero on success, a negative error code otherwise.
1408 */
1409int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1410 const u8 *oobbuf, int start, int nbytes)
1411{
1412 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1413 mtd_ooblayout_free);
1414}
1415EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1416
1417/**
1418 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1419 * @mtd: mtd info structure
1420 * @eccbuf: source buffer to get data bytes from
1421 * @oobbuf: OOB buffer
1422 * @start: first ECC byte to set
1423 * @nbytes: number of ECC bytes to set
1424 *
1425 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1426 *
1427 * Returns zero on success, a negative error code otherwise.
1428 */
1429int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1430 u8 *oobbuf, int start, int nbytes)
1431{
1432 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1433 mtd_ooblayout_free);
1434}
1435EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1436
1437/**
1438 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1439 * @mtd: mtd info structure
1440 *
1441 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1442 *
1443 * Returns zero on success, a negative error code otherwise.
1444 */
1445int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1446{
1447 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1448}
1449EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1450
1451/**
1452 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1453 * @mtd: mtd info structure
1454 *
1455 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1456 *
1457 * Returns zero on success, a negative error code otherwise.
1458 */
1459int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1460{
1461 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1462}
1463EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1464
Sergey Lapin3a38a552013-01-14 03:46:50 +00001465/*
1466 * Method to access the protection register area, present in some flash
1467 * devices. The user data is one time programmable but the factory data is read
1468 * only.
1469 */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001470int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1471 struct otp_info *buf)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001472{
1473 if (!mtd->_get_fact_prot_info)
1474 return -EOPNOTSUPP;
1475 if (!len)
1476 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001477 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001478}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001479EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001480
1481int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1482 size_t *retlen, u_char *buf)
1483{
1484 *retlen = 0;
1485 if (!mtd->_read_fact_prot_reg)
1486 return -EOPNOTSUPP;
1487 if (!len)
1488 return 0;
1489 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1490}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001491EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001492
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001493int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1494 struct otp_info *buf)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001495{
1496 if (!mtd->_get_user_prot_info)
1497 return -EOPNOTSUPP;
1498 if (!len)
1499 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001500 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001501}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001502EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001503
1504int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1505 size_t *retlen, u_char *buf)
1506{
1507 *retlen = 0;
1508 if (!mtd->_read_user_prot_reg)
1509 return -EOPNOTSUPP;
1510 if (!len)
1511 return 0;
1512 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1513}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001514EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001515
1516int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1517 size_t *retlen, u_char *buf)
1518{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001519 int ret;
1520
Sergey Lapin3a38a552013-01-14 03:46:50 +00001521 *retlen = 0;
1522 if (!mtd->_write_user_prot_reg)
1523 return -EOPNOTSUPP;
1524 if (!len)
1525 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001526 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1527 if (ret)
1528 return ret;
1529
1530 /*
1531 * If no data could be written at all, we are out of memory and
1532 * must return -ENOSPC.
1533 */
1534 return (*retlen) ? 0 : -ENOSPC;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001535}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001536EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001537
1538int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1539{
1540 if (!mtd->_lock_user_prot_reg)
1541 return -EOPNOTSUPP;
1542 if (!len)
1543 return 0;
1544 return mtd->_lock_user_prot_reg(mtd, from, len);
1545}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001546EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001547
1548/* Chip-supported device locking */
1549int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1550{
1551 if (!mtd->_lock)
1552 return -EOPNOTSUPP;
1553 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1554 return -EINVAL;
1555 if (!len)
1556 return 0;
1557 return mtd->_lock(mtd, ofs, len);
1558}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001559EXPORT_SYMBOL_GPL(mtd_lock);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001560
1561int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1562{
1563 if (!mtd->_unlock)
1564 return -EOPNOTSUPP;
1565 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1566 return -EINVAL;
1567 if (!len)
1568 return 0;
1569 return mtd->_unlock(mtd, ofs, len);
1570}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001571EXPORT_SYMBOL_GPL(mtd_unlock);
1572
1573int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1574{
1575 if (!mtd->_is_locked)
1576 return -EOPNOTSUPP;
1577 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1578 return -EINVAL;
1579 if (!len)
1580 return 0;
1581 return mtd->_is_locked(mtd, ofs, len);
1582}
1583EXPORT_SYMBOL_GPL(mtd_is_locked);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001584
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001585int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001586{
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001587 if (ofs < 0 || ofs > mtd->size)
1588 return -EINVAL;
1589 if (!mtd->_block_isreserved)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001590 return 0;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001591 return mtd->_block_isreserved(mtd, ofs);
1592}
1593EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1594
1595int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1596{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001597 if (ofs < 0 || ofs > mtd->size)
1598 return -EINVAL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001599 if (!mtd->_block_isbad)
1600 return 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001601 return mtd->_block_isbad(mtd, ofs);
1602}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001603EXPORT_SYMBOL_GPL(mtd_block_isbad);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001604
1605int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1606{
1607 if (!mtd->_block_markbad)
1608 return -EOPNOTSUPP;
1609 if (ofs < 0 || ofs > mtd->size)
1610 return -EINVAL;
1611 if (!(mtd->flags & MTD_WRITEABLE))
1612 return -EROFS;
1613 return mtd->_block_markbad(mtd, ofs);
1614}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001615EXPORT_SYMBOL_GPL(mtd_block_markbad);
1616
1617#ifndef __UBOOT__
1618/*
1619 * default_mtd_writev - the default writev method
1620 * @mtd: mtd device description object pointer
1621 * @vecs: the vectors to write
1622 * @count: count of vectors in @vecs
1623 * @to: the MTD device offset to write to
1624 * @retlen: on exit contains the count of bytes written to the MTD device.
1625 *
1626 * This function returns zero in case of success and a negative error code in
1627 * case of failure.
1628 */
1629static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1630 unsigned long count, loff_t to, size_t *retlen)
1631{
1632 unsigned long i;
1633 size_t totlen = 0, thislen;
1634 int ret = 0;
1635
1636 for (i = 0; i < count; i++) {
1637 if (!vecs[i].iov_len)
1638 continue;
1639 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1640 vecs[i].iov_base);
1641 totlen += thislen;
1642 if (ret || thislen != vecs[i].iov_len)
1643 break;
1644 to += vecs[i].iov_len;
1645 }
1646 *retlen = totlen;
1647 return ret;
1648}
1649
1650/*
1651 * mtd_writev - the vector-based MTD write method
1652 * @mtd: mtd device description object pointer
1653 * @vecs: the vectors to write
1654 * @count: count of vectors in @vecs
1655 * @to: the MTD device offset to write to
1656 * @retlen: on exit contains the count of bytes written to the MTD device.
1657 *
1658 * This function returns zero in case of success and a negative error code in
1659 * case of failure.
1660 */
1661int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1662 unsigned long count, loff_t to, size_t *retlen)
1663{
1664 *retlen = 0;
1665 if (!(mtd->flags & MTD_WRITEABLE))
1666 return -EROFS;
1667 if (!mtd->_writev)
1668 return default_mtd_writev(mtd, vecs, count, to, retlen);
1669 return mtd->_writev(mtd, vecs, count, to, retlen);
1670}
1671EXPORT_SYMBOL_GPL(mtd_writev);
1672
1673/**
1674 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1675 * @mtd: mtd device description object pointer
1676 * @size: a pointer to the ideal or maximum size of the allocation, points
1677 * to the actual allocation size on success.
1678 *
1679 * This routine attempts to allocate a contiguous kernel buffer up to
1680 * the specified size, backing off the size of the request exponentially
1681 * until the request succeeds or until the allocation size falls below
1682 * the system page size. This attempts to make sure it does not adversely
1683 * impact system performance, so when allocating more than one page, we
1684 * ask the memory allocator to avoid re-trying, swapping, writing back
1685 * or performing I/O.
1686 *
1687 * Note, this function also makes sure that the allocated buffer is aligned to
1688 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1689 *
1690 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1691 * to handle smaller (i.e. degraded) buffer allocations under low- or
1692 * fragmented-memory situations where such reduced allocations, from a
1693 * requested ideal, are allowed.
1694 *
1695 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1696 */
1697void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1698{
1699 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1700 __GFP_NORETRY | __GFP_NO_KSWAPD;
1701 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1702 void *kbuf;
1703
1704 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1705
1706 while (*size > min_alloc) {
1707 kbuf = kmalloc(*size, flags);
1708 if (kbuf)
1709 return kbuf;
1710
1711 *size >>= 1;
1712 *size = ALIGN(*size, mtd->writesize);
1713 }
1714
1715 /*
1716 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1717 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1718 */
1719 return kmalloc(*size, GFP_KERNEL);
1720}
1721EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1722#endif
1723
1724#ifdef CONFIG_PROC_FS
1725
1726/*====================================================================*/
1727/* Support for /proc/mtd */
1728
1729static int mtd_proc_show(struct seq_file *m, void *v)
1730{
1731 struct mtd_info *mtd;
1732
1733 seq_puts(m, "dev: size erasesize name\n");
1734 mutex_lock(&mtd_table_mutex);
1735 mtd_for_each_device(mtd) {
1736 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1737 mtd->index, (unsigned long long)mtd->size,
1738 mtd->erasesize, mtd->name);
1739 }
1740 mutex_unlock(&mtd_table_mutex);
1741 return 0;
1742}
1743
1744static int mtd_proc_open(struct inode *inode, struct file *file)
1745{
1746 return single_open(file, mtd_proc_show, NULL);
1747}
1748
1749static const struct file_operations mtd_proc_ops = {
1750 .open = mtd_proc_open,
1751 .read = seq_read,
1752 .llseek = seq_lseek,
1753 .release = single_release,
1754};
1755#endif /* CONFIG_PROC_FS */
1756
1757/*====================================================================*/
1758/* Init code */
1759
1760#ifndef __UBOOT__
1761static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1762{
1763 int ret;
1764
1765 ret = bdi_init(bdi);
1766 if (!ret)
1767 ret = bdi_register(bdi, NULL, "%s", name);
1768
1769 if (ret)
1770 bdi_destroy(bdi);
1771
1772 return ret;
1773}
1774
1775static struct proc_dir_entry *proc_mtd;
1776
1777static int __init init_mtd(void)
1778{
1779 int ret;
1780
1781 ret = class_register(&mtd_class);
1782 if (ret)
1783 goto err_reg;
1784
1785 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1786 if (ret)
1787 goto err_bdi1;
1788
1789 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1790 if (ret)
1791 goto err_bdi2;
1792
1793 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1794 if (ret)
1795 goto err_bdi3;
1796
1797 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1798
1799 ret = init_mtdchar();
1800 if (ret)
1801 goto out_procfs;
1802
1803 return 0;
1804
1805out_procfs:
1806 if (proc_mtd)
1807 remove_proc_entry("mtd", NULL);
1808err_bdi3:
1809 bdi_destroy(&mtd_bdi_ro_mappable);
1810err_bdi2:
1811 bdi_destroy(&mtd_bdi_unmappable);
1812err_bdi1:
1813 class_unregister(&mtd_class);
1814err_reg:
1815 pr_err("Error registering mtd class or bdi: %d\n", ret);
1816 return ret;
1817}
1818
1819static void __exit cleanup_mtd(void)
1820{
1821 cleanup_mtdchar();
1822 if (proc_mtd)
1823 remove_proc_entry("mtd", NULL);
1824 class_unregister(&mtd_class);
1825 bdi_destroy(&mtd_bdi_unmappable);
1826 bdi_destroy(&mtd_bdi_ro_mappable);
1827 bdi_destroy(&mtd_bdi_rw_mappable);
1828}
1829
1830module_init(init_mtd);
1831module_exit(cleanup_mtd);
1832#endif
1833
1834MODULE_LICENSE("GPL");
1835MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1836MODULE_DESCRIPTION("Core MTD registration and access routines");