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> |
Brian Norris | 215a02f | 2015-11-11 16:26:04 -0800 | [diff] [blame] | 35 | #include <linux/of.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/proc_fs.h> |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 37 | #include <linux/idr.h> |
Jörn Engel | a33eb6b | 2010-04-27 09:40:52 +0200 | [diff] [blame] | 38 | #include <linux/backing-dev.h> |
Tejun Heo | 05d71b46 | 2010-03-30 02:52:39 +0900 | [diff] [blame] | 39 | #include <linux/gfp.h> |
David Howells | 0d01ff2 | 2013-04-11 23:51:01 +0100 | [diff] [blame] | 40 | #include <linux/slab.h> |
Brian Norris | 3efe41b | 2014-11-26 01:01:08 -0800 | [diff] [blame] | 41 | #include <linux/reboot.h> |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 42 | #include <linux/leds.h> |
Mario Rugiero | e8e3edb | 2017-05-29 08:38:41 -0300 | [diff] [blame] | 43 | #include <linux/debugfs.h> |
Alban Bedel | c4dfa25 | 2018-11-13 15:01:10 +0100 | [diff] [blame] | 44 | #include <linux/nvmem-provider.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #include <linux/mtd/mtd.h> |
Jamie Iles | f5671ab | 2011-05-23 17:15:46 +0100 | [diff] [blame] | 47 | #include <linux/mtd/partitions.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 49 | #include "mtdcore.h" |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 50 | |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 51 | struct backing_dev_info *mtd_bdi; |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 52 | |
Lars-Peter Clausen | 57b8045d | 2015-04-06 12:00:39 +0200 | [diff] [blame] | 53 | #ifdef CONFIG_PM_SLEEP |
| 54 | |
| 55 | static int mtd_cls_suspend(struct device *dev) |
| 56 | { |
| 57 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 58 | |
| 59 | return mtd ? mtd_suspend(mtd) : 0; |
| 60 | } |
| 61 | |
| 62 | static int mtd_cls_resume(struct device *dev) |
| 63 | { |
| 64 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 65 | |
| 66 | if (mtd) |
| 67 | mtd_resume(mtd); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume); |
| 72 | #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops) |
| 73 | #else |
| 74 | #define MTD_CLS_PM_OPS NULL |
| 75 | #endif |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 76 | |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 77 | static struct class mtd_class = { |
| 78 | .name = "mtd", |
| 79 | .owner = THIS_MODULE, |
Lars-Peter Clausen | 57b8045d | 2015-04-06 12:00:39 +0200 | [diff] [blame] | 80 | .pm = MTD_CLS_PM_OPS, |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 81 | }; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 82 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 83 | static DEFINE_IDR(mtd_idr); |
| 84 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 85 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | should not use them for _anything_ else */ |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 87 | DEFINE_MUTEX(mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(mtd_table_mutex); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 89 | |
| 90 | struct mtd_info *__mtd_next_device(int i) |
| 91 | { |
| 92 | return idr_get_next(&mtd_idr, &i); |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(__mtd_next_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | static LIST_HEAD(mtd_notifiers); |
| 97 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 98 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 99 | #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 100 | |
| 101 | /* REVISIT once MTD uses the driver model better, whoever allocates |
| 102 | * the mtd_info will probably want to use the release() hook... |
| 103 | */ |
| 104 | static void mtd_release(struct device *dev) |
| 105 | { |
Brian Norris | 5e47212 | 2014-07-21 19:06:47 -0700 | [diff] [blame] | 106 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 107 | dev_t index = MTD_DEVT(mtd->index); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 108 | |
Brian Norris | 5e47212 | 2014-07-21 19:06:47 -0700 | [diff] [blame] | 109 | /* remove /dev/mtdXro node */ |
| 110 | device_destroy(&mtd_class, index + 1); |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 111 | } |
| 112 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 113 | static ssize_t mtd_type_show(struct device *dev, |
| 114 | struct device_attribute *attr, char *buf) |
| 115 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 116 | struct mtd_info *mtd = dev_get_drvdata(dev); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 117 | char *type; |
| 118 | |
| 119 | switch (mtd->type) { |
| 120 | case MTD_ABSENT: |
| 121 | type = "absent"; |
| 122 | break; |
| 123 | case MTD_RAM: |
| 124 | type = "ram"; |
| 125 | break; |
| 126 | case MTD_ROM: |
| 127 | type = "rom"; |
| 128 | break; |
| 129 | case MTD_NORFLASH: |
| 130 | type = "nor"; |
| 131 | break; |
| 132 | case MTD_NANDFLASH: |
| 133 | type = "nand"; |
| 134 | break; |
| 135 | case MTD_DATAFLASH: |
| 136 | type = "dataflash"; |
| 137 | break; |
| 138 | case MTD_UBIVOLUME: |
| 139 | type = "ubi"; |
| 140 | break; |
Huang Shijie | f483724 | 2013-09-25 14:58:19 +0800 | [diff] [blame] | 141 | case MTD_MLCNANDFLASH: |
| 142 | type = "mlc-nand"; |
| 143 | break; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 144 | default: |
| 145 | type = "unknown"; |
| 146 | } |
| 147 | |
| 148 | return snprintf(buf, PAGE_SIZE, "%s\n", type); |
| 149 | } |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 150 | static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); |
| 151 | |
| 152 | static ssize_t mtd_flags_show(struct device *dev, |
| 153 | struct device_attribute *attr, char *buf) |
| 154 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 155 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 156 | |
| 157 | return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); |
| 158 | |
| 159 | } |
| 160 | static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); |
| 161 | |
| 162 | static ssize_t mtd_size_show(struct device *dev, |
| 163 | struct device_attribute *attr, char *buf) |
| 164 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 165 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 166 | |
| 167 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
| 168 | (unsigned long long)mtd->size); |
| 169 | |
| 170 | } |
| 171 | static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); |
| 172 | |
| 173 | static ssize_t mtd_erasesize_show(struct device *dev, |
| 174 | struct device_attribute *attr, char *buf) |
| 175 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 176 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 177 | |
| 178 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); |
| 179 | |
| 180 | } |
| 181 | static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); |
| 182 | |
| 183 | static ssize_t mtd_writesize_show(struct device *dev, |
| 184 | struct device_attribute *attr, char *buf) |
| 185 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 186 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 187 | |
| 188 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); |
| 189 | |
| 190 | } |
| 191 | static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); |
| 192 | |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 193 | static ssize_t mtd_subpagesize_show(struct device *dev, |
| 194 | struct device_attribute *attr, char *buf) |
| 195 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 196 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 197 | unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 198 | |
| 199 | return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); |
| 200 | |
| 201 | } |
| 202 | static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); |
| 203 | |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 204 | static ssize_t mtd_oobsize_show(struct device *dev, |
| 205 | struct device_attribute *attr, char *buf) |
| 206 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 207 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 208 | |
| 209 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); |
| 210 | |
| 211 | } |
| 212 | static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); |
| 213 | |
Xiaolei Li | 7cc9aa6 | 2018-04-02 16:20:10 +0800 | [diff] [blame] | 214 | static ssize_t mtd_oobavail_show(struct device *dev, |
| 215 | struct device_attribute *attr, char *buf) |
| 216 | { |
| 217 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 218 | |
| 219 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->oobavail); |
| 220 | } |
| 221 | static DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL); |
| 222 | |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 223 | static ssize_t mtd_numeraseregions_show(struct device *dev, |
| 224 | struct device_attribute *attr, char *buf) |
| 225 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 226 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 227 | |
| 228 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); |
| 229 | |
| 230 | } |
| 231 | static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, |
| 232 | NULL); |
| 233 | |
| 234 | static ssize_t mtd_name_show(struct device *dev, |
| 235 | struct device_attribute *attr, char *buf) |
| 236 | { |
Artem Bityutskiy | d5de20a | 2011-12-29 18:00:29 +0200 | [diff] [blame] | 237 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 238 | |
| 239 | return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); |
| 240 | |
| 241 | } |
| 242 | static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 243 | |
Mike Dunn | a9b672e | 2012-04-25 12:06:07 -0700 | [diff] [blame] | 244 | static ssize_t mtd_ecc_strength_show(struct device *dev, |
| 245 | struct device_attribute *attr, char *buf) |
| 246 | { |
| 247 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 248 | |
| 249 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); |
| 250 | } |
| 251 | static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); |
| 252 | |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 253 | static ssize_t mtd_bitflip_threshold_show(struct device *dev, |
| 254 | struct device_attribute *attr, |
| 255 | char *buf) |
| 256 | { |
| 257 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 258 | |
| 259 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); |
| 260 | } |
| 261 | |
| 262 | static ssize_t mtd_bitflip_threshold_store(struct device *dev, |
| 263 | struct device_attribute *attr, |
| 264 | const char *buf, size_t count) |
| 265 | { |
| 266 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 267 | unsigned int bitflip_threshold; |
| 268 | int retval; |
| 269 | |
| 270 | retval = kstrtouint(buf, 0, &bitflip_threshold); |
| 271 | if (retval) |
| 272 | return retval; |
| 273 | |
| 274 | mtd->bitflip_threshold = bitflip_threshold; |
| 275 | return count; |
| 276 | } |
| 277 | static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, |
| 278 | mtd_bitflip_threshold_show, |
| 279 | mtd_bitflip_threshold_store); |
| 280 | |
Huang Shijie | bf977e3 | 2013-08-16 10:10:05 +0800 | [diff] [blame] | 281 | static ssize_t mtd_ecc_step_size_show(struct device *dev, |
| 282 | struct device_attribute *attr, char *buf) |
| 283 | { |
| 284 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 285 | |
| 286 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); |
| 287 | |
| 288 | } |
| 289 | static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); |
| 290 | |
Ezequiel Garcia | 990a3af | 2014-06-24 10:55:50 -0300 | [diff] [blame] | 291 | static ssize_t mtd_ecc_stats_corrected_show(struct device *dev, |
| 292 | struct device_attribute *attr, char *buf) |
| 293 | { |
| 294 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 295 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 296 | |
| 297 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected); |
| 298 | } |
| 299 | static DEVICE_ATTR(corrected_bits, S_IRUGO, |
| 300 | mtd_ecc_stats_corrected_show, NULL); |
| 301 | |
| 302 | static ssize_t mtd_ecc_stats_errors_show(struct device *dev, |
| 303 | struct device_attribute *attr, char *buf) |
| 304 | { |
| 305 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 306 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 307 | |
| 308 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed); |
| 309 | } |
| 310 | static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL); |
| 311 | |
| 312 | static ssize_t mtd_badblocks_show(struct device *dev, |
| 313 | struct device_attribute *attr, char *buf) |
| 314 | { |
| 315 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 316 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 317 | |
| 318 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks); |
| 319 | } |
| 320 | static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL); |
| 321 | |
| 322 | static ssize_t mtd_bbtblocks_show(struct device *dev, |
| 323 | struct device_attribute *attr, char *buf) |
| 324 | { |
| 325 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 326 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; |
| 327 | |
| 328 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks); |
| 329 | } |
| 330 | static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL); |
| 331 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 332 | static struct attribute *mtd_attrs[] = { |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 333 | &dev_attr_type.attr, |
| 334 | &dev_attr_flags.attr, |
| 335 | &dev_attr_size.attr, |
| 336 | &dev_attr_erasesize.attr, |
| 337 | &dev_attr_writesize.attr, |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 338 | &dev_attr_subpagesize.attr, |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 339 | &dev_attr_oobsize.attr, |
Xiaolei Li | 7cc9aa6 | 2018-04-02 16:20:10 +0800 | [diff] [blame] | 340 | &dev_attr_oobavail.attr, |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 341 | &dev_attr_numeraseregions.attr, |
| 342 | &dev_attr_name.attr, |
Mike Dunn | a9b672e | 2012-04-25 12:06:07 -0700 | [diff] [blame] | 343 | &dev_attr_ecc_strength.attr, |
Huang Shijie | bf977e3 | 2013-08-16 10:10:05 +0800 | [diff] [blame] | 344 | &dev_attr_ecc_step_size.attr, |
Ezequiel Garcia | 990a3af | 2014-06-24 10:55:50 -0300 | [diff] [blame] | 345 | &dev_attr_corrected_bits.attr, |
| 346 | &dev_attr_ecc_failures.attr, |
| 347 | &dev_attr_bad_blocks.attr, |
| 348 | &dev_attr_bbt_blocks.attr, |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 349 | &dev_attr_bitflip_threshold.attr, |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 350 | NULL, |
| 351 | }; |
Axel Lin | 54c738f | 2013-12-02 10:12:25 +0800 | [diff] [blame] | 352 | ATTRIBUTE_GROUPS(mtd); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 353 | |
Bhumika Goyal | 75864b3 | 2017-08-19 13:52:17 +0530 | [diff] [blame] | 354 | static const struct device_type mtd_devtype = { |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 355 | .name = "mtd", |
| 356 | .groups = mtd_groups, |
| 357 | .release = mtd_release, |
| 358 | }; |
| 359 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 360 | #ifndef CONFIG_MMU |
| 361 | unsigned mtd_mmap_capabilities(struct mtd_info *mtd) |
| 362 | { |
| 363 | switch (mtd->type) { |
| 364 | case MTD_RAM: |
| 365 | return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | |
| 366 | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 367 | case MTD_ROM: |
| 368 | return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | |
| 369 | NOMMU_MAP_READ; |
| 370 | default: |
| 371 | return NOMMU_MAP_COPY; |
| 372 | } |
| 373 | } |
Arnd Bergmann | 706a4e5 | 2015-01-28 11:09:20 -0700 | [diff] [blame] | 374 | EXPORT_SYMBOL_GPL(mtd_mmap_capabilities); |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 375 | #endif |
| 376 | |
Brian Norris | 3efe41b | 2014-11-26 01:01:08 -0800 | [diff] [blame] | 377 | static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state, |
| 378 | void *cmd) |
| 379 | { |
| 380 | struct mtd_info *mtd; |
| 381 | |
| 382 | mtd = container_of(n, struct mtd_info, reboot_notifier); |
| 383 | mtd->_reboot(mtd); |
| 384 | |
| 385 | return NOTIFY_DONE; |
| 386 | } |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | /** |
Boris Brezillon | 477b022 | 2015-11-16 15:53:13 +0100 | [diff] [blame] | 389 | * mtd_wunit_to_pairing_info - get pairing information of a wunit |
| 390 | * @mtd: pointer to new MTD device info structure |
| 391 | * @wunit: write unit we are interested in |
| 392 | * @info: returned pairing information |
| 393 | * |
| 394 | * Retrieve pairing information associated to the wunit. |
| 395 | * This is mainly useful when dealing with MLC/TLC NANDs where pages can be |
| 396 | * paired together, and where programming a page may influence the page it is |
| 397 | * paired with. |
| 398 | * The notion of page is replaced by the term wunit (write-unit) to stay |
| 399 | * consistent with the ->writesize field. |
| 400 | * |
| 401 | * The @wunit argument can be extracted from an absolute offset using |
| 402 | * mtd_offset_to_wunit(). @info is filled with the pairing information attached |
| 403 | * to @wunit. |
| 404 | * |
| 405 | * From the pairing info the MTD user can find all the wunits paired with |
| 406 | * @wunit using the following loop: |
| 407 | * |
| 408 | * for (i = 0; i < mtd_pairing_groups(mtd); i++) { |
| 409 | * info.pair = i; |
| 410 | * mtd_pairing_info_to_wunit(mtd, &info); |
| 411 | * ... |
| 412 | * } |
| 413 | */ |
| 414 | int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit, |
| 415 | struct mtd_pairing_info *info) |
| 416 | { |
| 417 | int npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd); |
| 418 | |
| 419 | if (wunit < 0 || wunit >= npairs) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | if (mtd->pairing && mtd->pairing->get_info) |
| 423 | return mtd->pairing->get_info(mtd, wunit, info); |
| 424 | |
| 425 | info->group = 0; |
| 426 | info->pair = wunit; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info); |
| 431 | |
| 432 | /** |
Xiaolei Li | c77a931 | 2018-03-29 09:34:58 +0800 | [diff] [blame] | 433 | * mtd_pairing_info_to_wunit - get wunit from pairing information |
Boris Brezillon | 477b022 | 2015-11-16 15:53:13 +0100 | [diff] [blame] | 434 | * @mtd: pointer to new MTD device info structure |
| 435 | * @info: pairing information struct |
| 436 | * |
| 437 | * Returns a positive number representing the wunit associated to the info |
| 438 | * struct, or a negative error code. |
| 439 | * |
| 440 | * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to |
| 441 | * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info() |
| 442 | * doc). |
| 443 | * |
| 444 | * It can also be used to only program the first page of each pair (i.e. |
| 445 | * page attached to group 0), which allows one to use an MLC NAND in |
| 446 | * software-emulated SLC mode: |
| 447 | * |
| 448 | * info.group = 0; |
| 449 | * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd); |
| 450 | * for (info.pair = 0; info.pair < npairs; info.pair++) { |
| 451 | * wunit = mtd_pairing_info_to_wunit(mtd, &info); |
| 452 | * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit), |
| 453 | * mtd->writesize, &retlen, buf + (i * mtd->writesize)); |
| 454 | * } |
| 455 | */ |
| 456 | int mtd_pairing_info_to_wunit(struct mtd_info *mtd, |
| 457 | const struct mtd_pairing_info *info) |
| 458 | { |
| 459 | int ngroups = mtd_pairing_groups(mtd); |
| 460 | int npairs = mtd_wunit_per_eb(mtd) / ngroups; |
| 461 | |
| 462 | if (!info || info->pair < 0 || info->pair >= npairs || |
| 463 | info->group < 0 || info->group >= ngroups) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | if (mtd->pairing && mtd->pairing->get_wunit) |
| 467 | return mtd->pairing->get_wunit(mtd, info); |
| 468 | |
| 469 | return info->pair; |
| 470 | } |
| 471 | EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit); |
| 472 | |
| 473 | /** |
| 474 | * mtd_pairing_groups - get the number of pairing groups |
| 475 | * @mtd: pointer to new MTD device info structure |
| 476 | * |
| 477 | * Returns the number of pairing groups. |
| 478 | * |
| 479 | * This number is usually equal to the number of bits exposed by a single |
| 480 | * cell, and can be used in conjunction with mtd_pairing_info_to_wunit() |
| 481 | * to iterate over all pages of a given pair. |
| 482 | */ |
| 483 | int mtd_pairing_groups(struct mtd_info *mtd) |
| 484 | { |
| 485 | if (!mtd->pairing || !mtd->pairing->ngroups) |
| 486 | return 1; |
| 487 | |
| 488 | return mtd->pairing->ngroups; |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(mtd_pairing_groups); |
| 491 | |
Alban Bedel | c4dfa25 | 2018-11-13 15:01:10 +0100 | [diff] [blame] | 492 | static int mtd_nvmem_reg_read(void *priv, unsigned int offset, |
| 493 | void *val, size_t bytes) |
| 494 | { |
| 495 | struct mtd_info *mtd = priv; |
| 496 | size_t retlen; |
| 497 | int err; |
| 498 | |
| 499 | err = mtd_read(mtd, offset, bytes, &retlen, val); |
| 500 | if (err && err != -EUCLEAN) |
| 501 | return err; |
| 502 | |
| 503 | return retlen == bytes ? 0 : -EIO; |
| 504 | } |
| 505 | |
| 506 | static int mtd_nvmem_add(struct mtd_info *mtd) |
| 507 | { |
| 508 | struct nvmem_config config = {}; |
| 509 | |
| 510 | config.dev = &mtd->dev; |
| 511 | config.name = mtd->name; |
| 512 | config.owner = THIS_MODULE; |
| 513 | config.reg_read = mtd_nvmem_reg_read; |
| 514 | config.size = mtd->size; |
| 515 | config.word_size = 1; |
| 516 | config.stride = 1; |
| 517 | config.read_only = true; |
| 518 | config.root_only = true; |
| 519 | config.no_of_node = true; |
| 520 | config.priv = mtd; |
| 521 | |
| 522 | mtd->nvmem = nvmem_register(&config); |
| 523 | if (IS_ERR(mtd->nvmem)) { |
| 524 | /* Just ignore if there is no NVMEM support in the kernel */ |
| 525 | if (PTR_ERR(mtd->nvmem) == -ENOSYS) { |
| 526 | mtd->nvmem = NULL; |
| 527 | } else { |
| 528 | dev_err(&mtd->dev, "Failed to register NVMEM device\n"); |
| 529 | return PTR_ERR(mtd->nvmem); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
Mario Rugiero | e8e3edb | 2017-05-29 08:38:41 -0300 | [diff] [blame] | 536 | static struct dentry *dfs_dir_mtd; |
| 537 | |
Boris Brezillon | 477b022 | 2015-11-16 15:53:13 +0100 | [diff] [blame] | 538 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | * add_mtd_device - register an MTD device |
| 540 | * @mtd: pointer to new MTD device info structure |
| 541 | * |
| 542 | * Add a device to the list of MTD devices present in the system, and |
| 543 | * notify each currently active MTD 'user' of its arrival. Returns |
Brian Norris | 57dd990 | 2015-06-01 16:17:18 -0700 | [diff] [blame] | 544 | * zero on success or non-zero on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | */ |
| 546 | |
| 547 | int add_mtd_device(struct mtd_info *mtd) |
| 548 | { |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 549 | struct mtd_notifier *not; |
| 550 | int i, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Brian Norris | be0dbff | 2015-06-01 16:17:20 -0700 | [diff] [blame] | 552 | /* |
| 553 | * May occur, for instance, on buggy drivers which call |
| 554 | * mtd_device_parse_register() multiple times on the same master MTD, |
| 555 | * especially with CONFIG_MTD_PARTITIONED_MASTER=y. |
| 556 | */ |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 557 | if (WARN_ONCE(mtd->dev.type, "MTD already registered\n")) |
Brian Norris | be0dbff | 2015-06-01 16:17:20 -0700 | [diff] [blame] | 558 | return -EEXIST; |
| 559 | |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 560 | BUG_ON(mtd->writesize == 0); |
Boris Brezillon | 33f45c4 | 2017-12-15 13:39:51 +0100 | [diff] [blame] | 561 | |
| 562 | if (WARN_ON((!mtd->erasesize || !mtd->_erase) && |
| 563 | !(mtd->flags & MTD_NO_ERASE))) |
| 564 | return -EINVAL; |
| 565 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 566 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Tejun Heo | 589e9c4 | 2013-02-27 17:04:33 -0800 | [diff] [blame] | 568 | i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); |
Brian Norris | 57dd990 | 2015-06-01 16:17:18 -0700 | [diff] [blame] | 569 | if (i < 0) { |
| 570 | error = i; |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 571 | goto fail_locked; |
Brian Norris | 57dd990 | 2015-06-01 16:17:18 -0700 | [diff] [blame] | 572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 574 | mtd->index = i; |
| 575 | mtd->usecount = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 576 | |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 577 | /* default value if not set by driver */ |
| 578 | if (mtd->bitflip_threshold == 0) |
| 579 | mtd->bitflip_threshold = mtd->ecc_strength; |
| 580 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 581 | if (is_power_of_2(mtd->erasesize)) |
| 582 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; |
| 583 | else |
| 584 | mtd->erasesize_shift = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 585 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 586 | if (is_power_of_2(mtd->writesize)) |
| 587 | mtd->writesize_shift = ffs(mtd->writesize) - 1; |
| 588 | else |
| 589 | mtd->writesize_shift = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 590 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 591 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; |
| 592 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; |
Håvard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 593 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 594 | /* Some chips always power up locked. Unlock them now */ |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 595 | if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { |
| 596 | error = mtd_unlock(mtd, 0, mtd->size); |
| 597 | if (error && error != -EOPNOTSUPP) |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 598 | printk(KERN_WARNING |
| 599 | "%s: unlock failed, writes may not work\n", |
| 600 | mtd->name); |
Brian Norris | 57dd990 | 2015-06-01 16:17:18 -0700 | [diff] [blame] | 601 | /* Ignore unlock failures? */ |
| 602 | error = 0; |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 603 | } |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 604 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 605 | /* Caller should have set dev.parent to match the |
Frans Klaver | 260e89a | 2015-06-10 22:38:15 +0200 | [diff] [blame] | 606 | * physical device, if appropriate. |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 607 | */ |
| 608 | mtd->dev.type = &mtd_devtype; |
| 609 | mtd->dev.class = &mtd_class; |
| 610 | mtd->dev.devt = MTD_DEVT(i); |
| 611 | dev_set_name(&mtd->dev, "mtd%d", i); |
| 612 | dev_set_drvdata(&mtd->dev, mtd); |
Brian Norris | 215a02f | 2015-11-11 16:26:04 -0800 | [diff] [blame] | 613 | of_node_get(mtd_get_of_node(mtd)); |
Brian Norris | 57dd990 | 2015-06-01 16:17:18 -0700 | [diff] [blame] | 614 | error = device_register(&mtd->dev); |
| 615 | if (error) |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 616 | goto fail_added; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 617 | |
Alban Bedel | c4dfa25 | 2018-11-13 15:01:10 +0100 | [diff] [blame] | 618 | /* Add the nvmem provider */ |
| 619 | error = mtd_nvmem_add(mtd); |
| 620 | if (error) |
| 621 | goto fail_nvmem_add; |
| 622 | |
Mario Rugiero | e8e3edb | 2017-05-29 08:38:41 -0300 | [diff] [blame] | 623 | if (!IS_ERR_OR_NULL(dfs_dir_mtd)) { |
| 624 | mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(&mtd->dev), dfs_dir_mtd); |
| 625 | if (IS_ERR_OR_NULL(mtd->dbg.dfs_dir)) { |
| 626 | pr_debug("mtd device %s won't show data in debugfs\n", |
| 627 | dev_name(&mtd->dev)); |
| 628 | } |
| 629 | } |
| 630 | |
Brian Norris | 5e47212 | 2014-07-21 19:06:47 -0700 | [diff] [blame] | 631 | device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, |
| 632 | "mtd%dro", i); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 633 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 634 | 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] | 635 | /* No need to get a refcount on the module containing |
| 636 | the notifier, since we hold the mtd_table_mutex */ |
| 637 | list_for_each_entry(not, &mtd_notifiers, list) |
| 638 | not->add(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 639 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 640 | mutex_unlock(&mtd_table_mutex); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 641 | /* We _know_ we aren't being removed, because |
| 642 | our caller is still holding us here. So none |
| 643 | of this try_ nonsense, and no bitching about it |
| 644 | either. :) */ |
| 645 | __module_get(THIS_MODULE); |
| 646 | return 0; |
| 647 | |
Alban Bedel | c4dfa25 | 2018-11-13 15:01:10 +0100 | [diff] [blame] | 648 | fail_nvmem_add: |
| 649 | device_unregister(&mtd->dev); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 650 | fail_added: |
Brian Norris | 215a02f | 2015-11-11 16:26:04 -0800 | [diff] [blame] | 651 | of_node_put(mtd_get_of_node(mtd)); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 652 | idr_remove(&mtd_idr, i); |
| 653 | fail_locked: |
| 654 | mutex_unlock(&mtd_table_mutex); |
Brian Norris | 57dd990 | 2015-06-01 16:17:18 -0700 | [diff] [blame] | 655 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /** |
| 659 | * del_mtd_device - unregister an MTD device |
| 660 | * @mtd: pointer to MTD device info structure |
| 661 | * |
| 662 | * Remove a device from the list of MTD devices present in the system, |
| 663 | * and notify each currently active MTD 'user' of its departure. |
| 664 | * Returns zero on success or 1 on failure, which currently will happen |
| 665 | * if the requested device does not appear to be present in the list. |
| 666 | */ |
| 667 | |
Jamie Iles | eea72d5 | 2011-05-23 10:23:42 +0100 | [diff] [blame] | 668 | int del_mtd_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | { |
| 670 | int ret; |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 671 | struct mtd_notifier *not; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 672 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 673 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Mario Rugiero | e8e3edb | 2017-05-29 08:38:41 -0300 | [diff] [blame] | 675 | debugfs_remove_recursive(mtd->dbg.dfs_dir); |
| 676 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 677 | if (idr_find(&mtd_idr, mtd->index) != mtd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | ret = -ENODEV; |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 679 | goto out_error; |
| 680 | } |
| 681 | |
| 682 | /* No need to get a refcount on the module containing |
| 683 | the notifier, since we hold the mtd_table_mutex */ |
| 684 | list_for_each_entry(not, &mtd_notifiers, list) |
| 685 | not->remove(mtd); |
| 686 | |
| 687 | if (mtd->usecount) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 688 | 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] | 689 | mtd->index, mtd->name, mtd->usecount); |
| 690 | ret = -EBUSY; |
| 691 | } else { |
Alban Bedel | c4dfa25 | 2018-11-13 15:01:10 +0100 | [diff] [blame] | 692 | /* Try to remove the NVMEM provider */ |
| 693 | if (mtd->nvmem) |
| 694 | nvmem_unregister(mtd->nvmem); |
| 695 | |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 696 | device_unregister(&mtd->dev); |
| 697 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 698 | idr_remove(&mtd_idr, mtd->index); |
Brian Norris | 215a02f | 2015-11-11 16:26:04 -0800 | [diff] [blame] | 699 | of_node_put(mtd_get_of_node(mtd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | module_put(THIS_MODULE); |
| 702 | ret = 0; |
| 703 | } |
| 704 | |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 705 | out_error: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 706 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return ret; |
| 708 | } |
| 709 | |
Brian Norris | 472b444 | 2015-12-11 15:58:01 -0800 | [diff] [blame] | 710 | /* |
| 711 | * Set a few defaults based on the parent devices, if not provided by the |
| 712 | * driver |
| 713 | */ |
| 714 | static void mtd_set_dev_defaults(struct mtd_info *mtd) |
| 715 | { |
| 716 | if (mtd->dev.parent) { |
| 717 | if (!mtd->owner && mtd->dev.parent->driver) |
| 718 | mtd->owner = mtd->dev.parent->driver->owner; |
| 719 | if (!mtd->name) |
| 720 | mtd->name = dev_name(mtd->dev.parent); |
| 721 | } else { |
| 722 | pr_debug("mtd device won't show a device symlink in sysfs\n"); |
| 723 | } |
Rafał Miłecki | 1186af4 | 2018-11-20 09:55:45 +0100 | [diff] [blame] | 724 | |
| 725 | mtd->orig_flags = mtd->flags; |
Brian Norris | 472b444 | 2015-12-11 15:58:01 -0800 | [diff] [blame] | 726 | } |
Dan Ehrenberg | 727dc61 | 2015-04-02 15:15:10 -0700 | [diff] [blame] | 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /** |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 729 | * mtd_device_parse_register - parse partitions and register an MTD device. |
| 730 | * |
| 731 | * @mtd: the MTD device to register |
| 732 | * @types: the list of MTD partition probes to try, see |
| 733 | * 'parse_mtd_partitions()' for more information |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 734 | * @parser_data: MTD partition parser-specific data |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 735 | * @parts: fallback partition information to register, if parsing fails; |
| 736 | * only valid if %nr_parts > %0 |
| 737 | * @nr_parts: the number of partitions in parts, if zero then the full |
| 738 | * MTD device is registered if no partition info is found |
| 739 | * |
| 740 | * This function aggregates MTD partitions parsing (done by |
| 741 | * 'parse_mtd_partitions()') and MTD device and partitions registering. It |
| 742 | * basically follows the most common pattern found in many MTD drivers: |
| 743 | * |
Rafał Miłecki | 55a999a | 2018-03-27 15:36:47 +0200 | [diff] [blame] | 744 | * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is |
| 745 | * registered first. |
| 746 | * * Then It tries to probe partitions on MTD device @mtd using parsers |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 747 | * specified in @types (if @types is %NULL, then the default list of parsers |
| 748 | * is used, see 'parse_mtd_partitions()' for more information). If none are |
| 749 | * found this functions tries to fallback to information specified in |
| 750 | * @parts/@nr_parts. |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 751 | * * If no partitions were found this function just registers the MTD device |
| 752 | * @mtd and exits. |
| 753 | * |
| 754 | * Returns zero in case of success and a negative error code in case of failure. |
| 755 | */ |
Artem Bityutskiy | 26a4734 | 2013-03-11 15:38:48 +0200 | [diff] [blame] | 756 | 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] | 757 | struct mtd_part_parser_data *parser_data, |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 758 | const struct mtd_partition *parts, |
| 759 | int nr_parts) |
| 760 | { |
Dan Ehrenberg | 727dc61 | 2015-04-02 15:15:10 -0700 | [diff] [blame] | 761 | int ret; |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 762 | |
Brian Norris | 472b444 | 2015-12-11 15:58:01 -0800 | [diff] [blame] | 763 | mtd_set_dev_defaults(mtd); |
| 764 | |
Rafał Miłecki | 2c77c57 | 2018-01-16 16:45:41 +0100 | [diff] [blame] | 765 | if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) { |
| 766 | ret = add_mtd_device(mtd); |
| 767 | if (ret) |
| 768 | return ret; |
| 769 | } |
| 770 | |
Rafał Miłecki | 0dbe4ea | 2018-01-16 16:45:42 +0100 | [diff] [blame] | 771 | /* Prefer parsed partitions over driver-provided fallback */ |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 772 | ret = parse_mtd_partitions(mtd, types, parser_data); |
| 773 | if (ret > 0) |
| 774 | ret = 0; |
| 775 | else if (nr_parts) |
Rafał Miłecki | 0dbe4ea | 2018-01-16 16:45:42 +0100 | [diff] [blame] | 776 | ret = add_mtd_partitions(mtd, parts, nr_parts); |
| 777 | else if (!device_is_registered(&mtd->dev)) |
| 778 | ret = add_mtd_device(mtd); |
| 779 | else |
| 780 | ret = 0; |
| 781 | |
Brian Norris | 3e00ed0 | 2015-06-01 16:17:19 -0700 | [diff] [blame] | 782 | if (ret) |
| 783 | goto out; |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 784 | |
Niklas Cassel | e1dd864 | 2015-02-01 02:08:50 +0100 | [diff] [blame] | 785 | /* |
| 786 | * FIXME: some drivers unfortunately call this function more than once. |
| 787 | * So we have to check if we've already assigned the reboot notifier. |
| 788 | * |
| 789 | * Generally, we can make multiple calls work for most cases, but it |
| 790 | * does cause problems with parse_mtd_partitions() above (e.g., |
| 791 | * cmdlineparts will register partitions more than once). |
| 792 | */ |
Brian Norris | f8479dd | 2015-11-03 17:01:53 -0800 | [diff] [blame] | 793 | WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, |
| 794 | "MTD already registered\n"); |
Niklas Cassel | e1dd864 | 2015-02-01 02:08:50 +0100 | [diff] [blame] | 795 | if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { |
Brian Norris | 3efe41b | 2014-11-26 01:01:08 -0800 | [diff] [blame] | 796 | mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; |
| 797 | register_reboot_notifier(&mtd->reboot_notifier); |
| 798 | } |
| 799 | |
Brian Norris | 3e00ed0 | 2015-06-01 16:17:19 -0700 | [diff] [blame] | 800 | out: |
Rafał Miłecki | 2c77c57 | 2018-01-16 16:45:41 +0100 | [diff] [blame] | 801 | if (ret && device_is_registered(&mtd->dev)) |
| 802 | del_mtd_device(mtd); |
| 803 | |
Dan Ehrenberg | 727dc61 | 2015-04-02 15:15:10 -0700 | [diff] [blame] | 804 | return ret; |
Dmitry Eremin-Solenikov | 1c4c215c | 2011-03-25 22:26:25 +0300 | [diff] [blame] | 805 | } |
| 806 | EXPORT_SYMBOL_GPL(mtd_device_parse_register); |
| 807 | |
| 808 | /** |
Jamie Iles | f5671ab | 2011-05-23 17:15:46 +0100 | [diff] [blame] | 809 | * mtd_device_unregister - unregister an existing MTD device. |
| 810 | * |
| 811 | * @master: the MTD device to unregister. This will unregister both the master |
| 812 | * and any partitions if registered. |
| 813 | */ |
| 814 | int mtd_device_unregister(struct mtd_info *master) |
| 815 | { |
| 816 | int err; |
| 817 | |
Brian Norris | 3efe41b | 2014-11-26 01:01:08 -0800 | [diff] [blame] | 818 | if (master->_reboot) |
| 819 | unregister_reboot_notifier(&master->reboot_notifier); |
| 820 | |
Jamie Iles | f5671ab | 2011-05-23 17:15:46 +0100 | [diff] [blame] | 821 | err = del_mtd_partitions(master); |
| 822 | if (err) |
| 823 | return err; |
| 824 | |
| 825 | if (!device_is_registered(&master->dev)) |
| 826 | return 0; |
| 827 | |
| 828 | return del_mtd_device(master); |
| 829 | } |
| 830 | EXPORT_SYMBOL_GPL(mtd_device_unregister); |
| 831 | |
| 832 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | * register_mtd_user - register a 'user' of MTD devices. |
| 834 | * @new: pointer to notifier info structure |
| 835 | * |
| 836 | * Registers a pair of callbacks function to be called upon addition |
| 837 | * or removal of MTD devices. Causes the 'add' callback to be immediately |
| 838 | * invoked for each MTD device currently present in the system. |
| 839 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | void register_mtd_user (struct mtd_notifier *new) |
| 841 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 842 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 844 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
| 846 | list_add(&new->list, &mtd_notifiers); |
| 847 | |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 848 | __module_get(THIS_MODULE); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 849 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 850 | mtd_for_each_device(mtd) |
| 851 | new->add(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 853 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 855 | EXPORT_SYMBOL_GPL(register_mtd_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | /** |
Artem B. Bityuckiy | 4945079 | 2005-02-18 14:34:54 +0000 | [diff] [blame] | 858 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
| 859 | * @old: pointer to notifier info structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | * |
| 861 | * Removes a callback function pair from the list of 'users' to be |
| 862 | * notified upon addition or removal of MTD devices. Causes the |
| 863 | * 'remove' callback to be immediately invoked for each MTD device |
| 864 | * currently present in the system. |
| 865 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | int unregister_mtd_user (struct mtd_notifier *old) |
| 867 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 868 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 870 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | module_put(THIS_MODULE); |
| 873 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 874 | mtd_for_each_device(mtd) |
| 875 | old->remove(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | list_del(&old->list); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 878 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 881 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | /** |
| 884 | * get_mtd_device - obtain a validated handle for an MTD device |
| 885 | * @mtd: last known address of the required MTD device |
| 886 | * @num: internal device number of the required MTD device |
| 887 | * |
| 888 | * Given a number and NULL address, return the num'th entry in the device |
| 889 | * table, if any. Given an address and num == -1, search the device table |
| 890 | * 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] | 891 | * both, return the num'th driver only if its address matches. Return |
| 892 | * error code if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 895 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 896 | struct mtd_info *ret = NULL, *other; |
| 897 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 899 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
| 901 | if (num == -1) { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 902 | mtd_for_each_device(other) { |
| 903 | if (other == mtd) { |
| 904 | ret = mtd; |
| 905 | break; |
| 906 | } |
| 907 | } |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 908 | } else if (num >= 0) { |
| 909 | ret = idr_find(&mtd_idr, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (mtd && mtd != ret) |
| 911 | ret = NULL; |
| 912 | } |
| 913 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 914 | if (!ret) { |
| 915 | ret = ERR_PTR(err); |
| 916 | goto out; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 919 | err = __get_mtd_device(ret); |
| 920 | if (err) |
| 921 | ret = ERR_PTR(err); |
| 922 | out: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 923 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return ret; |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 925 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 926 | EXPORT_SYMBOL_GPL(get_mtd_device); |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 927 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 928 | |
| 929 | int __get_mtd_device(struct mtd_info *mtd) |
| 930 | { |
| 931 | int err; |
| 932 | |
| 933 | if (!try_module_get(mtd->owner)) |
| 934 | return -ENODEV; |
| 935 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 936 | if (mtd->_get_device) { |
| 937 | err = mtd->_get_device(mtd); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 938 | |
| 939 | if (err) { |
| 940 | module_put(mtd->owner); |
| 941 | return err; |
| 942 | } |
| 943 | } |
| 944 | mtd->usecount++; |
| 945 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 947 | EXPORT_SYMBOL_GPL(__get_mtd_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 949 | /** |
| 950 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 951 | * device name |
| 952 | * @name: MTD device name to open |
| 953 | * |
| 954 | * This function returns MTD device description structure in case of |
| 955 | * success and an error code in case of failure. |
| 956 | */ |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 957 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 958 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 959 | int err = -ENODEV; |
| 960 | struct mtd_info *mtd = NULL, *other; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 961 | |
| 962 | mutex_lock(&mtd_table_mutex); |
| 963 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 964 | mtd_for_each_device(other) { |
| 965 | if (!strcmp(name, other->name)) { |
| 966 | mtd = other; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 967 | break; |
| 968 | } |
| 969 | } |
| 970 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 971 | if (!mtd) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 972 | goto out_unlock; |
| 973 | |
Wanlong Gao | 52534f2 | 2011-05-17 22:36:18 +0800 | [diff] [blame] | 974 | err = __get_mtd_device(mtd); |
| 975 | if (err) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 976 | goto out_unlock; |
| 977 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 978 | mutex_unlock(&mtd_table_mutex); |
| 979 | return mtd; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 980 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 981 | out_unlock: |
| 982 | mutex_unlock(&mtd_table_mutex); |
| 983 | return ERR_PTR(err); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 984 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 985 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | void put_mtd_device(struct mtd_info *mtd) |
| 988 | { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 989 | mutex_lock(&mtd_table_mutex); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 990 | __put_mtd_device(mtd); |
| 991 | mutex_unlock(&mtd_table_mutex); |
| 992 | |
| 993 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 994 | EXPORT_SYMBOL_GPL(put_mtd_device); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 995 | |
| 996 | void __put_mtd_device(struct mtd_info *mtd) |
| 997 | { |
| 998 | --mtd->usecount; |
| 999 | BUG_ON(mtd->usecount < 0); |
| 1000 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 1001 | if (mtd->_put_device) |
| 1002 | mtd->_put_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | module_put(mtd->owner); |
| 1005 | } |
Artem Bityutskiy | 33c87b4 | 2011-12-30 16:11:14 +0200 | [diff] [blame] | 1006 | EXPORT_SYMBOL_GPL(__put_mtd_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1008 | /* |
Boris Brezillon | 884cfd9 | 2018-02-12 22:03:09 +0100 | [diff] [blame] | 1009 | * Erase is an synchronous operation. Device drivers are epected to return a |
| 1010 | * negative error code if the operation failed and update instr->fail_addr |
| 1011 | * to point the portion that was not properly erased. |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1012 | */ |
| 1013 | int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 1014 | { |
Boris Brezillon | c585da9 | 2018-02-12 22:03:07 +0100 | [diff] [blame] | 1015 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
| 1016 | |
Boris Brezillon | e6e620f | 2018-01-22 10:38:01 +0100 | [diff] [blame] | 1017 | if (!mtd->erasesize || !mtd->_erase) |
| 1018 | return -ENOTSUPP; |
| 1019 | |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1020 | if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1021 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1022 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1023 | return -EROFS; |
Boris Brezillon | e6e620f | 2018-01-22 10:38:01 +0100 | [diff] [blame] | 1024 | |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 1025 | if (!instr->len) |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1026 | return 0; |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 1027 | |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 1028 | ledtrig_mtd_activity(); |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1029 | return mtd->_erase(mtd, instr); |
| 1030 | } |
| 1031 | EXPORT_SYMBOL_GPL(mtd_erase); |
| 1032 | |
| 1033 | /* |
| 1034 | * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. |
| 1035 | */ |
| 1036 | int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, |
| 1037 | void **virt, resource_size_t *phys) |
| 1038 | { |
| 1039 | *retlen = 0; |
Artem Bityutskiy | 0dd5235 | 2012-02-08 15:13:26 +0200 | [diff] [blame] | 1040 | *virt = NULL; |
| 1041 | if (phys) |
| 1042 | *phys = 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1043 | if (!mtd->_point) |
| 1044 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1045 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1046 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1047 | if (!len) |
| 1048 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1049 | return mtd->_point(mtd, from, len, retlen, virt, phys); |
| 1050 | } |
| 1051 | EXPORT_SYMBOL_GPL(mtd_point); |
| 1052 | |
| 1053 | /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ |
| 1054 | int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
| 1055 | { |
Boris Brezillon | b950424 | 2017-06-25 20:22:57 +0200 | [diff] [blame] | 1056 | if (!mtd->_unpoint) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1057 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1058 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1059 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1060 | if (!len) |
| 1061 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1062 | return mtd->_unpoint(mtd, from, len); |
| 1063 | } |
| 1064 | EXPORT_SYMBOL_GPL(mtd_unpoint); |
| 1065 | |
| 1066 | /* |
| 1067 | * Allow NOMMU mmap() to directly map the device (if not NULL) |
| 1068 | * - return the address to which the offset maps |
| 1069 | * - return -ENOSYS to indicate refusal to do the mapping |
| 1070 | */ |
| 1071 | unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, |
| 1072 | unsigned long offset, unsigned long flags) |
| 1073 | { |
Nicolas Pitre | 9eaa903 | 2017-10-30 14:48:32 -0400 | [diff] [blame] | 1074 | size_t retlen; |
| 1075 | void *virt; |
| 1076 | int ret; |
| 1077 | |
| 1078 | ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); |
| 1079 | if (ret) |
| 1080 | return ret; |
| 1081 | if (retlen != len) { |
| 1082 | mtd_unpoint(mtd, offset, retlen); |
| 1083 | return -ENOSYS; |
| 1084 | } |
| 1085 | return (unsigned long)virt; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1086 | } |
| 1087 | EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); |
| 1088 | |
| 1089 | int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, |
| 1090 | u_char *buf) |
| 1091 | { |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1092 | int ret_code; |
Artem Bityutskiy | 834247e | 2012-02-06 12:39:07 +0200 | [diff] [blame] | 1093 | *retlen = 0; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1094 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1095 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1096 | if (!len) |
| 1097 | return 0; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1098 | |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 1099 | ledtrig_mtd_activity(); |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1100 | /* |
| 1101 | * In the absence of an error, drivers return a non-negative integer |
| 1102 | * representing the maximum number of bitflips that were corrected on |
| 1103 | * any one ecc region (if applicable; zero otherwise). |
| 1104 | */ |
Boris Brezillon | 24ff129 | 2018-01-09 09:50:34 +0100 | [diff] [blame] | 1105 | if (mtd->_read) { |
| 1106 | ret_code = mtd->_read(mtd, from, len, retlen, buf); |
| 1107 | } else if (mtd->_read_oob) { |
| 1108 | struct mtd_oob_ops ops = { |
| 1109 | .len = len, |
| 1110 | .datbuf = buf, |
| 1111 | }; |
| 1112 | |
| 1113 | ret_code = mtd->_read_oob(mtd, from, &ops); |
| 1114 | *retlen = ops.retlen; |
| 1115 | } else { |
| 1116 | return -ENOTSUPP; |
| 1117 | } |
| 1118 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1119 | if (unlikely(ret_code < 0)) |
| 1120 | return ret_code; |
| 1121 | if (mtd->ecc_strength == 0) |
| 1122 | return 0; /* device lacks ecc */ |
| 1123 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1124 | } |
| 1125 | EXPORT_SYMBOL_GPL(mtd_read); |
| 1126 | |
| 1127 | int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, |
| 1128 | const u_char *buf) |
| 1129 | { |
| 1130 | *retlen = 0; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1131 | if (to < 0 || to >= mtd->size || len > mtd->size - to) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1132 | return -EINVAL; |
Boris Brezillon | 24ff129 | 2018-01-09 09:50:34 +0100 | [diff] [blame] | 1133 | if ((!mtd->_write && !mtd->_write_oob) || |
| 1134 | !(mtd->flags & MTD_WRITEABLE)) |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1135 | return -EROFS; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1136 | if (!len) |
| 1137 | return 0; |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 1138 | ledtrig_mtd_activity(); |
Boris Brezillon | 24ff129 | 2018-01-09 09:50:34 +0100 | [diff] [blame] | 1139 | |
| 1140 | if (!mtd->_write) { |
| 1141 | struct mtd_oob_ops ops = { |
| 1142 | .len = len, |
| 1143 | .datbuf = (u8 *)buf, |
| 1144 | }; |
| 1145 | int ret; |
| 1146 | |
| 1147 | ret = mtd->_write_oob(mtd, to, &ops); |
| 1148 | *retlen = ops.retlen; |
| 1149 | return ret; |
| 1150 | } |
| 1151 | |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1152 | return mtd->_write(mtd, to, len, retlen, buf); |
| 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(mtd_write); |
| 1155 | |
| 1156 | /* |
| 1157 | * In blackbox flight recorder like scenarios we want to make successful writes |
| 1158 | * in interrupt context. panic_write() is only intended to be called when its |
| 1159 | * known the kernel is about to panic and we need the write to succeed. Since |
| 1160 | * the kernel is not going to be running for much longer, this function can |
| 1161 | * break locks and delay to ensure the write succeeds (but not sleep). |
| 1162 | */ |
| 1163 | int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, |
| 1164 | const u_char *buf) |
| 1165 | { |
| 1166 | *retlen = 0; |
| 1167 | if (!mtd->_panic_write) |
| 1168 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1169 | if (to < 0 || to >= mtd->size || len > mtd->size - to) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1170 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1171 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1172 | return -EROFS; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1173 | if (!len) |
| 1174 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1175 | return mtd->_panic_write(mtd, to, len, retlen, buf); |
| 1176 | } |
| 1177 | EXPORT_SYMBOL_GPL(mtd_panic_write); |
| 1178 | |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1179 | static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, |
| 1180 | struct mtd_oob_ops *ops) |
| 1181 | { |
| 1182 | /* |
| 1183 | * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving |
| 1184 | * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in |
| 1185 | * this case. |
| 1186 | */ |
| 1187 | if (!ops->datbuf) |
| 1188 | ops->len = 0; |
| 1189 | |
| 1190 | if (!ops->oobbuf) |
| 1191 | ops->ooblen = 0; |
| 1192 | |
Miquel Raynal | d82c368 | 2017-12-18 08:26:28 +0100 | [diff] [blame] | 1193 | if (offs < 0 || offs + ops->len > mtd->size) |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1194 | return -EINVAL; |
| 1195 | |
| 1196 | if (ops->ooblen) { |
Miquel Raynal | 89f706d | 2018-11-18 21:18:31 +0100 | [diff] [blame] | 1197 | size_t maxooblen; |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1198 | |
| 1199 | if (ops->ooboffs >= mtd_oobavail(mtd, ops)) |
| 1200 | return -EINVAL; |
| 1201 | |
Miquel Raynal | 89f706d | 2018-11-18 21:18:31 +0100 | [diff] [blame] | 1202 | maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - |
| 1203 | mtd_div_by_ws(offs, mtd)) * |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1204 | mtd_oobavail(mtd, ops)) - ops->ooboffs; |
| 1205 | if (ops->ooblen > maxooblen) |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Brian Norris | d2d4848 | 2012-06-22 16:35:38 -0700 | [diff] [blame] | 1212 | int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) |
| 1213 | { |
Brian Norris | e47f685 | 2012-06-22 16:35:39 -0700 | [diff] [blame] | 1214 | int ret_code; |
Brian Norris | d2d4848 | 2012-06-22 16:35:38 -0700 | [diff] [blame] | 1215 | ops->retlen = ops->oobretlen = 0; |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 1216 | |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1217 | ret_code = mtd_check_oob_ops(mtd, from, ops); |
| 1218 | if (ret_code) |
| 1219 | return ret_code; |
| 1220 | |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 1221 | ledtrig_mtd_activity(); |
Miquel Raynal | 89fd23e | 2018-07-14 14:42:00 +0200 | [diff] [blame] | 1222 | |
| 1223 | /* Check the validity of a potential fallback on mtd->_read */ |
| 1224 | if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf)) |
| 1225 | return -EOPNOTSUPP; |
| 1226 | |
| 1227 | if (mtd->_read_oob) |
| 1228 | ret_code = mtd->_read_oob(mtd, from, ops); |
| 1229 | else |
| 1230 | ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen, |
| 1231 | ops->datbuf); |
| 1232 | |
Brian Norris | e47f685 | 2012-06-22 16:35:39 -0700 | [diff] [blame] | 1233 | /* |
| 1234 | * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics |
| 1235 | * similar to mtd->_read(), returning a non-negative integer |
| 1236 | * representing max bitflips. In other cases, mtd->_read_oob() may |
| 1237 | * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). |
| 1238 | */ |
Brian Norris | e47f685 | 2012-06-22 16:35:39 -0700 | [diff] [blame] | 1239 | if (unlikely(ret_code < 0)) |
| 1240 | return ret_code; |
| 1241 | if (mtd->ecc_strength == 0) |
| 1242 | return 0; /* device lacks ecc */ |
| 1243 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; |
Brian Norris | d2d4848 | 2012-06-22 16:35:38 -0700 | [diff] [blame] | 1244 | } |
| 1245 | EXPORT_SYMBOL_GPL(mtd_read_oob); |
| 1246 | |
Ezequiel Garcia | 0c034fe | 2016-04-12 17:46:39 -0300 | [diff] [blame] | 1247 | int mtd_write_oob(struct mtd_info *mtd, loff_t to, |
| 1248 | struct mtd_oob_ops *ops) |
| 1249 | { |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1250 | int ret; |
| 1251 | |
Ezequiel Garcia | 0c034fe | 2016-04-12 17:46:39 -0300 | [diff] [blame] | 1252 | ops->retlen = ops->oobretlen = 0; |
Miquel Raynal | 89fd23e | 2018-07-14 14:42:00 +0200 | [diff] [blame] | 1253 | |
Ezequiel Garcia | 0c034fe | 2016-04-12 17:46:39 -0300 | [diff] [blame] | 1254 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1255 | return -EROFS; |
Boris Brezillon | 5cdd929 | 2017-06-27 21:22:21 +0200 | [diff] [blame] | 1256 | |
| 1257 | ret = mtd_check_oob_ops(mtd, to, ops); |
| 1258 | if (ret) |
| 1259 | return ret; |
| 1260 | |
Ezequiel Garcia | fea728c | 2016-04-12 17:46:42 -0300 | [diff] [blame] | 1261 | ledtrig_mtd_activity(); |
Miquel Raynal | 89fd23e | 2018-07-14 14:42:00 +0200 | [diff] [blame] | 1262 | |
| 1263 | /* Check the validity of a potential fallback on mtd->_write */ |
| 1264 | if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf)) |
| 1265 | return -EOPNOTSUPP; |
| 1266 | |
| 1267 | if (mtd->_write_oob) |
| 1268 | return mtd->_write_oob(mtd, to, ops); |
| 1269 | else |
| 1270 | return mtd->_write(mtd, to, ops->len, &ops->retlen, |
| 1271 | ops->datbuf); |
Ezequiel Garcia | 0c034fe | 2016-04-12 17:46:39 -0300 | [diff] [blame] | 1272 | } |
| 1273 | EXPORT_SYMBOL_GPL(mtd_write_oob); |
| 1274 | |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1275 | /** |
| 1276 | * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section |
| 1277 | * @mtd: MTD device structure |
| 1278 | * @section: ECC section. Depending on the layout you may have all the ECC |
| 1279 | * bytes stored in a single contiguous section, or one section |
| 1280 | * per ECC chunk (and sometime several sections for a single ECC |
| 1281 | * ECC chunk) |
| 1282 | * @oobecc: OOB region struct filled with the appropriate ECC position |
| 1283 | * information |
| 1284 | * |
Masahiro Yamada | 7da0fff | 2016-12-14 09:31:01 +0900 | [diff] [blame] | 1285 | * This function returns ECC section information in the OOB area. If you want |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1286 | * to get all the ECC bytes information, then you should call |
| 1287 | * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE. |
| 1288 | * |
| 1289 | * Returns zero on success, a negative error code otherwise. |
| 1290 | */ |
| 1291 | int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 1292 | struct mtd_oob_region *oobecc) |
| 1293 | { |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1294 | memset(oobecc, 0, sizeof(*oobecc)); |
| 1295 | |
| 1296 | if (!mtd || section < 0) |
| 1297 | return -EINVAL; |
| 1298 | |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 1299 | if (!mtd->ooblayout || !mtd->ooblayout->ecc) |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1300 | return -ENOTSUPP; |
| 1301 | |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 1302 | return mtd->ooblayout->ecc(mtd, section, oobecc); |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1303 | } |
| 1304 | EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc); |
| 1305 | |
| 1306 | /** |
| 1307 | * mtd_ooblayout_free - Get the OOB region definition of a specific free |
| 1308 | * section |
| 1309 | * @mtd: MTD device structure |
| 1310 | * @section: Free section you are interested in. Depending on the layout |
| 1311 | * you may have all the free bytes stored in a single contiguous |
| 1312 | * section, or one section per ECC chunk plus an extra section |
| 1313 | * for the remaining bytes (or other funky layout). |
| 1314 | * @oobfree: OOB region struct filled with the appropriate free position |
| 1315 | * information |
| 1316 | * |
Masahiro Yamada | 7da0fff | 2016-12-14 09:31:01 +0900 | [diff] [blame] | 1317 | * This function returns free bytes position in the OOB area. If you want |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1318 | * to get all the free bytes information, then you should call |
| 1319 | * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE. |
| 1320 | * |
| 1321 | * Returns zero on success, a negative error code otherwise. |
| 1322 | */ |
| 1323 | int mtd_ooblayout_free(struct mtd_info *mtd, int section, |
| 1324 | struct mtd_oob_region *oobfree) |
| 1325 | { |
| 1326 | memset(oobfree, 0, sizeof(*oobfree)); |
| 1327 | |
| 1328 | if (!mtd || section < 0) |
| 1329 | return -EINVAL; |
| 1330 | |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 1331 | if (!mtd->ooblayout || !mtd->ooblayout->free) |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1332 | return -ENOTSUPP; |
| 1333 | |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 1334 | return mtd->ooblayout->free(mtd, section, oobfree); |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1335 | } |
| 1336 | EXPORT_SYMBOL_GPL(mtd_ooblayout_free); |
| 1337 | |
| 1338 | /** |
| 1339 | * mtd_ooblayout_find_region - Find the region attached to a specific byte |
| 1340 | * @mtd: mtd info structure |
| 1341 | * @byte: the byte we are searching for |
| 1342 | * @sectionp: pointer where the section id will be stored |
| 1343 | * @oobregion: used to retrieve the ECC position |
| 1344 | * @iter: iterator function. Should be either mtd_ooblayout_free or |
| 1345 | * mtd_ooblayout_ecc depending on the region type you're searching for |
| 1346 | * |
Masahiro Yamada | 7da0fff | 2016-12-14 09:31:01 +0900 | [diff] [blame] | 1347 | * This function returns the section id and oobregion information of a |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1348 | * specific byte. For example, say you want to know where the 4th ECC byte is |
| 1349 | * stored, you'll use: |
| 1350 | * |
| 1351 | * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc); |
| 1352 | * |
| 1353 | * Returns zero on success, a negative error code otherwise. |
| 1354 | */ |
| 1355 | static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, |
| 1356 | int *sectionp, struct mtd_oob_region *oobregion, |
| 1357 | int (*iter)(struct mtd_info *, |
| 1358 | int section, |
| 1359 | struct mtd_oob_region *oobregion)) |
| 1360 | { |
| 1361 | int pos = 0, ret, section = 0; |
| 1362 | |
| 1363 | memset(oobregion, 0, sizeof(*oobregion)); |
| 1364 | |
| 1365 | while (1) { |
| 1366 | ret = iter(mtd, section, oobregion); |
| 1367 | if (ret) |
| 1368 | return ret; |
| 1369 | |
| 1370 | if (pos + oobregion->length > byte) |
| 1371 | break; |
| 1372 | |
| 1373 | pos += oobregion->length; |
| 1374 | section++; |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | * Adjust region info to make it start at the beginning at the |
| 1379 | * 'start' ECC byte. |
| 1380 | */ |
| 1381 | oobregion->offset += byte - pos; |
| 1382 | oobregion->length -= byte - pos; |
| 1383 | *sectionp = section; |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | /** |
| 1389 | * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific |
| 1390 | * ECC byte |
| 1391 | * @mtd: mtd info structure |
| 1392 | * @eccbyte: the byte we are searching for |
| 1393 | * @sectionp: pointer where the section id will be stored |
| 1394 | * @oobregion: OOB region information |
| 1395 | * |
| 1396 | * Works like mtd_ooblayout_find_region() except it searches for a specific ECC |
| 1397 | * byte. |
| 1398 | * |
| 1399 | * Returns zero on success, a negative error code otherwise. |
| 1400 | */ |
| 1401 | int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, |
| 1402 | int *section, |
| 1403 | struct mtd_oob_region *oobregion) |
| 1404 | { |
| 1405 | return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, |
| 1406 | mtd_ooblayout_ecc); |
| 1407 | } |
| 1408 | EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion); |
| 1409 | |
| 1410 | /** |
| 1411 | * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer |
| 1412 | * @mtd: mtd info structure |
| 1413 | * @buf: destination buffer to store OOB bytes |
| 1414 | * @oobbuf: OOB buffer |
| 1415 | * @start: first byte to retrieve |
| 1416 | * @nbytes: number of bytes to retrieve |
| 1417 | * @iter: section iterator |
| 1418 | * |
| 1419 | * Extract bytes attached to a specific category (ECC or free) |
| 1420 | * from the OOB buffer and copy them into buf. |
| 1421 | * |
| 1422 | * Returns zero on success, a negative error code otherwise. |
| 1423 | */ |
| 1424 | static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, |
| 1425 | const u8 *oobbuf, int start, int nbytes, |
| 1426 | int (*iter)(struct mtd_info *, |
| 1427 | int section, |
| 1428 | struct mtd_oob_region *oobregion)) |
| 1429 | { |
Masahiro Yamada | 8e8fd4d | 2016-11-09 11:08:08 +0900 | [diff] [blame] | 1430 | struct mtd_oob_region oobregion; |
| 1431 | int section, ret; |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1432 | |
| 1433 | ret = mtd_ooblayout_find_region(mtd, start, §ion, |
| 1434 | &oobregion, iter); |
| 1435 | |
| 1436 | while (!ret) { |
| 1437 | int cnt; |
| 1438 | |
Masahiro Yamada | 7c295ef | 2016-11-09 11:08:09 +0900 | [diff] [blame] | 1439 | cnt = min_t(int, nbytes, oobregion.length); |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1440 | memcpy(buf, oobbuf + oobregion.offset, cnt); |
| 1441 | buf += cnt; |
| 1442 | nbytes -= cnt; |
| 1443 | |
| 1444 | if (!nbytes) |
| 1445 | break; |
| 1446 | |
| 1447 | ret = iter(mtd, ++section, &oobregion); |
| 1448 | } |
| 1449 | |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
| 1453 | /** |
| 1454 | * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer |
| 1455 | * @mtd: mtd info structure |
| 1456 | * @buf: source buffer to get OOB bytes from |
| 1457 | * @oobbuf: OOB buffer |
| 1458 | * @start: first OOB byte to set |
| 1459 | * @nbytes: number of OOB bytes to set |
| 1460 | * @iter: section iterator |
| 1461 | * |
| 1462 | * Fill the OOB buffer with data provided in buf. The category (ECC or free) |
| 1463 | * is selected by passing the appropriate iterator. |
| 1464 | * |
| 1465 | * Returns zero on success, a negative error code otherwise. |
| 1466 | */ |
| 1467 | static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, |
| 1468 | u8 *oobbuf, int start, int nbytes, |
| 1469 | int (*iter)(struct mtd_info *, |
| 1470 | int section, |
| 1471 | struct mtd_oob_region *oobregion)) |
| 1472 | { |
Masahiro Yamada | 8e8fd4d | 2016-11-09 11:08:08 +0900 | [diff] [blame] | 1473 | struct mtd_oob_region oobregion; |
| 1474 | int section, ret; |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1475 | |
| 1476 | ret = mtd_ooblayout_find_region(mtd, start, §ion, |
| 1477 | &oobregion, iter); |
| 1478 | |
| 1479 | while (!ret) { |
| 1480 | int cnt; |
| 1481 | |
Masahiro Yamada | 7c295ef | 2016-11-09 11:08:09 +0900 | [diff] [blame] | 1482 | cnt = min_t(int, nbytes, oobregion.length); |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1483 | memcpy(oobbuf + oobregion.offset, buf, cnt); |
| 1484 | buf += cnt; |
| 1485 | nbytes -= cnt; |
| 1486 | |
| 1487 | if (!nbytes) |
| 1488 | break; |
| 1489 | |
| 1490 | ret = iter(mtd, ++section, &oobregion); |
| 1491 | } |
| 1492 | |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
| 1497 | * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category |
| 1498 | * @mtd: mtd info structure |
| 1499 | * @iter: category iterator |
| 1500 | * |
| 1501 | * Count the number of bytes in a given category. |
| 1502 | * |
| 1503 | * Returns a positive value on success, a negative error code otherwise. |
| 1504 | */ |
| 1505 | static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, |
| 1506 | int (*iter)(struct mtd_info *, |
| 1507 | int section, |
| 1508 | struct mtd_oob_region *oobregion)) |
| 1509 | { |
Masahiro Yamada | 4d6aecf | 2016-11-09 11:08:10 +0900 | [diff] [blame] | 1510 | struct mtd_oob_region oobregion; |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1511 | int section = 0, ret, nbytes = 0; |
| 1512 | |
| 1513 | while (1) { |
| 1514 | ret = iter(mtd, section++, &oobregion); |
| 1515 | if (ret) { |
| 1516 | if (ret == -ERANGE) |
| 1517 | ret = nbytes; |
| 1518 | break; |
| 1519 | } |
| 1520 | |
| 1521 | nbytes += oobregion.length; |
| 1522 | } |
| 1523 | |
| 1524 | return ret; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer |
| 1529 | * @mtd: mtd info structure |
| 1530 | * @eccbuf: destination buffer to store ECC bytes |
| 1531 | * @oobbuf: OOB buffer |
| 1532 | * @start: first ECC byte to retrieve |
| 1533 | * @nbytes: number of ECC bytes to retrieve |
| 1534 | * |
| 1535 | * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes. |
| 1536 | * |
| 1537 | * Returns zero on success, a negative error code otherwise. |
| 1538 | */ |
| 1539 | int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, |
| 1540 | const u8 *oobbuf, int start, int nbytes) |
| 1541 | { |
| 1542 | return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, |
| 1543 | mtd_ooblayout_ecc); |
| 1544 | } |
| 1545 | EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes); |
| 1546 | |
| 1547 | /** |
| 1548 | * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer |
| 1549 | * @mtd: mtd info structure |
| 1550 | * @eccbuf: source buffer to get ECC bytes from |
| 1551 | * @oobbuf: OOB buffer |
| 1552 | * @start: first ECC byte to set |
| 1553 | * @nbytes: number of ECC bytes to set |
| 1554 | * |
| 1555 | * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes. |
| 1556 | * |
| 1557 | * Returns zero on success, a negative error code otherwise. |
| 1558 | */ |
| 1559 | int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, |
| 1560 | u8 *oobbuf, int start, int nbytes) |
| 1561 | { |
| 1562 | return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, |
| 1563 | mtd_ooblayout_ecc); |
| 1564 | } |
| 1565 | EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes); |
| 1566 | |
| 1567 | /** |
| 1568 | * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer |
| 1569 | * @mtd: mtd info structure |
| 1570 | * @databuf: destination buffer to store ECC bytes |
| 1571 | * @oobbuf: OOB buffer |
| 1572 | * @start: first ECC byte to retrieve |
| 1573 | * @nbytes: number of ECC bytes to retrieve |
| 1574 | * |
| 1575 | * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. |
| 1576 | * |
| 1577 | * Returns zero on success, a negative error code otherwise. |
| 1578 | */ |
| 1579 | int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, |
| 1580 | const u8 *oobbuf, int start, int nbytes) |
| 1581 | { |
| 1582 | return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, |
| 1583 | mtd_ooblayout_free); |
| 1584 | } |
| 1585 | EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes); |
| 1586 | |
| 1587 | /** |
Xiaolei Li | c77a931 | 2018-03-29 09:34:58 +0800 | [diff] [blame] | 1588 | * mtd_ooblayout_set_databytes - set data bytes into the oob buffer |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1589 | * @mtd: mtd info structure |
Xiaolei Li | c77a931 | 2018-03-29 09:34:58 +0800 | [diff] [blame] | 1590 | * @databuf: source buffer to get data bytes from |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1591 | * @oobbuf: OOB buffer |
| 1592 | * @start: first ECC byte to set |
| 1593 | * @nbytes: number of ECC bytes to set |
| 1594 | * |
| 1595 | * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. |
| 1596 | * |
| 1597 | * Returns zero on success, a negative error code otherwise. |
| 1598 | */ |
| 1599 | int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, |
| 1600 | u8 *oobbuf, int start, int nbytes) |
| 1601 | { |
| 1602 | return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, |
| 1603 | mtd_ooblayout_free); |
| 1604 | } |
| 1605 | EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes); |
| 1606 | |
| 1607 | /** |
| 1608 | * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB |
| 1609 | * @mtd: mtd info structure |
| 1610 | * |
| 1611 | * Works like mtd_ooblayout_count_bytes(), except it count free bytes. |
| 1612 | * |
| 1613 | * Returns zero on success, a negative error code otherwise. |
| 1614 | */ |
| 1615 | int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) |
| 1616 | { |
| 1617 | return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); |
| 1618 | } |
| 1619 | EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes); |
| 1620 | |
| 1621 | /** |
Xiaolei Li | c77a931 | 2018-03-29 09:34:58 +0800 | [diff] [blame] | 1622 | * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB |
Boris Brezillon | 75eb2ce | 2016-02-04 09:52:30 +0100 | [diff] [blame] | 1623 | * @mtd: mtd info structure |
| 1624 | * |
| 1625 | * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes. |
| 1626 | * |
| 1627 | * Returns zero on success, a negative error code otherwise. |
| 1628 | */ |
| 1629 | int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) |
| 1630 | { |
| 1631 | return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); |
| 1632 | } |
| 1633 | EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes); |
| 1634 | |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1635 | /* |
| 1636 | * Method to access the protection register area, present in some flash |
| 1637 | * devices. The user data is one time programmable but the factory data is read |
| 1638 | * only. |
| 1639 | */ |
Christian Riesch | 4b78fc42f | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 1640 | int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
| 1641 | struct otp_info *buf) |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1642 | { |
| 1643 | if (!mtd->_get_fact_prot_info) |
| 1644 | return -EOPNOTSUPP; |
| 1645 | if (!len) |
| 1646 | return 0; |
Christian Riesch | 4b78fc42f | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 1647 | return mtd->_get_fact_prot_info(mtd, len, retlen, buf); |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1648 | } |
| 1649 | EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); |
| 1650 | |
| 1651 | int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, |
| 1652 | size_t *retlen, u_char *buf) |
| 1653 | { |
| 1654 | *retlen = 0; |
| 1655 | if (!mtd->_read_fact_prot_reg) |
| 1656 | return -EOPNOTSUPP; |
| 1657 | if (!len) |
| 1658 | return 0; |
| 1659 | return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf); |
| 1660 | } |
| 1661 | EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); |
| 1662 | |
Christian Riesch | 4b78fc42f | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 1663 | int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
| 1664 | struct otp_info *buf) |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1665 | { |
| 1666 | if (!mtd->_get_user_prot_info) |
| 1667 | return -EOPNOTSUPP; |
| 1668 | if (!len) |
| 1669 | return 0; |
Christian Riesch | 4b78fc42f | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 1670 | return mtd->_get_user_prot_info(mtd, len, retlen, buf); |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1671 | } |
| 1672 | EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); |
| 1673 | |
| 1674 | int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, |
| 1675 | size_t *retlen, u_char *buf) |
| 1676 | { |
| 1677 | *retlen = 0; |
| 1678 | if (!mtd->_read_user_prot_reg) |
| 1679 | return -EOPNOTSUPP; |
| 1680 | if (!len) |
| 1681 | return 0; |
| 1682 | return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf); |
| 1683 | } |
| 1684 | EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); |
| 1685 | |
| 1686 | int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, |
| 1687 | size_t *retlen, u_char *buf) |
| 1688 | { |
Christian Riesch | 9a78bc8 | 2014-03-06 12:42:37 +0100 | [diff] [blame] | 1689 | int ret; |
| 1690 | |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1691 | *retlen = 0; |
| 1692 | if (!mtd->_write_user_prot_reg) |
| 1693 | return -EOPNOTSUPP; |
| 1694 | if (!len) |
| 1695 | return 0; |
Christian Riesch | 9a78bc8 | 2014-03-06 12:42:37 +0100 | [diff] [blame] | 1696 | ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf); |
| 1697 | if (ret) |
| 1698 | return ret; |
| 1699 | |
| 1700 | /* |
| 1701 | * If no data could be written at all, we are out of memory and |
| 1702 | * must return -ENOSPC. |
| 1703 | */ |
| 1704 | return (*retlen) ? 0 : -ENOSPC; |
Artem Bityutskiy | de3cac9 | 2012-02-08 16:37:14 +0200 | [diff] [blame] | 1705 | } |
| 1706 | EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); |
| 1707 | |
| 1708 | int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) |
| 1709 | { |
| 1710 | if (!mtd->_lock_user_prot_reg) |
| 1711 | return -EOPNOTSUPP; |
| 1712 | if (!len) |
| 1713 | return 0; |
| 1714 | return mtd->_lock_user_prot_reg(mtd, from, len); |
| 1715 | } |
| 1716 | EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); |
| 1717 | |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1718 | /* Chip-supported device locking */ |
| 1719 | int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1720 | { |
| 1721 | if (!mtd->_lock) |
| 1722 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1723 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1724 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1725 | if (!len) |
| 1726 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1727 | return mtd->_lock(mtd, ofs, len); |
| 1728 | } |
| 1729 | EXPORT_SYMBOL_GPL(mtd_lock); |
| 1730 | |
| 1731 | int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1732 | { |
| 1733 | if (!mtd->_unlock) |
| 1734 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1735 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1736 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1737 | if (!len) |
| 1738 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1739 | return mtd->_unlock(mtd, ofs, len); |
| 1740 | } |
| 1741 | EXPORT_SYMBOL_GPL(mtd_unlock); |
| 1742 | |
| 1743 | int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1744 | { |
| 1745 | if (!mtd->_is_locked) |
| 1746 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1747 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1748 | return -EINVAL; |
Artem Bityutskiy | bcb1d23 | 2012-02-06 13:27:43 +0200 | [diff] [blame] | 1749 | if (!len) |
| 1750 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1751 | return mtd->_is_locked(mtd, ofs, len); |
| 1752 | } |
| 1753 | EXPORT_SYMBOL_GPL(mtd_is_locked); |
| 1754 | |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1755 | int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1756 | { |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1757 | if (ofs < 0 || ofs >= mtd->size) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1758 | return -EINVAL; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1759 | if (!mtd->_block_isreserved) |
| 1760 | return 0; |
| 1761 | return mtd->_block_isreserved(mtd, ofs); |
| 1762 | } |
| 1763 | EXPORT_SYMBOL_GPL(mtd_block_isreserved); |
| 1764 | |
| 1765 | int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) |
| 1766 | { |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1767 | if (ofs < 0 || ofs >= mtd->size) |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1768 | return -EINVAL; |
| 1769 | if (!mtd->_block_isbad) |
| 1770 | return 0; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1771 | return mtd->_block_isbad(mtd, ofs); |
| 1772 | } |
| 1773 | EXPORT_SYMBOL_GPL(mtd_block_isbad); |
| 1774 | |
| 1775 | int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1776 | { |
| 1777 | if (!mtd->_block_markbad) |
| 1778 | return -EOPNOTSUPP; |
Brian Norris | 0c2b4e2 | 2014-07-21 19:06:27 -0700 | [diff] [blame] | 1779 | if (ofs < 0 || ofs >= mtd->size) |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1780 | return -EINVAL; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1781 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1782 | return -EROFS; |
Artem Bityutskiy | 8273a0c | 2012-02-03 14:34:14 +0200 | [diff] [blame] | 1783 | return mtd->_block_markbad(mtd, ofs); |
| 1784 | } |
| 1785 | EXPORT_SYMBOL_GPL(mtd_block_markbad); |
| 1786 | |
| 1787 | /* |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1788 | * default_mtd_writev - the default writev method |
| 1789 | * @mtd: mtd device description object pointer |
| 1790 | * @vecs: the vectors to write |
| 1791 | * @count: count of vectors in @vecs |
| 1792 | * @to: the MTD device offset to write to |
| 1793 | * @retlen: on exit contains the count of bytes written to the MTD device. |
| 1794 | * |
| 1795 | * This function returns zero in case of success and a negative error code in |
| 1796 | * case of failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | */ |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1798 | static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 1799 | unsigned long count, loff_t to, size_t *retlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | { |
| 1801 | unsigned long i; |
| 1802 | size_t totlen = 0, thislen; |
| 1803 | int ret = 0; |
| 1804 | |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1805 | for (i = 0; i < count; i++) { |
| 1806 | if (!vecs[i].iov_len) |
| 1807 | continue; |
| 1808 | ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, |
| 1809 | vecs[i].iov_base); |
| 1810 | totlen += thislen; |
| 1811 | if (ret || thislen != vecs[i].iov_len) |
| 1812 | break; |
| 1813 | to += vecs[i].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
Artem Bityutskiy | 52b0203 | 2011-12-30 15:57:25 +0200 | [diff] [blame] | 1815 | *retlen = totlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | return ret; |
| 1817 | } |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1818 | |
| 1819 | /* |
| 1820 | * mtd_writev - the vector-based MTD write method |
| 1821 | * @mtd: mtd device description object pointer |
| 1822 | * @vecs: the vectors to write |
| 1823 | * @count: count of vectors in @vecs |
| 1824 | * @to: the MTD device offset to write to |
| 1825 | * @retlen: on exit contains the count of bytes written to the MTD device. |
| 1826 | * |
| 1827 | * This function returns zero in case of success and a negative error code in |
| 1828 | * case of failure. |
| 1829 | */ |
| 1830 | int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 1831 | unsigned long count, loff_t to, size_t *retlen) |
| 1832 | { |
| 1833 | *retlen = 0; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 1834 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1835 | return -EROFS; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 1836 | if (!mtd->_writev) |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1837 | return default_mtd_writev(mtd, vecs, count, to, retlen); |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 1838 | return mtd->_writev(mtd, vecs, count, to, retlen); |
Artem Bityutskiy | 1dbebd3 | 2011-12-30 16:23:41 +0200 | [diff] [blame] | 1839 | } |
| 1840 | EXPORT_SYMBOL_GPL(mtd_writev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1842 | /** |
| 1843 | * 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] | 1844 | * @mtd: mtd device description object pointer |
| 1845 | * @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] | 1846 | * to the actual allocation size on success. |
| 1847 | * |
| 1848 | * This routine attempts to allocate a contiguous kernel buffer up to |
| 1849 | * the specified size, backing off the size of the request exponentially |
| 1850 | * until the request succeeds or until the allocation size falls below |
| 1851 | * the system page size. This attempts to make sure it does not adversely |
| 1852 | * impact system performance, so when allocating more than one page, we |
Linus Torvalds | caf4919 | 2012-12-10 10:51:16 -0800 | [diff] [blame] | 1853 | * ask the memory allocator to avoid re-trying, swapping, writing back |
| 1854 | * or performing I/O. |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1855 | * |
| 1856 | * Note, this function also makes sure that the allocated buffer is aligned to |
| 1857 | * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. |
| 1858 | * |
| 1859 | * This is called, for example by mtd_{read,write} and jffs2_scan_medium, |
| 1860 | * to handle smaller (i.e. degraded) buffer allocations under low- or |
| 1861 | * fragmented-memory situations where such reduced allocations, from a |
| 1862 | * requested ideal, are allowed. |
| 1863 | * |
| 1864 | * Returns a pointer to the allocated buffer on success; otherwise, NULL. |
| 1865 | */ |
| 1866 | void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) |
| 1867 | { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1868 | gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY; |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1869 | size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); |
| 1870 | void *kbuf; |
| 1871 | |
| 1872 | *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); |
| 1873 | |
| 1874 | while (*size > min_alloc) { |
| 1875 | kbuf = kmalloc(*size, flags); |
| 1876 | if (kbuf) |
| 1877 | return kbuf; |
| 1878 | |
| 1879 | *size >>= 1; |
| 1880 | *size = ALIGN(*size, mtd->writesize); |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * For the last resort allocation allow 'kmalloc()' to do all sorts of |
| 1885 | * things (write-back, dropping caches, etc) by using GFP_KERNEL. |
| 1886 | */ |
| 1887 | return kmalloc(*size, GFP_KERNEL); |
| 1888 | } |
Grant Erickson | 33b5371 | 2011-04-08 08:51:32 -0700 | [diff] [blame] | 1889 | EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 1891 | #ifdef CONFIG_PROC_FS |
| 1892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | /*====================================================================*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | /* Support for /proc/mtd */ |
| 1895 | |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1896 | static int mtd_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 1898 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1900 | seq_puts(m, "dev: size erasesize name\n"); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1901 | mutex_lock(&mtd_table_mutex); |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 1902 | mtd_for_each_device(mtd) { |
Alexey Dobriyan | 447d9bd | 2011-05-13 23:34:19 +0300 | [diff] [blame] | 1903 | seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", |
| 1904 | mtd->index, (unsigned long long)mtd->size, |
| 1905 | mtd->erasesize, mtd->name); |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 1906 | } |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1907 | mutex_unlock(&mtd_table_mutex); |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 1908 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 1910 | #endif /* CONFIG_PROC_FS */ |
| 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | /*====================================================================*/ |
| 1913 | /* Init code */ |
| 1914 | |
Steve Longerbeam | 445caaa | 2016-08-04 19:31:15 +0530 | [diff] [blame] | 1915 | static struct backing_dev_info * __init mtd_bdi_init(char *name) |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1916 | { |
Steve Longerbeam | 445caaa | 2016-08-04 19:31:15 +0530 | [diff] [blame] | 1917 | struct backing_dev_info *bdi; |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1918 | int ret; |
| 1919 | |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 1920 | bdi = bdi_alloc(GFP_KERNEL); |
Steve Longerbeam | 445caaa | 2016-08-04 19:31:15 +0530 | [diff] [blame] | 1921 | if (!bdi) |
| 1922 | return ERR_PTR(-ENOMEM); |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1923 | |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 1924 | bdi->name = name; |
| 1925 | /* |
| 1926 | * We put '-0' suffix to the name to get the same name format as we |
| 1927 | * used to get. Since this is called only once, we get a unique name. |
| 1928 | */ |
Jan Kara | 7c4cc30 | 2017-04-12 12:24:49 +0200 | [diff] [blame] | 1929 | ret = bdi_register(bdi, "%.28s-0", name); |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1930 | if (ret) |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 1931 | bdi_put(bdi); |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1932 | |
Steve Longerbeam | 445caaa | 2016-08-04 19:31:15 +0530 | [diff] [blame] | 1933 | return ret ? ERR_PTR(ret) : bdi; |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
Artem Bityutskiy | 93e5621 | 2013-03-15 12:56:05 +0200 | [diff] [blame] | 1936 | static struct proc_dir_entry *proc_mtd; |
| 1937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | static int __init init_mtd(void) |
| 1939 | { |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 1940 | int ret; |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 1941 | |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1942 | ret = class_register(&mtd_class); |
| 1943 | if (ret) |
| 1944 | goto err_reg; |
| 1945 | |
Steve Longerbeam | 445caaa | 2016-08-04 19:31:15 +0530 | [diff] [blame] | 1946 | mtd_bdi = mtd_bdi_init("mtd"); |
| 1947 | if (IS_ERR(mtd_bdi)) { |
| 1948 | ret = PTR_ERR(mtd_bdi); |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1949 | goto err_bdi; |
Steve Longerbeam | 445caaa | 2016-08-04 19:31:15 +0530 | [diff] [blame] | 1950 | } |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1951 | |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 1952 | proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); |
Artem Bityutskiy | 93e5621 | 2013-03-15 12:56:05 +0200 | [diff] [blame] | 1953 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1954 | ret = init_mtdchar(); |
| 1955 | if (ret) |
| 1956 | goto out_procfs; |
| 1957 | |
Mario Rugiero | e8e3edb | 2017-05-29 08:38:41 -0300 | [diff] [blame] | 1958 | dfs_dir_mtd = debugfs_create_dir("mtd", NULL); |
| 1959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | return 0; |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1961 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1962 | out_procfs: |
| 1963 | if (proc_mtd) |
| 1964 | remove_proc_entry("mtd", NULL); |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 1965 | bdi_put(mtd_bdi); |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 1966 | err_bdi: |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 1967 | class_unregister(&mtd_class); |
| 1968 | err_reg: |
| 1969 | pr_err("Error registering mtd class or bdi: %d\n", ret); |
| 1970 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | static void __exit cleanup_mtd(void) |
| 1974 | { |
Mario Rugiero | e8e3edb | 2017-05-29 08:38:41 -0300 | [diff] [blame] | 1975 | debugfs_remove_recursive(dfs_dir_mtd); |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1976 | cleanup_mtdchar(); |
Wanlong Gao | d5ca512 | 2011-05-20 21:14:30 +0800 | [diff] [blame] | 1977 | if (proc_mtd) |
Artem Bityutskiy | 93e5621 | 2013-03-15 12:56:05 +0200 | [diff] [blame] | 1978 | remove_proc_entry("mtd", NULL); |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 1979 | class_unregister(&mtd_class); |
Jan Kara | fa06052 | 2017-04-12 12:24:37 +0200 | [diff] [blame] | 1980 | bdi_put(mtd_bdi); |
Johannes Thumshirn | 35667b9 | 2015-07-08 17:15:34 +0200 | [diff] [blame] | 1981 | idr_destroy(&mtd_idr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | module_init(init_mtd); |
| 1985 | module_exit(cleanup_mtd); |
| 1986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | MODULE_LICENSE("GPL"); |
| 1988 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 1989 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |