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