blob: 11883bd26d9d35e8234cc7dc74f51b7ed57d7566 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Core registration and callback routines for MTD
3 * drivers and users.
4 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/ptrace.h>
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +030027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/major.h>
31#include <linux/fs.h>
Artem Bityutskiy77993082006-10-11 14:52:44 +030032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ioctl.h>
34#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/proc_fs.h>
Ben Hutchingsb520e412010-01-29 20:59:42 +000036#include <linux/idr.h>
Jörn Engela33eb6b2010-04-27 09:40:52 +020037#include <linux/backing-dev.h>
Tejun Heo05d71b462010-03-30 02:52:39 +090038#include <linux/gfp.h>
David Howells0d01ff22013-04-11 23:51:01 +010039#include <linux/slab.h>
Brian Norris3efe41b2014-11-26 01:01:08 -080040#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/mtd/mtd.h>
Jamie Ilesf5671ab2011-05-23 17:15:46 +010043#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ben Dooks356d70f2007-05-28 20:28:34 +010045#include "mtdcore.h"
Artem Bityutskiy660685d2013-03-14 13:27:40 +020046
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010047static struct backing_dev_info mtd_bdi = {
Jörn Engela33eb6b2010-04-27 09:40:52 +020048};
Ben Dooks356d70f2007-05-28 20:28:34 +010049
David Woodhouse15bce402009-04-05 07:40:58 -070050static int mtd_cls_suspend(struct device *dev, pm_message_t state);
51static int mtd_cls_resume(struct device *dev);
David Brownell1f24b5a2009-03-26 00:42:41 -070052
David Woodhouse15bce402009-04-05 07:40:58 -070053static struct class mtd_class = {
54 .name = "mtd",
55 .owner = THIS_MODULE,
56 .suspend = mtd_cls_suspend,
57 .resume = mtd_cls_resume,
58};
David Brownell1f24b5a2009-03-26 00:42:41 -070059
Ben Hutchingsb520e412010-01-29 20:59:42 +000060static DEFINE_IDR(mtd_idr);
61
Thomas Gleixner97894cd2005-11-07 11:15:26 +000062/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080064DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065EXPORT_SYMBOL_GPL(mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +000066
67struct mtd_info *__mtd_next_device(int i)
68{
69 return idr_get_next(&mtd_idr, &i);
70}
71EXPORT_SYMBOL_GPL(__mtd_next_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static LIST_HEAD(mtd_notifiers);
74
David Brownell1f24b5a2009-03-26 00:42:41 -070075
David Brownell1f24b5a2009-03-26 00:42:41 -070076#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
David Brownell1f24b5a2009-03-26 00:42:41 -070077
78/* REVISIT once MTD uses the driver model better, whoever allocates
79 * the mtd_info will probably want to use the release() hook...
80 */
81static void mtd_release(struct device *dev)
82{
Brian Norris5e472122014-07-21 19:06:47 -070083 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020084 dev_t index = MTD_DEVT(mtd->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070085
Brian Norris5e472122014-07-21 19:06:47 -070086 /* remove /dev/mtdXro node */
87 device_destroy(&mtd_class, index + 1);
David Woodhouse15bce402009-04-05 07:40:58 -070088}
89
90static int mtd_cls_suspend(struct device *dev, pm_message_t state)
91{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020092 struct mtd_info *mtd = dev_get_drvdata(dev);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070093
Artem Bityutskiy1a308712012-01-16 11:07:16 +020094 return mtd ? mtd_suspend(mtd) : 0;
David Woodhouse15bce402009-04-05 07:40:58 -070095}
96
97static int mtd_cls_resume(struct device *dev)
98{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020099 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200100
Brian Norris3ee5014185b2012-01-27 12:39:32 -0800101 if (mtd)
Artem Bityutskiyead995f2011-12-23 19:31:25 +0200102 mtd_resume(mtd);
David Woodhouse15bce402009-04-05 07:40:58 -0700103 return 0;
David Brownell1f24b5a2009-03-26 00:42:41 -0700104}
105
106static ssize_t mtd_type_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
108{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200109 struct mtd_info *mtd = dev_get_drvdata(dev);
David Brownell1f24b5a2009-03-26 00:42:41 -0700110 char *type;
111
112 switch (mtd->type) {
113 case MTD_ABSENT:
114 type = "absent";
115 break;
116 case MTD_RAM:
117 type = "ram";
118 break;
119 case MTD_ROM:
120 type = "rom";
121 break;
122 case MTD_NORFLASH:
123 type = "nor";
124 break;
125 case MTD_NANDFLASH:
126 type = "nand";
127 break;
128 case MTD_DATAFLASH:
129 type = "dataflash";
130 break;
131 case MTD_UBIVOLUME:
132 type = "ubi";
133 break;
Huang Shijief4837242013-09-25 14:58:19 +0800134 case MTD_MLCNANDFLASH:
135 type = "mlc-nand";
136 break;
David Brownell1f24b5a2009-03-26 00:42:41 -0700137 default:
138 type = "unknown";
139 }
140
141 return snprintf(buf, PAGE_SIZE, "%s\n", type);
142}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700143static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
144
145static ssize_t mtd_flags_show(struct device *dev,
146 struct device_attribute *attr, char *buf)
147{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200148 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700149
150 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
151
152}
153static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
154
155static ssize_t mtd_size_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
157{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200158 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700159
160 return snprintf(buf, PAGE_SIZE, "%llu\n",
161 (unsigned long long)mtd->size);
162
163}
164static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
165
166static ssize_t mtd_erasesize_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
168{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200169 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700170
171 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
172
173}
174static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
175
176static ssize_t mtd_writesize_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200179 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700180
181 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
182
183}
184static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
185
Artem Bityutskiye7693542009-04-18 12:29:42 +0300186static ssize_t mtd_subpagesize_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200189 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300190 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
191
192 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
193
194}
195static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
196
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700197static ssize_t mtd_oobsize_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200200 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700201
202 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
203
204}
205static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
206
207static ssize_t mtd_numeraseregions_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200210 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700211
212 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
213
214}
215static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
216 NULL);
217
218static ssize_t mtd_name_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
220{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200221 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700222
223 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
224
225}
226static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700227
Mike Dunna9b672e2012-04-25 12:06:07 -0700228static ssize_t mtd_ecc_strength_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
231 struct mtd_info *mtd = dev_get_drvdata(dev);
232
233 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
234}
235static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
236
Mike Dunnd062d4e2012-04-25 12:06:08 -0700237static ssize_t mtd_bitflip_threshold_show(struct device *dev,
238 struct device_attribute *attr,
239 char *buf)
240{
241 struct mtd_info *mtd = dev_get_drvdata(dev);
242
243 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
244}
245
246static ssize_t mtd_bitflip_threshold_store(struct device *dev,
247 struct device_attribute *attr,
248 const char *buf, size_t count)
249{
250 struct mtd_info *mtd = dev_get_drvdata(dev);
251 unsigned int bitflip_threshold;
252 int retval;
253
254 retval = kstrtouint(buf, 0, &bitflip_threshold);
255 if (retval)
256 return retval;
257
258 mtd->bitflip_threshold = bitflip_threshold;
259 return count;
260}
261static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
262 mtd_bitflip_threshold_show,
263 mtd_bitflip_threshold_store);
264
Huang Shijiebf977e32013-08-16 10:10:05 +0800265static ssize_t mtd_ecc_step_size_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
267{
268 struct mtd_info *mtd = dev_get_drvdata(dev);
269
270 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
271
272}
273static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
274
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300275static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
277{
278 struct mtd_info *mtd = dev_get_drvdata(dev);
279 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
280
281 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
282}
283static DEVICE_ATTR(corrected_bits, S_IRUGO,
284 mtd_ecc_stats_corrected_show, NULL);
285
286static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 struct mtd_info *mtd = dev_get_drvdata(dev);
290 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
291
292 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
293}
294static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
295
296static ssize_t mtd_badblocks_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
299 struct mtd_info *mtd = dev_get_drvdata(dev);
300 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
301
302 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
303}
304static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
305
306static ssize_t mtd_bbtblocks_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct mtd_info *mtd = dev_get_drvdata(dev);
310 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
311
312 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
313}
314static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
315
David Brownell1f24b5a2009-03-26 00:42:41 -0700316static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700317 &dev_attr_type.attr,
318 &dev_attr_flags.attr,
319 &dev_attr_size.attr,
320 &dev_attr_erasesize.attr,
321 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300322 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700323 &dev_attr_oobsize.attr,
324 &dev_attr_numeraseregions.attr,
325 &dev_attr_name.attr,
Mike Dunna9b672e2012-04-25 12:06:07 -0700326 &dev_attr_ecc_strength.attr,
Huang Shijiebf977e32013-08-16 10:10:05 +0800327 &dev_attr_ecc_step_size.attr,
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300328 &dev_attr_corrected_bits.attr,
329 &dev_attr_ecc_failures.attr,
330 &dev_attr_bad_blocks.attr,
331 &dev_attr_bbt_blocks.attr,
Mike Dunnd062d4e2012-04-25 12:06:08 -0700332 &dev_attr_bitflip_threshold.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700333 NULL,
334};
Axel Lin54c738f2013-12-02 10:12:25 +0800335ATTRIBUTE_GROUPS(mtd);
David Brownell1f24b5a2009-03-26 00:42:41 -0700336
337static struct device_type mtd_devtype = {
338 .name = "mtd",
339 .groups = mtd_groups,
340 .release = mtd_release,
341};
342
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100343#ifndef CONFIG_MMU
344unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
345{
346 switch (mtd->type) {
347 case MTD_RAM:
348 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
349 NOMMU_MAP_READ | NOMMU_MAP_WRITE;
350 case MTD_ROM:
351 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
352 NOMMU_MAP_READ;
353 default:
354 return NOMMU_MAP_COPY;
355 }
356}
Arnd Bergmann706a4e52015-01-28 11:09:20 -0700357EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100358#endif
359
Brian Norris3efe41b2014-11-26 01:01:08 -0800360static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
361 void *cmd)
362{
363 struct mtd_info *mtd;
364
365 mtd = container_of(n, struct mtd_info, reboot_notifier);
366 mtd->_reboot(mtd);
367
368 return NOTIFY_DONE;
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/**
372 * add_mtd_device - register an MTD device
373 * @mtd: pointer to new MTD device info structure
374 *
375 * Add a device to the list of MTD devices present in the system, and
376 * notify each currently active MTD 'user' of its arrival. Returns
377 * zero on success or 1 on failure, which currently will only happen
Ben Hutchingsb520e412010-01-29 20:59:42 +0000378 * if there is insufficient memory or a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380
381int add_mtd_device(struct mtd_info *mtd)
382{
Ben Hutchingsb520e412010-01-29 20:59:42 +0000383 struct mtd_notifier *not;
384 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100386 mtd->backing_dev_info = &mtd_bdi;
David Howells402d3262009-02-12 10:40:00 +0000387
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400388 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800389 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Tejun Heo589e9c42013-02-27 17:04:33 -0800391 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
392 if (i < 0)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000393 goto fail_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ben Hutchingsb520e412010-01-29 20:59:42 +0000395 mtd->index = i;
396 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000397
Mike Dunnd062d4e2012-04-25 12:06:08 -0700398 /* default value if not set by driver */
399 if (mtd->bitflip_threshold == 0)
400 mtd->bitflip_threshold = mtd->ecc_strength;
401
Ben Hutchingsb520e412010-01-29 20:59:42 +0000402 if (is_power_of_2(mtd->erasesize))
403 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
404 else
405 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000406
Ben Hutchingsb520e412010-01-29 20:59:42 +0000407 if (is_power_of_2(mtd->writesize))
408 mtd->writesize_shift = ffs(mtd->writesize) - 1;
409 else
410 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000411
Ben Hutchingsb520e412010-01-29 20:59:42 +0000412 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
413 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
Håvard Skinnemoen187ef152006-09-22 10:07:08 +0100414
Ben Hutchingsb520e412010-01-29 20:59:42 +0000415 /* Some chips always power up locked. Unlock them now */
Artem Bityutskiy38134562011-12-30 17:00:35 +0200416 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
417 error = mtd_unlock(mtd, 0, mtd->size);
418 if (error && error != -EOPNOTSUPP)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000419 printk(KERN_WARNING
420 "%s: unlock failed, writes may not work\n",
421 mtd->name);
422 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700423
Ben Hutchingsb520e412010-01-29 20:59:42 +0000424 /* Caller should have set dev.parent to match the
425 * physical device.
426 */
427 mtd->dev.type = &mtd_devtype;
428 mtd->dev.class = &mtd_class;
429 mtd->dev.devt = MTD_DEVT(i);
430 dev_set_name(&mtd->dev, "mtd%d", i);
431 dev_set_drvdata(&mtd->dev, mtd);
432 if (device_register(&mtd->dev) != 0)
433 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700434
Brian Norris5e472122014-07-21 19:06:47 -0700435 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
436 "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000437
Brian Norris289c0522011-07-19 10:06:09 -0700438 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000439 /* No need to get a refcount on the module containing
440 the notifier, since we hold the mtd_table_mutex */
441 list_for_each_entry(not, &mtd_notifiers, list)
442 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000443
Ingo Molnar48b19262006-03-31 02:29:41 -0800444 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000445 /* We _know_ we aren't being removed, because
446 our caller is still holding us here. So none
447 of this try_ nonsense, and no bitching about it
448 either. :) */
449 __module_get(THIS_MODULE);
450 return 0;
451
452fail_added:
453 idr_remove(&mtd_idr, i);
454fail_locked:
455 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 1;
457}
458
459/**
460 * del_mtd_device - unregister an MTD device
461 * @mtd: pointer to MTD device info structure
462 *
463 * Remove a device from the list of MTD devices present in the system,
464 * and notify each currently active MTD 'user' of its departure.
465 * Returns zero on success or 1 on failure, which currently will happen
466 * if the requested device does not appear to be present in the list.
467 */
468
Jamie Ileseea72d52011-05-23 10:23:42 +0100469int del_mtd_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int ret;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200472 struct mtd_notifier *not;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000473
Ingo Molnar48b19262006-03-31 02:29:41 -0800474 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Ben Hutchingsb520e412010-01-29 20:59:42 +0000476 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 ret = -ENODEV;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200478 goto out_error;
479 }
480
481 /* No need to get a refcount on the module containing
482 the notifier, since we hold the mtd_table_mutex */
483 list_for_each_entry(not, &mtd_notifiers, list)
484 not->remove(mtd);
485
486 if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000487 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 mtd->index, mtd->name, mtd->usecount);
489 ret = -EBUSY;
490 } else {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700491 device_unregister(&mtd->dev);
492
Ben Hutchingsb520e412010-01-29 20:59:42 +0000493 idr_remove(&mtd_idr, mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 module_put(THIS_MODULE);
496 ret = 0;
497 }
498
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200499out_error:
Ingo Molnar48b19262006-03-31 02:29:41 -0800500 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return ret;
502}
503
504/**
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300505 * mtd_device_parse_register - parse partitions and register an MTD device.
506 *
507 * @mtd: the MTD device to register
508 * @types: the list of MTD partition probes to try, see
509 * 'parse_mtd_partitions()' for more information
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400510 * @parser_data: MTD partition parser-specific data
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300511 * @parts: fallback partition information to register, if parsing fails;
512 * only valid if %nr_parts > %0
513 * @nr_parts: the number of partitions in parts, if zero then the full
514 * MTD device is registered if no partition info is found
515 *
516 * This function aggregates MTD partitions parsing (done by
517 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
518 * basically follows the most common pattern found in many MTD drivers:
519 *
520 * * It first tries to probe partitions on MTD device @mtd using parsers
521 * specified in @types (if @types is %NULL, then the default list of parsers
522 * is used, see 'parse_mtd_partitions()' for more information). If none are
523 * found this functions tries to fallback to information specified in
524 * @parts/@nr_parts.
Brian Norris92394b5c2011-07-20 09:53:42 -0700525 * * If any partitioning info was found, this function registers the found
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300526 * partitions.
527 * * If no partitions were found this function just registers the MTD device
528 * @mtd and exits.
529 *
530 * Returns zero in case of success and a negative error code in case of failure.
531 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200532int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400533 struct mtd_part_parser_data *parser_data,
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300534 const struct mtd_partition *parts,
535 int nr_parts)
536{
537 int err;
538 struct mtd_partition *real_parts;
539
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400540 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
Jason Liu4d523b602011-08-24 19:26:28 +0800541 if (err <= 0 && nr_parts && parts) {
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300542 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
543 GFP_KERNEL);
Jason Liu4d523b602011-08-24 19:26:28 +0800544 if (!real_parts)
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300545 err = -ENOMEM;
Jason Liu4d523b602011-08-24 19:26:28 +0800546 else
547 err = nr_parts;
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300548 }
549
550 if (err > 0) {
551 err = add_mtd_partitions(mtd, real_parts, err);
552 kfree(real_parts);
553 } else if (err == 0) {
554 err = add_mtd_device(mtd);
555 if (err == 1)
556 err = -ENODEV;
557 }
558
Niklas Cassele1dd8642015-02-01 02:08:50 +0100559 /*
560 * FIXME: some drivers unfortunately call this function more than once.
561 * So we have to check if we've already assigned the reboot notifier.
562 *
563 * Generally, we can make multiple calls work for most cases, but it
564 * does cause problems with parse_mtd_partitions() above (e.g.,
565 * cmdlineparts will register partitions more than once).
566 */
567 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
Brian Norris3efe41b2014-11-26 01:01:08 -0800568 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
569 register_reboot_notifier(&mtd->reboot_notifier);
570 }
571
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300572 return err;
573}
574EXPORT_SYMBOL_GPL(mtd_device_parse_register);
575
576/**
Jamie Ilesf5671ab2011-05-23 17:15:46 +0100577 * mtd_device_unregister - unregister an existing MTD device.
578 *
579 * @master: the MTD device to unregister. This will unregister both the master
580 * and any partitions if registered.
581 */
582int mtd_device_unregister(struct mtd_info *master)
583{
584 int err;
585
Brian Norris3efe41b2014-11-26 01:01:08 -0800586 if (master->_reboot)
587 unregister_reboot_notifier(&master->reboot_notifier);
588
Jamie Ilesf5671ab2011-05-23 17:15:46 +0100589 err = del_mtd_partitions(master);
590 if (err)
591 return err;
592
593 if (!device_is_registered(&master->dev))
594 return 0;
595
596 return del_mtd_device(master);
597}
598EXPORT_SYMBOL_GPL(mtd_device_unregister);
599
600/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * register_mtd_user - register a 'user' of MTD devices.
602 * @new: pointer to notifier info structure
603 *
604 * Registers a pair of callbacks function to be called upon addition
605 * or removal of MTD devices. Causes the 'add' callback to be immediately
606 * invoked for each MTD device currently present in the system.
607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608void register_mtd_user (struct mtd_notifier *new)
609{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000610 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Ingo Molnar48b19262006-03-31 02:29:41 -0800612 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 list_add(&new->list, &mtd_notifiers);
615
Wanlong Gaod5ca5122011-05-20 21:14:30 +0800616 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000617
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000618 mtd_for_each_device(mtd)
619 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Ingo Molnar48b19262006-03-31 02:29:41 -0800621 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200623EXPORT_SYMBOL_GPL(register_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000626 * unregister_mtd_user - unregister a 'user' of MTD devices.
627 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *
629 * Removes a callback function pair from the list of 'users' to be
630 * notified upon addition or removal of MTD devices. Causes the
631 * 'remove' callback to be immediately invoked for each MTD device
632 * currently present in the system.
633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634int unregister_mtd_user (struct mtd_notifier *old)
635{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000636 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Ingo Molnar48b19262006-03-31 02:29:41 -0800638 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 module_put(THIS_MODULE);
641
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000642 mtd_for_each_device(mtd)
643 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800646 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200649EXPORT_SYMBOL_GPL(unregister_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651/**
652 * get_mtd_device - obtain a validated handle for an MTD device
653 * @mtd: last known address of the required MTD device
654 * @num: internal device number of the required MTD device
655 *
656 * Given a number and NULL address, return the num'th entry in the device
657 * table, if any. Given an address and num == -1, search the device table
658 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300659 * both, return the num'th driver only if its address matches. Return
660 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
663{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000664 struct mtd_info *ret = NULL, *other;
665 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Ingo Molnar48b19262006-03-31 02:29:41 -0800667 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000670 mtd_for_each_device(other) {
671 if (other == mtd) {
672 ret = mtd;
673 break;
674 }
675 }
Ben Hutchingsb520e412010-01-29 20:59:42 +0000676 } else if (num >= 0) {
677 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (mtd && mtd != ret)
679 ret = NULL;
680 }
681
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200682 if (!ret) {
683 ret = ERR_PTR(err);
684 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200687 err = __get_mtd_device(ret);
688 if (err)
689 ret = ERR_PTR(err);
690out:
Ingo Molnar48b19262006-03-31 02:29:41 -0800691 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200693}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200694EXPORT_SYMBOL_GPL(get_mtd_device);
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300695
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200696
697int __get_mtd_device(struct mtd_info *mtd)
698{
699 int err;
700
701 if (!try_module_get(mtd->owner))
702 return -ENODEV;
703
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200704 if (mtd->_get_device) {
705 err = mtd->_get_device(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200706
707 if (err) {
708 module_put(mtd->owner);
709 return err;
710 }
711 }
712 mtd->usecount++;
713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200715EXPORT_SYMBOL_GPL(__get_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Artem Bityutskiy77993082006-10-11 14:52:44 +0300717/**
718 * get_mtd_device_nm - obtain a validated handle for an MTD device by
719 * device name
720 * @name: MTD device name to open
721 *
722 * This function returns MTD device description structure in case of
723 * success and an error code in case of failure.
724 */
Artem Bityutskiy77993082006-10-11 14:52:44 +0300725struct mtd_info *get_mtd_device_nm(const char *name)
726{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000727 int err = -ENODEV;
728 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300729
730 mutex_lock(&mtd_table_mutex);
731
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000732 mtd_for_each_device(other) {
733 if (!strcmp(name, other->name)) {
734 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300735 break;
736 }
737 }
738
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300739 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300740 goto out_unlock;
741
Wanlong Gao52534f22011-05-17 22:36:18 +0800742 err = __get_mtd_device(mtd);
743 if (err)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300744 goto out_unlock;
745
Artem Bityutskiy77993082006-10-11 14:52:44 +0300746 mutex_unlock(&mtd_table_mutex);
747 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300748
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300749out_unlock:
750 mutex_unlock(&mtd_table_mutex);
751 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300752}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200753EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755void put_mtd_device(struct mtd_info *mtd)
756{
Ingo Molnar48b19262006-03-31 02:29:41 -0800757 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200758 __put_mtd_device(mtd);
759 mutex_unlock(&mtd_table_mutex);
760
761}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200762EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200763
764void __put_mtd_device(struct mtd_info *mtd)
765{
766 --mtd->usecount;
767 BUG_ON(mtd->usecount < 0);
768
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200769 if (mtd->_put_device)
770 mtd->_put_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 module_put(mtd->owner);
773}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200774EXPORT_SYMBOL_GPL(__put_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Artem Bityutskiy52b02032011-12-30 15:57:25 +0200776/*
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200777 * Erase is an asynchronous operation. Device drivers are supposed
778 * to call instr->callback() whenever the operation completes, even
779 * if it completes with a failure.
780 * Callers are supposed to pass a callback function and wait for it
781 * to be called before writing to the block.
782 */
783int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
784{
Brian Norris0c2b4e22014-07-21 19:06:27 -0700785 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200786 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200787 if (!(mtd->flags & MTD_WRITEABLE))
788 return -EROFS;
Shmulik Ladkani3b27dac2012-02-09 15:36:29 +0200789 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200790 if (!instr->len) {
791 instr->state = MTD_ERASE_DONE;
792 mtd_erase_callback(instr);
793 return 0;
794 }
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200795 return mtd->_erase(mtd, instr);
796}
797EXPORT_SYMBOL_GPL(mtd_erase);
798
799/*
800 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
801 */
802int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
803 void **virt, resource_size_t *phys)
804{
805 *retlen = 0;
Artem Bityutskiy0dd52352012-02-08 15:13:26 +0200806 *virt = NULL;
807 if (phys)
808 *phys = 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200809 if (!mtd->_point)
810 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700811 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200812 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200813 if (!len)
814 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200815 return mtd->_point(mtd, from, len, retlen, virt, phys);
816}
817EXPORT_SYMBOL_GPL(mtd_point);
818
819/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
820int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
821{
822 if (!mtd->_point)
823 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700824 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200825 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200826 if (!len)
827 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200828 return mtd->_unpoint(mtd, from, len);
829}
830EXPORT_SYMBOL_GPL(mtd_unpoint);
831
832/*
833 * Allow NOMMU mmap() to directly map the device (if not NULL)
834 * - return the address to which the offset maps
835 * - return -ENOSYS to indicate refusal to do the mapping
836 */
837unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
838 unsigned long offset, unsigned long flags)
839{
840 if (!mtd->_get_unmapped_area)
841 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700842 if (offset >= mtd->size || len > mtd->size - offset)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200843 return -EINVAL;
844 return mtd->_get_unmapped_area(mtd, len, offset, flags);
845}
846EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
847
848int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
849 u_char *buf)
850{
Mike Dunnedbc45402012-04-25 12:06:11 -0700851 int ret_code;
Artem Bityutskiy834247e2012-02-06 12:39:07 +0200852 *retlen = 0;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700853 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200854 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200855 if (!len)
856 return 0;
Mike Dunnedbc45402012-04-25 12:06:11 -0700857
858 /*
859 * In the absence of an error, drivers return a non-negative integer
860 * representing the maximum number of bitflips that were corrected on
861 * any one ecc region (if applicable; zero otherwise).
862 */
863 ret_code = mtd->_read(mtd, from, len, retlen, buf);
864 if (unlikely(ret_code < 0))
865 return ret_code;
866 if (mtd->ecc_strength == 0)
867 return 0; /* device lacks ecc */
868 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200869}
870EXPORT_SYMBOL_GPL(mtd_read);
871
872int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
873 const u_char *buf)
874{
875 *retlen = 0;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700876 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200877 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200878 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
879 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200880 if (!len)
881 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200882 return mtd->_write(mtd, to, len, retlen, buf);
883}
884EXPORT_SYMBOL_GPL(mtd_write);
885
886/*
887 * In blackbox flight recorder like scenarios we want to make successful writes
888 * in interrupt context. panic_write() is only intended to be called when its
889 * known the kernel is about to panic and we need the write to succeed. Since
890 * the kernel is not going to be running for much longer, this function can
891 * break locks and delay to ensure the write succeeds (but not sleep).
892 */
893int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
894 const u_char *buf)
895{
896 *retlen = 0;
897 if (!mtd->_panic_write)
898 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700899 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200900 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200901 if (!(mtd->flags & MTD_WRITEABLE))
902 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200903 if (!len)
904 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200905 return mtd->_panic_write(mtd, to, len, retlen, buf);
906}
907EXPORT_SYMBOL_GPL(mtd_panic_write);
908
Brian Norrisd2d48482012-06-22 16:35:38 -0700909int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
910{
Brian Norrise47f6852012-06-22 16:35:39 -0700911 int ret_code;
Brian Norrisd2d48482012-06-22 16:35:38 -0700912 ops->retlen = ops->oobretlen = 0;
913 if (!mtd->_read_oob)
914 return -EOPNOTSUPP;
Brian Norrise47f6852012-06-22 16:35:39 -0700915 /*
916 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
917 * similar to mtd->_read(), returning a non-negative integer
918 * representing max bitflips. In other cases, mtd->_read_oob() may
919 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
920 */
921 ret_code = mtd->_read_oob(mtd, from, ops);
922 if (unlikely(ret_code < 0))
923 return ret_code;
924 if (mtd->ecc_strength == 0)
925 return 0; /* device lacks ecc */
926 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Brian Norrisd2d48482012-06-22 16:35:38 -0700927}
928EXPORT_SYMBOL_GPL(mtd_read_oob);
929
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200930/*
931 * Method to access the protection register area, present in some flash
932 * devices. The user data is one time programmable but the factory data is read
933 * only.
934 */
Christian Riesch4b78fc42014-01-28 09:29:44 +0100935int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
936 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200937{
938 if (!mtd->_get_fact_prot_info)
939 return -EOPNOTSUPP;
940 if (!len)
941 return 0;
Christian Riesch4b78fc42014-01-28 09:29:44 +0100942 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200943}
944EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
945
946int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
947 size_t *retlen, u_char *buf)
948{
949 *retlen = 0;
950 if (!mtd->_read_fact_prot_reg)
951 return -EOPNOTSUPP;
952 if (!len)
953 return 0;
954 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
955}
956EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
957
Christian Riesch4b78fc42014-01-28 09:29:44 +0100958int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
959 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200960{
961 if (!mtd->_get_user_prot_info)
962 return -EOPNOTSUPP;
963 if (!len)
964 return 0;
Christian Riesch4b78fc42014-01-28 09:29:44 +0100965 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200966}
967EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
968
969int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
970 size_t *retlen, u_char *buf)
971{
972 *retlen = 0;
973 if (!mtd->_read_user_prot_reg)
974 return -EOPNOTSUPP;
975 if (!len)
976 return 0;
977 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
978}
979EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
980
981int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
982 size_t *retlen, u_char *buf)
983{
Christian Riesch9a78bc82014-03-06 12:42:37 +0100984 int ret;
985
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200986 *retlen = 0;
987 if (!mtd->_write_user_prot_reg)
988 return -EOPNOTSUPP;
989 if (!len)
990 return 0;
Christian Riesch9a78bc82014-03-06 12:42:37 +0100991 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
992 if (ret)
993 return ret;
994
995 /*
996 * If no data could be written at all, we are out of memory and
997 * must return -ENOSPC.
998 */
999 return (*retlen) ? 0 : -ENOSPC;
Artem Bityutskiyde3cac92012-02-08 16:37:14 +02001000}
1001EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1002
1003int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1004{
1005 if (!mtd->_lock_user_prot_reg)
1006 return -EOPNOTSUPP;
1007 if (!len)
1008 return 0;
1009 return mtd->_lock_user_prot_reg(mtd, from, len);
1010}
1011EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1012
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001013/* Chip-supported device locking */
1014int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1015{
1016 if (!mtd->_lock)
1017 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001018 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001019 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001020 if (!len)
1021 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001022 return mtd->_lock(mtd, ofs, len);
1023}
1024EXPORT_SYMBOL_GPL(mtd_lock);
1025
1026int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1027{
1028 if (!mtd->_unlock)
1029 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001030 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001031 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001032 if (!len)
1033 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001034 return mtd->_unlock(mtd, ofs, len);
1035}
1036EXPORT_SYMBOL_GPL(mtd_unlock);
1037
1038int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1039{
1040 if (!mtd->_is_locked)
1041 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001042 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001043 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001044 if (!len)
1045 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001046 return mtd->_is_locked(mtd, ofs, len);
1047}
1048EXPORT_SYMBOL_GPL(mtd_is_locked);
1049
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001050int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001051{
Brian Norris0c2b4e22014-07-21 19:06:27 -07001052 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001053 return -EINVAL;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001054 if (!mtd->_block_isreserved)
1055 return 0;
1056 return mtd->_block_isreserved(mtd, ofs);
1057}
1058EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1059
1060int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1061{
Brian Norris0c2b4e22014-07-21 19:06:27 -07001062 if (ofs < 0 || ofs >= mtd->size)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001063 return -EINVAL;
1064 if (!mtd->_block_isbad)
1065 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001066 return mtd->_block_isbad(mtd, ofs);
1067}
1068EXPORT_SYMBOL_GPL(mtd_block_isbad);
1069
1070int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1071{
1072 if (!mtd->_block_markbad)
1073 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001074 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001075 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001076 if (!(mtd->flags & MTD_WRITEABLE))
1077 return -EROFS;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001078 return mtd->_block_markbad(mtd, ofs);
1079}
1080EXPORT_SYMBOL_GPL(mtd_block_markbad);
1081
1082/*
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001083 * default_mtd_writev - the default writev method
1084 * @mtd: mtd device description object pointer
1085 * @vecs: the vectors to write
1086 * @count: count of vectors in @vecs
1087 * @to: the MTD device offset to write to
1088 * @retlen: on exit contains the count of bytes written to the MTD device.
1089 *
1090 * This function returns zero in case of success and a negative error code in
1091 * case of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001093static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1094 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 unsigned long i;
1097 size_t totlen = 0, thislen;
1098 int ret = 0;
1099
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001100 for (i = 0; i < count; i++) {
1101 if (!vecs[i].iov_len)
1102 continue;
1103 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1104 vecs[i].iov_base);
1105 totlen += thislen;
1106 if (ret || thislen != vecs[i].iov_len)
1107 break;
1108 to += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001110 *retlen = totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return ret;
1112}
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001113
1114/*
1115 * mtd_writev - the vector-based MTD write method
1116 * @mtd: mtd device description object pointer
1117 * @vecs: the vectors to write
1118 * @count: count of vectors in @vecs
1119 * @to: the MTD device offset to write to
1120 * @retlen: on exit contains the count of bytes written to the MTD device.
1121 *
1122 * This function returns zero in case of success and a negative error code in
1123 * case of failure.
1124 */
1125int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1126 unsigned long count, loff_t to, size_t *retlen)
1127{
1128 *retlen = 0;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001129 if (!(mtd->flags & MTD_WRITEABLE))
1130 return -EROFS;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001131 if (!mtd->_writev)
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001132 return default_mtd_writev(mtd, vecs, count, to, retlen);
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001133 return mtd->_writev(mtd, vecs, count, to, retlen);
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001134}
1135EXPORT_SYMBOL_GPL(mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Grant Erickson33b53712011-04-08 08:51:32 -07001137/**
1138 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001139 * @mtd: mtd device description object pointer
1140 * @size: a pointer to the ideal or maximum size of the allocation, points
Grant Erickson33b53712011-04-08 08:51:32 -07001141 * to the actual allocation size on success.
1142 *
1143 * This routine attempts to allocate a contiguous kernel buffer up to
1144 * the specified size, backing off the size of the request exponentially
1145 * until the request succeeds or until the allocation size falls below
1146 * the system page size. This attempts to make sure it does not adversely
1147 * impact system performance, so when allocating more than one page, we
Linus Torvaldscaf49192012-12-10 10:51:16 -08001148 * ask the memory allocator to avoid re-trying, swapping, writing back
1149 * or performing I/O.
Grant Erickson33b53712011-04-08 08:51:32 -07001150 *
1151 * Note, this function also makes sure that the allocated buffer is aligned to
1152 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1153 *
1154 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1155 * to handle smaller (i.e. degraded) buffer allocations under low- or
1156 * fragmented-memory situations where such reduced allocations, from a
1157 * requested ideal, are allowed.
1158 *
1159 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1160 */
1161void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1162{
Linus Torvaldscaf49192012-12-10 10:51:16 -08001163 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1164 __GFP_NORETRY | __GFP_NO_KSWAPD;
Grant Erickson33b53712011-04-08 08:51:32 -07001165 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1166 void *kbuf;
1167
1168 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1169
1170 while (*size > min_alloc) {
1171 kbuf = kmalloc(*size, flags);
1172 if (kbuf)
1173 return kbuf;
1174
1175 *size >>= 1;
1176 *size = ALIGN(*size, mtd->writesize);
1177 }
1178
1179 /*
1180 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1181 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1182 */
1183 return kmalloc(*size, GFP_KERNEL);
1184}
Grant Erickson33b53712011-04-08 08:51:32 -07001185EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Pavel Machek2d2dce02006-03-31 02:29:49 -08001187#ifdef CONFIG_PROC_FS
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/* Support for /proc/mtd */
1191
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001192static int mtd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001194 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001196 seq_puts(m, "dev: size erasesize name\n");
Ingo Molnar48b19262006-03-31 02:29:41 -08001197 mutex_lock(&mtd_table_mutex);
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001198 mtd_for_each_device(mtd) {
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001199 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1200 mtd->index, (unsigned long long)mtd->size,
1201 mtd->erasesize, mtd->name);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001202 }
Ingo Molnar48b19262006-03-31 02:29:41 -08001203 mutex_unlock(&mtd_table_mutex);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001207static int mtd_proc_open(struct inode *inode, struct file *file)
1208{
1209 return single_open(file, mtd_proc_show, NULL);
1210}
1211
1212static const struct file_operations mtd_proc_ops = {
1213 .open = mtd_proc_open,
1214 .read = seq_read,
1215 .llseek = seq_lseek,
1216 .release = single_release,
1217};
Kevin Cernekee45b09072009-04-04 11:03:04 -07001218#endif /* CONFIG_PROC_FS */
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/*====================================================================*/
1221/* Init code */
1222
Jens Axboe0661b1a2010-04-27 09:49:47 +02001223static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1224{
1225 int ret;
1226
1227 ret = bdi_init(bdi);
1228 if (!ret)
Kees Cook02aa2a32013-07-03 15:04:56 -07001229 ret = bdi_register(bdi, NULL, "%s", name);
Jens Axboe0661b1a2010-04-27 09:49:47 +02001230
1231 if (ret)
1232 bdi_destroy(bdi);
1233
1234 return ret;
1235}
1236
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001237static struct proc_dir_entry *proc_mtd;
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239static int __init init_mtd(void)
1240{
David Woodhouse15bce402009-04-05 07:40:58 -07001241 int ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -07001242
Jens Axboe0661b1a2010-04-27 09:49:47 +02001243 ret = class_register(&mtd_class);
1244 if (ret)
1245 goto err_reg;
1246
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001247 ret = mtd_bdi_init(&mtd_bdi, "mtd");
Jens Axboe0661b1a2010-04-27 09:49:47 +02001248 if (ret)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001249 goto err_bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02001250
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001251 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001252
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001253 ret = init_mtdchar();
1254 if (ret)
1255 goto out_procfs;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02001258
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001259out_procfs:
1260 if (proc_mtd)
1261 remove_proc_entry("mtd", NULL);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001262err_bdi:
Jens Axboe0661b1a2010-04-27 09:49:47 +02001263 class_unregister(&mtd_class);
1264err_reg:
1265 pr_err("Error registering mtd class or bdi: %d\n", ret);
1266 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269static void __exit cleanup_mtd(void)
1270{
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001271 cleanup_mtdchar();
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001272 if (proc_mtd)
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001273 remove_proc_entry("mtd", NULL);
David Woodhouse15bce402009-04-05 07:40:58 -07001274 class_unregister(&mtd_class);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001275 bdi_destroy(&mtd_bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278module_init(init_mtd);
1279module_exit(cleanup_mtd);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281MODULE_LICENSE("GPL");
1282MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1283MODULE_DESCRIPTION("Core MTD registration and access routines");