Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Core registration and callback routines for MTD |
| 3 | * drivers and users. |
| 4 | * |
Jörn Engel | a33eb6b | 2010-04-27 09:40:52 +0200 | [diff] [blame] | 5 | * bdi bits are: |
| 6 | * Copyright © 2006 Red Hat, Inc. All Rights Reserved. |
| 7 | * Written by David Howells (dhowells@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/string.h> |
| 14 | #include <linux/timer.h> |
| 15 | #include <linux/major.h> |
| 16 | #include <linux/fs.h> |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 17 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/ioctl.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/mtd/compatmac.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/proc_fs.h> |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 22 | #include <linux/idr.h> |
Jörn Engel | a33eb6b | 2010-04-27 09:40:52 +0200 | [diff] [blame] | 23 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/mtd/mtd.h> |
| 26 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 27 | #include "mtdcore.h" |
Jörn Engel | a33eb6b | 2010-04-27 09:40:52 +0200 | [diff] [blame] | 28 | /* |
| 29 | * backing device capabilities for non-mappable devices (such as NAND flash) |
| 30 | * - permits private mappings, copies are taken of the data |
| 31 | */ |
| 32 | struct backing_dev_info mtd_bdi_unmappable = { |
| 33 | .capabilities = BDI_CAP_MAP_COPY, |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * backing device capabilities for R/O mappable devices (such as ROM) |
| 38 | * - permits private mappings, copies are taken of the data |
| 39 | * - permits non-writable shared mappings |
| 40 | */ |
| 41 | struct backing_dev_info mtd_bdi_ro_mappable = { |
| 42 | .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | |
| 43 | BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP), |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * backing device capabilities for writable mappable devices (such as RAM) |
| 48 | * - permits private mappings, copies are taken of the data |
| 49 | * - permits non-writable shared mappings |
| 50 | */ |
| 51 | struct backing_dev_info mtd_bdi_rw_mappable = { |
| 52 | .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | |
| 53 | BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP | |
| 54 | BDI_CAP_WRITE_MAP), |
| 55 | }; |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 56 | |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 57 | static int mtd_cls_suspend(struct device *dev, pm_message_t state); |
| 58 | static int mtd_cls_resume(struct device *dev); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 59 | |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 60 | static struct class mtd_class = { |
| 61 | .name = "mtd", |
| 62 | .owner = THIS_MODULE, |
| 63 | .suspend = mtd_cls_suspend, |
| 64 | .resume = mtd_cls_resume, |
| 65 | }; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 66 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 67 | static DEFINE_IDR(mtd_idr); |
| 68 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 69 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | should not use them for _anything_ else */ |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 71 | DEFINE_MUTEX(mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(mtd_table_mutex); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 73 | |
| 74 | struct mtd_info *__mtd_next_device(int i) |
| 75 | { |
| 76 | return idr_get_next(&mtd_idr, &i); |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(__mtd_next_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | static LIST_HEAD(mtd_notifiers); |
| 81 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 82 | |
| 83 | #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE) |
| 84 | #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) |
| 85 | #else |
| 86 | #define MTD_DEVT(index) 0 |
| 87 | #endif |
| 88 | |
| 89 | /* REVISIT once MTD uses the driver model better, whoever allocates |
| 90 | * the mtd_info will probably want to use the release() hook... |
| 91 | */ |
| 92 | static void mtd_release(struct device *dev) |
| 93 | { |
Denis V. Lunev | 2fdb114 | 2009-04-12 08:14:46 +0100 | [diff] [blame] | 94 | dev_t index = MTD_DEVT(dev_to_mtd(dev)->index); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 95 | |
| 96 | /* remove /dev/mtdXro node if needed */ |
Denis V. Lunev | 2fdb114 | 2009-04-12 08:14:46 +0100 | [diff] [blame] | 97 | if (index) |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 98 | device_destroy(&mtd_class, index + 1); |
| 99 | } |
| 100 | |
| 101 | static int mtd_cls_suspend(struct device *dev, pm_message_t state) |
| 102 | { |
| 103 | struct mtd_info *mtd = dev_to_mtd(dev); |
Saeed Bishara | 6afc4fd | 2009-07-28 04:56:43 -0700 | [diff] [blame] | 104 | |
| 105 | if (mtd && mtd->suspend) |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 106 | return mtd->suspend(mtd); |
| 107 | else |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int mtd_cls_resume(struct device *dev) |
| 112 | { |
| 113 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 114 | |
Saeed Bishara | 6afc4fd | 2009-07-28 04:56:43 -0700 | [diff] [blame] | 115 | if (mtd && mtd->resume) |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 116 | mtd->resume(mtd); |
| 117 | return 0; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static ssize_t mtd_type_show(struct device *dev, |
| 121 | struct device_attribute *attr, char *buf) |
| 122 | { |
| 123 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 124 | char *type; |
| 125 | |
| 126 | switch (mtd->type) { |
| 127 | case MTD_ABSENT: |
| 128 | type = "absent"; |
| 129 | break; |
| 130 | case MTD_RAM: |
| 131 | type = "ram"; |
| 132 | break; |
| 133 | case MTD_ROM: |
| 134 | type = "rom"; |
| 135 | break; |
| 136 | case MTD_NORFLASH: |
| 137 | type = "nor"; |
| 138 | break; |
| 139 | case MTD_NANDFLASH: |
| 140 | type = "nand"; |
| 141 | break; |
| 142 | case MTD_DATAFLASH: |
| 143 | type = "dataflash"; |
| 144 | break; |
| 145 | case MTD_UBIVOLUME: |
| 146 | type = "ubi"; |
| 147 | break; |
| 148 | default: |
| 149 | type = "unknown"; |
| 150 | } |
| 151 | |
| 152 | return snprintf(buf, PAGE_SIZE, "%s\n", type); |
| 153 | } |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 154 | static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); |
| 155 | |
| 156 | static ssize_t mtd_flags_show(struct device *dev, |
| 157 | struct device_attribute *attr, char *buf) |
| 158 | { |
| 159 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 160 | |
| 161 | return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); |
| 162 | |
| 163 | } |
| 164 | static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); |
| 165 | |
| 166 | static ssize_t mtd_size_show(struct device *dev, |
| 167 | struct device_attribute *attr, char *buf) |
| 168 | { |
| 169 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 170 | |
| 171 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
| 172 | (unsigned long long)mtd->size); |
| 173 | |
| 174 | } |
| 175 | static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); |
| 176 | |
| 177 | static ssize_t mtd_erasesize_show(struct device *dev, |
| 178 | struct device_attribute *attr, char *buf) |
| 179 | { |
| 180 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 181 | |
| 182 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); |
| 183 | |
| 184 | } |
| 185 | static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); |
| 186 | |
| 187 | static ssize_t mtd_writesize_show(struct device *dev, |
| 188 | struct device_attribute *attr, char *buf) |
| 189 | { |
| 190 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 191 | |
| 192 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); |
| 193 | |
| 194 | } |
| 195 | static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); |
| 196 | |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 197 | static ssize_t mtd_subpagesize_show(struct device *dev, |
| 198 | struct device_attribute *attr, char *buf) |
| 199 | { |
| 200 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 201 | unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 202 | |
| 203 | return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); |
| 204 | |
| 205 | } |
| 206 | static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); |
| 207 | |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 208 | static ssize_t mtd_oobsize_show(struct device *dev, |
| 209 | struct device_attribute *attr, char *buf) |
| 210 | { |
| 211 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 212 | |
| 213 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); |
| 214 | |
| 215 | } |
| 216 | static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); |
| 217 | |
| 218 | static ssize_t mtd_numeraseregions_show(struct device *dev, |
| 219 | struct device_attribute *attr, char *buf) |
| 220 | { |
| 221 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 222 | |
| 223 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); |
| 224 | |
| 225 | } |
| 226 | static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, |
| 227 | NULL); |
| 228 | |
| 229 | static ssize_t mtd_name_show(struct device *dev, |
| 230 | struct device_attribute *attr, char *buf) |
| 231 | { |
| 232 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 233 | |
| 234 | return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); |
| 235 | |
| 236 | } |
| 237 | static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 238 | |
| 239 | static struct attribute *mtd_attrs[] = { |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 240 | &dev_attr_type.attr, |
| 241 | &dev_attr_flags.attr, |
| 242 | &dev_attr_size.attr, |
| 243 | &dev_attr_erasesize.attr, |
| 244 | &dev_attr_writesize.attr, |
Artem Bityutskiy | e769354 | 2009-04-18 12:29:42 +0300 | [diff] [blame] | 245 | &dev_attr_subpagesize.attr, |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 246 | &dev_attr_oobsize.attr, |
| 247 | &dev_attr_numeraseregions.attr, |
| 248 | &dev_attr_name.attr, |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 249 | NULL, |
| 250 | }; |
| 251 | |
H Hartley Sweeten | fca9108 | 2009-08-06 16:05:32 -0700 | [diff] [blame] | 252 | static struct attribute_group mtd_group = { |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 253 | .attrs = mtd_attrs, |
| 254 | }; |
| 255 | |
David Woodhouse | 6469f54 | 2009-09-20 05:55:36 -0700 | [diff] [blame] | 256 | static const struct attribute_group *mtd_groups[] = { |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 257 | &mtd_group, |
| 258 | NULL, |
| 259 | }; |
| 260 | |
| 261 | static struct device_type mtd_devtype = { |
| 262 | .name = "mtd", |
| 263 | .groups = mtd_groups, |
| 264 | .release = mtd_release, |
| 265 | }; |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /** |
| 268 | * add_mtd_device - register an MTD device |
| 269 | * @mtd: pointer to new MTD device info structure |
| 270 | * |
| 271 | * Add a device to the list of MTD devices present in the system, and |
| 272 | * notify each currently active MTD 'user' of its arrival. Returns |
| 273 | * zero on success or 1 on failure, which currently will only happen |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 274 | * if there is insufficient memory or a sysfs error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | */ |
| 276 | |
| 277 | int add_mtd_device(struct mtd_info *mtd) |
| 278 | { |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 279 | struct mtd_notifier *not; |
| 280 | int i, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 282 | if (!mtd->backing_dev_info) { |
| 283 | switch (mtd->type) { |
| 284 | case MTD_RAM: |
| 285 | mtd->backing_dev_info = &mtd_bdi_rw_mappable; |
| 286 | break; |
| 287 | case MTD_ROM: |
| 288 | mtd->backing_dev_info = &mtd_bdi_ro_mappable; |
| 289 | break; |
| 290 | default: |
| 291 | mtd->backing_dev_info = &mtd_bdi_unmappable; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 296 | BUG_ON(mtd->writesize == 0); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 297 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 299 | do { |
| 300 | if (!idr_pre_get(&mtd_idr, GFP_KERNEL)) |
| 301 | goto fail_locked; |
| 302 | error = idr_get_new(&mtd_idr, mtd, &i); |
| 303 | } while (error == -EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 305 | if (error) |
| 306 | goto fail_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 308 | mtd->index = i; |
| 309 | mtd->usecount = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 310 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 311 | if (is_power_of_2(mtd->erasesize)) |
| 312 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; |
| 313 | else |
| 314 | mtd->erasesize_shift = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 315 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 316 | if (is_power_of_2(mtd->writesize)) |
| 317 | mtd->writesize_shift = ffs(mtd->writesize) - 1; |
| 318 | else |
| 319 | mtd->writesize_shift = 0; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 320 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 321 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; |
| 322 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; |
Håvard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 323 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 324 | /* Some chips always power up locked. Unlock them now */ |
| 325 | if ((mtd->flags & MTD_WRITEABLE) |
| 326 | && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) { |
| 327 | if (mtd->unlock(mtd, 0, mtd->size)) |
| 328 | printk(KERN_WARNING |
| 329 | "%s: unlock failed, writes may not work\n", |
| 330 | mtd->name); |
| 331 | } |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 332 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 333 | /* Caller should have set dev.parent to match the |
| 334 | * physical device. |
| 335 | */ |
| 336 | mtd->dev.type = &mtd_devtype; |
| 337 | mtd->dev.class = &mtd_class; |
| 338 | mtd->dev.devt = MTD_DEVT(i); |
| 339 | dev_set_name(&mtd->dev, "mtd%d", i); |
| 340 | dev_set_drvdata(&mtd->dev, mtd); |
| 341 | if (device_register(&mtd->dev) != 0) |
| 342 | goto fail_added; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 343 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 344 | if (MTD_DEVT(i)) |
| 345 | device_create(&mtd_class, mtd->dev.parent, |
| 346 | MTD_DEVT(i) + 1, |
| 347 | NULL, "mtd%dro", i); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 348 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 349 | DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name); |
| 350 | /* No need to get a refcount on the module containing |
| 351 | the notifier, since we hold the mtd_table_mutex */ |
| 352 | list_for_each_entry(not, &mtd_notifiers, list) |
| 353 | not->add(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 354 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 355 | mutex_unlock(&mtd_table_mutex); |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 356 | /* We _know_ we aren't being removed, because |
| 357 | our caller is still holding us here. So none |
| 358 | of this try_ nonsense, and no bitching about it |
| 359 | either. :) */ |
| 360 | __module_get(THIS_MODULE); |
| 361 | return 0; |
| 362 | |
| 363 | fail_added: |
| 364 | idr_remove(&mtd_idr, i); |
| 365 | fail_locked: |
| 366 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return 1; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * del_mtd_device - unregister an MTD device |
| 372 | * @mtd: pointer to MTD device info structure |
| 373 | * |
| 374 | * Remove a device from the list of MTD devices present in the system, |
| 375 | * and notify each currently active MTD 'user' of its departure. |
| 376 | * Returns zero on success or 1 on failure, which currently will happen |
| 377 | * if the requested device does not appear to be present in the list. |
| 378 | */ |
| 379 | |
| 380 | int del_mtd_device (struct mtd_info *mtd) |
| 381 | { |
| 382 | int ret; |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 383 | struct mtd_notifier *not; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 384 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 385 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 387 | if (idr_find(&mtd_idr, mtd->index) != mtd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | ret = -ENODEV; |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 389 | goto out_error; |
| 390 | } |
| 391 | |
| 392 | /* No need to get a refcount on the module containing |
| 393 | the notifier, since we hold the mtd_table_mutex */ |
| 394 | list_for_each_entry(not, &mtd_notifiers, list) |
| 395 | not->remove(mtd); |
| 396 | |
| 397 | if (mtd->usecount) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 398 | printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | mtd->index, mtd->name, mtd->usecount); |
| 400 | ret = -EBUSY; |
| 401 | } else { |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 402 | device_unregister(&mtd->dev); |
| 403 | |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 404 | idr_remove(&mtd_idr, mtd->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | module_put(THIS_MODULE); |
| 407 | ret = 0; |
| 408 | } |
| 409 | |
Maxim Levitsky | 75c0b84 | 2010-02-22 20:39:32 +0200 | [diff] [blame] | 410 | out_error: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 411 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * register_mtd_user - register a 'user' of MTD devices. |
| 417 | * @new: pointer to notifier info structure |
| 418 | * |
| 419 | * Registers a pair of callbacks function to be called upon addition |
| 420 | * or removal of MTD devices. Causes the 'add' callback to be immediately |
| 421 | * invoked for each MTD device currently present in the system. |
| 422 | */ |
| 423 | |
| 424 | void register_mtd_user (struct mtd_notifier *new) |
| 425 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 426 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 428 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | list_add(&new->list, &mtd_notifiers); |
| 431 | |
| 432 | __module_get(THIS_MODULE); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 433 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 434 | mtd_for_each_device(mtd) |
| 435 | new->add(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 437 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
Artem B. Bityuckiy | 4945079 | 2005-02-18 14:34:54 +0000 | [diff] [blame] | 441 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
| 442 | * @old: pointer to notifier info structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | * |
| 444 | * Removes a callback function pair from the list of 'users' to be |
| 445 | * notified upon addition or removal of MTD devices. Causes the |
| 446 | * 'remove' callback to be immediately invoked for each MTD device |
| 447 | * currently present in the system. |
| 448 | */ |
| 449 | |
| 450 | int unregister_mtd_user (struct mtd_notifier *old) |
| 451 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 452 | struct mtd_info *mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 454 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | module_put(THIS_MODULE); |
| 457 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 458 | mtd_for_each_device(mtd) |
| 459 | old->remove(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | list_del(&old->list); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 462 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /** |
| 468 | * get_mtd_device - obtain a validated handle for an MTD device |
| 469 | * @mtd: last known address of the required MTD device |
| 470 | * @num: internal device number of the required MTD device |
| 471 | * |
| 472 | * Given a number and NULL address, return the num'th entry in the device |
| 473 | * table, if any. Given an address and num == -1, search the device table |
| 474 | * for a device with that address and return if it's still present. Given |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 475 | * both, return the num'th driver only if its address matches. Return |
| 476 | * error code if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 480 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 481 | struct mtd_info *ret = NULL, *other; |
| 482 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 484 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | if (num == -1) { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 487 | mtd_for_each_device(other) { |
| 488 | if (other == mtd) { |
| 489 | ret = mtd; |
| 490 | break; |
| 491 | } |
| 492 | } |
Ben Hutchings | b520e41 | 2010-01-29 20:59:42 +0000 | [diff] [blame] | 493 | } else if (num >= 0) { |
| 494 | ret = idr_find(&mtd_idr, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (mtd && mtd != ret) |
| 496 | ret = NULL; |
| 497 | } |
| 498 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 499 | if (!ret) { |
| 500 | ret = ERR_PTR(err); |
| 501 | goto out; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 504 | err = __get_mtd_device(ret); |
| 505 | if (err) |
| 506 | ret = ERR_PTR(err); |
| 507 | out: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 508 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return ret; |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 510 | } |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 511 | |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 512 | |
| 513 | int __get_mtd_device(struct mtd_info *mtd) |
| 514 | { |
| 515 | int err; |
| 516 | |
| 517 | if (!try_module_get(mtd->owner)) |
| 518 | return -ENODEV; |
| 519 | |
| 520 | if (mtd->get_device) { |
| 521 | |
| 522 | err = mtd->get_device(mtd); |
| 523 | |
| 524 | if (err) { |
| 525 | module_put(mtd->owner); |
| 526 | return err; |
| 527 | } |
| 528 | } |
| 529 | mtd->usecount++; |
| 530 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 533 | /** |
| 534 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 535 | * device name |
| 536 | * @name: MTD device name to open |
| 537 | * |
| 538 | * This function returns MTD device description structure in case of |
| 539 | * success and an error code in case of failure. |
| 540 | */ |
| 541 | |
| 542 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 543 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 544 | int err = -ENODEV; |
| 545 | struct mtd_info *mtd = NULL, *other; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 546 | |
| 547 | mutex_lock(&mtd_table_mutex); |
| 548 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 549 | mtd_for_each_device(other) { |
| 550 | if (!strcmp(name, other->name)) { |
| 551 | mtd = other; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 556 | if (!mtd) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 557 | goto out_unlock; |
| 558 | |
| 559 | if (!try_module_get(mtd->owner)) |
| 560 | goto out_unlock; |
| 561 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 562 | if (mtd->get_device) { |
| 563 | err = mtd->get_device(mtd); |
| 564 | if (err) |
| 565 | goto out_put; |
| 566 | } |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 567 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 568 | mtd->usecount++; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 569 | mutex_unlock(&mtd_table_mutex); |
| 570 | return mtd; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 571 | |
| 572 | out_put: |
| 573 | module_put(mtd->owner); |
| 574 | out_unlock: |
| 575 | mutex_unlock(&mtd_table_mutex); |
| 576 | return ERR_PTR(err); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | void put_mtd_device(struct mtd_info *mtd) |
| 580 | { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 581 | mutex_lock(&mtd_table_mutex); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 582 | __put_mtd_device(mtd); |
| 583 | mutex_unlock(&mtd_table_mutex); |
| 584 | |
| 585 | } |
| 586 | |
| 587 | void __put_mtd_device(struct mtd_info *mtd) |
| 588 | { |
| 589 | --mtd->usecount; |
| 590 | BUG_ON(mtd->usecount < 0); |
| 591 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 592 | if (mtd->put_device) |
| 593 | mtd->put_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
| 595 | module_put(mtd->owner); |
| 596 | } |
| 597 | |
| 598 | /* default_mtd_writev - default mtd writev method for MTD devices that |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 599 | * don't implement their own |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | */ |
| 601 | |
| 602 | int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 603 | unsigned long count, loff_t to, size_t *retlen) |
| 604 | { |
| 605 | unsigned long i; |
| 606 | size_t totlen = 0, thislen; |
| 607 | int ret = 0; |
| 608 | |
| 609 | if(!mtd->write) { |
| 610 | ret = -EROFS; |
| 611 | } else { |
| 612 | for (i=0; i<count; i++) { |
| 613 | if (!vecs[i].iov_len) |
| 614 | continue; |
| 615 | ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base); |
| 616 | totlen += thislen; |
| 617 | if (ret || thislen != vecs[i].iov_len) |
| 618 | break; |
| 619 | to += vecs[i].iov_len; |
| 620 | } |
| 621 | } |
| 622 | if (retlen) |
| 623 | *retlen = totlen; |
| 624 | return ret; |
| 625 | } |
| 626 | |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 627 | EXPORT_SYMBOL_GPL(add_mtd_device); |
| 628 | EXPORT_SYMBOL_GPL(del_mtd_device); |
| 629 | EXPORT_SYMBOL_GPL(get_mtd_device); |
| 630 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 631 | EXPORT_SYMBOL_GPL(__get_mtd_device); |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 632 | EXPORT_SYMBOL_GPL(put_mtd_device); |
Maxim Levitsky | 3bd4565 | 2010-02-22 20:39:28 +0200 | [diff] [blame] | 633 | EXPORT_SYMBOL_GPL(__put_mtd_device); |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 634 | EXPORT_SYMBOL_GPL(register_mtd_user); |
| 635 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
| 636 | EXPORT_SYMBOL_GPL(default_mtd_writev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 638 | #ifdef CONFIG_PROC_FS |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | /*====================================================================*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | /* Support for /proc/mtd */ |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | static struct proc_dir_entry *proc_mtd; |
| 644 | |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 645 | static inline int mtd_proc_info(char *buf, struct mtd_info *this) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 647 | return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index, |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 648 | (unsigned long long)this->size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | this->erasesize, this->name); |
| 650 | } |
| 651 | |
| 652 | static int mtd_read_proc (char *page, char **start, off_t off, int count, |
| 653 | int *eof, void *data_unused) |
| 654 | { |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 655 | struct mtd_info *mtd; |
| 656 | int len, l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | off_t begin = 0; |
| 658 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 659 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | len = sprintf(page, "dev: size erasesize name\n"); |
Ben Hutchings | f1332ba | 2010-01-29 20:57:11 +0000 | [diff] [blame] | 662 | mtd_for_each_device(mtd) { |
| 663 | l = mtd_proc_info(page + len, mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | len += l; |
| 665 | if (len+begin > off+count) |
| 666 | goto done; |
| 667 | if (len+begin < off) { |
| 668 | begin += len; |
| 669 | len = 0; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | *eof = 1; |
| 674 | |
| 675 | done: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 676 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | if (off >= len+begin) |
| 678 | return 0; |
| 679 | *start = page + (off-begin); |
| 680 | return ((count < begin+len-off) ? count : begin+len-off); |
| 681 | } |
| 682 | |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 683 | #endif /* CONFIG_PROC_FS */ |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | /*====================================================================*/ |
| 686 | /* Init code */ |
| 687 | |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 688 | static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name) |
| 689 | { |
| 690 | int ret; |
| 691 | |
| 692 | ret = bdi_init(bdi); |
| 693 | if (!ret) |
| 694 | ret = bdi_register(bdi, NULL, name); |
| 695 | |
| 696 | if (ret) |
| 697 | bdi_destroy(bdi); |
| 698 | |
| 699 | return ret; |
| 700 | } |
| 701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | static int __init init_mtd(void) |
| 703 | { |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 704 | int ret; |
Kevin Cernekee | 694bb7f | 2009-04-03 13:00:45 -0700 | [diff] [blame] | 705 | |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 706 | ret = class_register(&mtd_class); |
| 707 | if (ret) |
| 708 | goto err_reg; |
| 709 | |
| 710 | ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap"); |
| 711 | if (ret) |
| 712 | goto err_bdi1; |
| 713 | |
| 714 | ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap"); |
| 715 | if (ret) |
| 716 | goto err_bdi2; |
| 717 | |
| 718 | ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap"); |
| 719 | if (ret) |
| 720 | goto err_bdi3; |
| 721 | |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 722 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if ((proc_mtd = create_proc_entry( "mtd", 0, NULL ))) |
| 724 | proc_mtd->read_proc = mtd_read_proc; |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 725 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | return 0; |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 727 | |
| 728 | err_bdi3: |
| 729 | bdi_destroy(&mtd_bdi_ro_mappable); |
| 730 | err_bdi2: |
| 731 | bdi_destroy(&mtd_bdi_unmappable); |
| 732 | err_bdi1: |
| 733 | class_unregister(&mtd_class); |
| 734 | err_reg: |
| 735 | pr_err("Error registering mtd class or bdi: %d\n", ret); |
| 736 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static void __exit cleanup_mtd(void) |
| 740 | { |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 741 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (proc_mtd) |
| 743 | remove_proc_entry( "mtd", NULL); |
Kevin Cernekee | 45b0907 | 2009-04-04 11:03:04 -0700 | [diff] [blame] | 744 | #endif /* CONFIG_PROC_FS */ |
David Woodhouse | 15bce40 | 2009-04-05 07:40:58 -0700 | [diff] [blame] | 745 | class_unregister(&mtd_class); |
Jens Axboe | 0661b1a | 2010-04-27 09:49:47 +0200 | [diff] [blame] | 746 | bdi_destroy(&mtd_bdi_unmappable); |
| 747 | bdi_destroy(&mtd_bdi_ro_mappable); |
| 748 | bdi_destroy(&mtd_bdi_rw_mappable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | module_init(init_mtd); |
| 752 | module_exit(cleanup_mtd); |
| 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | MODULE_LICENSE("GPL"); |
| 755 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 756 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |