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