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/config.h>
9 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/compatmac.h>
20 #include <asm/uaccess.h>
22 static struct class *mtd_class;
24 static void mtd_notify_add(struct mtd_info* mtd)
29 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
30 NULL, "mtd%d", mtd->index);
32 class_device_create(mtd_class, NULL,
33 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
34 NULL, "mtd%dro", mtd->index);
37 static void mtd_notify_remove(struct mtd_info* mtd)
42 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
43 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
46 static struct mtd_notifier notifier = {
47 .add = mtd_notify_add,
48 .remove = mtd_notify_remove,
52 * Data structure to hold the pointer to the mtd device as well
53 * as mode information ofr various use cases.
55 struct mtd_file_info {
57 enum mtd_file_modes mode;
60 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
62 struct mtd_file_info *mfi = file->private_data;
63 struct mtd_info *mtd = mfi->mtd;
71 offset += file->f_pos;
81 if (offset >= 0 && offset < mtd->size)
82 return file->f_pos = offset;
89 static int mtd_open(struct inode *inode, struct file *file)
91 int minor = iminor(inode);
92 int devnum = minor >> 1;
94 struct mtd_file_info *mfi;
96 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
98 if (devnum >= MAX_MTD_DEVICES)
101 /* You can't open the RO devices RW */
102 if ((file->f_mode & 2) && (minor & 1))
105 mtd = get_mtd_device(NULL, devnum);
110 if (MTD_ABSENT == mtd->type) {
115 /* You can't open it RW if it's not a writeable device */
116 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
121 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
127 file->private_data = mfi;
132 /*====================================================================*/
134 static int mtd_close(struct inode *inode, struct file *file)
136 struct mtd_file_info *mfi = file->private_data;
137 struct mtd_info *mtd = mfi->mtd;
139 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
145 file->private_data = NULL;
151 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
152 userspace buffer down and use it directly with readv/writev.
154 #define MAX_KMALLOC_SIZE 0x20000
156 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
158 struct mtd_file_info *mfi = file->private_data;
159 struct mtd_info *mtd = mfi->mtd;
161 size_t total_retlen=0;
166 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
168 if (*ppos + count > mtd->size)
169 count = mtd->size - *ppos;
174 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
175 and pass them directly to the MTD functions */
177 if (count > MAX_KMALLOC_SIZE)
178 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
180 kbuf=kmalloc(count, GFP_KERNEL);
187 if (count > MAX_KMALLOC_SIZE)
188 len = MAX_KMALLOC_SIZE;
193 case MTD_MODE_OTP_FACTORY:
194 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
196 case MTD_MODE_OTP_USER:
197 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
201 struct mtd_oob_ops ops;
203 ops.mode = MTD_OOB_RAW;
208 ret = mtd->read_oob(mtd, *ppos, &ops);
213 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
215 /* Nand returns -EBADMSG on ecc errors, but it returns
216 * the data. For our userspace tools it is important
217 * to dump areas with ecc errors !
218 * For kernel internal usage it also might return -EUCLEAN
219 * to signal the caller that a bitflip has occured and has
220 * been corrected by the ECC algorithm.
221 * Userspace software which accesses NAND this way
222 * must be aware of the fact that it deals with NAND
224 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
226 if (copy_to_user(buf, kbuf, retlen)) {
231 total_retlen += retlen;
249 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
251 struct mtd_file_info *mfi = file->private_data;
252 struct mtd_info *mtd = mfi->mtd;
255 size_t total_retlen=0;
259 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
261 if (*ppos == mtd->size)
264 if (*ppos + count > mtd->size)
265 count = mtd->size - *ppos;
270 if (count > MAX_KMALLOC_SIZE)
271 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
273 kbuf=kmalloc(count, GFP_KERNEL);
280 if (count > MAX_KMALLOC_SIZE)
281 len = MAX_KMALLOC_SIZE;
285 if (copy_from_user(kbuf, buf, len)) {
291 case MTD_MODE_OTP_FACTORY:
294 case MTD_MODE_OTP_USER:
295 if (!mtd->write_user_prot_reg) {
299 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
304 struct mtd_oob_ops ops;
306 ops.mode = MTD_OOB_RAW;
311 ret = mtd->write_oob(mtd, *ppos, &ops);
317 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
321 total_retlen += retlen;
335 /*======================================================================
337 IOCTL calls for getting device parameters.
339 ======================================================================*/
340 static void mtdchar_erase_callback (struct erase_info *instr)
342 wake_up((wait_queue_head_t *)instr->priv);
345 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
346 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
348 struct mtd_info *mtd = mfi->mtd;
352 case MTD_OTP_FACTORY:
353 if (!mtd->read_fact_prot_reg)
356 mfi->mode = MTD_MODE_OTP_FACTORY;
359 if (!mtd->read_fact_prot_reg)
362 mfi->mode = MTD_MODE_OTP_USER;
372 # define otp_select_filemode(f,m) -EOPNOTSUPP
375 static int mtd_ioctl(struct inode *inode, struct file *file,
376 u_int cmd, u_long arg)
378 struct mtd_file_info *mfi = file->private_data;
379 struct mtd_info *mtd = mfi->mtd;
380 void __user *argp = (void __user *)arg;
383 struct mtd_info_user info;
385 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
387 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
389 if (!access_ok(VERIFY_READ, argp, size))
393 if (!access_ok(VERIFY_WRITE, argp, size))
398 case MEMGETREGIONCOUNT:
399 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
403 case MEMGETREGIONINFO:
405 struct region_info_user ur;
407 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
410 if (ur.regionindex >= mtd->numeraseregions)
412 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
413 sizeof(struct mtd_erase_region_info)))
419 info.type = mtd->type;
420 info.flags = mtd->flags;
421 info.size = mtd->size;
422 info.erasesize = mtd->erasesize;
423 info.writesize = mtd->writesize;
424 info.oobsize = mtd->oobsize;
425 info.ecctype = mtd->ecctype;
426 info.eccsize = mtd->eccsize;
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=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
442 wait_queue_head_t waitq;
443 DECLARE_WAITQUEUE(wait, current);
445 init_waitqueue_head(&waitq);
447 memset (erase,0,sizeof(struct erase_info));
448 if (copy_from_user(&erase->addr, argp,
449 sizeof(struct erase_info_user))) {
454 erase->callback = mtdchar_erase_callback;
455 erase->priv = (unsigned long)&waitq;
458 FIXME: Allow INTERRUPTIBLE. Which means
459 not having the wait_queue head on the stack.
461 If the wq_head is on the stack, and we
462 leave because we got interrupted, then the
463 wq_head is no longer there when the
464 callback routine tries to wake us up.
466 ret = mtd->erase(mtd, erase);
468 set_current_state(TASK_UNINTERRUPTIBLE);
469 add_wait_queue(&waitq, &wait);
470 if (erase->state != MTD_ERASE_DONE &&
471 erase->state != MTD_ERASE_FAILED)
473 remove_wait_queue(&waitq, &wait);
474 set_current_state(TASK_RUNNING);
476 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
485 struct mtd_oob_buf buf;
486 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.len = buf.length;
507 ops.ooblen = buf.length;
508 ops.ooboffs = buf.start & (mtd->oobsize - 1);
510 ops.mode = MTD_OOB_PLACE;
512 if (ops.ooboffs && ops.len > (mtd->oobsize - ops.ooboffs))
515 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
519 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
524 buf.start &= ~(mtd->oobsize - 1);
525 ret = mtd->write_oob(mtd, buf.start, &ops);
527 if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
538 struct mtd_oob_buf buf;
539 struct mtd_oob_ops ops;
541 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
544 if (buf.length > 4096)
550 ret = access_ok(VERIFY_WRITE, buf.ptr,
551 buf.length) ? 0 : -EFAULT;
555 ops.len = buf.length;
556 ops.ooblen = buf.length;
557 ops.ooboffs = buf.start & (mtd->oobsize - 1);
559 ops.mode = MTD_OOB_PLACE;
561 if (ops.ooboffs && ops.len > (mtd->oobsize - ops.ooboffs))
564 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
568 buf.start &= ~(mtd->oobsize - 1);
569 ret = mtd->read_oob(mtd, buf.start, &ops);
571 if (put_user(ops.retlen, (uint32_t __user *)argp))
573 else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
583 struct erase_info_user info;
585 if (copy_from_user(&info, argp, sizeof(info)))
591 ret = mtd->lock(mtd, info.start, info.length);
597 struct erase_info_user info;
599 if (copy_from_user(&info, argp, sizeof(info)))
605 ret = mtd->unlock(mtd, info.start, info.length);
609 /* Legacy interface */
612 struct nand_oobinfo oi;
616 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
619 oi.useecc = MTD_NANDECC_AUTOPLACE;
620 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
621 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
624 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
633 if (copy_from_user(&offs, argp, sizeof(loff_t)))
635 if (!mtd->block_isbad)
638 return mtd->block_isbad(mtd, offs);
646 if (copy_from_user(&offs, argp, sizeof(loff_t)))
648 if (!mtd->block_markbad)
651 return mtd->block_markbad(mtd, offs);
655 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
659 if (copy_from_user(&mode, argp, sizeof(int)))
662 mfi->mode = MTD_MODE_NORMAL;
664 ret = otp_select_filemode(mfi, mode);
670 case OTPGETREGIONCOUNT:
671 case OTPGETREGIONINFO:
673 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
678 case MTD_MODE_OTP_FACTORY:
679 if (mtd->get_fact_prot_info)
680 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
682 case MTD_MODE_OTP_USER:
683 if (mtd->get_user_prot_info)
684 ret = mtd->get_user_prot_info(mtd, buf, 4096);
690 if (cmd == OTPGETREGIONCOUNT) {
691 int nbr = ret / sizeof(struct otp_info);
692 ret = copy_to_user(argp, &nbr, sizeof(int));
694 ret = copy_to_user(argp, buf, ret);
704 struct otp_info info;
706 if (mfi->mode != MTD_MODE_OTP_USER)
708 if (copy_from_user(&info, argp, sizeof(info)))
710 if (!mtd->lock_user_prot_reg)
712 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
722 if (copy_to_user(argp, &mtd->ecclayout,
723 sizeof(struct nand_ecclayout)))
730 if (copy_to_user(argp, &mtd->ecc_stats,
731 sizeof(struct mtd_ecc_stats)))
741 case MTD_MODE_OTP_FACTORY:
742 case MTD_MODE_OTP_USER:
743 ret = otp_select_filemode(mfi, arg);
747 if (!mtd->read_oob || !mtd->write_oob)
751 case MTD_MODE_NORMAL:
767 static struct file_operations mtd_fops = {
768 .owner = THIS_MODULE,
774 .release = mtd_close,
777 static int __init init_mtdchar(void)
779 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
780 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
785 mtd_class = class_create(THIS_MODULE, "mtd");
787 if (IS_ERR(mtd_class)) {
788 printk(KERN_ERR "Error creating mtd class.\n");
789 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
790 return PTR_ERR(mtd_class);
793 register_mtd_user(¬ifier);
797 static void __exit cleanup_mtdchar(void)
799 unregister_mtd_user(¬ifier);
800 class_destroy(mtd_class);
801 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
804 module_init(init_mtdchar);
805 module_exit(cleanup_mtdchar);
808 MODULE_LICENSE("GPL");
809 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
810 MODULE_DESCRIPTION("Direct character-device access to MTD devices");