blob: dd04d676d56c37199fee42056484248d75c6c763 [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
Heiko Schocherf5895d12014-06-24 10:10:04 +020079#define MAX_IDR_ID 64
80
81struct idr_layer {
82 int used;
83 void *ptr;
84};
85
86struct idr {
87 struct idr_layer id[MAX_IDR_ID];
Boris Brezillon059e4a62018-12-02 10:54:22 +010088 bool updated;
Heiko Schocherf5895d12014-06-24 10:10:04 +020089};
90
91#define DEFINE_IDR(name) struct idr name;
92
93void idr_remove(struct idr *idp, int id)
94{
Boris Brezillon059e4a62018-12-02 10:54:22 +010095 if (idp->id[id].used) {
Heiko Schocherf5895d12014-06-24 10:10:04 +020096 idp->id[id].used = 0;
Boris Brezillon059e4a62018-12-02 10:54:22 +010097 idp->updated = true;
98 }
Heiko Schocherf5895d12014-06-24 10:10:04 +020099
100 return;
101}
102void *idr_find(struct idr *idp, int id)
103{
104 if (idp->id[id].used)
105 return idp->id[id].ptr;
106
107 return NULL;
108}
109
110void *idr_get_next(struct idr *idp, int *next)
111{
112 void *ret;
113 int id = *next;
114
115 ret = idr_find(idp, id);
116 if (ret) {
117 id ++;
118 if (!idp->id[id].used)
119 id = 0;
120 *next = id;
121 } else {
122 *next = 0;
123 }
124
125 return ret;
126}
127
128int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
129{
130 struct idr_layer *idl;
131 int i = 0;
132
133 while (i < MAX_IDR_ID) {
134 idl = &idp->id[i];
135 if (idl->used == 0) {
136 idl->used = 1;
137 idl->ptr = ptr;
Boris Brezillon059e4a62018-12-02 10:54:22 +0100138 idp->updated = true;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200139 return i;
140 }
141 i++;
142 }
143 return -ENOSPC;
144}
145#endif
146
147static DEFINE_IDR(mtd_idr);
148
149/* These are exported solely for the purpose of mtd_blkdevs.c. You
150 should not use them for _anything_ else */
151DEFINE_MUTEX(mtd_table_mutex);
152EXPORT_SYMBOL_GPL(mtd_table_mutex);
153
154struct mtd_info *__mtd_next_device(int i)
155{
156 return idr_get_next(&mtd_idr, &i);
157}
158EXPORT_SYMBOL_GPL(__mtd_next_device);
159
Boris Brezillon059e4a62018-12-02 10:54:22 +0100160bool mtd_dev_list_updated(void)
161{
162 if (mtd_idr.updated) {
163 mtd_idr.updated = false;
164 return true;
165 }
166
167 return false;
168}
169
Heiko Schocherf5895d12014-06-24 10:10:04 +0200170#ifndef __UBOOT__
171static LIST_HEAD(mtd_notifiers);
172
173
174#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
175
176/* REVISIT once MTD uses the driver model better, whoever allocates
177 * the mtd_info will probably want to use the release() hook...
178 */
179static void mtd_release(struct device *dev)
180{
181 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
182 dev_t index = MTD_DEVT(mtd->index);
183
184 /* remove /dev/mtdXro node if needed */
185 if (index)
186 device_destroy(&mtd_class, index + 1);
187}
188
189static int mtd_cls_suspend(struct device *dev, pm_message_t state)
190{
191 struct mtd_info *mtd = dev_get_drvdata(dev);
192
193 return mtd ? mtd_suspend(mtd) : 0;
194}
195
196static int mtd_cls_resume(struct device *dev)
197{
198 struct mtd_info *mtd = dev_get_drvdata(dev);
199
200 if (mtd)
201 mtd_resume(mtd);
202 return 0;
203}
204
205static ssize_t mtd_type_show(struct device *dev,
206 struct device_attribute *attr, char *buf)
207{
208 struct mtd_info *mtd = dev_get_drvdata(dev);
209 char *type;
210
211 switch (mtd->type) {
212 case MTD_ABSENT:
213 type = "absent";
214 break;
215 case MTD_RAM:
216 type = "ram";
217 break;
218 case MTD_ROM:
219 type = "rom";
220 break;
221 case MTD_NORFLASH:
222 type = "nor";
223 break;
224 case MTD_NANDFLASH:
225 type = "nand";
226 break;
227 case MTD_DATAFLASH:
228 type = "dataflash";
229 break;
230 case MTD_UBIVOLUME:
231 type = "ubi";
232 break;
233 case MTD_MLCNANDFLASH:
234 type = "mlc-nand";
235 break;
236 default:
237 type = "unknown";
238 }
239
240 return snprintf(buf, PAGE_SIZE, "%s\n", type);
241}
242static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
243
244static ssize_t mtd_flags_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, "0x%lx\n", (unsigned long)mtd->flags);
250
251}
252static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
253
254static ssize_t mtd_size_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
256{
257 struct mtd_info *mtd = dev_get_drvdata(dev);
258
259 return snprintf(buf, PAGE_SIZE, "%llu\n",
260 (unsigned long long)mtd->size);
261
262}
263static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
264
265static ssize_t mtd_erasesize_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
267{
268 struct mtd_info *mtd = dev_get_drvdata(dev);
269
270 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
271
272}
273static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
274
275static ssize_t mtd_writesize_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
277{
278 struct mtd_info *mtd = dev_get_drvdata(dev);
279
280 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
281
282}
283static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
284
285static ssize_t mtd_subpagesize_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct mtd_info *mtd = dev_get_drvdata(dev);
289 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
290
291 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
292
293}
294static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
295
296static ssize_t mtd_oobsize_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
299 struct mtd_info *mtd = dev_get_drvdata(dev);
300
301 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
302
303}
304static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
305
306static ssize_t mtd_numeraseregions_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct mtd_info *mtd = dev_get_drvdata(dev);
310
311 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
312
313}
314static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
315 NULL);
316
317static ssize_t mtd_name_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
319{
320 struct mtd_info *mtd = dev_get_drvdata(dev);
321
322 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
323
324}
325static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
326
327static ssize_t mtd_ecc_strength_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329{
330 struct mtd_info *mtd = dev_get_drvdata(dev);
331
332 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
333}
334static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
335
336static ssize_t mtd_bitflip_threshold_show(struct device *dev,
337 struct device_attribute *attr,
338 char *buf)
339{
340 struct mtd_info *mtd = dev_get_drvdata(dev);
341
342 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
343}
344
345static ssize_t mtd_bitflip_threshold_store(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
348{
349 struct mtd_info *mtd = dev_get_drvdata(dev);
350 unsigned int bitflip_threshold;
351 int retval;
352
353 retval = kstrtouint(buf, 0, &bitflip_threshold);
354 if (retval)
355 return retval;
356
357 mtd->bitflip_threshold = bitflip_threshold;
358 return count;
359}
360static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
361 mtd_bitflip_threshold_show,
362 mtd_bitflip_threshold_store);
363
364static ssize_t mtd_ecc_step_size_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
367 struct mtd_info *mtd = dev_get_drvdata(dev);
368
369 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
370
371}
372static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
373
374static struct attribute *mtd_attrs[] = {
375 &dev_attr_type.attr,
376 &dev_attr_flags.attr,
377 &dev_attr_size.attr,
378 &dev_attr_erasesize.attr,
379 &dev_attr_writesize.attr,
380 &dev_attr_subpagesize.attr,
381 &dev_attr_oobsize.attr,
382 &dev_attr_numeraseregions.attr,
383 &dev_attr_name.attr,
384 &dev_attr_ecc_strength.attr,
385 &dev_attr_ecc_step_size.attr,
386 &dev_attr_bitflip_threshold.attr,
387 NULL,
388};
389ATTRIBUTE_GROUPS(mtd);
390
391static struct device_type mtd_devtype = {
392 .name = "mtd",
393 .groups = mtd_groups,
394 .release = mtd_release,
395};
396#endif
397
398/**
399 * add_mtd_device - register an MTD device
400 * @mtd: pointer to new MTD device info structure
401 *
402 * Add a device to the list of MTD devices present in the system, and
403 * notify each currently active MTD 'user' of its arrival. Returns
404 * zero on success or 1 on failure, which currently will only happen
405 * if there is insufficient memory or a sysfs error.
406 */
407
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100408int add_mtd_device(struct mtd_info *mtd)
409{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200410#ifndef __UBOOT__
411 struct mtd_notifier *not;
412#endif
413 int i, error;
414
415#ifndef __UBOOT__
416 if (!mtd->backing_dev_info) {
417 switch (mtd->type) {
418 case MTD_RAM:
419 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
420 break;
421 case MTD_ROM:
422 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
423 break;
424 default:
425 mtd->backing_dev_info = &mtd_bdi_unmappable;
426 break;
427 }
428 }
429#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100430
431 BUG_ON(mtd->writesize == 0);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200432 mutex_lock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100433
Heiko Schocherf5895d12014-06-24 10:10:04 +0200434 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
435 if (i < 0)
436 goto fail_locked;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100437
Heiko Schocherf5895d12014-06-24 10:10:04 +0200438 mtd->index = i;
439 mtd->usecount = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000440
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200441 INIT_LIST_HEAD(&mtd->partitions);
442
Heiko Schocherf5895d12014-06-24 10:10:04 +0200443 /* default value if not set by driver */
444 if (mtd->bitflip_threshold == 0)
445 mtd->bitflip_threshold = mtd->ecc_strength;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000446
Heiko Schocherf5895d12014-06-24 10:10:04 +0200447 if (is_power_of_2(mtd->erasesize))
448 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
449 else
450 mtd->erasesize_shift = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100451
Heiko Schocherf5895d12014-06-24 10:10:04 +0200452 if (is_power_of_2(mtd->writesize))
453 mtd->writesize_shift = ffs(mtd->writesize) - 1;
454 else
455 mtd->writesize_shift = 0;
456
457 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
458 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
459
460 /* Some chips always power up locked. Unlock them now */
461 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
462 error = mtd_unlock(mtd, 0, mtd->size);
463 if (error && error != -EOPNOTSUPP)
464 printk(KERN_WARNING
465 "%s: unlock failed, writes may not work\n",
466 mtd->name);
467 }
468
469#ifndef __UBOOT__
470 /* Caller should have set dev.parent to match the
471 * physical device.
472 */
473 mtd->dev.type = &mtd_devtype;
474 mtd->dev.class = &mtd_class;
475 mtd->dev.devt = MTD_DEVT(i);
476 dev_set_name(&mtd->dev, "mtd%d", i);
477 dev_set_drvdata(&mtd->dev, mtd);
478 if (device_register(&mtd->dev) != 0)
479 goto fail_added;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100480
Heiko Schocherf5895d12014-06-24 10:10:04 +0200481 if (MTD_DEVT(i))
482 device_create(&mtd_class, mtd->dev.parent,
483 MTD_DEVT(i) + 1,
484 NULL, "mtd%dro", i);
485
486 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
487 /* No need to get a refcount on the module containing
488 the notifier, since we hold the mtd_table_mutex */
489 list_for_each_entry(not, &mtd_notifiers, list)
490 not->add(mtd);
Heiko Schocherb24c4272014-07-15 16:08:42 +0200491#else
492 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200493#endif
494
495 mutex_unlock(&mtd_table_mutex);
496 /* We _know_ we aren't being removed, because
497 our caller is still holding us here. So none
498 of this try_ nonsense, and no bitching about it
499 either. :) */
500 __module_get(THIS_MODULE);
501 return 0;
502
503#ifndef __UBOOT__
504fail_added:
505 idr_remove(&mtd_idr, i);
506#endif
507fail_locked:
508 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100509 return 1;
510}
511
512/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200513 * del_mtd_device - unregister an MTD device
514 * @mtd: pointer to MTD device info structure
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100515 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200516 * Remove a device from the list of MTD devices present in the system,
517 * and notify each currently active MTD 'user' of its departure.
518 * Returns zero on success or 1 on failure, which currently will happen
519 * if the requested device does not appear to be present in the list.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100520 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200521
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100522int del_mtd_device(struct mtd_info *mtd)
523{
524 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200525#ifndef __UBOOT__
526 struct mtd_notifier *not;
527#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100528
Boris Brezillondb0d8052018-12-02 10:54:24 +0100529 ret = del_mtd_partitions(mtd);
530 if (ret) {
531 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
532 mtd->name, ret);
533 return ret;
534 }
535
Heiko Schocherf5895d12014-06-24 10:10:04 +0200536 mutex_lock(&mtd_table_mutex);
537
538 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100539 ret = -ENODEV;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200540 goto out_error;
541 }
542
543#ifndef __UBOOT__
544 /* No need to get a refcount on the module containing
545 the notifier, since we hold the mtd_table_mutex */
546 list_for_each_entry(not, &mtd_notifiers, list)
547 not->remove(mtd);
548#endif
549
550 if (mtd->usecount) {
551 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
552 mtd->index, mtd->name, mtd->usecount);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100553 ret = -EBUSY;
554 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200555#ifndef __UBOOT__
556 device_unregister(&mtd->dev);
557#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100558
Heiko Schocherf5895d12014-06-24 10:10:04 +0200559 idr_remove(&mtd_idr, mtd->index);
560
561 module_put(THIS_MODULE);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100562 ret = 0;
563 }
564
Heiko Schocherf5895d12014-06-24 10:10:04 +0200565out_error:
566 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100567 return ret;
568}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200569
570#ifndef __UBOOT__
571/**
572 * mtd_device_parse_register - parse partitions and register an MTD device.
573 *
574 * @mtd: the MTD device to register
575 * @types: the list of MTD partition probes to try, see
576 * 'parse_mtd_partitions()' for more information
577 * @parser_data: MTD partition parser-specific data
578 * @parts: fallback partition information to register, if parsing fails;
579 * only valid if %nr_parts > %0
580 * @nr_parts: the number of partitions in parts, if zero then the full
581 * MTD device is registered if no partition info is found
582 *
583 * This function aggregates MTD partitions parsing (done by
584 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
585 * basically follows the most common pattern found in many MTD drivers:
586 *
587 * * It first tries to probe partitions on MTD device @mtd using parsers
588 * specified in @types (if @types is %NULL, then the default list of parsers
589 * is used, see 'parse_mtd_partitions()' for more information). If none are
590 * found this functions tries to fallback to information specified in
591 * @parts/@nr_parts.
592 * * If any partitioning info was found, this function registers the found
593 * partitions.
594 * * If no partitions were found this function just registers the MTD device
595 * @mtd and exits.
596 *
597 * Returns zero in case of success and a negative error code in case of failure.
598 */
599int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
600 struct mtd_part_parser_data *parser_data,
601 const struct mtd_partition *parts,
602 int nr_parts)
603{
604 int err;
605 struct mtd_partition *real_parts;
606
607 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
608 if (err <= 0 && nr_parts && parts) {
609 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
610 GFP_KERNEL);
611 if (!real_parts)
612 err = -ENOMEM;
613 else
614 err = nr_parts;
615 }
616
617 if (err > 0) {
618 err = add_mtd_partitions(mtd, real_parts, err);
619 kfree(real_parts);
620 } else if (err == 0) {
621 err = add_mtd_device(mtd);
622 if (err == 1)
623 err = -ENODEV;
624 }
625
626 return err;
627}
628EXPORT_SYMBOL_GPL(mtd_device_parse_register);
629
630/**
631 * mtd_device_unregister - unregister an existing MTD device.
632 *
633 * @master: the MTD device to unregister. This will unregister both the master
634 * and any partitions if registered.
635 */
636int mtd_device_unregister(struct mtd_info *master)
637{
638 int err;
639
640 err = del_mtd_partitions(master);
641 if (err)
642 return err;
643
644 if (!device_is_registered(&master->dev))
645 return 0;
646
647 return del_mtd_device(master);
648}
649EXPORT_SYMBOL_GPL(mtd_device_unregister);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100650
651/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200652 * register_mtd_user - register a 'user' of MTD devices.
653 * @new: pointer to notifier info structure
654 *
655 * Registers a pair of callbacks function to be called upon addition
656 * or removal of MTD devices. Causes the 'add' callback to be immediately
657 * invoked for each MTD device currently present in the system.
658 */
659void register_mtd_user (struct mtd_notifier *new)
660{
661 struct mtd_info *mtd;
662
663 mutex_lock(&mtd_table_mutex);
664
665 list_add(&new->list, &mtd_notifiers);
666
667 __module_get(THIS_MODULE);
668
669 mtd_for_each_device(mtd)
670 new->add(mtd);
671
672 mutex_unlock(&mtd_table_mutex);
673}
674EXPORT_SYMBOL_GPL(register_mtd_user);
675
676/**
677 * unregister_mtd_user - unregister a 'user' of MTD devices.
678 * @old: pointer to notifier info structure
679 *
680 * Removes a callback function pair from the list of 'users' to be
681 * notified upon addition or removal of MTD devices. Causes the
682 * 'remove' callback to be immediately invoked for each MTD device
683 * currently present in the system.
684 */
685int unregister_mtd_user (struct mtd_notifier *old)
686{
687 struct mtd_info *mtd;
688
689 mutex_lock(&mtd_table_mutex);
690
691 module_put(THIS_MODULE);
692
693 mtd_for_each_device(mtd)
694 old->remove(mtd);
695
696 list_del(&old->list);
697 mutex_unlock(&mtd_table_mutex);
698 return 0;
699}
700EXPORT_SYMBOL_GPL(unregister_mtd_user);
701#endif
702
703/**
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100704 * get_mtd_device - obtain a validated handle for an MTD device
705 * @mtd: last known address of the required MTD device
706 * @num: internal device number of the required MTD device
707 *
708 * Given a number and NULL address, return the num'th entry in the device
Heiko Schocherf5895d12014-06-24 10:10:04 +0200709 * table, if any. Given an address and num == -1, search the device table
710 * for a device with that address and return if it's still present. Given
711 * both, return the num'th driver only if its address matches. Return
712 * error code if not.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100713 */
714struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
715{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200716 struct mtd_info *ret = NULL, *other;
717 int err = -ENODEV;
718
719 mutex_lock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100720
721 if (num == -1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200722 mtd_for_each_device(other) {
723 if (other == mtd) {
724 ret = mtd;
725 break;
726 }
727 }
728 } else if (num >= 0) {
729 ret = idr_find(&mtd_idr, num);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100730 if (mtd && mtd != ret)
731 ret = NULL;
732 }
733
Heiko Schocherf5895d12014-06-24 10:10:04 +0200734 if (!ret) {
735 ret = ERR_PTR(err);
736 goto out;
737 }
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100738
Heiko Schocherf5895d12014-06-24 10:10:04 +0200739 err = __get_mtd_device(ret);
740 if (err)
741 ret = ERR_PTR(err);
742out:
743 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100744 return ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200745}
746EXPORT_SYMBOL_GPL(get_mtd_device);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100747
Heiko Schocherf5895d12014-06-24 10:10:04 +0200748
749int __get_mtd_device(struct mtd_info *mtd)
750{
751 int err;
752
753 if (!try_module_get(mtd->owner))
754 return -ENODEV;
755
756 if (mtd->_get_device) {
757 err = mtd->_get_device(mtd);
758
759 if (err) {
760 module_put(mtd->owner);
761 return err;
762 }
763 }
764 mtd->usecount++;
765 return 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100766}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200767EXPORT_SYMBOL_GPL(__get_mtd_device);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100768
769/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200770 * get_mtd_device_nm - obtain a validated handle for an MTD device by
771 * device name
772 * @name: MTD device name to open
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100773 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200774 * This function returns MTD device description structure in case of
775 * success and an error code in case of failure.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100776 */
777struct mtd_info *get_mtd_device_nm(const char *name)
778{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200779 int err = -ENODEV;
780 struct mtd_info *mtd = NULL, *other;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100781
Heiko Schocherf5895d12014-06-24 10:10:04 +0200782 mutex_lock(&mtd_table_mutex);
783
784 mtd_for_each_device(other) {
785 if (!strcmp(name, other->name)) {
786 mtd = other;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100787 break;
788 }
789 }
790
791 if (!mtd)
792 goto out_unlock;
793
Heiko Schocherf5895d12014-06-24 10:10:04 +0200794 err = __get_mtd_device(mtd);
795 if (err)
796 goto out_unlock;
797
798 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100799 return mtd;
800
801out_unlock:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200802 mutex_unlock(&mtd_table_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100803 return ERR_PTR(err);
804}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200805EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Ben Gardiner50bae732010-08-31 17:48:01 -0400806
807#if defined(CONFIG_CMD_MTDPARTS_SPREAD)
808/**
809 * mtd_get_len_incl_bad
810 *
811 * Check if length including bad blocks fits into device.
812 *
813 * @param mtd an MTD device
814 * @param offset offset in flash
815 * @param length image length
816 * @return image length including bad blocks in *len_incl_bad and whether or not
817 * the length returned was truncated in *truncated
818 */
819void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
820 const uint64_t length, uint64_t *len_incl_bad,
821 int *truncated)
822{
823 *truncated = 0;
824 *len_incl_bad = 0;
825
maxin.john@enea.comb5ee6e22014-09-08 19:04:16 +0200826 if (!mtd->_block_isbad) {
Ben Gardiner50bae732010-08-31 17:48:01 -0400827 *len_incl_bad = length;
828 return;
829 }
830
831 uint64_t len_excl_bad = 0;
832 uint64_t block_len;
833
834 while (len_excl_bad < length) {
Scott Wood10390ce2010-09-09 15:40:03 -0500835 if (offset >= mtd->size) {
836 *truncated = 1;
837 return;
838 }
839
Ben Gardiner50bae732010-08-31 17:48:01 -0400840 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
841
maxin.john@enea.comb5ee6e22014-09-08 19:04:16 +0200842 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
Ben Gardiner50bae732010-08-31 17:48:01 -0400843 len_excl_bad += block_len;
844
845 *len_incl_bad += block_len;
846 offset += block_len;
Ben Gardiner50bae732010-08-31 17:48:01 -0400847 }
848}
849#endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000850
Heiko Schocherf5895d12014-06-24 10:10:04 +0200851void put_mtd_device(struct mtd_info *mtd)
852{
853 mutex_lock(&mtd_table_mutex);
854 __put_mtd_device(mtd);
855 mutex_unlock(&mtd_table_mutex);
856
857}
858EXPORT_SYMBOL_GPL(put_mtd_device);
859
860void __put_mtd_device(struct mtd_info *mtd)
861{
862 --mtd->usecount;
863 BUG_ON(mtd->usecount < 0);
864
865 if (mtd->_put_device)
866 mtd->_put_device(mtd);
867
868 module_put(mtd->owner);
869}
870EXPORT_SYMBOL_GPL(__put_mtd_device);
871
872/*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000873 * Erase is an asynchronous operation. Device drivers are supposed
874 * to call instr->callback() whenever the operation completes, even
875 * if it completes with a failure.
876 * Callers are supposed to pass a callback function and wait for it
877 * to be called before writing to the block.
878 */
879int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
880{
881 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
882 return -EINVAL;
883 if (!(mtd->flags & MTD_WRITEABLE))
884 return -EROFS;
885 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
886 if (!instr->len) {
887 instr->state = MTD_ERASE_DONE;
888 mtd_erase_callback(instr);
889 return 0;
890 }
891 return mtd->_erase(mtd, instr);
892}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200893EXPORT_SYMBOL_GPL(mtd_erase);
894
895#ifndef __UBOOT__
896/*
897 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
898 */
899int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
900 void **virt, resource_size_t *phys)
901{
902 *retlen = 0;
903 *virt = NULL;
904 if (phys)
905 *phys = 0;
906 if (!mtd->_point)
907 return -EOPNOTSUPP;
908 if (from < 0 || from > mtd->size || len > mtd->size - from)
909 return -EINVAL;
910 if (!len)
911 return 0;
912 return mtd->_point(mtd, from, len, retlen, virt, phys);
913}
914EXPORT_SYMBOL_GPL(mtd_point);
915
916/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
917int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
918{
919 if (!mtd->_point)
920 return -EOPNOTSUPP;
921 if (from < 0 || from > mtd->size || len > mtd->size - from)
922 return -EINVAL;
923 if (!len)
924 return 0;
925 return mtd->_unpoint(mtd, from, len);
926}
927EXPORT_SYMBOL_GPL(mtd_unpoint);
928#endif
929
930/*
931 * Allow NOMMU mmap() to directly map the device (if not NULL)
932 * - return the address to which the offset maps
933 * - return -ENOSYS to indicate refusal to do the mapping
934 */
935unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
936 unsigned long offset, unsigned long flags)
937{
938 if (!mtd->_get_unmapped_area)
939 return -EOPNOTSUPP;
940 if (offset > mtd->size || len > mtd->size - offset)
941 return -EINVAL;
942 return mtd->_get_unmapped_area(mtd, len, offset, flags);
943}
944EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000945
946int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
947 u_char *buf)
948{
Paul Burton700a76c2013-09-04 15:16:56 +0100949 int ret_code;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200950 *retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000951 if (from < 0 || from > mtd->size || len > mtd->size - from)
952 return -EINVAL;
953 if (!len)
954 return 0;
Paul Burton700a76c2013-09-04 15:16:56 +0100955
956 /*
957 * In the absence of an error, drivers return a non-negative integer
958 * representing the maximum number of bitflips that were corrected on
959 * any one ecc region (if applicable; zero otherwise).
960 */
Boris Brezillon6c20df72018-08-16 17:29:59 +0200961 if (mtd->_read) {
962 ret_code = mtd->_read(mtd, from, len, retlen, buf);
963 } else if (mtd->_read_oob) {
964 struct mtd_oob_ops ops = {
965 .len = len,
966 .datbuf = buf,
967 };
968
969 ret_code = mtd->_read_oob(mtd, from, &ops);
970 *retlen = ops.retlen;
971 } else {
972 return -ENOTSUPP;
973 }
974
Paul Burton700a76c2013-09-04 15:16:56 +0100975 if (unlikely(ret_code < 0))
976 return ret_code;
977 if (mtd->ecc_strength == 0)
978 return 0; /* device lacks ecc */
979 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000980}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200981EXPORT_SYMBOL_GPL(mtd_read);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000982
983int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
984 const u_char *buf)
985{
986 *retlen = 0;
987 if (to < 0 || to > mtd->size || len > mtd->size - to)
988 return -EINVAL;
Boris Brezillon6c20df72018-08-16 17:29:59 +0200989 if ((!mtd->_write && !mtd->_write_oob) ||
990 !(mtd->flags & MTD_WRITEABLE))
Sergey Lapin3a38a552013-01-14 03:46:50 +0000991 return -EROFS;
992 if (!len)
993 return 0;
Boris Brezillon6c20df72018-08-16 17:29:59 +0200994
995 if (!mtd->_write) {
996 struct mtd_oob_ops ops = {
997 .len = len,
998 .datbuf = (u8 *)buf,
999 };
1000 int ret;
1001
1002 ret = mtd->_write_oob(mtd, to, &ops);
1003 *retlen = ops.retlen;
1004 return ret;
1005 }
1006
Sergey Lapin3a38a552013-01-14 03:46:50 +00001007 return mtd->_write(mtd, to, len, retlen, buf);
1008}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001009EXPORT_SYMBOL_GPL(mtd_write);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001010
1011/*
1012 * In blackbox flight recorder like scenarios we want to make successful writes
1013 * in interrupt context. panic_write() is only intended to be called when its
1014 * known the kernel is about to panic and we need the write to succeed. Since
1015 * the kernel is not going to be running for much longer, this function can
1016 * break locks and delay to ensure the write succeeds (but not sleep).
1017 */
1018int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1019 const u_char *buf)
1020{
1021 *retlen = 0;
1022 if (!mtd->_panic_write)
1023 return -EOPNOTSUPP;
1024 if (to < 0 || to > mtd->size || len > mtd->size - to)
1025 return -EINVAL;
1026 if (!(mtd->flags & MTD_WRITEABLE))
1027 return -EROFS;
1028 if (!len)
1029 return 0;
1030 return mtd->_panic_write(mtd, to, len, retlen, buf);
1031}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001032EXPORT_SYMBOL_GPL(mtd_panic_write);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001033
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001034static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1035 struct mtd_oob_ops *ops)
1036{
1037 /*
1038 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1039 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1040 * this case.
1041 */
1042 if (!ops->datbuf)
1043 ops->len = 0;
1044
1045 if (!ops->oobbuf)
1046 ops->ooblen = 0;
1047
1048 if (offs < 0 || offs + ops->len > mtd->size)
1049 return -EINVAL;
1050
1051 if (ops->ooblen) {
Miquel Raynal3a3146c2018-11-18 21:11:47 +01001052 size_t maxooblen;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001053
1054 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1055 return -EINVAL;
1056
Miquel Raynal3a3146c2018-11-18 21:11:47 +01001057 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1058 mtd_div_by_ws(offs, mtd)) *
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001059 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1060 if (ops->ooblen > maxooblen)
1061 return -EINVAL;
1062 }
1063
1064 return 0;
1065}
1066
Sergey Lapin3a38a552013-01-14 03:46:50 +00001067int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1068{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001069 int ret_code;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001070 ops->retlen = ops->oobretlen = 0;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001071
1072 ret_code = mtd_check_oob_ops(mtd, from, ops);
1073 if (ret_code)
1074 return ret_code;
1075
Miquel Raynal19ea9252018-08-16 17:30:02 +02001076 /* Check the validity of a potential fallback on mtd->_read */
1077 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1078 return -EOPNOTSUPP;
1079
1080 if (mtd->_read_oob)
1081 ret_code = mtd->_read_oob(mtd, from, ops);
1082 else
1083 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1084 ops->datbuf);
1085
Heiko Schocherf5895d12014-06-24 10:10:04 +02001086 /*
1087 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1088 * similar to mtd->_read(), returning a non-negative integer
1089 * representing max bitflips. In other cases, mtd->_read_oob() may
1090 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1091 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001092 if (unlikely(ret_code < 0))
1093 return ret_code;
1094 if (mtd->ecc_strength == 0)
1095 return 0; /* device lacks ecc */
1096 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001097}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001098EXPORT_SYMBOL_GPL(mtd_read_oob);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001099
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001100int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1101 struct mtd_oob_ops *ops)
1102{
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001103 int ret;
1104
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001105 ops->retlen = ops->oobretlen = 0;
Miquel Raynal19ea9252018-08-16 17:30:02 +02001106
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001107 if (!(mtd->flags & MTD_WRITEABLE))
1108 return -EROFS;
Boris Brezillonb74b94f2018-08-16 17:30:01 +02001109
1110 ret = mtd_check_oob_ops(mtd, to, ops);
1111 if (ret)
1112 return ret;
1113
Miquel Raynal19ea9252018-08-16 17:30:02 +02001114 /* Check the validity of a potential fallback on mtd->_write */
1115 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1116 return -EOPNOTSUPP;
1117
1118 if (mtd->_write_oob)
1119 return mtd->_write_oob(mtd, to, ops);
1120 else
1121 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1122 ops->datbuf);
Ezequiel Garcia18e75412018-08-16 17:30:00 +02001123}
1124EXPORT_SYMBOL_GPL(mtd_write_oob);
1125
Boris Brezillone1b1e3a2017-11-22 02:38:23 +09001126/**
1127 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1128 * @mtd: MTD device structure
1129 * @section: ECC section. Depending on the layout you may have all the ECC
1130 * bytes stored in a single contiguous section, or one section
1131 * per ECC chunk (and sometime several sections for a single ECC
1132 * ECC chunk)
1133 * @oobecc: OOB region struct filled with the appropriate ECC position
1134 * information
1135 *
1136 * This function returns ECC section information in the OOB area. If you want
1137 * to get all the ECC bytes information, then you should call
1138 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1139 *
1140 * Returns zero on success, a negative error code otherwise.
1141 */
1142int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1143 struct mtd_oob_region *oobecc)
1144{
1145 memset(oobecc, 0, sizeof(*oobecc));
1146
1147 if (!mtd || section < 0)
1148 return -EINVAL;
1149
1150 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1151 return -ENOTSUPP;
1152
1153 return mtd->ooblayout->ecc(mtd, section, oobecc);
1154}
1155EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1156
1157/**
1158 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1159 * section
1160 * @mtd: MTD device structure
1161 * @section: Free section you are interested in. Depending on the layout
1162 * you may have all the free bytes stored in a single contiguous
1163 * section, or one section per ECC chunk plus an extra section
1164 * for the remaining bytes (or other funky layout).
1165 * @oobfree: OOB region struct filled with the appropriate free position
1166 * information
1167 *
1168 * This function returns free bytes position in the OOB area. If you want
1169 * to get all the free bytes information, then you should call
1170 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1171 *
1172 * Returns zero on success, a negative error code otherwise.
1173 */
1174int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1175 struct mtd_oob_region *oobfree)
1176{
1177 memset(oobfree, 0, sizeof(*oobfree));
1178
1179 if (!mtd || section < 0)
1180 return -EINVAL;
1181
1182 if (!mtd->ooblayout || !mtd->ooblayout->free)
1183 return -ENOTSUPP;
1184
1185 return mtd->ooblayout->free(mtd, section, oobfree);
1186}
1187EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1188
1189/**
1190 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1191 * @mtd: mtd info structure
1192 * @byte: the byte we are searching for
1193 * @sectionp: pointer where the section id will be stored
1194 * @oobregion: used to retrieve the ECC position
1195 * @iter: iterator function. Should be either mtd_ooblayout_free or
1196 * mtd_ooblayout_ecc depending on the region type you're searching for
1197 *
1198 * This function returns the section id and oobregion information of a
1199 * specific byte. For example, say you want to know where the 4th ECC byte is
1200 * stored, you'll use:
1201 *
1202 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1203 *
1204 * Returns zero on success, a negative error code otherwise.
1205 */
1206static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1207 int *sectionp, struct mtd_oob_region *oobregion,
1208 int (*iter)(struct mtd_info *,
1209 int section,
1210 struct mtd_oob_region *oobregion))
1211{
1212 int pos = 0, ret, section = 0;
1213
1214 memset(oobregion, 0, sizeof(*oobregion));
1215
1216 while (1) {
1217 ret = iter(mtd, section, oobregion);
1218 if (ret)
1219 return ret;
1220
1221 if (pos + oobregion->length > byte)
1222 break;
1223
1224 pos += oobregion->length;
1225 section++;
1226 }
1227
1228 /*
1229 * Adjust region info to make it start at the beginning at the
1230 * 'start' ECC byte.
1231 */
1232 oobregion->offset += byte - pos;
1233 oobregion->length -= byte - pos;
1234 *sectionp = section;
1235
1236 return 0;
1237}
1238
1239/**
1240 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1241 * ECC byte
1242 * @mtd: mtd info structure
1243 * @eccbyte: the byte we are searching for
1244 * @sectionp: pointer where the section id will be stored
1245 * @oobregion: OOB region information
1246 *
1247 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1248 * byte.
1249 *
1250 * Returns zero on success, a negative error code otherwise.
1251 */
1252int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1253 int *section,
1254 struct mtd_oob_region *oobregion)
1255{
1256 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1257 mtd_ooblayout_ecc);
1258}
1259EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1260
1261/**
1262 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1263 * @mtd: mtd info structure
1264 * @buf: destination buffer to store OOB bytes
1265 * @oobbuf: OOB buffer
1266 * @start: first byte to retrieve
1267 * @nbytes: number of bytes to retrieve
1268 * @iter: section iterator
1269 *
1270 * Extract bytes attached to a specific category (ECC or free)
1271 * from the OOB buffer and copy them into buf.
1272 *
1273 * Returns zero on success, a negative error code otherwise.
1274 */
1275static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1276 const u8 *oobbuf, int start, int nbytes,
1277 int (*iter)(struct mtd_info *,
1278 int section,
1279 struct mtd_oob_region *oobregion))
1280{
1281 struct mtd_oob_region oobregion;
1282 int section, ret;
1283
1284 ret = mtd_ooblayout_find_region(mtd, start, &section,
1285 &oobregion, iter);
1286
1287 while (!ret) {
1288 int cnt;
1289
1290 cnt = min_t(int, nbytes, oobregion.length);
1291 memcpy(buf, oobbuf + oobregion.offset, cnt);
1292 buf += cnt;
1293 nbytes -= cnt;
1294
1295 if (!nbytes)
1296 break;
1297
1298 ret = iter(mtd, ++section, &oobregion);
1299 }
1300
1301 return ret;
1302}
1303
1304/**
1305 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1306 * @mtd: mtd info structure
1307 * @buf: source buffer to get OOB bytes from
1308 * @oobbuf: OOB buffer
1309 * @start: first OOB byte to set
1310 * @nbytes: number of OOB bytes to set
1311 * @iter: section iterator
1312 *
1313 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1314 * is selected by passing the appropriate iterator.
1315 *
1316 * Returns zero on success, a negative error code otherwise.
1317 */
1318static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1319 u8 *oobbuf, int start, int nbytes,
1320 int (*iter)(struct mtd_info *,
1321 int section,
1322 struct mtd_oob_region *oobregion))
1323{
1324 struct mtd_oob_region oobregion;
1325 int section, ret;
1326
1327 ret = mtd_ooblayout_find_region(mtd, start, &section,
1328 &oobregion, iter);
1329
1330 while (!ret) {
1331 int cnt;
1332
1333 cnt = min_t(int, nbytes, oobregion.length);
1334 memcpy(oobbuf + oobregion.offset, buf, cnt);
1335 buf += cnt;
1336 nbytes -= cnt;
1337
1338 if (!nbytes)
1339 break;
1340
1341 ret = iter(mtd, ++section, &oobregion);
1342 }
1343
1344 return ret;
1345}
1346
1347/**
1348 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1349 * @mtd: mtd info structure
1350 * @iter: category iterator
1351 *
1352 * Count the number of bytes in a given category.
1353 *
1354 * Returns a positive value on success, a negative error code otherwise.
1355 */
1356static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1357 int (*iter)(struct mtd_info *,
1358 int section,
1359 struct mtd_oob_region *oobregion))
1360{
1361 struct mtd_oob_region oobregion;
1362 int section = 0, ret, nbytes = 0;
1363
1364 while (1) {
1365 ret = iter(mtd, section++, &oobregion);
1366 if (ret) {
1367 if (ret == -ERANGE)
1368 ret = nbytes;
1369 break;
1370 }
1371
1372 nbytes += oobregion.length;
1373 }
1374
1375 return ret;
1376}
1377
1378/**
1379 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1380 * @mtd: mtd info structure
1381 * @eccbuf: destination buffer to store ECC bytes
1382 * @oobbuf: OOB buffer
1383 * @start: first ECC byte to retrieve
1384 * @nbytes: number of ECC bytes to retrieve
1385 *
1386 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1387 *
1388 * Returns zero on success, a negative error code otherwise.
1389 */
1390int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1391 const u8 *oobbuf, int start, int nbytes)
1392{
1393 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1394 mtd_ooblayout_ecc);
1395}
1396EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1397
1398/**
1399 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1400 * @mtd: mtd info structure
1401 * @eccbuf: source buffer to get ECC bytes from
1402 * @oobbuf: OOB buffer
1403 * @start: first ECC byte to set
1404 * @nbytes: number of ECC bytes to set
1405 *
1406 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1407 *
1408 * Returns zero on success, a negative error code otherwise.
1409 */
1410int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1411 u8 *oobbuf, int start, int nbytes)
1412{
1413 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1414 mtd_ooblayout_ecc);
1415}
1416EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1417
1418/**
1419 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1420 * @mtd: mtd info structure
1421 * @databuf: destination buffer to store ECC bytes
1422 * @oobbuf: OOB buffer
1423 * @start: first ECC byte to retrieve
1424 * @nbytes: number of ECC bytes to retrieve
1425 *
1426 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1427 *
1428 * Returns zero on success, a negative error code otherwise.
1429 */
1430int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1431 const u8 *oobbuf, int start, int nbytes)
1432{
1433 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1434 mtd_ooblayout_free);
1435}
1436EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1437
1438/**
1439 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1440 * @mtd: mtd info structure
1441 * @eccbuf: source buffer to get data bytes from
1442 * @oobbuf: OOB buffer
1443 * @start: first ECC byte to set
1444 * @nbytes: number of ECC bytes to set
1445 *
1446 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1447 *
1448 * Returns zero on success, a negative error code otherwise.
1449 */
1450int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1451 u8 *oobbuf, int start, int nbytes)
1452{
1453 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1454 mtd_ooblayout_free);
1455}
1456EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1457
1458/**
1459 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1460 * @mtd: mtd info structure
1461 *
1462 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1463 *
1464 * Returns zero on success, a negative error code otherwise.
1465 */
1466int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1467{
1468 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1469}
1470EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1471
1472/**
1473 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1474 * @mtd: mtd info structure
1475 *
1476 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1477 *
1478 * Returns zero on success, a negative error code otherwise.
1479 */
1480int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1481{
1482 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1483}
1484EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1485
Sergey Lapin3a38a552013-01-14 03:46:50 +00001486/*
1487 * Method to access the protection register area, present in some flash
1488 * devices. The user data is one time programmable but the factory data is read
1489 * only.
1490 */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001491int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1492 struct otp_info *buf)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001493{
1494 if (!mtd->_get_fact_prot_info)
1495 return -EOPNOTSUPP;
1496 if (!len)
1497 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001498 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001499}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001500EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001501
1502int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1503 size_t *retlen, u_char *buf)
1504{
1505 *retlen = 0;
1506 if (!mtd->_read_fact_prot_reg)
1507 return -EOPNOTSUPP;
1508 if (!len)
1509 return 0;
1510 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1511}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001512EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001513
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001514int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1515 struct otp_info *buf)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001516{
1517 if (!mtd->_get_user_prot_info)
1518 return -EOPNOTSUPP;
1519 if (!len)
1520 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001521 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001522}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001523EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001524
1525int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1526 size_t *retlen, u_char *buf)
1527{
1528 *retlen = 0;
1529 if (!mtd->_read_user_prot_reg)
1530 return -EOPNOTSUPP;
1531 if (!len)
1532 return 0;
1533 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1534}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001535EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001536
1537int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1538 size_t *retlen, u_char *buf)
1539{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001540 int ret;
1541
Sergey Lapin3a38a552013-01-14 03:46:50 +00001542 *retlen = 0;
1543 if (!mtd->_write_user_prot_reg)
1544 return -EOPNOTSUPP;
1545 if (!len)
1546 return 0;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001547 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1548 if (ret)
1549 return ret;
1550
1551 /*
1552 * If no data could be written at all, we are out of memory and
1553 * must return -ENOSPC.
1554 */
1555 return (*retlen) ? 0 : -ENOSPC;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001556}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001557EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001558
1559int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1560{
1561 if (!mtd->_lock_user_prot_reg)
1562 return -EOPNOTSUPP;
1563 if (!len)
1564 return 0;
1565 return mtd->_lock_user_prot_reg(mtd, from, len);
1566}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001567EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001568
1569/* Chip-supported device locking */
1570int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1571{
1572 if (!mtd->_lock)
1573 return -EOPNOTSUPP;
1574 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1575 return -EINVAL;
1576 if (!len)
1577 return 0;
1578 return mtd->_lock(mtd, ofs, len);
1579}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001580EXPORT_SYMBOL_GPL(mtd_lock);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001581
1582int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1583{
1584 if (!mtd->_unlock)
1585 return -EOPNOTSUPP;
1586 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1587 return -EINVAL;
1588 if (!len)
1589 return 0;
1590 return mtd->_unlock(mtd, ofs, len);
1591}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001592EXPORT_SYMBOL_GPL(mtd_unlock);
1593
1594int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1595{
1596 if (!mtd->_is_locked)
1597 return -EOPNOTSUPP;
1598 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1599 return -EINVAL;
1600 if (!len)
1601 return 0;
1602 return mtd->_is_locked(mtd, ofs, len);
1603}
1604EXPORT_SYMBOL_GPL(mtd_is_locked);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001605
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001606int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001607{
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001608 if (ofs < 0 || ofs > mtd->size)
1609 return -EINVAL;
1610 if (!mtd->_block_isreserved)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001611 return 0;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001612 return mtd->_block_isreserved(mtd, ofs);
1613}
1614EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1615
1616int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1617{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001618 if (ofs < 0 || ofs > mtd->size)
1619 return -EINVAL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001620 if (!mtd->_block_isbad)
1621 return 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001622 return mtd->_block_isbad(mtd, ofs);
1623}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001624EXPORT_SYMBOL_GPL(mtd_block_isbad);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001625
1626int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1627{
1628 if (!mtd->_block_markbad)
1629 return -EOPNOTSUPP;
1630 if (ofs < 0 || ofs > mtd->size)
1631 return -EINVAL;
1632 if (!(mtd->flags & MTD_WRITEABLE))
1633 return -EROFS;
1634 return mtd->_block_markbad(mtd, ofs);
1635}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001636EXPORT_SYMBOL_GPL(mtd_block_markbad);
1637
1638#ifndef __UBOOT__
1639/*
1640 * default_mtd_writev - the default writev method
1641 * @mtd: mtd device description object pointer
1642 * @vecs: the vectors to write
1643 * @count: count of vectors in @vecs
1644 * @to: the MTD device offset to write to
1645 * @retlen: on exit contains the count of bytes written to the MTD device.
1646 *
1647 * This function returns zero in case of success and a negative error code in
1648 * case of failure.
1649 */
1650static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1651 unsigned long count, loff_t to, size_t *retlen)
1652{
1653 unsigned long i;
1654 size_t totlen = 0, thislen;
1655 int ret = 0;
1656
1657 for (i = 0; i < count; i++) {
1658 if (!vecs[i].iov_len)
1659 continue;
1660 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1661 vecs[i].iov_base);
1662 totlen += thislen;
1663 if (ret || thislen != vecs[i].iov_len)
1664 break;
1665 to += vecs[i].iov_len;
1666 }
1667 *retlen = totlen;
1668 return ret;
1669}
1670
1671/*
1672 * mtd_writev - the vector-based MTD write method
1673 * @mtd: mtd device description object pointer
1674 * @vecs: the vectors to write
1675 * @count: count of vectors in @vecs
1676 * @to: the MTD device offset to write to
1677 * @retlen: on exit contains the count of bytes written to the MTD device.
1678 *
1679 * This function returns zero in case of success and a negative error code in
1680 * case of failure.
1681 */
1682int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1683 unsigned long count, loff_t to, size_t *retlen)
1684{
1685 *retlen = 0;
1686 if (!(mtd->flags & MTD_WRITEABLE))
1687 return -EROFS;
1688 if (!mtd->_writev)
1689 return default_mtd_writev(mtd, vecs, count, to, retlen);
1690 return mtd->_writev(mtd, vecs, count, to, retlen);
1691}
1692EXPORT_SYMBOL_GPL(mtd_writev);
1693
1694/**
1695 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1696 * @mtd: mtd device description object pointer
1697 * @size: a pointer to the ideal or maximum size of the allocation, points
1698 * to the actual allocation size on success.
1699 *
1700 * This routine attempts to allocate a contiguous kernel buffer up to
1701 * the specified size, backing off the size of the request exponentially
1702 * until the request succeeds or until the allocation size falls below
1703 * the system page size. This attempts to make sure it does not adversely
1704 * impact system performance, so when allocating more than one page, we
1705 * ask the memory allocator to avoid re-trying, swapping, writing back
1706 * or performing I/O.
1707 *
1708 * Note, this function also makes sure that the allocated buffer is aligned to
1709 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1710 *
1711 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1712 * to handle smaller (i.e. degraded) buffer allocations under low- or
1713 * fragmented-memory situations where such reduced allocations, from a
1714 * requested ideal, are allowed.
1715 *
1716 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1717 */
1718void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1719{
1720 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1721 __GFP_NORETRY | __GFP_NO_KSWAPD;
1722 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1723 void *kbuf;
1724
1725 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1726
1727 while (*size > min_alloc) {
1728 kbuf = kmalloc(*size, flags);
1729 if (kbuf)
1730 return kbuf;
1731
1732 *size >>= 1;
1733 *size = ALIGN(*size, mtd->writesize);
1734 }
1735
1736 /*
1737 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1738 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1739 */
1740 return kmalloc(*size, GFP_KERNEL);
1741}
1742EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1743#endif
1744
1745#ifdef CONFIG_PROC_FS
1746
1747/*====================================================================*/
1748/* Support for /proc/mtd */
1749
1750static int mtd_proc_show(struct seq_file *m, void *v)
1751{
1752 struct mtd_info *mtd;
1753
1754 seq_puts(m, "dev: size erasesize name\n");
1755 mutex_lock(&mtd_table_mutex);
1756 mtd_for_each_device(mtd) {
1757 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1758 mtd->index, (unsigned long long)mtd->size,
1759 mtd->erasesize, mtd->name);
1760 }
1761 mutex_unlock(&mtd_table_mutex);
1762 return 0;
1763}
1764
1765static int mtd_proc_open(struct inode *inode, struct file *file)
1766{
1767 return single_open(file, mtd_proc_show, NULL);
1768}
1769
1770static const struct file_operations mtd_proc_ops = {
1771 .open = mtd_proc_open,
1772 .read = seq_read,
1773 .llseek = seq_lseek,
1774 .release = single_release,
1775};
1776#endif /* CONFIG_PROC_FS */
1777
1778/*====================================================================*/
1779/* Init code */
1780
1781#ifndef __UBOOT__
1782static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1783{
1784 int ret;
1785
1786 ret = bdi_init(bdi);
1787 if (!ret)
1788 ret = bdi_register(bdi, NULL, "%s", name);
1789
1790 if (ret)
1791 bdi_destroy(bdi);
1792
1793 return ret;
1794}
1795
1796static struct proc_dir_entry *proc_mtd;
1797
1798static int __init init_mtd(void)
1799{
1800 int ret;
1801
1802 ret = class_register(&mtd_class);
1803 if (ret)
1804 goto err_reg;
1805
1806 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1807 if (ret)
1808 goto err_bdi1;
1809
1810 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1811 if (ret)
1812 goto err_bdi2;
1813
1814 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1815 if (ret)
1816 goto err_bdi3;
1817
1818 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1819
1820 ret = init_mtdchar();
1821 if (ret)
1822 goto out_procfs;
1823
1824 return 0;
1825
1826out_procfs:
1827 if (proc_mtd)
1828 remove_proc_entry("mtd", NULL);
1829err_bdi3:
1830 bdi_destroy(&mtd_bdi_ro_mappable);
1831err_bdi2:
1832 bdi_destroy(&mtd_bdi_unmappable);
1833err_bdi1:
1834 class_unregister(&mtd_class);
1835err_reg:
1836 pr_err("Error registering mtd class or bdi: %d\n", ret);
1837 return ret;
1838}
1839
1840static void __exit cleanup_mtd(void)
1841{
1842 cleanup_mtdchar();
1843 if (proc_mtd)
1844 remove_proc_entry("mtd", NULL);
1845 class_unregister(&mtd_class);
1846 bdi_destroy(&mtd_bdi_unmappable);
1847 bdi_destroy(&mtd_bdi_ro_mappable);
1848 bdi_destroy(&mtd_bdi_rw_mappable);
1849}
1850
1851module_init(init_mtd);
1852module_exit(cleanup_mtd);
1853#endif
1854
1855MODULE_LICENSE("GPL");
1856MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1857MODULE_DESCRIPTION("Core MTD registration and access routines");