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");
143 file
->private_data
= NULL
;
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_file_info
*mfi
= file
->private_data
;
157 struct mtd_info
*mtd
= mfi
->mtd
;
159 size_t total_retlen
=0;
164 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
166 if (*ppos
+ count
> mtd
->size
)
167 count
= mtd
->size
- *ppos
;
172 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
173 and pass them directly to the MTD functions */
175 if (count
> MAX_KMALLOC_SIZE
)
176 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
178 kbuf
=kmalloc(count
, GFP_KERNEL
);
185 if (count
> MAX_KMALLOC_SIZE
)
186 len
= MAX_KMALLOC_SIZE
;
191 case MTD_MODE_OTP_FACTORY
:
192 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
194 case MTD_MODE_OTP_USER
:
195 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
199 struct mtd_oob_ops ops
;
201 ops
.mode
= MTD_OOB_RAW
;
206 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
211 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
213 /* Nand returns -EBADMSG on ecc errors, but it returns
214 * the data. For our userspace tools it is important
215 * to dump areas with ecc errors !
216 * For kernel internal usage it also might return -EUCLEAN
217 * to signal the caller that a bitflip has occured and has
218 * been corrected by the ECC algorithm.
219 * Userspace software which accesses NAND this way
220 * must be aware of the fact that it deals with NAND
222 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
224 if (copy_to_user(buf
, kbuf
, retlen
)) {
229 total_retlen
+= retlen
;
247 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
249 struct mtd_file_info
*mfi
= file
->private_data
;
250 struct mtd_info
*mtd
= mfi
->mtd
;
253 size_t total_retlen
=0;
257 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
259 if (*ppos
== mtd
->size
)
262 if (*ppos
+ count
> mtd
->size
)
263 count
= mtd
->size
- *ppos
;
268 if (count
> MAX_KMALLOC_SIZE
)
269 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
271 kbuf
=kmalloc(count
, GFP_KERNEL
);
278 if (count
> MAX_KMALLOC_SIZE
)
279 len
= MAX_KMALLOC_SIZE
;
283 if (copy_from_user(kbuf
, buf
, len
)) {
289 case MTD_MODE_OTP_FACTORY
:
292 case MTD_MODE_OTP_USER
:
293 if (!mtd
->write_user_prot_reg
) {
297 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
302 struct mtd_oob_ops ops
;
304 ops
.mode
= MTD_OOB_RAW
;
309 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
315 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
319 total_retlen
+= retlen
;
333 /*======================================================================
335 IOCTL calls for getting device parameters.
337 ======================================================================*/
338 static void mtdchar_erase_callback (struct erase_info
*instr
)
340 wake_up((wait_queue_head_t
*)instr
->priv
);
343 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
344 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
346 struct mtd_info
*mtd
= mfi
->mtd
;
350 case MTD_OTP_FACTORY
:
351 if (!mtd
->read_fact_prot_reg
)
354 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
357 if (!mtd
->read_fact_prot_reg
)
360 mfi
->mode
= MTD_MODE_OTP_USER
;
370 # define otp_select_filemode(f,m) -EOPNOTSUPP
373 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
374 u_int cmd
, u_long arg
)
376 struct mtd_file_info
*mfi
= file
->private_data
;
377 struct mtd_info
*mtd
= mfi
->mtd
;
378 void __user
*argp
= (void __user
*)arg
;
381 struct mtd_info_user info
;
383 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
385 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
387 if (!access_ok(VERIFY_READ
, argp
, size
))
391 if (!access_ok(VERIFY_WRITE
, argp
, size
))
396 case MEMGETREGIONCOUNT
:
397 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
401 case MEMGETREGIONINFO
:
403 struct region_info_user ur
;
405 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
408 if (ur
.regionindex
>= mtd
->numeraseregions
)
410 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
411 sizeof(struct mtd_erase_region_info
)))
417 info
.type
= mtd
->type
;
418 info
.flags
= mtd
->flags
;
419 info
.size
= mtd
->size
;
420 info
.erasesize
= mtd
->erasesize
;
421 info
.writesize
= mtd
->writesize
;
422 info
.oobsize
= mtd
->oobsize
;
423 /* The below fields are obsolete */
426 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
432 struct erase_info
*erase
;
434 if(!(file
->f_mode
& 2))
437 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
441 wait_queue_head_t waitq
;
442 DECLARE_WAITQUEUE(wait
, current
);
444 init_waitqueue_head(&waitq
);
446 if (copy_from_user(&erase
->addr
, argp
,
447 sizeof(struct erase_info_user
))) {
452 erase
->callback
= mtdchar_erase_callback
;
453 erase
->priv
= (unsigned long)&waitq
;
456 FIXME: Allow INTERRUPTIBLE. Which means
457 not having the wait_queue head on the stack.
459 If the wq_head is on the stack, and we
460 leave because we got interrupted, then the
461 wq_head is no longer there when the
462 callback routine tries to wake us up.
464 ret
= mtd
->erase(mtd
, erase
);
466 set_current_state(TASK_UNINTERRUPTIBLE
);
467 add_wait_queue(&waitq
, &wait
);
468 if (erase
->state
!= MTD_ERASE_DONE
&&
469 erase
->state
!= MTD_ERASE_FAILED
)
471 remove_wait_queue(&waitq
, &wait
);
472 set_current_state(TASK_RUNNING
);
474 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
483 struct mtd_oob_buf buf
;
484 struct mtd_oob_ops ops
;
486 if(!(file
->f_mode
& 2))
489 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
492 if (buf
.length
> 4096)
498 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
499 buf
.length
) ? 0 : EFAULT
;
504 ops
.ooblen
= buf
.length
;
505 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
507 ops
.mode
= MTD_OOB_PLACE
;
509 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
512 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
516 if (copy_from_user(ops
.oobbuf
, buf
.ptr
, buf
.length
)) {
521 buf
.start
&= ~(mtd
->oobsize
- 1);
522 ret
= mtd
->write_oob(mtd
, buf
.start
, &ops
);
524 if (copy_to_user(argp
+ sizeof(uint32_t), &ops
.oobretlen
,
535 struct mtd_oob_buf buf
;
536 struct mtd_oob_ops ops
;
538 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
541 if (buf
.length
> 4096)
547 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
548 buf
.length
) ? 0 : -EFAULT
;
552 ops
.ooblen
= buf
.length
;
553 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
555 ops
.mode
= MTD_OOB_PLACE
;
557 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
560 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
564 buf
.start
&= ~(mtd
->oobsize
- 1);
565 ret
= mtd
->read_oob(mtd
, buf
.start
, &ops
);
567 if (put_user(ops
.oobretlen
, (uint32_t __user
*)argp
))
569 else if (ops
.oobretlen
&& copy_to_user(buf
.ptr
, ops
.oobbuf
,
579 struct erase_info_user info
;
581 if (copy_from_user(&info
, argp
, sizeof(info
)))
587 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
593 struct erase_info_user info
;
595 if (copy_from_user(&info
, argp
, sizeof(info
)))
601 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
605 /* Legacy interface */
608 struct nand_oobinfo oi
;
612 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
615 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
616 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
617 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
619 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
621 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
630 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
632 if (!mtd
->block_isbad
)
635 return mtd
->block_isbad(mtd
, offs
);
643 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
645 if (!mtd
->block_markbad
)
648 return mtd
->block_markbad(mtd
, offs
);
652 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
656 if (copy_from_user(&mode
, argp
, sizeof(int)))
659 mfi
->mode
= MTD_MODE_NORMAL
;
661 ret
= otp_select_filemode(mfi
, mode
);
667 case OTPGETREGIONCOUNT
:
668 case OTPGETREGIONINFO
:
670 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
675 case MTD_MODE_OTP_FACTORY
:
676 if (mtd
->get_fact_prot_info
)
677 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
679 case MTD_MODE_OTP_USER
:
680 if (mtd
->get_user_prot_info
)
681 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
687 if (cmd
== OTPGETREGIONCOUNT
) {
688 int nbr
= ret
/ sizeof(struct otp_info
);
689 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
691 ret
= copy_to_user(argp
, buf
, ret
);
701 struct otp_info info
;
703 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
705 if (copy_from_user(&info
, argp
, sizeof(info
)))
707 if (!mtd
->lock_user_prot_reg
)
709 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
719 if (copy_to_user(argp
, mtd
->ecclayout
,
720 sizeof(struct nand_ecclayout
)))
727 if (copy_to_user(argp
, &mtd
->ecc_stats
,
728 sizeof(struct mtd_ecc_stats
)))
738 case MTD_MODE_OTP_FACTORY
:
739 case MTD_MODE_OTP_USER
:
740 ret
= otp_select_filemode(mfi
, arg
);
744 if (!mtd
->read_oob
|| !mtd
->write_oob
)
748 case MTD_MODE_NORMAL
:
764 static const struct file_operations mtd_fops
= {
765 .owner
= THIS_MODULE
,
771 .release
= mtd_close
,
774 static int __init
init_mtdchar(void)
776 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
777 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
782 mtd_class
= class_create(THIS_MODULE
, "mtd");
784 if (IS_ERR(mtd_class
)) {
785 printk(KERN_ERR
"Error creating mtd class.\n");
786 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
787 return PTR_ERR(mtd_class
);
790 register_mtd_user(¬ifier
);
794 static void __exit
cleanup_mtdchar(void)
796 unregister_mtd_user(¬ifier
);
797 class_destroy(mtd_class
);
798 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
801 module_init(init_mtdchar
);
802 module_exit(cleanup_mtdchar
);
805 MODULE_LICENSE("GPL");
806 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
807 MODULE_DESCRIPTION("Direct character-device access to MTD devices");