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 <asm/uaccess.h>
18 #include <linux/device.h>
20 static struct class *mtd_class;
22 static void mtd_notify_add(struct mtd_info* mtd)
27 class_device_create(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
28 NULL, "mtd%d", mtd->index);
30 class_device_create(mtd_class,
31 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
32 NULL, "mtd%dro", mtd->index);
35 static void mtd_notify_remove(struct mtd_info* mtd)
40 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
41 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
44 static struct mtd_notifier notifier = {
45 .add = mtd_notify_add,
46 .remove = mtd_notify_remove,
50 * We use file->private_data to store a pointer to the MTDdevice.
51 * Since alighment is at least 32 bits, we have 2 bits free for OTP
55 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
57 #define MTD_MODE_OTP_FACT 1
58 #define MTD_MODE_OTP_USER 2
59 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
61 #define SET_MTD_MODE(file, mode) \
62 do { long __p = (long)((file)->private_data); \
63 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
65 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
67 struct mtd_info *mtd = TO_MTD(file);
75 offset += file->f_pos;
85 if (offset >= 0 && offset < mtd->size)
86 return file->f_pos = offset;
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");
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 = TO_MTD(file);
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 switch (MTD_MODE(file)) {
182 case MTD_MODE_OTP_FACT:
183 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
185 case MTD_MODE_OTP_USER:
186 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
189 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
191 /* Nand returns -EBADMSG on ecc errors, but it returns
192 * the data. For our userspace tools it is important
193 * to dump areas with ecc errors !
194 * Userspace software which accesses NAND this way
195 * must be aware of the fact that it deals with NAND
197 if (!ret || (ret == -EBADMSG)) {
199 if (copy_to_user(buf, kbuf, retlen)) {
204 total_retlen += retlen;
222 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
224 struct mtd_info *mtd = TO_MTD(file);
227 size_t total_retlen=0;
231 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
233 if (*ppos == mtd->size)
236 if (*ppos + count > mtd->size)
237 count = mtd->size - *ppos;
243 if (count > MAX_KMALLOC_SIZE)
244 len = MAX_KMALLOC_SIZE;
248 kbuf=kmalloc(len,GFP_KERNEL);
250 printk("kmalloc is null\n");
254 if (copy_from_user(kbuf, buf, len)) {
259 switch (MTD_MODE(file)) {
260 case MTD_MODE_OTP_FACT:
263 case MTD_MODE_OTP_USER:
264 if (!mtd->write_user_prot_reg) {
268 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
271 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
275 total_retlen += retlen;
290 /*======================================================================
292 IOCTL calls for getting device parameters.
294 ======================================================================*/
295 static void mtdchar_erase_callback (struct erase_info *instr)
297 wake_up((wait_queue_head_t *)instr->priv);
300 static int mtd_ioctl(struct inode *inode, struct file *file,
301 u_int cmd, u_long arg)
303 struct mtd_info *mtd = TO_MTD(file);
304 void __user *argp = (void __user *)arg;
308 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
310 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
312 if (!access_ok(VERIFY_READ, argp, size))
316 if (!access_ok(VERIFY_WRITE, argp, size))
321 case MEMGETREGIONCOUNT:
322 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
326 case MEMGETREGIONINFO:
328 struct region_info_user ur;
330 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
333 if (ur.regionindex >= mtd->numeraseregions)
335 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
336 sizeof(struct mtd_erase_region_info)))
342 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
348 struct erase_info *erase;
350 if(!(file->f_mode & 2))
353 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
357 wait_queue_head_t waitq;
358 DECLARE_WAITQUEUE(wait, current);
360 init_waitqueue_head(&waitq);
362 memset (erase,0,sizeof(struct erase_info));
363 if (copy_from_user(&erase->addr, argp,
364 sizeof(struct erase_info_user))) {
369 erase->callback = mtdchar_erase_callback;
370 erase->priv = (unsigned long)&waitq;
373 FIXME: Allow INTERRUPTIBLE. Which means
374 not having the wait_queue head on the stack.
376 If the wq_head is on the stack, and we
377 leave because we got interrupted, then the
378 wq_head is no longer there when the
379 callback routine tries to wake us up.
381 ret = mtd->erase(mtd, erase);
383 set_current_state(TASK_UNINTERRUPTIBLE);
384 add_wait_queue(&waitq, &wait);
385 if (erase->state != MTD_ERASE_DONE &&
386 erase->state != MTD_ERASE_FAILED)
388 remove_wait_queue(&waitq, &wait);
389 set_current_state(TASK_RUNNING);
391 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
400 struct mtd_oob_buf buf;
404 if(!(file->f_mode & 2))
407 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
410 if (buf.length > 0x4096)
416 ret = access_ok(VERIFY_READ, buf.ptr,
417 buf.length) ? 0 : EFAULT;
422 databuf = kmalloc(buf.length, GFP_KERNEL);
426 if (copy_from_user(databuf, buf.ptr, buf.length)) {
431 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
433 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
443 struct mtd_oob_buf buf;
447 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
450 if (buf.length > 0x4096)
456 ret = access_ok(VERIFY_WRITE, buf.ptr,
457 buf.length) ? 0 : -EFAULT;
462 databuf = kmalloc(buf.length, GFP_KERNEL);
466 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
468 if (put_user(retlen, (uint32_t __user *)argp))
470 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
479 struct erase_info_user info;
481 if (copy_from_user(&info, argp, sizeof(info)))
487 ret = mtd->lock(mtd, info.start, info.length);
493 struct erase_info_user info;
495 if (copy_from_user(&info, argp, sizeof(info)))
501 ret = mtd->unlock(mtd, info.start, info.length);
507 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
514 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
523 if (copy_from_user(&offs, argp, sizeof(loff_t)))
525 if (!mtd->block_isbad)
528 return mtd->block_isbad(mtd, offs);
536 if (copy_from_user(&offs, argp, sizeof(loff_t)))
538 if (!mtd->block_markbad)
541 return mtd->block_markbad(mtd, offs);
545 #ifdef CONFIG_MTD_OTP
549 if (copy_from_user(&mode, argp, sizeof(int)))
551 SET_MTD_MODE(file, 0);
553 case MTD_OTP_FACTORY:
554 if (!mtd->read_fact_prot_reg)
557 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
560 if (!mtd->read_fact_prot_reg)
563 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
574 case OTPGETREGIONCOUNT:
575 case OTPGETREGIONINFO:
577 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
581 switch (MTD_MODE(file)) {
582 case MTD_MODE_OTP_FACT:
583 if (mtd->get_fact_prot_info)
584 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
586 case MTD_MODE_OTP_USER:
587 if (mtd->get_user_prot_info)
588 ret = mtd->get_user_prot_info(mtd, buf, 4096);
592 if (cmd == OTPGETREGIONCOUNT) {
593 int nbr = ret / sizeof(struct otp_info);
594 ret = copy_to_user(argp, &nbr, sizeof(int));
596 ret = copy_to_user(argp, buf, ret);
606 struct otp_info info;
608 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
610 if (copy_from_user(&info, argp, sizeof(info)))
612 if (!mtd->lock_user_prot_reg)
614 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
626 static struct file_operations mtd_fops = {
627 .owner = THIS_MODULE,
633 .release = mtd_close,
636 static int __init init_mtdchar(void)
638 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
639 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
644 mtd_class = class_create(THIS_MODULE, "mtd");
646 if (IS_ERR(mtd_class)) {
647 printk(KERN_ERR "Error creating mtd class.\n");
648 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
649 return PTR_ERR(mtd_class);
652 register_mtd_user(¬ifier);
656 static void __exit cleanup_mtdchar(void)
658 unregister_mtd_user(¬ifier);
659 class_destroy(mtd_class);
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");