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