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