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