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> |
Murali Palnati | 3e8c3bf | 2012-02-15 16:51:17 +0530 | [diff] [blame] | 38 | #include <linux/mtd/partitions.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 40 | #include <asm/uaccess.h> |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 41 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 42 | static DEFINE_MUTEX(mtd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 44 | /* |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 45 | * Data structure to hold the pointer to the mtd device as well |
Brian Norris | 92394b5 | 2011-07-20 09:53:42 -0700 | [diff] [blame] | 46 | * as mode information of various use cases. |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 47 | */ |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 48 | struct mtd_file_info { |
| 49 | struct mtd_info *mtd; |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 50 | struct inode *ino; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 51 | enum mtd_file_modes mode; |
| 52 | }; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 53 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 54 | 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] | 55 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 56 | struct mtd_file_info *mfi = file->private_data; |
| 57 | struct mtd_info *mtd = mfi->mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | switch (orig) { |
Josef 'Jeff' Sipek | ea59830 | 2006-09-16 21:09:29 -0400 | [diff] [blame] | 60 | case SEEK_SET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | break; |
Josef 'Jeff' Sipek | ea59830 | 2006-09-16 21:09:29 -0400 | [diff] [blame] | 62 | case SEEK_CUR: |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 63 | offset += file->f_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | break; |
Josef 'Jeff' Sipek | ea59830 | 2006-09-16 21:09:29 -0400 | [diff] [blame] | 65 | case SEEK_END: |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 66 | offset += mtd->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | break; |
| 68 | default: |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
Herbert Valerio Riedel | 1887f51 | 2006-06-24 00:03:36 +0200 | [diff] [blame] | 72 | if (offset >= 0 && offset <= mtd->size) |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 73 | return file->f_pos = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 75 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 78 | static int count; |
| 79 | static struct vfsmount *mnt; |
| 80 | static struct file_system_type mtd_inodefs_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 82 | static int mtdchar_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
| 84 | int minor = iminor(inode); |
| 85 | int devnum = minor >> 1; |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 86 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct mtd_info *mtd; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 88 | struct mtd_file_info *mfi; |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 89 | struct inode *mtd_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 91 | pr_debug("MTD_open\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* You can't open the RO devices RW */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 94 | if ((file->f_mode & FMODE_WRITE) && (minor & 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return -EACCES; |
| 96 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 97 | ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 101 | mutex_lock(&mtd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | mtd = get_mtd_device(NULL, devnum); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 103 | |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 104 | if (IS_ERR(mtd)) { |
| 105 | ret = PTR_ERR(mtd); |
| 106 | goto out; |
| 107 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 108 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 109 | if (mtd->type == MTD_ABSENT) { |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 110 | ret = -ENODEV; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 111 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 114 | mtd_ino = iget_locked(mnt->mnt_sb, devnum); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 115 | if (!mtd_ino) { |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 116 | ret = -ENOMEM; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 117 | goto out1; |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 118 | } |
| 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 Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* 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] | 128 | if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) { |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 129 | ret = -EACCES; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 130 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 132 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 133 | mfi = kzalloc(sizeof(*mfi), GFP_KERNEL); |
| 134 | if (!mfi) { |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 135 | ret = -ENOMEM; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 136 | goto out2; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 137 | } |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 138 | mfi->ino = mtd_ino; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 139 | mfi->mtd = mtd; |
| 140 | file->private_data = mfi; |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 141 | mutex_unlock(&mtd_mutex); |
| 142 | return 0; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 143 | |
Al Viro | c65390f | 2012-04-09 01:36:28 -0400 | [diff] [blame] | 144 | out2: |
| 145 | iput(mtd_ino); |
| 146 | out1: |
| 147 | put_mtd_device(mtd); |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 148 | out: |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 149 | mutex_unlock(&mtd_mutex); |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 150 | simple_release_fs(&mnt, &count); |
Jonathan Corbet | 6071239 | 2008-05-15 10:10:37 -0600 | [diff] [blame] | 151 | return ret; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 152 | } /* mtdchar_open */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /*====================================================================*/ |
| 155 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 156 | static int mtdchar_close(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 158 | struct mtd_file_info *mfi = file->private_data; |
| 159 | struct mtd_info *mtd = mfi->mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 161 | pr_debug("MTD_close\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Joakim Tjernlund | 7eafaed | 2007-06-27 00:56:40 +0200 | [diff] [blame] | 163 | /* Only sync if opened RW */ |
Artem Bityutskiy | 327cf29 | 2011-12-30 16:35:35 +0200 | [diff] [blame] | 164 | if ((file->f_mode & FMODE_WRITE)) |
Artem Bityutskiy | 85f2f2a | 2011-12-23 19:03:12 +0200 | [diff] [blame] | 165 | mtd_sync(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 166 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 167 | iput(mfi->ino); |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | put_mtd_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 170 | file->private_data = NULL; |
| 171 | kfree(mfi); |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 172 | simple_release_fs(&mnt, &count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | return 0; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 175 | } /* mtdchar_close */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 177 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 195 | static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, |
| 196 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 198 | struct mtd_file_info *mfi = file->private_data; |
| 199 | struct mtd_info *mtd = mfi->mtd; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 200 | size_t retlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | size_t total_retlen=0; |
| 202 | int ret=0; |
| 203 | int len; |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 204 | size_t size = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | char *kbuf; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 206 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 207 | pr_debug("MTD_read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (*ppos + count > mtd->size) |
| 210 | count = mtd->size - *ppos; |
| 211 | |
| 212 | if (!count) |
| 213 | return 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 214 | |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 215 | kbuf = mtd_kmalloc_up_to(mtd, &size); |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 216 | if (!kbuf) |
| 217 | return -ENOMEM; |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | while (count) { |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 220 | len = min_t(size_t, count, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 222 | switch (mfi->mode) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 223 | case MTD_FILE_MODE_OTP_FACTORY: |
Artem Bityutskiy | d264f72 | 2011-12-23 18:40:06 +0200 | [diff] [blame] | 224 | ret = mtd_read_fact_prot_reg(mtd, *ppos, len, |
| 225 | &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 226 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 227 | case MTD_FILE_MODE_OTP_USER: |
Artem Bityutskiy | 4ea1cab | 2011-12-23 18:47:59 +0200 | [diff] [blame] | 228 | ret = mtd_read_user_prot_reg(mtd, *ppos, len, |
| 229 | &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 230 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 231 | case MTD_FILE_MODE_RAW: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 232 | { |
| 233 | struct mtd_oob_ops ops; |
| 234 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 235 | ops.mode = MTD_OPS_RAW; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 236 | ops.datbuf = kbuf; |
| 237 | ops.oobbuf = NULL; |
| 238 | ops.len = len; |
| 239 | |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 240 | ret = mtd_read_oob(mtd, *ppos, &ops); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 241 | retlen = ops.retlen; |
| 242 | break; |
| 243 | } |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 244 | default: |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 245 | ret = mtd_read(mtd, *ppos, len, &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 246 | } |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 247 | /* Nand returns -EBADMSG on ECC errors, but it returns |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | * the data. For our userspace tools it is important |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 249 | * to dump areas with ECC errors! |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 250 | * For kernel internal usage it also might return -EUCLEAN |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 251 | * to signal the caller that a bitflip has occurred and has |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 252 | * been corrected by the ECC algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * Userspace software which accesses NAND this way |
| 254 | * must be aware of the fact that it deals with NAND |
| 255 | */ |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 256 | if (!ret || mtd_is_bitflip_or_eccerr(ret)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | *ppos += retlen; |
| 258 | if (copy_to_user(buf, kbuf, retlen)) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 259 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return -EFAULT; |
| 261 | } |
| 262 | else |
| 263 | total_retlen += retlen; |
| 264 | |
| 265 | count -= retlen; |
| 266 | buf += retlen; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 267 | if (retlen == 0) |
| 268 | count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | else { |
| 271 | kfree(kbuf); |
| 272 | return ret; |
| 273 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 277 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return total_retlen; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 279 | } /* mtdchar_read */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 281 | static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count, |
| 282 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 284 | struct mtd_file_info *mfi = file->private_data; |
| 285 | struct mtd_info *mtd = mfi->mtd; |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 286 | size_t size = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | char *kbuf; |
| 288 | size_t retlen; |
| 289 | size_t total_retlen=0; |
| 290 | int ret=0; |
| 291 | int len; |
| 292 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 293 | pr_debug("MTD_write\n"); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (*ppos == mtd->size) |
| 296 | return -ENOSPC; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (*ppos + count > mtd->size) |
| 299 | count = mtd->size - *ppos; |
| 300 | |
| 301 | if (!count) |
| 302 | return 0; |
| 303 | |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 304 | kbuf = mtd_kmalloc_up_to(mtd, &size); |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 305 | if (!kbuf) |
| 306 | return -ENOMEM; |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | while (count) { |
Grant Erickson | 3e45cf5 | 2011-04-08 08:51:33 -0700 | [diff] [blame] | 309 | len = min_t(size_t, count, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (copy_from_user(kbuf, buf, len)) { |
| 312 | kfree(kbuf); |
| 313 | return -EFAULT; |
| 314 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 315 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 316 | switch (mfi->mode) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 317 | case MTD_FILE_MODE_OTP_FACTORY: |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 318 | ret = -EROFS; |
| 319 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 320 | case MTD_FILE_MODE_OTP_USER: |
Artem Bityutskiy | 482b43a | 2011-12-23 18:50:04 +0200 | [diff] [blame] | 321 | ret = mtd_write_user_prot_reg(mtd, *ppos, len, |
| 322 | &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 323 | break; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 324 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 325 | case MTD_FILE_MODE_RAW: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 326 | { |
| 327 | struct mtd_oob_ops ops; |
| 328 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 329 | ops.mode = MTD_OPS_RAW; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 330 | ops.datbuf = kbuf; |
| 331 | ops.oobbuf = NULL; |
Peter Wippich | bf51408 | 2011-06-06 15:50:58 +0200 | [diff] [blame] | 332 | ops.ooboffs = 0; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 333 | ops.len = len; |
| 334 | |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 335 | ret = mtd_write_oob(mtd, *ppos, &ops); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 336 | retlen = ops.retlen; |
| 337 | break; |
| 338 | } |
| 339 | |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 340 | default: |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 341 | ret = mtd_write(mtd, *ppos, len, &retlen, kbuf); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 355 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return total_retlen; |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 357 | } /* mtdchar_write */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | /*====================================================================== |
| 360 | |
| 361 | IOCTL calls for getting device parameters. |
| 362 | |
| 363 | ======================================================================*/ |
| 364 | static void mtdchar_erase_callback (struct erase_info *instr) |
| 365 | { |
| 366 | wake_up((wait_queue_head_t *)instr->priv); |
| 367 | } |
| 368 | |
David Brownell | 34a8244 | 2008-07-30 12:35:05 -0700 | [diff] [blame] | 369 | #ifdef CONFIG_HAVE_MTD_OTP |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 370 | static int otp_select_filemode(struct mtd_file_info *mfi, int mode) |
| 371 | { |
| 372 | struct mtd_info *mtd = mfi->mtd; |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 373 | size_t retlen; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 374 | int ret = 0; |
| 375 | |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 376 | /* |
| 377 | * Make a fake call to mtd_read_fact_prot_reg() to check if OTP |
| 378 | * operations are supported. |
| 379 | */ |
Will Newton | 7a84477 | 2012-03-30 11:51:02 +0100 | [diff] [blame] | 380 | if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == -EOPNOTSUPP) |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 381 | return -EOPNOTSUPP; |
| 382 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 383 | switch (mode) { |
| 384 | case MTD_OTP_FACTORY: |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 385 | mfi->mode = MTD_FILE_MODE_OTP_FACTORY; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 386 | break; |
| 387 | case MTD_OTP_USER: |
Artem Bityutskiy | b6de3d6 | 2011-12-29 10:06:32 +0200 | [diff] [blame] | 388 | mfi->mode = MTD_FILE_MODE_OTP_USER; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 389 | 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 401 | static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 402 | uint64_t start, uint32_t length, void __user *ptr, |
| 403 | uint32_t __user *retp) |
| 404 | { |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 405 | struct mtd_file_info *mfi = file->private_data; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 406 | 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 Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 416 | if (!mtd->_write_oob) |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 417 | ret = -EOPNOTSUPP; |
| 418 | else |
Roel Kluin | 0040476 | 2010-01-29 10:35:04 +0100 | [diff] [blame] | 419 | ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 420 | |
| 421 | if (ret) |
| 422 | return ret; |
| 423 | |
| 424 | ops.ooblen = length; |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 425 | ops.ooboffs = start & (mtd->writesize - 1); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 426 | ops.datbuf = NULL; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 427 | ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW : |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 428 | MTD_OPS_PLACE_OOB; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 429 | |
| 430 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) |
| 431 | return -EINVAL; |
| 432 | |
Julia Lawall | df1f1d1 | 2010-05-22 10:22:49 +0200 | [diff] [blame] | 433 | ops.oobbuf = memdup_user(ptr, length); |
| 434 | if (IS_ERR(ops.oobbuf)) |
| 435 | return PTR_ERR(ops.oobbuf); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 436 | |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 437 | start &= ~((uint64_t)mtd->writesize - 1); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 438 | ret = mtd_write_oob(mtd, start, &ops); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 439 | |
| 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 450 | static int mtdchar_readoob(struct file *file, struct mtd_info *mtd, |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 451 | uint64_t start, uint32_t length, void __user *ptr, |
| 452 | uint32_t __user *retp) |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 453 | { |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 454 | struct mtd_file_info *mfi = file->private_data; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 455 | struct mtd_oob_ops ops; |
| 456 | int ret = 0; |
| 457 | |
| 458 | if (length > 4096) |
| 459 | return -EINVAL; |
| 460 | |
Artem Bityutskiy | dac2639 | 2011-12-28 17:50:34 +0200 | [diff] [blame] | 461 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 462 | return -EFAULT; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 463 | |
| 464 | ops.ooblen = length; |
Brian Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 465 | ops.ooboffs = start & (mtd->writesize - 1); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 466 | ops.datbuf = NULL; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 467 | ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW : |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 468 | MTD_OPS_PLACE_OOB; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 469 | |
| 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 Norris | 305b93f | 2011-08-23 17:17:32 -0700 | [diff] [blame] | 477 | start &= ~((uint64_t)mtd->writesize - 1); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 478 | ret = mtd_read_oob(mtd, start, &ops); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 479 | |
| 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 Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 487 | |
| 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 Norris | c478d7e | 2011-06-28 16:29:00 -0700 | [diff] [blame] | 496 | * 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 Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 499 | */ |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 500 | if (mtd_is_bitflip_or_eccerr(ret)) |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 501 | return 0; |
| 502 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 503 | return ret; |
| 504 | } |
| 505 | |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 506 | /* |
| 507 | * Copies (and truncates, if necessary) data from the larger struct, |
| 508 | * nand_ecclayout, to the smaller, deprecated layout struct, |
Brian Norris | 92394b5 | 2011-07-20 09:53:42 -0700 | [diff] [blame] | 509 | * nand_ecclayout_user. This is necessary only to support the deprecated |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 510 | * 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 | */ |
| 514 | static 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 Norris | 0ceacf3 | 2010-09-19 23:57:12 -0700 | [diff] [blame] | 524 | to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES); |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 525 | 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 539 | static int mtdchar_blkpg_ioctl(struct mtd_info *mtd, |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 540 | 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 Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 548 | 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 Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 557 | /* Only master mtd device must be used to add partitions */ |
| 558 | if (mtd_is_partition(mtd)) |
| 559 | return -EINVAL; |
| 560 | |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 561 | 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 Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 574 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 575 | static int mtdchar_write_ioctl(struct mtd_info *mtd, |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 576 | 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 Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 587 | if (!mtd->_write_oob) |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 588 | 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 Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 616 | ret = mtd_write_oob(mtd, (loff_t)req.start, &ops); |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 617 | |
| 618 | kfree(ops.datbuf); |
| 619 | kfree(ops.oobbuf); |
| 620 | |
| 621 | return ret; |
| 622 | } |
| 623 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 624 | 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] | 625 | { |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 626 | struct mtd_file_info *mfi = file->private_data; |
| 627 | struct mtd_info *mtd = mfi->mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | void __user *argp = (void __user *)arg; |
| 629 | int ret = 0; |
| 630 | u_long size; |
Joern Engel | 73c619e | 2006-05-30 14:25:35 +0200 | [diff] [blame] | 631 | struct mtd_info_user info; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 632 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 633 | pr_debug("MTD_ioctl\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 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 Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | 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 Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 653 | uint32_t ur_idx; |
| 654 | struct mtd_erase_region_info *kr; |
H Hartley Sweeten | bcc98a4 | 2010-01-15 11:25:38 -0700 | [diff] [blame] | 655 | struct region_info_user __user *ur = argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 657 | if (get_user(ur_idx, &(ur->regionindex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return -EFAULT; |
| 659 | |
Dan Carpenter | 5e59be1 | 2010-09-08 21:39:56 +0200 | [diff] [blame] | 660 | if (ur_idx >= mtd->numeraseregions) |
| 661 | return -EINVAL; |
| 662 | |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 663 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return -EFAULT; |
Zev Weiss | b67c5f8 | 2008-09-01 05:02:12 -0700 | [diff] [blame] | 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | break; |
| 671 | } |
| 672 | |
| 673 | case MEMGETINFO: |
Vasiliy Kulikov | a0c5a39 | 2010-11-06 17:41:24 +0300 | [diff] [blame] | 674 | memset(&info, 0, sizeof(info)); |
Joern Engel | 73c619e | 2006-05-30 14:25:35 +0200 | [diff] [blame] | 675 | 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 Norris | 19fb434 | 2011-08-30 18:45:46 -0700 | [diff] [blame] | 681 | /* The below field is obsolete */ |
| 682 | info.padding = 0; |
Joern Engel | 73c619e | 2006-05-30 14:25:35 +0200 | [diff] [blame] | 683 | if (copy_to_user(argp, &info, sizeof(struct mtd_info_user))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return -EFAULT; |
| 685 | break; |
| 686 | |
| 687 | case MEMERASE: |
Kevin Cernekee | 0dc54e9 | 2009-04-08 22:52:28 -0700 | [diff] [blame] | 688 | case MEMERASE64: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
| 690 | struct erase_info *erase; |
| 691 | |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 692 | if(!(file->f_mode & FMODE_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | return -EPERM; |
| 694 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 695 | erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | 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 Cernekee | 0dc54e9 | 2009-04-08 22:52:28 -0700 | [diff] [blame] | 704 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
| 725 | erase->mtd = mtd; |
| 726 | erase->callback = mtdchar_erase_callback; |
| 727 | erase->priv = (unsigned long)&waitq; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* |
| 730 | FIXME: Allow INTERRUPTIBLE. Which means |
| 731 | not having the wait_queue head on the stack. |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | 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 Bityutskiy | 7e1f0dc | 2011-12-23 15:25:39 +0200 | [diff] [blame] | 738 | ret = mtd_erase(mtd, erase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | 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 Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 758 | struct mtd_oob_buf __user *buf_user = argp; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 759 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 760 | /* NOTE: writes return length to buf_user->length */ |
| 761 | if (copy_from_user(&buf, argp, sizeof(buf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | ret = -EFAULT; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 763 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 764 | ret = mtdchar_writeoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 765 | buf.ptr, &buf_user->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | case MEMREADOOB: |
| 770 | { |
| 771 | struct mtd_oob_buf buf; |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 772 | struct mtd_oob_buf __user *buf_user = argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 774 | /* NOTE: writes return length to buf_user->start */ |
| 775 | if (copy_from_user(&buf, argp, sizeof(buf))) |
| 776 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | else |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 778 | ret = mtdchar_readoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 779 | buf.ptr, &buf_user->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | break; |
| 781 | } |
| 782 | |
Kevin Cernekee | aea7cea | 2009-04-08 22:53:49 -0700 | [diff] [blame] | 783 | 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 791 | ret = mtdchar_writeoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | aea7cea | 2009-04-08 22:53:49 -0700 | [diff] [blame] | 792 | (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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 805 | ret = mtdchar_readoob(file, mtd, buf.start, buf.length, |
Kevin Cernekee | aea7cea | 2009-04-08 22:53:49 -0700 | [diff] [blame] | 806 | (void __user *)(uintptr_t)buf.usr_ptr, |
| 807 | &buf_user->length); |
| 808 | break; |
| 809 | } |
| 810 | |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 811 | case MEMWRITE: |
| 812 | { |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 813 | ret = mtdchar_write_ioctl(mtd, |
Brian Norris | e99d8b0 | 2011-09-09 09:59:03 -0700 | [diff] [blame] | 814 | (struct mtd_write_req __user *)arg); |
| 815 | break; |
| 816 | } |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | case MEMLOCK: |
| 819 | { |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 820 | struct erase_info_user einfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 822 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return -EFAULT; |
| 824 | |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 825 | ret = mtd_lock(mtd, einfo.start, einfo.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | |
| 829 | case MEMUNLOCK: |
| 830 | { |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 831 | struct erase_info_user einfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 833 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return -EFAULT; |
| 835 | |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 836 | ret = mtd_unlock(mtd, einfo.start, einfo.length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 840 | case MEMISLOCKED: |
| 841 | { |
| 842 | struct erase_info_user einfo; |
| 843 | |
| 844 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
| 845 | return -EFAULT; |
| 846 | |
Artem Bityutskiy | 3813456 | 2011-12-30 17:00:35 +0200 | [diff] [blame] | 847 | ret = mtd_is_locked(mtd, einfo.start, einfo.length); |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 851 | /* Legacy interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | case MEMGETOOBSEL: |
| 853 | { |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 854 | 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öf | d25ade7 | 2006-10-17 17:27:11 +0200 | [diff] [blame] | 865 | oi.eccbytes = mtd->ecclayout->eccbytes; |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 866 | |
| 867 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | return -EFAULT; |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | case MEMGETBADBLOCK: |
| 873 | { |
| 874 | loff_t offs; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
| 877 | return -EFAULT; |
Artem Bityutskiy | 8f461a7 | 2012-01-02 13:48:54 +0200 | [diff] [blame] | 878 | return mtd_block_isbad(mtd, offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | 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 Bityutskiy | 800ffd3 | 2012-01-02 13:59:12 +0200 | [diff] [blame] | 888 | return mtd_block_markbad(mtd, offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | break; |
| 890 | } |
| 891 | |
David Brownell | 34a8244 | 2008-07-30 12:35:05 -0700 | [diff] [blame] | 892 | #ifdef CONFIG_HAVE_MTD_OTP |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 893 | case OTPSELECT: |
| 894 | { |
| 895 | int mode; |
| 896 | if (copy_from_user(&mode, argp, sizeof(int))) |
| 897 | return -EFAULT; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 898 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 899 | mfi->mode = MTD_FILE_MODE_NORMAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 900 | |
| 901 | ret = otp_select_filemode(mfi, mode); |
| 902 | |
Nicolas Pitre | 81dba48 | 2005-04-01 16:36:15 +0100 | [diff] [blame] | 903 | file->f_pos = 0; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 904 | 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 Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 913 | switch (mfi->mode) { |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 914 | case MTD_FILE_MODE_OTP_FACTORY: |
Artem Bityutskiy | 87e858a9 | 2011-12-28 18:47:46 +0200 | [diff] [blame] | 915 | ret = mtd_get_fact_prot_info(mtd, buf, 4096); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 916 | break; |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 917 | case MTD_FILE_MODE_OTP_USER: |
Artem Bityutskiy | 87e858a9 | 2011-12-28 18:47:46 +0200 | [diff] [blame] | 918 | ret = mtd_get_user_prot_info(mtd, buf, 4096); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 919 | break; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 920 | default: |
Artem Bityutskiy | 87e858a9 | 2011-12-28 18:47:46 +0200 | [diff] [blame] | 921 | ret = -EINVAL; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 922 | break; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 923 | } |
| 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 Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 939 | struct otp_info oinfo; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 940 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 941 | if (mfi->mode != MTD_FILE_MODE_OTP_USER) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 942 | return -EINVAL; |
Harvey Harrison | 175428b | 2008-07-03 23:40:14 -0700 | [diff] [blame] | 943 | if (copy_from_user(&oinfo, argp, sizeof(oinfo))) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 944 | return -EFAULT; |
Artem Bityutskiy | 4403dbf | 2011-12-23 18:55:49 +0200 | [diff] [blame] | 945 | ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | #endif |
| 949 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 950 | /* This ioctl is being deprecated - it truncates the ECC layout */ |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 951 | case ECCGETLAYOUT: |
| 952 | { |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 953 | struct nand_ecclayout_user *usrlay; |
| 954 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 955 | if (!mtd->ecclayout) |
| 956 | return -EOPNOTSUPP; |
| 957 | |
Brian Norris | cc26c3c | 2010-08-24 18:12:00 -0700 | [diff] [blame] | 958 | 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 Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 967 | break; |
| 968 | } |
| 969 | |
| 970 | case ECCGETSTATS: |
| 971 | { |
Murali Palnati | 3e8c3bf | 2012-02-15 16:51:17 +0530 | [diff] [blame] | 972 | #ifdef CONFIG_MTD_LAZYECCSTATS |
| 973 | part_fill_badblockstats(mtd); |
| 974 | #endif |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 975 | 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 Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 986 | case MTD_FILE_MODE_OTP_FACTORY: |
| 987 | case MTD_FILE_MODE_OTP_USER: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 988 | ret = otp_select_filemode(mfi, arg); |
| 989 | break; |
| 990 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 991 | case MTD_FILE_MODE_RAW: |
Artem Bityutskiy | fc002e3 | 2011-12-28 18:35:07 +0200 | [diff] [blame] | 992 | if (!mtd_has_oob(mtd)) |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 993 | return -EOPNOTSUPP; |
| 994 | mfi->mode = arg; |
| 995 | |
Brian Norris | beb133f | 2011-08-30 18:45:41 -0700 | [diff] [blame] | 996 | case MTD_FILE_MODE_NORMAL: |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 997 | break; |
| 998 | default: |
| 999 | ret = -EINVAL; |
| 1000 | } |
| 1001 | file->f_pos = 0; |
| 1002 | break; |
| 1003 | } |
| 1004 | |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 1005 | case BLKPG: |
| 1006 | { |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1007 | ret = mtdchar_blkpg_ioctl(mtd, |
Roman Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 1008 | (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 Tereshonkov | d0f7959 | 2010-09-17 13:31:42 +0300 | [diff] [blame] | 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | default: |
| 1020 | ret = -ENOTTY; |
| 1021 | } |
| 1022 | |
| 1023 | return ret; |
| 1024 | } /* memory_ioctl */ |
| 1025 | |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1026 | 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] | 1027 | { |
| 1028 | int ret; |
| 1029 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1030 | mutex_lock(&mtd_mutex); |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1031 | ret = mtdchar_ioctl(file, cmd, arg); |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1032 | mutex_unlock(&mtd_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 1033 | |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1037 | #ifdef CONFIG_COMPAT |
| 1038 | |
| 1039 | struct 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1048 | static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1049 | unsigned long arg) |
| 1050 | { |
| 1051 | struct mtd_file_info *mfi = file->private_data; |
| 1052 | struct mtd_info *mtd = mfi->mtd; |
David Woodhouse | 0b6585c | 2009-05-29 16:09:08 +0100 | [diff] [blame] | 1053 | void __user *argp = compat_ptr(arg); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1054 | int ret = 0; |
| 1055 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1056 | mutex_lock(&mtd_mutex); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1057 | |
| 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1067 | ret = mtdchar_writeoob(file, mtd, buf.start, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1068 | 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1082 | ret = mtdchar_readoob(file, mtd, buf.start, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1083 | buf.length, compat_ptr(buf.ptr), |
| 1084 | &buf_user->start); |
| 1085 | break; |
| 1086 | } |
| 1087 | default: |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1088 | ret = mtdchar_ioctl(file, cmd, (unsigned long)argp); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Arnd Bergmann | 5aa8294 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1091 | mutex_unlock(&mtd_mutex); |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1092 | |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | #endif /* CONFIG_COMPAT */ |
| 1097 | |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1098 | /* |
| 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 Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1104 | static unsigned long mtdchar_get_unmapped_area(struct file *file, |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1105 | 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 Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1112 | unsigned long offset; |
| 1113 | int ret; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1114 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1115 | if (addr != 0) |
| 1116 | return (unsigned long) -EINVAL; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1117 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1118 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) |
| 1119 | return (unsigned long) -EINVAL; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1120 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1121 | offset = pgoff << PAGE_SHIFT; |
| 1122 | if (offset > mtd->size - len) |
| 1123 | return (unsigned long) -EINVAL; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1124 | |
Artem Bityutskiy | cd62127 | 2011-12-30 14:31:57 +0200 | [diff] [blame] | 1125 | ret = mtd_get_unmapped_area(mtd, len, offset, flags); |
| 1126 | return ret == -EOPNOTSUPP ? -ENOSYS : ret; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1127 | } |
| 1128 | #endif |
| 1129 | |
Linus Torvalds | 3f0e06c | 2012-09-08 12:57:30 -0700 | [diff] [blame] | 1130 | static inline unsigned long get_vm_size(struct vm_area_struct *vma) |
| 1131 | { |
| 1132 | return vma->vm_end - vma->vm_start; |
| 1133 | } |
| 1134 | |
| 1135 | static 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 | */ |
| 1146 | static 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 Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1157 | /* |
| 1158 | * set up a mapping for shared memory segments |
| 1159 | */ |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1160 | static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma) |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1161 | { |
| 1162 | #ifdef CONFIG_MMU |
| 1163 | struct mtd_file_info *mfi = file->private_data; |
| 1164 | struct mtd_info *mtd = mfi->mtd; |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1165 | struct map_info *map = mtd->priv; |
Linus Torvalds | 3f0e06c | 2012-09-08 12:57:30 -0700 | [diff] [blame] | 1166 | resource_size_t start, off; |
| 1167 | unsigned long len, vma_len; |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1168 | |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1169 | if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) { |
Linus Torvalds | 3f0e06c | 2012-09-08 12:57:30 -0700 | [diff] [blame] | 1170 | off = get_vm_offset(vma); |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1171 | start = map->phys; |
| 1172 | len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size); |
| 1173 | start &= PAGE_MASK; |
Linus Torvalds | 3f0e06c | 2012-09-08 12:57:30 -0700 | [diff] [blame] | 1174 | 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 Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1181 | return -EINVAL; |
| 1182 | |
| 1183 | off += start; |
Linus Torvalds | 3f0e06c | 2012-09-08 12:57:30 -0700 | [diff] [blame] | 1184 | /* Did that overflow? */ |
| 1185 | if (off < start) |
| 1186 | return -EINVAL; |
| 1187 | if (set_vm_offset(vma, off) < 0) |
| 1188 | return -EINVAL; |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1189 | 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 Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1200 | return 0; |
Anatolij Gustschin | dd02b67 | 2010-06-15 09:30:15 +0200 | [diff] [blame] | 1201 | } |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1202 | return -ENOSYS; |
| 1203 | #else |
| 1204 | return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS; |
| 1205 | #endif |
| 1206 | } |
| 1207 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1208 | static const struct file_operations mtd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | .owner = THIS_MODULE, |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1210 | .llseek = mtdchar_lseek, |
| 1211 | .read = mtdchar_read, |
| 1212 | .write = mtdchar_write, |
| 1213 | .unlocked_ioctl = mtdchar_unlocked_ioctl, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1214 | #ifdef CONFIG_COMPAT |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1215 | .compat_ioctl = mtdchar_compat_ioctl, |
Kevin Cernekee | 9771854 | 2009-04-08 22:53:13 -0700 | [diff] [blame] | 1216 | #endif |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1217 | .open = mtdchar_open, |
| 1218 | .release = mtdchar_close, |
| 1219 | .mmap = mtdchar_mmap, |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1220 | #ifndef CONFIG_MMU |
Artem Bityutskiy | 969e57a | 2011-12-23 17:27:46 +0200 | [diff] [blame] | 1221 | .get_unmapped_area = mtdchar_get_unmapped_area, |
David Howells | 402d326 | 2009-02-12 10:40:00 +0000 | [diff] [blame] | 1222 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | }; |
| 1224 | |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 1225 | static const struct super_operations mtd_ops = { |
| 1226 | .drop_inode = generic_delete_inode, |
| 1227 | .statfs = simple_statfs, |
| 1228 | }; |
| 1229 | |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1230 | static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type, |
| 1231 | int flags, const char *dev_name, void *data) |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1232 | { |
Al Viro | 00292bb | 2012-03-23 13:47:52 -0400 | [diff] [blame] | 1233 | 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] | 1234 | } |
| 1235 | |
| 1236 | static struct file_system_type mtd_inodefs_type = { |
| 1237 | .name = "mtd_inodefs", |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 1238 | .mount = mtd_inodefs_mount, |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1239 | .kill_sb = kill_anon_super, |
| 1240 | }; |
| 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | static int __init init_mtdchar(void) |
| 1243 | { |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1244 | int ret; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 1245 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1246 | ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, |
Ben Hutchings | dad0db3 | 2010-01-29 21:00:04 +0000 | [diff] [blame] | 1247 | "mtd", &mtd_fops); |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1248 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1254 | 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. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1259 | return ret; |
| 1260 | |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1261 | err_unregister_chdev: |
| 1262 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); |
| 1263 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static void __exit cleanup_mtdchar(void) |
| 1267 | { |
Kirill A. Shutemov | cd87423 | 2010-05-17 16:55:47 +0300 | [diff] [blame] | 1268 | unregister_filesystem(&mtd_inodefs_type); |
Ben Hutchings | dad0db3 | 2010-01-29 21:00:04 +0000 | [diff] [blame] | 1269 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | module_init(init_mtdchar); |
| 1273 | module_exit(cleanup_mtdchar); |
| 1274 | |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 1275 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
| 1277 | MODULE_LICENSE("GPL"); |
| 1278 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 1279 | MODULE_DESCRIPTION("Direct character-device access to MTD devices"); |
Scott James Remnant | 90160e1 | 2009-03-02 18:42:39 +0000 | [diff] [blame] | 1280 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |