Skip to content

Commit f1332ba

Browse files
Ben HutchingsDavid Woodhouse
authored andcommitted
mtd: Introduce and use iteration macro for reading the MTD device table
Signed-off-by: Ben Hutchings <bhutchings@solarflare.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent 0040476 commit f1332ba

3 files changed

Lines changed: 45 additions & 34 deletions

File tree

‎drivers/mtd/mtd_blkdevs.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ static struct mtd_notifier blktrans_notifier = {
335335

336336
int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
337337
{
338-
int ret, i;
338+
struct mtd_info *mtd;
339+
int ret;
339340

340341
/* Register the notifier if/when the first device type is
341342
registered, to prevent the link/init ordering from fucking
@@ -389,10 +390,9 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
389390
INIT_LIST_HEAD(&tr->devs);
390391
list_add(&tr->list, &blktrans_majors);
391392

392-
for (i=0; i<MAX_MTD_DEVICES; i++) {
393-
if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
394-
tr->add_mtd(tr, mtd_table[i]);
395-
}
393+
mtd_for_each_device(mtd)
394+
if (mtd->type != MTD_ABSENT)
395+
tr->add_mtd(tr, mtd);
396396

397397
mutex_unlock(&mtd_table_mutex);
398398

‎drivers/mtd/mtdcore.c‎

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,16 @@ int del_mtd_device (struct mtd_info *mtd)
381381

382382
void register_mtd_user (struct mtd_notifier *new)
383383
{
384-
int i;
384+
struct mtd_info *mtd;
385385

386386
mutex_lock(&mtd_table_mutex);
387387

388388
list_add(&new->list, &mtd_notifiers);
389389

390390
__module_get(THIS_MODULE);
391391

392-
for (i=0; i< MAX_MTD_DEVICES; i++)
393-
if (mtd_table[i])
394-
new->add(mtd_table[i]);
392+
mtd_for_each_device(mtd)
393+
new->add(mtd);
395394

396395
mutex_unlock(&mtd_table_mutex);
397396
}
@@ -408,15 +407,14 @@ void register_mtd_user (struct mtd_notifier *new)
408407

409408
int unregister_mtd_user (struct mtd_notifier *old)
410409
{
411-
int i;
410+
struct mtd_info *mtd;
412411

413412
mutex_lock(&mtd_table_mutex);
414413

415414
module_put(THIS_MODULE);
416415

417-
for (i=0; i< MAX_MTD_DEVICES; i++)
418-
if (mtd_table[i])
419-
old->remove(mtd_table[i]);
416+
mtd_for_each_device(mtd)
417+
old->remove(mtd);
420418

421419
list_del(&old->list);
422420
mutex_unlock(&mtd_table_mutex);
@@ -438,15 +436,18 @@ int unregister_mtd_user (struct mtd_notifier *old)
438436

439437
struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
440438
{
441-
struct mtd_info *ret = NULL;
442-
int i, err = -ENODEV;
439+
struct mtd_info *ret = NULL, *other;
440+
int err = -ENODEV;
443441

444442
mutex_lock(&mtd_table_mutex);
445443

446444
if (num == -1) {
447-
for (i=0; i< MAX_MTD_DEVICES; i++)
448-
if (mtd_table[i] == mtd)
449-
ret = mtd_table[i];
445+
mtd_for_each_device(other) {
446+
if (other == mtd) {
447+
ret = mtd;
448+
break;
449+
}
450+
}
450451
} else if (num >= 0 && num < MAX_MTD_DEVICES) {
451452
ret = mtd_table[num];
452453
if (mtd && mtd != ret)
@@ -487,14 +488,14 @@ struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
487488

488489
struct mtd_info *get_mtd_device_nm(const char *name)
489490
{
490-
int i, err = -ENODEV;
491-
struct mtd_info *mtd = NULL;
491+
int err = -ENODEV;
492+
struct mtd_info *mtd = NULL, *other;
492493

493494
mutex_lock(&mtd_table_mutex);
494495

495-
for (i = 0; i < MAX_MTD_DEVICES; i++) {
496-
if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
497-
mtd = mtd_table[i];
496+
mtd_for_each_device(other) {
497+
if (!strcmp(name, other->name)) {
498+
mtd = other;
498499
break;
499500
}
500501
}
@@ -581,30 +582,25 @@ EXPORT_SYMBOL_GPL(default_mtd_writev);
581582

582583
static struct proc_dir_entry *proc_mtd;
583584

584-
static inline int mtd_proc_info (char *buf, int i)
585+
static inline int mtd_proc_info(char *buf, struct mtd_info *this)
585586
{
586-
struct mtd_info *this = mtd_table[i];
587-
588-
if (!this)
589-
return 0;
590-
591-
return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
587+
return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
592588
(unsigned long long)this->size,
593589
this->erasesize, this->name);
594590
}
595591

596592
static int mtd_read_proc (char *page, char **start, off_t off, int count,
597593
int *eof, void *data_unused)
598594
{
599-
int len, l, i;
595+
struct mtd_info *mtd;
596+
int len, l;
600597
off_t begin = 0;
601598

602599
mutex_lock(&mtd_table_mutex);
603600

604601
len = sprintf(page, "dev: size erasesize name\n");
605-
for (i=0; i< MAX_MTD_DEVICES; i++) {
606-
607-
l = mtd_proc_info(page + len, i);
602+
mtd_for_each_device(mtd) {
603+
l = mtd_proc_info(page + len, mtd);
608604
len += l;
609605
if (len+begin > off+count)
610606
goto done;

‎drivers/mtd/mtdcore.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@
99

1010
extern struct mutex mtd_table_mutex;
1111
extern struct mtd_info *mtd_table[MAX_MTD_DEVICES];
12+
13+
static inline struct mtd_info *__mtd_next_device(int i)
14+
{
15+
while (i < MAX_MTD_DEVICES) {
16+
if (mtd_table[i])
17+
return mtd_table[i];
18+
i++;
19+
}
20+
return NULL;
21+
}
22+
23+
#define mtd_for_each_device(mtd) \
24+
for ((mtd) = __mtd_next_device(0); \
25+
(mtd) != NULL; \
26+
(mtd) = __mtd_next_device(mtd->index + 1))

0 commit comments

Comments
 (0)