V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / mtd / mtdpart.c
blob6174a97d790274f5aa1245dc53171c324482ebb9
1 /*
2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
6 * This code is GPL
8 * $Id: mtdpart.c,v 1.55 2005/11/07 11:14:20 gleixner Exp $
10 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
11 * added support for read_oob, write_oob
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>
24 /* Our partition linked list */
25 static LIST_HEAD(mtd_partitions);
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;
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.
41 #define PART(x) ((struct mtd_part *)(x))
45 * MTD methods which simply translate the effective address and pass through
46 * to the _real_ device.
49 static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
50 size_t *retlen, u_char *buf)
52 struct mtd_part *part = PART(mtd);
53 int res;
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++;
67 return res;
70 static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
71 size_t *retlen, u_char **buf)
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);
82 static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
84 struct mtd_part *part = PART(mtd);
86 part->master->unpoint (part->master, addr, from + part->offset, len);
89 static int part_read_oob(struct mtd_info *mtd, loff_t from,
90 struct mtd_oob_ops *ops)
92 struct mtd_part *part = PART(mtd);
93 int res;
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);
101 if (unlikely(res)) {
102 if (res == -EUCLEAN)
103 mtd->ecc_stats.corrected++;
104 if (res == -EBADMSG)
105 mtd->ecc_stats.failed++;
107 return res;
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)
113 struct mtd_part *part = PART(mtd);
114 return part->master->read_user_prot_reg (part->master, from,
115 len, retlen, buf);
118 static int part_get_user_prot_info (struct mtd_info *mtd,
119 struct otp_info *buf, size_t len)
121 struct mtd_part *part = PART(mtd);
122 return part->master->get_user_prot_info (part->master, buf, len);
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)
128 struct mtd_part *part = PART(mtd);
129 return part->master->read_fact_prot_reg (part->master, from,
130 len, retlen, buf);
133 static int part_get_fact_prot_info (struct mtd_info *mtd,
134 struct otp_info *buf, size_t len)
136 struct mtd_part *part = PART(mtd);
137 return part->master->get_fact_prot_info (part->master, buf, len);
140 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
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);
154 static int part_write_oob(struct mtd_info *mtd, loff_t to,
155 struct mtd_oob_ops *ops)
157 struct mtd_part *part = PART(mtd);
159 if (!(mtd->flags & MTD_WRITEABLE))
160 return -EROFS;
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);
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)
172 struct mtd_part *part = PART(mtd);
173 return part->master->write_user_prot_reg (part->master, from,
174 len, retlen, buf);
177 static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
179 struct mtd_part *part = PART(mtd);
180 return part->master->lock_user_prot_reg (part->master, from, len);
183 static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
184 unsigned long count, loff_t to, size_t *retlen)
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);
193 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
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 if (ret) {
204 if (instr->fail_addr != 0xffffffff)
205 instr->fail_addr -= part->offset;
206 instr->addr -= part->offset;
208 return ret;
211 void mtd_erase_callback(struct erase_info *instr)
213 if (instr->mtd->erase == part_erase) {
214 struct mtd_part *part = PART(instr->mtd);
216 if (instr->fail_addr != 0xffffffff)
217 instr->fail_addr -= part->offset;
218 instr->addr -= part->offset;
220 if (instr->callback)
221 instr->callback(instr);
223 EXPORT_SYMBOL_GPL(mtd_erase_callback);
225 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
227 struct mtd_part *part = PART(mtd);
228 if ((len + ofs) > mtd->size)
229 return -EINVAL;
230 return part->master->lock(part->master, ofs + part->offset, len);
233 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
235 struct mtd_part *part = PART(mtd);
236 if ((len + ofs) > mtd->size)
237 return -EINVAL;
238 return part->master->unlock(part->master, ofs + part->offset, len);
241 static void part_sync(struct mtd_info *mtd)
243 struct mtd_part *part = PART(mtd);
244 part->master->sync(part->master);
247 static int part_suspend(struct mtd_info *mtd)
249 struct mtd_part *part = PART(mtd);
250 return part->master->suspend(part->master);
253 static void part_resume(struct mtd_info *mtd)
255 struct mtd_part *part = PART(mtd);
256 part->master->resume(part->master);
259 static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
261 struct mtd_part *part = PART(mtd);
262 if (ofs >= mtd->size)
263 return -EINVAL;
264 ofs += part->offset;
265 return part->master->block_isbad(part->master, ofs);
268 static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
270 struct mtd_part *part = PART(mtd);
271 int res;
273 if (!(mtd->flags & MTD_WRITEABLE))
274 return -EROFS;
275 if (ofs >= mtd->size)
276 return -EINVAL;
277 ofs += part->offset;
278 res = part->master->block_markbad(part->master, ofs);
279 if (!res)
280 mtd->ecc_stats.badblocks++;
281 return res;
285 * This function unregisters and destroy all slave MTD objects which are
286 * attached to the given master MTD object.
289 int del_mtd_partitions(struct mtd_info *master)
291 struct list_head *node;
292 struct mtd_part *slave;
294 for (node = mtd_partitions.next;
295 node != &mtd_partitions;
296 node = node->next) {
297 slave = list_entry(node, struct mtd_part, list);
298 if (slave->master == master) {
299 struct list_head *prev = node->prev;
300 __list_del(prev, node->next);
301 if(slave->registered)
302 del_mtd_device(&slave->mtd);
303 kfree(slave);
304 node = prev;
308 return 0;
312 * This function, given a master MTD object and a partition table, creates
313 * and registers slave MTD objects which are bound to the master according to
314 * the partition definitions.
315 * (Q: should we register the master MTD object as well?)
318 int add_mtd_partitions(struct mtd_info *master,
319 const struct mtd_partition *parts,
320 int nbparts)
322 struct mtd_part *slave;
323 u_int32_t cur_offset = 0;
324 int i;
326 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
328 for (i = 0; i < nbparts; i++) {
330 /* allocate the partition structure */
331 slave = kzalloc (sizeof(*slave), GFP_KERNEL);
332 if (!slave) {
333 printk ("memory allocation error while creating partitions for \"%s\"\n",
334 master->name);
335 del_mtd_partitions(master);
336 return -ENOMEM;
338 list_add(&slave->list, &mtd_partitions);
340 /* set up the MTD object for this partition */
341 slave->mtd.type = master->type;
342 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
343 slave->mtd.size = parts[i].size;
344 slave->mtd.writesize = master->writesize;
345 slave->mtd.oobsize = master->oobsize;
346 slave->mtd.oobavail = master->oobavail;
347 slave->mtd.subpage_sft = master->subpage_sft;
349 slave->mtd.name = parts[i].name;
350 slave->mtd.owner = master->owner;
352 slave->mtd.read = part_read;
353 slave->mtd.write = part_write;
355 if(master->point && master->unpoint){
356 slave->mtd.point = part_point;
357 slave->mtd.unpoint = part_unpoint;
360 if (master->read_oob)
361 slave->mtd.read_oob = part_read_oob;
362 if (master->write_oob)
363 slave->mtd.write_oob = part_write_oob;
364 if(master->read_user_prot_reg)
365 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
366 if(master->read_fact_prot_reg)
367 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
368 if(master->write_user_prot_reg)
369 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
370 if(master->lock_user_prot_reg)
371 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
372 if(master->get_user_prot_info)
373 slave->mtd.get_user_prot_info = part_get_user_prot_info;
374 if(master->get_fact_prot_info)
375 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
376 if (master->sync)
377 slave->mtd.sync = part_sync;
378 if (!i && master->suspend && master->resume) {
379 slave->mtd.suspend = part_suspend;
380 slave->mtd.resume = part_resume;
382 if (master->writev)
383 slave->mtd.writev = part_writev;
384 if (master->lock)
385 slave->mtd.lock = part_lock;
386 if (master->unlock)
387 slave->mtd.unlock = part_unlock;
388 if (master->block_isbad)
389 slave->mtd.block_isbad = part_block_isbad;
390 if (master->block_markbad)
391 slave->mtd.block_markbad = part_block_markbad;
392 slave->mtd.erase = part_erase;
393 slave->master = master;
394 slave->offset = parts[i].offset;
395 slave->index = i;
397 if (slave->offset == MTDPART_OFS_APPEND)
398 slave->offset = cur_offset;
399 if (slave->offset == MTDPART_OFS_NXTBLK) {
400 slave->offset = cur_offset;
401 if ((cur_offset % master->erasesize) != 0) {
402 /* Round up to next erasesize */
403 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
404 printk(KERN_NOTICE "Moving partition %d: "
405 "0x%08x -> 0x%08x\n", i,
406 cur_offset, slave->offset);
409 if (slave->mtd.size == MTDPART_SIZ_FULL)
410 slave->mtd.size = master->size - slave->offset;
411 cur_offset = slave->offset + slave->mtd.size;
413 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
414 slave->offset + slave->mtd.size, slave->mtd.name);
416 /* let's do some sanity checks */
417 if (slave->offset >= master->size) {
418 /* let's register it anyway to preserve ordering */
419 slave->offset = 0;
420 slave->mtd.size = 0;
421 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
422 parts[i].name);
424 if (slave->offset + slave->mtd.size > master->size) {
425 slave->mtd.size = master->size - slave->offset;
426 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
427 parts[i].name, master->name, slave->mtd.size);
429 if (master->numeraseregions>1) {
430 /* Deal with variable erase size stuff */
431 int i;
432 struct mtd_erase_region_info *regions = master->eraseregions;
434 /* Find the first erase regions which is part of this partition. */
435 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
438 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
439 if (slave->mtd.erasesize < regions[i].erasesize) {
440 slave->mtd.erasesize = regions[i].erasesize;
443 } else {
444 /* Single erase size */
445 slave->mtd.erasesize = master->erasesize;
448 if ((slave->mtd.flags & MTD_WRITEABLE) &&
449 (slave->offset % slave->mtd.erasesize)) {
450 /* Doesn't start on a boundary of major erase size */
451 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
452 slave->mtd.flags &= ~MTD_WRITEABLE;
453 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
454 parts[i].name);
456 if ((slave->mtd.flags & MTD_WRITEABLE) &&
457 (slave->mtd.size % slave->mtd.erasesize)) {
458 slave->mtd.flags &= ~MTD_WRITEABLE;
459 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
460 parts[i].name);
463 slave->mtd.ecclayout = master->ecclayout;
464 if (master->block_isbad) {
465 uint32_t offs = 0;
467 while(offs < slave->mtd.size) {
468 if (master->block_isbad(master,
469 offs + slave->offset))
470 slave->mtd.ecc_stats.badblocks++;
471 offs += slave->mtd.erasesize;
475 if(parts[i].mtdp)
476 { /* store the object pointer (caller may or may not register it */
477 *parts[i].mtdp = &slave->mtd;
478 slave->registered = 0;
480 else
482 /* register our partition */
483 add_mtd_device(&slave->mtd);
484 slave->registered = 1;
488 return 0;
491 EXPORT_SYMBOL(add_mtd_partitions);
492 EXPORT_SYMBOL(del_mtd_partitions);
494 static DEFINE_SPINLOCK(part_parser_lock);
495 static LIST_HEAD(part_parsers);
497 static struct mtd_part_parser *get_partition_parser(const char *name)
499 struct list_head *this;
500 void *ret = NULL;
501 spin_lock(&part_parser_lock);
503 list_for_each(this, &part_parsers) {
504 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
506 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
507 ret = p;
508 break;
511 spin_unlock(&part_parser_lock);
513 return ret;
516 int register_mtd_parser(struct mtd_part_parser *p)
518 spin_lock(&part_parser_lock);
519 list_add(&p->list, &part_parsers);
520 spin_unlock(&part_parser_lock);
522 return 0;
525 int deregister_mtd_parser(struct mtd_part_parser *p)
527 spin_lock(&part_parser_lock);
528 list_del(&p->list);
529 spin_unlock(&part_parser_lock);
530 return 0;
533 int parse_mtd_partitions(struct mtd_info *master, const char **types,
534 struct mtd_partition **pparts, unsigned long origin)
536 struct mtd_part_parser *parser;
537 int ret = 0;
539 for ( ; ret <= 0 && *types; types++) {
540 parser = get_partition_parser(*types);
541 #ifdef CONFIG_KMOD
542 if (!parser && !request_module("%s", *types))
543 parser = get_partition_parser(*types);
544 #endif
545 if (!parser) {
546 printk(KERN_NOTICE "%s partition parsing not available\n",
547 *types);
548 continue;
550 ret = (*parser->parse_fn)(master, pparts, origin);
551 if (ret > 0) {
552 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
553 ret, parser->name, master->name);
555 put_partition_parser(parser);
557 return ret;
560 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
561 EXPORT_SYMBOL_GPL(register_mtd_parser);
562 EXPORT_SYMBOL_GPL(deregister_mtd_parser);