Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Core registration and callback routines for MTD |
| 4 | * drivers and users. |
| 5 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 6 | * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> |
| 7 | * Copyright © 2006 Red Hat UK Limited |
| 8 | * |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 11 | #ifndef __UBOOT__ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/ptrace.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/timer.h> |
| 18 | #include <linux/major.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/ioctl.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/proc_fs.h> |
| 24 | #include <linux/idr.h> |
| 25 | #include <linux/backing-dev.h> |
| 26 | #include <linux/gfp.h> |
| 27 | #include <linux/slab.h> |
| 28 | #else |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 29 | #include <linux/err.h> |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 30 | #include <ubi_uboot.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 31 | #endif |
| 32 | |
Fabio Estevam | 0297d1e | 2015-11-05 12:43:39 -0200 | [diff] [blame] | 33 | #include <linux/log2.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 34 | #include <linux/mtd/mtd.h> |
| 35 | #include <linux/mtd/partitions.h> |
| 36 | |
| 37 | #include "mtdcore.h" |
| 38 | |
| 39 | #ifndef __UBOOT__ |
| 40 | /* |
| 41 | * backing device capabilities for non-mappable devices (such as NAND flash) |
| 42 | * - permits private mappings, copies are taken of the data |
| 43 | */ |
| 44 | static struct backing_dev_info mtd_bdi_unmappable = { |
| 45 | .capabilities = BDI_CAP_MAP_COPY, |
| 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * backing device capabilities for R/O mappable devices (such as ROM) |
| 50 | * - permits private mappings, copies are taken of the data |
| 51 | * - permits non-writable shared mappings |
| 52 | */ |
| 53 | static struct backing_dev_info mtd_bdi_ro_mappable = { |
| 54 | .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | |
| 55 | BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP), |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * backing device capabilities for writable mappable devices (such as RAM) |
| 60 | * - permits private mappings, copies are taken of the data |
| 61 | * - permits non-writable shared mappings |
| 62 | */ |
| 63 | static struct backing_dev_info mtd_bdi_rw_mappable = { |
| 64 | .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | |
| 65 | BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP | |
| 66 | BDI_CAP_WRITE_MAP), |
| 67 | }; |
| 68 | |
| 69 | static int mtd_cls_suspend(struct device *dev, pm_message_t state); |
| 70 | static int mtd_cls_resume(struct device *dev); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 71 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 72 | static struct class mtd_class = { |
| 73 | .name = "mtd", |
| 74 | .owner = THIS_MODULE, |
| 75 | .suspend = mtd_cls_suspend, |
| 76 | .resume = mtd_cls_resume, |
| 77 | }; |
| 78 | #else |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 79 | struct mtd_info *mtd_table[MAX_MTD_DEVICES]; |
| 80 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 81 | #define MAX_IDR_ID 64 |
| 82 | |
| 83 | struct idr_layer { |
| 84 | int used; |
| 85 | void *ptr; |
| 86 | }; |
| 87 | |
| 88 | struct idr { |
| 89 | struct idr_layer id[MAX_IDR_ID]; |
| 90 | }; |
| 91 | |
| 92 | #define DEFINE_IDR(name) struct idr name; |
| 93 | |
| 94 | void idr_remove(struct idr *idp, int id) |
| 95 | { |
| 96 | if (idp->id[id].used) |
| 97 | idp->id[id].used = 0; |
| 98 | |
| 99 | return; |
| 100 | } |
| 101 | void *idr_find(struct idr *idp, int id) |
| 102 | { |
| 103 | if (idp->id[id].used) |
| 104 | return idp->id[id].ptr; |
| 105 | |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | void *idr_get_next(struct idr *idp, int *next) |
| 110 | { |
| 111 | void *ret; |
| 112 | int id = *next; |
| 113 | |
| 114 | ret = idr_find(idp, id); |
| 115 | if (ret) { |
| 116 | id ++; |
| 117 | if (!idp->id[id].used) |
| 118 | id = 0; |
| 119 | *next = id; |
| 120 | } else { |
| 121 | *next = 0; |
| 122 | } |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask) |
| 128 | { |
| 129 | struct idr_layer *idl; |
| 130 | int i = 0; |
| 131 | |
| 132 | while (i < MAX_IDR_ID) { |
| 133 | idl = &idp->id[i]; |
| 134 | if (idl->used == 0) { |
| 135 | idl->used = 1; |
| 136 | idl->ptr = ptr; |
| 137 | return i; |
| 138 | } |
| 139 | i++; |
| 140 | } |
| 141 | return -ENOSPC; |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | static DEFINE_IDR(mtd_idr); |
| 146 | |
| 147 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
| 148 | should not use them for _anything_ else */ |
| 149 | DEFINE_MUTEX(mtd_table_mutex); |
| 150 | EXPORT_SYMBOL_GPL(mtd_table_mutex); |
| 151 | |
| 152 | struct mtd_info *__mtd_next_device(int i) |
| 153 | { |
| 154 | return idr_get_next(&mtd_idr, &i); |
| 155 | } |
| 156 | EXPORT_SYMBOL_GPL(__mtd_next_device); |
| 157 | |
| 158 | #ifndef __UBOOT__ |
| 159 | static LIST_HEAD(mtd_notifiers); |
| 160 | |
| 161 | |
| 162 | #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) |
| 163 | |
| 164 | /* REVISIT once MTD uses the driver model better, whoever allocates |
| 165 | * the mtd_info will probably want to use the release() hook... |
| 166 | */ |
| 167 | static void mtd_release(struct device *dev) |
| 168 | { |
| 169 | struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev); |
| 170 | dev_t index = MTD_DEVT(mtd->index); |
| 171 | |
| 172 | /* remove /dev/mtdXro node if needed */ |
| 173 | if (index) |
| 174 | device_destroy(&mtd_class, index + 1); |
| 175 | } |
| 176 | |
| 177 | static int mtd_cls_suspend(struct device *dev, pm_message_t state) |
| 178 | { |
| 179 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 180 | |
| 181 | return mtd ? mtd_suspend(mtd) : 0; |
| 182 | } |
| 183 | |
| 184 | static int mtd_cls_resume(struct device *dev) |
| 185 | { |
| 186 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 187 | |
| 188 | if (mtd) |
| 189 | mtd_resume(mtd); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static ssize_t mtd_type_show(struct device *dev, |
| 194 | struct device_attribute *attr, char *buf) |
| 195 | { |
| 196 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 197 | char *type; |
| 198 | |
| 199 | switch (mtd->type) { |
| 200 | case MTD_ABSENT: |
| 201 | type = "absent"; |
| 202 | break; |
| 203 | case MTD_RAM: |
| 204 | type = "ram"; |
| 205 | break; |
| 206 | case MTD_ROM: |
| 207 | type = "rom"; |
| 208 | break; |
| 209 | case MTD_NORFLASH: |
| 210 | type = "nor"; |
| 211 | break; |
| 212 | case MTD_NANDFLASH: |
| 213 | type = "nand"; |
| 214 | break; |
| 215 | case MTD_DATAFLASH: |
| 216 | type = "dataflash"; |
| 217 | break; |
| 218 | case MTD_UBIVOLUME: |
| 219 | type = "ubi"; |
| 220 | break; |
| 221 | case MTD_MLCNANDFLASH: |
| 222 | type = "mlc-nand"; |
| 223 | break; |
| 224 | default: |
| 225 | type = "unknown"; |
| 226 | } |
| 227 | |
| 228 | return snprintf(buf, PAGE_SIZE, "%s\n", type); |
| 229 | } |
| 230 | static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); |
| 231 | |
| 232 | static ssize_t mtd_flags_show(struct device *dev, |
| 233 | struct device_attribute *attr, char *buf) |
| 234 | { |
| 235 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 236 | |
| 237 | return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); |
| 238 | |
| 239 | } |
| 240 | static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); |
| 241 | |
| 242 | static ssize_t mtd_size_show(struct device *dev, |
| 243 | struct device_attribute *attr, char *buf) |
| 244 | { |
| 245 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 246 | |
| 247 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
| 248 | (unsigned long long)mtd->size); |
| 249 | |
| 250 | } |
| 251 | static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); |
| 252 | |
| 253 | static ssize_t mtd_erasesize_show(struct device *dev, |
| 254 | struct device_attribute *attr, char *buf) |
| 255 | { |
| 256 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 257 | |
| 258 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); |
| 259 | |
| 260 | } |
| 261 | static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); |
| 262 | |
| 263 | static ssize_t mtd_writesize_show(struct device *dev, |
| 264 | struct device_attribute *attr, char *buf) |
| 265 | { |
| 266 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 267 | |
| 268 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); |
| 269 | |
| 270 | } |
| 271 | static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); |
| 272 | |
| 273 | static ssize_t mtd_subpagesize_show(struct device *dev, |
| 274 | struct device_attribute *attr, char *buf) |
| 275 | { |
| 276 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 277 | unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 278 | |
| 279 | return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); |
| 280 | |
| 281 | } |
| 282 | static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); |
| 283 | |
| 284 | static ssize_t mtd_oobsize_show(struct device *dev, |
| 285 | struct device_attribute *attr, char *buf) |
| 286 | { |
| 287 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 288 | |
| 289 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); |
| 290 | |
| 291 | } |
| 292 | static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); |
| 293 | |
| 294 | static ssize_t mtd_numeraseregions_show(struct device *dev, |
| 295 | struct device_attribute *attr, char *buf) |
| 296 | { |
| 297 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 298 | |
| 299 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); |
| 300 | |
| 301 | } |
| 302 | static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, |
| 303 | NULL); |
| 304 | |
| 305 | static ssize_t mtd_name_show(struct device *dev, |
| 306 | struct device_attribute *attr, char *buf) |
| 307 | { |
| 308 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 309 | |
| 310 | return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); |
| 311 | |
| 312 | } |
| 313 | static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); |
| 314 | |
| 315 | static ssize_t mtd_ecc_strength_show(struct device *dev, |
| 316 | struct device_attribute *attr, char *buf) |
| 317 | { |
| 318 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 319 | |
| 320 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); |
| 321 | } |
| 322 | static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); |
| 323 | |
| 324 | static ssize_t mtd_bitflip_threshold_show(struct device *dev, |
| 325 | struct device_attribute *attr, |
| 326 | char *buf) |
| 327 | { |
| 328 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 329 | |
| 330 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); |
| 331 | } |
| 332 | |
| 333 | static ssize_t mtd_bitflip_threshold_store(struct device *dev, |
| 334 | struct device_attribute *attr, |
| 335 | const char *buf, size_t count) |
| 336 | { |
| 337 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 338 | unsigned int bitflip_threshold; |
| 339 | int retval; |
| 340 | |
| 341 | retval = kstrtouint(buf, 0, &bitflip_threshold); |
| 342 | if (retval) |
| 343 | return retval; |
| 344 | |
| 345 | mtd->bitflip_threshold = bitflip_threshold; |
| 346 | return count; |
| 347 | } |
| 348 | static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, |
| 349 | mtd_bitflip_threshold_show, |
| 350 | mtd_bitflip_threshold_store); |
| 351 | |
| 352 | static ssize_t mtd_ecc_step_size_show(struct device *dev, |
| 353 | struct device_attribute *attr, char *buf) |
| 354 | { |
| 355 | struct mtd_info *mtd = dev_get_drvdata(dev); |
| 356 | |
| 357 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); |
| 358 | |
| 359 | } |
| 360 | static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); |
| 361 | |
| 362 | static struct attribute *mtd_attrs[] = { |
| 363 | &dev_attr_type.attr, |
| 364 | &dev_attr_flags.attr, |
| 365 | &dev_attr_size.attr, |
| 366 | &dev_attr_erasesize.attr, |
| 367 | &dev_attr_writesize.attr, |
| 368 | &dev_attr_subpagesize.attr, |
| 369 | &dev_attr_oobsize.attr, |
| 370 | &dev_attr_numeraseregions.attr, |
| 371 | &dev_attr_name.attr, |
| 372 | &dev_attr_ecc_strength.attr, |
| 373 | &dev_attr_ecc_step_size.attr, |
| 374 | &dev_attr_bitflip_threshold.attr, |
| 375 | NULL, |
| 376 | }; |
| 377 | ATTRIBUTE_GROUPS(mtd); |
| 378 | |
| 379 | static struct device_type mtd_devtype = { |
| 380 | .name = "mtd", |
| 381 | .groups = mtd_groups, |
| 382 | .release = mtd_release, |
| 383 | }; |
| 384 | #endif |
| 385 | |
| 386 | /** |
| 387 | * add_mtd_device - register an MTD device |
| 388 | * @mtd: pointer to new MTD device info structure |
| 389 | * |
| 390 | * Add a device to the list of MTD devices present in the system, and |
| 391 | * notify each currently active MTD 'user' of its arrival. Returns |
| 392 | * zero on success or 1 on failure, which currently will only happen |
| 393 | * if there is insufficient memory or a sysfs error. |
| 394 | */ |
| 395 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 396 | int add_mtd_device(struct mtd_info *mtd) |
| 397 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 398 | #ifndef __UBOOT__ |
| 399 | struct mtd_notifier *not; |
| 400 | #endif |
| 401 | int i, error; |
| 402 | |
| 403 | #ifndef __UBOOT__ |
| 404 | if (!mtd->backing_dev_info) { |
| 405 | switch (mtd->type) { |
| 406 | case MTD_RAM: |
| 407 | mtd->backing_dev_info = &mtd_bdi_rw_mappable; |
| 408 | break; |
| 409 | case MTD_ROM: |
| 410 | mtd->backing_dev_info = &mtd_bdi_ro_mappable; |
| 411 | break; |
| 412 | default: |
| 413 | mtd->backing_dev_info = &mtd_bdi_unmappable; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | #endif |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 418 | |
| 419 | BUG_ON(mtd->writesize == 0); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 420 | mutex_lock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 421 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 422 | i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); |
| 423 | if (i < 0) |
| 424 | goto fail_locked; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 425 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 426 | mtd->index = i; |
| 427 | mtd->usecount = 0; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 428 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 429 | /* default value if not set by driver */ |
| 430 | if (mtd->bitflip_threshold == 0) |
| 431 | mtd->bitflip_threshold = mtd->ecc_strength; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 432 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 433 | if (is_power_of_2(mtd->erasesize)) |
| 434 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; |
| 435 | else |
| 436 | mtd->erasesize_shift = 0; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 437 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 438 | if (is_power_of_2(mtd->writesize)) |
| 439 | mtd->writesize_shift = ffs(mtd->writesize) - 1; |
| 440 | else |
| 441 | mtd->writesize_shift = 0; |
| 442 | |
| 443 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; |
| 444 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; |
| 445 | |
| 446 | /* Some chips always power up locked. Unlock them now */ |
| 447 | if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { |
| 448 | error = mtd_unlock(mtd, 0, mtd->size); |
| 449 | if (error && error != -EOPNOTSUPP) |
| 450 | printk(KERN_WARNING |
| 451 | "%s: unlock failed, writes may not work\n", |
| 452 | mtd->name); |
| 453 | } |
| 454 | |
| 455 | #ifndef __UBOOT__ |
| 456 | /* Caller should have set dev.parent to match the |
| 457 | * physical device. |
| 458 | */ |
| 459 | mtd->dev.type = &mtd_devtype; |
| 460 | mtd->dev.class = &mtd_class; |
| 461 | mtd->dev.devt = MTD_DEVT(i); |
| 462 | dev_set_name(&mtd->dev, "mtd%d", i); |
| 463 | dev_set_drvdata(&mtd->dev, mtd); |
| 464 | if (device_register(&mtd->dev) != 0) |
| 465 | goto fail_added; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 466 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 467 | if (MTD_DEVT(i)) |
| 468 | device_create(&mtd_class, mtd->dev.parent, |
| 469 | MTD_DEVT(i) + 1, |
| 470 | NULL, "mtd%dro", i); |
| 471 | |
| 472 | pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); |
| 473 | /* No need to get a refcount on the module containing |
| 474 | the notifier, since we hold the mtd_table_mutex */ |
| 475 | list_for_each_entry(not, &mtd_notifiers, list) |
| 476 | not->add(mtd); |
Heiko Schocher | b24c427 | 2014-07-15 16:08:42 +0200 | [diff] [blame] | 477 | #else |
| 478 | pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 479 | #endif |
| 480 | |
| 481 | mutex_unlock(&mtd_table_mutex); |
| 482 | /* We _know_ we aren't being removed, because |
| 483 | our caller is still holding us here. So none |
| 484 | of this try_ nonsense, and no bitching about it |
| 485 | either. :) */ |
| 486 | __module_get(THIS_MODULE); |
| 487 | return 0; |
| 488 | |
| 489 | #ifndef __UBOOT__ |
| 490 | fail_added: |
| 491 | idr_remove(&mtd_idr, i); |
| 492 | #endif |
| 493 | fail_locked: |
| 494 | mutex_unlock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 499 | * del_mtd_device - unregister an MTD device |
| 500 | * @mtd: pointer to MTD device info structure |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 501 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 502 | * Remove a device from the list of MTD devices present in the system, |
| 503 | * and notify each currently active MTD 'user' of its departure. |
| 504 | * Returns zero on success or 1 on failure, which currently will happen |
| 505 | * if the requested device does not appear to be present in the list. |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 506 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 507 | |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 508 | int del_mtd_device(struct mtd_info *mtd) |
| 509 | { |
| 510 | int ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 511 | #ifndef __UBOOT__ |
| 512 | struct mtd_notifier *not; |
| 513 | #endif |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 514 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 515 | mutex_lock(&mtd_table_mutex); |
| 516 | |
| 517 | if (idr_find(&mtd_idr, mtd->index) != mtd) { |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 518 | ret = -ENODEV; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 519 | goto out_error; |
| 520 | } |
| 521 | |
| 522 | #ifndef __UBOOT__ |
| 523 | /* No need to get a refcount on the module containing |
| 524 | the notifier, since we hold the mtd_table_mutex */ |
| 525 | list_for_each_entry(not, &mtd_notifiers, list) |
| 526 | not->remove(mtd); |
| 527 | #endif |
| 528 | |
| 529 | if (mtd->usecount) { |
| 530 | printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", |
| 531 | mtd->index, mtd->name, mtd->usecount); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 532 | ret = -EBUSY; |
| 533 | } else { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 534 | #ifndef __UBOOT__ |
| 535 | device_unregister(&mtd->dev); |
| 536 | #endif |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 537 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 538 | idr_remove(&mtd_idr, mtd->index); |
| 539 | |
| 540 | module_put(THIS_MODULE); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 541 | ret = 0; |
| 542 | } |
| 543 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 544 | out_error: |
| 545 | mutex_unlock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 546 | return ret; |
| 547 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 548 | |
| 549 | #ifndef __UBOOT__ |
| 550 | /** |
| 551 | * mtd_device_parse_register - parse partitions and register an MTD device. |
| 552 | * |
| 553 | * @mtd: the MTD device to register |
| 554 | * @types: the list of MTD partition probes to try, see |
| 555 | * 'parse_mtd_partitions()' for more information |
| 556 | * @parser_data: MTD partition parser-specific data |
| 557 | * @parts: fallback partition information to register, if parsing fails; |
| 558 | * only valid if %nr_parts > %0 |
| 559 | * @nr_parts: the number of partitions in parts, if zero then the full |
| 560 | * MTD device is registered if no partition info is found |
| 561 | * |
| 562 | * This function aggregates MTD partitions parsing (done by |
| 563 | * 'parse_mtd_partitions()') and MTD device and partitions registering. It |
| 564 | * basically follows the most common pattern found in many MTD drivers: |
| 565 | * |
| 566 | * * It first tries to probe partitions on MTD device @mtd using parsers |
| 567 | * specified in @types (if @types is %NULL, then the default list of parsers |
| 568 | * is used, see 'parse_mtd_partitions()' for more information). If none are |
| 569 | * found this functions tries to fallback to information specified in |
| 570 | * @parts/@nr_parts. |
| 571 | * * If any partitioning info was found, this function registers the found |
| 572 | * partitions. |
| 573 | * * If no partitions were found this function just registers the MTD device |
| 574 | * @mtd and exits. |
| 575 | * |
| 576 | * Returns zero in case of success and a negative error code in case of failure. |
| 577 | */ |
| 578 | int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, |
| 579 | struct mtd_part_parser_data *parser_data, |
| 580 | const struct mtd_partition *parts, |
| 581 | int nr_parts) |
| 582 | { |
| 583 | int err; |
| 584 | struct mtd_partition *real_parts; |
| 585 | |
| 586 | err = parse_mtd_partitions(mtd, types, &real_parts, parser_data); |
| 587 | if (err <= 0 && nr_parts && parts) { |
| 588 | real_parts = kmemdup(parts, sizeof(*parts) * nr_parts, |
| 589 | GFP_KERNEL); |
| 590 | if (!real_parts) |
| 591 | err = -ENOMEM; |
| 592 | else |
| 593 | err = nr_parts; |
| 594 | } |
| 595 | |
| 596 | if (err > 0) { |
| 597 | err = add_mtd_partitions(mtd, real_parts, err); |
| 598 | kfree(real_parts); |
| 599 | } else if (err == 0) { |
| 600 | err = add_mtd_device(mtd); |
| 601 | if (err == 1) |
| 602 | err = -ENODEV; |
| 603 | } |
| 604 | |
| 605 | return err; |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(mtd_device_parse_register); |
| 608 | |
| 609 | /** |
| 610 | * mtd_device_unregister - unregister an existing MTD device. |
| 611 | * |
| 612 | * @master: the MTD device to unregister. This will unregister both the master |
| 613 | * and any partitions if registered. |
| 614 | */ |
| 615 | int mtd_device_unregister(struct mtd_info *master) |
| 616 | { |
| 617 | int err; |
| 618 | |
| 619 | err = del_mtd_partitions(master); |
| 620 | if (err) |
| 621 | return err; |
| 622 | |
| 623 | if (!device_is_registered(&master->dev)) |
| 624 | return 0; |
| 625 | |
| 626 | return del_mtd_device(master); |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(mtd_device_unregister); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 629 | |
| 630 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 631 | * register_mtd_user - register a 'user' of MTD devices. |
| 632 | * @new: pointer to notifier info structure |
| 633 | * |
| 634 | * Registers a pair of callbacks function to be called upon addition |
| 635 | * or removal of MTD devices. Causes the 'add' callback to be immediately |
| 636 | * invoked for each MTD device currently present in the system. |
| 637 | */ |
| 638 | void register_mtd_user (struct mtd_notifier *new) |
| 639 | { |
| 640 | struct mtd_info *mtd; |
| 641 | |
| 642 | mutex_lock(&mtd_table_mutex); |
| 643 | |
| 644 | list_add(&new->list, &mtd_notifiers); |
| 645 | |
| 646 | __module_get(THIS_MODULE); |
| 647 | |
| 648 | mtd_for_each_device(mtd) |
| 649 | new->add(mtd); |
| 650 | |
| 651 | mutex_unlock(&mtd_table_mutex); |
| 652 | } |
| 653 | EXPORT_SYMBOL_GPL(register_mtd_user); |
| 654 | |
| 655 | /** |
| 656 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
| 657 | * @old: pointer to notifier info structure |
| 658 | * |
| 659 | * Removes a callback function pair from the list of 'users' to be |
| 660 | * notified upon addition or removal of MTD devices. Causes the |
| 661 | * 'remove' callback to be immediately invoked for each MTD device |
| 662 | * currently present in the system. |
| 663 | */ |
| 664 | int unregister_mtd_user (struct mtd_notifier *old) |
| 665 | { |
| 666 | struct mtd_info *mtd; |
| 667 | |
| 668 | mutex_lock(&mtd_table_mutex); |
| 669 | |
| 670 | module_put(THIS_MODULE); |
| 671 | |
| 672 | mtd_for_each_device(mtd) |
| 673 | old->remove(mtd); |
| 674 | |
| 675 | list_del(&old->list); |
| 676 | mutex_unlock(&mtd_table_mutex); |
| 677 | return 0; |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
| 680 | #endif |
| 681 | |
| 682 | /** |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 683 | * get_mtd_device - obtain a validated handle for an MTD device |
| 684 | * @mtd: last known address of the required MTD device |
| 685 | * @num: internal device number of the required MTD device |
| 686 | * |
| 687 | * Given a number and NULL address, return the num'th entry in the device |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 688 | * table, if any. Given an address and num == -1, search the device table |
| 689 | * for a device with that address and return if it's still present. Given |
| 690 | * both, return the num'th driver only if its address matches. Return |
| 691 | * error code if not. |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 692 | */ |
| 693 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 694 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 695 | struct mtd_info *ret = NULL, *other; |
| 696 | int err = -ENODEV; |
| 697 | |
| 698 | mutex_lock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 699 | |
| 700 | if (num == -1) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 701 | mtd_for_each_device(other) { |
| 702 | if (other == mtd) { |
| 703 | ret = mtd; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | } else if (num >= 0) { |
| 708 | ret = idr_find(&mtd_idr, num); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 709 | if (mtd && mtd != ret) |
| 710 | ret = NULL; |
| 711 | } |
| 712 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 713 | if (!ret) { |
| 714 | ret = ERR_PTR(err); |
| 715 | goto out; |
| 716 | } |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 717 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 718 | err = __get_mtd_device(ret); |
| 719 | if (err) |
| 720 | ret = ERR_PTR(err); |
| 721 | out: |
| 722 | mutex_unlock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 723 | return ret; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 724 | } |
| 725 | EXPORT_SYMBOL_GPL(get_mtd_device); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 726 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 727 | |
| 728 | int __get_mtd_device(struct mtd_info *mtd) |
| 729 | { |
| 730 | int err; |
| 731 | |
| 732 | if (!try_module_get(mtd->owner)) |
| 733 | return -ENODEV; |
| 734 | |
| 735 | if (mtd->_get_device) { |
| 736 | err = mtd->_get_device(mtd); |
| 737 | |
| 738 | if (err) { |
| 739 | module_put(mtd->owner); |
| 740 | return err; |
| 741 | } |
| 742 | } |
| 743 | mtd->usecount++; |
| 744 | return 0; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 745 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 746 | EXPORT_SYMBOL_GPL(__get_mtd_device); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 747 | |
| 748 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 749 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 750 | * device name |
| 751 | * @name: MTD device name to open |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 752 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 753 | * This function returns MTD device description structure in case of |
| 754 | * success and an error code in case of failure. |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 755 | */ |
| 756 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 757 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 758 | int err = -ENODEV; |
| 759 | struct mtd_info *mtd = NULL, *other; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 760 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 761 | mutex_lock(&mtd_table_mutex); |
| 762 | |
| 763 | mtd_for_each_device(other) { |
| 764 | if (!strcmp(name, other->name)) { |
| 765 | mtd = other; |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | if (!mtd) |
| 771 | goto out_unlock; |
| 772 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 773 | err = __get_mtd_device(mtd); |
| 774 | if (err) |
| 775 | goto out_unlock; |
| 776 | |
| 777 | mutex_unlock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 778 | return mtd; |
| 779 | |
| 780 | out_unlock: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 781 | mutex_unlock(&mtd_table_mutex); |
Kyungmin Park | f6d5e25 | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 782 | return ERR_PTR(err); |
| 783 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 784 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
Ben Gardiner | 50bae73 | 2010-08-31 17:48:01 -0400 | [diff] [blame] | 785 | |
| 786 | #if defined(CONFIG_CMD_MTDPARTS_SPREAD) |
| 787 | /** |
| 788 | * mtd_get_len_incl_bad |
| 789 | * |
| 790 | * Check if length including bad blocks fits into device. |
| 791 | * |
| 792 | * @param mtd an MTD device |
| 793 | * @param offset offset in flash |
| 794 | * @param length image length |
| 795 | * @return image length including bad blocks in *len_incl_bad and whether or not |
| 796 | * the length returned was truncated in *truncated |
| 797 | */ |
| 798 | void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset, |
| 799 | const uint64_t length, uint64_t *len_incl_bad, |
| 800 | int *truncated) |
| 801 | { |
| 802 | *truncated = 0; |
| 803 | *len_incl_bad = 0; |
| 804 | |
maxin.john@enea.com | b5ee6e2 | 2014-09-08 19:04:16 +0200 | [diff] [blame] | 805 | if (!mtd->_block_isbad) { |
Ben Gardiner | 50bae73 | 2010-08-31 17:48:01 -0400 | [diff] [blame] | 806 | *len_incl_bad = length; |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | uint64_t len_excl_bad = 0; |
| 811 | uint64_t block_len; |
| 812 | |
| 813 | while (len_excl_bad < length) { |
Scott Wood | 10390ce | 2010-09-09 15:40:03 -0500 | [diff] [blame] | 814 | if (offset >= mtd->size) { |
| 815 | *truncated = 1; |
| 816 | return; |
| 817 | } |
| 818 | |
Ben Gardiner | 50bae73 | 2010-08-31 17:48:01 -0400 | [diff] [blame] | 819 | block_len = mtd->erasesize - (offset & (mtd->erasesize - 1)); |
| 820 | |
maxin.john@enea.com | b5ee6e2 | 2014-09-08 19:04:16 +0200 | [diff] [blame] | 821 | if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1))) |
Ben Gardiner | 50bae73 | 2010-08-31 17:48:01 -0400 | [diff] [blame] | 822 | len_excl_bad += block_len; |
| 823 | |
| 824 | *len_incl_bad += block_len; |
| 825 | offset += block_len; |
Ben Gardiner | 50bae73 | 2010-08-31 17:48:01 -0400 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 829 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 830 | void put_mtd_device(struct mtd_info *mtd) |
| 831 | { |
| 832 | mutex_lock(&mtd_table_mutex); |
| 833 | __put_mtd_device(mtd); |
| 834 | mutex_unlock(&mtd_table_mutex); |
| 835 | |
| 836 | } |
| 837 | EXPORT_SYMBOL_GPL(put_mtd_device); |
| 838 | |
| 839 | void __put_mtd_device(struct mtd_info *mtd) |
| 840 | { |
| 841 | --mtd->usecount; |
| 842 | BUG_ON(mtd->usecount < 0); |
| 843 | |
| 844 | if (mtd->_put_device) |
| 845 | mtd->_put_device(mtd); |
| 846 | |
| 847 | module_put(mtd->owner); |
| 848 | } |
| 849 | EXPORT_SYMBOL_GPL(__put_mtd_device); |
| 850 | |
| 851 | /* |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 852 | * Erase is an asynchronous operation. Device drivers are supposed |
| 853 | * to call instr->callback() whenever the operation completes, even |
| 854 | * if it completes with a failure. |
| 855 | * Callers are supposed to pass a callback function and wait for it |
| 856 | * to be called before writing to the block. |
| 857 | */ |
| 858 | int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 859 | { |
| 860 | if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr) |
| 861 | return -EINVAL; |
| 862 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 863 | return -EROFS; |
| 864 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
| 865 | if (!instr->len) { |
| 866 | instr->state = MTD_ERASE_DONE; |
| 867 | mtd_erase_callback(instr); |
| 868 | return 0; |
| 869 | } |
| 870 | return mtd->_erase(mtd, instr); |
| 871 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 872 | EXPORT_SYMBOL_GPL(mtd_erase); |
| 873 | |
| 874 | #ifndef __UBOOT__ |
| 875 | /* |
| 876 | * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. |
| 877 | */ |
| 878 | int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, |
| 879 | void **virt, resource_size_t *phys) |
| 880 | { |
| 881 | *retlen = 0; |
| 882 | *virt = NULL; |
| 883 | if (phys) |
| 884 | *phys = 0; |
| 885 | if (!mtd->_point) |
| 886 | return -EOPNOTSUPP; |
| 887 | if (from < 0 || from > mtd->size || len > mtd->size - from) |
| 888 | return -EINVAL; |
| 889 | if (!len) |
| 890 | return 0; |
| 891 | return mtd->_point(mtd, from, len, retlen, virt, phys); |
| 892 | } |
| 893 | EXPORT_SYMBOL_GPL(mtd_point); |
| 894 | |
| 895 | /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ |
| 896 | int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
| 897 | { |
| 898 | if (!mtd->_point) |
| 899 | return -EOPNOTSUPP; |
| 900 | if (from < 0 || from > mtd->size || len > mtd->size - from) |
| 901 | return -EINVAL; |
| 902 | if (!len) |
| 903 | return 0; |
| 904 | return mtd->_unpoint(mtd, from, len); |
| 905 | } |
| 906 | EXPORT_SYMBOL_GPL(mtd_unpoint); |
| 907 | #endif |
| 908 | |
| 909 | /* |
| 910 | * Allow NOMMU mmap() to directly map the device (if not NULL) |
| 911 | * - return the address to which the offset maps |
| 912 | * - return -ENOSYS to indicate refusal to do the mapping |
| 913 | */ |
| 914 | unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, |
| 915 | unsigned long offset, unsigned long flags) |
| 916 | { |
| 917 | if (!mtd->_get_unmapped_area) |
| 918 | return -EOPNOTSUPP; |
| 919 | if (offset > mtd->size || len > mtd->size - offset) |
| 920 | return -EINVAL; |
| 921 | return mtd->_get_unmapped_area(mtd, len, offset, flags); |
| 922 | } |
| 923 | EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 924 | |
| 925 | int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, |
| 926 | u_char *buf) |
| 927 | { |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 928 | int ret_code; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 929 | *retlen = 0; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 930 | if (from < 0 || from > mtd->size || len > mtd->size - from) |
| 931 | return -EINVAL; |
| 932 | if (!len) |
| 933 | return 0; |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 934 | |
| 935 | /* |
| 936 | * In the absence of an error, drivers return a non-negative integer |
| 937 | * representing the maximum number of bitflips that were corrected on |
| 938 | * any one ecc region (if applicable; zero otherwise). |
| 939 | */ |
Boris Brezillon | 6c20df7 | 2018-08-16 17:29:59 +0200 | [diff] [blame^] | 940 | if (mtd->_read) { |
| 941 | ret_code = mtd->_read(mtd, from, len, retlen, buf); |
| 942 | } else if (mtd->_read_oob) { |
| 943 | struct mtd_oob_ops ops = { |
| 944 | .len = len, |
| 945 | .datbuf = buf, |
| 946 | }; |
| 947 | |
| 948 | ret_code = mtd->_read_oob(mtd, from, &ops); |
| 949 | *retlen = ops.retlen; |
| 950 | } else { |
| 951 | return -ENOTSUPP; |
| 952 | } |
| 953 | |
Paul Burton | 700a76c | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 954 | if (unlikely(ret_code < 0)) |
| 955 | return ret_code; |
| 956 | if (mtd->ecc_strength == 0) |
| 957 | return 0; /* device lacks ecc */ |
| 958 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 959 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 960 | EXPORT_SYMBOL_GPL(mtd_read); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 961 | |
| 962 | int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, |
| 963 | const u_char *buf) |
| 964 | { |
| 965 | *retlen = 0; |
| 966 | if (to < 0 || to > mtd->size || len > mtd->size - to) |
| 967 | return -EINVAL; |
Boris Brezillon | 6c20df7 | 2018-08-16 17:29:59 +0200 | [diff] [blame^] | 968 | if ((!mtd->_write && !mtd->_write_oob) || |
| 969 | !(mtd->flags & MTD_WRITEABLE)) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 970 | return -EROFS; |
| 971 | if (!len) |
| 972 | return 0; |
Boris Brezillon | 6c20df7 | 2018-08-16 17:29:59 +0200 | [diff] [blame^] | 973 | |
| 974 | if (!mtd->_write) { |
| 975 | struct mtd_oob_ops ops = { |
| 976 | .len = len, |
| 977 | .datbuf = (u8 *)buf, |
| 978 | }; |
| 979 | int ret; |
| 980 | |
| 981 | ret = mtd->_write_oob(mtd, to, &ops); |
| 982 | *retlen = ops.retlen; |
| 983 | return ret; |
| 984 | } |
| 985 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 986 | return mtd->_write(mtd, to, len, retlen, buf); |
| 987 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 988 | EXPORT_SYMBOL_GPL(mtd_write); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 989 | |
| 990 | /* |
| 991 | * In blackbox flight recorder like scenarios we want to make successful writes |
| 992 | * in interrupt context. panic_write() is only intended to be called when its |
| 993 | * known the kernel is about to panic and we need the write to succeed. Since |
| 994 | * the kernel is not going to be running for much longer, this function can |
| 995 | * break locks and delay to ensure the write succeeds (but not sleep). |
| 996 | */ |
| 997 | int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, |
| 998 | const u_char *buf) |
| 999 | { |
| 1000 | *retlen = 0; |
| 1001 | if (!mtd->_panic_write) |
| 1002 | return -EOPNOTSUPP; |
| 1003 | if (to < 0 || to > mtd->size || len > mtd->size - to) |
| 1004 | return -EINVAL; |
| 1005 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1006 | return -EROFS; |
| 1007 | if (!len) |
| 1008 | return 0; |
| 1009 | return mtd->_panic_write(mtd, to, len, retlen, buf); |
| 1010 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1011 | EXPORT_SYMBOL_GPL(mtd_panic_write); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1012 | |
| 1013 | int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) |
| 1014 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1015 | int ret_code; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1016 | ops->retlen = ops->oobretlen = 0; |
| 1017 | if (!mtd->_read_oob) |
| 1018 | return -EOPNOTSUPP; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1019 | /* |
| 1020 | * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics |
| 1021 | * similar to mtd->_read(), returning a non-negative integer |
| 1022 | * representing max bitflips. In other cases, mtd->_read_oob() may |
| 1023 | * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). |
| 1024 | */ |
| 1025 | ret_code = mtd->_read_oob(mtd, from, ops); |
| 1026 | if (unlikely(ret_code < 0)) |
| 1027 | return ret_code; |
| 1028 | if (mtd->ecc_strength == 0) |
| 1029 | return 0; /* device lacks ecc */ |
| 1030 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1031 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1032 | EXPORT_SYMBOL_GPL(mtd_read_oob); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1033 | |
Boris Brezillon | e1b1e3a | 2017-11-22 02:38:23 +0900 | [diff] [blame] | 1034 | /** |
| 1035 | * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section |
| 1036 | * @mtd: MTD device structure |
| 1037 | * @section: ECC section. Depending on the layout you may have all the ECC |
| 1038 | * bytes stored in a single contiguous section, or one section |
| 1039 | * per ECC chunk (and sometime several sections for a single ECC |
| 1040 | * ECC chunk) |
| 1041 | * @oobecc: OOB region struct filled with the appropriate ECC position |
| 1042 | * information |
| 1043 | * |
| 1044 | * This function returns ECC section information in the OOB area. If you want |
| 1045 | * to get all the ECC bytes information, then you should call |
| 1046 | * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE. |
| 1047 | * |
| 1048 | * Returns zero on success, a negative error code otherwise. |
| 1049 | */ |
| 1050 | int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 1051 | struct mtd_oob_region *oobecc) |
| 1052 | { |
| 1053 | memset(oobecc, 0, sizeof(*oobecc)); |
| 1054 | |
| 1055 | if (!mtd || section < 0) |
| 1056 | return -EINVAL; |
| 1057 | |
| 1058 | if (!mtd->ooblayout || !mtd->ooblayout->ecc) |
| 1059 | return -ENOTSUPP; |
| 1060 | |
| 1061 | return mtd->ooblayout->ecc(mtd, section, oobecc); |
| 1062 | } |
| 1063 | EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc); |
| 1064 | |
| 1065 | /** |
| 1066 | * mtd_ooblayout_free - Get the OOB region definition of a specific free |
| 1067 | * section |
| 1068 | * @mtd: MTD device structure |
| 1069 | * @section: Free section you are interested in. Depending on the layout |
| 1070 | * you may have all the free bytes stored in a single contiguous |
| 1071 | * section, or one section per ECC chunk plus an extra section |
| 1072 | * for the remaining bytes (or other funky layout). |
| 1073 | * @oobfree: OOB region struct filled with the appropriate free position |
| 1074 | * information |
| 1075 | * |
| 1076 | * This function returns free bytes position in the OOB area. If you want |
| 1077 | * to get all the free bytes information, then you should call |
| 1078 | * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE. |
| 1079 | * |
| 1080 | * Returns zero on success, a negative error code otherwise. |
| 1081 | */ |
| 1082 | int mtd_ooblayout_free(struct mtd_info *mtd, int section, |
| 1083 | struct mtd_oob_region *oobfree) |
| 1084 | { |
| 1085 | memset(oobfree, 0, sizeof(*oobfree)); |
| 1086 | |
| 1087 | if (!mtd || section < 0) |
| 1088 | return -EINVAL; |
| 1089 | |
| 1090 | if (!mtd->ooblayout || !mtd->ooblayout->free) |
| 1091 | return -ENOTSUPP; |
| 1092 | |
| 1093 | return mtd->ooblayout->free(mtd, section, oobfree); |
| 1094 | } |
| 1095 | EXPORT_SYMBOL_GPL(mtd_ooblayout_free); |
| 1096 | |
| 1097 | /** |
| 1098 | * mtd_ooblayout_find_region - Find the region attached to a specific byte |
| 1099 | * @mtd: mtd info structure |
| 1100 | * @byte: the byte we are searching for |
| 1101 | * @sectionp: pointer where the section id will be stored |
| 1102 | * @oobregion: used to retrieve the ECC position |
| 1103 | * @iter: iterator function. Should be either mtd_ooblayout_free or |
| 1104 | * mtd_ooblayout_ecc depending on the region type you're searching for |
| 1105 | * |
| 1106 | * This function returns the section id and oobregion information of a |
| 1107 | * specific byte. For example, say you want to know where the 4th ECC byte is |
| 1108 | * stored, you'll use: |
| 1109 | * |
| 1110 | * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc); |
| 1111 | * |
| 1112 | * Returns zero on success, a negative error code otherwise. |
| 1113 | */ |
| 1114 | static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, |
| 1115 | int *sectionp, struct mtd_oob_region *oobregion, |
| 1116 | int (*iter)(struct mtd_info *, |
| 1117 | int section, |
| 1118 | struct mtd_oob_region *oobregion)) |
| 1119 | { |
| 1120 | int pos = 0, ret, section = 0; |
| 1121 | |
| 1122 | memset(oobregion, 0, sizeof(*oobregion)); |
| 1123 | |
| 1124 | while (1) { |
| 1125 | ret = iter(mtd, section, oobregion); |
| 1126 | if (ret) |
| 1127 | return ret; |
| 1128 | |
| 1129 | if (pos + oobregion->length > byte) |
| 1130 | break; |
| 1131 | |
| 1132 | pos += oobregion->length; |
| 1133 | section++; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Adjust region info to make it start at the beginning at the |
| 1138 | * 'start' ECC byte. |
| 1139 | */ |
| 1140 | oobregion->offset += byte - pos; |
| 1141 | oobregion->length -= byte - pos; |
| 1142 | *sectionp = section; |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific |
| 1149 | * ECC byte |
| 1150 | * @mtd: mtd info structure |
| 1151 | * @eccbyte: the byte we are searching for |
| 1152 | * @sectionp: pointer where the section id will be stored |
| 1153 | * @oobregion: OOB region information |
| 1154 | * |
| 1155 | * Works like mtd_ooblayout_find_region() except it searches for a specific ECC |
| 1156 | * byte. |
| 1157 | * |
| 1158 | * Returns zero on success, a negative error code otherwise. |
| 1159 | */ |
| 1160 | int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, |
| 1161 | int *section, |
| 1162 | struct mtd_oob_region *oobregion) |
| 1163 | { |
| 1164 | return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, |
| 1165 | mtd_ooblayout_ecc); |
| 1166 | } |
| 1167 | EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion); |
| 1168 | |
| 1169 | /** |
| 1170 | * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer |
| 1171 | * @mtd: mtd info structure |
| 1172 | * @buf: destination buffer to store OOB bytes |
| 1173 | * @oobbuf: OOB buffer |
| 1174 | * @start: first byte to retrieve |
| 1175 | * @nbytes: number of bytes to retrieve |
| 1176 | * @iter: section iterator |
| 1177 | * |
| 1178 | * Extract bytes attached to a specific category (ECC or free) |
| 1179 | * from the OOB buffer and copy them into buf. |
| 1180 | * |
| 1181 | * Returns zero on success, a negative error code otherwise. |
| 1182 | */ |
| 1183 | static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, |
| 1184 | const u8 *oobbuf, int start, int nbytes, |
| 1185 | int (*iter)(struct mtd_info *, |
| 1186 | int section, |
| 1187 | struct mtd_oob_region *oobregion)) |
| 1188 | { |
| 1189 | struct mtd_oob_region oobregion; |
| 1190 | int section, ret; |
| 1191 | |
| 1192 | ret = mtd_ooblayout_find_region(mtd, start, §ion, |
| 1193 | &oobregion, iter); |
| 1194 | |
| 1195 | while (!ret) { |
| 1196 | int cnt; |
| 1197 | |
| 1198 | cnt = min_t(int, nbytes, oobregion.length); |
| 1199 | memcpy(buf, oobbuf + oobregion.offset, cnt); |
| 1200 | buf += cnt; |
| 1201 | nbytes -= cnt; |
| 1202 | |
| 1203 | if (!nbytes) |
| 1204 | break; |
| 1205 | |
| 1206 | ret = iter(mtd, ++section, &oobregion); |
| 1207 | } |
| 1208 | |
| 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer |
| 1214 | * @mtd: mtd info structure |
| 1215 | * @buf: source buffer to get OOB bytes from |
| 1216 | * @oobbuf: OOB buffer |
| 1217 | * @start: first OOB byte to set |
| 1218 | * @nbytes: number of OOB bytes to set |
| 1219 | * @iter: section iterator |
| 1220 | * |
| 1221 | * Fill the OOB buffer with data provided in buf. The category (ECC or free) |
| 1222 | * is selected by passing the appropriate iterator. |
| 1223 | * |
| 1224 | * Returns zero on success, a negative error code otherwise. |
| 1225 | */ |
| 1226 | static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, |
| 1227 | u8 *oobbuf, int start, int nbytes, |
| 1228 | int (*iter)(struct mtd_info *, |
| 1229 | int section, |
| 1230 | struct mtd_oob_region *oobregion)) |
| 1231 | { |
| 1232 | struct mtd_oob_region oobregion; |
| 1233 | int section, ret; |
| 1234 | |
| 1235 | ret = mtd_ooblayout_find_region(mtd, start, §ion, |
| 1236 | &oobregion, iter); |
| 1237 | |
| 1238 | while (!ret) { |
| 1239 | int cnt; |
| 1240 | |
| 1241 | cnt = min_t(int, nbytes, oobregion.length); |
| 1242 | memcpy(oobbuf + oobregion.offset, buf, cnt); |
| 1243 | buf += cnt; |
| 1244 | nbytes -= cnt; |
| 1245 | |
| 1246 | if (!nbytes) |
| 1247 | break; |
| 1248 | |
| 1249 | ret = iter(mtd, ++section, &oobregion); |
| 1250 | } |
| 1251 | |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category |
| 1257 | * @mtd: mtd info structure |
| 1258 | * @iter: category iterator |
| 1259 | * |
| 1260 | * Count the number of bytes in a given category. |
| 1261 | * |
| 1262 | * Returns a positive value on success, a negative error code otherwise. |
| 1263 | */ |
| 1264 | static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, |
| 1265 | int (*iter)(struct mtd_info *, |
| 1266 | int section, |
| 1267 | struct mtd_oob_region *oobregion)) |
| 1268 | { |
| 1269 | struct mtd_oob_region oobregion; |
| 1270 | int section = 0, ret, nbytes = 0; |
| 1271 | |
| 1272 | while (1) { |
| 1273 | ret = iter(mtd, section++, &oobregion); |
| 1274 | if (ret) { |
| 1275 | if (ret == -ERANGE) |
| 1276 | ret = nbytes; |
| 1277 | break; |
| 1278 | } |
| 1279 | |
| 1280 | nbytes += oobregion.length; |
| 1281 | } |
| 1282 | |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer |
| 1288 | * @mtd: mtd info structure |
| 1289 | * @eccbuf: destination buffer to store ECC bytes |
| 1290 | * @oobbuf: OOB buffer |
| 1291 | * @start: first ECC byte to retrieve |
| 1292 | * @nbytes: number of ECC bytes to retrieve |
| 1293 | * |
| 1294 | * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes. |
| 1295 | * |
| 1296 | * Returns zero on success, a negative error code otherwise. |
| 1297 | */ |
| 1298 | int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, |
| 1299 | const u8 *oobbuf, int start, int nbytes) |
| 1300 | { |
| 1301 | return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, |
| 1302 | mtd_ooblayout_ecc); |
| 1303 | } |
| 1304 | EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes); |
| 1305 | |
| 1306 | /** |
| 1307 | * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer |
| 1308 | * @mtd: mtd info structure |
| 1309 | * @eccbuf: source buffer to get ECC bytes from |
| 1310 | * @oobbuf: OOB buffer |
| 1311 | * @start: first ECC byte to set |
| 1312 | * @nbytes: number of ECC bytes to set |
| 1313 | * |
| 1314 | * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes. |
| 1315 | * |
| 1316 | * Returns zero on success, a negative error code otherwise. |
| 1317 | */ |
| 1318 | int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, |
| 1319 | u8 *oobbuf, int start, int nbytes) |
| 1320 | { |
| 1321 | return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, |
| 1322 | mtd_ooblayout_ecc); |
| 1323 | } |
| 1324 | EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes); |
| 1325 | |
| 1326 | /** |
| 1327 | * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer |
| 1328 | * @mtd: mtd info structure |
| 1329 | * @databuf: destination buffer to store ECC bytes |
| 1330 | * @oobbuf: OOB buffer |
| 1331 | * @start: first ECC byte to retrieve |
| 1332 | * @nbytes: number of ECC bytes to retrieve |
| 1333 | * |
| 1334 | * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. |
| 1335 | * |
| 1336 | * Returns zero on success, a negative error code otherwise. |
| 1337 | */ |
| 1338 | int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, |
| 1339 | const u8 *oobbuf, int start, int nbytes) |
| 1340 | { |
| 1341 | return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, |
| 1342 | mtd_ooblayout_free); |
| 1343 | } |
| 1344 | EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes); |
| 1345 | |
| 1346 | /** |
| 1347 | * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer |
| 1348 | * @mtd: mtd info structure |
| 1349 | * @eccbuf: source buffer to get data bytes from |
| 1350 | * @oobbuf: OOB buffer |
| 1351 | * @start: first ECC byte to set |
| 1352 | * @nbytes: number of ECC bytes to set |
| 1353 | * |
| 1354 | * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. |
| 1355 | * |
| 1356 | * Returns zero on success, a negative error code otherwise. |
| 1357 | */ |
| 1358 | int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, |
| 1359 | u8 *oobbuf, int start, int nbytes) |
| 1360 | { |
| 1361 | return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, |
| 1362 | mtd_ooblayout_free); |
| 1363 | } |
| 1364 | EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes); |
| 1365 | |
| 1366 | /** |
| 1367 | * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB |
| 1368 | * @mtd: mtd info structure |
| 1369 | * |
| 1370 | * Works like mtd_ooblayout_count_bytes(), except it count free bytes. |
| 1371 | * |
| 1372 | * Returns zero on success, a negative error code otherwise. |
| 1373 | */ |
| 1374 | int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) |
| 1375 | { |
| 1376 | return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); |
| 1377 | } |
| 1378 | EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes); |
| 1379 | |
| 1380 | /** |
| 1381 | * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB |
| 1382 | * @mtd: mtd info structure |
| 1383 | * |
| 1384 | * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes. |
| 1385 | * |
| 1386 | * Returns zero on success, a negative error code otherwise. |
| 1387 | */ |
| 1388 | int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) |
| 1389 | { |
| 1390 | return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); |
| 1391 | } |
| 1392 | EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes); |
| 1393 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1394 | /* |
| 1395 | * Method to access the protection register area, present in some flash |
| 1396 | * devices. The user data is one time programmable but the factory data is read |
| 1397 | * only. |
| 1398 | */ |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1399 | int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
| 1400 | struct otp_info *buf) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1401 | { |
| 1402 | if (!mtd->_get_fact_prot_info) |
| 1403 | return -EOPNOTSUPP; |
| 1404 | if (!len) |
| 1405 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1406 | return mtd->_get_fact_prot_info(mtd, len, retlen, buf); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1407 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1408 | EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1409 | |
| 1410 | int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, |
| 1411 | size_t *retlen, u_char *buf) |
| 1412 | { |
| 1413 | *retlen = 0; |
| 1414 | if (!mtd->_read_fact_prot_reg) |
| 1415 | return -EOPNOTSUPP; |
| 1416 | if (!len) |
| 1417 | return 0; |
| 1418 | return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf); |
| 1419 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1420 | EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1421 | |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1422 | int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
| 1423 | struct otp_info *buf) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1424 | { |
| 1425 | if (!mtd->_get_user_prot_info) |
| 1426 | return -EOPNOTSUPP; |
| 1427 | if (!len) |
| 1428 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1429 | return mtd->_get_user_prot_info(mtd, len, retlen, buf); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1430 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1431 | EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1432 | |
| 1433 | int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, |
| 1434 | size_t *retlen, u_char *buf) |
| 1435 | { |
| 1436 | *retlen = 0; |
| 1437 | if (!mtd->_read_user_prot_reg) |
| 1438 | return -EOPNOTSUPP; |
| 1439 | if (!len) |
| 1440 | return 0; |
| 1441 | return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf); |
| 1442 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1443 | EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1444 | |
| 1445 | int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, |
| 1446 | size_t *retlen, u_char *buf) |
| 1447 | { |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1448 | int ret; |
| 1449 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1450 | *retlen = 0; |
| 1451 | if (!mtd->_write_user_prot_reg) |
| 1452 | return -EOPNOTSUPP; |
| 1453 | if (!len) |
| 1454 | return 0; |
Heiko Schocher | 081fe9e | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 1455 | ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf); |
| 1456 | if (ret) |
| 1457 | return ret; |
| 1458 | |
| 1459 | /* |
| 1460 | * If no data could be written at all, we are out of memory and |
| 1461 | * must return -ENOSPC. |
| 1462 | */ |
| 1463 | return (*retlen) ? 0 : -ENOSPC; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1464 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1465 | EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1466 | |
| 1467 | int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) |
| 1468 | { |
| 1469 | if (!mtd->_lock_user_prot_reg) |
| 1470 | return -EOPNOTSUPP; |
| 1471 | if (!len) |
| 1472 | return 0; |
| 1473 | return mtd->_lock_user_prot_reg(mtd, from, len); |
| 1474 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1475 | EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1476 | |
| 1477 | /* Chip-supported device locking */ |
| 1478 | int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1479 | { |
| 1480 | if (!mtd->_lock) |
| 1481 | return -EOPNOTSUPP; |
| 1482 | if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) |
| 1483 | return -EINVAL; |
| 1484 | if (!len) |
| 1485 | return 0; |
| 1486 | return mtd->_lock(mtd, ofs, len); |
| 1487 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1488 | EXPORT_SYMBOL_GPL(mtd_lock); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1489 | |
| 1490 | int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1491 | { |
| 1492 | if (!mtd->_unlock) |
| 1493 | return -EOPNOTSUPP; |
| 1494 | if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) |
| 1495 | return -EINVAL; |
| 1496 | if (!len) |
| 1497 | return 0; |
| 1498 | return mtd->_unlock(mtd, ofs, len); |
| 1499 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1500 | EXPORT_SYMBOL_GPL(mtd_unlock); |
| 1501 | |
| 1502 | int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1503 | { |
| 1504 | if (!mtd->_is_locked) |
| 1505 | return -EOPNOTSUPP; |
| 1506 | if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) |
| 1507 | return -EINVAL; |
| 1508 | if (!len) |
| 1509 | return 0; |
| 1510 | return mtd->_is_locked(mtd, ofs, len); |
| 1511 | } |
| 1512 | EXPORT_SYMBOL_GPL(mtd_is_locked); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1513 | |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1514 | int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1515 | { |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1516 | if (ofs < 0 || ofs > mtd->size) |
| 1517 | return -EINVAL; |
| 1518 | if (!mtd->_block_isreserved) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1519 | return 0; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1520 | return mtd->_block_isreserved(mtd, ofs); |
| 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(mtd_block_isreserved); |
| 1523 | |
| 1524 | int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) |
| 1525 | { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1526 | if (ofs < 0 || ofs > mtd->size) |
| 1527 | return -EINVAL; |
Ezequiel Garcia | fc9d57c | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1528 | if (!mtd->_block_isbad) |
| 1529 | return 0; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1530 | return mtd->_block_isbad(mtd, ofs); |
| 1531 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1532 | EXPORT_SYMBOL_GPL(mtd_block_isbad); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 1533 | |
| 1534 | int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1535 | { |
| 1536 | if (!mtd->_block_markbad) |
| 1537 | return -EOPNOTSUPP; |
| 1538 | if (ofs < 0 || ofs > mtd->size) |
| 1539 | return -EINVAL; |
| 1540 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1541 | return -EROFS; |
| 1542 | return mtd->_block_markbad(mtd, ofs); |
| 1543 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1544 | EXPORT_SYMBOL_GPL(mtd_block_markbad); |
| 1545 | |
| 1546 | #ifndef __UBOOT__ |
| 1547 | /* |
| 1548 | * default_mtd_writev - the default writev method |
| 1549 | * @mtd: mtd device description object pointer |
| 1550 | * @vecs: the vectors to write |
| 1551 | * @count: count of vectors in @vecs |
| 1552 | * @to: the MTD device offset to write to |
| 1553 | * @retlen: on exit contains the count of bytes written to the MTD device. |
| 1554 | * |
| 1555 | * This function returns zero in case of success and a negative error code in |
| 1556 | * case of failure. |
| 1557 | */ |
| 1558 | static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 1559 | unsigned long count, loff_t to, size_t *retlen) |
| 1560 | { |
| 1561 | unsigned long i; |
| 1562 | size_t totlen = 0, thislen; |
| 1563 | int ret = 0; |
| 1564 | |
| 1565 | for (i = 0; i < count; i++) { |
| 1566 | if (!vecs[i].iov_len) |
| 1567 | continue; |
| 1568 | ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, |
| 1569 | vecs[i].iov_base); |
| 1570 | totlen += thislen; |
| 1571 | if (ret || thislen != vecs[i].iov_len) |
| 1572 | break; |
| 1573 | to += vecs[i].iov_len; |
| 1574 | } |
| 1575 | *retlen = totlen; |
| 1576 | return ret; |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * mtd_writev - the vector-based MTD write method |
| 1581 | * @mtd: mtd device description object pointer |
| 1582 | * @vecs: the vectors to write |
| 1583 | * @count: count of vectors in @vecs |
| 1584 | * @to: the MTD device offset to write to |
| 1585 | * @retlen: on exit contains the count of bytes written to the MTD device. |
| 1586 | * |
| 1587 | * This function returns zero in case of success and a negative error code in |
| 1588 | * case of failure. |
| 1589 | */ |
| 1590 | int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 1591 | unsigned long count, loff_t to, size_t *retlen) |
| 1592 | { |
| 1593 | *retlen = 0; |
| 1594 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 1595 | return -EROFS; |
| 1596 | if (!mtd->_writev) |
| 1597 | return default_mtd_writev(mtd, vecs, count, to, retlen); |
| 1598 | return mtd->_writev(mtd, vecs, count, to, retlen); |
| 1599 | } |
| 1600 | EXPORT_SYMBOL_GPL(mtd_writev); |
| 1601 | |
| 1602 | /** |
| 1603 | * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size |
| 1604 | * @mtd: mtd device description object pointer |
| 1605 | * @size: a pointer to the ideal or maximum size of the allocation, points |
| 1606 | * to the actual allocation size on success. |
| 1607 | * |
| 1608 | * This routine attempts to allocate a contiguous kernel buffer up to |
| 1609 | * the specified size, backing off the size of the request exponentially |
| 1610 | * until the request succeeds or until the allocation size falls below |
| 1611 | * the system page size. This attempts to make sure it does not adversely |
| 1612 | * impact system performance, so when allocating more than one page, we |
| 1613 | * ask the memory allocator to avoid re-trying, swapping, writing back |
| 1614 | * or performing I/O. |
| 1615 | * |
| 1616 | * Note, this function also makes sure that the allocated buffer is aligned to |
| 1617 | * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. |
| 1618 | * |
| 1619 | * This is called, for example by mtd_{read,write} and jffs2_scan_medium, |
| 1620 | * to handle smaller (i.e. degraded) buffer allocations under low- or |
| 1621 | * fragmented-memory situations where such reduced allocations, from a |
| 1622 | * requested ideal, are allowed. |
| 1623 | * |
| 1624 | * Returns a pointer to the allocated buffer on success; otherwise, NULL. |
| 1625 | */ |
| 1626 | void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) |
| 1627 | { |
| 1628 | gfp_t flags = __GFP_NOWARN | __GFP_WAIT | |
| 1629 | __GFP_NORETRY | __GFP_NO_KSWAPD; |
| 1630 | size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); |
| 1631 | void *kbuf; |
| 1632 | |
| 1633 | *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); |
| 1634 | |
| 1635 | while (*size > min_alloc) { |
| 1636 | kbuf = kmalloc(*size, flags); |
| 1637 | if (kbuf) |
| 1638 | return kbuf; |
| 1639 | |
| 1640 | *size >>= 1; |
| 1641 | *size = ALIGN(*size, mtd->writesize); |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * For the last resort allocation allow 'kmalloc()' to do all sorts of |
| 1646 | * things (write-back, dropping caches, etc) by using GFP_KERNEL. |
| 1647 | */ |
| 1648 | return kmalloc(*size, GFP_KERNEL); |
| 1649 | } |
| 1650 | EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); |
| 1651 | #endif |
| 1652 | |
| 1653 | #ifdef CONFIG_PROC_FS |
| 1654 | |
| 1655 | /*====================================================================*/ |
| 1656 | /* Support for /proc/mtd */ |
| 1657 | |
| 1658 | static int mtd_proc_show(struct seq_file *m, void *v) |
| 1659 | { |
| 1660 | struct mtd_info *mtd; |
| 1661 | |
| 1662 | seq_puts(m, "dev: size erasesize name\n"); |
| 1663 | mutex_lock(&mtd_table_mutex); |
| 1664 | mtd_for_each_device(mtd) { |
| 1665 | seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", |
| 1666 | mtd->index, (unsigned long long)mtd->size, |
| 1667 | mtd->erasesize, mtd->name); |
| 1668 | } |
| 1669 | mutex_unlock(&mtd_table_mutex); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static int mtd_proc_open(struct inode *inode, struct file *file) |
| 1674 | { |
| 1675 | return single_open(file, mtd_proc_show, NULL); |
| 1676 | } |
| 1677 | |
| 1678 | static const struct file_operations mtd_proc_ops = { |
| 1679 | .open = mtd_proc_open, |
| 1680 | .read = seq_read, |
| 1681 | .llseek = seq_lseek, |
| 1682 | .release = single_release, |
| 1683 | }; |
| 1684 | #endif /* CONFIG_PROC_FS */ |
| 1685 | |
| 1686 | /*====================================================================*/ |
| 1687 | /* Init code */ |
| 1688 | |
| 1689 | #ifndef __UBOOT__ |
| 1690 | static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name) |
| 1691 | { |
| 1692 | int ret; |
| 1693 | |
| 1694 | ret = bdi_init(bdi); |
| 1695 | if (!ret) |
| 1696 | ret = bdi_register(bdi, NULL, "%s", name); |
| 1697 | |
| 1698 | if (ret) |
| 1699 | bdi_destroy(bdi); |
| 1700 | |
| 1701 | return ret; |
| 1702 | } |
| 1703 | |
| 1704 | static struct proc_dir_entry *proc_mtd; |
| 1705 | |
| 1706 | static int __init init_mtd(void) |
| 1707 | { |
| 1708 | int ret; |
| 1709 | |
| 1710 | ret = class_register(&mtd_class); |
| 1711 | if (ret) |
| 1712 | goto err_reg; |
| 1713 | |
| 1714 | ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap"); |
| 1715 | if (ret) |
| 1716 | goto err_bdi1; |
| 1717 | |
| 1718 | ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap"); |
| 1719 | if (ret) |
| 1720 | goto err_bdi2; |
| 1721 | |
| 1722 | ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap"); |
| 1723 | if (ret) |
| 1724 | goto err_bdi3; |
| 1725 | |
| 1726 | proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops); |
| 1727 | |
| 1728 | ret = init_mtdchar(); |
| 1729 | if (ret) |
| 1730 | goto out_procfs; |
| 1731 | |
| 1732 | return 0; |
| 1733 | |
| 1734 | out_procfs: |
| 1735 | if (proc_mtd) |
| 1736 | remove_proc_entry("mtd", NULL); |
| 1737 | err_bdi3: |
| 1738 | bdi_destroy(&mtd_bdi_ro_mappable); |
| 1739 | err_bdi2: |
| 1740 | bdi_destroy(&mtd_bdi_unmappable); |
| 1741 | err_bdi1: |
| 1742 | class_unregister(&mtd_class); |
| 1743 | err_reg: |
| 1744 | pr_err("Error registering mtd class or bdi: %d\n", ret); |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
| 1748 | static void __exit cleanup_mtd(void) |
| 1749 | { |
| 1750 | cleanup_mtdchar(); |
| 1751 | if (proc_mtd) |
| 1752 | remove_proc_entry("mtd", NULL); |
| 1753 | class_unregister(&mtd_class); |
| 1754 | bdi_destroy(&mtd_bdi_unmappable); |
| 1755 | bdi_destroy(&mtd_bdi_ro_mappable); |
| 1756 | bdi_destroy(&mtd_bdi_rw_mappable); |
| 1757 | } |
| 1758 | |
| 1759 | module_init(init_mtd); |
| 1760 | module_exit(cleanup_mtd); |
| 1761 | #endif |
| 1762 | |
| 1763 | MODULE_LICENSE("GPL"); |
| 1764 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 1765 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |