blob: d0f9c4b0285c7d31581346ac45691c20f96d6399 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
David Woodhousea1452a32010-08-08 20:58:20 +01003 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Thomas Gleixner15fdc522005-11-07 00:14:42 +01006#include <linux/device.h>
7#include <linux/fs.h>
Andrew Morton0c1eafd2007-08-10 13:01:06 -07008#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +03009#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010010#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010013#include <linux/slab.h>
14#include <linux/sched.h>
Arnd Bergmann5aa82942010-06-02 14:28:52 +020015#include <linux/mutex.h>
David Howells402d3262009-02-12 10:40:00 +000016#include <linux/backing-dev.h>
Kevin Cernekee97718542009-04-08 22:53:13 -070017#include <linux/compat.h>
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030018#include <linux/mount.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030019#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070020#include <linux/magic.h>
Ezequiel Garciaf83c3832013-10-13 18:05:23 -030021#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mtd/mtd.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030023#include <linux/mtd/partitions.h>
Anatolij Gustschindd02b672010-06-15 09:30:15 +020024#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010027
Artem Bityutskiy660685d2013-03-14 13:27:40 +020028#include "mtdcore.h"
29
Nicolas Pitre045e9a52005-02-08 19:12:53 +000030/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020031 * Data structure to hold the pointer to the mtd device as well
Brian Norris92394b5c2011-07-20 09:53:42 -070032 * as mode information of various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000033 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020034struct mtd_file_info {
35 struct mtd_info *mtd;
36 enum mtd_file_modes mode;
37};
Nicolas Pitre31f42332005-02-08 17:45:55 +000038
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020039static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020041 struct mtd_file_info *mfi = file->private_data;
Al Virob9599572013-06-16 20:27:42 +040042 return fixed_size_llseek(file, offset, orig, mfi->mtd->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020045static int mtdchar_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 int minor = iminor(inode);
48 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060049 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020051 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Brian Norris289c0522011-07-19 10:06:09 -070053 pr_debug("MTD_open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040056 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -EACCES;
58
59 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000060
Alexander Sverdlinecd400c2021-02-17 22:18:44 +010061 if (IS_ERR(mtd))
62 return PTR_ERR(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000063
David Howells402d3262009-02-12 10:40:00 +000064 if (mtd->type == MTD_ABSENT) {
Jonathan Corbet60712392008-05-15 10:10:37 -060065 ret = -ENODEV;
Al Viroc65390f2012-04-09 01:36:28 -040066 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -040070 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Jonathan Corbet60712392008-05-15 10:10:37 -060071 ret = -EACCES;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010072 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +000074
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020075 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
76 if (!mfi) {
Jonathan Corbet60712392008-05-15 10:10:37 -060077 ret = -ENOMEM;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010078 goto out1;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020079 }
80 mfi->mtd = mtd;
81 file->private_data = mfi;
Al Viroc65390f2012-04-09 01:36:28 -040082 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020083
Al Viroc65390f2012-04-09 01:36:28 -040084out1:
85 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -060086 return ret;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020087} /* mtdchar_open */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/*====================================================================*/
90
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020091static int mtdchar_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020093 struct mtd_file_info *mfi = file->private_data;
94 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Brian Norris289c0522011-07-19 10:06:09 -070096 pr_debug("MTD_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +020098 /* Only sync if opened RW */
Artem Bityutskiy327cf292011-12-30 16:35:35 +020099 if ((file->f_mode & FMODE_WRITE))
Artem Bityutskiy85f2f2a2011-12-23 19:03:12 +0200100 mtd_sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200103 file->private_data = NULL;
104 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 return 0;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200107} /* mtdchar_close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Grant Erickson3e45cf52011-04-08 08:51:33 -0700109/* Back in June 2001, dwmw2 wrote:
110 *
111 * FIXME: This _really_ needs to die. In 2.5, we should lock the
112 * userspace buffer down and use it directly with readv/writev.
113 *
114 * The implementation below, using mtd_kmalloc_up_to, mitigates
115 * allocation failures when the system is under low-memory situations
116 * or if memory is highly fragmented at the cost of reducing the
117 * performance of the requested transfer due to a smaller buffer size.
118 *
119 * A more complex but more memory-efficient implementation based on
120 * get_user_pages and iovecs to cover extents of those pages is a
121 * longer-term goal, as intimated by dwmw2 above. However, for the
122 * write case, this requires yet more complex head and tail transfer
123 * handling when those head and tail offsets and sizes are such that
124 * alignment requirements are not met in the NAND subdriver.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200127static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
128 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200130 struct mtd_file_info *mfi = file->private_data;
131 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200132 size_t retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 size_t total_retlen=0;
134 int ret=0;
135 int len;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700136 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000138
Brian Norris289c0522011-07-19 10:06:09 -0700139 pr_debug("MTD_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jann Horn6c6bc9e2018-07-07 05:37:22 +0200141 if (*ppos + count > mtd->size) {
142 if (*ppos < mtd->size)
143 count = mtd->size - *ppos;
144 else
145 count = 0;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (!count)
149 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000150
Grant Erickson3e45cf52011-04-08 08:51:33 -0700151 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100152 if (!kbuf)
153 return -ENOMEM;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700156 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200158 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700159 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiyd264f722011-12-23 18:40:06 +0200160 ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
161 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000162 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700163 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy4ea1cab2011-12-23 18:47:59 +0200164 ret = mtd_read_user_prot_reg(mtd, *ppos, len,
165 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000166 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700167 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200168 {
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200169 struct mtd_oob_ops ops = {};
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200170
Brian Norris0612b9d2011-08-30 18:45:40 -0700171 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200172 ops.datbuf = kbuf;
173 ops.oobbuf = NULL;
174 ops.len = len;
175
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200176 ret = mtd_read_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200177 retlen = ops.retlen;
178 break;
179 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000180 default:
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200181 ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000182 }
Brian Norris7854d3f2011-06-23 14:12:08 -0700183 /* Nand returns -EBADMSG on ECC errors, but it returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * the data. For our userspace tools it is important
Brian Norris7854d3f2011-06-23 14:12:08 -0700185 * to dump areas with ECC errors!
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200186 * For kernel internal usage it also might return -EUCLEAN
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300187 * to signal the caller that a bitflip has occurred and has
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200188 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * Userspace software which accesses NAND this way
190 * must be aware of the fact that it deals with NAND
191 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700192 if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *ppos += retlen;
194 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200195 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EFAULT;
197 }
198 else
199 total_retlen += retlen;
200
201 count -= retlen;
202 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000203 if (retlen == 0)
204 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 else {
207 kfree(kbuf);
208 return ret;
209 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
Thago Galesib802c072006-04-17 17:38:15 +0100213 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200215} /* mtdchar_read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200217static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
218 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200220 struct mtd_file_info *mfi = file->private_data;
221 struct mtd_info *mtd = mfi->mtd;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700222 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 char *kbuf;
224 size_t retlen;
225 size_t total_retlen=0;
226 int ret=0;
227 int len;
228
Brian Norris289c0522011-07-19 10:06:09 -0700229 pr_debug("MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000230
Jann Horn6c6bc9e2018-07-07 05:37:22 +0200231 if (*ppos >= mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (*ppos + count > mtd->size)
235 count = mtd->size - *ppos;
236
237 if (!count)
238 return 0;
239
Grant Erickson3e45cf52011-04-08 08:51:33 -0700240 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100241 if (!kbuf)
242 return -ENOMEM;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700245 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (copy_from_user(kbuf, buf, len)) {
248 kfree(kbuf);
249 return -EFAULT;
250 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000251
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200252 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700253 case MTD_FILE_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000254 ret = -EROFS;
255 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700256 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy482b43a2011-12-23 18:50:04 +0200257 ret = mtd_write_user_prot_reg(mtd, *ppos, len,
258 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000259 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200260
Brian Norrisbeb133f2011-08-30 18:45:41 -0700261 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200262 {
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200263 struct mtd_oob_ops ops = {};
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200264
Brian Norris0612b9d2011-08-30 18:45:40 -0700265 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200266 ops.datbuf = kbuf;
267 ops.oobbuf = NULL;
Peter Wippichbf514082011-06-06 15:50:58 +0200268 ops.ooboffs = 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200269 ops.len = len;
270
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200271 ret = mtd_write_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200272 retlen = ops.retlen;
273 break;
274 }
275
Nicolas Pitre31f42332005-02-08 17:45:55 +0000276 default:
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200277 ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000278 }
Christian Riesch9a78bc82014-03-06 12:42:37 +0100279
280 /*
281 * Return -ENOSPC only if no data could be written at all.
282 * Otherwise just return the number of bytes that actually
283 * have been written.
284 */
285 if ((ret == -ENOSPC) && (total_retlen))
286 break;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!ret) {
289 *ppos += retlen;
290 total_retlen += retlen;
291 count -= retlen;
292 buf += retlen;
293 }
294 else {
295 kfree(kbuf);
296 return ret;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Thago Galesib802c072006-04-17 17:38:15 +0100300 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200302} /* mtdchar_write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/*======================================================================
305
306 IOCTL calls for getting device parameters.
307
308======================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200310static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
311{
312 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200313 size_t retlen;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200314
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200315 switch (mode) {
316 case MTD_OTP_FACTORY:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100317 if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
318 -EOPNOTSUPP)
319 return -EOPNOTSUPP;
320
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200321 mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200322 break;
323 case MTD_OTP_USER:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100324 if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
325 -EOPNOTSUPP)
326 return -EOPNOTSUPP;
327
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200328 mfi->mode = MTD_FILE_MODE_OTP_USER;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200329 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200330 case MTD_OTP_OFF:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100331 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200332 break;
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100333 default:
334 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200335 }
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100336
337 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200338}
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200339
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200340static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
Kevin Cernekee97718542009-04-08 22:53:13 -0700341 uint64_t start, uint32_t length, void __user *ptr,
342 uint32_t __user *retp)
343{
Miquel Raynal46b58892020-01-14 10:09:52 +0100344 struct mtd_info *master = mtd_get_master(mtd);
Brian Norris9ce244b2011-08-30 18:45:37 -0700345 struct mtd_file_info *mfi = file->private_data;
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200346 struct mtd_oob_ops ops = {};
Kevin Cernekee97718542009-04-08 22:53:13 -0700347 uint32_t retlen;
348 int ret = 0;
349
Kevin Cernekee97718542009-04-08 22:53:13 -0700350 if (length > 4096)
351 return -EINVAL;
352
Miquel Raynal46b58892020-01-14 10:09:52 +0100353 if (!master->_write_oob)
Al Viro0db188f2017-09-29 13:55:19 -0400354 return -EOPNOTSUPP;
Kevin Cernekee97718542009-04-08 22:53:13 -0700355
356 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700357 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700358 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700359 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700360 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700361
362 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
363 return -EINVAL;
364
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200365 ops.oobbuf = memdup_user(ptr, length);
366 if (IS_ERR(ops.oobbuf))
367 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700368
Brian Norris305b93f2011-08-23 17:17:32 -0700369 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200370 ret = mtd_write_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700371
372 if (ops.oobretlen > 0xFFFFFFFFU)
373 ret = -EOVERFLOW;
374 retlen = ops.oobretlen;
375 if (copy_to_user(retp, &retlen, sizeof(length)))
376 ret = -EFAULT;
377
378 kfree(ops.oobbuf);
379 return ret;
380}
381
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200382static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
Brian Norrisc46f6482011-08-30 18:45:38 -0700383 uint64_t start, uint32_t length, void __user *ptr,
384 uint32_t __user *retp)
Kevin Cernekee97718542009-04-08 22:53:13 -0700385{
Brian Norrisc46f6482011-08-30 18:45:38 -0700386 struct mtd_file_info *mfi = file->private_data;
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200387 struct mtd_oob_ops ops = {};
Kevin Cernekee97718542009-04-08 22:53:13 -0700388 int ret = 0;
389
390 if (length > 4096)
391 return -EINVAL;
392
Kevin Cernekee97718542009-04-08 22:53:13 -0700393 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700394 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700395 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700396 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700397 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700398
399 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
400 return -EINVAL;
401
402 ops.oobbuf = kmalloc(length, GFP_KERNEL);
403 if (!ops.oobbuf)
404 return -ENOMEM;
405
Brian Norris305b93f2011-08-23 17:17:32 -0700406 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200407 ret = mtd_read_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700408
409 if (put_user(ops.oobretlen, retp))
410 ret = -EFAULT;
411 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
412 ops.oobretlen))
413 ret = -EFAULT;
414
415 kfree(ops.oobbuf);
Brian Norris041e4572011-06-23 16:45:24 -0700416
417 /*
418 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
419 * data. For our userspace tools it is important to dump areas
420 * with ECC errors!
421 * For kernel internal usage it also might return -EUCLEAN
Nobuhiro Iwamatsu5d708ec2017-01-27 10:36:38 +0900422 * to signal the caller that a bitflip has occurred and has
Brian Norris041e4572011-06-23 16:45:24 -0700423 * been corrected by the ECC algorithm.
424 *
Brian Norrisc478d7e2011-06-28 16:29:00 -0700425 * Note: currently the standard NAND function, nand_read_oob_std,
426 * does not calculate ECC for the OOB area, so do not rely on
427 * this behavior unless you have replaced it with your own.
Brian Norris041e4572011-06-23 16:45:24 -0700428 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700429 if (mtd_is_bitflip_or_eccerr(ret))
Brian Norris041e4572011-06-23 16:45:24 -0700430 return 0;
431
Kevin Cernekee97718542009-04-08 22:53:13 -0700432 return ret;
433}
434
Brian Norriscc26c3c2010-08-24 18:12:00 -0700435/*
Boris Brezillonaab616e2016-02-04 10:16:18 +0100436 * Copies (and truncates, if necessary) OOB layout information to the
437 * deprecated layout struct, nand_ecclayout_user. This is necessary only to
438 * support the deprecated API ioctl ECCGETLAYOUT while allowing all new
439 * functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops
440 * can describe any kind of OOB layout with almost zero overhead from a
441 * memory usage point of view).
Brian Norriscc26c3c2010-08-24 18:12:00 -0700442 */
Boris Brezillonc2b78452016-02-03 20:10:30 +0100443static int shrink_ecclayout(struct mtd_info *mtd,
444 struct nand_ecclayout_user *to)
Brian Norriscc26c3c2010-08-24 18:12:00 -0700445{
Boris Brezillonc2b78452016-02-03 20:10:30 +0100446 struct mtd_oob_region oobregion;
447 int i, section = 0, ret;
Brian Norriscc26c3c2010-08-24 18:12:00 -0700448
Boris Brezillonc2b78452016-02-03 20:10:30 +0100449 if (!mtd || !to)
Brian Norriscc26c3c2010-08-24 18:12:00 -0700450 return -EINVAL;
451
452 memset(to, 0, sizeof(*to));
453
Boris Brezillonc2b78452016-02-03 20:10:30 +0100454 to->eccbytes = 0;
455 for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
456 u32 eccpos;
457
OuYang ZhiZhong6de56492018-03-11 15:59:07 +0800458 ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
Boris Brezillonc2b78452016-02-03 20:10:30 +0100459 if (ret < 0) {
460 if (ret != -ERANGE)
461 return ret;
462
463 break;
464 }
465
466 eccpos = oobregion.offset;
467 for (; i < MTD_MAX_ECCPOS_ENTRIES &&
468 eccpos < oobregion.offset + oobregion.length; i++) {
469 to->eccpos[i] = eccpos++;
470 to->eccbytes++;
471 }
472 }
Brian Norriscc26c3c2010-08-24 18:12:00 -0700473
474 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
Boris Brezillonc2b78452016-02-03 20:10:30 +0100475 ret = mtd_ooblayout_free(mtd, i, &oobregion);
476 if (ret < 0) {
477 if (ret != -ERANGE)
478 return ret;
479
Brian Norriscc26c3c2010-08-24 18:12:00 -0700480 break;
Boris Brezillonc2b78452016-02-03 20:10:30 +0100481 }
482
483 to->oobfree[i].offset = oobregion.offset;
484 to->oobfree[i].length = oobregion.length;
485 to->oobavail += to->oobfree[i].length;
Brian Norriscc26c3c2010-08-24 18:12:00 -0700486 }
487
488 return 0;
489}
490
Boris Brezillonc2b78452016-02-03 20:10:30 +0100491static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
492{
493 struct mtd_oob_region oobregion;
494 int i, section = 0, ret;
495
496 if (!mtd || !to)
497 return -EINVAL;
498
499 memset(to, 0, sizeof(*to));
500
501 to->eccbytes = 0;
502 for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
503 u32 eccpos;
504
OuYang ZhiZhong6de56492018-03-11 15:59:07 +0800505 ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
Boris Brezillonc2b78452016-02-03 20:10:30 +0100506 if (ret < 0) {
507 if (ret != -ERANGE)
508 return ret;
509
510 break;
511 }
512
513 if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
514 return -EINVAL;
515
516 eccpos = oobregion.offset;
517 for (; eccpos < oobregion.offset + oobregion.length; i++) {
518 to->eccpos[i] = eccpos++;
519 to->eccbytes++;
520 }
521 }
522
523 for (i = 0; i < 8; i++) {
524 ret = mtd_ooblayout_free(mtd, i, &oobregion);
525 if (ret < 0) {
526 if (ret != -ERANGE)
527 return ret;
528
529 break;
530 }
531
532 to->oobfree[i][0] = oobregion.offset;
533 to->oobfree[i][1] = oobregion.length;
534 }
535
536 to->useecc = MTD_NANDECC_AUTOPLACE;
537
538 return 0;
539}
540
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200541static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
Brian Norris53bb7242015-09-21 13:26:59 -0700542 struct blkpg_ioctl_arg *arg)
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300543{
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300544 struct blkpg_partition p;
545
546 if (!capable(CAP_SYS_ADMIN))
547 return -EPERM;
548
Brian Norris53bb7242015-09-21 13:26:59 -0700549 if (copy_from_user(&p, arg->data, sizeof(p)))
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300550 return -EFAULT;
551
Brian Norris53bb7242015-09-21 13:26:59 -0700552 switch (arg->op) {
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300553 case BLKPG_ADD_PARTITION:
554
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200555 /* Only master mtd device must be used to add partitions */
556 if (mtd_is_partition(mtd))
557 return -EINVAL;
558
Brian Norris1cc8d842014-07-21 19:08:13 -0700559 /* Sanitize user input */
560 p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
561
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300562 return mtd_add_partition(mtd, p.devname, p.start, p.length);
563
564 case BLKPG_DEL_PARTITION:
565
566 if (p.pno < 0)
567 return -EINVAL;
568
569 return mtd_del_partition(mtd, p.pno);
570
571 default:
572 return -EINVAL;
573 }
574}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300575
Michał Kępień6420ac02021-11-30 12:31:49 +0100576static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
577 struct mtd_oob_ops *ops)
578{
579 uint32_t start_page, end_page;
580 u32 oob_per_page;
581
582 if (ops->len == 0 || ops->ooblen == 0)
583 return;
584
585 start_page = mtd_div_by_ws(start, mtd);
586 end_page = mtd_div_by_ws(start + ops->len - 1, mtd);
587 oob_per_page = mtd_oobavail(mtd, ops);
588
589 ops->ooblen = min_t(size_t, ops->ooblen,
590 (end_page - start_page + 1) * oob_per_page);
591}
592
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200593static int mtdchar_write_ioctl(struct mtd_info *mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700594 struct mtd_write_req __user *argp)
595{
Miquel Raynal46b58892020-01-14 10:09:52 +0100596 struct mtd_info *master = mtd_get_master(mtd);
Brian Norrise99d8b02011-09-09 09:59:03 -0700597 struct mtd_write_req req;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200598 const void __user *usr_data, *usr_oob;
Michał Kępień6420ac02021-11-30 12:31:49 +0100599 uint8_t *datbuf = NULL, *oobbuf = NULL;
600 size_t datbuf_len, oobbuf_len;
601 int ret = 0;
Brian Norrise99d8b02011-09-09 09:59:03 -0700602
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200603 if (copy_from_user(&req, argp, sizeof(req)))
Brian Norrise99d8b02011-09-09 09:59:03 -0700604 return -EFAULT;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200605
606 usr_data = (const void __user *)(uintptr_t)req.usr_data;
607 usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200608
Miquel Raynal46b58892020-01-14 10:09:52 +0100609 if (!master->_write_oob)
Brian Norrise99d8b02011-09-09 09:59:03 -0700610 return -EOPNOTSUPP;
Brian Norrise99d8b02011-09-09 09:59:03 -0700611
Michał Kępień6420ac02021-11-30 12:31:49 +0100612 if (!usr_data)
613 req.len = 0;
614
615 if (!usr_oob)
616 req.ooblen = 0;
617
618 if (req.start + req.len > mtd->size)
619 return -EINVAL;
620
621 datbuf_len = min_t(size_t, req.len, mtd->erasesize);
622 if (datbuf_len > 0) {
623 datbuf = kmalloc(datbuf_len, GFP_KERNEL);
624 if (!datbuf)
625 return -ENOMEM;
Brian Norrise99d8b02011-09-09 09:59:03 -0700626 }
627
Michał Kępień6420ac02021-11-30 12:31:49 +0100628 oobbuf_len = min_t(size_t, req.ooblen, mtd->erasesize);
629 if (oobbuf_len > 0) {
630 oobbuf = kmalloc(oobbuf_len, GFP_KERNEL);
631 if (!oobbuf) {
632 kfree(datbuf);
633 return -ENOMEM;
Brian Norrise99d8b02011-09-09 09:59:03 -0700634 }
Brian Norrise99d8b02011-09-09 09:59:03 -0700635 }
636
Michał Kępień6420ac02021-11-30 12:31:49 +0100637 while (req.len > 0 || (!usr_data && req.ooblen > 0)) {
638 struct mtd_oob_ops ops = {
639 .mode = req.mode,
640 .len = min_t(size_t, req.len, datbuf_len),
641 .ooblen = min_t(size_t, req.ooblen, oobbuf_len),
642 .datbuf = datbuf,
643 .oobbuf = oobbuf,
644 };
Brian Norrise99d8b02011-09-09 09:59:03 -0700645
Michał Kępień6420ac02021-11-30 12:31:49 +0100646 /*
647 * Shorten non-page-aligned, eraseblock-sized writes so that
648 * the write ends on an eraseblock boundary. This is necessary
649 * for adjust_oob_length() to properly handle non-page-aligned
650 * writes.
651 */
652 if (ops.len == mtd->erasesize)
653 ops.len -= mtd_mod_by_ws(req.start + ops.len, mtd);
654
655 /*
656 * For writes which are not OOB-only, adjust the amount of OOB
657 * data written according to the number of data pages written.
658 * This is necessary to prevent OOB data from being skipped
659 * over in data+OOB writes requiring multiple mtd_write_oob()
660 * calls to be completed.
661 */
662 adjust_oob_length(mtd, req.start, &ops);
663
664 if (copy_from_user(datbuf, usr_data, ops.len) ||
665 copy_from_user(oobbuf, usr_oob, ops.ooblen)) {
666 ret = -EFAULT;
667 break;
668 }
669
670 ret = mtd_write_oob(mtd, req.start, &ops);
671 if (ret)
672 break;
673
674 req.start += ops.retlen;
675 req.len -= ops.retlen;
676 usr_data += ops.retlen;
677
678 req.ooblen -= ops.oobretlen;
679 usr_oob += ops.oobretlen;
680 }
681
682 kfree(datbuf);
683 kfree(oobbuf);
Brian Norrise99d8b02011-09-09 09:59:03 -0700684
685 return ret;
686}
687
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200688static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200690 struct mtd_file_info *mfi = file->private_data;
691 struct mtd_info *mtd = mfi->mtd;
Miquel Raynal46b58892020-01-14 10:09:52 +0100692 struct mtd_info *master = mtd_get_master(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 void __user *argp = (void __user *)arg;
694 int ret = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200695 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000696
Brian Norris289c0522011-07-19 10:06:09 -0700697 pr_debug("MTD_ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200699 /*
700 * Check the file mode to require "dangerous" commands to have write
701 * permissions.
702 */
703 switch (cmd) {
704 /* "safe" commands */
705 case MEMGETREGIONCOUNT:
706 case MEMGETREGIONINFO:
707 case MEMGETINFO:
708 case MEMREADOOB:
709 case MEMREADOOB64:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200710 case MEMISLOCKED:
711 case MEMGETOOBSEL:
712 case MEMGETBADBLOCK:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200713 case OTPSELECT:
714 case OTPGETREGIONCOUNT:
715 case OTPGETREGIONINFO:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200716 case ECCGETLAYOUT:
717 case ECCGETSTATS:
718 case MTDFILEMODE:
719 case BLKPG:
720 case BLKRRPART:
721 break;
722
723 /* "dangerous" commands */
724 case MEMERASE:
725 case MEMERASE64:
Michael Walle1e977432021-03-03 16:57:35 +0100726 case MEMLOCK:
727 case MEMUNLOCK:
728 case MEMSETBADBLOCK:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200729 case MEMWRITEOOB:
730 case MEMWRITEOOB64:
731 case MEMWRITE:
Michael Walle1e977432021-03-03 16:57:35 +0100732 case OTPLOCK:
Michael Wallee3c1f1c2021-03-03 21:18:19 +0100733 case OTPERASE:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200734 if (!(file->f_mode & FMODE_WRITE))
735 return -EPERM;
736 break;
737
738 default:
739 return -ENOTTY;
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 switch (cmd) {
743 case MEMGETREGIONCOUNT:
744 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
745 return -EFAULT;
746 break;
747
748 case MEMGETREGIONINFO:
749 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700750 uint32_t ur_idx;
751 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700752 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Zev Weissb67c5f82008-09-01 05:02:12 -0700754 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EFAULT;
756
Dan Carpenter5e59be12010-09-08 21:39:56 +0200757 if (ur_idx >= mtd->numeraseregions)
758 return -EINVAL;
759
Zev Weissb67c5f82008-09-01 05:02:12 -0700760 kr = &(mtd->eraseregions[ur_idx]);
761
762 if (put_user(kr->offset, &(ur->offset))
763 || put_user(kr->erasesize, &(ur->erasesize))
764 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
768 }
769
770 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300771 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200772 info.type = mtd->type;
773 info.flags = mtd->flags;
774 info.size = mtd->size;
775 info.erasesize = mtd->erasesize;
776 info.writesize = mtd->writesize;
777 info.oobsize = mtd->oobsize;
Brian Norris19fb4342011-08-30 18:45:46 -0700778 /* The below field is obsolete */
779 info.padding = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200780 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EFAULT;
782 break;
783
784 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700785 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 {
787 struct erase_info *erase;
788
Burman Yan95b93a02006-11-15 21:10:29 +0200789 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!erase)
791 ret = -ENOMEM;
792 else {
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700793 if (cmd == MEMERASE64) {
794 struct erase_info_user64 einfo64;
795
796 if (copy_from_user(&einfo64, argp,
797 sizeof(struct erase_info_user64))) {
798 kfree(erase);
799 return -EFAULT;
800 }
801 erase->addr = einfo64.start;
802 erase->len = einfo64.length;
803 } else {
804 struct erase_info_user einfo32;
805
806 if (copy_from_user(&einfo32, argp,
807 sizeof(struct erase_info_user))) {
808 kfree(erase);
809 return -EFAULT;
810 }
811 erase->addr = einfo32.start;
812 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000814
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200815 ret = mtd_erase(mtd, erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 kfree(erase);
817 }
818 break;
819 }
820
821 case MEMWRITEOOB:
822 {
823 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700824 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000825
Kevin Cernekee97718542009-04-08 22:53:13 -0700826 /* NOTE: writes return length to buf_user->length */
827 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700829 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200830 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700831 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 case MEMREADOOB:
836 {
837 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700838 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Kevin Cernekee97718542009-04-08 22:53:13 -0700840 /* NOTE: writes return length to buf_user->start */
841 if (copy_from_user(&buf, argp, sizeof(buf)))
842 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200844 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700845 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 }
848
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700849 case MEMWRITEOOB64:
850 {
851 struct mtd_oob_buf64 buf;
852 struct mtd_oob_buf64 __user *buf_user = argp;
853
854 if (copy_from_user(&buf, argp, sizeof(buf)))
855 ret = -EFAULT;
856 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200857 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700858 (void __user *)(uintptr_t)buf.usr_ptr,
859 &buf_user->length);
860 break;
861 }
862
863 case MEMREADOOB64:
864 {
865 struct mtd_oob_buf64 buf;
866 struct mtd_oob_buf64 __user *buf_user = argp;
867
868 if (copy_from_user(&buf, argp, sizeof(buf)))
869 ret = -EFAULT;
870 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200871 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700872 (void __user *)(uintptr_t)buf.usr_ptr,
873 &buf_user->length);
874 break;
875 }
876
Brian Norrise99d8b02011-09-09 09:59:03 -0700877 case MEMWRITE:
878 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200879 ret = mtdchar_write_ioctl(mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700880 (struct mtd_write_req __user *)arg);
881 break;
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case MEMLOCK:
885 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700886 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Harvey Harrison175428b2008-07-03 23:40:14 -0700888 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -EFAULT;
890
Artem Bityutskiy38134562011-12-30 17:00:35 +0200891 ret = mtd_lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
893 }
894
895 case MEMUNLOCK:
896 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700897 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Harvey Harrison175428b2008-07-03 23:40:14 -0700899 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -EFAULT;
901
Artem Bityutskiy38134562011-12-30 17:00:35 +0200902 ret = mtd_unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
904 }
905
Richard Cochran99384242010-06-14 18:10:33 +0200906 case MEMISLOCKED:
907 {
908 struct erase_info_user einfo;
909
910 if (copy_from_user(&einfo, argp, sizeof(einfo)))
911 return -EFAULT;
912
Artem Bityutskiy38134562011-12-30 17:00:35 +0200913 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
Richard Cochran99384242010-06-14 18:10:33 +0200914 break;
915 }
916
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200917 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 case MEMGETOOBSEL:
919 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200920 struct nand_oobinfo oi;
921
Miquel Raynal46b58892020-01-14 10:09:52 +0100922 if (!master->ooblayout)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200923 return -EOPNOTSUPP;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200924
Boris Brezillonc2b78452016-02-03 20:10:30 +0100925 ret = get_oobinfo(mtd, &oi);
926 if (ret)
927 return ret;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200928
929 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EFAULT;
931 break;
932 }
933
934 case MEMGETBADBLOCK:
935 {
936 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (copy_from_user(&offs, argp, sizeof(loff_t)))
939 return -EFAULT;
Artem Bityutskiy8f461a72012-01-02 13:48:54 +0200940 return mtd_block_isbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 case MEMSETBADBLOCK:
944 {
945 loff_t offs;
946
947 if (copy_from_user(&offs, argp, sizeof(loff_t)))
948 return -EFAULT;
Artem Bityutskiy800ffd32012-01-02 13:59:12 +0200949 return mtd_block_markbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Nicolas Pitre31f42332005-02-08 17:45:55 +0000952 case OTPSELECT:
953 {
954 int mode;
955 if (copy_from_user(&mode, argp, sizeof(int)))
956 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200957
Brian Norrisbeb133f2011-08-30 18:45:41 -0700958 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200959
960 ret = otp_select_filemode(mfi, mode);
961
Nicolas Pitre81dba482005-04-01 16:36:15 +0100962 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000963 break;
964 }
965
966 case OTPGETREGIONCOUNT:
967 case OTPGETREGIONINFO:
968 {
969 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100970 size_t retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000971 if (!buf)
972 return -ENOMEM;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200973 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700974 case MTD_FILE_MODE_OTP_FACTORY:
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100975 ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000976 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700977 case MTD_FILE_MODE_OTP_USER:
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100978 ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000979 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200980 default:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200981 ret = -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200982 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000983 }
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100984 if (!ret) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000985 if (cmd == OTPGETREGIONCOUNT) {
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100986 int nbr = retlen / sizeof(struct otp_info);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000987 ret = copy_to_user(argp, &nbr, sizeof(int));
988 } else
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100989 ret = copy_to_user(argp, buf, retlen);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000990 if (ret)
991 ret = -EFAULT;
992 }
993 kfree(buf);
994 break;
995 }
996
997 case OTPLOCK:
Michael Wallee3c1f1c2021-03-03 21:18:19 +0100998 case OTPERASE:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000999 {
Harvey Harrison175428b2008-07-03 23:40:14 -07001000 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +00001001
Brian Norrisbeb133f2011-08-30 18:45:41 -07001002 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +00001003 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -07001004 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +00001005 return -EFAULT;
Michael Wallee3c1f1c2021-03-03 21:18:19 +01001006 if (cmd == OTPLOCK)
1007 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
1008 else
1009 ret = mtd_erase_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +00001010 break;
1011 }
Nicolas Pitre31f42332005-02-08 17:45:55 +00001012
Brian Norris7854d3f2011-06-23 14:12:08 -07001013 /* This ioctl is being deprecated - it truncates the ECC layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001014 case ECCGETLAYOUT:
1015 {
Brian Norriscc26c3c2010-08-24 18:12:00 -07001016 struct nand_ecclayout_user *usrlay;
1017
Miquel Raynal46b58892020-01-14 10:09:52 +01001018 if (!master->ooblayout)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001019 return -EOPNOTSUPP;
1020
Brian Norriscc26c3c2010-08-24 18:12:00 -07001021 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
1022 if (!usrlay)
1023 return -ENOMEM;
1024
Boris Brezillonc2b78452016-02-03 20:10:30 +01001025 shrink_ecclayout(mtd, usrlay);
Brian Norriscc26c3c2010-08-24 18:12:00 -07001026
1027 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
1028 ret = -EFAULT;
1029 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001030 break;
1031 }
1032
1033 case ECCGETSTATS:
1034 {
1035 if (copy_to_user(argp, &mtd->ecc_stats,
1036 sizeof(struct mtd_ecc_stats)))
1037 return -EFAULT;
1038 break;
1039 }
1040
1041 case MTDFILEMODE:
1042 {
1043 mfi->mode = 0;
1044
1045 switch(arg) {
Brian Norrisbeb133f2011-08-30 18:45:41 -07001046 case MTD_FILE_MODE_OTP_FACTORY:
1047 case MTD_FILE_MODE_OTP_USER:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001048 ret = otp_select_filemode(mfi, arg);
1049 break;
1050
Brian Norrisbeb133f2011-08-30 18:45:41 -07001051 case MTD_FILE_MODE_RAW:
Artem Bityutskiyfc002e32011-12-28 18:35:07 +02001052 if (!mtd_has_oob(mtd))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001053 return -EOPNOTSUPP;
1054 mfi->mode = arg;
Gustavo A. R. Silva0975b632021-03-05 02:22:24 -06001055 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001056
Brian Norrisbeb133f2011-08-30 18:45:41 -07001057 case MTD_FILE_MODE_NORMAL:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001058 break;
1059 default:
1060 ret = -EINVAL;
1061 }
1062 file->f_pos = 0;
1063 break;
1064 }
1065
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001066 case BLKPG:
1067 {
Brian Norris53bb7242015-09-21 13:26:59 -07001068 struct blkpg_ioctl_arg __user *blk_arg = argp;
1069 struct blkpg_ioctl_arg a;
1070
1071 if (copy_from_user(&a, blk_arg, sizeof(a)))
1072 ret = -EFAULT;
1073 else
1074 ret = mtdchar_blkpg_ioctl(mtd, &a);
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001075 break;
1076 }
1077
1078 case BLKRRPART:
1079 {
1080 /* No reread partition feature. Just return ok */
1081 ret = 0;
1082 break;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 return ret;
1087} /* memory_ioctl */
1088
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001089static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
Arnd Bergmann55929332010-04-27 00:24:05 +02001090{
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001091 struct mtd_file_info *mfi = file->private_data;
1092 struct mtd_info *mtd = mfi->mtd;
1093 struct mtd_info *master = mtd_get_master(mtd);
Arnd Bergmann55929332010-04-27 00:24:05 +02001094 int ret;
1095
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001096 mutex_lock(&master->master.chrdev_lock);
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001097 ret = mtdchar_ioctl(file, cmd, arg);
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001098 mutex_unlock(&master->master.chrdev_lock);
Arnd Bergmann55929332010-04-27 00:24:05 +02001099
1100 return ret;
1101}
1102
Kevin Cernekee97718542009-04-08 22:53:13 -07001103#ifdef CONFIG_COMPAT
1104
1105struct mtd_oob_buf32 {
1106 u_int32_t start;
1107 u_int32_t length;
1108 compat_caddr_t ptr; /* unsigned char* */
1109};
1110
1111#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1112#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1113
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001114static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
Kevin Cernekee97718542009-04-08 22:53:13 -07001115 unsigned long arg)
1116{
1117 struct mtd_file_info *mfi = file->private_data;
1118 struct mtd_info *mtd = mfi->mtd;
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001119 struct mtd_info *master = mtd_get_master(mtd);
David Woodhouse0b6585c2009-05-29 16:09:08 +01001120 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -07001121 int ret = 0;
1122
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001123 mutex_lock(&master->master.chrdev_lock);
Kevin Cernekee97718542009-04-08 22:53:13 -07001124
1125 switch (cmd) {
1126 case MEMWRITEOOB32:
1127 {
1128 struct mtd_oob_buf32 buf;
1129 struct mtd_oob_buf32 __user *buf_user = argp;
1130
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +02001131 if (!(file->f_mode & FMODE_WRITE)) {
1132 ret = -EPERM;
1133 break;
1134 }
1135
Kevin Cernekee97718542009-04-08 22:53:13 -07001136 if (copy_from_user(&buf, argp, sizeof(buf)))
1137 ret = -EFAULT;
1138 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001139 ret = mtdchar_writeoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001140 buf.length, compat_ptr(buf.ptr),
1141 &buf_user->length);
1142 break;
1143 }
1144
1145 case MEMREADOOB32:
1146 {
1147 struct mtd_oob_buf32 buf;
1148 struct mtd_oob_buf32 __user *buf_user = argp;
1149
1150 /* NOTE: writes return length to buf->start */
1151 if (copy_from_user(&buf, argp, sizeof(buf)))
1152 ret = -EFAULT;
1153 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001154 ret = mtdchar_readoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001155 buf.length, compat_ptr(buf.ptr),
1156 &buf_user->start);
1157 break;
1158 }
Brian Norris53bb7242015-09-21 13:26:59 -07001159
1160 case BLKPG:
1161 {
1162 /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
1163 struct blkpg_compat_ioctl_arg __user *uarg = argp;
1164 struct blkpg_compat_ioctl_arg compat_arg;
1165 struct blkpg_ioctl_arg a;
1166
1167 if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
1168 ret = -EFAULT;
1169 break;
1170 }
1171
1172 memset(&a, 0, sizeof(a));
1173 a.op = compat_arg.op;
1174 a.flags = compat_arg.flags;
1175 a.datalen = compat_arg.datalen;
1176 a.data = compat_ptr(compat_arg.data);
1177
1178 ret = mtdchar_blkpg_ioctl(mtd, &a);
1179 break;
1180 }
1181
Kevin Cernekee97718542009-04-08 22:53:13 -07001182 default:
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001183 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001184 }
1185
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001186 mutex_unlock(&master->master.chrdev_lock);
Kevin Cernekee97718542009-04-08 22:53:13 -07001187
1188 return ret;
1189}
1190
1191#endif /* CONFIG_COMPAT */
1192
David Howells402d3262009-02-12 10:40:00 +00001193/*
1194 * try to determine where a shared mapping can be made
1195 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1196 * mappings)
1197 */
1198#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001199static unsigned long mtdchar_get_unmapped_area(struct file *file,
David Howells402d3262009-02-12 10:40:00 +00001200 unsigned long addr,
1201 unsigned long len,
1202 unsigned long pgoff,
1203 unsigned long flags)
1204{
1205 struct mtd_file_info *mfi = file->private_data;
1206 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiycd621272011-12-30 14:31:57 +02001207 unsigned long offset;
1208 int ret;
David Howells402d3262009-02-12 10:40:00 +00001209
Artem Bityutskiycd621272011-12-30 14:31:57 +02001210 if (addr != 0)
1211 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001212
Artem Bityutskiycd621272011-12-30 14:31:57 +02001213 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1214 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001215
Artem Bityutskiycd621272011-12-30 14:31:57 +02001216 offset = pgoff << PAGE_SHIFT;
1217 if (offset > mtd->size - len)
1218 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001219
Artem Bityutskiycd621272011-12-30 14:31:57 +02001220 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001221 return ret == -EOPNOTSUPP ? -ENODEV : ret;
David Howells402d3262009-02-12 10:40:00 +00001222}
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001223
1224static unsigned mtdchar_mmap_capabilities(struct file *file)
1225{
1226 struct mtd_file_info *mfi = file->private_data;
1227
1228 return mtd_mmap_capabilities(mfi->mtd);
1229}
David Howells402d3262009-02-12 10:40:00 +00001230#endif
1231
1232/*
1233 * set up a mapping for shared memory segments
1234 */
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001235static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
David Howells402d3262009-02-12 10:40:00 +00001236{
1237#ifdef CONFIG_MMU
1238 struct mtd_file_info *mfi = file->private_data;
1239 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001240 struct map_info *map = mtd->priv;
David Howells402d3262009-02-12 10:40:00 +00001241
David Woodhousef5cf8f02012-10-09 15:08:10 +01001242 /* This is broken because it assumes the MTD device is map-based
1243 and that mtd->priv is a valid struct map_info. It should be
1244 replaced with something that uses the mtd_get_unmapped_area()
1245 operation properly. */
1246 if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001247#ifdef pgprot_noncached
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001248 if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001249 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1250#endif
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001251 return vm_iomap_memory(vma, map->phys, map->size);
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001252 }
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001253 return -ENODEV;
David Howells402d3262009-02-12 10:40:00 +00001254#else
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001255 return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
David Howells402d3262009-02-12 10:40:00 +00001256#endif
1257}
1258
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001259static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 .owner = THIS_MODULE,
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001261 .llseek = mtdchar_lseek,
1262 .read = mtdchar_read,
1263 .write = mtdchar_write,
1264 .unlocked_ioctl = mtdchar_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001265#ifdef CONFIG_COMPAT
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001266 .compat_ioctl = mtdchar_compat_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001267#endif
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001268 .open = mtdchar_open,
1269 .release = mtdchar_close,
1270 .mmap = mtdchar_mmap,
David Howells402d3262009-02-12 10:40:00 +00001271#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001272 .get_unmapped_area = mtdchar_get_unmapped_area,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001273 .mmap_capabilities = mtdchar_mmap_capabilities,
David Howells402d3262009-02-12 10:40:00 +00001274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275};
1276
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001277int __init init_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001279 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001280
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001281 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001282 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001283 if (ret < 0) {
Artem Bityutskiy57ae2b62013-03-15 12:59:36 +02001284 pr_err("Can't allocate major number %d for MTD\n",
1285 MTD_CHAR_MAJOR);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001286 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001292void __exit cleanup_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001294 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Scott James Remnant90160e12009-03-02 18:42:39 +00001297MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);