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 | { |
| 51 | struct mtd_info *mtd = dev_to_mtd(dev); |
| 52 | |
| 53 | /* remove /dev/mtdXro node if needed */ |
| 54 | if (MTD_DEVT(mtd->index)) |
| 55 | device_destroy(mtd_class, MTD_DEVT(mtd->index) + 1); |
| 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 | } |
| 92 | static DEVICE_ATTR(mtd_type, S_IRUGO, mtd_type_show, NULL); |
| 93 | |
| 94 | static struct attribute *mtd_attrs[] = { |
| 95 | &dev_attr_mtd_type.attr, |
| 96 | /* FIXME provide a /proc/mtd superset */ |
| 97 | NULL, |
| 98 | }; |
| 99 | |
| 100 | struct attribute_group mtd_group = { |
| 101 | .attrs = mtd_attrs, |
| 102 | }; |
| 103 | |
| 104 | struct attribute_group *mtd_groups[] = { |
| 105 | &mtd_group, |
| 106 | NULL, |
| 107 | }; |
| 108 | |
| 109 | static struct device_type mtd_devtype = { |
| 110 | .name = "mtd", |
| 111 | .groups = mtd_groups, |
| 112 | .release = mtd_release, |
| 113 | }; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /** |
| 116 | * add_mtd_device - register an MTD device |
| 117 | * @mtd: pointer to new MTD device info structure |
| 118 | * |
| 119 | * Add a device to the list of MTD devices present in the system, and |
| 120 | * notify each currently active MTD 'user' of its arrival. Returns |
| 121 | * zero on success or 1 on failure, which currently will only happen |
| 122 | * 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^] | 123 | * or there's a sysfs error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | */ |
| 125 | |
| 126 | int add_mtd_device(struct mtd_info *mtd) |
| 127 | { |
| 128 | int i; |
| 129 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 130 | if (!mtd->backing_dev_info) { |
| 131 | switch (mtd->type) { |
| 132 | case MTD_RAM: |
| 133 | mtd->backing_dev_info = &mtd_bdi_rw_mappable; |
| 134 | break; |
| 135 | case MTD_ROM: |
| 136 | mtd->backing_dev_info = &mtd_bdi_ro_mappable; |
| 137 | break; |
| 138 | default: |
| 139 | mtd->backing_dev_info = &mtd_bdi_unmappable; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 144 | BUG_ON(mtd->writesize == 0); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 145 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | for (i=0; i < MAX_MTD_DEVICES; i++) |
| 148 | if (!mtd_table[i]) { |
matthias@kaehlcke.net | 2606c79 | 2008-05-31 15:28:09 +0200 | [diff] [blame] | 149 | struct mtd_notifier *not; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | mtd_table[i] = mtd; |
| 152 | mtd->index = i; |
| 153 | mtd->usecount = 0; |
| 154 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 155 | if (is_power_of_2(mtd->erasesize)) |
| 156 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; |
| 157 | else |
| 158 | mtd->erasesize_shift = 0; |
| 159 | |
| 160 | if (is_power_of_2(mtd->writesize)) |
| 161 | mtd->writesize_shift = ffs(mtd->writesize) - 1; |
| 162 | else |
| 163 | mtd->writesize_shift = 0; |
| 164 | |
| 165 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; |
| 166 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; |
| 167 | |
HÃ¥vard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 168 | /* Some chips always power up locked. Unlock them now */ |
| 169 | if ((mtd->flags & MTD_WRITEABLE) |
Justin Treon | e619a75 | 2008-01-30 10:25:49 -0800 | [diff] [blame] | 170 | && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) { |
HÃ¥vard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 171 | if (mtd->unlock(mtd, 0, mtd->size)) |
| 172 | printk(KERN_WARNING |
| 173 | "%s: unlock failed, " |
| 174 | "writes may not work\n", |
| 175 | mtd->name); |
| 176 | } |
| 177 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame^] | 178 | /* Caller should have set dev.parent to match the |
| 179 | * physical device. |
| 180 | */ |
| 181 | mtd->dev.type = &mtd_devtype; |
| 182 | mtd->dev.class = mtd_class; |
| 183 | mtd->dev.devt = MTD_DEVT(i); |
| 184 | dev_set_name(&mtd->dev, "mtd%d", i); |
| 185 | if (device_register(&mtd->dev) != 0) { |
| 186 | mtd_table[i] = NULL; |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | if (MTD_DEVT(i)) |
| 191 | device_create(mtd_class, mtd->dev.parent, |
| 192 | MTD_DEVT(i) + 1, |
| 193 | NULL, "mtd%dro", i); |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name); |
| 196 | /* No need to get a refcount on the module containing |
| 197 | the notifier, since we hold the mtd_table_mutex */ |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 198 | list_for_each_entry(not, &mtd_notifiers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | not->add(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 200 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 201 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* We _know_ we aren't being removed, because |
| 203 | our caller is still holding us here. So none |
| 204 | of this try_ nonsense, and no bitching about it |
| 205 | either. :) */ |
| 206 | __module_get(THIS_MODULE); |
| 207 | return 0; |
| 208 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 209 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 210 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * del_mtd_device - unregister an MTD device |
| 216 | * @mtd: pointer to MTD device info structure |
| 217 | * |
| 218 | * Remove a device from the list of MTD devices present in the system, |
| 219 | * and notify each currently active MTD 'user' of its departure. |
| 220 | * Returns zero on success or 1 on failure, which currently will happen |
| 221 | * if the requested device does not appear to be present in the list. |
| 222 | */ |
| 223 | |
| 224 | int del_mtd_device (struct mtd_info *mtd) |
| 225 | { |
| 226 | int ret; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 227 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 228 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | if (mtd_table[mtd->index] != mtd) { |
| 231 | ret = -ENODEV; |
| 232 | } else if (mtd->usecount) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 233 | 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] | 234 | mtd->index, mtd->name, mtd->usecount); |
| 235 | ret = -EBUSY; |
| 236 | } else { |
matthias@kaehlcke.net | 856613c | 2008-05-31 15:28:10 +0200 | [diff] [blame] | 237 | struct mtd_notifier *not; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | /* No need to get a refcount on the module containing |
| 240 | the notifier, since we hold the mtd_table_mutex */ |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 241 | list_for_each_entry(not, &mtd_notifiers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | not->remove(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | mtd_table[mtd->index] = NULL; |
| 245 | |
| 246 | module_put(THIS_MODULE); |
| 247 | ret = 0; |
| 248 | } |
| 249 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 250 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * register_mtd_user - register a 'user' of MTD devices. |
| 256 | * @new: pointer to notifier info structure |
| 257 | * |
| 258 | * Registers a pair of callbacks function to be called upon addition |
| 259 | * or removal of MTD devices. Causes the 'add' callback to be immediately |
| 260 | * invoked for each MTD device currently present in the system. |
| 261 | */ |
| 262 | |
| 263 | void register_mtd_user (struct mtd_notifier *new) |
| 264 | { |
| 265 | int i; |
| 266 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 267 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | list_add(&new->list, &mtd_notifiers); |
| 270 | |
| 271 | __module_get(THIS_MODULE); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | for (i=0; i< MAX_MTD_DEVICES; i++) |
| 274 | if (mtd_table[i]) |
| 275 | new->add(mtd_table[i]); |
| 276 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 277 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Artem B. Bityuckiy | 4945079 | 2005-02-18 14:34:54 +0000 | [diff] [blame] | 281 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
| 282 | * @old: pointer to notifier info structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * |
| 284 | * Removes a callback function pair from the list of 'users' to be |
| 285 | * notified upon addition or removal of MTD devices. Causes the |
| 286 | * 'remove' callback to be immediately invoked for each MTD device |
| 287 | * currently present in the system. |
| 288 | */ |
| 289 | |
| 290 | int unregister_mtd_user (struct mtd_notifier *old) |
| 291 | { |
| 292 | int i; |
| 293 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 294 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | module_put(THIS_MODULE); |
| 297 | |
| 298 | for (i=0; i< MAX_MTD_DEVICES; i++) |
| 299 | if (mtd_table[i]) |
| 300 | old->remove(mtd_table[i]); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | list_del(&old->list); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 303 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /** |
| 309 | * get_mtd_device - obtain a validated handle for an MTD device |
| 310 | * @mtd: last known address of the required MTD device |
| 311 | * @num: internal device number of the required MTD device |
| 312 | * |
| 313 | * Given a number and NULL address, return the num'th entry in the device |
| 314 | * table, if any. Given an address and num == -1, search the device table |
| 315 | * 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] | 316 | * both, return the num'th driver only if its address matches. Return |
| 317 | * error code if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 321 | { |
| 322 | struct mtd_info *ret = NULL; |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 323 | int i, err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 325 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | if (num == -1) { |
| 328 | for (i=0; i< MAX_MTD_DEVICES; i++) |
| 329 | if (mtd_table[i] == mtd) |
| 330 | ret = mtd_table[i]; |
| 331 | } else if (num < MAX_MTD_DEVICES) { |
| 332 | ret = mtd_table[num]; |
| 333 | if (mtd && mtd != ret) |
| 334 | ret = NULL; |
| 335 | } |
| 336 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 337 | if (!ret) |
| 338 | goto out_unlock; |
| 339 | |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 340 | if (!try_module_get(ret->owner)) |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 341 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 343 | if (ret->get_device) { |
| 344 | err = ret->get_device(ret); |
| 345 | if (err) |
| 346 | goto out_put; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 349 | ret->usecount++; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 350 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return ret; |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 352 | |
| 353 | out_put: |
| 354 | module_put(ret->owner); |
| 355 | out_unlock: |
| 356 | mutex_unlock(&mtd_table_mutex); |
| 357 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 360 | /** |
| 361 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 362 | * device name |
| 363 | * @name: MTD device name to open |
| 364 | * |
| 365 | * This function returns MTD device description structure in case of |
| 366 | * success and an error code in case of failure. |
| 367 | */ |
| 368 | |
| 369 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 370 | { |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 371 | int i, err = -ENODEV; |
| 372 | struct mtd_info *mtd = NULL; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 373 | |
| 374 | mutex_lock(&mtd_table_mutex); |
| 375 | |
| 376 | for (i = 0; i < MAX_MTD_DEVICES; i++) { |
| 377 | if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) { |
| 378 | mtd = mtd_table[i]; |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 383 | if (!mtd) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 384 | goto out_unlock; |
| 385 | |
| 386 | if (!try_module_get(mtd->owner)) |
| 387 | goto out_unlock; |
| 388 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 389 | if (mtd->get_device) { |
| 390 | err = mtd->get_device(mtd); |
| 391 | if (err) |
| 392 | goto out_put; |
| 393 | } |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 394 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 395 | mtd->usecount++; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 396 | mutex_unlock(&mtd_table_mutex); |
| 397 | return mtd; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 398 | |
| 399 | out_put: |
| 400 | module_put(mtd->owner); |
| 401 | out_unlock: |
| 402 | mutex_unlock(&mtd_table_mutex); |
| 403 | return ERR_PTR(err); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 404 | } |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | void put_mtd_device(struct mtd_info *mtd) |
| 407 | { |
| 408 | int c; |
| 409 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 410 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | c = --mtd->usecount; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 412 | if (mtd->put_device) |
| 413 | mtd->put_device(mtd); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 414 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | BUG_ON(c < 0); |
| 416 | |
| 417 | module_put(mtd->owner); |
| 418 | } |
| 419 | |
| 420 | /* default_mtd_writev - default mtd writev method for MTD devices that |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 421 | * don't implement their own |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | */ |
| 423 | |
| 424 | int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 425 | unsigned long count, loff_t to, size_t *retlen) |
| 426 | { |
| 427 | unsigned long i; |
| 428 | size_t totlen = 0, thislen; |
| 429 | int ret = 0; |
| 430 | |
| 431 | if(!mtd->write) { |
| 432 | ret = -EROFS; |
| 433 | } else { |
| 434 | for (i=0; i<count; i++) { |
| 435 | if (!vecs[i].iov_len) |
| 436 | continue; |
| 437 | ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base); |
| 438 | totlen += thislen; |
| 439 | if (ret || thislen != vecs[i].iov_len) |
| 440 | break; |
| 441 | to += vecs[i].iov_len; |
| 442 | } |
| 443 | } |
| 444 | if (retlen) |
| 445 | *retlen = totlen; |
| 446 | return ret; |
| 447 | } |
| 448 | |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 449 | EXPORT_SYMBOL_GPL(add_mtd_device); |
| 450 | EXPORT_SYMBOL_GPL(del_mtd_device); |
| 451 | EXPORT_SYMBOL_GPL(get_mtd_device); |
| 452 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
| 453 | EXPORT_SYMBOL_GPL(put_mtd_device); |
| 454 | EXPORT_SYMBOL_GPL(register_mtd_user); |
| 455 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
| 456 | EXPORT_SYMBOL_GPL(default_mtd_writev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame^] | 458 | static int __init mtd_setup(void) |
| 459 | { |
| 460 | mtd_class = class_create(THIS_MODULE, "mtd"); |
| 461 | |
| 462 | if (IS_ERR(mtd_class)) { |
| 463 | pr_err("Error creating mtd class.\n"); |
| 464 | return PTR_ERR(mtd_class); |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | core_initcall(mtd_setup); |
| 469 | |
| 470 | static void __exit mtd_teardown(void) |
| 471 | { |
| 472 | class_destroy(mtd_class); |
| 473 | } |
| 474 | __exitcall(mtd_teardown); |
| 475 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 476 | #ifdef CONFIG_PROC_FS |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /*====================================================================*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | /* Support for /proc/mtd */ |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | static struct proc_dir_entry *proc_mtd; |
| 482 | |
| 483 | static inline int mtd_proc_info (char *buf, int i) |
| 484 | { |
| 485 | struct mtd_info *this = mtd_table[i]; |
| 486 | |
| 487 | if (!this) |
| 488 | return 0; |
| 489 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 490 | return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i, |
| 491 | (unsigned long long)this->size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | this->erasesize, this->name); |
| 493 | } |
| 494 | |
| 495 | static int mtd_read_proc (char *page, char **start, off_t off, int count, |
| 496 | int *eof, void *data_unused) |
| 497 | { |
| 498 | int len, l, i; |
| 499 | off_t begin = 0; |
| 500 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 501 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | len = sprintf(page, "dev: size erasesize name\n"); |
| 504 | for (i=0; i< MAX_MTD_DEVICES; i++) { |
| 505 | |
| 506 | l = mtd_proc_info(page + len, i); |
| 507 | len += l; |
| 508 | if (len+begin > off+count) |
| 509 | goto done; |
| 510 | if (len+begin < off) { |
| 511 | begin += len; |
| 512 | len = 0; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | *eof = 1; |
| 517 | |
| 518 | done: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 519 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | if (off >= len+begin) |
| 521 | return 0; |
| 522 | *start = page + (off-begin); |
| 523 | return ((count < begin+len-off) ? count : begin+len-off); |
| 524 | } |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /*====================================================================*/ |
| 527 | /* Init code */ |
| 528 | |
| 529 | static int __init init_mtd(void) |
| 530 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if ((proc_mtd = create_proc_entry( "mtd", 0, NULL ))) |
| 532 | proc_mtd->read_proc = mtd_read_proc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static void __exit cleanup_mtd(void) |
| 537 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | if (proc_mtd) |
| 539 | remove_proc_entry( "mtd", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | module_init(init_mtd); |
| 543 | module_exit(cleanup_mtd); |
| 544 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 545 | #endif /* CONFIG_PROC_FS */ |
| 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | MODULE_LICENSE("GPL"); |
| 549 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 550 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |