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