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