blob: 796ac0729e3a3f84545736f09f849ae18f7d0984 [file] [log] [blame]
Kyungmin Parke29c22f2008-11-19 16:20:36 +01001/*
2 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02005 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2006 Red Hat UK Limited
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
Kyungmin Parke29c22f2008-11-19 16:20:36 +010010 */
11
Heiko Schocherff94bc42014-06-24 10:10:04 +020012#define __UBOOT__
13#ifndef __UBOOT__
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/ptrace.h>
17#include <linux/seq_file.h>
18#include <linux/string.h>
19#include <linux/timer.h>
20#include <linux/major.h>
21#include <linux/fs.h>
22#include <linux/err.h>
23#include <linux/ioctl.h>
24#include <linux/init.h>
25#include <linux/proc_fs.h>
26#include <linux/idr.h>
27#include <linux/backing-dev.h>
28#include <linux/gfp.h>
29#include <linux/slab.h>
30#else
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000031#include <linux/compat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020032#include <linux/err.h>
Kyungmin Parke29c22f2008-11-19 16:20:36 +010033#include <ubi_uboot.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020034#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +010035
Heiko Schocherff94bc42014-06-24 10:10:04 +020036#include <linux/mtd/mtd.h>
37#include <linux/mtd/partitions.h>
38
39#include "mtdcore.h"
40
41#ifndef __UBOOT__
42/*
43 * backing device capabilities for non-mappable devices (such as NAND flash)
44 * - permits private mappings, copies are taken of the data
45 */
46static struct backing_dev_info mtd_bdi_unmappable = {
47 .capabilities = BDI_CAP_MAP_COPY,
48};
49
50/*
51 * backing device capabilities for R/O mappable devices (such as ROM)
52 * - permits private mappings, copies are taken of the data
53 * - permits non-writable shared mappings
54 */
55static struct backing_dev_info mtd_bdi_ro_mappable = {
56 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
57 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
58};
59
60/*
61 * backing device capabilities for writable mappable devices (such as RAM)
62 * - permits private mappings, copies are taken of the data
63 * - permits non-writable shared mappings
64 */
65static struct backing_dev_info mtd_bdi_rw_mappable = {
66 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
67 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
68 BDI_CAP_WRITE_MAP),
69};
70
71static int mtd_cls_suspend(struct device *dev, pm_message_t state);
72static int mtd_cls_resume(struct device *dev);
73
74static struct class mtd_class = {
75 .name = "mtd",
76 .owner = THIS_MODULE,
77 .suspend = mtd_cls_suspend,
78 .resume = mtd_cls_resume,
79};
80#else
Kyungmin Parke29c22f2008-11-19 16:20:36 +010081struct mtd_info *mtd_table[MAX_MTD_DEVICES];
82
Heiko Schocherff94bc42014-06-24 10:10:04 +020083#define MAX_IDR_ID 64
84
85struct idr_layer {
86 int used;
87 void *ptr;
88};
89
90struct idr {
91 struct idr_layer id[MAX_IDR_ID];
92};
93
94#define DEFINE_IDR(name) struct idr name;
95
96void idr_remove(struct idr *idp, int id)
97{
98 if (idp->id[id].used)
99 idp->id[id].used = 0;
100
101 return;
102}
103void *idr_find(struct idr *idp, int id)
104{
105 if (idp->id[id].used)
106 return idp->id[id].ptr;
107
108 return NULL;
109}
110
111void *idr_get_next(struct idr *idp, int *next)
112{
113 void *ret;
114 int id = *next;
115
116 ret = idr_find(idp, id);
117 if (ret) {
118 id ++;
119 if (!idp->id[id].used)
120 id = 0;
121 *next = id;
122 } else {
123 *next = 0;
124 }
125
126 return ret;
127}
128
129int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
130{
131 struct idr_layer *idl;
132 int i = 0;
133
134 while (i < MAX_IDR_ID) {
135 idl = &idp->id[i];
136 if (idl->used == 0) {
137 idl->used = 1;
138 idl->ptr = ptr;
139 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
160#ifndef __UBOOT__
161static LIST_HEAD(mtd_notifiers);
162
163
164#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
165
166/* REVISIT once MTD uses the driver model better, whoever allocates
167 * the mtd_info will probably want to use the release() hook...
168 */
169static void mtd_release(struct device *dev)
170{
171 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
172 dev_t index = MTD_DEVT(mtd->index);
173
174 /* remove /dev/mtdXro node if needed */
175 if (index)
176 device_destroy(&mtd_class, index + 1);
177}
178
179static int mtd_cls_suspend(struct device *dev, pm_message_t state)
180{
181 struct mtd_info *mtd = dev_get_drvdata(dev);
182
183 return mtd ? mtd_suspend(mtd) : 0;
184}
185
186static int mtd_cls_resume(struct device *dev)
187{
188 struct mtd_info *mtd = dev_get_drvdata(dev);
189
190 if (mtd)
191 mtd_resume(mtd);
192 return 0;
193}
194
195static ssize_t mtd_type_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
197{
198 struct mtd_info *mtd = dev_get_drvdata(dev);
199 char *type;
200
201 switch (mtd->type) {
202 case MTD_ABSENT:
203 type = "absent";
204 break;
205 case MTD_RAM:
206 type = "ram";
207 break;
208 case MTD_ROM:
209 type = "rom";
210 break;
211 case MTD_NORFLASH:
212 type = "nor";
213 break;
214 case MTD_NANDFLASH:
215 type = "nand";
216 break;
217 case MTD_DATAFLASH:
218 type = "dataflash";
219 break;
220 case MTD_UBIVOLUME:
221 type = "ubi";
222 break;
223 case MTD_MLCNANDFLASH:
224 type = "mlc-nand";
225 break;
226 default:
227 type = "unknown";
228 }
229
230 return snprintf(buf, PAGE_SIZE, "%s\n", type);
231}
232static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
233
234static ssize_t mtd_flags_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
236{
237 struct mtd_info *mtd = dev_get_drvdata(dev);
238
239 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
240
241}
242static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
243
244static ssize_t mtd_size_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, "%llu\n",
250 (unsigned long long)mtd->size);
251
252}
253static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
254
255static ssize_t mtd_erasesize_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
257{
258 struct mtd_info *mtd = dev_get_drvdata(dev);
259
260 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
261
262}
263static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
264
265static ssize_t mtd_writesize_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->writesize);
271
272}
273static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
274
275static ssize_t mtd_subpagesize_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
277{
278 struct mtd_info *mtd = dev_get_drvdata(dev);
279 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
280
281 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
282
283}
284static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
285
286static ssize_t mtd_oobsize_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 struct mtd_info *mtd = dev_get_drvdata(dev);
290
291 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
292
293}
294static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
295
296static ssize_t mtd_numeraseregions_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, "%u\n", mtd->numeraseregions);
302
303}
304static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
305 NULL);
306
307static ssize_t mtd_name_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
309{
310 struct mtd_info *mtd = dev_get_drvdata(dev);
311
312 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
313
314}
315static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
316
317static ssize_t mtd_ecc_strength_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, "%u\n", mtd->ecc_strength);
323}
324static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
325
326static ssize_t mtd_bitflip_threshold_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
330 struct mtd_info *mtd = dev_get_drvdata(dev);
331
332 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
333}
334
335static ssize_t mtd_bitflip_threshold_store(struct device *dev,
336 struct device_attribute *attr,
337 const char *buf, size_t count)
338{
339 struct mtd_info *mtd = dev_get_drvdata(dev);
340 unsigned int bitflip_threshold;
341 int retval;
342
343 retval = kstrtouint(buf, 0, &bitflip_threshold);
344 if (retval)
345 return retval;
346
347 mtd->bitflip_threshold = bitflip_threshold;
348 return count;
349}
350static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
351 mtd_bitflip_threshold_show,
352 mtd_bitflip_threshold_store);
353
354static ssize_t mtd_ecc_step_size_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
357 struct mtd_info *mtd = dev_get_drvdata(dev);
358
359 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
360
361}
362static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
363
364static struct attribute *mtd_attrs[] = {
365 &dev_attr_type.attr,
366 &dev_attr_flags.attr,
367 &dev_attr_size.attr,
368 &dev_attr_erasesize.attr,
369 &dev_attr_writesize.attr,
370 &dev_attr_subpagesize.attr,
371 &dev_attr_oobsize.attr,
372 &dev_attr_numeraseregions.attr,
373 &dev_attr_name.attr,
374 &dev_attr_ecc_strength.attr,
375 &dev_attr_ecc_step_size.attr,
376 &dev_attr_bitflip_threshold.attr,
377 NULL,
378};
379ATTRIBUTE_GROUPS(mtd);
380
381static struct device_type mtd_devtype = {
382 .name = "mtd",
383 .groups = mtd_groups,
384 .release = mtd_release,
385};
386#endif
387
388/**
389 * add_mtd_device - register an MTD device
390 * @mtd: pointer to new MTD device info structure
391 *
392 * Add a device to the list of MTD devices present in the system, and
393 * notify each currently active MTD 'user' of its arrival. Returns
394 * zero on success or 1 on failure, which currently will only happen
395 * if there is insufficient memory or a sysfs error.
396 */
397
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100398int add_mtd_device(struct mtd_info *mtd)
399{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200400#ifndef __UBOOT__
401 struct mtd_notifier *not;
402#endif
403 int i, error;
404
405#ifndef __UBOOT__
406 if (!mtd->backing_dev_info) {
407 switch (mtd->type) {
408 case MTD_RAM:
409 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
410 break;
411 case MTD_ROM:
412 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
413 break;
414 default:
415 mtd->backing_dev_info = &mtd_bdi_unmappable;
416 break;
417 }
418 }
419#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100420
421 BUG_ON(mtd->writesize == 0);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200422 mutex_lock(&mtd_table_mutex);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100423
Heiko Schocherff94bc42014-06-24 10:10:04 +0200424 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
425 if (i < 0)
426 goto fail_locked;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100427
Heiko Schocherff94bc42014-06-24 10:10:04 +0200428 mtd->index = i;
429 mtd->usecount = 0;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000430
Heiko Schocherff94bc42014-06-24 10:10:04 +0200431 /* default value if not set by driver */
432 if (mtd->bitflip_threshold == 0)
433 mtd->bitflip_threshold = mtd->ecc_strength;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000434
Heiko Schocherff94bc42014-06-24 10:10:04 +0200435 if (is_power_of_2(mtd->erasesize))
436 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
437 else
438 mtd->erasesize_shift = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100439
Heiko Schocherff94bc42014-06-24 10:10:04 +0200440 if (is_power_of_2(mtd->writesize))
441 mtd->writesize_shift = ffs(mtd->writesize) - 1;
442 else
443 mtd->writesize_shift = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100444
Heiko Schocherff94bc42014-06-24 10:10:04 +0200445 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
446 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
447
448 /* Some chips always power up locked. Unlock them now */
449 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
450 error = mtd_unlock(mtd, 0, mtd->size);
451 if (error && error != -EOPNOTSUPP)
452 printk(KERN_WARNING
453 "%s: unlock failed, writes may not work\n",
454 mtd->name);
455 }
456
457#ifndef __UBOOT__
458 /* Caller should have set dev.parent to match the
459 * physical device.
460 */
461 mtd->dev.type = &mtd_devtype;
462 mtd->dev.class = &mtd_class;
463 mtd->dev.devt = MTD_DEVT(i);
464 dev_set_name(&mtd->dev, "mtd%d", i);
465 dev_set_drvdata(&mtd->dev, mtd);
466 if (device_register(&mtd->dev) != 0)
467 goto fail_added;
468
469 if (MTD_DEVT(i))
470 device_create(&mtd_class, mtd->dev.parent,
471 MTD_DEVT(i) + 1,
472 NULL, "mtd%dro", i);
473
474 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
475 /* No need to get a refcount on the module containing
476 the notifier, since we hold the mtd_table_mutex */
477 list_for_each_entry(not, &mtd_notifiers, list)
478 not->add(mtd);
479#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 Parke29c22f2008-11-19 16:20:36 +0100495 return 1;
496}
497
498/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200499 * del_mtd_device - unregister an MTD device
500 * @mtd: pointer to MTD device info structure
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100501 *
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100506 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200507
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100508int del_mtd_device(struct mtd_info *mtd)
509{
510 int ret;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200511#ifndef __UBOOT__
512 struct mtd_notifier *not;
513#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100514
Heiko Schocherff94bc42014-06-24 10:10:04 +0200515 mutex_lock(&mtd_table_mutex);
516
517 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100518 ret = -ENODEV;
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100532 ret = -EBUSY;
533 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200534#ifndef __UBOOT__
535 device_unregister(&mtd->dev);
536#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100537
Heiko Schocherff94bc42014-06-24 10:10:04 +0200538 idr_remove(&mtd_idr, mtd->index);
539
540 module_put(THIS_MODULE);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100541 ret = 0;
542 }
543
Heiko Schocherff94bc42014-06-24 10:10:04 +0200544out_error:
545 mutex_unlock(&mtd_table_mutex);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100546 return ret;
547}
548
Heiko Schocherff94bc42014-06-24 10:10:04 +0200549#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);
629
630/**
631 * 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
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100682/**
683 * 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 Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100692 */
693struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
694{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200695 struct mtd_info *ret = NULL, *other;
696 int err = -ENODEV;
697
698 mutex_lock(&mtd_table_mutex);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100699
700 if (num == -1) {
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100709 if (mtd && mtd != ret)
710 ret = NULL;
711 }
712
Heiko Schocherff94bc42014-06-24 10:10:04 +0200713 if (!ret) {
714 ret = ERR_PTR(err);
715 goto out;
716 }
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100717
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100723 return ret;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100724}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200725EXPORT_SYMBOL_GPL(get_mtd_device);
726
727
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;
745}
746EXPORT_SYMBOL_GPL(__get_mtd_device);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100747
748/**
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100752 *
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100755 */
756struct mtd_info *get_mtd_device_nm(const char *name)
757{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200758 int err = -ENODEV;
759 struct mtd_info *mtd = NULL, *other;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100760
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100766 break;
767 }
768 }
769
770 if (!mtd)
771 goto out_unlock;
772
Heiko Schocherff94bc42014-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 Parke29c22f2008-11-19 16:20:36 +0100778 return mtd;
779
780out_unlock:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200781 mutex_unlock(&mtd_table_mutex);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100782 return ERR_PTR(err);
783}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200784EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Ben Gardiner4ba692f2010-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
Ben Gardiner4ba692f2010-08-31 17:48:01 -0400805 if (!mtd->block_isbad) {
806 *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 Wood36650ca2010-09-09 15:40:03 -0500814 if (offset >= mtd->size) {
815 *truncated = 1;
816 return;
817 }
818
Ben Gardiner4ba692f2010-08-31 17:48:01 -0400819 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
820
821 if (!mtd->block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
822 len_excl_bad += block_len;
823
824 *len_incl_bad += block_len;
825 offset += block_len;
Ben Gardiner4ba692f2010-08-31 17:48:01 -0400826 }
827}
828#endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000829
Heiko Schocherff94bc42014-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 Lapindfe64e22013-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 Schocherff94bc42014-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 Lapindfe64e22013-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 Burton40462e52013-09-04 15:16:56 +0100928 int ret_code;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200929 *retlen = 0;
Sergey Lapindfe64e22013-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 Burton40462e52013-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 */
940 ret_code = mtd->_read(mtd, from, len, retlen, buf);
941 if (unlikely(ret_code < 0))
942 return ret_code;
943 if (mtd->ecc_strength == 0)
944 return 0; /* device lacks ecc */
945 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000946}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200947EXPORT_SYMBOL_GPL(mtd_read);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000948
949int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
950 const u_char *buf)
951{
952 *retlen = 0;
953 if (to < 0 || to > mtd->size || len > mtd->size - to)
954 return -EINVAL;
955 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
956 return -EROFS;
957 if (!len)
958 return 0;
959 return mtd->_write(mtd, to, len, retlen, buf);
960}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200961EXPORT_SYMBOL_GPL(mtd_write);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000962
963/*
964 * In blackbox flight recorder like scenarios we want to make successful writes
965 * in interrupt context. panic_write() is only intended to be called when its
966 * known the kernel is about to panic and we need the write to succeed. Since
967 * the kernel is not going to be running for much longer, this function can
968 * break locks and delay to ensure the write succeeds (but not sleep).
969 */
970int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
971 const u_char *buf)
972{
973 *retlen = 0;
974 if (!mtd->_panic_write)
975 return -EOPNOTSUPP;
976 if (to < 0 || to > mtd->size || len > mtd->size - to)
977 return -EINVAL;
978 if (!(mtd->flags & MTD_WRITEABLE))
979 return -EROFS;
980 if (!len)
981 return 0;
982 return mtd->_panic_write(mtd, to, len, retlen, buf);
983}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200984EXPORT_SYMBOL_GPL(mtd_panic_write);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000985
986int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
987{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200988 int ret_code;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000989 ops->retlen = ops->oobretlen = 0;
990 if (!mtd->_read_oob)
991 return -EOPNOTSUPP;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200992 /*
993 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
994 * similar to mtd->_read(), returning a non-negative integer
995 * representing max bitflips. In other cases, mtd->_read_oob() may
996 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
997 */
998 ret_code = mtd->_read_oob(mtd, from, ops);
999 if (unlikely(ret_code < 0))
1000 return ret_code;
1001 if (mtd->ecc_strength == 0)
1002 return 0; /* device lacks ecc */
1003 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001004}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001005EXPORT_SYMBOL_GPL(mtd_read_oob);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001006
1007/*
1008 * Method to access the protection register area, present in some flash
1009 * devices. The user data is one time programmable but the factory data is read
1010 * only.
1011 */
1012int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
1013 size_t len)
1014{
1015 if (!mtd->_get_fact_prot_info)
1016 return -EOPNOTSUPP;
1017 if (!len)
1018 return 0;
1019 return mtd->_get_fact_prot_info(mtd, buf, len);
1020}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001021EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001022
1023int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1024 size_t *retlen, u_char *buf)
1025{
1026 *retlen = 0;
1027 if (!mtd->_read_fact_prot_reg)
1028 return -EOPNOTSUPP;
1029 if (!len)
1030 return 0;
1031 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1032}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001033EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001034
1035int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf,
1036 size_t len)
1037{
1038 if (!mtd->_get_user_prot_info)
1039 return -EOPNOTSUPP;
1040 if (!len)
1041 return 0;
1042 return mtd->_get_user_prot_info(mtd, buf, len);
1043}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001044EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001045
1046int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1047 size_t *retlen, u_char *buf)
1048{
1049 *retlen = 0;
1050 if (!mtd->_read_user_prot_reg)
1051 return -EOPNOTSUPP;
1052 if (!len)
1053 return 0;
1054 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1055}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001056EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001057
1058int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1059 size_t *retlen, u_char *buf)
1060{
1061 *retlen = 0;
1062 if (!mtd->_write_user_prot_reg)
1063 return -EOPNOTSUPP;
1064 if (!len)
1065 return 0;
1066 return mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1067}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001068EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001069
1070int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1071{
1072 if (!mtd->_lock_user_prot_reg)
1073 return -EOPNOTSUPP;
1074 if (!len)
1075 return 0;
1076 return mtd->_lock_user_prot_reg(mtd, from, len);
1077}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001078EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001079
1080/* Chip-supported device locking */
1081int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1082{
1083 if (!mtd->_lock)
1084 return -EOPNOTSUPP;
1085 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1086 return -EINVAL;
1087 if (!len)
1088 return 0;
1089 return mtd->_lock(mtd, ofs, len);
1090}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001091EXPORT_SYMBOL_GPL(mtd_lock);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001092
1093int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1094{
1095 if (!mtd->_unlock)
1096 return -EOPNOTSUPP;
1097 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1098 return -EINVAL;
1099 if (!len)
1100 return 0;
1101 return mtd->_unlock(mtd, ofs, len);
1102}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001103EXPORT_SYMBOL_GPL(mtd_unlock);
1104
1105int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1106{
1107 if (!mtd->_is_locked)
1108 return -EOPNOTSUPP;
1109 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1110 return -EINVAL;
1111 if (!len)
1112 return 0;
1113 return mtd->_is_locked(mtd, ofs, len);
1114}
1115EXPORT_SYMBOL_GPL(mtd_is_locked);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001116
1117int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1118{
1119 if (!mtd->_block_isbad)
1120 return 0;
1121 if (ofs < 0 || ofs > mtd->size)
1122 return -EINVAL;
1123 return mtd->_block_isbad(mtd, ofs);
1124}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001125EXPORT_SYMBOL_GPL(mtd_block_isbad);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001126
1127int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1128{
1129 if (!mtd->_block_markbad)
1130 return -EOPNOTSUPP;
1131 if (ofs < 0 || ofs > mtd->size)
1132 return -EINVAL;
1133 if (!(mtd->flags & MTD_WRITEABLE))
1134 return -EROFS;
1135 return mtd->_block_markbad(mtd, ofs);
1136}
Heiko Schocherff94bc42014-06-24 10:10:04 +02001137EXPORT_SYMBOL_GPL(mtd_block_markbad);
1138
1139#ifndef __UBOOT__
1140/*
1141 * default_mtd_writev - the default writev method
1142 * @mtd: mtd device description object pointer
1143 * @vecs: the vectors to write
1144 * @count: count of vectors in @vecs
1145 * @to: the MTD device offset to write to
1146 * @retlen: on exit contains the count of bytes written to the MTD device.
1147 *
1148 * This function returns zero in case of success and a negative error code in
1149 * case of failure.
1150 */
1151static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1152 unsigned long count, loff_t to, size_t *retlen)
1153{
1154 unsigned long i;
1155 size_t totlen = 0, thislen;
1156 int ret = 0;
1157
1158 for (i = 0; i < count; i++) {
1159 if (!vecs[i].iov_len)
1160 continue;
1161 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1162 vecs[i].iov_base);
1163 totlen += thislen;
1164 if (ret || thislen != vecs[i].iov_len)
1165 break;
1166 to += vecs[i].iov_len;
1167 }
1168 *retlen = totlen;
1169 return ret;
1170}
1171
1172/*
1173 * mtd_writev - the vector-based MTD write method
1174 * @mtd: mtd device description object pointer
1175 * @vecs: the vectors to write
1176 * @count: count of vectors in @vecs
1177 * @to: the MTD device offset to write to
1178 * @retlen: on exit contains the count of bytes written to the MTD device.
1179 *
1180 * This function returns zero in case of success and a negative error code in
1181 * case of failure.
1182 */
1183int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1184 unsigned long count, loff_t to, size_t *retlen)
1185{
1186 *retlen = 0;
1187 if (!(mtd->flags & MTD_WRITEABLE))
1188 return -EROFS;
1189 if (!mtd->_writev)
1190 return default_mtd_writev(mtd, vecs, count, to, retlen);
1191 return mtd->_writev(mtd, vecs, count, to, retlen);
1192}
1193EXPORT_SYMBOL_GPL(mtd_writev);
1194
1195/**
1196 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1197 * @mtd: mtd device description object pointer
1198 * @size: a pointer to the ideal or maximum size of the allocation, points
1199 * to the actual allocation size on success.
1200 *
1201 * This routine attempts to allocate a contiguous kernel buffer up to
1202 * the specified size, backing off the size of the request exponentially
1203 * until the request succeeds or until the allocation size falls below
1204 * the system page size. This attempts to make sure it does not adversely
1205 * impact system performance, so when allocating more than one page, we
1206 * ask the memory allocator to avoid re-trying, swapping, writing back
1207 * or performing I/O.
1208 *
1209 * Note, this function also makes sure that the allocated buffer is aligned to
1210 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1211 *
1212 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1213 * to handle smaller (i.e. degraded) buffer allocations under low- or
1214 * fragmented-memory situations where such reduced allocations, from a
1215 * requested ideal, are allowed.
1216 *
1217 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1218 */
1219void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1220{
1221 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1222 __GFP_NORETRY | __GFP_NO_KSWAPD;
1223 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1224 void *kbuf;
1225
1226 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1227
1228 while (*size > min_alloc) {
1229 kbuf = kmalloc(*size, flags);
1230 if (kbuf)
1231 return kbuf;
1232
1233 *size >>= 1;
1234 *size = ALIGN(*size, mtd->writesize);
1235 }
1236
1237 /*
1238 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1239 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1240 */
1241 return kmalloc(*size, GFP_KERNEL);
1242}
1243EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1244#endif
1245
1246#ifdef CONFIG_PROC_FS
1247
1248/*====================================================================*/
1249/* Support for /proc/mtd */
1250
1251static int mtd_proc_show(struct seq_file *m, void *v)
1252{
1253 struct mtd_info *mtd;
1254
1255 seq_puts(m, "dev: size erasesize name\n");
1256 mutex_lock(&mtd_table_mutex);
1257 mtd_for_each_device(mtd) {
1258 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1259 mtd->index, (unsigned long long)mtd->size,
1260 mtd->erasesize, mtd->name);
1261 }
1262 mutex_unlock(&mtd_table_mutex);
1263 return 0;
1264}
1265
1266static int mtd_proc_open(struct inode *inode, struct file *file)
1267{
1268 return single_open(file, mtd_proc_show, NULL);
1269}
1270
1271static const struct file_operations mtd_proc_ops = {
1272 .open = mtd_proc_open,
1273 .read = seq_read,
1274 .llseek = seq_lseek,
1275 .release = single_release,
1276};
1277#endif /* CONFIG_PROC_FS */
1278
1279/*====================================================================*/
1280/* Init code */
1281
1282#ifndef __UBOOT__
1283static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1284{
1285 int ret;
1286
1287 ret = bdi_init(bdi);
1288 if (!ret)
1289 ret = bdi_register(bdi, NULL, "%s", name);
1290
1291 if (ret)
1292 bdi_destroy(bdi);
1293
1294 return ret;
1295}
1296
1297static struct proc_dir_entry *proc_mtd;
1298
1299static int __init init_mtd(void)
1300{
1301 int ret;
1302
1303 ret = class_register(&mtd_class);
1304 if (ret)
1305 goto err_reg;
1306
1307 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1308 if (ret)
1309 goto err_bdi1;
1310
1311 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1312 if (ret)
1313 goto err_bdi2;
1314
1315 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1316 if (ret)
1317 goto err_bdi3;
1318
1319 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1320
1321 ret = init_mtdchar();
1322 if (ret)
1323 goto out_procfs;
1324
1325 return 0;
1326
1327out_procfs:
1328 if (proc_mtd)
1329 remove_proc_entry("mtd", NULL);
1330err_bdi3:
1331 bdi_destroy(&mtd_bdi_ro_mappable);
1332err_bdi2:
1333 bdi_destroy(&mtd_bdi_unmappable);
1334err_bdi1:
1335 class_unregister(&mtd_class);
1336err_reg:
1337 pr_err("Error registering mtd class or bdi: %d\n", ret);
1338 return ret;
1339}
1340
1341static void __exit cleanup_mtd(void)
1342{
1343 cleanup_mtdchar();
1344 if (proc_mtd)
1345 remove_proc_entry("mtd", NULL);
1346 class_unregister(&mtd_class);
1347 bdi_destroy(&mtd_bdi_unmappable);
1348 bdi_destroy(&mtd_bdi_ro_mappable);
1349 bdi_destroy(&mtd_bdi_rw_mappable);
1350}
1351
1352module_init(init_mtd);
1353module_exit(cleanup_mtd);
1354#endif
1355
1356MODULE_LICENSE("GPL");
1357MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1358MODULE_DESCRIPTION("Core MTD registration and access routines");