ALSA: cs4232: fix crash during chip PNP detection
[linux-2.6/cjktty.git] / drivers / mtd / mtdchar.c
blob129d429cd2da5abc55ea1aa9c97a21041a5c85ff
1 /*
2 * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
4 * Character-device access to raw MTD devices.
6 */
8 #include <linux/device.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/smp_lock.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/compatmac.h>
22 #include <asm/uaccess.h>
24 static struct class *mtd_class;
26 static void mtd_notify_add(struct mtd_info* mtd)
28 if (!mtd)
29 return;
31 device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), "mtd%d", mtd->index);
33 device_create(mtd_class, NULL,
34 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), "mtd%dro", mtd->index);
37 static void mtd_notify_remove(struct mtd_info* mtd)
39 if (!mtd)
40 return;
42 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
43 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 * Data structure to hold the pointer to the mtd device as well
53 * as mode information ofr various use cases.
55 struct mtd_file_info {
56 struct mtd_info *mtd;
57 enum mtd_file_modes mode;
60 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
62 struct mtd_file_info *mfi = file->private_data;
63 struct mtd_info *mtd = mfi->mtd;
65 switch (orig) {
66 case SEEK_SET:
67 break;
68 case SEEK_CUR:
69 offset += file->f_pos;
70 break;
71 case SEEK_END:
72 offset += mtd->size;
73 break;
74 default:
75 return -EINVAL;
78 if (offset >= 0 && offset <= mtd->size)
79 return file->f_pos = offset;
81 return -EINVAL;
86 static int mtd_open(struct inode *inode, struct file *file)
88 int minor = iminor(inode);
89 int devnum = minor >> 1;
90 int ret = 0;
91 struct mtd_info *mtd;
92 struct mtd_file_info *mfi;
94 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
96 if (devnum >= MAX_MTD_DEVICES)
97 return -ENODEV;
99 /* You can't open the RO devices RW */
100 if ((file->f_mode & 2) && (minor & 1))
101 return -EACCES;
103 lock_kernel();
104 mtd = get_mtd_device(NULL, devnum);
106 if (IS_ERR(mtd)) {
107 ret = PTR_ERR(mtd);
108 goto out;
111 if (MTD_ABSENT == mtd->type) {
112 put_mtd_device(mtd);
113 ret = -ENODEV;
114 goto out;
117 /* You can't open it RW if it's not a writeable device */
118 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
119 put_mtd_device(mtd);
120 ret = -EACCES;
121 goto out;
124 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
125 if (!mfi) {
126 put_mtd_device(mtd);
127 ret = -ENOMEM;
128 goto out;
130 mfi->mtd = mtd;
131 file->private_data = mfi;
133 out:
134 unlock_kernel();
135 return ret;
136 } /* mtd_open */
138 /*====================================================================*/
140 static int mtd_close(struct inode *inode, struct file *file)
142 struct mtd_file_info *mfi = file->private_data;
143 struct mtd_info *mtd = mfi->mtd;
145 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
147 /* Only sync if opened RW */
148 if ((file->f_mode & 2) && mtd->sync)
149 mtd->sync(mtd);
151 put_mtd_device(mtd);
152 file->private_data = NULL;
153 kfree(mfi);
155 return 0;
156 } /* mtd_close */
158 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
159 userspace buffer down and use it directly with readv/writev.
161 #define MAX_KMALLOC_SIZE 0x20000
163 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
165 struct mtd_file_info *mfi = file->private_data;
166 struct mtd_info *mtd = mfi->mtd;
167 size_t retlen=0;
168 size_t total_retlen=0;
169 int ret=0;
170 int len;
171 char *kbuf;
173 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
175 if (*ppos + count > mtd->size)
176 count = mtd->size - *ppos;
178 if (!count)
179 return 0;
181 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
182 and pass them directly to the MTD functions */
184 if (count > MAX_KMALLOC_SIZE)
185 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
186 else
187 kbuf=kmalloc(count, GFP_KERNEL);
189 if (!kbuf)
190 return -ENOMEM;
192 while (count) {
194 if (count > MAX_KMALLOC_SIZE)
195 len = MAX_KMALLOC_SIZE;
196 else
197 len = count;
199 switch (mfi->mode) {
200 case MTD_MODE_OTP_FACTORY:
201 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
202 break;
203 case MTD_MODE_OTP_USER:
204 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
205 break;
206 case MTD_MODE_RAW:
208 struct mtd_oob_ops ops;
210 ops.mode = MTD_OOB_RAW;
211 ops.datbuf = kbuf;
212 ops.oobbuf = NULL;
213 ops.len = len;
215 ret = mtd->read_oob(mtd, *ppos, &ops);
216 retlen = ops.retlen;
217 break;
219 default:
220 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
222 /* Nand returns -EBADMSG on ecc errors, but it returns
223 * the data. For our userspace tools it is important
224 * to dump areas with ecc errors !
225 * For kernel internal usage it also might return -EUCLEAN
226 * to signal the caller that a bitflip has occured and has
227 * been corrected by the ECC algorithm.
228 * Userspace software which accesses NAND this way
229 * must be aware of the fact that it deals with NAND
231 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
232 *ppos += retlen;
233 if (copy_to_user(buf, kbuf, retlen)) {
234 kfree(kbuf);
235 return -EFAULT;
237 else
238 total_retlen += retlen;
240 count -= retlen;
241 buf += retlen;
242 if (retlen == 0)
243 count = 0;
245 else {
246 kfree(kbuf);
247 return ret;
252 kfree(kbuf);
253 return total_retlen;
254 } /* mtd_read */
256 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
258 struct mtd_file_info *mfi = file->private_data;
259 struct mtd_info *mtd = mfi->mtd;
260 char *kbuf;
261 size_t retlen;
262 size_t total_retlen=0;
263 int ret=0;
264 int len;
266 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
268 if (*ppos == mtd->size)
269 return -ENOSPC;
271 if (*ppos + count > mtd->size)
272 count = mtd->size - *ppos;
274 if (!count)
275 return 0;
277 if (count > MAX_KMALLOC_SIZE)
278 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
279 else
280 kbuf=kmalloc(count, GFP_KERNEL);
282 if (!kbuf)
283 return -ENOMEM;
285 while (count) {
287 if (count > MAX_KMALLOC_SIZE)
288 len = MAX_KMALLOC_SIZE;
289 else
290 len = count;
292 if (copy_from_user(kbuf, buf, len)) {
293 kfree(kbuf);
294 return -EFAULT;
297 switch (mfi->mode) {
298 case MTD_MODE_OTP_FACTORY:
299 ret = -EROFS;
300 break;
301 case MTD_MODE_OTP_USER:
302 if (!mtd->write_user_prot_reg) {
303 ret = -EOPNOTSUPP;
304 break;
306 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
307 break;
309 case MTD_MODE_RAW:
311 struct mtd_oob_ops ops;
313 ops.mode = MTD_OOB_RAW;
314 ops.datbuf = kbuf;
315 ops.oobbuf = NULL;
316 ops.len = len;
318 ret = mtd->write_oob(mtd, *ppos, &ops);
319 retlen = ops.retlen;
320 break;
323 default:
324 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
326 if (!ret) {
327 *ppos += retlen;
328 total_retlen += retlen;
329 count -= retlen;
330 buf += retlen;
332 else {
333 kfree(kbuf);
334 return ret;
338 kfree(kbuf);
339 return total_retlen;
340 } /* mtd_write */
342 /*======================================================================
344 IOCTL calls for getting device parameters.
346 ======================================================================*/
347 static void mtdchar_erase_callback (struct erase_info *instr)
349 wake_up((wait_queue_head_t *)instr->priv);
352 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
353 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
355 struct mtd_info *mtd = mfi->mtd;
356 int ret = 0;
358 switch (mode) {
359 case MTD_OTP_FACTORY:
360 if (!mtd->read_fact_prot_reg)
361 ret = -EOPNOTSUPP;
362 else
363 mfi->mode = MTD_MODE_OTP_FACTORY;
364 break;
365 case MTD_OTP_USER:
366 if (!mtd->read_fact_prot_reg)
367 ret = -EOPNOTSUPP;
368 else
369 mfi->mode = MTD_MODE_OTP_USER;
370 break;
371 default:
372 ret = -EINVAL;
373 case MTD_OTP_OFF:
374 break;
376 return ret;
378 #else
379 # define otp_select_filemode(f,m) -EOPNOTSUPP
380 #endif
382 static int mtd_ioctl(struct inode *inode, struct file *file,
383 u_int cmd, u_long arg)
385 struct mtd_file_info *mfi = file->private_data;
386 struct mtd_info *mtd = mfi->mtd;
387 void __user *argp = (void __user *)arg;
388 int ret = 0;
389 u_long size;
390 struct mtd_info_user info;
392 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
394 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
395 if (cmd & IOC_IN) {
396 if (!access_ok(VERIFY_READ, argp, size))
397 return -EFAULT;
399 if (cmd & IOC_OUT) {
400 if (!access_ok(VERIFY_WRITE, argp, size))
401 return -EFAULT;
404 switch (cmd) {
405 case MEMGETREGIONCOUNT:
406 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
407 return -EFAULT;
408 break;
410 case MEMGETREGIONINFO:
412 struct region_info_user ur;
414 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
415 return -EFAULT;
417 if (ur.regionindex >= mtd->numeraseregions)
418 return -EINVAL;
419 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
420 sizeof(struct mtd_erase_region_info)))
421 return -EFAULT;
422 break;
425 case MEMGETINFO:
426 info.type = mtd->type;
427 info.flags = mtd->flags;
428 info.size = mtd->size;
429 info.erasesize = mtd->erasesize;
430 info.writesize = mtd->writesize;
431 info.oobsize = mtd->oobsize;
432 /* The below fields are obsolete */
433 info.ecctype = -1;
434 info.eccsize = 0;
435 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
436 return -EFAULT;
437 break;
439 case MEMERASE:
441 struct erase_info *erase;
443 if(!(file->f_mode & 2))
444 return -EPERM;
446 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
447 if (!erase)
448 ret = -ENOMEM;
449 else {
450 wait_queue_head_t waitq;
451 DECLARE_WAITQUEUE(wait, current);
453 init_waitqueue_head(&waitq);
455 if (copy_from_user(&erase->addr, argp,
456 sizeof(struct erase_info_user))) {
457 kfree(erase);
458 return -EFAULT;
460 erase->mtd = mtd;
461 erase->callback = mtdchar_erase_callback;
462 erase->priv = (unsigned long)&waitq;
465 FIXME: Allow INTERRUPTIBLE. Which means
466 not having the wait_queue head on the stack.
468 If the wq_head is on the stack, and we
469 leave because we got interrupted, then the
470 wq_head is no longer there when the
471 callback routine tries to wake us up.
473 ret = mtd->erase(mtd, erase);
474 if (!ret) {
475 set_current_state(TASK_UNINTERRUPTIBLE);
476 add_wait_queue(&waitq, &wait);
477 if (erase->state != MTD_ERASE_DONE &&
478 erase->state != MTD_ERASE_FAILED)
479 schedule();
480 remove_wait_queue(&waitq, &wait);
481 set_current_state(TASK_RUNNING);
483 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
485 kfree(erase);
487 break;
490 case MEMWRITEOOB:
492 struct mtd_oob_buf buf;
493 struct mtd_oob_ops ops;
494 uint32_t retlen;
496 if(!(file->f_mode & 2))
497 return -EPERM;
499 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
500 return -EFAULT;
502 if (buf.length > 4096)
503 return -EINVAL;
505 if (!mtd->write_oob)
506 ret = -EOPNOTSUPP;
507 else
508 ret = access_ok(VERIFY_READ, buf.ptr,
509 buf.length) ? 0 : EFAULT;
511 if (ret)
512 return ret;
514 ops.ooblen = buf.length;
515 ops.ooboffs = buf.start & (mtd->oobsize - 1);
516 ops.datbuf = NULL;
517 ops.mode = MTD_OOB_PLACE;
519 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
520 return -EINVAL;
522 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
523 if (!ops.oobbuf)
524 return -ENOMEM;
526 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
527 kfree(ops.oobbuf);
528 return -EFAULT;
531 buf.start &= ~(mtd->oobsize - 1);
532 ret = mtd->write_oob(mtd, buf.start, &ops);
534 if (ops.oobretlen > 0xFFFFFFFFU)
535 ret = -EOVERFLOW;
536 retlen = ops.oobretlen;
537 if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
538 &retlen, sizeof(buf.length)))
539 ret = -EFAULT;
541 kfree(ops.oobbuf);
542 break;
546 case MEMREADOOB:
548 struct mtd_oob_buf buf;
549 struct mtd_oob_ops ops;
551 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
552 return -EFAULT;
554 if (buf.length > 4096)
555 return -EINVAL;
557 if (!mtd->read_oob)
558 ret = -EOPNOTSUPP;
559 else
560 ret = access_ok(VERIFY_WRITE, buf.ptr,
561 buf.length) ? 0 : -EFAULT;
562 if (ret)
563 return ret;
565 ops.ooblen = buf.length;
566 ops.ooboffs = buf.start & (mtd->oobsize - 1);
567 ops.datbuf = NULL;
568 ops.mode = MTD_OOB_PLACE;
570 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
571 return -EINVAL;
573 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
574 if (!ops.oobbuf)
575 return -ENOMEM;
577 buf.start &= ~(mtd->oobsize - 1);
578 ret = mtd->read_oob(mtd, buf.start, &ops);
580 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
581 ret = -EFAULT;
582 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
583 ops.oobretlen))
584 ret = -EFAULT;
586 kfree(ops.oobbuf);
587 break;
590 case MEMLOCK:
592 struct erase_info_user info;
594 if (copy_from_user(&info, argp, sizeof(info)))
595 return -EFAULT;
597 if (!mtd->lock)
598 ret = -EOPNOTSUPP;
599 else
600 ret = mtd->lock(mtd, info.start, info.length);
601 break;
604 case MEMUNLOCK:
606 struct erase_info_user info;
608 if (copy_from_user(&info, argp, sizeof(info)))
609 return -EFAULT;
611 if (!mtd->unlock)
612 ret = -EOPNOTSUPP;
613 else
614 ret = mtd->unlock(mtd, info.start, info.length);
615 break;
618 /* Legacy interface */
619 case MEMGETOOBSEL:
621 struct nand_oobinfo oi;
623 if (!mtd->ecclayout)
624 return -EOPNOTSUPP;
625 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
626 return -EINVAL;
628 oi.useecc = MTD_NANDECC_AUTOPLACE;
629 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
630 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
631 sizeof(oi.oobfree));
632 oi.eccbytes = mtd->ecclayout->eccbytes;
634 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
635 return -EFAULT;
636 break;
639 case MEMGETBADBLOCK:
641 loff_t offs;
643 if (copy_from_user(&offs, argp, sizeof(loff_t)))
644 return -EFAULT;
645 if (!mtd->block_isbad)
646 ret = -EOPNOTSUPP;
647 else
648 return mtd->block_isbad(mtd, offs);
649 break;
652 case MEMSETBADBLOCK:
654 loff_t offs;
656 if (copy_from_user(&offs, argp, sizeof(loff_t)))
657 return -EFAULT;
658 if (!mtd->block_markbad)
659 ret = -EOPNOTSUPP;
660 else
661 return mtd->block_markbad(mtd, offs);
662 break;
665 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
666 case OTPSELECT:
668 int mode;
669 if (copy_from_user(&mode, argp, sizeof(int)))
670 return -EFAULT;
672 mfi->mode = MTD_MODE_NORMAL;
674 ret = otp_select_filemode(mfi, mode);
676 file->f_pos = 0;
677 break;
680 case OTPGETREGIONCOUNT:
681 case OTPGETREGIONINFO:
683 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
684 if (!buf)
685 return -ENOMEM;
686 ret = -EOPNOTSUPP;
687 switch (mfi->mode) {
688 case MTD_MODE_OTP_FACTORY:
689 if (mtd->get_fact_prot_info)
690 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
691 break;
692 case MTD_MODE_OTP_USER:
693 if (mtd->get_user_prot_info)
694 ret = mtd->get_user_prot_info(mtd, buf, 4096);
695 break;
696 default:
697 break;
699 if (ret >= 0) {
700 if (cmd == OTPGETREGIONCOUNT) {
701 int nbr = ret / sizeof(struct otp_info);
702 ret = copy_to_user(argp, &nbr, sizeof(int));
703 } else
704 ret = copy_to_user(argp, buf, ret);
705 if (ret)
706 ret = -EFAULT;
708 kfree(buf);
709 break;
712 case OTPLOCK:
714 struct otp_info info;
716 if (mfi->mode != MTD_MODE_OTP_USER)
717 return -EINVAL;
718 if (copy_from_user(&info, argp, sizeof(info)))
719 return -EFAULT;
720 if (!mtd->lock_user_prot_reg)
721 return -EOPNOTSUPP;
722 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
723 break;
725 #endif
727 case ECCGETLAYOUT:
729 if (!mtd->ecclayout)
730 return -EOPNOTSUPP;
732 if (copy_to_user(argp, mtd->ecclayout,
733 sizeof(struct nand_ecclayout)))
734 return -EFAULT;
735 break;
738 case ECCGETSTATS:
740 if (copy_to_user(argp, &mtd->ecc_stats,
741 sizeof(struct mtd_ecc_stats)))
742 return -EFAULT;
743 break;
746 case MTDFILEMODE:
748 mfi->mode = 0;
750 switch(arg) {
751 case MTD_MODE_OTP_FACTORY:
752 case MTD_MODE_OTP_USER:
753 ret = otp_select_filemode(mfi, arg);
754 break;
756 case MTD_MODE_RAW:
757 if (!mtd->read_oob || !mtd->write_oob)
758 return -EOPNOTSUPP;
759 mfi->mode = arg;
761 case MTD_MODE_NORMAL:
762 break;
763 default:
764 ret = -EINVAL;
766 file->f_pos = 0;
767 break;
770 default:
771 ret = -ENOTTY;
774 return ret;
775 } /* memory_ioctl */
777 static const struct file_operations mtd_fops = {
778 .owner = THIS_MODULE,
779 .llseek = mtd_lseek,
780 .read = mtd_read,
781 .write = mtd_write,
782 .ioctl = mtd_ioctl,
783 .open = mtd_open,
784 .release = mtd_close,
787 static int __init init_mtdchar(void)
789 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
790 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
791 MTD_CHAR_MAJOR);
792 return -EAGAIN;
795 mtd_class = class_create(THIS_MODULE, "mtd");
797 if (IS_ERR(mtd_class)) {
798 printk(KERN_ERR "Error creating mtd class.\n");
799 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
800 return PTR_ERR(mtd_class);
803 register_mtd_user(&notifier);
804 return 0;
807 static void __exit cleanup_mtdchar(void)
809 unregister_mtd_user(&notifier);
810 class_destroy(mtd_class);
811 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
814 module_init(init_mtdchar);
815 module_exit(cleanup_mtdchar);
818 MODULE_LICENSE("GPL");
819 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
820 MODULE_DESCRIPTION("Direct character-device access to MTD devices");