2 * $Id: mtdchar.c,v 1.66 2005/01/05 18:05:11 dwmw2 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 <asm/uaccess.h>
18 #ifdef CONFIG_DEVFS_FS
19 #include <linux/devfs_fs_kernel.h>
21 static void mtd_notify_add(struct mtd_info
* mtd
)
26 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2),
27 S_IFCHR
| S_IRUGO
| S_IWUGO
, "mtd/%d", mtd
->index
);
29 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR
, mtd
->index
*2+1),
30 S_IFCHR
| S_IRUGO
, "mtd/%dro", mtd
->index
);
33 static void mtd_notify_remove(struct mtd_info
* mtd
)
37 devfs_remove("mtd/%d", mtd
->index
);
38 devfs_remove("mtd/%dro", mtd
->index
);
41 static struct mtd_notifier notifier
= {
42 .add
= mtd_notify_add
,
43 .remove
= mtd_notify_remove
,
46 static inline void mtdchar_devfs_init(void)
49 register_mtd_user(¬ifier
);
52 static inline void mtdchar_devfs_exit(void)
54 unregister_mtd_user(¬ifier
);
58 #define mtdchar_devfs_init() do { } while(0)
59 #define mtdchar_devfs_exit() do { } while(0)
62 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
64 struct mtd_info
*mtd
= file
->private_data
;
73 file
->f_pos
+= offset
;
77 file
->f_pos
=mtd
->size
+ offset
;
85 else if (file
->f_pos
>= mtd
->size
)
86 file
->f_pos
= mtd
->size
- 1;
93 static int mtd_open(struct inode
*inode
, struct file
*file
)
95 int minor
= iminor(inode
);
96 int devnum
= minor
>> 1;
99 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
101 if (devnum
>= MAX_MTD_DEVICES
)
104 /* You can't open the RO devices RW */
105 if ((file
->f_mode
& 2) && (minor
& 1))
108 mtd
= get_mtd_device(NULL
, devnum
);
113 if (MTD_ABSENT
== mtd
->type
) {
118 file
->private_data
= mtd
;
120 /* You can't open it RW if it's not a writeable device */
121 if ((file
->f_mode
& 2) && !(mtd
->flags
& MTD_WRITEABLE
)) {
129 /*====================================================================*/
131 static int mtd_close(struct inode
*inode
, struct file
*file
)
133 struct mtd_info
*mtd
;
135 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
137 mtd
= file
->private_data
;
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_info
*mtd
= file
->private_data
;
156 size_t total_retlen
=0;
161 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
163 if (*ppos
+ count
> mtd
->size
)
164 count
= mtd
->size
- *ppos
;
169 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
170 and pass them directly to the MTD functions */
172 if (count
> MAX_KMALLOC_SIZE
)
173 len
= MAX_KMALLOC_SIZE
;
177 kbuf
=kmalloc(len
,GFP_KERNEL
);
181 ret
= MTD_READ(mtd
, *ppos
, len
, &retlen
, kbuf
);
182 /* Nand returns -EBADMSG on ecc errors, but it returns
183 * the data. For our userspace tools it is important
184 * to dump areas with ecc errors !
185 * Userspace software which accesses NAND this way
186 * must be aware of the fact that it deals with NAND
188 if (!ret
|| (ret
== -EBADMSG
)) {
190 if (copy_to_user(buf
, kbuf
, retlen
)) {
195 total_retlen
+= retlen
;
211 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
213 struct mtd_info
*mtd
= file
->private_data
;
216 size_t total_retlen
=0;
220 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
222 if (*ppos
== mtd
->size
)
225 if (*ppos
+ count
> mtd
->size
)
226 count
= mtd
->size
- *ppos
;
232 if (count
> MAX_KMALLOC_SIZE
)
233 len
= MAX_KMALLOC_SIZE
;
237 kbuf
=kmalloc(len
,GFP_KERNEL
);
239 printk("kmalloc is null\n");
243 if (copy_from_user(kbuf
, buf
, len
)) {
248 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
251 total_retlen
+= retlen
;
266 /*======================================================================
268 IOCTL calls for getting device parameters.
270 ======================================================================*/
271 static void mtdchar_erase_callback (struct erase_info
*instr
)
273 wake_up((wait_queue_head_t
*)instr
->priv
);
276 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
277 u_int cmd
, u_long arg
)
279 struct mtd_info
*mtd
= file
->private_data
;
280 void __user
*argp
= (void __user
*)arg
;
284 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
286 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
288 if (!access_ok(VERIFY_READ
, argp
, size
))
292 if (!access_ok(VERIFY_WRITE
, argp
, size
))
297 case MEMGETREGIONCOUNT
:
298 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
302 case MEMGETREGIONINFO
:
304 struct region_info_user ur
;
306 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
309 if (ur
.regionindex
>= mtd
->numeraseregions
)
311 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
312 sizeof(struct mtd_erase_region_info
)))
318 if (copy_to_user(argp
, mtd
, sizeof(struct mtd_info_user
)))
324 struct erase_info
*erase
;
326 if(!(file
->f_mode
& 2))
329 erase
=kmalloc(sizeof(struct erase_info
),GFP_KERNEL
);
333 wait_queue_head_t waitq
;
334 DECLARE_WAITQUEUE(wait
, current
);
336 init_waitqueue_head(&waitq
);
338 memset (erase
,0,sizeof(struct erase_info
));
339 if (copy_from_user(&erase
->addr
, argp
,
340 sizeof(struct erase_info_user
))) {
345 erase
->callback
= mtdchar_erase_callback
;
346 erase
->priv
= (unsigned long)&waitq
;
349 FIXME: Allow INTERRUPTIBLE. Which means
350 not having the wait_queue head on the stack.
352 If the wq_head is on the stack, and we
353 leave because we got interrupted, then the
354 wq_head is no longer there when the
355 callback routine tries to wake us up.
357 ret
= mtd
->erase(mtd
, erase
);
359 set_current_state(TASK_UNINTERRUPTIBLE
);
360 add_wait_queue(&waitq
, &wait
);
361 if (erase
->state
!= MTD_ERASE_DONE
&&
362 erase
->state
!= MTD_ERASE_FAILED
)
364 remove_wait_queue(&waitq
, &wait
);
365 set_current_state(TASK_RUNNING
);
367 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
376 struct mtd_oob_buf buf
;
380 if(!(file
->f_mode
& 2))
383 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
386 if (buf
.length
> 0x4096)
392 ret
= access_ok(VERIFY_READ
, buf
.ptr
,
393 buf
.length
) ? 0 : EFAULT
;
398 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
402 if (copy_from_user(databuf
, buf
.ptr
, buf
.length
)) {
407 ret
= (mtd
->write_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
409 if (copy_to_user(argp
+ sizeof(uint32_t), &retlen
, sizeof(uint32_t)))
419 struct mtd_oob_buf buf
;
423 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
426 if (buf
.length
> 0x4096)
432 ret
= access_ok(VERIFY_WRITE
, buf
.ptr
,
433 buf
.length
) ? 0 : -EFAULT
;
438 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
442 ret
= (mtd
->read_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
444 if (put_user(retlen
, (uint32_t __user
*)argp
))
446 else if (retlen
&& copy_to_user(buf
.ptr
, databuf
, retlen
))
455 struct erase_info_user info
;
457 if (copy_from_user(&info
, argp
, sizeof(info
)))
463 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
469 struct erase_info_user info
;
471 if (copy_from_user(&info
, argp
, sizeof(info
)))
477 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
483 if (copy_from_user(&mtd
->oobinfo
, argp
, sizeof(struct nand_oobinfo
)))
490 if (copy_to_user(argp
, &(mtd
->oobinfo
), sizeof(struct nand_oobinfo
)))
499 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
501 if (!mtd
->block_isbad
)
504 return mtd
->block_isbad(mtd
, offs
);
512 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
514 if (!mtd
->block_markbad
)
517 return mtd
->block_markbad(mtd
, offs
);
528 static struct file_operations mtd_fops
= {
529 .owner
= THIS_MODULE
,
535 .release
= mtd_close
,
538 static int __init
init_mtdchar(void)
540 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
541 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
546 mtdchar_devfs_init();
550 static void __exit
cleanup_mtdchar(void)
552 mtdchar_devfs_exit();
553 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
556 module_init(init_mtdchar
);
557 module_exit(cleanup_mtdchar
);
560 MODULE_LICENSE("GPL");
561 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
562 MODULE_DESCRIPTION("Direct character-device access to MTD devices");