Automatic merge of /spare/repo/netdev-2.6 branch 8139cp
[linux-2.6] / drivers / mtd / mtdpart.c
1 /*
2  * Simple MTD partitioning layer
3  *
4  * (C) 2000 Nicolas Pitre <nico@cam.org>
5  *
6  * This code is GPL
7  *
8  * $Id: mtdpart.c,v 1.51 2004/11/16 18:28:59 dwmw2 Exp $
9  *
10  *      02-21-2002      Thomas Gleixner <gleixner@autronix.de>
11  *                      added support for read_oob, write_oob
12  */     
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/config.h>
20 #include <linux/kmod.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/compatmac.h>
24
25 /* Our partition linked list */
26 static LIST_HEAD(mtd_partitions);
27
28 /* Our partition node structure */
29 struct mtd_part {
30         struct mtd_info mtd;
31         struct mtd_info *master;
32         u_int32_t offset;
33         int index;
34         struct list_head list;
35         int registered;
36 };
37
38 /*
39  * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
40  * the pointer to that structure with this macro.
41  */
42 #define PART(x)  ((struct mtd_part *)(x))
43
44         
45 /* 
46  * MTD methods which simply translate the effective address and pass through
47  * to the _real_ device.
48  */
49
50 static int part_read (struct mtd_info *mtd, loff_t from, size_t len, 
51                         size_t *retlen, u_char *buf)
52 {
53         struct mtd_part *part = PART(mtd);
54         if (from >= mtd->size)
55                 len = 0;
56         else if (from + len > mtd->size)
57                 len = mtd->size - from;
58         if (part->master->read_ecc == NULL)     
59                 return part->master->read (part->master, from + part->offset, 
60                                         len, retlen, buf);
61         else
62                 return part->master->read_ecc (part->master, from + part->offset, 
63                                         len, retlen, buf, NULL, &mtd->oobinfo);
64 }
65
66 static int part_point (struct mtd_info *mtd, loff_t from, size_t len, 
67                         size_t *retlen, u_char **buf)
68 {
69         struct mtd_part *part = PART(mtd);
70         if (from >= mtd->size)
71                 len = 0;
72         else if (from + len > mtd->size)
73                 len = mtd->size - from;
74         return part->master->point (part->master, from + part->offset, 
75                                     len, retlen, buf);
76 }
77 static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
78 {
79         struct mtd_part *part = PART(mtd);
80
81         part->master->unpoint (part->master, addr, from + part->offset, len);
82 }
83
84
85 static int part_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, 
86                         size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel)
87 {
88         struct mtd_part *part = PART(mtd);
89         if (oobsel == NULL)
90                 oobsel = &mtd->oobinfo;
91         if (from >= mtd->size)
92                 len = 0;
93         else if (from + len > mtd->size)
94                 len = mtd->size - from;
95         return part->master->read_ecc (part->master, from + part->offset, 
96                                         len, retlen, buf, eccbuf, oobsel);
97 }
98
99 static int part_read_oob (struct mtd_info *mtd, loff_t from, size_t len, 
100                         size_t *retlen, u_char *buf)
101 {
102         struct mtd_part *part = PART(mtd);
103         if (from >= mtd->size)
104                 len = 0;
105         else if (from + len > mtd->size)
106                 len = mtd->size - from;
107         return part->master->read_oob (part->master, from + part->offset, 
108                                         len, retlen, buf);
109 }
110
111 static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, 
112                         size_t *retlen, u_char *buf)
113 {
114         struct mtd_part *part = PART(mtd);
115         return part->master->read_user_prot_reg (part->master, from, 
116                                         len, retlen, buf);
117 }
118
119 static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, 
120                         size_t *retlen, u_char *buf)
121 {
122         struct mtd_part *part = PART(mtd);
123         return part->master->read_fact_prot_reg (part->master, from, 
124                                         len, retlen, buf);
125 }
126
127 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
128                         size_t *retlen, const u_char *buf)
129 {
130         struct mtd_part *part = PART(mtd);
131         if (!(mtd->flags & MTD_WRITEABLE))
132                 return -EROFS;
133         if (to >= mtd->size)
134                 len = 0;
135         else if (to + len > mtd->size)
136                 len = mtd->size - to;
137         if (part->master->write_ecc == NULL)    
138                 return part->master->write (part->master, to + part->offset, 
139                                         len, retlen, buf);
140         else
141                 return part->master->write_ecc (part->master, to + part->offset, 
142                                         len, retlen, buf, NULL, &mtd->oobinfo);
143                                                         
144 }
145
146 static int part_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
147                         size_t *retlen, const u_char *buf,
148                          u_char *eccbuf, struct nand_oobinfo *oobsel)
149 {
150         struct mtd_part *part = PART(mtd);
151         if (!(mtd->flags & MTD_WRITEABLE))
152                 return -EROFS;
153         if (oobsel == NULL)
154                 oobsel = &mtd->oobinfo;
155         if (to >= mtd->size)
156                 len = 0;
157         else if (to + len > mtd->size)
158                 len = mtd->size - to;
159         return part->master->write_ecc (part->master, to + part->offset, 
160                                         len, retlen, buf, eccbuf, oobsel);
161 }
162
163 static int part_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
164                         size_t *retlen, const u_char *buf)
165 {
166         struct mtd_part *part = PART(mtd);
167         if (!(mtd->flags & MTD_WRITEABLE))
168                 return -EROFS;
169         if (to >= mtd->size)
170                 len = 0;
171         else if (to + len > mtd->size)
172                 len = mtd->size - to;
173         return part->master->write_oob (part->master, to + part->offset, 
174                                         len, retlen, buf);
175 }
176
177 static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, 
178                         size_t *retlen, u_char *buf)
179 {
180         struct mtd_part *part = PART(mtd);
181         return part->master->write_user_prot_reg (part->master, from, 
182                                         len, retlen, buf);
183 }
184
185 static int part_writev (struct mtd_info *mtd,  const struct kvec *vecs,
186                          unsigned long count, loff_t to, size_t *retlen)
187 {
188         struct mtd_part *part = PART(mtd);
189         if (!(mtd->flags & MTD_WRITEABLE))
190                 return -EROFS;
191         if (part->master->writev_ecc == NULL)   
192                 return part->master->writev (part->master, vecs, count,
193                                         to + part->offset, retlen);
194         else
195                 return part->master->writev_ecc (part->master, vecs, count,
196                                         to + part->offset, retlen,
197                                         NULL, &mtd->oobinfo);
198 }
199
200 static int part_readv (struct mtd_info *mtd,  struct kvec *vecs,
201                          unsigned long count, loff_t from, size_t *retlen)
202 {
203         struct mtd_part *part = PART(mtd);
204         if (part->master->readv_ecc == NULL)    
205                 return part->master->readv (part->master, vecs, count,
206                                         from + part->offset, retlen);
207         else
208                 return part->master->readv_ecc (part->master, vecs, count,
209                                         from + part->offset, retlen, 
210                                         NULL, &mtd->oobinfo);
211 }
212
213 static int part_writev_ecc (struct mtd_info *mtd,  const struct kvec *vecs,
214                          unsigned long count, loff_t to, size_t *retlen,
215                          u_char *eccbuf,  struct nand_oobinfo *oobsel)
216 {
217         struct mtd_part *part = PART(mtd);
218         if (!(mtd->flags & MTD_WRITEABLE))
219                 return -EROFS;
220         if (oobsel == NULL)
221                 oobsel = &mtd->oobinfo;
222         return part->master->writev_ecc (part->master, vecs, count,
223                                         to + part->offset, retlen,
224                                         eccbuf, oobsel);
225 }
226
227 static int part_readv_ecc (struct mtd_info *mtd,  struct kvec *vecs,
228                          unsigned long count, loff_t from, size_t *retlen,
229                          u_char *eccbuf,  struct nand_oobinfo *oobsel)
230 {
231         struct mtd_part *part = PART(mtd);
232         if (oobsel == NULL)
233                 oobsel = &mtd->oobinfo;
234         return part->master->readv_ecc (part->master, vecs, count,
235                                         from + part->offset, retlen, 
236                                         eccbuf, oobsel);
237 }
238
239 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
240 {
241         struct mtd_part *part = PART(mtd);
242         int ret;
243         if (!(mtd->flags & MTD_WRITEABLE))
244                 return -EROFS;
245         if (instr->addr >= mtd->size)
246                 return -EINVAL;
247         instr->addr += part->offset;
248         ret = part->master->erase(part->master, instr);
249         return ret;
250 }
251
252 void mtd_erase_callback(struct erase_info *instr)
253 {
254         if (instr->mtd->erase == part_erase) {
255                 struct mtd_part *part = PART(instr->mtd);
256
257                 if (instr->fail_addr != 0xffffffff)
258                         instr->fail_addr -= part->offset;
259                 instr->addr -= part->offset;
260         }
261         if (instr->callback)
262                 instr->callback(instr);
263 }
264 EXPORT_SYMBOL_GPL(mtd_erase_callback);
265
266 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
267 {
268         struct mtd_part *part = PART(mtd);
269         if ((len + ofs) > mtd->size) 
270                 return -EINVAL;
271         return part->master->lock(part->master, ofs + part->offset, len);
272 }
273
274 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
275 {
276         struct mtd_part *part = PART(mtd);
277         if ((len + ofs) > mtd->size) 
278                 return -EINVAL;
279         return part->master->unlock(part->master, ofs + part->offset, len);
280 }
281
282 static void part_sync(struct mtd_info *mtd)
283 {
284         struct mtd_part *part = PART(mtd);
285         part->master->sync(part->master);
286 }
287
288 static int part_suspend(struct mtd_info *mtd)
289 {
290         struct mtd_part *part = PART(mtd);
291         return part->master->suspend(part->master);
292 }
293
294 static void part_resume(struct mtd_info *mtd)
295 {
296         struct mtd_part *part = PART(mtd);
297         part->master->resume(part->master);
298 }
299
300 static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
301 {
302         struct mtd_part *part = PART(mtd);
303         if (ofs >= mtd->size)
304                 return -EINVAL;
305         ofs += part->offset;
306         return part->master->block_isbad(part->master, ofs);
307 }
308
309 static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
310 {
311         struct mtd_part *part = PART(mtd);
312         if (!(mtd->flags & MTD_WRITEABLE))
313                 return -EROFS;
314         if (ofs >= mtd->size)
315                 return -EINVAL;
316         ofs += part->offset;
317         return part->master->block_markbad(part->master, ofs);
318 }
319
320 /* 
321  * This function unregisters and destroy all slave MTD objects which are 
322  * attached to the given master MTD object.
323  */
324
325 int del_mtd_partitions(struct mtd_info *master)
326 {
327         struct list_head *node;
328         struct mtd_part *slave;
329
330         for (node = mtd_partitions.next;
331              node != &mtd_partitions;
332              node = node->next) {
333                 slave = list_entry(node, struct mtd_part, list);
334                 if (slave->master == master) {
335                         struct list_head *prev = node->prev;
336                         __list_del(prev, node->next);
337                         if(slave->registered)
338                                 del_mtd_device(&slave->mtd);
339                         kfree(slave);
340                         node = prev;
341                 }
342         }
343
344         return 0;
345 }
346
347 /*
348  * This function, given a master MTD object and a partition table, creates
349  * and registers slave MTD objects which are bound to the master according to
350  * the partition definitions.
351  * (Q: should we register the master MTD object as well?)
352  */
353
354 int add_mtd_partitions(struct mtd_info *master, 
355                        const struct mtd_partition *parts,
356                        int nbparts)
357 {
358         struct mtd_part *slave;
359         u_int32_t cur_offset = 0;
360         int i;
361
362         printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
363
364         for (i = 0; i < nbparts; i++) {
365
366                 /* allocate the partition structure */
367                 slave = kmalloc (sizeof(*slave), GFP_KERNEL);
368                 if (!slave) {
369                         printk ("memory allocation error while creating partitions for \"%s\"\n",
370                                 master->name);
371                         del_mtd_partitions(master);
372                         return -ENOMEM;
373                 }
374                 memset(slave, 0, sizeof(*slave));
375                 list_add(&slave->list, &mtd_partitions);
376
377                 /* set up the MTD object for this partition */
378                 slave->mtd.type = master->type;
379                 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
380                 slave->mtd.size = parts[i].size;
381                 slave->mtd.oobblock = master->oobblock;
382                 slave->mtd.oobsize = master->oobsize;
383                 slave->mtd.ecctype = master->ecctype;
384                 slave->mtd.eccsize = master->eccsize;
385
386                 slave->mtd.name = parts[i].name;
387                 slave->mtd.bank_size = master->bank_size;
388                 slave->mtd.owner = master->owner;
389
390                 slave->mtd.read = part_read;
391                 slave->mtd.write = part_write;
392
393                 if(master->point && master->unpoint){
394                         slave->mtd.point = part_point;
395                         slave->mtd.unpoint = part_unpoint;
396                 }
397                 
398                 if (master->read_ecc)
399                         slave->mtd.read_ecc = part_read_ecc;
400                 if (master->write_ecc)
401                         slave->mtd.write_ecc = part_write_ecc;
402                 if (master->read_oob)
403                         slave->mtd.read_oob = part_read_oob;
404                 if (master->write_oob)
405                         slave->mtd.write_oob = part_write_oob;
406                 if(master->read_user_prot_reg)
407                         slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
408                 if(master->read_fact_prot_reg)
409                         slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
410                 if(master->write_user_prot_reg)
411                         slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
412                 if (master->sync)
413                         slave->mtd.sync = part_sync;
414                 if (!i && master->suspend && master->resume) {
415                                 slave->mtd.suspend = part_suspend;
416                                 slave->mtd.resume = part_resume;
417                 }
418                 if (master->writev)
419                         slave->mtd.writev = part_writev;
420                 if (master->readv)
421                         slave->mtd.readv = part_readv;
422                 if (master->writev_ecc)
423                         slave->mtd.writev_ecc = part_writev_ecc;
424                 if (master->readv_ecc)
425                         slave->mtd.readv_ecc = part_readv_ecc;
426                 if (master->lock)
427                         slave->mtd.lock = part_lock;
428                 if (master->unlock)
429                         slave->mtd.unlock = part_unlock;
430                 if (master->block_isbad)
431                         slave->mtd.block_isbad = part_block_isbad;
432                 if (master->block_markbad)
433                         slave->mtd.block_markbad = part_block_markbad;
434                 slave->mtd.erase = part_erase;
435                 slave->master = master;
436                 slave->offset = parts[i].offset;
437                 slave->index = i;
438
439                 if (slave->offset == MTDPART_OFS_APPEND)
440                         slave->offset = cur_offset;
441                 if (slave->offset == MTDPART_OFS_NXTBLK) {
442                         u_int32_t emask = master->erasesize-1;
443                         slave->offset = (cur_offset + emask) & ~emask;
444                         if (slave->offset != cur_offset) {
445                                 printk(KERN_NOTICE "Moving partition %d: "
446                                        "0x%08x -> 0x%08x\n", i,
447                                        cur_offset, slave->offset);
448                         }
449                 }
450                 if (slave->mtd.size == MTDPART_SIZ_FULL)
451                         slave->mtd.size = master->size - slave->offset;
452                 cur_offset = slave->offset + slave->mtd.size;
453         
454                 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset, 
455                         slave->offset + slave->mtd.size, slave->mtd.name);
456
457                 /* let's do some sanity checks */
458                 if (slave->offset >= master->size) {
459                                 /* let's register it anyway to preserve ordering */
460                         slave->offset = 0;
461                         slave->mtd.size = 0;
462                         printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
463                                 parts[i].name);
464                 }
465                 if (slave->offset + slave->mtd.size > master->size) {
466                         slave->mtd.size = master->size - slave->offset;
467                         printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
468                                 parts[i].name, master->name, slave->mtd.size);
469                 }
470                 if (master->numeraseregions>1) {
471                         /* Deal with variable erase size stuff */
472                         int i;
473                         struct mtd_erase_region_info *regions = master->eraseregions;
474                         
475                         /* Find the first erase regions which is part of this partition. */
476                         for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
477                                 ;
478
479                         for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
480                                 if (slave->mtd.erasesize < regions[i].erasesize) {
481                                         slave->mtd.erasesize = regions[i].erasesize;
482                                 }
483                         }
484                 } else {
485                         /* Single erase size */
486                         slave->mtd.erasesize = master->erasesize;
487                 }
488
489                 if ((slave->mtd.flags & MTD_WRITEABLE) && 
490                     (slave->offset % slave->mtd.erasesize)) {
491                         /* Doesn't start on a boundary of major erase size */
492                         /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
493                         slave->mtd.flags &= ~MTD_WRITEABLE;
494                         printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
495                                 parts[i].name);
496                 }
497                 if ((slave->mtd.flags & MTD_WRITEABLE) && 
498                     (slave->mtd.size % slave->mtd.erasesize)) {
499                         slave->mtd.flags &= ~MTD_WRITEABLE;
500                         printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
501                                 parts[i].name);
502                 }
503
504                 /* copy oobinfo from master */ 
505                 memcpy(&slave->mtd.oobinfo, &master->oobinfo, sizeof(slave->mtd.oobinfo));
506
507                 if(parts[i].mtdp)
508                 {       /* store the object pointer (caller may or may not register it */
509                         *parts[i].mtdp = &slave->mtd;
510                         slave->registered = 0;
511                 }
512                 else
513                 {
514                         /* register our partition */
515                         add_mtd_device(&slave->mtd);
516                         slave->registered = 1;
517                 }
518         }
519
520         return 0;
521 }
522
523 EXPORT_SYMBOL(add_mtd_partitions);
524 EXPORT_SYMBOL(del_mtd_partitions);
525
526 static DEFINE_SPINLOCK(part_parser_lock);
527 static LIST_HEAD(part_parsers);
528
529 static struct mtd_part_parser *get_partition_parser(const char *name)
530 {
531         struct list_head *this;
532         void *ret = NULL;
533         spin_lock(&part_parser_lock);
534
535         list_for_each(this, &part_parsers) {
536                 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
537
538                 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
539                         ret = p;
540                         break;
541                 }
542         }
543         spin_unlock(&part_parser_lock);
544
545         return ret;
546 }
547
548 int register_mtd_parser(struct mtd_part_parser *p)
549 {
550         spin_lock(&part_parser_lock);
551         list_add(&p->list, &part_parsers);
552         spin_unlock(&part_parser_lock);
553
554         return 0;
555 }
556
557 int deregister_mtd_parser(struct mtd_part_parser *p)
558 {
559         spin_lock(&part_parser_lock);
560         list_del(&p->list);
561         spin_unlock(&part_parser_lock);
562         return 0;
563 }
564
565 int parse_mtd_partitions(struct mtd_info *master, const char **types, 
566                          struct mtd_partition **pparts, unsigned long origin)
567 {
568         struct mtd_part_parser *parser;
569         int ret = 0;
570                 
571         for ( ; ret <= 0 && *types; types++) {
572                 parser = get_partition_parser(*types);
573 #ifdef CONFIG_KMOD
574                 if (!parser && !request_module("%s", *types))
575                                 parser = get_partition_parser(*types);
576 #endif
577                 if (!parser) {
578                         printk(KERN_NOTICE "%s partition parsing not available\n",
579                                *types);
580                         continue;
581                 }
582                 ret = (*parser->parse_fn)(master, pparts, origin);
583                 if (ret > 0) {
584                         printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", 
585                                ret, parser->name, master->name);
586                 }
587                 put_partition_parser(parser);
588         }
589         return ret;
590 }
591
592 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
593 EXPORT_SYMBOL_GPL(register_mtd_parser);
594 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
595
596 MODULE_LICENSE("GPL");
597 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
598 MODULE_DESCRIPTION("Generic support for partitioning of MTD devices");
599