2 * $Id: mtdchar.c,v 1.67 2005/02/08 17:45:51 nico 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)
63 /* Well... let's abuse the unused bits in file->f_mode for those */
64 #define MTD_MODE_OTP_FACT 0x1000
65 #define MTD_MODE_OTP_USER 0x2000
66 #define MTD_MODE_MASK 0xf000
68 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
70 struct mtd_info *mtd = file->private_data;
79 file->f_pos += offset;
83 file->f_pos =mtd->size + offset;
91 else if (file->f_pos >= mtd->size)
92 file->f_pos = mtd->size - 1;
99 static int mtd_open(struct inode *inode, struct file *file)
101 int minor = iminor(inode);
102 int devnum = minor >> 1;
103 struct mtd_info *mtd;
105 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
107 if (devnum >= MAX_MTD_DEVICES)
110 /* You can't open the RO devices RW */
111 if ((file->f_mode & 2) && (minor & 1))
114 /* make sure the locally abused bits are initialy clear */
115 if (file->f_mode & MTD_MODE_MASK)
118 mtd = get_mtd_device(NULL, devnum);
123 if (MTD_ABSENT == mtd->type) {
128 file->private_data = mtd;
130 /* You can't open it RW if it's not a writeable device */
131 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
139 /*====================================================================*/
141 static int mtd_close(struct inode *inode, struct file *file)
143 struct mtd_info *mtd;
145 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
147 mtd = file->private_data;
157 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
158 userspace buffer down and use it directly with readv/writev.
160 #define MAX_KMALLOC_SIZE 0x20000
162 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
164 struct mtd_info *mtd = file->private_data;
166 size_t total_retlen=0;
171 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
173 if (*ppos + count > mtd->size)
174 count = mtd->size - *ppos;
179 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
180 and pass them directly to the MTD functions */
182 if (count > MAX_KMALLOC_SIZE)
183 len = MAX_KMALLOC_SIZE;
187 kbuf=kmalloc(len,GFP_KERNEL);
191 switch (file->f_mode & MTD_MODE_MASK) {
192 case MTD_MODE_OTP_FACT:
193 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
195 case MTD_MODE_OTP_USER:
196 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
199 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
201 /* Nand returns -EBADMSG on ecc errors, but it returns
202 * the data. For our userspace tools it is important
203 * to dump areas with ecc errors !
204 * Userspace software which accesses NAND this way
205 * must be aware of the fact that it deals with NAND
207 if (!ret || (ret == -EBADMSG)) {
209 if (copy_to_user(buf, kbuf, retlen)) {
214 total_retlen += retlen;
232 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
234 struct mtd_info *mtd = file->private_data;
237 size_t total_retlen=0;
241 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
243 if (*ppos == mtd->size)
246 if (*ppos + count > mtd->size)
247 count = mtd->size - *ppos;
253 if (count > MAX_KMALLOC_SIZE)
254 len = MAX_KMALLOC_SIZE;
258 kbuf=kmalloc(len,GFP_KERNEL);
260 printk("kmalloc is null\n");
264 if (copy_from_user(kbuf, buf, len)) {
269 switch (file->f_mode & MTD_MODE_MASK) {
270 case MTD_MODE_OTP_FACT:
273 case MTD_MODE_OTP_USER:
274 if (!mtd->write_user_prot_reg) {
278 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
281 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
285 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 = file->private_data;
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);
517 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
524 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
533 if (copy_from_user(&offs, argp, sizeof(loff_t)))
535 if (!mtd->block_isbad)
538 return mtd->block_isbad(mtd, offs);
546 if (copy_from_user(&offs, argp, sizeof(loff_t)))
548 if (!mtd->block_markbad)
551 return mtd->block_markbad(mtd, offs);
555 #ifdef CONFIG_MTD_OTP
559 if (copy_from_user(&mode, argp, sizeof(int)))
561 file->f_mode &= ~MTD_MODE_MASK;
563 case MTD_OTP_FACTORY:
564 if (!mtd->read_fact_prot_reg)
567 file->f_mode |= MTD_MODE_OTP_FACT;
570 if (!mtd->read_fact_prot_reg)
573 file->f_mode |= MTD_MODE_OTP_USER;
583 case OTPGETREGIONCOUNT:
584 case OTPGETREGIONINFO:
586 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
590 switch (file->f_mode & MTD_MODE_MASK) {
591 case MTD_MODE_OTP_FACT:
592 if (mtd->get_fact_prot_info)
593 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
595 case MTD_MODE_OTP_USER:
596 if (mtd->get_user_prot_info)
597 ret = mtd->get_user_prot_info(mtd, buf, 4096);
601 if (cmd == OTPGETREGIONCOUNT) {
602 int nbr = ret / sizeof(struct otp_info);
603 ret = copy_to_user(argp, &nbr, sizeof(int));
605 ret = copy_to_user(argp, buf, ret);
615 struct otp_info info;
617 if ((file->f_mode & MTD_MODE_MASK) != MTD_MODE_OTP_USER)
619 if (copy_from_user(&info, argp, sizeof(info)))
621 if (!mtd->lock_user_prot_reg)
623 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
635 static struct file_operations mtd_fops = {
636 .owner = THIS_MODULE,
642 .release = mtd_close,
645 static int __init init_mtdchar(void)
647 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
648 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
653 mtdchar_devfs_init();
657 static void __exit cleanup_mtdchar(void)
659 mtdchar_devfs_exit();
660 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
663 module_init(init_mtdchar);
664 module_exit(cleanup_mtdchar);
667 MODULE_LICENSE("GPL");
668 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
669 MODULE_DESCRIPTION("Direct character-device access to MTD devices");