]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Character-device access to raw MTD devices. |
3 | * | |
4 | */ | |
5 | ||
15fdc52f TG |
6 | #include <linux/device.h> |
7 | #include <linux/fs.h> | |
0c1eafdb | 8 | #include <linux/mm.h> |
9c74034f | 9 | #include <linux/err.h> |
15fdc52f | 10 | #include <linux/init.h> |
1da177e4 LT |
11 | #include <linux/kernel.h> |
12 | #include <linux/module.h> | |
15fdc52f TG |
13 | #include <linux/slab.h> |
14 | #include <linux/sched.h> | |
6071239e | 15 | #include <linux/smp_lock.h> |
402d3265 | 16 | #include <linux/backing-dev.h> |
97718540 | 17 | #include <linux/compat.h> |
15fdc52f | 18 | |
1da177e4 LT |
19 | #include <linux/mtd/mtd.h> |
20 | #include <linux/mtd/compatmac.h> | |
1da177e4 | 21 | |
15fdc52f | 22 | #include <asm/uaccess.h> |
9bc7b387 | 23 | |
1da177e4 | 24 | |
045e9a5d | 25 | /* |
f1a28c02 TG |
26 | * Data structure to hold the pointer to the mtd device as well |
27 | * as mode information ofr various use cases. | |
045e9a5d | 28 | */ |
f1a28c02 TG |
29 | struct mtd_file_info { |
30 | struct mtd_info *mtd; | |
31 | enum mtd_file_modes mode; | |
32 | }; | |
31f4233b | 33 | |
1da177e4 LT |
34 | static loff_t mtd_lseek (struct file *file, loff_t offset, int orig) |
35 | { | |
f1a28c02 TG |
36 | struct mtd_file_info *mfi = file->private_data; |
37 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
38 | |
39 | switch (orig) { | |
ea59830d | 40 | case SEEK_SET: |
1da177e4 | 41 | break; |
ea59830d | 42 | case SEEK_CUR: |
8b491d75 | 43 | offset += file->f_pos; |
1da177e4 | 44 | break; |
ea59830d | 45 | case SEEK_END: |
8b491d75 | 46 | offset += mtd->size; |
1da177e4 LT |
47 | break; |
48 | default: | |
49 | return -EINVAL; | |
50 | } | |
51 | ||
1887f517 | 52 | if (offset >= 0 && offset <= mtd->size) |
8b491d75 | 53 | return file->f_pos = offset; |
1da177e4 | 54 | |
8b491d75 | 55 | return -EINVAL; |
1da177e4 LT |
56 | } |
57 | ||
58 | ||
59 | ||
60 | static int mtd_open(struct inode *inode, struct file *file) | |
61 | { | |
62 | int minor = iminor(inode); | |
63 | int devnum = minor >> 1; | |
6071239e | 64 | int ret = 0; |
1da177e4 | 65 | struct mtd_info *mtd; |
f1a28c02 | 66 | struct mtd_file_info *mfi; |
1da177e4 LT |
67 | |
68 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n"); | |
69 | ||
1da177e4 | 70 | /* You can't open the RO devices RW */ |
aeb5d727 | 71 | if ((file->f_mode & FMODE_WRITE) && (minor & 1)) |
1da177e4 LT |
72 | return -EACCES; |
73 | ||
6071239e | 74 | lock_kernel(); |
1da177e4 | 75 | mtd = get_mtd_device(NULL, devnum); |
97894cda | 76 | |
6071239e JC |
77 | if (IS_ERR(mtd)) { |
78 | ret = PTR_ERR(mtd); | |
79 | goto out; | |
80 | } | |
97894cda | 81 | |
402d3265 | 82 | if (mtd->type == MTD_ABSENT) { |
1da177e4 | 83 | put_mtd_device(mtd); |
6071239e JC |
84 | ret = -ENODEV; |
85 | goto out; | |
1da177e4 LT |
86 | } |
87 | ||
402d3265 DH |
88 | if (mtd->backing_dev_info) |
89 | file->f_mapping->backing_dev_info = mtd->backing_dev_info; | |
90 | ||
1da177e4 | 91 | /* You can't open it RW if it's not a writeable device */ |
aeb5d727 | 92 | if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) { |
1da177e4 | 93 | put_mtd_device(mtd); |
6071239e JC |
94 | ret = -EACCES; |
95 | goto out; | |
1da177e4 | 96 | } |
97894cda | 97 | |
f1a28c02 TG |
98 | mfi = kzalloc(sizeof(*mfi), GFP_KERNEL); |
99 | if (!mfi) { | |
100 | put_mtd_device(mtd); | |
6071239e JC |
101 | ret = -ENOMEM; |
102 | goto out; | |
f1a28c02 TG |
103 | } |
104 | mfi->mtd = mtd; | |
105 | file->private_data = mfi; | |
106 | ||
6071239e JC |
107 | out: |
108 | unlock_kernel(); | |
109 | return ret; | |
1da177e4 LT |
110 | } /* mtd_open */ |
111 | ||
112 | /*====================================================================*/ | |
113 | ||
114 | static int mtd_close(struct inode *inode, struct file *file) | |
115 | { | |
f1a28c02 TG |
116 | struct mtd_file_info *mfi = file->private_data; |
117 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
118 | |
119 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n"); | |
120 | ||
7eafaed5 | 121 | /* Only sync if opened RW */ |
aeb5d727 | 122 | if ((file->f_mode & FMODE_WRITE) && mtd->sync) |
1da177e4 | 123 | mtd->sync(mtd); |
97894cda | 124 | |
1da177e4 | 125 | put_mtd_device(mtd); |
f1a28c02 TG |
126 | file->private_data = NULL; |
127 | kfree(mfi); | |
1da177e4 LT |
128 | |
129 | return 0; | |
130 | } /* mtd_close */ | |
131 | ||
132 | /* FIXME: This _really_ needs to die. In 2.5, we should lock the | |
133 | userspace buffer down and use it directly with readv/writev. | |
134 | */ | |
135 | #define MAX_KMALLOC_SIZE 0x20000 | |
136 | ||
137 | static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos) | |
138 | { | |
f1a28c02 TG |
139 | struct mtd_file_info *mfi = file->private_data; |
140 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
141 | size_t retlen=0; |
142 | size_t total_retlen=0; | |
143 | int ret=0; | |
144 | int len; | |
145 | char *kbuf; | |
97894cda | 146 | |
1da177e4 LT |
147 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n"); |
148 | ||
149 | if (*ppos + count > mtd->size) | |
150 | count = mtd->size - *ppos; | |
151 | ||
152 | if (!count) | |
153 | return 0; | |
97894cda | 154 | |
1da177e4 LT |
155 | /* FIXME: Use kiovec in 2.5 to lock down the user's buffers |
156 | and pass them directly to the MTD functions */ | |
b802c074 TG |
157 | |
158 | if (count > MAX_KMALLOC_SIZE) | |
159 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); | |
160 | else | |
161 | kbuf=kmalloc(count, GFP_KERNEL); | |
162 | ||
163 | if (!kbuf) | |
164 | return -ENOMEM; | |
165 | ||
1da177e4 | 166 | while (count) { |
b802c074 | 167 | |
97894cda | 168 | if (count > MAX_KMALLOC_SIZE) |
1da177e4 LT |
169 | len = MAX_KMALLOC_SIZE; |
170 | else | |
171 | len = count; | |
172 | ||
f1a28c02 TG |
173 | switch (mfi->mode) { |
174 | case MTD_MODE_OTP_FACTORY: | |
31f4233b NP |
175 | ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf); |
176 | break; | |
177 | case MTD_MODE_OTP_USER: | |
178 | ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
179 | break; | |
f1a28c02 TG |
180 | case MTD_MODE_RAW: |
181 | { | |
182 | struct mtd_oob_ops ops; | |
183 | ||
184 | ops.mode = MTD_OOB_RAW; | |
185 | ops.datbuf = kbuf; | |
186 | ops.oobbuf = NULL; | |
187 | ops.len = len; | |
188 | ||
189 | ret = mtd->read_oob(mtd, *ppos, &ops); | |
190 | retlen = ops.retlen; | |
191 | break; | |
192 | } | |
31f4233b | 193 | default: |
f4a43cfc | 194 | ret = mtd->read(mtd, *ppos, len, &retlen, kbuf); |
31f4233b | 195 | } |
1da177e4 LT |
196 | /* Nand returns -EBADMSG on ecc errors, but it returns |
197 | * the data. For our userspace tools it is important | |
97894cda | 198 | * to dump areas with ecc errors ! |
9a1fcdfd TG |
199 | * For kernel internal usage it also might return -EUCLEAN |
200 | * to signal the caller that a bitflip has occured and has | |
201 | * been corrected by the ECC algorithm. | |
1da177e4 LT |
202 | * Userspace software which accesses NAND this way |
203 | * must be aware of the fact that it deals with NAND | |
204 | */ | |
9a1fcdfd | 205 | if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) { |
1da177e4 LT |
206 | *ppos += retlen; |
207 | if (copy_to_user(buf, kbuf, retlen)) { | |
f4a43cfc | 208 | kfree(kbuf); |
1da177e4 LT |
209 | return -EFAULT; |
210 | } | |
211 | else | |
212 | total_retlen += retlen; | |
213 | ||
214 | count -= retlen; | |
215 | buf += retlen; | |
31f4233b NP |
216 | if (retlen == 0) |
217 | count = 0; | |
1da177e4 LT |
218 | } |
219 | else { | |
220 | kfree(kbuf); | |
221 | return ret; | |
222 | } | |
97894cda | 223 | |
1da177e4 LT |
224 | } |
225 | ||
b802c074 | 226 | kfree(kbuf); |
1da177e4 LT |
227 | return total_retlen; |
228 | } /* mtd_read */ | |
229 | ||
230 | static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos) | |
231 | { | |
f1a28c02 TG |
232 | struct mtd_file_info *mfi = file->private_data; |
233 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
234 | char *kbuf; |
235 | size_t retlen; | |
236 | size_t total_retlen=0; | |
237 | int ret=0; | |
238 | int len; | |
239 | ||
240 | DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n"); | |
97894cda | 241 | |
1da177e4 LT |
242 | if (*ppos == mtd->size) |
243 | return -ENOSPC; | |
97894cda | 244 | |
1da177e4 LT |
245 | if (*ppos + count > mtd->size) |
246 | count = mtd->size - *ppos; | |
247 | ||
248 | if (!count) | |
249 | return 0; | |
250 | ||
b802c074 TG |
251 | if (count > MAX_KMALLOC_SIZE) |
252 | kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL); | |
253 | else | |
254 | kbuf=kmalloc(count, GFP_KERNEL); | |
255 | ||
256 | if (!kbuf) | |
257 | return -ENOMEM; | |
258 | ||
1da177e4 | 259 | while (count) { |
b802c074 | 260 | |
97894cda | 261 | if (count > MAX_KMALLOC_SIZE) |
1da177e4 LT |
262 | len = MAX_KMALLOC_SIZE; |
263 | else | |
264 | len = count; | |
265 | ||
1da177e4 LT |
266 | if (copy_from_user(kbuf, buf, len)) { |
267 | kfree(kbuf); | |
268 | return -EFAULT; | |
269 | } | |
97894cda | 270 | |
f1a28c02 TG |
271 | switch (mfi->mode) { |
272 | case MTD_MODE_OTP_FACTORY: | |
31f4233b NP |
273 | ret = -EROFS; |
274 | break; | |
275 | case MTD_MODE_OTP_USER: | |
276 | if (!mtd->write_user_prot_reg) { | |
277 | ret = -EOPNOTSUPP; | |
278 | break; | |
279 | } | |
280 | ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf); | |
281 | break; | |
f1a28c02 TG |
282 | |
283 | case MTD_MODE_RAW: | |
284 | { | |
285 | struct mtd_oob_ops ops; | |
286 | ||
287 | ops.mode = MTD_OOB_RAW; | |
288 | ops.datbuf = kbuf; | |
289 | ops.oobbuf = NULL; | |
290 | ops.len = len; | |
291 | ||
292 | ret = mtd->write_oob(mtd, *ppos, &ops); | |
293 | retlen = ops.retlen; | |
294 | break; | |
295 | } | |
296 | ||
31f4233b NP |
297 | default: |
298 | ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf); | |
299 | } | |
1da177e4 LT |
300 | if (!ret) { |
301 | *ppos += retlen; | |
302 | total_retlen += retlen; | |
303 | count -= retlen; | |
304 | buf += retlen; | |
305 | } | |
306 | else { | |
307 | kfree(kbuf); | |
308 | return ret; | |
309 | } | |
1da177e4 LT |
310 | } |
311 | ||
b802c074 | 312 | kfree(kbuf); |
1da177e4 LT |
313 | return total_retlen; |
314 | } /* mtd_write */ | |
315 | ||
316 | /*====================================================================== | |
317 | ||
318 | IOCTL calls for getting device parameters. | |
319 | ||
320 | ======================================================================*/ | |
321 | static void mtdchar_erase_callback (struct erase_info *instr) | |
322 | { | |
323 | wake_up((wait_queue_head_t *)instr->priv); | |
324 | } | |
325 | ||
34a82443 | 326 | #ifdef CONFIG_HAVE_MTD_OTP |
f1a28c02 TG |
327 | static int otp_select_filemode(struct mtd_file_info *mfi, int mode) |
328 | { | |
329 | struct mtd_info *mtd = mfi->mtd; | |
330 | int ret = 0; | |
331 | ||
332 | switch (mode) { | |
333 | case MTD_OTP_FACTORY: | |
334 | if (!mtd->read_fact_prot_reg) | |
335 | ret = -EOPNOTSUPP; | |
336 | else | |
337 | mfi->mode = MTD_MODE_OTP_FACTORY; | |
338 | break; | |
339 | case MTD_OTP_USER: | |
340 | if (!mtd->read_fact_prot_reg) | |
341 | ret = -EOPNOTSUPP; | |
342 | else | |
343 | mfi->mode = MTD_MODE_OTP_USER; | |
344 | break; | |
345 | default: | |
346 | ret = -EINVAL; | |
347 | case MTD_OTP_OFF: | |
348 | break; | |
349 | } | |
350 | return ret; | |
351 | } | |
352 | #else | |
353 | # define otp_select_filemode(f,m) -EOPNOTSUPP | |
354 | #endif | |
355 | ||
97718540 KC |
356 | static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd, |
357 | uint64_t start, uint32_t length, void __user *ptr, | |
358 | uint32_t __user *retp) | |
359 | { | |
360 | struct mtd_oob_ops ops; | |
361 | uint32_t retlen; | |
362 | int ret = 0; | |
363 | ||
364 | if (!(file->f_mode & FMODE_WRITE)) | |
365 | return -EPERM; | |
366 | ||
367 | if (length > 4096) | |
368 | return -EINVAL; | |
369 | ||
370 | if (!mtd->write_oob) | |
371 | ret = -EOPNOTSUPP; | |
372 | else | |
0040476b | 373 | ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT; |
97718540 KC |
374 | |
375 | if (ret) | |
376 | return ret; | |
377 | ||
378 | ops.ooblen = length; | |
379 | ops.ooboffs = start & (mtd->oobsize - 1); | |
380 | ops.datbuf = NULL; | |
381 | ops.mode = MTD_OOB_PLACE; | |
382 | ||
383 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) | |
384 | return -EINVAL; | |
385 | ||
386 | ops.oobbuf = kmalloc(length, GFP_KERNEL); | |
387 | if (!ops.oobbuf) | |
388 | return -ENOMEM; | |
389 | ||
390 | if (copy_from_user(ops.oobbuf, ptr, length)) { | |
391 | kfree(ops.oobbuf); | |
392 | return -EFAULT; | |
393 | } | |
394 | ||
395 | start &= ~((uint64_t)mtd->oobsize - 1); | |
396 | ret = mtd->write_oob(mtd, start, &ops); | |
397 | ||
398 | if (ops.oobretlen > 0xFFFFFFFFU) | |
399 | ret = -EOVERFLOW; | |
400 | retlen = ops.oobretlen; | |
401 | if (copy_to_user(retp, &retlen, sizeof(length))) | |
402 | ret = -EFAULT; | |
403 | ||
404 | kfree(ops.oobbuf); | |
405 | return ret; | |
406 | } | |
407 | ||
408 | static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start, | |
409 | uint32_t length, void __user *ptr, uint32_t __user *retp) | |
410 | { | |
411 | struct mtd_oob_ops ops; | |
412 | int ret = 0; | |
413 | ||
414 | if (length > 4096) | |
415 | return -EINVAL; | |
416 | ||
417 | if (!mtd->read_oob) | |
418 | ret = -EOPNOTSUPP; | |
419 | else | |
420 | ret = access_ok(VERIFY_WRITE, ptr, | |
421 | length) ? 0 : -EFAULT; | |
422 | if (ret) | |
423 | return ret; | |
424 | ||
425 | ops.ooblen = length; | |
426 | ops.ooboffs = start & (mtd->oobsize - 1); | |
427 | ops.datbuf = NULL; | |
428 | ops.mode = MTD_OOB_PLACE; | |
429 | ||
430 | if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs)) | |
431 | return -EINVAL; | |
432 | ||
433 | ops.oobbuf = kmalloc(length, GFP_KERNEL); | |
434 | if (!ops.oobbuf) | |
435 | return -ENOMEM; | |
436 | ||
437 | start &= ~((uint64_t)mtd->oobsize - 1); | |
438 | ret = mtd->read_oob(mtd, start, &ops); | |
439 | ||
440 | if (put_user(ops.oobretlen, retp)) | |
441 | ret = -EFAULT; | |
442 | else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf, | |
443 | ops.oobretlen)) | |
444 | ret = -EFAULT; | |
445 | ||
446 | kfree(ops.oobbuf); | |
447 | return ret; | |
448 | } | |
449 | ||
1da177e4 LT |
450 | static int mtd_ioctl(struct inode *inode, struct file *file, |
451 | u_int cmd, u_long arg) | |
452 | { | |
f1a28c02 TG |
453 | struct mtd_file_info *mfi = file->private_data; |
454 | struct mtd_info *mtd = mfi->mtd; | |
1da177e4 LT |
455 | void __user *argp = (void __user *)arg; |
456 | int ret = 0; | |
457 | u_long size; | |
73c619ea | 458 | struct mtd_info_user info; |
97894cda | 459 | |
1da177e4 LT |
460 | DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n"); |
461 | ||
462 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; | |
463 | if (cmd & IOC_IN) { | |
464 | if (!access_ok(VERIFY_READ, argp, size)) | |
465 | return -EFAULT; | |
466 | } | |
467 | if (cmd & IOC_OUT) { | |
468 | if (!access_ok(VERIFY_WRITE, argp, size)) | |
469 | return -EFAULT; | |
470 | } | |
97894cda | 471 | |
1da177e4 LT |
472 | switch (cmd) { |
473 | case MEMGETREGIONCOUNT: | |
474 | if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) | |
475 | return -EFAULT; | |
476 | break; | |
477 | ||
478 | case MEMGETREGIONINFO: | |
479 | { | |
b67c5f87 ZW |
480 | uint32_t ur_idx; |
481 | struct mtd_erase_region_info *kr; | |
bcc98a46 | 482 | struct region_info_user __user *ur = argp; |
1da177e4 | 483 | |
b67c5f87 | 484 | if (get_user(ur_idx, &(ur->regionindex))) |
1da177e4 LT |
485 | return -EFAULT; |
486 | ||
b67c5f87 ZW |
487 | kr = &(mtd->eraseregions[ur_idx]); |
488 | ||
489 | if (put_user(kr->offset, &(ur->offset)) | |
490 | || put_user(kr->erasesize, &(ur->erasesize)) | |
491 | || put_user(kr->numblocks, &(ur->numblocks))) | |
1da177e4 | 492 | return -EFAULT; |
b67c5f87 | 493 | |
1da177e4 LT |
494 | break; |
495 | } | |
496 | ||
497 | case MEMGETINFO: | |
73c619ea JE |
498 | info.type = mtd->type; |
499 | info.flags = mtd->flags; | |
500 | info.size = mtd->size; | |
501 | info.erasesize = mtd->erasesize; | |
502 | info.writesize = mtd->writesize; | |
503 | info.oobsize = mtd->oobsize; | |
64f60710 AB |
504 | /* The below fields are obsolete */ |
505 | info.ecctype = -1; | |
506 | info.eccsize = 0; | |
73c619ea | 507 | if (copy_to_user(argp, &info, sizeof(struct mtd_info_user))) |
1da177e4 LT |
508 | return -EFAULT; |
509 | break; | |
510 | ||
511 | case MEMERASE: | |
0dc54e9f | 512 | case MEMERASE64: |
1da177e4 LT |
513 | { |
514 | struct erase_info *erase; | |
515 | ||
aeb5d727 | 516 | if(!(file->f_mode & FMODE_WRITE)) |
1da177e4 LT |
517 | return -EPERM; |
518 | ||
95b93a0c | 519 | erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL); |
1da177e4 LT |
520 | if (!erase) |
521 | ret = -ENOMEM; | |
522 | else { | |
523 | wait_queue_head_t waitq; | |
524 | DECLARE_WAITQUEUE(wait, current); | |
525 | ||
526 | init_waitqueue_head(&waitq); | |
527 | ||
0dc54e9f KC |
528 | if (cmd == MEMERASE64) { |
529 | struct erase_info_user64 einfo64; | |
530 | ||
531 | if (copy_from_user(&einfo64, argp, | |
532 | sizeof(struct erase_info_user64))) { | |
533 | kfree(erase); | |
534 | return -EFAULT; | |
535 | } | |
536 | erase->addr = einfo64.start; | |
537 | erase->len = einfo64.length; | |
538 | } else { | |
539 | struct erase_info_user einfo32; | |
540 | ||
541 | if (copy_from_user(&einfo32, argp, | |
542 | sizeof(struct erase_info_user))) { | |
543 | kfree(erase); | |
544 | return -EFAULT; | |
545 | } | |
546 | erase->addr = einfo32.start; | |
547 | erase->len = einfo32.length; | |
1da177e4 LT |
548 | } |
549 | erase->mtd = mtd; | |
550 | erase->callback = mtdchar_erase_callback; | |
551 | erase->priv = (unsigned long)&waitq; | |
97894cda | 552 | |
1da177e4 LT |
553 | /* |
554 | FIXME: Allow INTERRUPTIBLE. Which means | |
555 | not having the wait_queue head on the stack. | |
97894cda | 556 | |
1da177e4 LT |
557 | If the wq_head is on the stack, and we |
558 | leave because we got interrupted, then the | |
559 | wq_head is no longer there when the | |
560 | callback routine tries to wake us up. | |
561 | */ | |
562 | ret = mtd->erase(mtd, erase); | |
563 | if (!ret) { | |
564 | set_current_state(TASK_UNINTERRUPTIBLE); | |
565 | add_wait_queue(&waitq, &wait); | |
566 | if (erase->state != MTD_ERASE_DONE && | |
567 | erase->state != MTD_ERASE_FAILED) | |
568 | schedule(); | |
569 | remove_wait_queue(&waitq, &wait); | |
570 | set_current_state(TASK_RUNNING); | |
571 | ||
572 | ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0; | |
573 | } | |
574 | kfree(erase); | |
575 | } | |
576 | break; | |
577 | } | |
578 | ||
579 | case MEMWRITEOOB: | |
580 | { | |
581 | struct mtd_oob_buf buf; | |
97718540 | 582 | struct mtd_oob_buf __user *buf_user = argp; |
1da177e4 | 583 | |
97718540 KC |
584 | /* NOTE: writes return length to buf_user->length */ |
585 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1da177e4 | 586 | ret = -EFAULT; |
97718540 KC |
587 | else |
588 | ret = mtd_do_writeoob(file, mtd, buf.start, buf.length, | |
589 | buf.ptr, &buf_user->length); | |
1da177e4 | 590 | break; |
1da177e4 LT |
591 | } |
592 | ||
593 | case MEMREADOOB: | |
594 | { | |
595 | struct mtd_oob_buf buf; | |
97718540 | 596 | struct mtd_oob_buf __user *buf_user = argp; |
8593fbc6 | 597 | |
97718540 KC |
598 | /* NOTE: writes return length to buf_user->start */ |
599 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
1da177e4 | 600 | ret = -EFAULT; |
97718540 KC |
601 | else |
602 | ret = mtd_do_readoob(mtd, buf.start, buf.length, | |
603 | buf.ptr, &buf_user->start); | |
1da177e4 LT |
604 | break; |
605 | } | |
606 | ||
aea7cea9 KC |
607 | case MEMWRITEOOB64: |
608 | { | |
609 | struct mtd_oob_buf64 buf; | |
610 | struct mtd_oob_buf64 __user *buf_user = argp; | |
611 | ||
612 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
613 | ret = -EFAULT; | |
614 | else | |
615 | ret = mtd_do_writeoob(file, mtd, buf.start, buf.length, | |
616 | (void __user *)(uintptr_t)buf.usr_ptr, | |
617 | &buf_user->length); | |
618 | break; | |
619 | } | |
620 | ||
621 | case MEMREADOOB64: | |
622 | { | |
623 | struct mtd_oob_buf64 buf; | |
624 | struct mtd_oob_buf64 __user *buf_user = argp; | |
625 | ||
626 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
627 | ret = -EFAULT; | |
628 | else | |
629 | ret = mtd_do_readoob(mtd, buf.start, buf.length, | |
630 | (void __user *)(uintptr_t)buf.usr_ptr, | |
631 | &buf_user->length); | |
632 | break; | |
633 | } | |
634 | ||
1da177e4 LT |
635 | case MEMLOCK: |
636 | { | |
175428b2 | 637 | struct erase_info_user einfo; |
1da177e4 | 638 | |
175428b2 | 639 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
1da177e4 LT |
640 | return -EFAULT; |
641 | ||
642 | if (!mtd->lock) | |
643 | ret = -EOPNOTSUPP; | |
644 | else | |
175428b2 | 645 | ret = mtd->lock(mtd, einfo.start, einfo.length); |
1da177e4 LT |
646 | break; |
647 | } | |
648 | ||
649 | case MEMUNLOCK: | |
650 | { | |
175428b2 | 651 | struct erase_info_user einfo; |
1da177e4 | 652 | |
175428b2 | 653 | if (copy_from_user(&einfo, argp, sizeof(einfo))) |
1da177e4 LT |
654 | return -EFAULT; |
655 | ||
656 | if (!mtd->unlock) | |
657 | ret = -EOPNOTSUPP; | |
658 | else | |
175428b2 | 659 | ret = mtd->unlock(mtd, einfo.start, einfo.length); |
1da177e4 LT |
660 | break; |
661 | } | |
662 | ||
5bd34c09 | 663 | /* Legacy interface */ |
1da177e4 LT |
664 | case MEMGETOOBSEL: |
665 | { | |
5bd34c09 TG |
666 | struct nand_oobinfo oi; |
667 | ||
668 | if (!mtd->ecclayout) | |
669 | return -EOPNOTSUPP; | |
670 | if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos)) | |
671 | return -EINVAL; | |
672 | ||
673 | oi.useecc = MTD_NANDECC_AUTOPLACE; | |
674 | memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos)); | |
675 | memcpy(&oi.oobfree, mtd->ecclayout->oobfree, | |
676 | sizeof(oi.oobfree)); | |
d25ade71 | 677 | oi.eccbytes = mtd->ecclayout->eccbytes; |
5bd34c09 TG |
678 | |
679 | if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo))) | |
1da177e4 LT |
680 | return -EFAULT; |
681 | break; | |
682 | } | |
683 | ||
684 | case MEMGETBADBLOCK: | |
685 | { | |
686 | loff_t offs; | |
97894cda | 687 | |
1da177e4 LT |
688 | if (copy_from_user(&offs, argp, sizeof(loff_t))) |
689 | return -EFAULT; | |
690 | if (!mtd->block_isbad) | |
691 | ret = -EOPNOTSUPP; | |
692 | else | |
693 | return mtd->block_isbad(mtd, offs); | |
694 | break; | |
695 | } | |
696 | ||
697 | case MEMSETBADBLOCK: | |
698 | { | |
699 | loff_t offs; | |
700 | ||
701 | if (copy_from_user(&offs, argp, sizeof(loff_t))) | |
702 | return -EFAULT; | |
703 | if (!mtd->block_markbad) | |
704 | ret = -EOPNOTSUPP; | |
705 | else | |
706 | return mtd->block_markbad(mtd, offs); | |
707 | break; | |
708 | } | |
709 | ||
34a82443 | 710 | #ifdef CONFIG_HAVE_MTD_OTP |
31f4233b NP |
711 | case OTPSELECT: |
712 | { | |
713 | int mode; | |
714 | if (copy_from_user(&mode, argp, sizeof(int))) | |
715 | return -EFAULT; | |
f1a28c02 TG |
716 | |
717 | mfi->mode = MTD_MODE_NORMAL; | |
718 | ||
719 | ret = otp_select_filemode(mfi, mode); | |
720 | ||
81dba488 | 721 | file->f_pos = 0; |
31f4233b NP |
722 | break; |
723 | } | |
724 | ||
725 | case OTPGETREGIONCOUNT: | |
726 | case OTPGETREGIONINFO: | |
727 | { | |
728 | struct otp_info *buf = kmalloc(4096, GFP_KERNEL); | |
729 | if (!buf) | |
730 | return -ENOMEM; | |
731 | ret = -EOPNOTSUPP; | |
f1a28c02 TG |
732 | switch (mfi->mode) { |
733 | case MTD_MODE_OTP_FACTORY: | |
31f4233b NP |
734 | if (mtd->get_fact_prot_info) |
735 | ret = mtd->get_fact_prot_info(mtd, buf, 4096); | |
736 | break; | |
737 | case MTD_MODE_OTP_USER: | |
738 | if (mtd->get_user_prot_info) | |
739 | ret = mtd->get_user_prot_info(mtd, buf, 4096); | |
740 | break; | |
f1a28c02 TG |
741 | default: |
742 | break; | |
31f4233b NP |
743 | } |
744 | if (ret >= 0) { | |
745 | if (cmd == OTPGETREGIONCOUNT) { | |
746 | int nbr = ret / sizeof(struct otp_info); | |
747 | ret = copy_to_user(argp, &nbr, sizeof(int)); | |
748 | } else | |
749 | ret = copy_to_user(argp, buf, ret); | |
750 | if (ret) | |
751 | ret = -EFAULT; | |
752 | } | |
753 | kfree(buf); | |
754 | break; | |
755 | } | |
756 | ||
757 | case OTPLOCK: | |
758 | { | |
175428b2 | 759 | struct otp_info oinfo; |
31f4233b | 760 | |
f1a28c02 | 761 | if (mfi->mode != MTD_MODE_OTP_USER) |
31f4233b | 762 | return -EINVAL; |
175428b2 | 763 | if (copy_from_user(&oinfo, argp, sizeof(oinfo))) |
31f4233b NP |
764 | return -EFAULT; |
765 | if (!mtd->lock_user_prot_reg) | |
766 | return -EOPNOTSUPP; | |
175428b2 | 767 | ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length); |
31f4233b NP |
768 | break; |
769 | } | |
770 | #endif | |
771 | ||
f1a28c02 TG |
772 | case ECCGETLAYOUT: |
773 | { | |
774 | if (!mtd->ecclayout) | |
775 | return -EOPNOTSUPP; | |
776 | ||
d25ade71 | 777 | if (copy_to_user(argp, mtd->ecclayout, |
f1a28c02 TG |
778 | sizeof(struct nand_ecclayout))) |
779 | return -EFAULT; | |
780 | break; | |
781 | } | |
782 | ||
783 | case ECCGETSTATS: | |
784 | { | |
785 | if (copy_to_user(argp, &mtd->ecc_stats, | |
786 | sizeof(struct mtd_ecc_stats))) | |
787 | return -EFAULT; | |
788 | break; | |
789 | } | |
790 | ||
791 | case MTDFILEMODE: | |
792 | { | |
793 | mfi->mode = 0; | |
794 | ||
795 | switch(arg) { | |
796 | case MTD_MODE_OTP_FACTORY: | |
797 | case MTD_MODE_OTP_USER: | |
798 | ret = otp_select_filemode(mfi, arg); | |
799 | break; | |
800 | ||
801 | case MTD_MODE_RAW: | |
802 | if (!mtd->read_oob || !mtd->write_oob) | |
803 | return -EOPNOTSUPP; | |
804 | mfi->mode = arg; | |
805 | ||
806 | case MTD_MODE_NORMAL: | |
807 | break; | |
808 | default: | |
809 | ret = -EINVAL; | |
810 | } | |
811 | file->f_pos = 0; | |
812 | break; | |
813 | } | |
814 | ||
1da177e4 LT |
815 | default: |
816 | ret = -ENOTTY; | |
817 | } | |
818 | ||
819 | return ret; | |
820 | } /* memory_ioctl */ | |
821 | ||
97718540 KC |
822 | #ifdef CONFIG_COMPAT |
823 | ||
824 | struct mtd_oob_buf32 { | |
825 | u_int32_t start; | |
826 | u_int32_t length; | |
827 | compat_caddr_t ptr; /* unsigned char* */ | |
828 | }; | |
829 | ||
830 | #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32) | |
831 | #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32) | |
832 | ||
833 | static long mtd_compat_ioctl(struct file *file, unsigned int cmd, | |
834 | unsigned long arg) | |
835 | { | |
668ff9ab | 836 | struct inode *inode = file->f_path.dentry->d_inode; |
97718540 KC |
837 | struct mtd_file_info *mfi = file->private_data; |
838 | struct mtd_info *mtd = mfi->mtd; | |
0b6585ce | 839 | void __user *argp = compat_ptr(arg); |
97718540 KC |
840 | int ret = 0; |
841 | ||
842 | lock_kernel(); | |
843 | ||
844 | switch (cmd) { | |
845 | case MEMWRITEOOB32: | |
846 | { | |
847 | struct mtd_oob_buf32 buf; | |
848 | struct mtd_oob_buf32 __user *buf_user = argp; | |
849 | ||
850 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
851 | ret = -EFAULT; | |
852 | else | |
853 | ret = mtd_do_writeoob(file, mtd, buf.start, | |
854 | buf.length, compat_ptr(buf.ptr), | |
855 | &buf_user->length); | |
856 | break; | |
857 | } | |
858 | ||
859 | case MEMREADOOB32: | |
860 | { | |
861 | struct mtd_oob_buf32 buf; | |
862 | struct mtd_oob_buf32 __user *buf_user = argp; | |
863 | ||
864 | /* NOTE: writes return length to buf->start */ | |
865 | if (copy_from_user(&buf, argp, sizeof(buf))) | |
866 | ret = -EFAULT; | |
867 | else | |
868 | ret = mtd_do_readoob(mtd, buf.start, | |
869 | buf.length, compat_ptr(buf.ptr), | |
870 | &buf_user->start); | |
871 | break; | |
872 | } | |
873 | default: | |
0b6585ce | 874 | ret = mtd_ioctl(inode, file, cmd, (unsigned long)argp); |
97718540 KC |
875 | } |
876 | ||
877 | unlock_kernel(); | |
878 | ||
879 | return ret; | |
880 | } | |
881 | ||
882 | #endif /* CONFIG_COMPAT */ | |
883 | ||
402d3265 DH |
884 | /* |
885 | * try to determine where a shared mapping can be made | |
886 | * - only supported for NOMMU at the moment (MMU can't doesn't copy private | |
887 | * mappings) | |
888 | */ | |
889 | #ifndef CONFIG_MMU | |
890 | static unsigned long mtd_get_unmapped_area(struct file *file, | |
891 | unsigned long addr, | |
892 | unsigned long len, | |
893 | unsigned long pgoff, | |
894 | unsigned long flags) | |
895 | { | |
896 | struct mtd_file_info *mfi = file->private_data; | |
897 | struct mtd_info *mtd = mfi->mtd; | |
898 | ||
899 | if (mtd->get_unmapped_area) { | |
900 | unsigned long offset; | |
901 | ||
902 | if (addr != 0) | |
903 | return (unsigned long) -EINVAL; | |
904 | ||
905 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) | |
906 | return (unsigned long) -EINVAL; | |
907 | ||
908 | offset = pgoff << PAGE_SHIFT; | |
909 | if (offset > mtd->size - len) | |
910 | return (unsigned long) -EINVAL; | |
911 | ||
912 | return mtd->get_unmapped_area(mtd, len, offset, flags); | |
913 | } | |
914 | ||
915 | /* can't map directly */ | |
916 | return (unsigned long) -ENOSYS; | |
917 | } | |
918 | #endif | |
919 | ||
920 | /* | |
921 | * set up a mapping for shared memory segments | |
922 | */ | |
923 | static int mtd_mmap(struct file *file, struct vm_area_struct *vma) | |
924 | { | |
925 | #ifdef CONFIG_MMU | |
926 | struct mtd_file_info *mfi = file->private_data; | |
927 | struct mtd_info *mtd = mfi->mtd; | |
928 | ||
929 | if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) | |
930 | return 0; | |
931 | return -ENOSYS; | |
932 | #else | |
933 | return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS; | |
934 | #endif | |
935 | } | |
936 | ||
d54b1fdb | 937 | static const struct file_operations mtd_fops = { |
1da177e4 LT |
938 | .owner = THIS_MODULE, |
939 | .llseek = mtd_lseek, | |
940 | .read = mtd_read, | |
941 | .write = mtd_write, | |
942 | .ioctl = mtd_ioctl, | |
97718540 KC |
943 | #ifdef CONFIG_COMPAT |
944 | .compat_ioctl = mtd_compat_ioctl, | |
945 | #endif | |
1da177e4 LT |
946 | .open = mtd_open, |
947 | .release = mtd_close, | |
402d3265 DH |
948 | .mmap = mtd_mmap, |
949 | #ifndef CONFIG_MMU | |
950 | .get_unmapped_area = mtd_get_unmapped_area, | |
951 | #endif | |
1da177e4 LT |
952 | }; |
953 | ||
954 | static int __init init_mtdchar(void) | |
955 | { | |
1f24b5a8 DB |
956 | int status; |
957 | ||
958 | status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops); | |
959 | if (status < 0) { | |
1da177e4 LT |
960 | printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n", |
961 | MTD_CHAR_MAJOR); | |
9bc7b387 TP |
962 | } |
963 | ||
1f24b5a8 | 964 | return status; |
1da177e4 LT |
965 | } |
966 | ||
967 | static void __exit cleanup_mtdchar(void) | |
968 | { | |
1da177e4 LT |
969 | unregister_chrdev(MTD_CHAR_MAJOR, "mtd"); |
970 | } | |
971 | ||
972 | module_init(init_mtdchar); | |
973 | module_exit(cleanup_mtdchar); | |
974 | ||
1f24b5a8 | 975 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |
1da177e4 LT |
976 | |
977 | MODULE_LICENSE("GPL"); | |
978 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
979 | MODULE_DESCRIPTION("Direct character-device access to MTD devices"); | |
90160e13 | 980 | MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR); |