upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / drivers / char / mem.c
1 /*
2  *  linux/drivers/char/mem.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Added devfs support. 
7  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8  *  Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9  */
10
11 #include <linux/config.h>
12 #include <linux/mm.h>
13 #include <linux/miscdevice.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mman.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/raw.h>
20 #include <linux/tty.h>
21 #include <linux/capability.h>
22 #include <linux/smp_lock.h>
23 #include <linux/devfs_fs_kernel.h>
24 #include <linux/ptrace.h>
25 #include <linux/device.h>
26 #include <linux/backing-dev.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30
31 #ifdef CONFIG_IA64
32 # include <linux/efi.h>
33 #endif
34
35 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
36 extern void tapechar_init(void);
37 #endif
38
39 static inline int range_is_allowed(unsigned long from, unsigned long to)
40 {
41         unsigned long cursor;
42
43         cursor = from >> PAGE_SHIFT;
44         while ((cursor << PAGE_SHIFT) < to) {
45                 if (!devmem_is_allowed(cursor)) {
46                         printk ("Program %s tried to read /dev/mem between %lx->%lx."
47                                         "We stopped at %lx\n", current->comm, from, to, cursor);
48                         return 0;
49                 }
50                 cursor++;
51         }
52         return 1;
53 }
54
55 /*
56  * Architectures vary in how they handle caching for addresses
57  * outside of main memory.
58  *
59  */
60 static inline int uncached_access(struct file *file, unsigned long addr)
61 {
62 #if defined(__i386__)
63         /*
64          * On the PPro and successors, the MTRRs are used to set
65          * memory types for physical addresses outside main memory,
66          * so blindly setting PCD or PWT on those pages is wrong.
67          * For Pentiums and earlier, the surround logic should disable
68          * caching for the high addresses through the KEN pin, but
69          * we maintain the tradition of paranoia in this code.
70          */
71         if (file->f_flags & O_SYNC)
72                 return 1;
73         return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
74                   test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
75                   test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
76                   test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
77           && addr >= __pa(high_memory);
78 #elif defined(__x86_64__)
79         /* 
80          * This is broken because it can generate memory type aliases,
81          * which can cause cache corruptions
82          * But it is only available for root and we have to be bug-to-bug
83          * compatible with i386.
84          */
85         if (file->f_flags & O_SYNC)
86                 return 1;
87         /* same behaviour as i386. PAT always set to cached and MTRRs control the
88            caching behaviour. 
89            Hopefully a full PAT implementation will fix that soon. */      
90         return 0;
91 #elif defined(CONFIG_IA64)
92         /*
93          * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
94          */
95         return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
96 #else
97         /*
98          * Accessing memory above the top the kernel knows about or through a file pointer
99          * that was marked O_SYNC will be done non-cached.
100          */
101         if (file->f_flags & O_SYNC)
102                 return 1;
103         return addr >= __pa(high_memory);
104 #endif
105 }
106
107 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
108 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
109 {
110         unsigned long end_mem;
111
112         end_mem = __pa(high_memory);
113         if (addr >= end_mem)
114                 return 0;
115
116         if (*count > end_mem - addr)
117                 *count = end_mem - addr;
118
119         return 1;
120 }
121 #endif
122
123 #ifndef ARCH_HAS_DEV_MEM
124 /*
125  * This funcion reads the *physical* memory. The f_pos points directly to the 
126  * memory location. 
127  */
128 static ssize_t read_mem(struct file * file, char __user * buf,
129                         size_t count, loff_t *ppos)
130 {
131         unsigned long p = *ppos;
132         ssize_t read, sz;
133         char *ptr;
134
135         if (!valid_phys_addr_range(p, &count))
136                 return -EFAULT;
137         read = 0;
138 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
139         /* we don't have page 0 mapped on sparc and m68k.. */
140         if (p < PAGE_SIZE) {
141                 sz = PAGE_SIZE - p;
142                 if (sz > count) 
143                         sz = count; 
144                 if (sz > 0) {
145                         if (clear_user(buf, sz))
146                                 return -EFAULT;
147                         buf += sz; 
148                         p += sz; 
149                         count -= sz; 
150                         read += sz; 
151                 }
152         }
153 #endif
154
155         while (count > 0) {
156                 /*
157                  * Handle first page in case it's not aligned
158                  */
159                 if (-p & (PAGE_SIZE - 1))
160                         sz = -p & (PAGE_SIZE - 1);
161                 else
162                         sz = PAGE_SIZE;
163
164                 sz = min_t(unsigned long, sz, count);
165
166                 /*
167                  * On ia64 if a page has been mapped somewhere as
168                  * uncached, then it must also be accessed uncached
169                  * by the kernel or data corruption may occur
170                  */
171                 ptr = xlate_dev_mem_ptr(p);
172
173                 if (!range_is_allowed(p, p+count))
174                         return -EPERM;
175                 if (copy_to_user(buf, ptr, sz))
176                         return -EFAULT;
177                 buf += sz;
178                 p += sz;
179                 count -= sz;
180                 read += sz;
181         }
182
183         *ppos += read;
184         return read;
185 }
186
187 static ssize_t write_mem(struct file * file, const char __user * buf, 
188                          size_t count, loff_t *ppos)
189 {
190         unsigned long p = *ppos;
191         ssize_t written, sz;
192         unsigned long copied;
193         void *ptr;
194
195         if (!valid_phys_addr_range(p, &count))
196                 return -EFAULT;
197
198         written = 0;
199
200 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
201         /* we don't have page 0 mapped on sparc and m68k.. */
202         if (p < PAGE_SIZE) {
203                 unsigned long sz = PAGE_SIZE - p;
204                 if (sz > count)
205                         sz = count;
206                 /* Hmm. Do something? */
207                 buf += sz;
208                 p += sz;
209                 count -= sz;
210                 written += sz;
211         }
212 #endif
213
214         while (count > 0) {
215                 /*
216                  * Handle first page in case it's not aligned
217                  */
218                 if (-p & (PAGE_SIZE - 1))
219                         sz = -p & (PAGE_SIZE - 1);
220                 else
221                         sz = PAGE_SIZE;
222
223                 sz = min_t(unsigned long, sz, count);
224
225                 /*
226                  * On ia64 if a page has been mapped somewhere as
227                  * uncached, then it must also be accessed uncached
228                  * by the kernel or data corruption may occur
229                  */
230                 ptr = xlate_dev_mem_ptr(p);
231
232                 if (!range_is_allowed(ptr, ptr+sz))
233                         return -EPERM;
234                 copied = copy_from_user(ptr, buf, sz);
235                 if (copied) {
236                         ssize_t ret;
237
238                         ret = written + (sz - copied);
239                         if (ret)
240                                 return ret;
241                         return -EFAULT;
242                 }
243                 buf += sz;
244                 p += sz;
245                 count -= sz;
246                 written += sz;
247         }
248
249         *ppos += written;
250         return written;
251 }
252 #endif
253
254 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
255 {
256 #if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
257         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
258
259         vma->vm_page_prot = phys_mem_access_prot(file, offset,
260                                                  vma->vm_end - vma->vm_start,
261                                                  vma->vm_page_prot);
262 #elif defined(pgprot_noncached)
263         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
264         int uncached;
265
266         uncached = uncached_access(file, offset);
267         if (uncached)
268                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
269 #endif
270
271         /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
272         if (remap_pfn_range(vma,
273                             vma->vm_start,
274                             vma->vm_pgoff,
275                             vma->vm_end-vma->vm_start,
276                             vma->vm_page_prot))
277                 return -EAGAIN;
278         return 0;
279 }
280
281 #ifdef CONFIG_CRASH_DUMP
282 /*
283  * Read memory corresponding to the old kernel.
284  * If we are reading from the reserved section, which is
285  * actually used by the current kernel, we just return zeroes.
286  * Or if we are reading from the first 640k, we return from the
287  * backed up area.
288  */
289 static ssize_t read_oldmem(struct file * file, char * buf,
290                                 size_t count, loff_t *ppos)
291 {
292         unsigned long pfn;
293         unsigned backup_start, backup_end, relocate_start;
294         size_t read=0, csize;
295
296         backup_start = CRASH_BACKUP_BASE / PAGE_SIZE;
297         backup_end = backup_start + (CRASH_BACKUP_SIZE / PAGE_SIZE);
298         relocate_start = (CRASH_BACKUP_BASE + CRASH_BACKUP_SIZE) / PAGE_SIZE;
299
300         while(count) {
301                 pfn = *ppos / PAGE_SIZE;
302
303                 csize = (count > PAGE_SIZE) ? PAGE_SIZE : count;
304
305                 /* Perform translation (see comment above) */
306                 if ((pfn >= backup_start) && (pfn < backup_end)) {
307                         if (clear_user(buf, csize)) {
308                                 read = -EFAULT;
309                                 goto done;
310                         }
311
312                         goto copy_done;
313                 } else if (pfn < (CRASH_RELOCATE_SIZE / PAGE_SIZE))
314                         pfn += relocate_start;
315
316                 if (pfn > saved_max_pfn) {
317                         read = 0;
318                         goto done;
319                 }
320
321                 if (copy_oldmem_page(pfn, buf, csize, 1)) {
322                         read = -EFAULT;
323                         goto done;
324                 }
325
326 copy_done:
327                 buf += csize;
328                 *ppos += csize;
329                 read += csize;
330                 count -= csize;
331         }
332 done:
333         return read;
334 }
335 #endif
336
337 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
338 {
339         unsigned long long val;
340         /*
341          * RED-PEN: on some architectures there is more mapped memory
342          * than available in mem_map which pfn_valid checks
343          * for. Perhaps should add a new macro here.
344          *
345          * RED-PEN: vmalloc is not supported right now.
346          */
347         if (!pfn_valid(vma->vm_pgoff))
348                 return -EIO;
349         val = (u64)vma->vm_pgoff << PAGE_SHIFT;
350         vma->vm_pgoff = __pa(val) >> PAGE_SHIFT;
351         return mmap_mem(file, vma);
352 }
353
354 extern long vread(char *buf, char *addr, unsigned long count);
355 extern long vwrite(char *buf, char *addr, unsigned long count);
356
357 /*
358  * This function reads the *virtual* memory as seen by the kernel.
359  */
360 static ssize_t read_kmem(struct file *file, char __user *buf, 
361                          size_t count, loff_t *ppos)
362 {
363         unsigned long p = *ppos;
364         ssize_t low_count, read, sz;
365         char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
366
367         return -EPERM;
368
369         read = 0;
370         if (p < (unsigned long) high_memory) {
371                 low_count = count;
372                 if (count > (unsigned long) high_memory - p)
373                         low_count = (unsigned long) high_memory - p;
374
375 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
376                 /* we don't have page 0 mapped on sparc and m68k.. */
377                 if (p < PAGE_SIZE && low_count > 0) {
378                         size_t tmp = PAGE_SIZE - p;
379                         if (tmp > low_count) tmp = low_count;
380                         if (clear_user(buf, tmp))
381                                 return -EFAULT;
382                         buf += tmp;
383                         p += tmp;
384                         read += tmp;
385                         low_count -= tmp;
386                         count -= tmp;
387                 }
388 #endif
389                 while (low_count > 0) {
390                         /*
391                          * Handle first page in case it's not aligned
392                          */
393                         if (-p & (PAGE_SIZE - 1))
394                                 sz = -p & (PAGE_SIZE - 1);
395                         else
396                                 sz = PAGE_SIZE;
397
398                         sz = min_t(unsigned long, sz, low_count);
399
400                         /*
401                          * On ia64 if a page has been mapped somewhere as
402                          * uncached, then it must also be accessed uncached
403                          * by the kernel or data corruption may occur
404                          */
405                         kbuf = xlate_dev_kmem_ptr((char *)p);
406
407                         if (copy_to_user(buf, kbuf, sz))
408                                 return -EFAULT;
409                         buf += sz;
410                         p += sz;
411                         read += sz;
412                         low_count -= sz;
413                         count -= sz;
414                 }
415         }
416
417         if (count > 0) {
418                 kbuf = (char *)__get_free_page(GFP_KERNEL);
419                 if (!kbuf)
420                         return -ENOMEM;
421                 while (count > 0) {
422                         int len = count;
423
424                         if (len > PAGE_SIZE)
425                                 len = PAGE_SIZE;
426                         len = vread(kbuf, (char *)p, len);
427                         if (!len)
428                                 break;
429                         if (copy_to_user(buf, kbuf, len)) {
430                                 free_page((unsigned long)kbuf);
431                                 return -EFAULT;
432                         }
433                         count -= len;
434                         buf += len;
435                         read += len;
436                         p += len;
437                 }
438                 free_page((unsigned long)kbuf);
439         }
440         *ppos = p;
441         return read;
442 }
443
444
445 #if defined(CONFIG_ISA) || !defined(__mc68000__)
446 static ssize_t read_port(struct file * file, char __user * buf,
447                          size_t count, loff_t *ppos)
448 {
449         unsigned long i = *ppos;
450         char __user *tmp = buf;
451
452         if (!access_ok(VERIFY_WRITE, buf, count))
453                 return -EFAULT; 
454         while (count-- > 0 && i < 65536) {
455                 if (__put_user(inb(i),tmp) < 0) 
456                         return -EFAULT;  
457                 i++;
458                 tmp++;
459         }
460         *ppos = i;
461         return tmp-buf;
462 }
463
464 static ssize_t write_port(struct file * file, const char __user * buf,
465                           size_t count, loff_t *ppos)
466 {
467         unsigned long i = *ppos;
468         const char __user * tmp = buf;
469
470         if (!access_ok(VERIFY_READ,buf,count))
471                 return -EFAULT;
472         while (count-- > 0 && i < 65536) {
473                 char c;
474                 if (__get_user(c, tmp)) 
475                         return -EFAULT; 
476                 outb(c,i);
477                 i++;
478                 tmp++;
479         }
480         *ppos = i;
481         return tmp-buf;
482 }
483 #endif
484
485 static ssize_t read_null(struct file * file, char __user * buf,
486                          size_t count, loff_t *ppos)
487 {
488         return 0;
489 }
490
491 static ssize_t write_null(struct file * file, const char __user * buf,
492                           size_t count, loff_t *ppos)
493 {
494         return count;
495 }
496
497 #ifdef CONFIG_MMU
498 /*
499  * For fun, we are using the MMU for this.
500  */
501 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
502 {
503         struct mm_struct *mm;
504         struct vm_area_struct * vma;
505         unsigned long addr=(unsigned long)buf;
506
507         mm = current->mm;
508         /* Oops, this was forgotten before. -ben */
509         down_read(&mm->mmap_sem);
510
511         /* For private mappings, just map in zero pages. */
512         for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
513                 unsigned long count;
514
515                 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
516                         goto out_up;
517                 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
518                         break;
519                 count = vma->vm_end - addr;
520                 if (count > size)
521                         count = size;
522
523                 zap_page_range(vma, addr, count, NULL);
524                 zeromap_page_range(vma, addr, count, PAGE_COPY);
525
526                 size -= count;
527                 buf += count;
528                 addr += count;
529                 if (size == 0)
530                         goto out_up;
531         }
532
533         up_read(&mm->mmap_sem);
534         
535         /* The shared case is hard. Let's do the conventional zeroing. */ 
536         do {
537                 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
538                 if (unwritten)
539                         return size + unwritten - PAGE_SIZE;
540                 cond_resched();
541                 buf += PAGE_SIZE;
542                 size -= PAGE_SIZE;
543         } while (size);
544
545         return size;
546 out_up:
547         up_read(&mm->mmap_sem);
548         return size;
549 }
550
551 static ssize_t read_zero(struct file * file, char __user * buf, 
552                          size_t count, loff_t *ppos)
553 {
554         unsigned long left, unwritten, written = 0;
555
556         if (!count)
557                 return 0;
558
559         if (!access_ok(VERIFY_WRITE, buf, count))
560                 return -EFAULT;
561
562         left = count;
563
564         /* do we want to be clever? Arbitrary cut-off */
565         if (count >= PAGE_SIZE*4) {
566                 unsigned long partial;
567
568                 /* How much left of the page? */
569                 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
570                 unwritten = clear_user(buf, partial);
571                 written = partial - unwritten;
572                 if (unwritten)
573                         goto out;
574                 left -= partial;
575                 buf += partial;
576                 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
577                 written += (left & PAGE_MASK) - unwritten;
578                 if (unwritten)
579                         goto out;
580                 buf += left & PAGE_MASK;
581                 left &= ~PAGE_MASK;
582         }
583         unwritten = clear_user(buf, left);
584         written += left - unwritten;
585 out:
586         return written ? written : -EFAULT;
587 }
588
589 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
590 {
591         if (vma->vm_flags & VM_SHARED)
592                 return shmem_zero_setup(vma);
593         if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
594                 return -EAGAIN;
595         return 0;
596 }
597 #else /* CONFIG_MMU */
598 static ssize_t read_zero(struct file * file, char * buf, 
599                          size_t count, loff_t *ppos)
600 {
601         size_t todo = count;
602
603         while (todo) {
604                 size_t chunk = todo;
605
606                 if (chunk > 4096)
607                         chunk = 4096;   /* Just for latency reasons */
608                 if (clear_user(buf, chunk))
609                         return -EFAULT;
610                 buf += chunk;
611                 todo -= chunk;
612                 cond_resched();
613         }
614         return count;
615 }
616
617 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
618 {
619         return -ENOSYS;
620 }
621 #endif /* CONFIG_MMU */
622
623 static ssize_t write_full(struct file * file, const char __user * buf,
624                           size_t count, loff_t *ppos)
625 {
626         return -ENOSPC;
627 }
628
629 /*
630  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
631  * can fopen() both devices with "a" now.  This was previously impossible.
632  * -- SRB.
633  */
634
635 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
636 {
637         return file->f_pos = 0;
638 }
639
640 /*
641  * The memory devices use the full 32/64 bits of the offset, and so we cannot
642  * check against negative addresses: they are ok. The return value is weird,
643  * though, in that case (0).
644  *
645  * also note that seeking relative to the "end of file" isn't supported:
646  * it has no meaning, so it returns -EINVAL.
647  */
648 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
649 {
650         loff_t ret;
651
652         down(&file->f_dentry->d_inode->i_sem);
653         switch (orig) {
654                 case 0:
655                         file->f_pos = offset;
656                         ret = file->f_pos;
657                         force_successful_syscall_return();
658                         break;
659                 case 1:
660                         file->f_pos += offset;
661                         ret = file->f_pos;
662                         force_successful_syscall_return();
663                         break;
664                 default:
665                         ret = -EINVAL;
666         }
667         up(&file->f_dentry->d_inode->i_sem);
668         return ret;
669 }
670
671 static int open_port(struct inode * inode, struct file * filp)
672 {
673         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
674 }
675
676 #define zero_lseek      null_lseek
677 #define full_lseek      null_lseek
678 #define write_zero      write_null
679 #define read_full       read_zero
680 #define open_mem        open_port
681 #define open_kmem       open_mem
682 #define open_oldmem     open_mem
683
684 #ifndef ARCH_HAS_DEV_MEM
685 static struct file_operations mem_fops = {
686         .llseek         = memory_lseek,
687         .read           = read_mem,
688         .write          = write_mem,
689         .mmap           = mmap_mem,
690         .open           = open_mem,
691 };
692 #else
693 extern struct file_operations mem_fops;
694 #endif
695
696 static struct file_operations kmem_fops = {
697         .llseek         = memory_lseek,
698         .read           = read_kmem,
699         .mmap           = mmap_kmem,
700         .open           = open_kmem,
701 };
702
703 static struct file_operations null_fops = {
704         .llseek         = null_lseek,
705         .read           = read_null,
706         .write          = write_null,
707 };
708
709 #if defined(CONFIG_ISA) || !defined(__mc68000__)
710 static struct file_operations port_fops = {
711         .llseek         = memory_lseek,
712         .read           = read_port,
713         .write          = write_port,
714         .open           = open_port,
715 };
716 #endif
717
718 static struct file_operations zero_fops = {
719         .llseek         = zero_lseek,
720         .read           = read_zero,
721         .write          = write_zero,
722         .mmap           = mmap_zero,
723 };
724
725 static struct backing_dev_info zero_bdi = {
726         .capabilities   = BDI_CAP_MAP_COPY,
727 };
728
729 static struct file_operations full_fops = {
730         .llseek         = full_lseek,
731         .read           = read_full,
732         .write          = write_full,
733 };
734
735 #ifdef CONFIG_CRASH_DUMP
736 static struct file_operations oldmem_fops = {
737         .read   = read_oldmem,
738         .open   = open_oldmem,
739 };
740 #endif
741
742 static ssize_t kmsg_write(struct file * file, const char __user * buf,
743                           size_t count, loff_t *ppos)
744 {
745         char *tmp;
746         int ret;
747
748         tmp = kmalloc(count + 1, GFP_KERNEL);
749         if (tmp == NULL)
750                 return -ENOMEM;
751         ret = -EFAULT;
752         if (!copy_from_user(tmp, buf, count)) {
753                 tmp[count] = 0;
754                 ret = printk("%s", tmp);
755         }
756         kfree(tmp);
757         return ret;
758 }
759
760 static struct file_operations kmsg_fops = {
761         .write =        kmsg_write,
762 };
763
764 static int memory_open(struct inode * inode, struct file * filp)
765 {
766         switch (iminor(inode)) {
767                 case 1:
768                         filp->f_op = &mem_fops;
769                         break;
770                 case 2:
771                         filp->f_op = &kmem_fops;
772                         break;
773                 case 3:
774                         filp->f_op = &null_fops;
775                         break;
776 #if defined(CONFIG_ISA) || !defined(__mc68000__)
777                 case 4:
778                         filp->f_op = &port_fops;
779                         break;
780 #endif
781                 case 5:
782                         filp->f_mapping->backing_dev_info = &zero_bdi;
783                         filp->f_op = &zero_fops;
784                         break;
785                 case 7:
786                         filp->f_op = &full_fops;
787                         break;
788                 case 8:
789                         filp->f_op = &random_fops;
790                         break;
791                 case 9:
792                         filp->f_op = &urandom_fops;
793                         break;
794                 case 11:
795                         filp->f_op = &kmsg_fops;
796                         break;
797 #ifdef CONFIG_CRASH_DUMP
798                 case 12:
799                         filp->f_op = &oldmem_fops;
800                         break;
801 #endif
802                 default:
803                         return -ENXIO;
804         }
805         if (filp->f_op && filp->f_op->open)
806                 return filp->f_op->open(inode,filp);
807         return 0;
808 }
809
810 static struct file_operations memory_fops = {
811         .open           = memory_open,  /* just a selector for the real open */
812 };
813
814 static const struct {
815         unsigned int            minor;
816         char                    *name;
817         umode_t                 mode;
818         struct file_operations  *fops;
819 } devlist[] = { /* list of minor devices */
820         {1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
821         {3, "null",    S_IRUGO | S_IWUGO,           &null_fops},
822 #if defined(CONFIG_ISA) || !defined(__mc68000__)
823         {4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
824 #endif
825         {5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
826         {7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
827         {8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
828         {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops},
829         {11,"kmsg",    S_IRUGO | S_IWUSR,           &kmsg_fops},
830 #ifdef CONFIG_CRASH_DUMP
831         {12,"oldmem",    S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
832 #endif
833 };
834
835 static struct class_simple *mem_class;
836
837 static int __init chr_dev_init(void)
838 {
839         int i;
840
841         if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
842                 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
843
844         mem_class = class_simple_create(THIS_MODULE, "mem");
845         for (i = 0; i < ARRAY_SIZE(devlist); i++) {
846                 class_simple_device_add(mem_class,
847                                         MKDEV(MEM_MAJOR, devlist[i].minor),
848                                         NULL, devlist[i].name);
849                 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
850                                 S_IFCHR | devlist[i].mode, devlist[i].name);
851         }
852         
853         return 0;
854 }
855
856 fs_initcall(chr_dev_init);