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