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