Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Core registration and callback routines for MTD |
3 | * drivers and users. | |
4 | * | |
a1452a37 DW |
5 | * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> |
6 | * Copyright © 2006 Red Hat UK Limited | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | * | |
1da177e4 LT |
22 | */ |
23 | ||
1da177e4 LT |
24 | #include <linux/module.h> |
25 | #include <linux/kernel.h> | |
1da177e4 | 26 | #include <linux/ptrace.h> |
447d9bd8 | 27 | #include <linux/seq_file.h> |
1da177e4 LT |
28 | #include <linux/string.h> |
29 | #include <linux/timer.h> | |
30 | #include <linux/major.h> | |
31 | #include <linux/fs.h> | |
7799308f | 32 | #include <linux/err.h> |
1da177e4 LT |
33 | #include <linux/ioctl.h> |
34 | #include <linux/init.h> | |
1da177e4 | 35 | #include <linux/proc_fs.h> |
b520e412 | 36 | #include <linux/idr.h> |
a33eb6b9 | 37 | #include <linux/backing-dev.h> |
05d71b46 | 38 | #include <linux/gfp.h> |
0d01ff25 | 39 | #include <linux/slab.h> |
3efe41be | 40 | #include <linux/reboot.h> |
1da177e4 LT |
41 | |
42 | #include <linux/mtd/mtd.h> | |
f5671ab3 | 43 | #include <linux/mtd/partitions.h> |
1da177e4 | 44 | |
356d70f1 | 45 | #include "mtdcore.h" |
660685d9 | 46 | |
b4caecd4 | 47 | static struct backing_dev_info mtd_bdi = { |
a33eb6b9 | 48 | }; |
356d70f1 | 49 | |
15bce40c DW |
50 | static int mtd_cls_suspend(struct device *dev, pm_message_t state); |
51 | static int mtd_cls_resume(struct device *dev); | |
52 | ||
53 | static struct class mtd_class = { | |
54 | .name = "mtd", | |
55 | .owner = THIS_MODULE, | |
56 | .suspend = mtd_cls_suspend, | |
57 | .resume = mtd_cls_resume, | |
58 | }; | |
1f24b5a8 | 59 | |
b520e412 BH |
60 | static DEFINE_IDR(mtd_idr); |
61 | ||
97894cda | 62 | /* These are exported solely for the purpose of mtd_blkdevs.c. You |
1da177e4 | 63 | should not use them for _anything_ else */ |
48b19268 | 64 | DEFINE_MUTEX(mtd_table_mutex); |
1da177e4 | 65 | EXPORT_SYMBOL_GPL(mtd_table_mutex); |
b520e412 BH |
66 | |
67 | struct mtd_info *__mtd_next_device(int i) | |
68 | { | |
69 | return idr_get_next(&mtd_idr, &i); | |
70 | } | |
71 | EXPORT_SYMBOL_GPL(__mtd_next_device); | |
1da177e4 LT |
72 | |
73 | static LIST_HEAD(mtd_notifiers); | |
74 | ||
1f24b5a8 | 75 | |
1f24b5a8 | 76 | #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) |
1f24b5a8 DB |
77 | |
78 | /* REVISIT once MTD uses the driver model better, whoever allocates | |
79 | * the mtd_info will probably want to use the release() hook... | |
80 | */ | |
81 | static void mtd_release(struct device *dev) | |
82 | { | |
5e472128 | 83 | struct mtd_info *mtd = dev_get_drvdata(dev); |
d5de20a9 | 84 | dev_t index = MTD_DEVT(mtd->index); |
1f24b5a8 | 85 | |
5e472128 BN |
86 | /* remove /dev/mtdXro node */ |
87 | device_destroy(&mtd_class, index + 1); | |
15bce40c DW |
88 | } |
89 | ||
90 | static int mtd_cls_suspend(struct device *dev, pm_message_t state) | |
91 | { | |
d5de20a9 | 92 | struct mtd_info *mtd = dev_get_drvdata(dev); |
6afc4fdb | 93 | |
1a30871f | 94 | return mtd ? mtd_suspend(mtd) : 0; |
15bce40c DW |
95 | } |
96 | ||
97 | static int mtd_cls_resume(struct device *dev) | |
98 | { | |
d5de20a9 | 99 | struct mtd_info *mtd = dev_get_drvdata(dev); |
33c87b4a | 100 | |
3ee50141 | 101 | if (mtd) |
ead995f8 | 102 | mtd_resume(mtd); |
15bce40c | 103 | return 0; |
1f24b5a8 DB |
104 | } |
105 | ||
106 | static ssize_t mtd_type_show(struct device *dev, | |
107 | struct device_attribute *attr, char *buf) | |
108 | { | |
d5de20a9 | 109 | struct mtd_info *mtd = dev_get_drvdata(dev); |
1f24b5a8 DB |
110 | char *type; |
111 | ||
112 | switch (mtd->type) { | |
113 | case MTD_ABSENT: | |
114 | type = "absent"; | |
115 | break; | |
116 | case MTD_RAM: | |
117 | type = "ram"; | |
118 | break; | |
119 | case MTD_ROM: | |
120 | type = "rom"; | |
121 | break; | |
122 | case MTD_NORFLASH: | |
123 | type = "nor"; | |
124 | break; | |
125 | case MTD_NANDFLASH: | |
126 | type = "nand"; | |
127 | break; | |
128 | case MTD_DATAFLASH: | |
129 | type = "dataflash"; | |
130 | break; | |
131 | case MTD_UBIVOLUME: | |
132 | type = "ubi"; | |
133 | break; | |
f4837246 HS |
134 | case MTD_MLCNANDFLASH: |
135 | type = "mlc-nand"; | |
136 | break; | |
1f24b5a8 DB |
137 | default: |
138 | type = "unknown"; | |
139 | } | |
140 | ||
141 | return snprintf(buf, PAGE_SIZE, "%s\n", type); | |
142 | } | |
694bb7fc KC |
143 | static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); |
144 | ||
145 | static ssize_t mtd_flags_show(struct device *dev, | |
146 | struct device_attribute *attr, char *buf) | |
147 | { | |
d5de20a9 | 148 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
149 | |
150 | return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); | |
151 | ||
152 | } | |
153 | static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); | |
154 | ||
155 | static ssize_t mtd_size_show(struct device *dev, | |
156 | struct device_attribute *attr, char *buf) | |
157 | { | |
d5de20a9 | 158 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
159 | |
160 | return snprintf(buf, PAGE_SIZE, "%llu\n", | |
161 | (unsigned long long)mtd->size); | |
162 | ||
163 | } | |
164 | static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); | |
165 | ||
166 | static ssize_t mtd_erasesize_show(struct device *dev, | |
167 | struct device_attribute *attr, char *buf) | |
168 | { | |
d5de20a9 | 169 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
170 | |
171 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); | |
172 | ||
173 | } | |
174 | static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); | |
175 | ||
176 | static ssize_t mtd_writesize_show(struct device *dev, | |
177 | struct device_attribute *attr, char *buf) | |
178 | { | |
d5de20a9 | 179 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
180 | |
181 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); | |
182 | ||
183 | } | |
184 | static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); | |
185 | ||
e7693548 AB |
186 | static ssize_t mtd_subpagesize_show(struct device *dev, |
187 | struct device_attribute *attr, char *buf) | |
188 | { | |
d5de20a9 | 189 | struct mtd_info *mtd = dev_get_drvdata(dev); |
e7693548 AB |
190 | unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; |
191 | ||
192 | return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); | |
193 | ||
194 | } | |
195 | static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); | |
196 | ||
694bb7fc KC |
197 | static ssize_t mtd_oobsize_show(struct device *dev, |
198 | struct device_attribute *attr, char *buf) | |
199 | { | |
d5de20a9 | 200 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
201 | |
202 | return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); | |
203 | ||
204 | } | |
205 | static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); | |
206 | ||
207 | static ssize_t mtd_numeraseregions_show(struct device *dev, | |
208 | struct device_attribute *attr, char *buf) | |
209 | { | |
d5de20a9 | 210 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
211 | |
212 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); | |
213 | ||
214 | } | |
215 | static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, | |
216 | NULL); | |
217 | ||
218 | static ssize_t mtd_name_show(struct device *dev, | |
219 | struct device_attribute *attr, char *buf) | |
220 | { | |
d5de20a9 | 221 | struct mtd_info *mtd = dev_get_drvdata(dev); |
694bb7fc KC |
222 | |
223 | return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); | |
224 | ||
225 | } | |
226 | static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); | |
1f24b5a8 | 227 | |
a9b672e8 MD |
228 | static ssize_t mtd_ecc_strength_show(struct device *dev, |
229 | struct device_attribute *attr, char *buf) | |
230 | { | |
231 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
232 | ||
233 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); | |
234 | } | |
235 | static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); | |
236 | ||
d062d4ed MD |
237 | static ssize_t mtd_bitflip_threshold_show(struct device *dev, |
238 | struct device_attribute *attr, | |
239 | char *buf) | |
240 | { | |
241 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
242 | ||
243 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); | |
244 | } | |
245 | ||
246 | static ssize_t mtd_bitflip_threshold_store(struct device *dev, | |
247 | struct device_attribute *attr, | |
248 | const char *buf, size_t count) | |
249 | { | |
250 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
251 | unsigned int bitflip_threshold; | |
252 | int retval; | |
253 | ||
254 | retval = kstrtouint(buf, 0, &bitflip_threshold); | |
255 | if (retval) | |
256 | return retval; | |
257 | ||
258 | mtd->bitflip_threshold = bitflip_threshold; | |
259 | return count; | |
260 | } | |
261 | static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, | |
262 | mtd_bitflip_threshold_show, | |
263 | mtd_bitflip_threshold_store); | |
264 | ||
bf977e3f HS |
265 | static ssize_t mtd_ecc_step_size_show(struct device *dev, |
266 | struct device_attribute *attr, char *buf) | |
267 | { | |
268 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
269 | ||
270 | return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); | |
271 | ||
272 | } | |
273 | static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); | |
274 | ||
990a3af0 EG |
275 | static ssize_t mtd_ecc_stats_corrected_show(struct device *dev, |
276 | struct device_attribute *attr, char *buf) | |
277 | { | |
278 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
279 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; | |
280 | ||
281 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected); | |
282 | } | |
283 | static DEVICE_ATTR(corrected_bits, S_IRUGO, | |
284 | mtd_ecc_stats_corrected_show, NULL); | |
285 | ||
286 | static ssize_t mtd_ecc_stats_errors_show(struct device *dev, | |
287 | struct device_attribute *attr, char *buf) | |
288 | { | |
289 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
290 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; | |
291 | ||
292 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed); | |
293 | } | |
294 | static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL); | |
295 | ||
296 | static ssize_t mtd_badblocks_show(struct device *dev, | |
297 | struct device_attribute *attr, char *buf) | |
298 | { | |
299 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
300 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; | |
301 | ||
302 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks); | |
303 | } | |
304 | static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL); | |
305 | ||
306 | static ssize_t mtd_bbtblocks_show(struct device *dev, | |
307 | struct device_attribute *attr, char *buf) | |
308 | { | |
309 | struct mtd_info *mtd = dev_get_drvdata(dev); | |
310 | struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; | |
311 | ||
312 | return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks); | |
313 | } | |
314 | static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL); | |
315 | ||
1f24b5a8 | 316 | static struct attribute *mtd_attrs[] = { |
694bb7fc KC |
317 | &dev_attr_type.attr, |
318 | &dev_attr_flags.attr, | |
319 | &dev_attr_size.attr, | |
320 | &dev_attr_erasesize.attr, | |
321 | &dev_attr_writesize.attr, | |
e7693548 | 322 | &dev_attr_subpagesize.attr, |
694bb7fc KC |
323 | &dev_attr_oobsize.attr, |
324 | &dev_attr_numeraseregions.attr, | |
325 | &dev_attr_name.attr, | |
a9b672e8 | 326 | &dev_attr_ecc_strength.attr, |
bf977e3f | 327 | &dev_attr_ecc_step_size.attr, |
990a3af0 EG |
328 | &dev_attr_corrected_bits.attr, |
329 | &dev_attr_ecc_failures.attr, | |
330 | &dev_attr_bad_blocks.attr, | |
331 | &dev_attr_bbt_blocks.attr, | |
d062d4ed | 332 | &dev_attr_bitflip_threshold.attr, |
1f24b5a8 DB |
333 | NULL, |
334 | }; | |
54c738f6 | 335 | ATTRIBUTE_GROUPS(mtd); |
1f24b5a8 DB |
336 | |
337 | static struct device_type mtd_devtype = { | |
338 | .name = "mtd", | |
339 | .groups = mtd_groups, | |
340 | .release = mtd_release, | |
341 | }; | |
342 | ||
b4caecd4 CH |
343 | #ifndef CONFIG_MMU |
344 | unsigned mtd_mmap_capabilities(struct mtd_info *mtd) | |
345 | { | |
346 | switch (mtd->type) { | |
347 | case MTD_RAM: | |
348 | return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | | |
349 | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
350 | case MTD_ROM: | |
351 | return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | | |
352 | NOMMU_MAP_READ; | |
353 | default: | |
354 | return NOMMU_MAP_COPY; | |
355 | } | |
356 | } | |
706a4e5a | 357 | EXPORT_SYMBOL_GPL(mtd_mmap_capabilities); |
b4caecd4 CH |
358 | #endif |
359 | ||
3efe41be BN |
360 | static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state, |
361 | void *cmd) | |
362 | { | |
363 | struct mtd_info *mtd; | |
364 | ||
365 | mtd = container_of(n, struct mtd_info, reboot_notifier); | |
366 | mtd->_reboot(mtd); | |
367 | ||
368 | return NOTIFY_DONE; | |
369 | } | |
370 | ||
1da177e4 LT |
371 | /** |
372 | * add_mtd_device - register an MTD device | |
373 | * @mtd: pointer to new MTD device info structure | |
374 | * | |
375 | * Add a device to the list of MTD devices present in the system, and | |
376 | * notify each currently active MTD 'user' of its arrival. Returns | |
377 | * zero on success or 1 on failure, which currently will only happen | |
b520e412 | 378 | * if there is insufficient memory or a sysfs error. |
1da177e4 LT |
379 | */ |
380 | ||
381 | int add_mtd_device(struct mtd_info *mtd) | |
382 | { | |
b520e412 BH |
383 | struct mtd_notifier *not; |
384 | int i, error; | |
1da177e4 | 385 | |
b4caecd4 | 386 | mtd->backing_dev_info = &mtd_bdi; |
402d3265 | 387 | |
783ed81f | 388 | BUG_ON(mtd->writesize == 0); |
48b19268 | 389 | mutex_lock(&mtd_table_mutex); |
1da177e4 | 390 | |
589e9c4d TH |
391 | i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); |
392 | if (i < 0) | |
b520e412 | 393 | goto fail_locked; |
1f24b5a8 | 394 | |
b520e412 BH |
395 | mtd->index = i; |
396 | mtd->usecount = 0; | |
397 | ||
d062d4ed MD |
398 | /* default value if not set by driver */ |
399 | if (mtd->bitflip_threshold == 0) | |
400 | mtd->bitflip_threshold = mtd->ecc_strength; | |
401 | ||
b520e412 BH |
402 | if (is_power_of_2(mtd->erasesize)) |
403 | mtd->erasesize_shift = ffs(mtd->erasesize) - 1; | |
404 | else | |
405 | mtd->erasesize_shift = 0; | |
406 | ||
407 | if (is_power_of_2(mtd->writesize)) | |
408 | mtd->writesize_shift = ffs(mtd->writesize) - 1; | |
409 | else | |
410 | mtd->writesize_shift = 0; | |
411 | ||
412 | mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; | |
413 | mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; | |
414 | ||
415 | /* Some chips always power up locked. Unlock them now */ | |
38134565 AB |
416 | if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { |
417 | error = mtd_unlock(mtd, 0, mtd->size); | |
418 | if (error && error != -EOPNOTSUPP) | |
b520e412 BH |
419 | printk(KERN_WARNING |
420 | "%s: unlock failed, writes may not work\n", | |
421 | mtd->name); | |
422 | } | |
423 | ||
424 | /* Caller should have set dev.parent to match the | |
425 | * physical device. | |
426 | */ | |
427 | mtd->dev.type = &mtd_devtype; | |
428 | mtd->dev.class = &mtd_class; | |
429 | mtd->dev.devt = MTD_DEVT(i); | |
430 | dev_set_name(&mtd->dev, "mtd%d", i); | |
431 | dev_set_drvdata(&mtd->dev, mtd); | |
432 | if (device_register(&mtd->dev) != 0) | |
433 | goto fail_added; | |
434 | ||
5e472128 BN |
435 | device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, |
436 | "mtd%dro", i); | |
b520e412 | 437 | |
289c0522 | 438 | pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); |
b520e412 BH |
439 | /* No need to get a refcount on the module containing |
440 | the notifier, since we hold the mtd_table_mutex */ | |
441 | list_for_each_entry(not, &mtd_notifiers, list) | |
442 | not->add(mtd); | |
443 | ||
444 | mutex_unlock(&mtd_table_mutex); | |
445 | /* We _know_ we aren't being removed, because | |
446 | our caller is still holding us here. So none | |
447 | of this try_ nonsense, and no bitching about it | |
448 | either. :) */ | |
449 | __module_get(THIS_MODULE); | |
450 | return 0; | |
97894cda | 451 | |
b520e412 BH |
452 | fail_added: |
453 | idr_remove(&mtd_idr, i); | |
454 | fail_locked: | |
48b19268 | 455 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
456 | return 1; |
457 | } | |
458 | ||
459 | /** | |
460 | * del_mtd_device - unregister an MTD device | |
461 | * @mtd: pointer to MTD device info structure | |
462 | * | |
463 | * Remove a device from the list of MTD devices present in the system, | |
464 | * and notify each currently active MTD 'user' of its departure. | |
465 | * Returns zero on success or 1 on failure, which currently will happen | |
466 | * if the requested device does not appear to be present in the list. | |
467 | */ | |
468 | ||
eea72d5f | 469 | int del_mtd_device(struct mtd_info *mtd) |
1da177e4 LT |
470 | { |
471 | int ret; | |
75c0b84d | 472 | struct mtd_notifier *not; |
97894cda | 473 | |
48b19268 | 474 | mutex_lock(&mtd_table_mutex); |
1da177e4 | 475 | |
b520e412 | 476 | if (idr_find(&mtd_idr, mtd->index) != mtd) { |
1da177e4 | 477 | ret = -ENODEV; |
75c0b84d ML |
478 | goto out_error; |
479 | } | |
480 | ||
481 | /* No need to get a refcount on the module containing | |
482 | the notifier, since we hold the mtd_table_mutex */ | |
483 | list_for_each_entry(not, &mtd_notifiers, list) | |
484 | not->remove(mtd); | |
485 | ||
486 | if (mtd->usecount) { | |
97894cda | 487 | printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", |
1da177e4 LT |
488 | mtd->index, mtd->name, mtd->usecount); |
489 | ret = -EBUSY; | |
490 | } else { | |
694bb7fc KC |
491 | device_unregister(&mtd->dev); |
492 | ||
b520e412 | 493 | idr_remove(&mtd_idr, mtd->index); |
1da177e4 LT |
494 | |
495 | module_put(THIS_MODULE); | |
496 | ret = 0; | |
497 | } | |
498 | ||
75c0b84d | 499 | out_error: |
48b19268 | 500 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
501 | return ret; |
502 | } | |
503 | ||
1c4c215c DES |
504 | /** |
505 | * mtd_device_parse_register - parse partitions and register an MTD device. | |
506 | * | |
507 | * @mtd: the MTD device to register | |
508 | * @types: the list of MTD partition probes to try, see | |
509 | * 'parse_mtd_partitions()' for more information | |
c7975330 | 510 | * @parser_data: MTD partition parser-specific data |
1c4c215c DES |
511 | * @parts: fallback partition information to register, if parsing fails; |
512 | * only valid if %nr_parts > %0 | |
513 | * @nr_parts: the number of partitions in parts, if zero then the full | |
514 | * MTD device is registered if no partition info is found | |
515 | * | |
516 | * This function aggregates MTD partitions parsing (done by | |
517 | * 'parse_mtd_partitions()') and MTD device and partitions registering. It | |
518 | * basically follows the most common pattern found in many MTD drivers: | |
519 | * | |
520 | * * It first tries to probe partitions on MTD device @mtd using parsers | |
521 | * specified in @types (if @types is %NULL, then the default list of parsers | |
522 | * is used, see 'parse_mtd_partitions()' for more information). If none are | |
523 | * found this functions tries to fallback to information specified in | |
524 | * @parts/@nr_parts. | |
92394b5c | 525 | * * If any partitioning info was found, this function registers the found |
1c4c215c DES |
526 | * partitions. |
527 | * * If no partitions were found this function just registers the MTD device | |
528 | * @mtd and exits. | |
529 | * | |
530 | * Returns zero in case of success and a negative error code in case of failure. | |
531 | */ | |
26a47346 | 532 | int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, |
c7975330 | 533 | struct mtd_part_parser_data *parser_data, |
1c4c215c DES |
534 | const struct mtd_partition *parts, |
535 | int nr_parts) | |
536 | { | |
537 | int err; | |
538 | struct mtd_partition *real_parts; | |
539 | ||
c7975330 | 540 | err = parse_mtd_partitions(mtd, types, &real_parts, parser_data); |
4d523b60 | 541 | if (err <= 0 && nr_parts && parts) { |
1c4c215c DES |
542 | real_parts = kmemdup(parts, sizeof(*parts) * nr_parts, |
543 | GFP_KERNEL); | |
4d523b60 | 544 | if (!real_parts) |
1c4c215c | 545 | err = -ENOMEM; |
4d523b60 JL |
546 | else |
547 | err = nr_parts; | |
1c4c215c DES |
548 | } |
549 | ||
550 | if (err > 0) { | |
551 | err = add_mtd_partitions(mtd, real_parts, err); | |
552 | kfree(real_parts); | |
553 | } else if (err == 0) { | |
554 | err = add_mtd_device(mtd); | |
555 | if (err == 1) | |
556 | err = -ENODEV; | |
557 | } | |
558 | ||
e1dd8641 NC |
559 | /* |
560 | * FIXME: some drivers unfortunately call this function more than once. | |
561 | * So we have to check if we've already assigned the reboot notifier. | |
562 | * | |
563 | * Generally, we can make multiple calls work for most cases, but it | |
564 | * does cause problems with parse_mtd_partitions() above (e.g., | |
565 | * cmdlineparts will register partitions more than once). | |
566 | */ | |
567 | if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { | |
3efe41be BN |
568 | mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; |
569 | register_reboot_notifier(&mtd->reboot_notifier); | |
570 | } | |
571 | ||
1c4c215c DES |
572 | return err; |
573 | } | |
574 | EXPORT_SYMBOL_GPL(mtd_device_parse_register); | |
575 | ||
f5671ab3 JI |
576 | /** |
577 | * mtd_device_unregister - unregister an existing MTD device. | |
578 | * | |
579 | * @master: the MTD device to unregister. This will unregister both the master | |
580 | * and any partitions if registered. | |
581 | */ | |
582 | int mtd_device_unregister(struct mtd_info *master) | |
583 | { | |
584 | int err; | |
585 | ||
3efe41be BN |
586 | if (master->_reboot) |
587 | unregister_reboot_notifier(&master->reboot_notifier); | |
588 | ||
f5671ab3 JI |
589 | err = del_mtd_partitions(master); |
590 | if (err) | |
591 | return err; | |
592 | ||
593 | if (!device_is_registered(&master->dev)) | |
594 | return 0; | |
595 | ||
596 | return del_mtd_device(master); | |
597 | } | |
598 | EXPORT_SYMBOL_GPL(mtd_device_unregister); | |
599 | ||
1da177e4 LT |
600 | /** |
601 | * register_mtd_user - register a 'user' of MTD devices. | |
602 | * @new: pointer to notifier info structure | |
603 | * | |
604 | * Registers a pair of callbacks function to be called upon addition | |
605 | * or removal of MTD devices. Causes the 'add' callback to be immediately | |
606 | * invoked for each MTD device currently present in the system. | |
607 | */ | |
1da177e4 LT |
608 | void register_mtd_user (struct mtd_notifier *new) |
609 | { | |
f1332ba2 | 610 | struct mtd_info *mtd; |
1da177e4 | 611 | |
48b19268 | 612 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
613 | |
614 | list_add(&new->list, &mtd_notifiers); | |
615 | ||
d5ca5129 | 616 | __module_get(THIS_MODULE); |
97894cda | 617 | |
f1332ba2 BH |
618 | mtd_for_each_device(mtd) |
619 | new->add(mtd); | |
1da177e4 | 620 | |
48b19268 | 621 | mutex_unlock(&mtd_table_mutex); |
1da177e4 | 622 | } |
33c87b4a | 623 | EXPORT_SYMBOL_GPL(register_mtd_user); |
1da177e4 LT |
624 | |
625 | /** | |
49450795 AB |
626 | * unregister_mtd_user - unregister a 'user' of MTD devices. |
627 | * @old: pointer to notifier info structure | |
1da177e4 LT |
628 | * |
629 | * Removes a callback function pair from the list of 'users' to be | |
630 | * notified upon addition or removal of MTD devices. Causes the | |
631 | * 'remove' callback to be immediately invoked for each MTD device | |
632 | * currently present in the system. | |
633 | */ | |
1da177e4 LT |
634 | int unregister_mtd_user (struct mtd_notifier *old) |
635 | { | |
f1332ba2 | 636 | struct mtd_info *mtd; |
1da177e4 | 637 | |
48b19268 | 638 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
639 | |
640 | module_put(THIS_MODULE); | |
641 | ||
f1332ba2 BH |
642 | mtd_for_each_device(mtd) |
643 | old->remove(mtd); | |
97894cda | 644 | |
1da177e4 | 645 | list_del(&old->list); |
48b19268 | 646 | mutex_unlock(&mtd_table_mutex); |
1da177e4 LT |
647 | return 0; |
648 | } | |
33c87b4a | 649 | EXPORT_SYMBOL_GPL(unregister_mtd_user); |
1da177e4 LT |
650 | |
651 | /** | |
652 | * get_mtd_device - obtain a validated handle for an MTD device | |
653 | * @mtd: last known address of the required MTD device | |
654 | * @num: internal device number of the required MTD device | |
655 | * | |
656 | * Given a number and NULL address, return the num'th entry in the device | |
657 | * table, if any. Given an address and num == -1, search the device table | |
658 | * for a device with that address and return if it's still present. Given | |
9c74034f AB |
659 | * both, return the num'th driver only if its address matches. Return |
660 | * error code if not. | |
1da177e4 | 661 | */ |
1da177e4 LT |
662 | struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) |
663 | { | |
f1332ba2 BH |
664 | struct mtd_info *ret = NULL, *other; |
665 | int err = -ENODEV; | |
1da177e4 | 666 | |
48b19268 | 667 | mutex_lock(&mtd_table_mutex); |
1da177e4 LT |
668 | |
669 | if (num == -1) { | |
f1332ba2 BH |
670 | mtd_for_each_device(other) { |
671 | if (other == mtd) { | |
672 | ret = mtd; | |
673 | break; | |
674 | } | |
675 | } | |
b520e412 BH |
676 | } else if (num >= 0) { |
677 | ret = idr_find(&mtd_idr, num); | |
1da177e4 LT |
678 | if (mtd && mtd != ret) |
679 | ret = NULL; | |
680 | } | |
681 | ||
3bd45657 ML |
682 | if (!ret) { |
683 | ret = ERR_PTR(err); | |
684 | goto out; | |
9fe912ce | 685 | } |
1da177e4 | 686 | |
3bd45657 ML |
687 | err = __get_mtd_device(ret); |
688 | if (err) | |
689 | ret = ERR_PTR(err); | |
690 | out: | |
9c74034f AB |
691 | mutex_unlock(&mtd_table_mutex); |
692 | return ret; | |
3bd45657 | 693 | } |
33c87b4a | 694 | EXPORT_SYMBOL_GPL(get_mtd_device); |
1da177e4 | 695 | |
3bd45657 ML |
696 | |
697 | int __get_mtd_device(struct mtd_info *mtd) | |
698 | { | |
699 | int err; | |
700 | ||
701 | if (!try_module_get(mtd->owner)) | |
702 | return -ENODEV; | |
703 | ||
3c3c10bb AB |
704 | if (mtd->_get_device) { |
705 | err = mtd->_get_device(mtd); | |
3bd45657 ML |
706 | |
707 | if (err) { | |
708 | module_put(mtd->owner); | |
709 | return err; | |
710 | } | |
711 | } | |
712 | mtd->usecount++; | |
713 | return 0; | |
1da177e4 | 714 | } |
33c87b4a | 715 | EXPORT_SYMBOL_GPL(__get_mtd_device); |
1da177e4 | 716 | |
7799308f AB |
717 | /** |
718 | * get_mtd_device_nm - obtain a validated handle for an MTD device by | |
719 | * device name | |
720 | * @name: MTD device name to open | |
721 | * | |
722 | * This function returns MTD device description structure in case of | |
723 | * success and an error code in case of failure. | |
724 | */ | |
7799308f AB |
725 | struct mtd_info *get_mtd_device_nm(const char *name) |
726 | { | |
f1332ba2 BH |
727 | int err = -ENODEV; |
728 | struct mtd_info *mtd = NULL, *other; | |
7799308f AB |
729 | |
730 | mutex_lock(&mtd_table_mutex); | |
731 | ||
f1332ba2 BH |
732 | mtd_for_each_device(other) { |
733 | if (!strcmp(name, other->name)) { | |
734 | mtd = other; | |
7799308f AB |
735 | break; |
736 | } | |
737 | } | |
738 | ||
9fe912ce | 739 | if (!mtd) |
7799308f AB |
740 | goto out_unlock; |
741 | ||
52534f2d WG |
742 | err = __get_mtd_device(mtd); |
743 | if (err) | |
7799308f AB |
744 | goto out_unlock; |
745 | ||
9fe912ce AB |
746 | mutex_unlock(&mtd_table_mutex); |
747 | return mtd; | |
7799308f AB |
748 | |
749 | out_unlock: | |
750 | mutex_unlock(&mtd_table_mutex); | |
9fe912ce | 751 | return ERR_PTR(err); |
7799308f | 752 | } |
33c87b4a | 753 | EXPORT_SYMBOL_GPL(get_mtd_device_nm); |
7799308f | 754 | |
1da177e4 LT |
755 | void put_mtd_device(struct mtd_info *mtd) |
756 | { | |
48b19268 | 757 | mutex_lock(&mtd_table_mutex); |
3bd45657 ML |
758 | __put_mtd_device(mtd); |
759 | mutex_unlock(&mtd_table_mutex); | |
760 | ||
761 | } | |
33c87b4a | 762 | EXPORT_SYMBOL_GPL(put_mtd_device); |
3bd45657 ML |
763 | |
764 | void __put_mtd_device(struct mtd_info *mtd) | |
765 | { | |
766 | --mtd->usecount; | |
767 | BUG_ON(mtd->usecount < 0); | |
768 | ||
3c3c10bb AB |
769 | if (mtd->_put_device) |
770 | mtd->_put_device(mtd); | |
1da177e4 LT |
771 | |
772 | module_put(mtd->owner); | |
773 | } | |
33c87b4a | 774 | EXPORT_SYMBOL_GPL(__put_mtd_device); |
1da177e4 | 775 | |
8273a0c9 AB |
776 | /* |
777 | * Erase is an asynchronous operation. Device drivers are supposed | |
778 | * to call instr->callback() whenever the operation completes, even | |
779 | * if it completes with a failure. | |
780 | * Callers are supposed to pass a callback function and wait for it | |
781 | * to be called before writing to the block. | |
782 | */ | |
783 | int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) | |
784 | { | |
0c2b4e21 | 785 | if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) |
8273a0c9 | 786 | return -EINVAL; |
664addc2 AB |
787 | if (!(mtd->flags & MTD_WRITEABLE)) |
788 | return -EROFS; | |
3b27dac0 | 789 | instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; |
bcb1d238 AB |
790 | if (!instr->len) { |
791 | instr->state = MTD_ERASE_DONE; | |
792 | mtd_erase_callback(instr); | |
793 | return 0; | |
794 | } | |
8273a0c9 AB |
795 | return mtd->_erase(mtd, instr); |
796 | } | |
797 | EXPORT_SYMBOL_GPL(mtd_erase); | |
798 | ||
799 | /* | |
800 | * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. | |
801 | */ | |
802 | int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, | |
803 | void **virt, resource_size_t *phys) | |
804 | { | |
805 | *retlen = 0; | |
0dd5235f AB |
806 | *virt = NULL; |
807 | if (phys) | |
808 | *phys = 0; | |
8273a0c9 AB |
809 | if (!mtd->_point) |
810 | return -EOPNOTSUPP; | |
0c2b4e21 | 811 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
8273a0c9 | 812 | return -EINVAL; |
bcb1d238 AB |
813 | if (!len) |
814 | return 0; | |
8273a0c9 AB |
815 | return mtd->_point(mtd, from, len, retlen, virt, phys); |
816 | } | |
817 | EXPORT_SYMBOL_GPL(mtd_point); | |
818 | ||
819 | /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ | |
820 | int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) | |
821 | { | |
822 | if (!mtd->_point) | |
823 | return -EOPNOTSUPP; | |
0c2b4e21 | 824 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
8273a0c9 | 825 | return -EINVAL; |
bcb1d238 AB |
826 | if (!len) |
827 | return 0; | |
8273a0c9 AB |
828 | return mtd->_unpoint(mtd, from, len); |
829 | } | |
830 | EXPORT_SYMBOL_GPL(mtd_unpoint); | |
831 | ||
832 | /* | |
833 | * Allow NOMMU mmap() to directly map the device (if not NULL) | |
834 | * - return the address to which the offset maps | |
835 | * - return -ENOSYS to indicate refusal to do the mapping | |
836 | */ | |
837 | unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, | |
838 | unsigned long offset, unsigned long flags) | |
839 | { | |
840 | if (!mtd->_get_unmapped_area) | |
841 | return -EOPNOTSUPP; | |
0c2b4e21 | 842 | if (offset >= mtd->size || len > mtd->size - offset) |
8273a0c9 AB |
843 | return -EINVAL; |
844 | return mtd->_get_unmapped_area(mtd, len, offset, flags); | |
845 | } | |
846 | EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); | |
847 | ||
848 | int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, | |
849 | u_char *buf) | |
850 | { | |
edbc4540 | 851 | int ret_code; |
834247ec | 852 | *retlen = 0; |
0c2b4e21 | 853 | if (from < 0 || from >= mtd->size || len > mtd->size - from) |
8273a0c9 | 854 | return -EINVAL; |
bcb1d238 AB |
855 | if (!len) |
856 | return 0; | |
edbc4540 MD |
857 | |
858 | /* | |
859 | * In the absence of an error, drivers return a non-negative integer | |
860 | * representing the maximum number of bitflips that were corrected on | |
861 | * any one ecc region (if applicable; zero otherwise). | |
862 | */ | |
863 | ret_code = mtd->_read(mtd, from, len, retlen, buf); | |
864 | if (unlikely(ret_code < 0)) | |
865 | return ret_code; | |
866 | if (mtd->ecc_strength == 0) | |
867 | return 0; /* device lacks ecc */ | |
868 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; | |
8273a0c9 AB |
869 | } |
870 | EXPORT_SYMBOL_GPL(mtd_read); | |
871 | ||
872 | int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, | |
873 | const u_char *buf) | |
874 | { | |
875 | *retlen = 0; | |
0c2b4e21 | 876 | if (to < 0 || to >= mtd->size || len > mtd->size - to) |
8273a0c9 | 877 | return -EINVAL; |
664addc2 AB |
878 | if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE)) |
879 | return -EROFS; | |
bcb1d238 AB |
880 | if (!len) |
881 | return 0; | |
8273a0c9 AB |
882 | return mtd->_write(mtd, to, len, retlen, buf); |
883 | } | |
884 | EXPORT_SYMBOL_GPL(mtd_write); | |
885 | ||
886 | /* | |
887 | * In blackbox flight recorder like scenarios we want to make successful writes | |
888 | * in interrupt context. panic_write() is only intended to be called when its | |
889 | * known the kernel is about to panic and we need the write to succeed. Since | |
890 | * the kernel is not going to be running for much longer, this function can | |
891 | * break locks and delay to ensure the write succeeds (but not sleep). | |
892 | */ | |
893 | int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, | |
894 | const u_char *buf) | |
895 | { | |
896 | *retlen = 0; | |
897 | if (!mtd->_panic_write) | |
898 | return -EOPNOTSUPP; | |
0c2b4e21 | 899 | if (to < 0 || to >= mtd->size || len > mtd->size - to) |
8273a0c9 | 900 | return -EINVAL; |
664addc2 AB |
901 | if (!(mtd->flags & MTD_WRITEABLE)) |
902 | return -EROFS; | |
bcb1d238 AB |
903 | if (!len) |
904 | return 0; | |
8273a0c9 AB |
905 | return mtd->_panic_write(mtd, to, len, retlen, buf); |
906 | } | |
907 | EXPORT_SYMBOL_GPL(mtd_panic_write); | |
908 | ||
d2d48480 BN |
909 | int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) |
910 | { | |
e47f6858 | 911 | int ret_code; |
d2d48480 BN |
912 | ops->retlen = ops->oobretlen = 0; |
913 | if (!mtd->_read_oob) | |
914 | return -EOPNOTSUPP; | |
e47f6858 BN |
915 | /* |
916 | * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics | |
917 | * similar to mtd->_read(), returning a non-negative integer | |
918 | * representing max bitflips. In other cases, mtd->_read_oob() may | |
919 | * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). | |
920 | */ | |
921 | ret_code = mtd->_read_oob(mtd, from, ops); | |
922 | if (unlikely(ret_code < 0)) | |
923 | return ret_code; | |
924 | if (mtd->ecc_strength == 0) | |
925 | return 0; /* device lacks ecc */ | |
926 | return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; | |
d2d48480 BN |
927 | } |
928 | EXPORT_SYMBOL_GPL(mtd_read_oob); | |
929 | ||
de3cac93 AB |
930 | /* |
931 | * Method to access the protection register area, present in some flash | |
932 | * devices. The user data is one time programmable but the factory data is read | |
933 | * only. | |
934 | */ | |
4b78fc42 CR |
935 | int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
936 | struct otp_info *buf) | |
de3cac93 AB |
937 | { |
938 | if (!mtd->_get_fact_prot_info) | |
939 | return -EOPNOTSUPP; | |
940 | if (!len) | |
941 | return 0; | |
4b78fc42 | 942 | return mtd->_get_fact_prot_info(mtd, len, retlen, buf); |
de3cac93 AB |
943 | } |
944 | EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); | |
945 | ||
946 | int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, | |
947 | size_t *retlen, u_char *buf) | |
948 | { | |
949 | *retlen = 0; | |
950 | if (!mtd->_read_fact_prot_reg) | |
951 | return -EOPNOTSUPP; | |
952 | if (!len) | |
953 | return 0; | |
954 | return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf); | |
955 | } | |
956 | EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); | |
957 | ||
4b78fc42 CR |
958 | int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, |
959 | struct otp_info *buf) | |
de3cac93 AB |
960 | { |
961 | if (!mtd->_get_user_prot_info) | |
962 | return -EOPNOTSUPP; | |
963 | if (!len) | |
964 | return 0; | |
4b78fc42 | 965 | return mtd->_get_user_prot_info(mtd, len, retlen, buf); |
de3cac93 AB |
966 | } |
967 | EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); | |
968 | ||
969 | int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, | |
970 | size_t *retlen, u_char *buf) | |
971 | { | |
972 | *retlen = 0; | |
973 | if (!mtd->_read_user_prot_reg) | |
974 | return -EOPNOTSUPP; | |
975 | if (!len) | |
976 | return 0; | |
977 | return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf); | |
978 | } | |
979 | EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); | |
980 | ||
981 | int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, | |
982 | size_t *retlen, u_char *buf) | |
983 | { | |
9a78bc83 CR |
984 | int ret; |
985 | ||
de3cac93 AB |
986 | *retlen = 0; |
987 | if (!mtd->_write_user_prot_reg) | |
988 | return -EOPNOTSUPP; | |
989 | if (!len) | |
990 | return 0; | |
9a78bc83 CR |
991 | ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf); |
992 | if (ret) | |
993 | return ret; | |
994 | ||
995 | /* | |
996 | * If no data could be written at all, we are out of memory and | |
997 | * must return -ENOSPC. | |
998 | */ | |
999 | return (*retlen) ? 0 : -ENOSPC; | |
de3cac93 AB |
1000 | } |
1001 | EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); | |
1002 | ||
1003 | int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) | |
1004 | { | |
1005 | if (!mtd->_lock_user_prot_reg) | |
1006 | return -EOPNOTSUPP; | |
1007 | if (!len) | |
1008 | return 0; | |
1009 | return mtd->_lock_user_prot_reg(mtd, from, len); | |
1010 | } | |
1011 | EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); | |
1012 | ||
8273a0c9 AB |
1013 | /* Chip-supported device locking */ |
1014 | int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
1015 | { | |
1016 | if (!mtd->_lock) | |
1017 | return -EOPNOTSUPP; | |
0c2b4e21 | 1018 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
8273a0c9 | 1019 | return -EINVAL; |
bcb1d238 AB |
1020 | if (!len) |
1021 | return 0; | |
8273a0c9 AB |
1022 | return mtd->_lock(mtd, ofs, len); |
1023 | } | |
1024 | EXPORT_SYMBOL_GPL(mtd_lock); | |
1025 | ||
1026 | int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
1027 | { | |
1028 | if (!mtd->_unlock) | |
1029 | return -EOPNOTSUPP; | |
0c2b4e21 | 1030 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
8273a0c9 | 1031 | return -EINVAL; |
bcb1d238 AB |
1032 | if (!len) |
1033 | return 0; | |
8273a0c9 AB |
1034 | return mtd->_unlock(mtd, ofs, len); |
1035 | } | |
1036 | EXPORT_SYMBOL_GPL(mtd_unlock); | |
1037 | ||
1038 | int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) | |
1039 | { | |
1040 | if (!mtd->_is_locked) | |
1041 | return -EOPNOTSUPP; | |
0c2b4e21 | 1042 | if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) |
8273a0c9 | 1043 | return -EINVAL; |
bcb1d238 AB |
1044 | if (!len) |
1045 | return 0; | |
8273a0c9 AB |
1046 | return mtd->_is_locked(mtd, ofs, len); |
1047 | } | |
1048 | EXPORT_SYMBOL_GPL(mtd_is_locked); | |
1049 | ||
8471bb73 | 1050 | int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
8273a0c9 | 1051 | { |
0c2b4e21 | 1052 | if (ofs < 0 || ofs >= mtd->size) |
8471bb73 EG |
1053 | return -EINVAL; |
1054 | if (!mtd->_block_isreserved) | |
8273a0c9 | 1055 | return 0; |
8471bb73 EG |
1056 | return mtd->_block_isreserved(mtd, ofs); |
1057 | } | |
1058 | EXPORT_SYMBOL_GPL(mtd_block_isreserved); | |
1059 | ||
1060 | int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) | |
1061 | { | |
0c2b4e21 | 1062 | if (ofs < 0 || ofs >= mtd->size) |
8273a0c9 | 1063 | return -EINVAL; |
8471bb73 EG |
1064 | if (!mtd->_block_isbad) |
1065 | return 0; | |
8273a0c9 AB |
1066 | return mtd->_block_isbad(mtd, ofs); |
1067 | } | |
1068 | EXPORT_SYMBOL_GPL(mtd_block_isbad); | |
1069 | ||
1070 | int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
1071 | { | |
1072 | if (!mtd->_block_markbad) | |
1073 | return -EOPNOTSUPP; | |
0c2b4e21 | 1074 | if (ofs < 0 || ofs >= mtd->size) |
8273a0c9 | 1075 | return -EINVAL; |
664addc2 AB |
1076 | if (!(mtd->flags & MTD_WRITEABLE)) |
1077 | return -EROFS; | |
8273a0c9 AB |
1078 | return mtd->_block_markbad(mtd, ofs); |
1079 | } | |
1080 | EXPORT_SYMBOL_GPL(mtd_block_markbad); | |
1081 | ||
52b02031 AB |
1082 | /* |
1083 | * default_mtd_writev - the default writev method | |
1084 | * @mtd: mtd device description object pointer | |
1085 | * @vecs: the vectors to write | |
1086 | * @count: count of vectors in @vecs | |
1087 | * @to: the MTD device offset to write to | |
1088 | * @retlen: on exit contains the count of bytes written to the MTD device. | |
1089 | * | |
1090 | * This function returns zero in case of success and a negative error code in | |
1091 | * case of failure. | |
1da177e4 | 1092 | */ |
1dbebd32 AB |
1093 | static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, |
1094 | unsigned long count, loff_t to, size_t *retlen) | |
1da177e4 LT |
1095 | { |
1096 | unsigned long i; | |
1097 | size_t totlen = 0, thislen; | |
1098 | int ret = 0; | |
1099 | ||
52b02031 AB |
1100 | for (i = 0; i < count; i++) { |
1101 | if (!vecs[i].iov_len) | |
1102 | continue; | |
1103 | ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, | |
1104 | vecs[i].iov_base); | |
1105 | totlen += thislen; | |
1106 | if (ret || thislen != vecs[i].iov_len) | |
1107 | break; | |
1108 | to += vecs[i].iov_len; | |
1da177e4 | 1109 | } |
52b02031 | 1110 | *retlen = totlen; |
1da177e4 LT |
1111 | return ret; |
1112 | } | |
1dbebd32 AB |
1113 | |
1114 | /* | |
1115 | * mtd_writev - the vector-based MTD write method | |
1116 | * @mtd: mtd device description object pointer | |
1117 | * @vecs: the vectors to write | |
1118 | * @count: count of vectors in @vecs | |
1119 | * @to: the MTD device offset to write to | |
1120 | * @retlen: on exit contains the count of bytes written to the MTD device. | |
1121 | * | |
1122 | * This function returns zero in case of success and a negative error code in | |
1123 | * case of failure. | |
1124 | */ | |
1125 | int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, | |
1126 | unsigned long count, loff_t to, size_t *retlen) | |
1127 | { | |
1128 | *retlen = 0; | |
664addc2 AB |
1129 | if (!(mtd->flags & MTD_WRITEABLE)) |
1130 | return -EROFS; | |
3c3c10bb | 1131 | if (!mtd->_writev) |
1dbebd32 | 1132 | return default_mtd_writev(mtd, vecs, count, to, retlen); |
3c3c10bb | 1133 | return mtd->_writev(mtd, vecs, count, to, retlen); |
1dbebd32 AB |
1134 | } |
1135 | EXPORT_SYMBOL_GPL(mtd_writev); | |
1da177e4 | 1136 | |
33b53716 GE |
1137 | /** |
1138 | * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size | |
52b02031 AB |
1139 | * @mtd: mtd device description object pointer |
1140 | * @size: a pointer to the ideal or maximum size of the allocation, points | |
33b53716 GE |
1141 | * to the actual allocation size on success. |
1142 | * | |
1143 | * This routine attempts to allocate a contiguous kernel buffer up to | |
1144 | * the specified size, backing off the size of the request exponentially | |
1145 | * until the request succeeds or until the allocation size falls below | |
1146 | * the system page size. This attempts to make sure it does not adversely | |
1147 | * impact system performance, so when allocating more than one page, we | |
caf49191 LT |
1148 | * ask the memory allocator to avoid re-trying, swapping, writing back |
1149 | * or performing I/O. | |
33b53716 GE |
1150 | * |
1151 | * Note, this function also makes sure that the allocated buffer is aligned to | |
1152 | * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. | |
1153 | * | |
1154 | * This is called, for example by mtd_{read,write} and jffs2_scan_medium, | |
1155 | * to handle smaller (i.e. degraded) buffer allocations under low- or | |
1156 | * fragmented-memory situations where such reduced allocations, from a | |
1157 | * requested ideal, are allowed. | |
1158 | * | |
1159 | * Returns a pointer to the allocated buffer on success; otherwise, NULL. | |
1160 | */ | |
1161 | void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) | |
1162 | { | |
caf49191 LT |
1163 | gfp_t flags = __GFP_NOWARN | __GFP_WAIT | |
1164 | __GFP_NORETRY | __GFP_NO_KSWAPD; | |
33b53716 GE |
1165 | size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); |
1166 | void *kbuf; | |
1167 | ||
1168 | *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); | |
1169 | ||
1170 | while (*size > min_alloc) { | |
1171 | kbuf = kmalloc(*size, flags); | |
1172 | if (kbuf) | |
1173 | return kbuf; | |
1174 | ||
1175 | *size >>= 1; | |
1176 | *size = ALIGN(*size, mtd->writesize); | |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * For the last resort allocation allow 'kmalloc()' to do all sorts of | |
1181 | * things (write-back, dropping caches, etc) by using GFP_KERNEL. | |
1182 | */ | |
1183 | return kmalloc(*size, GFP_KERNEL); | |
1184 | } | |
33b53716 | 1185 | EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); |
1da177e4 | 1186 | |
2d2dce0e PM |
1187 | #ifdef CONFIG_PROC_FS |
1188 | ||
1da177e4 LT |
1189 | /*====================================================================*/ |
1190 | /* Support for /proc/mtd */ | |
1191 | ||
447d9bd8 | 1192 | static int mtd_proc_show(struct seq_file *m, void *v) |
1da177e4 | 1193 | { |
f1332ba2 | 1194 | struct mtd_info *mtd; |
1da177e4 | 1195 | |
447d9bd8 | 1196 | seq_puts(m, "dev: size erasesize name\n"); |
48b19268 | 1197 | mutex_lock(&mtd_table_mutex); |
f1332ba2 | 1198 | mtd_for_each_device(mtd) { |
447d9bd8 AD |
1199 | seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", |
1200 | mtd->index, (unsigned long long)mtd->size, | |
1201 | mtd->erasesize, mtd->name); | |
d5ca5129 | 1202 | } |
48b19268 | 1203 | mutex_unlock(&mtd_table_mutex); |
d5ca5129 | 1204 | return 0; |
1da177e4 LT |
1205 | } |
1206 | ||
447d9bd8 AD |
1207 | static int mtd_proc_open(struct inode *inode, struct file *file) |
1208 | { | |
1209 | return single_open(file, mtd_proc_show, NULL); | |
1210 | } | |
1211 | ||
1212 | static const struct file_operations mtd_proc_ops = { | |
1213 | .open = mtd_proc_open, | |
1214 | .read = seq_read, | |
1215 | .llseek = seq_lseek, | |
1216 | .release = single_release, | |
1217 | }; | |
45b09076 KC |
1218 | #endif /* CONFIG_PROC_FS */ |
1219 | ||
1da177e4 LT |
1220 | /*====================================================================*/ |
1221 | /* Init code */ | |
1222 | ||
0661b1ac JA |
1223 | static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name) |
1224 | { | |
1225 | int ret; | |
1226 | ||
1227 | ret = bdi_init(bdi); | |
1228 | if (!ret) | |
02aa2a37 | 1229 | ret = bdi_register(bdi, NULL, "%s", name); |
0661b1ac JA |
1230 | |
1231 | if (ret) | |
1232 | bdi_destroy(bdi); | |
1233 | ||
1234 | return ret; | |
1235 | } | |
1236 | ||
93e56214 AB |
1237 | static struct proc_dir_entry *proc_mtd; |
1238 | ||
1da177e4 LT |
1239 | static int __init init_mtd(void) |
1240 | { | |
15bce40c | 1241 | int ret; |
0661b1ac | 1242 | |
15bce40c | 1243 | ret = class_register(&mtd_class); |
0661b1ac JA |
1244 | if (ret) |
1245 | goto err_reg; | |
1246 | ||
b4caecd4 | 1247 | ret = mtd_bdi_init(&mtd_bdi, "mtd"); |
0661b1ac | 1248 | if (ret) |
b4caecd4 | 1249 | goto err_bdi; |
694bb7fc | 1250 | |
447d9bd8 | 1251 | proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops); |
93e56214 | 1252 | |
660685d9 AB |
1253 | ret = init_mtdchar(); |
1254 | if (ret) | |
1255 | goto out_procfs; | |
1256 | ||
1da177e4 | 1257 | return 0; |
0661b1ac | 1258 | |
660685d9 AB |
1259 | out_procfs: |
1260 | if (proc_mtd) | |
1261 | remove_proc_entry("mtd", NULL); | |
b4caecd4 | 1262 | err_bdi: |
0661b1ac JA |
1263 | class_unregister(&mtd_class); |
1264 | err_reg: | |
1265 | pr_err("Error registering mtd class or bdi: %d\n", ret); | |
1266 | return ret; | |
1da177e4 LT |
1267 | } |
1268 | ||
1269 | static void __exit cleanup_mtd(void) | |
1270 | { | |
660685d9 | 1271 | cleanup_mtdchar(); |
d5ca5129 | 1272 | if (proc_mtd) |
93e56214 | 1273 | remove_proc_entry("mtd", NULL); |
15bce40c | 1274 | class_unregister(&mtd_class); |
b4caecd4 | 1275 | bdi_destroy(&mtd_bdi); |
1da177e4 LT |
1276 | } |
1277 | ||
1278 | module_init(init_mtd); | |
1279 | module_exit(cleanup_mtd); | |
1280 | ||
1da177e4 LT |
1281 | MODULE_LICENSE("GPL"); |
1282 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
1283 | MODULE_DESCRIPTION("Core MTD registration and access routines"); |