Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Core registration and callback routines for MTD |
| 3 | * drivers and users. |
| 4 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 5 | * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> |
| 6 | * Copyright © 2006 Red Hat UK Limited |
| 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/ptrace.h> |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/string.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/major.h> |
| 31 | #include <linux/fs.h> |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 32 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/ioctl.h> |
| 34 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/proc_fs.h> |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 36 | #include <linux/idr.h> |
Jörn Engel | a33eb6b | 2010-04-27 09:40:52 +0200 | [diff] [blame] | 37 | #include <linux/backing-dev.h> |
Tejun Heo | 05d71b46 | 2010-03-30 02:52:39 +0900 | [diff] [blame] | 38 | #include <linux/gfp.h> |
David Howells | 0d01ff2 | 2013-04-11 23:51:01 +0100 | [diff] [blame] | 39 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <linux/mtd/mtd.h> |
Jamie Iles | f5671ab | 2011-05-23 17:15:46 +0100 | [diff] [blame] | 42 | #include <linux/mtd/partitions.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 44 | #include "mtdcore.h" |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 45 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 46 | static struct backing_dev_info mtd_bdi = { |
Jörn Engel | a33eb6b | 2010-04-27 09:40:52 +0200 | [diff] [blame] | 47 | }; |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 48 | |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 49 | static int mtd_cls_suspend(struct device *dev, pm_message_t state); |
| 50 | static int mtd_cls_resume(struct device *dev); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 51 | |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 52 | static struct class mtd_class = { |
| 53 | .name = "mtd", |
| 54 | .owner = THIS_MODULE, |
| 55 | .suspend = mtd_cls_suspend, |
| 56 | .resume = mtd_cls_resume, |
| 57 | }; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 58 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 59 | static DEFINE_IDR(mtd_idr); |
| 60 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 61 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | should not use them for _anything_ else */ |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 63 | DEFINE_MUTEX(mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(mtd_table_mutex); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 65 | |
| 66 | struct mtd_info *__mtd_next_device(int i) |
| 67 | { |
| 68 | return idr_get_next(&mtd_idr, &i); |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(__mtd_next_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | static LIST_HEAD(mtd_notifiers); |
| 73 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 74 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 75 | #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 76 | |
| 77 | /* REVISIT once MTD uses the driver model better, whoever allocates |
| 78 | * the mtd_info will probably want to use the release() hook... |
| 79 | */ |
| 80 | static void mtd_release(struct device *dev) |
| 81 | { |
Brian Norris | 5e47212 | 2014-07-21 19:06:47 -0700 | [diff] [blame] | 82 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 83 | dev_t index = MTD_DEVT(mtd->index); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 84 | |
Brian Norris | 5e47212 | 2014-07-21 19:06:47 -0700 | [diff] [blame] | 85 | /* remove /dev/mtdXro node */ |
| 86 | device_destroy(&mtd_class, index + 1); |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int mtd_cls_suspend(struct device *dev, pm_message_t state) |
| 90 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 91 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Saeed Bishara | 6afc4fd | 2009-07-28 04:56:43 -0700 | [diff] [blame] | 92 | |
Artem Bityutskiy | 1a30871 | 2012-01-16 11:07:16 +0200 | [diff] [blame] | 93 | return mtd ? mtd_suspend(mtd) : 0; |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int mtd_cls_resume(struct device *dev) |
| 97 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 98 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 99 | |
Brian Norris | 3ee5014185b | 2012-01-27 12:39:32 -0800 | [diff] [blame] | 100 | if (mtd) |
Artem Bityutskiy | ead995f | 2011-12-23 19:31:25 +0200 | [diff] [blame] | 101 | mtd_resume(mtd); |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 102 | return 0; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static ssize_t mtd_type_show(struct device *dev, |
| 106 | struct device_attribute *attr, char *buf) |
| 107 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 108 | struct mtd_info *mtd = dev_get_drvdata(dev); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 109 | char *type; |
| 110 | |
| 111 | switch (mtd->type) { |
| 112 | case MTD_ABSENT: |
| 113 | type = "absent"; |
| 114 | break; |
| 115 | case MTD_RAM: |
| 116 | type = "ram"; |
| 117 | break; |
| 118 | case MTD_ROM: |
| 119 | type = "rom"; |
| 120 | break; |
| 121 | case MTD_NORFLASH: |
| 122 | type = "nor"; |
| 123 | break; |
| 124 | case MTD_NANDFLASH: |
| 125 | type = "nand"; |
| 126 | break; |
| 127 | case MTD_DATAFLASH: |
| 128 | type = "dataflash"; |
| 129 | break; |
| 130 | case MTD_UBIVOLUME: |
| 131 | type = "ubi"; |
| 132 | break; |
Huang Shijie | f483724 | 2013-09-25 14:58:19 +0800 | [diff] [blame] | 133 | case MTD_MLCNANDFLASH: |
| 134 | type = "mlc-nand"; |
| 135 | break; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 136 | default: |
| 137 | type = "unknown"; |
| 138 | } |
| 139 | |
| 140 | return snprintf(buf, PAGE_SIZE, "%s\n", type); |
| 141 | } |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 142 | static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); |
| 143 | |
| 144 | static ssize_t mtd_flags_show(struct device *dev, |
| 145 | struct device_attribute *attr, char *buf) |
| 146 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 147 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 148 | |
| 149 | return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); |
| 150 | |
| 151 | } |
| 152 | static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); |
| 153 | |
| 154 | static ssize_t mtd_size_show(struct device *dev, |
| 155 | struct device_attribute *attr, char *buf) |
| 156 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 157 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 158 | |
| 159 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
| 160 | (unsigned long long)mtd->size); |
| 161 | |
| 162 | } |
| 163 | static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); |
| 164 | |
| 165 | static ssize_t mtd_erasesize_show(struct device *dev, |
| 166 | struct device_attribute *attr, char *buf) |
| 167 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 168 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 169 | |
| 170 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); |
| 171 | |
| 172 | } |
| 173 | static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); |
| 174 | |
| 175 | static ssize_t mtd_writesize_show(struct device *dev, |
| 176 | struct device_attribute *attr, char *buf) |
| 177 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 178 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 179 | |
| 180 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); |
| 181 | |
| 182 | } |
| 183 | static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); |
| 184 | |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 185 | static ssize_t mtd_subpagesize_show(struct device *dev, |
| 186 | struct device_attribute *attr, char *buf) |
| 187 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 188 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 189 | unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 190 | |
| 191 | return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); |
| 192 | |
| 193 | } |
| 194 | static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); |
| 195 | |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 196 | static ssize_t mtd_oobsize_show(struct device *dev, |
| 197 | struct device_attribute *attr, char *buf) |
| 198 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 199 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 200 | |
| 201 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); |
| 202 | |
| 203 | } |
| 204 | static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); |
| 205 | |
| 206 | static ssize_t mtd_numeraseregions_show(struct device *dev, |
| 207 | struct device_attribute *attr, char *buf) |
| 208 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 209 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 210 | |
| 211 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); |
| 212 | |
| 213 | } |
| 214 | static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, |
| 215 | NULL); |
| 216 | |
| 217 | static ssize_t mtd_name_show(struct device *dev, |
| 218 | struct device_attribute *attr, char *buf) |
| 219 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 220 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 221 | |
| 222 | return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); |
| 223 | |
| 224 | } |
| 225 | static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 226 | |
Mike Dunn | a9b672e | 2012-04-25 12:06:07 -0700 | [diff] [blame] | 227 | static ssize_t mtd_ecc_strength_show(struct device *dev, |
| 228 | struct device_attribute *attr, char *buf) |
| 229 | { |
| 230 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 231 | |
| 232 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); |
| 233 | } |
| 234 | static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); |
| 235 | |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 236 | static ssize_t mtd_bitflip_threshold_show(struct device *dev, |
| 237 | struct device_attribute *attr, |
| 238 | char *buf) |
| 239 | { |
| 240 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 241 | |
| 242 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); |
| 243 | } |
| 244 | |
| 245 | static ssize_t mtd_bitflip_threshold_store(struct device *dev, |
| 246 | struct device_attribute *attr, |
| 247 | const char *buf, size_t count) |
| 248 | { |
| 249 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 250 | unsigned int bitflip_threshold; |
| 251 | int retval; |
| 252 | |
| 253 | retval = kstrtouint(buf, 0, &bitflip_threshold); |
| 254 | if (retval) |
| 255 | return retval; |
| 256 | |
| 257 | mtd->bitflip_threshold = bitflip_threshold; |
| 258 | return count; |
| 259 | } |
| 260 | static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, |
| 261 | mtd_bitflip_threshold_show, |
| 262 | mtd_bitflip_threshold_store); |
| 263 | |
Huang Shijie | bf977e3 | 2013-08-16 10:10:05 +0800 | [diff] [blame] | 264 | static ssize_t mtd_ecc_step_size_show(struct device *dev, |
| 265 | struct device_attribute *attr, char *buf) |
| 266 | { |
| 267 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 268 | |
| 269 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); |
| 270 | |
| 271 | } |
| 272 | static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); |
| 273 | |
Ezequiel Garcia | 990a3af | 2014-06-24 10:55:50 -0300 | [diff] [blame] | 274 | static ssize_t mtd_ecc_stats_corrected_show(struct device *dev, |
| 275 | struct device_attribute *attr, char *buf) |
| 276 | { |
| 277 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 278 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 279 | |
| 280 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected); |
| 281 | } |
| 282 | static DEVICE_ATTR(corrected_bits, S_IRUGO, |
| 283 | mtd_ecc_stats_corrected_show, NULL); |
| 284 | |
| 285 | static ssize_t mtd_ecc_stats_errors_show(struct device *dev, |
| 286 | struct device_attribute *attr, char *buf) |
| 287 | { |
| 288 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 289 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 290 | |
| 291 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed); |
| 292 | } |
| 293 | static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL); |
| 294 | |
| 295 | static ssize_t mtd_badblocks_show(struct device *dev, |
| 296 | struct device_attribute *attr, char *buf) |
| 297 | { |
| 298 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 299 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 300 | |
| 301 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks); |
| 302 | } |
| 303 | static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL); |
| 304 | |
| 305 | static ssize_t mtd_bbtblocks_show(struct device *dev, |
| 306 | struct device_attribute *attr, char *buf) |
| 307 | { |
| 308 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 309 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 310 | |
| 311 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks); |
| 312 | } |
| 313 | static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL); |
| 314 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 315 | static struct attribute *mtd_attrs[] = { |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 316 | &dev_attr_type.attr, |
| 317 | &dev_attr_flags.attr, |
| 318 | &dev_attr_size.attr, |
| 319 | &dev_attr_erasesize.attr, |
| 320 | &dev_attr_writesize.attr, |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 321 | &dev_attr_subpagesize.attr, |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 322 | &dev_attr_oobsize.attr, |
| 323 | &dev_attr_numeraseregions.attr, |
| 324 | &dev_attr_name.attr, |
Mike Dunn | a9b672e | 2012-04-25 12:06:07 -0700 | [diff] [blame] | 325 | &dev_attr_ecc_strength.attr, |
Huang Shijie | bf977e3 | 2013-08-16 10:10:05 +0800 | [diff] [blame] | 326 | &dev_attr_ecc_step_size.attr, |
Ezequiel Garcia | 990a3af | 2014-06-24 10:55:50 -0300 | [diff] [blame] | 327 | &dev_attr_corrected_bits.attr, |
| 328 | &dev_attr_ecc_failures.attr, |
| 329 | &dev_attr_bad_blocks.attr, |
| 330 | &dev_attr_bbt_blocks.attr, |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 331 | &dev_attr_bitflip_threshold.attr, |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 332 | NULL, |
| 333 | }; |
Axel Lin | 54c738f | 2013-12-02 10:12:25 +0800 | [diff] [blame] | 334 | ATTRIBUTE_GROUPS(mtd); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 335 | |
| 336 | static struct device_type mtd_devtype = { |
| 337 | .name = "mtd", |
| 338 | .groups = mtd_groups, |
| 339 | .release = mtd_release, |
| 340 | }; |
| 341 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 342 | #ifndef CONFIG_MMU |
| 343 | unsigned mtd_mmap_capabilities(struct mtd_info *mtd) |
| 344 | { |
| 345 | switch (mtd->type) { |
| 346 | case MTD_RAM: |
| 347 | return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | |
| 348 | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 349 | case MTD_ROM: |
| 350 | return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | |
| 351 | NOMMU_MAP_READ; |
| 352 | default: |
| 353 | return NOMMU_MAP_COPY; |
| 354 | } |
| 355 | } |
| 356 | #endif |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /** |
| 359 | * add_mtd_device - register an MTD device |
| 360 | * @mtd: pointer to new MTD device info structure |
| 361 | * |
| 362 | * Add a device to the list of MTD devices present in the system, and |
| 363 | * notify each currently active MTD 'user' of its arrival. Returns |
| 364 | * zero on success or 1 on failure, which currently will only happen |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 365 | * if there is insufficient memory or a sysfs error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
| 367 | |
| 368 | int add_mtd_device(struct mtd_info *mtd) |
| 369 | { |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 370 | struct mtd_notifier *not; |
| 371 | int i, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 373 | mtd->backing_dev_info = &mtd_bdi; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 374 | |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 375 | BUG_ON(mtd->writesize == 0); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 376 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Tejun Heo | 589e9c4 | 2013-02-27 17:04:33 -0800 | [diff] [blame] | 378 | i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); |
| 379 | if (i < 0) |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 380 | goto fail_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 382 | mtd->index = i; |
| 383 | mtd->usecount = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 384 | |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 385 | /* default value if not set by driver */ |
| 386 | if (mtd->bitflip_threshold == 0) |
| 387 | mtd->bitflip_threshold = mtd->ecc_strength; |
| 388 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 389 | if (is_power_of_2(mtd->erasesize)) |
| 390 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; |
| 391 | else |
| 392 | mtd->erasesize_shift = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 393 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 394 | if (is_power_of_2(mtd->writesize)) |
| 395 | mtd->writesize_shift = ffs(mtd->writesize) - 1; |
| 396 | else |
| 397 | mtd->writesize_shift = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 398 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 399 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; |
| 400 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; |
Håvard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 401 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 402 | /* Some chips always power up locked. Unlock them now */ |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 403 | if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { |
| 404 | error = mtd_unlock(mtd, 0, mtd->size); |
| 405 | if (error && error != -EOPNOTSUPP) |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 406 | printk(KERN_WARNING |
| 407 | "%s: unlock failed, writes may not work\n", |
| 408 | mtd->name); |
| 409 | } |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 410 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 411 | /* Caller should have set dev.parent to match the |
| 412 | * physical device. |
| 413 | */ |
| 414 | mtd->dev.type = &mtd_devtype; |
| 415 | mtd->dev.class = &mtd_class; |
| 416 | mtd->dev.devt = MTD_DEVT(i); |
| 417 | dev_set_name(&mtd->dev, "mtd%d", i); |
| 418 | dev_set_drvdata(&mtd->dev, mtd); |
| 419 | if (device_register(&mtd->dev) != 0) |
| 420 | goto fail_added; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 421 | |
Brian Norris | 5e47212 | 2014-07-21 19:06:47 -0700 | [diff] [blame] | 422 | device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, |
| 423 | "mtd%dro", i); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 424 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 425 | pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 426 | /* No need to get a refcount on the module containing |
| 427 | the notifier, since we hold the mtd_table_mutex */ |
| 428 | list_for_each_entry(not, &mtd_notifiers, list) |
| 429 | not->add(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 430 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 431 | mutex_unlock(&mtd_table_mutex); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 432 | /* We _know_ we aren't being removed, because |
| 433 | our caller is still holding us here. So none |
| 434 | of this try_ nonsense, and no bitching about it |
| 435 | either. :) */ |
| 436 | __module_get(THIS_MODULE); |
| 437 | return 0; |
| 438 | |
| 439 | fail_added: |
| 440 | idr_remove(&mtd_idr, i); |
| 441 | fail_locked: |
| 442 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * del_mtd_device - unregister an MTD device |
| 448 | * @mtd: pointer to MTD device info structure |
| 449 | * |
| 450 | * Remove a device from the list of MTD devices present in the system, |
| 451 | * and notify each currently active MTD 'user' of its departure. |
| 452 | * Returns zero on success or 1 on failure, which currently will happen |
| 453 | * if the requested device does not appear to be present in the list. |
| 454 | */ |
| 455 | |
Jamie Iles | eea72d5 | 2011-05-23 10:23:42 +0100 | [diff] [blame] | 456 | int del_mtd_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
| 458 | int ret; |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 459 | struct mtd_notifier *not; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 460 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 461 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 463 | if (idr_find(&mtd_idr, mtd->index) != mtd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | ret = -ENODEV; |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 465 | goto out_error; |
| 466 | } |
| 467 | |
| 468 | /* No need to get a refcount on the module containing |
| 469 | the notifier, since we hold the mtd_table_mutex */ |
| 470 | list_for_each_entry(not, &mtd_notifiers, list) |
| 471 | not->remove(mtd); |
| 472 | |
| 473 | if (mtd->usecount) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 474 | printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | mtd->index, mtd->name, mtd->usecount); |
| 476 | ret = -EBUSY; |
| 477 | } else { |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 478 | device_unregister(&mtd->dev); |
| 479 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 480 | idr_remove(&mtd_idr, mtd->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | module_put(THIS_MODULE); |
| 483 | ret = 0; |
| 484 | } |
| 485 | |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 486 | out_error: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 487 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | /** |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 492 | * mtd_device_parse_register - parse partitions and register an MTD device. |
| 493 | * |
| 494 | * @mtd: the MTD device to register |
| 495 | * @types: the list of MTD partition probes to try, see |
| 496 | * 'parse_mtd_partitions()' for more information |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 497 | * @parser_data: MTD partition parser-specific data |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 498 | * @parts: fallback partition information to register, if parsing fails; |
| 499 | * only valid if %nr_parts > %0 |
| 500 | * @nr_parts: the number of partitions in parts, if zero then the full |
| 501 | * MTD device is registered if no partition info is found |
| 502 | * |
| 503 | * This function aggregates MTD partitions parsing (done by |
| 504 | * 'parse_mtd_partitions()') and MTD device and partitions registering. It |
| 505 | * basically follows the most common pattern found in many MTD drivers: |
| 506 | * |
| 507 | * * It first tries to probe partitions on MTD device @mtd using parsers |
| 508 | * specified in @types (if @types is %NULL, then the default list of parsers |
| 509 | * is used, see 'parse_mtd_partitions()' for more information). If none are |
| 510 | * found this functions tries to fallback to information specified in |
| 511 | * @parts/@nr_parts. |
Brian Norris | 92394b5c | 2011-07-20 09:53:42 -0700 | [diff] [blame] | 512 | * * If any partitioning info was found, this function registers the found |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 513 | * partitions. |
| 514 | * * If no partitions were found this function just registers the MTD device |
| 515 | * @mtd and exits. |
| 516 | * |
| 517 | * Returns zero in case of success and a negative error code in case of failure. |
| 518 | */ |
Artem Bityutskiy | 26a4734 | 2013-03-11 15:38:48 +0200 | [diff] [blame] | 519 | int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 520 | struct mtd_part_parser_data *parser_data, |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 521 | const struct mtd_partition *parts, |
| 522 | int nr_parts) |
| 523 | { |
| 524 | int err; |
| 525 | struct mtd_partition *real_parts; |
| 526 | |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 527 | err = parse_mtd_partitions(mtd, types, &real_parts, parser_data); |
Jason Liu | 4d523b60 | 2011-08-24 19:26:28 +0800 | [diff] [blame] | 528 | if (err <= 0 && nr_parts && parts) { |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 529 | real_parts = kmemdup(parts, sizeof(*parts) * nr_parts, |
| 530 | GFP_KERNEL); |
Jason Liu | 4d523b60 | 2011-08-24 19:26:28 +0800 | [diff] [blame] | 531 | if (!real_parts) |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 532 | err = -ENOMEM; |
Jason Liu | 4d523b60 | 2011-08-24 19:26:28 +0800 | [diff] [blame] | 533 | else |
| 534 | err = nr_parts; |
Dmitry Eremin-Solenikov | 1c4c215 | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | if (err > 0) { |
| 538 | err = add_mtd_partitions(mtd, real_parts, err); |
| 539 | kfree(real_parts); |
| 540 | } else if (err == 0) { |
| 541 | err = add_mtd_device(mtd); |
| 542 | if (err == 1) |
| 543 | err = -ENODEV; |
| 544 | } |
| 545 | |
| 546 | return err; |
| 547 | } |
| 548 | EXPORT_SYMBOL_GPL(mtd_device_parse_register); |
| 549 | |
| 550 | /** |
Jamie Iles | f5671ab | 2011-05-23 17:15:46 +0100 | [diff] [blame] | 551 | * mtd_device_unregister - unregister an existing MTD device. |
| 552 | * |
| 553 | * @master: the MTD device to unregister. This will unregister both the master |
| 554 | * and any partitions if registered. |
| 555 | */ |
| 556 | int mtd_device_unregister(struct mtd_info *master) |
| 557 | { |
| 558 | int err; |
| 559 | |
| 560 | err = del_mtd_partitions(master); |
| 561 | if (err) |
| 562 | return err; |
| 563 | |
| 564 | if (!device_is_registered(&master->dev)) |
| 565 | return 0; |
| 566 | |
| 567 | return del_mtd_device(master); |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(mtd_device_unregister); |
| 570 | |
| 571 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | * register_mtd_user - register a 'user' of MTD devices. |
| 573 | * @new: pointer to notifier info structure |
| 574 | * |
| 575 | * Registers a pair of callbacks function to be called upon addition |
| 576 | * or removal of MTD devices. Causes the 'add' callback to be immediately |
| 577 | * invoked for each MTD device currently present in the system. |
| 578 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | void register_mtd_user (struct mtd_notifier *new) |
| 580 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 581 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 583 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
| 585 | list_add(&new->list, &mtd_notifiers); |
| 586 | |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 587 | __module_get(THIS_MODULE); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 588 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 589 | mtd_for_each_device(mtd) |
| 590 | new->add(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 592 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 594 | EXPORT_SYMBOL_GPL(register_mtd_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /** |
Artem B. Bityuckiy | 4945079 | 2005-02-18 14:34:54 +0000 | [diff] [blame] | 597 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
| 598 | * @old: pointer to notifier info structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | * |
| 600 | * Removes a callback function pair from the list of 'users' to be |
| 601 | * notified upon addition or removal of MTD devices. Causes the |
| 602 | * 'remove' callback to be immediately invoked for each MTD device |
| 603 | * currently present in the system. |
| 604 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | int unregister_mtd_user (struct mtd_notifier *old) |
| 606 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 607 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 609 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | module_put(THIS_MODULE); |
| 612 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 613 | mtd_for_each_device(mtd) |
| 614 | old->remove(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | list_del(&old->list); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 617 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return 0; |
| 619 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 620 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | /** |
| 623 | * get_mtd_device - obtain a validated handle for an MTD device |
| 624 | * @mtd: last known address of the required MTD device |
| 625 | * @num: internal device number of the required MTD device |
| 626 | * |
| 627 | * Given a number and NULL address, return the num'th entry in the device |
| 628 | * table, if any. Given an address and num == -1, search the device table |
| 629 | * for a device with that address and return if it's still present. Given |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 630 | * both, return the num'th driver only if its address matches. Return |
| 631 | * error code if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 634 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 635 | struct mtd_info *ret = NULL, *other; |
| 636 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 638 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | if (num == -1) { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 641 | mtd_for_each_device(other) { |
| 642 | if (other == mtd) { |
| 643 | ret = mtd; |
| 644 | break; |
| 645 | } |
| 646 | } |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 647 | } else if (num >= 0) { |
| 648 | ret = idr_find(&mtd_idr, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if (mtd && mtd != ret) |
| 650 | ret = NULL; |
| 651 | } |
| 652 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 653 | if (!ret) { |
| 654 | ret = ERR_PTR(err); |
| 655 | goto out; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 656 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 658 | err = __get_mtd_device(ret); |
| 659 | if (err) |
| 660 | ret = ERR_PTR(err); |
| 661 | out: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 662 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | return ret; |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 664 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 665 | EXPORT_SYMBOL_GPL(get_mtd_device); |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 666 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 667 | |
| 668 | int __get_mtd_device(struct mtd_info *mtd) |
| 669 | { |
| 670 | int err; |
| 671 | |
| 672 | if (!try_module_get(mtd->owner)) |
| 673 | return -ENODEV; |
| 674 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 675 | if (mtd->_get_device) { |
| 676 | err = mtd->_get_device(mtd); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 677 | |
| 678 | if (err) { |
| 679 | module_put(mtd->owner); |
| 680 | return err; |
| 681 | } |
| 682 | } |
| 683 | mtd->usecount++; |
| 684 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 686 | EXPORT_SYMBOL_GPL(__get_mtd_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 688 | /** |
| 689 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 690 | * device name |
| 691 | * @name: MTD device name to open |
| 692 | * |
| 693 | * This function returns MTD device description structure in case of |
| 694 | * success and an error code in case of failure. |
| 695 | */ |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 696 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 697 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 698 | int err = -ENODEV; |
| 699 | struct mtd_info *mtd = NULL, *other; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 700 | |
| 701 | mutex_lock(&mtd_table_mutex); |
| 702 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 703 | mtd_for_each_device(other) { |
| 704 | if (!strcmp(name, other->name)) { |
| 705 | mtd = other; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | } |
| 709 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 710 | if (!mtd) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 711 | goto out_unlock; |
| 712 | |
Wanlong Gao | 52534f2 | 2011-05-17 22:36:18 +0800 | [diff] [blame] | 713 | err = __get_mtd_device(mtd); |
| 714 | if (err) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 715 | goto out_unlock; |
| 716 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 717 | mutex_unlock(&mtd_table_mutex); |
| 718 | return mtd; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 719 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 720 | out_unlock: |
| 721 | mutex_unlock(&mtd_table_mutex); |
| 722 | return ERR_PTR(err); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 723 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 724 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | void put_mtd_device(struct mtd_info *mtd) |
| 727 | { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 728 | mutex_lock(&mtd_table_mutex); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 729 | __put_mtd_device(mtd); |
| 730 | mutex_unlock(&mtd_table_mutex); |
| 731 | |
| 732 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 733 | EXPORT_SYMBOL_GPL(put_mtd_device); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 734 | |
| 735 | void __put_mtd_device(struct mtd_info *mtd) |
| 736 | { |
| 737 | --mtd->usecount; |
| 738 | BUG_ON(mtd->usecount < 0); |
| 739 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 740 | if (mtd->_put_device) |
| 741 | mtd->_put_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | module_put(mtd->owner); |
| 744 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 745 | EXPORT_SYMBOL_GPL(__put_mtd_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 747 | /* |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 748 | * Erase is an asynchronous operation. Device drivers are supposed |
| 749 | * to call instr->callback() whenever the operation completes, even |
| 750 | * if it completes with a failure. |
| 751 | * Callers are supposed to pass a callback function and wait for it |
| 752 | * to be called before writing to the block. |
| 753 | */ |
| 754 | int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 755 | { |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 756 | if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 757 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 758 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 759 | return -EROFS; |
Shmulik Ladkani | 3b27dac | 2012-02-09 15:36:29 +0200 | [diff] [blame] | 760 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 761 | if (!instr->len) { |
| 762 | instr->state = MTD_ERASE_DONE; |
| 763 | mtd_erase_callback(instr); |
| 764 | return 0; |
| 765 | } |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 766 | return mtd->_erase(mtd, instr); |
| 767 | } |
| 768 | EXPORT_SYMBOL_GPL(mtd_erase); |
| 769 | |
| 770 | /* |
| 771 | * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. |
| 772 | */ |
| 773 | int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, |
| 774 | void **virt, resource_size_t *phys) |
| 775 | { |
| 776 | *retlen = 0; |
Artem Bityutskiy | 0dd5235 | 2012-02-08 15:13:26 +0200 | [diff] [blame] | 777 | *virt = NULL; |
| 778 | if (phys) |
| 779 | *phys = 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 780 | if (!mtd->_point) |
| 781 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 782 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 783 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 784 | if (!len) |
| 785 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 786 | return mtd->_point(mtd, from, len, retlen, virt, phys); |
| 787 | } |
| 788 | EXPORT_SYMBOL_GPL(mtd_point); |
| 789 | |
| 790 | /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ |
| 791 | int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
| 792 | { |
| 793 | if (!mtd->_point) |
| 794 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 795 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 796 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 797 | if (!len) |
| 798 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 799 | return mtd->_unpoint(mtd, from, len); |
| 800 | } |
| 801 | EXPORT_SYMBOL_GPL(mtd_unpoint); |
| 802 | |
| 803 | /* |
| 804 | * Allow NOMMU mmap() to directly map the device (if not NULL) |
| 805 | * - return the address to which the offset maps |
| 806 | * - return -ENOSYS to indicate refusal to do the mapping |
| 807 | */ |
| 808 | unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, |
| 809 | unsigned long offset, unsigned long flags) |
| 810 | { |
| 811 | if (!mtd->_get_unmapped_area) |
| 812 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 813 | if (offset >= mtd->size || len > mtd->size - offset) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 814 | return -EINVAL; |
| 815 | return mtd->_get_unmapped_area(mtd, len, offset, flags); |
| 816 | } |
| 817 | EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); |
| 818 | |
| 819 | int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, |
| 820 | u_char *buf) |
| 821 | { |
Mike Dunn | edbc454 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 822 | int ret_code; |
Artem Bityutskiy | 834247e | 2012-02-06 12:39:07 +0200 | [diff] [blame] | 823 | *retlen = 0; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 824 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 825 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 826 | if (!len) |
| 827 | return 0; |
Mike Dunn | edbc454 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 828 | |
| 829 | /* |
| 830 | * In the absence of an error, drivers return a non-negative integer |
| 831 | * representing the maximum number of bitflips that were corrected on |
| 832 | * any one ecc region (if applicable; zero otherwise). |
| 833 | */ |
| 834 | ret_code = mtd->_read(mtd, from, len, retlen, buf); |
| 835 | if (unlikely(ret_code < 0)) |
| 836 | return ret_code; |
| 837 | if (mtd->ecc_strength == 0) |
| 838 | return 0; /* device lacks ecc */ |
| 839 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 840 | } |
| 841 | EXPORT_SYMBOL_GPL(mtd_read); |
| 842 | |
| 843 | int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, |
| 844 | const u_char *buf) |
| 845 | { |
| 846 | *retlen = 0; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 847 | if (to < 0 || to >= mtd->size || len > mtd->size - to) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 848 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 849 | if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE)) |
| 850 | return -EROFS; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 851 | if (!len) |
| 852 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 853 | return mtd->_write(mtd, to, len, retlen, buf); |
| 854 | } |
| 855 | EXPORT_SYMBOL_GPL(mtd_write); |
| 856 | |
| 857 | /* |
| 858 | * In blackbox flight recorder like scenarios we want to make successful writes |
| 859 | * in interrupt context. panic_write() is only intended to be called when its |
| 860 | * known the kernel is about to panic and we need the write to succeed. Since |
| 861 | * the kernel is not going to be running for much longer, this function can |
| 862 | * break locks and delay to ensure the write succeeds (but not sleep). |
| 863 | */ |
| 864 | int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, |
| 865 | const u_char *buf) |
| 866 | { |
| 867 | *retlen = 0; |
| 868 | if (!mtd->_panic_write) |
| 869 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 870 | if (to < 0 || to >= mtd->size || len > mtd->size - to) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 871 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 872 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 873 | return -EROFS; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 874 | if (!len) |
| 875 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 876 | return mtd->_panic_write(mtd, to, len, retlen, buf); |
| 877 | } |
| 878 | EXPORT_SYMBOL_GPL(mtd_panic_write); |
| 879 | |
Brian Norris | d2d4848 | 2012-06-22 16:35:38 -0700 | [diff] [blame] | 880 | int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) |
| 881 | { |
Brian Norris | e47f685 | 2012-06-22 16:35:39 -0700 | [diff] [blame] | 882 | int ret_code; |
Brian Norris | d2d4848 | 2012-06-22 16:35:38 -0700 | [diff] [blame] | 883 | ops->retlen = ops->oobretlen = 0; |
| 884 | if (!mtd->_read_oob) |
| 885 | return -EOPNOTSUPP; |
Brian Norris | e47f685 | 2012-06-22 16:35:39 -0700 | [diff] [blame] | 886 | /* |
| 887 | * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics |
| 888 | * similar to mtd->_read(), returning a non-negative integer |
| 889 | * representing max bitflips. In other cases, mtd->_read_oob() may |
| 890 | * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). |
| 891 | */ |
| 892 | ret_code = mtd->_read_oob(mtd, from, ops); |
| 893 | if (unlikely(ret_code < 0)) |
| 894 | return ret_code; |
| 895 | if (mtd->ecc_strength == 0) |
| 896 | return 0; /* device lacks ecc */ |
| 897 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; |
Brian Norris | d2d4848 | 2012-06-22 16:35:38 -0700 | [diff] [blame] | 898 | } |
| 899 | EXPORT_SYMBOL_GPL(mtd_read_oob); |
| 900 | |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 901 | /* |
| 902 | * Method to access the protection register area, present in some flash |
| 903 | * devices. The user data is one time programmable but the factory data is read |
| 904 | * only. |
| 905 | */ |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 906 | int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
| 907 | struct otp_info *buf) |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 908 | { |
| 909 | if (!mtd->_get_fact_prot_info) |
| 910 | return -EOPNOTSUPP; |
| 911 | if (!len) |
| 912 | return 0; |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 913 | return mtd->_get_fact_prot_info(mtd, len, retlen, buf); |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 914 | } |
| 915 | EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); |
| 916 | |
| 917 | int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, |
| 918 | size_t *retlen, u_char *buf) |
| 919 | { |
| 920 | *retlen = 0; |
| 921 | if (!mtd->_read_fact_prot_reg) |
| 922 | return -EOPNOTSUPP; |
| 923 | if (!len) |
| 924 | return 0; |
| 925 | return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf); |
| 926 | } |
| 927 | EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); |
| 928 | |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 929 | int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
| 930 | struct otp_info *buf) |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 931 | { |
| 932 | if (!mtd->_get_user_prot_info) |
| 933 | return -EOPNOTSUPP; |
| 934 | if (!len) |
| 935 | return 0; |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 936 | return mtd->_get_user_prot_info(mtd, len, retlen, buf); |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 937 | } |
| 938 | EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); |
| 939 | |
| 940 | int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, |
| 941 | size_t *retlen, u_char *buf) |
| 942 | { |
| 943 | *retlen = 0; |
| 944 | if (!mtd->_read_user_prot_reg) |
| 945 | return -EOPNOTSUPP; |
| 946 | if (!len) |
| 947 | return 0; |
| 948 | return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf); |
| 949 | } |
| 950 | EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); |
| 951 | |
| 952 | int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, |
| 953 | size_t *retlen, u_char *buf) |
| 954 | { |
Christian Riesch | 9a78bc8 | 2014-03-06 12:42:37 +0100 | [diff] [blame] | 955 | int ret; |
| 956 | |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 957 | *retlen = 0; |
| 958 | if (!mtd->_write_user_prot_reg) |
| 959 | return -EOPNOTSUPP; |
| 960 | if (!len) |
| 961 | return 0; |
Christian Riesch | 9a78bc8 | 2014-03-06 12:42:37 +0100 | [diff] [blame] | 962 | ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf); |
| 963 | if (ret) |
| 964 | return ret; |
| 965 | |
| 966 | /* |
| 967 | * If no data could be written at all, we are out of memory and |
| 968 | * must return -ENOSPC. |
| 969 | */ |
| 970 | return (*retlen) ? 0 : -ENOSPC; |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 971 | } |
| 972 | EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); |
| 973 | |
| 974 | int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) |
| 975 | { |
| 976 | if (!mtd->_lock_user_prot_reg) |
| 977 | return -EOPNOTSUPP; |
| 978 | if (!len) |
| 979 | return 0; |
| 980 | return mtd->_lock_user_prot_reg(mtd, from, len); |
| 981 | } |
| 982 | EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); |
| 983 | |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 984 | /* Chip-supported device locking */ |
| 985 | int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 986 | { |
| 987 | if (!mtd->_lock) |
| 988 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 989 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 990 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 991 | if (!len) |
| 992 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 993 | return mtd->_lock(mtd, ofs, len); |
| 994 | } |
| 995 | EXPORT_SYMBOL_GPL(mtd_lock); |
| 996 | |
| 997 | int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 998 | { |
| 999 | if (!mtd->_unlock) |
| 1000 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1001 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1002 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1003 | if (!len) |
| 1004 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1005 | return mtd->_unlock(mtd, ofs, len); |
| 1006 | } |
| 1007 | EXPORT_SYMBOL_GPL(mtd_unlock); |
| 1008 | |
| 1009 | int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1010 | { |
| 1011 | if (!mtd->_is_locked) |
| 1012 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1013 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1014 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1015 | if (!len) |
| 1016 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1017 | return mtd->_is_locked(mtd, ofs, len); |
| 1018 | } |
| 1019 | EXPORT_SYMBOL_GPL(mtd_is_locked); |
| 1020 | |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1021 | int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1022 | { |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1023 | if (ofs < 0 || ofs >= mtd->size) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1024 | return -EINVAL; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1025 | if (!mtd->_block_isreserved) |
| 1026 | return 0; |
| 1027 | return mtd->_block_isreserved(mtd, ofs); |
| 1028 | } |
| 1029 | EXPORT_SYMBOL_GPL(mtd_block_isreserved); |
| 1030 | |
| 1031 | int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) |
| 1032 | { |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1033 | if (ofs < 0 || ofs >= mtd->size) |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1034 | return -EINVAL; |
| 1035 | if (!mtd->_block_isbad) |
| 1036 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1037 | return mtd->_block_isbad(mtd, ofs); |
| 1038 | } |
| 1039 | EXPORT_SYMBOL_GPL(mtd_block_isbad); |
| 1040 | |
| 1041 | int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1042 | { |
| 1043 | if (!mtd->_block_markbad) |
| 1044 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1045 | if (ofs < 0 || ofs >= mtd->size) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1046 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1047 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1048 | return -EROFS; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1049 | return mtd->_block_markbad(mtd, ofs); |
| 1050 | } |
| 1051 | EXPORT_SYMBOL_GPL(mtd_block_markbad); |
| 1052 | |
| 1053 | /* |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1054 | * default_mtd_writev - the default writev method |
| 1055 | * @mtd: mtd device description object pointer |
| 1056 | * @vecs: the vectors to write |
| 1057 | * @count: count of vectors in @vecs |
| 1058 | * @to: the MTD device offset to write to |
| 1059 | * @retlen: on exit contains the count of bytes written to the MTD device. |
| 1060 | * |
| 1061 | * This function returns zero in case of success and a negative error code in |
| 1062 | * case of failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | */ |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1064 | static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 1065 | unsigned long count, loff_t to, size_t *retlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
| 1067 | unsigned long i; |
| 1068 | size_t totlen = 0, thislen; |
| 1069 | int ret = 0; |
| 1070 | |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1071 | for (i = 0; i < count; i++) { |
| 1072 | if (!vecs[i].iov_len) |
| 1073 | continue; |
| 1074 | ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, |
| 1075 | vecs[i].iov_base); |
| 1076 | totlen += thislen; |
| 1077 | if (ret || thislen != vecs[i].iov_len) |
| 1078 | break; |
| 1079 | to += vecs[i].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | } |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1081 | *retlen = totlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return ret; |
| 1083 | } |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* |
| 1086 | * mtd_writev - the vector-based MTD write method |
| 1087 | * @mtd: mtd device description object pointer |
| 1088 | * @vecs: the vectors to write |
| 1089 | * @count: count of vectors in @vecs |
| 1090 | * @to: the MTD device offset to write to |
| 1091 | * @retlen: on exit contains the count of bytes written to the MTD device. |
| 1092 | * |
| 1093 | * This function returns zero in case of success and a negative error code in |
| 1094 | * case of failure. |
| 1095 | */ |
| 1096 | int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 1097 | unsigned long count, loff_t to, size_t *retlen) |
| 1098 | { |
| 1099 | *retlen = 0; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1100 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1101 | return -EROFS; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 1102 | if (!mtd->_writev) |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1103 | return default_mtd_writev(mtd, vecs, count, to, retlen); |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 1104 | return mtd->_writev(mtd, vecs, count, to, retlen); |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1105 | } |
| 1106 | EXPORT_SYMBOL_GPL(mtd_writev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1108 | /** |
| 1109 | * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1110 | * @mtd: mtd device description object pointer |
| 1111 | * @size: a pointer to the ideal or maximum size of the allocation, points |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1112 | * to the actual allocation size on success. |
| 1113 | * |
| 1114 | * This routine attempts to allocate a contiguous kernel buffer up to |
| 1115 | * the specified size, backing off the size of the request exponentially |
| 1116 | * until the request succeeds or until the allocation size falls below |
| 1117 | * the system page size. This attempts to make sure it does not adversely |
| 1118 | * impact system performance, so when allocating more than one page, we |
Linus Torvalds | caf4919 | 2012-12-10 10:51:16 -0800 | [diff] [blame] | 1119 | * ask the memory allocator to avoid re-trying, swapping, writing back |
| 1120 | * or performing I/O. |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1121 | * |
| 1122 | * Note, this function also makes sure that the allocated buffer is aligned to |
| 1123 | * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. |
| 1124 | * |
| 1125 | * This is called, for example by mtd_{read,write} and jffs2_scan_medium, |
| 1126 | * to handle smaller (i.e. degraded) buffer allocations under low- or |
| 1127 | * fragmented-memory situations where such reduced allocations, from a |
| 1128 | * requested ideal, are allowed. |
| 1129 | * |
| 1130 | * Returns a pointer to the allocated buffer on success; otherwise, NULL. |
| 1131 | */ |
| 1132 | void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) |
| 1133 | { |
Linus Torvalds | caf4919 | 2012-12-10 10:51:16 -0800 | [diff] [blame] | 1134 | gfp_t flags = __GFP_NOWARN | __GFP_WAIT | |
| 1135 | __GFP_NORETRY | __GFP_NO_KSWAPD; |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1136 | size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); |
| 1137 | void *kbuf; |
| 1138 | |
| 1139 | *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); |
| 1140 | |
| 1141 | while (*size > min_alloc) { |
| 1142 | kbuf = kmalloc(*size, flags); |
| 1143 | if (kbuf) |
| 1144 | return kbuf; |
| 1145 | |
| 1146 | *size >>= 1; |
| 1147 | *size = ALIGN(*size, mtd->writesize); |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * For the last resort allocation allow 'kmalloc()' to do all sorts of |
| 1152 | * things (write-back, dropping caches, etc) by using GFP_KERNEL. |
| 1153 | */ |
| 1154 | return kmalloc(*size, GFP_KERNEL); |
| 1155 | } |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1156 | EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 1158 | #ifdef CONFIG_PROC_FS |
| 1159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | /*====================================================================*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | /* Support for /proc/mtd */ |
| 1162 | |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1163 | static int mtd_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 1165 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1167 | seq_puts(m, "dev: size erasesize name\n"); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1168 | mutex_lock(&mtd_table_mutex); |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 1169 | mtd_for_each_device(mtd) { |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1170 | seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", |
| 1171 | mtd->index, (unsigned long long)mtd->size, |
| 1172 | mtd->erasesize, mtd->name); |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 1173 | } |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1174 | mutex_unlock(&mtd_table_mutex); |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 1175 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1178 | static int mtd_proc_open(struct inode *inode, struct file *file) |
| 1179 | { |
| 1180 | return single_open(file, mtd_proc_show, NULL); |
| 1181 | } |
| 1182 | |
| 1183 | static const struct file_operations mtd_proc_ops = { |
| 1184 | .open = mtd_proc_open, |
| 1185 | .read = seq_read, |
| 1186 | .llseek = seq_lseek, |
| 1187 | .release = single_release, |
| 1188 | }; |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 1189 | #endif /* CONFIG_PROC_FS */ |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | /*====================================================================*/ |
| 1192 | /* Init code */ |
| 1193 | |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1194 | static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name) |
| 1195 | { |
| 1196 | int ret; |
| 1197 | |
| 1198 | ret = bdi_init(bdi); |
| 1199 | if (!ret) |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 1200 | ret = bdi_register(bdi, NULL, "%s", name); |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1201 | |
| 1202 | if (ret) |
| 1203 | bdi_destroy(bdi); |
| 1204 | |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
Artem Bityutskiy | 93e5621 | 2013-03-15 12:56:05 +0200 | [diff] [blame] | 1208 | static struct proc_dir_entry *proc_mtd; |
| 1209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | static int __init init_mtd(void) |
| 1211 | { |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 1212 | int ret; |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 1213 | |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1214 | ret = class_register(&mtd_class); |
| 1215 | if (ret) |
| 1216 | goto err_reg; |
| 1217 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 1218 | ret = mtd_bdi_init(&mtd_bdi, "mtd"); |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1219 | if (ret) |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 1220 | goto err_bdi; |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1221 | |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1222 | proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops); |
Artem Bityutskiy | 93e5621 | 2013-03-15 12:56:05 +0200 | [diff] [blame] | 1223 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1224 | ret = init_mtdchar(); |
| 1225 | if (ret) |
| 1226 | goto out_procfs; |
| 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | return 0; |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1229 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1230 | out_procfs: |
| 1231 | if (proc_mtd) |
| 1232 | remove_proc_entry("mtd", NULL); |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 1233 | err_bdi: |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1234 | class_unregister(&mtd_class); |
| 1235 | err_reg: |
| 1236 | pr_err("Error registering mtd class or bdi: %d\n", ret); |
| 1237 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static void __exit cleanup_mtd(void) |
| 1241 | { |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1242 | cleanup_mtdchar(); |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 1243 | if (proc_mtd) |
Artem Bityutskiy | 93e5621 | 2013-03-15 12:56:05 +0200 | [diff] [blame] | 1244 | remove_proc_entry("mtd", NULL); |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 1245 | class_unregister(&mtd_class); |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame^] | 1246 | bdi_destroy(&mtd_bdi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | module_init(init_mtd); |
| 1250 | module_exit(cleanup_mtd); |
| 1251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | MODULE_LICENSE("GPL"); |
| 1253 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 1254 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |