2 * Character-device access to raw MTD devices.
6 #include <linux/device.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>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/compatmac.h>
22 #include <asm/uaccess.h>
26 * Data structure to hold the pointer to the mtd device as well
27 * as mode information ofr various use cases.
29 struct mtd_file_info {
31 enum mtd_file_modes mode;
34 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
36 struct mtd_file_info *mfi = file->private_data;
37 struct mtd_info *mtd = mfi->mtd;
43 offset += file->f_pos;
52 if (offset >= 0 && offset <= mtd->size)
53 return file->f_pos = offset;
60 static int mtd_open(struct inode *inode, struct file *file)
62 int minor = iminor(inode);
63 int devnum = minor >> 1;
66 struct mtd_file_info *mfi;
68 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
70 /* You can't open the RO devices RW */
71 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
75 mtd = get_mtd_device(NULL, devnum);
82 if (mtd->type == MTD_ABSENT) {
88 if (mtd->backing_dev_info)
89 file->f_mapping->backing_dev_info = mtd->backing_dev_info;
91 /* You can't open it RW if it's not a writeable device */
92 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
98 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
105 file->private_data = mfi;
112 /*====================================================================*/
114 static int mtd_close(struct inode *inode, struct file *file)
116 struct mtd_file_info *mfi = file->private_data;
117 struct mtd_info *mtd = mfi->mtd;
119 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
121 /* Only sync if opened RW */
122 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
126 file->private_data = NULL;
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.
135 #define MAX_KMALLOC_SIZE 0x20000
137 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
139 struct mtd_file_info *mfi = file->private_data;
140 struct mtd_info *mtd = mfi->mtd;
142 size_t total_retlen=0;
147 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
149 if (*ppos + count > mtd->size)
150 count = mtd->size - *ppos;
155 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
156 and pass them directly to the MTD functions */
158 if (count > MAX_KMALLOC_SIZE)
159 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
161 kbuf=kmalloc(count, GFP_KERNEL);
168 if (count > MAX_KMALLOC_SIZE)
169 len = MAX_KMALLOC_SIZE;
174 case MTD_MODE_OTP_FACTORY:
175 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
177 case MTD_MODE_OTP_USER:
178 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
182 struct mtd_oob_ops ops;
184 ops.mode = MTD_OOB_RAW;
189 ret = mtd->read_oob(mtd, *ppos, &ops);
194 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
196 /* Nand returns -EBADMSG on ecc errors, but it returns
197 * the data. For our userspace tools it is important
198 * to dump areas with ecc errors !
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.
202 * Userspace software which accesses NAND this way
203 * must be aware of the fact that it deals with NAND
205 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
207 if (copy_to_user(buf, kbuf, retlen)) {
212 total_retlen += retlen;
230 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
232 struct mtd_file_info *mfi = file->private_data;
233 struct mtd_info *mtd = mfi->mtd;
236 size_t total_retlen=0;
240 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
242 if (*ppos == mtd->size)
245 if (*ppos + count > mtd->size)
246 count = mtd->size - *ppos;
251 if (count > MAX_KMALLOC_SIZE)
252 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
254 kbuf=kmalloc(count, GFP_KERNEL);
261 if (count > MAX_KMALLOC_SIZE)
262 len = MAX_KMALLOC_SIZE;
266 if (copy_from_user(kbuf, buf, len)) {
272 case MTD_MODE_OTP_FACTORY:
275 case MTD_MODE_OTP_USER:
276 if (!mtd->write_user_prot_reg) {
280 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
285 struct mtd_oob_ops ops;
287 ops.mode = MTD_OOB_RAW;
292 ret = mtd->write_oob(mtd, *ppos, &ops);
298 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
302 total_retlen += retlen;
316 /*======================================================================
318 IOCTL calls for getting device parameters.
320 ======================================================================*/
321 static void mtdchar_erase_callback (struct erase_info *instr)
323 wake_up((wait_queue_head_t *)instr->priv);
326 #ifdef CONFIG_HAVE_MTD_OTP
327 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
329 struct mtd_info *mtd = mfi->mtd;
333 case MTD_OTP_FACTORY:
334 if (!mtd->read_fact_prot_reg)
337 mfi->mode = MTD_MODE_OTP_FACTORY;
340 if (!mtd->read_fact_prot_reg)
343 mfi->mode = MTD_MODE_OTP_USER;
353 # define otp_select_filemode(f,m) -EOPNOTSUPP
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)
360 struct mtd_oob_ops ops;
364 if (!(file->f_mode & FMODE_WRITE))
373 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
379 ops.ooboffs = start & (mtd->oobsize - 1);
381 ops.mode = MTD_OOB_PLACE;
383 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
386 ops.oobbuf = kmalloc(length, GFP_KERNEL);
390 if (copy_from_user(ops.oobbuf, ptr, length)) {
395 start &= ~((uint64_t)mtd->oobsize - 1);
396 ret = mtd->write_oob(mtd, start, &ops);
398 if (ops.oobretlen > 0xFFFFFFFFU)
400 retlen = ops.oobretlen;
401 if (copy_to_user(retp, &retlen, sizeof(length)))
408 static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
409 uint32_t length, void __user *ptr, uint32_t __user *retp)
411 struct mtd_oob_ops ops;
420 ret = access_ok(VERIFY_WRITE, ptr,
421 length) ? 0 : -EFAULT;
426 ops.ooboffs = start & (mtd->oobsize - 1);
428 ops.mode = MTD_OOB_PLACE;
430 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
433 ops.oobbuf = kmalloc(length, GFP_KERNEL);
437 start &= ~((uint64_t)mtd->oobsize - 1);
438 ret = mtd->read_oob(mtd, start, &ops);
440 if (put_user(ops.oobretlen, retp))
442 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
450 static int mtd_ioctl(struct inode *inode, struct file *file,
451 u_int cmd, u_long arg)
453 struct mtd_file_info *mfi = file->private_data;
454 struct mtd_info *mtd = mfi->mtd;
455 void __user *argp = (void __user *)arg;
458 struct mtd_info_user info;
460 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
462 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
464 if (!access_ok(VERIFY_READ, argp, size))
468 if (!access_ok(VERIFY_WRITE, argp, size))
473 case MEMGETREGIONCOUNT:
474 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
478 case MEMGETREGIONINFO:
481 struct mtd_erase_region_info *kr;
482 struct region_info_user __user *ur = argp;
484 if (get_user(ur_idx, &(ur->regionindex)))
487 kr = &(mtd->eraseregions[ur_idx]);
489 if (put_user(kr->offset, &(ur->offset))
490 || put_user(kr->erasesize, &(ur->erasesize))
491 || put_user(kr->numblocks, &(ur->numblocks)))
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;
504 /* The below fields are obsolete */
507 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
514 struct erase_info *erase;
516 if(!(file->f_mode & FMODE_WRITE))
519 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
523 wait_queue_head_t waitq;
524 DECLARE_WAITQUEUE(wait, current);
526 init_waitqueue_head(&waitq);
528 if (cmd == MEMERASE64) {
529 struct erase_info_user64 einfo64;
531 if (copy_from_user(&einfo64, argp,
532 sizeof(struct erase_info_user64))) {
536 erase->addr = einfo64.start;
537 erase->len = einfo64.length;
539 struct erase_info_user einfo32;
541 if (copy_from_user(&einfo32, argp,
542 sizeof(struct erase_info_user))) {
546 erase->addr = einfo32.start;
547 erase->len = einfo32.length;
550 erase->callback = mtdchar_erase_callback;
551 erase->priv = (unsigned long)&waitq;
554 FIXME: Allow INTERRUPTIBLE. Which means
555 not having the wait_queue head on the stack.
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.
562 ret = mtd->erase(mtd, erase);
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)
569 remove_wait_queue(&waitq, &wait);
570 set_current_state(TASK_RUNNING);
572 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
581 struct mtd_oob_buf buf;
582 struct mtd_oob_buf __user *buf_user = argp;
584 /* NOTE: writes return length to buf_user->length */
585 if (copy_from_user(&buf, argp, sizeof(buf)))
588 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
589 buf.ptr, &buf_user->length);
595 struct mtd_oob_buf buf;
596 struct mtd_oob_buf __user *buf_user = argp;
598 /* NOTE: writes return length to buf_user->start */
599 if (copy_from_user(&buf, argp, sizeof(buf)))
602 ret = mtd_do_readoob(mtd, buf.start, buf.length,
603 buf.ptr, &buf_user->start);
609 struct mtd_oob_buf64 buf;
610 struct mtd_oob_buf64 __user *buf_user = argp;
612 if (copy_from_user(&buf, argp, sizeof(buf)))
615 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
616 (void __user *)(uintptr_t)buf.usr_ptr,
623 struct mtd_oob_buf64 buf;
624 struct mtd_oob_buf64 __user *buf_user = argp;
626 if (copy_from_user(&buf, argp, sizeof(buf)))
629 ret = mtd_do_readoob(mtd, buf.start, buf.length,
630 (void __user *)(uintptr_t)buf.usr_ptr,
637 struct erase_info_user einfo;
639 if (copy_from_user(&einfo, argp, sizeof(einfo)))
645 ret = mtd->lock(mtd, einfo.start, einfo.length);
651 struct erase_info_user einfo;
653 if (copy_from_user(&einfo, argp, sizeof(einfo)))
659 ret = mtd->unlock(mtd, einfo.start, einfo.length);
663 /* Legacy interface */
666 struct nand_oobinfo oi;
670 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
673 oi.useecc = MTD_NANDECC_AUTOPLACE;
674 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
675 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
677 oi.eccbytes = mtd->ecclayout->eccbytes;
679 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
688 if (copy_from_user(&offs, argp, sizeof(loff_t)))
690 if (!mtd->block_isbad)
693 return mtd->block_isbad(mtd, offs);
701 if (copy_from_user(&offs, argp, sizeof(loff_t)))
703 if (!mtd->block_markbad)
706 return mtd->block_markbad(mtd, offs);
710 #ifdef CONFIG_HAVE_MTD_OTP
714 if (copy_from_user(&mode, argp, sizeof(int)))
717 mfi->mode = MTD_MODE_NORMAL;
719 ret = otp_select_filemode(mfi, mode);
725 case OTPGETREGIONCOUNT:
726 case OTPGETREGIONINFO:
728 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
733 case MTD_MODE_OTP_FACTORY:
734 if (mtd->get_fact_prot_info)
735 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
737 case MTD_MODE_OTP_USER:
738 if (mtd->get_user_prot_info)
739 ret = mtd->get_user_prot_info(mtd, buf, 4096);
745 if (cmd == OTPGETREGIONCOUNT) {
746 int nbr = ret / sizeof(struct otp_info);
747 ret = copy_to_user(argp, &nbr, sizeof(int));
749 ret = copy_to_user(argp, buf, ret);
759 struct otp_info oinfo;
761 if (mfi->mode != MTD_MODE_OTP_USER)
763 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
765 if (!mtd->lock_user_prot_reg)
767 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
777 if (copy_to_user(argp, mtd->ecclayout,
778 sizeof(struct nand_ecclayout)))
785 if (copy_to_user(argp, &mtd->ecc_stats,
786 sizeof(struct mtd_ecc_stats)))
796 case MTD_MODE_OTP_FACTORY:
797 case MTD_MODE_OTP_USER:
798 ret = otp_select_filemode(mfi, arg);
802 if (!mtd->read_oob || !mtd->write_oob)
806 case MTD_MODE_NORMAL:
824 struct mtd_oob_buf32 {
827 compat_caddr_t ptr; /* unsigned char* */
830 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
831 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
833 static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
836 struct inode *inode = file->f_path.dentry->d_inode;
837 struct mtd_file_info *mfi = file->private_data;
838 struct mtd_info *mtd = mfi->mtd;
839 void __user *argp = compat_ptr(arg);
847 struct mtd_oob_buf32 buf;
848 struct mtd_oob_buf32 __user *buf_user = argp;
850 if (copy_from_user(&buf, argp, sizeof(buf)))
853 ret = mtd_do_writeoob(file, mtd, buf.start,
854 buf.length, compat_ptr(buf.ptr),
861 struct mtd_oob_buf32 buf;
862 struct mtd_oob_buf32 __user *buf_user = argp;
864 /* NOTE: writes return length to buf->start */
865 if (copy_from_user(&buf, argp, sizeof(buf)))
868 ret = mtd_do_readoob(mtd, buf.start,
869 buf.length, compat_ptr(buf.ptr),
874 ret = mtd_ioctl(inode, file, cmd, (unsigned long)argp);
882 #endif /* CONFIG_COMPAT */
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
890 static unsigned long mtd_get_unmapped_area(struct file *file,
896 struct mtd_file_info *mfi = file->private_data;
897 struct mtd_info *mtd = mfi->mtd;
899 if (mtd->get_unmapped_area) {
900 unsigned long offset;
903 return (unsigned long) -EINVAL;
905 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
906 return (unsigned long) -EINVAL;
908 offset = pgoff << PAGE_SHIFT;
909 if (offset > mtd->size - len)
910 return (unsigned long) -EINVAL;
912 return mtd->get_unmapped_area(mtd, len, offset, flags);
915 /* can't map directly */
916 return (unsigned long) -ENOSYS;
921 * set up a mapping for shared memory segments
923 static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
926 struct mtd_file_info *mfi = file->private_data;
927 struct mtd_info *mtd = mfi->mtd;
929 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
933 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
937 static const struct file_operations mtd_fops = {
938 .owner = THIS_MODULE,
944 .compat_ioctl = mtd_compat_ioctl,
947 .release = mtd_close,
950 .get_unmapped_area = mtd_get_unmapped_area,
954 static int __init init_mtdchar(void)
958 status = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
961 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
968 static void __exit cleanup_mtdchar(void)
970 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
973 module_init(init_mtdchar);
974 module_exit(cleanup_mtdchar);
976 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
978 MODULE_LICENSE("GPL");
979 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
980 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
981 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);