2 * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
4 * Character-device access to raw MTD devices.
8 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/compatmac.h>
21 #include <asm/uaccess.h>
23 static struct class *mtd_class;
25 static void mtd_notify_add(struct mtd_info* mtd)
30 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
31 NULL, "mtd%d", mtd->index);
33 class_device_create(mtd_class, NULL,
34 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
35 NULL, "mtd%dro", mtd->index);
38 static void mtd_notify_remove(struct mtd_info* mtd)
43 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
44 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
47 static struct mtd_notifier notifier = {
48 .add = mtd_notify_add,
49 .remove = mtd_notify_remove,
53 * Data structure to hold the pointer to the mtd device as well
54 * as mode information ofr various use cases.
56 struct mtd_file_info {
58 enum mtd_file_modes mode;
61 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
63 struct mtd_file_info *mfi = file->private_data;
64 struct mtd_info *mtd = mfi->mtd;
70 offset += file->f_pos;
79 if (offset >= 0 && offset <= mtd->size)
80 return file->f_pos = offset;
87 static int mtd_open(struct inode *inode, struct file *file)
89 int minor = iminor(inode);
90 int devnum = minor >> 1;
92 struct mtd_file_info *mfi;
94 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
96 if (devnum >= MAX_MTD_DEVICES)
99 /* You can't open the RO devices RW */
100 if ((file->f_mode & 2) && (minor & 1))
103 mtd = get_mtd_device(NULL, devnum);
108 if (MTD_ABSENT == mtd->type) {
113 /* You can't open it RW if it's not a writeable device */
114 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
119 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
125 file->private_data = mfi;
130 /*====================================================================*/
132 static int mtd_close(struct inode *inode, struct file *file)
134 struct mtd_file_info *mfi = file->private_data;
135 struct mtd_info *mtd = mfi->mtd;
137 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
139 /* Only sync if opened RW */
140 if ((file->f_mode & 2) && mtd->sync)
144 file->private_data = NULL;
150 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
151 userspace buffer down and use it directly with readv/writev.
153 #define MAX_KMALLOC_SIZE 0x20000
155 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
157 struct mtd_file_info *mfi = file->private_data;
158 struct mtd_info *mtd = mfi->mtd;
160 size_t total_retlen=0;
165 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
167 if (*ppos + count > mtd->size)
168 count = mtd->size - *ppos;
173 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
174 and pass them directly to the MTD functions */
176 if (count > MAX_KMALLOC_SIZE)
177 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
179 kbuf=kmalloc(count, GFP_KERNEL);
186 if (count > MAX_KMALLOC_SIZE)
187 len = MAX_KMALLOC_SIZE;
192 case MTD_MODE_OTP_FACTORY:
193 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
195 case MTD_MODE_OTP_USER:
196 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
200 struct mtd_oob_ops ops;
202 ops.mode = MTD_OOB_RAW;
207 ret = mtd->read_oob(mtd, *ppos, &ops);
212 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
214 /* Nand returns -EBADMSG on ecc errors, but it returns
215 * the data. For our userspace tools it is important
216 * to dump areas with ecc errors !
217 * For kernel internal usage it also might return -EUCLEAN
218 * to signal the caller that a bitflip has occured and has
219 * been corrected by the ECC algorithm.
220 * Userspace software which accesses NAND this way
221 * must be aware of the fact that it deals with NAND
223 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
225 if (copy_to_user(buf, kbuf, retlen)) {
230 total_retlen += retlen;
248 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
250 struct mtd_file_info *mfi = file->private_data;
251 struct mtd_info *mtd = mfi->mtd;
254 size_t total_retlen=0;
258 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
260 if (*ppos == mtd->size)
263 if (*ppos + count > mtd->size)
264 count = mtd->size - *ppos;
269 if (count > MAX_KMALLOC_SIZE)
270 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
272 kbuf=kmalloc(count, GFP_KERNEL);
279 if (count > MAX_KMALLOC_SIZE)
280 len = MAX_KMALLOC_SIZE;
284 if (copy_from_user(kbuf, buf, len)) {
290 case MTD_MODE_OTP_FACTORY:
293 case MTD_MODE_OTP_USER:
294 if (!mtd->write_user_prot_reg) {
298 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
303 struct mtd_oob_ops ops;
305 ops.mode = MTD_OOB_RAW;
310 ret = mtd->write_oob(mtd, *ppos, &ops);
316 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
320 total_retlen += retlen;
334 /*======================================================================
336 IOCTL calls for getting device parameters.
338 ======================================================================*/
339 static void mtdchar_erase_callback (struct erase_info *instr)
341 wake_up((wait_queue_head_t *)instr->priv);
344 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
345 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
347 struct mtd_info *mtd = mfi->mtd;
351 case MTD_OTP_FACTORY:
352 if (!mtd->read_fact_prot_reg)
355 mfi->mode = MTD_MODE_OTP_FACTORY;
358 if (!mtd->read_fact_prot_reg)
361 mfi->mode = MTD_MODE_OTP_USER;
371 # define otp_select_filemode(f,m) -EOPNOTSUPP
374 static int mtd_ioctl(struct inode *inode, struct file *file,
375 u_int cmd, u_long arg)
377 struct mtd_file_info *mfi = file->private_data;
378 struct mtd_info *mtd = mfi->mtd;
379 void __user *argp = (void __user *)arg;
382 struct mtd_info_user info;
384 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
386 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
388 if (!access_ok(VERIFY_READ, argp, size))
392 if (!access_ok(VERIFY_WRITE, argp, size))
397 case MEMGETREGIONCOUNT:
398 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
402 case MEMGETREGIONINFO:
404 struct region_info_user ur;
406 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
409 if (ur.regionindex >= mtd->numeraseregions)
411 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
412 sizeof(struct mtd_erase_region_info)))
418 info.type = mtd->type;
419 info.flags = mtd->flags;
420 info.size = mtd->size;
421 info.erasesize = mtd->erasesize;
422 info.writesize = mtd->writesize;
423 info.oobsize = mtd->oobsize;
424 /* The below fields are obsolete */
427 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
433 struct erase_info *erase;
435 if(!(file->f_mode & 2))
438 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
442 wait_queue_head_t waitq;
443 DECLARE_WAITQUEUE(wait, current);
445 init_waitqueue_head(&waitq);
447 if (copy_from_user(&erase->addr, argp,
448 sizeof(struct erase_info_user))) {
453 erase->callback = mtdchar_erase_callback;
454 erase->priv = (unsigned long)&waitq;
457 FIXME: Allow INTERRUPTIBLE. Which means
458 not having the wait_queue head on the stack.
460 If the wq_head is on the stack, and we
461 leave because we got interrupted, then the
462 wq_head is no longer there when the
463 callback routine tries to wake us up.
465 ret = mtd->erase(mtd, erase);
467 set_current_state(TASK_UNINTERRUPTIBLE);
468 add_wait_queue(&waitq, &wait);
469 if (erase->state != MTD_ERASE_DONE &&
470 erase->state != MTD_ERASE_FAILED)
472 remove_wait_queue(&waitq, &wait);
473 set_current_state(TASK_RUNNING);
475 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
484 struct mtd_oob_buf buf;
485 struct mtd_oob_ops ops;
488 if(!(file->f_mode & 2))
491 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
494 if (buf.length > 4096)
500 ret = access_ok(VERIFY_READ, buf.ptr,
501 buf.length) ? 0 : EFAULT;
506 ops.ooblen = buf.length;
507 ops.ooboffs = buf.start & (mtd->oobsize - 1);
509 ops.mode = MTD_OOB_PLACE;
511 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
514 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
518 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
523 buf.start &= ~(mtd->oobsize - 1);
524 ret = mtd->write_oob(mtd, buf.start, &ops);
526 if (ops.oobretlen > 0xFFFFFFFFU)
528 retlen = ops.oobretlen;
529 if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
530 &retlen, sizeof(buf.length)))
540 struct mtd_oob_buf buf;
541 struct mtd_oob_ops ops;
543 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
546 if (buf.length > 4096)
552 ret = access_ok(VERIFY_WRITE, buf.ptr,
553 buf.length) ? 0 : -EFAULT;
557 ops.ooblen = buf.length;
558 ops.ooboffs = buf.start & (mtd->oobsize - 1);
560 ops.mode = MTD_OOB_PLACE;
562 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
565 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
569 buf.start &= ~(mtd->oobsize - 1);
570 ret = mtd->read_oob(mtd, buf.start, &ops);
572 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
574 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
584 struct erase_info_user info;
586 if (copy_from_user(&info, argp, sizeof(info)))
592 ret = mtd->lock(mtd, info.start, info.length);
598 struct erase_info_user info;
600 if (copy_from_user(&info, argp, sizeof(info)))
606 ret = mtd->unlock(mtd, info.start, info.length);
610 /* Legacy interface */
613 struct nand_oobinfo oi;
617 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
620 oi.useecc = MTD_NANDECC_AUTOPLACE;
621 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
622 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
624 oi.eccbytes = mtd->ecclayout->eccbytes;
626 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
635 if (copy_from_user(&offs, argp, sizeof(loff_t)))
637 if (!mtd->block_isbad)
640 return mtd->block_isbad(mtd, offs);
648 if (copy_from_user(&offs, argp, sizeof(loff_t)))
650 if (!mtd->block_markbad)
653 return mtd->block_markbad(mtd, offs);
657 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
661 if (copy_from_user(&mode, argp, sizeof(int)))
664 mfi->mode = MTD_MODE_NORMAL;
666 ret = otp_select_filemode(mfi, mode);
672 case OTPGETREGIONCOUNT:
673 case OTPGETREGIONINFO:
675 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
680 case MTD_MODE_OTP_FACTORY:
681 if (mtd->get_fact_prot_info)
682 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
684 case MTD_MODE_OTP_USER:
685 if (mtd->get_user_prot_info)
686 ret = mtd->get_user_prot_info(mtd, buf, 4096);
692 if (cmd == OTPGETREGIONCOUNT) {
693 int nbr = ret / sizeof(struct otp_info);
694 ret = copy_to_user(argp, &nbr, sizeof(int));
696 ret = copy_to_user(argp, buf, ret);
706 struct otp_info info;
708 if (mfi->mode != MTD_MODE_OTP_USER)
710 if (copy_from_user(&info, argp, sizeof(info)))
712 if (!mtd->lock_user_prot_reg)
714 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
724 if (copy_to_user(argp, mtd->ecclayout,
725 sizeof(struct nand_ecclayout)))
732 if (copy_to_user(argp, &mtd->ecc_stats,
733 sizeof(struct mtd_ecc_stats)))
743 case MTD_MODE_OTP_FACTORY:
744 case MTD_MODE_OTP_USER:
745 ret = otp_select_filemode(mfi, arg);
749 if (!mtd->read_oob || !mtd->write_oob)
753 case MTD_MODE_NORMAL:
769 static const struct file_operations mtd_fops = {
770 .owner = THIS_MODULE,
776 .release = mtd_close,
779 static int __init init_mtdchar(void)
781 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
782 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
787 mtd_class = class_create(THIS_MODULE, "mtd");
789 if (IS_ERR(mtd_class)) {
790 printk(KERN_ERR "Error creating mtd class.\n");
791 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
792 return PTR_ERR(mtd_class);
795 register_mtd_user(¬ifier);
799 static void __exit cleanup_mtdchar(void)
801 unregister_mtd_user(¬ifier);
802 class_destroy(mtd_class);
803 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
806 module_init(init_mtdchar);
807 module_exit(cleanup_mtdchar);
810 MODULE_LICENSE("GPL");
811 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
812 MODULE_DESCRIPTION("Direct character-device access to MTD devices");