mtd: introduce mtd_read_user_prot_reg interface
[deliverable/linux.git] / drivers / mtd / mtdpart.c
1 /*
2 * Simple MTD partitioning layer
3 *
4 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/kmod.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/err.h>
33
34 #include "mtdcore.h"
35
36 /* Our partition linked list */
37 static LIST_HEAD(mtd_partitions);
38 static DEFINE_MUTEX(mtd_partitions_mutex);
39
40 /* Our partition node structure */
41 struct mtd_part {
42 struct mtd_info mtd;
43 struct mtd_info *master;
44 uint64_t offset;
45 struct list_head list;
46 };
47
48 /*
49 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50 * the pointer to that structure with this macro.
51 */
52 #define PART(x) ((struct mtd_part *)(x))
53
54
55 /*
56 * MTD methods which simply translate the effective address and pass through
57 * to the _real_ device.
58 */
59
60 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
61 size_t *retlen, u_char *buf)
62 {
63 struct mtd_part *part = PART(mtd);
64 struct mtd_ecc_stats stats;
65 int res;
66
67 stats = part->master->ecc_stats;
68
69 if (from >= mtd->size)
70 len = 0;
71 else if (from + len > mtd->size)
72 len = mtd->size - from;
73 res = mtd_read(part->master, from + part->offset, len, retlen, buf);
74 if (unlikely(res)) {
75 if (mtd_is_bitflip(res))
76 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
77 if (mtd_is_eccerr(res))
78 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
79 }
80 return res;
81 }
82
83 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
84 size_t *retlen, void **virt, resource_size_t *phys)
85 {
86 struct mtd_part *part = PART(mtd);
87 if (from >= mtd->size)
88 len = 0;
89 else if (from + len > mtd->size)
90 len = mtd->size - from;
91 return mtd_point(part->master, from + part->offset, len, retlen,
92 virt, phys);
93 }
94
95 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
96 {
97 struct mtd_part *part = PART(mtd);
98
99 mtd_unpoint(part->master, from + part->offset, len);
100 }
101
102 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
103 unsigned long len,
104 unsigned long offset,
105 unsigned long flags)
106 {
107 struct mtd_part *part = PART(mtd);
108
109 offset += part->offset;
110 return mtd_get_unmapped_area(part->master, len, offset, flags);
111 }
112
113 static int part_read_oob(struct mtd_info *mtd, loff_t from,
114 struct mtd_oob_ops *ops)
115 {
116 struct mtd_part *part = PART(mtd);
117 int res;
118
119 if (from >= mtd->size)
120 return -EINVAL;
121 if (ops->datbuf && from + ops->len > mtd->size)
122 return -EINVAL;
123
124 /*
125 * If OOB is also requested, make sure that we do not read past the end
126 * of this partition.
127 */
128 if (ops->oobbuf) {
129 size_t len, pages;
130
131 if (ops->mode == MTD_OPS_AUTO_OOB)
132 len = mtd->oobavail;
133 else
134 len = mtd->oobsize;
135 pages = mtd_div_by_ws(mtd->size, mtd);
136 pages -= mtd_div_by_ws(from, mtd);
137 if (ops->ooboffs + ops->ooblen > pages * len)
138 return -EINVAL;
139 }
140
141 res = mtd_read_oob(part->master, from + part->offset, ops);
142 if (unlikely(res)) {
143 if (mtd_is_bitflip(res))
144 mtd->ecc_stats.corrected++;
145 if (mtd_is_eccerr(res))
146 mtd->ecc_stats.failed++;
147 }
148 return res;
149 }
150
151 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
152 size_t len, size_t *retlen, u_char *buf)
153 {
154 struct mtd_part *part = PART(mtd);
155 return mtd_read_user_prot_reg(part->master, from, len, retlen, buf);
156 }
157
158 static int part_get_user_prot_info(struct mtd_info *mtd,
159 struct otp_info *buf, size_t len)
160 {
161 struct mtd_part *part = PART(mtd);
162 return mtd_get_user_prot_info(part->master, buf, len);
163 }
164
165 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
166 size_t len, size_t *retlen, u_char *buf)
167 {
168 struct mtd_part *part = PART(mtd);
169 return mtd_read_fact_prot_reg(part->master, from, len, retlen, buf);
170 }
171
172 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
173 size_t len)
174 {
175 struct mtd_part *part = PART(mtd);
176 return mtd_get_fact_prot_info(part->master, buf, len);
177 }
178
179 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
180 size_t *retlen, const u_char *buf)
181 {
182 struct mtd_part *part = PART(mtd);
183 if (!(mtd->flags & MTD_WRITEABLE))
184 return -EROFS;
185 if (to >= mtd->size)
186 len = 0;
187 else if (to + len > mtd->size)
188 len = mtd->size - to;
189 return mtd_write(part->master, to + part->offset, len, retlen, buf);
190 }
191
192 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
193 size_t *retlen, const u_char *buf)
194 {
195 struct mtd_part *part = PART(mtd);
196 if (!(mtd->flags & MTD_WRITEABLE))
197 return -EROFS;
198 if (to >= mtd->size)
199 len = 0;
200 else if (to + len > mtd->size)
201 len = mtd->size - to;
202 return mtd_panic_write(part->master, to + part->offset, len, retlen,
203 buf);
204 }
205
206 static int part_write_oob(struct mtd_info *mtd, loff_t to,
207 struct mtd_oob_ops *ops)
208 {
209 struct mtd_part *part = PART(mtd);
210
211 if (!(mtd->flags & MTD_WRITEABLE))
212 return -EROFS;
213
214 if (to >= mtd->size)
215 return -EINVAL;
216 if (ops->datbuf && to + ops->len > mtd->size)
217 return -EINVAL;
218 return mtd_write_oob(part->master, to + part->offset, ops);
219 }
220
221 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
222 size_t len, size_t *retlen, u_char *buf)
223 {
224 struct mtd_part *part = PART(mtd);
225 return part->master->write_user_prot_reg(part->master, from,
226 len, retlen, buf);
227 }
228
229 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
230 size_t len)
231 {
232 struct mtd_part *part = PART(mtd);
233 return part->master->lock_user_prot_reg(part->master, from, len);
234 }
235
236 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
237 unsigned long count, loff_t to, size_t *retlen)
238 {
239 struct mtd_part *part = PART(mtd);
240 if (!(mtd->flags & MTD_WRITEABLE))
241 return -EROFS;
242 return part->master->writev(part->master, vecs, count,
243 to + part->offset, retlen);
244 }
245
246 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
247 {
248 struct mtd_part *part = PART(mtd);
249 int ret;
250 if (!(mtd->flags & MTD_WRITEABLE))
251 return -EROFS;
252 if (instr->addr >= mtd->size)
253 return -EINVAL;
254 instr->addr += part->offset;
255 ret = mtd_erase(part->master, instr);
256 if (ret) {
257 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
258 instr->fail_addr -= part->offset;
259 instr->addr -= part->offset;
260 }
261 return ret;
262 }
263
264 void mtd_erase_callback(struct erase_info *instr)
265 {
266 if (instr->mtd->erase == part_erase) {
267 struct mtd_part *part = PART(instr->mtd);
268
269 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
270 instr->fail_addr -= part->offset;
271 instr->addr -= part->offset;
272 }
273 if (instr->callback)
274 instr->callback(instr);
275 }
276 EXPORT_SYMBOL_GPL(mtd_erase_callback);
277
278 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
279 {
280 struct mtd_part *part = PART(mtd);
281 if ((len + ofs) > mtd->size)
282 return -EINVAL;
283 return part->master->lock(part->master, ofs + part->offset, len);
284 }
285
286 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
287 {
288 struct mtd_part *part = PART(mtd);
289 if ((len + ofs) > mtd->size)
290 return -EINVAL;
291 return part->master->unlock(part->master, ofs + part->offset, len);
292 }
293
294 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
295 {
296 struct mtd_part *part = PART(mtd);
297 if ((len + ofs) > mtd->size)
298 return -EINVAL;
299 return part->master->is_locked(part->master, ofs + part->offset, len);
300 }
301
302 static void part_sync(struct mtd_info *mtd)
303 {
304 struct mtd_part *part = PART(mtd);
305 part->master->sync(part->master);
306 }
307
308 static int part_suspend(struct mtd_info *mtd)
309 {
310 struct mtd_part *part = PART(mtd);
311 return part->master->suspend(part->master);
312 }
313
314 static void part_resume(struct mtd_info *mtd)
315 {
316 struct mtd_part *part = PART(mtd);
317 part->master->resume(part->master);
318 }
319
320 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
321 {
322 struct mtd_part *part = PART(mtd);
323 if (ofs >= mtd->size)
324 return -EINVAL;
325 ofs += part->offset;
326 return part->master->block_isbad(part->master, ofs);
327 }
328
329 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
330 {
331 struct mtd_part *part = PART(mtd);
332 int res;
333
334 if (!(mtd->flags & MTD_WRITEABLE))
335 return -EROFS;
336 if (ofs >= mtd->size)
337 return -EINVAL;
338 ofs += part->offset;
339 res = part->master->block_markbad(part->master, ofs);
340 if (!res)
341 mtd->ecc_stats.badblocks++;
342 return res;
343 }
344
345 static inline void free_partition(struct mtd_part *p)
346 {
347 kfree(p->mtd.name);
348 kfree(p);
349 }
350
351 /*
352 * This function unregisters and destroy all slave MTD objects which are
353 * attached to the given master MTD object.
354 */
355
356 int del_mtd_partitions(struct mtd_info *master)
357 {
358 struct mtd_part *slave, *next;
359 int ret, err = 0;
360
361 mutex_lock(&mtd_partitions_mutex);
362 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
363 if (slave->master == master) {
364 ret = del_mtd_device(&slave->mtd);
365 if (ret < 0) {
366 err = ret;
367 continue;
368 }
369 list_del(&slave->list);
370 free_partition(slave);
371 }
372 mutex_unlock(&mtd_partitions_mutex);
373
374 return err;
375 }
376
377 static struct mtd_part *allocate_partition(struct mtd_info *master,
378 const struct mtd_partition *part, int partno,
379 uint64_t cur_offset)
380 {
381 struct mtd_part *slave;
382 char *name;
383
384 /* allocate the partition structure */
385 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
386 name = kstrdup(part->name, GFP_KERNEL);
387 if (!name || !slave) {
388 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
389 master->name);
390 kfree(name);
391 kfree(slave);
392 return ERR_PTR(-ENOMEM);
393 }
394
395 /* set up the MTD object for this partition */
396 slave->mtd.type = master->type;
397 slave->mtd.flags = master->flags & ~part->mask_flags;
398 slave->mtd.size = part->size;
399 slave->mtd.writesize = master->writesize;
400 slave->mtd.writebufsize = master->writebufsize;
401 slave->mtd.oobsize = master->oobsize;
402 slave->mtd.oobavail = master->oobavail;
403 slave->mtd.subpage_sft = master->subpage_sft;
404
405 slave->mtd.name = name;
406 slave->mtd.owner = master->owner;
407 slave->mtd.backing_dev_info = master->backing_dev_info;
408
409 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
410 * to have the same data be in two different partitions.
411 */
412 slave->mtd.dev.parent = master->dev.parent;
413
414 slave->mtd.read = part_read;
415 slave->mtd.write = part_write;
416
417 if (master->panic_write)
418 slave->mtd.panic_write = part_panic_write;
419
420 if (master->point && master->unpoint) {
421 slave->mtd.point = part_point;
422 slave->mtd.unpoint = part_unpoint;
423 }
424
425 if (master->get_unmapped_area)
426 slave->mtd.get_unmapped_area = part_get_unmapped_area;
427 if (master->read_oob)
428 slave->mtd.read_oob = part_read_oob;
429 if (master->write_oob)
430 slave->mtd.write_oob = part_write_oob;
431 if (master->read_user_prot_reg)
432 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
433 if (master->read_fact_prot_reg)
434 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
435 if (master->write_user_prot_reg)
436 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
437 if (master->lock_user_prot_reg)
438 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
439 if (master->get_user_prot_info)
440 slave->mtd.get_user_prot_info = part_get_user_prot_info;
441 if (master->get_fact_prot_info)
442 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
443 if (master->sync)
444 slave->mtd.sync = part_sync;
445 if (!partno && !master->dev.class && master->suspend && master->resume) {
446 slave->mtd.suspend = part_suspend;
447 slave->mtd.resume = part_resume;
448 }
449 if (master->writev)
450 slave->mtd.writev = part_writev;
451 if (master->lock)
452 slave->mtd.lock = part_lock;
453 if (master->unlock)
454 slave->mtd.unlock = part_unlock;
455 if (master->is_locked)
456 slave->mtd.is_locked = part_is_locked;
457 if (master->block_isbad)
458 slave->mtd.block_isbad = part_block_isbad;
459 if (master->block_markbad)
460 slave->mtd.block_markbad = part_block_markbad;
461 slave->mtd.erase = part_erase;
462 slave->master = master;
463 slave->offset = part->offset;
464
465 if (slave->offset == MTDPART_OFS_APPEND)
466 slave->offset = cur_offset;
467 if (slave->offset == MTDPART_OFS_NXTBLK) {
468 slave->offset = cur_offset;
469 if (mtd_mod_by_eb(cur_offset, master) != 0) {
470 /* Round up to next erasesize */
471 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
472 printk(KERN_NOTICE "Moving partition %d: "
473 "0x%012llx -> 0x%012llx\n", partno,
474 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
475 }
476 }
477 if (slave->offset == MTDPART_OFS_RETAIN) {
478 slave->offset = cur_offset;
479 if (master->size - slave->offset >= slave->mtd.size) {
480 slave->mtd.size = master->size - slave->offset
481 - slave->mtd.size;
482 } else {
483 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
484 part->name, master->size - slave->offset,
485 slave->mtd.size);
486 /* register to preserve ordering */
487 goto out_register;
488 }
489 }
490 if (slave->mtd.size == MTDPART_SIZ_FULL)
491 slave->mtd.size = master->size - slave->offset;
492
493 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
494 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
495
496 /* let's do some sanity checks */
497 if (slave->offset >= master->size) {
498 /* let's register it anyway to preserve ordering */
499 slave->offset = 0;
500 slave->mtd.size = 0;
501 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
502 part->name);
503 goto out_register;
504 }
505 if (slave->offset + slave->mtd.size > master->size) {
506 slave->mtd.size = master->size - slave->offset;
507 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
508 part->name, master->name, (unsigned long long)slave->mtd.size);
509 }
510 if (master->numeraseregions > 1) {
511 /* Deal with variable erase size stuff */
512 int i, max = master->numeraseregions;
513 u64 end = slave->offset + slave->mtd.size;
514 struct mtd_erase_region_info *regions = master->eraseregions;
515
516 /* Find the first erase regions which is part of this
517 * partition. */
518 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
519 ;
520 /* The loop searched for the region _behind_ the first one */
521 if (i > 0)
522 i--;
523
524 /* Pick biggest erasesize */
525 for (; i < max && regions[i].offset < end; i++) {
526 if (slave->mtd.erasesize < regions[i].erasesize) {
527 slave->mtd.erasesize = regions[i].erasesize;
528 }
529 }
530 BUG_ON(slave->mtd.erasesize == 0);
531 } else {
532 /* Single erase size */
533 slave->mtd.erasesize = master->erasesize;
534 }
535
536 if ((slave->mtd.flags & MTD_WRITEABLE) &&
537 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
538 /* Doesn't start on a boundary of major erase size */
539 /* FIXME: Let it be writable if it is on a boundary of
540 * _minor_ erase size though */
541 slave->mtd.flags &= ~MTD_WRITEABLE;
542 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
543 part->name);
544 }
545 if ((slave->mtd.flags & MTD_WRITEABLE) &&
546 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
547 slave->mtd.flags &= ~MTD_WRITEABLE;
548 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
549 part->name);
550 }
551
552 slave->mtd.ecclayout = master->ecclayout;
553 if (master->block_isbad) {
554 uint64_t offs = 0;
555
556 while (offs < slave->mtd.size) {
557 if (master->block_isbad(master,
558 offs + slave->offset))
559 slave->mtd.ecc_stats.badblocks++;
560 offs += slave->mtd.erasesize;
561 }
562 }
563
564 out_register:
565 return slave;
566 }
567
568 int mtd_add_partition(struct mtd_info *master, char *name,
569 long long offset, long long length)
570 {
571 struct mtd_partition part;
572 struct mtd_part *p, *new;
573 uint64_t start, end;
574 int ret = 0;
575
576 /* the direct offset is expected */
577 if (offset == MTDPART_OFS_APPEND ||
578 offset == MTDPART_OFS_NXTBLK)
579 return -EINVAL;
580
581 if (length == MTDPART_SIZ_FULL)
582 length = master->size - offset;
583
584 if (length <= 0)
585 return -EINVAL;
586
587 part.name = name;
588 part.size = length;
589 part.offset = offset;
590 part.mask_flags = 0;
591 part.ecclayout = NULL;
592
593 new = allocate_partition(master, &part, -1, offset);
594 if (IS_ERR(new))
595 return PTR_ERR(new);
596
597 start = offset;
598 end = offset + length;
599
600 mutex_lock(&mtd_partitions_mutex);
601 list_for_each_entry(p, &mtd_partitions, list)
602 if (p->master == master) {
603 if ((start >= p->offset) &&
604 (start < (p->offset + p->mtd.size)))
605 goto err_inv;
606
607 if ((end >= p->offset) &&
608 (end < (p->offset + p->mtd.size)))
609 goto err_inv;
610 }
611
612 list_add(&new->list, &mtd_partitions);
613 mutex_unlock(&mtd_partitions_mutex);
614
615 add_mtd_device(&new->mtd);
616
617 return ret;
618 err_inv:
619 mutex_unlock(&mtd_partitions_mutex);
620 free_partition(new);
621 return -EINVAL;
622 }
623 EXPORT_SYMBOL_GPL(mtd_add_partition);
624
625 int mtd_del_partition(struct mtd_info *master, int partno)
626 {
627 struct mtd_part *slave, *next;
628 int ret = -EINVAL;
629
630 mutex_lock(&mtd_partitions_mutex);
631 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
632 if ((slave->master == master) &&
633 (slave->mtd.index == partno)) {
634 ret = del_mtd_device(&slave->mtd);
635 if (ret < 0)
636 break;
637
638 list_del(&slave->list);
639 free_partition(slave);
640 break;
641 }
642 mutex_unlock(&mtd_partitions_mutex);
643
644 return ret;
645 }
646 EXPORT_SYMBOL_GPL(mtd_del_partition);
647
648 /*
649 * This function, given a master MTD object and a partition table, creates
650 * and registers slave MTD objects which are bound to the master according to
651 * the partition definitions.
652 *
653 * We don't register the master, or expect the caller to have done so,
654 * for reasons of data integrity.
655 */
656
657 int add_mtd_partitions(struct mtd_info *master,
658 const struct mtd_partition *parts,
659 int nbparts)
660 {
661 struct mtd_part *slave;
662 uint64_t cur_offset = 0;
663 int i;
664
665 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
666
667 for (i = 0; i < nbparts; i++) {
668 slave = allocate_partition(master, parts + i, i, cur_offset);
669 if (IS_ERR(slave))
670 return PTR_ERR(slave);
671
672 mutex_lock(&mtd_partitions_mutex);
673 list_add(&slave->list, &mtd_partitions);
674 mutex_unlock(&mtd_partitions_mutex);
675
676 add_mtd_device(&slave->mtd);
677
678 cur_offset = slave->offset + slave->mtd.size;
679 }
680
681 return 0;
682 }
683
684 static DEFINE_SPINLOCK(part_parser_lock);
685 static LIST_HEAD(part_parsers);
686
687 static struct mtd_part_parser *get_partition_parser(const char *name)
688 {
689 struct mtd_part_parser *p, *ret = NULL;
690
691 spin_lock(&part_parser_lock);
692
693 list_for_each_entry(p, &part_parsers, list)
694 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
695 ret = p;
696 break;
697 }
698
699 spin_unlock(&part_parser_lock);
700
701 return ret;
702 }
703
704 #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
705
706 int register_mtd_parser(struct mtd_part_parser *p)
707 {
708 spin_lock(&part_parser_lock);
709 list_add(&p->list, &part_parsers);
710 spin_unlock(&part_parser_lock);
711
712 return 0;
713 }
714 EXPORT_SYMBOL_GPL(register_mtd_parser);
715
716 int deregister_mtd_parser(struct mtd_part_parser *p)
717 {
718 spin_lock(&part_parser_lock);
719 list_del(&p->list);
720 spin_unlock(&part_parser_lock);
721 return 0;
722 }
723 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
724
725 /*
726 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
727 * are changing this array!
728 */
729 static const char *default_mtd_part_types[] = {
730 "cmdlinepart",
731 "ofpart",
732 NULL
733 };
734
735 /**
736 * parse_mtd_partitions - parse MTD partitions
737 * @master: the master partition (describes whole MTD device)
738 * @types: names of partition parsers to try or %NULL
739 * @pparts: array of partitions found is returned here
740 * @data: MTD partition parser-specific data
741 *
742 * This function tries to find partition on MTD device @master. It uses MTD
743 * partition parsers, specified in @types. However, if @types is %NULL, then
744 * the default list of parsers is used. The default list contains only the
745 * "cmdlinepart" and "ofpart" parsers ATM.
746 *
747 * This function may return:
748 * o a negative error code in case of failure
749 * o zero if no partitions were found
750 * o a positive number of found partitions, in which case on exit @pparts will
751 * point to an array containing this number of &struct mtd_info objects.
752 */
753 int parse_mtd_partitions(struct mtd_info *master, const char **types,
754 struct mtd_partition **pparts,
755 struct mtd_part_parser_data *data)
756 {
757 struct mtd_part_parser *parser;
758 int ret = 0;
759
760 if (!types)
761 types = default_mtd_part_types;
762
763 for ( ; ret <= 0 && *types; types++) {
764 parser = get_partition_parser(*types);
765 if (!parser && !request_module("%s", *types))
766 parser = get_partition_parser(*types);
767 if (!parser)
768 continue;
769 ret = (*parser->parse_fn)(master, pparts, data);
770 if (ret > 0) {
771 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
772 ret, parser->name, master->name);
773 }
774 put_partition_parser(parser);
775 }
776 return ret;
777 }
778
779 int mtd_is_partition(struct mtd_info *mtd)
780 {
781 struct mtd_part *part;
782 int ispart = 0;
783
784 mutex_lock(&mtd_partitions_mutex);
785 list_for_each_entry(part, &mtd_partitions, list)
786 if (&part->mtd == mtd) {
787 ispart = 1;
788 break;
789 }
790 mutex_unlock(&mtd_partitions_mutex);
791
792 return ispart;
793 }
794 EXPORT_SYMBOL_GPL(mtd_is_partition);
This page took 0.048479 seconds and 5 git commands to generate.