ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / mtdconcat.c
1 /*
2  * MTD device concatenation layer
3  *
4  * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
5  *
6  * NAND support by Christian Gan <cgan@iders.ca>
7  *
8  * This code is GPL
9  *
10  * $Id: mtdconcat.c,v 1.8 2003/06/30 11:01:26 dwmw2 Exp $
11  */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/concat.h>
20
21 /*
22  * Our storage structure:
23  * Subdev points to an array of pointers to struct mtd_info objects
24  * which is allocated along with this structure
25  *
26  */
27 struct mtd_concat {
28         struct mtd_info mtd;
29         int num_subdev;
30         struct mtd_info **subdev;
31 };
32
33 /*
34  * how to calculate the size required for the above structure,
35  * including the pointer array subdev points to:
36  */
37 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev)    \
38         ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
39
40 /*
41  * Given a pointer to the MTD object in the mtd_concat structure,
42  * we can retrieve the pointer to that structure with this macro.
43  */
44 #define CONCAT(x)  ((struct mtd_concat *)(x))
45
46 /* 
47  * MTD methods which look up the relevant subdevice, translate the
48  * effective address and pass through to the subdevice.
49  */
50
51 static int
52 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
53             size_t * retlen, u_char * buf)
54 {
55         struct mtd_concat *concat = CONCAT(mtd);
56         int err = -EINVAL;
57         int i;
58
59         *retlen = 0;
60
61         for (i = 0; i < concat->num_subdev; i++) {
62                 struct mtd_info *subdev = concat->subdev[i];
63                 size_t size, retsize;
64
65                 if (from >= subdev->size) {
66                         /* Not destined for this subdev */
67                         size = 0;
68                         from -= subdev->size;
69                         continue;
70                 }
71                 if (from + len > subdev->size)
72                         /* First part goes into this subdev */
73                         size = subdev->size - from;
74                 else
75                         /* Entire transaction goes into this subdev */
76                         size = len;
77
78                 err = subdev->read(subdev, from, size, &retsize, buf);
79
80                 if (err)
81                         break;
82
83                 *retlen += retsize;
84                 len -= size;
85                 if (len == 0)
86                         break;
87
88                 err = -EINVAL;
89                 buf += size;
90                 from = 0;
91         }
92         return err;
93 }
94
95 static int
96 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
97              size_t * retlen, const u_char * buf)
98 {
99         struct mtd_concat *concat = CONCAT(mtd);
100         int err = -EINVAL;
101         int i;
102
103         if (!(mtd->flags & MTD_WRITEABLE))
104                 return -EROFS;
105
106         *retlen = 0;
107
108         for (i = 0; i < concat->num_subdev; i++) {
109                 struct mtd_info *subdev = concat->subdev[i];
110                 size_t size, retsize;
111
112                 if (to >= subdev->size) {
113                         size = 0;
114                         to -= subdev->size;
115                         continue;
116                 }
117                 if (to + len > subdev->size)
118                         size = subdev->size - to;
119                 else
120                         size = len;
121
122                 if (!(subdev->flags & MTD_WRITEABLE))
123                         err = -EROFS;
124                 else
125                         err = subdev->write(subdev, to, size, &retsize, buf);
126
127                 if (err)
128                         break;
129
130                 *retlen += retsize;
131                 len -= size;
132                 if (len == 0)
133                         break;
134
135                 err = -EINVAL;
136                 buf += size;
137                 to = 0;
138         }
139         return err;
140 }
141
142 static int
143 concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
144                 size_t * retlen, u_char * buf, u_char * eccbuf,
145                 struct nand_oobinfo *oobsel)
146 {
147         struct mtd_concat *concat = CONCAT(mtd);
148         int err = -EINVAL;
149         int i;
150
151         *retlen = 0;
152
153         for (i = 0; i < concat->num_subdev; i++) {
154                 struct mtd_info *subdev = concat->subdev[i];
155                 size_t size, retsize;
156
157                 if (from >= subdev->size) {
158                         /* Not destined for this subdev */
159                         size = 0;
160                         from -= subdev->size;
161                         continue;
162                 }
163
164                 if (from + len > subdev->size)
165                         /* First part goes into this subdev */
166                         size = subdev->size - from;
167                 else
168                         /* Entire transaction goes into this subdev */
169                         size = len;
170
171                 if (subdev->read_ecc)
172                         err = subdev->read_ecc(subdev, from, size,
173                                                &retsize, buf, eccbuf, oobsel);
174                 else
175                         err = -EINVAL;
176
177                 if (err)
178                         break;
179
180                 *retlen += retsize;
181                 len -= size;
182                 if (len == 0)
183                         break;
184
185                 err = -EINVAL;
186                 buf += size;
187                 if (eccbuf) {
188                         eccbuf += subdev->oobsize;
189                         /* in nand.c at least, eccbufs are
190                            tagged with 2 (int)eccstatus'; we
191                            must account for these */
192                         eccbuf += 2 * (sizeof (int));
193                 }
194                 from = 0;
195         }
196         return err;
197 }
198
199 static int
200 concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
201                  size_t * retlen, const u_char * buf, u_char * eccbuf,
202                  struct nand_oobinfo *oobsel)
203 {
204         struct mtd_concat *concat = CONCAT(mtd);
205         int err = -EINVAL;
206         int i;
207
208         if (!(mtd->flags & MTD_WRITEABLE))
209                 return -EROFS;
210
211         *retlen = 0;
212
213         for (i = 0; i < concat->num_subdev; i++) {
214                 struct mtd_info *subdev = concat->subdev[i];
215                 size_t size, retsize;
216
217                 if (to >= subdev->size) {
218                         size = 0;
219                         to -= subdev->size;
220                         continue;
221                 }
222                 if (to + len > subdev->size)
223                         size = subdev->size - to;
224                 else
225                         size = len;
226
227                 if (!(subdev->flags & MTD_WRITEABLE))
228                         err = -EROFS;
229                 else if (subdev->write_ecc)
230                         err = subdev->write_ecc(subdev, to, size,
231                                                 &retsize, buf, eccbuf, oobsel);
232                 else
233                         err = -EINVAL;
234
235                 if (err)
236                         break;
237
238                 *retlen += retsize;
239                 len -= size;
240                 if (len == 0)
241                         break;
242
243                 err = -EINVAL;
244                 buf += size;
245                 if (eccbuf)
246                         eccbuf += subdev->oobsize;
247                 to = 0;
248         }
249         return err;
250 }
251
252 static int
253 concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
254                 size_t * retlen, u_char * buf)
255 {
256         struct mtd_concat *concat = CONCAT(mtd);
257         int err = -EINVAL;
258         int i;
259
260         *retlen = 0;
261
262         for (i = 0; i < concat->num_subdev; i++) {
263                 struct mtd_info *subdev = concat->subdev[i];
264                 size_t size, retsize;
265
266                 if (from >= subdev->size) {
267                         /* Not destined for this subdev */
268                         size = 0;
269                         from -= subdev->size;
270                         continue;
271                 }
272                 if (from + len > subdev->size)
273                         /* First part goes into this subdev */
274                         size = subdev->size - from;
275                 else
276                         /* Entire transaction goes into this subdev */
277                         size = len;
278
279                 if (subdev->read_oob)
280                         err = subdev->read_oob(subdev, from, size,
281                                                &retsize, buf);
282                 else
283                         err = -EINVAL;
284
285                 if (err)
286                         break;
287
288                 *retlen += retsize;
289                 len -= size;
290                 if (len == 0)
291                         break;
292
293                 err = -EINVAL;
294                 buf += size;
295                 from = 0;
296         }
297         return err;
298 }
299
300 static int
301 concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
302                  size_t * retlen, const u_char * buf)
303 {
304         struct mtd_concat *concat = CONCAT(mtd);
305         int err = -EINVAL;
306         int i;
307
308         if (!(mtd->flags & MTD_WRITEABLE))
309                 return -EROFS;
310
311         *retlen = 0;
312
313         for (i = 0; i < concat->num_subdev; i++) {
314                 struct mtd_info *subdev = concat->subdev[i];
315                 size_t size, retsize;
316
317                 if (to >= subdev->size) {
318                         size = 0;
319                         to -= subdev->size;
320                         continue;
321                 }
322                 if (to + len > subdev->size)
323                         size = subdev->size - to;
324                 else
325                         size = len;
326
327                 if (!(subdev->flags & MTD_WRITEABLE))
328                         err = -EROFS;
329                 else if (subdev->write_oob)
330                         err = subdev->write_oob(subdev, to, size, &retsize,
331                                                 buf);
332                 else
333                         err = -EINVAL;
334
335                 if (err)
336                         break;
337
338                 *retlen += retsize;
339                 len -= size;
340                 if (len == 0)
341                         break;
342
343                 err = -EINVAL;
344                 buf += size;
345                 to = 0;
346         }
347         return err;
348 }
349
350 static void concat_erase_callback(struct erase_info *instr)
351 {
352         wake_up((wait_queue_head_t *) instr->priv);
353 }
354
355 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
356 {
357         int err;
358         wait_queue_head_t waitq;
359         DECLARE_WAITQUEUE(wait, current);
360
361         /*
362          * This code was stol^H^H^H^Hinspired by mtdchar.c
363          */
364         init_waitqueue_head(&waitq);
365
366         erase->mtd = mtd;
367         erase->callback = concat_erase_callback;
368         erase->priv = (unsigned long) &waitq;
369
370         /*
371          * FIXME: Allow INTERRUPTIBLE. Which means
372          * not having the wait_queue head on the stack.
373          */
374         err = mtd->erase(mtd, erase);
375         if (!err) {
376                 set_current_state(TASK_UNINTERRUPTIBLE);
377                 add_wait_queue(&waitq, &wait);
378                 if (erase->state != MTD_ERASE_DONE
379                     && erase->state != MTD_ERASE_FAILED)
380                         schedule();
381                 remove_wait_queue(&waitq, &wait);
382                 set_current_state(TASK_RUNNING);
383
384                 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
385         }
386         return err;
387 }
388
389 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
390 {
391         struct mtd_concat *concat = CONCAT(mtd);
392         struct mtd_info *subdev;
393         int i, err;
394         u_int32_t length;
395         struct erase_info *erase;
396
397         if (!(mtd->flags & MTD_WRITEABLE))
398                 return -EROFS;
399
400         if (instr->addr > concat->mtd.size)
401                 return -EINVAL;
402
403         if (instr->len + instr->addr > concat->mtd.size)
404                 return -EINVAL;
405
406         /*
407          * Check for proper erase block alignment of the to-be-erased area.
408          * It is easier to do this based on the super device's erase
409          * region info rather than looking at each particular sub-device
410          * in turn.
411          */
412         if (!concat->mtd.numeraseregions) {
413                 /* the easy case: device has uniform erase block size */
414                 if (instr->addr & (concat->mtd.erasesize - 1))
415                         return -EINVAL;
416                 if (instr->len & (concat->mtd.erasesize - 1))
417                         return -EINVAL;
418         } else {
419                 /* device has variable erase size */
420                 struct mtd_erase_region_info *erase_regions =
421                     concat->mtd.eraseregions;
422
423                 /*
424                  * Find the erase region where the to-be-erased area begins:
425                  */
426                 for (i = 0; i < concat->mtd.numeraseregions &&
427                      instr->addr >= erase_regions[i].offset; i++) ;
428                 --i;
429
430                 /*
431                  * Now erase_regions[i] is the region in which the
432                  * to-be-erased area begins. Verify that the starting
433                  * offset is aligned to this region's erase size:
434                  */
435                 if (instr->addr & (erase_regions[i].erasesize - 1))
436                         return -EINVAL;
437
438                 /*
439                  * now find the erase region where the to-be-erased area ends:
440                  */
441                 for (; i < concat->mtd.numeraseregions &&
442                      (instr->addr + instr->len) >= erase_regions[i].offset;
443                      ++i) ;
444                 --i;
445                 /*
446                  * check if the ending offset is aligned to this region's erase size
447                  */
448                 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
449                                                   1))
450                         return -EINVAL;
451         }
452
453         /* make a local copy of instr to avoid modifying the caller's struct */
454         erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
455
456         if (!erase)
457                 return -ENOMEM;
458
459         *erase = *instr;
460         length = instr->len;
461
462         /*
463          * find the subdevice where the to-be-erased area begins, adjust
464          * starting offset to be relative to the subdevice start
465          */
466         for (i = 0; i < concat->num_subdev; i++) {
467                 subdev = concat->subdev[i];
468                 if (subdev->size <= erase->addr)
469                         erase->addr -= subdev->size;
470                 else
471                         break;
472         }
473
474         /* must never happen since size limit has been verified above */
475         if (i >= concat->num_subdev)
476                 BUG();
477
478         /* now do the erase: */
479         err = 0;
480         for (; length > 0; i++) {
481                 /* loop for all subdevices affected by this request */
482                 subdev = concat->subdev[i];     /* get current subdevice */
483
484                 /* limit length to subdevice's size: */
485                 if (erase->addr + length > subdev->size)
486                         erase->len = subdev->size - erase->addr;
487                 else
488                         erase->len = length;
489
490                 if (!(subdev->flags & MTD_WRITEABLE)) {
491                         err = -EROFS;
492                         break;
493                 }
494                 length -= erase->len;
495                 if ((err = concat_dev_erase(subdev, erase))) {
496                         /* sanity check: should never happen since
497                          * block alignment has been checked above */
498                         if (err == -EINVAL)
499                                 BUG();
500                         break;
501                 }
502                 /*
503                  * erase->addr specifies the offset of the area to be
504                  * erased *within the current subdevice*. It can be
505                  * non-zero only the first time through this loop, i.e.
506                  * for the first subdevice where blocks need to be erased.
507                  * All the following erases must begin at the start of the
508                  * current subdevice, i.e. at offset zero.
509                  */
510                 erase->addr = 0;
511         }
512         kfree(erase);
513         if (err)
514                 return err;
515
516         instr->state = MTD_ERASE_DONE;
517         if (instr->callback)
518                 instr->callback(instr);
519         return 0;
520 }
521
522 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
523 {
524         struct mtd_concat *concat = CONCAT(mtd);
525         int i, err = -EINVAL;
526
527         if ((len + ofs) > mtd->size)
528                 return -EINVAL;
529
530         for (i = 0; i < concat->num_subdev; i++) {
531                 struct mtd_info *subdev = concat->subdev[i];
532                 size_t size;
533
534                 if (ofs >= subdev->size) {
535                         size = 0;
536                         ofs -= subdev->size;
537                         continue;
538                 }
539                 if (ofs + len > subdev->size)
540                         size = subdev->size - ofs;
541                 else
542                         size = len;
543
544                 err = subdev->lock(subdev, ofs, size);
545
546                 if (err)
547                         break;
548
549                 len -= size;
550                 if (len == 0)
551                         break;
552
553                 err = -EINVAL;
554                 ofs = 0;
555         }
556
557         return err;
558 }
559
560 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
561 {
562         struct mtd_concat *concat = CONCAT(mtd);
563         int i, err = 0;
564
565         if ((len + ofs) > mtd->size)
566                 return -EINVAL;
567
568         for (i = 0; i < concat->num_subdev; i++) {
569                 struct mtd_info *subdev = concat->subdev[i];
570                 size_t size;
571
572                 if (ofs >= subdev->size) {
573                         size = 0;
574                         ofs -= subdev->size;
575                         continue;
576                 }
577                 if (ofs + len > subdev->size)
578                         size = subdev->size - ofs;
579                 else
580                         size = len;
581
582                 err = subdev->unlock(subdev, ofs, size);
583
584                 if (err)
585                         break;
586
587                 len -= size;
588                 if (len == 0)
589                         break;
590
591                 err = -EINVAL;
592                 ofs = 0;
593         }
594
595         return err;
596 }
597
598 static void concat_sync(struct mtd_info *mtd)
599 {
600         struct mtd_concat *concat = CONCAT(mtd);
601         int i;
602
603         for (i = 0; i < concat->num_subdev; i++) {
604                 struct mtd_info *subdev = concat->subdev[i];
605                 subdev->sync(subdev);
606         }
607 }
608
609 static int concat_suspend(struct mtd_info *mtd)
610 {
611         struct mtd_concat *concat = CONCAT(mtd);
612         int i, rc = 0;
613
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;
618         }
619         return rc;
620 }
621
622 static void concat_resume(struct mtd_info *mtd)
623 {
624         struct mtd_concat *concat = CONCAT(mtd);
625         int i;
626
627         for (i = 0; i < concat->num_subdev; i++) {
628                 struct mtd_info *subdev = concat->subdev[i];
629                 subdev->resume(subdev);
630         }
631 }
632
633 /*
634  * This function constructs a virtual MTD device by concatenating
635  * num_devs MTD devices. A pointer to the new device object is
636  * stored to *new_dev upon success. This function does _not_
637  * register any devices: this is the caller's responsibility.
638  */
639 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],   /* subdevices to concatenate */
640                                    int num_devs,        /* number of subdevices      */
641                                    char *name)
642 {                               /* name for the new device   */
643         int i;
644         size_t size;
645         struct mtd_concat *concat;
646         u_int32_t max_erasesize, curr_erasesize;
647         int num_erase_region;
648
649         printk(KERN_NOTICE "Concatenating MTD devices:\n");
650         for (i = 0; i < num_devs; i++)
651                 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
652         printk(KERN_NOTICE "into device \"%s\"\n", name);
653
654         /* allocate the device structure */
655         size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
656         concat = kmalloc(size, GFP_KERNEL);
657         if (!concat) {
658                 printk
659                     ("memory allocation error while creating concatenated device \"%s\"\n",
660                      name);
661                 return NULL;
662         }
663         memset(concat, 0, size);
664         concat->subdev = (struct mtd_info **) (concat + 1);
665
666         /*
667          * Set up the new "super" device's MTD object structure, check for
668          * incompatibilites between the subdevices.
669          */
670         concat->mtd.type = subdev[0]->type;
671         concat->mtd.flags = subdev[0]->flags;
672         concat->mtd.size = subdev[0]->size;
673         concat->mtd.erasesize = subdev[0]->erasesize;
674         concat->mtd.oobblock = subdev[0]->oobblock;
675         concat->mtd.oobsize = subdev[0]->oobsize;
676         concat->mtd.ecctype = subdev[0]->ecctype;
677         concat->mtd.eccsize = subdev[0]->eccsize;
678         if (subdev[0]->read_ecc)
679                 concat->mtd.read_ecc = concat_read_ecc;
680         if (subdev[0]->write_ecc)
681                 concat->mtd.write_ecc = concat_write_ecc;
682         if (subdev[0]->read_oob)
683                 concat->mtd.read_oob = concat_read_oob;
684         if (subdev[0]->write_oob)
685                 concat->mtd.write_oob = concat_write_oob;
686
687         concat->subdev[0] = subdev[0];
688
689         for (i = 1; i < num_devs; i++) {
690                 if (concat->mtd.type != subdev[i]->type) {
691                         kfree(concat);
692                         printk("Incompatible device type on \"%s\"\n",
693                                subdev[i]->name);
694                         return NULL;
695                 }
696                 if (concat->mtd.flags != subdev[i]->flags) {
697                         /*
698                          * Expect all flags except MTD_WRITEABLE to be
699                          * equal on all subdevices.
700                          */
701                         if ((concat->mtd.flags ^ subdev[i]->
702                              flags) & ~MTD_WRITEABLE) {
703                                 kfree(concat);
704                                 printk("Incompatible device flags on \"%s\"\n",
705                                        subdev[i]->name);
706                                 return NULL;
707                         } else
708                                 /* if writeable attribute differs,
709                                    make super device writeable */
710                                 concat->mtd.flags |=
711                                     subdev[i]->flags & MTD_WRITEABLE;
712                 }
713                 concat->mtd.size += subdev[i]->size;
714                 if (concat->mtd.oobblock   !=  subdev[i]->oobblock ||
715                     concat->mtd.oobsize    !=  subdev[i]->oobsize ||
716                     concat->mtd.ecctype    !=  subdev[i]->ecctype ||
717                     concat->mtd.eccsize    !=  subdev[i]->eccsize ||
718                     !concat->mtd.read_ecc  != !subdev[i]->read_ecc ||
719                     !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
720                     !concat->mtd.read_oob  != !subdev[i]->read_oob ||
721                     !concat->mtd.write_oob != !subdev[i]->write_oob) {
722                         kfree(concat);
723                         printk("Incompatible OOB or ECC data on \"%s\"\n",
724                                subdev[i]->name);
725                         return NULL;
726                 }
727                 concat->subdev[i] = subdev[i];
728
729         }
730
731         concat->num_subdev = num_devs;
732         concat->mtd.name = name;
733
734         /*
735          * NOTE: for now, we do not provide any readv()/writev() methods
736          *       because they are messy to implement and they are not
737          *       used to a great extent anyway.
738          */
739         concat->mtd.erase = concat_erase;
740         concat->mtd.read = concat_read;
741         concat->mtd.write = concat_write;
742         concat->mtd.sync = concat_sync;
743         concat->mtd.lock = concat_lock;
744         concat->mtd.unlock = concat_unlock;
745         concat->mtd.suspend = concat_suspend;
746         concat->mtd.resume = concat_resume;
747
748         /*
749          * Combine the erase block size info of the subdevices:
750          *
751          * first, walk the map of the new device and see how
752          * many changes in erase size we have
753          */
754         max_erasesize = curr_erasesize = subdev[0]->erasesize;
755         num_erase_region = 1;
756         for (i = 0; i < num_devs; i++) {
757                 if (subdev[i]->numeraseregions == 0) {
758                         /* current subdevice has uniform erase size */
759                         if (subdev[i]->erasesize != curr_erasesize) {
760                                 /* if it differs from the last subdevice's erase size, count it */
761                                 ++num_erase_region;
762                                 curr_erasesize = subdev[i]->erasesize;
763                                 if (curr_erasesize > max_erasesize)
764                                         max_erasesize = curr_erasesize;
765                         }
766                 } else {
767                         /* current subdevice has variable erase size */
768                         int j;
769                         for (j = 0; j < subdev[i]->numeraseregions; j++) {
770
771                                 /* walk the list of erase regions, count any changes */
772                                 if (subdev[i]->eraseregions[j].erasesize !=
773                                     curr_erasesize) {
774                                         ++num_erase_region;
775                                         curr_erasesize =
776                                             subdev[i]->eraseregions[j].
777                                             erasesize;
778                                         if (curr_erasesize > max_erasesize)
779                                                 max_erasesize = curr_erasesize;
780                                 }
781                         }
782                 }
783         }
784
785         if (num_erase_region == 1) {
786                 /*
787                  * All subdevices have the same uniform erase size.
788                  * This is easy:
789                  */
790                 concat->mtd.erasesize = curr_erasesize;
791                 concat->mtd.numeraseregions = 0;
792         } else {
793                 /*
794                  * erase block size varies across the subdevices: allocate
795                  * space to store the data describing the variable erase regions
796                  */
797                 struct mtd_erase_region_info *erase_region_p;
798                 u_int32_t begin, position;
799
800                 concat->mtd.erasesize = max_erasesize;
801                 concat->mtd.numeraseregions = num_erase_region;
802                 concat->mtd.eraseregions = erase_region_p =
803                     kmalloc(num_erase_region *
804                             sizeof (struct mtd_erase_region_info), GFP_KERNEL);
805                 if (!erase_region_p) {
806                         kfree(concat);
807                         printk
808                             ("memory allocation error while creating erase region list"
809                              " for device \"%s\"\n", name);
810                         return NULL;
811                 }
812
813                 /*
814                  * walk the map of the new device once more and fill in
815                  * in erase region info:
816                  */
817                 curr_erasesize = subdev[0]->erasesize;
818                 begin = position = 0;
819                 for (i = 0; i < num_devs; i++) {
820                         if (subdev[i]->numeraseregions == 0) {
821                                 /* current subdevice has uniform erase size */
822                                 if (subdev[i]->erasesize != curr_erasesize) {
823                                         /*
824                                          *  fill in an mtd_erase_region_info structure for the area
825                                          *  we have walked so far:
826                                          */
827                                         erase_region_p->offset = begin;
828                                         erase_region_p->erasesize =
829                                             curr_erasesize;
830                                         erase_region_p->numblocks =
831                                             (position - begin) / curr_erasesize;
832                                         begin = position;
833
834                                         curr_erasesize = subdev[i]->erasesize;
835                                         ++erase_region_p;
836                                 }
837                                 position += subdev[i]->size;
838                         } else {
839                                 /* current subdevice has variable erase size */
840                                 int j;
841                                 for (j = 0; j < subdev[i]->numeraseregions; j++) {
842                                         /* walk the list of erase regions, count any changes */
843                                         if (subdev[i]->eraseregions[j].
844                                             erasesize != curr_erasesize) {
845                                                 erase_region_p->offset = begin;
846                                                 erase_region_p->erasesize =
847                                                     curr_erasesize;
848                                                 erase_region_p->numblocks =
849                                                     (position -
850                                                      begin) / curr_erasesize;
851                                                 begin = position;
852
853                                                 curr_erasesize =
854                                                     subdev[i]->eraseregions[j].
855                                                     erasesize;
856                                                 ++erase_region_p;
857                                         }
858                                         position +=
859                                             subdev[i]->eraseregions[j].
860                                             numblocks * curr_erasesize;
861                                 }
862                         }
863                 }
864                 /* Now write the final entry */
865                 erase_region_p->offset = begin;
866                 erase_region_p->erasesize = curr_erasesize;
867                 erase_region_p->numblocks = (position - begin) / curr_erasesize;
868         }
869
870         return &concat->mtd;
871 }
872
873 /* 
874  * This function destroys an MTD object obtained from concat_mtd_devs()
875  */
876
877 void mtd_concat_destroy(struct mtd_info *mtd)
878 {
879         struct mtd_concat *concat = CONCAT(mtd);
880         if (concat->mtd.numeraseregions)
881                 kfree(concat->mtd.eraseregions);
882         kfree(concat);
883 }
884
885 EXPORT_SYMBOL(mtd_concat_create);
886 EXPORT_SYMBOL(mtd_concat_destroy);
887
888 MODULE_LICENSE("GPL");
889 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
890 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");