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/err.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 * Data structure to hold the pointer to the mtd device as well
53 * as mode information ofr various use cases.
55 struct mtd_file_info
{
57 enum mtd_file_modes mode
;
60 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
62 struct mtd_file_info
*mfi
= file
->private_data
;
63 struct mtd_info
*mtd
= mfi
->mtd
;
69 offset
+= file
->f_pos
;
78 if (offset
>= 0 && offset
<= mtd
->size
)
79 return file
->f_pos
= offset
;
86 static int mtd_open(struct inode
*inode
, struct file
*file
)
88 int minor
= iminor(inode
);
89 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
& 2) && (minor
& 1))
102 mtd
= get_mtd_device(NULL
, devnum
);
107 if (MTD_ABSENT
== mtd
->type
) {
112 /* You can't open it RW if it's not a writeable device */
113 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
118 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
124 file
->private_data
= mfi
;
129 /*====================================================================*/
131 static int mtd_close(struct inode
*inode
, struct file
*file
)
133 struct mtd_file_info
*mfi
= file
->private_data
;
134 struct mtd_info
*mtd
= mfi
->mtd
;
136 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
142 file
->private_data
= NULL
;
148 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
149 userspace buffer down and use it directly with readv/writev.
151 #define MAX_KMALLOC_SIZE 0x20000
153 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
155 struct mtd_file_info
*mfi
= file
->private_data
;
156 struct mtd_info
*mtd
= mfi
->mtd
;
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
;
190 case MTD_MODE_OTP_FACTORY
:
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
);
198 struct mtd_oob_ops ops
;
200 ops
.mode
= MTD_OOB_RAW
;
205 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
210 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
212 /* Nand returns -EBADMSG on ecc errors, but it returns
213 * the data. For our userspace tools it is important
214 * to dump areas with ecc errors !
215 * For kernel internal usage it also might return -EUCLEAN
216 * to signal the caller that a bitflip has occured and has
217 * been corrected by the ECC algorithm.
218 * Userspace software which accesses NAND this way
219 * must be aware of the fact that it deals with NAND
221 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
223 if (copy_to_user(buf
, kbuf
, retlen
)) {
228 total_retlen
+= retlen
;
246 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
248 struct mtd_file_info
*mfi
= file
->private_data
;
249 struct mtd_info
*mtd
= mfi
->mtd
;
252 size_t total_retlen
=0;
256 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
258 if (*ppos
== mtd
->size
)
261 if (*ppos
+ count
> mtd
->size
)
262 count
= mtd
->size
- *ppos
;
267 if (count
> MAX_KMALLOC_SIZE
)
268 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
270 kbuf
=kmalloc(count
, GFP_KERNEL
);
277 if (count
> MAX_KMALLOC_SIZE
)
278 len
= MAX_KMALLOC_SIZE
;
282 if (copy_from_user(kbuf
, buf
, len
)) {
288 case MTD_MODE_OTP_FACTORY
:
291 case MTD_MODE_OTP_USER
:
292 if (!mtd
->write_user_prot_reg
) {
296 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
301 struct mtd_oob_ops ops
;
303 ops
.mode
= MTD_OOB_RAW
;
308 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
314 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
318 total_retlen
+= retlen
;
332 /*======================================================================
334 IOCTL calls for getting device parameters.
336 ======================================================================*/
337 static void mtdchar_erase_callback (struct erase_info
*instr
)
339 wake_up((wait_queue_head_t
*)instr
->priv
);
342 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
343 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
345 struct mtd_info
*mtd
= mfi
->mtd
;
349 case MTD_OTP_FACTORY
:
350 if (!mtd
->read_fact_prot_reg
)
353 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
356 if (!mtd
->read_fact_prot_reg
)
359 mfi
->mode
= MTD_MODE_OTP_USER
;
369 # define otp_select_filemode(f,m) -EOPNOTSUPP
372 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
373 u_int cmd
, u_long arg
)
375 struct mtd_file_info
*mfi
= file
->private_data
;
376 struct mtd_info
*mtd
= mfi
->mtd
;
377 void __user
*argp
= (void __user
*)arg
;
380 struct mtd_info_user info
;
382 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
384 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
386 if (!access_ok(VERIFY_READ
, argp
, size
))
390 if (!access_ok(VERIFY_WRITE
, argp
, size
))
395 case MEMGETREGIONCOUNT
:
396 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
400 case MEMGETREGIONINFO
:
402 struct region_info_user ur
;
404 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
407 if (ur
.regionindex
>= mtd
->numeraseregions
)
409 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
410 sizeof(struct mtd_erase_region_info
)))
416 info
.type
= mtd
->type
;
417 info
.flags
= mtd
->flags
;
418 info
.size
= mtd
->size
;
419 info
.erasesize
= mtd
->erasesize
;
420 info
.writesize
= mtd
->writesize
;
421 info
.oobsize
= mtd
->oobsize
;
422 info
.ecctype
= mtd
->ecctype
;
423 info
.eccsize
= mtd
->eccsize
;
424 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
430 struct erase_info
*erase
;
432 if(!(file
->f_mode
& 2))
435 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
439 wait_queue_head_t waitq
;
440 DECLARE_WAITQUEUE(wait
, current
);
442 init_waitqueue_head(&waitq
);
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
.ooblen
= buf
.length
;
503 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
505 ops
.mode
= MTD_OOB_PLACE
;
507 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
510 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
514 if (copy_from_user(ops
.oobbuf
, buf
.ptr
, buf
.length
)) {
519 buf
.start
&= ~(mtd
->oobsize
- 1);
520 ret
= mtd
->write_oob(mtd
, buf
.start
, &ops
);
522 if (copy_to_user(argp
+ sizeof(uint32_t), &ops
.oobretlen
,
533 struct mtd_oob_buf buf
;
534 struct mtd_oob_ops ops
;
536 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
539 if (buf
.length
> 4096)
545 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
546 buf
.length
) ? 0 : -EFAULT
;
550 ops
.ooblen
= buf
.length
;
551 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
553 ops
.mode
= MTD_OOB_PLACE
;
555 if (ops
.ooboffs
&& ops
.len
> (mtd
->oobsize
- ops
.ooboffs
))
558 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
562 buf
.start
&= ~(mtd
->oobsize
- 1);
563 ret
= mtd
->read_oob(mtd
, buf
.start
, &ops
);
565 if (put_user(ops
.oobretlen
, (uint32_t __user
*)argp
))
567 else if (ops
.oobretlen
&& copy_to_user(buf
.ptr
, ops
.oobbuf
,
577 struct erase_info_user info
;
579 if (copy_from_user(&info
, argp
, sizeof(info
)))
585 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
591 struct erase_info_user info
;
593 if (copy_from_user(&info
, argp
, sizeof(info
)))
599 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
603 /* Legacy interface */
606 struct nand_oobinfo oi
;
610 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
613 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
614 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
615 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
617 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
619 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
628 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
630 if (!mtd
->block_isbad
)
633 return mtd
->block_isbad(mtd
, offs
);
641 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
643 if (!mtd
->block_markbad
)
646 return mtd
->block_markbad(mtd
, offs
);
650 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
654 if (copy_from_user(&mode
, argp
, sizeof(int)))
657 mfi
->mode
= MTD_MODE_NORMAL
;
659 ret
= otp_select_filemode(mfi
, mode
);
665 case OTPGETREGIONCOUNT
:
666 case OTPGETREGIONINFO
:
668 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
673 case MTD_MODE_OTP_FACTORY
:
674 if (mtd
->get_fact_prot_info
)
675 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
677 case MTD_MODE_OTP_USER
:
678 if (mtd
->get_user_prot_info
)
679 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
685 if (cmd
== OTPGETREGIONCOUNT
) {
686 int nbr
= ret
/ sizeof(struct otp_info
);
687 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
689 ret
= copy_to_user(argp
, buf
, ret
);
699 struct otp_info info
;
701 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
703 if (copy_from_user(&info
, argp
, sizeof(info
)))
705 if (!mtd
->lock_user_prot_reg
)
707 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
717 if (copy_to_user(argp
, mtd
->ecclayout
,
718 sizeof(struct nand_ecclayout
)))
725 if (copy_to_user(argp
, &mtd
->ecc_stats
,
726 sizeof(struct mtd_ecc_stats
)))
736 case MTD_MODE_OTP_FACTORY
:
737 case MTD_MODE_OTP_USER
:
738 ret
= otp_select_filemode(mfi
, arg
);
742 if (!mtd
->read_oob
|| !mtd
->write_oob
)
746 case MTD_MODE_NORMAL
:
762 static struct file_operations mtd_fops
= {
763 .owner
= THIS_MODULE
,
769 .release
= mtd_close
,
772 static int __init
init_mtdchar(void)
774 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
775 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
780 mtd_class
= class_create(THIS_MODULE
, "mtd");
782 if (IS_ERR(mtd_class
)) {
783 printk(KERN_ERR
"Error creating mtd class.\n");
784 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
785 return PTR_ERR(mtd_class
);
788 register_mtd_user(¬ifier
);
792 static void __exit
cleanup_mtdchar(void)
794 unregister_mtd_user(¬ifier
);
795 class_destroy(mtd_class
);
796 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
799 module_init(init_mtdchar
);
800 module_exit(cleanup_mtdchar
);
803 MODULE_LICENSE("GPL");
804 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
805 MODULE_DESCRIPTION("Direct character-device access to MTD devices");