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