a48210d58b9286628743308316555827fafb5327
[safe/jmp/linux-2.6] / drivers / mtd / mtdchar.c
1 /*
2  * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
3  *
4  * Character-device access to raw MTD devices.
5  *
6  */
7
8 #include <linux/config.h>
9 #include <linux/device.h>
10 #include <linux/fs.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/compatmac.h>
19
20 #include <asm/uaccess.h>
21
22 static struct class *mtd_class;
23
24 static void mtd_notify_add(struct mtd_info* mtd)
25 {
26         if (!mtd)
27                 return;
28
29         class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
30                             NULL, "mtd%d", mtd->index);
31
32         class_device_create(mtd_class, NULL,
33                             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         class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
43         class_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  * We use file->private_data to store a pointer to the MTDdevice.
53  * Since alighment is at least 32 bits, we have 2 bits free for OTP
54  * modes as well.
55  */
56
57 #define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
58
59 #define MTD_MODE_OTP_FACT       1
60 #define MTD_MODE_OTP_USER       2
61 #define MTD_MODE(file)          ((long)((file)->private_data) & 3)
62
63 #define SET_MTD_MODE(file, mode) \
64         do { long __p = (long)((file)->private_data); \
65              (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
66
67 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
68 {
69         struct mtd_info *mtd = TO_MTD(file);
70
71         switch (orig) {
72         case 0:
73                 /* SEEK_SET */
74                 break;
75         case 1:
76                 /* SEEK_CUR */
77                 offset += file->f_pos;
78                 break;
79         case 2:
80                 /* SEEK_END */
81                 offset += mtd->size;
82                 break;
83         default:
84                 return -EINVAL;
85         }
86
87         if (offset >= 0 && offset < mtd->size)
88                 return file->f_pos = offset;
89
90         return -EINVAL;
91 }
92
93
94
95 static int mtd_open(struct inode *inode, struct file *file)
96 {
97         int minor = iminor(inode);
98         int devnum = minor >> 1;
99         struct mtd_info *mtd;
100
101         DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
102
103         if (devnum >= MAX_MTD_DEVICES)
104                 return -ENODEV;
105
106         /* You can't open the RO devices RW */
107         if ((file->f_mode & 2) && (minor & 1))
108                 return -EACCES;
109
110         mtd = get_mtd_device(NULL, devnum);
111
112         if (!mtd)
113                 return -ENODEV;
114
115         if (MTD_ABSENT == mtd->type) {
116                 put_mtd_device(mtd);
117                 return -ENODEV;
118         }
119
120         file->private_data = mtd;
121
122         /* You can't open it RW if it's not a writeable device */
123         if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
124                 put_mtd_device(mtd);
125                 return -EACCES;
126         }
127
128         return 0;
129 } /* mtd_open */
130
131 /*====================================================================*/
132
133 static int mtd_close(struct inode *inode, struct file *file)
134 {
135         struct mtd_info *mtd;
136
137         DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
138
139         mtd = TO_MTD(file);
140
141         if (mtd->sync)
142                 mtd->sync(mtd);
143
144         put_mtd_device(mtd);
145
146         return 0;
147 } /* mtd_close */
148
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.
151 */
152 #define MAX_KMALLOC_SIZE 0x20000
153
154 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
155 {
156         struct mtd_info *mtd = TO_MTD(file);
157         size_t retlen=0;
158         size_t total_retlen=0;
159         int ret=0;
160         int len;
161         char *kbuf;
162
163         DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
164
165         if (*ppos + count > mtd->size)
166                 count = mtd->size - *ppos;
167
168         if (!count)
169                 return 0;
170
171         /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
172            and pass them directly to the MTD functions */
173
174         if (count > MAX_KMALLOC_SIZE)
175                 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
176         else
177                 kbuf=kmalloc(count, GFP_KERNEL);
178
179         if (!kbuf)
180                 return -ENOMEM;
181
182         while (count) {
183
184                 if (count > MAX_KMALLOC_SIZE)
185                         len = MAX_KMALLOC_SIZE;
186                 else
187                         len = count;
188
189                 switch (MTD_MODE(file)) {
190                 case MTD_MODE_OTP_FACT:
191                         ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
192                         break;
193                 case MTD_MODE_OTP_USER:
194                         ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
195                         break;
196                 default:
197                         ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
198                 }
199                 /* Nand returns -EBADMSG on ecc errors, but it returns
200                  * the data. For our userspace tools it is important
201                  * to dump areas with ecc errors !
202                  * For kernel internal usage it also might return -EUCLEAN
203                  * to signal the caller that a bitflip has occured and has
204                  * been corrected by the ECC algorithm.
205                  * Userspace software which accesses NAND this way
206                  * must be aware of the fact that it deals with NAND
207                  */
208                 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
209                         *ppos += retlen;
210                         if (copy_to_user(buf, kbuf, retlen)) {
211                                 kfree(kbuf);
212                                 return -EFAULT;
213                         }
214                         else
215                                 total_retlen += retlen;
216
217                         count -= retlen;
218                         buf += retlen;
219                         if (retlen == 0)
220                                 count = 0;
221                 }
222                 else {
223                         kfree(kbuf);
224                         return ret;
225                 }
226
227         }
228
229         kfree(kbuf);
230         return total_retlen;
231 } /* mtd_read */
232
233 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
234 {
235         struct mtd_info *mtd = TO_MTD(file);
236         char *kbuf;
237         size_t retlen;
238         size_t total_retlen=0;
239         int ret=0;
240         int len;
241
242         DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
243
244         if (*ppos == mtd->size)
245                 return -ENOSPC;
246
247         if (*ppos + count > mtd->size)
248                 count = mtd->size - *ppos;
249
250         if (!count)
251                 return 0;
252
253         if (count > MAX_KMALLOC_SIZE)
254                 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
255         else
256                 kbuf=kmalloc(count, GFP_KERNEL);
257
258         if (!kbuf)
259                 return -ENOMEM;
260
261         while (count) {
262
263                 if (count > MAX_KMALLOC_SIZE)
264                         len = MAX_KMALLOC_SIZE;
265                 else
266                         len = count;
267
268                 if (copy_from_user(kbuf, buf, len)) {
269                         kfree(kbuf);
270                         return -EFAULT;
271                 }
272
273                 switch (MTD_MODE(file)) {
274                 case MTD_MODE_OTP_FACT:
275                         ret = -EROFS;
276                         break;
277                 case MTD_MODE_OTP_USER:
278                         if (!mtd->write_user_prot_reg) {
279                                 ret = -EOPNOTSUPP;
280                                 break;
281                         }
282                         ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
283                         break;
284                 default:
285                         ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
286                 }
287                 if (!ret) {
288                         *ppos += retlen;
289                         total_retlen += retlen;
290                         count -= retlen;
291                         buf += retlen;
292                 }
293                 else {
294                         kfree(kbuf);
295                         return ret;
296                 }
297         }
298
299         kfree(kbuf);
300         return total_retlen;
301 } /* mtd_write */
302
303 /*======================================================================
304
305     IOCTL calls for getting device parameters.
306
307 ======================================================================*/
308 static void mtdchar_erase_callback (struct erase_info *instr)
309 {
310         wake_up((wait_queue_head_t *)instr->priv);
311 }
312
313 static int mtd_ioctl(struct inode *inode, struct file *file,
314                      u_int cmd, u_long arg)
315 {
316         struct mtd_info *mtd = TO_MTD(file);
317         void __user *argp = (void __user *)arg;
318         int ret = 0;
319         u_long size;
320
321         DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
322
323         size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
324         if (cmd & IOC_IN) {
325                 if (!access_ok(VERIFY_READ, argp, size))
326                         return -EFAULT;
327         }
328         if (cmd & IOC_OUT) {
329                 if (!access_ok(VERIFY_WRITE, argp, size))
330                         return -EFAULT;
331         }
332
333         switch (cmd) {
334         case MEMGETREGIONCOUNT:
335                 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
336                         return -EFAULT;
337                 break;
338
339         case MEMGETREGIONINFO:
340         {
341                 struct region_info_user ur;
342
343                 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
344                         return -EFAULT;
345
346                 if (ur.regionindex >= mtd->numeraseregions)
347                         return -EINVAL;
348                 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
349                                 sizeof(struct mtd_erase_region_info)))
350                         return -EFAULT;
351                 break;
352         }
353
354         case MEMGETINFO:
355                 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
356                         return -EFAULT;
357                 break;
358
359         case MEMERASE:
360         {
361                 struct erase_info *erase;
362
363                 if(!(file->f_mode & 2))
364                         return -EPERM;
365
366                 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
367                 if (!erase)
368                         ret = -ENOMEM;
369                 else {
370                         wait_queue_head_t waitq;
371                         DECLARE_WAITQUEUE(wait, current);
372
373                         init_waitqueue_head(&waitq);
374
375                         memset (erase,0,sizeof(struct erase_info));
376                         if (copy_from_user(&erase->addr, argp,
377                                     sizeof(struct erase_info_user))) {
378                                 kfree(erase);
379                                 return -EFAULT;
380                         }
381                         erase->mtd = mtd;
382                         erase->callback = mtdchar_erase_callback;
383                         erase->priv = (unsigned long)&waitq;
384
385                         /*
386                           FIXME: Allow INTERRUPTIBLE. Which means
387                           not having the wait_queue head on the stack.
388
389                           If the wq_head is on the stack, and we
390                           leave because we got interrupted, then the
391                           wq_head is no longer there when the
392                           callback routine tries to wake us up.
393                         */
394                         ret = mtd->erase(mtd, erase);
395                         if (!ret) {
396                                 set_current_state(TASK_UNINTERRUPTIBLE);
397                                 add_wait_queue(&waitq, &wait);
398                                 if (erase->state != MTD_ERASE_DONE &&
399                                     erase->state != MTD_ERASE_FAILED)
400                                         schedule();
401                                 remove_wait_queue(&waitq, &wait);
402                                 set_current_state(TASK_RUNNING);
403
404                                 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
405                         }
406                         kfree(erase);
407                 }
408                 break;
409         }
410
411         case MEMWRITEOOB:
412         {
413                 struct mtd_oob_buf buf;
414                 struct mtd_oob_ops ops;
415
416                 if(!(file->f_mode & 2))
417                         return -EPERM;
418
419                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
420                         return -EFAULT;
421
422                 if (buf.length > 4096)
423                         return -EINVAL;
424
425                 if (!mtd->write_oob)
426                         ret = -EOPNOTSUPP;
427                 else
428                         ret = access_ok(VERIFY_READ, buf.ptr,
429                                         buf.length) ? 0 : EFAULT;
430
431                 if (ret)
432                         return ret;
433
434                 ops.len = buf.length;
435                 ops.ooblen = mtd->oobsize;
436                 ops.ooboffs = buf.start & (mtd->oobsize - 1);
437                 ops.datbuf = NULL;
438                 ops.mode = MTD_OOB_PLACE;
439
440                 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
441                         return -EINVAL;
442
443                 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
444                 if (!ops.oobbuf)
445                         return -ENOMEM;
446
447                 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
448                         kfree(ops.oobbuf);
449                         return -EFAULT;
450                 }
451
452                 buf.start &= ~(mtd->oobsize - 1);
453                 ret = mtd->write_oob(mtd, buf.start, &ops);
454
455                 if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
456                                  sizeof(uint32_t)))
457                         ret = -EFAULT;
458
459                 kfree(ops.oobbuf);
460                 break;
461
462         }
463
464         case MEMREADOOB:
465         {
466                 struct mtd_oob_buf buf;
467                 struct mtd_oob_ops ops;
468
469                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
470                         return -EFAULT;
471
472                 if (buf.length > 4096)
473                         return -EINVAL;
474
475                 if (!mtd->read_oob)
476                         ret = -EOPNOTSUPP;
477                 else
478                         ret = access_ok(VERIFY_WRITE, buf.ptr,
479                                         buf.length) ? 0 : -EFAULT;
480                 if (ret)
481                         return ret;
482
483                 ops.len = buf.length;
484                 ops.ooblen = mtd->oobsize;
485                 ops.ooboffs = buf.start & (mtd->oobsize - 1);
486                 ops.datbuf = NULL;
487                 ops.mode = MTD_OOB_PLACE;
488
489                 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
490                         return -EINVAL;
491
492                 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
493                 if (!ops.oobbuf)
494                         return -ENOMEM;
495
496                 buf.start &= ~(mtd->oobsize - 1);
497                 ret = mtd->read_oob(mtd, buf.start, &ops);
498
499                 if (put_user(ops.retlen, (uint32_t __user *)argp))
500                         ret = -EFAULT;
501                 else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
502                                                     ops.retlen))
503                         ret = -EFAULT;
504
505                 kfree(ops.oobbuf);
506                 break;
507         }
508
509         case MEMLOCK:
510         {
511                 struct erase_info_user info;
512
513                 if (copy_from_user(&info, argp, sizeof(info)))
514                         return -EFAULT;
515
516                 if (!mtd->lock)
517                         ret = -EOPNOTSUPP;
518                 else
519                         ret = mtd->lock(mtd, info.start, info.length);
520                 break;
521         }
522
523         case MEMUNLOCK:
524         {
525                 struct erase_info_user info;
526
527                 if (copy_from_user(&info, argp, sizeof(info)))
528                         return -EFAULT;
529
530                 if (!mtd->unlock)
531                         ret = -EOPNOTSUPP;
532                 else
533                         ret = mtd->unlock(mtd, info.start, info.length);
534                 break;
535         }
536
537         /* Legacy interface */
538         case MEMGETOOBSEL:
539         {
540                 struct nand_oobinfo oi;
541
542                 if (!mtd->ecclayout)
543                         return -EOPNOTSUPP;
544                 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
545                         return -EINVAL;
546
547                 oi.useecc = MTD_NANDECC_AUTOPLACE;
548                 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
549                 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
550                        sizeof(oi.oobfree));
551
552                 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
553                         return -EFAULT;
554                 break;
555         }
556
557         case ECCGETLAYOUT:
558
559                 if (!mtd->ecclayout)
560                         return -EOPNOTSUPP;
561
562                 if (copy_to_user(argp, &mtd->ecclayout,
563                                  sizeof(struct nand_ecclayout)))
564                         return -EFAULT;
565                 break;
566
567         case MEMGETBADBLOCK:
568         {
569                 loff_t offs;
570
571                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
572                         return -EFAULT;
573                 if (!mtd->block_isbad)
574                         ret = -EOPNOTSUPP;
575                 else
576                         return mtd->block_isbad(mtd, offs);
577                 break;
578         }
579
580         case MEMSETBADBLOCK:
581         {
582                 loff_t offs;
583
584                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
585                         return -EFAULT;
586                 if (!mtd->block_markbad)
587                         ret = -EOPNOTSUPP;
588                 else
589                         return mtd->block_markbad(mtd, offs);
590                 break;
591         }
592
593 #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
594         case OTPSELECT:
595         {
596                 int mode;
597                 if (copy_from_user(&mode, argp, sizeof(int)))
598                         return -EFAULT;
599                 SET_MTD_MODE(file, 0);
600                 switch (mode) {
601                 case MTD_OTP_FACTORY:
602                         if (!mtd->read_fact_prot_reg)
603                                 ret = -EOPNOTSUPP;
604                         else
605                                 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
606                         break;
607                 case MTD_OTP_USER:
608                         if (!mtd->read_fact_prot_reg)
609                                 ret = -EOPNOTSUPP;
610                         else
611                                 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
612                         break;
613                 default:
614                         ret = -EINVAL;
615                 case MTD_OTP_OFF:
616                         break;
617                 }
618                 file->f_pos = 0;
619                 break;
620         }
621
622         case OTPGETREGIONCOUNT:
623         case OTPGETREGIONINFO:
624         {
625                 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
626                 if (!buf)
627                         return -ENOMEM;
628                 ret = -EOPNOTSUPP;
629                 switch (MTD_MODE(file)) {
630                 case MTD_MODE_OTP_FACT:
631                         if (mtd->get_fact_prot_info)
632                                 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
633                         break;
634                 case MTD_MODE_OTP_USER:
635                         if (mtd->get_user_prot_info)
636                                 ret = mtd->get_user_prot_info(mtd, buf, 4096);
637                         break;
638                 }
639                 if (ret >= 0) {
640                         if (cmd == OTPGETREGIONCOUNT) {
641                                 int nbr = ret / sizeof(struct otp_info);
642                                 ret = copy_to_user(argp, &nbr, sizeof(int));
643                         } else
644                                 ret = copy_to_user(argp, buf, ret);
645                         if (ret)
646                                 ret = -EFAULT;
647                 }
648                 kfree(buf);
649                 break;
650         }
651
652         case OTPLOCK:
653         {
654                 struct otp_info info;
655
656                 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
657                         return -EINVAL;
658                 if (copy_from_user(&info, argp, sizeof(info)))
659                         return -EFAULT;
660                 if (!mtd->lock_user_prot_reg)
661                         return -EOPNOTSUPP;
662                 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
663                 break;
664         }
665 #endif
666
667         default:
668                 ret = -ENOTTY;
669         }
670
671         return ret;
672 } /* memory_ioctl */
673
674 static struct file_operations mtd_fops = {
675         .owner          = THIS_MODULE,
676         .llseek         = mtd_lseek,
677         .read           = mtd_read,
678         .write          = mtd_write,
679         .ioctl          = mtd_ioctl,
680         .open           = mtd_open,
681         .release        = mtd_close,
682 };
683
684 static int __init init_mtdchar(void)
685 {
686         if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
687                 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
688                        MTD_CHAR_MAJOR);
689                 return -EAGAIN;
690         }
691
692         mtd_class = class_create(THIS_MODULE, "mtd");
693
694         if (IS_ERR(mtd_class)) {
695                 printk(KERN_ERR "Error creating mtd class.\n");
696                 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
697                 return PTR_ERR(mtd_class);
698         }
699
700         register_mtd_user(&notifier);
701         return 0;
702 }
703
704 static void __exit cleanup_mtdchar(void)
705 {
706         unregister_mtd_user(&notifier);
707         class_destroy(mtd_class);
708         unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
709 }
710
711 module_init(init_mtdchar);
712 module_exit(cleanup_mtdchar);
713
714
715 MODULE_LICENSE("GPL");
716 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
717 MODULE_DESCRIPTION("Direct character-device access to MTD devices");