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