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/config.h>
9 #include <linux/device.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 * We use file->private_data to store a pointer to the MTDdevice.
53 * Since alighment is at least 32 bits, we have 2 bits free for OTP
57 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
59 #define MTD_MODE_OTP_FACT 1
60 #define MTD_MODE_OTP_USER 2
61 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
63 #define SET_MTD_MODE(file, mode) \
64 do { long __p = (long)((file)->private_data); \
65 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
67 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
69 struct mtd_info
*mtd
= TO_MTD(file
);
77 offset
+= file
->f_pos
;
87 if (offset
>= 0 && offset
< mtd
->size
)
88 return file
->f_pos
= offset
;
95 static int mtd_open(struct inode
*inode
, struct file
*file
)
97 int minor
= iminor(inode
);
98 int devnum
= minor
>> 1;
101 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
103 if (devnum
>= MAX_MTD_DEVICES
)
106 /* You can't open the RO devices RW */
107 if ((file
->f_mode
& 2) && (minor
& 1))
110 mtd
= get_mtd_device(NULL
, devnum
);
115 if (MTD_ABSENT
== mtd
->type
) {
120 file
->private_data
= mtd
;
122 /* You can't open it RW if it's not a writeable device */
123 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
131 /*====================================================================*/
133 static int mtd_close(struct inode
*inode
, struct file
*file
)
135 struct mtd_info
*mtd
;
137 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
149 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
150 userspace buffer down and use it directly with readv/writev.
152 #define MAX_KMALLOC_SIZE 0x20000
154 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
156 struct mtd_info
*mtd
= TO_MTD(file
);
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
;
189 switch (MTD_MODE(file
)) {
190 case MTD_MODE_OTP_FACT
:
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
);
197 ret
= MTD_READ(mtd
, *ppos
, len
, &retlen
, kbuf
);
199 /* Nand returns -EBADMSG on ecc errors, but it returns
200 * the data. For our userspace tools it is important
201 * to dump areas with ecc errors !
202 * Userspace software which accesses NAND this way
203 * must be aware of the fact that it deals with NAND
205 if (!ret
|| (ret
== -EBADMSG
)) {
207 if (copy_to_user(buf
, kbuf
, retlen
)) {
212 total_retlen
+= retlen
;
230 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
232 struct mtd_info
*mtd
= TO_MTD(file
);
235 size_t total_retlen
=0;
239 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
241 if (*ppos
== mtd
->size
)
244 if (*ppos
+ count
> mtd
->size
)
245 count
= mtd
->size
- *ppos
;
250 if (count
> MAX_KMALLOC_SIZE
)
251 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
253 kbuf
=kmalloc(count
, GFP_KERNEL
);
260 if (count
> MAX_KMALLOC_SIZE
)
261 len
= MAX_KMALLOC_SIZE
;
265 if (copy_from_user(kbuf
, buf
, len
)) {
270 switch (MTD_MODE(file
)) {
271 case MTD_MODE_OTP_FACT
:
274 case MTD_MODE_OTP_USER
:
275 if (!mtd
->write_user_prot_reg
) {
279 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
282 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
286 total_retlen
+= retlen
;
300 /*======================================================================
302 IOCTL calls for getting device parameters.
304 ======================================================================*/
305 static void mtdchar_erase_callback (struct erase_info
*instr
)
307 wake_up((wait_queue_head_t
*)instr
->priv
);
310 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
311 u_int cmd
, u_long arg
)
313 struct mtd_info
*mtd
= TO_MTD(file
);
314 void __user
*argp
= (void __user
*)arg
;
318 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
320 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
322 if (!access_ok(VERIFY_READ
, argp
, size
))
326 if (!access_ok(VERIFY_WRITE
, argp
, size
))
331 case MEMGETREGIONCOUNT
:
332 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
336 case MEMGETREGIONINFO
:
338 struct region_info_user ur
;
340 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
343 if (ur
.regionindex
>= mtd
->numeraseregions
)
345 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
346 sizeof(struct mtd_erase_region_info
)))
352 if (copy_to_user(argp
, mtd
, sizeof(struct mtd_info_user
)))
358 struct erase_info
*erase
;
360 if(!(file
->f_mode
& 2))
363 erase
=kmalloc(sizeof(struct erase_info
),GFP_KERNEL
);
367 wait_queue_head_t waitq
;
368 DECLARE_WAITQUEUE(wait
, current
);
370 init_waitqueue_head(&waitq
);
372 memset (erase
,0,sizeof(struct erase_info
));
373 if (copy_from_user(&erase
->addr
, argp
,
374 sizeof(struct erase_info_user
))) {
379 erase
->callback
= mtdchar_erase_callback
;
380 erase
->priv
= (unsigned long)&waitq
;
383 FIXME: Allow INTERRUPTIBLE. Which means
384 not having the wait_queue head on the stack.
386 If the wq_head is on the stack, and we
387 leave because we got interrupted, then the
388 wq_head is no longer there when the
389 callback routine tries to wake us up.
391 ret
= mtd
->erase(mtd
, erase
);
393 set_current_state(TASK_UNINTERRUPTIBLE
);
394 add_wait_queue(&waitq
, &wait
);
395 if (erase
->state
!= MTD_ERASE_DONE
&&
396 erase
->state
!= MTD_ERASE_FAILED
)
398 remove_wait_queue(&waitq
, &wait
);
399 set_current_state(TASK_RUNNING
);
401 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
410 struct mtd_oob_buf buf
;
414 if(!(file
->f_mode
& 2))
417 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
420 if (buf
.length
> 0x4096)
426 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
427 buf
.length
) ? 0 : EFAULT
;
432 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
436 if (copy_from_user(databuf
, buf
.ptr
, buf
.length
)) {
441 ret
= (mtd
->write_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
443 if (copy_to_user(argp
+ sizeof(uint32_t), &retlen
, sizeof(uint32_t)))
453 struct mtd_oob_buf buf
;
457 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
460 if (buf
.length
> 0x4096)
466 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
467 buf
.length
) ? 0 : -EFAULT
;
472 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
476 ret
= (mtd
->read_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
478 if (put_user(retlen
, (uint32_t __user
*)argp
))
480 else if (retlen
&& copy_to_user(buf
.ptr
, databuf
, retlen
))
489 struct erase_info_user info
;
491 if (copy_from_user(&info
, argp
, sizeof(info
)))
497 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
503 struct erase_info_user info
;
505 if (copy_from_user(&info
, argp
, sizeof(info
)))
511 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
515 /* Legacy interface */
518 struct nand_oobinfo oi
;
522 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
525 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
526 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
527 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
530 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
540 if (copy_to_user(argp
, &mtd
->ecclayout
,
541 sizeof(struct nand_ecclayout
)))
549 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
551 if (!mtd
->block_isbad
)
554 return mtd
->block_isbad(mtd
, offs
);
562 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
564 if (!mtd
->block_markbad
)
567 return mtd
->block_markbad(mtd
, offs
);
571 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
575 if (copy_from_user(&mode
, argp
, sizeof(int)))
577 SET_MTD_MODE(file
, 0);
579 case MTD_OTP_FACTORY
:
580 if (!mtd
->read_fact_prot_reg
)
583 SET_MTD_MODE(file
, MTD_MODE_OTP_FACT
);
586 if (!mtd
->read_fact_prot_reg
)
589 SET_MTD_MODE(file
, MTD_MODE_OTP_USER
);
600 case OTPGETREGIONCOUNT
:
601 case OTPGETREGIONINFO
:
603 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
607 switch (MTD_MODE(file
)) {
608 case MTD_MODE_OTP_FACT
:
609 if (mtd
->get_fact_prot_info
)
610 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
612 case MTD_MODE_OTP_USER
:
613 if (mtd
->get_user_prot_info
)
614 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
618 if (cmd
== OTPGETREGIONCOUNT
) {
619 int nbr
= ret
/ sizeof(struct otp_info
);
620 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
622 ret
= copy_to_user(argp
, buf
, ret
);
632 struct otp_info info
;
634 if (MTD_MODE(file
) != MTD_MODE_OTP_USER
)
636 if (copy_from_user(&info
, argp
, sizeof(info
)))
638 if (!mtd
->lock_user_prot_reg
)
640 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
652 static struct file_operations mtd_fops
= {
653 .owner
= THIS_MODULE
,
659 .release
= mtd_close
,
662 static int __init
init_mtdchar(void)
664 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
665 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
670 mtd_class
= class_create(THIS_MODULE
, "mtd");
672 if (IS_ERR(mtd_class
)) {
673 printk(KERN_ERR
"Error creating mtd class.\n");
674 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
675 return PTR_ERR(mtd_class
);
678 register_mtd_user(¬ifier
);
682 static void __exit
cleanup_mtdchar(void)
684 unregister_mtd_user(¬ifier
);
685 class_destroy(mtd_class
);
686 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
689 module_init(init_mtdchar
);
690 module_exit(cleanup_mtdchar
);
693 MODULE_LICENSE("GPL");
694 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
695 MODULE_DESCRIPTION("Direct character-device access to MTD devices");