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