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