Commit f1332ba2 authored by Ben Hutchings's avatar Ben Hutchings Committed by David Woodhouse

mtd: Introduce and use iteration macro for reading the MTD device table

Signed-off-by: default avatarBen Hutchings <bhutchings@solarflare.com>
Signed-off-by: default avatarArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 0040476b
...@@ -335,7 +335,8 @@ static struct mtd_notifier blktrans_notifier = { ...@@ -335,7 +335,8 @@ static struct mtd_notifier blktrans_notifier = {
int register_mtd_blktrans(struct mtd_blktrans_ops *tr) int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
{ {
int ret, i; struct mtd_info *mtd;
int ret;
/* Register the notifier if/when the first device type is /* Register the notifier if/when the first device type is
registered, to prevent the link/init ordering from fucking registered, to prevent the link/init ordering from fucking
...@@ -389,10 +390,9 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) ...@@ -389,10 +390,9 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
INIT_LIST_HEAD(&tr->devs); INIT_LIST_HEAD(&tr->devs);
list_add(&tr->list, &blktrans_majors); list_add(&tr->list, &blktrans_majors);
for (i=0; i<MAX_MTD_DEVICES; i++) { mtd_for_each_device(mtd)
if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT) if (mtd->type != MTD_ABSENT)
tr->add_mtd(tr, mtd_table[i]); tr->add_mtd(tr, mtd);
}
mutex_unlock(&mtd_table_mutex); mutex_unlock(&mtd_table_mutex);
......
...@@ -381,7 +381,7 @@ int del_mtd_device (struct mtd_info *mtd) ...@@ -381,7 +381,7 @@ int del_mtd_device (struct mtd_info *mtd)
void register_mtd_user (struct mtd_notifier *new) void register_mtd_user (struct mtd_notifier *new)
{ {
int i; struct mtd_info *mtd;
mutex_lock(&mtd_table_mutex); mutex_lock(&mtd_table_mutex);
...@@ -389,9 +389,8 @@ void register_mtd_user (struct mtd_notifier *new) ...@@ -389,9 +389,8 @@ void register_mtd_user (struct mtd_notifier *new)
__module_get(THIS_MODULE); __module_get(THIS_MODULE);
for (i=0; i< MAX_MTD_DEVICES; i++) mtd_for_each_device(mtd)
if (mtd_table[i]) new->add(mtd);
new->add(mtd_table[i]);
mutex_unlock(&mtd_table_mutex); mutex_unlock(&mtd_table_mutex);
} }
...@@ -408,15 +407,14 @@ void register_mtd_user (struct mtd_notifier *new) ...@@ -408,15 +407,14 @@ void register_mtd_user (struct mtd_notifier *new)
int unregister_mtd_user (struct mtd_notifier *old) int unregister_mtd_user (struct mtd_notifier *old)
{ {
int i; struct mtd_info *mtd;
mutex_lock(&mtd_table_mutex); mutex_lock(&mtd_table_mutex);
module_put(THIS_MODULE); module_put(THIS_MODULE);
for (i=0; i< MAX_MTD_DEVICES; i++) mtd_for_each_device(mtd)
if (mtd_table[i]) old->remove(mtd);
old->remove(mtd_table[i]);
list_del(&old->list); list_del(&old->list);
mutex_unlock(&mtd_table_mutex); mutex_unlock(&mtd_table_mutex);
...@@ -438,15 +436,18 @@ int unregister_mtd_user (struct mtd_notifier *old) ...@@ -438,15 +436,18 @@ int unregister_mtd_user (struct mtd_notifier *old)
struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
{ {
struct mtd_info *ret = NULL; struct mtd_info *ret = NULL, *other;
int i, err = -ENODEV; int err = -ENODEV;
mutex_lock(&mtd_table_mutex); mutex_lock(&mtd_table_mutex);
if (num == -1) { if (num == -1) {
for (i=0; i< MAX_MTD_DEVICES; i++) mtd_for_each_device(other) {
if (mtd_table[i] == mtd) if (other == mtd) {
ret = mtd_table[i]; ret = mtd;
break;
}
}
} else if (num >= 0 && num < MAX_MTD_DEVICES) { } else if (num >= 0 && num < MAX_MTD_DEVICES) {
ret = mtd_table[num]; ret = mtd_table[num];
if (mtd && mtd != ret) if (mtd && mtd != ret)
...@@ -487,14 +488,14 @@ struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) ...@@ -487,14 +488,14 @@ struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
struct mtd_info *get_mtd_device_nm(const char *name) struct mtd_info *get_mtd_device_nm(const char *name)
{ {
int i, err = -ENODEV; int err = -ENODEV;
struct mtd_info *mtd = NULL; struct mtd_info *mtd = NULL, *other;
mutex_lock(&mtd_table_mutex); mutex_lock(&mtd_table_mutex);
for (i = 0; i < MAX_MTD_DEVICES; i++) { mtd_for_each_device(other) {
if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) { if (!strcmp(name, other->name)) {
mtd = mtd_table[i]; mtd = other;
break; break;
} }
} }
...@@ -581,14 +582,9 @@ EXPORT_SYMBOL_GPL(default_mtd_writev); ...@@ -581,14 +582,9 @@ EXPORT_SYMBOL_GPL(default_mtd_writev);
static struct proc_dir_entry *proc_mtd; static struct proc_dir_entry *proc_mtd;
static inline int mtd_proc_info (char *buf, int i) static inline int mtd_proc_info(char *buf, struct mtd_info *this)
{ {
struct mtd_info *this = mtd_table[i]; return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
if (!this)
return 0;
return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
(unsigned long long)this->size, (unsigned long long)this->size,
this->erasesize, this->name); this->erasesize, this->name);
} }
...@@ -596,15 +592,15 @@ static inline int mtd_proc_info (char *buf, int i) ...@@ -596,15 +592,15 @@ static inline int mtd_proc_info (char *buf, int i)
static int mtd_read_proc (char *page, char **start, off_t off, int count, static int mtd_read_proc (char *page, char **start, off_t off, int count,
int *eof, void *data_unused) int *eof, void *data_unused)
{ {
int len, l, i; struct mtd_info *mtd;
int len, l;
off_t begin = 0; off_t begin = 0;
mutex_lock(&mtd_table_mutex); mutex_lock(&mtd_table_mutex);
len = sprintf(page, "dev: size erasesize name\n"); len = sprintf(page, "dev: size erasesize name\n");
for (i=0; i< MAX_MTD_DEVICES; i++) { mtd_for_each_device(mtd) {
l = mtd_proc_info(page + len, mtd);
l = mtd_proc_info(page + len, i);
len += l; len += l;
if (len+begin > off+count) if (len+begin > off+count)
goto done; goto done;
......
...@@ -9,3 +9,18 @@ ...@@ -9,3 +9,18 @@
extern struct mutex mtd_table_mutex; extern struct mutex mtd_table_mutex;
extern struct mtd_info *mtd_table[MAX_MTD_DEVICES]; extern struct mtd_info *mtd_table[MAX_MTD_DEVICES];
static inline struct mtd_info *__mtd_next_device(int i)
{
while (i < MAX_MTD_DEVICES) {
if (mtd_table[i])
return mtd_table[i];
i++;
}
return NULL;
}
#define mtd_for_each_device(mtd) \
for ((mtd) = __mtd_next_device(0); \
(mtd) != NULL; \
(mtd) = __mtd_next_device(mtd->index + 1))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment