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