6a7aa93148fafedf49763cfa9a77cf3030754353
[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/highmem.h>
27 #include <linux/crash_dump.h>
28 #include <linux/backing-dev.h>
29 #include <linux/bootmem.h>
30 #include <linux/pipe_fs_i.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/io.h>
34
35 #ifdef CONFIG_IA64
36 # include <linux/efi.h>
37 #endif
38
39 /*
40  * Architectures vary in how they handle caching for addresses
41  * outside of main memory.
42  *
43  */
44 static inline int uncached_access(struct file *file, unsigned long addr)
45 {
46 #if defined(__i386__)
47         /*
48          * On the PPro and successors, the MTRRs are used to set
49          * memory types for physical addresses outside main memory,
50          * so blindly setting PCD or PWT on those pages is wrong.
51          * For Pentiums and earlier, the surround logic should disable
52          * caching for the high addresses through the KEN pin, but
53          * we maintain the tradition of paranoia in this code.
54          */
55         if (file->f_flags & O_SYNC)
56                 return 1;
57         return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
58                   test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
59                   test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
60                   test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
61           && addr >= __pa(high_memory);
62 #elif defined(__x86_64__)
63         /* 
64          * This is broken because it can generate memory type aliases,
65          * which can cause cache corruptions
66          * But it is only available for root and we have to be bug-to-bug
67          * compatible with i386.
68          */
69         if (file->f_flags & O_SYNC)
70                 return 1;
71         /* same behaviour as i386. PAT always set to cached and MTRRs control the
72            caching behaviour. 
73            Hopefully a full PAT implementation will fix that soon. */      
74         return 0;
75 #elif defined(CONFIG_IA64)
76         /*
77          * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
78          */
79         return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
80 #else
81         /*
82          * Accessing memory above the top the kernel knows about or through a file pointer
83          * that was marked O_SYNC will be done non-cached.
84          */
85         if (file->f_flags & O_SYNC)
86                 return 1;
87         return addr >= __pa(high_memory);
88 #endif
89 }
90
91 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
92 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
93 {
94         if (addr + count > __pa(high_memory))
95                 return 0;
96
97         return 1;
98 }
99
100 static inline int valid_mmap_phys_addr_range(unsigned long addr, size_t size)
101 {
102         return 1;
103 }
104 #endif
105
106 static inline int range_is_allowed(unsigned long from, unsigned long to)
107 {
108         unsigned long cursor;
109
110         cursor = from >> PAGE_SHIFT;
111         while ((cursor << PAGE_SHIFT) < to) {
112                 if (!devmem_is_allowed(cursor)) {
113                         printk ("Program %s tried to read /dev/mem between %lx->%lx.\n",
114                                         current->comm, from, to);
115                         return 0;
116                 }
117                 cursor++;
118         }
119         return 1;
120 }
121
122 /*
123  * This funcion reads the *physical* memory. The f_pos points directly to the 
124  * memory location. 
125  */
126 static ssize_t read_mem(struct file * file, char __user * buf,
127                         size_t count, loff_t *ppos)
128 {
129         unsigned long p = *ppos;
130         ssize_t read, sz;
131         char *ptr;
132
133         if (!valid_phys_addr_range(p, count))
134                 return -EFAULT;
135         read = 0;
136 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
137         /* we don't have page 0 mapped on sparc and m68k.. */
138         if (p < PAGE_SIZE) {
139                 sz = PAGE_SIZE - p;
140                 if (sz > count) 
141                         sz = count; 
142                 if (sz > 0) {
143                         if (clear_user(buf, sz))
144                                 return -EFAULT;
145                         buf += sz; 
146                         p += sz; 
147                         count -= sz; 
148                         read += sz; 
149                 }
150         }
151 #endif
152
153         while (count > 0) {
154                 /*
155                  * Handle first page in case it's not aligned
156                  */
157                 if (-p & (PAGE_SIZE - 1))
158                         sz = -p & (PAGE_SIZE - 1);
159                 else
160                         sz = PAGE_SIZE;
161
162                 sz = min_t(unsigned long, sz, count);
163
164                 /*
165                  * On ia64 if a page has been mapped somewhere as
166                  * uncached, then it must also be accessed uncached
167                  * by the kernel or data corruption may occur
168                  */
169                 ptr = xlate_dev_mem_ptr(p);
170
171                 if (!range_is_allowed(p, p+count))
172                         return -EPERM;
173                 if (copy_to_user(buf, ptr, sz))
174                         return -EFAULT;
175                 buf += sz;
176                 p += sz;
177                 count -= sz;
178                 read += sz;
179         }
180
181         *ppos += read;
182         return read;
183 }
184
185 static ssize_t write_mem(struct file * file, const char __user * buf, 
186                          size_t count, loff_t *ppos)
187 {
188         unsigned long p = *ppos;
189         ssize_t written, sz;
190         unsigned long copied;
191         void *ptr;
192
193         if (!valid_phys_addr_range(p, count))
194                 return -EFAULT;
195
196         written = 0;
197
198 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
199         /* we don't have page 0 mapped on sparc and m68k.. */
200         if (p < PAGE_SIZE) {
201                 unsigned long sz = PAGE_SIZE - p;
202                 if (sz > count)
203                         sz = count;
204                 /* Hmm. Do something? */
205                 buf += sz;
206                 p += sz;
207                 count -= sz;
208                 written += sz;
209         }
210 #endif
211
212         while (count > 0) {
213                 /*
214                  * Handle first page in case it's not aligned
215                  */
216                 if (-p & (PAGE_SIZE - 1))
217                         sz = -p & (PAGE_SIZE - 1);
218                 else
219                         sz = PAGE_SIZE;
220
221                 sz = min_t(unsigned long, sz, count);
222
223                 /*
224                  * On ia64 if a page has been mapped somewhere as
225                  * uncached, then it must also be accessed uncached
226                  * by the kernel or data corruption may occur
227                  */
228                 ptr = xlate_dev_mem_ptr(p);
229
230                 if (!range_is_allowed(ptr, ptr+sz))
231                         return -EPERM;
232                 copied = copy_from_user(ptr, buf, sz);
233                 if (copied) {
234                         written += sz - copied;
235                         if (written)
236                                 break;
237                         return -EFAULT;
238                 }
239                 buf += sz;
240                 p += sz;
241                 count -= sz;
242                 written += sz;
243         }
244
245         *ppos += written;
246         return written;
247 }
248
249 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
250 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
251                                      unsigned long size, pgprot_t vma_prot)
252 {
253 #ifdef pgprot_noncached
254         unsigned long offset = pfn << PAGE_SHIFT;
255
256         if (uncached_access(file, offset))
257                 return pgprot_noncached(vma_prot);
258 #endif
259         return vma_prot;
260 }
261 #endif
262
263 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
264 {
265         size_t size = vma->vm_end - vma->vm_start;
266
267         if (!valid_mmap_phys_addr_range(vma->vm_pgoff << PAGE_SHIFT, size))
268                 return -EINVAL;
269
270         vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
271                                                  size,
272                                                  vma->vm_page_prot);
273
274         /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
275         if (remap_pfn_range(vma,
276                             vma->vm_start,
277                             vma->vm_pgoff,
278                             size,
279                             vma->vm_page_prot))
280                 return -EAGAIN;
281         return 0;
282 }
283
284 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
285 {
286         unsigned long pfn;
287
288         /* Turn a kernel-virtual address into a physical page frame */
289         pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
290
291         /*
292          * RED-PEN: on some architectures there is more mapped memory
293          * than available in mem_map which pfn_valid checks
294          * for. Perhaps should add a new macro here.
295          *
296          * RED-PEN: vmalloc is not supported right now.
297          */
298         if (!pfn_valid(pfn))
299                 return -EIO;
300
301         vma->vm_pgoff = pfn;
302         return mmap_mem(file, vma);
303 }
304
305 #ifdef CONFIG_CRASH_DUMP
306 /*
307  * Read memory corresponding to the old kernel.
308  */
309 static ssize_t read_oldmem(struct file *file, char __user *buf,
310                                 size_t count, loff_t *ppos)
311 {
312         unsigned long pfn, offset;
313         size_t read = 0, csize;
314         int rc = 0;
315
316         while (count) {
317                 pfn = *ppos / PAGE_SIZE;
318                 if (pfn > saved_max_pfn)
319                         return read;
320
321                 offset = (unsigned long)(*ppos % PAGE_SIZE);
322                 if (count > PAGE_SIZE - offset)
323                         csize = PAGE_SIZE - offset;
324                 else
325                         csize = count;
326
327                 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
328                 if (rc < 0)
329                         return rc;
330                 buf += csize;
331                 *ppos += csize;
332                 read += csize;
333                 count -= csize;
334         }
335         return read;
336 }
337 #endif
338
339 extern long vread(char *buf, char *addr, unsigned long count);
340 extern long vwrite(char *buf, char *addr, unsigned long count);
341
342 /*
343  * This function reads the *virtual* memory as seen by the kernel.
344  */
345 static ssize_t read_kmem(struct file *file, char __user *buf, 
346                          size_t count, loff_t *ppos)
347 {
348         unsigned long p = *ppos;
349         ssize_t low_count, read, sz;
350         char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
351
352         return -EPERM;
353
354         read = 0;
355         if (p < (unsigned long) high_memory) {
356                 low_count = count;
357                 if (count > (unsigned long) high_memory - p)
358                         low_count = (unsigned long) high_memory - p;
359
360 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
361                 /* we don't have page 0 mapped on sparc and m68k.. */
362                 if (p < PAGE_SIZE && low_count > 0) {
363                         size_t tmp = PAGE_SIZE - p;
364                         if (tmp > low_count) tmp = low_count;
365                         if (clear_user(buf, tmp))
366                                 return -EFAULT;
367                         buf += tmp;
368                         p += tmp;
369                         read += tmp;
370                         low_count -= tmp;
371                         count -= tmp;
372                 }
373 #endif
374                 while (low_count > 0) {
375                         /*
376                          * Handle first page in case it's not aligned
377                          */
378                         if (-p & (PAGE_SIZE - 1))
379                                 sz = -p & (PAGE_SIZE - 1);
380                         else
381                                 sz = PAGE_SIZE;
382
383                         sz = min_t(unsigned long, sz, low_count);
384
385                         /*
386                          * On ia64 if a page has been mapped somewhere as
387                          * uncached, then it must also be accessed uncached
388                          * by the kernel or data corruption may occur
389                          */
390                         kbuf = xlate_dev_kmem_ptr((char *)p);
391
392                         if (copy_to_user(buf, kbuf, sz))
393                                 return -EFAULT;
394                         buf += sz;
395                         p += sz;
396                         read += sz;
397                         low_count -= sz;
398                         count -= sz;
399                 }
400         }
401
402         if (count > 0) {
403                 kbuf = (char *)__get_free_page(GFP_KERNEL);
404                 if (!kbuf)
405                         return -ENOMEM;
406                 while (count > 0) {
407                         int len = count;
408
409                         if (len > PAGE_SIZE)
410                                 len = PAGE_SIZE;
411                         len = vread(kbuf, (char *)p, len);
412                         if (!len)
413                                 break;
414                         if (copy_to_user(buf, kbuf, len)) {
415                                 free_page((unsigned long)kbuf);
416                                 return -EFAULT;
417                         }
418                         count -= len;
419                         buf += len;
420                         read += len;
421                         p += len;
422                 }
423                 free_page((unsigned long)kbuf);
424         }
425         *ppos = p;
426         return read;
427 }
428
429
430 #if defined(CONFIG_ISA) || !defined(__mc68000__)
431 static ssize_t read_port(struct file * file, char __user * buf,
432                          size_t count, loff_t *ppos)
433 {
434         unsigned long i = *ppos;
435         char __user *tmp = buf;
436
437         if (!access_ok(VERIFY_WRITE, buf, count))
438                 return -EFAULT; 
439         while (count-- > 0 && i < 65536) {
440                 if (__put_user(inb(i),tmp) < 0) 
441                         return -EFAULT;  
442                 i++;
443                 tmp++;
444         }
445         *ppos = i;
446         return tmp-buf;
447 }
448
449 static ssize_t write_port(struct file * file, const char __user * buf,
450                           size_t count, loff_t *ppos)
451 {
452         unsigned long i = *ppos;
453         const char __user * tmp = buf;
454
455         if (!access_ok(VERIFY_READ,buf,count))
456                 return -EFAULT;
457         while (count-- > 0 && i < 65536) {
458                 char c;
459                 if (__get_user(c, tmp)) {
460                         if (tmp > buf)
461                                 break;
462                         return -EFAULT; 
463                 }
464                 outb(c,i);
465                 i++;
466                 tmp++;
467         }
468         *ppos = i;
469         return tmp-buf;
470 }
471 #endif
472
473 static ssize_t read_null(struct file * file, char __user * buf,
474                          size_t count, loff_t *ppos)
475 {
476         return 0;
477 }
478
479 static ssize_t write_null(struct file * file, const char __user * buf,
480                           size_t count, loff_t *ppos)
481 {
482         return count;
483 }
484
485 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
486                         struct splice_desc *sd)
487 {
488         return sd->len;
489 }
490
491 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
492                                  loff_t *ppos, size_t len, unsigned int flags)
493 {
494         return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
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         mutex_lock(&file->f_dentry->d_inode->i_mutex);
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         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
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 static struct file_operations mem_fops = {
685         .llseek         = memory_lseek,
686         .read           = read_mem,
687         .write          = write_mem,
688         .mmap           = mmap_mem,
689         .open           = open_mem,
690 };
691
692 static struct file_operations kmem_fops = {
693         .llseek         = memory_lseek,
694         .read           = read_kmem,
695         .mmap           = mmap_kmem,
696         .open           = open_kmem,
697 };
698
699 static struct file_operations null_fops = {
700         .llseek         = null_lseek,
701         .read           = read_null,
702         .write          = write_null,
703         .splice_write   = splice_write_null,
704 };
705
706 #if defined(CONFIG_ISA) || !defined(__mc68000__)
707 static struct file_operations port_fops = {
708         .llseek         = memory_lseek,
709         .read           = read_port,
710         .write          = write_port,
711         .open           = open_port,
712 };
713 #endif
714
715 static struct file_operations zero_fops = {
716         .llseek         = zero_lseek,
717         .read           = read_zero,
718         .write          = write_zero,
719         .mmap           = mmap_zero,
720 };
721
722 static struct backing_dev_info zero_bdi = {
723         .capabilities   = BDI_CAP_MAP_COPY,
724 };
725
726 static struct file_operations full_fops = {
727         .llseek         = full_lseek,
728         .read           = read_full,
729         .write          = write_full,
730 };
731
732 #ifdef CONFIG_CRASH_DUMP
733 static struct file_operations oldmem_fops = {
734         .read   = read_oldmem,
735         .open   = open_oldmem,
736 };
737 #endif
738
739 static ssize_t kmsg_write(struct file * file, const char __user * buf,
740                           size_t count, loff_t *ppos)
741 {
742         char *tmp;
743         ssize_t ret;
744
745         tmp = kmalloc(count + 1, GFP_KERNEL);
746         if (tmp == NULL)
747                 return -ENOMEM;
748         ret = -EFAULT;
749         if (!copy_from_user(tmp, buf, count)) {
750                 tmp[count] = 0;
751                 ret = printk("%s", tmp);
752                 if (ret > count)
753                         /* printk can add a prefix */
754                         ret = count;
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         const 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 *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_create(THIS_MODULE, "mem");
845         for (i = 0; i < ARRAY_SIZE(devlist); i++) {
846                 class_device_create(mem_class, NULL,
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);