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>
15 #include <linux/smp_lock.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 device_create_drvdata(mtd_class
, NULL
,
30 MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2),
31 NULL
, "mtd%d", mtd
->index
);
33 device_create_drvdata(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 device_destroy(mtd_class
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2));
44 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;
93 struct mtd_file_info
*mfi
;
95 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
97 if (devnum
>= MAX_MTD_DEVICES
)
100 /* You can't open the RO devices RW */
101 if ((file
->f_mode
& 2) && (minor
& 1))
105 mtd
= get_mtd_device(NULL
, devnum
);
112 if (MTD_ABSENT
== mtd
->type
) {
118 /* You can't open it RW if it's not a writeable device */
119 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
125 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
132 file
->private_data
= mfi
;
139 /*====================================================================*/
141 static int mtd_close(struct inode
*inode
, struct file
*file
)
143 struct mtd_file_info
*mfi
= file
->private_data
;
144 struct mtd_info
*mtd
= mfi
->mtd
;
146 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
148 /* Only sync if opened RW */
149 if ((file
->f_mode
& 2) && mtd
->sync
)
153 file
->private_data
= NULL
;
159 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
160 userspace buffer down and use it directly with readv/writev.
162 #define MAX_KMALLOC_SIZE 0x20000
164 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
166 struct mtd_file_info
*mfi
= file
->private_data
;
167 struct mtd_info
*mtd
= mfi
->mtd
;
169 size_t total_retlen
=0;
174 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
176 if (*ppos
+ count
> mtd
->size
)
177 count
= mtd
->size
- *ppos
;
182 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
183 and pass them directly to the MTD functions */
185 if (count
> MAX_KMALLOC_SIZE
)
186 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
188 kbuf
=kmalloc(count
, GFP_KERNEL
);
195 if (count
> MAX_KMALLOC_SIZE
)
196 len
= MAX_KMALLOC_SIZE
;
201 case MTD_MODE_OTP_FACTORY
:
202 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
204 case MTD_MODE_OTP_USER
:
205 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
209 struct mtd_oob_ops ops
;
211 ops
.mode
= MTD_OOB_RAW
;
216 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
221 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
223 /* Nand returns -EBADMSG on ecc errors, but it returns
224 * the data. For our userspace tools it is important
225 * to dump areas with ecc errors !
226 * For kernel internal usage it also might return -EUCLEAN
227 * to signal the caller that a bitflip has occured and has
228 * been corrected by the ECC algorithm.
229 * Userspace software which accesses NAND this way
230 * must be aware of the fact that it deals with NAND
232 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
234 if (copy_to_user(buf
, kbuf
, retlen
)) {
239 total_retlen
+= retlen
;
257 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
259 struct mtd_file_info
*mfi
= file
->private_data
;
260 struct mtd_info
*mtd
= mfi
->mtd
;
263 size_t total_retlen
=0;
267 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
269 if (*ppos
== mtd
->size
)
272 if (*ppos
+ count
> mtd
->size
)
273 count
= mtd
->size
- *ppos
;
278 if (count
> MAX_KMALLOC_SIZE
)
279 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
281 kbuf
=kmalloc(count
, GFP_KERNEL
);
288 if (count
> MAX_KMALLOC_SIZE
)
289 len
= MAX_KMALLOC_SIZE
;
293 if (copy_from_user(kbuf
, buf
, len
)) {
299 case MTD_MODE_OTP_FACTORY
:
302 case MTD_MODE_OTP_USER
:
303 if (!mtd
->write_user_prot_reg
) {
307 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
312 struct mtd_oob_ops ops
;
314 ops
.mode
= MTD_OOB_RAW
;
319 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
325 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
329 total_retlen
+= retlen
;
343 /*======================================================================
345 IOCTL calls for getting device parameters.
347 ======================================================================*/
348 static void mtdchar_erase_callback (struct erase_info
*instr
)
350 wake_up((wait_queue_head_t
*)instr
->priv
);
353 #ifdef CONFIG_HAVE_MTD_OTP
354 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
356 struct mtd_info
*mtd
= mfi
->mtd
;
360 case MTD_OTP_FACTORY
:
361 if (!mtd
->read_fact_prot_reg
)
364 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
367 if (!mtd
->read_fact_prot_reg
)
370 mfi
->mode
= MTD_MODE_OTP_USER
;
380 # define otp_select_filemode(f,m) -EOPNOTSUPP
383 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
384 u_int cmd
, u_long arg
)
386 struct mtd_file_info
*mfi
= file
->private_data
;
387 struct mtd_info
*mtd
= mfi
->mtd
;
388 void __user
*argp
= (void __user
*)arg
;
391 struct mtd_info_user info
;
393 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
395 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
397 if (!access_ok(VERIFY_READ
, argp
, size
))
401 if (!access_ok(VERIFY_WRITE
, argp
, size
))
406 case MEMGETREGIONCOUNT
:
407 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
411 case MEMGETREGIONINFO
:
413 struct region_info_user ur
;
415 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
418 if (ur
.regionindex
>= mtd
->numeraseregions
)
420 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
421 sizeof(struct mtd_erase_region_info
)))
427 info
.type
= mtd
->type
;
428 info
.flags
= mtd
->flags
;
429 info
.size
= mtd
->size
;
430 info
.erasesize
= mtd
->erasesize
;
431 info
.writesize
= mtd
->writesize
;
432 info
.oobsize
= mtd
->oobsize
;
433 /* The below fields are obsolete */
436 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
442 struct erase_info
*erase
;
444 if(!(file
->f_mode
& 2))
447 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
451 wait_queue_head_t waitq
;
452 DECLARE_WAITQUEUE(wait
, current
);
454 init_waitqueue_head(&waitq
);
456 if (copy_from_user(&erase
->addr
, argp
,
457 sizeof(struct erase_info_user
))) {
462 erase
->callback
= mtdchar_erase_callback
;
463 erase
->priv
= (unsigned long)&waitq
;
466 FIXME: Allow INTERRUPTIBLE. Which means
467 not having the wait_queue head on the stack.
469 If the wq_head is on the stack, and we
470 leave because we got interrupted, then the
471 wq_head is no longer there when the
472 callback routine tries to wake us up.
474 ret
= mtd
->erase(mtd
, erase
);
476 set_current_state(TASK_UNINTERRUPTIBLE
);
477 add_wait_queue(&waitq
, &wait
);
478 if (erase
->state
!= MTD_ERASE_DONE
&&
479 erase
->state
!= MTD_ERASE_FAILED
)
481 remove_wait_queue(&waitq
, &wait
);
482 set_current_state(TASK_RUNNING
);
484 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
493 struct mtd_oob_buf buf
;
494 struct mtd_oob_ops ops
;
495 struct mtd_oob_buf __user
*user_buf
= argp
;
498 if(!(file
->f_mode
& 2))
501 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
504 if (buf
.length
> 4096)
510 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
511 buf
.length
) ? 0 : EFAULT
;
516 ops
.ooblen
= buf
.length
;
517 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
519 ops
.mode
= MTD_OOB_PLACE
;
521 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
524 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
528 if (copy_from_user(ops
.oobbuf
, buf
.ptr
, buf
.length
)) {
533 buf
.start
&= ~(mtd
->oobsize
- 1);
534 ret
= mtd
->write_oob(mtd
, buf
.start
, &ops
);
536 if (ops
.oobretlen
> 0xFFFFFFFFU
)
538 retlen
= ops
.oobretlen
;
539 if (copy_to_user(&user_buf
->length
, &retlen
, sizeof(buf
.length
)))
549 struct mtd_oob_buf buf
;
550 struct mtd_oob_ops ops
;
552 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
555 if (buf
.length
> 4096)
561 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
562 buf
.length
) ? 0 : -EFAULT
;
566 ops
.ooblen
= buf
.length
;
567 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
569 ops
.mode
= MTD_OOB_PLACE
;
571 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
574 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
578 buf
.start
&= ~(mtd
->oobsize
- 1);
579 ret
= mtd
->read_oob(mtd
, buf
.start
, &ops
);
581 if (put_user(ops
.oobretlen
, (uint32_t __user
*)argp
))
583 else if (ops
.oobretlen
&& copy_to_user(buf
.ptr
, ops
.oobbuf
,
593 struct erase_info_user einfo
;
595 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
601 ret
= mtd
->lock(mtd
, einfo
.start
, einfo
.length
);
607 struct erase_info_user einfo
;
609 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
615 ret
= mtd
->unlock(mtd
, einfo
.start
, einfo
.length
);
619 /* Legacy interface */
622 struct nand_oobinfo oi
;
626 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
629 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
630 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
631 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
633 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
635 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
644 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
646 if (!mtd
->block_isbad
)
649 return mtd
->block_isbad(mtd
, offs
);
657 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
659 if (!mtd
->block_markbad
)
662 return mtd
->block_markbad(mtd
, offs
);
666 #ifdef CONFIG_HAVE_MTD_OTP
670 if (copy_from_user(&mode
, argp
, sizeof(int)))
673 mfi
->mode
= MTD_MODE_NORMAL
;
675 ret
= otp_select_filemode(mfi
, mode
);
681 case OTPGETREGIONCOUNT
:
682 case OTPGETREGIONINFO
:
684 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
689 case MTD_MODE_OTP_FACTORY
:
690 if (mtd
->get_fact_prot_info
)
691 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
693 case MTD_MODE_OTP_USER
:
694 if (mtd
->get_user_prot_info
)
695 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
701 if (cmd
== OTPGETREGIONCOUNT
) {
702 int nbr
= ret
/ sizeof(struct otp_info
);
703 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
705 ret
= copy_to_user(argp
, buf
, ret
);
715 struct otp_info oinfo
;
717 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
719 if (copy_from_user(&oinfo
, argp
, sizeof(oinfo
)))
721 if (!mtd
->lock_user_prot_reg
)
723 ret
= mtd
->lock_user_prot_reg(mtd
, oinfo
.start
, oinfo
.length
);
733 if (copy_to_user(argp
, mtd
->ecclayout
,
734 sizeof(struct nand_ecclayout
)))
741 if (copy_to_user(argp
, &mtd
->ecc_stats
,
742 sizeof(struct mtd_ecc_stats
)))
752 case MTD_MODE_OTP_FACTORY
:
753 case MTD_MODE_OTP_USER
:
754 ret
= otp_select_filemode(mfi
, arg
);
758 if (!mtd
->read_oob
|| !mtd
->write_oob
)
762 case MTD_MODE_NORMAL
:
778 static const struct file_operations mtd_fops
= {
779 .owner
= THIS_MODULE
,
785 .release
= mtd_close
,
788 static int __init
init_mtdchar(void)
790 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
791 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
796 mtd_class
= class_create(THIS_MODULE
, "mtd");
798 if (IS_ERR(mtd_class
)) {
799 printk(KERN_ERR
"Error creating mtd class.\n");
800 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
801 return PTR_ERR(mtd_class
);
804 register_mtd_user(¬ifier
);
808 static void __exit
cleanup_mtdchar(void)
810 unregister_mtd_user(¬ifier
);
811 class_destroy(mtd_class
);
812 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
815 module_init(init_mtdchar
);
816 module_exit(cleanup_mtdchar
);
819 MODULE_LICENSE("GPL");
820 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
821 MODULE_DESCRIPTION("Direct character-device access to MTD devices");