Skip to content

Commit 1c4c215

Browse files
lumagdedekind
authored andcommitted
mtd: add new API for handling MTD registration
Lots (nearly all) mtd drivers contain nearly the similar code that calls parse_mtd_partitions, provides some platform-default values, if parsing fails, and registers mtd device. This is an aim to provide single implementation of this scenario: mtd_device_parse_register() which will handle all this parsing and defaults. Artem: amended comments Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent 3761a6d commit 1c4c215

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎drivers/mtd/mtdcore.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,64 @@ int mtd_device_register(struct mtd_info *master,
451451
}
452452
EXPORT_SYMBOL_GPL(mtd_device_register);
453453

454+
/**
455+
* mtd_device_parse_register - parse partitions and register an MTD device.
456+
*
457+
* @mtd: the MTD device to register
458+
* @types: the list of MTD partition probes to try, see
459+
* 'parse_mtd_partitions()' for more information
460+
* @origin: start address of MTD device, %0 unless you are sure you need this.
461+
* @parts: fallback partition information to register, if parsing fails;
462+
* only valid if %nr_parts > %0
463+
* @nr_parts: the number of partitions in parts, if zero then the full
464+
* MTD device is registered if no partition info is found
465+
*
466+
* This function aggregates MTD partitions parsing (done by
467+
* 'parse_mtd_partitions()') and MTD device and partitions registering. It
468+
* basically follows the most common pattern found in many MTD drivers:
469+
*
470+
* * It first tries to probe partitions on MTD device @mtd using parsers
471+
* specified in @types (if @types is %NULL, then the default list of parsers
472+
* is used, see 'parse_mtd_partitions()' for more information). If none are
473+
* found this functions tries to fallback to information specified in
474+
* @parts/@nr_parts.
475+
* * If any parititioning info was found, this function registers the found
476+
* partitions.
477+
* * If no partitions were found this function just registers the MTD device
478+
* @mtd and exits.
479+
*
480+
* Returns zero in case of success and a negative error code in case of failure.
481+
*/
482+
int mtd_device_parse_register(struct mtd_info *mtd, const char **types,
483+
unsigned long origin,
484+
const struct mtd_partition *parts,
485+
int nr_parts)
486+
{
487+
int err;
488+
struct mtd_partition *real_parts;
489+
490+
err = parse_mtd_partitions(mtd, types, &real_parts, origin);
491+
if (err <= 0 && nr_parts) {
492+
real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
493+
GFP_KERNEL);
494+
err = nr_parts;
495+
if (!parts)
496+
err = -ENOMEM;
497+
}
498+
499+
if (err > 0) {
500+
err = add_mtd_partitions(mtd, real_parts, err);
501+
kfree(real_parts);
502+
} else if (err == 0) {
503+
err = add_mtd_device(mtd);
504+
if (err == 1)
505+
err = -ENODEV;
506+
}
507+
508+
return err;
509+
}
510+
EXPORT_SYMBOL_GPL(mtd_device_parse_register);
511+
454512
/**
455513
* mtd_device_unregister - unregister an existing MTD device.
456514
*

‎include/linux/mtd/mtd.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ struct mtd_partition;
327327
extern int mtd_device_register(struct mtd_info *master,
328328
const struct mtd_partition *parts,
329329
int nr_parts);
330+
extern int mtd_device_parse_register(struct mtd_info *mtd,
331+
const char **part_probe_types,
332+
unsigned long origin,
333+
const struct mtd_partition *defparts,
334+
int defnr_parts);
330335
extern int mtd_device_unregister(struct mtd_info *master);
331336
extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
332337
extern int __get_mtd_device(struct mtd_info *mtd);

0 commit comments

Comments
 (0)