[MTD] Improve heuristic for detecting wrong-endian RedBoot partition table
[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 ret = 0, err;
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                 /* 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;
95                 }
96
97                 *retlen += retsize;
98                 len -= size;
99                 if (len == 0)
100                         return ret;
101
102                 buf += size;
103                 from = 0;
104         }
105         return -EINVAL;
106 }
107
108 static int
109 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
110              size_t * retlen, const u_char * buf)
111 {
112         struct mtd_concat *concat = CONCAT(mtd);
113         int err = -EINVAL;
114         int i;
115
116         if (!(mtd->flags & MTD_WRITEABLE))
117                 return -EROFS;
118
119         *retlen = 0;
120
121         for (i = 0; i < concat->num_subdev; i++) {
122                 struct mtd_info *subdev = concat->subdev[i];
123                 size_t size, retsize;
124
125                 if (to >= subdev->size) {
126                         size = 0;
127                         to -= subdev->size;
128                         continue;
129                 }
130                 if (to + len > subdev->size)
131                         size = subdev->size - to;
132                 else
133                         size = len;
134
135                 if (!(subdev->flags & MTD_WRITEABLE))
136                         err = -EROFS;
137                 else
138                         err = subdev->write(subdev, to, size, &retsize, buf);
139
140                 if (err)
141                         break;
142
143                 *retlen += retsize;
144                 len -= size;
145                 if (len == 0)
146                         break;
147
148                 err = -EINVAL;
149                 buf += size;
150                 to = 0;
151         }
152         return err;
153 }
154
155 static int
156 concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
157                 unsigned long count, loff_t to, size_t * retlen)
158 {
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;
165
166         if (!(mtd->flags & MTD_WRITEABLE))
167                 return -EROFS;
168
169         *retlen = 0;
170
171         /* Calculate total length of data */
172         for (i = 0; i < count; i++)
173                 total_len += vecs[i].iov_len;
174
175         /* Do not allow write past end of device */
176         if ((to + total_len) > mtd->size)
177                 return -EINVAL;
178
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;
184         }
185
186         /* make a copy of vecs */
187         vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
188         if (!vecs_copy)
189                 return -ENOMEM;
190         memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
191
192         entry_low = 0;
193         for (i = 0; i < concat->num_subdev; i++) {
194                 struct mtd_info *subdev = concat->subdev[i];
195                 size_t size, wsize, retsize, old_iov_len;
196
197                 if (to >= subdev->size) {
198                         to -= subdev->size;
199                         continue;
200                 }
201
202                 size = min(total_len, (size_t)(subdev->size - to));
203                 wsize = size; /* store for future use */
204
205                 entry_high = entry_low;
206                 while (entry_high < count) {
207                         if (size <= vecs_copy[entry_high].iov_len)
208                                 break;
209                         size -= vecs_copy[entry_high++].iov_len;
210                 }
211
212                 old_iov_len = vecs_copy[entry_high].iov_len;
213                 vecs_copy[entry_high].iov_len = size;
214
215                 if (!(subdev->flags & MTD_WRITEABLE))
216                         err = -EROFS;
217                 else
218                         err = subdev->writev(subdev, &vecs_copy[entry_low],
219                                 entry_high - entry_low + 1, to, &retsize);
220
221                 vecs_copy[entry_high].iov_len = old_iov_len - size;
222                 vecs_copy[entry_high].iov_base += size;
223
224                 entry_low = entry_high;
225
226                 if (err)
227                         break;
228
229                 *retlen += retsize;
230                 total_len -= wsize;
231
232                 if (total_len == 0)
233                         break;
234
235                 err = -EINVAL;
236                 to = 0;
237         }
238
239         kfree(vecs_copy);
240         return err;
241 }
242
243 static int
244 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
245 {
246         struct mtd_concat *concat = CONCAT(mtd);
247         struct mtd_oob_ops devops = *ops;
248         int i, err, ret = 0;
249
250         ops->retlen = ops->oobretlen = 0;
251
252         for (i = 0; i < concat->num_subdev; i++) {
253                 struct mtd_info *subdev = concat->subdev[i];
254
255                 if (from >= subdev->size) {
256                         from -= subdev->size;
257                         continue;
258                 }
259
260                 /* partial read ? */
261                 if (from + devops.len > subdev->size)
262                         devops.len = subdev->size - from;
263
264                 err = subdev->read_oob(subdev, from, &devops);
265                 ops->retlen += devops.retlen;
266                 ops->oobretlen += devops.oobretlen;
267
268                 /* Save information about bitflips! */
269                 if (unlikely(err)) {
270                         if (err == -EBADMSG) {
271                                 mtd->ecc_stats.failed++;
272                                 ret = err;
273                         } else if (err == -EUCLEAN) {
274                                 mtd->ecc_stats.corrected++;
275                                 /* Do not overwrite -EBADMSG !! */
276                                 if (!ret)
277                                         ret = err;
278                         } else
279                                 return err;
280                 }
281
282                 if (devops.datbuf) {
283                         devops.len = ops->len - ops->retlen;
284                         if (!devops.len)
285                                 return ret;
286                         devops.datbuf += devops.retlen;
287                 }
288                 if (devops.oobbuf) {
289                         devops.ooblen = ops->ooblen - ops->oobretlen;
290                         if (!devops.ooblen)
291                                 return ret;
292                         devops.oobbuf += ops->oobretlen;
293                 }
294
295                 from = 0;
296         }
297         return -EINVAL;
298 }
299
300 static int
301 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
302 {
303         struct mtd_concat *concat = CONCAT(mtd);
304         struct mtd_oob_ops devops = *ops;
305         int i, err;
306
307         if (!(mtd->flags & MTD_WRITEABLE))
308                 return -EROFS;
309
310         ops->retlen = 0;
311
312         for (i = 0; i < concat->num_subdev; i++) {
313                 struct mtd_info *subdev = concat->subdev[i];
314
315                 if (to >= subdev->size) {
316                         to -= subdev->size;
317                         continue;
318                 }
319
320                 /* partial write ? */
321                 if (to + devops.len > subdev->size)
322                         devops.len = subdev->size - to;
323
324                 err = subdev->write_oob(subdev, to, &devops);
325                 ops->retlen += devops.retlen;
326                 if (err)
327                         return err;
328
329                 if (devops.datbuf) {
330                         devops.len = ops->len - ops->retlen;
331                         if (!devops.len)
332                                 return 0;
333                         devops.datbuf += devops.retlen;
334                 }
335                 if (devops.oobbuf) {
336                         devops.ooblen = ops->ooblen - ops->oobretlen;
337                         if (!devops.ooblen)
338                                 return 0;
339                         devops.oobbuf += devops.oobretlen;
340                 }
341                 to = 0;
342         }
343         return -EINVAL;
344 }
345
346 static void concat_erase_callback(struct erase_info *instr)
347 {
348         wake_up((wait_queue_head_t *) instr->priv);
349 }
350
351 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
352 {
353         int err;
354         wait_queue_head_t waitq;
355         DECLARE_WAITQUEUE(wait, current);
356
357         /*
358          * This code was stol^H^H^H^Hinspired by mtdchar.c
359          */
360         init_waitqueue_head(&waitq);
361
362         erase->mtd = mtd;
363         erase->callback = concat_erase_callback;
364         erase->priv = (unsigned long) &waitq;
365
366         /*
367          * FIXME: Allow INTERRUPTIBLE. Which means
368          * not having the wait_queue head on the stack.
369          */
370         err = mtd->erase(mtd, erase);
371         if (!err) {
372                 set_current_state(TASK_UNINTERRUPTIBLE);
373                 add_wait_queue(&waitq, &wait);
374                 if (erase->state != MTD_ERASE_DONE
375                     && erase->state != MTD_ERASE_FAILED)
376                         schedule();
377                 remove_wait_queue(&waitq, &wait);
378                 set_current_state(TASK_RUNNING);
379
380                 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
381         }
382         return err;
383 }
384
385 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
386 {
387         struct mtd_concat *concat = CONCAT(mtd);
388         struct mtd_info *subdev;
389         int i, err;
390         u_int32_t length, offset = 0;
391         struct erase_info *erase;
392
393         if (!(mtd->flags & MTD_WRITEABLE))
394                 return -EROFS;
395
396         if (instr->addr > concat->mtd.size)
397                 return -EINVAL;
398
399         if (instr->len + instr->addr > concat->mtd.size)
400                 return -EINVAL;
401
402         /*
403          * Check for proper erase block alignment of the to-be-erased area.
404          * It is easier to do this based on the super device's erase
405          * region info rather than looking at each particular sub-device
406          * in turn.
407          */
408         if (!concat->mtd.numeraseregions) {
409                 /* the easy case: device has uniform erase block size */
410                 if (instr->addr & (concat->mtd.erasesize - 1))
411                         return -EINVAL;
412                 if (instr->len & (concat->mtd.erasesize - 1))
413                         return -EINVAL;
414         } else {
415                 /* device has variable erase size */
416                 struct mtd_erase_region_info *erase_regions =
417                     concat->mtd.eraseregions;
418
419                 /*
420                  * Find the erase region where the to-be-erased area begins:
421                  */
422                 for (i = 0; i < concat->mtd.numeraseregions &&
423                      instr->addr >= erase_regions[i].offset; i++) ;
424                 --i;
425
426                 /*
427                  * Now erase_regions[i] is the region in which the
428                  * to-be-erased area begins. Verify that the starting
429                  * offset is aligned to this region's erase size:
430                  */
431                 if (instr->addr & (erase_regions[i].erasesize - 1))
432                         return -EINVAL;
433
434                 /*
435                  * now find the erase region where the to-be-erased area ends:
436                  */
437                 for (; i < concat->mtd.numeraseregions &&
438                      (instr->addr + instr->len) >= erase_regions[i].offset;
439                      ++i) ;
440                 --i;
441                 /*
442                  * check if the ending offset is aligned to this region's erase size
443                  */
444                 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
445                                                   1))
446                         return -EINVAL;
447         }
448
449         instr->fail_addr = 0xffffffff;
450
451         /* make a local copy of instr to avoid modifying the caller's struct */
452         erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
453
454         if (!erase)
455                 return -ENOMEM;
456
457         *erase = *instr;
458         length = instr->len;
459
460         /*
461          * find the subdevice where the to-be-erased area begins, adjust
462          * starting offset to be relative to the subdevice start
463          */
464         for (i = 0; i < concat->num_subdev; i++) {
465                 subdev = concat->subdev[i];
466                 if (subdev->size <= erase->addr) {
467                         erase->addr -= subdev->size;
468                         offset += subdev->size;
469                 } else {
470                         break;
471                 }
472         }
473
474         /* must never happen since size limit has been verified above */
475         BUG_ON(i >= concat->num_subdev);
476
477         /* now do the erase: */
478         err = 0;
479         for (; length > 0; i++) {
480                 /* loop for all subdevices affected by this request */
481                 subdev = concat->subdev[i];     /* get current subdevice */
482
483                 /* limit length to subdevice's size: */
484                 if (erase->addr + length > subdev->size)
485                         erase->len = subdev->size - erase->addr;
486                 else
487                         erase->len = length;
488
489                 if (!(subdev->flags & MTD_WRITEABLE)) {
490                         err = -EROFS;
491                         break;
492                 }
493                 length -= erase->len;
494                 if ((err = concat_dev_erase(subdev, erase))) {
495                         /* sanity check: should never happen since
496                          * block alignment has been checked above */
497                         BUG_ON(err == -EINVAL);
498                         if (erase->fail_addr != 0xffffffff)
499                                 instr->fail_addr = erase->fail_addr + offset;
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                 offset += subdev->size;
512         }
513         instr->state = erase->state;
514         kfree(erase);
515         if (err)
516                 return err;
517
518         if (instr->callback)
519                 instr->callback(instr);
520         return 0;
521 }
522
523 static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
524 {
525         struct mtd_concat *concat = CONCAT(mtd);
526         int i, err = -EINVAL;
527
528         if ((len + ofs) > mtd->size)
529                 return -EINVAL;
530
531         for (i = 0; i < concat->num_subdev; i++) {
532                 struct mtd_info *subdev = concat->subdev[i];
533                 size_t size;
534
535                 if (ofs >= subdev->size) {
536                         size = 0;
537                         ofs -= subdev->size;
538                         continue;
539                 }
540                 if (ofs + len > subdev->size)
541                         size = subdev->size - ofs;
542                 else
543                         size = len;
544
545                 err = subdev->lock(subdev, ofs, size);
546
547                 if (err)
548                         break;
549
550                 len -= size;
551                 if (len == 0)
552                         break;
553
554                 err = -EINVAL;
555                 ofs = 0;
556         }
557
558         return err;
559 }
560
561 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
562 {
563         struct mtd_concat *concat = CONCAT(mtd);
564         int i, err = 0;
565
566         if ((len + ofs) > mtd->size)
567                 return -EINVAL;
568
569         for (i = 0; i < concat->num_subdev; i++) {
570                 struct mtd_info *subdev = concat->subdev[i];
571                 size_t size;
572
573                 if (ofs >= subdev->size) {
574                         size = 0;
575                         ofs -= subdev->size;
576                         continue;
577                 }
578                 if (ofs + len > subdev->size)
579                         size = subdev->size - ofs;
580                 else
581                         size = len;
582
583                 err = subdev->unlock(subdev, ofs, size);
584
585                 if (err)
586                         break;
587
588                 len -= size;
589                 if (len == 0)
590                         break;
591
592                 err = -EINVAL;
593                 ofs = 0;
594         }
595
596         return err;
597 }
598
599 static void concat_sync(struct mtd_info *mtd)
600 {
601         struct mtd_concat *concat = CONCAT(mtd);
602         int i;
603
604         for (i = 0; i < concat->num_subdev; i++) {
605                 struct mtd_info *subdev = concat->subdev[i];
606                 subdev->sync(subdev);
607         }
608 }
609
610 static int concat_suspend(struct mtd_info *mtd)
611 {
612         struct mtd_concat *concat = CONCAT(mtd);
613         int i, rc = 0;
614
615         for (i = 0; i < concat->num_subdev; i++) {
616                 struct mtd_info *subdev = concat->subdev[i];
617                 if ((rc = subdev->suspend(subdev)) < 0)
618                         return rc;
619         }
620         return rc;
621 }
622
623 static void concat_resume(struct mtd_info *mtd)
624 {
625         struct mtd_concat *concat = CONCAT(mtd);
626         int i;
627
628         for (i = 0; i < concat->num_subdev; i++) {
629                 struct mtd_info *subdev = concat->subdev[i];
630                 subdev->resume(subdev);
631         }
632 }
633
634 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
635 {
636         struct mtd_concat *concat = CONCAT(mtd);
637         int i, res = 0;
638
639         if (!concat->subdev[0]->block_isbad)
640                 return res;
641
642         if (ofs > mtd->size)
643                 return -EINVAL;
644
645         for (i = 0; i < concat->num_subdev; i++) {
646                 struct mtd_info *subdev = concat->subdev[i];
647
648                 if (ofs >= subdev->size) {
649                         ofs -= subdev->size;
650                         continue;
651                 }
652
653                 res = subdev->block_isbad(subdev, ofs);
654                 break;
655         }
656
657         return res;
658 }
659
660 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
661 {
662         struct mtd_concat *concat = CONCAT(mtd);
663         int i, err = -EINVAL;
664
665         if (!concat->subdev[0]->block_markbad)
666                 return 0;
667
668         if (ofs > mtd->size)
669                 return -EINVAL;
670
671         for (i = 0; i < concat->num_subdev; i++) {
672                 struct mtd_info *subdev = concat->subdev[i];
673
674                 if (ofs >= subdev->size) {
675                         ofs -= subdev->size;
676                         continue;
677                 }
678
679                 err = subdev->block_markbad(subdev, ofs);
680                 if (!err)
681                         mtd->ecc_stats.badblocks++;
682                 break;
683         }
684
685         return err;
686 }
687
688 /*
689  * This function constructs a virtual MTD device by concatenating
690  * num_devs MTD devices. A pointer to the new device object is
691  * stored to *new_dev upon success. This function does _not_
692  * register any devices: this is the caller's responsibility.
693  */
694 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],   /* subdevices to concatenate */
695                                    int num_devs,        /* number of subdevices      */
696                                    char *name)
697 {                               /* name for the new device   */
698         int i;
699         size_t size;
700         struct mtd_concat *concat;
701         u_int32_t max_erasesize, curr_erasesize;
702         int num_erase_region;
703
704         printk(KERN_NOTICE "Concatenating MTD devices:\n");
705         for (i = 0; i < num_devs; i++)
706                 printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
707         printk(KERN_NOTICE "into device \"%s\"\n", name);
708
709         /* allocate the device structure */
710         size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
711         concat = kzalloc(size, GFP_KERNEL);
712         if (!concat) {
713                 printk
714                     ("memory allocation error while creating concatenated device \"%s\"\n",
715                      name);
716                 return NULL;
717         }
718         concat->subdev = (struct mtd_info **) (concat + 1);
719
720         /*
721          * Set up the new "super" device's MTD object structure, check for
722          * incompatibilites between the subdevices.
723          */
724         concat->mtd.type = subdev[0]->type;
725         concat->mtd.flags = subdev[0]->flags;
726         concat->mtd.size = subdev[0]->size;
727         concat->mtd.erasesize = subdev[0]->erasesize;
728         concat->mtd.writesize = subdev[0]->writesize;
729         concat->mtd.oobsize = subdev[0]->oobsize;
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;
740
741         concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
742
743         concat->subdev[0] = subdev[0];
744
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;
751                 }
752                 if (concat->mtd.flags != subdev[i]->flags) {
753                         /*
754                          * Expect all flags except MTD_WRITEABLE to be
755                          * equal on all subdevices.
756                          */
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;
768                 }
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;
781                 }
782                 concat->subdev[i] = subdev[i];
783
784         }
785
786         concat->mtd.ecclayout = subdev[0]->ecclayout;
787
788         concat->num_subdev = num_devs;
789         concat->mtd.name = name;
790
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;
799
800         /*
801          * Combine the erase block size info of the subdevices:
802          *
803          * first, walk the map of the new device and see how
804          * many changes in erase size we have
805          */
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;
817                         }
818                 } else {
819                         /* current subdevice has variable erase size */
820                         int j;
821                         for (j = 0; j < subdev[i]->numeraseregions; j++) {
822
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;
832                                 }
833                         }
834                 }
835         }
836
837         if (num_erase_region == 1) {
838                 /*
839                  * All subdevices have the same uniform erase size.
840                  * This is easy:
841                  */
842                 concat->mtd.erasesize = curr_erasesize;
843                 concat->mtd.numeraseregions = 0;
844         } else {
845                 /*
846                  * erase block size varies across the subdevices: allocate
847                  * space to store the data describing the variable erase regions
848                  */
849                 struct mtd_erase_region_info *erase_region_p;
850                 u_int32_t begin, position;
851
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;
863                 }
864
865                 /*
866                  * walk the map of the new device once more and fill in
867                  * in erase region info:
868                  */
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) {
875                                         /*
876                                          *  fill in an mtd_erase_region_info structure for the area
877                                          *  we have walked so far:
878                                          */
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;
885
886                                         curr_erasesize = subdev[i]->erasesize;
887                                         ++erase_region_p;
888                                 }
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;
904
905                                                 curr_erasesize =
906                                                     subdev[i]->eraseregions[j].
907                                                     erasesize;
908                                                 ++erase_region_p;
909                                         }
910                                         position +=
911                                             subdev[i]->eraseregions[j].
912                                             numblocks * curr_erasesize;
913                                 }
914                         }
915                 }
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;
920         }
921
922         return &concat->mtd;
923 }
924
925 /*
926  * This function destroys an MTD object obtained from concat_mtd_devs()
927  */
928
929 void mtd_concat_destroy(struct mtd_info *mtd)
930 {
931         struct mtd_concat *concat = CONCAT(mtd);
932         if (concat->mtd.numeraseregions)
933                 kfree(concat->mtd.eraseregions);
934         kfree(concat);
935 }
936
937 EXPORT_SYMBOL(mtd_concat_create);
938 EXPORT_SYMBOL(mtd_concat_destroy);
939
940 MODULE_LICENSE("GPL");
941 MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
942 MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");