Driver core: remove subsys_get()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / mtdchar.c
blobd091b2430b480254c4cb2743a28def0e6805486a
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>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/compatmac.h>
21 #include <asm/uaccess.h>
23 static struct class *mtd_class;
25 static void mtd_notify_add(struct mtd_info* mtd)
27 if (!mtd)
28 return;
30 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
31 NULL, "mtd%d", mtd->index);
33 class_device_create(mtd_class, NULL,
34 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
35 NULL, "mtd%dro", mtd->index);
38 static void mtd_notify_remove(struct mtd_info* mtd)
40 if (!mtd)
41 return;
43 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
44 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
47 static struct mtd_notifier notifier = {
48 .add = mtd_notify_add,
49 .remove = mtd_notify_remove,
53 * Data structure to hold the pointer to the mtd device as well
54 * as mode information ofr various use cases.
56 struct mtd_file_info {
57 struct mtd_info *mtd;
58 enum mtd_file_modes mode;
61 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
63 struct mtd_file_info *mfi = file->private_data;
64 struct mtd_info *mtd = mfi->mtd;
66 switch (orig) {
67 case SEEK_SET:
68 break;
69 case SEEK_CUR:
70 offset += file->f_pos;
71 break;
72 case SEEK_END:
73 offset += mtd->size;
74 break;
75 default:
76 return -EINVAL;
79 if (offset >= 0 && offset <= mtd->size)
80 return file->f_pos = offset;
82 return -EINVAL;
87 static int mtd_open(struct inode *inode, struct file *file)
89 int minor = iminor(inode);
90 int devnum = minor >> 1;
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 mtd = get_mtd_device(NULL, devnum);
105 if (IS_ERR(mtd))
106 return PTR_ERR(mtd);
108 if (MTD_ABSENT == mtd->type) {
109 put_mtd_device(mtd);
110 return -ENODEV;
113 /* You can't open it RW if it's not a writeable device */
114 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
115 put_mtd_device(mtd);
116 return -EACCES;
119 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
120 if (!mfi) {
121 put_mtd_device(mtd);
122 return -ENOMEM;
124 mfi->mtd = mtd;
125 file->private_data = mfi;
127 return 0;
128 } /* mtd_open */
130 /*====================================================================*/
132 static int mtd_close(struct inode *inode, struct file *file)
134 struct mtd_file_info *mfi = file->private_data;
135 struct mtd_info *mtd = mfi->mtd;
137 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
139 if (mtd->sync)
140 mtd->sync(mtd);
142 put_mtd_device(mtd);
143 file->private_data = NULL;
144 kfree(mfi);
146 return 0;
147 } /* mtd_close */
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_file_info *mfi = file->private_data;
157 struct mtd_info *mtd = mfi->mtd;
158 size_t retlen=0;
159 size_t total_retlen=0;
160 int ret=0;
161 int len;
162 char *kbuf;
164 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
166 if (*ppos + count > mtd->size)
167 count = mtd->size - *ppos;
169 if (!count)
170 return 0;
172 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
173 and pass them directly to the MTD functions */
175 if (count > MAX_KMALLOC_SIZE)
176 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
177 else
178 kbuf=kmalloc(count, GFP_KERNEL);
180 if (!kbuf)
181 return -ENOMEM;
183 while (count) {
185 if (count > MAX_KMALLOC_SIZE)
186 len = MAX_KMALLOC_SIZE;
187 else
188 len = count;
190 switch (mfi->mode) {
191 case MTD_MODE_OTP_FACTORY:
192 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
193 break;
194 case MTD_MODE_OTP_USER:
195 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
196 break;
197 case MTD_MODE_RAW:
199 struct mtd_oob_ops ops;
201 ops.mode = MTD_OOB_RAW;
202 ops.datbuf = kbuf;
203 ops.oobbuf = NULL;
204 ops.len = len;
206 ret = mtd->read_oob(mtd, *ppos, &ops);
207 retlen = ops.retlen;
208 break;
210 default:
211 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
213 /* Nand returns -EBADMSG on ecc errors, but it returns
214 * the data. For our userspace tools it is important
215 * to dump areas with ecc errors !
216 * For kernel internal usage it also might return -EUCLEAN
217 * to signal the caller that a bitflip has occured and has
218 * been corrected by the ECC algorithm.
219 * Userspace software which accesses NAND this way
220 * must be aware of the fact that it deals with NAND
222 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
223 *ppos += retlen;
224 if (copy_to_user(buf, kbuf, retlen)) {
225 kfree(kbuf);
226 return -EFAULT;
228 else
229 total_retlen += retlen;
231 count -= retlen;
232 buf += retlen;
233 if (retlen == 0)
234 count = 0;
236 else {
237 kfree(kbuf);
238 return ret;
243 kfree(kbuf);
244 return total_retlen;
245 } /* mtd_read */
247 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
249 struct mtd_file_info *mfi = file->private_data;
250 struct mtd_info *mtd = mfi->mtd;
251 char *kbuf;
252 size_t retlen;
253 size_t total_retlen=0;
254 int ret=0;
255 int len;
257 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
259 if (*ppos == mtd->size)
260 return -ENOSPC;
262 if (*ppos + count > mtd->size)
263 count = mtd->size - *ppos;
265 if (!count)
266 return 0;
268 if (count > MAX_KMALLOC_SIZE)
269 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
270 else
271 kbuf=kmalloc(count, GFP_KERNEL);
273 if (!kbuf)
274 return -ENOMEM;
276 while (count) {
278 if (count > MAX_KMALLOC_SIZE)
279 len = MAX_KMALLOC_SIZE;
280 else
281 len = count;
283 if (copy_from_user(kbuf, buf, len)) {
284 kfree(kbuf);
285 return -EFAULT;
288 switch (mfi->mode) {
289 case MTD_MODE_OTP_FACTORY:
290 ret = -EROFS;
291 break;
292 case MTD_MODE_OTP_USER:
293 if (!mtd->write_user_prot_reg) {
294 ret = -EOPNOTSUPP;
295 break;
297 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
298 break;
300 case MTD_MODE_RAW:
302 struct mtd_oob_ops ops;
304 ops.mode = MTD_OOB_RAW;
305 ops.datbuf = kbuf;
306 ops.oobbuf = NULL;
307 ops.len = len;
309 ret = mtd->write_oob(mtd, *ppos, &ops);
310 retlen = ops.retlen;
311 break;
314 default:
315 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
317 if (!ret) {
318 *ppos += retlen;
319 total_retlen += retlen;
320 count -= retlen;
321 buf += retlen;
323 else {
324 kfree(kbuf);
325 return ret;
329 kfree(kbuf);
330 return total_retlen;
331 } /* mtd_write */
333 /*======================================================================
335 IOCTL calls for getting device parameters.
337 ======================================================================*/
338 static void mtdchar_erase_callback (struct erase_info *instr)
340 wake_up((wait_queue_head_t *)instr->priv);
343 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
344 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
346 struct mtd_info *mtd = mfi->mtd;
347 int ret = 0;
349 switch (mode) {
350 case MTD_OTP_FACTORY:
351 if (!mtd->read_fact_prot_reg)
352 ret = -EOPNOTSUPP;
353 else
354 mfi->mode = MTD_MODE_OTP_FACTORY;
355 break;
356 case MTD_OTP_USER:
357 if (!mtd->read_fact_prot_reg)
358 ret = -EOPNOTSUPP;
359 else
360 mfi->mode = MTD_MODE_OTP_USER;
361 break;
362 default:
363 ret = -EINVAL;
364 case MTD_OTP_OFF:
365 break;
367 return ret;
369 #else
370 # define otp_select_filemode(f,m) -EOPNOTSUPP
371 #endif
373 static int mtd_ioctl(struct inode *inode, struct file *file,
374 u_int cmd, u_long arg)
376 struct mtd_file_info *mfi = file->private_data;
377 struct mtd_info *mtd = mfi->mtd;
378 void __user *argp = (void __user *)arg;
379 int ret = 0;
380 u_long size;
381 struct mtd_info_user info;
383 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
385 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
386 if (cmd & IOC_IN) {
387 if (!access_ok(VERIFY_READ, argp, size))
388 return -EFAULT;
390 if (cmd & IOC_OUT) {
391 if (!access_ok(VERIFY_WRITE, argp, size))
392 return -EFAULT;
395 switch (cmd) {
396 case MEMGETREGIONCOUNT:
397 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
398 return -EFAULT;
399 break;
401 case MEMGETREGIONINFO:
403 struct region_info_user ur;
405 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
406 return -EFAULT;
408 if (ur.regionindex >= mtd->numeraseregions)
409 return -EINVAL;
410 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
411 sizeof(struct mtd_erase_region_info)))
412 return -EFAULT;
413 break;
416 case MEMGETINFO:
417 info.type = mtd->type;
418 info.flags = mtd->flags;
419 info.size = mtd->size;
420 info.erasesize = mtd->erasesize;
421 info.writesize = mtd->writesize;
422 info.oobsize = mtd->oobsize;
423 /* The below fields are obsolete */
424 info.ecctype = -1;
425 info.eccsize = 0;
426 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
427 return -EFAULT;
428 break;
430 case MEMERASE:
432 struct erase_info *erase;
434 if(!(file->f_mode & 2))
435 return -EPERM;
437 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
438 if (!erase)
439 ret = -ENOMEM;
440 else {
441 wait_queue_head_t waitq;
442 DECLARE_WAITQUEUE(wait, current);
444 init_waitqueue_head(&waitq);
446 if (copy_from_user(&erase->addr, argp,
447 sizeof(struct erase_info_user))) {
448 kfree(erase);
449 return -EFAULT;
451 erase->mtd = mtd;
452 erase->callback = mtdchar_erase_callback;
453 erase->priv = (unsigned long)&waitq;
456 FIXME: Allow INTERRUPTIBLE. Which means
457 not having the wait_queue head on the stack.
459 If the wq_head is on the stack, and we
460 leave because we got interrupted, then the
461 wq_head is no longer there when the
462 callback routine tries to wake us up.
464 ret = mtd->erase(mtd, erase);
465 if (!ret) {
466 set_current_state(TASK_UNINTERRUPTIBLE);
467 add_wait_queue(&waitq, &wait);
468 if (erase->state != MTD_ERASE_DONE &&
469 erase->state != MTD_ERASE_FAILED)
470 schedule();
471 remove_wait_queue(&waitq, &wait);
472 set_current_state(TASK_RUNNING);
474 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
476 kfree(erase);
478 break;
481 case MEMWRITEOOB:
483 struct mtd_oob_buf buf;
484 struct mtd_oob_ops ops;
486 if(!(file->f_mode & 2))
487 return -EPERM;
489 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
490 return -EFAULT;
492 if (buf.length > 4096)
493 return -EINVAL;
495 if (!mtd->write_oob)
496 ret = -EOPNOTSUPP;
497 else
498 ret = access_ok(VERIFY_READ, buf.ptr,
499 buf.length) ? 0 : EFAULT;
501 if (ret)
502 return ret;
504 ops.ooblen = buf.length;
505 ops.ooboffs = buf.start & (mtd->oobsize - 1);
506 ops.datbuf = NULL;
507 ops.mode = MTD_OOB_PLACE;
509 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
510 return -EINVAL;
512 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
513 if (!ops.oobbuf)
514 return -ENOMEM;
516 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
517 kfree(ops.oobbuf);
518 return -EFAULT;
521 buf.start &= ~(mtd->oobsize - 1);
522 ret = mtd->write_oob(mtd, buf.start, &ops);
524 if (copy_to_user(argp + sizeof(uint32_t), &ops.oobretlen,
525 sizeof(uint32_t)))
526 ret = -EFAULT;
528 kfree(ops.oobbuf);
529 break;
533 case MEMREADOOB:
535 struct mtd_oob_buf buf;
536 struct mtd_oob_ops ops;
538 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
539 return -EFAULT;
541 if (buf.length > 4096)
542 return -EINVAL;
544 if (!mtd->read_oob)
545 ret = -EOPNOTSUPP;
546 else
547 ret = access_ok(VERIFY_WRITE, buf.ptr,
548 buf.length) ? 0 : -EFAULT;
549 if (ret)
550 return ret;
552 ops.ooblen = buf.length;
553 ops.ooboffs = buf.start & (mtd->oobsize - 1);
554 ops.datbuf = NULL;
555 ops.mode = MTD_OOB_PLACE;
557 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
558 return -EINVAL;
560 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
561 if (!ops.oobbuf)
562 return -ENOMEM;
564 buf.start &= ~(mtd->oobsize - 1);
565 ret = mtd->read_oob(mtd, buf.start, &ops);
567 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
568 ret = -EFAULT;
569 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
570 ops.oobretlen))
571 ret = -EFAULT;
573 kfree(ops.oobbuf);
574 break;
577 case MEMLOCK:
579 struct erase_info_user info;
581 if (copy_from_user(&info, argp, sizeof(info)))
582 return -EFAULT;
584 if (!mtd->lock)
585 ret = -EOPNOTSUPP;
586 else
587 ret = mtd->lock(mtd, info.start, info.length);
588 break;
591 case MEMUNLOCK:
593 struct erase_info_user info;
595 if (copy_from_user(&info, argp, sizeof(info)))
596 return -EFAULT;
598 if (!mtd->unlock)
599 ret = -EOPNOTSUPP;
600 else
601 ret = mtd->unlock(mtd, info.start, info.length);
602 break;
605 /* Legacy interface */
606 case MEMGETOOBSEL:
608 struct nand_oobinfo oi;
610 if (!mtd->ecclayout)
611 return -EOPNOTSUPP;
612 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
613 return -EINVAL;
615 oi.useecc = MTD_NANDECC_AUTOPLACE;
616 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
617 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
618 sizeof(oi.oobfree));
619 oi.eccbytes = mtd->ecclayout->eccbytes;
621 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
622 return -EFAULT;
623 break;
626 case MEMGETBADBLOCK:
628 loff_t offs;
630 if (copy_from_user(&offs, argp, sizeof(loff_t)))
631 return -EFAULT;
632 if (!mtd->block_isbad)
633 ret = -EOPNOTSUPP;
634 else
635 return mtd->block_isbad(mtd, offs);
636 break;
639 case MEMSETBADBLOCK:
641 loff_t offs;
643 if (copy_from_user(&offs, argp, sizeof(loff_t)))
644 return -EFAULT;
645 if (!mtd->block_markbad)
646 ret = -EOPNOTSUPP;
647 else
648 return mtd->block_markbad(mtd, offs);
649 break;
652 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
653 case OTPSELECT:
655 int mode;
656 if (copy_from_user(&mode, argp, sizeof(int)))
657 return -EFAULT;
659 mfi->mode = MTD_MODE_NORMAL;
661 ret = otp_select_filemode(mfi, mode);
663 file->f_pos = 0;
664 break;
667 case OTPGETREGIONCOUNT:
668 case OTPGETREGIONINFO:
670 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
671 if (!buf)
672 return -ENOMEM;
673 ret = -EOPNOTSUPP;
674 switch (mfi->mode) {
675 case MTD_MODE_OTP_FACTORY:
676 if (mtd->get_fact_prot_info)
677 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
678 break;
679 case MTD_MODE_OTP_USER:
680 if (mtd->get_user_prot_info)
681 ret = mtd->get_user_prot_info(mtd, buf, 4096);
682 break;
683 default:
684 break;
686 if (ret >= 0) {
687 if (cmd == OTPGETREGIONCOUNT) {
688 int nbr = ret / sizeof(struct otp_info);
689 ret = copy_to_user(argp, &nbr, sizeof(int));
690 } else
691 ret = copy_to_user(argp, buf, ret);
692 if (ret)
693 ret = -EFAULT;
695 kfree(buf);
696 break;
699 case OTPLOCK:
701 struct otp_info info;
703 if (mfi->mode != MTD_MODE_OTP_USER)
704 return -EINVAL;
705 if (copy_from_user(&info, argp, sizeof(info)))
706 return -EFAULT;
707 if (!mtd->lock_user_prot_reg)
708 return -EOPNOTSUPP;
709 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
710 break;
712 #endif
714 case ECCGETLAYOUT:
716 if (!mtd->ecclayout)
717 return -EOPNOTSUPP;
719 if (copy_to_user(argp, mtd->ecclayout,
720 sizeof(struct nand_ecclayout)))
721 return -EFAULT;
722 break;
725 case ECCGETSTATS:
727 if (copy_to_user(argp, &mtd->ecc_stats,
728 sizeof(struct mtd_ecc_stats)))
729 return -EFAULT;
730 break;
733 case MTDFILEMODE:
735 mfi->mode = 0;
737 switch(arg) {
738 case MTD_MODE_OTP_FACTORY:
739 case MTD_MODE_OTP_USER:
740 ret = otp_select_filemode(mfi, arg);
741 break;
743 case MTD_MODE_RAW:
744 if (!mtd->read_oob || !mtd->write_oob)
745 return -EOPNOTSUPP;
746 mfi->mode = arg;
748 case MTD_MODE_NORMAL:
749 break;
750 default:
751 ret = -EINVAL;
753 file->f_pos = 0;
754 break;
757 default:
758 ret = -ENOTTY;
761 return ret;
762 } /* memory_ioctl */
764 static const struct file_operations mtd_fops = {
765 .owner = THIS_MODULE,
766 .llseek = mtd_lseek,
767 .read = mtd_read,
768 .write = mtd_write,
769 .ioctl = mtd_ioctl,
770 .open = mtd_open,
771 .release = mtd_close,
774 static int __init init_mtdchar(void)
776 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
777 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
778 MTD_CHAR_MAJOR);
779 return -EAGAIN;
782 mtd_class = class_create(THIS_MODULE, "mtd");
784 if (IS_ERR(mtd_class)) {
785 printk(KERN_ERR "Error creating mtd class.\n");
786 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
787 return PTR_ERR(mtd_class);
790 register_mtd_user(&notifier);
791 return 0;
794 static void __exit cleanup_mtdchar(void)
796 unregister_mtd_user(&notifier);
797 class_destroy(mtd_class);
798 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
801 module_init(init_mtdchar);
802 module_exit(cleanup_mtdchar);
805 MODULE_LICENSE("GPL");
806 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
807 MODULE_DESCRIPTION("Direct character-device access to MTD devices");