25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/types.h>
35 #include <asm/div64.h>
53 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
54 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
60 #define CONCAT(x) ((struct mtd_concat *)(x))
68 concat_read(
struct mtd_info *mtd, loff_t
from,
size_t len,
79 if (from >= subdev->
size) {
85 if (from + len > subdev->
size)
96 if (mtd_is_eccerr(
err)) {
99 }
else if (mtd_is_bitflip(
err)) {
120 concat_write(
struct mtd_info *mtd, loff_t to,
size_t len,
129 size_t size, retsize;
131 if (to >= subdev->
size) {
136 if (to + len > subdev->
size)
137 size = subdev->
size - to;
141 err =
mtd_write(subdev, to, size, &retsize, buf);
158 concat_writev(
struct mtd_info *mtd,
const struct kvec *vecs,
159 unsigned long count, loff_t to,
size_t * retlen)
162 struct kvec *vecs_copy;
163 unsigned long entry_low, entry_high;
169 for (i = 0; i <
count; i++)
187 size_t size, wsize, retsize, old_iov_len;
189 if (to >= subdev->
size) {
197 entry_high = entry_low;
198 while (entry_high < count) {
199 if (size <= vecs_copy[entry_high].iov_len)
201 size -= vecs_copy[entry_high++].
iov_len;
204 old_iov_len = vecs_copy[entry_high].
iov_len;
207 err =
mtd_writev(subdev, &vecs_copy[entry_low],
208 entry_high - entry_low + 1, to, &retsize);
210 vecs_copy[entry_high].
iov_len = old_iov_len -
size;
213 entry_low = entry_high;
244 if (from >= subdev->
size) {
245 from -= subdev->
size;
250 if (from + devops.
len > subdev->
size)
259 if (mtd_is_eccerr(err)) {
262 }
else if (mtd_is_bitflip(err)) {
304 if (to >= subdev->
size) {
310 if (to + devops.
len > subdev->
size)
311 devops.
len = subdev->
size - to;
313 err = mtd_write_oob(subdev, to, &devops);
352 erase->
callback = concat_erase_callback;
353 erase->
priv = (
unsigned long) &waitq;
388 if (!concat->
mtd.numeraseregions) {
390 if (instr->
addr & (concat->
mtd.erasesize - 1))
392 if (instr->
len & (concat->
mtd.erasesize - 1))
397 concat->
mtd.eraseregions;
402 for (i = 0; i < concat->
mtd.numeraseregions &&
417 for (; i < concat->
mtd.numeraseregions &&
418 (instr->
addr + instr->
len) >= erase_regions[i].offset;
424 if (i < 0 || ((instr->
addr + instr->
len) &
446 offset += subdev->
size;
457 for (; length > 0; i++) {
462 if (erase->
addr + length > subdev->
size)
467 length -= erase->
len;
468 if ((err = concat_dev_erase(subdev, erase))) {
485 offset += subdev->
size;
506 if (ofs >= subdev->
size) {
511 if (ofs + len > subdev->
size)
512 size = subdev->
size - ofs;
540 if (ofs >= subdev->
size) {
545 if (ofs + len > subdev->
size)
546 size = subdev->
size - ofs;
565 static void concat_sync(
struct mtd_info *mtd)
576 static int concat_suspend(
struct mtd_info *mtd)
583 if ((rc = mtd_suspend(subdev)) < 0)
589 static void concat_resume(
struct mtd_info *mtd)
600 static int concat_block_isbad(
struct mtd_info *mtd, loff_t ofs)
605 if (!mtd_can_have_bb(concat->
subdev[0]))
611 if (ofs >= subdev->
size) {
623 static int concat_block_markbad(
struct mtd_info *mtd, loff_t ofs)
631 if (ofs >= subdev->
size) {
649 static unsigned long concat_get_unmapped_area(
struct mtd_info *mtd,
651 unsigned long offset,
660 if (offset >= subdev->
size) {
661 offset -= subdev->
size;
668 return (
unsigned long) -
ENOSYS;
684 uint32_t max_erasesize, curr_erasesize;
685 int num_erase_region;
686 int max_writebufsize = 0;
689 for (i = 0; i < num_devs; i++)
698 (
"memory allocation error while creating concatenated device \"%s\"\n",
708 concat->
mtd.type = subdev[0]->
type;
709 concat->
mtd.flags = subdev[0]->
flags;
710 concat->
mtd.size = subdev[0]->
size;
714 for (i = 0; i < num_devs; i++)
717 concat->
mtd.writebufsize = max_writebufsize;
723 concat->
mtd._writev = concat_writev;
725 concat->
mtd._read_oob = concat_read_oob;
727 concat->
mtd._write_oob = concat_write_oob;
729 concat->
mtd._block_isbad = concat_block_isbad;
731 concat->
mtd._block_markbad = concat_block_markbad;
733 concat->
mtd.ecc_stats.badblocks = subdev[0]->
ecc_stats.badblocks;
737 concat->
subdev[0] = subdev[0];
739 for (i = 1; i < num_devs; i++) {
740 if (concat->
mtd.type != subdev[i]->
type) {
742 printk(
"Incompatible device type on \"%s\"\n",
746 if (concat->
mtd.flags != subdev[i]->
flags) {
751 if ((concat->
mtd.flags ^ subdev[i]->
754 printk(
"Incompatible device flags on \"%s\"\n",
767 if (concat->
mtd.backing_dev_info !=
769 concat->
mtd.backing_dev_info =
772 concat->
mtd.size += subdev[
i]->
size;
773 concat->
mtd.ecc_stats.badblocks +=
781 printk(
"Incompatible OOB or ECC data on \"%s\"\n",
794 concat->
mtd._erase = concat_erase;
795 concat->
mtd._read = concat_read;
796 concat->
mtd._write = concat_write;
797 concat->
mtd._sync = concat_sync;
798 concat->
mtd._lock = concat_lock;
799 concat->
mtd._unlock = concat_unlock;
800 concat->
mtd._suspend = concat_suspend;
801 concat->
mtd._resume = concat_resume;
802 concat->
mtd._get_unmapped_area = concat_get_unmapped_area;
810 max_erasesize = curr_erasesize = subdev[0]->
erasesize;
811 num_erase_region = 1;
812 for (i = 0; i < num_devs; i++) {
815 if (subdev[i]->
erasesize != curr_erasesize) {
819 if (curr_erasesize > max_erasesize)
820 max_erasesize = curr_erasesize;
834 if (curr_erasesize > max_erasesize)
835 max_erasesize = curr_erasesize;
841 if (num_erase_region == 1) {
846 concat->
mtd.erasesize = curr_erasesize;
847 concat->
mtd.numeraseregions = 0;
858 concat->
mtd.erasesize = max_erasesize;
859 concat->
mtd.numeraseregions = num_erase_region;
860 concat->
mtd.eraseregions = erase_region_p =
863 if (!erase_region_p) {
866 (
"memory allocation error while creating erase region list"
867 " for device \"%s\"\n", name);
876 begin = position = 0;
877 for (i = 0; i < num_devs; i++) {
878 if (subdev[i]->numeraseregions == 0) {
880 if (subdev[i]->
erasesize != curr_erasesize) {
885 erase_region_p->
offset = begin;
888 tmp64 = position - begin;
889 do_div(tmp64, curr_erasesize);
896 position += subdev[
i]->
size;
902 if (subdev[i]->eraseregions[j].
904 erase_region_p->
offset = begin;
907 tmp64 = position - begin;
908 do_div(tmp64, curr_erasesize);
924 erase_region_p->
offset = begin;
925 erase_region_p->
erasesize = curr_erasesize;
926 tmp64 = position - begin;
927 do_div(tmp64, curr_erasesize);
941 if (concat->
mtd.numeraseregions)