2 * Core registration and callback routines for MTD
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/compat.h>
12 #include <ubi_uboot.h>
14 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
16 int add_mtd_device(struct mtd_info *mtd)
20 BUG_ON(mtd->writesize == 0);
22 for (i = 0; i < MAX_MTD_DEVICES; i++)
28 /* No need to get a refcount on the module containing
29 the notifier, since we hold the mtd_table_mutex */
31 /* We _know_ we aren't being removed, because
32 our caller is still holding us here. So none
33 of this try_ nonsense, and no bitching about it
42 * del_mtd_device - unregister an MTD device
43 * @mtd: pointer to MTD device info structure
45 * Remove a device from the list of MTD devices present in the system,
46 * and notify each currently active MTD 'user' of its departure.
47 * Returns zero on success or 1 on failure, which currently will happen
48 * if the requested device does not appear to be present in the list.
50 int del_mtd_device(struct mtd_info *mtd)
54 if (mtd_table[mtd->index] != mtd) {
56 } else if (mtd->usecount) {
57 printk(KERN_NOTICE "Removing MTD device #%d (%s)"
58 " with use count %d\n",
59 mtd->index, mtd->name, mtd->usecount);
62 /* No need to get a refcount on the module containing
63 * the notifier, since we hold the mtd_table_mutex */
64 mtd_table[mtd->index] = NULL;
73 * get_mtd_device - obtain a validated handle for an MTD device
74 * @mtd: last known address of the required MTD device
75 * @num: internal device number of the required MTD device
77 * Given a number and NULL address, return the num'th entry in the device
78 * table, if any. Given an address and num == -1, search the device table
79 * for a device with that address and return if it's still present. Given
80 * both, return the num'th driver only if its address matches. Return
83 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
85 struct mtd_info *ret = NULL;
89 for (i = 0; i < MAX_MTD_DEVICES; i++)
90 if (mtd_table[i] == mtd)
92 } else if (num < MAX_MTD_DEVICES) {
94 if (mtd && mtd != ret)
109 * get_mtd_device_nm - obtain a validated handle for an MTD device by
111 * @name: MTD device name to open
113 * This function returns MTD device description structure in case of
114 * success and an error code in case of failure.
116 struct mtd_info *get_mtd_device_nm(const char *name)
118 int i, err = -ENODEV;
119 struct mtd_info *mtd = NULL;
121 for (i = 0; i < MAX_MTD_DEVICES; i++) {
122 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
138 void put_mtd_device(struct mtd_info *mtd)
146 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
148 * mtd_get_len_incl_bad
150 * Check if length including bad blocks fits into device.
152 * @param mtd an MTD device
153 * @param offset offset in flash
154 * @param length image length
155 * @return image length including bad blocks in *len_incl_bad and whether or not
156 * the length returned was truncated in *truncated
158 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
159 const uint64_t length, uint64_t *len_incl_bad,
165 if (!mtd->block_isbad) {
166 *len_incl_bad = length;
170 uint64_t len_excl_bad = 0;
173 while (len_excl_bad < length) {
174 if (offset >= mtd->size) {
179 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
181 if (!mtd->block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
182 len_excl_bad += block_len;
184 *len_incl_bad += block_len;
188 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */