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