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 * We use file->private_data to store a pointer to the MTDdevice.
53 * Since alighment is at least 32 bits, we have 2 bits free for OTP
57 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
59 #define MTD_MODE_OTP_FACT 1
60 #define MTD_MODE_OTP_USER 2
61 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
63 #define SET_MTD_MODE(file, mode) \
64 do { long __p = (long)((file)->private_data); \
65 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
67 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
69 struct mtd_info *mtd = TO_MTD(file);
77 offset += file->f_pos;
87 if (offset >= 0 && offset < mtd->size)
88 return file->f_pos = offset;
95 static int mtd_open(struct inode *inode, struct file *file)
97 int minor = iminor(inode);
98 int devnum = minor >> 1;
101 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
103 if (devnum >= MAX_MTD_DEVICES)
106 /* You can't open the RO devices RW */
107 if ((file->f_mode & 2) && (minor & 1))
110 mtd = get_mtd_device(NULL, devnum);
115 if (MTD_ABSENT == mtd->type) {
120 file->private_data = mtd;
122 /* You can't open it RW if it's not a writeable device */
123 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
131 /*====================================================================*/
133 static int mtd_close(struct inode *inode, struct file *file)
135 struct mtd_info *mtd;
137 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
149 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
150 userspace buffer down and use it directly with readv/writev.
152 #define MAX_KMALLOC_SIZE 0x20000
154 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
156 struct mtd_info *mtd = TO_MTD(file);
158 size_t total_retlen=0;
163 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
165 if (*ppos + count > mtd->size)
166 count = mtd->size - *ppos;
171 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
172 and pass them directly to the MTD functions */
174 if (count > MAX_KMALLOC_SIZE)
175 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
177 kbuf=kmalloc(count, GFP_KERNEL);
184 if (count > MAX_KMALLOC_SIZE)
185 len = MAX_KMALLOC_SIZE;
189 switch (MTD_MODE(file)) {
190 case MTD_MODE_OTP_FACT:
191 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
193 case MTD_MODE_OTP_USER:
194 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
197 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
199 /* Nand returns -EBADMSG on ecc errors, but it returns
200 * the data. For our userspace tools it is important
201 * to dump areas with ecc errors !
202 * For kernel internal usage it also might return -EUCLEAN
203 * to signal the caller that a bitflip has occured and has
204 * been corrected by the ECC algorithm.
205 * Userspace software which accesses NAND this way
206 * must be aware of the fact that it deals with NAND
208 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
210 if (copy_to_user(buf, kbuf, retlen)) {
215 total_retlen += retlen;
233 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
235 struct mtd_info *mtd = TO_MTD(file);
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)) {
273 switch (MTD_MODE(file)) {
274 case MTD_MODE_OTP_FACT:
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);
285 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
289 total_retlen += retlen;
303 /*======================================================================
305 IOCTL calls for getting device parameters.
307 ======================================================================*/
308 static void mtdchar_erase_callback (struct erase_info *instr)
310 wake_up((wait_queue_head_t *)instr->priv);
313 static int mtd_ioctl(struct inode *inode, struct file *file,
314 u_int cmd, u_long arg)
316 struct mtd_info *mtd = TO_MTD(file);
317 void __user *argp = (void __user *)arg;
321 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
323 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
325 if (!access_ok(VERIFY_READ, argp, size))
329 if (!access_ok(VERIFY_WRITE, argp, size))
334 case MEMGETREGIONCOUNT:
335 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
339 case MEMGETREGIONINFO:
341 struct region_info_user ur;
343 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
346 if (ur.regionindex >= mtd->numeraseregions)
348 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
349 sizeof(struct mtd_erase_region_info)))
355 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
361 struct erase_info *erase;
363 if(!(file->f_mode & 2))
366 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
370 wait_queue_head_t waitq;
371 DECLARE_WAITQUEUE(wait, current);
373 init_waitqueue_head(&waitq);
375 memset (erase,0,sizeof(struct erase_info));
376 if (copy_from_user(&erase->addr, argp,
377 sizeof(struct erase_info_user))) {
382 erase->callback = mtdchar_erase_callback;
383 erase->priv = (unsigned long)&waitq;
386 FIXME: Allow INTERRUPTIBLE. Which means
387 not having the wait_queue head on the stack.
389 If the wq_head is on the stack, and we
390 leave because we got interrupted, then the
391 wq_head is no longer there when the
392 callback routine tries to wake us up.
394 ret = mtd->erase(mtd, erase);
396 set_current_state(TASK_UNINTERRUPTIBLE);
397 add_wait_queue(&waitq, &wait);
398 if (erase->state != MTD_ERASE_DONE &&
399 erase->state != MTD_ERASE_FAILED)
401 remove_wait_queue(&waitq, &wait);
402 set_current_state(TASK_RUNNING);
404 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
413 struct mtd_oob_buf buf;
414 struct mtd_oob_ops ops;
416 if(!(file->f_mode & 2))
419 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
422 if (buf.length > 4096)
428 ret = access_ok(VERIFY_READ, buf.ptr,
429 buf.length) ? 0 : EFAULT;
434 ops.len = buf.length;
435 ops.ooblen = mtd->oobsize;
436 ops.ooboffs = buf.start & (mtd->oobsize - 1);
438 ops.mode = MTD_OOB_PLACE;
440 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
443 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
447 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
452 buf.start &= ~(mtd->oobsize - 1);
453 ret = mtd->write_oob(mtd, buf.start, &ops);
455 if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
466 struct mtd_oob_buf buf;
467 struct mtd_oob_ops ops;
469 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
472 if (buf.length > 4096)
478 ret = access_ok(VERIFY_WRITE, buf.ptr,
479 buf.length) ? 0 : -EFAULT;
483 ops.len = buf.length;
484 ops.ooblen = mtd->oobsize;
485 ops.ooboffs = buf.start & (mtd->oobsize - 1);
487 ops.mode = MTD_OOB_PLACE;
489 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
492 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
496 buf.start &= ~(mtd->oobsize - 1);
497 ret = mtd->read_oob(mtd, buf.start, &ops);
499 if (put_user(ops.retlen, (uint32_t __user *)argp))
501 else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
511 struct erase_info_user info;
513 if (copy_from_user(&info, argp, sizeof(info)))
519 ret = mtd->lock(mtd, info.start, info.length);
525 struct erase_info_user info;
527 if (copy_from_user(&info, argp, sizeof(info)))
533 ret = mtd->unlock(mtd, info.start, info.length);
537 /* Legacy interface */
540 struct nand_oobinfo oi;
544 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
547 oi.useecc = MTD_NANDECC_AUTOPLACE;
548 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
549 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
552 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
562 if (copy_to_user(argp, &mtd->ecclayout,
563 sizeof(struct nand_ecclayout)))
571 if (copy_from_user(&offs, argp, sizeof(loff_t)))
573 if (!mtd->block_isbad)
576 return mtd->block_isbad(mtd, offs);
584 if (copy_from_user(&offs, argp, sizeof(loff_t)))
586 if (!mtd->block_markbad)
589 return mtd->block_markbad(mtd, offs);
593 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
597 if (copy_from_user(&mode, argp, sizeof(int)))
599 SET_MTD_MODE(file, 0);
601 case MTD_OTP_FACTORY:
602 if (!mtd->read_fact_prot_reg)
605 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
608 if (!mtd->read_fact_prot_reg)
611 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
622 case OTPGETREGIONCOUNT:
623 case OTPGETREGIONINFO:
625 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
629 switch (MTD_MODE(file)) {
630 case MTD_MODE_OTP_FACT:
631 if (mtd->get_fact_prot_info)
632 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
634 case MTD_MODE_OTP_USER:
635 if (mtd->get_user_prot_info)
636 ret = mtd->get_user_prot_info(mtd, buf, 4096);
640 if (cmd == OTPGETREGIONCOUNT) {
641 int nbr = ret / sizeof(struct otp_info);
642 ret = copy_to_user(argp, &nbr, sizeof(int));
644 ret = copy_to_user(argp, buf, ret);
654 struct otp_info info;
656 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
658 if (copy_from_user(&info, argp, sizeof(info)))
660 if (!mtd->lock_user_prot_reg)
662 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
674 static struct file_operations mtd_fops = {
675 .owner = THIS_MODULE,
681 .release = mtd_close,
684 static int __init init_mtdchar(void)
686 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
687 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
692 mtd_class = class_create(THIS_MODULE, "mtd");
694 if (IS_ERR(mtd_class)) {
695 printk(KERN_ERR "Error creating mtd class.\n");
696 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
697 return PTR_ERR(mtd_class);
700 register_mtd_user(¬ifier);
704 static void __exit cleanup_mtdchar(void)
706 unregister_mtd_user(¬ifier);
707 class_destroy(mtd_class);
708 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
711 module_init(init_mtdchar);
712 module_exit(cleanup_mtdchar);
715 MODULE_LICENSE("GPL");
716 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
717 MODULE_DESCRIPTION("Direct character-device access to MTD devices");