@@ -2202,6 +2202,37 @@ out:
return err;
}
+static unsigned int mmc_align_erase_size(struct mmc_card *card,
+ unsigned int *from,
+ unsigned int *to,
+ unsigned int nr)
+{
+ unsigned int from_new = *from, nr_new = nr, rem;
+
+ rem = from_new % card->erase_size;
+ if (rem) {
+ rem = card->erase_size - rem;
+ from_new += rem;
+ if (nr_new > rem)
+ nr_new -= rem;
+ else
+ return 0;
+ }
+
+ rem = nr_new % card->erase_size;
+ if (rem)
+ nr_new -= rem;
+
+ if (nr_new == 0)
+ return 0;
+
+ /* 'from' and 'to' are inclusive */
+ *to = from_new + nr_new - 1;
+ *from = from_new;
+
+ return nr_new;
+}
+
/**
* mmc_erase - erase sectors.
* @card: card to erase
@@ -2234,31 +2265,14 @@ int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
}
if (arg == MMC_ERASE_ARG) {
- rem = from % card->erase_size;
- if (rem) {
- rem = card->erase_size - rem;
- from += rem;
- if (nr > rem)
- nr -= rem;
- else
- return 0;
- }
- rem = nr % card->erase_size;
- if (rem)
- nr -= rem;
+ nr = mmc_align_erase_size(card, &from, &to, nr);
+ if (nr == 0)
+ return 0;
+ } else {
+ /* 'from' and 'to' are inclusive */
+ to -= 1;
}
- if (nr == 0)
- return 0;
-
- to = from + nr;
-
- if (to <= from)
- return -EINVAL;
-
- /* 'from' and 'to' are inclusive */
- to -= 1;
-
/*
* Special case where only one erase-group fits in the timeout budget:
* If the region crosses an erase-group boundary on this particular