blob: 155e991d9d7533b1232d2a4c088b7ff3bc4f9cc7 [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
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200576static int mtdchar_write_ioctl(struct mtd_info *mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700577 struct mtd_write_req __user *argp)
578{
Miquel Raynal46b58892020-01-14 10:09:52 +0100579 struct mtd_info *master = mtd_get_master(mtd);
Brian Norrise99d8b02011-09-09 09:59:03 -0700580 struct mtd_write_req req;
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200581 struct mtd_oob_ops ops = {};
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200582 const void __user *usr_data, *usr_oob;
Brian Norrise99d8b02011-09-09 09:59:03 -0700583 int ret;
584
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200585 if (copy_from_user(&req, argp, sizeof(req)))
Brian Norrise99d8b02011-09-09 09:59:03 -0700586 return -EFAULT;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200587
588 usr_data = (const void __user *)(uintptr_t)req.usr_data;
589 usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200590
Miquel Raynal46b58892020-01-14 10:09:52 +0100591 if (!master->_write_oob)
Brian Norrise99d8b02011-09-09 09:59:03 -0700592 return -EOPNOTSUPP;
Brian Norrise99d8b02011-09-09 09:59:03 -0700593 ops.mode = req.mode;
594 ops.len = (size_t)req.len;
595 ops.ooblen = (size_t)req.ooblen;
596 ops.ooboffs = 0;
597
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200598 if (usr_data) {
Brian Norrise99d8b02011-09-09 09:59:03 -0700599 ops.datbuf = memdup_user(usr_data, ops.len);
600 if (IS_ERR(ops.datbuf))
601 return PTR_ERR(ops.datbuf);
602 } else {
603 ops.datbuf = NULL;
604 }
605
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200606 if (usr_oob) {
Brian Norrise99d8b02011-09-09 09:59:03 -0700607 ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
608 if (IS_ERR(ops.oobbuf)) {
609 kfree(ops.datbuf);
610 return PTR_ERR(ops.oobbuf);
611 }
612 } else {
613 ops.oobbuf = NULL;
614 }
615
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200616 ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
Brian Norrise99d8b02011-09-09 09:59:03 -0700617
618 kfree(ops.datbuf);
619 kfree(ops.oobbuf);
620
621 return ret;
622}
623
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200624static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200626 struct mtd_file_info *mfi = file->private_data;
627 struct mtd_info *mtd = mfi->mtd;
Miquel Raynal46b58892020-01-14 10:09:52 +0100628 struct mtd_info *master = mtd_get_master(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 void __user *argp = (void __user *)arg;
630 int ret = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200631 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000632
Brian Norris289c0522011-07-19 10:06:09 -0700633 pr_debug("MTD_ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200635 /*
636 * Check the file mode to require "dangerous" commands to have write
637 * permissions.
638 */
639 switch (cmd) {
640 /* "safe" commands */
641 case MEMGETREGIONCOUNT:
642 case MEMGETREGIONINFO:
643 case MEMGETINFO:
644 case MEMREADOOB:
645 case MEMREADOOB64:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200646 case MEMISLOCKED:
647 case MEMGETOOBSEL:
648 case MEMGETBADBLOCK:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200649 case OTPSELECT:
650 case OTPGETREGIONCOUNT:
651 case OTPGETREGIONINFO:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200652 case ECCGETLAYOUT:
653 case ECCGETSTATS:
654 case MTDFILEMODE:
655 case BLKPG:
656 case BLKRRPART:
657 break;
658
659 /* "dangerous" commands */
660 case MEMERASE:
661 case MEMERASE64:
Michael Walle1e977432021-03-03 16:57:35 +0100662 case MEMLOCK:
663 case MEMUNLOCK:
664 case MEMSETBADBLOCK:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200665 case MEMWRITEOOB:
666 case MEMWRITEOOB64:
667 case MEMWRITE:
Michael Walle1e977432021-03-03 16:57:35 +0100668 case OTPLOCK:
Michael Wallee3c1f1c2021-03-03 21:18:19 +0100669 case OTPERASE:
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +0200670 if (!(file->f_mode & FMODE_WRITE))
671 return -EPERM;
672 break;
673
674 default:
675 return -ENOTTY;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 switch (cmd) {
679 case MEMGETREGIONCOUNT:
680 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
681 return -EFAULT;
682 break;
683
684 case MEMGETREGIONINFO:
685 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700686 uint32_t ur_idx;
687 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700688 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Zev Weissb67c5f82008-09-01 05:02:12 -0700690 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -EFAULT;
692
Dan Carpenter5e59be12010-09-08 21:39:56 +0200693 if (ur_idx >= mtd->numeraseregions)
694 return -EINVAL;
695
Zev Weissb67c5f82008-09-01 05:02:12 -0700696 kr = &(mtd->eraseregions[ur_idx]);
697
698 if (put_user(kr->offset, &(ur->offset))
699 || put_user(kr->erasesize, &(ur->erasesize))
700 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 }
705
706 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300707 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200708 info.type = mtd->type;
709 info.flags = mtd->flags;
710 info.size = mtd->size;
711 info.erasesize = mtd->erasesize;
712 info.writesize = mtd->writesize;
713 info.oobsize = mtd->oobsize;
Brian Norris19fb4342011-08-30 18:45:46 -0700714 /* The below field is obsolete */
715 info.padding = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200716 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -EFAULT;
718 break;
719
720 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700721 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 {
723 struct erase_info *erase;
724
Burman Yan95b93a02006-11-15 21:10:29 +0200725 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!erase)
727 ret = -ENOMEM;
728 else {
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700729 if (cmd == MEMERASE64) {
730 struct erase_info_user64 einfo64;
731
732 if (copy_from_user(&einfo64, argp,
733 sizeof(struct erase_info_user64))) {
734 kfree(erase);
735 return -EFAULT;
736 }
737 erase->addr = einfo64.start;
738 erase->len = einfo64.length;
739 } else {
740 struct erase_info_user einfo32;
741
742 if (copy_from_user(&einfo32, argp,
743 sizeof(struct erase_info_user))) {
744 kfree(erase);
745 return -EFAULT;
746 }
747 erase->addr = einfo32.start;
748 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000750
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200751 ret = mtd_erase(mtd, erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 kfree(erase);
753 }
754 break;
755 }
756
757 case MEMWRITEOOB:
758 {
759 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700760 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000761
Kevin Cernekee97718542009-04-08 22:53:13 -0700762 /* NOTE: writes return length to buf_user->length */
763 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700765 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200766 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700767 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 case MEMREADOOB:
772 {
773 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700774 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Kevin Cernekee97718542009-04-08 22:53:13 -0700776 /* NOTE: writes return length to buf_user->start */
777 if (copy_from_user(&buf, argp, sizeof(buf)))
778 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200780 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700781 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783 }
784
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700785 case MEMWRITEOOB64:
786 {
787 struct mtd_oob_buf64 buf;
788 struct mtd_oob_buf64 __user *buf_user = argp;
789
790 if (copy_from_user(&buf, argp, sizeof(buf)))
791 ret = -EFAULT;
792 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200793 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700794 (void __user *)(uintptr_t)buf.usr_ptr,
795 &buf_user->length);
796 break;
797 }
798
799 case MEMREADOOB64:
800 {
801 struct mtd_oob_buf64 buf;
802 struct mtd_oob_buf64 __user *buf_user = argp;
803
804 if (copy_from_user(&buf, argp, sizeof(buf)))
805 ret = -EFAULT;
806 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200807 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700808 (void __user *)(uintptr_t)buf.usr_ptr,
809 &buf_user->length);
810 break;
811 }
812
Brian Norrise99d8b02011-09-09 09:59:03 -0700813 case MEMWRITE:
814 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200815 ret = mtdchar_write_ioctl(mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700816 (struct mtd_write_req __user *)arg);
817 break;
818 }
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 case MEMLOCK:
821 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700822 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Harvey Harrison175428b2008-07-03 23:40:14 -0700824 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EFAULT;
826
Artem Bityutskiy38134562011-12-30 17:00:35 +0200827 ret = mtd_lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 break;
829 }
830
831 case MEMUNLOCK:
832 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700833 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Harvey Harrison175428b2008-07-03 23:40:14 -0700835 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -EFAULT;
837
Artem Bityutskiy38134562011-12-30 17:00:35 +0200838 ret = mtd_unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
840 }
841
Richard Cochran99384242010-06-14 18:10:33 +0200842 case MEMISLOCKED:
843 {
844 struct erase_info_user einfo;
845
846 if (copy_from_user(&einfo, argp, sizeof(einfo)))
847 return -EFAULT;
848
Artem Bityutskiy38134562011-12-30 17:00:35 +0200849 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
Richard Cochran99384242010-06-14 18:10:33 +0200850 break;
851 }
852
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200853 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 case MEMGETOOBSEL:
855 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200856 struct nand_oobinfo oi;
857
Miquel Raynal46b58892020-01-14 10:09:52 +0100858 if (!master->ooblayout)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200859 return -EOPNOTSUPP;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200860
Boris Brezillonc2b78452016-02-03 20:10:30 +0100861 ret = get_oobinfo(mtd, &oi);
862 if (ret)
863 return ret;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200864
865 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -EFAULT;
867 break;
868 }
869
870 case MEMGETBADBLOCK:
871 {
872 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (copy_from_user(&offs, argp, sizeof(loff_t)))
875 return -EFAULT;
Artem Bityutskiy8f461a72012-01-02 13:48:54 +0200876 return mtd_block_isbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 case MEMSETBADBLOCK:
880 {
881 loff_t offs;
882
883 if (copy_from_user(&offs, argp, sizeof(loff_t)))
884 return -EFAULT;
Artem Bityutskiy800ffd32012-01-02 13:59:12 +0200885 return mtd_block_markbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
Nicolas Pitre31f42332005-02-08 17:45:55 +0000888 case OTPSELECT:
889 {
890 int mode;
891 if (copy_from_user(&mode, argp, sizeof(int)))
892 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200893
Brian Norrisbeb133f2011-08-30 18:45:41 -0700894 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200895
896 ret = otp_select_filemode(mfi, mode);
897
Nicolas Pitre81dba482005-04-01 16:36:15 +0100898 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000899 break;
900 }
901
902 case OTPGETREGIONCOUNT:
903 case OTPGETREGIONINFO:
904 {
905 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100906 size_t retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000907 if (!buf)
908 return -ENOMEM;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200909 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700910 case MTD_FILE_MODE_OTP_FACTORY:
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100911 ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000912 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700913 case MTD_FILE_MODE_OTP_USER:
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100914 ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000915 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200916 default:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200917 ret = -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200918 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000919 }
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100920 if (!ret) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000921 if (cmd == OTPGETREGIONCOUNT) {
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100922 int nbr = retlen / sizeof(struct otp_info);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000923 ret = copy_to_user(argp, &nbr, sizeof(int));
924 } else
Christian Riesch4b78fc42f2014-01-28 09:29:44 +0100925 ret = copy_to_user(argp, buf, retlen);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000926 if (ret)
927 ret = -EFAULT;
928 }
929 kfree(buf);
930 break;
931 }
932
933 case OTPLOCK:
Michael Wallee3c1f1c2021-03-03 21:18:19 +0100934 case OTPERASE:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000935 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700936 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000937
Brian Norrisbeb133f2011-08-30 18:45:41 -0700938 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000939 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700940 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000941 return -EFAULT;
Michael Wallee3c1f1c2021-03-03 21:18:19 +0100942 if (cmd == OTPLOCK)
943 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
944 else
945 ret = mtd_erase_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000946 break;
947 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000948
Brian Norris7854d3f2011-06-23 14:12:08 -0700949 /* This ioctl is being deprecated - it truncates the ECC layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200950 case ECCGETLAYOUT:
951 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700952 struct nand_ecclayout_user *usrlay;
953
Miquel Raynal46b58892020-01-14 10:09:52 +0100954 if (!master->ooblayout)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200955 return -EOPNOTSUPP;
956
Brian Norriscc26c3c2010-08-24 18:12:00 -0700957 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
958 if (!usrlay)
959 return -ENOMEM;
960
Boris Brezillonc2b78452016-02-03 20:10:30 +0100961 shrink_ecclayout(mtd, usrlay);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700962
963 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
964 ret = -EFAULT;
965 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200966 break;
967 }
968
969 case ECCGETSTATS:
970 {
971 if (copy_to_user(argp, &mtd->ecc_stats,
972 sizeof(struct mtd_ecc_stats)))
973 return -EFAULT;
974 break;
975 }
976
977 case MTDFILEMODE:
978 {
979 mfi->mode = 0;
980
981 switch(arg) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700982 case MTD_FILE_MODE_OTP_FACTORY:
983 case MTD_FILE_MODE_OTP_USER:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200984 ret = otp_select_filemode(mfi, arg);
985 break;
986
Brian Norrisbeb133f2011-08-30 18:45:41 -0700987 case MTD_FILE_MODE_RAW:
Artem Bityutskiyfc002e32011-12-28 18:35:07 +0200988 if (!mtd_has_oob(mtd))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200989 return -EOPNOTSUPP;
990 mfi->mode = arg;
Gustavo A. R. Silva0975b632021-03-05 02:22:24 -0600991 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200992
Brian Norrisbeb133f2011-08-30 18:45:41 -0700993 case MTD_FILE_MODE_NORMAL:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200994 break;
995 default:
996 ret = -EINVAL;
997 }
998 file->f_pos = 0;
999 break;
1000 }
1001
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001002 case BLKPG:
1003 {
Brian Norris53bb7242015-09-21 13:26:59 -07001004 struct blkpg_ioctl_arg __user *blk_arg = argp;
1005 struct blkpg_ioctl_arg a;
1006
1007 if (copy_from_user(&a, blk_arg, sizeof(a)))
1008 ret = -EFAULT;
1009 else
1010 ret = mtdchar_blkpg_ioctl(mtd, &a);
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001011 break;
1012 }
1013
1014 case BLKRRPART:
1015 {
1016 /* No reread partition feature. Just return ok */
1017 ret = 0;
1018 break;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
1022 return ret;
1023} /* memory_ioctl */
1024
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001025static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
Arnd Bergmann55929332010-04-27 00:24:05 +02001026{
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001027 struct mtd_file_info *mfi = file->private_data;
1028 struct mtd_info *mtd = mfi->mtd;
1029 struct mtd_info *master = mtd_get_master(mtd);
Arnd Bergmann55929332010-04-27 00:24:05 +02001030 int ret;
1031
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001032 mutex_lock(&master->master.chrdev_lock);
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001033 ret = mtdchar_ioctl(file, cmd, arg);
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001034 mutex_unlock(&master->master.chrdev_lock);
Arnd Bergmann55929332010-04-27 00:24:05 +02001035
1036 return ret;
1037}
1038
Kevin Cernekee97718542009-04-08 22:53:13 -07001039#ifdef CONFIG_COMPAT
1040
1041struct mtd_oob_buf32 {
1042 u_int32_t start;
1043 u_int32_t length;
1044 compat_caddr_t ptr; /* unsigned char* */
1045};
1046
1047#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1048#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1049
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001050static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
Kevin Cernekee97718542009-04-08 22:53:13 -07001051 unsigned long arg)
1052{
1053 struct mtd_file_info *mfi = file->private_data;
1054 struct mtd_info *mtd = mfi->mtd;
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001055 struct mtd_info *master = mtd_get_master(mtd);
David Woodhouse0b6585c2009-05-29 16:09:08 +01001056 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -07001057 int ret = 0;
1058
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001059 mutex_lock(&master->master.chrdev_lock);
Kevin Cernekee97718542009-04-08 22:53:13 -07001060
1061 switch (cmd) {
1062 case MEMWRITEOOB32:
1063 {
1064 struct mtd_oob_buf32 buf;
1065 struct mtd_oob_buf32 __user *buf_user = argp;
1066
Greg Kroah-Hartmanf7e6b192020-07-16 13:53:46 +02001067 if (!(file->f_mode & FMODE_WRITE)) {
1068 ret = -EPERM;
1069 break;
1070 }
1071
Kevin Cernekee97718542009-04-08 22:53:13 -07001072 if (copy_from_user(&buf, argp, sizeof(buf)))
1073 ret = -EFAULT;
1074 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001075 ret = mtdchar_writeoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001076 buf.length, compat_ptr(buf.ptr),
1077 &buf_user->length);
1078 break;
1079 }
1080
1081 case MEMREADOOB32:
1082 {
1083 struct mtd_oob_buf32 buf;
1084 struct mtd_oob_buf32 __user *buf_user = argp;
1085
1086 /* NOTE: writes return length to buf->start */
1087 if (copy_from_user(&buf, argp, sizeof(buf)))
1088 ret = -EFAULT;
1089 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001090 ret = mtdchar_readoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001091 buf.length, compat_ptr(buf.ptr),
1092 &buf_user->start);
1093 break;
1094 }
Brian Norris53bb7242015-09-21 13:26:59 -07001095
1096 case BLKPG:
1097 {
1098 /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
1099 struct blkpg_compat_ioctl_arg __user *uarg = argp;
1100 struct blkpg_compat_ioctl_arg compat_arg;
1101 struct blkpg_ioctl_arg a;
1102
1103 if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
1104 ret = -EFAULT;
1105 break;
1106 }
1107
1108 memset(&a, 0, sizeof(a));
1109 a.op = compat_arg.op;
1110 a.flags = compat_arg.flags;
1111 a.datalen = compat_arg.datalen;
1112 a.data = compat_ptr(compat_arg.data);
1113
1114 ret = mtdchar_blkpg_ioctl(mtd, &a);
1115 break;
1116 }
1117
Kevin Cernekee97718542009-04-08 22:53:13 -07001118 default:
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001119 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001120 }
1121
Alexander Sverdlin1ad55282021-02-17 22:18:45 +01001122 mutex_unlock(&master->master.chrdev_lock);
Kevin Cernekee97718542009-04-08 22:53:13 -07001123
1124 return ret;
1125}
1126
1127#endif /* CONFIG_COMPAT */
1128
David Howells402d3262009-02-12 10:40:00 +00001129/*
1130 * try to determine where a shared mapping can be made
1131 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1132 * mappings)
1133 */
1134#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001135static unsigned long mtdchar_get_unmapped_area(struct file *file,
David Howells402d3262009-02-12 10:40:00 +00001136 unsigned long addr,
1137 unsigned long len,
1138 unsigned long pgoff,
1139 unsigned long flags)
1140{
1141 struct mtd_file_info *mfi = file->private_data;
1142 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiycd621272011-12-30 14:31:57 +02001143 unsigned long offset;
1144 int ret;
David Howells402d3262009-02-12 10:40:00 +00001145
Artem Bityutskiycd621272011-12-30 14:31:57 +02001146 if (addr != 0)
1147 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001148
Artem Bityutskiycd621272011-12-30 14:31:57 +02001149 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1150 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001151
Artem Bityutskiycd621272011-12-30 14:31:57 +02001152 offset = pgoff << PAGE_SHIFT;
1153 if (offset > mtd->size - len)
1154 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001155
Artem Bityutskiycd621272011-12-30 14:31:57 +02001156 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001157 return ret == -EOPNOTSUPP ? -ENODEV : ret;
David Howells402d3262009-02-12 10:40:00 +00001158}
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001159
1160static unsigned mtdchar_mmap_capabilities(struct file *file)
1161{
1162 struct mtd_file_info *mfi = file->private_data;
1163
1164 return mtd_mmap_capabilities(mfi->mtd);
1165}
David Howells402d3262009-02-12 10:40:00 +00001166#endif
1167
1168/*
1169 * set up a mapping for shared memory segments
1170 */
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001171static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
David Howells402d3262009-02-12 10:40:00 +00001172{
1173#ifdef CONFIG_MMU
1174 struct mtd_file_info *mfi = file->private_data;
1175 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001176 struct map_info *map = mtd->priv;
David Howells402d3262009-02-12 10:40:00 +00001177
David Woodhousef5cf8f02012-10-09 15:08:10 +01001178 /* This is broken because it assumes the MTD device is map-based
1179 and that mtd->priv is a valid struct map_info. It should be
1180 replaced with something that uses the mtd_get_unmapped_area()
1181 operation properly. */
1182 if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001183#ifdef pgprot_noncached
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001184 if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001185 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1186#endif
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001187 return vm_iomap_memory(vma, map->phys, map->size);
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001188 }
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001189 return -ENODEV;
David Howells402d3262009-02-12 10:40:00 +00001190#else
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001191 return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
David Howells402d3262009-02-12 10:40:00 +00001192#endif
1193}
1194
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001195static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 .owner = THIS_MODULE,
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001197 .llseek = mtdchar_lseek,
1198 .read = mtdchar_read,
1199 .write = mtdchar_write,
1200 .unlocked_ioctl = mtdchar_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001201#ifdef CONFIG_COMPAT
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001202 .compat_ioctl = mtdchar_compat_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001203#endif
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001204 .open = mtdchar_open,
1205 .release = mtdchar_close,
1206 .mmap = mtdchar_mmap,
David Howells402d3262009-02-12 10:40:00 +00001207#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001208 .get_unmapped_area = mtdchar_get_unmapped_area,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001209 .mmap_capabilities = mtdchar_mmap_capabilities,
David Howells402d3262009-02-12 10:40:00 +00001210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211};
1212
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001213int __init init_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001215 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001216
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001217 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001218 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001219 if (ret < 0) {
Artem Bityutskiy57ae2b62013-03-15 12:59:36 +02001220 pr_err("Can't allocate major number %d for MTD\n",
1221 MTD_CHAR_MAJOR);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001228void __exit cleanup_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001230 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Scott James Remnant90160e12009-03-02 18:42:39 +00001233MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);