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>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/compatmac.h>
19 #include <asm/uaccess.h>
21 static struct class *mtd_class;
23 static void mtd_notify_add(struct mtd_info* mtd)
28 device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), "mtd%d", mtd->index);
30 device_create(mtd_class, NULL,
31 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), "mtd%dro", mtd->index);
34 static void mtd_notify_remove(struct mtd_info* mtd)
39 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
40 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
43 static struct mtd_notifier notifier = {
44 .add = mtd_notify_add,
45 .remove = mtd_notify_remove,
49 * Data structure to hold the pointer to the mtd device as well
50 * as mode information ofr various use cases.
52 struct mtd_file_info {
54 enum mtd_file_modes mode;
57 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
59 struct mtd_file_info *mfi = file->private_data;
60 struct mtd_info *mtd = mfi->mtd;
66 offset += file->f_pos;
75 if (offset >= 0 && offset <= mtd->size)
76 return file->f_pos = offset;
83 static int mtd_open(struct inode *inode, struct file *file)
85 int minor = iminor(inode);
86 int devnum = minor >> 1;
88 struct mtd_file_info *mfi;
90 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
92 if (devnum >= MAX_MTD_DEVICES)
95 /* You can't open the RO devices RW */
96 if ((file->f_mode & 2) && (minor & 1))
99 mtd = get_mtd_device(NULL, devnum);
104 if (MTD_ABSENT == mtd->type) {
109 /* You can't open it RW if it's not a writeable device */
110 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
115 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
121 file->private_data = mfi;
126 /*====================================================================*/
128 static int mtd_close(struct inode *inode, struct file *file)
130 struct mtd_file_info *mfi = file->private_data;
131 struct mtd_info *mtd = mfi->mtd;
133 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
135 /* Only sync if opened RW */
136 if ((file->f_mode & 2) && mtd->sync)
140 file->private_data = NULL;
146 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
147 userspace buffer down and use it directly with readv/writev.
149 #define MAX_KMALLOC_SIZE 0x20000
151 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
153 struct mtd_file_info *mfi = file->private_data;
154 struct mtd_info *mtd = mfi->mtd;
156 size_t total_retlen=0;
161 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
163 if (*ppos + count > mtd->size)
164 count = mtd->size - *ppos;
169 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
170 and pass them directly to the MTD functions */
172 if (count > MAX_KMALLOC_SIZE)
173 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
175 kbuf=kmalloc(count, GFP_KERNEL);
182 if (count > MAX_KMALLOC_SIZE)
183 len = MAX_KMALLOC_SIZE;
188 case MTD_MODE_OTP_FACTORY:
189 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
191 case MTD_MODE_OTP_USER:
192 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
196 struct mtd_oob_ops ops;
198 ops.mode = MTD_OOB_RAW;
203 ret = mtd->read_oob(mtd, *ppos, &ops);
208 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
210 /* Nand returns -EBADMSG on ecc errors, but it returns
211 * the data. For our userspace tools it is important
212 * to dump areas with ecc errors !
213 * For kernel internal usage it also might return -EUCLEAN
214 * to signal the caller that a bitflip has occured and has
215 * been corrected by the ECC algorithm.
216 * Userspace software which accesses NAND this way
217 * must be aware of the fact that it deals with NAND
219 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
221 if (copy_to_user(buf, kbuf, retlen)) {
226 total_retlen += retlen;
244 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
246 struct mtd_file_info *mfi = file->private_data;
247 struct mtd_info *mtd = mfi->mtd;
250 size_t total_retlen=0;
254 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
256 if (*ppos == mtd->size)
259 if (*ppos + count > mtd->size)
260 count = mtd->size - *ppos;
265 if (count > MAX_KMALLOC_SIZE)
266 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
268 kbuf=kmalloc(count, GFP_KERNEL);
275 if (count > MAX_KMALLOC_SIZE)
276 len = MAX_KMALLOC_SIZE;
280 if (copy_from_user(kbuf, buf, len)) {
286 case MTD_MODE_OTP_FACTORY:
289 case MTD_MODE_OTP_USER:
290 if (!mtd->write_user_prot_reg) {
294 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
299 struct mtd_oob_ops ops;
301 ops.mode = MTD_OOB_RAW;
306 ret = mtd->write_oob(mtd, *ppos, &ops);
312 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
316 total_retlen += retlen;
330 /*======================================================================
332 IOCTL calls for getting device parameters.
334 ======================================================================*/
335 static void mtdchar_erase_callback (struct erase_info *instr)
337 wake_up((wait_queue_head_t *)instr->priv);
340 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
341 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
343 struct mtd_info *mtd = mfi->mtd;
347 case MTD_OTP_FACTORY:
348 if (!mtd->read_fact_prot_reg)
351 mfi->mode = MTD_MODE_OTP_FACTORY;
354 if (!mtd->read_fact_prot_reg)
357 mfi->mode = MTD_MODE_OTP_USER;
367 # define otp_select_filemode(f,m) -EOPNOTSUPP
370 static int mtd_ioctl(struct inode *inode, struct file *file,
371 u_int cmd, u_long arg)
373 struct mtd_file_info *mfi = file->private_data;
374 struct mtd_info *mtd = mfi->mtd;
375 void __user *argp = (void __user *)arg;
378 struct mtd_info_user info;
380 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
382 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
384 if (!access_ok(VERIFY_READ, argp, size))
388 if (!access_ok(VERIFY_WRITE, argp, size))
393 case MEMGETREGIONCOUNT:
394 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
398 case MEMGETREGIONINFO:
400 struct region_info_user ur;
402 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
405 if (ur.regionindex >= mtd->numeraseregions)
407 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
408 sizeof(struct mtd_erase_region_info)))
414 info.type = mtd->type;
415 info.flags = mtd->flags;
416 info.size = mtd->size;
417 info.erasesize = mtd->erasesize;
418 info.writesize = mtd->writesize;
419 info.oobsize = mtd->oobsize;
420 /* The below fields are obsolete */
423 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
429 struct erase_info *erase;
431 if(!(file->f_mode & 2))
434 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
438 wait_queue_head_t waitq;
439 DECLARE_WAITQUEUE(wait, current);
441 init_waitqueue_head(&waitq);
443 if (copy_from_user(&erase->addr, argp,
444 sizeof(struct erase_info_user))) {
449 erase->callback = mtdchar_erase_callback;
450 erase->priv = (unsigned long)&waitq;
453 FIXME: Allow INTERRUPTIBLE. Which means
454 not having the wait_queue head on the stack.
456 If the wq_head is on the stack, and we
457 leave because we got interrupted, then the
458 wq_head is no longer there when the
459 callback routine tries to wake us up.
461 ret = mtd->erase(mtd, erase);
463 set_current_state(TASK_UNINTERRUPTIBLE);
464 add_wait_queue(&waitq, &wait);
465 if (erase->state != MTD_ERASE_DONE &&
466 erase->state != MTD_ERASE_FAILED)
468 remove_wait_queue(&waitq, &wait);
469 set_current_state(TASK_RUNNING);
471 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
480 struct mtd_oob_buf buf;
481 struct mtd_oob_ops ops;
482 struct mtd_oob_buf __user *user_buf = argp;
485 if(!(file->f_mode & 2))
488 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
491 if (buf.length > 4096)
497 ret = access_ok(VERIFY_READ, buf.ptr,
498 buf.length) ? 0 : EFAULT;
503 ops.ooblen = buf.length;
504 ops.ooboffs = buf.start & (mtd->oobsize - 1);
506 ops.mode = MTD_OOB_PLACE;
508 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
511 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
515 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
520 buf.start &= ~(mtd->oobsize - 1);
521 ret = mtd->write_oob(mtd, buf.start, &ops);
523 if (ops.oobretlen > 0xFFFFFFFFU)
525 retlen = ops.oobretlen;
526 if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
536 struct mtd_oob_buf buf;
537 struct mtd_oob_ops ops;
539 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
542 if (buf.length > 4096)
548 ret = access_ok(VERIFY_WRITE, buf.ptr,
549 buf.length) ? 0 : -EFAULT;
553 ops.ooblen = buf.length;
554 ops.ooboffs = buf.start & (mtd->oobsize - 1);
556 ops.mode = MTD_OOB_PLACE;
558 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
561 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
565 buf.start &= ~(mtd->oobsize - 1);
566 ret = mtd->read_oob(mtd, buf.start, &ops);
568 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
570 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
580 struct erase_info_user einfo;
582 if (copy_from_user(&einfo, argp, sizeof(einfo)))
588 ret = mtd->lock(mtd, einfo.start, einfo.length);
594 struct erase_info_user einfo;
596 if (copy_from_user(&einfo, argp, sizeof(einfo)))
602 ret = mtd->unlock(mtd, einfo.start, einfo.length);
606 /* Legacy interface */
609 struct nand_oobinfo oi;
613 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
616 oi.useecc = MTD_NANDECC_AUTOPLACE;
617 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
618 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
620 oi.eccbytes = mtd->ecclayout->eccbytes;
622 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
631 if (copy_from_user(&offs, argp, sizeof(loff_t)))
633 if (!mtd->block_isbad)
636 return mtd->block_isbad(mtd, offs);
644 if (copy_from_user(&offs, argp, sizeof(loff_t)))
646 if (!mtd->block_markbad)
649 return mtd->block_markbad(mtd, offs);
653 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
657 if (copy_from_user(&mode, argp, sizeof(int)))
660 mfi->mode = MTD_MODE_NORMAL;
662 ret = otp_select_filemode(mfi, mode);
668 case OTPGETREGIONCOUNT:
669 case OTPGETREGIONINFO:
671 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
676 case MTD_MODE_OTP_FACTORY:
677 if (mtd->get_fact_prot_info)
678 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
680 case MTD_MODE_OTP_USER:
681 if (mtd->get_user_prot_info)
682 ret = mtd->get_user_prot_info(mtd, buf, 4096);
688 if (cmd == OTPGETREGIONCOUNT) {
689 int nbr = ret / sizeof(struct otp_info);
690 ret = copy_to_user(argp, &nbr, sizeof(int));
692 ret = copy_to_user(argp, buf, ret);
702 struct otp_info oinfo;
704 if (mfi->mode != MTD_MODE_OTP_USER)
706 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
708 if (!mtd->lock_user_prot_reg)
710 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
720 if (copy_to_user(argp, mtd->ecclayout,
721 sizeof(struct nand_ecclayout)))
728 if (copy_to_user(argp, &mtd->ecc_stats,
729 sizeof(struct mtd_ecc_stats)))
739 case MTD_MODE_OTP_FACTORY:
740 case MTD_MODE_OTP_USER:
741 ret = otp_select_filemode(mfi, arg);
745 if (!mtd->read_oob || !mtd->write_oob)
749 case MTD_MODE_NORMAL:
765 static const struct file_operations mtd_fops = {
766 .owner = THIS_MODULE,
772 .release = mtd_close,
775 static int __init init_mtdchar(void)
777 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
778 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
783 mtd_class = class_create(THIS_MODULE, "mtd");
785 if (IS_ERR(mtd_class)) {
786 printk(KERN_ERR "Error creating mtd class.\n");
787 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
788 return PTR_ERR(mtd_class);
791 register_mtd_user(¬ifier);
795 static void __exit cleanup_mtdchar(void)
797 unregister_mtd_user(¬ifier);
798 class_destroy(mtd_class);
799 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
802 module_init(init_mtdchar);
803 module_exit(cleanup_mtdchar);
806 MODULE_LICENSE("GPL");
807 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
808 MODULE_DESCRIPTION("Direct character-device access to MTD devices");