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)))
421 struct erase_info *erase;
423 if(!(file->f_mode & FMODE_WRITE))
426 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
430 struct erase_info_user einfo;
432 wait_queue_head_t waitq;
433 DECLARE_WAITQUEUE(wait, current);
435 init_waitqueue_head(&waitq);
437 if (copy_from_user(&einfo, argp,
438 sizeof(struct erase_info_user))) {
442 erase->addr = einfo.start;
443 erase->len = einfo.length;
445 erase->callback = mtdchar_erase_callback;
446 erase->priv = (unsigned long)&waitq;
449 FIXME: Allow INTERRUPTIBLE. Which means
450 not having the wait_queue head on the stack.
452 If the wq_head is on the stack, and we
453 leave because we got interrupted, then the
454 wq_head is no longer there when the
455 callback routine tries to wake us up.
457 ret = mtd->erase(mtd, erase);
459 set_current_state(TASK_UNINTERRUPTIBLE);
460 add_wait_queue(&waitq, &wait);
461 if (erase->state != MTD_ERASE_DONE &&
462 erase->state != MTD_ERASE_FAILED)
464 remove_wait_queue(&waitq, &wait);
465 set_current_state(TASK_RUNNING);
467 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
476 struct mtd_oob_buf buf;
477 struct mtd_oob_ops ops;
478 struct mtd_oob_buf __user *user_buf = argp;
481 if(!(file->f_mode & FMODE_WRITE))
484 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
487 if (buf.length > 4096)
493 ret = access_ok(VERIFY_READ, buf.ptr,
494 buf.length) ? 0 : EFAULT;
499 ops.ooblen = buf.length;
500 ops.ooboffs = buf.start & (mtd->oobsize - 1);
502 ops.mode = MTD_OOB_PLACE;
504 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
507 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
511 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
516 buf.start &= ~(mtd->oobsize - 1);
517 ret = mtd->write_oob(mtd, buf.start, &ops);
519 if (ops.oobretlen > 0xFFFFFFFFU)
521 retlen = ops.oobretlen;
522 if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
532 struct mtd_oob_buf buf;
533 struct mtd_oob_ops ops;
535 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
538 if (buf.length > 4096)
544 ret = access_ok(VERIFY_WRITE, buf.ptr,
545 buf.length) ? 0 : -EFAULT;
549 ops.ooblen = buf.length;
550 ops.ooboffs = buf.start & (mtd->oobsize - 1);
552 ops.mode = MTD_OOB_PLACE;
554 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
557 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
561 buf.start &= ~(mtd->oobsize - 1);
562 ret = mtd->read_oob(mtd, buf.start, &ops);
564 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
566 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
576 struct erase_info_user einfo;
578 if (copy_from_user(&einfo, argp, sizeof(einfo)))
584 ret = mtd->lock(mtd, einfo.start, einfo.length);
590 struct erase_info_user einfo;
592 if (copy_from_user(&einfo, argp, sizeof(einfo)))
598 ret = mtd->unlock(mtd, einfo.start, einfo.length);
602 /* Legacy interface */
605 struct nand_oobinfo oi;
609 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
612 oi.useecc = MTD_NANDECC_AUTOPLACE;
613 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
614 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
616 oi.eccbytes = mtd->ecclayout->eccbytes;
618 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
627 if (copy_from_user(&offs, argp, sizeof(loff_t)))
629 if (!mtd->block_isbad)
632 return mtd->block_isbad(mtd, offs);
640 if (copy_from_user(&offs, argp, sizeof(loff_t)))
642 if (!mtd->block_markbad)
645 return mtd->block_markbad(mtd, offs);
649 #ifdef CONFIG_HAVE_MTD_OTP
653 if (copy_from_user(&mode, argp, sizeof(int)))
656 mfi->mode = MTD_MODE_NORMAL;
658 ret = otp_select_filemode(mfi, mode);
664 case OTPGETREGIONCOUNT:
665 case OTPGETREGIONINFO:
667 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
672 case MTD_MODE_OTP_FACTORY:
673 if (mtd->get_fact_prot_info)
674 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
676 case MTD_MODE_OTP_USER:
677 if (mtd->get_user_prot_info)
678 ret = mtd->get_user_prot_info(mtd, buf, 4096);
684 if (cmd == OTPGETREGIONCOUNT) {
685 int nbr = ret / sizeof(struct otp_info);
686 ret = copy_to_user(argp, &nbr, sizeof(int));
688 ret = copy_to_user(argp, buf, ret);
698 struct otp_info oinfo;
700 if (mfi->mode != MTD_MODE_OTP_USER)
702 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
704 if (!mtd->lock_user_prot_reg)
706 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
716 if (copy_to_user(argp, mtd->ecclayout,
717 sizeof(struct nand_ecclayout)))
724 if (copy_to_user(argp, &mtd->ecc_stats,
725 sizeof(struct mtd_ecc_stats)))
735 case MTD_MODE_OTP_FACTORY:
736 case MTD_MODE_OTP_USER:
737 ret = otp_select_filemode(mfi, arg);
741 if (!mtd->read_oob || !mtd->write_oob)
745 case MTD_MODE_NORMAL:
762 * try to determine where a shared mapping can be made
763 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
767 static unsigned long mtd_get_unmapped_area(struct file *file,
773 struct mtd_file_info *mfi = file->private_data;
774 struct mtd_info *mtd = mfi->mtd;
776 if (mtd->get_unmapped_area) {
777 unsigned long offset;
780 return (unsigned long) -EINVAL;
782 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
783 return (unsigned long) -EINVAL;
785 offset = pgoff << PAGE_SHIFT;
786 if (offset > mtd->size - len)
787 return (unsigned long) -EINVAL;
789 return mtd->get_unmapped_area(mtd, len, offset, flags);
792 /* can't map directly */
793 return (unsigned long) -ENOSYS;
798 * set up a mapping for shared memory segments
800 static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
803 struct mtd_file_info *mfi = file->private_data;
804 struct mtd_info *mtd = mfi->mtd;
806 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
810 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
814 static const struct file_operations mtd_fops = {
815 .owner = THIS_MODULE,
821 .release = mtd_close,
824 .get_unmapped_area = mtd_get_unmapped_area,
828 static int __init init_mtdchar(void)
832 status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops);
834 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
841 static void __exit cleanup_mtdchar(void)
843 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
846 module_init(init_mtdchar);
847 module_exit(cleanup_mtdchar);
849 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
851 MODULE_LICENSE("GPL");
852 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
853 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
854 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);