]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/mtd/mtdchar.c
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>
17 #include <linux/smp_lock.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/compatmac.h>
22 #include <asm/uaccess.h>
24 static struct class *mtd_class
;
26 static void mtd_notify_add(struct mtd_info
* mtd
)
31 device_create(mtd_class
, NULL
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2), "mtd%d", mtd
->index
);
33 device_create(mtd_class
, NULL
,
34 MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2+1), "mtd%dro", mtd
->index
);
37 static void mtd_notify_remove(struct mtd_info
* mtd
)
42 device_destroy(mtd_class
, MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2));
43 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;
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))
104 mtd
= get_mtd_device(NULL
, devnum
);
111 if (MTD_ABSENT
== mtd
->type
) {
117 /* You can't open it RW if it's not a writeable device */
118 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
124 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
131 file
->private_data
= mfi
;
138 /*====================================================================*/
140 static int mtd_close(struct inode
*inode
, struct file
*file
)
142 struct mtd_file_info
*mfi
= file
->private_data
;
143 struct mtd_info
*mtd
= mfi
->mtd
;
145 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
147 /* Only sync if opened RW */
148 if ((file
->f_mode
& 2) && mtd
->sync
)
152 file
->private_data
= NULL
;
158 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
159 userspace buffer down and use it directly with readv/writev.
161 #define MAX_KMALLOC_SIZE 0x20000
163 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
165 struct mtd_file_info
*mfi
= file
->private_data
;
166 struct mtd_info
*mtd
= mfi
->mtd
;
168 size_t total_retlen
=0;
173 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
175 if (*ppos
+ count
> mtd
->size
)
176 count
= mtd
->size
- *ppos
;
181 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
182 and pass them directly to the MTD functions */
184 if (count
> MAX_KMALLOC_SIZE
)
185 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
187 kbuf
=kmalloc(count
, GFP_KERNEL
);
194 if (count
> MAX_KMALLOC_SIZE
)
195 len
= MAX_KMALLOC_SIZE
;
200 case MTD_MODE_OTP_FACTORY
:
201 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
203 case MTD_MODE_OTP_USER
:
204 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
208 struct mtd_oob_ops ops
;
210 ops
.mode
= MTD_OOB_RAW
;
215 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
220 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
222 /* Nand returns -EBADMSG on ecc errors, but it returns
223 * the data. For our userspace tools it is important
224 * to dump areas with ecc errors !
225 * For kernel internal usage it also might return -EUCLEAN
226 * to signal the caller that a bitflip has occured and has
227 * been corrected by the ECC algorithm.
228 * Userspace software which accesses NAND this way
229 * must be aware of the fact that it deals with NAND
231 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
233 if (copy_to_user(buf
, kbuf
, retlen
)) {
238 total_retlen
+= retlen
;
256 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
258 struct mtd_file_info
*mfi
= file
->private_data
;
259 struct mtd_info
*mtd
= mfi
->mtd
;
262 size_t total_retlen
=0;
266 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
268 if (*ppos
== mtd
->size
)
271 if (*ppos
+ count
> mtd
->size
)
272 count
= mtd
->size
- *ppos
;
277 if (count
> MAX_KMALLOC_SIZE
)
278 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
280 kbuf
=kmalloc(count
, GFP_KERNEL
);
287 if (count
> MAX_KMALLOC_SIZE
)
288 len
= MAX_KMALLOC_SIZE
;
292 if (copy_from_user(kbuf
, buf
, len
)) {
298 case MTD_MODE_OTP_FACTORY
:
301 case MTD_MODE_OTP_USER
:
302 if (!mtd
->write_user_prot_reg
) {
306 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
311 struct mtd_oob_ops ops
;
313 ops
.mode
= MTD_OOB_RAW
;
318 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
324 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
328 total_retlen
+= retlen
;
342 /*======================================================================
344 IOCTL calls for getting device parameters.
346 ======================================================================*/
347 static void mtdchar_erase_callback (struct erase_info
*instr
)
349 wake_up((wait_queue_head_t
*)instr
->priv
);
352 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
353 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
355 struct mtd_info
*mtd
= mfi
->mtd
;
359 case MTD_OTP_FACTORY
:
360 if (!mtd
->read_fact_prot_reg
)
363 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
366 if (!mtd
->read_fact_prot_reg
)
369 mfi
->mode
= MTD_MODE_OTP_USER
;
379 # define otp_select_filemode(f,m) -EOPNOTSUPP
382 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
383 u_int cmd
, u_long arg
)
385 struct mtd_file_info
*mfi
= file
->private_data
;
386 struct mtd_info
*mtd
= mfi
->mtd
;
387 void __user
*argp
= (void __user
*)arg
;
390 struct mtd_info_user info
;
392 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
394 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
396 if (!access_ok(VERIFY_READ
, argp
, size
))
400 if (!access_ok(VERIFY_WRITE
, argp
, size
))
405 case MEMGETREGIONCOUNT
:
406 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
410 case MEMGETREGIONINFO
:
412 struct region_info_user ur
;
414 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
417 if (ur
.regionindex
>= mtd
->numeraseregions
)
419 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
420 sizeof(struct mtd_erase_region_info
)))
426 info
.type
= mtd
->type
;
427 info
.flags
= mtd
->flags
;
428 info
.size
= mtd
->size
;
429 info
.erasesize
= mtd
->erasesize
;
430 info
.writesize
= mtd
->writesize
;
431 info
.oobsize
= mtd
->oobsize
;
432 /* The below fields are obsolete */
435 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
441 struct erase_info
*erase
;
443 if(!(file
->f_mode
& 2))
446 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
450 wait_queue_head_t waitq
;
451 DECLARE_WAITQUEUE(wait
, current
);
453 init_waitqueue_head(&waitq
);
455 if (copy_from_user(&erase
->addr
, argp
,
456 sizeof(struct erase_info_user
))) {
461 erase
->callback
= mtdchar_erase_callback
;
462 erase
->priv
= (unsigned long)&waitq
;
465 FIXME: Allow INTERRUPTIBLE. Which means
466 not having the wait_queue head on the stack.
468 If the wq_head is on the stack, and we
469 leave because we got interrupted, then the
470 wq_head is no longer there when the
471 callback routine tries to wake us up.
473 ret
= mtd
->erase(mtd
, erase
);
475 set_current_state(TASK_UNINTERRUPTIBLE
);
476 add_wait_queue(&waitq
, &wait
);
477 if (erase
->state
!= MTD_ERASE_DONE
&&
478 erase
->state
!= MTD_ERASE_FAILED
)
480 remove_wait_queue(&waitq
, &wait
);
481 set_current_state(TASK_RUNNING
);
483 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
492 struct mtd_oob_buf buf
;
493 struct mtd_oob_ops ops
;
496 if(!(file
->f_mode
& 2))
499 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
502 if (buf
.length
> 4096)
508 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
509 buf
.length
) ? 0 : EFAULT
;
514 ops
.ooblen
= buf
.length
;
515 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
517 ops
.mode
= MTD_OOB_PLACE
;
519 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
522 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
526 if (copy_from_user(ops
.oobbuf
, buf
.ptr
, buf
.length
)) {
531 buf
.start
&= ~(mtd
->oobsize
- 1);
532 ret
= mtd
->write_oob(mtd
, buf
.start
, &ops
);
534 if (ops
.oobretlen
> 0xFFFFFFFFU
)
536 retlen
= ops
.oobretlen
;
537 if (copy_to_user(&((struct mtd_oob_buf
*)argp
)->length
,
538 &retlen
, sizeof(buf
.length
)))
548 struct mtd_oob_buf buf
;
549 struct mtd_oob_ops ops
;
551 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
554 if (buf
.length
> 4096)
560 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
561 buf
.length
) ? 0 : -EFAULT
;
565 ops
.ooblen
= buf
.length
;
566 ops
.ooboffs
= buf
.start
& (mtd
->oobsize
- 1);
568 ops
.mode
= MTD_OOB_PLACE
;
570 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
573 ops
.oobbuf
= kmalloc(buf
.length
, GFP_KERNEL
);
577 buf
.start
&= ~(mtd
->oobsize
- 1);
578 ret
= mtd
->read_oob(mtd
, buf
.start
, &ops
);
580 if (put_user(ops
.oobretlen
, (uint32_t __user
*)argp
))
582 else if (ops
.oobretlen
&& copy_to_user(buf
.ptr
, ops
.oobbuf
,
592 struct erase_info_user info
;
594 if (copy_from_user(&info
, argp
, sizeof(info
)))
600 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
606 struct erase_info_user info
;
608 if (copy_from_user(&info
, argp
, sizeof(info
)))
614 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
618 /* Legacy interface */
621 struct nand_oobinfo oi
;
625 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
628 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
629 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
630 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
632 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
634 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
643 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
645 if (!mtd
->block_isbad
)
648 return mtd
->block_isbad(mtd
, offs
);
656 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
658 if (!mtd
->block_markbad
)
661 return mtd
->block_markbad(mtd
, offs
);
665 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
669 if (copy_from_user(&mode
, argp
, sizeof(int)))
672 mfi
->mode
= MTD_MODE_NORMAL
;
674 ret
= otp_select_filemode(mfi
, mode
);
680 case OTPGETREGIONCOUNT
:
681 case OTPGETREGIONINFO
:
683 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
688 case MTD_MODE_OTP_FACTORY
:
689 if (mtd
->get_fact_prot_info
)
690 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
692 case MTD_MODE_OTP_USER
:
693 if (mtd
->get_user_prot_info
)
694 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
700 if (cmd
== OTPGETREGIONCOUNT
) {
701 int nbr
= ret
/ sizeof(struct otp_info
);
702 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
704 ret
= copy_to_user(argp
, buf
, ret
);
714 struct otp_info info
;
716 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
718 if (copy_from_user(&info
, argp
, sizeof(info
)))
720 if (!mtd
->lock_user_prot_reg
)
722 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
732 if (copy_to_user(argp
, mtd
->ecclayout
,
733 sizeof(struct nand_ecclayout
)))
740 if (copy_to_user(argp
, &mtd
->ecc_stats
,
741 sizeof(struct mtd_ecc_stats
)))
751 case MTD_MODE_OTP_FACTORY
:
752 case MTD_MODE_OTP_USER
:
753 ret
= otp_select_filemode(mfi
, arg
);
757 if (!mtd
->read_oob
|| !mtd
->write_oob
)
761 case MTD_MODE_NORMAL
:
777 static const struct file_operations mtd_fops
= {
778 .owner
= THIS_MODULE
,
784 .release
= mtd_close
,
787 static int __init
init_mtdchar(void)
789 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
790 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
795 mtd_class
= class_create(THIS_MODULE
, "mtd");
797 if (IS_ERR(mtd_class
)) {
798 printk(KERN_ERR
"Error creating mtd class.\n");
799 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
800 return PTR_ERR(mtd_class
);
803 register_mtd_user(¬ifier
);
807 static void __exit
cleanup_mtdchar(void)
809 unregister_mtd_user(¬ifier
);
810 class_destroy(mtd_class
);
811 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
814 module_init(init_mtdchar
);
815 module_exit(cleanup_mtdchar
);
818 MODULE_LICENSE("GPL");
819 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
820 MODULE_DESCRIPTION("Direct character-device access to MTD devices");