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