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