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(mtd_class
, NULL
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2),
30 NULL
, "mtd%d", mtd
->index
);
32 device_create(mtd_class
, NULL
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2+1),
33 NULL
, "mtd%dro", mtd
->index
);
36 static void mtd_notify_remove(struct mtd_info
* mtd
)
41 device_destroy(mtd_class
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2));
42 device_destroy(mtd_class
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2+1));
45 static struct mtd_notifier notifier
= {
46 .add
= mtd_notify_add
,
47 .remove
= mtd_notify_remove
,
51 * Data structure to hold the pointer to the mtd device as well
52 * as mode information ofr various use cases.
54 struct mtd_file_info
{
56 enum mtd_file_modes mode
;
59 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
61 struct mtd_file_info
*mfi
= file
->private_data
;
62 struct mtd_info
*mtd
= mfi
->mtd
;
68 offset
+= file
->f_pos
;
77 if (offset
>= 0 && offset
<= mtd
->size
)
78 return file
->f_pos
= offset
;
85 static int mtd_open(struct inode
*inode
, struct file
*file
)
87 int minor
= iminor(inode
);
88 int devnum
= minor
>> 1;
91 struct mtd_file_info
*mfi
;
93 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
95 if (devnum
>= MAX_MTD_DEVICES
)
98 /* You can't open the RO devices RW */
99 if ((file
->f_mode
& FMODE_WRITE
) && (minor
& 1))
103 mtd
= get_mtd_device(NULL
, devnum
);
110 if (MTD_ABSENT
== mtd
->type
) {
116 /* You can't open it RW if it's not a writeable device */
117 if ((file
->f_mode
& FMODE_WRITE
) && !(mtd
->flags
& MTD_WRITEABLE
)) {
123 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
130 file
->private_data
= mfi
;
137 /*====================================================================*/
139 static int mtd_close(struct inode
*inode
, struct file
*file
)
141 struct mtd_file_info
*mfi
= file
->private_data
;
142 struct mtd_info
*mtd
= mfi
->mtd
;
144 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
146 /* Only sync if opened RW */
147 if ((file
->f_mode
& FMODE_WRITE
) && mtd
->sync
)
151 file
->private_data
= NULL
;
157 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
158 userspace buffer down and use it directly with readv/writev.
160 #define MAX_KMALLOC_SIZE 0x20000
162 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
164 struct mtd_file_info
*mfi
= file
->private_data
;
165 struct mtd_info
*mtd
= mfi
->mtd
;
167 size_t total_retlen
=0;
172 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
174 if (*ppos
+ count
> mtd
->size
)
175 count
= mtd
->size
- *ppos
;
180 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
181 and pass them directly to the MTD functions */
183 if (count
> MAX_KMALLOC_SIZE
)
184 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
186 kbuf
=kmalloc(count
, GFP_KERNEL
);
193 if (count
> MAX_KMALLOC_SIZE
)
194 len
= MAX_KMALLOC_SIZE
;
199 case MTD_MODE_OTP_FACTORY
:
200 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
202 case MTD_MODE_OTP_USER
:
203 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
207 struct mtd_oob_ops ops
;
209 ops
.mode
= MTD_OOB_RAW
;
214 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
219 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
221 /* Nand returns -EBADMSG on ecc errors, but it returns
222 * the data. For our userspace tools it is important
223 * to dump areas with ecc errors !
224 * For kernel internal usage it also might return -EUCLEAN
225 * to signal the caller that a bitflip has occured and has
226 * been corrected by the ECC algorithm.
227 * Userspace software which accesses NAND this way
228 * must be aware of the fact that it deals with NAND
230 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
232 if (copy_to_user(buf
, kbuf
, retlen
)) {
237 total_retlen
+= retlen
;
255 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
257 struct mtd_file_info
*mfi
= file
->private_data
;
258 struct mtd_info
*mtd
= mfi
->mtd
;
261 size_t total_retlen
=0;
265 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
267 if (*ppos
== mtd
->size
)
270 if (*ppos
+ count
> mtd
->size
)
271 count
= mtd
->size
- *ppos
;
276 if (count
> MAX_KMALLOC_SIZE
)
277 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
279 kbuf
=kmalloc(count
, GFP_KERNEL
);
286 if (count
> MAX_KMALLOC_SIZE
)
287 len
= MAX_KMALLOC_SIZE
;
291 if (copy_from_user(kbuf
, buf
, len
)) {
297 case MTD_MODE_OTP_FACTORY
:
300 case MTD_MODE_OTP_USER
:
301 if (!mtd
->write_user_prot_reg
) {
305 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
310 struct mtd_oob_ops ops
;
312 ops
.mode
= MTD_OOB_RAW
;
317 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
323 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
327 total_retlen
+= retlen
;
341 /*======================================================================
343 IOCTL calls for getting device parameters.
345 ======================================================================*/
346 static void mtdchar_erase_callback (struct erase_info
*instr
)
348 wake_up((wait_queue_head_t
*)instr
->priv
);
351 #ifdef CONFIG_HAVE_MTD_OTP
352 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
354 struct mtd_info
*mtd
= mfi
->mtd
;
358 case MTD_OTP_FACTORY
:
359 if (!mtd
->read_fact_prot_reg
)
362 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
365 if (!mtd
->read_fact_prot_reg
)
368 mfi
->mode
= MTD_MODE_OTP_USER
;
378 # define otp_select_filemode(f,m) -EOPNOTSUPP
381 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
382 u_int cmd
, u_long arg
)
384 struct mtd_file_info
*mfi
= file
->private_data
;
385 struct mtd_info
*mtd
= mfi
->mtd
;
386 void __user
*argp
= (void __user
*)arg
;
389 struct mtd_info_user info
;
391 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
393 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
395 if (!access_ok(VERIFY_READ
, argp
, size
))
399 if (!access_ok(VERIFY_WRITE
, argp
, size
))
404 case MEMGETREGIONCOUNT
:
405 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
409 case MEMGETREGIONINFO
:
412 struct mtd_erase_region_info
*kr
;
413 struct region_info_user
*ur
= (struct region_info_user
*) argp
;
415 if (get_user(ur_idx
, &(ur
->regionindex
)))
418 kr
= &(mtd
->eraseregions
[ur_idx
]);
420 if (put_user(kr
->offset
, &(ur
->offset
))
421 || put_user(kr
->erasesize
, &(ur
->erasesize
))
422 || put_user(kr
->numblocks
, &(ur
->numblocks
)))
429 info
.type
= mtd
->type
;
430 info
.flags
= mtd
->flags
;
431 info
.size
= mtd
->size
;
432 info
.erasesize
= mtd
->erasesize
;
433 info
.writesize
= mtd
->writesize
;
434 info
.oobsize
= mtd
->oobsize
;
435 /* The below fields are obsolete */
438 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
444 struct erase_info
*erase
;
446 if(!(file
->f_mode
& FMODE_WRITE
))
449 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
453 wait_queue_head_t waitq
;
454 DECLARE_WAITQUEUE(wait
, current
);
456 init_waitqueue_head(&waitq
);
458 if (copy_from_user(&erase
->addr
, argp
,
459 sizeof(struct erase_info_user
))) {
464 erase
->callback
= mtdchar_erase_callback
;
465 erase
->priv
= (unsigned long)&waitq
;
468 FIXME: Allow INTERRUPTIBLE. Which means
469 not having the wait_queue head on the stack.
471 If the wq_head is on the stack, and we
472 leave because we got interrupted, then the
473 wq_head is no longer there when the
474 callback routine tries to wake us up.
476 ret
= mtd
->erase(mtd
, erase
);
478 set_current_state(TASK_UNINTERRUPTIBLE
);
479 add_wait_queue(&waitq
, &wait
);
480 if (erase
->state
!= MTD_ERASE_DONE
&&
481 erase
->state
!= MTD_ERASE_FAILED
)
483 remove_wait_queue(&waitq
, &wait
);
484 set_current_state(TASK_RUNNING
);
486 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
495 struct mtd_oob_buf buf
;
496 struct mtd_oob_ops ops
;
497 struct mtd_oob_buf __user
*user_buf
= argp
;
500 if(!(file
->f_mode
& FMODE_WRITE
))
503 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
506 if (buf
.length
> 4096)
512 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
513 buf
.length
) ? 0 : EFAULT
;
518 ops
.ooblen
= buf
.length
;
519 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
521 ops
.mode
= MTD_OOB_PLACE
;
523 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
526 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
530 if (copy_from_user(ops
.oobbuf
, buf
.ptr
, buf
.length
)) {
535 buf
.start
&= ~(mtd
->oobsize
- 1);
536 ret
= mtd
->write_oob(mtd
, buf
.start
, &ops
);
538 if (ops
.oobretlen
> 0xFFFFFFFFU
)
540 retlen
= ops
.oobretlen
;
541 if (copy_to_user(&user_buf
->length
, &retlen
, sizeof(buf
.length
)))
551 struct mtd_oob_buf buf
;
552 struct mtd_oob_ops ops
;
554 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
557 if (buf
.length
> 4096)
563 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
564 buf
.length
) ? 0 : -EFAULT
;
568 ops
.ooblen
= buf
.length
;
569 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
571 ops
.mode
= MTD_OOB_PLACE
;
573 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
576 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
580 buf
.start
&= ~(mtd
->oobsize
- 1);
581 ret
= mtd
->read_oob(mtd
, buf
.start
, &ops
);
583 if (put_user(ops
.oobretlen
, (uint32_t __user
*)argp
))
585 else if (ops
.oobretlen
&& copy_to_user(buf
.ptr
, ops
.oobbuf
,
595 struct erase_info_user einfo
;
597 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
603 ret
= mtd
->lock(mtd
, einfo
.start
, einfo
.length
);
609 struct erase_info_user einfo
;
611 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
617 ret
= mtd
->unlock(mtd
, einfo
.start
, einfo
.length
);
621 /* Legacy interface */
624 struct nand_oobinfo oi
;
628 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
631 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
632 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
633 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
635 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
637 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
646 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
648 if (!mtd
->block_isbad
)
651 return mtd
->block_isbad(mtd
, offs
);
659 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
661 if (!mtd
->block_markbad
)
664 return mtd
->block_markbad(mtd
, offs
);
668 #ifdef CONFIG_HAVE_MTD_OTP
672 if (copy_from_user(&mode
, argp
, sizeof(int)))
675 mfi
->mode
= MTD_MODE_NORMAL
;
677 ret
= otp_select_filemode(mfi
, mode
);
683 case OTPGETREGIONCOUNT
:
684 case OTPGETREGIONINFO
:
686 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
691 case MTD_MODE_OTP_FACTORY
:
692 if (mtd
->get_fact_prot_info
)
693 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
695 case MTD_MODE_OTP_USER
:
696 if (mtd
->get_user_prot_info
)
697 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
703 if (cmd
== OTPGETREGIONCOUNT
) {
704 int nbr
= ret
/ sizeof(struct otp_info
);
705 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
707 ret
= copy_to_user(argp
, buf
, ret
);
717 struct otp_info oinfo
;
719 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
721 if (copy_from_user(&oinfo
, argp
, sizeof(oinfo
)))
723 if (!mtd
->lock_user_prot_reg
)
725 ret
= mtd
->lock_user_prot_reg(mtd
, oinfo
.start
, oinfo
.length
);
735 if (copy_to_user(argp
, mtd
->ecclayout
,
736 sizeof(struct nand_ecclayout
)))
743 if (copy_to_user(argp
, &mtd
->ecc_stats
,
744 sizeof(struct mtd_ecc_stats
)))
754 case MTD_MODE_OTP_FACTORY
:
755 case MTD_MODE_OTP_USER
:
756 ret
= otp_select_filemode(mfi
, arg
);
760 if (!mtd
->read_oob
|| !mtd
->write_oob
)
764 case MTD_MODE_NORMAL
:
780 static const struct file_operations mtd_fops
= {
781 .owner
= THIS_MODULE
,
787 .release
= mtd_close
,
790 static int __init
init_mtdchar(void)
792 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
793 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
798 mtd_class
= class_create(THIS_MODULE
, "mtd");
800 if (IS_ERR(mtd_class
)) {
801 printk(KERN_ERR
"Error creating mtd class.\n");
802 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
803 return PTR_ERR(mtd_class
);
806 register_mtd_user(¬ifier
);
810 static void __exit
cleanup_mtdchar(void)
812 unregister_mtd_user(¬ifier
);
813 class_destroy(mtd_class
);
814 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
817 module_init(init_mtdchar
);
818 module_exit(cleanup_mtdchar
);
821 MODULE_LICENSE("GPL");
822 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
823 MODULE_DESCRIPTION("Direct character-device access to MTD devices");