Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 2 | * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Character-device access to raw MTD devices. |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/config.h> |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/sched.h> |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mtd/mtd.h> |
| 18 | #include <linux/mtd/compatmac.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 20 | #include <asm/uaccess.h> |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 21 | |
| 22 | static struct class *mtd_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | static void mtd_notify_add(struct mtd_info* mtd) |
| 25 | { |
| 26 | if (!mtd) |
| 27 | return; |
| 28 | |
Greg Kroah-Hartman | 53f4654 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 29 | class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 30 | NULL, "mtd%d", mtd->index); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 31 | |
Greg Kroah-Hartman | 53f4654 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 32 | class_device_create(mtd_class, NULL, |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 33 | 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 | |
| 42 | class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2)); |
| 43 | class_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 | /* |
| 52 | * We use file->private_data to store a pointer to the MTDdevice. |
| 53 | * Since alighment is at least 32 bits, we have 2 bits free for OTP |
| 54 | * modes as well. |
| 55 | */ |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 56 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 57 | #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L) |
| 58 | |
| 59 | #define MTD_MODE_OTP_FACT 1 |
| 60 | #define MTD_MODE_OTP_USER 2 |
| 61 | #define MTD_MODE(file) ((long)((file)->private_data) & 3) |
| 62 | |
| 63 | #define SET_MTD_MODE(file, mode) \ |
| 64 | do { long __p = (long)((file)->private_data); \ |
| 65 | (file)->private_data = (void *)((__p & ~3L) | mode); } while (0) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static loff_t mtd_lseek (struct file *file, loff_t offset, int orig) |
| 68 | { |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 69 | struct mtd_info *mtd = TO_MTD(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | switch (orig) { |
| 72 | case 0: |
| 73 | /* SEEK_SET */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | break; |
| 75 | case 1: |
| 76 | /* SEEK_CUR */ |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 77 | offset += file->f_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | case 2: |
| 80 | /* SEEK_END */ |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 81 | offset += mtd->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; |
| 83 | default: |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 87 | if (offset >= 0 && offset < mtd->size) |
| 88 | return file->f_pos = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Todd Poynor | 8b491d7 | 2005-08-04 02:05:51 +0100 | [diff] [blame] | 90 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | static int mtd_open(struct inode *inode, struct file *file) |
| 96 | { |
| 97 | int minor = iminor(inode); |
| 98 | int devnum = minor >> 1; |
| 99 | struct mtd_info *mtd; |
| 100 | |
| 101 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n"); |
| 102 | |
| 103 | if (devnum >= MAX_MTD_DEVICES) |
| 104 | return -ENODEV; |
| 105 | |
| 106 | /* You can't open the RO devices RW */ |
| 107 | if ((file->f_mode & 2) && (minor & 1)) |
| 108 | return -EACCES; |
| 109 | |
| 110 | mtd = get_mtd_device(NULL, devnum); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (!mtd) |
| 113 | return -ENODEV; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (MTD_ABSENT == mtd->type) { |
| 116 | put_mtd_device(mtd); |
| 117 | return -ENODEV; |
| 118 | } |
| 119 | |
| 120 | file->private_data = mtd; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* You can't open it RW if it's not a writeable device */ |
| 123 | if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) { |
| 124 | put_mtd_device(mtd); |
| 125 | return -EACCES; |
| 126 | } |
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 | return 0; |
| 129 | } /* mtd_open */ |
| 130 | |
| 131 | /*====================================================================*/ |
| 132 | |
| 133 | static int mtd_close(struct inode *inode, struct file *file) |
| 134 | { |
| 135 | struct mtd_info *mtd; |
| 136 | |
| 137 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n"); |
| 138 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 139 | mtd = TO_MTD(file); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (mtd->sync) |
| 142 | mtd->sync(mtd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | put_mtd_device(mtd); |
| 145 | |
| 146 | return 0; |
| 147 | } /* mtd_close */ |
| 148 | |
| 149 | /* FIXME: This _really_ needs to die. In 2.5, we should lock the |
| 150 | userspace buffer down and use it directly with readv/writev. |
| 151 | */ |
| 152 | #define MAX_KMALLOC_SIZE 0x20000 |
| 153 | |
| 154 | static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos) |
| 155 | { |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 156 | struct mtd_info *mtd = TO_MTD(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | size_t retlen=0; |
| 158 | size_t total_retlen=0; |
| 159 | int ret=0; |
| 160 | int len; |
| 161 | char *kbuf; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n"); |
| 164 | |
| 165 | if (*ppos + count > mtd->size) |
| 166 | count = mtd->size - *ppos; |
| 167 | |
| 168 | if (!count) |
| 169 | return 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* FIXME: Use kiovec in 2.5 to lock down the user's buffers |
| 172 | and pass them directly to the MTD functions */ |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 173 | |
| 174 | if (count > MAX_KMALLOC_SIZE) |
| 175 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); |
| 176 | else |
| 177 | kbuf=kmalloc(count, GFP_KERNEL); |
| 178 | |
| 179 | if (!kbuf) |
| 180 | return -ENOMEM; |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | while (count) { |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 183 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 184 | if (count > MAX_KMALLOC_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | len = MAX_KMALLOC_SIZE; |
| 186 | else |
| 187 | len = count; |
| 188 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 189 | switch (MTD_MODE(file)) { |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 190 | case MTD_MODE_OTP_FACT: |
| 191 | ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf); |
| 192 | break; |
| 193 | case MTD_MODE_OTP_USER: |
| 194 | ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); |
| 195 | break; |
| 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 | { |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 235 | struct mtd_info *mtd = TO_MTD(file); |
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 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 273 | switch (MTD_MODE(file)) { |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 274 | case MTD_MODE_OTP_FACT: |
| 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; |
| 284 | default: |
| 285 | ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf); |
| 286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (!ret) { |
| 288 | *ppos += retlen; |
| 289 | total_retlen += retlen; |
| 290 | count -= retlen; |
| 291 | buf += retlen; |
| 292 | } |
| 293 | else { |
| 294 | kfree(kbuf); |
| 295 | return ret; |
| 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Thago Galesi | b802c07 | 2006-04-17 17:38:15 +0100 | [diff] [blame] | 299 | kfree(kbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return total_retlen; |
| 301 | } /* mtd_write */ |
| 302 | |
| 303 | /*====================================================================== |
| 304 | |
| 305 | IOCTL calls for getting device parameters. |
| 306 | |
| 307 | ======================================================================*/ |
| 308 | static void mtdchar_erase_callback (struct erase_info *instr) |
| 309 | { |
| 310 | wake_up((wait_queue_head_t *)instr->priv); |
| 311 | } |
| 312 | |
| 313 | static int mtd_ioctl(struct inode *inode, struct file *file, |
| 314 | u_int cmd, u_long arg) |
| 315 | { |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 316 | struct mtd_info *mtd = TO_MTD(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | void __user *argp = (void __user *)arg; |
| 318 | int ret = 0; |
| 319 | u_long size; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n"); |
| 322 | |
| 323 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; |
| 324 | if (cmd & IOC_IN) { |
| 325 | if (!access_ok(VERIFY_READ, argp, size)) |
| 326 | return -EFAULT; |
| 327 | } |
| 328 | if (cmd & IOC_OUT) { |
| 329 | if (!access_ok(VERIFY_WRITE, argp, size)) |
| 330 | return -EFAULT; |
| 331 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | switch (cmd) { |
| 334 | case MEMGETREGIONCOUNT: |
| 335 | if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) |
| 336 | return -EFAULT; |
| 337 | break; |
| 338 | |
| 339 | case MEMGETREGIONINFO: |
| 340 | { |
| 341 | struct region_info_user ur; |
| 342 | |
| 343 | if (copy_from_user(&ur, argp, sizeof(struct region_info_user))) |
| 344 | return -EFAULT; |
| 345 | |
| 346 | if (ur.regionindex >= mtd->numeraseregions) |
| 347 | return -EINVAL; |
| 348 | if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]), |
| 349 | sizeof(struct mtd_erase_region_info))) |
| 350 | return -EFAULT; |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | case MEMGETINFO: |
| 355 | if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user))) |
| 356 | return -EFAULT; |
| 357 | break; |
| 358 | |
| 359 | case MEMERASE: |
| 360 | { |
| 361 | struct erase_info *erase; |
| 362 | |
| 363 | if(!(file->f_mode & 2)) |
| 364 | return -EPERM; |
| 365 | |
| 366 | erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL); |
| 367 | if (!erase) |
| 368 | ret = -ENOMEM; |
| 369 | else { |
| 370 | wait_queue_head_t waitq; |
| 371 | DECLARE_WAITQUEUE(wait, current); |
| 372 | |
| 373 | init_waitqueue_head(&waitq); |
| 374 | |
| 375 | memset (erase,0,sizeof(struct erase_info)); |
| 376 | if (copy_from_user(&erase->addr, argp, |
| 377 | sizeof(struct erase_info_user))) { |
| 378 | kfree(erase); |
| 379 | return -EFAULT; |
| 380 | } |
| 381 | erase->mtd = mtd; |
| 382 | erase->callback = mtdchar_erase_callback; |
| 383 | erase->priv = (unsigned long)&waitq; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | /* |
| 386 | FIXME: Allow INTERRUPTIBLE. Which means |
| 387 | not having the wait_queue head on the stack. |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | If the wq_head is on the stack, and we |
| 390 | leave because we got interrupted, then the |
| 391 | wq_head is no longer there when the |
| 392 | callback routine tries to wake us up. |
| 393 | */ |
| 394 | ret = mtd->erase(mtd, erase); |
| 395 | if (!ret) { |
| 396 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 397 | add_wait_queue(&waitq, &wait); |
| 398 | if (erase->state != MTD_ERASE_DONE && |
| 399 | erase->state != MTD_ERASE_FAILED) |
| 400 | schedule(); |
| 401 | remove_wait_queue(&waitq, &wait); |
| 402 | set_current_state(TASK_RUNNING); |
| 403 | |
| 404 | ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0; |
| 405 | } |
| 406 | kfree(erase); |
| 407 | } |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | case MEMWRITEOOB: |
| 412 | { |
| 413 | struct mtd_oob_buf buf; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 414 | struct mtd_oob_ops ops; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if(!(file->f_mode & 2)) |
| 417 | return -EPERM; |
| 418 | |
| 419 | if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) |
| 420 | return -EFAULT; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 421 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 422 | if (buf.length > 4096) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | |
| 425 | if (!mtd->write_oob) |
| 426 | ret = -EOPNOTSUPP; |
| 427 | else |
| 428 | ret = access_ok(VERIFY_READ, buf.ptr, |
| 429 | buf.length) ? 0 : EFAULT; |
| 430 | |
| 431 | if (ret) |
| 432 | return ret; |
| 433 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 434 | ops.len = buf.length; |
| 435 | ops.ooblen = mtd->oobsize; |
| 436 | ops.ooboffs = buf.start & (mtd->oobsize - 1); |
| 437 | ops.datbuf = NULL; |
| 438 | ops.mode = MTD_OOB_PLACE; |
| 439 | |
| 440 | if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs)) |
| 441 | return -EINVAL; |
| 442 | |
| 443 | ops.oobbuf = kmalloc(buf.length, GFP_KERNEL); |
| 444 | if (!ops.oobbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return -ENOMEM; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 446 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 447 | if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) { |
| 448 | kfree(ops.oobbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return -EFAULT; |
| 450 | } |
| 451 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 452 | buf.start &= ~(mtd->oobsize - 1); |
| 453 | ret = mtd->write_oob(mtd, buf.start, &ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 455 | if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen, |
| 456 | sizeof(uint32_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | ret = -EFAULT; |
| 458 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 459 | kfree(ops.oobbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | break; |
| 461 | |
| 462 | } |
| 463 | |
| 464 | case MEMREADOOB: |
| 465 | { |
| 466 | struct mtd_oob_buf buf; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 467 | struct mtd_oob_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) |
| 470 | return -EFAULT; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 471 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 472 | if (buf.length > 4096) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return -EINVAL; |
| 474 | |
| 475 | if (!mtd->read_oob) |
| 476 | ret = -EOPNOTSUPP; |
| 477 | else |
| 478 | ret = access_ok(VERIFY_WRITE, buf.ptr, |
| 479 | buf.length) ? 0 : -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (ret) |
| 481 | return ret; |
| 482 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 483 | ops.len = buf.length; |
| 484 | ops.ooblen = mtd->oobsize; |
| 485 | ops.ooboffs = buf.start & (mtd->oobsize - 1); |
| 486 | ops.datbuf = NULL; |
| 487 | ops.mode = MTD_OOB_PLACE; |
| 488 | |
| 489 | if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs)) |
| 490 | return -EINVAL; |
| 491 | |
| 492 | ops.oobbuf = kmalloc(buf.length, GFP_KERNEL); |
| 493 | if (!ops.oobbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return -ENOMEM; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 495 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 496 | buf.start &= ~(mtd->oobsize - 1); |
| 497 | ret = mtd->read_oob(mtd, buf.start, &ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 499 | if (put_user(ops.retlen, (uint32_t __user *)argp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | ret = -EFAULT; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 501 | else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf, |
| 502 | ops.retlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | ret = -EFAULT; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 504 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 505 | kfree(ops.oobbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | |
| 509 | case MEMLOCK: |
| 510 | { |
| 511 | struct erase_info_user info; |
| 512 | |
| 513 | if (copy_from_user(&info, argp, sizeof(info))) |
| 514 | return -EFAULT; |
| 515 | |
| 516 | if (!mtd->lock) |
| 517 | ret = -EOPNOTSUPP; |
| 518 | else |
| 519 | ret = mtd->lock(mtd, info.start, info.length); |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | case MEMUNLOCK: |
| 524 | { |
| 525 | struct erase_info_user info; |
| 526 | |
| 527 | if (copy_from_user(&info, argp, sizeof(info))) |
| 528 | return -EFAULT; |
| 529 | |
| 530 | if (!mtd->unlock) |
| 531 | ret = -EOPNOTSUPP; |
| 532 | else |
| 533 | ret = mtd->unlock(mtd, info.start, info.length); |
| 534 | break; |
| 535 | } |
| 536 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 537 | /* Legacy interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | case MEMGETOOBSEL: |
| 539 | { |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 540 | struct nand_oobinfo oi; |
| 541 | |
| 542 | if (!mtd->ecclayout) |
| 543 | return -EOPNOTSUPP; |
| 544 | if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos)) |
| 545 | return -EINVAL; |
| 546 | |
| 547 | oi.useecc = MTD_NANDECC_AUTOPLACE; |
| 548 | memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos)); |
| 549 | memcpy(&oi.oobfree, mtd->ecclayout->oobfree, |
| 550 | sizeof(oi.oobfree)); |
| 551 | |
| 552 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return -EFAULT; |
| 554 | break; |
| 555 | } |
| 556 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 557 | case ECCGETLAYOUT: |
| 558 | |
| 559 | if (!mtd->ecclayout) |
| 560 | return -EOPNOTSUPP; |
| 561 | |
| 562 | if (copy_to_user(argp, &mtd->ecclayout, |
| 563 | sizeof(struct nand_ecclayout))) |
| 564 | return -EFAULT; |
| 565 | break; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | case MEMGETBADBLOCK: |
| 568 | { |
| 569 | loff_t offs; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
| 572 | return -EFAULT; |
| 573 | if (!mtd->block_isbad) |
| 574 | ret = -EOPNOTSUPP; |
| 575 | else |
| 576 | return mtd->block_isbad(mtd, offs); |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | case MEMSETBADBLOCK: |
| 581 | { |
| 582 | loff_t offs; |
| 583 | |
| 584 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
| 585 | return -EFAULT; |
| 586 | if (!mtd->block_markbad) |
| 587 | ret = -EOPNOTSUPP; |
| 588 | else |
| 589 | return mtd->block_markbad(mtd, offs); |
| 590 | break; |
| 591 | } |
| 592 | |
Kyungmin Park | 493c646 | 2006-05-12 17:03:07 +0300 | [diff] [blame] | 593 | #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 594 | case OTPSELECT: |
| 595 | { |
| 596 | int mode; |
| 597 | if (copy_from_user(&mode, argp, sizeof(int))) |
| 598 | return -EFAULT; |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 599 | SET_MTD_MODE(file, 0); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 600 | switch (mode) { |
| 601 | case MTD_OTP_FACTORY: |
| 602 | if (!mtd->read_fact_prot_reg) |
| 603 | ret = -EOPNOTSUPP; |
| 604 | else |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 605 | SET_MTD_MODE(file, MTD_MODE_OTP_FACT); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 606 | break; |
| 607 | case MTD_OTP_USER: |
| 608 | if (!mtd->read_fact_prot_reg) |
| 609 | ret = -EOPNOTSUPP; |
| 610 | else |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 611 | SET_MTD_MODE(file, MTD_MODE_OTP_USER); |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 612 | break; |
| 613 | default: |
| 614 | ret = -EINVAL; |
| 615 | case MTD_OTP_OFF: |
| 616 | break; |
| 617 | } |
Nicolas Pitre | 81dba48 | 2005-04-01 16:36:15 +0100 | [diff] [blame] | 618 | file->f_pos = 0; |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 619 | break; |
| 620 | } |
| 621 | |
| 622 | case OTPGETREGIONCOUNT: |
| 623 | case OTPGETREGIONINFO: |
| 624 | { |
| 625 | struct otp_info *buf = kmalloc(4096, GFP_KERNEL); |
| 626 | if (!buf) |
| 627 | return -ENOMEM; |
| 628 | ret = -EOPNOTSUPP; |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 629 | switch (MTD_MODE(file)) { |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 630 | case MTD_MODE_OTP_FACT: |
| 631 | if (mtd->get_fact_prot_info) |
| 632 | ret = mtd->get_fact_prot_info(mtd, buf, 4096); |
| 633 | break; |
| 634 | case MTD_MODE_OTP_USER: |
| 635 | if (mtd->get_user_prot_info) |
| 636 | ret = mtd->get_user_prot_info(mtd, buf, 4096); |
| 637 | break; |
| 638 | } |
| 639 | if (ret >= 0) { |
| 640 | if (cmd == OTPGETREGIONCOUNT) { |
| 641 | int nbr = ret / sizeof(struct otp_info); |
| 642 | ret = copy_to_user(argp, &nbr, sizeof(int)); |
| 643 | } else |
| 644 | ret = copy_to_user(argp, buf, ret); |
| 645 | if (ret) |
| 646 | ret = -EFAULT; |
| 647 | } |
| 648 | kfree(buf); |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | case OTPLOCK: |
| 653 | { |
| 654 | struct otp_info info; |
| 655 | |
Nicolas Pitre | 045e9a5 | 2005-02-08 19:12:53 +0000 | [diff] [blame] | 656 | if (MTD_MODE(file) != MTD_MODE_OTP_USER) |
Nicolas Pitre | 31f4233 | 2005-02-08 17:45:55 +0000 | [diff] [blame] | 657 | return -EINVAL; |
| 658 | if (copy_from_user(&info, argp, sizeof(info))) |
| 659 | return -EFAULT; |
| 660 | if (!mtd->lock_user_prot_reg) |
| 661 | return -EOPNOTSUPP; |
| 662 | ret = mtd->lock_user_prot_reg(mtd, info.start, info.length); |
| 663 | break; |
| 664 | } |
| 665 | #endif |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | default: |
| 668 | ret = -ENOTTY; |
| 669 | } |
| 670 | |
| 671 | return ret; |
| 672 | } /* memory_ioctl */ |
| 673 | |
| 674 | static struct file_operations mtd_fops = { |
| 675 | .owner = THIS_MODULE, |
| 676 | .llseek = mtd_lseek, |
| 677 | .read = mtd_read, |
| 678 | .write = mtd_write, |
| 679 | .ioctl = mtd_ioctl, |
| 680 | .open = mtd_open, |
| 681 | .release = mtd_close, |
| 682 | }; |
| 683 | |
| 684 | static int __init init_mtdchar(void) |
| 685 | { |
| 686 | if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) { |
| 687 | printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n", |
| 688 | MTD_CHAR_MAJOR); |
| 689 | return -EAGAIN; |
| 690 | } |
| 691 | |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 692 | mtd_class = class_create(THIS_MODULE, "mtd"); |
| 693 | |
| 694 | if (IS_ERR(mtd_class)) { |
| 695 | printk(KERN_ERR "Error creating mtd class.\n"); |
| 696 | unregister_chrdev(MTD_CHAR_MAJOR, "mtd"); |
Coywolf Qi Hunt | 3a7a882 | 2005-07-04 12:15:28 -0500 | [diff] [blame] | 697 | return PTR_ERR(mtd_class); |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | register_mtd_user(¬ifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static void __exit cleanup_mtdchar(void) |
| 705 | { |
Todd Poynor | 9bc7b38 | 2005-06-30 01:23:27 +0100 | [diff] [blame] | 706 | unregister_mtd_user(¬ifier); |
| 707 | class_destroy(mtd_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | unregister_chrdev(MTD_CHAR_MAJOR, "mtd"); |
| 709 | } |
| 710 | |
| 711 | module_init(init_mtdchar); |
| 712 | module_exit(cleanup_mtdchar); |
| 713 | |
| 714 | |
| 715 | MODULE_LICENSE("GPL"); |
| 716 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 717 | MODULE_DESCRIPTION("Direct character-device access to MTD devices"); |