Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 2 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 20 | #include <linux/device.h> |
| 21 | #include <linux/fs.h> |
Andrew Morton | 0c1eafd | 2007-08-10 13:01:06 -0700 | [diff] [blame] | 22 | #include <linux/mm.h> |
Artem Bityutskiy | 9c74034 | 2006-10-11 14:52:47 +0300 | [diff] [blame] | 23 | #include <linux/err.h> |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 24 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/sched.h> |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 29 | #include <linux/mutex.h> |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 31 | #include <linux/compat.h> |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 32 | #include <linux/mount.h> |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 33 | #include <linux/blkpg.h> |
Muthu Kumar | b502bd1 | 2012-03-23 15:01:50 -0700 | [diff] [blame] | 34 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/mtd/mtd.h> |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 36 | #include <linux/mtd/partitions.h> |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 37 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 39 | #include <asm/uaccess.h> |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 40 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 41 | #include "mtdcore.h" |
| 42 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 43 | static DEFINE_MUTEX(mtd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 45 | /* |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 46 | * Data structure to hold the pointer to the mtd device as well |
Brian Norris | 92394b5c | 2011-07-20 09:53:42 -0700 | [diff] [blame] | 47 | * as mode information of various use cases. |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 48 | */ |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 49 | struct mtd_file_info { |
| 50 | struct mtd_info *mtd; |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 51 | struct inode *ino; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 52 | enum mtd_file_modes mode; |
| 53 | }; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 54 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 55 | static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 57 | struct mtd_file_info *mfi = file->private_data; |
Al Viro | b959957 | 2013-06-16 20:27:42 +0400 | [diff] [blame] | 58 | return fixed_size_llseek(file, offset, orig, mfi->mtd->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 61 | static int count; |
| 62 | static struct vfsmount *mnt; |
| 63 | static struct file_system_type mtd_inodefs_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 65 | static int mtdchar_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | int minor = iminor(inode); |
| 68 | int devnum = minor >> 1; |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 69 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct mtd_info *mtd; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 71 | struct mtd_file_info *mfi; |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 72 | struct inode *mtd_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 74 | pr_debug("MTD_open\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* You can't open the RO devices RW */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 77 | if ((file->f_mode & FMODE_WRITE) && (minor & 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return -EACCES; |
| 79 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 80 | ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 84 | mutex_lock(&mtd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | mtd = get_mtd_device(NULL, devnum); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 86 | |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 87 | if (IS_ERR(mtd)) { |
| 88 | ret = PTR_ERR(mtd); |
| 89 | goto out; |
| 90 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 91 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 92 | if (mtd->type == MTD_ABSENT) { |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 93 | ret = -ENODEV; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 94 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 97 | mtd_ino = iget_locked(mnt->mnt_sb, devnum); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 98 | if (!mtd_ino) { |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 99 | ret = -ENOMEM; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 100 | goto out1; |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 101 | } |
| 102 | if (mtd_ino->i_state & I_NEW) { |
| 103 | mtd_ino->i_private = mtd; |
| 104 | mtd_ino->i_mode = S_IFCHR; |
| 105 | mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info; |
| 106 | unlock_new_inode(mtd_ino); |
| 107 | } |
| 108 | file->f_mapping = mtd_ino->i_mapping; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* You can't open it RW if it's not a writeable device */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 111 | if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) { |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 112 | ret = -EACCES; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 113 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 115 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 116 | mfi = kzalloc(sizeof(*mfi), GFP_KERNEL); |
| 117 | if (!mfi) { |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 118 | ret = -ENOMEM; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 119 | goto out2; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 120 | } |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 121 | mfi->ino = mtd_ino; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 122 | mfi->mtd = mtd; |
| 123 | file->private_data = mfi; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 124 | mutex_unlock(&mtd_mutex); |
| 125 | return 0; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 126 | |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 127 | out2: |
| 128 | iput(mtd_ino); |
| 129 | out1: |
| 130 | put_mtd_device(mtd); |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 131 | out: |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 132 | mutex_unlock(&mtd_mutex); |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 133 | simple_release_fs(&mnt, &count); |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 134 | return ret; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 135 | } /* mtdchar_open */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /*====================================================================*/ |
| 138 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 139 | static int mtdchar_close(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 141 | struct mtd_file_info *mfi = file->private_data; |
| 142 | struct mtd_info *mtd = mfi->mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 144 | pr_debug("MTD_close\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Joakim Tjernlund | 7eafaed | 2007-06-27 00:56:40 +0200 | [diff] [blame] | 146 | /* Only sync if opened RW */ |
Artem Bityutskiy | 327cf29 | 2011-12-30 16:35:35 +0200 | [diff] [blame] | 147 | if ((file->f_mode & FMODE_WRITE)) |
Artem Bityutskiy | 85f2f2a | 2011-12-23 19:03:12 +0200 | [diff] [blame] | 148 | mtd_sync(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 149 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 150 | iput(mfi->ino); |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | put_mtd_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 153 | file->private_data = NULL; |
| 154 | kfree(mfi); |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 155 | simple_release_fs(&mnt, &count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | return 0; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 158 | } /* mtdchar_close */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 160 | /* Back in June 2001, dwmw2 wrote: |
| 161 | * |
| 162 | * FIXME: This _really_ needs to die. In 2.5, we should lock the |
| 163 | * userspace buffer down and use it directly with readv/writev. |
| 164 | * |
| 165 | * The implementation below, using mtd_kmalloc_up_to, mitigates |
| 166 | * allocation failures when the system is under low-memory situations |
| 167 | * or if memory is highly fragmented at the cost of reducing the |
| 168 | * performance of the requested transfer due to a smaller buffer size. |
| 169 | * |
| 170 | * A more complex but more memory-efficient implementation based on |
| 171 | * get_user_pages and iovecs to cover extents of those pages is a |
| 172 | * longer-term goal, as intimated by dwmw2 above. However, for the |
| 173 | * write case, this requires yet more complex head and tail transfer |
| 174 | * handling when those head and tail offsets and sizes are such that |
| 175 | * alignment requirements are not met in the NAND subdriver. |
| 176 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 178 | static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, |
| 179 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 181 | struct mtd_file_info *mfi = file->private_data; |
| 182 | struct mtd_info *mtd = mfi->mtd; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 183 | size_t retlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | size_t total_retlen=0; |
| 185 | int ret=0; |
| 186 | int len; |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 187 | size_t size = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | char *kbuf; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 189 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 190 | pr_debug("MTD_read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | if (*ppos + count > mtd->size) |
| 193 | count = mtd->size - *ppos; |
| 194 | |
| 195 | if (!count) |
| 196 | return 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 197 | |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 198 | kbuf = mtd_kmalloc_up_to(mtd, &size); |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 199 | if (!kbuf) |
| 200 | return -ENOMEM; |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | while (count) { |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 203 | len = min_t(size_t, count, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 205 | switch (mfi->mode) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 206 | case MTD_FILE_MODE_OTP_FACTORY: |
Artem Bityutskiy | d264f72 | 2011-12-23 18:40:06 +0200 | [diff] [blame] | 207 | ret = mtd_read_fact_prot_reg(mtd, *ppos, len, |
| 208 | &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 209 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 210 | case MTD_FILE_MODE_OTP_USER: |
Artem Bityutskiy | 4ea1cab | 2011-12-23 18:47:59 +0200 | [diff] [blame] | 211 | ret = mtd_read_user_prot_reg(mtd, *ppos, len, |
| 212 | &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 213 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 214 | case MTD_FILE_MODE_RAW: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 215 | { |
| 216 | struct mtd_oob_ops ops; |
| 217 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 218 | ops.mode = MTD_OPS_RAW; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 219 | ops.datbuf = kbuf; |
| 220 | ops.oobbuf = NULL; |
| 221 | ops.len = len; |
| 222 | |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 223 | ret = mtd_read_oob(mtd, *ppos, &ops); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 224 | retlen = ops.retlen; |
| 225 | break; |
| 226 | } |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 227 | default: |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 228 | ret = mtd_read(mtd, *ppos, len, &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 229 | } |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 230 | /* Nand returns -EBADMSG on ECC errors, but it returns |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | * the data. For our userspace tools it is important |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 232 | * to dump areas with ECC errors! |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 233 | * For kernel internal usage it also might return -EUCLEAN |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 234 | * to signal the caller that a bitflip has occurred and has |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 235 | * been corrected by the ECC algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * Userspace software which accesses NAND this way |
| 237 | * must be aware of the fact that it deals with NAND |
| 238 | */ |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 239 | if (!ret || mtd_is_bitflip_or_eccerr(ret)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | *ppos += retlen; |
| 241 | if (copy_to_user(buf, kbuf, retlen)) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 242 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return -EFAULT; |
| 244 | } |
| 245 | else |
| 246 | total_retlen += retlen; |
| 247 | |
| 248 | count -= retlen; |
| 249 | buf += retlen; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 250 | if (retlen == 0) |
| 251 | count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | else { |
| 254 | kfree(kbuf); |
| 255 | return ret; |
| 256 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 260 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | return total_retlen; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 262 | } /* mtdchar_read */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 264 | static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count, |
| 265 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 267 | struct mtd_file_info *mfi = file->private_data; |
| 268 | struct mtd_info *mtd = mfi->mtd; |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 269 | size_t size = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | char *kbuf; |
| 271 | size_t retlen; |
| 272 | size_t total_retlen=0; |
| 273 | int ret=0; |
| 274 | int len; |
| 275 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 276 | pr_debug("MTD_write\n"); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (*ppos == mtd->size) |
| 279 | return -ENOSPC; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (*ppos + count > mtd->size) |
| 282 | count = mtd->size - *ppos; |
| 283 | |
| 284 | if (!count) |
| 285 | return 0; |
| 286 | |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 287 | kbuf = mtd_kmalloc_up_to(mtd, &size); |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 288 | if (!kbuf) |
| 289 | return -ENOMEM; |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | while (count) { |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 292 | len = min_t(size_t, count, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (copy_from_user(kbuf, buf, len)) { |
| 295 | kfree(kbuf); |
| 296 | return -EFAULT; |
| 297 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 298 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 299 | switch (mfi->mode) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 300 | case MTD_FILE_MODE_OTP_FACTORY: |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 301 | ret = -EROFS; |
| 302 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 303 | case MTD_FILE_MODE_OTP_USER: |
Artem Bityutskiy | 482b43a | 2011-12-23 18:50:04 +0200 | [diff] [blame] | 304 | ret = mtd_write_user_prot_reg(mtd, *ppos, len, |
| 305 | &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 306 | break; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 307 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 308 | case MTD_FILE_MODE_RAW: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 309 | { |
| 310 | struct mtd_oob_ops ops; |
| 311 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 312 | ops.mode = MTD_OPS_RAW; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 313 | ops.datbuf = kbuf; |
| 314 | ops.oobbuf = NULL; |
Peter Wippich | bf51408 | 2011-06-06 15:50:58 +0200 | [diff] [blame] | 315 | ops.ooboffs = 0; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 316 | ops.len = len; |
| 317 | |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 318 | ret = mtd_write_oob(mtd, *ppos, &ops); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 319 | retlen = ops.retlen; |
| 320 | break; |
| 321 | } |
| 322 | |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 323 | default: |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 324 | ret = mtd_write(mtd, *ppos, len, &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (!ret) { |
| 327 | *ppos += retlen; |
| 328 | total_retlen += retlen; |
| 329 | count -= retlen; |
| 330 | buf += retlen; |
| 331 | } |
| 332 | else { |
| 333 | kfree(kbuf); |
| 334 | return ret; |
| 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 338 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return total_retlen; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 340 | } /* mtdchar_write */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | /*====================================================================== |
| 343 | |
| 344 | IOCTL calls for getting device parameters. |
| 345 | |
| 346 | ======================================================================*/ |
| 347 | static void mtdchar_erase_callback (struct erase_info *instr) |
| 348 | { |
| 349 | wake_up((wait_queue_head_t *)instr->priv); |
| 350 | } |
| 351 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 352 | static int otp_select_filemode(struct mtd_file_info *mfi, int mode) |
| 353 | { |
| 354 | struct mtd_info *mtd = mfi->mtd; |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 355 | size_t retlen; |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 356 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 357 | switch (mode) { |
| 358 | case MTD_OTP_FACTORY: |
Uwe Kleine-König | 5dc63fa | 2013-03-04 17:35:24 +0100 | [diff] [blame] | 359 | if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == |
| 360 | -EOPNOTSUPP) |
| 361 | return -EOPNOTSUPP; |
| 362 | |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 363 | mfi->mode = MTD_FILE_MODE_OTP_FACTORY; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 364 | break; |
| 365 | case MTD_OTP_USER: |
Uwe Kleine-König | 5dc63fa | 2013-03-04 17:35:24 +0100 | [diff] [blame] | 366 | if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) == |
| 367 | -EOPNOTSUPP) |
| 368 | return -EOPNOTSUPP; |
| 369 | |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 370 | mfi->mode = MTD_FILE_MODE_OTP_USER; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 371 | break; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 372 | case MTD_OTP_OFF: |
Uwe Kleine-König | 5dc63fa | 2013-03-04 17:35:24 +0100 | [diff] [blame] | 373 | mfi->mode = MTD_FILE_MODE_NORMAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 374 | break; |
Uwe Kleine-König | 5dc63fa | 2013-03-04 17:35:24 +0100 | [diff] [blame] | 375 | default: |
| 376 | return -EINVAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 377 | } |
Uwe Kleine-König | 5dc63fa | 2013-03-04 17:35:24 +0100 | [diff] [blame] | 378 | |
| 379 | return 0; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 380 | } |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 381 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 382 | static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 383 | uint64_t start, uint32_t length, void __user *ptr, |
| 384 | uint32_t __user *retp) |
| 385 | { |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 386 | struct mtd_file_info *mfi = file->private_data; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 387 | struct mtd_oob_ops ops; |
| 388 | uint32_t retlen; |
| 389 | int ret = 0; |
| 390 | |
| 391 | if (!(file->f_mode & FMODE_WRITE)) |
| 392 | return -EPERM; |
| 393 | |
| 394 | if (length > 4096) |
| 395 | return -EINVAL; |
| 396 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 397 | if (!mtd->_write_oob) |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 398 | ret = -EOPNOTSUPP; |
| 399 | else |
Roel Kluin | 0040476 | 2010-01-29 10:35:04 +0100 | [diff] [blame] | 400 | ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 401 | |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | |
| 405 | ops.ooblen = length; |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 406 | ops.ooboffs = start & (mtd->writesize - 1); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 407 | ops.datbuf = NULL; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 408 | ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW : |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 409 | MTD_OPS_PLACE_OOB; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 410 | |
| 411 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) |
| 412 | return -EINVAL; |
| 413 | |
Julia Lawall | df1f1d1 | 2010-05-22 10:22:49 +0200 | [diff] [blame] | 414 | ops.oobbuf = memdup_user(ptr, length); |
| 415 | if (IS_ERR(ops.oobbuf)) |
| 416 | return PTR_ERR(ops.oobbuf); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 417 | |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 418 | start &= ~((uint64_t)mtd->writesize - 1); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 419 | ret = mtd_write_oob(mtd, start, &ops); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 420 | |
| 421 | if (ops.oobretlen > 0xFFFFFFFFU) |
| 422 | ret = -EOVERFLOW; |
| 423 | retlen = ops.oobretlen; |
| 424 | if (copy_to_user(retp, &retlen, sizeof(length))) |
| 425 | ret = -EFAULT; |
| 426 | |
| 427 | kfree(ops.oobbuf); |
| 428 | return ret; |
| 429 | } |
| 430 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 431 | static int mtdchar_readoob(struct file *file, struct mtd_info *mtd, |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 432 | uint64_t start, uint32_t length, void __user *ptr, |
| 433 | uint32_t __user *retp) |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 434 | { |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 435 | struct mtd_file_info *mfi = file->private_data; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 436 | struct mtd_oob_ops ops; |
| 437 | int ret = 0; |
| 438 | |
| 439 | if (length > 4096) |
| 440 | return -EINVAL; |
| 441 | |
Artem Bityutskiy | dac2639 | 2011-12-28 17:50:34 +0200 | [diff] [blame] | 442 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 443 | return -EFAULT; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 444 | |
| 445 | ops.ooblen = length; |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 446 | ops.ooboffs = start & (mtd->writesize - 1); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 447 | ops.datbuf = NULL; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 448 | ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW : |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 449 | MTD_OPS_PLACE_OOB; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 450 | |
| 451 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | ops.oobbuf = kmalloc(length, GFP_KERNEL); |
| 455 | if (!ops.oobbuf) |
| 456 | return -ENOMEM; |
| 457 | |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 458 | start &= ~((uint64_t)mtd->writesize - 1); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 459 | ret = mtd_read_oob(mtd, start, &ops); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 460 | |
| 461 | if (put_user(ops.oobretlen, retp)) |
| 462 | ret = -EFAULT; |
| 463 | else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf, |
| 464 | ops.oobretlen)) |
| 465 | ret = -EFAULT; |
| 466 | |
| 467 | kfree(ops.oobbuf); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * NAND returns -EBADMSG on ECC errors, but it returns the OOB |
| 471 | * data. For our userspace tools it is important to dump areas |
| 472 | * with ECC errors! |
| 473 | * For kernel internal usage it also might return -EUCLEAN |
| 474 | * to signal the caller that a bitflip has occured and has |
| 475 | * been corrected by the ECC algorithm. |
| 476 | * |
Brian Norris | c478d7e | 2011-06-28 16:29:00 -0700 | [diff] [blame] | 477 | * Note: currently the standard NAND function, nand_read_oob_std, |
| 478 | * does not calculate ECC for the OOB area, so do not rely on |
| 479 | * this behavior unless you have replaced it with your own. |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 480 | */ |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 481 | if (mtd_is_bitflip_or_eccerr(ret)) |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 482 | return 0; |
| 483 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 484 | return ret; |
| 485 | } |
| 486 | |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 487 | /* |
| 488 | * Copies (and truncates, if necessary) data from the larger struct, |
| 489 | * nand_ecclayout, to the smaller, deprecated layout struct, |
Brian Norris | 92394b5c | 2011-07-20 09:53:42 -0700 | [diff] [blame] | 490 | * nand_ecclayout_user. This is necessary only to support the deprecated |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 491 | * API ioctl ECCGETLAYOUT while allowing all new functionality to use |
| 492 | * nand_ecclayout flexibly (i.e. the struct may change size in new |
| 493 | * releases without requiring major rewrites). |
| 494 | */ |
| 495 | static int shrink_ecclayout(const struct nand_ecclayout *from, |
| 496 | struct nand_ecclayout_user *to) |
| 497 | { |
| 498 | int i; |
| 499 | |
| 500 | if (!from || !to) |
| 501 | return -EINVAL; |
| 502 | |
| 503 | memset(to, 0, sizeof(*to)); |
| 504 | |
Brian Norris | 0ceacf3 | 2010-09-19 23:57:12 -0700 | [diff] [blame] | 505 | to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES); |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 506 | for (i = 0; i < to->eccbytes; i++) |
| 507 | to->eccpos[i] = from->eccpos[i]; |
| 508 | |
| 509 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) { |
| 510 | if (from->oobfree[i].length == 0 && |
| 511 | from->oobfree[i].offset == 0) |
| 512 | break; |
| 513 | to->oobavail += from->oobfree[i].length; |
| 514 | to->oobfree[i] = from->oobfree[i]; |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 520 | static int mtdchar_blkpg_ioctl(struct mtd_info *mtd, |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 521 | struct blkpg_ioctl_arg __user *arg) |
| 522 | { |
| 523 | struct blkpg_ioctl_arg a; |
| 524 | struct blkpg_partition p; |
| 525 | |
| 526 | if (!capable(CAP_SYS_ADMIN)) |
| 527 | return -EPERM; |
| 528 | |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 529 | if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg))) |
| 530 | return -EFAULT; |
| 531 | |
| 532 | if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition))) |
| 533 | return -EFAULT; |
| 534 | |
| 535 | switch (a.op) { |
| 536 | case BLKPG_ADD_PARTITION: |
| 537 | |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 538 | /* Only master mtd device must be used to add partitions */ |
| 539 | if (mtd_is_partition(mtd)) |
| 540 | return -EINVAL; |
| 541 | |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 542 | return mtd_add_partition(mtd, p.devname, p.start, p.length); |
| 543 | |
| 544 | case BLKPG_DEL_PARTITION: |
| 545 | |
| 546 | if (p.pno < 0) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | return mtd_del_partition(mtd, p.pno); |
| 550 | |
| 551 | default: |
| 552 | return -EINVAL; |
| 553 | } |
| 554 | } |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 555 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 556 | static int mtdchar_write_ioctl(struct mtd_info *mtd, |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 557 | struct mtd_write_req __user *argp) |
| 558 | { |
| 559 | struct mtd_write_req req; |
| 560 | struct mtd_oob_ops ops; |
| 561 | void __user *usr_data, *usr_oob; |
| 562 | int ret; |
| 563 | |
| 564 | if (copy_from_user(&req, argp, sizeof(req)) || |
| 565 | !access_ok(VERIFY_READ, req.usr_data, req.len) || |
| 566 | !access_ok(VERIFY_READ, req.usr_oob, req.ooblen)) |
| 567 | return -EFAULT; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 568 | if (!mtd->_write_oob) |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 569 | return -EOPNOTSUPP; |
| 570 | |
| 571 | ops.mode = req.mode; |
| 572 | ops.len = (size_t)req.len; |
| 573 | ops.ooblen = (size_t)req.ooblen; |
| 574 | ops.ooboffs = 0; |
| 575 | |
| 576 | usr_data = (void __user *)(uintptr_t)req.usr_data; |
| 577 | usr_oob = (void __user *)(uintptr_t)req.usr_oob; |
| 578 | |
| 579 | if (req.usr_data) { |
| 580 | ops.datbuf = memdup_user(usr_data, ops.len); |
| 581 | if (IS_ERR(ops.datbuf)) |
| 582 | return PTR_ERR(ops.datbuf); |
| 583 | } else { |
| 584 | ops.datbuf = NULL; |
| 585 | } |
| 586 | |
| 587 | if (req.usr_oob) { |
| 588 | ops.oobbuf = memdup_user(usr_oob, ops.ooblen); |
| 589 | if (IS_ERR(ops.oobbuf)) { |
| 590 | kfree(ops.datbuf); |
| 591 | return PTR_ERR(ops.oobbuf); |
| 592 | } |
| 593 | } else { |
| 594 | ops.oobbuf = NULL; |
| 595 | } |
| 596 | |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 597 | ret = mtd_write_oob(mtd, (loff_t)req.start, &ops); |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 598 | |
| 599 | kfree(ops.datbuf); |
| 600 | kfree(ops.oobbuf); |
| 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 605 | static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 607 | struct mtd_file_info *mfi = file->private_data; |
| 608 | struct mtd_info *mtd = mfi->mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | void __user *argp = (void __user *)arg; |
| 610 | int ret = 0; |
| 611 | u_long size; |
Joern Engel | 73c619e | 2006-05-30 14:25:35 +0200 | [diff] [blame] | 612 | struct mtd_info_user info; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 613 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 614 | pr_debug("MTD_ioctl\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; |
| 617 | if (cmd & IOC_IN) { |
| 618 | if (!access_ok(VERIFY_READ, argp, size)) |
| 619 | return -EFAULT; |
| 620 | } |
| 621 | if (cmd & IOC_OUT) { |
| 622 | if (!access_ok(VERIFY_WRITE, argp, size)) |
| 623 | return -EFAULT; |
| 624 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | switch (cmd) { |
| 627 | case MEMGETREGIONCOUNT: |
| 628 | if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) |
| 629 | return -EFAULT; |
| 630 | break; |
| 631 | |
| 632 | case MEMGETREGIONINFO: |
| 633 | { |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 634 | uint32_t ur_idx; |
| 635 | struct mtd_erase_region_info *kr; |
H Hartley Sweeten | bcc98a4 | 2010-01-15 11:25:38 -0700 | [diff] [blame] | 636 | struct region_info_user __user *ur = argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 638 | if (get_user(ur_idx, &(ur->regionindex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | return -EFAULT; |
| 640 | |
Dan Carpenter | 5e59be1 | 2010-09-08 21:39:56 +0200 | [diff] [blame] | 641 | if (ur_idx >= mtd->numeraseregions) |
| 642 | return -EINVAL; |
| 643 | |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 644 | kr = &(mtd->eraseregions[ur_idx]); |
| 645 | |
| 646 | if (put_user(kr->offset, &(ur->offset)) |
| 647 | || put_user(kr->erasesize, &(ur->erasesize)) |
| 648 | || put_user(kr->numblocks, &(ur->numblocks))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return -EFAULT; |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | break; |
| 652 | } |
| 653 | |
| 654 | case MEMGETINFO: |
Vasiliy Kulikov | a0c5a39 | 2010-11-06 17:41:24 +0300 | [diff] [blame] | 655 | memset(&info, 0, sizeof(info)); |
Joern Engel | 73c619e | 2006-05-30 14:25:35 +0200 | [diff] [blame] | 656 | info.type = mtd->type; |
| 657 | info.flags = mtd->flags; |
| 658 | info.size = mtd->size; |
| 659 | info.erasesize = mtd->erasesize; |
| 660 | info.writesize = mtd->writesize; |
| 661 | info.oobsize = mtd->oobsize; |
Brian Norris | 19fb434 | 2011-08-30 18:45:46 -0700 | [diff] [blame] | 662 | /* The below field is obsolete */ |
| 663 | info.padding = 0; |
Joern Engel | 73c619e | 2006-05-30 14:25:35 +0200 | [diff] [blame] | 664 | if (copy_to_user(argp, &info, sizeof(struct mtd_info_user))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | return -EFAULT; |
| 666 | break; |
| 667 | |
| 668 | case MEMERASE: |
Kevin Cernekee | 0dc54e9 | 2009-04-08 22:52:28 -0700 | [diff] [blame] | 669 | case MEMERASE64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
| 671 | struct erase_info *erase; |
| 672 | |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 673 | if(!(file->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return -EPERM; |
| 675 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 676 | erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | if (!erase) |
| 678 | ret = -ENOMEM; |
| 679 | else { |
| 680 | wait_queue_head_t waitq; |
| 681 | DECLARE_WAITQUEUE(wait, current); |
| 682 | |
| 683 | init_waitqueue_head(&waitq); |
| 684 | |
Kevin Cernekee | 0dc54e9 | 2009-04-08 22:52:28 -0700 | [diff] [blame] | 685 | if (cmd == MEMERASE64) { |
| 686 | struct erase_info_user64 einfo64; |
| 687 | |
| 688 | if (copy_from_user(&einfo64, argp, |
| 689 | sizeof(struct erase_info_user64))) { |
| 690 | kfree(erase); |
| 691 | return -EFAULT; |
| 692 | } |
| 693 | erase->addr = einfo64.start; |
| 694 | erase->len = einfo64.length; |
| 695 | } else { |
| 696 | struct erase_info_user einfo32; |
| 697 | |
| 698 | if (copy_from_user(&einfo32, argp, |
| 699 | sizeof(struct erase_info_user))) { |
| 700 | kfree(erase); |
| 701 | return -EFAULT; |
| 702 | } |
| 703 | erase->addr = einfo32.start; |
| 704 | erase->len = einfo32.length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | erase->mtd = mtd; |
| 707 | erase->callback = mtdchar_erase_callback; |
| 708 | erase->priv = (unsigned long)&waitq; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | /* |
| 711 | FIXME: Allow INTERRUPTIBLE. Which means |
| 712 | not having the wait_queue head on the stack. |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | If the wq_head is on the stack, and we |
| 715 | leave because we got interrupted, then the |
| 716 | wq_head is no longer there when the |
| 717 | callback routine tries to wake us up. |
| 718 | */ |
Artem Bityutskiy | 7e1f0dc | 2011-12-23 15:25:39 +0200 | [diff] [blame] | 719 | ret = mtd_erase(mtd, erase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if (!ret) { |
| 721 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 722 | add_wait_queue(&waitq, &wait); |
| 723 | if (erase->state != MTD_ERASE_DONE && |
| 724 | erase->state != MTD_ERASE_FAILED) |
| 725 | schedule(); |
| 726 | remove_wait_queue(&waitq, &wait); |
| 727 | set_current_state(TASK_RUNNING); |
| 728 | |
| 729 | ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0; |
| 730 | } |
| 731 | kfree(erase); |
| 732 | } |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | case MEMWRITEOOB: |
| 737 | { |
| 738 | struct mtd_oob_buf buf; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 739 | struct mtd_oob_buf __user *buf_user = argp; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 740 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 741 | /* NOTE: writes return length to buf_user->length */ |
| 742 | if (copy_from_user(&buf, argp, sizeof(buf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | ret = -EFAULT; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 744 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 745 | ret = mtdchar_writeoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 746 | buf.ptr, &buf_user->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | case MEMREADOOB: |
| 751 | { |
| 752 | struct mtd_oob_buf buf; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 753 | struct mtd_oob_buf __user *buf_user = argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 755 | /* NOTE: writes return length to buf_user->start */ |
| 756 | if (copy_from_user(&buf, argp, sizeof(buf))) |
| 757 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 759 | ret = mtdchar_readoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 760 | buf.ptr, &buf_user->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | break; |
| 762 | } |
| 763 | |
Kevin Cernekee | aea7cea | 2009-04-08 22:53:49 -0700 | [diff] [blame] | 764 | case MEMWRITEOOB64: |
| 765 | { |
| 766 | struct mtd_oob_buf64 buf; |
| 767 | struct mtd_oob_buf64 __user *buf_user = argp; |
| 768 | |
| 769 | if (copy_from_user(&buf, argp, sizeof(buf))) |
| 770 | ret = -EFAULT; |
| 771 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 772 | ret = mtdchar_writeoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | aea7cea | 2009-04-08 22:53:49 -0700 | [diff] [blame] | 773 | (void __user *)(uintptr_t)buf.usr_ptr, |
| 774 | &buf_user->length); |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | case MEMREADOOB64: |
| 779 | { |
| 780 | struct mtd_oob_buf64 buf; |
| 781 | struct mtd_oob_buf64 __user *buf_user = argp; |
| 782 | |
| 783 | if (copy_from_user(&buf, argp, sizeof(buf))) |
| 784 | ret = -EFAULT; |
| 785 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 786 | ret = mtdchar_readoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | aea7cea | 2009-04-08 22:53:49 -0700 | [diff] [blame] | 787 | (void __user *)(uintptr_t)buf.usr_ptr, |
| 788 | &buf_user->length); |
| 789 | break; |
| 790 | } |
| 791 | |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 792 | case MEMWRITE: |
| 793 | { |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 794 | ret = mtdchar_write_ioctl(mtd, |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 795 | (struct mtd_write_req __user *)arg); |
| 796 | break; |
| 797 | } |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | case MEMLOCK: |
| 800 | { |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 801 | struct erase_info_user einfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 803 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | return -EFAULT; |
| 805 | |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 806 | ret = mtd_lock(mtd, einfo.start, einfo.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | break; |
| 808 | } |
| 809 | |
| 810 | case MEMUNLOCK: |
| 811 | { |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 812 | struct erase_info_user einfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 814 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | return -EFAULT; |
| 816 | |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 817 | ret = mtd_unlock(mtd, einfo.start, einfo.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 821 | case MEMISLOCKED: |
| 822 | { |
| 823 | struct erase_info_user einfo; |
| 824 | |
| 825 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
| 826 | return -EFAULT; |
| 827 | |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 828 | ret = mtd_is_locked(mtd, einfo.start, einfo.length); |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 829 | break; |
| 830 | } |
| 831 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 832 | /* Legacy interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | case MEMGETOOBSEL: |
| 834 | { |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 835 | struct nand_oobinfo oi; |
| 836 | |
| 837 | if (!mtd->ecclayout) |
| 838 | return -EOPNOTSUPP; |
| 839 | if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos)) |
| 840 | return -EINVAL; |
| 841 | |
| 842 | oi.useecc = MTD_NANDECC_AUTOPLACE; |
| 843 | memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos)); |
| 844 | memcpy(&oi.oobfree, mtd->ecclayout->oobfree, |
| 845 | sizeof(oi.oobfree)); |
Ricard Wanderlöf | d25ade7 | 2006-10-17 17:27:11 +0200 | [diff] [blame] | 846 | oi.eccbytes = mtd->ecclayout->eccbytes; |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 847 | |
| 848 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return -EFAULT; |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | case MEMGETBADBLOCK: |
| 854 | { |
| 855 | loff_t offs; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
| 858 | return -EFAULT; |
Artem Bityutskiy | 8f461a7 | 2012-01-02 13:48:54 +0200 | [diff] [blame] | 859 | return mtd_block_isbad(mtd, offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | |
| 863 | case MEMSETBADBLOCK: |
| 864 | { |
| 865 | loff_t offs; |
| 866 | |
| 867 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
| 868 | return -EFAULT; |
Artem Bityutskiy | 800ffd3 | 2012-01-02 13:59:12 +0200 | [diff] [blame] | 869 | return mtd_block_markbad(mtd, offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | break; |
| 871 | } |
| 872 | |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 873 | case OTPSELECT: |
| 874 | { |
| 875 | int mode; |
| 876 | if (copy_from_user(&mode, argp, sizeof(int))) |
| 877 | return -EFAULT; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 878 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 879 | mfi->mode = MTD_FILE_MODE_NORMAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 880 | |
| 881 | ret = otp_select_filemode(mfi, mode); |
| 882 | |
Nicolas Pitre | 81dba48 | 2005-04-01 16:36:15 +0100 | [diff] [blame] | 883 | file->f_pos = 0; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 884 | break; |
| 885 | } |
| 886 | |
| 887 | case OTPGETREGIONCOUNT: |
| 888 | case OTPGETREGIONINFO: |
| 889 | { |
| 890 | struct otp_info *buf = kmalloc(4096, GFP_KERNEL); |
| 891 | if (!buf) |
| 892 | return -ENOMEM; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 893 | switch (mfi->mode) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 894 | case MTD_FILE_MODE_OTP_FACTORY: |
Artem Bityutskiy | 87e858a9 | 2011-12-28 18:47:46 +0200 | [diff] [blame] | 895 | ret = mtd_get_fact_prot_info(mtd, buf, 4096); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 896 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 897 | case MTD_FILE_MODE_OTP_USER: |
Artem Bityutskiy | 87e858a9 | 2011-12-28 18:47:46 +0200 | [diff] [blame] | 898 | ret = mtd_get_user_prot_info(mtd, buf, 4096); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 899 | break; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 900 | default: |
Artem Bityutskiy | 87e858a9 | 2011-12-28 18:47:46 +0200 | [diff] [blame] | 901 | ret = -EINVAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 902 | break; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 903 | } |
| 904 | if (ret >= 0) { |
| 905 | if (cmd == OTPGETREGIONCOUNT) { |
| 906 | int nbr = ret / sizeof(struct otp_info); |
| 907 | ret = copy_to_user(argp, &nbr, sizeof(int)); |
| 908 | } else |
| 909 | ret = copy_to_user(argp, buf, ret); |
| 910 | if (ret) |
| 911 | ret = -EFAULT; |
| 912 | } |
| 913 | kfree(buf); |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | case OTPLOCK: |
| 918 | { |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 919 | struct otp_info oinfo; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 920 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 921 | if (mfi->mode != MTD_FILE_MODE_OTP_USER) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 922 | return -EINVAL; |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 923 | if (copy_from_user(&oinfo, argp, sizeof(oinfo))) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 924 | return -EFAULT; |
Artem Bityutskiy | 4403dbfb | 2011-12-23 18:55:49 +0200 | [diff] [blame] | 925 | ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 926 | break; |
| 927 | } |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 928 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 929 | /* This ioctl is being deprecated - it truncates the ECC layout */ |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 930 | case ECCGETLAYOUT: |
| 931 | { |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 932 | struct nand_ecclayout_user *usrlay; |
| 933 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 934 | if (!mtd->ecclayout) |
| 935 | return -EOPNOTSUPP; |
| 936 | |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 937 | usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL); |
| 938 | if (!usrlay) |
| 939 | return -ENOMEM; |
| 940 | |
| 941 | shrink_ecclayout(mtd->ecclayout, usrlay); |
| 942 | |
| 943 | if (copy_to_user(argp, usrlay, sizeof(*usrlay))) |
| 944 | ret = -EFAULT; |
| 945 | kfree(usrlay); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | |
| 949 | case ECCGETSTATS: |
| 950 | { |
| 951 | if (copy_to_user(argp, &mtd->ecc_stats, |
| 952 | sizeof(struct mtd_ecc_stats))) |
| 953 | return -EFAULT; |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | case MTDFILEMODE: |
| 958 | { |
| 959 | mfi->mode = 0; |
| 960 | |
| 961 | switch(arg) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 962 | case MTD_FILE_MODE_OTP_FACTORY: |
| 963 | case MTD_FILE_MODE_OTP_USER: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 964 | ret = otp_select_filemode(mfi, arg); |
| 965 | break; |
| 966 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 967 | case MTD_FILE_MODE_RAW: |
Artem Bityutskiy | fc002e3 | 2011-12-28 18:35:07 +0200 | [diff] [blame] | 968 | if (!mtd_has_oob(mtd)) |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 969 | return -EOPNOTSUPP; |
| 970 | mfi->mode = arg; |
| 971 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 972 | case MTD_FILE_MODE_NORMAL: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 973 | break; |
| 974 | default: |
| 975 | ret = -EINVAL; |
| 976 | } |
| 977 | file->f_pos = 0; |
| 978 | break; |
| 979 | } |
| 980 | |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 981 | case BLKPG: |
| 982 | { |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 983 | ret = mtdchar_blkpg_ioctl(mtd, |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 984 | (struct blkpg_ioctl_arg __user *)arg); |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | case BLKRRPART: |
| 989 | { |
| 990 | /* No reread partition feature. Just return ok */ |
| 991 | ret = 0; |
| 992 | break; |
| 993 | } |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | default: |
| 996 | ret = -ENOTTY; |
| 997 | } |
| 998 | |
| 999 | return ret; |
| 1000 | } /* memory_ioctl */ |
| 1001 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1002 | static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg) |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 1003 | { |
| 1004 | int ret; |
| 1005 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1006 | mutex_lock(&mtd_mutex); |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1007 | ret = mtdchar_ioctl(file, cmd, arg); |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1008 | mutex_unlock(&mtd_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1013 | #ifdef CONFIG_COMPAT |
| 1014 | |
| 1015 | struct mtd_oob_buf32 { |
| 1016 | u_int32_t start; |
| 1017 | u_int32_t length; |
| 1018 | compat_caddr_t ptr; /* unsigned char* */ |
| 1019 | }; |
| 1020 | |
| 1021 | #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32) |
| 1022 | #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32) |
| 1023 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1024 | static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1025 | unsigned long arg) |
| 1026 | { |
| 1027 | struct mtd_file_info *mfi = file->private_data; |
| 1028 | struct mtd_info *mtd = mfi->mtd; |
David Woodhouse | 0b6585c | 2009-05-29 16:09:08 +0100 | [diff] [blame] | 1029 | void __user *argp = compat_ptr(arg); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1030 | int ret = 0; |
| 1031 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1032 | mutex_lock(&mtd_mutex); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1033 | |
| 1034 | switch (cmd) { |
| 1035 | case MEMWRITEOOB32: |
| 1036 | { |
| 1037 | struct mtd_oob_buf32 buf; |
| 1038 | struct mtd_oob_buf32 __user *buf_user = argp; |
| 1039 | |
| 1040 | if (copy_from_user(&buf, argp, sizeof(buf))) |
| 1041 | ret = -EFAULT; |
| 1042 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1043 | ret = mtdchar_writeoob(file, mtd, buf.start, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1044 | buf.length, compat_ptr(buf.ptr), |
| 1045 | &buf_user->length); |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | case MEMREADOOB32: |
| 1050 | { |
| 1051 | struct mtd_oob_buf32 buf; |
| 1052 | struct mtd_oob_buf32 __user *buf_user = argp; |
| 1053 | |
| 1054 | /* NOTE: writes return length to buf->start */ |
| 1055 | if (copy_from_user(&buf, argp, sizeof(buf))) |
| 1056 | ret = -EFAULT; |
| 1057 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1058 | ret = mtdchar_readoob(file, mtd, buf.start, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1059 | buf.length, compat_ptr(buf.ptr), |
| 1060 | &buf_user->start); |
| 1061 | break; |
| 1062 | } |
| 1063 | default: |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1064 | ret = mtdchar_ioctl(file, cmd, (unsigned long)argp); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1067 | mutex_unlock(&mtd_mutex); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1068 | |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | #endif /* CONFIG_COMPAT */ |
| 1073 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1074 | /* |
| 1075 | * try to determine where a shared mapping can be made |
| 1076 | * - only supported for NOMMU at the moment (MMU can't doesn't copy private |
| 1077 | * mappings) |
| 1078 | */ |
| 1079 | #ifndef CONFIG_MMU |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1080 | static unsigned long mtdchar_get_unmapped_area(struct file *file, |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1081 | unsigned long addr, |
| 1082 | unsigned long len, |
| 1083 | unsigned long pgoff, |
| 1084 | unsigned long flags) |
| 1085 | { |
| 1086 | struct mtd_file_info *mfi = file->private_data; |
| 1087 | struct mtd_info *mtd = mfi->mtd; |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1088 | unsigned long offset; |
| 1089 | int ret; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1090 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1091 | if (addr != 0) |
| 1092 | return (unsigned long) -EINVAL; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1093 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1094 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) |
| 1095 | return (unsigned long) -EINVAL; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1096 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1097 | offset = pgoff << PAGE_SHIFT; |
| 1098 | if (offset > mtd->size - len) |
| 1099 | return (unsigned long) -EINVAL; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1100 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1101 | ret = mtd_get_unmapped_area(mtd, len, offset, flags); |
| 1102 | return ret == -EOPNOTSUPP ? -ENOSYS : ret; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1103 | } |
| 1104 | #endif |
| 1105 | |
| 1106 | /* |
| 1107 | * set up a mapping for shared memory segments |
| 1108 | */ |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1109 | static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma) |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1110 | { |
| 1111 | #ifdef CONFIG_MMU |
| 1112 | struct mtd_file_info *mfi = file->private_data; |
| 1113 | struct mtd_info *mtd = mfi->mtd; |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1114 | struct map_info *map = mtd->priv; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1115 | |
David Woodhouse | f5cf8f0 | 2012-10-09 15:08:10 +0100 | [diff] [blame] | 1116 | /* This is broken because it assumes the MTD device is map-based |
| 1117 | and that mtd->priv is a valid struct map_info. It should be |
| 1118 | replaced with something that uses the mtd_get_unmapped_area() |
| 1119 | operation properly. */ |
| 1120 | if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) { |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1121 | #ifdef pgprot_noncached |
Linus Torvalds | 8558e4a | 2013-04-19 09:53:07 -0700 | [diff] [blame] | 1122 | if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory)) |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1123 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 1124 | #endif |
Linus Torvalds | 8558e4a | 2013-04-19 09:53:07 -0700 | [diff] [blame] | 1125 | return vm_iomap_memory(vma, map->phys, map->size); |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1126 | } |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1127 | return -ENOSYS; |
| 1128 | #else |
| 1129 | return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS; |
| 1130 | #endif |
| 1131 | } |
| 1132 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1133 | static const struct file_operations mtd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | .owner = THIS_MODULE, |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1135 | .llseek = mtdchar_lseek, |
| 1136 | .read = mtdchar_read, |
| 1137 | .write = mtdchar_write, |
| 1138 | .unlocked_ioctl = mtdchar_unlocked_ioctl, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1139 | #ifdef CONFIG_COMPAT |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1140 | .compat_ioctl = mtdchar_compat_ioctl, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1141 | #endif |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1142 | .open = mtdchar_open, |
| 1143 | .release = mtdchar_close, |
| 1144 | .mmap = mtdchar_mmap, |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1145 | #ifndef CONFIG_MMU |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1146 | .get_unmapped_area = mtdchar_get_unmapped_area, |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1147 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | }; |
| 1149 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 1150 | static const struct super_operations mtd_ops = { |
| 1151 | .drop_inode = generic_delete_inode, |
| 1152 | .statfs = simple_statfs, |
| 1153 | }; |
| 1154 | |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1155 | static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type, |
| 1156 | int flags, const char *dev_name, void *data) |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1157 | { |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 1158 | return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | static struct file_system_type mtd_inodefs_type = { |
| 1162 | .name = "mtd_inodefs", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1163 | .mount = mtd_inodefs_mount, |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1164 | .kill_sb = kill_anon_super, |
| 1165 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 1166 | MODULE_ALIAS_FS("mtd_inodefs"); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1167 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1168 | int __init init_mtdchar(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | { |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1170 | int ret; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 1171 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1172 | ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, |
Ben Hutchings | dad0db3 | 2010-01-29 21:00:04 +0000 | [diff] [blame] | 1173 | "mtd", &mtd_fops); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1174 | if (ret < 0) { |
Artem Bityutskiy | 57ae2b6 | 2013-03-15 12:59:36 +0200 | [diff] [blame] | 1175 | pr_err("Can't allocate major number %d for MTD\n", |
| 1176 | MTD_CHAR_MAJOR); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1177 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1180 | ret = register_filesystem(&mtd_inodefs_type); |
| 1181 | if (ret) { |
Artem Bityutskiy | 57ae2b6 | 2013-03-15 12:59:36 +0200 | [diff] [blame] | 1182 | pr_err("Can't register mtd_inodefs filesystem, error %d\n", |
| 1183 | ret); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1184 | goto err_unregister_chdev; |
| 1185 | } |
Artem Bityutskiy | 57ae2b6 | 2013-03-15 12:59:36 +0200 | [diff] [blame] | 1186 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1187 | return ret; |
| 1188 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1189 | err_unregister_chdev: |
| 1190 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); |
| 1191 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Artem Bityutskiy | 660685d | 2013-03-14 13:27:40 +0200 | [diff] [blame] | 1194 | void __exit cleanup_mtdchar(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1196 | unregister_filesystem(&mtd_inodefs_type); |
Ben Hutchings | dad0db3 | 2010-01-29 21:00:04 +0000 | [diff] [blame] | 1197 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Scott James Remnant | 90160e1 | 2009-03-02 18:42:39 +0000 | [diff] [blame] | 1200 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |