RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / mtd / mtdconcat.c
blob9d9ed2a92b9fd0dd7075ac59a06ad22c23c40202
1 /*
2 * MTD device concatenation layer
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * NAND support by Christian Gan <cgan@iders.ca>
8 * This code is GPL
10 * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/types.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/concat.h>
22 #include <asm/div64.h>
25 * Our storage structure:
26 * Subdev points to an array of pointers to struct mtd_info objects
27 * which is allocated along with this structure
30 struct mtd_concat {
31 struct mtd_info mtd;
32 int num_subdev;
33 struct mtd_info **subdev;
37 * how to calculate the size required for the above structure,
38 * including the pointer array subdev points to:
40 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
41 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
44 * Given a pointer to the MTD object in the mtd_concat structure,
45 * we can retrieve the pointer to that structure with this macro.
47 #define CONCAT(x) ((struct mtd_concat *)(x))
50 * MTD methods which look up the relevant subdevice, translate the
51 * effective address and pass through to the subdevice.
54 static int
55 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
56 size_t * retlen, u_char * buf)
58 struct mtd_concat *concat = CONCAT(mtd);
59 int ret = 0, err;
60 int i;
62 *retlen = 0;
64 for (i = 0; i < concat->num_subdev; i++) {
65 struct mtd_info *subdev = concat->subdev[i];
66 size_t size, retsize;
68 if (from >= subdev->size) {
69 /* Not destined for this subdev */
70 size = 0;
71 from -= subdev->size;
72 continue;
74 if (from + len > subdev->size)
75 /* First part goes into this subdev */
76 size = subdev->size - from;
77 else
78 /* Entire transaction goes into this subdev */
79 size = len;
81 err = subdev->read(subdev, from, size, &retsize, buf);
83 /* Save information about bitflips! */
84 if (unlikely(err)) {
85 if (err == -EBADMSG) {
86 mtd->ecc_stats.failed++;
87 ret = err;
88 } else if (err == -EUCLEAN) {
89 mtd->ecc_stats.corrected++;
90 /* Do not overwrite -EBADMSG !! */
91 if (!ret)
92 ret = err;
93 } else
94 return err;
97 *retlen += retsize;
98 len -= size;
99 if (len == 0)
100 return ret;
102 buf += size;
103 from = 0;
105 return -EINVAL;
108 static int
109 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
110 size_t * retlen, const u_char * buf)
112 struct mtd_concat *concat = CONCAT(mtd);
113 int err = -EINVAL;
114 int i;
116 if (!(mtd->flags & MTD_WRITEABLE))
117 return -EROFS;
119 *retlen = 0;
121 for (i = 0; i < concat->num_subdev; i++) {
122 struct mtd_info *subdev = concat->subdev[i];
123 size_t size, retsize;
125 if (to >= subdev->size) {
126 size = 0;
127 to -= subdev->size;
128 continue;
130 if (to + len > subdev->size)
131 size = subdev->size - to;
132 else
133 size = len;
135 if (!(subdev->flags & MTD_WRITEABLE))
136 err = -EROFS;
137 else
138 err = subdev->write(subdev, to, size, &retsize, buf);
140 if (err)
141 break;
143 *retlen += retsize;
144 len -= size;
145 if (len == 0)
146 break;
148 err = -EINVAL;
149 buf += size;
150 to = 0;
152 return err;
155 static int
156 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
157 unsigned long count, loff_t to, size_t * retlen)
159 struct mtd_concat *concat = CONCAT(mtd);
160 struct kvec *vecs_copy;
161 unsigned long entry_low, entry_high;
162 size_t total_len = 0;
163 int i;
164 int err = -EINVAL;
166 if (!(mtd->flags & MTD_WRITEABLE))
167 return -EROFS;
169 *retlen = 0;
171 /* Calculate total length of data */
172 for (i = 0; i < count; i++)
173 total_len += vecs[i].iov_len;
175 /* Do not allow write past end of device */
176 if ((to + total_len) > mtd->size)
177 return -EINVAL;
179 /* Check alignment */
180 if (mtd->writesize > 1) {
181 loff_t __to = to;
182 if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
183 return -EINVAL;
186 /* make a copy of vecs */
187 vecs_copy = kmemdup(vecs, sizeof(struct kvec) * count, GFP_KERNEL);
188 if (!vecs_copy)
189 return -ENOMEM;
191 entry_low = 0;
192 for (i = 0; i < concat->num_subdev; i++) {
193 struct mtd_info *subdev = concat->subdev[i];
194 size_t size, wsize, retsize, old_iov_len;
196 if (to >= subdev->size) {
197 to -= subdev->size;
198 continue;
201 size = min(total_len, (size_t)(subdev->size - to));
202 wsize = size; /* store for future use */
204 entry_high = entry_low;
205 while (entry_high < count) {
206 if (size <= vecs_copy[entry_high].iov_len)
207 break;
208 size -= vecs_copy[entry_high++].iov_len;
211 old_iov_len = vecs_copy[entry_high].iov_len;
212 vecs_copy[entry_high].iov_len = size;
214 if (!(subdev->flags & MTD_WRITEABLE))
215 err = -EROFS;
216 else
217 err = subdev->writev(subdev, &vecs_copy[entry_low],
218 entry_high - entry_low + 1, to, &retsize);
220 vecs_copy[entry_high].iov_len = old_iov_len - size;
221 vecs_copy[entry_high].iov_base += size;
223 entry_low = entry_high;
225 if (err)
226 break;
228 *retlen += retsize;
229 total_len -= wsize;
231 if (total_len == 0)
232 break;
234 err = -EINVAL;
235 to = 0;
238 kfree(vecs_copy);
239 return err;
242 static int
243 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
245 struct mtd_concat *concat = CONCAT(mtd);
246 struct mtd_oob_ops devops = *ops;
247 int i, err, ret = 0;
249 ops->retlen = ops->oobretlen = 0;
251 for (i = 0; i < concat->num_subdev; i++) {
252 struct mtd_info *subdev = concat->subdev[i];
254 if (from >= subdev->size) {
255 from -= subdev->size;
256 continue;
259 /* partial read ? */
260 if (from + devops.len > subdev->size)
261 devops.len = subdev->size - from;
263 err = subdev->read_oob(subdev, from, &devops);
264 ops->retlen += devops.retlen;
265 ops->oobretlen += devops.oobretlen;
267 /* Save information about bitflips! */
268 if (unlikely(err)) {
269 if (err == -EBADMSG) {
270 mtd->ecc_stats.failed++;
271 ret = err;
272 } else if (err == -EUCLEAN) {
273 mtd->ecc_stats.corrected++;
274 /* Do not overwrite -EBADMSG !! */
275 if (!ret)
276 ret = err;
277 } else
278 return err;
281 if (devops.datbuf) {
282 devops.len = ops->len - ops->retlen;
283 if (!devops.len)
284 return ret;
285 devops.datbuf += devops.retlen;
287 if (devops.oobbuf) {
288 devops.ooblen = ops->ooblen - ops->oobretlen;
289 if (!devops.ooblen)
290 return ret;
291 devops.oobbuf += ops->oobretlen;
294 from = 0;
296 return -EINVAL;
299 static int
300 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
302 struct mtd_concat *concat = CONCAT(mtd);
303 struct mtd_oob_ops devops = *ops;
304 int i, err;
306 if (!(mtd->flags & MTD_WRITEABLE))
307 return -EROFS;
309 ops->retlen = 0;
311 for (i = 0; i < concat->num_subdev; i++) {
312 struct mtd_info *subdev = concat->subdev[i];
314 if (to >= subdev->size) {
315 to -= subdev->size;
316 continue;
319 /* partial write ? */
320 if (to + devops.len > subdev->size)
321 devops.len = subdev->size - to;
323 err = subdev->write_oob(subdev, to, &devops);
324 ops->retlen += devops.retlen;
325 if (err)
326 return err;
328 if (devops.datbuf) {
329 devops.len = ops->len - ops->retlen;
330 if (!devops.len)
331 return 0;
332 devops.datbuf += devops.retlen;
334 if (devops.oobbuf) {
335 devops.ooblen = ops->ooblen - ops->oobretlen;
336 if (!devops.ooblen)
337 return 0;
338 devops.oobbuf += devops.oobretlen;
340 to = 0;
342 return -EINVAL;
345 static void concat_erase_callback(struct erase_info *instr)
347 wake_up((wait_queue_head_t *) instr->priv);
350 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
352 int err;
353 wait_queue_head_t waitq;
354 DECLARE_WAITQUEUE(wait, current);
357 * This code was stol^H^H^H^Hinspired by mtdchar.c
359 init_waitqueue_head(&waitq);
361 erase->mtd = mtd;
362 erase->callback = concat_erase_callback;
363 erase->priv = (unsigned long) &waitq;
366 * FIXME: Allow INTERRUPTIBLE. Which means
367 * not having the wait_queue head on the stack.
369 err = mtd->erase(mtd, erase);
370 if (!err) {
371 set_current_state(TASK_UNINTERRUPTIBLE);
372 add_wait_queue(&waitq, &wait);
373 if (erase->state != MTD_ERASE_DONE
374 && erase->state != MTD_ERASE_FAILED)
375 schedule();
376 remove_wait_queue(&waitq, &wait);
377 set_current_state(TASK_RUNNING);
379 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
381 return err;
384 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
386 struct mtd_concat *concat = CONCAT(mtd);
387 struct mtd_info *subdev;
388 int i, err;
389 u_int32_t length, offset = 0;
390 struct erase_info *erase;
392 if (!(mtd->flags & MTD_WRITEABLE))
393 return -EROFS;
395 if (instr->addr > concat->mtd.size)
396 return -EINVAL;
398 if (instr->len + instr->addr > concat->mtd.size)
399 return -EINVAL;
402 * Check for proper erase block alignment of the to-be-erased area.
403 * It is easier to do this based on the super device's erase
404 * region info rather than looking at each particular sub-device
405 * in turn.
407 if (!concat->mtd.numeraseregions) {
408 /* the easy case: device has uniform erase block size */
409 if (instr->addr & (concat->mtd.erasesize - 1))
410 return -EINVAL;
411 if (instr->len & (concat->mtd.erasesize - 1))
412 return -EINVAL;
413 } else {
414 /* device has variable erase size */
415 struct mtd_erase_region_info *erase_regions =
416 concat->mtd.eraseregions;
419 * Find the erase region where the to-be-erased area begins:
421 for (i = 0; i < concat->mtd.numeraseregions &&
422 instr->addr >= erase_regions[i].offset; i++) ;
423 --i;
426 * Now erase_regions[i] is the region in which the
427 * to-be-erased area begins. Verify that the starting
428 * offset is aligned to this region's erase size:
430 if (instr->addr & (erase_regions[i].erasesize - 1))
431 return -EINVAL;
434 * now find the erase region where the to-be-erased area ends:
436 for (; i < concat->mtd.numeraseregions &&
437 (instr->addr + instr->len) >= erase_regions[i].offset;
438 ++i) ;
439 --i;
441 * check if the ending offset is aligned to this region's erase size
443 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
445 return -EINVAL;
448 instr->fail_addr = 0xffffffff;
450 /* make a local copy of instr to avoid modifying the caller's struct */
451 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
453 if (!erase)
454 return -ENOMEM;
456 *erase = *instr;
457 length = instr->len;
460 * find the subdevice where the to-be-erased area begins, adjust
461 * starting offset to be relative to the subdevice start
463 for (i = 0; i < concat->num_subdev; i++) {
464 subdev = concat->subdev[i];
465 if (subdev->size <= erase->addr) {
466 erase->addr -= subdev->size;
467 offset += subdev->size;
468 } else {
469 break;
473 /* must never happen since size limit has been verified above */
474 BUG_ON(i >= concat->num_subdev);
476 /* now do the erase: */
477 err = 0;
478 for (; length > 0; i++) {
479 /* loop for all subdevices affected by this request */
480 subdev = concat->subdev[i]; /* get current subdevice */
482 /* limit length to subdevice's size: */
483 if (erase->addr + length > subdev->size)
484 erase->len = subdev->size - erase->addr;
485 else
486 erase->len = length;
488 if (!(subdev->flags & MTD_WRITEABLE)) {
489 err = -EROFS;
490 break;
492 length -= erase->len;
493 if ((err = concat_dev_erase(subdev, erase))) {
494 /* sanity check: should never happen since
495 * block alignment has been checked above */
496 BUG_ON(err == -EINVAL);
497 if (erase->fail_addr != 0xffffffff)
498 instr->fail_addr = erase->fail_addr + offset;
499 break;
502 * erase->addr specifies the offset of the area to be
503 * erased *within the current subdevice*. It can be
504 * non-zero only the first time through this loop, i.e.
505 * for the first subdevice where blocks need to be erased.
506 * All the following erases must begin at the start of the
507 * current subdevice, i.e. at offset zero.
509 erase->addr = 0;
510 offset += subdev->size;
512 instr->state = erase->state;
513 kfree(erase);
514 if (err)
515 return err;
517 if (instr->callback)
518 instr->callback(instr);
519 return 0;
522 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
524 struct mtd_concat *concat = CONCAT(mtd);
525 int i, err = -EINVAL;
527 if ((len + ofs) > mtd->size)
528 return -EINVAL;
530 for (i = 0; i < concat->num_subdev; i++) {
531 struct mtd_info *subdev = concat->subdev[i];
532 size_t size;
534 if (ofs >= subdev->size) {
535 size = 0;
536 ofs -= subdev->size;
537 continue;
539 if (ofs + len > subdev->size)
540 size = subdev->size - ofs;
541 else
542 size = len;
544 err = subdev->lock(subdev, ofs, size);
546 if (err)
547 break;
549 len -= size;
550 if (len == 0)
551 break;
553 err = -EINVAL;
554 ofs = 0;
557 return err;
560 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
562 struct mtd_concat *concat = CONCAT(mtd);
563 int i, err = 0;
565 if ((len + ofs) > mtd->size)
566 return -EINVAL;
568 for (i = 0; i < concat->num_subdev; i++) {
569 struct mtd_info *subdev = concat->subdev[i];
570 size_t size;
572 if (ofs >= subdev->size) {
573 size = 0;
574 ofs -= subdev->size;
575 continue;
577 if (ofs + len > subdev->size)
578 size = subdev->size - ofs;
579 else
580 size = len;
582 err = subdev->unlock(subdev, ofs, size);
584 if (err)
585 break;
587 len -= size;
588 if (len == 0)
589 break;
591 err = -EINVAL;
592 ofs = 0;
595 return err;
598 static void concat_sync(struct mtd_info *mtd)
600 struct mtd_concat *concat = CONCAT(mtd);
601 int i;
603 for (i = 0; i < concat->num_subdev; i++) {
604 struct mtd_info *subdev = concat->subdev[i];
605 subdev->sync(subdev);
609 static int concat_suspend(struct mtd_info *mtd)
611 struct mtd_concat *concat = CONCAT(mtd);
612 int i, rc = 0;
614 for (i = 0; i < concat->num_subdev; i++) {
615 struct mtd_info *subdev = concat->subdev[i];
616 if ((rc = subdev->suspend(subdev)) < 0)
617 return rc;
619 return rc;
622 static void concat_resume(struct mtd_info *mtd)
624 struct mtd_concat *concat = CONCAT(mtd);
625 int i;
627 for (i = 0; i < concat->num_subdev; i++) {
628 struct mtd_info *subdev = concat->subdev[i];
629 subdev->resume(subdev);
633 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
635 struct mtd_concat *concat = CONCAT(mtd);
636 int i, res = 0;
638 if (!concat->subdev[0]->block_isbad)
639 return res;
641 if (ofs > mtd->size)
642 return -EINVAL;
644 for (i = 0; i < concat->num_subdev; i++) {
645 struct mtd_info *subdev = concat->subdev[i];
647 if (ofs >= subdev->size) {
648 ofs -= subdev->size;
649 continue;
652 res = subdev->block_isbad(subdev, ofs);
653 break;
656 return res;
659 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
661 struct mtd_concat *concat = CONCAT(mtd);
662 int i, err = -EINVAL;
664 if (!concat->subdev[0]->block_markbad)
665 return 0;
667 if (ofs > mtd->size)
668 return -EINVAL;
670 for (i = 0; i < concat->num_subdev; i++) {
671 struct mtd_info *subdev = concat->subdev[i];
673 if (ofs >= subdev->size) {
674 ofs -= subdev->size;
675 continue;
678 err = subdev->block_markbad(subdev, ofs);
679 if (!err)
680 mtd->ecc_stats.badblocks++;
681 break;
684 return err;
688 * This function constructs a virtual MTD device by concatenating
689 * num_devs MTD devices. A pointer to the new device object is
690 * stored to *new_dev upon success. This function does _not_
691 * register any devices: this is the caller's responsibility.
693 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
694 int num_devs, /* number of subdevices */
695 char *name)
696 { /* name for the new device */
697 int i;
698 size_t size;
699 struct mtd_concat *concat;
700 u_int32_t max_erasesize, curr_erasesize;
701 int num_erase_region;
703 printk(KERN_NOTICE "Concatenating MTD devices:\n");
704 for (i = 0; i < num_devs; i++)
705 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
706 printk(KERN_NOTICE "into device \"%s\"\n", name);
708 /* allocate the device structure */
709 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
710 concat = kzalloc(size, GFP_KERNEL);
711 if (!concat) {
712 printk
713 ("memory allocation error while creating concatenated device \"%s\"\n",
714 name);
715 return NULL;
717 concat->subdev = (struct mtd_info **) (concat + 1);
720 * Set up the new "super" device's MTD object structure, check for
721 * incompatibilites between the subdevices.
723 concat->mtd.type = subdev[0]->type;
724 concat->mtd.flags = subdev[0]->flags;
725 concat->mtd.size = subdev[0]->size;
726 concat->mtd.erasesize = subdev[0]->erasesize;
727 concat->mtd.writesize = subdev[0]->writesize;
728 concat->mtd.oobsize = subdev[0]->oobsize;
729 concat->mtd.oobavail = subdev[0]->oobavail;
730 if (subdev[0]->writev)
731 concat->mtd.writev = concat_writev;
732 if (subdev[0]->read_oob)
733 concat->mtd.read_oob = concat_read_oob;
734 if (subdev[0]->write_oob)
735 concat->mtd.write_oob = concat_write_oob;
736 if (subdev[0]->block_isbad)
737 concat->mtd.block_isbad = concat_block_isbad;
738 if (subdev[0]->block_markbad)
739 concat->mtd.block_markbad = concat_block_markbad;
741 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
743 concat->subdev[0] = subdev[0];
745 for (i = 1; i < num_devs; i++) {
746 if (concat->mtd.type != subdev[i]->type) {
747 kfree(concat);
748 printk("Incompatible device type on \"%s\"\n",
749 subdev[i]->name);
750 return NULL;
752 if (concat->mtd.flags != subdev[i]->flags) {
754 * Expect all flags except MTD_WRITEABLE to be
755 * equal on all subdevices.
757 if ((concat->mtd.flags ^ subdev[i]->
758 flags) & ~MTD_WRITEABLE) {
759 kfree(concat);
760 printk("Incompatible device flags on \"%s\"\n",
761 subdev[i]->name);
762 return NULL;
763 } else
764 /* if writeable attribute differs,
765 make super device writeable */
766 concat->mtd.flags |=
767 subdev[i]->flags & MTD_WRITEABLE;
769 concat->mtd.size += subdev[i]->size;
770 concat->mtd.ecc_stats.badblocks +=
771 subdev[i]->ecc_stats.badblocks;
772 if (concat->mtd.writesize != subdev[i]->writesize ||
773 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
774 concat->mtd.oobsize != subdev[i]->oobsize ||
775 !concat->mtd.read_oob != !subdev[i]->read_oob ||
776 !concat->mtd.write_oob != !subdev[i]->write_oob) {
777 kfree(concat);
778 printk("Incompatible OOB or ECC data on \"%s\"\n",
779 subdev[i]->name);
780 return NULL;
782 concat->subdev[i] = subdev[i];
786 concat->mtd.ecclayout = subdev[0]->ecclayout;
788 concat->num_subdev = num_devs;
789 concat->mtd.name = name;
791 concat->mtd.erase = concat_erase;
792 concat->mtd.read = concat_read;
793 concat->mtd.write = concat_write;
794 concat->mtd.sync = concat_sync;
795 concat->mtd.lock = concat_lock;
796 concat->mtd.unlock = concat_unlock;
797 concat->mtd.suspend = concat_suspend;
798 concat->mtd.resume = concat_resume;
801 * Combine the erase block size info of the subdevices:
803 * first, walk the map of the new device and see how
804 * many changes in erase size we have
806 max_erasesize = curr_erasesize = subdev[0]->erasesize;
807 num_erase_region = 1;
808 for (i = 0; i < num_devs; i++) {
809 if (subdev[i]->numeraseregions == 0) {
810 /* current subdevice has uniform erase size */
811 if (subdev[i]->erasesize != curr_erasesize) {
812 /* if it differs from the last subdevice's erase size, count it */
813 ++num_erase_region;
814 curr_erasesize = subdev[i]->erasesize;
815 if (curr_erasesize > max_erasesize)
816 max_erasesize = curr_erasesize;
818 } else {
819 /* current subdevice has variable erase size */
820 int j;
821 for (j = 0; j < subdev[i]->numeraseregions; j++) {
823 /* walk the list of erase regions, count any changes */
824 if (subdev[i]->eraseregions[j].erasesize !=
825 curr_erasesize) {
826 ++num_erase_region;
827 curr_erasesize =
828 subdev[i]->eraseregions[j].
829 erasesize;
830 if (curr_erasesize > max_erasesize)
831 max_erasesize = curr_erasesize;
837 if (num_erase_region == 1) {
839 * All subdevices have the same uniform erase size.
840 * This is easy:
842 concat->mtd.erasesize = curr_erasesize;
843 concat->mtd.numeraseregions = 0;
844 } else {
846 * erase block size varies across the subdevices: allocate
847 * space to store the data describing the variable erase regions
849 struct mtd_erase_region_info *erase_region_p;
850 u_int32_t begin, position;
852 concat->mtd.erasesize = max_erasesize;
853 concat->mtd.numeraseregions = num_erase_region;
854 concat->mtd.eraseregions = erase_region_p =
855 kmalloc(num_erase_region *
856 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
857 if (!erase_region_p) {
858 kfree(concat);
859 printk
860 ("memory allocation error while creating erase region list"
861 " for device \"%s\"\n", name);
862 return NULL;
866 * walk the map of the new device once more and fill in
867 * in erase region info:
869 curr_erasesize = subdev[0]->erasesize;
870 begin = position = 0;
871 for (i = 0; i < num_devs; i++) {
872 if (subdev[i]->numeraseregions == 0) {
873 /* current subdevice has uniform erase size */
874 if (subdev[i]->erasesize != curr_erasesize) {
876 * fill in an mtd_erase_region_info structure for the area
877 * we have walked so far:
879 erase_region_p->offset = begin;
880 erase_region_p->erasesize =
881 curr_erasesize;
882 erase_region_p->numblocks =
883 (position - begin) / curr_erasesize;
884 begin = position;
886 curr_erasesize = subdev[i]->erasesize;
887 ++erase_region_p;
889 position += subdev[i]->size;
890 } else {
891 /* current subdevice has variable erase size */
892 int j;
893 for (j = 0; j < subdev[i]->numeraseregions; j++) {
894 /* walk the list of erase regions, count any changes */
895 if (subdev[i]->eraseregions[j].
896 erasesize != curr_erasesize) {
897 erase_region_p->offset = begin;
898 erase_region_p->erasesize =
899 curr_erasesize;
900 erase_region_p->numblocks =
901 (position -
902 begin) / curr_erasesize;
903 begin = position;
905 curr_erasesize =
906 subdev[i]->eraseregions[j].
907 erasesize;
908 ++erase_region_p;
910 position +=
911 subdev[i]->eraseregions[j].
912 numblocks * curr_erasesize;
916 /* Now write the final entry */
917 erase_region_p->offset = begin;
918 erase_region_p->erasesize = curr_erasesize;
919 erase_region_p->numblocks = (position - begin) / curr_erasesize;
922 return &concat->mtd;
926 * This function destroys an MTD object obtained from concat_mtd_devs()
929 void mtd_concat_destroy(struct mtd_info *mtd)
931 struct mtd_concat *concat = CONCAT(mtd);
932 if (concat->mtd.numeraseregions)
933 kfree(concat->mtd.eraseregions);
934 kfree(concat);
937 EXPORT_SYMBOL(mtd_concat_create);
938 EXPORT_SYMBOL(mtd_concat_destroy);
940 MODULE_LICENSE("GPL");
941 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
942 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");