import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / mtd / mtdcore.c
1 /*
2 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
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 *
22 */
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/ptrace.h>
27 #include <linux/seq_file.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/major.h>
31 #include <linux/fs.h>
32 #include <linux/err.h>
33 #include <linux/ioctl.h>
34 #include <linux/init.h>
35 #include <linux/proc_fs.h>
36 #include <linux/idr.h>
37 #include <linux/backing-dev.h>
38 #include <linux/gfp.h>
39 #include <linux/slab.h>
40
41 #include <linux/mtd/mtd.h>
42 #include <linux/mtd/partitions.h>
43 #include <linux/seq_file.h>
44
45 #include "mtdcore.h"
46
47 /*
48 * backing device capabilities for non-mappable devices (such as NAND flash)
49 * - permits private mappings, copies are taken of the data
50 */
51 static struct backing_dev_info mtd_bdi_unmappable = {
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 */
60 static struct backing_dev_info mtd_bdi_ro_mappable = {
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 */
70 static struct backing_dev_info mtd_bdi_rw_mappable = {
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 };
75
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 };
85
86 static DEFINE_IDR(mtd_idr);
87
88 /* These are exported solely for the purpose of mtd_blkdevs.c. You
89 should not use them for _anything_ else */
90 DEFINE_MUTEX(mtd_table_mutex);
91 EXPORT_SYMBOL_GPL(mtd_table_mutex);
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);
98
99 static LIST_HEAD(mtd_notifiers);
100
101
102 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
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 {
109 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
110 dev_t index = MTD_DEVT(mtd->index);
111
112 /* remove /dev/mtdXro node if needed */
113 if (index)
114 device_destroy(&mtd_class, index + 1);
115 }
116
117 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
118 {
119 struct mtd_info *mtd = dev_get_drvdata(dev);
120
121 return mtd ? mtd_suspend(mtd) : 0;
122 }
123
124 static int mtd_cls_resume(struct device *dev)
125 {
126 struct mtd_info *mtd = dev_get_drvdata(dev);
127
128 if (mtd)
129 mtd_resume(mtd);
130 return 0;
131 }
132
133 static ssize_t mtd_type_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135 {
136 struct mtd_info *mtd = dev_get_drvdata(dev);
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 }
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 {
172 struct mtd_info *mtd = dev_get_drvdata(dev);
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 {
182 struct mtd_info *mtd = dev_get_drvdata(dev);
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 {
193 struct mtd_info *mtd = dev_get_drvdata(dev);
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 {
203 struct mtd_info *mtd = dev_get_drvdata(dev);
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
210 static ssize_t mtd_subpagesize_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
212 {
213 struct mtd_info *mtd = dev_get_drvdata(dev);
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
221 static ssize_t mtd_oobsize_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223 {
224 struct mtd_info *mtd = dev_get_drvdata(dev);
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 {
234 struct mtd_info *mtd = dev_get_drvdata(dev);
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 {
245 struct mtd_info *mtd = dev_get_drvdata(dev);
246
247 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
248
249 }
250 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
251
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
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
289 static struct attribute *mtd_attrs[] = {
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,
295 &dev_attr_subpagesize.attr,
296 &dev_attr_oobsize.attr,
297 &dev_attr_numeraseregions.attr,
298 &dev_attr_name.attr,
299 &dev_attr_ecc_strength.attr,
300 &dev_attr_bitflip_threshold.attr,
301 NULL,
302 };
303
304 static struct attribute_group mtd_group = {
305 .attrs = mtd_attrs,
306 };
307
308 static const struct attribute_group *mtd_groups[] = {
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
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
326 * if there is insufficient memory or a sysfs error.
327 */
328
329 int add_mtd_device(struct mtd_info *mtd)
330 {
331 struct mtd_notifier *not;
332 int i, error;
333
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
348 BUG_ON(mtd->writesize == 0);
349 mutex_lock(&mtd_table_mutex);
350
351 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
352 if (i < 0)
353 goto fail_locked;
354
355 mtd->index = i;
356 mtd->usecount = 0;
357
358 /* default value if not set by driver */
359 if (mtd->bitflip_threshold == 0)
360 mtd->bitflip_threshold = mtd->ecc_strength;
361
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 */
376 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
377 error = mtd_unlock(mtd, 0, mtd->size);
378 if (error && error != -EOPNOTSUPP)
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
400 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
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;
413
414 fail_added:
415 idr_remove(&mtd_idr, i);
416 fail_locked:
417 mutex_unlock(&mtd_table_mutex);
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
431 int del_mtd_device(struct mtd_info *mtd)
432 {
433 int ret;
434 struct mtd_notifier *not;
435
436 mutex_lock(&mtd_table_mutex);
437
438 if (idr_find(&mtd_idr, mtd->index) != mtd) {
439 ret = -ENODEV;
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) {
449 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
450 mtd->index, mtd->name, mtd->usecount);
451 ret = -EBUSY;
452 } else {
453 device_unregister(&mtd->dev);
454
455 idr_remove(&mtd_idr, mtd->index);
456
457 module_put(THIS_MODULE);
458 ret = 0;
459 }
460
461 out_error:
462 mutex_unlock(&mtd_table_mutex);
463 return ret;
464 }
465
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
472 * @parser_data: MTD partition parser-specific data
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.
487 * * If any partitioning info was found, this function registers the found
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 */
494 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
495 struct mtd_part_parser_data *parser_data,
496 const struct mtd_partition *parts,
497 int nr_parts)
498 {
499 int err;
500 struct mtd_partition *real_parts;
501
502 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
503 if (err <= 0 && nr_parts && parts) {
504 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
505 GFP_KERNEL);
506 if (!real_parts)
507 err = -ENOMEM;
508 else
509 err = nr_parts;
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
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
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 */
554 void register_mtd_user (struct mtd_notifier *new)
555 {
556 struct mtd_info *mtd;
557
558 mutex_lock(&mtd_table_mutex);
559
560 list_add(&new->list, &mtd_notifiers);
561
562 __module_get(THIS_MODULE);
563
564 mtd_for_each_device(mtd)
565 new->add(mtd);
566
567 mutex_unlock(&mtd_table_mutex);
568 }
569 EXPORT_SYMBOL_GPL(register_mtd_user);
570
571 /**
572 * unregister_mtd_user - unregister a 'user' of MTD devices.
573 * @old: pointer to notifier info structure
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 */
580 int unregister_mtd_user (struct mtd_notifier *old)
581 {
582 struct mtd_info *mtd;
583
584 mutex_lock(&mtd_table_mutex);
585
586 module_put(THIS_MODULE);
587
588 mtd_for_each_device(mtd)
589 old->remove(mtd);
590
591 list_del(&old->list);
592 mutex_unlock(&mtd_table_mutex);
593 return 0;
594 }
595 EXPORT_SYMBOL_GPL(unregister_mtd_user);
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
605 * both, return the num'th driver only if its address matches. Return
606 * error code if not.
607 */
608 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
609 {
610 struct mtd_info *ret = NULL, *other;
611 int err = -ENODEV;
612
613 mutex_lock(&mtd_table_mutex);
614
615 if (num == -1) {
616 mtd_for_each_device(other) {
617 if (other == mtd) {
618 ret = mtd;
619 break;
620 }
621 }
622 } else if (num >= 0) {
623 ret = idr_find(&mtd_idr, num);
624 if (mtd && mtd != ret)
625 ret = NULL;
626 }
627
628 if (!ret) {
629 ret = ERR_PTR(err);
630 goto out;
631 }
632
633 err = __get_mtd_device(ret);
634 if (err)
635 ret = ERR_PTR(err);
636 out:
637 mutex_unlock(&mtd_table_mutex);
638 return ret;
639 }
640 EXPORT_SYMBOL_GPL(get_mtd_device);
641
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
650 if (mtd->_get_device) {
651 err = mtd->_get_device(mtd);
652
653 if (err) {
654 module_put(mtd->owner);
655 return err;
656 }
657 }
658 mtd->usecount++;
659 return 0;
660 }
661 EXPORT_SYMBOL_GPL(__get_mtd_device);
662
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 */
671 struct mtd_info *get_mtd_device_nm(const char *name)
672 {
673 int err = -ENODEV;
674 struct mtd_info *mtd = NULL, *other;
675
676 mutex_lock(&mtd_table_mutex);
677
678 mtd_for_each_device(other) {
679 if (!strcmp(name, other->name)) {
680 mtd = other;
681 break;
682 }
683 }
684
685 if (!mtd)
686 goto out_unlock;
687
688 err = __get_mtd_device(mtd);
689 if (err)
690 goto out_unlock;
691
692 mutex_unlock(&mtd_table_mutex);
693 return mtd;
694
695 out_unlock:
696 mutex_unlock(&mtd_table_mutex);
697 return ERR_PTR(err);
698 }
699 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
700
701 void put_mtd_device(struct mtd_info *mtd)
702 {
703 mutex_lock(&mtd_table_mutex);
704 __put_mtd_device(mtd);
705 mutex_unlock(&mtd_table_mutex);
706
707 }
708 EXPORT_SYMBOL_GPL(put_mtd_device);
709
710 void __put_mtd_device(struct mtd_info *mtd)
711 {
712 --mtd->usecount;
713 BUG_ON(mtd->usecount < 0);
714
715 if (mtd->_put_device)
716 mtd->_put_device(mtd);
717
718 module_put(mtd->owner);
719 }
720 EXPORT_SYMBOL_GPL(__put_mtd_device);
721
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;
733 if (!(mtd->flags & MTD_WRITEABLE))
734 return -EROFS;
735 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
736 if (!instr->len) {
737 instr->state = MTD_ERASE_DONE;
738 mtd_erase_callback(instr);
739 return 0;
740 }
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;
752 *virt = NULL;
753 if (phys)
754 *phys = 0;
755 if (!mtd->_point)
756 return -EOPNOTSUPP;
757 if (from < 0 || from > mtd->size || len > mtd->size - from)
758 return -EINVAL;
759 if (!len)
760 return 0;
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;
772 if (!len)
773 return 0;
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 {
797 int ret_code;
798 *retlen = 0;
799 if (from < 0 || from > mtd->size || len > mtd->size - from)
800 return -EINVAL;
801 if (!len)
802 return 0;
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;
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;
822 if (to < 0 || to > mtd->size || len > mtd->size - to)
823 return -EINVAL;
824 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
825 return -EROFS;
826 if (!len)
827 return 0;
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;
847 if (!(mtd->flags & MTD_WRITEABLE))
848 return -EROFS;
849 if (!len)
850 return 0;
851 return mtd->_panic_write(mtd, to, len, retlen, buf);
852 }
853 EXPORT_SYMBOL_GPL(mtd_panic_write);
854
855 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
856 {
857 int ret_code;
858 ops->retlen = ops->oobretlen = 0;
859 if (!mtd->_read_oob)
860 return -EOPNOTSUPP;
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;
873 }
874 EXPORT_SYMBOL_GPL(mtd_read_oob);
875
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
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;
956 if (!len)
957 return 0;
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;
968 if (!len)
969 return 0;
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;
980 if (!len)
981 return 0;
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;
1002 if (!(mtd->flags & MTD_WRITEABLE))
1003 return -EROFS;
1004 return mtd->_block_markbad(mtd, ofs);
1005 }
1006 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1007
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.
1018 */
1019 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1020 unsigned long count, loff_t to, size_t *retlen)
1021 {
1022 unsigned long i;
1023 size_t totlen = 0, thislen;
1024 int ret = 0;
1025
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;
1035 }
1036 *retlen = totlen;
1037 return ret;
1038 }
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;
1055 if (!(mtd->flags & MTD_WRITEABLE))
1056 return -EROFS;
1057 if (!mtd->_writev)
1058 return default_mtd_writev(mtd, vecs, count, to, retlen);
1059 return mtd->_writev(mtd, vecs, count, to, retlen);
1060 }
1061 EXPORT_SYMBOL_GPL(mtd_writev);
1062
1063 /**
1064 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1065 * @mtd: mtd device description object pointer
1066 * @size: a pointer to the ideal or maximum size of the allocation, points
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
1074 * ask the memory allocator to avoid re-trying, swapping, writing back
1075 * or performing I/O.
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 {
1089 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1090 __GFP_NORETRY | __GFP_NO_KSWAPD;
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 }
1111 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1112
1113 #ifdef CONFIG_PROC_FS
1114
1115 /*====================================================================*/
1116 /* Support for /proc/mtd */
1117
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
1127 static int mtd_proc_show(struct seq_file *m, void *v)
1128 {
1129 struct mtd_info *mtd;
1130
1131 seq_puts(m, "dev: size erasesize name\n");
1132 mutex_lock(&mtd_table_mutex);
1133 mtd_for_each_device(mtd) {
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);
1137 }
1138 mutex_unlock(&mtd_table_mutex);
1139 return 0;
1140 }
1141
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 };
1153 #endif /* CONFIG_PROC_FS */
1154
1155 /*====================================================================*/
1156 /* Init code */
1157
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
1172 static struct proc_dir_entry *proc_mtd;
1173
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
1185 static int __init init_mtd(void)
1186 {
1187 int ret;
1188
1189 ret = class_register(&mtd_class);
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;
1204
1205 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1206
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
1219 ret = init_mtdchar();
1220 if (ret)
1221 goto out_procfs;
1222
1223 return 0;
1224
1225 out_procfs:
1226 if (proc_mtd)
1227 remove_proc_entry("mtd", NULL);
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;
1237 }
1238
1239 static void __exit cleanup_mtd(void)
1240 {
1241 cleanup_mtdchar();
1242 if (proc_mtd)
1243 remove_proc_entry("mtd", NULL);
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
1251 class_unregister(&mtd_class);
1252 bdi_destroy(&mtd_bdi_unmappable);
1253 bdi_destroy(&mtd_bdi_ro_mappable);
1254 bdi_destroy(&mtd_bdi_rw_mappable);
1255 }
1256
1257 module_init(init_mtd);
1258 module_exit(cleanup_mtd);
1259
1260 MODULE_LICENSE("GPL");
1261 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1262 MODULE_DESCRIPTION("Core MTD registration and access routines");