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 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 26 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | should not use them for _anything_ else */ |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 28 | DEFINE_MUTEX(mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | struct mtd_info *mtd_table[MAX_MTD_DEVICES]; |
| 30 | |
| 31 | EXPORT_SYMBOL_GPL(mtd_table_mutex); |
| 32 | EXPORT_SYMBOL_GPL(mtd_table); |
| 33 | |
| 34 | static LIST_HEAD(mtd_notifiers); |
| 35 | |
| 36 | /** |
| 37 | * add_mtd_device - register an MTD device |
| 38 | * @mtd: pointer to new MTD device info structure |
| 39 | * |
| 40 | * Add a device to the list of MTD devices present in the system, and |
| 41 | * notify each currently active MTD 'user' of its arrival. Returns |
| 42 | * zero on success or 1 on failure, which currently will only happen |
| 43 | * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16) |
| 44 | */ |
| 45 | |
| 46 | int add_mtd_device(struct mtd_info *mtd) |
| 47 | { |
| 48 | int i; |
| 49 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame^] | 50 | if (!mtd->backing_dev_info) { |
| 51 | switch (mtd->type) { |
| 52 | case MTD_RAM: |
| 53 | mtd->backing_dev_info = &mtd_bdi_rw_mappable; |
| 54 | break; |
| 55 | case MTD_ROM: |
| 56 | mtd->backing_dev_info = &mtd_bdi_ro_mappable; |
| 57 | break; |
| 58 | default: |
| 59 | mtd->backing_dev_info = &mtd_bdi_unmappable; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
Artem B. Bityutskiy | 783ed81 | 2006-06-14 19:53:44 +0400 | [diff] [blame] | 64 | BUG_ON(mtd->writesize == 0); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 65 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | for (i=0; i < MAX_MTD_DEVICES; i++) |
| 68 | if (!mtd_table[i]) { |
matthias@kaehlcke.net | 2606c79 | 2008-05-31 15:28:09 +0200 | [diff] [blame] | 69 | struct mtd_notifier *not; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | mtd_table[i] = mtd; |
| 72 | mtd->index = i; |
| 73 | mtd->usecount = 0; |
| 74 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 75 | if (is_power_of_2(mtd->erasesize)) |
| 76 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; |
| 77 | else |
| 78 | mtd->erasesize_shift = 0; |
| 79 | |
| 80 | if (is_power_of_2(mtd->writesize)) |
| 81 | mtd->writesize_shift = ffs(mtd->writesize) - 1; |
| 82 | else |
| 83 | mtd->writesize_shift = 0; |
| 84 | |
| 85 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; |
| 86 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; |
| 87 | |
Håvard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 88 | /* Some chips always power up locked. Unlock them now */ |
| 89 | if ((mtd->flags & MTD_WRITEABLE) |
Justin Treon | e619a75 | 2008-01-30 10:25:49 -0800 | [diff] [blame] | 90 | && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) { |
Håvard Skinnemoen | 187ef15 | 2006-09-22 10:07:08 +0100 | [diff] [blame] | 91 | if (mtd->unlock(mtd, 0, mtd->size)) |
| 92 | printk(KERN_WARNING |
| 93 | "%s: unlock failed, " |
| 94 | "writes may not work\n", |
| 95 | mtd->name); |
| 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name); |
| 99 | /* No need to get a refcount on the module containing |
| 100 | the notifier, since we hold the mtd_table_mutex */ |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 101 | list_for_each_entry(not, &mtd_notifiers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | not->add(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 103 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 104 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* We _know_ we aren't being removed, because |
| 106 | our caller is still holding us here. So none |
| 107 | of this try_ nonsense, and no bitching about it |
| 108 | either. :) */ |
| 109 | __module_get(THIS_MODULE); |
| 110 | return 0; |
| 111 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 112 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 113 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * del_mtd_device - unregister an MTD device |
| 119 | * @mtd: pointer to MTD device info structure |
| 120 | * |
| 121 | * Remove a device from the list of MTD devices present in the system, |
| 122 | * and notify each currently active MTD 'user' of its departure. |
| 123 | * Returns zero on success or 1 on failure, which currently will happen |
| 124 | * if the requested device does not appear to be present in the list. |
| 125 | */ |
| 126 | |
| 127 | int del_mtd_device (struct mtd_info *mtd) |
| 128 | { |
| 129 | int ret; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 130 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 131 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | if (mtd_table[mtd->index] != mtd) { |
| 134 | ret = -ENODEV; |
| 135 | } else if (mtd->usecount) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 136 | 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] | 137 | mtd->index, mtd->name, mtd->usecount); |
| 138 | ret = -EBUSY; |
| 139 | } else { |
matthias@kaehlcke.net | 856613c | 2008-05-31 15:28:10 +0200 | [diff] [blame] | 140 | struct mtd_notifier *not; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* No need to get a refcount on the module containing |
| 143 | the notifier, since we hold the mtd_table_mutex */ |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 144 | list_for_each_entry(not, &mtd_notifiers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | not->remove(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | mtd_table[mtd->index] = NULL; |
| 148 | |
| 149 | module_put(THIS_MODULE); |
| 150 | ret = 0; |
| 151 | } |
| 152 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 153 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * register_mtd_user - register a 'user' of MTD devices. |
| 159 | * @new: pointer to notifier info structure |
| 160 | * |
| 161 | * Registers a pair of callbacks function to be called upon addition |
| 162 | * or removal of MTD devices. Causes the 'add' callback to be immediately |
| 163 | * invoked for each MTD device currently present in the system. |
| 164 | */ |
| 165 | |
| 166 | void register_mtd_user (struct mtd_notifier *new) |
| 167 | { |
| 168 | int i; |
| 169 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 170 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | list_add(&new->list, &mtd_notifiers); |
| 173 | |
| 174 | __module_get(THIS_MODULE); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | for (i=0; i< MAX_MTD_DEVICES; i++) |
| 177 | if (mtd_table[i]) |
| 178 | new->add(mtd_table[i]); |
| 179 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 180 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Artem B. Bityuckiy | 4945079 | 2005-02-18 14:34:54 +0000 | [diff] [blame] | 184 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
| 185 | * @old: pointer to notifier info structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * |
| 187 | * Removes a callback function pair from the list of 'users' to be |
| 188 | * notified upon addition or removal of MTD devices. Causes the |
| 189 | * 'remove' callback to be immediately invoked for each MTD device |
| 190 | * currently present in the system. |
| 191 | */ |
| 192 | |
| 193 | int unregister_mtd_user (struct mtd_notifier *old) |
| 194 | { |
| 195 | int i; |
| 196 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 197 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | module_put(THIS_MODULE); |
| 200 | |
| 201 | for (i=0; i< MAX_MTD_DEVICES; i++) |
| 202 | if (mtd_table[i]) |
| 203 | old->remove(mtd_table[i]); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | list_del(&old->list); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 206 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * get_mtd_device - obtain a validated handle for an MTD device |
| 213 | * @mtd: last known address of the required MTD device |
| 214 | * @num: internal device number of the required MTD device |
| 215 | * |
| 216 | * Given a number and NULL address, return the num'th entry in the device |
| 217 | * table, if any. Given an address and num == -1, search the device table |
| 218 | * 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] | 219 | * both, return the num'th driver only if its address matches. Return |
| 220 | * error code if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
| 224 | { |
| 225 | struct mtd_info *ret = NULL; |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 226 | int i, err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [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 (num == -1) { |
| 231 | for (i=0; i< MAX_MTD_DEVICES; i++) |
| 232 | if (mtd_table[i] == mtd) |
| 233 | ret = mtd_table[i]; |
| 234 | } else if (num < MAX_MTD_DEVICES) { |
| 235 | ret = mtd_table[num]; |
| 236 | if (mtd && mtd != ret) |
| 237 | ret = NULL; |
| 238 | } |
| 239 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 240 | if (!ret) |
| 241 | goto out_unlock; |
| 242 | |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 243 | if (!try_module_get(ret->owner)) |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 244 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 246 | if (ret->get_device) { |
| 247 | err = ret->get_device(ret); |
| 248 | if (err) |
| 249 | goto out_put; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 252 | ret->usecount++; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 253 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return ret; |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 255 | |
| 256 | out_put: |
| 257 | module_put(ret->owner); |
| 258 | out_unlock: |
| 259 | mutex_unlock(&mtd_table_mutex); |
| 260 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 263 | /** |
| 264 | * get_mtd_device_nm - obtain a validated handle for an MTD device by |
| 265 | * device name |
| 266 | * @name: MTD device name to open |
| 267 | * |
| 268 | * This function returns MTD device description structure in case of |
| 269 | * success and an error code in case of failure. |
| 270 | */ |
| 271 | |
| 272 | struct mtd_info *get_mtd_device_nm(const char *name) |
| 273 | { |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 274 | int i, err = -ENODEV; |
| 275 | struct mtd_info *mtd = NULL; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 276 | |
| 277 | mutex_lock(&mtd_table_mutex); |
| 278 | |
| 279 | for (i = 0; i < MAX_MTD_DEVICES; i++) { |
| 280 | if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) { |
| 281 | mtd = mtd_table[i]; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 286 | if (!mtd) |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 287 | goto out_unlock; |
| 288 | |
| 289 | if (!try_module_get(mtd->owner)) |
| 290 | goto out_unlock; |
| 291 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 292 | if (mtd->get_device) { |
| 293 | err = mtd->get_device(mtd); |
| 294 | if (err) |
| 295 | goto out_put; |
| 296 | } |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 297 | |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 298 | mtd->usecount++; |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 299 | mutex_unlock(&mtd_table_mutex); |
| 300 | return mtd; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 301 | |
| 302 | out_put: |
| 303 | module_put(mtd->owner); |
| 304 | out_unlock: |
| 305 | mutex_unlock(&mtd_table_mutex); |
| 306 | return ERR_PTR(err); |
Artem Bityutskiy | 7799308 | 2006-10-11 14:52:44 +0300 | [diff] [blame] | 307 | } |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | void put_mtd_device(struct mtd_info *mtd) |
| 310 | { |
| 311 | int c; |
| 312 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 313 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | c = --mtd->usecount; |
Artem Bityutskiy | 9fe912c | 2006-10-11 14:52:45 +0300 | [diff] [blame] | 315 | if (mtd->put_device) |
| 316 | mtd->put_device(mtd); |
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 | BUG_ON(c < 0); |
| 319 | |
| 320 | module_put(mtd->owner); |
| 321 | } |
| 322 | |
| 323 | /* default_mtd_writev - default mtd writev method for MTD devices that |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 324 | * don't implement their own |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | */ |
| 326 | |
| 327 | int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 328 | unsigned long count, loff_t to, size_t *retlen) |
| 329 | { |
| 330 | unsigned long i; |
| 331 | size_t totlen = 0, thislen; |
| 332 | int ret = 0; |
| 333 | |
| 334 | if(!mtd->write) { |
| 335 | ret = -EROFS; |
| 336 | } else { |
| 337 | for (i=0; i<count; i++) { |
| 338 | if (!vecs[i].iov_len) |
| 339 | continue; |
| 340 | ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base); |
| 341 | totlen += thislen; |
| 342 | if (ret || thislen != vecs[i].iov_len) |
| 343 | break; |
| 344 | to += vecs[i].iov_len; |
| 345 | } |
| 346 | } |
| 347 | if (retlen) |
| 348 | *retlen = totlen; |
| 349 | return ret; |
| 350 | } |
| 351 | |
David Woodhouse | dd36f26 | 2006-11-29 16:33:03 +0000 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(add_mtd_device); |
| 353 | EXPORT_SYMBOL_GPL(del_mtd_device); |
| 354 | EXPORT_SYMBOL_GPL(get_mtd_device); |
| 355 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
| 356 | EXPORT_SYMBOL_GPL(put_mtd_device); |
| 357 | EXPORT_SYMBOL_GPL(register_mtd_user); |
| 358 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
| 359 | EXPORT_SYMBOL_GPL(default_mtd_writev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 361 | #ifdef CONFIG_PROC_FS |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /*====================================================================*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* Support for /proc/mtd */ |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | static struct proc_dir_entry *proc_mtd; |
| 367 | |
| 368 | static inline int mtd_proc_info (char *buf, int i) |
| 369 | { |
| 370 | struct mtd_info *this = mtd_table[i]; |
| 371 | |
| 372 | if (!this) |
| 373 | return 0; |
| 374 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 375 | return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i, |
| 376 | (unsigned long long)this->size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | this->erasesize, this->name); |
| 378 | } |
| 379 | |
| 380 | static int mtd_read_proc (char *page, char **start, off_t off, int count, |
| 381 | int *eof, void *data_unused) |
| 382 | { |
| 383 | int len, l, i; |
| 384 | off_t begin = 0; |
| 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 | len = sprintf(page, "dev: size erasesize name\n"); |
| 389 | for (i=0; i< MAX_MTD_DEVICES; i++) { |
| 390 | |
| 391 | l = mtd_proc_info(page + len, i); |
| 392 | len += l; |
| 393 | if (len+begin > off+count) |
| 394 | goto done; |
| 395 | if (len+begin < off) { |
| 396 | begin += len; |
| 397 | len = 0; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | *eof = 1; |
| 402 | |
| 403 | done: |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 404 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (off >= len+begin) |
| 406 | return 0; |
| 407 | *start = page + (off-begin); |
| 408 | return ((count < begin+len-off) ? count : begin+len-off); |
| 409 | } |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | /*====================================================================*/ |
| 412 | /* Init code */ |
| 413 | |
| 414 | static int __init init_mtd(void) |
| 415 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if ((proc_mtd = create_proc_entry( "mtd", 0, NULL ))) |
| 417 | proc_mtd->read_proc = mtd_read_proc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static void __exit cleanup_mtd(void) |
| 422 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | if (proc_mtd) |
| 424 | remove_proc_entry( "mtd", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | module_init(init_mtd); |
| 428 | module_exit(cleanup_mtd); |
| 429 | |
Pavel Machek | 2d2dce0 | 2006-03-31 02:29:49 -0800 | [diff] [blame] | 430 | #endif /* CONFIG_PROC_FS */ |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | MODULE_LICENSE("GPL"); |
| 434 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 435 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |