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