quota: Refactor dquot_transfer code so that OCFS2 can pass in its references
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / mtdchar.c
blob8bb5e4a663284b5e0fbbf1e617ec80813d3d71c9
1 /*
2 * Character-device access to raw MTD devices.
4 */
6 #include <linux/device.h>
7 #include <linux/fs.h>
8 #include <linux/mm.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/backing-dev.h>
17 #include <linux/compat.h>
18 #include <linux/mount.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/compatmac.h>
23 #include <asm/uaccess.h>
25 #define MTD_INODE_FS_MAGIC 0x11307854
26 static struct vfsmount *mtd_inode_mnt __read_mostly;
29 * Data structure to hold the pointer to the mtd device as well
30 * as mode information ofr various use cases.
32 struct mtd_file_info {
33 struct mtd_info *mtd;
34 struct inode *ino;
35 enum mtd_file_modes mode;
38 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
40 struct mtd_file_info *mfi = file->private_data;
41 struct mtd_info *mtd = mfi->mtd;
43 switch (orig) {
44 case SEEK_SET:
45 break;
46 case SEEK_CUR:
47 offset += file->f_pos;
48 break;
49 case SEEK_END:
50 offset += mtd->size;
51 break;
52 default:
53 return -EINVAL;
56 if (offset >= 0 && offset <= mtd->size)
57 return file->f_pos = offset;
59 return -EINVAL;
64 static int mtd_open(struct inode *inode, struct file *file)
66 int minor = iminor(inode);
67 int devnum = minor >> 1;
68 int ret = 0;
69 struct mtd_info *mtd;
70 struct mtd_file_info *mfi;
71 struct inode *mtd_ino;
73 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
75 /* You can't open the RO devices RW */
76 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
77 return -EACCES;
79 lock_kernel();
80 mtd = get_mtd_device(NULL, devnum);
82 if (IS_ERR(mtd)) {
83 ret = PTR_ERR(mtd);
84 goto out;
87 if (mtd->type == MTD_ABSENT) {
88 put_mtd_device(mtd);
89 ret = -ENODEV;
90 goto out;
93 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
94 if (!mtd_ino) {
95 put_mtd_device(mtd);
96 ret = -ENOMEM;
97 goto out;
99 if (mtd_ino->i_state & I_NEW) {
100 mtd_ino->i_private = mtd;
101 mtd_ino->i_mode = S_IFCHR;
102 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
103 unlock_new_inode(mtd_ino);
105 file->f_mapping = mtd_ino->i_mapping;
107 /* You can't open it RW if it's not a writeable device */
108 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
109 iput(mtd_ino);
110 put_mtd_device(mtd);
111 ret = -EACCES;
112 goto out;
115 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
116 if (!mfi) {
117 iput(mtd_ino);
118 put_mtd_device(mtd);
119 ret = -ENOMEM;
120 goto out;
122 mfi->ino = mtd_ino;
123 mfi->mtd = mtd;
124 file->private_data = mfi;
126 out:
127 unlock_kernel();
128 return ret;
129 } /* mtd_open */
131 /*====================================================================*/
133 static int mtd_close(struct inode *inode, struct file *file)
135 struct mtd_file_info *mfi = file->private_data;
136 struct mtd_info *mtd = mfi->mtd;
138 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
140 /* Only sync if opened RW */
141 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
142 mtd->sync(mtd);
144 iput(mfi->ino);
146 put_mtd_device(mtd);
147 file->private_data = NULL;
148 kfree(mfi);
150 return 0;
151 } /* mtd_close */
153 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
154 userspace buffer down and use it directly with readv/writev.
156 #define MAX_KMALLOC_SIZE 0x20000
158 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
160 struct mtd_file_info *mfi = file->private_data;
161 struct mtd_info *mtd = mfi->mtd;
162 size_t retlen=0;
163 size_t total_retlen=0;
164 int ret=0;
165 int len;
166 char *kbuf;
168 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
170 if (*ppos + count > mtd->size)
171 count = mtd->size - *ppos;
173 if (!count)
174 return 0;
176 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
177 and pass them directly to the MTD functions */
179 if (count > MAX_KMALLOC_SIZE)
180 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
181 else
182 kbuf=kmalloc(count, GFP_KERNEL);
184 if (!kbuf)
185 return -ENOMEM;
187 while (count) {
189 if (count > MAX_KMALLOC_SIZE)
190 len = MAX_KMALLOC_SIZE;
191 else
192 len = count;
194 switch (mfi->mode) {
195 case MTD_MODE_OTP_FACTORY:
196 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
197 break;
198 case MTD_MODE_OTP_USER:
199 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
200 break;
201 case MTD_MODE_RAW:
203 struct mtd_oob_ops ops;
205 ops.mode = MTD_OOB_RAW;
206 ops.datbuf = kbuf;
207 ops.oobbuf = NULL;
208 ops.len = len;
210 ret = mtd->read_oob(mtd, *ppos, &ops);
211 retlen = ops.retlen;
212 break;
214 default:
215 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
217 /* Nand returns -EBADMSG on ecc errors, but it returns
218 * the data. For our userspace tools it is important
219 * to dump areas with ecc errors !
220 * For kernel internal usage it also might return -EUCLEAN
221 * to signal the caller that a bitflip has occured and has
222 * been corrected by the ECC algorithm.
223 * Userspace software which accesses NAND this way
224 * must be aware of the fact that it deals with NAND
226 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
227 *ppos += retlen;
228 if (copy_to_user(buf, kbuf, retlen)) {
229 kfree(kbuf);
230 return -EFAULT;
232 else
233 total_retlen += retlen;
235 count -= retlen;
236 buf += retlen;
237 if (retlen == 0)
238 count = 0;
240 else {
241 kfree(kbuf);
242 return ret;
247 kfree(kbuf);
248 return total_retlen;
249 } /* mtd_read */
251 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
253 struct mtd_file_info *mfi = file->private_data;
254 struct mtd_info *mtd = mfi->mtd;
255 char *kbuf;
256 size_t retlen;
257 size_t total_retlen=0;
258 int ret=0;
259 int len;
261 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
263 if (*ppos == mtd->size)
264 return -ENOSPC;
266 if (*ppos + count > mtd->size)
267 count = mtd->size - *ppos;
269 if (!count)
270 return 0;
272 if (count > MAX_KMALLOC_SIZE)
273 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
274 else
275 kbuf=kmalloc(count, GFP_KERNEL);
277 if (!kbuf)
278 return -ENOMEM;
280 while (count) {
282 if (count > MAX_KMALLOC_SIZE)
283 len = MAX_KMALLOC_SIZE;
284 else
285 len = count;
287 if (copy_from_user(kbuf, buf, len)) {
288 kfree(kbuf);
289 return -EFAULT;
292 switch (mfi->mode) {
293 case MTD_MODE_OTP_FACTORY:
294 ret = -EROFS;
295 break;
296 case MTD_MODE_OTP_USER:
297 if (!mtd->write_user_prot_reg) {
298 ret = -EOPNOTSUPP;
299 break;
301 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
302 break;
304 case MTD_MODE_RAW:
306 struct mtd_oob_ops ops;
308 ops.mode = MTD_OOB_RAW;
309 ops.datbuf = kbuf;
310 ops.oobbuf = NULL;
311 ops.len = len;
313 ret = mtd->write_oob(mtd, *ppos, &ops);
314 retlen = ops.retlen;
315 break;
318 default:
319 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
321 if (!ret) {
322 *ppos += retlen;
323 total_retlen += retlen;
324 count -= retlen;
325 buf += retlen;
327 else {
328 kfree(kbuf);
329 return ret;
333 kfree(kbuf);
334 return total_retlen;
335 } /* mtd_write */
337 /*======================================================================
339 IOCTL calls for getting device parameters.
341 ======================================================================*/
342 static void mtdchar_erase_callback (struct erase_info *instr)
344 wake_up((wait_queue_head_t *)instr->priv);
347 #ifdef CONFIG_HAVE_MTD_OTP
348 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
350 struct mtd_info *mtd = mfi->mtd;
351 int ret = 0;
353 switch (mode) {
354 case MTD_OTP_FACTORY:
355 if (!mtd->read_fact_prot_reg)
356 ret = -EOPNOTSUPP;
357 else
358 mfi->mode = MTD_MODE_OTP_FACTORY;
359 break;
360 case MTD_OTP_USER:
361 if (!mtd->read_fact_prot_reg)
362 ret = -EOPNOTSUPP;
363 else
364 mfi->mode = MTD_MODE_OTP_USER;
365 break;
366 default:
367 ret = -EINVAL;
368 case MTD_OTP_OFF:
369 break;
371 return ret;
373 #else
374 # define otp_select_filemode(f,m) -EOPNOTSUPP
375 #endif
377 static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
378 uint64_t start, uint32_t length, void __user *ptr,
379 uint32_t __user *retp)
381 struct mtd_oob_ops ops;
382 uint32_t retlen;
383 int ret = 0;
385 if (!(file->f_mode & FMODE_WRITE))
386 return -EPERM;
388 if (length > 4096)
389 return -EINVAL;
391 if (!mtd->write_oob)
392 ret = -EOPNOTSUPP;
393 else
394 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
396 if (ret)
397 return ret;
399 ops.ooblen = length;
400 ops.ooboffs = start & (mtd->oobsize - 1);
401 ops.datbuf = NULL;
402 ops.mode = MTD_OOB_PLACE;
404 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
405 return -EINVAL;
407 ops.oobbuf = kmalloc(length, GFP_KERNEL);
408 if (!ops.oobbuf)
409 return -ENOMEM;
411 if (copy_from_user(ops.oobbuf, ptr, length)) {
412 kfree(ops.oobbuf);
413 return -EFAULT;
416 start &= ~((uint64_t)mtd->oobsize - 1);
417 ret = mtd->write_oob(mtd, start, &ops);
419 if (ops.oobretlen > 0xFFFFFFFFU)
420 ret = -EOVERFLOW;
421 retlen = ops.oobretlen;
422 if (copy_to_user(retp, &retlen, sizeof(length)))
423 ret = -EFAULT;
425 kfree(ops.oobbuf);
426 return ret;
429 static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
430 uint32_t length, void __user *ptr, uint32_t __user *retp)
432 struct mtd_oob_ops ops;
433 int ret = 0;
435 if (length > 4096)
436 return -EINVAL;
438 if (!mtd->read_oob)
439 ret = -EOPNOTSUPP;
440 else
441 ret = access_ok(VERIFY_WRITE, ptr,
442 length) ? 0 : -EFAULT;
443 if (ret)
444 return ret;
446 ops.ooblen = length;
447 ops.ooboffs = start & (mtd->oobsize - 1);
448 ops.datbuf = NULL;
449 ops.mode = MTD_OOB_PLACE;
451 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
452 return -EINVAL;
454 ops.oobbuf = kmalloc(length, GFP_KERNEL);
455 if (!ops.oobbuf)
456 return -ENOMEM;
458 start &= ~((uint64_t)mtd->oobsize - 1);
459 ret = mtd->read_oob(mtd, start, &ops);
461 if (put_user(ops.oobretlen, retp))
462 ret = -EFAULT;
463 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
464 ops.oobretlen))
465 ret = -EFAULT;
467 kfree(ops.oobbuf);
468 return ret;
471 static int mtd_ioctl(struct inode *inode, struct file *file,
472 u_int cmd, u_long arg)
474 struct mtd_file_info *mfi = file->private_data;
475 struct mtd_info *mtd = mfi->mtd;
476 void __user *argp = (void __user *)arg;
477 int ret = 0;
478 u_long size;
479 struct mtd_info_user info;
481 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
483 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
484 if (cmd & IOC_IN) {
485 if (!access_ok(VERIFY_READ, argp, size))
486 return -EFAULT;
488 if (cmd & IOC_OUT) {
489 if (!access_ok(VERIFY_WRITE, argp, size))
490 return -EFAULT;
493 switch (cmd) {
494 case MEMGETREGIONCOUNT:
495 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
496 return -EFAULT;
497 break;
499 case MEMGETREGIONINFO:
501 uint32_t ur_idx;
502 struct mtd_erase_region_info *kr;
503 struct region_info_user __user *ur = argp;
505 if (get_user(ur_idx, &(ur->regionindex)))
506 return -EFAULT;
508 kr = &(mtd->eraseregions[ur_idx]);
510 if (put_user(kr->offset, &(ur->offset))
511 || put_user(kr->erasesize, &(ur->erasesize))
512 || put_user(kr->numblocks, &(ur->numblocks)))
513 return -EFAULT;
515 break;
518 case MEMGETINFO:
519 info.type = mtd->type;
520 info.flags = mtd->flags;
521 info.size = mtd->size;
522 info.erasesize = mtd->erasesize;
523 info.writesize = mtd->writesize;
524 info.oobsize = mtd->oobsize;
525 /* The below fields are obsolete */
526 info.ecctype = -1;
527 info.eccsize = 0;
528 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
529 return -EFAULT;
530 break;
532 case MEMERASE:
533 case MEMERASE64:
535 struct erase_info *erase;
537 if(!(file->f_mode & FMODE_WRITE))
538 return -EPERM;
540 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
541 if (!erase)
542 ret = -ENOMEM;
543 else {
544 wait_queue_head_t waitq;
545 DECLARE_WAITQUEUE(wait, current);
547 init_waitqueue_head(&waitq);
549 if (cmd == MEMERASE64) {
550 struct erase_info_user64 einfo64;
552 if (copy_from_user(&einfo64, argp,
553 sizeof(struct erase_info_user64))) {
554 kfree(erase);
555 return -EFAULT;
557 erase->addr = einfo64.start;
558 erase->len = einfo64.length;
559 } else {
560 struct erase_info_user einfo32;
562 if (copy_from_user(&einfo32, argp,
563 sizeof(struct erase_info_user))) {
564 kfree(erase);
565 return -EFAULT;
567 erase->addr = einfo32.start;
568 erase->len = einfo32.length;
570 erase->mtd = mtd;
571 erase->callback = mtdchar_erase_callback;
572 erase->priv = (unsigned long)&waitq;
575 FIXME: Allow INTERRUPTIBLE. Which means
576 not having the wait_queue head on the stack.
578 If the wq_head is on the stack, and we
579 leave because we got interrupted, then the
580 wq_head is no longer there when the
581 callback routine tries to wake us up.
583 ret = mtd->erase(mtd, erase);
584 if (!ret) {
585 set_current_state(TASK_UNINTERRUPTIBLE);
586 add_wait_queue(&waitq, &wait);
587 if (erase->state != MTD_ERASE_DONE &&
588 erase->state != MTD_ERASE_FAILED)
589 schedule();
590 remove_wait_queue(&waitq, &wait);
591 set_current_state(TASK_RUNNING);
593 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
595 kfree(erase);
597 break;
600 case MEMWRITEOOB:
602 struct mtd_oob_buf buf;
603 struct mtd_oob_buf __user *buf_user = argp;
605 /* NOTE: writes return length to buf_user->length */
606 if (copy_from_user(&buf, argp, sizeof(buf)))
607 ret = -EFAULT;
608 else
609 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
610 buf.ptr, &buf_user->length);
611 break;
614 case MEMREADOOB:
616 struct mtd_oob_buf buf;
617 struct mtd_oob_buf __user *buf_user = argp;
619 /* NOTE: writes return length to buf_user->start */
620 if (copy_from_user(&buf, argp, sizeof(buf)))
621 ret = -EFAULT;
622 else
623 ret = mtd_do_readoob(mtd, buf.start, buf.length,
624 buf.ptr, &buf_user->start);
625 break;
628 case MEMWRITEOOB64:
630 struct mtd_oob_buf64 buf;
631 struct mtd_oob_buf64 __user *buf_user = argp;
633 if (copy_from_user(&buf, argp, sizeof(buf)))
634 ret = -EFAULT;
635 else
636 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
637 (void __user *)(uintptr_t)buf.usr_ptr,
638 &buf_user->length);
639 break;
642 case MEMREADOOB64:
644 struct mtd_oob_buf64 buf;
645 struct mtd_oob_buf64 __user *buf_user = argp;
647 if (copy_from_user(&buf, argp, sizeof(buf)))
648 ret = -EFAULT;
649 else
650 ret = mtd_do_readoob(mtd, buf.start, buf.length,
651 (void __user *)(uintptr_t)buf.usr_ptr,
652 &buf_user->length);
653 break;
656 case MEMLOCK:
658 struct erase_info_user einfo;
660 if (copy_from_user(&einfo, argp, sizeof(einfo)))
661 return -EFAULT;
663 if (!mtd->lock)
664 ret = -EOPNOTSUPP;
665 else
666 ret = mtd->lock(mtd, einfo.start, einfo.length);
667 break;
670 case MEMUNLOCK:
672 struct erase_info_user einfo;
674 if (copy_from_user(&einfo, argp, sizeof(einfo)))
675 return -EFAULT;
677 if (!mtd->unlock)
678 ret = -EOPNOTSUPP;
679 else
680 ret = mtd->unlock(mtd, einfo.start, einfo.length);
681 break;
684 /* Legacy interface */
685 case MEMGETOOBSEL:
687 struct nand_oobinfo oi;
689 if (!mtd->ecclayout)
690 return -EOPNOTSUPP;
691 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
692 return -EINVAL;
694 oi.useecc = MTD_NANDECC_AUTOPLACE;
695 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
696 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
697 sizeof(oi.oobfree));
698 oi.eccbytes = mtd->ecclayout->eccbytes;
700 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
701 return -EFAULT;
702 break;
705 case MEMGETBADBLOCK:
707 loff_t offs;
709 if (copy_from_user(&offs, argp, sizeof(loff_t)))
710 return -EFAULT;
711 if (!mtd->block_isbad)
712 ret = -EOPNOTSUPP;
713 else
714 return mtd->block_isbad(mtd, offs);
715 break;
718 case MEMSETBADBLOCK:
720 loff_t offs;
722 if (copy_from_user(&offs, argp, sizeof(loff_t)))
723 return -EFAULT;
724 if (!mtd->block_markbad)
725 ret = -EOPNOTSUPP;
726 else
727 return mtd->block_markbad(mtd, offs);
728 break;
731 #ifdef CONFIG_HAVE_MTD_OTP
732 case OTPSELECT:
734 int mode;
735 if (copy_from_user(&mode, argp, sizeof(int)))
736 return -EFAULT;
738 mfi->mode = MTD_MODE_NORMAL;
740 ret = otp_select_filemode(mfi, mode);
742 file->f_pos = 0;
743 break;
746 case OTPGETREGIONCOUNT:
747 case OTPGETREGIONINFO:
749 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
750 if (!buf)
751 return -ENOMEM;
752 ret = -EOPNOTSUPP;
753 switch (mfi->mode) {
754 case MTD_MODE_OTP_FACTORY:
755 if (mtd->get_fact_prot_info)
756 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
757 break;
758 case MTD_MODE_OTP_USER:
759 if (mtd->get_user_prot_info)
760 ret = mtd->get_user_prot_info(mtd, buf, 4096);
761 break;
762 default:
763 break;
765 if (ret >= 0) {
766 if (cmd == OTPGETREGIONCOUNT) {
767 int nbr = ret / sizeof(struct otp_info);
768 ret = copy_to_user(argp, &nbr, sizeof(int));
769 } else
770 ret = copy_to_user(argp, buf, ret);
771 if (ret)
772 ret = -EFAULT;
774 kfree(buf);
775 break;
778 case OTPLOCK:
780 struct otp_info oinfo;
782 if (mfi->mode != MTD_MODE_OTP_USER)
783 return -EINVAL;
784 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
785 return -EFAULT;
786 if (!mtd->lock_user_prot_reg)
787 return -EOPNOTSUPP;
788 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
789 break;
791 #endif
793 case ECCGETLAYOUT:
795 if (!mtd->ecclayout)
796 return -EOPNOTSUPP;
798 if (copy_to_user(argp, mtd->ecclayout,
799 sizeof(struct nand_ecclayout)))
800 return -EFAULT;
801 break;
804 case ECCGETSTATS:
806 if (copy_to_user(argp, &mtd->ecc_stats,
807 sizeof(struct mtd_ecc_stats)))
808 return -EFAULT;
809 break;
812 case MTDFILEMODE:
814 mfi->mode = 0;
816 switch(arg) {
817 case MTD_MODE_OTP_FACTORY:
818 case MTD_MODE_OTP_USER:
819 ret = otp_select_filemode(mfi, arg);
820 break;
822 case MTD_MODE_RAW:
823 if (!mtd->read_oob || !mtd->write_oob)
824 return -EOPNOTSUPP;
825 mfi->mode = arg;
827 case MTD_MODE_NORMAL:
828 break;
829 default:
830 ret = -EINVAL;
832 file->f_pos = 0;
833 break;
836 default:
837 ret = -ENOTTY;
840 return ret;
841 } /* memory_ioctl */
843 #ifdef CONFIG_COMPAT
845 struct mtd_oob_buf32 {
846 u_int32_t start;
847 u_int32_t length;
848 compat_caddr_t ptr; /* unsigned char* */
851 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
852 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
854 static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
855 unsigned long arg)
857 struct inode *inode = file->f_path.dentry->d_inode;
858 struct mtd_file_info *mfi = file->private_data;
859 struct mtd_info *mtd = mfi->mtd;
860 void __user *argp = compat_ptr(arg);
861 int ret = 0;
863 lock_kernel();
865 switch (cmd) {
866 case MEMWRITEOOB32:
868 struct mtd_oob_buf32 buf;
869 struct mtd_oob_buf32 __user *buf_user = argp;
871 if (copy_from_user(&buf, argp, sizeof(buf)))
872 ret = -EFAULT;
873 else
874 ret = mtd_do_writeoob(file, mtd, buf.start,
875 buf.length, compat_ptr(buf.ptr),
876 &buf_user->length);
877 break;
880 case MEMREADOOB32:
882 struct mtd_oob_buf32 buf;
883 struct mtd_oob_buf32 __user *buf_user = argp;
885 /* NOTE: writes return length to buf->start */
886 if (copy_from_user(&buf, argp, sizeof(buf)))
887 ret = -EFAULT;
888 else
889 ret = mtd_do_readoob(mtd, buf.start,
890 buf.length, compat_ptr(buf.ptr),
891 &buf_user->start);
892 break;
894 default:
895 ret = mtd_ioctl(inode, file, cmd, (unsigned long)argp);
898 unlock_kernel();
900 return ret;
903 #endif /* CONFIG_COMPAT */
906 * try to determine where a shared mapping can be made
907 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
908 * mappings)
910 #ifndef CONFIG_MMU
911 static unsigned long mtd_get_unmapped_area(struct file *file,
912 unsigned long addr,
913 unsigned long len,
914 unsigned long pgoff,
915 unsigned long flags)
917 struct mtd_file_info *mfi = file->private_data;
918 struct mtd_info *mtd = mfi->mtd;
920 if (mtd->get_unmapped_area) {
921 unsigned long offset;
923 if (addr != 0)
924 return (unsigned long) -EINVAL;
926 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
927 return (unsigned long) -EINVAL;
929 offset = pgoff << PAGE_SHIFT;
930 if (offset > mtd->size - len)
931 return (unsigned long) -EINVAL;
933 return mtd->get_unmapped_area(mtd, len, offset, flags);
936 /* can't map directly */
937 return (unsigned long) -ENOSYS;
939 #endif
942 * set up a mapping for shared memory segments
944 static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
946 #ifdef CONFIG_MMU
947 struct mtd_file_info *mfi = file->private_data;
948 struct mtd_info *mtd = mfi->mtd;
950 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
951 return 0;
952 return -ENOSYS;
953 #else
954 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
955 #endif
958 static const struct file_operations mtd_fops = {
959 .owner = THIS_MODULE,
960 .llseek = mtd_lseek,
961 .read = mtd_read,
962 .write = mtd_write,
963 .ioctl = mtd_ioctl,
964 #ifdef CONFIG_COMPAT
965 .compat_ioctl = mtd_compat_ioctl,
966 #endif
967 .open = mtd_open,
968 .release = mtd_close,
969 .mmap = mtd_mmap,
970 #ifndef CONFIG_MMU
971 .get_unmapped_area = mtd_get_unmapped_area,
972 #endif
975 static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
976 const char *dev_name, void *data,
977 struct vfsmount *mnt)
979 return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
980 mnt);
983 static struct file_system_type mtd_inodefs_type = {
984 .name = "mtd_inodefs",
985 .get_sb = mtd_inodefs_get_sb,
986 .kill_sb = kill_anon_super,
989 static void mtdchar_notify_add(struct mtd_info *mtd)
993 static void mtdchar_notify_remove(struct mtd_info *mtd)
995 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
997 if (mtd_ino) {
998 /* Destroy the inode if it exists */
999 mtd_ino->i_nlink = 0;
1000 iput(mtd_ino);
1004 static struct mtd_notifier mtdchar_notifier = {
1005 .add = mtdchar_notify_add,
1006 .remove = mtdchar_notify_remove,
1009 static int __init init_mtdchar(void)
1011 int ret;
1013 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
1014 "mtd", &mtd_fops);
1015 if (ret < 0) {
1016 pr_notice("Can't allocate major number %d for "
1017 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1018 return ret;
1021 ret = register_filesystem(&mtd_inodefs_type);
1022 if (ret) {
1023 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1024 goto err_unregister_chdev;
1027 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1028 if (IS_ERR(mtd_inode_mnt)) {
1029 ret = PTR_ERR(mtd_inode_mnt);
1030 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1031 goto err_unregister_filesystem;
1033 register_mtd_user(&mtdchar_notifier);
1035 return ret;
1037 err_unregister_filesystem:
1038 unregister_filesystem(&mtd_inodefs_type);
1039 err_unregister_chdev:
1040 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1041 return ret;
1044 static void __exit cleanup_mtdchar(void)
1046 unregister_mtd_user(&mtdchar_notifier);
1047 mntput(mtd_inode_mnt);
1048 unregister_filesystem(&mtd_inodefs_type);
1049 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1052 module_init(init_mtdchar);
1053 module_exit(cleanup_mtdchar);
1055 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
1057 MODULE_LICENSE("GPL");
1058 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1059 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
1060 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);