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