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