2 * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
4 * Character-device access to raw MTD devices.
8 #include <linux/config.h>
9 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/compatmac.h>
20 #include <asm/uaccess.h>
22 static struct class *mtd_class;
24 static void mtd_notify_add(struct mtd_info* mtd)
29 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
30 NULL, "mtd%d", mtd->index);
32 class_device_create(mtd_class, NULL,
33 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
34 NULL, "mtd%dro", mtd->index);
37 static void mtd_notify_remove(struct mtd_info* mtd)
42 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
43 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
46 static struct mtd_notifier notifier = {
47 .add = mtd_notify_add,
48 .remove = mtd_notify_remove,
52 * We use file->private_data to store a pointer to the MTDdevice.
53 * Since alighment is at least 32 bits, we have 2 bits free for OTP
57 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
59 #define MTD_MODE_OTP_FACT 1
60 #define MTD_MODE_OTP_USER 2
61 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
63 #define SET_MTD_MODE(file, mode) \
64 do { long __p = (long)((file)->private_data); \
65 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
67 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
69 struct mtd_info *mtd = TO_MTD(file);
77 offset += file->f_pos;
87 if (offset >= 0 && offset < mtd->size)
88 return file->f_pos = offset;
95 static int mtd_open(struct inode *inode, struct file *file)
97 int minor = iminor(inode);
98 int devnum = minor >> 1;
101 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
103 if (devnum >= MAX_MTD_DEVICES)
106 /* You can't open the RO devices RW */
107 if ((file->f_mode & 2) && (minor & 1))
110 mtd = get_mtd_device(NULL, devnum);
115 if (MTD_ABSENT == mtd->type) {
120 file->private_data = mtd;
122 /* You can't open it RW if it's not a writeable device */
123 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
131 /*====================================================================*/
133 static int mtd_close(struct inode *inode, struct file *file)
135 struct mtd_info *mtd;
137 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
149 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
150 userspace buffer down and use it directly with readv/writev.
152 #define MAX_KMALLOC_SIZE 0x20000
154 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
156 struct mtd_info *mtd = TO_MTD(file);
158 size_t total_retlen=0;
163 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
165 if (*ppos + count > mtd->size)
166 count = mtd->size - *ppos;
171 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
172 and pass them directly to the MTD functions */
174 if (count > MAX_KMALLOC_SIZE)
175 len = MAX_KMALLOC_SIZE;
179 kbuf=kmalloc(len,GFP_KERNEL);
183 switch (MTD_MODE(file)) {
184 case MTD_MODE_OTP_FACT:
185 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
187 case MTD_MODE_OTP_USER:
188 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
191 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
193 /* Nand returns -EBADMSG on ecc errors, but it returns
194 * the data. For our userspace tools it is important
195 * to dump areas with ecc errors !
196 * Userspace software which accesses NAND this way
197 * must be aware of the fact that it deals with NAND
199 if (!ret || (ret == -EBADMSG)) {
201 if (copy_to_user(buf, kbuf, retlen)) {
206 total_retlen += retlen;
224 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
226 struct mtd_info *mtd = TO_MTD(file);
229 size_t total_retlen=0;
233 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
235 if (*ppos == mtd->size)
238 if (*ppos + count > mtd->size)
239 count = mtd->size - *ppos;
245 if (count > MAX_KMALLOC_SIZE)
246 len = MAX_KMALLOC_SIZE;
250 kbuf=kmalloc(len,GFP_KERNEL);
252 printk("kmalloc is null\n");
256 if (copy_from_user(kbuf, buf, len)) {
261 switch (MTD_MODE(file)) {
262 case MTD_MODE_OTP_FACT:
265 case MTD_MODE_OTP_USER:
266 if (!mtd->write_user_prot_reg) {
270 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
273 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
277 total_retlen += retlen;
292 /*======================================================================
294 IOCTL calls for getting device parameters.
296 ======================================================================*/
297 static void mtdchar_erase_callback (struct erase_info *instr)
299 wake_up((wait_queue_head_t *)instr->priv);
302 static int mtd_ioctl(struct inode *inode, struct file *file,
303 u_int cmd, u_long arg)
305 struct mtd_info *mtd = TO_MTD(file);
306 void __user *argp = (void __user *)arg;
310 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
312 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
314 if (!access_ok(VERIFY_READ, argp, size))
318 if (!access_ok(VERIFY_WRITE, argp, size))
323 case MEMGETREGIONCOUNT:
324 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
328 case MEMGETREGIONINFO:
330 struct region_info_user ur;
332 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
335 if (ur.regionindex >= mtd->numeraseregions)
337 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
338 sizeof(struct mtd_erase_region_info)))
344 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
350 struct erase_info *erase;
352 if(!(file->f_mode & 2))
355 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
359 wait_queue_head_t waitq;
360 DECLARE_WAITQUEUE(wait, current);
362 init_waitqueue_head(&waitq);
364 memset (erase,0,sizeof(struct erase_info));
365 if (copy_from_user(&erase->addr, argp,
366 sizeof(struct erase_info_user))) {
371 erase->callback = mtdchar_erase_callback;
372 erase->priv = (unsigned long)&waitq;
375 FIXME: Allow INTERRUPTIBLE. Which means
376 not having the wait_queue head on the stack.
378 If the wq_head is on the stack, and we
379 leave because we got interrupted, then the
380 wq_head is no longer there when the
381 callback routine tries to wake us up.
383 ret = mtd->erase(mtd, erase);
385 set_current_state(TASK_UNINTERRUPTIBLE);
386 add_wait_queue(&waitq, &wait);
387 if (erase->state != MTD_ERASE_DONE &&
388 erase->state != MTD_ERASE_FAILED)
390 remove_wait_queue(&waitq, &wait);
391 set_current_state(TASK_RUNNING);
393 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
402 struct mtd_oob_buf buf;
406 if(!(file->f_mode & 2))
409 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
412 if (buf.length > 0x4096)
418 ret = access_ok(VERIFY_READ, buf.ptr,
419 buf.length) ? 0 : EFAULT;
424 databuf = kmalloc(buf.length, GFP_KERNEL);
428 if (copy_from_user(databuf, buf.ptr, buf.length)) {
433 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
435 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
445 struct mtd_oob_buf buf;
449 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
452 if (buf.length > 0x4096)
458 ret = access_ok(VERIFY_WRITE, buf.ptr,
459 buf.length) ? 0 : -EFAULT;
464 databuf = kmalloc(buf.length, GFP_KERNEL);
468 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
470 if (put_user(retlen, (uint32_t __user *)argp))
472 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
481 struct erase_info_user info;
483 if (copy_from_user(&info, argp, sizeof(info)))
489 ret = mtd->lock(mtd, info.start, info.length);
495 struct erase_info_user info;
497 if (copy_from_user(&info, argp, sizeof(info)))
503 ret = mtd->unlock(mtd, info.start, info.length);
509 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
516 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
525 if (copy_from_user(&offs, argp, sizeof(loff_t)))
527 if (!mtd->block_isbad)
530 return mtd->block_isbad(mtd, offs);
538 if (copy_from_user(&offs, argp, sizeof(loff_t)))
540 if (!mtd->block_markbad)
543 return mtd->block_markbad(mtd, offs);
547 #ifdef CONFIG_MTD_OTP
551 if (copy_from_user(&mode, argp, sizeof(int)))
553 SET_MTD_MODE(file, 0);
555 case MTD_OTP_FACTORY:
556 if (!mtd->read_fact_prot_reg)
559 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
562 if (!mtd->read_fact_prot_reg)
565 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
576 case OTPGETREGIONCOUNT:
577 case OTPGETREGIONINFO:
579 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
583 switch (MTD_MODE(file)) {
584 case MTD_MODE_OTP_FACT:
585 if (mtd->get_fact_prot_info)
586 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
588 case MTD_MODE_OTP_USER:
589 if (mtd->get_user_prot_info)
590 ret = mtd->get_user_prot_info(mtd, buf, 4096);
594 if (cmd == OTPGETREGIONCOUNT) {
595 int nbr = ret / sizeof(struct otp_info);
596 ret = copy_to_user(argp, &nbr, sizeof(int));
598 ret = copy_to_user(argp, buf, ret);
608 struct otp_info info;
610 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
612 if (copy_from_user(&info, argp, sizeof(info)))
614 if (!mtd->lock_user_prot_reg)
616 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
628 static struct file_operations mtd_fops = {
629 .owner = THIS_MODULE,
635 .release = mtd_close,
638 static int __init init_mtdchar(void)
640 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
641 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
646 mtd_class = class_create(THIS_MODULE, "mtd");
648 if (IS_ERR(mtd_class)) {
649 printk(KERN_ERR "Error creating mtd class.\n");
650 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
651 return PTR_ERR(mtd_class);
654 register_mtd_user(¬ifier);
658 static void __exit cleanup_mtdchar(void)
660 unregister_mtd_user(¬ifier);
661 class_destroy(mtd_class);
662 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
665 module_init(init_mtdchar);
666 module_exit(cleanup_mtdchar);
669 MODULE_LICENSE("GPL");
670 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
671 MODULE_DESCRIPTION("Direct character-device access to MTD devices");