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