2 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
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.
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.
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
20 #include <linux/device.h>
23 #include <linux/err.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/mutex.h>
30 #include <linux/backing-dev.h>
31 #include <linux/compat.h>
32 #include <linux/mount.h>
33 #include <linux/blkpg.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/mtd/map.h>
38 #include <asm/uaccess.h>
40 #define MTD_INODE_FS_MAGIC 0x11307854
41 static DEFINE_MUTEX(mtd_mutex
);
42 static struct vfsmount
*mtd_inode_mnt __read_mostly
;
45 * Data structure to hold the pointer to the mtd device as well
46 * as mode information of various use cases.
48 struct mtd_file_info
{
51 enum mtd_file_modes mode
;
54 static loff_t
mtdchar_lseek(struct file
*file
, loff_t offset
, int orig
)
56 struct mtd_file_info
*mfi
= file
->private_data
;
57 struct mtd_info
*mtd
= mfi
->mtd
;
63 offset
+= file
->f_pos
;
72 if (offset
>= 0 && offset
<= mtd
->size
)
73 return file
->f_pos
= offset
;
80 static int mtdchar_open(struct inode
*inode
, struct file
*file
)
82 int minor
= iminor(inode
);
83 int devnum
= minor
>> 1;
86 struct mtd_file_info
*mfi
;
87 struct inode
*mtd_ino
;
89 pr_debug("MTD_open\n");
91 /* You can't open the RO devices RW */
92 if ((file
->f_mode
& FMODE_WRITE
) && (minor
& 1))
95 mutex_lock(&mtd_mutex
);
96 mtd
= get_mtd_device(NULL
, devnum
);
103 if (mtd
->type
== MTD_ABSENT
) {
109 mtd_ino
= iget_locked(mtd_inode_mnt
->mnt_sb
, devnum
);
115 if (mtd_ino
->i_state
& I_NEW
) {
116 mtd_ino
->i_private
= mtd
;
117 mtd_ino
->i_mode
= S_IFCHR
;
118 mtd_ino
->i_data
.backing_dev_info
= mtd
->backing_dev_info
;
119 unlock_new_inode(mtd_ino
);
121 file
->f_mapping
= mtd_ino
->i_mapping
;
123 /* You can't open it RW if it's not a writeable device */
124 if ((file
->f_mode
& FMODE_WRITE
) && !(mtd
->flags
& MTD_WRITEABLE
)) {
131 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
140 file
->private_data
= mfi
;
143 mutex_unlock(&mtd_mutex
);
147 /*====================================================================*/
149 static int mtdchar_close(struct inode
*inode
, struct file
*file
)
151 struct mtd_file_info
*mfi
= file
->private_data
;
152 struct mtd_info
*mtd
= mfi
->mtd
;
154 pr_debug("MTD_close\n");
156 /* Only sync if opened RW */
157 if ((file
->f_mode
& FMODE_WRITE
) && mtd
->sync
)
163 file
->private_data
= NULL
;
167 } /* mtdchar_close */
169 /* Back in June 2001, dwmw2 wrote:
171 * FIXME: This _really_ needs to die. In 2.5, we should lock the
172 * userspace buffer down and use it directly with readv/writev.
174 * The implementation below, using mtd_kmalloc_up_to, mitigates
175 * allocation failures when the system is under low-memory situations
176 * or if memory is highly fragmented at the cost of reducing the
177 * performance of the requested transfer due to a smaller buffer size.
179 * A more complex but more memory-efficient implementation based on
180 * get_user_pages and iovecs to cover extents of those pages is a
181 * longer-term goal, as intimated by dwmw2 above. However, for the
182 * write case, this requires yet more complex head and tail transfer
183 * handling when those head and tail offsets and sizes are such that
184 * alignment requirements are not met in the NAND subdriver.
187 static ssize_t
mtdchar_read(struct file
*file
, char __user
*buf
, size_t count
,
190 struct mtd_file_info
*mfi
= file
->private_data
;
191 struct mtd_info
*mtd
= mfi
->mtd
;
193 size_t total_retlen
=0;
199 pr_debug("MTD_read\n");
201 if (*ppos
+ count
> mtd
->size
)
202 count
= mtd
->size
- *ppos
;
207 kbuf
= mtd_kmalloc_up_to(mtd
, &size
);
212 len
= min_t(size_t, count
, size
);
215 case MTD_FILE_MODE_OTP_FACTORY
:
216 ret
= mtd_read_fact_prot_reg(mtd
, *ppos
, len
,
219 case MTD_FILE_MODE_OTP_USER
:
220 ret
= mtd_read_user_prot_reg(mtd
, *ppos
, len
,
223 case MTD_FILE_MODE_RAW
:
225 struct mtd_oob_ops ops
;
227 ops
.mode
= MTD_OPS_RAW
;
232 ret
= mtd_read_oob(mtd
, *ppos
, &ops
);
237 ret
= mtd_read(mtd
, *ppos
, len
, &retlen
, kbuf
);
239 /* Nand returns -EBADMSG on ECC errors, but it returns
240 * the data. For our userspace tools it is important
241 * to dump areas with ECC errors!
242 * For kernel internal usage it also might return -EUCLEAN
243 * to signal the caller that a bitflip has occurred and has
244 * been corrected by the ECC algorithm.
245 * Userspace software which accesses NAND this way
246 * must be aware of the fact that it deals with NAND
248 if (!ret
|| mtd_is_bitflip_or_eccerr(ret
)) {
250 if (copy_to_user(buf
, kbuf
, retlen
)) {
255 total_retlen
+= retlen
;
273 static ssize_t
mtdchar_write(struct file
*file
, const char __user
*buf
, size_t count
,
276 struct mtd_file_info
*mfi
= file
->private_data
;
277 struct mtd_info
*mtd
= mfi
->mtd
;
281 size_t total_retlen
=0;
285 pr_debug("MTD_write\n");
287 if (*ppos
== mtd
->size
)
290 if (*ppos
+ count
> mtd
->size
)
291 count
= mtd
->size
- *ppos
;
296 kbuf
= mtd_kmalloc_up_to(mtd
, &size
);
301 len
= min_t(size_t, count
, size
);
303 if (copy_from_user(kbuf
, buf
, len
)) {
309 case MTD_FILE_MODE_OTP_FACTORY
:
312 case MTD_FILE_MODE_OTP_USER
:
313 if (!mtd
->write_user_prot_reg
) {
317 ret
= mtd_write_user_prot_reg(mtd
, *ppos
, len
,
321 case MTD_FILE_MODE_RAW
:
323 struct mtd_oob_ops ops
;
325 ops
.mode
= MTD_OPS_RAW
;
331 ret
= mtd_write_oob(mtd
, *ppos
, &ops
);
337 ret
= mtd_write(mtd
, *ppos
, len
, &retlen
, kbuf
);
341 total_retlen
+= retlen
;
353 } /* mtdchar_write */
355 /*======================================================================
357 IOCTL calls for getting device parameters.
359 ======================================================================*/
360 static void mtdchar_erase_callback (struct erase_info
*instr
)
362 wake_up((wait_queue_head_t
*)instr
->priv
);
365 #ifdef CONFIG_HAVE_MTD_OTP
366 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
368 struct mtd_info
*mtd
= mfi
->mtd
;
372 case MTD_OTP_FACTORY
:
373 if (!mtd
->read_fact_prot_reg
)
376 mfi
->mode
= MTD_FILE_MODE_OTP_FACTORY
;
379 if (!mtd
->read_fact_prot_reg
)
382 mfi
->mode
= MTD_FILE_MODE_OTP_USER
;
392 # define otp_select_filemode(f,m) -EOPNOTSUPP
395 static int mtdchar_writeoob(struct file
*file
, struct mtd_info
*mtd
,
396 uint64_t start
, uint32_t length
, void __user
*ptr
,
397 uint32_t __user
*retp
)
399 struct mtd_file_info
*mfi
= file
->private_data
;
400 struct mtd_oob_ops ops
;
404 if (!(file
->f_mode
& FMODE_WRITE
))
413 ret
= access_ok(VERIFY_READ
, ptr
, length
) ? 0 : -EFAULT
;
419 ops
.ooboffs
= start
& (mtd
->writesize
- 1);
421 ops
.mode
= (mfi
->mode
== MTD_FILE_MODE_RAW
) ? MTD_OPS_RAW
:
424 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
427 ops
.oobbuf
= memdup_user(ptr
, length
);
428 if (IS_ERR(ops
.oobbuf
))
429 return PTR_ERR(ops
.oobbuf
);
431 start
&= ~((uint64_t)mtd
->writesize
- 1);
432 ret
= mtd_write_oob(mtd
, start
, &ops
);
434 if (ops
.oobretlen
> 0xFFFFFFFFU
)
436 retlen
= ops
.oobretlen
;
437 if (copy_to_user(retp
, &retlen
, sizeof(length
)))
444 static int mtdchar_readoob(struct file
*file
, struct mtd_info
*mtd
,
445 uint64_t start
, uint32_t length
, void __user
*ptr
,
446 uint32_t __user
*retp
)
448 struct mtd_file_info
*mfi
= file
->private_data
;
449 struct mtd_oob_ops ops
;
458 ret
= access_ok(VERIFY_WRITE
, ptr
,
459 length
) ? 0 : -EFAULT
;
464 ops
.ooboffs
= start
& (mtd
->writesize
- 1);
466 ops
.mode
= (mfi
->mode
== MTD_FILE_MODE_RAW
) ? MTD_OPS_RAW
:
469 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
472 ops
.oobbuf
= kmalloc(length
, GFP_KERNEL
);
476 start
&= ~((uint64_t)mtd
->writesize
- 1);
477 ret
= mtd_read_oob(mtd
, start
, &ops
);
479 if (put_user(ops
.oobretlen
, retp
))
481 else if (ops
.oobretlen
&& copy_to_user(ptr
, ops
.oobbuf
,
488 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
489 * data. For our userspace tools it is important to dump areas
491 * For kernel internal usage it also might return -EUCLEAN
492 * to signal the caller that a bitflip has occured and has
493 * been corrected by the ECC algorithm.
495 * Note: currently the standard NAND function, nand_read_oob_std,
496 * does not calculate ECC for the OOB area, so do not rely on
497 * this behavior unless you have replaced it with your own.
499 if (mtd_is_bitflip_or_eccerr(ret
))
506 * Copies (and truncates, if necessary) data from the larger struct,
507 * nand_ecclayout, to the smaller, deprecated layout struct,
508 * nand_ecclayout_user. This is necessary only to support the deprecated
509 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
510 * nand_ecclayout flexibly (i.e. the struct may change size in new
511 * releases without requiring major rewrites).
513 static int shrink_ecclayout(const struct nand_ecclayout
*from
,
514 struct nand_ecclayout_user
*to
)
521 memset(to
, 0, sizeof(*to
));
523 to
->eccbytes
= min((int)from
->eccbytes
, MTD_MAX_ECCPOS_ENTRIES
);
524 for (i
= 0; i
< to
->eccbytes
; i
++)
525 to
->eccpos
[i
] = from
->eccpos
[i
];
527 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
; i
++) {
528 if (from
->oobfree
[i
].length
== 0 &&
529 from
->oobfree
[i
].offset
== 0)
531 to
->oobavail
+= from
->oobfree
[i
].length
;
532 to
->oobfree
[i
] = from
->oobfree
[i
];
538 static int mtdchar_blkpg_ioctl(struct mtd_info
*mtd
,
539 struct blkpg_ioctl_arg __user
*arg
)
541 struct blkpg_ioctl_arg a
;
542 struct blkpg_partition p
;
544 if (!capable(CAP_SYS_ADMIN
))
547 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
550 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
554 case BLKPG_ADD_PARTITION
:
556 /* Only master mtd device must be used to add partitions */
557 if (mtd_is_partition(mtd
))
560 return mtd_add_partition(mtd
, p
.devname
, p
.start
, p
.length
);
562 case BLKPG_DEL_PARTITION
:
567 return mtd_del_partition(mtd
, p
.pno
);
574 static int mtdchar_write_ioctl(struct mtd_info
*mtd
,
575 struct mtd_write_req __user
*argp
)
577 struct mtd_write_req req
;
578 struct mtd_oob_ops ops
;
579 void __user
*usr_data
, *usr_oob
;
582 if (copy_from_user(&req
, argp
, sizeof(req
)) ||
583 !access_ok(VERIFY_READ
, req
.usr_data
, req
.len
) ||
584 !access_ok(VERIFY_READ
, req
.usr_oob
, req
.ooblen
))
590 ops
.len
= (size_t)req
.len
;
591 ops
.ooblen
= (size_t)req
.ooblen
;
594 usr_data
= (void __user
*)(uintptr_t)req
.usr_data
;
595 usr_oob
= (void __user
*)(uintptr_t)req
.usr_oob
;
598 ops
.datbuf
= memdup_user(usr_data
, ops
.len
);
599 if (IS_ERR(ops
.datbuf
))
600 return PTR_ERR(ops
.datbuf
);
606 ops
.oobbuf
= memdup_user(usr_oob
, ops
.ooblen
);
607 if (IS_ERR(ops
.oobbuf
)) {
609 return PTR_ERR(ops
.oobbuf
);
615 ret
= mtd_write_oob(mtd
, (loff_t
)req
.start
, &ops
);
623 static int mtdchar_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
625 struct mtd_file_info
*mfi
= file
->private_data
;
626 struct mtd_info
*mtd
= mfi
->mtd
;
627 void __user
*argp
= (void __user
*)arg
;
630 struct mtd_info_user info
;
632 pr_debug("MTD_ioctl\n");
634 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
636 if (!access_ok(VERIFY_READ
, argp
, size
))
640 if (!access_ok(VERIFY_WRITE
, argp
, size
))
645 case MEMGETREGIONCOUNT
:
646 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
650 case MEMGETREGIONINFO
:
653 struct mtd_erase_region_info
*kr
;
654 struct region_info_user __user
*ur
= argp
;
656 if (get_user(ur_idx
, &(ur
->regionindex
)))
659 if (ur_idx
>= mtd
->numeraseregions
)
662 kr
= &(mtd
->eraseregions
[ur_idx
]);
664 if (put_user(kr
->offset
, &(ur
->offset
))
665 || put_user(kr
->erasesize
, &(ur
->erasesize
))
666 || put_user(kr
->numblocks
, &(ur
->numblocks
)))
673 memset(&info
, 0, sizeof(info
));
674 info
.type
= mtd
->type
;
675 info
.flags
= mtd
->flags
;
676 info
.size
= mtd
->size
;
677 info
.erasesize
= mtd
->erasesize
;
678 info
.writesize
= mtd
->writesize
;
679 info
.oobsize
= mtd
->oobsize
;
680 /* The below field is obsolete */
682 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
689 struct erase_info
*erase
;
691 if(!(file
->f_mode
& FMODE_WRITE
))
694 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
698 wait_queue_head_t waitq
;
699 DECLARE_WAITQUEUE(wait
, current
);
701 init_waitqueue_head(&waitq
);
703 if (cmd
== MEMERASE64
) {
704 struct erase_info_user64 einfo64
;
706 if (copy_from_user(&einfo64
, argp
,
707 sizeof(struct erase_info_user64
))) {
711 erase
->addr
= einfo64
.start
;
712 erase
->len
= einfo64
.length
;
714 struct erase_info_user einfo32
;
716 if (copy_from_user(&einfo32
, argp
,
717 sizeof(struct erase_info_user
))) {
721 erase
->addr
= einfo32
.start
;
722 erase
->len
= einfo32
.length
;
725 erase
->callback
= mtdchar_erase_callback
;
726 erase
->priv
= (unsigned long)&waitq
;
729 FIXME: Allow INTERRUPTIBLE. Which means
730 not having the wait_queue head on the stack.
732 If the wq_head is on the stack, and we
733 leave because we got interrupted, then the
734 wq_head is no longer there when the
735 callback routine tries to wake us up.
737 ret
= mtd_erase(mtd
, erase
);
739 set_current_state(TASK_UNINTERRUPTIBLE
);
740 add_wait_queue(&waitq
, &wait
);
741 if (erase
->state
!= MTD_ERASE_DONE
&&
742 erase
->state
!= MTD_ERASE_FAILED
)
744 remove_wait_queue(&waitq
, &wait
);
745 set_current_state(TASK_RUNNING
);
747 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
756 struct mtd_oob_buf buf
;
757 struct mtd_oob_buf __user
*buf_user
= argp
;
759 /* NOTE: writes return length to buf_user->length */
760 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
763 ret
= mtdchar_writeoob(file
, mtd
, buf
.start
, buf
.length
,
764 buf
.ptr
, &buf_user
->length
);
770 struct mtd_oob_buf buf
;
771 struct mtd_oob_buf __user
*buf_user
= argp
;
773 /* NOTE: writes return length to buf_user->start */
774 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
777 ret
= mtdchar_readoob(file
, mtd
, buf
.start
, buf
.length
,
778 buf
.ptr
, &buf_user
->start
);
784 struct mtd_oob_buf64 buf
;
785 struct mtd_oob_buf64 __user
*buf_user
= argp
;
787 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
790 ret
= mtdchar_writeoob(file
, mtd
, buf
.start
, buf
.length
,
791 (void __user
*)(uintptr_t)buf
.usr_ptr
,
798 struct mtd_oob_buf64 buf
;
799 struct mtd_oob_buf64 __user
*buf_user
= argp
;
801 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
804 ret
= mtdchar_readoob(file
, mtd
, buf
.start
, buf
.length
,
805 (void __user
*)(uintptr_t)buf
.usr_ptr
,
812 ret
= mtdchar_write_ioctl(mtd
,
813 (struct mtd_write_req __user
*)arg
);
819 struct erase_info_user einfo
;
821 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
827 ret
= mtd
->lock(mtd
, einfo
.start
, einfo
.length
);
833 struct erase_info_user einfo
;
835 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
841 ret
= mtd
->unlock(mtd
, einfo
.start
, einfo
.length
);
847 struct erase_info_user einfo
;
849 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
855 ret
= mtd
->is_locked(mtd
, einfo
.start
, einfo
.length
);
859 /* Legacy interface */
862 struct nand_oobinfo oi
;
866 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
869 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
870 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
871 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
873 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
875 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
884 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
886 if (!mtd
->block_isbad
)
889 return mtd
->block_isbad(mtd
, offs
);
897 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
899 if (!mtd
->block_markbad
)
902 return mtd
->block_markbad(mtd
, offs
);
906 #ifdef CONFIG_HAVE_MTD_OTP
910 if (copy_from_user(&mode
, argp
, sizeof(int)))
913 mfi
->mode
= MTD_FILE_MODE_NORMAL
;
915 ret
= otp_select_filemode(mfi
, mode
);
921 case OTPGETREGIONCOUNT
:
922 case OTPGETREGIONINFO
:
924 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
929 case MTD_FILE_MODE_OTP_FACTORY
:
930 if (mtd
->get_fact_prot_info
)
931 ret
= mtd_get_fact_prot_info(mtd
, buf
, 4096);
933 case MTD_FILE_MODE_OTP_USER
:
934 if (mtd
->get_user_prot_info
)
935 ret
= mtd_get_user_prot_info(mtd
, buf
, 4096);
941 if (cmd
== OTPGETREGIONCOUNT
) {
942 int nbr
= ret
/ sizeof(struct otp_info
);
943 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
945 ret
= copy_to_user(argp
, buf
, ret
);
955 struct otp_info oinfo
;
957 if (mfi
->mode
!= MTD_FILE_MODE_OTP_USER
)
959 if (copy_from_user(&oinfo
, argp
, sizeof(oinfo
)))
961 if (!mtd
->lock_user_prot_reg
)
963 ret
= mtd_lock_user_prot_reg(mtd
, oinfo
.start
, oinfo
.length
);
968 /* This ioctl is being deprecated - it truncates the ECC layout */
971 struct nand_ecclayout_user
*usrlay
;
976 usrlay
= kmalloc(sizeof(*usrlay
), GFP_KERNEL
);
980 shrink_ecclayout(mtd
->ecclayout
, usrlay
);
982 if (copy_to_user(argp
, usrlay
, sizeof(*usrlay
)))
990 if (copy_to_user(argp
, &mtd
->ecc_stats
,
991 sizeof(struct mtd_ecc_stats
)))
1001 case MTD_FILE_MODE_OTP_FACTORY
:
1002 case MTD_FILE_MODE_OTP_USER
:
1003 ret
= otp_select_filemode(mfi
, arg
);
1006 case MTD_FILE_MODE_RAW
:
1007 if (!mtd
->read_oob
|| !mtd
->write_oob
)
1011 case MTD_FILE_MODE_NORMAL
:
1022 ret
= mtdchar_blkpg_ioctl(mtd
,
1023 (struct blkpg_ioctl_arg __user
*)arg
);
1029 /* No reread partition feature. Just return ok */
1039 } /* memory_ioctl */
1041 static long mtdchar_unlocked_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
1045 mutex_lock(&mtd_mutex
);
1046 ret
= mtdchar_ioctl(file
, cmd
, arg
);
1047 mutex_unlock(&mtd_mutex
);
1052 #ifdef CONFIG_COMPAT
1054 struct mtd_oob_buf32
{
1057 compat_caddr_t ptr
; /* unsigned char* */
1060 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1061 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1063 static long mtdchar_compat_ioctl(struct file
*file
, unsigned int cmd
,
1066 struct mtd_file_info
*mfi
= file
->private_data
;
1067 struct mtd_info
*mtd
= mfi
->mtd
;
1068 void __user
*argp
= compat_ptr(arg
);
1071 mutex_lock(&mtd_mutex
);
1076 struct mtd_oob_buf32 buf
;
1077 struct mtd_oob_buf32 __user
*buf_user
= argp
;
1079 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
1082 ret
= mtdchar_writeoob(file
, mtd
, buf
.start
,
1083 buf
.length
, compat_ptr(buf
.ptr
),
1090 struct mtd_oob_buf32 buf
;
1091 struct mtd_oob_buf32 __user
*buf_user
= argp
;
1093 /* NOTE: writes return length to buf->start */
1094 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
1097 ret
= mtdchar_readoob(file
, mtd
, buf
.start
,
1098 buf
.length
, compat_ptr(buf
.ptr
),
1103 ret
= mtdchar_ioctl(file
, cmd
, (unsigned long)argp
);
1106 mutex_unlock(&mtd_mutex
);
1111 #endif /* CONFIG_COMPAT */
1114 * try to determine where a shared mapping can be made
1115 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1119 static unsigned long mtdchar_get_unmapped_area(struct file
*file
,
1122 unsigned long pgoff
,
1123 unsigned long flags
)
1125 struct mtd_file_info
*mfi
= file
->private_data
;
1126 struct mtd_info
*mtd
= mfi
->mtd
;
1128 if (mtd
->get_unmapped_area
) {
1129 unsigned long offset
;
1132 return (unsigned long) -EINVAL
;
1134 if (len
> mtd
->size
|| pgoff
>= (mtd
->size
>> PAGE_SHIFT
))
1135 return (unsigned long) -EINVAL
;
1137 offset
= pgoff
<< PAGE_SHIFT
;
1138 if (offset
> mtd
->size
- len
)
1139 return (unsigned long) -EINVAL
;
1141 return mtd_get_unmapped_area(mtd
, len
, offset
, flags
);
1144 /* can't map directly */
1145 return (unsigned long) -ENOSYS
;
1150 * set up a mapping for shared memory segments
1152 static int mtdchar_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1155 struct mtd_file_info
*mfi
= file
->private_data
;
1156 struct mtd_info
*mtd
= mfi
->mtd
;
1157 struct map_info
*map
= mtd
->priv
;
1158 unsigned long start
;
1162 if (mtd
->type
== MTD_RAM
|| mtd
->type
== MTD_ROM
) {
1163 off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1165 len
= PAGE_ALIGN((start
& ~PAGE_MASK
) + map
->size
);
1167 if ((vma
->vm_end
- vma
->vm_start
+ off
) > len
)
1171 vma
->vm_pgoff
= off
>> PAGE_SHIFT
;
1172 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
1174 #ifdef pgprot_noncached
1175 if (file
->f_flags
& O_DSYNC
|| off
>= __pa(high_memory
))
1176 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1178 if (io_remap_pfn_range(vma
, vma
->vm_start
, off
>> PAGE_SHIFT
,
1179 vma
->vm_end
- vma
->vm_start
,
1187 return vma
->vm_flags
& VM_SHARED
? 0 : -ENOSYS
;
1191 static const struct file_operations mtd_fops
= {
1192 .owner
= THIS_MODULE
,
1193 .llseek
= mtdchar_lseek
,
1194 .read
= mtdchar_read
,
1195 .write
= mtdchar_write
,
1196 .unlocked_ioctl
= mtdchar_unlocked_ioctl
,
1197 #ifdef CONFIG_COMPAT
1198 .compat_ioctl
= mtdchar_compat_ioctl
,
1200 .open
= mtdchar_open
,
1201 .release
= mtdchar_close
,
1202 .mmap
= mtdchar_mmap
,
1204 .get_unmapped_area
= mtdchar_get_unmapped_area
,
1208 static struct dentry
*mtd_inodefs_mount(struct file_system_type
*fs_type
,
1209 int flags
, const char *dev_name
, void *data
)
1211 return mount_pseudo(fs_type
, "mtd_inode:", NULL
, NULL
, MTD_INODE_FS_MAGIC
);
1214 static struct file_system_type mtd_inodefs_type
= {
1215 .name
= "mtd_inodefs",
1216 .mount
= mtd_inodefs_mount
,
1217 .kill_sb
= kill_anon_super
,
1220 static void mtdchar_notify_add(struct mtd_info
*mtd
)
1224 static void mtdchar_notify_remove(struct mtd_info
*mtd
)
1226 struct inode
*mtd_ino
= ilookup(mtd_inode_mnt
->mnt_sb
, mtd
->index
);
1229 /* Destroy the inode if it exists */
1230 clear_nlink(mtd_ino
);
1235 static struct mtd_notifier mtdchar_notifier
= {
1236 .add
= mtdchar_notify_add
,
1237 .remove
= mtdchar_notify_remove
,
1240 static int __init
init_mtdchar(void)
1244 ret
= __register_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
,
1247 pr_notice("Can't allocate major number %d for "
1248 "Memory Technology Devices.\n", MTD_CHAR_MAJOR
);
1252 ret
= register_filesystem(&mtd_inodefs_type
);
1254 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret
);
1255 goto err_unregister_chdev
;
1258 mtd_inode_mnt
= kern_mount(&mtd_inodefs_type
);
1259 if (IS_ERR(mtd_inode_mnt
)) {
1260 ret
= PTR_ERR(mtd_inode_mnt
);
1261 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret
);
1262 goto err_unregister_filesystem
;
1264 register_mtd_user(&mtdchar_notifier
);
1268 err_unregister_filesystem
:
1269 unregister_filesystem(&mtd_inodefs_type
);
1270 err_unregister_chdev
:
1271 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1275 static void __exit
cleanup_mtdchar(void)
1277 unregister_mtd_user(&mtdchar_notifier
);
1278 kern_unmount(mtd_inode_mnt
);
1279 unregister_filesystem(&mtd_inodefs_type
);
1280 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1283 module_init(init_mtdchar
);
1284 module_exit(cleanup_mtdchar
);
1286 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);
1288 MODULE_LICENSE("GPL");
1289 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1290 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
1291 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);