fedora core 6 1.2949 + vserver 2.2.0
[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/mm.h>
12 #include <linux/miscdevice.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mman.h>
16 #include <linux/random.h>
17 #include <linux/init.h>
18 #include <linux/raw.h>
19 #include <linux/tty.h>
20 #include <linux/capability.h>
21 #include <linux/smp_lock.h>
22 #include <linux/ptrace.h>
23 #include <linux/device.h>
24 #include <linux/highmem.h>
25 #include <linux/crash_dump.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bootmem.h>
28 #include <linux/pipe_fs_i.h>
29 #include <linux/pfn.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
36 #endif
37
38 static inline int range_is_allowed(unsigned long from, unsigned long to)
39 {
40         unsigned long cursor;
41
42         cursor = from >> PAGE_SHIFT;
43         while ((cursor << PAGE_SHIFT) < to) {
44                 if (!devmem_is_allowed(cursor)) {
45                         printk ("Program %s tried to read /dev/mem between %lx->%lx.\n",
46                                         current->comm, from, to);
47                         return 0;
48                 }
49                 cursor++;
50         }
51         return 1;
52 }
53
54 /*
55  * Architectures vary in how they handle caching for addresses
56  * outside of main memory.
57  *
58  */
59 static inline int uncached_access(struct file *file, unsigned long addr)
60 {
61 #if defined(__i386__)
62         /*
63          * On the PPro and successors, the MTRRs are used to set
64          * memory types for physical addresses outside main memory,
65          * so blindly setting PCD or PWT on those pages is wrong.
66          * For Pentiums and earlier, the surround logic should disable
67          * caching for the high addresses through the KEN pin, but
68          * we maintain the tradition of paranoia in this code.
69          */
70         if (file->f_flags & O_SYNC)
71                 return 1;
72         return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
73                   test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
74                   test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
75                   test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
76           && addr >= __pa(high_memory);
77 #elif defined(__x86_64__)
78         /* 
79          * This is broken because it can generate memory type aliases,
80          * which can cause cache corruptions
81          * But it is only available for root and we have to be bug-to-bug
82          * compatible with i386.
83          */
84         if (file->f_flags & O_SYNC)
85                 return 1;
86         /* same behaviour as i386. PAT always set to cached and MTRRs control the
87            caching behaviour. 
88            Hopefully a full PAT implementation will fix that soon. */      
89         return 0;
90 #elif defined(CONFIG_IA64)
91         /*
92          * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
93          */
94         return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
95 #else
96         /*
97          * Accessing memory above the top the kernel knows about or through a file pointer
98          * that was marked O_SYNC will be done non-cached.
99          */
100         if (file->f_flags & O_SYNC)
101                 return 1;
102         return addr >= __pa(high_memory);
103 #endif
104 }
105
106 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
107 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
108 {
109         if (addr + count > __pa(high_memory))
110                 return 0;
111
112         return 1;
113 }
114
115 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
116 {
117         return 1;
118 }
119 #endif
120
121 #ifndef ARCH_HAS_DEV_MEM
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 #endif
249
250 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
251 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
252                                      unsigned long size, pgprot_t vma_prot)
253 {
254 #ifdef pgprot_noncached
255         unsigned long offset = pfn << PAGE_SHIFT;
256
257         if (uncached_access(file, offset))
258                 return pgprot_noncached(vma_prot);
259 #endif
260         return vma_prot;
261 }
262 #endif
263
264 #ifndef CONFIG_MMU
265 static unsigned long get_unmapped_area_mem(struct file *file,
266                                            unsigned long addr,
267                                            unsigned long len,
268                                            unsigned long pgoff,
269                                            unsigned long flags)
270 {
271         if (!valid_mmap_phys_addr_range(pgoff, len))
272                 return (unsigned long) -EINVAL;
273         return pgoff << PAGE_SHIFT;
274 }
275
276 /* can't do an in-place private mapping if there's no MMU */
277 static inline int private_mapping_ok(struct vm_area_struct *vma)
278 {
279         return vma->vm_flags & VM_MAYSHARE;
280 }
281 #else
282 #define get_unmapped_area_mem   NULL
283
284 static inline int private_mapping_ok(struct vm_area_struct *vma)
285 {
286         return 1;
287 }
288 #endif
289
290 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
291 {
292         size_t size = vma->vm_end - vma->vm_start;
293
294         if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
295                 return -EINVAL;
296
297         if (!private_mapping_ok(vma))
298                 return -ENOSYS;
299
300         vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
301                                                  size,
302                                                  vma->vm_page_prot);
303
304         /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
305         if (remap_pfn_range(vma,
306                             vma->vm_start,
307                             vma->vm_pgoff,
308                             size,
309                             vma->vm_page_prot))
310                 return -EAGAIN;
311         return 0;
312 }
313
314 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
315 {
316         unsigned long pfn;
317
318         /* Turn a kernel-virtual address into a physical page frame */
319         pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
320
321         /*
322          * RED-PEN: on some architectures there is more mapped memory
323          * than available in mem_map which pfn_valid checks
324          * for. Perhaps should add a new macro here.
325          *
326          * RED-PEN: vmalloc is not supported right now.
327          */
328         if (!pfn_valid(pfn))
329                 return -EIO;
330
331         vma->vm_pgoff = pfn;
332         return mmap_mem(file, vma);
333 }
334
335 #ifdef CONFIG_CRASH_DUMP
336 /*
337  * Read memory corresponding to the old kernel.
338  */
339 static ssize_t read_oldmem(struct file *file, char __user *buf,
340                                 size_t count, loff_t *ppos)
341 {
342         unsigned long pfn, offset;
343         size_t read = 0, csize;
344         int rc = 0;
345
346         while (count) {
347                 pfn = *ppos / PAGE_SIZE;
348                 if (pfn > saved_max_pfn)
349                         return read;
350
351                 offset = (unsigned long)(*ppos % PAGE_SIZE);
352                 if (count > PAGE_SIZE - offset)
353                         csize = PAGE_SIZE - offset;
354                 else
355                         csize = count;
356
357                 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
358                 if (rc < 0)
359                         return rc;
360                 buf += csize;
361                 *ppos += csize;
362                 read += csize;
363                 count -= csize;
364         }
365         return read;
366 }
367 #endif
368
369 extern long vread(char *buf, char *addr, unsigned long count);
370 extern long vwrite(char *buf, char *addr, unsigned long count);
371
372 /*
373  * This function reads the *virtual* memory as seen by the kernel.
374  */
375 static ssize_t read_kmem(struct file *file, char __user *buf, 
376                          size_t count, loff_t *ppos)
377 {
378         unsigned long p = *ppos;
379         ssize_t low_count, read, sz;
380         char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
381
382         return -EPERM;
383
384         read = 0;
385         if (p < (unsigned long) high_memory) {
386                 low_count = count;
387                 if (count > (unsigned long) high_memory - p)
388                         low_count = (unsigned long) high_memory - p;
389
390 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
391                 /* we don't have page 0 mapped on sparc and m68k.. */
392                 if (p < PAGE_SIZE && low_count > 0) {
393                         size_t tmp = PAGE_SIZE - p;
394                         if (tmp > low_count) tmp = low_count;
395                         if (clear_user(buf, tmp))
396                                 return -EFAULT;
397                         buf += tmp;
398                         p += tmp;
399                         read += tmp;
400                         low_count -= tmp;
401                         count -= tmp;
402                 }
403 #endif
404                 while (low_count > 0) {
405                         /*
406                          * Handle first page in case it's not aligned
407                          */
408                         if (-p & (PAGE_SIZE - 1))
409                                 sz = -p & (PAGE_SIZE - 1);
410                         else
411                                 sz = PAGE_SIZE;
412
413                         sz = min_t(unsigned long, sz, low_count);
414
415                         /*
416                          * On ia64 if a page has been mapped somewhere as
417                          * uncached, then it must also be accessed uncached
418                          * by the kernel or data corruption may occur
419                          */
420                         kbuf = xlate_dev_kmem_ptr((char *)p);
421
422                         if (copy_to_user(buf, kbuf, sz))
423                                 return -EFAULT;
424                         buf += sz;
425                         p += sz;
426                         read += sz;
427                         low_count -= sz;
428                         count -= sz;
429                 }
430         }
431
432         if (count > 0) {
433                 kbuf = (char *)__get_free_page(GFP_KERNEL);
434                 if (!kbuf)
435                         return -ENOMEM;
436                 while (count > 0) {
437                         int len = count;
438
439                         if (len > PAGE_SIZE)
440                                 len = PAGE_SIZE;
441                         len = vread(kbuf, (char *)p, len);
442                         if (!len)
443                                 break;
444                         if (copy_to_user(buf, kbuf, len)) {
445                                 free_page((unsigned long)kbuf);
446                                 return -EFAULT;
447                         }
448                         count -= len;
449                         buf += len;
450                         read += len;
451                         p += len;
452                 }
453                 free_page((unsigned long)kbuf);
454         }
455         *ppos = p;
456         return read;
457 }
458
459 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
460 static ssize_t read_port(struct file * file, char __user * buf,
461                          size_t count, loff_t *ppos)
462 {
463         unsigned long i = *ppos;
464         char __user *tmp = buf;
465
466         if (!access_ok(VERIFY_WRITE, buf, count))
467                 return -EFAULT; 
468         while (count-- > 0 && i < 65536) {
469                 if (__put_user(inb(i),tmp) < 0) 
470                         return -EFAULT;  
471                 i++;
472                 tmp++;
473         }
474         *ppos = i;
475         return tmp-buf;
476 }
477
478 static ssize_t write_port(struct file * file, const char __user * buf,
479                           size_t count, loff_t *ppos)
480 {
481         unsigned long i = *ppos;
482         const char __user * tmp = buf;
483
484         if (!access_ok(VERIFY_READ,buf,count))
485                 return -EFAULT;
486         while (count-- > 0 && i < 65536) {
487                 char c;
488                 if (__get_user(c, tmp)) {
489                         if (tmp > buf)
490                                 break;
491                         return -EFAULT; 
492                 }
493                 outb(c,i);
494                 i++;
495                 tmp++;
496         }
497         *ppos = i;
498         return tmp-buf;
499 }
500 #endif
501
502 static ssize_t read_null(struct file * file, char __user * buf,
503                          size_t count, loff_t *ppos)
504 {
505         return 0;
506 }
507
508 static ssize_t write_null(struct file * file, const char __user * buf,
509                           size_t count, loff_t *ppos)
510 {
511         return count;
512 }
513
514 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
515                         struct splice_desc *sd)
516 {
517         return sd->len;
518 }
519
520 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
521                                  loff_t *ppos, size_t len, unsigned int flags)
522 {
523         return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
524 }
525
526 #ifdef CONFIG_MMU
527 /*
528  * For fun, we are using the MMU for this.
529  */
530 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
531 {
532         struct mm_struct *mm;
533         struct vm_area_struct * vma;
534         unsigned long addr=(unsigned long)buf;
535
536         mm = current->mm;
537         /* Oops, this was forgotten before. -ben */
538         down_read(&mm->mmap_sem);
539
540         /* For private mappings, just map in zero pages. */
541         for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
542                 unsigned long count;
543
544                 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
545                         goto out_up;
546                 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
547                         break;
548                 count = vma->vm_end - addr;
549                 if (count > size)
550                         count = size;
551
552                 zap_page_range(vma, addr, count, NULL);
553                 if (zeromap_page_range(vma, addr, count, PAGE_COPY))
554                         break;
555
556                 size -= count;
557                 buf += count;
558                 addr += count;
559                 if (size == 0)
560                         goto out_up;
561         }
562
563         up_read(&mm->mmap_sem);
564         
565         /* The shared case is hard. Let's do the conventional zeroing. */ 
566         do {
567                 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
568                 if (unwritten)
569                         return size + unwritten - PAGE_SIZE;
570                 cond_resched();
571                 buf += PAGE_SIZE;
572                 size -= PAGE_SIZE;
573         } while (size);
574
575         return size;
576 out_up:
577         up_read(&mm->mmap_sem);
578         return size;
579 }
580
581 static ssize_t read_zero(struct file * file, char __user * buf, 
582                          size_t count, loff_t *ppos)
583 {
584         unsigned long left, unwritten, written = 0;
585
586         if (!count)
587                 return 0;
588
589         if (!access_ok(VERIFY_WRITE, buf, count))
590                 return -EFAULT;
591
592         left = count;
593
594         /* do we want to be clever? Arbitrary cut-off */
595         if (count >= PAGE_SIZE*4) {
596                 unsigned long partial;
597
598                 /* How much left of the page? */
599                 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
600                 unwritten = clear_user(buf, partial);
601                 written = partial - unwritten;
602                 if (unwritten)
603                         goto out;
604                 left -= partial;
605                 buf += partial;
606                 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
607                 written += (left & PAGE_MASK) - unwritten;
608                 if (unwritten)
609                         goto out;
610                 buf += left & PAGE_MASK;
611                 left &= ~PAGE_MASK;
612         }
613         unwritten = clear_user(buf, left);
614         written += left - unwritten;
615 out:
616         return written ? written : -EFAULT;
617 }
618
619 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
620 {
621         int err;
622
623         if (vma->vm_flags & VM_SHARED)
624                 return shmem_zero_setup(vma);
625         err = zeromap_page_range(vma, vma->vm_start,
626                         vma->vm_end - vma->vm_start, vma->vm_page_prot);
627         BUG_ON(err == -EEXIST);
628         return err;
629 }
630 #else /* CONFIG_MMU */
631 static ssize_t read_zero(struct file * file, char * buf, 
632                          size_t count, loff_t *ppos)
633 {
634         size_t todo = count;
635
636         while (todo) {
637                 size_t chunk = todo;
638
639                 if (chunk > 4096)
640                         chunk = 4096;   /* Just for latency reasons */
641                 if (clear_user(buf, chunk))
642                         return -EFAULT;
643                 buf += chunk;
644                 todo -= chunk;
645                 cond_resched();
646         }
647         return count;
648 }
649
650 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
651 {
652         return -ENOSYS;
653 }
654 #endif /* CONFIG_MMU */
655
656 static ssize_t write_full(struct file * file, const char __user * buf,
657                           size_t count, loff_t *ppos)
658 {
659         return -ENOSPC;
660 }
661
662 /*
663  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
664  * can fopen() both devices with "a" now.  This was previously impossible.
665  * -- SRB.
666  */
667
668 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
669 {
670         return file->f_pos = 0;
671 }
672
673 /*
674  * The memory devices use the full 32/64 bits of the offset, and so we cannot
675  * check against negative addresses: they are ok. The return value is weird,
676  * though, in that case (0).
677  *
678  * also note that seeking relative to the "end of file" isn't supported:
679  * it has no meaning, so it returns -EINVAL.
680  */
681 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
682 {
683         loff_t ret;
684
685         mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
686         switch (orig) {
687                 case 0:
688                         file->f_pos = offset;
689                         ret = file->f_pos;
690                         force_successful_syscall_return();
691                         break;
692                 case 1:
693                         file->f_pos += offset;
694                         ret = file->f_pos;
695                         force_successful_syscall_return();
696                         break;
697                 default:
698                         ret = -EINVAL;
699         }
700         mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
701         return ret;
702 }
703
704 static int open_port(struct inode * inode, struct file * filp)
705 {
706         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
707 }
708
709 #define zero_lseek      null_lseek
710 #define full_lseek      null_lseek
711 #define write_zero      write_null
712 #define read_full       read_zero
713 #define open_mem        open_port
714 #define open_kmem       open_mem
715 #define open_oldmem     open_mem
716
717 #ifndef ARCH_HAS_DEV_MEM
718 static const struct file_operations mem_fops = {
719         .llseek         = memory_lseek,
720         .read           = read_mem,
721         .write          = write_mem,
722         .mmap           = mmap_mem,
723         .open           = open_mem,
724         .get_unmapped_area = get_unmapped_area_mem,
725 };
726 #else
727 extern struct file_operations mem_fops;
728 #endif
729
730 static const struct file_operations kmem_fops = {
731         .llseek         = memory_lseek,
732         .read           = read_kmem,
733         .mmap           = mmap_kmem,
734         .open           = open_kmem,
735         .get_unmapped_area = get_unmapped_area_mem,
736 };
737
738 static const struct file_operations null_fops = {
739         .llseek         = null_lseek,
740         .read           = read_null,
741         .write          = write_null,
742         .splice_write   = splice_write_null,
743 };
744
745 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
746 static const struct file_operations port_fops = {
747         .llseek         = memory_lseek,
748         .read           = read_port,
749         .write          = write_port,
750         .open           = open_port,
751 };
752 #endif
753
754 static const struct file_operations zero_fops = {
755         .llseek         = zero_lseek,
756         .read           = read_zero,
757         .write          = write_zero,
758         .mmap           = mmap_zero,
759 };
760
761 /*
762  * capabilities for /dev/zero
763  * - permits private mappings, "copies" are taken of the source of zeros
764  */
765 static struct backing_dev_info zero_bdi = {
766         .capabilities   = BDI_CAP_MAP_COPY,
767 };
768
769 static const struct file_operations full_fops = {
770         .llseek         = full_lseek,
771         .read           = read_full,
772         .write          = write_full,
773 };
774
775 #ifdef CONFIG_CRASH_DUMP
776 static const struct file_operations oldmem_fops = {
777         .read   = read_oldmem,
778         .open   = open_oldmem,
779 };
780 #endif
781
782 static ssize_t kmsg_write(struct file * file, const char __user * buf,
783                           size_t count, loff_t *ppos)
784 {
785         char *tmp;
786         ssize_t ret;
787
788         tmp = kmalloc(count + 1, GFP_KERNEL);
789         if (tmp == NULL)
790                 return -ENOMEM;
791         ret = -EFAULT;
792         if (!copy_from_user(tmp, buf, count)) {
793                 tmp[count] = 0;
794                 ret = printk("%s", tmp);
795                 if (ret > count)
796                         /* printk can add a prefix */
797                         ret = count;
798         }
799         kfree(tmp);
800         return ret;
801 }
802
803 static const struct file_operations kmsg_fops = {
804         .write =        kmsg_write,
805 };
806
807 static int memory_open(struct inode * inode, struct file * filp)
808 {
809         switch (iminor(inode)) {
810                 case 1:
811                         filp->f_op = &mem_fops;
812                         filp->f_mapping->backing_dev_info =
813                                 &directly_mappable_cdev_bdi;
814                         break;
815                 case 2:
816                         filp->f_op = &kmem_fops;
817                         filp->f_mapping->backing_dev_info =
818                                 &directly_mappable_cdev_bdi;
819                         break;
820                 case 3:
821                         filp->f_op = &null_fops;
822                         break;
823 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
824                 case 4:
825                         filp->f_op = &port_fops;
826                         break;
827 #endif
828                 case 5:
829                         filp->f_mapping->backing_dev_info = &zero_bdi;
830                         filp->f_op = &zero_fops;
831                         break;
832                 case 7:
833                         filp->f_op = &full_fops;
834                         break;
835                 case 8:
836                         filp->f_op = &random_fops;
837                         break;
838                 case 9:
839                         filp->f_op = &urandom_fops;
840                         break;
841                 case 11:
842                         filp->f_op = &kmsg_fops;
843                         break;
844 #ifdef CONFIG_CRASH_DUMP
845                 case 12:
846                         filp->f_op = &oldmem_fops;
847                         break;
848 #endif
849                 default:
850                         return -ENXIO;
851         }
852         if (filp->f_op && filp->f_op->open)
853                 return filp->f_op->open(inode,filp);
854         return 0;
855 }
856
857 static const struct file_operations memory_fops = {
858         .open           = memory_open,  /* just a selector for the real open */
859 };
860
861 static const struct {
862         unsigned int            minor;
863         char                    *name;
864         umode_t                 mode;
865         const struct file_operations    *fops;
866 } devlist[] = { /* list of minor devices */
867         {1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
868         {3, "null",    S_IRUGO | S_IWUGO,           &null_fops},
869 #if (defined(CONFIG_ISA) || defined(CONFIG_PCI)) && !defined(__mc68000__)
870         {4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
871 #endif
872         {5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
873         {7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
874         {8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
875         {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops},
876         {11,"kmsg",    S_IRUGO | S_IWUSR,           &kmsg_fops},
877 #ifdef CONFIG_CRASH_DUMP
878         {12,"oldmem",    S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
879 #endif
880 };
881
882 static struct class *mem_class;
883
884 static int __init chr_dev_init(void)
885 {
886         int i;
887
888         if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
889                 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
890
891         mem_class = class_create(THIS_MODULE, "mem");
892         for (i = 0; i < ARRAY_SIZE(devlist); i++)
893                 device_create(mem_class, NULL,
894                               MKDEV(MEM_MAJOR, devlist[i].minor),
895                               devlist[i].name);
896
897         return 0;
898 }
899
900 fs_initcall(chr_dev_init);