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