2 * $Id: mtdchar.c,v 1.73 2005/07/04 17:36:41 gleixner 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 <linux/sched.h> /* TASK_* */
17 #include <asm/uaccess.h>
19 #include <linux/device.h>
21 static struct class *mtd_class;
23 static void mtd_notify_add(struct mtd_info* mtd)
28 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
29 NULL, "mtd%d", mtd->index);
31 class_device_create(mtd_class, NULL,
32 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
33 NULL, "mtd%dro", mtd->index);
36 static void mtd_notify_remove(struct mtd_info* mtd)
41 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
42 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
45 static struct mtd_notifier notifier = {
46 .add = mtd_notify_add,
47 .remove = mtd_notify_remove,
51 * We use file->private_data to store a pointer to the MTDdevice.
52 * Since alighment is at least 32 bits, we have 2 bits free for OTP
56 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
58 #define MTD_MODE_OTP_FACT 1
59 #define MTD_MODE_OTP_USER 2
60 #define MTD_MODE(file) ((long)((file)->private_data) & 3)
62 #define SET_MTD_MODE(file, mode) \
63 do { long __p = (long)((file)->private_data); \
64 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
66 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
68 struct mtd_info *mtd = TO_MTD(file);
77 file->f_pos += offset;
81 file->f_pos =mtd->size + offset;
89 else if (file->f_pos >= mtd->size)
90 file->f_pos = mtd->size - 1;
97 static int mtd_open(struct inode *inode, struct file *file)
99 int minor = iminor(inode);
100 int devnum = minor >> 1;
101 struct mtd_info *mtd;
103 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
105 if (devnum >= MAX_MTD_DEVICES)
108 /* You can't open the RO devices RW */
109 if ((file->f_mode & 2) && (minor & 1))
112 mtd = get_mtd_device(NULL, devnum);
117 if (MTD_ABSENT == mtd->type) {
122 file->private_data = mtd;
124 /* You can't open it RW if it's not a writeable device */
125 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
133 /*====================================================================*/
135 static int mtd_close(struct inode *inode, struct file *file)
137 struct mtd_info *mtd;
139 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
151 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
152 userspace buffer down and use it directly with readv/writev.
154 #define MAX_KMALLOC_SIZE 0x20000
156 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
158 struct mtd_info *mtd = TO_MTD(file);
160 size_t total_retlen=0;
165 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
167 if (*ppos + count > mtd->size)
168 count = mtd->size - *ppos;
173 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
174 and pass them directly to the MTD functions */
176 if (count > MAX_KMALLOC_SIZE)
177 len = MAX_KMALLOC_SIZE;
181 kbuf=kmalloc(len,GFP_KERNEL);
185 switch (MTD_MODE(file)) {
186 case MTD_MODE_OTP_FACT:
187 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
189 case MTD_MODE_OTP_USER:
190 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
193 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
195 /* Nand returns -EBADMSG on ecc errors, but it returns
196 * the data. For our userspace tools it is important
197 * to dump areas with ecc errors !
198 * Userspace software which accesses NAND this way
199 * must be aware of the fact that it deals with NAND
201 if (!ret || (ret == -EBADMSG)) {
203 if (copy_to_user(buf, kbuf, retlen)) {
208 total_retlen += retlen;
226 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
228 struct mtd_info *mtd = TO_MTD(file);
231 size_t total_retlen=0;
235 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
237 if (*ppos == mtd->size)
240 if (*ppos + count > mtd->size)
241 count = mtd->size - *ppos;
247 if (count > MAX_KMALLOC_SIZE)
248 len = MAX_KMALLOC_SIZE;
252 kbuf=kmalloc(len,GFP_KERNEL);
254 printk("kmalloc is null\n");
258 if (copy_from_user(kbuf, buf, len)) {
263 switch (MTD_MODE(file)) {
264 case MTD_MODE_OTP_FACT:
267 case MTD_MODE_OTP_USER:
268 if (!mtd->write_user_prot_reg) {
272 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
275 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
279 total_retlen += retlen;
294 /*======================================================================
296 IOCTL calls for getting device parameters.
298 ======================================================================*/
299 static void mtdchar_erase_callback (struct erase_info *instr)
301 wake_up((wait_queue_head_t *)instr->priv);
304 static int mtd_ioctl(struct inode *inode, struct file *file,
305 u_int cmd, u_long arg)
307 struct mtd_info *mtd = TO_MTD(file);
308 void __user *argp = (void __user *)arg;
312 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
314 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
316 if (!access_ok(VERIFY_READ, argp, size))
320 if (!access_ok(VERIFY_WRITE, argp, size))
325 case MEMGETREGIONCOUNT:
326 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
330 case MEMGETREGIONINFO:
332 struct region_info_user ur;
334 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
337 if (ur.regionindex >= mtd->numeraseregions)
339 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
340 sizeof(struct mtd_erase_region_info)))
346 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
352 struct erase_info *erase;
354 if(!(file->f_mode & 2))
357 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
361 wait_queue_head_t waitq;
362 DECLARE_WAITQUEUE(wait, current);
364 init_waitqueue_head(&waitq);
366 memset (erase,0,sizeof(struct erase_info));
367 if (copy_from_user(&erase->addr, argp,
368 sizeof(struct erase_info_user))) {
373 erase->callback = mtdchar_erase_callback;
374 erase->priv = (unsigned long)&waitq;
377 FIXME: Allow INTERRUPTIBLE. Which means
378 not having the wait_queue head on the stack.
380 If the wq_head is on the stack, and we
381 leave because we got interrupted, then the
382 wq_head is no longer there when the
383 callback routine tries to wake us up.
385 ret = mtd->erase(mtd, erase);
387 set_current_state(TASK_UNINTERRUPTIBLE);
388 add_wait_queue(&waitq, &wait);
389 if (erase->state != MTD_ERASE_DONE &&
390 erase->state != MTD_ERASE_FAILED)
392 remove_wait_queue(&waitq, &wait);
393 set_current_state(TASK_RUNNING);
395 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
404 struct mtd_oob_buf buf;
408 if(!(file->f_mode & 2))
411 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
414 if (buf.length > 0x4096)
420 ret = access_ok(VERIFY_READ, buf.ptr,
421 buf.length) ? 0 : EFAULT;
426 databuf = kmalloc(buf.length, GFP_KERNEL);
430 if (copy_from_user(databuf, buf.ptr, buf.length)) {
435 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
437 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
447 struct mtd_oob_buf buf;
451 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
454 if (buf.length > 0x4096)
460 ret = access_ok(VERIFY_WRITE, buf.ptr,
461 buf.length) ? 0 : -EFAULT;
466 databuf = kmalloc(buf.length, GFP_KERNEL);
470 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
472 if (put_user(retlen, (uint32_t __user *)argp))
474 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
483 struct erase_info_user info;
485 if (copy_from_user(&info, argp, sizeof(info)))
491 ret = mtd->lock(mtd, info.start, info.length);
497 struct erase_info_user info;
499 if (copy_from_user(&info, argp, sizeof(info)))
505 ret = mtd->unlock(mtd, info.start, info.length);
511 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
518 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
527 if (copy_from_user(&offs, argp, sizeof(loff_t)))
529 if (!mtd->block_isbad)
532 return mtd->block_isbad(mtd, offs);
540 if (copy_from_user(&offs, argp, sizeof(loff_t)))
542 if (!mtd->block_markbad)
545 return mtd->block_markbad(mtd, offs);
549 #ifdef CONFIG_MTD_OTP
553 if (copy_from_user(&mode, argp, sizeof(int)))
555 SET_MTD_MODE(file, 0);
557 case MTD_OTP_FACTORY:
558 if (!mtd->read_fact_prot_reg)
561 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
564 if (!mtd->read_fact_prot_reg)
567 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
578 case OTPGETREGIONCOUNT:
579 case OTPGETREGIONINFO:
581 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
585 switch (MTD_MODE(file)) {
586 case MTD_MODE_OTP_FACT:
587 if (mtd->get_fact_prot_info)
588 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
590 case MTD_MODE_OTP_USER:
591 if (mtd->get_user_prot_info)
592 ret = mtd->get_user_prot_info(mtd, buf, 4096);
596 if (cmd == OTPGETREGIONCOUNT) {
597 int nbr = ret / sizeof(struct otp_info);
598 ret = copy_to_user(argp, &nbr, sizeof(int));
600 ret = copy_to_user(argp, buf, ret);
610 struct otp_info info;
612 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
614 if (copy_from_user(&info, argp, sizeof(info)))
616 if (!mtd->lock_user_prot_reg)
618 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
630 static struct file_operations mtd_fops = {
631 .owner = THIS_MODULE,
637 .release = mtd_close,
640 static int __init init_mtdchar(void)
642 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
643 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
648 mtd_class = class_create(THIS_MODULE, "mtd");
650 if (IS_ERR(mtd_class)) {
651 printk(KERN_ERR "Error creating mtd class.\n");
652 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
653 return PTR_ERR(mtd_class);
656 register_mtd_user(¬ifier);
660 static void __exit cleanup_mtdchar(void)
662 unregister_mtd_user(¬ifier);
663 class_destroy(mtd_class);
664 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
667 module_init(init_mtdchar);
668 module_exit(cleanup_mtdchar);
671 MODULE_LICENSE("GPL");
672 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
673 MODULE_DESCRIPTION("Direct character-device access to MTD devices");