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