blob: ff38a1df22f71dc84209550c1c6fdf3ce776e02d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <linux/mtd/mtd.h>
Jamie Ilesf5671ab2011-05-23 17:15:46 +010042#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ben Dooks356d70f2007-05-28 20:28:34 +010044#include "mtdcore.h"
Artem Bityutskiy660685d2013-03-14 13:27:40 +020045
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010046static struct backing_dev_info mtd_bdi = {
Jörn Engela33eb6b2010-04-27 09:40:52 +020047};
Ben Dooks356d70f2007-05-28 20:28:34 +010048
David Woodhouse15bce402009-04-05 07:40:58 -070049static int mtd_cls_suspend(struct device *dev, pm_message_t state);
50static int mtd_cls_resume(struct device *dev);
David Brownell1f24b5a2009-03-26 00:42:41 -070051
David Woodhouse15bce402009-04-05 07:40:58 -070052static struct class mtd_class = {
53 .name = "mtd",
54 .owner = THIS_MODULE,
55 .suspend = mtd_cls_suspend,
56 .resume = mtd_cls_resume,
57};
David Brownell1f24b5a2009-03-26 00:42:41 -070058
Ben Hutchingsb520e412010-01-29 20:59:42 +000059static DEFINE_IDR(mtd_idr);
60
Thomas Gleixner97894cd2005-11-07 11:15:26 +000061/* These are exported solely for the purpose of mtd_blkdevs.c. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 should not use them for _anything_ else */
Ingo Molnar48b19262006-03-31 02:29:41 -080063DEFINE_MUTEX(mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064EXPORT_SYMBOL_GPL(mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +000065
66struct mtd_info *__mtd_next_device(int i)
67{
68 return idr_get_next(&mtd_idr, &i);
69}
70EXPORT_SYMBOL_GPL(__mtd_next_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static LIST_HEAD(mtd_notifiers);
73
David Brownell1f24b5a2009-03-26 00:42:41 -070074
David Brownell1f24b5a2009-03-26 00:42:41 -070075#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
David Brownell1f24b5a2009-03-26 00:42:41 -070076
77/* REVISIT once MTD uses the driver model better, whoever allocates
78 * the mtd_info will probably want to use the release() hook...
79 */
80static void mtd_release(struct device *dev)
81{
Brian Norris5e472122014-07-21 19:06:47 -070082 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020083 dev_t index = MTD_DEVT(mtd->index);
David Brownell1f24b5a2009-03-26 00:42:41 -070084
Brian Norris5e472122014-07-21 19:06:47 -070085 /* remove /dev/mtdXro node */
86 device_destroy(&mtd_class, index + 1);
David Woodhouse15bce402009-04-05 07:40:58 -070087}
88
89static int mtd_cls_suspend(struct device *dev, pm_message_t state)
90{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020091 struct mtd_info *mtd = dev_get_drvdata(dev);
Saeed Bishara6afc4fd2009-07-28 04:56:43 -070092
Artem Bityutskiy1a308712012-01-16 11:07:16 +020093 return mtd ? mtd_suspend(mtd) : 0;
David Woodhouse15bce402009-04-05 07:40:58 -070094}
95
96static int mtd_cls_resume(struct device *dev)
97{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +020098 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiy33c87b42011-12-30 16:11:14 +020099
Brian Norris3ee5014185b2012-01-27 12:39:32 -0800100 if (mtd)
Artem Bityutskiyead995f2011-12-23 19:31:25 +0200101 mtd_resume(mtd);
David Woodhouse15bce402009-04-05 07:40:58 -0700102 return 0;
David Brownell1f24b5a2009-03-26 00:42:41 -0700103}
104
105static ssize_t mtd_type_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
107{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200108 struct mtd_info *mtd = dev_get_drvdata(dev);
David Brownell1f24b5a2009-03-26 00:42:41 -0700109 char *type;
110
111 switch (mtd->type) {
112 case MTD_ABSENT:
113 type = "absent";
114 break;
115 case MTD_RAM:
116 type = "ram";
117 break;
118 case MTD_ROM:
119 type = "rom";
120 break;
121 case MTD_NORFLASH:
122 type = "nor";
123 break;
124 case MTD_NANDFLASH:
125 type = "nand";
126 break;
127 case MTD_DATAFLASH:
128 type = "dataflash";
129 break;
130 case MTD_UBIVOLUME:
131 type = "ubi";
132 break;
Huang Shijief4837242013-09-25 14:58:19 +0800133 case MTD_MLCNANDFLASH:
134 type = "mlc-nand";
135 break;
David Brownell1f24b5a2009-03-26 00:42:41 -0700136 default:
137 type = "unknown";
138 }
139
140 return snprintf(buf, PAGE_SIZE, "%s\n", type);
141}
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700142static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
143
144static ssize_t mtd_flags_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200147 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700148
149 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
150
151}
152static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
153
154static ssize_t mtd_size_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200157 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700158
159 return snprintf(buf, PAGE_SIZE, "%llu\n",
160 (unsigned long long)mtd->size);
161
162}
163static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
164
165static ssize_t mtd_erasesize_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
167{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200168 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700169
170 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
171
172}
173static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
174
175static ssize_t mtd_writesize_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
177{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200178 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700179
180 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
181
182}
183static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
184
Artem Bityutskiye7693542009-04-18 12:29:42 +0300185static ssize_t mtd_subpagesize_show(struct device *dev,
186 struct device_attribute *attr, char *buf)
187{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200188 struct mtd_info *mtd = dev_get_drvdata(dev);
Artem Bityutskiye7693542009-04-18 12:29:42 +0300189 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
190
191 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
192
193}
194static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
195
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700196static ssize_t mtd_oobsize_show(struct device *dev,
197 struct device_attribute *attr, char *buf)
198{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200199 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700200
201 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
202
203}
204static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
205
206static ssize_t mtd_numeraseregions_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200209 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700210
211 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
212
213}
214static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
215 NULL);
216
217static ssize_t mtd_name_show(struct device *dev,
218 struct device_attribute *attr, char *buf)
219{
Artem Bityutskiyd5de20a2011-12-29 18:00:29 +0200220 struct mtd_info *mtd = dev_get_drvdata(dev);
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700221
222 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
223
224}
225static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
David Brownell1f24b5a2009-03-26 00:42:41 -0700226
Mike Dunna9b672e2012-04-25 12:06:07 -0700227static ssize_t mtd_ecc_strength_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
230 struct mtd_info *mtd = dev_get_drvdata(dev);
231
232 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
233}
234static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
235
Mike Dunnd062d4e2012-04-25 12:06:08 -0700236static ssize_t mtd_bitflip_threshold_show(struct device *dev,
237 struct device_attribute *attr,
238 char *buf)
239{
240 struct mtd_info *mtd = dev_get_drvdata(dev);
241
242 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
243}
244
245static ssize_t mtd_bitflip_threshold_store(struct device *dev,
246 struct device_attribute *attr,
247 const char *buf, size_t count)
248{
249 struct mtd_info *mtd = dev_get_drvdata(dev);
250 unsigned int bitflip_threshold;
251 int retval;
252
253 retval = kstrtouint(buf, 0, &bitflip_threshold);
254 if (retval)
255 return retval;
256
257 mtd->bitflip_threshold = bitflip_threshold;
258 return count;
259}
260static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
261 mtd_bitflip_threshold_show,
262 mtd_bitflip_threshold_store);
263
Huang Shijiebf977e32013-08-16 10:10:05 +0800264static ssize_t mtd_ecc_step_size_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
266{
267 struct mtd_info *mtd = dev_get_drvdata(dev);
268
269 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
270
271}
272static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
273
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300274static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
277 struct mtd_info *mtd = dev_get_drvdata(dev);
278 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
279
280 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
281}
282static DEVICE_ATTR(corrected_bits, S_IRUGO,
283 mtd_ecc_stats_corrected_show, NULL);
284
285static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct mtd_info *mtd = dev_get_drvdata(dev);
289 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
290
291 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
292}
293static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
294
295static ssize_t mtd_badblocks_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct mtd_info *mtd = dev_get_drvdata(dev);
299 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
300
301 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
302}
303static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
304
305static ssize_t mtd_bbtblocks_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct mtd_info *mtd = dev_get_drvdata(dev);
309 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
310
311 return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
312}
313static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
314
David Brownell1f24b5a2009-03-26 00:42:41 -0700315static struct attribute *mtd_attrs[] = {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700316 &dev_attr_type.attr,
317 &dev_attr_flags.attr,
318 &dev_attr_size.attr,
319 &dev_attr_erasesize.attr,
320 &dev_attr_writesize.attr,
Artem Bityutskiye7693542009-04-18 12:29:42 +0300321 &dev_attr_subpagesize.attr,
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700322 &dev_attr_oobsize.attr,
323 &dev_attr_numeraseregions.attr,
324 &dev_attr_name.attr,
Mike Dunna9b672e2012-04-25 12:06:07 -0700325 &dev_attr_ecc_strength.attr,
Huang Shijiebf977e32013-08-16 10:10:05 +0800326 &dev_attr_ecc_step_size.attr,
Ezequiel Garcia990a3af2014-06-24 10:55:50 -0300327 &dev_attr_corrected_bits.attr,
328 &dev_attr_ecc_failures.attr,
329 &dev_attr_bad_blocks.attr,
330 &dev_attr_bbt_blocks.attr,
Mike Dunnd062d4e2012-04-25 12:06:08 -0700331 &dev_attr_bitflip_threshold.attr,
David Brownell1f24b5a2009-03-26 00:42:41 -0700332 NULL,
333};
Axel Lin54c738f2013-12-02 10:12:25 +0800334ATTRIBUTE_GROUPS(mtd);
David Brownell1f24b5a2009-03-26 00:42:41 -0700335
336static struct device_type mtd_devtype = {
337 .name = "mtd",
338 .groups = mtd_groups,
339 .release = mtd_release,
340};
341
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100342#ifndef CONFIG_MMU
343unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
344{
345 switch (mtd->type) {
346 case MTD_RAM:
347 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
348 NOMMU_MAP_READ | NOMMU_MAP_WRITE;
349 case MTD_ROM:
350 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
351 NOMMU_MAP_READ;
352 default:
353 return NOMMU_MAP_COPY;
354 }
355}
356#endif
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
359 * add_mtd_device - register an MTD device
360 * @mtd: pointer to new MTD device info structure
361 *
362 * Add a device to the list of MTD devices present in the system, and
363 * notify each currently active MTD 'user' of its arrival. Returns
364 * zero on success or 1 on failure, which currently will only happen
Ben Hutchingsb520e412010-01-29 20:59:42 +0000365 * if there is insufficient memory or a sysfs error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
367
368int add_mtd_device(struct mtd_info *mtd)
369{
Ben Hutchingsb520e412010-01-29 20:59:42 +0000370 struct mtd_notifier *not;
371 int i, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Christoph Hellwigb4caecd2015-01-14 10:42:32 +0100373 mtd->backing_dev_info = &mtd_bdi;
David Howells402d3262009-02-12 10:40:00 +0000374
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400375 BUG_ON(mtd->writesize == 0);
Ingo Molnar48b19262006-03-31 02:29:41 -0800376 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Tejun Heo589e9c42013-02-27 17:04:33 -0800378 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
379 if (i < 0)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000380 goto fail_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Ben Hutchingsb520e412010-01-29 20:59:42 +0000382 mtd->index = i;
383 mtd->usecount = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000384
Mike Dunnd062d4e2012-04-25 12:06:08 -0700385 /* default value if not set by driver */
386 if (mtd->bitflip_threshold == 0)
387 mtd->bitflip_threshold = mtd->ecc_strength;
388
Ben Hutchingsb520e412010-01-29 20:59:42 +0000389 if (is_power_of_2(mtd->erasesize))
390 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
391 else
392 mtd->erasesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000393
Ben Hutchingsb520e412010-01-29 20:59:42 +0000394 if (is_power_of_2(mtd->writesize))
395 mtd->writesize_shift = ffs(mtd->writesize) - 1;
396 else
397 mtd->writesize_shift = 0;
Adrian Hunter69423d92008-12-10 13:37:21 +0000398
Ben Hutchingsb520e412010-01-29 20:59:42 +0000399 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
400 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
Håvard Skinnemoen187ef152006-09-22 10:07:08 +0100401
Ben Hutchingsb520e412010-01-29 20:59:42 +0000402 /* Some chips always power up locked. Unlock them now */
Artem Bityutskiy38134562011-12-30 17:00:35 +0200403 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
404 error = mtd_unlock(mtd, 0, mtd->size);
405 if (error && error != -EOPNOTSUPP)
Ben Hutchingsb520e412010-01-29 20:59:42 +0000406 printk(KERN_WARNING
407 "%s: unlock failed, writes may not work\n",
408 mtd->name);
409 }
David Brownell1f24b5a2009-03-26 00:42:41 -0700410
Ben Hutchingsb520e412010-01-29 20:59:42 +0000411 /* Caller should have set dev.parent to match the
412 * physical device.
413 */
414 mtd->dev.type = &mtd_devtype;
415 mtd->dev.class = &mtd_class;
416 mtd->dev.devt = MTD_DEVT(i);
417 dev_set_name(&mtd->dev, "mtd%d", i);
418 dev_set_drvdata(&mtd->dev, mtd);
419 if (device_register(&mtd->dev) != 0)
420 goto fail_added;
David Brownell1f24b5a2009-03-26 00:42:41 -0700421
Brian Norris5e472122014-07-21 19:06:47 -0700422 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
423 "mtd%dro", i);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000424
Brian Norris289c0522011-07-19 10:06:09 -0700425 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000426 /* No need to get a refcount on the module containing
427 the notifier, since we hold the mtd_table_mutex */
428 list_for_each_entry(not, &mtd_notifiers, list)
429 not->add(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000430
Ingo Molnar48b19262006-03-31 02:29:41 -0800431 mutex_unlock(&mtd_table_mutex);
Ben Hutchingsb520e412010-01-29 20:59:42 +0000432 /* We _know_ we aren't being removed, because
433 our caller is still holding us here. So none
434 of this try_ nonsense, and no bitching about it
435 either. :) */
436 __module_get(THIS_MODULE);
437 return 0;
438
439fail_added:
440 idr_remove(&mtd_idr, i);
441fail_locked:
442 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 1;
444}
445
446/**
447 * del_mtd_device - unregister an MTD device
448 * @mtd: pointer to MTD device info structure
449 *
450 * Remove a device from the list of MTD devices present in the system,
451 * and notify each currently active MTD 'user' of its departure.
452 * Returns zero on success or 1 on failure, which currently will happen
453 * if the requested device does not appear to be present in the list.
454 */
455
Jamie Ileseea72d52011-05-23 10:23:42 +0100456int del_mtd_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int ret;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200459 struct mtd_notifier *not;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000460
Ingo Molnar48b19262006-03-31 02:29:41 -0800461 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Ben Hutchingsb520e412010-01-29 20:59:42 +0000463 if (idr_find(&mtd_idr, mtd->index) != mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ret = -ENODEV;
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200465 goto out_error;
466 }
467
468 /* No need to get a refcount on the module containing
469 the notifier, since we hold the mtd_table_mutex */
470 list_for_each_entry(not, &mtd_notifiers, list)
471 not->remove(mtd);
472
473 if (mtd->usecount) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000474 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 mtd->index, mtd->name, mtd->usecount);
476 ret = -EBUSY;
477 } else {
Kevin Cernekee694bb7f2009-04-03 13:00:45 -0700478 device_unregister(&mtd->dev);
479
Ben Hutchingsb520e412010-01-29 20:59:42 +0000480 idr_remove(&mtd_idr, mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 module_put(THIS_MODULE);
483 ret = 0;
484 }
485
Maxim Levitsky75c0b842010-02-22 20:39:32 +0200486out_error:
Ingo Molnar48b19262006-03-31 02:29:41 -0800487 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return ret;
489}
490
491/**
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300492 * mtd_device_parse_register - parse partitions and register an MTD device.
493 *
494 * @mtd: the MTD device to register
495 * @types: the list of MTD partition probes to try, see
496 * 'parse_mtd_partitions()' for more information
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400497 * @parser_data: MTD partition parser-specific data
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300498 * @parts: fallback partition information to register, if parsing fails;
499 * only valid if %nr_parts > %0
500 * @nr_parts: the number of partitions in parts, if zero then the full
501 * MTD device is registered if no partition info is found
502 *
503 * This function aggregates MTD partitions parsing (done by
504 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
505 * basically follows the most common pattern found in many MTD drivers:
506 *
507 * * It first tries to probe partitions on MTD device @mtd using parsers
508 * specified in @types (if @types is %NULL, then the default list of parsers
509 * is used, see 'parse_mtd_partitions()' for more information). If none are
510 * found this functions tries to fallback to information specified in
511 * @parts/@nr_parts.
Brian Norris92394b5c2011-07-20 09:53:42 -0700512 * * If any partitioning info was found, this function registers the found
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300513 * partitions.
514 * * If no partitions were found this function just registers the MTD device
515 * @mtd and exits.
516 *
517 * Returns zero in case of success and a negative error code in case of failure.
518 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200519int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400520 struct mtd_part_parser_data *parser_data,
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300521 const struct mtd_partition *parts,
522 int nr_parts)
523{
524 int err;
525 struct mtd_partition *real_parts;
526
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400527 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
Jason Liu4d523b602011-08-24 19:26:28 +0800528 if (err <= 0 && nr_parts && parts) {
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300529 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
530 GFP_KERNEL);
Jason Liu4d523b602011-08-24 19:26:28 +0800531 if (!real_parts)
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300532 err = -ENOMEM;
Jason Liu4d523b602011-08-24 19:26:28 +0800533 else
534 err = nr_parts;
Dmitry Eremin-Solenikov1c4c2152011-03-25 22:26:25 +0300535 }
536
537 if (err > 0) {
538 err = add_mtd_partitions(mtd, real_parts, err);
539 kfree(real_parts);
540 } else if (err == 0) {
541 err = add_mtd_device(mtd);
542 if (err == 1)
543 err = -ENODEV;
544 }
545
546 return err;
547}
548EXPORT_SYMBOL_GPL(mtd_device_parse_register);
549
550/**
Jamie Ilesf5671ab2011-05-23 17:15:46 +0100551 * mtd_device_unregister - unregister an existing MTD device.
552 *
553 * @master: the MTD device to unregister. This will unregister both the master
554 * and any partitions if registered.
555 */
556int mtd_device_unregister(struct mtd_info *master)
557{
558 int err;
559
560 err = del_mtd_partitions(master);
561 if (err)
562 return err;
563
564 if (!device_is_registered(&master->dev))
565 return 0;
566
567 return del_mtd_device(master);
568}
569EXPORT_SYMBOL_GPL(mtd_device_unregister);
570
571/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * register_mtd_user - register a 'user' of MTD devices.
573 * @new: pointer to notifier info structure
574 *
575 * Registers a pair of callbacks function to be called upon addition
576 * or removal of MTD devices. Causes the 'add' callback to be immediately
577 * invoked for each MTD device currently present in the system.
578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579void register_mtd_user (struct mtd_notifier *new)
580{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000581 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ingo Molnar48b19262006-03-31 02:29:41 -0800583 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 list_add(&new->list, &mtd_notifiers);
586
Wanlong Gaod5ca5122011-05-20 21:14:30 +0800587 __module_get(THIS_MODULE);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000588
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000589 mtd_for_each_device(mtd)
590 new->add(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Ingo Molnar48b19262006-03-31 02:29:41 -0800592 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200594EXPORT_SYMBOL_GPL(register_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/**
Artem B. Bityuckiy49450792005-02-18 14:34:54 +0000597 * unregister_mtd_user - unregister a 'user' of MTD devices.
598 * @old: pointer to notifier info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
600 * Removes a callback function pair from the list of 'users' to be
601 * notified upon addition or removal of MTD devices. Causes the
602 * 'remove' callback to be immediately invoked for each MTD device
603 * currently present in the system.
604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605int unregister_mtd_user (struct mtd_notifier *old)
606{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000607 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Ingo Molnar48b19262006-03-31 02:29:41 -0800609 mutex_lock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 module_put(THIS_MODULE);
612
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000613 mtd_for_each_device(mtd)
614 old->remove(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 list_del(&old->list);
Ingo Molnar48b19262006-03-31 02:29:41 -0800617 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200620EXPORT_SYMBOL_GPL(unregister_mtd_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/**
623 * get_mtd_device - obtain a validated handle for an MTD device
624 * @mtd: last known address of the required MTD device
625 * @num: internal device number of the required MTD device
626 *
627 * Given a number and NULL address, return the num'th entry in the device
628 * table, if any. Given an address and num == -1, search the device table
629 * for a device with that address and return if it's still present. Given
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300630 * both, return the num'th driver only if its address matches. Return
631 * error code if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
634{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000635 struct mtd_info *ret = NULL, *other;
636 int err = -ENODEV;
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 if (num == -1) {
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000641 mtd_for_each_device(other) {
642 if (other == mtd) {
643 ret = mtd;
644 break;
645 }
646 }
Ben Hutchingsb520e412010-01-29 20:59:42 +0000647 } else if (num >= 0) {
648 ret = idr_find(&mtd_idr, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (mtd && mtd != ret)
650 ret = NULL;
651 }
652
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200653 if (!ret) {
654 ret = ERR_PTR(err);
655 goto out;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200658 err = __get_mtd_device(ret);
659 if (err)
660 ret = ERR_PTR(err);
661out:
Ingo Molnar48b19262006-03-31 02:29:41 -0800662 mutex_unlock(&mtd_table_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return ret;
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200664}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200665EXPORT_SYMBOL_GPL(get_mtd_device);
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300666
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200667
668int __get_mtd_device(struct mtd_info *mtd)
669{
670 int err;
671
672 if (!try_module_get(mtd->owner))
673 return -ENODEV;
674
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200675 if (mtd->_get_device) {
676 err = mtd->_get_device(mtd);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200677
678 if (err) {
679 module_put(mtd->owner);
680 return err;
681 }
682 }
683 mtd->usecount++;
684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200686EXPORT_SYMBOL_GPL(__get_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Artem Bityutskiy77993082006-10-11 14:52:44 +0300688/**
689 * get_mtd_device_nm - obtain a validated handle for an MTD device by
690 * device name
691 * @name: MTD device name to open
692 *
693 * This function returns MTD device description structure in case of
694 * success and an error code in case of failure.
695 */
Artem Bityutskiy77993082006-10-11 14:52:44 +0300696struct mtd_info *get_mtd_device_nm(const char *name)
697{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000698 int err = -ENODEV;
699 struct mtd_info *mtd = NULL, *other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300700
701 mutex_lock(&mtd_table_mutex);
702
Ben Hutchingsf1332ba2010-01-29 20:57:11 +0000703 mtd_for_each_device(other) {
704 if (!strcmp(name, other->name)) {
705 mtd = other;
Artem Bityutskiy77993082006-10-11 14:52:44 +0300706 break;
707 }
708 }
709
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300710 if (!mtd)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300711 goto out_unlock;
712
Wanlong Gao52534f22011-05-17 22:36:18 +0800713 err = __get_mtd_device(mtd);
714 if (err)
Artem Bityutskiy77993082006-10-11 14:52:44 +0300715 goto out_unlock;
716
Artem Bityutskiy77993082006-10-11 14:52:44 +0300717 mutex_unlock(&mtd_table_mutex);
718 return mtd;
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300719
Artem Bityutskiy9fe912c2006-10-11 14:52:45 +0300720out_unlock:
721 mutex_unlock(&mtd_table_mutex);
722 return ERR_PTR(err);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300723}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200724EXPORT_SYMBOL_GPL(get_mtd_device_nm);
Artem Bityutskiy77993082006-10-11 14:52:44 +0300725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726void put_mtd_device(struct mtd_info *mtd)
727{
Ingo Molnar48b19262006-03-31 02:29:41 -0800728 mutex_lock(&mtd_table_mutex);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200729 __put_mtd_device(mtd);
730 mutex_unlock(&mtd_table_mutex);
731
732}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200733EXPORT_SYMBOL_GPL(put_mtd_device);
Maxim Levitsky3bd45652010-02-22 20:39:28 +0200734
735void __put_mtd_device(struct mtd_info *mtd)
736{
737 --mtd->usecount;
738 BUG_ON(mtd->usecount < 0);
739
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200740 if (mtd->_put_device)
741 mtd->_put_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 module_put(mtd->owner);
744}
Artem Bityutskiy33c87b42011-12-30 16:11:14 +0200745EXPORT_SYMBOL_GPL(__put_mtd_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Artem Bityutskiy52b02032011-12-30 15:57:25 +0200747/*
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200748 * Erase is an asynchronous operation. Device drivers are supposed
749 * to call instr->callback() whenever the operation completes, even
750 * if it completes with a failure.
751 * Callers are supposed to pass a callback function and wait for it
752 * to be called before writing to the block.
753 */
754int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
755{
Brian Norris0c2b4e22014-07-21 19:06:27 -0700756 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200757 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200758 if (!(mtd->flags & MTD_WRITEABLE))
759 return -EROFS;
Shmulik Ladkani3b27dac2012-02-09 15:36:29 +0200760 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200761 if (!instr->len) {
762 instr->state = MTD_ERASE_DONE;
763 mtd_erase_callback(instr);
764 return 0;
765 }
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200766 return mtd->_erase(mtd, instr);
767}
768EXPORT_SYMBOL_GPL(mtd_erase);
769
770/*
771 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
772 */
773int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
774 void **virt, resource_size_t *phys)
775{
776 *retlen = 0;
Artem Bityutskiy0dd52352012-02-08 15:13:26 +0200777 *virt = NULL;
778 if (phys)
779 *phys = 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200780 if (!mtd->_point)
781 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700782 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200783 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200784 if (!len)
785 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200786 return mtd->_point(mtd, from, len, retlen, virt, phys);
787}
788EXPORT_SYMBOL_GPL(mtd_point);
789
790/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
791int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
792{
793 if (!mtd->_point)
794 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700795 if (from < 0 || from >= mtd->size || len > mtd->size - from)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200796 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200797 if (!len)
798 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200799 return mtd->_unpoint(mtd, from, len);
800}
801EXPORT_SYMBOL_GPL(mtd_unpoint);
802
803/*
804 * Allow NOMMU mmap() to directly map the device (if not NULL)
805 * - return the address to which the offset maps
806 * - return -ENOSYS to indicate refusal to do the mapping
807 */
808unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
809 unsigned long offset, unsigned long flags)
810{
811 if (!mtd->_get_unmapped_area)
812 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700813 if (offset >= mtd->size || len > mtd->size - offset)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200814 return -EINVAL;
815 return mtd->_get_unmapped_area(mtd, len, offset, flags);
816}
817EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
818
819int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
820 u_char *buf)
821{
Mike Dunnedbc4542012-04-25 12:06:11 -0700822 int ret_code;
Artem Bityutskiy834247e2012-02-06 12:39:07 +0200823 *retlen = 0;
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;
Mike Dunnedbc4542012-04-25 12:06:11 -0700828
829 /*
830 * In the absence of an error, drivers return a non-negative integer
831 * representing the maximum number of bitflips that were corrected on
832 * any one ecc region (if applicable; zero otherwise).
833 */
834 ret_code = mtd->_read(mtd, from, len, retlen, buf);
835 if (unlikely(ret_code < 0))
836 return ret_code;
837 if (mtd->ecc_strength == 0)
838 return 0; /* device lacks ecc */
839 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200840}
841EXPORT_SYMBOL_GPL(mtd_read);
842
843int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
844 const u_char *buf)
845{
846 *retlen = 0;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700847 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200848 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200849 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
850 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200851 if (!len)
852 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200853 return mtd->_write(mtd, to, len, retlen, buf);
854}
855EXPORT_SYMBOL_GPL(mtd_write);
856
857/*
858 * In blackbox flight recorder like scenarios we want to make successful writes
859 * in interrupt context. panic_write() is only intended to be called when its
860 * known the kernel is about to panic and we need the write to succeed. Since
861 * the kernel is not going to be running for much longer, this function can
862 * break locks and delay to ensure the write succeeds (but not sleep).
863 */
864int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
865 const u_char *buf)
866{
867 *retlen = 0;
868 if (!mtd->_panic_write)
869 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700870 if (to < 0 || to >= mtd->size || len > mtd->size - to)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200871 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200872 if (!(mtd->flags & MTD_WRITEABLE))
873 return -EROFS;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200874 if (!len)
875 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200876 return mtd->_panic_write(mtd, to, len, retlen, buf);
877}
878EXPORT_SYMBOL_GPL(mtd_panic_write);
879
Brian Norrisd2d48482012-06-22 16:35:38 -0700880int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
881{
Brian Norrise47f6852012-06-22 16:35:39 -0700882 int ret_code;
Brian Norrisd2d48482012-06-22 16:35:38 -0700883 ops->retlen = ops->oobretlen = 0;
884 if (!mtd->_read_oob)
885 return -EOPNOTSUPP;
Brian Norrise47f6852012-06-22 16:35:39 -0700886 /*
887 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
888 * similar to mtd->_read(), returning a non-negative integer
889 * representing max bitflips. In other cases, mtd->_read_oob() may
890 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
891 */
892 ret_code = mtd->_read_oob(mtd, from, ops);
893 if (unlikely(ret_code < 0))
894 return ret_code;
895 if (mtd->ecc_strength == 0)
896 return 0; /* device lacks ecc */
897 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
Brian Norrisd2d48482012-06-22 16:35:38 -0700898}
899EXPORT_SYMBOL_GPL(mtd_read_oob);
900
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200901/*
902 * Method to access the protection register area, present in some flash
903 * devices. The user data is one time programmable but the factory data is read
904 * only.
905 */
Christian Riesch4b78fc42014-01-28 09:29:44 +0100906int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
907 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200908{
909 if (!mtd->_get_fact_prot_info)
910 return -EOPNOTSUPP;
911 if (!len)
912 return 0;
Christian Riesch4b78fc42014-01-28 09:29:44 +0100913 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200914}
915EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
916
917int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
918 size_t *retlen, u_char *buf)
919{
920 *retlen = 0;
921 if (!mtd->_read_fact_prot_reg)
922 return -EOPNOTSUPP;
923 if (!len)
924 return 0;
925 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
926}
927EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
928
Christian Riesch4b78fc42014-01-28 09:29:44 +0100929int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
930 struct otp_info *buf)
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200931{
932 if (!mtd->_get_user_prot_info)
933 return -EOPNOTSUPP;
934 if (!len)
935 return 0;
Christian Riesch4b78fc42014-01-28 09:29:44 +0100936 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200937}
938EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
939
940int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
941 size_t *retlen, u_char *buf)
942{
943 *retlen = 0;
944 if (!mtd->_read_user_prot_reg)
945 return -EOPNOTSUPP;
946 if (!len)
947 return 0;
948 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
949}
950EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
951
952int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
953 size_t *retlen, u_char *buf)
954{
Christian Riesch9a78bc82014-03-06 12:42:37 +0100955 int ret;
956
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200957 *retlen = 0;
958 if (!mtd->_write_user_prot_reg)
959 return -EOPNOTSUPP;
960 if (!len)
961 return 0;
Christian Riesch9a78bc82014-03-06 12:42:37 +0100962 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
963 if (ret)
964 return ret;
965
966 /*
967 * If no data could be written at all, we are out of memory and
968 * must return -ENOSPC.
969 */
970 return (*retlen) ? 0 : -ENOSPC;
Artem Bityutskiyde3cac92012-02-08 16:37:14 +0200971}
972EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
973
974int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
975{
976 if (!mtd->_lock_user_prot_reg)
977 return -EOPNOTSUPP;
978 if (!len)
979 return 0;
980 return mtd->_lock_user_prot_reg(mtd, from, len);
981}
982EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
983
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200984/* Chip-supported device locking */
985int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
986{
987 if (!mtd->_lock)
988 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -0700989 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200990 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +0200991 if (!len)
992 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +0200993 return mtd->_lock(mtd, ofs, len);
994}
995EXPORT_SYMBOL_GPL(mtd_lock);
996
997int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
998{
999 if (!mtd->_unlock)
1000 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001001 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001002 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001003 if (!len)
1004 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001005 return mtd->_unlock(mtd, ofs, len);
1006}
1007EXPORT_SYMBOL_GPL(mtd_unlock);
1008
1009int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1010{
1011 if (!mtd->_is_locked)
1012 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001013 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001014 return -EINVAL;
Artem Bityutskiybcb1d232012-02-06 13:27:43 +02001015 if (!len)
1016 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001017 return mtd->_is_locked(mtd, ofs, len);
1018}
1019EXPORT_SYMBOL_GPL(mtd_is_locked);
1020
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001021int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001022{
Brian Norris0c2b4e22014-07-21 19:06:27 -07001023 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001024 return -EINVAL;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001025 if (!mtd->_block_isreserved)
1026 return 0;
1027 return mtd->_block_isreserved(mtd, ofs);
1028}
1029EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1030
1031int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1032{
Brian Norris0c2b4e22014-07-21 19:06:27 -07001033 if (ofs < 0 || ofs >= mtd->size)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001034 return -EINVAL;
1035 if (!mtd->_block_isbad)
1036 return 0;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001037 return mtd->_block_isbad(mtd, ofs);
1038}
1039EXPORT_SYMBOL_GPL(mtd_block_isbad);
1040
1041int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1042{
1043 if (!mtd->_block_markbad)
1044 return -EOPNOTSUPP;
Brian Norris0c2b4e22014-07-21 19:06:27 -07001045 if (ofs < 0 || ofs >= mtd->size)
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001046 return -EINVAL;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001047 if (!(mtd->flags & MTD_WRITEABLE))
1048 return -EROFS;
Artem Bityutskiy8273a0c2012-02-03 14:34:14 +02001049 return mtd->_block_markbad(mtd, ofs);
1050}
1051EXPORT_SYMBOL_GPL(mtd_block_markbad);
1052
1053/*
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001054 * default_mtd_writev - the default writev method
1055 * @mtd: mtd device description object pointer
1056 * @vecs: the vectors to write
1057 * @count: count of vectors in @vecs
1058 * @to: the MTD device offset to write to
1059 * @retlen: on exit contains the count of bytes written to the MTD device.
1060 *
1061 * This function returns zero in case of success and a negative error code in
1062 * case of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001064static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1065 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 unsigned long i;
1068 size_t totlen = 0, thislen;
1069 int ret = 0;
1070
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001071 for (i = 0; i < count; i++) {
1072 if (!vecs[i].iov_len)
1073 continue;
1074 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1075 vecs[i].iov_base);
1076 totlen += thislen;
1077 if (ret || thislen != vecs[i].iov_len)
1078 break;
1079 to += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001081 *retlen = totlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return ret;
1083}
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001084
1085/*
1086 * mtd_writev - the vector-based MTD write method
1087 * @mtd: mtd device description object pointer
1088 * @vecs: the vectors to write
1089 * @count: count of vectors in @vecs
1090 * @to: the MTD device offset to write to
1091 * @retlen: on exit contains the count of bytes written to the MTD device.
1092 *
1093 * This function returns zero in case of success and a negative error code in
1094 * case of failure.
1095 */
1096int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1097 unsigned long count, loff_t to, size_t *retlen)
1098{
1099 *retlen = 0;
Artem Bityutskiy664addc2012-02-03 18:13:23 +02001100 if (!(mtd->flags & MTD_WRITEABLE))
1101 return -EROFS;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001102 if (!mtd->_writev)
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001103 return default_mtd_writev(mtd, vecs, count, to, retlen);
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02001104 return mtd->_writev(mtd, vecs, count, to, retlen);
Artem Bityutskiy1dbebd32011-12-30 16:23:41 +02001105}
1106EXPORT_SYMBOL_GPL(mtd_writev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Grant Erickson33b53712011-04-08 08:51:32 -07001108/**
1109 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
Artem Bityutskiy52b02032011-12-30 15:57:25 +02001110 * @mtd: mtd device description object pointer
1111 * @size: a pointer to the ideal or maximum size of the allocation, points
Grant Erickson33b53712011-04-08 08:51:32 -07001112 * to the actual allocation size on success.
1113 *
1114 * This routine attempts to allocate a contiguous kernel buffer up to
1115 * the specified size, backing off the size of the request exponentially
1116 * until the request succeeds or until the allocation size falls below
1117 * the system page size. This attempts to make sure it does not adversely
1118 * impact system performance, so when allocating more than one page, we
Linus Torvaldscaf49192012-12-10 10:51:16 -08001119 * ask the memory allocator to avoid re-trying, swapping, writing back
1120 * or performing I/O.
Grant Erickson33b53712011-04-08 08:51:32 -07001121 *
1122 * Note, this function also makes sure that the allocated buffer is aligned to
1123 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1124 *
1125 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1126 * to handle smaller (i.e. degraded) buffer allocations under low- or
1127 * fragmented-memory situations where such reduced allocations, from a
1128 * requested ideal, are allowed.
1129 *
1130 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1131 */
1132void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1133{
Linus Torvaldscaf49192012-12-10 10:51:16 -08001134 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1135 __GFP_NORETRY | __GFP_NO_KSWAPD;
Grant Erickson33b53712011-04-08 08:51:32 -07001136 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1137 void *kbuf;
1138
1139 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1140
1141 while (*size > min_alloc) {
1142 kbuf = kmalloc(*size, flags);
1143 if (kbuf)
1144 return kbuf;
1145
1146 *size >>= 1;
1147 *size = ALIGN(*size, mtd->writesize);
1148 }
1149
1150 /*
1151 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1152 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1153 */
1154 return kmalloc(*size, GFP_KERNEL);
1155}
Grant Erickson33b53712011-04-08 08:51:32 -07001156EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Pavel Machek2d2dce02006-03-31 02:29:49 -08001158#ifdef CONFIG_PROC_FS
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*====================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/* Support for /proc/mtd */
1162
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001163static int mtd_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001165 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001167 seq_puts(m, "dev: size erasesize name\n");
Ingo Molnar48b19262006-03-31 02:29:41 -08001168 mutex_lock(&mtd_table_mutex);
Ben Hutchingsf1332ba2010-01-29 20:57:11 +00001169 mtd_for_each_device(mtd) {
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001170 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1171 mtd->index, (unsigned long long)mtd->size,
1172 mtd->erasesize, mtd->name);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001173 }
Ingo Molnar48b19262006-03-31 02:29:41 -08001174 mutex_unlock(&mtd_table_mutex);
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001178static int mtd_proc_open(struct inode *inode, struct file *file)
1179{
1180 return single_open(file, mtd_proc_show, NULL);
1181}
1182
1183static const struct file_operations mtd_proc_ops = {
1184 .open = mtd_proc_open,
1185 .read = seq_read,
1186 .llseek = seq_lseek,
1187 .release = single_release,
1188};
Kevin Cernekee45b09072009-04-04 11:03:04 -07001189#endif /* CONFIG_PROC_FS */
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/*====================================================================*/
1192/* Init code */
1193
Jens Axboe0661b1a2010-04-27 09:49:47 +02001194static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1195{
1196 int ret;
1197
1198 ret = bdi_init(bdi);
1199 if (!ret)
Kees Cook02aa2a32013-07-03 15:04:56 -07001200 ret = bdi_register(bdi, NULL, "%s", name);
Jens Axboe0661b1a2010-04-27 09:49:47 +02001201
1202 if (ret)
1203 bdi_destroy(bdi);
1204
1205 return ret;
1206}
1207
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001208static struct proc_dir_entry *proc_mtd;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static int __init init_mtd(void)
1211{
David Woodhouse15bce402009-04-05 07:40:58 -07001212 int ret;
Kevin Cernekee694bb7f2009-04-03 13:00:45 -07001213
Jens Axboe0661b1a2010-04-27 09:49:47 +02001214 ret = class_register(&mtd_class);
1215 if (ret)
1216 goto err_reg;
1217
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001218 ret = mtd_bdi_init(&mtd_bdi, "mtd");
Jens Axboe0661b1a2010-04-27 09:49:47 +02001219 if (ret)
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001220 goto err_bdi;
Jens Axboe0661b1a2010-04-27 09:49:47 +02001221
Alexey Dobriyan447d9bd2011-05-13 23:34:19 +03001222 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001223
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001224 ret = init_mtdchar();
1225 if (ret)
1226 goto out_procfs;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return 0;
Jens Axboe0661b1a2010-04-27 09:49:47 +02001229
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001230out_procfs:
1231 if (proc_mtd)
1232 remove_proc_entry("mtd", NULL);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001233err_bdi:
Jens Axboe0661b1a2010-04-27 09:49:47 +02001234 class_unregister(&mtd_class);
1235err_reg:
1236 pr_err("Error registering mtd class or bdi: %d\n", ret);
1237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240static void __exit cleanup_mtd(void)
1241{
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001242 cleanup_mtdchar();
Wanlong Gaod5ca5122011-05-20 21:14:30 +08001243 if (proc_mtd)
Artem Bityutskiy93e56212013-03-15 12:56:05 +02001244 remove_proc_entry("mtd", NULL);
David Woodhouse15bce402009-04-05 07:40:58 -07001245 class_unregister(&mtd_class);
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001246 bdi_destroy(&mtd_bdi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249module_init(init_mtd);
1250module_exit(cleanup_mtd);
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252MODULE_LICENSE("GPL");
1253MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1254MODULE_DESCRIPTION("Core MTD registration and access routines");