NOMMU: Present backing device capabilities for MTD chardevs
[safe/jmp/linux-2.6] / drivers / mtd / mtdchar.c
1 /*
2  * Character-device access to raw MTD devices.
3  *
4  */
5
6 #include <linux/device.h>
7 #include <linux/fs.h>
8 #include <linux/mm.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/backing-dev.h>
17
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/compatmac.h>
20
21 #include <asm/uaccess.h>
22
23 static struct class *mtd_class;
24
25 static void mtd_notify_add(struct mtd_info* mtd)
26 {
27         if (!mtd)
28                 return;
29
30         device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
31                       NULL, "mtd%d", mtd->index);
32
33         device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
34                       NULL, "mtd%dro", mtd->index);
35 }
36
37 static void mtd_notify_remove(struct mtd_info* mtd)
38 {
39         if (!mtd)
40                 return;
41
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));
44 }
45
46 static struct mtd_notifier notifier = {
47         .add    = mtd_notify_add,
48         .remove = mtd_notify_remove,
49 };
50
51 /*
52  * Data structure to hold the pointer to the mtd device as well
53  * as mode information ofr various use cases.
54  */
55 struct mtd_file_info {
56         struct mtd_info *mtd;
57         enum mtd_file_modes mode;
58 };
59
60 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
61 {
62         struct mtd_file_info *mfi = file->private_data;
63         struct mtd_info *mtd = mfi->mtd;
64
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;
76         }
77
78         if (offset >= 0 && offset <= mtd->size)
79                 return file->f_pos = offset;
80
81         return -EINVAL;
82 }
83
84
85
86 static int mtd_open(struct inode *inode, struct file *file)
87 {
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;
93
94         DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
95
96         if (devnum >= MAX_MTD_DEVICES)
97                 return -ENODEV;
98
99         /* You can't open the RO devices RW */
100         if ((file->f_mode & FMODE_WRITE) && (minor & 1))
101                 return -EACCES;
102
103         lock_kernel();
104         mtd = get_mtd_device(NULL, devnum);
105
106         if (IS_ERR(mtd)) {
107                 ret = PTR_ERR(mtd);
108                 goto out;
109         }
110
111         if (mtd->type == MTD_ABSENT) {
112                 put_mtd_device(mtd);
113                 ret = -ENODEV;
114                 goto out;
115         }
116
117         if (mtd->backing_dev_info)
118                 file->f_mapping->backing_dev_info = mtd->backing_dev_info;
119
120         /* You can't open it RW if it's not a writeable device */
121         if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
122                 put_mtd_device(mtd);
123                 ret = -EACCES;
124                 goto out;
125         }
126
127         mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
128         if (!mfi) {
129                 put_mtd_device(mtd);
130                 ret = -ENOMEM;
131                 goto out;
132         }
133         mfi->mtd = mtd;
134         file->private_data = mfi;
135
136 out:
137         unlock_kernel();
138         return ret;
139 } /* mtd_open */
140
141 /*====================================================================*/
142
143 static int mtd_close(struct inode *inode, struct file *file)
144 {
145         struct mtd_file_info *mfi = file->private_data;
146         struct mtd_info *mtd = mfi->mtd;
147
148         DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
149
150         /* Only sync if opened RW */
151         if ((file->f_mode & FMODE_WRITE) && mtd->sync)
152                 mtd->sync(mtd);
153
154         put_mtd_device(mtd);
155         file->private_data = NULL;
156         kfree(mfi);
157
158         return 0;
159 } /* mtd_close */
160
161 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
162    userspace buffer down and use it directly with readv/writev.
163 */
164 #define MAX_KMALLOC_SIZE 0x20000
165
166 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
167 {
168         struct mtd_file_info *mfi = file->private_data;
169         struct mtd_info *mtd = mfi->mtd;
170         size_t retlen=0;
171         size_t total_retlen=0;
172         int ret=0;
173         int len;
174         char *kbuf;
175
176         DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
177
178         if (*ppos + count > mtd->size)
179                 count = mtd->size - *ppos;
180
181         if (!count)
182                 return 0;
183
184         /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
185            and pass them directly to the MTD functions */
186
187         if (count > MAX_KMALLOC_SIZE)
188                 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
189         else
190                 kbuf=kmalloc(count, GFP_KERNEL);
191
192         if (!kbuf)
193                 return -ENOMEM;
194
195         while (count) {
196
197                 if (count > MAX_KMALLOC_SIZE)
198                         len = MAX_KMALLOC_SIZE;
199                 else
200                         len = count;
201
202                 switch (mfi->mode) {
203                 case MTD_MODE_OTP_FACTORY:
204                         ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
205                         break;
206                 case MTD_MODE_OTP_USER:
207                         ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
208                         break;
209                 case MTD_MODE_RAW:
210                 {
211                         struct mtd_oob_ops ops;
212
213                         ops.mode = MTD_OOB_RAW;
214                         ops.datbuf = kbuf;
215                         ops.oobbuf = NULL;
216                         ops.len = len;
217
218                         ret = mtd->read_oob(mtd, *ppos, &ops);
219                         retlen = ops.retlen;
220                         break;
221                 }
222                 default:
223                         ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
224                 }
225                 /* Nand returns -EBADMSG on ecc errors, but it returns
226                  * the data. For our userspace tools it is important
227                  * to dump areas with ecc errors !
228                  * For kernel internal usage it also might return -EUCLEAN
229                  * to signal the caller that a bitflip has occured and has
230                  * been corrected by the ECC algorithm.
231                  * Userspace software which accesses NAND this way
232                  * must be aware of the fact that it deals with NAND
233                  */
234                 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
235                         *ppos += retlen;
236                         if (copy_to_user(buf, kbuf, retlen)) {
237                                 kfree(kbuf);
238                                 return -EFAULT;
239                         }
240                         else
241                                 total_retlen += retlen;
242
243                         count -= retlen;
244                         buf += retlen;
245                         if (retlen == 0)
246                                 count = 0;
247                 }
248                 else {
249                         kfree(kbuf);
250                         return ret;
251                 }
252
253         }
254
255         kfree(kbuf);
256         return total_retlen;
257 } /* mtd_read */
258
259 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
260 {
261         struct mtd_file_info *mfi = file->private_data;
262         struct mtd_info *mtd = mfi->mtd;
263         char *kbuf;
264         size_t retlen;
265         size_t total_retlen=0;
266         int ret=0;
267         int len;
268
269         DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
270
271         if (*ppos == mtd->size)
272                 return -ENOSPC;
273
274         if (*ppos + count > mtd->size)
275                 count = mtd->size - *ppos;
276
277         if (!count)
278                 return 0;
279
280         if (count > MAX_KMALLOC_SIZE)
281                 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
282         else
283                 kbuf=kmalloc(count, GFP_KERNEL);
284
285         if (!kbuf)
286                 return -ENOMEM;
287
288         while (count) {
289
290                 if (count > MAX_KMALLOC_SIZE)
291                         len = MAX_KMALLOC_SIZE;
292                 else
293                         len = count;
294
295                 if (copy_from_user(kbuf, buf, len)) {
296                         kfree(kbuf);
297                         return -EFAULT;
298                 }
299
300                 switch (mfi->mode) {
301                 case MTD_MODE_OTP_FACTORY:
302                         ret = -EROFS;
303                         break;
304                 case MTD_MODE_OTP_USER:
305                         if (!mtd->write_user_prot_reg) {
306                                 ret = -EOPNOTSUPP;
307                                 break;
308                         }
309                         ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
310                         break;
311
312                 case MTD_MODE_RAW:
313                 {
314                         struct mtd_oob_ops ops;
315
316                         ops.mode = MTD_OOB_RAW;
317                         ops.datbuf = kbuf;
318                         ops.oobbuf = NULL;
319                         ops.len = len;
320
321                         ret = mtd->write_oob(mtd, *ppos, &ops);
322                         retlen = ops.retlen;
323                         break;
324                 }
325
326                 default:
327                         ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
328                 }
329                 if (!ret) {
330                         *ppos += retlen;
331                         total_retlen += retlen;
332                         count -= retlen;
333                         buf += retlen;
334                 }
335                 else {
336                         kfree(kbuf);
337                         return ret;
338                 }
339         }
340
341         kfree(kbuf);
342         return total_retlen;
343 } /* mtd_write */
344
345 /*======================================================================
346
347     IOCTL calls for getting device parameters.
348
349 ======================================================================*/
350 static void mtdchar_erase_callback (struct erase_info *instr)
351 {
352         wake_up((wait_queue_head_t *)instr->priv);
353 }
354
355 #ifdef CONFIG_HAVE_MTD_OTP
356 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
357 {
358         struct mtd_info *mtd = mfi->mtd;
359         int ret = 0;
360
361         switch (mode) {
362         case MTD_OTP_FACTORY:
363                 if (!mtd->read_fact_prot_reg)
364                         ret = -EOPNOTSUPP;
365                 else
366                         mfi->mode = MTD_MODE_OTP_FACTORY;
367                 break;
368         case MTD_OTP_USER:
369                 if (!mtd->read_fact_prot_reg)
370                         ret = -EOPNOTSUPP;
371                 else
372                         mfi->mode = MTD_MODE_OTP_USER;
373                 break;
374         default:
375                 ret = -EINVAL;
376         case MTD_OTP_OFF:
377                 break;
378         }
379         return ret;
380 }
381 #else
382 # define otp_select_filemode(f,m)       -EOPNOTSUPP
383 #endif
384
385 static int mtd_ioctl(struct inode *inode, struct file *file,
386                      u_int cmd, u_long arg)
387 {
388         struct mtd_file_info *mfi = file->private_data;
389         struct mtd_info *mtd = mfi->mtd;
390         void __user *argp = (void __user *)arg;
391         int ret = 0;
392         u_long size;
393         struct mtd_info_user info;
394
395         DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
396
397         size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
398         if (cmd & IOC_IN) {
399                 if (!access_ok(VERIFY_READ, argp, size))
400                         return -EFAULT;
401         }
402         if (cmd & IOC_OUT) {
403                 if (!access_ok(VERIFY_WRITE, argp, size))
404                         return -EFAULT;
405         }
406
407         switch (cmd) {
408         case MEMGETREGIONCOUNT:
409                 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
410                         return -EFAULT;
411                 break;
412
413         case MEMGETREGIONINFO:
414         {
415                 uint32_t ur_idx;
416                 struct mtd_erase_region_info *kr;
417                 struct region_info_user *ur = (struct region_info_user *) argp;
418
419                 if (get_user(ur_idx, &(ur->regionindex)))
420                         return -EFAULT;
421
422                 kr = &(mtd->eraseregions[ur_idx]);
423
424                 if (put_user(kr->offset, &(ur->offset))
425                     || put_user(kr->erasesize, &(ur->erasesize))
426                     || put_user(kr->numblocks, &(ur->numblocks)))
427                         return -EFAULT;
428
429                 break;
430         }
431
432         case MEMGETINFO:
433                 info.type       = mtd->type;
434                 info.flags      = mtd->flags;
435                 info.size       = mtd->size;
436                 info.erasesize  = mtd->erasesize;
437                 info.writesize  = mtd->writesize;
438                 info.oobsize    = mtd->oobsize;
439                 /* The below fields are obsolete */
440                 info.ecctype    = -1;
441                 info.eccsize    = 0;
442                 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
443                         return -EFAULT;
444                 break;
445
446         case MEMERASE:
447         {
448                 struct erase_info *erase;
449
450                 if(!(file->f_mode & FMODE_WRITE))
451                         return -EPERM;
452
453                 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
454                 if (!erase)
455                         ret = -ENOMEM;
456                 else {
457                         struct erase_info_user einfo;
458
459                         wait_queue_head_t waitq;
460                         DECLARE_WAITQUEUE(wait, current);
461
462                         init_waitqueue_head(&waitq);
463
464                         if (copy_from_user(&einfo, argp,
465                                     sizeof(struct erase_info_user))) {
466                                 kfree(erase);
467                                 return -EFAULT;
468                         }
469                         erase->addr = einfo.start;
470                         erase->len = einfo.length;
471                         erase->mtd = mtd;
472                         erase->callback = mtdchar_erase_callback;
473                         erase->priv = (unsigned long)&waitq;
474
475                         /*
476                           FIXME: Allow INTERRUPTIBLE. Which means
477                           not having the wait_queue head on the stack.
478
479                           If the wq_head is on the stack, and we
480                           leave because we got interrupted, then the
481                           wq_head is no longer there when the
482                           callback routine tries to wake us up.
483                         */
484                         ret = mtd->erase(mtd, erase);
485                         if (!ret) {
486                                 set_current_state(TASK_UNINTERRUPTIBLE);
487                                 add_wait_queue(&waitq, &wait);
488                                 if (erase->state != MTD_ERASE_DONE &&
489                                     erase->state != MTD_ERASE_FAILED)
490                                         schedule();
491                                 remove_wait_queue(&waitq, &wait);
492                                 set_current_state(TASK_RUNNING);
493
494                                 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
495                         }
496                         kfree(erase);
497                 }
498                 break;
499         }
500
501         case MEMWRITEOOB:
502         {
503                 struct mtd_oob_buf buf;
504                 struct mtd_oob_ops ops;
505                 struct mtd_oob_buf __user *user_buf = argp;
506                 uint32_t retlen;
507
508                 if(!(file->f_mode & FMODE_WRITE))
509                         return -EPERM;
510
511                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
512                         return -EFAULT;
513
514                 if (buf.length > 4096)
515                         return -EINVAL;
516
517                 if (!mtd->write_oob)
518                         ret = -EOPNOTSUPP;
519                 else
520                         ret = access_ok(VERIFY_READ, buf.ptr,
521                                         buf.length) ? 0 : EFAULT;
522
523                 if (ret)
524                         return ret;
525
526                 ops.ooblen = buf.length;
527                 ops.ooboffs = buf.start & (mtd->oobsize - 1);
528                 ops.datbuf = NULL;
529                 ops.mode = MTD_OOB_PLACE;
530
531                 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
532                         return -EINVAL;
533
534                 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
535                 if (!ops.oobbuf)
536                         return -ENOMEM;
537
538                 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
539                         kfree(ops.oobbuf);
540                         return -EFAULT;
541                 }
542
543                 buf.start &= ~(mtd->oobsize - 1);
544                 ret = mtd->write_oob(mtd, buf.start, &ops);
545
546                 if (ops.oobretlen > 0xFFFFFFFFU)
547                         ret = -EOVERFLOW;
548                 retlen = ops.oobretlen;
549                 if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
550                         ret = -EFAULT;
551
552                 kfree(ops.oobbuf);
553                 break;
554
555         }
556
557         case MEMREADOOB:
558         {
559                 struct mtd_oob_buf buf;
560                 struct mtd_oob_ops ops;
561
562                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
563                         return -EFAULT;
564
565                 if (buf.length > 4096)
566                         return -EINVAL;
567
568                 if (!mtd->read_oob)
569                         ret = -EOPNOTSUPP;
570                 else
571                         ret = access_ok(VERIFY_WRITE, buf.ptr,
572                                         buf.length) ? 0 : -EFAULT;
573                 if (ret)
574                         return ret;
575
576                 ops.ooblen = buf.length;
577                 ops.ooboffs = buf.start & (mtd->oobsize - 1);
578                 ops.datbuf = NULL;
579                 ops.mode = MTD_OOB_PLACE;
580
581                 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
582                         return -EINVAL;
583
584                 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
585                 if (!ops.oobbuf)
586                         return -ENOMEM;
587
588                 buf.start &= ~(mtd->oobsize - 1);
589                 ret = mtd->read_oob(mtd, buf.start, &ops);
590
591                 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
592                         ret = -EFAULT;
593                 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
594                                                     ops.oobretlen))
595                         ret = -EFAULT;
596
597                 kfree(ops.oobbuf);
598                 break;
599         }
600
601         case MEMLOCK:
602         {
603                 struct erase_info_user einfo;
604
605                 if (copy_from_user(&einfo, argp, sizeof(einfo)))
606                         return -EFAULT;
607
608                 if (!mtd->lock)
609                         ret = -EOPNOTSUPP;
610                 else
611                         ret = mtd->lock(mtd, einfo.start, einfo.length);
612                 break;
613         }
614
615         case MEMUNLOCK:
616         {
617                 struct erase_info_user einfo;
618
619                 if (copy_from_user(&einfo, argp, sizeof(einfo)))
620                         return -EFAULT;
621
622                 if (!mtd->unlock)
623                         ret = -EOPNOTSUPP;
624                 else
625                         ret = mtd->unlock(mtd, einfo.start, einfo.length);
626                 break;
627         }
628
629         /* Legacy interface */
630         case MEMGETOOBSEL:
631         {
632                 struct nand_oobinfo oi;
633
634                 if (!mtd->ecclayout)
635                         return -EOPNOTSUPP;
636                 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
637                         return -EINVAL;
638
639                 oi.useecc = MTD_NANDECC_AUTOPLACE;
640                 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
641                 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
642                        sizeof(oi.oobfree));
643                 oi.eccbytes = mtd->ecclayout->eccbytes;
644
645                 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
646                         return -EFAULT;
647                 break;
648         }
649
650         case MEMGETBADBLOCK:
651         {
652                 loff_t offs;
653
654                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
655                         return -EFAULT;
656                 if (!mtd->block_isbad)
657                         ret = -EOPNOTSUPP;
658                 else
659                         return mtd->block_isbad(mtd, offs);
660                 break;
661         }
662
663         case MEMSETBADBLOCK:
664         {
665                 loff_t offs;
666
667                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
668                         return -EFAULT;
669                 if (!mtd->block_markbad)
670                         ret = -EOPNOTSUPP;
671                 else
672                         return mtd->block_markbad(mtd, offs);
673                 break;
674         }
675
676 #ifdef CONFIG_HAVE_MTD_OTP
677         case OTPSELECT:
678         {
679                 int mode;
680                 if (copy_from_user(&mode, argp, sizeof(int)))
681                         return -EFAULT;
682
683                 mfi->mode = MTD_MODE_NORMAL;
684
685                 ret = otp_select_filemode(mfi, mode);
686
687                 file->f_pos = 0;
688                 break;
689         }
690
691         case OTPGETREGIONCOUNT:
692         case OTPGETREGIONINFO:
693         {
694                 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
695                 if (!buf)
696                         return -ENOMEM;
697                 ret = -EOPNOTSUPP;
698                 switch (mfi->mode) {
699                 case MTD_MODE_OTP_FACTORY:
700                         if (mtd->get_fact_prot_info)
701                                 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
702                         break;
703                 case MTD_MODE_OTP_USER:
704                         if (mtd->get_user_prot_info)
705                                 ret = mtd->get_user_prot_info(mtd, buf, 4096);
706                         break;
707                 default:
708                         break;
709                 }
710                 if (ret >= 0) {
711                         if (cmd == OTPGETREGIONCOUNT) {
712                                 int nbr = ret / sizeof(struct otp_info);
713                                 ret = copy_to_user(argp, &nbr, sizeof(int));
714                         } else
715                                 ret = copy_to_user(argp, buf, ret);
716                         if (ret)
717                                 ret = -EFAULT;
718                 }
719                 kfree(buf);
720                 break;
721         }
722
723         case OTPLOCK:
724         {
725                 struct otp_info oinfo;
726
727                 if (mfi->mode != MTD_MODE_OTP_USER)
728                         return -EINVAL;
729                 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
730                         return -EFAULT;
731                 if (!mtd->lock_user_prot_reg)
732                         return -EOPNOTSUPP;
733                 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
734                 break;
735         }
736 #endif
737
738         case ECCGETLAYOUT:
739         {
740                 if (!mtd->ecclayout)
741                         return -EOPNOTSUPP;
742
743                 if (copy_to_user(argp, mtd->ecclayout,
744                                  sizeof(struct nand_ecclayout)))
745                         return -EFAULT;
746                 break;
747         }
748
749         case ECCGETSTATS:
750         {
751                 if (copy_to_user(argp, &mtd->ecc_stats,
752                                  sizeof(struct mtd_ecc_stats)))
753                         return -EFAULT;
754                 break;
755         }
756
757         case MTDFILEMODE:
758         {
759                 mfi->mode = 0;
760
761                 switch(arg) {
762                 case MTD_MODE_OTP_FACTORY:
763                 case MTD_MODE_OTP_USER:
764                         ret = otp_select_filemode(mfi, arg);
765                         break;
766
767                 case MTD_MODE_RAW:
768                         if (!mtd->read_oob || !mtd->write_oob)
769                                 return -EOPNOTSUPP;
770                         mfi->mode = arg;
771
772                 case MTD_MODE_NORMAL:
773                         break;
774                 default:
775                         ret = -EINVAL;
776                 }
777                 file->f_pos = 0;
778                 break;
779         }
780
781         default:
782                 ret = -ENOTTY;
783         }
784
785         return ret;
786 } /* memory_ioctl */
787
788 /*
789  * try to determine where a shared mapping can be made
790  * - only supported for NOMMU at the moment (MMU can't doesn't copy private
791  *   mappings)
792  */
793 #ifndef CONFIG_MMU
794 static unsigned long mtd_get_unmapped_area(struct file *file,
795                                            unsigned long addr,
796                                            unsigned long len,
797                                            unsigned long pgoff,
798                                            unsigned long flags)
799 {
800         struct mtd_file_info *mfi = file->private_data;
801         struct mtd_info *mtd = mfi->mtd;
802
803         if (mtd->get_unmapped_area) {
804                 unsigned long offset;
805
806                 if (addr != 0)
807                         return (unsigned long) -EINVAL;
808
809                 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
810                         return (unsigned long) -EINVAL;
811
812                 offset = pgoff << PAGE_SHIFT;
813                 if (offset > mtd->size - len)
814                         return (unsigned long) -EINVAL;
815
816                 return mtd->get_unmapped_area(mtd, len, offset, flags);
817         }
818
819         /* can't map directly */
820         return (unsigned long) -ENOSYS;
821 }
822 #endif
823
824 /*
825  * set up a mapping for shared memory segments
826  */
827 static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
828 {
829 #ifdef CONFIG_MMU
830         struct mtd_file_info *mfi = file->private_data;
831         struct mtd_info *mtd = mfi->mtd;
832
833         if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
834                 return 0;
835         return -ENOSYS;
836 #else
837         return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
838 #endif
839 }
840
841 static const struct file_operations mtd_fops = {
842         .owner          = THIS_MODULE,
843         .llseek         = mtd_lseek,
844         .read           = mtd_read,
845         .write          = mtd_write,
846         .ioctl          = mtd_ioctl,
847         .open           = mtd_open,
848         .release        = mtd_close,
849         .mmap           = mtd_mmap,
850 #ifndef CONFIG_MMU
851         .get_unmapped_area = mtd_get_unmapped_area,
852 #endif
853 };
854
855 static int __init init_mtdchar(void)
856 {
857         if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
858                 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
859                        MTD_CHAR_MAJOR);
860                 return -EAGAIN;
861         }
862
863         mtd_class = class_create(THIS_MODULE, "mtd");
864
865         if (IS_ERR(mtd_class)) {
866                 printk(KERN_ERR "Error creating mtd class.\n");
867                 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
868                 return PTR_ERR(mtd_class);
869         }
870
871         register_mtd_user(&notifier);
872         return 0;
873 }
874
875 static void __exit cleanup_mtdchar(void)
876 {
877         unregister_mtd_user(&notifier);
878         class_destroy(mtd_class);
879         unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
880 }
881
882 module_init(init_mtdchar);
883 module_exit(cleanup_mtdchar);
884
885
886 MODULE_LICENSE("GPL");
887 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
888 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
889 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);