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