2 * $Id: mtdchar.c,v 1.74 2005/08/04 01:05:48 tpoynor Exp $
4 * Character-device access to raw MTD devices.
8 #include <linux/config.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/compatmac.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
16 #include <linux/sched.h> /* TASK_* */
17 #include <asm/uaccess.h>
19 #include <linux/device.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 * We use file->private_data to store a pointer to the MTDdevice.
52 * Since alighment is at least 32 bits, we have 2 bits free for OTP
56 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
58 #define MTD_MODE_OTP_FACT 1
59 #define MTD_MODE_OTP_USER 2
60 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
62 #define SET_MTD_MODE(file, mode) \
63 do { long __p = (long)((file)->private_data); \
64 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
66 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
68 struct mtd_info
*mtd
= TO_MTD(file
);
76 offset
+= file
->f_pos
;
86 if (offset
>= 0 && offset
< mtd
->size
)
87 return file
->f_pos
= offset
;
94 static int mtd_open(struct inode
*inode
, struct file
*file
)
96 int minor
= iminor(inode
);
97 int devnum
= minor
>> 1;
100 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
102 if (devnum
>= MAX_MTD_DEVICES
)
105 /* You can't open the RO devices RW */
106 if ((file
->f_mode
& 2) && (minor
& 1))
109 mtd
= get_mtd_device(NULL
, devnum
);
114 if (MTD_ABSENT
== mtd
->type
) {
119 file
->private_data
= mtd
;
121 /* You can't open it RW if it's not a writeable device */
122 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
130 /*====================================================================*/
132 static int mtd_close(struct inode
*inode
, struct file
*file
)
134 struct mtd_info
*mtd
;
136 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
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_info
*mtd
= TO_MTD(file
);
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 len
= MAX_KMALLOC_SIZE
;
178 kbuf
=kmalloc(len
,GFP_KERNEL
);
182 switch (MTD_MODE(file
)) {
183 case MTD_MODE_OTP_FACT
:
184 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
186 case MTD_MODE_OTP_USER
:
187 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
190 ret
= MTD_READ(mtd
, *ppos
, len
, &retlen
, kbuf
);
192 /* Nand returns -EBADMSG on ecc errors, but it returns
193 * the data. For our userspace tools it is important
194 * to dump areas with ecc errors !
195 * Userspace software which accesses NAND this way
196 * must be aware of the fact that it deals with NAND
198 if (!ret
|| (ret
== -EBADMSG
)) {
200 if (copy_to_user(buf
, kbuf
, retlen
)) {
205 total_retlen
+= retlen
;
223 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
225 struct mtd_info
*mtd
= TO_MTD(file
);
228 size_t total_retlen
=0;
232 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
234 if (*ppos
== mtd
->size
)
237 if (*ppos
+ count
> mtd
->size
)
238 count
= mtd
->size
- *ppos
;
244 if (count
> MAX_KMALLOC_SIZE
)
245 len
= MAX_KMALLOC_SIZE
;
249 kbuf
=kmalloc(len
,GFP_KERNEL
);
251 printk("kmalloc is null\n");
255 if (copy_from_user(kbuf
, buf
, len
)) {
260 switch (MTD_MODE(file
)) {
261 case MTD_MODE_OTP_FACT
:
264 case MTD_MODE_OTP_USER
:
265 if (!mtd
->write_user_prot_reg
) {
269 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
272 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
276 total_retlen
+= retlen
;
291 /*======================================================================
293 IOCTL calls for getting device parameters.
295 ======================================================================*/
296 static void mtdchar_erase_callback (struct erase_info
*instr
)
298 wake_up((wait_queue_head_t
*)instr
->priv
);
301 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
302 u_int cmd
, u_long arg
)
304 struct mtd_info
*mtd
= TO_MTD(file
);
305 void __user
*argp
= (void __user
*)arg
;
309 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
311 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
313 if (!access_ok(VERIFY_READ
, argp
, size
))
317 if (!access_ok(VERIFY_WRITE
, argp
, size
))
322 case MEMGETREGIONCOUNT
:
323 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
327 case MEMGETREGIONINFO
:
329 struct region_info_user ur
;
331 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
334 if (ur
.regionindex
>= mtd
->numeraseregions
)
336 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
337 sizeof(struct mtd_erase_region_info
)))
343 if (copy_to_user(argp
, mtd
, sizeof(struct mtd_info_user
)))
349 struct erase_info
*erase
;
351 if(!(file
->f_mode
& 2))
354 erase
=kmalloc(sizeof(struct erase_info
),GFP_KERNEL
);
358 wait_queue_head_t waitq
;
359 DECLARE_WAITQUEUE(wait
, current
);
361 init_waitqueue_head(&waitq
);
363 memset (erase
,0,sizeof(struct erase_info
));
364 if (copy_from_user(&erase
->addr
, argp
,
365 sizeof(struct erase_info_user
))) {
370 erase
->callback
= mtdchar_erase_callback
;
371 erase
->priv
= (unsigned long)&waitq
;
374 FIXME: Allow INTERRUPTIBLE. Which means
375 not having the wait_queue head on the stack.
377 If the wq_head is on the stack, and we
378 leave because we got interrupted, then the
379 wq_head is no longer there when the
380 callback routine tries to wake us up.
382 ret
= mtd
->erase(mtd
, erase
);
384 set_current_state(TASK_UNINTERRUPTIBLE
);
385 add_wait_queue(&waitq
, &wait
);
386 if (erase
->state
!= MTD_ERASE_DONE
&&
387 erase
->state
!= MTD_ERASE_FAILED
)
389 remove_wait_queue(&waitq
, &wait
);
390 set_current_state(TASK_RUNNING
);
392 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
401 struct mtd_oob_buf buf
;
405 if(!(file
->f_mode
& 2))
408 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
411 if (buf
.length
> 0x4096)
417 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
418 buf
.length
) ? 0 : EFAULT
;
423 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
427 if (copy_from_user(databuf
, buf
.ptr
, buf
.length
)) {
432 ret
= (mtd
->write_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
434 if (copy_to_user(argp
+ sizeof(uint32_t), &retlen
, sizeof(uint32_t)))
444 struct mtd_oob_buf buf
;
448 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
451 if (buf
.length
> 0x4096)
457 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
458 buf
.length
) ? 0 : -EFAULT
;
463 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
467 ret
= (mtd
->read_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
469 if (put_user(retlen
, (uint32_t __user
*)argp
))
471 else if (retlen
&& copy_to_user(buf
.ptr
, databuf
, retlen
))
480 struct erase_info_user info
;
482 if (copy_from_user(&info
, argp
, sizeof(info
)))
488 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
494 struct erase_info_user info
;
496 if (copy_from_user(&info
, argp
, sizeof(info
)))
502 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
508 if (copy_from_user(&mtd
->oobinfo
, argp
, sizeof(struct nand_oobinfo
)))
515 if (copy_to_user(argp
, &(mtd
->oobinfo
), sizeof(struct nand_oobinfo
)))
524 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
526 if (!mtd
->block_isbad
)
529 return mtd
->block_isbad(mtd
, offs
);
537 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
539 if (!mtd
->block_markbad
)
542 return mtd
->block_markbad(mtd
, offs
);
546 #ifdef CONFIG_MTD_OTP
550 if (copy_from_user(&mode
, argp
, sizeof(int)))
552 SET_MTD_MODE(file
, 0);
554 case MTD_OTP_FACTORY
:
555 if (!mtd
->read_fact_prot_reg
)
558 SET_MTD_MODE(file
, MTD_MODE_OTP_FACT
);
561 if (!mtd
->read_fact_prot_reg
)
564 SET_MTD_MODE(file
, MTD_MODE_OTP_USER
);
575 case OTPGETREGIONCOUNT
:
576 case OTPGETREGIONINFO
:
578 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
582 switch (MTD_MODE(file
)) {
583 case MTD_MODE_OTP_FACT
:
584 if (mtd
->get_fact_prot_info
)
585 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
587 case MTD_MODE_OTP_USER
:
588 if (mtd
->get_user_prot_info
)
589 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
593 if (cmd
== OTPGETREGIONCOUNT
) {
594 int nbr
= ret
/ sizeof(struct otp_info
);
595 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
597 ret
= copy_to_user(argp
, buf
, ret
);
607 struct otp_info info
;
609 if (MTD_MODE(file
) != MTD_MODE_OTP_USER
)
611 if (copy_from_user(&info
, argp
, sizeof(info
)))
613 if (!mtd
->lock_user_prot_reg
)
615 ret
= mtd
->lock_user_prot_reg(mtd
, info
.start
, info
.length
);
627 static struct file_operations mtd_fops
= {
628 .owner
= THIS_MODULE
,
634 .release
= mtd_close
,
637 static int __init
init_mtdchar(void)
639 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
640 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
645 mtd_class
= class_create(THIS_MODULE
, "mtd");
647 if (IS_ERR(mtd_class
)) {
648 printk(KERN_ERR
"Error creating mtd class.\n");
649 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
650 return PTR_ERR(mtd_class
);
653 register_mtd_user(¬ifier
);
657 static void __exit
cleanup_mtdchar(void)
659 unregister_mtd_user(¬ifier
);
660 class_destroy(mtd_class
);
661 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
664 module_init(init_mtdchar
);
665 module_exit(cleanup_mtdchar
);
668 MODULE_LICENSE("GPL");
669 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
670 MODULE_DESCRIPTION("Direct character-device access to MTD devices");