Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
a1452a37 DW |
2 | * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 LT |
17 | * |
18 | */ | |
19 | ||
15fdc52f TG |
20 | #include <linux/device.h> |
21 | #include <linux/fs.h> | |
0c1eafdb | 22 | #include <linux/mm.h> |
9c74034f | 23 | #include <linux/err.h> |
15fdc52f | 24 | #include <linux/init.h> |
1da177e4 LT |
25 | #include <linux/kernel.h> |
26 | #include <linux/module.h> | |
15fdc52f TG |
27 | #include <linux/slab.h> |
28 | #include <linux/sched.h> | |
5aa82940 | 29 | #include <linux/mutex.h> |
402d3265 | 30 | #include <linux/backing-dev.h> |
97718540 | 31 | #include <linux/compat.h> |
cd874237 | 32 | #include <linux/mount.h> |
d0f7959e | 33 | #include <linux/blkpg.h> |
b502bd11 | 34 | #include <linux/magic.h> |
f83c3838 | 35 | #include <linux/major.h> |
1da177e4 | 36 | #include <linux/mtd/mtd.h> |
d0f7959e | 37 | #include <linux/mtd/partitions.h> |
dd02b67d | 38 | #include <linux/mtd/map.h> |
1da177e4 | 39 | |
7c0f6ba6 | 40 | #include <linux/uaccess.h> |
9bc7b387 | 41 | |
660685d9 AB |
42 | #include "mtdcore.h" |
43 | ||
5aa82940 | 44 | static DEFINE_MUTEX(mtd_mutex); |
1da177e4 | 45 | |
045e9a5d | 46 | /* |
f1a28c02 | 47 | * Data structure to hold the pointer to the mtd device as well |
92394b5c | 48 | * as mode information of various use cases. |
045e9a5d | 49 | */ |
f1a28c02 TG |
50 | struct mtd_file_info { |
51 | struct mtd_info *mtd; | |
52 | enum mtd_file_modes mode; | |
53 | }; | |
31f4233b | 54 | |
969e57ad | 55 | static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig) |
1da177e4 | 56 | { |
f1a28c02 | 57 | struct mtd_file_info *mfi = file->private_data; |
b959957f | 58 | return fixed_size_llseek(file, offset, orig, mfi->mtd->size); |
1da177e4 LT |
59 | } |
60 | ||
969e57ad | 61 | static int mtdchar_open(struct inode *inode, struct file *file) |
1da177e4 LT |
62 | { |
63 | int minor = iminor(inode); | |
64 | int devnum = minor >> 1; | |
6071239e | 65 | int ret = 0; |
1da177e4 | 66 | struct mtd_info *mtd; |
f1a28c02 | 67 | struct mtd_file_info *mfi; |
1da177e4 | 68 | |
289c0522 | 69 | pr_debug("MTD_open\n"); |
1da177e4 | 70 | |
1da177e4 | 71 | /* You can't open the RO devices RW */ |
aeb5d727 | 72 | if ((file->f_mode & FMODE_WRITE) && (minor & 1)) |
1da177e4 LT |
73 | return -EACCES; |
74 | ||
5aa82940 | 75 | mutex_lock(&mtd_mutex); |
1da177e4 | 76 | mtd = get_mtd_device(NULL, devnum); |
97894cda | 77 | |
6071239e JC |
78 | if (IS_ERR(mtd)) { |
79 | ret = PTR_ERR(mtd); | |
80 | goto out; | |
81 | } | |
97894cda | 82 | |
402d3265 | 83 | if (mtd->type == MTD_ABSENT) { |
6071239e | 84 | ret = -ENODEV; |
c65390f4 | 85 | goto out1; |
1da177e4 LT |
86 | } |
87 | ||
1da177e4 | 88 | /* You can't open it RW if it's not a writeable device */ |
aeb5d727 | 89 | if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) { |
6071239e | 90 | ret = -EACCES; |
b4caecd4 | 91 | goto out1; |
1da177e4 | 92 | } |
97894cda | 93 | |
f1a28c02 TG |
94 | mfi = kzalloc(sizeof(*mfi), GFP_KERNEL); |
95 | if (!mfi) { | |
6071239e | 96 | ret = -ENOMEM; |
b4caecd4 | 97 | goto out1; |
f1a28c02 TG |
98 | } |
99 | mfi->mtd = mtd; | |
100 | file->private_data = mfi; | |
c65390f4 AV |
101 | mutex_unlock(&mtd_mutex); |
102 | return 0; | |
f1a28c02 | 103 | |
c65390f4 AV |
104 | out1: |
105 | put_mtd_device(mtd); | |
6071239e | 106 | out: |
5aa82940 | 107 | mutex_unlock(&mtd_mutex); |
6071239e | 108 | return ret; |
969e57ad | 109 | } /* mtdchar_open */ |
1da177e4 LT |
110 | |
111 | /*====================================================================*/ | |
112 | ||
969e57ad | 113 | static int mtdchar_close(struct inode *inode, struct file *file) |
1da177e4 | 114 | { |
f1a28c02 TG |
115 | struct mtd_file_info *mfi = file->private_data; |
116 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 | 117 | |
289c0522 | 118 | pr_debug("MTD_close\n"); |
1da177e4 | 119 | |
7eafaed5 | 120 | /* Only sync if opened RW */ |
327cf292 | 121 | if ((file->f_mode & FMODE_WRITE)) |
85f2f2a8 | 122 | mtd_sync(mtd); |
97894cda | 123 | |
1da177e4 | 124 | put_mtd_device(mtd); |
f1a28c02 TG |
125 | file->private_data = NULL; |
126 | kfree(mfi); | |
1da177e4 LT |
127 | |
128 | return 0; | |
969e57ad | 129 | } /* mtdchar_close */ |
1da177e4 | 130 | |
3e45cf5e GE |
131 | /* Back in June 2001, dwmw2 wrote: |
132 | * | |
133 | * FIXME: This _really_ needs to die. In 2.5, we should lock the | |
134 | * userspace buffer down and use it directly with readv/writev. | |
135 | * | |
136 | * The implementation below, using mtd_kmalloc_up_to, mitigates | |
137 | * allocation failures when the system is under low-memory situations | |
138 | * or if memory is highly fragmented at the cost of reducing the | |
139 | * performance of the requested transfer due to a smaller buffer size. | |
140 | * | |
141 | * A more complex but more memory-efficient implementation based on | |
142 | * get_user_pages and iovecs to cover extents of those pages is a | |
143 | * longer-term goal, as intimated by dwmw2 above. However, for the | |
144 | * write case, this requires yet more complex head and tail transfer | |
145 | * handling when those head and tail offsets and sizes are such that | |
146 | * alignment requirements are not met in the NAND subdriver. | |
147 | */ | |
1da177e4 | 148 | |
969e57ad AB |
149 | static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, |
150 | loff_t *ppos) | |
1da177e4 | 151 | { |
f1a28c02 TG |
152 | struct mtd_file_info *mfi = file->private_data; |
153 | struct mtd_info *mtd = mfi->mtd; | |
30fa9848 | 154 | size_t retlen; |
1da177e4 LT |
155 | size_t total_retlen=0; |
156 | int ret=0; | |
157 | int len; | |
3e45cf5e | 158 | size_t size = count; |
1da177e4 | 159 | char *kbuf; |
97894cda | 160 | |
289c0522 | 161 | pr_debug("MTD_read\n"); |
1da177e4 LT |
162 | |
163 | if (*ppos + count > mtd->size) | |
164 | count = mtd->size - *ppos; | |
165 | ||
166 | if (!count) | |
167 | return 0; | |
97894cda | 168 | |
3e45cf5e | 169 | kbuf = mtd_kmalloc_up_to(mtd, &size); |
b802c074 TG |
170 | if (!kbuf) |
171 | return -ENOMEM; | |
172 | ||
1da177e4 | 173 | while (count) { |
3e45cf5e | 174 | len = min_t(size_t, count, size); |
1da177e4 | 175 | |
f1a28c02 | 176 | switch (mfi->mode) { |
beb133fc | 177 | case MTD_FILE_MODE_OTP_FACTORY: |
d264f72a AB |
178 | ret = mtd_read_fact_prot_reg(mtd, *ppos, len, |
179 | &retlen, kbuf); | |
31f4233b | 180 | break; |
beb133fc | 181 | case MTD_FILE_MODE_OTP_USER: |
4ea1cabb AB |
182 | ret = mtd_read_user_prot_reg(mtd, *ppos, len, |
183 | &retlen, kbuf); | |
31f4233b | 184 | break; |
beb133fc | 185 | case MTD_FILE_MODE_RAW: |
f1a28c02 TG |
186 | { |
187 | struct mtd_oob_ops ops; | |
188 | ||
0612b9dd | 189 | ops.mode = MTD_OPS_RAW; |
f1a28c02 TG |
190 | ops.datbuf = kbuf; |
191 | ops.oobbuf = NULL; | |
192 | ops.len = len; | |
193 | ||
fd2819bb | 194 | ret = mtd_read_oob(mtd, *ppos, &ops); |
f1a28c02 TG |
195 | retlen = ops.retlen; |
196 | break; | |
197 | } | |
31f4233b | 198 | default: |
329ad399 | 199 | ret = mtd_read(mtd, *ppos, len, &retlen, kbuf); |
31f4233b | 200 | } |
7854d3f7 | 201 | /* Nand returns -EBADMSG on ECC errors, but it returns |
1da177e4 | 202 | * the data. For our userspace tools it is important |
7854d3f7 | 203 | * to dump areas with ECC errors! |
9a1fcdfd | 204 | * For kernel internal usage it also might return -EUCLEAN |
25985edc | 205 | * to signal the caller that a bitflip has occurred and has |
9a1fcdfd | 206 | * been corrected by the ECC algorithm. |
1da177e4 LT |
207 | * Userspace software which accesses NAND this way |
208 | * must be aware of the fact that it deals with NAND | |
209 | */ | |
d57f4054 | 210 | if (!ret || mtd_is_bitflip_or_eccerr(ret)) { |
1da177e4 LT |
211 | *ppos += retlen; |
212 | if (copy_to_user(buf, kbuf, retlen)) { | |
f4a43cfc | 213 | kfree(kbuf); |
1da177e4 LT |
214 | return -EFAULT; |
215 | } | |
216 | else | |
217 | total_retlen += retlen; | |
218 | ||
219 | count -= retlen; | |
220 | buf += retlen; | |
31f4233b NP |
221 | if (retlen == 0) |
222 | count = 0; | |
1da177e4 LT |
223 | } |
224 | else { | |
225 | kfree(kbuf); | |
226 | return ret; | |
227 | } | |
97894cda | 228 | |
1da177e4 LT |
229 | } |
230 | ||
b802c074 | 231 | kfree(kbuf); |
1da177e4 | 232 | return total_retlen; |
969e57ad | 233 | } /* mtdchar_read */ |
1da177e4 | 234 | |
969e57ad AB |
235 | static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count, |
236 | loff_t *ppos) | |
1da177e4 | 237 | { |
f1a28c02 TG |
238 | struct mtd_file_info *mfi = file->private_data; |
239 | struct mtd_info *mtd = mfi->mtd; | |
3e45cf5e | 240 | size_t size = count; |
1da177e4 LT |
241 | char *kbuf; |
242 | size_t retlen; | |
243 | size_t total_retlen=0; | |
244 | int ret=0; | |
245 | int len; | |
246 | ||
289c0522 | 247 | pr_debug("MTD_write\n"); |
97894cda | 248 | |
1da177e4 LT |
249 | if (*ppos == mtd->size) |
250 | return -ENOSPC; | |
97894cda | 251 | |
1da177e4 LT |
252 | if (*ppos + count > mtd->size) |
253 | count = mtd->size - *ppos; | |
254 | ||
255 | if (!count) | |
256 | return 0; | |
257 | ||
3e45cf5e | 258 | kbuf = mtd_kmalloc_up_to(mtd, &size); |
b802c074 TG |
259 | if (!kbuf) |
260 | return -ENOMEM; | |
261 | ||
1da177e4 | 262 | while (count) { |
3e45cf5e | 263 | len = min_t(size_t, count, size); |
1da177e4 | 264 | |
1da177e4 LT |
265 | if (copy_from_user(kbuf, buf, len)) { |
266 | kfree(kbuf); | |
267 | return -EFAULT; | |
268 | } | |
97894cda | 269 | |
f1a28c02 | 270 | switch (mfi->mode) { |
beb133fc | 271 | case MTD_FILE_MODE_OTP_FACTORY: |
31f4233b NP |
272 | ret = -EROFS; |
273 | break; | |
beb133fc | 274 | case MTD_FILE_MODE_OTP_USER: |
482b43ad AB |
275 | ret = mtd_write_user_prot_reg(mtd, *ppos, len, |
276 | &retlen, kbuf); | |
31f4233b | 277 | break; |
f1a28c02 | 278 | |
beb133fc | 279 | case MTD_FILE_MODE_RAW: |
f1a28c02 TG |
280 | { |
281 | struct mtd_oob_ops ops; | |
282 | ||
0612b9dd | 283 | ops.mode = MTD_OPS_RAW; |
f1a28c02 TG |
284 | ops.datbuf = kbuf; |
285 | ops.oobbuf = NULL; | |
bf514081 | 286 | ops.ooboffs = 0; |
f1a28c02 TG |
287 | ops.len = len; |
288 | ||
a2cc5ba0 | 289 | ret = mtd_write_oob(mtd, *ppos, &ops); |
f1a28c02 TG |
290 | retlen = ops.retlen; |
291 | break; | |
292 | } | |
293 | ||
31f4233b | 294 | default: |
eda95cbf | 295 | ret = mtd_write(mtd, *ppos, len, &retlen, kbuf); |
31f4233b | 296 | } |
9a78bc83 CR |
297 | |
298 | /* | |
299 | * Return -ENOSPC only if no data could be written at all. | |
300 | * Otherwise just return the number of bytes that actually | |
301 | * have been written. | |
302 | */ | |
303 | if ((ret == -ENOSPC) && (total_retlen)) | |
304 | break; | |
305 | ||
1da177e4 LT |
306 | if (!ret) { |
307 | *ppos += retlen; | |
308 | total_retlen += retlen; | |
309 | count -= retlen; | |
310 | buf += retlen; | |
311 | } | |
312 | else { | |
313 | kfree(kbuf); | |
314 | return ret; | |
315 | } | |
1da177e4 LT |
316 | } |
317 | ||
b802c074 | 318 | kfree(kbuf); |
1da177e4 | 319 | return total_retlen; |
969e57ad | 320 | } /* mtdchar_write */ |
1da177e4 LT |
321 | |
322 | /*====================================================================== | |
323 | ||
324 | IOCTL calls for getting device parameters. | |
325 | ||
326 | ======================================================================*/ | |
1da177e4 | 327 | |
f1a28c02 TG |
328 | static int otp_select_filemode(struct mtd_file_info *mfi, int mode) |
329 | { | |
330 | struct mtd_info *mtd = mfi->mtd; | |
b6de3d6c | 331 | size_t retlen; |
b6de3d6c | 332 | |
f1a28c02 TG |
333 | switch (mode) { |
334 | case MTD_OTP_FACTORY: | |
5dc63fa2 UKK |
335 | if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == |
336 | -EOPNOTSUPP) | |
337 | return -EOPNOTSUPP; | |
338 | ||
b6de3d6c | 339 | mfi->mode = MTD_FILE_MODE_OTP_FACTORY; |
f1a28c02 TG |
340 | break; |
341 | case MTD_OTP_USER: | |
5dc63fa2 UKK |
342 | if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) == |
343 | -EOPNOTSUPP) | |
344 | return -EOPNOTSUPP; | |
345 | ||
b6de3d6c | 346 | mfi->mode = MTD_FILE_MODE_OTP_USER; |
f1a28c02 | 347 | break; |
f1a28c02 | 348 | case MTD_OTP_OFF: |
5dc63fa2 | 349 | mfi->mode = MTD_FILE_MODE_NORMAL; |
f1a28c02 | 350 | break; |
5dc63fa2 UKK |
351 | default: |
352 | return -EINVAL; | |
f1a28c02 | 353 | } |
5dc63fa2 UKK |
354 | |
355 | return 0; | |
f1a28c02 | 356 | } |
f1a28c02 | 357 | |
969e57ad | 358 | static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd, |
97718540 KC |
359 | uint64_t start, uint32_t length, void __user *ptr, |
360 | uint32_t __user *retp) | |
361 | { | |
9ce244b3 | 362 | struct mtd_file_info *mfi = file->private_data; |
97718540 KC |
363 | struct mtd_oob_ops ops; |
364 | uint32_t retlen; | |
365 | int ret = 0; | |
366 | ||
367 | if (!(file->f_mode & FMODE_WRITE)) | |
368 | return -EPERM; | |
369 | ||
370 | if (length > 4096) | |
371 | return -EINVAL; | |
372 | ||
3c3c10bb | 373 | if (!mtd->_write_oob) |
0db188f9 | 374 | return -EOPNOTSUPP; |
97718540 KC |
375 | |
376 | ops.ooblen = length; | |
305b93f1 | 377 | ops.ooboffs = start & (mtd->writesize - 1); |
97718540 | 378 | ops.datbuf = NULL; |
beb133fc | 379 | ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW : |
0612b9dd | 380 | MTD_OPS_PLACE_OOB; |
97718540 KC |
381 | |
382 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) | |
383 | return -EINVAL; | |
384 | ||
df1f1d1c JL |
385 | ops.oobbuf = memdup_user(ptr, length); |
386 | if (IS_ERR(ops.oobbuf)) | |
387 | return PTR_ERR(ops.oobbuf); | |
97718540 | 388 | |
305b93f1 | 389 | start &= ~((uint64_t)mtd->writesize - 1); |
a2cc5ba0 | 390 | ret = mtd_write_oob(mtd, start, &ops); |
97718540 KC |
391 | |
392 | if (ops.oobretlen > 0xFFFFFFFFU) | |
393 | ret = -EOVERFLOW; | |
394 | retlen = ops.oobretlen; | |
395 | if (copy_to_user(retp, &retlen, sizeof(length))) | |
396 | ret = -EFAULT; | |
397 | ||
398 | kfree(ops.oobbuf); | |
399 | return ret; | |
400 | } | |
401 | ||
969e57ad | 402 | static int mtdchar_readoob(struct file *file, struct mtd_info *mtd, |
c46f6483 BN |
403 | uint64_t start, uint32_t length, void __user *ptr, |
404 | uint32_t __user *retp) | |
97718540 | 405 | { |
c46f6483 | 406 | struct mtd_file_info *mfi = file->private_data; |
97718540 KC |
407 | struct mtd_oob_ops ops; |
408 | int ret = 0; | |
409 | ||
410 | if (length > 4096) | |
411 | return -EINVAL; | |
412 | ||
97718540 | 413 | ops.ooblen = length; |
305b93f1 | 414 | ops.ooboffs = start & (mtd->writesize - 1); |
97718540 | 415 | ops.datbuf = NULL; |
beb133fc | 416 | ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW : |
0612b9dd | 417 | MTD_OPS_PLACE_OOB; |
97718540 KC |
418 | |
419 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) | |
420 | return -EINVAL; | |
421 | ||
422 | ops.oobbuf = kmalloc(length, GFP_KERNEL); | |
423 | if (!ops.oobbuf) | |
424 | return -ENOMEM; | |
425 | ||
305b93f1 | 426 | start &= ~((uint64_t)mtd->writesize - 1); |
fd2819bb | 427 | ret = mtd_read_oob(mtd, start, &ops); |
97718540 KC |
428 | |
429 | if (put_user(ops.oobretlen, retp)) | |
430 | ret = -EFAULT; | |
431 | else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf, | |
432 | ops.oobretlen)) | |
433 | ret = -EFAULT; | |
434 | ||
435 | kfree(ops.oobbuf); | |
041e4575 BN |
436 | |
437 | /* | |
438 | * NAND returns -EBADMSG on ECC errors, but it returns the OOB | |
439 | * data. For our userspace tools it is important to dump areas | |
440 | * with ECC errors! | |
441 | * For kernel internal usage it also might return -EUCLEAN | |
5d708ecc | 442 | * to signal the caller that a bitflip has occurred and has |
041e4575 BN |
443 | * been corrected by the ECC algorithm. |
444 | * | |
c478d7e4 BN |
445 | * Note: currently the standard NAND function, nand_read_oob_std, |
446 | * does not calculate ECC for the OOB area, so do not rely on | |
447 | * this behavior unless you have replaced it with your own. | |
041e4575 | 448 | */ |
d57f4054 | 449 | if (mtd_is_bitflip_or_eccerr(ret)) |
041e4575 BN |
450 | return 0; |
451 | ||
97718540 KC |
452 | return ret; |
453 | } | |
454 | ||
cc26c3cd | 455 | /* |
aab616e3 BB |
456 | * Copies (and truncates, if necessary) OOB layout information to the |
457 | * deprecated layout struct, nand_ecclayout_user. This is necessary only to | |
458 | * support the deprecated API ioctl ECCGETLAYOUT while allowing all new | |
459 | * functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops | |
460 | * can describe any kind of OOB layout with almost zero overhead from a | |
461 | * memory usage point of view). | |
cc26c3cd | 462 | */ |
c2b78452 BB |
463 | static int shrink_ecclayout(struct mtd_info *mtd, |
464 | struct nand_ecclayout_user *to) | |
cc26c3cd | 465 | { |
c2b78452 BB |
466 | struct mtd_oob_region oobregion; |
467 | int i, section = 0, ret; | |
cc26c3cd | 468 | |
c2b78452 | 469 | if (!mtd || !to) |
cc26c3cd BN |
470 | return -EINVAL; |
471 | ||
472 | memset(to, 0, sizeof(*to)); | |
473 | ||
c2b78452 BB |
474 | to->eccbytes = 0; |
475 | for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) { | |
476 | u32 eccpos; | |
477 | ||
6de56493 | 478 | ret = mtd_ooblayout_ecc(mtd, section++, &oobregion); |
c2b78452 BB |
479 | if (ret < 0) { |
480 | if (ret != -ERANGE) | |
481 | return ret; | |
482 | ||
483 | break; | |
484 | } | |
485 | ||
486 | eccpos = oobregion.offset; | |
487 | for (; i < MTD_MAX_ECCPOS_ENTRIES && | |
488 | eccpos < oobregion.offset + oobregion.length; i++) { | |
489 | to->eccpos[i] = eccpos++; | |
490 | to->eccbytes++; | |
491 | } | |
492 | } | |
cc26c3cd BN |
493 | |
494 | for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) { | |
c2b78452 BB |
495 | ret = mtd_ooblayout_free(mtd, i, &oobregion); |
496 | if (ret < 0) { | |
497 | if (ret != -ERANGE) | |
498 | return ret; | |
499 | ||
500 | break; | |
501 | } | |
502 | ||
503 | to->oobfree[i].offset = oobregion.offset; | |
504 | to->oobfree[i].length = oobregion.length; | |
505 | to->oobavail += to->oobfree[i].length; | |
506 | } | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
511 | static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to) | |
512 | { | |
513 | struct mtd_oob_region oobregion; | |
514 | int i, section = 0, ret; | |
515 | ||
516 | if (!mtd || !to) | |
517 | return -EINVAL; | |
518 | ||
519 | memset(to, 0, sizeof(*to)); | |
520 | ||
521 | to->eccbytes = 0; | |
522 | for (i = 0; i < ARRAY_SIZE(to->eccpos);) { | |
523 | u32 eccpos; | |
524 | ||
6de56493 | 525 | ret = mtd_ooblayout_ecc(mtd, section++, &oobregion); |
c2b78452 BB |
526 | if (ret < 0) { |
527 | if (ret != -ERANGE) | |
528 | return ret; | |
529 | ||
cc26c3cd | 530 | break; |
c2b78452 BB |
531 | } |
532 | ||
533 | if (oobregion.length + i > ARRAY_SIZE(to->eccpos)) | |
534 | return -EINVAL; | |
535 | ||
536 | eccpos = oobregion.offset; | |
537 | for (; eccpos < oobregion.offset + oobregion.length; i++) { | |
538 | to->eccpos[i] = eccpos++; | |
539 | to->eccbytes++; | |
540 | } | |
cc26c3cd BN |
541 | } |
542 | ||
c2b78452 BB |
543 | for (i = 0; i < 8; i++) { |
544 | ret = mtd_ooblayout_free(mtd, i, &oobregion); | |
545 | if (ret < 0) { | |
546 | if (ret != -ERANGE) | |
547 | return ret; | |
548 | ||
549 | break; | |
550 | } | |
551 | ||
552 | to->oobfree[i][0] = oobregion.offset; | |
553 | to->oobfree[i][1] = oobregion.length; | |
554 | } | |
555 | ||
556 | to->useecc = MTD_NANDECC_AUTOPLACE; | |
557 | ||
cc26c3cd BN |
558 | return 0; |
559 | } | |
560 | ||
969e57ad | 561 | static int mtdchar_blkpg_ioctl(struct mtd_info *mtd, |
53bb724f | 562 | struct blkpg_ioctl_arg *arg) |
d0f7959e | 563 | { |
d0f7959e RT |
564 | struct blkpg_partition p; |
565 | ||
566 | if (!capable(CAP_SYS_ADMIN)) | |
567 | return -EPERM; | |
568 | ||
53bb724f | 569 | if (copy_from_user(&p, arg->data, sizeof(p))) |
d0f7959e RT |
570 | return -EFAULT; |
571 | ||
53bb724f | 572 | switch (arg->op) { |
d0f7959e RT |
573 | case BLKPG_ADD_PARTITION: |
574 | ||
a7e93dcd RT |
575 | /* Only master mtd device must be used to add partitions */ |
576 | if (mtd_is_partition(mtd)) | |
577 | return -EINVAL; | |
578 | ||
1cc8d841 BN |
579 | /* Sanitize user input */ |
580 | p.devname[BLKPG_DEVNAMELTH - 1] = '\0'; | |
581 | ||
d0f7959e RT |
582 | return mtd_add_partition(mtd, p.devname, p.start, p.length); |
583 | ||
584 | case BLKPG_DEL_PARTITION: | |
585 | ||
586 | if (p.pno < 0) | |
587 | return -EINVAL; | |
588 | ||
589 | return mtd_del_partition(mtd, p.pno); | |
590 | ||
591 | default: | |
592 | return -EINVAL; | |
593 | } | |
594 | } | |
d0f7959e | 595 | |
969e57ad | 596 | static int mtdchar_write_ioctl(struct mtd_info *mtd, |
e99d8b08 BN |
597 | struct mtd_write_req __user *argp) |
598 | { | |
599 | struct mtd_write_req req; | |
600 | struct mtd_oob_ops ops; | |
f62cde49 | 601 | const void __user *usr_data, *usr_oob; |
e99d8b08 BN |
602 | int ret; |
603 | ||
f62cde49 | 604 | if (copy_from_user(&req, argp, sizeof(req))) |
e99d8b08 | 605 | return -EFAULT; |
f62cde49 GU |
606 | |
607 | usr_data = (const void __user *)(uintptr_t)req.usr_data; | |
608 | usr_oob = (const void __user *)(uintptr_t)req.usr_oob; | |
f62cde49 | 609 | |
3c3c10bb | 610 | if (!mtd->_write_oob) |
e99d8b08 BN |
611 | return -EOPNOTSUPP; |
612 | ||
613 | ops.mode = req.mode; | |
614 | ops.len = (size_t)req.len; | |
615 | ops.ooblen = (size_t)req.ooblen; | |
616 | ops.ooboffs = 0; | |
617 | ||
f62cde49 | 618 | if (usr_data) { |
e99d8b08 BN |
619 | ops.datbuf = memdup_user(usr_data, ops.len); |
620 | if (IS_ERR(ops.datbuf)) | |
621 | return PTR_ERR(ops.datbuf); | |
622 | } else { | |
623 | ops.datbuf = NULL; | |
624 | } | |
625 | ||
f62cde49 | 626 | if (usr_oob) { |
e99d8b08 BN |
627 | ops.oobbuf = memdup_user(usr_oob, ops.ooblen); |
628 | if (IS_ERR(ops.oobbuf)) { | |
629 | kfree(ops.datbuf); | |
630 | return PTR_ERR(ops.oobbuf); | |
631 | } | |
632 | } else { | |
633 | ops.oobbuf = NULL; | |
634 | } | |
635 | ||
a2cc5ba0 | 636 | ret = mtd_write_oob(mtd, (loff_t)req.start, &ops); |
e99d8b08 BN |
637 | |
638 | kfree(ops.datbuf); | |
639 | kfree(ops.oobbuf); | |
640 | ||
641 | return ret; | |
642 | } | |
643 | ||
969e57ad | 644 | static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg) |
1da177e4 | 645 | { |
f1a28c02 TG |
646 | struct mtd_file_info *mfi = file->private_data; |
647 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
648 | void __user *argp = (void __user *)arg; |
649 | int ret = 0; | |
73c619ea | 650 | struct mtd_info_user info; |
97894cda | 651 | |
289c0522 | 652 | pr_debug("MTD_ioctl\n"); |
1da177e4 | 653 | |
1da177e4 LT |
654 | switch (cmd) { |
655 | case MEMGETREGIONCOUNT: | |
656 | if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) | |
657 | return -EFAULT; | |
658 | break; | |
659 | ||
660 | case MEMGETREGIONINFO: | |
661 | { | |
b67c5f87 ZW |
662 | uint32_t ur_idx; |
663 | struct mtd_erase_region_info *kr; | |
bcc98a46 | 664 | struct region_info_user __user *ur = argp; |
1da177e4 | 665 | |
b67c5f87 | 666 | if (get_user(ur_idx, &(ur->regionindex))) |
1da177e4 LT |
667 | return -EFAULT; |
668 | ||
5e59be1f DC |
669 | if (ur_idx >= mtd->numeraseregions) |
670 | return -EINVAL; | |
671 | ||
b67c5f87 ZW |
672 | kr = &(mtd->eraseregions[ur_idx]); |
673 | ||
674 | if (put_user(kr->offset, &(ur->offset)) | |
675 | || put_user(kr->erasesize, &(ur->erasesize)) | |
676 | || put_user(kr->numblocks, &(ur->numblocks))) | |
1da177e4 | 677 | return -EFAULT; |
b67c5f87 | 678 | |
1da177e4 LT |
679 | break; |
680 | } | |
681 | ||
682 | case MEMGETINFO: | |
a0c5a394 | 683 | memset(&info, 0, sizeof(info)); |
73c619ea JE |
684 | info.type = mtd->type; |
685 | info.flags = mtd->flags; | |
686 | info.size = mtd->size; | |
687 | info.erasesize = mtd->erasesize; | |
688 | info.writesize = mtd->writesize; | |
689 | info.oobsize = mtd->oobsize; | |
19fb4341 BN |
690 | /* The below field is obsolete */ |
691 | info.padding = 0; | |
73c619ea | 692 | if (copy_to_user(argp, &info, sizeof(struct mtd_info_user))) |
1da177e4 LT |
693 | return -EFAULT; |
694 | break; | |
695 | ||
696 | case MEMERASE: | |
0dc54e9f | 697 | case MEMERASE64: |
1da177e4 LT |
698 | { |
699 | struct erase_info *erase; | |
700 | ||
aeb5d727 | 701 | if(!(file->f_mode & FMODE_WRITE)) |
1da177e4 LT |
702 | return -EPERM; |
703 | ||
95b93a0c | 704 | erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL); |
1da177e4 LT |
705 | if (!erase) |
706 | ret = -ENOMEM; | |
707 | else { | |
0dc54e9f KC |
708 | if (cmd == MEMERASE64) { |
709 | struct erase_info_user64 einfo64; | |
710 | ||
711 | if (copy_from_user(&einfo64, argp, | |
712 | sizeof(struct erase_info_user64))) { | |
713 | kfree(erase); | |
714 | return -EFAULT; | |
715 | } | |
716 | erase->addr = einfo64.start; | |
717 | erase->len = einfo64.length; | |
718 | } else { | |
719 | struct erase_info_user einfo32; | |
720 | ||
721 | if (copy_from_user(&einfo32, argp, | |
722 | sizeof(struct erase_info_user))) { | |
723 | kfree(erase); | |
724 | return -EFAULT; | |
725 | } | |
726 | erase->addr = einfo32.start; | |
727 | erase->len = einfo32.length; | |
1da177e4 | 728 | } |
884cfd90 | 729 | |
7e1f0dc0 | 730 | ret = mtd_erase(mtd, erase); |
1da177e4 LT |
731 | kfree(erase); |
732 | } | |
733 | break; | |
734 | } | |
735 | ||
736 | case MEMWRITEOOB: | |
737 | { | |
738 | struct mtd_oob_buf buf; | |
97718540 | 739 | struct mtd_oob_buf __user *buf_user = argp; |
1da177e4 | 740 | |
97718540 KC |
741 | /* NOTE: writes return length to buf_user->length */ |
742 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1da177e4 | 743 | ret = -EFAULT; |
97718540 | 744 | else |
969e57ad | 745 | ret = mtdchar_writeoob(file, mtd, buf.start, buf.length, |
97718540 | 746 | buf.ptr, &buf_user->length); |
1da177e4 | 747 | break; |
1da177e4 LT |
748 | } |
749 | ||
750 | case MEMREADOOB: | |
751 | { | |
752 | struct mtd_oob_buf buf; | |
97718540 | 753 | struct mtd_oob_buf __user *buf_user = argp; |
8593fbc6 | 754 | |
97718540 KC |
755 | /* NOTE: writes return length to buf_user->start */ |
756 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1da177e4 | 757 | ret = -EFAULT; |
97718540 | 758 | else |
969e57ad | 759 | ret = mtdchar_readoob(file, mtd, buf.start, buf.length, |
97718540 | 760 | buf.ptr, &buf_user->start); |
1da177e4 LT |
761 | break; |
762 | } | |
763 | ||
aea7cea9 KC |
764 | case MEMWRITEOOB64: |
765 | { | |
766 | struct mtd_oob_buf64 buf; | |
767 | struct mtd_oob_buf64 __user *buf_user = argp; | |
768 | ||
769 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
770 | ret = -EFAULT; | |
771 | else | |
969e57ad | 772 | ret = mtdchar_writeoob(file, mtd, buf.start, buf.length, |
aea7cea9 KC |
773 | (void __user *)(uintptr_t)buf.usr_ptr, |
774 | &buf_user->length); | |
775 | break; | |
776 | } | |
777 | ||
778 | case MEMREADOOB64: | |
779 | { | |
780 | struct mtd_oob_buf64 buf; | |
781 | struct mtd_oob_buf64 __user *buf_user = argp; | |
782 | ||
783 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
784 | ret = -EFAULT; | |
785 | else | |
969e57ad | 786 | ret = mtdchar_readoob(file, mtd, buf.start, buf.length, |
aea7cea9 KC |
787 | (void __user *)(uintptr_t)buf.usr_ptr, |
788 | &buf_user->length); | |
789 | break; | |
790 | } | |
791 | ||
e99d8b08 BN |
792 | case MEMWRITE: |
793 | { | |
969e57ad | 794 | ret = mtdchar_write_ioctl(mtd, |
e99d8b08 BN |
795 | (struct mtd_write_req __user *)arg); |
796 | break; | |
797 | } | |
798 | ||
1da177e4 LT |
799 | case MEMLOCK: |
800 | { | |
175428b2 | 801 | struct erase_info_user einfo; |
1da177e4 | 802 | |
175428b2 | 803 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
1da177e4 LT |
804 | return -EFAULT; |
805 | ||
38134565 | 806 | ret = mtd_lock(mtd, einfo.start, einfo.length); |
1da177e4 LT |
807 | break; |
808 | } | |
809 | ||
810 | case MEMUNLOCK: | |
811 | { | |
175428b2 | 812 | struct erase_info_user einfo; |
1da177e4 | 813 | |
175428b2 | 814 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
1da177e4 LT |
815 | return -EFAULT; |
816 | ||
38134565 | 817 | ret = mtd_unlock(mtd, einfo.start, einfo.length); |
1da177e4 LT |
818 | break; |
819 | } | |
820 | ||
9938424f RC |
821 | case MEMISLOCKED: |
822 | { | |
823 | struct erase_info_user einfo; | |
824 | ||
825 | if (copy_from_user(&einfo, argp, sizeof(einfo))) | |
826 | return -EFAULT; | |
827 | ||
38134565 | 828 | ret = mtd_is_locked(mtd, einfo.start, einfo.length); |
9938424f RC |
829 | break; |
830 | } | |
831 | ||
5bd34c09 | 832 | /* Legacy interface */ |
1da177e4 LT |
833 | case MEMGETOOBSEL: |
834 | { | |
5bd34c09 TG |
835 | struct nand_oobinfo oi; |
836 | ||
adbbc3bc | 837 | if (!mtd->ooblayout) |
5bd34c09 | 838 | return -EOPNOTSUPP; |
5bd34c09 | 839 | |
c2b78452 BB |
840 | ret = get_oobinfo(mtd, &oi); |
841 | if (ret) | |
842 | return ret; | |
5bd34c09 TG |
843 | |
844 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) | |
1da177e4 LT |
845 | return -EFAULT; |
846 | break; | |
847 | } | |
848 | ||
849 | case MEMGETBADBLOCK: | |
850 | { | |
851 | loff_t offs; | |
97894cda | 852 | |
1da177e4 LT |
853 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
854 | return -EFAULT; | |
8f461a73 | 855 | return mtd_block_isbad(mtd, offs); |
1da177e4 LT |
856 | break; |
857 | } | |
858 | ||
859 | case MEMSETBADBLOCK: | |
860 | { | |
861 | loff_t offs; | |
862 | ||
863 | if (copy_from_user(&offs, argp, sizeof(loff_t))) | |
864 | return -EFAULT; | |
800ffd34 | 865 | return mtd_block_markbad(mtd, offs); |
1da177e4 LT |
866 | break; |
867 | } | |
868 | ||
31f4233b NP |
869 | case OTPSELECT: |
870 | { | |
871 | int mode; | |
872 | if (copy_from_user(&mode, argp, sizeof(int))) | |
873 | return -EFAULT; | |
f1a28c02 | 874 | |
beb133fc | 875 | mfi->mode = MTD_FILE_MODE_NORMAL; |
f1a28c02 TG |
876 | |
877 | ret = otp_select_filemode(mfi, mode); | |
878 | ||
81dba488 | 879 | file->f_pos = 0; |
31f4233b NP |
880 | break; |
881 | } | |
882 | ||
883 | case OTPGETREGIONCOUNT: | |
884 | case OTPGETREGIONINFO: | |
885 | { | |
886 | struct otp_info *buf = kmalloc(4096, GFP_KERNEL); | |
4b78fc42 | 887 | size_t retlen; |
31f4233b NP |
888 | if (!buf) |
889 | return -ENOMEM; | |
f1a28c02 | 890 | switch (mfi->mode) { |
beb133fc | 891 | case MTD_FILE_MODE_OTP_FACTORY: |
4b78fc42 | 892 | ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf); |
31f4233b | 893 | break; |
beb133fc | 894 | case MTD_FILE_MODE_OTP_USER: |
4b78fc42 | 895 | ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf); |
31f4233b | 896 | break; |
f1a28c02 | 897 | default: |
87e858a9 | 898 | ret = -EINVAL; |
f1a28c02 | 899 | break; |
31f4233b | 900 | } |
4b78fc42 | 901 | if (!ret) { |
31f4233b | 902 | if (cmd == OTPGETREGIONCOUNT) { |
4b78fc42 | 903 | int nbr = retlen / sizeof(struct otp_info); |
31f4233b NP |
904 | ret = copy_to_user(argp, &nbr, sizeof(int)); |
905 | } else | |
4b78fc42 | 906 | ret = copy_to_user(argp, buf, retlen); |
31f4233b NP |
907 | if (ret) |
908 | ret = -EFAULT; | |
909 | } | |
910 | kfree(buf); | |
911 | break; | |
912 | } | |
913 | ||
914 | case OTPLOCK: | |
915 | { | |
175428b2 | 916 | struct otp_info oinfo; |
31f4233b | 917 | |
beb133fc | 918 | if (mfi->mode != MTD_FILE_MODE_OTP_USER) |
31f4233b | 919 | return -EINVAL; |
175428b2 | 920 | if (copy_from_user(&oinfo, argp, sizeof(oinfo))) |
31f4233b | 921 | return -EFAULT; |
4403dbfb | 922 | ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length); |
31f4233b NP |
923 | break; |
924 | } | |
31f4233b | 925 | |
7854d3f7 | 926 | /* This ioctl is being deprecated - it truncates the ECC layout */ |
f1a28c02 TG |
927 | case ECCGETLAYOUT: |
928 | { | |
cc26c3cd BN |
929 | struct nand_ecclayout_user *usrlay; |
930 | ||
adbbc3bc | 931 | if (!mtd->ooblayout) |
f1a28c02 TG |
932 | return -EOPNOTSUPP; |
933 | ||
cc26c3cd BN |
934 | usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL); |
935 | if (!usrlay) | |
936 | return -ENOMEM; | |
937 | ||
c2b78452 | 938 | shrink_ecclayout(mtd, usrlay); |
cc26c3cd BN |
939 | |
940 | if (copy_to_user(argp, usrlay, sizeof(*usrlay))) | |
941 | ret = -EFAULT; | |
942 | kfree(usrlay); | |
f1a28c02 TG |
943 | break; |
944 | } | |
945 | ||
946 | case ECCGETSTATS: | |
947 | { | |
948 | if (copy_to_user(argp, &mtd->ecc_stats, | |
949 | sizeof(struct mtd_ecc_stats))) | |
950 | return -EFAULT; | |
951 | break; | |
952 | } | |
953 | ||
954 | case MTDFILEMODE: | |
955 | { | |
956 | mfi->mode = 0; | |
957 | ||
958 | switch(arg) { | |
beb133fc BN |
959 | case MTD_FILE_MODE_OTP_FACTORY: |
960 | case MTD_FILE_MODE_OTP_USER: | |
f1a28c02 TG |
961 | ret = otp_select_filemode(mfi, arg); |
962 | break; | |
963 | ||
beb133fc | 964 | case MTD_FILE_MODE_RAW: |
fc002e3c | 965 | if (!mtd_has_oob(mtd)) |
f1a28c02 TG |
966 | return -EOPNOTSUPP; |
967 | mfi->mode = arg; | |
968 | ||
beb133fc | 969 | case MTD_FILE_MODE_NORMAL: |
f1a28c02 TG |
970 | break; |
971 | default: | |
972 | ret = -EINVAL; | |
973 | } | |
974 | file->f_pos = 0; | |
975 | break; | |
976 | } | |
977 | ||
d0f7959e RT |
978 | case BLKPG: |
979 | { | |
53bb724f BN |
980 | struct blkpg_ioctl_arg __user *blk_arg = argp; |
981 | struct blkpg_ioctl_arg a; | |
982 | ||
983 | if (copy_from_user(&a, blk_arg, sizeof(a))) | |
984 | ret = -EFAULT; | |
985 | else | |
986 | ret = mtdchar_blkpg_ioctl(mtd, &a); | |
d0f7959e RT |
987 | break; |
988 | } | |
989 | ||
990 | case BLKRRPART: | |
991 | { | |
992 | /* No reread partition feature. Just return ok */ | |
993 | ret = 0; | |
994 | break; | |
995 | } | |
d0f7959e | 996 | |
1da177e4 LT |
997 | default: |
998 | ret = -ENOTTY; | |
999 | } | |
1000 | ||
1001 | return ret; | |
1002 | } /* memory_ioctl */ | |
1003 | ||
969e57ad | 1004 | static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg) |
55929332 AB |
1005 | { |
1006 | int ret; | |
1007 | ||
5aa82940 | 1008 | mutex_lock(&mtd_mutex); |
969e57ad | 1009 | ret = mtdchar_ioctl(file, cmd, arg); |
5aa82940 | 1010 | mutex_unlock(&mtd_mutex); |
55929332 AB |
1011 | |
1012 | return ret; | |
1013 | } | |
1014 | ||
97718540 KC |
1015 | #ifdef CONFIG_COMPAT |
1016 | ||
1017 | struct mtd_oob_buf32 { | |
1018 | u_int32_t start; | |
1019 | u_int32_t length; | |
1020 | compat_caddr_t ptr; /* unsigned char* */ | |
1021 | }; | |
1022 | ||
1023 | #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32) | |
1024 | #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32) | |
1025 | ||
969e57ad | 1026 | static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd, |
97718540 KC |
1027 | unsigned long arg) |
1028 | { | |
1029 | struct mtd_file_info *mfi = file->private_data; | |
1030 | struct mtd_info *mtd = mfi->mtd; | |
0b6585ce | 1031 | void __user *argp = compat_ptr(arg); |
97718540 KC |
1032 | int ret = 0; |
1033 | ||
5aa82940 | 1034 | mutex_lock(&mtd_mutex); |
97718540 KC |
1035 | |
1036 | switch (cmd) { | |
1037 | case MEMWRITEOOB32: | |
1038 | { | |
1039 | struct mtd_oob_buf32 buf; | |
1040 | struct mtd_oob_buf32 __user *buf_user = argp; | |
1041 | ||
1042 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1043 | ret = -EFAULT; | |
1044 | else | |
969e57ad | 1045 | ret = mtdchar_writeoob(file, mtd, buf.start, |
97718540 KC |
1046 | buf.length, compat_ptr(buf.ptr), |
1047 | &buf_user->length); | |
1048 | break; | |
1049 | } | |
1050 | ||
1051 | case MEMREADOOB32: | |
1052 | { | |
1053 | struct mtd_oob_buf32 buf; | |
1054 | struct mtd_oob_buf32 __user *buf_user = argp; | |
1055 | ||
1056 | /* NOTE: writes return length to buf->start */ | |
1057 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1058 | ret = -EFAULT; | |
1059 | else | |
969e57ad | 1060 | ret = mtdchar_readoob(file, mtd, buf.start, |
97718540 KC |
1061 | buf.length, compat_ptr(buf.ptr), |
1062 | &buf_user->start); | |
1063 | break; | |
1064 | } | |
53bb724f BN |
1065 | |
1066 | case BLKPG: | |
1067 | { | |
1068 | /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */ | |
1069 | struct blkpg_compat_ioctl_arg __user *uarg = argp; | |
1070 | struct blkpg_compat_ioctl_arg compat_arg; | |
1071 | struct blkpg_ioctl_arg a; | |
1072 | ||
1073 | if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) { | |
1074 | ret = -EFAULT; | |
1075 | break; | |
1076 | } | |
1077 | ||
1078 | memset(&a, 0, sizeof(a)); | |
1079 | a.op = compat_arg.op; | |
1080 | a.flags = compat_arg.flags; | |
1081 | a.datalen = compat_arg.datalen; | |
1082 | a.data = compat_ptr(compat_arg.data); | |
1083 | ||
1084 | ret = mtdchar_blkpg_ioctl(mtd, &a); | |
1085 | break; | |
1086 | } | |
1087 | ||
97718540 | 1088 | default: |
969e57ad | 1089 | ret = mtdchar_ioctl(file, cmd, (unsigned long)argp); |
97718540 KC |
1090 | } |
1091 | ||
5aa82940 | 1092 | mutex_unlock(&mtd_mutex); |
97718540 KC |
1093 | |
1094 | return ret; | |
1095 | } | |
1096 | ||
1097 | #endif /* CONFIG_COMPAT */ | |
1098 | ||
402d3265 DH |
1099 | /* |
1100 | * try to determine where a shared mapping can be made | |
1101 | * - only supported for NOMMU at the moment (MMU can't doesn't copy private | |
1102 | * mappings) | |
1103 | */ | |
1104 | #ifndef CONFIG_MMU | |
969e57ad | 1105 | static unsigned long mtdchar_get_unmapped_area(struct file *file, |
402d3265 DH |
1106 | unsigned long addr, |
1107 | unsigned long len, | |
1108 | unsigned long pgoff, | |
1109 | unsigned long flags) | |
1110 | { | |
1111 | struct mtd_file_info *mfi = file->private_data; | |
1112 | struct mtd_info *mtd = mfi->mtd; | |
cd621274 AB |
1113 | unsigned long offset; |
1114 | int ret; | |
402d3265 | 1115 | |
cd621274 AB |
1116 | if (addr != 0) |
1117 | return (unsigned long) -EINVAL; | |
402d3265 | 1118 | |
cd621274 AB |
1119 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) |
1120 | return (unsigned long) -EINVAL; | |
402d3265 | 1121 | |
cd621274 AB |
1122 | offset = pgoff << PAGE_SHIFT; |
1123 | if (offset > mtd->size - len) | |
1124 | return (unsigned long) -EINVAL; | |
402d3265 | 1125 | |
cd621274 | 1126 | ret = mtd_get_unmapped_area(mtd, len, offset, flags); |
b9995932 | 1127 | return ret == -EOPNOTSUPP ? -ENODEV : ret; |
402d3265 | 1128 | } |
b4caecd4 CH |
1129 | |
1130 | static unsigned mtdchar_mmap_capabilities(struct file *file) | |
1131 | { | |
1132 | struct mtd_file_info *mfi = file->private_data; | |
1133 | ||
1134 | return mtd_mmap_capabilities(mfi->mtd); | |
1135 | } | |
402d3265 DH |
1136 | #endif |
1137 | ||
1138 | /* | |
1139 | * set up a mapping for shared memory segments | |
1140 | */ | |
969e57ad | 1141 | static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma) |
402d3265 DH |
1142 | { |
1143 | #ifdef CONFIG_MMU | |
1144 | struct mtd_file_info *mfi = file->private_data; | |
1145 | struct mtd_info *mtd = mfi->mtd; | |
dd02b67d | 1146 | struct map_info *map = mtd->priv; |
dd02b67d | 1147 | |
f5cf8f07 DW |
1148 | /* This is broken because it assumes the MTD device is map-based |
1149 | and that mtd->priv is a valid struct map_info. It should be | |
1150 | replaced with something that uses the mtd_get_unmapped_area() | |
1151 | operation properly. */ | |
1152 | if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) { | |
dd02b67d | 1153 | #ifdef pgprot_noncached |
8558e4a2 | 1154 | if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory)) |
dd02b67d AG |
1155 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
1156 | #endif | |
8558e4a2 | 1157 | return vm_iomap_memory(vma, map->phys, map->size); |
dd02b67d | 1158 | } |
b9995932 | 1159 | return -ENODEV; |
402d3265 | 1160 | #else |
b9995932 | 1161 | return vma->vm_flags & VM_SHARED ? 0 : -EACCES; |
402d3265 DH |
1162 | #endif |
1163 | } | |
1164 | ||
d54b1fdb | 1165 | static const struct file_operations mtd_fops = { |
1da177e4 | 1166 | .owner = THIS_MODULE, |
969e57ad AB |
1167 | .llseek = mtdchar_lseek, |
1168 | .read = mtdchar_read, | |
1169 | .write = mtdchar_write, | |
1170 | .unlocked_ioctl = mtdchar_unlocked_ioctl, | |
97718540 | 1171 | #ifdef CONFIG_COMPAT |
969e57ad | 1172 | .compat_ioctl = mtdchar_compat_ioctl, |
97718540 | 1173 | #endif |
969e57ad AB |
1174 | .open = mtdchar_open, |
1175 | .release = mtdchar_close, | |
1176 | .mmap = mtdchar_mmap, | |
402d3265 | 1177 | #ifndef CONFIG_MMU |
969e57ad | 1178 | .get_unmapped_area = mtdchar_get_unmapped_area, |
b4caecd4 | 1179 | .mmap_capabilities = mtdchar_mmap_capabilities, |
402d3265 | 1180 | #endif |
1da177e4 LT |
1181 | }; |
1182 | ||
660685d9 | 1183 | int __init init_mtdchar(void) |
1da177e4 | 1184 | { |
cd874237 | 1185 | int ret; |
1f24b5a8 | 1186 | |
cd874237 | 1187 | ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, |
dad0db31 | 1188 | "mtd", &mtd_fops); |
cd874237 | 1189 | if (ret < 0) { |
57ae2b60 AB |
1190 | pr_err("Can't allocate major number %d for MTD\n", |
1191 | MTD_CHAR_MAJOR); | |
cd874237 | 1192 | return ret; |
9bc7b387 TP |
1193 | } |
1194 | ||
cd874237 | 1195 | return ret; |
1da177e4 LT |
1196 | } |
1197 | ||
660685d9 | 1198 | void __exit cleanup_mtdchar(void) |
1da177e4 | 1199 | { |
dad0db31 | 1200 | __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd"); |
1da177e4 LT |
1201 | } |
1202 | ||
90160e13 | 1203 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |