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>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/compatmac.h>
21 #include <asm/uaccess.h>
25 * Data structure to hold the pointer to the mtd device as well
26 * as mode information ofr various use cases.
28 struct mtd_file_info {
30 enum mtd_file_modes mode;
33 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
35 struct mtd_file_info *mfi = file->private_data;
36 struct mtd_info *mtd = mfi->mtd;
42 offset += file->f_pos;
51 if (offset >= 0 && offset <= mtd->size)
52 return file->f_pos = offset;
59 static int mtd_open(struct inode *inode, struct file *file)
61 int minor = iminor(inode);
62 int devnum = minor >> 1;
65 struct mtd_file_info *mfi;
67 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
69 if (devnum >= MAX_MTD_DEVICES)
72 /* You can't open the RO devices RW */
73 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
77 mtd = get_mtd_device(NULL, devnum);
84 if (mtd->type == MTD_ABSENT) {
90 if (mtd->backing_dev_info)
91 file->f_mapping->backing_dev_info = mtd->backing_dev_info;
93 /* You can't open it RW if it's not a writeable device */
94 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
100 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
107 file->private_data = mfi;
114 /*====================================================================*/
116 static int mtd_close(struct inode *inode, struct file *file)
118 struct mtd_file_info *mfi = file->private_data;
119 struct mtd_info *mtd = mfi->mtd;
121 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
123 /* Only sync if opened RW */
124 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
128 file->private_data = NULL;
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.
137 #define MAX_KMALLOC_SIZE 0x20000
139 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
141 struct mtd_file_info *mfi = file->private_data;
142 struct mtd_info *mtd = mfi->mtd;
144 size_t total_retlen=0;
149 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
151 if (*ppos + count > mtd->size)
152 count = mtd->size - *ppos;
157 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
158 and pass them directly to the MTD functions */
160 if (count > MAX_KMALLOC_SIZE)
161 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
163 kbuf=kmalloc(count, GFP_KERNEL);
170 if (count > MAX_KMALLOC_SIZE)
171 len = MAX_KMALLOC_SIZE;
176 case MTD_MODE_OTP_FACTORY:
177 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
179 case MTD_MODE_OTP_USER:
180 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
184 struct mtd_oob_ops ops;
186 ops.mode = MTD_OOB_RAW;
191 ret = mtd->read_oob(mtd, *ppos, &ops);
196 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
198 /* Nand returns -EBADMSG on ecc errors, but it returns
199 * the data. For our userspace tools it is important
200 * to dump areas with ecc errors !
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.
204 * Userspace software which accesses NAND this way
205 * must be aware of the fact that it deals with NAND
207 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
209 if (copy_to_user(buf, kbuf, retlen)) {
214 total_retlen += retlen;
232 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
234 struct mtd_file_info *mfi = file->private_data;
235 struct mtd_info *mtd = mfi->mtd;
238 size_t total_retlen=0;
242 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
244 if (*ppos == mtd->size)
247 if (*ppos + count > mtd->size)
248 count = mtd->size - *ppos;
253 if (count > MAX_KMALLOC_SIZE)
254 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
256 kbuf=kmalloc(count, GFP_KERNEL);
263 if (count > MAX_KMALLOC_SIZE)
264 len = MAX_KMALLOC_SIZE;
268 if (copy_from_user(kbuf, buf, len)) {
274 case MTD_MODE_OTP_FACTORY:
277 case MTD_MODE_OTP_USER:
278 if (!mtd->write_user_prot_reg) {
282 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
287 struct mtd_oob_ops ops;
289 ops.mode = MTD_OOB_RAW;
294 ret = mtd->write_oob(mtd, *ppos, &ops);
300 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
304 total_retlen += retlen;
318 /*======================================================================
320 IOCTL calls for getting device parameters.
322 ======================================================================*/
323 static void mtdchar_erase_callback (struct erase_info *instr)
325 wake_up((wait_queue_head_t *)instr->priv);
328 #ifdef CONFIG_HAVE_MTD_OTP
329 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
331 struct mtd_info *mtd = mfi->mtd;
335 case MTD_OTP_FACTORY:
336 if (!mtd->read_fact_prot_reg)
339 mfi->mode = MTD_MODE_OTP_FACTORY;
342 if (!mtd->read_fact_prot_reg)
345 mfi->mode = MTD_MODE_OTP_USER;
355 # define otp_select_filemode(f,m) -EOPNOTSUPP
358 static int mtd_ioctl(struct inode *inode, struct file *file,
359 u_int cmd, u_long arg)
361 struct mtd_file_info *mfi = file->private_data;
362 struct mtd_info *mtd = mfi->mtd;
363 void __user *argp = (void __user *)arg;
366 struct mtd_info_user info;
368 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
370 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
372 if (!access_ok(VERIFY_READ, argp, size))
376 if (!access_ok(VERIFY_WRITE, argp, size))
381 case MEMGETREGIONCOUNT:
382 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
386 case MEMGETREGIONINFO:
389 struct mtd_erase_region_info *kr;
390 struct region_info_user *ur = (struct region_info_user *) argp;
392 if (get_user(ur_idx, &(ur->regionindex)))
395 kr = &(mtd->eraseregions[ur_idx]);
397 if (put_user(kr->offset, &(ur->offset))
398 || put_user(kr->erasesize, &(ur->erasesize))
399 || put_user(kr->numblocks, &(ur->numblocks)))
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;
412 /* The below fields are obsolete */
415 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
422 struct erase_info *erase;
424 if(!(file->f_mode & FMODE_WRITE))
427 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
431 wait_queue_head_t waitq;
432 DECLARE_WAITQUEUE(wait, current);
434 init_waitqueue_head(&waitq);
436 if (cmd == MEMERASE64) {
437 struct erase_info_user64 einfo64;
439 if (copy_from_user(&einfo64, argp,
440 sizeof(struct erase_info_user64))) {
444 erase->addr = einfo64.start;
445 erase->len = einfo64.length;
447 struct erase_info_user einfo32;
449 if (copy_from_user(&einfo32, argp,
450 sizeof(struct erase_info_user))) {
454 erase->addr = einfo32.start;
455 erase->len = einfo32.length;
458 erase->callback = mtdchar_erase_callback;
459 erase->priv = (unsigned long)&waitq;
462 FIXME: Allow INTERRUPTIBLE. Which means
463 not having the wait_queue head on the stack.
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.
470 ret = mtd->erase(mtd, erase);
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)
477 remove_wait_queue(&waitq, &wait);
478 set_current_state(TASK_RUNNING);
480 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
489 struct mtd_oob_buf buf;
490 struct mtd_oob_ops ops;
491 struct mtd_oob_buf __user *user_buf = argp;
494 if(!(file->f_mode & FMODE_WRITE))
497 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
500 if (buf.length > 4096)
506 ret = access_ok(VERIFY_READ, buf.ptr,
507 buf.length) ? 0 : EFAULT;
512 ops.ooblen = buf.length;
513 ops.ooboffs = buf.start & (mtd->oobsize - 1);
515 ops.mode = MTD_OOB_PLACE;
517 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
520 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
524 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
529 buf.start &= ~(mtd->oobsize - 1);
530 ret = mtd->write_oob(mtd, buf.start, &ops);
532 if (ops.oobretlen > 0xFFFFFFFFU)
534 retlen = ops.oobretlen;
535 if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
545 struct mtd_oob_buf buf;
546 struct mtd_oob_ops ops;
548 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
551 if (buf.length > 4096)
557 ret = access_ok(VERIFY_WRITE, buf.ptr,
558 buf.length) ? 0 : -EFAULT;
562 ops.ooblen = buf.length;
563 ops.ooboffs = buf.start & (mtd->oobsize - 1);
565 ops.mode = MTD_OOB_PLACE;
567 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
570 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
574 buf.start &= ~(mtd->oobsize - 1);
575 ret = mtd->read_oob(mtd, buf.start, &ops);
577 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
579 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
589 struct erase_info_user einfo;
591 if (copy_from_user(&einfo, argp, sizeof(einfo)))
597 ret = mtd->lock(mtd, einfo.start, einfo.length);
603 struct erase_info_user einfo;
605 if (copy_from_user(&einfo, argp, sizeof(einfo)))
611 ret = mtd->unlock(mtd, einfo.start, einfo.length);
615 /* Legacy interface */
618 struct nand_oobinfo oi;
622 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
625 oi.useecc = MTD_NANDECC_AUTOPLACE;
626 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
627 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
629 oi.eccbytes = mtd->ecclayout->eccbytes;
631 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
640 if (copy_from_user(&offs, argp, sizeof(loff_t)))
642 if (!mtd->block_isbad)
645 return mtd->block_isbad(mtd, offs);
653 if (copy_from_user(&offs, argp, sizeof(loff_t)))
655 if (!mtd->block_markbad)
658 return mtd->block_markbad(mtd, offs);
662 #ifdef CONFIG_HAVE_MTD_OTP
666 if (copy_from_user(&mode, argp, sizeof(int)))
669 mfi->mode = MTD_MODE_NORMAL;
671 ret = otp_select_filemode(mfi, mode);
677 case OTPGETREGIONCOUNT:
678 case OTPGETREGIONINFO:
680 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
685 case MTD_MODE_OTP_FACTORY:
686 if (mtd->get_fact_prot_info)
687 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
689 case MTD_MODE_OTP_USER:
690 if (mtd->get_user_prot_info)
691 ret = mtd->get_user_prot_info(mtd, buf, 4096);
697 if (cmd == OTPGETREGIONCOUNT) {
698 int nbr = ret / sizeof(struct otp_info);
699 ret = copy_to_user(argp, &nbr, sizeof(int));
701 ret = copy_to_user(argp, buf, ret);
711 struct otp_info oinfo;
713 if (mfi->mode != MTD_MODE_OTP_USER)
715 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
717 if (!mtd->lock_user_prot_reg)
719 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
729 if (copy_to_user(argp, mtd->ecclayout,
730 sizeof(struct nand_ecclayout)))
737 if (copy_to_user(argp, &mtd->ecc_stats,
738 sizeof(struct mtd_ecc_stats)))
748 case MTD_MODE_OTP_FACTORY:
749 case MTD_MODE_OTP_USER:
750 ret = otp_select_filemode(mfi, arg);
754 if (!mtd->read_oob || !mtd->write_oob)
758 case MTD_MODE_NORMAL:
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
780 static unsigned long mtd_get_unmapped_area(struct file *file,
786 struct mtd_file_info *mfi = file->private_data;
787 struct mtd_info *mtd = mfi->mtd;
789 if (mtd->get_unmapped_area) {
790 unsigned long offset;
793 return (unsigned long) -EINVAL;
795 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
796 return (unsigned long) -EINVAL;
798 offset = pgoff << PAGE_SHIFT;
799 if (offset > mtd->size - len)
800 return (unsigned long) -EINVAL;
802 return mtd->get_unmapped_area(mtd, len, offset, flags);
805 /* can't map directly */
806 return (unsigned long) -ENOSYS;
811 * set up a mapping for shared memory segments
813 static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
816 struct mtd_file_info *mfi = file->private_data;
817 struct mtd_info *mtd = mfi->mtd;
819 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
823 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
827 static const struct file_operations mtd_fops = {
828 .owner = THIS_MODULE,
834 .release = mtd_close,
837 .get_unmapped_area = mtd_get_unmapped_area,
841 static int __init init_mtdchar(void)
845 status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops);
847 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
854 static void __exit cleanup_mtdchar(void)
856 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
859 module_init(init_mtdchar);
860 module_exit(cleanup_mtdchar);
862 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
864 MODULE_LICENSE("GPL");
865 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
866 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
867 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);