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