2 * $Id: mtdchar.c,v 1.64 2004/08/09 13:59:46 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
=(struct mtd_info
*)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
= (struct mtd_info
*)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
= (struct mtd_info
*)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
);
184 if (copy_to_user(buf
, kbuf
, retlen
)) {
189 total_retlen
+= retlen
;
205 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
207 struct mtd_info
*mtd
= (struct mtd_info
*)file
->private_data
;
210 size_t total_retlen
=0;
214 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
216 if (*ppos
== mtd
->size
)
219 if (*ppos
+ count
> mtd
->size
)
220 count
= mtd
->size
- *ppos
;
226 if (count
> MAX_KMALLOC_SIZE
)
227 len
= MAX_KMALLOC_SIZE
;
231 kbuf
=kmalloc(len
,GFP_KERNEL
);
233 printk("kmalloc is null\n");
237 if (copy_from_user(kbuf
, buf
, len
)) {
242 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
245 total_retlen
+= retlen
;
260 /*======================================================================
262 IOCTL calls for getting device parameters.
264 ======================================================================*/
265 static void mtdchar_erase_callback (struct erase_info
*instr
)
267 wake_up((wait_queue_head_t
*)instr
->priv
);
270 static int mtd_ioctl(struct inode
*inode
, struct file
*file
,
271 u_int cmd
, u_long arg
)
273 struct mtd_info
*mtd
= (struct mtd_info
*)file
->private_data
;
274 void __user
*argp
= (void __user
*)arg
;
278 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
280 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
282 ret
= verify_area(VERIFY_READ
, argp
, size
);
286 ret
= verify_area(VERIFY_WRITE
, argp
, size
);
291 case MEMGETREGIONCOUNT
:
292 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
296 case MEMGETREGIONINFO
:
298 struct region_info_user ur
;
300 if (copy_from_user(&ur
, argp
, sizeof(struct region_info_user
)))
303 if (ur
.regionindex
>= mtd
->numeraseregions
)
305 if (copy_to_user(argp
, &(mtd
->eraseregions
[ur
.regionindex
]),
306 sizeof(struct mtd_erase_region_info
)))
312 if (copy_to_user(argp
, mtd
, sizeof(struct mtd_info_user
)))
318 struct erase_info
*erase
;
320 if(!(file
->f_mode
& 2))
323 erase
=kmalloc(sizeof(struct erase_info
),GFP_KERNEL
);
327 wait_queue_head_t waitq
;
328 DECLARE_WAITQUEUE(wait
, current
);
330 init_waitqueue_head(&waitq
);
332 memset (erase
,0,sizeof(struct erase_info
));
333 if (copy_from_user(&erase
->addr
, argp
,
334 sizeof(struct erase_info_user
))) {
339 erase
->callback
= mtdchar_erase_callback
;
340 erase
->priv
= (unsigned long)&waitq
;
343 FIXME: Allow INTERRUPTIBLE. Which means
344 not having the wait_queue head on the stack.
346 If the wq_head is on the stack, and we
347 leave because we got interrupted, then the
348 wq_head is no longer there when the
349 callback routine tries to wake us up.
351 ret
= mtd
->erase(mtd
, erase
);
353 #if 1 // mask by Victor Yu. 05-14-2007
354 set_current_state(TASK_UNINTERRUPTIBLE
);
356 add_wait_queue(&waitq
, &wait
);
357 if (erase
->state
!= MTD_ERASE_DONE
&&
358 erase
->state
!= MTD_ERASE_FAILED
)
360 remove_wait_queue(&waitq
, &wait
);
361 #if 1 // mask by Victor Yu. 05-14-2007
362 set_current_state(TASK_RUNNING
);
365 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
374 struct mtd_oob_buf buf
;
378 if(!(file
->f_mode
& 2))
381 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
384 if (buf
.length
> 0x4096)
390 ret
= verify_area(VERIFY_READ
, buf
.ptr
, buf
.length
);
395 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
399 if (copy_from_user(databuf
, buf
.ptr
, buf
.length
)) {
404 ret
= (mtd
->write_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
406 if (copy_to_user(argp
+ sizeof(uint32_t), &retlen
, sizeof(uint32_t)))
416 struct mtd_oob_buf buf
;
420 if (copy_from_user(&buf
, argp
, sizeof(struct mtd_oob_buf
)))
423 if (buf
.length
> 0x4096)
429 ret
= verify_area(VERIFY_WRITE
, buf
.ptr
, buf
.length
);
434 databuf
= kmalloc(buf
.length
, GFP_KERNEL
);
438 ret
= (mtd
->read_oob
)(mtd
, buf
.start
, buf
.length
, &retlen
, databuf
);
440 if (put_user(retlen
, (uint32_t __user
*)argp
))
442 else if (retlen
&& copy_to_user(buf
.ptr
, databuf
, retlen
))
451 struct erase_info_user info
;
453 if (copy_from_user(&info
, argp
, sizeof(info
)))
459 ret
= mtd
->lock(mtd
, info
.start
, info
.length
);
465 struct erase_info_user info
;
467 if (copy_from_user(&info
, argp
, sizeof(info
)))
473 ret
= mtd
->unlock(mtd
, info
.start
, info
.length
);
479 if (copy_from_user(&mtd
->oobinfo
, argp
, sizeof(struct nand_oobinfo
)))
486 if (copy_to_user(argp
, &(mtd
->oobinfo
), sizeof(struct nand_oobinfo
)))
495 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
497 if (!mtd
->block_isbad
)
500 return mtd
->block_isbad(mtd
, offs
);
508 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
510 if (!mtd
->block_markbad
)
513 return mtd
->block_markbad(mtd
, offs
);
524 static struct file_operations mtd_fops
= {
525 .owner
= THIS_MODULE
,
531 .release
= mtd_close
,
534 static int __init
init_mtdchar(void)
536 if (register_chrdev(MTD_CHAR_MAJOR
, "mtd", &mtd_fops
)) {
537 printk(KERN_NOTICE
"Can't allocate major number %d for Memory Technology Devices.\n",
542 mtdchar_devfs_init();
546 static void __exit
cleanup_mtdchar(void)
548 mtdchar_devfs_exit();
549 unregister_chrdev(MTD_CHAR_MAJOR
, "mtd");
552 module_init(init_mtdchar
);
553 module_exit(cleanup_mtdchar
);
556 MODULE_LICENSE("GPL");
557 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
558 MODULE_DESCRIPTION("Direct character-device access to MTD devices");