Commit f1332ba2f23800bb5d52457ac150c568dfb1f3bf

Authored by Ben Hutchings
Committed by David Woodhouse
1 parent 0040476b0e

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>

Showing 3 changed files with 45 additions and 34 deletions Side-by-side Diff

drivers/mtd/mtd_blkdevs.c
... ... @@ -335,7 +335,8 @@
335 335  
336 336 int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
337 337 {
338   - int ret, i;
  338 + struct mtd_info *mtd;
  339 + int ret;
339 340  
340 341 /* Register the notifier if/when the first device type is
341 342 registered, to prevent the link/init ordering from fucking
... ... @@ -389,10 +390,9 @@
389 390 INIT_LIST_HEAD(&tr->devs);
390 391 list_add(&tr->list, &blktrans_majors);
391 392  
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);
396 396  
397 397 mutex_unlock(&mtd_table_mutex);
398 398  
drivers/mtd/mtdcore.c
... ... @@ -381,7 +381,7 @@
381 381  
382 382 void register_mtd_user (struct mtd_notifier *new)
383 383 {
384   - int i;
  384 + struct mtd_info *mtd;
385 385  
386 386 mutex_lock(&mtd_table_mutex);
387 387  
... ... @@ -389,9 +389,8 @@
389 389  
390 390 __module_get(THIS_MODULE);
391 391  
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);
395 394  
396 395 mutex_unlock(&mtd_table_mutex);
397 396 }
398 397  
... ... @@ -408,15 +407,14 @@
408 407  
409 408 int unregister_mtd_user (struct mtd_notifier *old)
410 409 {
411   - int i;
  410 + struct mtd_info *mtd;
412 411  
413 412 mutex_lock(&mtd_table_mutex);
414 413  
415 414 module_put(THIS_MODULE);
416 415  
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);
420 418  
421 419 list_del(&old->list);
422 420 mutex_unlock(&mtd_table_mutex);
423 421  
... ... @@ -438,15 +436,18 @@
438 436  
439 437 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
440 438 {
441   - struct mtd_info *ret = NULL;
442   - int i, err = -ENODEV;
  439 + struct mtd_info *ret = NULL, *other;
  440 + int err = -ENODEV;
443 441  
444 442 mutex_lock(&mtd_table_mutex);
445 443  
446 444 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 + }
450 451 } else if (num >= 0 && num < MAX_MTD_DEVICES) {
451 452 ret = mtd_table[num];
452 453 if (mtd && mtd != ret)
453 454  
... ... @@ -487,14 +488,14 @@
487 488  
488 489 struct mtd_info *get_mtd_device_nm(const char *name)
489 490 {
490   - int i, err = -ENODEV;
491   - struct mtd_info *mtd = NULL;
  491 + int err = -ENODEV;
  492 + struct mtd_info *mtd = NULL, *other;
492 493  
493 494 mutex_lock(&mtd_table_mutex);
494 495  
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;
498 499 break;
499 500 }
500 501 }
501 502  
... ... @@ -581,14 +582,9 @@
581 582  
582 583 static struct proc_dir_entry *proc_mtd;
583 584  
584   -static inline int mtd_proc_info (char *buf, int i)
  585 +static inline int mtd_proc_info(char *buf, struct mtd_info *this)
585 586 {
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,
592 588 (unsigned long long)this->size,
593 589 this->erasesize, this->name);
594 590 }
595 591  
... ... @@ -596,15 +592,15 @@
596 592 static int mtd_read_proc (char *page, char **start, off_t off, int count,
597 593 int *eof, void *data_unused)
598 594 {
599   - int len, l, i;
  595 + struct mtd_info *mtd;
  596 + int len, l;
600 597 off_t begin = 0;
601 598  
602 599 mutex_lock(&mtd_table_mutex);
603 600  
604 601 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);
608 604 len += l;
609 605 if (len+begin > off+count)
610 606 goto done;
drivers/mtd/mtdcore.h
... ... @@ -9,4 +9,19 @@
9 9  
10 10 extern struct mutex mtd_table_mutex;
11 11 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))