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