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>
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>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/compatmac.h>
19 #include <asm/uaccess.h>
21 static struct class *mtd_class
;
23 static void mtd_notify_add(struct mtd_info
* mtd
)
28 class_device_create(mtd_class
, NULL
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2),
29 NULL
, "mtd%d", mtd
->index
);
31 class_device_create(mtd_class
, NULL
,
32 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 class_device_destroy(mtd_class
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2));
42 class_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;
90 struct mtd_file_info
*mfi
;
92 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
94 if (devnum
>= MAX_MTD_DEVICES
)
97 /* You can't open the RO devices RW */
98 if ((file
->f_mode
& 2) && (minor
& 1))
101 mtd
= get_mtd_device(NULL
, devnum
);
106 if (MTD_ABSENT
== mtd
->type
) {
111 /* You can't open it RW if it's not a writeable device */
112 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
117 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
123 file
->private_data
= mfi
;
128 /*====================================================================*/
130 static int mtd_close(struct inode
*inode
, struct file
*file
)
132 struct mtd_file_info
*mfi
= file
->private_data
;
133 struct mtd_info
*mtd
= mfi
->mtd
;
135 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
141 file
->private_data
= NULL
;
147 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
148 userspace buffer down and use it directly with readv/writev.
150 #define MAX_KMALLOC_SIZE 0x20000
152 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
154 struct mtd_file_info
*mfi
= file
->private_data
;
155 struct mtd_info
*mtd
= mfi
->mtd
;
157 size_t total_retlen
=0;
162 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
164 if (*ppos
+ count
> mtd
->size
)
165 count
= mtd
->size
- *ppos
;
170 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
171 and pass them directly to the MTD functions */
173 if (count
> MAX_KMALLOC_SIZE
)
174 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
176 kbuf
=kmalloc(count
, GFP_KERNEL
);
183 if (count
> MAX_KMALLOC_SIZE
)
184 len
= MAX_KMALLOC_SIZE
;
189 case MTD_MODE_OTP_FACTORY
:
190 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
192 case MTD_MODE_OTP_USER
:
193 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
197 struct mtd_oob_ops ops
;
199 ops
.mode
= MTD_OOB_RAW
;
204 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
209 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
211 /* Nand returns -EBADMSG on ecc errors, but it returns
212 * the data. For our userspace tools it is important
213 * to dump areas with ecc errors !
214 * For kernel internal usage it also might return -EUCLEAN
215 * to signal the caller that a bitflip has occured and has
216 * been corrected by the ECC algorithm.
217 * Userspace software which accesses NAND this way
218 * must be aware of the fact that it deals with NAND
220 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
222 if (copy_to_user(buf
, kbuf
, retlen
)) {
227 total_retlen
+= retlen
;
245 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
247 struct mtd_file_info
*mfi
= file
->private_data
;
248 struct mtd_info
*mtd
= mfi
->mtd
;
251 size_t total_retlen
=0;
255 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
257 if (*ppos
== mtd
->size
)
260 if (*ppos
+ count
> mtd
->size
)
261 count
= mtd
->size
- *ppos
;
266 if (count
> MAX_KMALLOC_SIZE
)
267 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
269 kbuf
=kmalloc(count
, GFP_KERNEL
);
276 if (count
> MAX_KMALLOC_SIZE
)
277 len
= MAX_KMALLOC_SIZE
;
281 if (copy_from_user(kbuf
, buf
, len
)) {
287 case MTD_MODE_OTP_FACTORY
:
290 case MTD_MODE_OTP_USER
:
291 if (!mtd
->write_user_prot_reg
) {
295 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
300 struct mtd_oob_ops ops
;
302 ops
.mode
= MTD_OOB_RAW
;
307 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
313 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
317 total_retlen
+= retlen
;
331 /*======================================================================
333 IOCTL calls for getting device parameters.
335 ======================================================================*/
336 static void mtdchar_erase_callback (struct erase_info
*instr
)
338 wake_up((wait_queue_head_t
*)instr
->priv
);
341 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
342 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
344 struct mtd_info
*mtd
= mfi
->mtd
;
348 case MTD_OTP_FACTORY
:
349 if (!mtd
->read_fact_prot_reg
)
352 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
355 if (!mtd
->read_fact_prot_reg
)
358 mfi
->mode
= MTD_MODE_OTP_USER
;
368 # define otp_select_filemode(f,m) -EOPNOTSUPP
371 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
372 u_int cmd
, u_long arg
)
374 struct mtd_file_info
*mfi
= file
->private_data
;
375 struct mtd_info
*mtd
= mfi
->mtd
;
376 void __user
*argp
= (void __user
*)arg
;
379 struct mtd_info_user info
;
381 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
383 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
385 if (!access_ok(VERIFY_READ
, argp
, size
))
389 if (!access_ok(VERIFY_WRITE
, argp
, size
))
394 case MEMGETREGIONCOUNT
:
395 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
399 case MEMGETREGIONINFO
:
401 struct region_info_user ur
;
403 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
406 if (ur
.regionindex
>= mtd
->numeraseregions
)
408 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
409 sizeof(struct mtd_erase_region_info
)))
415 info
.type
= mtd
->type
;
416 info
.flags
= mtd
->flags
;
417 info
.size
= mtd
->size
;
418 info
.erasesize
= mtd
->erasesize
;
419 info
.writesize
= mtd
->writesize
;
420 info
.oobsize
= mtd
->oobsize
;
421 info
.ecctype
= mtd
->ecctype
;
422 info
.eccsize
= mtd
->eccsize
;
423 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
429 struct erase_info
*erase
;
431 if(!(file
->f_mode
& 2))
434 erase
=kmalloc(sizeof(struct erase_info
),GFP_KERNEL
);
438 wait_queue_head_t waitq
;
439 DECLARE_WAITQUEUE(wait
, current
);
441 init_waitqueue_head(&waitq
);
443 memset (erase
,0,sizeof(struct erase_info
));
444 if (copy_from_user(&erase
->addr
, argp
,
445 sizeof(struct erase_info_user
))) {
450 erase
->callback
= mtdchar_erase_callback
;
451 erase
->priv
= (unsigned long)&waitq
;
454 FIXME: Allow INTERRUPTIBLE. Which means
455 not having the wait_queue head on the stack.
457 If the wq_head is on the stack, and we
458 leave because we got interrupted, then the
459 wq_head is no longer there when the
460 callback routine tries to wake us up.
462 ret
= mtd
->erase(mtd
, erase
);
464 set_current_state(TASK_UNINTERRUPTIBLE
);
465 add_wait_queue(&waitq
, &wait
);
466 if (erase
->state
!= MTD_ERASE_DONE
&&
467 erase
->state
!= MTD_ERASE_FAILED
)
469 remove_wait_queue(&waitq
, &wait
);
470 set_current_state(TASK_RUNNING
);
472 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
481 struct mtd_oob_buf buf
;
482 struct mtd_oob_ops ops
;
484 if(!(file
->f_mode
& 2))
487 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
490 if (buf
.length
> 4096)
496 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
497 buf
.length
) ? 0 : EFAULT
;
502 ops
.len
= buf
.length
;
503 ops
.ooblen
= buf
.length
;
504 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
506 ops
.mode
= MTD_OOB_PLACE
;
508 if (ops
.ooboffs
&& ops
.len
> (mtd
->oobsize
- ops
.ooboffs
))
511 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
515 if (copy_from_user(ops
.oobbuf
, buf
.ptr
, buf
.length
)) {
520 buf
.start
&= ~(mtd
->oobsize
- 1);
521 ret
= mtd
->write_oob(mtd
, buf
.start
, &ops
);
523 if (copy_to_user(argp
+ sizeof(uint32_t), &ops
.retlen
,
534 struct mtd_oob_buf buf
;
535 struct mtd_oob_ops ops
;
537 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
540 if (buf
.length
> 4096)
546 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
547 buf
.length
) ? 0 : -EFAULT
;
551 ops
.len
= buf
.length
;
552 ops
.ooblen
= buf
.length
;
553 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
555 ops
.mode
= MTD_OOB_PLACE
;
557 if (ops
.ooboffs
&& ops
.len
> (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
.retlen
, (uint32_t __user
*)argp
))
569 else if (ops
.retlen
&& 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
,
620 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
629 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
631 if (!mtd
->block_isbad
)
634 return mtd
->block_isbad(mtd
, offs
);
642 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
644 if (!mtd
->block_markbad
)
647 return mtd
->block_markbad(mtd
, offs
);
651 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
655 if (copy_from_user(&mode
, argp
, sizeof(int)))
658 mfi
->mode
= MTD_MODE_NORMAL
;
660 ret
= otp_select_filemode(mfi
, mode
);
666 case OTPGETREGIONCOUNT
:
667 case OTPGETREGIONINFO
:
669 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
674 case MTD_MODE_OTP_FACTORY
:
675 if (mtd
->get_fact_prot_info
)
676 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
678 case MTD_MODE_OTP_USER
:
679 if (mtd
->get_user_prot_info
)
680 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
686 if (cmd
== OTPGETREGIONCOUNT
) {
687 int nbr
= ret
/ sizeof(struct otp_info
);
688 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
690 ret
= copy_to_user(argp
, buf
, ret
);
700 struct otp_info info
;
702 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
704 if (copy_from_user(&info
, argp
, sizeof(info
)))
706 if (!mtd
->lock_user_prot_reg
)
708 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
718 if (copy_to_user(argp
, &mtd
->ecclayout
,
719 sizeof(struct nand_ecclayout
)))
726 if (copy_to_user(argp
, &mtd
->ecc_stats
,
727 sizeof(struct mtd_ecc_stats
)))
737 case MTD_MODE_OTP_FACTORY
:
738 case MTD_MODE_OTP_USER
:
739 ret
= otp_select_filemode(mfi
, arg
);
743 if (!mtd
->read_oob
|| !mtd
->write_oob
)
747 case MTD_MODE_NORMAL
:
763 static struct file_operations mtd_fops
= {
764 .owner
= THIS_MODULE
,
770 .release
= mtd_close
,
773 static int __init
init_mtdchar(void)
775 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
776 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
781 mtd_class
= class_create(THIS_MODULE
, "mtd");
783 if (IS_ERR(mtd_class
)) {
784 printk(KERN_ERR
"Error creating mtd class.\n");
785 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
786 return PTR_ERR(mtd_class
);
789 register_mtd_user(¬ifier
);
793 static void __exit
cleanup_mtdchar(void)
795 unregister_mtd_user(¬ifier
);
796 class_destroy(mtd_class
);
797 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
800 module_init(init_mtdchar
);
801 module_exit(cleanup_mtdchar
);
804 MODULE_LICENSE("GPL");
805 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
806 MODULE_DESCRIPTION("Direct character-device access to MTD devices");