blob: 147e3785b791e0c2da59434b7b9dc4b8da840bd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Woodhousea1452a32010-08-08 20:58:20 +01002 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 */
19
Thomas Gleixner15fdc522005-11-07 00:14:42 +010020#include <linux/device.h>
21#include <linux/fs.h>
Andrew Morton0c1eafd2007-08-10 13:01:06 -070022#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +030023#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010024#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010027#include <linux/slab.h>
28#include <linux/sched.h>
Arnd Bergmann5aa82942010-06-02 14:28:52 +020029#include <linux/mutex.h>
David Howells402d3262009-02-12 10:40:00 +000030#include <linux/backing-dev.h>
Kevin Cernekee97718542009-04-08 22:53:13 -070031#include <linux/compat.h>
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030032#include <linux/mount.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030033#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070034#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/mtd/mtd.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030036#include <linux/mtd/partitions.h>
Anatolij Gustschindd02b672010-06-15 09:30:15 +020037#include <linux/mtd/map.h>
Murali Palnati3e8c3bf2012-02-15 16:51:17 +053038#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Thomas Gleixner15fdc522005-11-07 00:14:42 +010040#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010041
Arnd Bergmann5aa82942010-06-02 14:28:52 +020042static DEFINE_MUTEX(mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Nicolas Pitre045e9a52005-02-08 19:12:53 +000044/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020045 * Data structure to hold the pointer to the mtd device as well
Brian Norris92394b52011-07-20 09:53:42 -070046 * as mode information of various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000047 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020048struct mtd_file_info {
49 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030050 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020051 enum mtd_file_modes mode;
52};
Nicolas Pitre31f42332005-02-08 17:45:55 +000053
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020054static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020056 struct mtd_file_info *mfi = file->private_data;
57 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040060 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040062 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010063 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040065 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010066 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
68 default:
69 return -EINVAL;
70 }
71
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020072 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010073 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Todd Poynor8b491d72005-08-04 02:05:51 +010075 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
Al Viro00292bb2012-03-23 13:47:52 -040078static int count;
79static struct vfsmount *mnt;
80static struct file_system_type mtd_inodefs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020082static int mtdchar_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 int minor = iminor(inode);
85 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060086 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020088 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030089 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Brian Norris289c0522011-07-19 10:06:09 -070091 pr_debug("MTD_open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040094 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -EACCES;
96
Al Viro00292bb2012-03-23 13:47:52 -040097 ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
98 if (ret)
99 return ret;
100
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200101 mutex_lock(&mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000103
Jonathan Corbet60712392008-05-15 10:10:37 -0600104 if (IS_ERR(mtd)) {
105 ret = PTR_ERR(mtd);
106 goto out;
107 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000108
David Howells402d3262009-02-12 10:40:00 +0000109 if (mtd->type == MTD_ABSENT) {
Jonathan Corbet60712392008-05-15 10:10:37 -0600110 ret = -ENODEV;
Al Viroc65390f2012-04-09 01:36:28 -0400111 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Al Viro00292bb2012-03-23 13:47:52 -0400114 mtd_ino = iget_locked(mnt->mnt_sb, devnum);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300115 if (!mtd_ino) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300116 ret = -ENOMEM;
Al Viroc65390f2012-04-09 01:36:28 -0400117 goto out1;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300118 }
119 if (mtd_ino->i_state & I_NEW) {
120 mtd_ino->i_private = mtd;
121 mtd_ino->i_mode = S_IFCHR;
122 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
123 unlock_new_inode(mtd_ino);
124 }
125 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400128 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Jonathan Corbet60712392008-05-15 10:10:37 -0600129 ret = -EACCES;
Al Viroc65390f2012-04-09 01:36:28 -0400130 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000132
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200133 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
134 if (!mfi) {
Jonathan Corbet60712392008-05-15 10:10:37 -0600135 ret = -ENOMEM;
Al Viroc65390f2012-04-09 01:36:28 -0400136 goto out2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200137 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300138 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200139 mfi->mtd = mtd;
140 file->private_data = mfi;
Al Viroc65390f2012-04-09 01:36:28 -0400141 mutex_unlock(&mtd_mutex);
142 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200143
Al Viroc65390f2012-04-09 01:36:28 -0400144out2:
145 iput(mtd_ino);
146out1:
147 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600148out:
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200149 mutex_unlock(&mtd_mutex);
Al Viro00292bb2012-03-23 13:47:52 -0400150 simple_release_fs(&mnt, &count);
Jonathan Corbet60712392008-05-15 10:10:37 -0600151 return ret;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200152} /* mtdchar_open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*====================================================================*/
155
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200156static int mtdchar_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200158 struct mtd_file_info *mfi = file->private_data;
159 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Brian Norris289c0522011-07-19 10:06:09 -0700161 pr_debug("MTD_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200163 /* Only sync if opened RW */
Artem Bityutskiy327cf292011-12-30 16:35:35 +0200164 if ((file->f_mode & FMODE_WRITE))
Artem Bityutskiy85f2f2a2011-12-23 19:03:12 +0200165 mtd_sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000166
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300167 iput(mfi->ino);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200170 file->private_data = NULL;
171 kfree(mfi);
Al Viro00292bb2012-03-23 13:47:52 -0400172 simple_release_fs(&mnt, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return 0;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200175} /* mtdchar_close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Grant Erickson3e45cf52011-04-08 08:51:33 -0700177/* Back in June 2001, dwmw2 wrote:
178 *
179 * FIXME: This _really_ needs to die. In 2.5, we should lock the
180 * userspace buffer down and use it directly with readv/writev.
181 *
182 * The implementation below, using mtd_kmalloc_up_to, mitigates
183 * allocation failures when the system is under low-memory situations
184 * or if memory is highly fragmented at the cost of reducing the
185 * performance of the requested transfer due to a smaller buffer size.
186 *
187 * A more complex but more memory-efficient implementation based on
188 * get_user_pages and iovecs to cover extents of those pages is a
189 * longer-term goal, as intimated by dwmw2 above. However, for the
190 * write case, this requires yet more complex head and tail transfer
191 * handling when those head and tail offsets and sizes are such that
192 * alignment requirements are not met in the NAND subdriver.
193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200195static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
196 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200198 struct mtd_file_info *mfi = file->private_data;
199 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200200 size_t retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 size_t total_retlen=0;
202 int ret=0;
203 int len;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700204 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000206
Brian Norris289c0522011-07-19 10:06:09 -0700207 pr_debug("MTD_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (*ppos + count > mtd->size)
210 count = mtd->size - *ppos;
211
212 if (!count)
213 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000214
Grant Erickson3e45cf52011-04-08 08:51:33 -0700215 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100216 if (!kbuf)
217 return -ENOMEM;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700220 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200222 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700223 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiyd264f722011-12-23 18:40:06 +0200224 ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
225 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000226 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700227 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy4ea1cab2011-12-23 18:47:59 +0200228 ret = mtd_read_user_prot_reg(mtd, *ppos, len,
229 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000230 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700231 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200232 {
233 struct mtd_oob_ops ops;
234
Brian Norris0612b9d2011-08-30 18:45:40 -0700235 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200236 ops.datbuf = kbuf;
237 ops.oobbuf = NULL;
238 ops.len = len;
239
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200240 ret = mtd_read_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200241 retlen = ops.retlen;
242 break;
243 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000244 default:
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200245 ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000246 }
Brian Norris7854d3f2011-06-23 14:12:08 -0700247 /* Nand returns -EBADMSG on ECC errors, but it returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * the data. For our userspace tools it is important
Brian Norris7854d3f2011-06-23 14:12:08 -0700249 * to dump areas with ECC errors!
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200250 * For kernel internal usage it also might return -EUCLEAN
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300251 * to signal the caller that a bitflip has occurred and has
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200252 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * Userspace software which accesses NAND this way
254 * must be aware of the fact that it deals with NAND
255 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700256 if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *ppos += retlen;
258 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200259 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -EFAULT;
261 }
262 else
263 total_retlen += retlen;
264
265 count -= retlen;
266 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000267 if (retlen == 0)
268 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 else {
271 kfree(kbuf);
272 return ret;
273 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
Thago Galesib802c072006-04-17 17:38:15 +0100277 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200279} /* mtdchar_read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200281static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
282 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200284 struct mtd_file_info *mfi = file->private_data;
285 struct mtd_info *mtd = mfi->mtd;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700286 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 char *kbuf;
288 size_t retlen;
289 size_t total_retlen=0;
290 int ret=0;
291 int len;
292
Brian Norris289c0522011-07-19 10:06:09 -0700293 pr_debug("MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (*ppos == mtd->size)
296 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (*ppos + count > mtd->size)
299 count = mtd->size - *ppos;
300
301 if (!count)
302 return 0;
303
Grant Erickson3e45cf52011-04-08 08:51:33 -0700304 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100305 if (!kbuf)
306 return -ENOMEM;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700309 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (copy_from_user(kbuf, buf, len)) {
312 kfree(kbuf);
313 return -EFAULT;
314 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000315
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200316 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700317 case MTD_FILE_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000318 ret = -EROFS;
319 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700320 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy482b43a2011-12-23 18:50:04 +0200321 ret = mtd_write_user_prot_reg(mtd, *ppos, len,
322 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000323 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200324
Brian Norrisbeb133f2011-08-30 18:45:41 -0700325 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200326 {
327 struct mtd_oob_ops ops;
328
Brian Norris0612b9d2011-08-30 18:45:40 -0700329 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200330 ops.datbuf = kbuf;
331 ops.oobbuf = NULL;
Peter Wippichbf514082011-06-06 15:50:58 +0200332 ops.ooboffs = 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200333 ops.len = len;
334
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200335 ret = mtd_write_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200336 retlen = ops.retlen;
337 break;
338 }
339
Nicolas Pitre31f42332005-02-08 17:45:55 +0000340 default:
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200341 ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!ret) {
344 *ppos += retlen;
345 total_retlen += retlen;
346 count -= retlen;
347 buf += retlen;
348 }
349 else {
350 kfree(kbuf);
351 return ret;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Thago Galesib802c072006-04-17 17:38:15 +0100355 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200357} /* mtdchar_write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/*======================================================================
360
361 IOCTL calls for getting device parameters.
362
363======================================================================*/
364static void mtdchar_erase_callback (struct erase_info *instr)
365{
366 wake_up((wait_queue_head_t *)instr->priv);
367}
368
David Brownell34a82442008-07-30 12:35:05 -0700369#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200370static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
371{
372 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200373 size_t retlen;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200374 int ret = 0;
375
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200376 /*
377 * Make a fake call to mtd_read_fact_prot_reg() to check if OTP
378 * operations are supported.
379 */
Will Newton7a844772012-03-30 11:51:02 +0100380 if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == -EOPNOTSUPP)
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200381 return -EOPNOTSUPP;
382
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200383 switch (mode) {
384 case MTD_OTP_FACTORY:
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200385 mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200386 break;
387 case MTD_OTP_USER:
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200388 mfi->mode = MTD_FILE_MODE_OTP_USER;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200389 break;
390 default:
391 ret = -EINVAL;
392 case MTD_OTP_OFF:
393 break;
394 }
395 return ret;
396}
397#else
398# define otp_select_filemode(f,m) -EOPNOTSUPP
399#endif
400
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200401static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
Kevin Cernekee97718542009-04-08 22:53:13 -0700402 uint64_t start, uint32_t length, void __user *ptr,
403 uint32_t __user *retp)
404{
Brian Norris9ce244b2011-08-30 18:45:37 -0700405 struct mtd_file_info *mfi = file->private_data;
Kevin Cernekee97718542009-04-08 22:53:13 -0700406 struct mtd_oob_ops ops;
407 uint32_t retlen;
408 int ret = 0;
409
410 if (!(file->f_mode & FMODE_WRITE))
411 return -EPERM;
412
413 if (length > 4096)
414 return -EINVAL;
415
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200416 if (!mtd->_write_oob)
Kevin Cernekee97718542009-04-08 22:53:13 -0700417 ret = -EOPNOTSUPP;
418 else
Roel Kluin00404762010-01-29 10:35:04 +0100419 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700420
421 if (ret)
422 return ret;
423
424 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700425 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700426 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700427 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700428 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700429
430 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
431 return -EINVAL;
432
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200433 ops.oobbuf = memdup_user(ptr, length);
434 if (IS_ERR(ops.oobbuf))
435 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700436
Brian Norris305b93f2011-08-23 17:17:32 -0700437 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200438 ret = mtd_write_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700439
440 if (ops.oobretlen > 0xFFFFFFFFU)
441 ret = -EOVERFLOW;
442 retlen = ops.oobretlen;
443 if (copy_to_user(retp, &retlen, sizeof(length)))
444 ret = -EFAULT;
445
446 kfree(ops.oobbuf);
447 return ret;
448}
449
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200450static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
Brian Norrisc46f6482011-08-30 18:45:38 -0700451 uint64_t start, uint32_t length, void __user *ptr,
452 uint32_t __user *retp)
Kevin Cernekee97718542009-04-08 22:53:13 -0700453{
Brian Norrisc46f6482011-08-30 18:45:38 -0700454 struct mtd_file_info *mfi = file->private_data;
Kevin Cernekee97718542009-04-08 22:53:13 -0700455 struct mtd_oob_ops ops;
456 int ret = 0;
457
458 if (length > 4096)
459 return -EINVAL;
460
Artem Bityutskiydac26392011-12-28 17:50:34 +0200461 if (!access_ok(VERIFY_WRITE, ptr, length))
462 return -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700463
464 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700465 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700466 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700467 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700468 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700469
470 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
471 return -EINVAL;
472
473 ops.oobbuf = kmalloc(length, GFP_KERNEL);
474 if (!ops.oobbuf)
475 return -ENOMEM;
476
Brian Norris305b93f2011-08-23 17:17:32 -0700477 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200478 ret = mtd_read_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700479
480 if (put_user(ops.oobretlen, retp))
481 ret = -EFAULT;
482 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
483 ops.oobretlen))
484 ret = -EFAULT;
485
486 kfree(ops.oobbuf);
Brian Norris041e4572011-06-23 16:45:24 -0700487
488 /*
489 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
490 * data. For our userspace tools it is important to dump areas
491 * with ECC errors!
492 * For kernel internal usage it also might return -EUCLEAN
493 * to signal the caller that a bitflip has occured and has
494 * been corrected by the ECC algorithm.
495 *
Brian Norrisc478d7e2011-06-28 16:29:00 -0700496 * Note: currently the standard NAND function, nand_read_oob_std,
497 * does not calculate ECC for the OOB area, so do not rely on
498 * this behavior unless you have replaced it with your own.
Brian Norris041e4572011-06-23 16:45:24 -0700499 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700500 if (mtd_is_bitflip_or_eccerr(ret))
Brian Norris041e4572011-06-23 16:45:24 -0700501 return 0;
502
Kevin Cernekee97718542009-04-08 22:53:13 -0700503 return ret;
504}
505
Brian Norriscc26c3c2010-08-24 18:12:00 -0700506/*
507 * Copies (and truncates, if necessary) data from the larger struct,
508 * nand_ecclayout, to the smaller, deprecated layout struct,
Brian Norris92394b52011-07-20 09:53:42 -0700509 * nand_ecclayout_user. This is necessary only to support the deprecated
Brian Norriscc26c3c2010-08-24 18:12:00 -0700510 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
511 * nand_ecclayout flexibly (i.e. the struct may change size in new
512 * releases without requiring major rewrites).
513 */
514static int shrink_ecclayout(const struct nand_ecclayout *from,
515 struct nand_ecclayout_user *to)
516{
517 int i;
518
519 if (!from || !to)
520 return -EINVAL;
521
522 memset(to, 0, sizeof(*to));
523
Brian Norris0ceacf32010-09-19 23:57:12 -0700524 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700525 for (i = 0; i < to->eccbytes; i++)
526 to->eccpos[i] = from->eccpos[i];
527
528 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
529 if (from->oobfree[i].length == 0 &&
530 from->oobfree[i].offset == 0)
531 break;
532 to->oobavail += from->oobfree[i].length;
533 to->oobfree[i] = from->oobfree[i];
534 }
535
536 return 0;
537}
538
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200539static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300540 struct blkpg_ioctl_arg __user *arg)
541{
542 struct blkpg_ioctl_arg a;
543 struct blkpg_partition p;
544
545 if (!capable(CAP_SYS_ADMIN))
546 return -EPERM;
547
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300548 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
549 return -EFAULT;
550
551 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
552 return -EFAULT;
553
554 switch (a.op) {
555 case BLKPG_ADD_PARTITION:
556
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200557 /* Only master mtd device must be used to add partitions */
558 if (mtd_is_partition(mtd))
559 return -EINVAL;
560
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300561 return mtd_add_partition(mtd, p.devname, p.start, p.length);
562
563 case BLKPG_DEL_PARTITION:
564
565 if (p.pno < 0)
566 return -EINVAL;
567
568 return mtd_del_partition(mtd, p.pno);
569
570 default:
571 return -EINVAL;
572 }
573}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300574
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200575static int mtdchar_write_ioctl(struct mtd_info *mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700576 struct mtd_write_req __user *argp)
577{
578 struct mtd_write_req req;
579 struct mtd_oob_ops ops;
580 void __user *usr_data, *usr_oob;
581 int ret;
582
583 if (copy_from_user(&req, argp, sizeof(req)) ||
584 !access_ok(VERIFY_READ, req.usr_data, req.len) ||
585 !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
586 return -EFAULT;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200587 if (!mtd->_write_oob)
Brian Norrise99d8b02011-09-09 09:59:03 -0700588 return -EOPNOTSUPP;
589
590 ops.mode = req.mode;
591 ops.len = (size_t)req.len;
592 ops.ooblen = (size_t)req.ooblen;
593 ops.ooboffs = 0;
594
595 usr_data = (void __user *)(uintptr_t)req.usr_data;
596 usr_oob = (void __user *)(uintptr_t)req.usr_oob;
597
598 if (req.usr_data) {
599 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
606 if (req.usr_oob) {
607 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 void __user *argp = (void __user *)arg;
629 int ret = 0;
630 u_long size;
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
635 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
636 if (cmd & IOC_IN) {
637 if (!access_ok(VERIFY_READ, argp, size))
638 return -EFAULT;
639 }
640 if (cmd & IOC_OUT) {
641 if (!access_ok(VERIFY_WRITE, argp, size))
642 return -EFAULT;
643 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 switch (cmd) {
646 case MEMGETREGIONCOUNT:
647 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
648 return -EFAULT;
649 break;
650
651 case MEMGETREGIONINFO:
652 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700653 uint32_t ur_idx;
654 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700655 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Zev Weissb67c5f82008-09-01 05:02:12 -0700657 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EFAULT;
659
Dan Carpenter5e59be12010-09-08 21:39:56 +0200660 if (ur_idx >= mtd->numeraseregions)
661 return -EINVAL;
662
Zev Weissb67c5f82008-09-01 05:02:12 -0700663 kr = &(mtd->eraseregions[ur_idx]);
664
665 if (put_user(kr->offset, &(ur->offset))
666 || put_user(kr->erasesize, &(ur->erasesize))
667 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break;
671 }
672
673 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300674 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200675 info.type = mtd->type;
676 info.flags = mtd->flags;
677 info.size = mtd->size;
678 info.erasesize = mtd->erasesize;
679 info.writesize = mtd->writesize;
680 info.oobsize = mtd->oobsize;
Brian Norris19fb4342011-08-30 18:45:46 -0700681 /* The below field is obsolete */
682 info.padding = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200683 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return -EFAULT;
685 break;
686
687 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700688 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 {
690 struct erase_info *erase;
691
Al Viroaeb5d722008-09-02 15:28:45 -0400692 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EPERM;
694
Burman Yan95b93a02006-11-15 21:10:29 +0200695 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!erase)
697 ret = -ENOMEM;
698 else {
699 wait_queue_head_t waitq;
700 DECLARE_WAITQUEUE(wait, current);
701
702 init_waitqueue_head(&waitq);
703
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700704 if (cmd == MEMERASE64) {
705 struct erase_info_user64 einfo64;
706
707 if (copy_from_user(&einfo64, argp,
708 sizeof(struct erase_info_user64))) {
709 kfree(erase);
710 return -EFAULT;
711 }
712 erase->addr = einfo64.start;
713 erase->len = einfo64.length;
714 } else {
715 struct erase_info_user einfo32;
716
717 if (copy_from_user(&einfo32, argp,
718 sizeof(struct erase_info_user))) {
719 kfree(erase);
720 return -EFAULT;
721 }
722 erase->addr = einfo32.start;
723 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 erase->mtd = mtd;
726 erase->callback = mtdchar_erase_callback;
727 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 FIXME: Allow INTERRUPTIBLE. Which means
731 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 If the wq_head is on the stack, and we
734 leave because we got interrupted, then the
735 wq_head is no longer there when the
736 callback routine tries to wake us up.
737 */
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200738 ret = mtd_erase(mtd, erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!ret) {
740 set_current_state(TASK_UNINTERRUPTIBLE);
741 add_wait_queue(&waitq, &wait);
742 if (erase->state != MTD_ERASE_DONE &&
743 erase->state != MTD_ERASE_FAILED)
744 schedule();
745 remove_wait_queue(&waitq, &wait);
746 set_current_state(TASK_RUNNING);
747
748 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
749 }
750 kfree(erase);
751 }
752 break;
753 }
754
755 case MEMWRITEOOB:
756 {
757 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700758 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000759
Kevin Cernekee97718542009-04-08 22:53:13 -0700760 /* NOTE: writes return length to buf_user->length */
761 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700763 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200764 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700765 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 case MEMREADOOB:
770 {
771 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700772 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Kevin Cernekee97718542009-04-08 22:53:13 -0700774 /* NOTE: writes return length to buf_user->start */
775 if (copy_from_user(&buf, argp, sizeof(buf)))
776 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200778 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700779 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781 }
782
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700783 case MEMWRITEOOB64:
784 {
785 struct mtd_oob_buf64 buf;
786 struct mtd_oob_buf64 __user *buf_user = argp;
787
788 if (copy_from_user(&buf, argp, sizeof(buf)))
789 ret = -EFAULT;
790 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200791 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700792 (void __user *)(uintptr_t)buf.usr_ptr,
793 &buf_user->length);
794 break;
795 }
796
797 case MEMREADOOB64:
798 {
799 struct mtd_oob_buf64 buf;
800 struct mtd_oob_buf64 __user *buf_user = argp;
801
802 if (copy_from_user(&buf, argp, sizeof(buf)))
803 ret = -EFAULT;
804 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200805 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700806 (void __user *)(uintptr_t)buf.usr_ptr,
807 &buf_user->length);
808 break;
809 }
810
Brian Norrise99d8b02011-09-09 09:59:03 -0700811 case MEMWRITE:
812 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200813 ret = mtdchar_write_ioctl(mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700814 (struct mtd_write_req __user *)arg);
815 break;
816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case MEMLOCK:
819 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700820 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Harvey Harrison175428b2008-07-03 23:40:14 -0700822 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -EFAULT;
824
Artem Bityutskiy38134562011-12-30 17:00:35 +0200825 ret = mtd_lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827 }
828
829 case MEMUNLOCK:
830 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700831 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Harvey Harrison175428b2008-07-03 23:40:14 -0700833 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -EFAULT;
835
Artem Bityutskiy38134562011-12-30 17:00:35 +0200836 ret = mtd_unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
838 }
839
Richard Cochran99384242010-06-14 18:10:33 +0200840 case MEMISLOCKED:
841 {
842 struct erase_info_user einfo;
843
844 if (copy_from_user(&einfo, argp, sizeof(einfo)))
845 return -EFAULT;
846
Artem Bityutskiy38134562011-12-30 17:00:35 +0200847 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
Richard Cochran99384242010-06-14 18:10:33 +0200848 break;
849 }
850
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200851 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case MEMGETOOBSEL:
853 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200854 struct nand_oobinfo oi;
855
856 if (!mtd->ecclayout)
857 return -EOPNOTSUPP;
858 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
859 return -EINVAL;
860
861 oi.useecc = MTD_NANDECC_AUTOPLACE;
862 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
863 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
864 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200865 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200866
867 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -EFAULT;
869 break;
870 }
871
872 case MEMGETBADBLOCK:
873 {
874 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (copy_from_user(&offs, argp, sizeof(loff_t)))
877 return -EFAULT;
Artem Bityutskiy8f461a72012-01-02 13:48:54 +0200878 return mtd_block_isbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
880 }
881
882 case MEMSETBADBLOCK:
883 {
884 loff_t offs;
885
886 if (copy_from_user(&offs, argp, sizeof(loff_t)))
887 return -EFAULT;
Artem Bityutskiy800ffd32012-01-02 13:59:12 +0200888 return mtd_block_markbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890 }
891
David Brownell34a82442008-07-30 12:35:05 -0700892#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000893 case OTPSELECT:
894 {
895 int mode;
896 if (copy_from_user(&mode, argp, sizeof(int)))
897 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200898
Brian Norrisbeb133f2011-08-30 18:45:41 -0700899 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200900
901 ret = otp_select_filemode(mfi, mode);
902
Nicolas Pitre81dba482005-04-01 16:36:15 +0100903 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000904 break;
905 }
906
907 case OTPGETREGIONCOUNT:
908 case OTPGETREGIONINFO:
909 {
910 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
911 if (!buf)
912 return -ENOMEM;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200913 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700914 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200915 ret = mtd_get_fact_prot_info(mtd, buf, 4096);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000916 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700917 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200918 ret = mtd_get_user_prot_info(mtd, buf, 4096);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000919 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200920 default:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200921 ret = -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200922 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000923 }
924 if (ret >= 0) {
925 if (cmd == OTPGETREGIONCOUNT) {
926 int nbr = ret / sizeof(struct otp_info);
927 ret = copy_to_user(argp, &nbr, sizeof(int));
928 } else
929 ret = copy_to_user(argp, buf, ret);
930 if (ret)
931 ret = -EFAULT;
932 }
933 kfree(buf);
934 break;
935 }
936
937 case OTPLOCK:
938 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700939 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000940
Brian Norrisbeb133f2011-08-30 18:45:41 -0700941 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000942 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700943 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000944 return -EFAULT;
Artem Bityutskiy4403dbf2011-12-23 18:55:49 +0200945 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000946 break;
947 }
948#endif
949
Brian Norris7854d3f2011-06-23 14:12:08 -0700950 /* This ioctl is being deprecated - it truncates the ECC layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200951 case ECCGETLAYOUT:
952 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700953 struct nand_ecclayout_user *usrlay;
954
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200955 if (!mtd->ecclayout)
956 return -EOPNOTSUPP;
957
Brian Norriscc26c3c2010-08-24 18:12:00 -0700958 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
959 if (!usrlay)
960 return -ENOMEM;
961
962 shrink_ecclayout(mtd->ecclayout, usrlay);
963
964 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
965 ret = -EFAULT;
966 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200967 break;
968 }
969
970 case ECCGETSTATS:
971 {
Murali Palnati3e8c3bf2012-02-15 16:51:17 +0530972#ifdef CONFIG_MTD_LAZYECCSTATS
973 part_fill_badblockstats(mtd);
974#endif
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200975 if (copy_to_user(argp, &mtd->ecc_stats,
976 sizeof(struct mtd_ecc_stats)))
977 return -EFAULT;
978 break;
979 }
980
981 case MTDFILEMODE:
982 {
983 mfi->mode = 0;
984
985 switch(arg) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700986 case MTD_FILE_MODE_OTP_FACTORY:
987 case MTD_FILE_MODE_OTP_USER:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200988 ret = otp_select_filemode(mfi, arg);
989 break;
990
Brian Norrisbeb133f2011-08-30 18:45:41 -0700991 case MTD_FILE_MODE_RAW:
Artem Bityutskiyfc002e32011-12-28 18:35:07 +0200992 if (!mtd_has_oob(mtd))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200993 return -EOPNOTSUPP;
994 mfi->mode = arg;
995
Brian Norrisbeb133f2011-08-30 18:45:41 -0700996 case MTD_FILE_MODE_NORMAL:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200997 break;
998 default:
999 ret = -EINVAL;
1000 }
1001 file->f_pos = 0;
1002 break;
1003 }
1004
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001005 case BLKPG:
1006 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001007 ret = mtdchar_blkpg_ioctl(mtd,
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001008 (struct blkpg_ioctl_arg __user *)arg);
1009 break;
1010 }
1011
1012 case BLKRRPART:
1013 {
1014 /* No reread partition feature. Just return ok */
1015 ret = 0;
1016 break;
1017 }
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 default:
1020 ret = -ENOTTY;
1021 }
1022
1023 return ret;
1024} /* memory_ioctl */
1025
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001026static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
Arnd Bergmann55929332010-04-27 00:24:05 +02001027{
1028 int ret;
1029
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001030 mutex_lock(&mtd_mutex);
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001031 ret = mtdchar_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001032 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +02001033
1034 return ret;
1035}
1036
Kevin Cernekee97718542009-04-08 22:53:13 -07001037#ifdef CONFIG_COMPAT
1038
1039struct mtd_oob_buf32 {
1040 u_int32_t start;
1041 u_int32_t length;
1042 compat_caddr_t ptr; /* unsigned char* */
1043};
1044
1045#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1046#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1047
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001048static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
Kevin Cernekee97718542009-04-08 22:53:13 -07001049 unsigned long arg)
1050{
1051 struct mtd_file_info *mfi = file->private_data;
1052 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +01001053 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -07001054 int ret = 0;
1055
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001056 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001057
1058 switch (cmd) {
1059 case MEMWRITEOOB32:
1060 {
1061 struct mtd_oob_buf32 buf;
1062 struct mtd_oob_buf32 __user *buf_user = argp;
1063
1064 if (copy_from_user(&buf, argp, sizeof(buf)))
1065 ret = -EFAULT;
1066 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001067 ret = mtdchar_writeoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001068 buf.length, compat_ptr(buf.ptr),
1069 &buf_user->length);
1070 break;
1071 }
1072
1073 case MEMREADOOB32:
1074 {
1075 struct mtd_oob_buf32 buf;
1076 struct mtd_oob_buf32 __user *buf_user = argp;
1077
1078 /* NOTE: writes return length to buf->start */
1079 if (copy_from_user(&buf, argp, sizeof(buf)))
1080 ret = -EFAULT;
1081 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001082 ret = mtdchar_readoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001083 buf.length, compat_ptr(buf.ptr),
1084 &buf_user->start);
1085 break;
1086 }
1087 default:
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001088 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001089 }
1090
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001091 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001092
1093 return ret;
1094}
1095
1096#endif /* CONFIG_COMPAT */
1097
David Howells402d3262009-02-12 10:40:00 +00001098/*
1099 * try to determine where a shared mapping can be made
1100 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1101 * mappings)
1102 */
1103#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001104static unsigned long mtdchar_get_unmapped_area(struct file *file,
David Howells402d3262009-02-12 10:40:00 +00001105 unsigned long addr,
1106 unsigned long len,
1107 unsigned long pgoff,
1108 unsigned long flags)
1109{
1110 struct mtd_file_info *mfi = file->private_data;
1111 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiycd621272011-12-30 14:31:57 +02001112 unsigned long offset;
1113 int ret;
David Howells402d3262009-02-12 10:40:00 +00001114
Artem Bityutskiycd621272011-12-30 14:31:57 +02001115 if (addr != 0)
1116 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001117
Artem Bityutskiycd621272011-12-30 14:31:57 +02001118 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1119 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001120
Artem Bityutskiycd621272011-12-30 14:31:57 +02001121 offset = pgoff << PAGE_SHIFT;
1122 if (offset > mtd->size - len)
1123 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001124
Artem Bityutskiycd621272011-12-30 14:31:57 +02001125 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
1126 return ret == -EOPNOTSUPP ? -ENOSYS : ret;
David Howells402d3262009-02-12 10:40:00 +00001127}
1128#endif
1129
Linus Torvalds3f0e06c2012-09-08 12:57:30 -07001130static inline unsigned long get_vm_size(struct vm_area_struct *vma)
1131{
1132 return vma->vm_end - vma->vm_start;
1133}
1134
1135static inline resource_size_t get_vm_offset(struct vm_area_struct *vma)
1136{
1137 return (resource_size_t) vma->vm_pgoff << PAGE_SHIFT;
1138}
1139
1140/*
1141 * Set a new vm offset.
1142 *
1143 * Verify that the incoming offset really works as a page offset,
1144 * and that the offset and size fit in a resource_size_t.
1145 */
1146static inline int set_vm_offset(struct vm_area_struct *vma, resource_size_t off)
1147{
1148 pgoff_t pgoff = off >> PAGE_SHIFT;
1149 if (off != (resource_size_t) pgoff << PAGE_SHIFT)
1150 return -EINVAL;
1151 if (off + get_vm_size(vma) - 1 < off)
1152 return -EINVAL;
1153 vma->vm_pgoff = pgoff;
1154 return 0;
1155}
1156
David Howells402d3262009-02-12 10:40:00 +00001157/*
1158 * set up a mapping for shared memory segments
1159 */
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001160static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
David Howells402d3262009-02-12 10:40:00 +00001161{
1162#ifdef CONFIG_MMU
1163 struct mtd_file_info *mfi = file->private_data;
1164 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001165 struct map_info *map = mtd->priv;
Linus Torvalds3f0e06c2012-09-08 12:57:30 -07001166 resource_size_t start, off;
1167 unsigned long len, vma_len;
David Howells402d3262009-02-12 10:40:00 +00001168
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001169 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
Linus Torvalds3f0e06c2012-09-08 12:57:30 -07001170 off = get_vm_offset(vma);
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001171 start = map->phys;
1172 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1173 start &= PAGE_MASK;
Linus Torvalds3f0e06c2012-09-08 12:57:30 -07001174 vma_len = get_vm_size(vma);
1175
1176 /* Overflow in off+len? */
1177 if (vma_len + off < off)
1178 return -EINVAL;
1179 /* Does it fit in the mapping? */
1180 if (vma_len + off > len)
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001181 return -EINVAL;
1182
1183 off += start;
Linus Torvalds3f0e06c2012-09-08 12:57:30 -07001184 /* Did that overflow? */
1185 if (off < start)
1186 return -EINVAL;
1187 if (set_vm_offset(vma, off) < 0)
1188 return -EINVAL;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001189 vma->vm_flags |= VM_IO | VM_RESERVED;
1190
1191#ifdef pgprot_noncached
1192 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1193 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1194#endif
1195 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1196 vma->vm_end - vma->vm_start,
1197 vma->vm_page_prot))
1198 return -EAGAIN;
1199
David Howells402d3262009-02-12 10:40:00 +00001200 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001201 }
David Howells402d3262009-02-12 10:40:00 +00001202 return -ENOSYS;
1203#else
1204 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1205#endif
1206}
1207
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001208static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 .owner = THIS_MODULE,
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001210 .llseek = mtdchar_lseek,
1211 .read = mtdchar_read,
1212 .write = mtdchar_write,
1213 .unlocked_ioctl = mtdchar_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001214#ifdef CONFIG_COMPAT
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001215 .compat_ioctl = mtdchar_compat_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001216#endif
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001217 .open = mtdchar_open,
1218 .release = mtdchar_close,
1219 .mmap = mtdchar_mmap,
David Howells402d3262009-02-12 10:40:00 +00001220#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001221 .get_unmapped_area = mtdchar_get_unmapped_area,
David Howells402d3262009-02-12 10:40:00 +00001222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223};
1224
Al Viro00292bb2012-03-23 13:47:52 -04001225static const struct super_operations mtd_ops = {
1226 .drop_inode = generic_delete_inode,
1227 .statfs = simple_statfs,
1228};
1229
Al Viro51139ad2010-07-25 23:47:46 +04001230static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1231 int flags, const char *dev_name, void *data)
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001232{
Al Viro00292bb2012-03-23 13:47:52 -04001233 return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001234}
1235
1236static struct file_system_type mtd_inodefs_type = {
1237 .name = "mtd_inodefs",
Al Viro51139ad2010-07-25 23:47:46 +04001238 .mount = mtd_inodefs_mount,
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001239 .kill_sb = kill_anon_super,
1240};
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static int __init init_mtdchar(void)
1243{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001244 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001245
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001246 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001247 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001248 if (ret < 0) {
1249 pr_notice("Can't allocate major number %d for "
1250 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001254 ret = register_filesystem(&mtd_inodefs_type);
1255 if (ret) {
1256 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1257 goto err_unregister_chdev;
1258 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001259 return ret;
1260
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001261err_unregister_chdev:
1262 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1263 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static void __exit cleanup_mtdchar(void)
1267{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001268 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001269 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272module_init(init_mtdchar);
1273module_exit(cleanup_mtdchar);
1274
David Brownell1f24b5a2009-03-26 00:42:41 -07001275MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277MODULE_LICENSE("GPL");
1278MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1279MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001280MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);