drm: fix MGA on non AGP systems
[linux-2.6.22.y-op.git] / drivers / char / mem.c
blob850a78c9c4bc9c639dd5b2c5816a4c89e7568eb1
1 /*
2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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 */
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>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
36 #endif
38 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
39 extern void tapechar_init(void);
40 #endif
43 * Architectures vary in how they handle caching for addresses
44 * outside of main memory.
47 static inline int uncached_access(struct file *file, unsigned long addr)
49 #if defined(__i386__)
51 * On the PPro and successors, the MTRRs are used to set
52 * memory types for physical addresses outside main memory,
53 * so blindly setting PCD or PWT on those pages is wrong.
54 * For Pentiums and earlier, the surround logic should disable
55 * caching for the high addresses through the KEN pin, but
56 * we maintain the tradition of paranoia in this code.
58 if (file->f_flags & O_SYNC)
59 return 1;
60 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
61 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
62 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
63 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
64 && addr >= __pa(high_memory);
65 #elif defined(__x86_64__)
66 /*
67 * This is broken because it can generate memory type aliases,
68 * which can cause cache corruptions
69 * But it is only available for root and we have to be bug-to-bug
70 * compatible with i386.
72 if (file->f_flags & O_SYNC)
73 return 1;
74 /* same behaviour as i386. PAT always set to cached and MTRRs control the
75 caching behaviour.
76 Hopefully a full PAT implementation will fix that soon. */
77 return 0;
78 #elif defined(CONFIG_IA64)
80 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
82 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
83 #else
85 * Accessing memory above the top the kernel knows about or through a file pointer
86 * that was marked O_SYNC will be done non-cached.
88 if (file->f_flags & O_SYNC)
89 return 1;
90 return addr >= __pa(high_memory);
91 #endif
94 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
95 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
97 unsigned long end_mem;
99 end_mem = __pa(high_memory);
100 if (addr >= end_mem)
101 return 0;
103 if (*count > end_mem - addr)
104 *count = end_mem - addr;
106 return 1;
108 #endif
111 * This funcion reads the *physical* memory. The f_pos points directly to the
112 * memory location.
114 static ssize_t read_mem(struct file * file, char __user * buf,
115 size_t count, loff_t *ppos)
117 unsigned long p = *ppos;
118 ssize_t read, sz;
119 char *ptr;
121 if (!valid_phys_addr_range(p, &count))
122 return -EFAULT;
123 read = 0;
124 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
125 /* we don't have page 0 mapped on sparc and m68k.. */
126 if (p < PAGE_SIZE) {
127 sz = PAGE_SIZE - p;
128 if (sz > count)
129 sz = count;
130 if (sz > 0) {
131 if (clear_user(buf, sz))
132 return -EFAULT;
133 buf += sz;
134 p += sz;
135 count -= sz;
136 read += sz;
139 #endif
141 while (count > 0) {
143 * Handle first page in case it's not aligned
145 if (-p & (PAGE_SIZE - 1))
146 sz = -p & (PAGE_SIZE - 1);
147 else
148 sz = PAGE_SIZE;
150 sz = min_t(unsigned long, sz, count);
153 * On ia64 if a page has been mapped somewhere as
154 * uncached, then it must also be accessed uncached
155 * by the kernel or data corruption may occur
157 ptr = xlate_dev_mem_ptr(p);
159 if (copy_to_user(buf, ptr, sz))
160 return -EFAULT;
161 buf += sz;
162 p += sz;
163 count -= sz;
164 read += sz;
167 *ppos += read;
168 return read;
171 static ssize_t write_mem(struct file * file, const char __user * buf,
172 size_t count, loff_t *ppos)
174 unsigned long p = *ppos;
175 ssize_t written, sz;
176 unsigned long copied;
177 void *ptr;
179 if (!valid_phys_addr_range(p, &count))
180 return -EFAULT;
182 written = 0;
184 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
185 /* we don't have page 0 mapped on sparc and m68k.. */
186 if (p < PAGE_SIZE) {
187 unsigned long sz = PAGE_SIZE - p;
188 if (sz > count)
189 sz = count;
190 /* Hmm. Do something? */
191 buf += sz;
192 p += sz;
193 count -= sz;
194 written += sz;
196 #endif
198 while (count > 0) {
200 * Handle first page in case it's not aligned
202 if (-p & (PAGE_SIZE - 1))
203 sz = -p & (PAGE_SIZE - 1);
204 else
205 sz = PAGE_SIZE;
207 sz = min_t(unsigned long, sz, count);
210 * On ia64 if a page has been mapped somewhere as
211 * uncached, then it must also be accessed uncached
212 * by the kernel or data corruption may occur
214 ptr = xlate_dev_mem_ptr(p);
216 copied = copy_from_user(ptr, buf, sz);
217 if (copied) {
218 ssize_t ret;
220 ret = written + (sz - copied);
221 if (ret)
222 return ret;
223 return -EFAULT;
225 buf += sz;
226 p += sz;
227 count -= sz;
228 written += sz;
231 *ppos += written;
232 return written;
235 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
237 #if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
238 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
240 vma->vm_page_prot = phys_mem_access_prot(file, offset,
241 vma->vm_end - vma->vm_start,
242 vma->vm_page_prot);
243 #elif defined(pgprot_noncached)
244 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
245 int uncached;
247 uncached = uncached_access(file, offset);
248 if (uncached)
249 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
250 #endif
252 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
253 if (remap_pfn_range(vma,
254 vma->vm_start,
255 vma->vm_pgoff,
256 vma->vm_end-vma->vm_start,
257 vma->vm_page_prot))
258 return -EAGAIN;
259 return 0;
262 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
264 unsigned long pfn;
266 /* Turn a kernel-virtual address into a physical page frame */
267 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
270 * RED-PEN: on some architectures there is more mapped memory
271 * than available in mem_map which pfn_valid checks
272 * for. Perhaps should add a new macro here.
274 * RED-PEN: vmalloc is not supported right now.
276 if (!pfn_valid(pfn))
277 return -EIO;
279 vma->vm_pgoff = pfn;
280 return mmap_mem(file, vma);
283 #ifdef CONFIG_CRASH_DUMP
285 * Read memory corresponding to the old kernel.
287 static ssize_t read_oldmem(struct file *file, char __user *buf,
288 size_t count, loff_t *ppos)
290 unsigned long pfn, offset;
291 size_t read = 0, csize;
292 int rc = 0;
294 while (count) {
295 pfn = *ppos / PAGE_SIZE;
296 if (pfn > saved_max_pfn)
297 return read;
299 offset = (unsigned long)(*ppos % PAGE_SIZE);
300 if (count > PAGE_SIZE - offset)
301 csize = PAGE_SIZE - offset;
302 else
303 csize = count;
305 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
306 if (rc < 0)
307 return rc;
308 buf += csize;
309 *ppos += csize;
310 read += csize;
311 count -= csize;
313 return read;
315 #endif
317 extern long vread(char *buf, char *addr, unsigned long count);
318 extern long vwrite(char *buf, char *addr, unsigned long count);
321 * This function reads the *virtual* memory as seen by the kernel.
323 static ssize_t read_kmem(struct file *file, char __user *buf,
324 size_t count, loff_t *ppos)
326 unsigned long p = *ppos;
327 ssize_t low_count, read, sz;
328 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
330 read = 0;
331 if (p < (unsigned long) high_memory) {
332 low_count = count;
333 if (count > (unsigned long) high_memory - p)
334 low_count = (unsigned long) high_memory - p;
336 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
337 /* we don't have page 0 mapped on sparc and m68k.. */
338 if (p < PAGE_SIZE && low_count > 0) {
339 size_t tmp = PAGE_SIZE - p;
340 if (tmp > low_count) tmp = low_count;
341 if (clear_user(buf, tmp))
342 return -EFAULT;
343 buf += tmp;
344 p += tmp;
345 read += tmp;
346 low_count -= tmp;
347 count -= tmp;
349 #endif
350 while (low_count > 0) {
352 * Handle first page in case it's not aligned
354 if (-p & (PAGE_SIZE - 1))
355 sz = -p & (PAGE_SIZE - 1);
356 else
357 sz = PAGE_SIZE;
359 sz = min_t(unsigned long, sz, low_count);
362 * On ia64 if a page has been mapped somewhere as
363 * uncached, then it must also be accessed uncached
364 * by the kernel or data corruption may occur
366 kbuf = xlate_dev_kmem_ptr((char *)p);
368 if (copy_to_user(buf, kbuf, sz))
369 return -EFAULT;
370 buf += sz;
371 p += sz;
372 read += sz;
373 low_count -= sz;
374 count -= sz;
378 if (count > 0) {
379 kbuf = (char *)__get_free_page(GFP_KERNEL);
380 if (!kbuf)
381 return -ENOMEM;
382 while (count > 0) {
383 int len = count;
385 if (len > PAGE_SIZE)
386 len = PAGE_SIZE;
387 len = vread(kbuf, (char *)p, len);
388 if (!len)
389 break;
390 if (copy_to_user(buf, kbuf, len)) {
391 free_page((unsigned long)kbuf);
392 return -EFAULT;
394 count -= len;
395 buf += len;
396 read += len;
397 p += len;
399 free_page((unsigned long)kbuf);
401 *ppos = p;
402 return read;
406 static inline ssize_t
407 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
408 size_t count, loff_t *ppos)
410 ssize_t written, sz;
411 unsigned long copied;
413 written = 0;
414 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
415 /* we don't have page 0 mapped on sparc and m68k.. */
416 if (realp < PAGE_SIZE) {
417 unsigned long sz = PAGE_SIZE - realp;
418 if (sz > count)
419 sz = count;
420 /* Hmm. Do something? */
421 buf += sz;
422 p += sz;
423 realp += sz;
424 count -= sz;
425 written += sz;
427 #endif
429 while (count > 0) {
430 char *ptr;
432 * Handle first page in case it's not aligned
434 if (-realp & (PAGE_SIZE - 1))
435 sz = -realp & (PAGE_SIZE - 1);
436 else
437 sz = PAGE_SIZE;
439 sz = min_t(unsigned long, sz, count);
442 * On ia64 if a page has been mapped somewhere as
443 * uncached, then it must also be accessed uncached
444 * by the kernel or data corruption may occur
446 ptr = xlate_dev_kmem_ptr(p);
448 copied = copy_from_user(ptr, buf, sz);
449 if (copied) {
450 ssize_t ret;
452 ret = written + (sz - copied);
453 if (ret)
454 return ret;
455 return -EFAULT;
457 buf += sz;
458 p += sz;
459 realp += sz;
460 count -= sz;
461 written += sz;
464 *ppos += written;
465 return written;
470 * This function writes to the *virtual* memory as seen by the kernel.
472 static ssize_t write_kmem(struct file * file, const char __user * buf,
473 size_t count, loff_t *ppos)
475 unsigned long p = *ppos;
476 ssize_t wrote = 0;
477 ssize_t virtr = 0;
478 ssize_t written;
479 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
481 if (p < (unsigned long) high_memory) {
483 wrote = count;
484 if (count > (unsigned long) high_memory - p)
485 wrote = (unsigned long) high_memory - p;
487 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
488 if (written != wrote)
489 return written;
490 wrote = written;
491 p += wrote;
492 buf += wrote;
493 count -= wrote;
496 if (count > 0) {
497 kbuf = (char *)__get_free_page(GFP_KERNEL);
498 if (!kbuf)
499 return wrote ? wrote : -ENOMEM;
500 while (count > 0) {
501 int len = count;
503 if (len > PAGE_SIZE)
504 len = PAGE_SIZE;
505 if (len) {
506 written = copy_from_user(kbuf, buf, len);
507 if (written) {
508 ssize_t ret;
510 free_page((unsigned long)kbuf);
511 ret = wrote + virtr + (len - written);
512 return ret ? ret : -EFAULT;
515 len = vwrite(kbuf, (char *)p, len);
516 count -= len;
517 buf += len;
518 virtr += len;
519 p += len;
521 free_page((unsigned long)kbuf);
524 *ppos = p;
525 return virtr + wrote;
528 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
529 static ssize_t read_port(struct file * file, char __user * buf,
530 size_t count, loff_t *ppos)
532 unsigned long i = *ppos;
533 char __user *tmp = buf;
535 if (!access_ok(VERIFY_WRITE, buf, count))
536 return -EFAULT;
537 while (count-- > 0 && i < 65536) {
538 if (__put_user(inb(i),tmp) < 0)
539 return -EFAULT;
540 i++;
541 tmp++;
543 *ppos = i;
544 return tmp-buf;
547 static ssize_t write_port(struct file * file, const char __user * buf,
548 size_t count, loff_t *ppos)
550 unsigned long i = *ppos;
551 const char __user * tmp = buf;
553 if (!access_ok(VERIFY_READ,buf,count))
554 return -EFAULT;
555 while (count-- > 0 && i < 65536) {
556 char c;
557 if (__get_user(c, tmp))
558 return -EFAULT;
559 outb(c,i);
560 i++;
561 tmp++;
563 *ppos = i;
564 return tmp-buf;
566 #endif
568 static ssize_t read_null(struct file * file, char __user * buf,
569 size_t count, loff_t *ppos)
571 return 0;
574 static ssize_t write_null(struct file * file, const char __user * buf,
575 size_t count, loff_t *ppos)
577 return count;
580 #ifdef CONFIG_MMU
582 * For fun, we are using the MMU for this.
584 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
586 struct mm_struct *mm;
587 struct vm_area_struct * vma;
588 unsigned long addr=(unsigned long)buf;
590 mm = current->mm;
591 /* Oops, this was forgotten before. -ben */
592 down_read(&mm->mmap_sem);
594 /* For private mappings, just map in zero pages. */
595 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
596 unsigned long count;
598 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
599 goto out_up;
600 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
601 break;
602 count = vma->vm_end - addr;
603 if (count > size)
604 count = size;
606 zap_page_range(vma, addr, count, NULL);
607 zeromap_page_range(vma, addr, count, PAGE_COPY);
609 size -= count;
610 buf += count;
611 addr += count;
612 if (size == 0)
613 goto out_up;
616 up_read(&mm->mmap_sem);
618 /* The shared case is hard. Let's do the conventional zeroing. */
619 do {
620 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
621 if (unwritten)
622 return size + unwritten - PAGE_SIZE;
623 cond_resched();
624 buf += PAGE_SIZE;
625 size -= PAGE_SIZE;
626 } while (size);
628 return size;
629 out_up:
630 up_read(&mm->mmap_sem);
631 return size;
634 static ssize_t read_zero(struct file * file, char __user * buf,
635 size_t count, loff_t *ppos)
637 unsigned long left, unwritten, written = 0;
639 if (!count)
640 return 0;
642 if (!access_ok(VERIFY_WRITE, buf, count))
643 return -EFAULT;
645 left = count;
647 /* do we want to be clever? Arbitrary cut-off */
648 if (count >= PAGE_SIZE*4) {
649 unsigned long partial;
651 /* How much left of the page? */
652 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
653 unwritten = clear_user(buf, partial);
654 written = partial - unwritten;
655 if (unwritten)
656 goto out;
657 left -= partial;
658 buf += partial;
659 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
660 written += (left & PAGE_MASK) - unwritten;
661 if (unwritten)
662 goto out;
663 buf += left & PAGE_MASK;
664 left &= ~PAGE_MASK;
666 unwritten = clear_user(buf, left);
667 written += left - unwritten;
668 out:
669 return written ? written : -EFAULT;
672 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
674 if (vma->vm_flags & VM_SHARED)
675 return shmem_zero_setup(vma);
676 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
677 return -EAGAIN;
678 return 0;
680 #else /* CONFIG_MMU */
681 static ssize_t read_zero(struct file * file, char * buf,
682 size_t count, loff_t *ppos)
684 size_t todo = count;
686 while (todo) {
687 size_t chunk = todo;
689 if (chunk > 4096)
690 chunk = 4096; /* Just for latency reasons */
691 if (clear_user(buf, chunk))
692 return -EFAULT;
693 buf += chunk;
694 todo -= chunk;
695 cond_resched();
697 return count;
700 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
702 return -ENOSYS;
704 #endif /* CONFIG_MMU */
706 static ssize_t write_full(struct file * file, const char __user * buf,
707 size_t count, loff_t *ppos)
709 return -ENOSPC;
713 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
714 * can fopen() both devices with "a" now. This was previously impossible.
715 * -- SRB.
718 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
720 return file->f_pos = 0;
724 * The memory devices use the full 32/64 bits of the offset, and so we cannot
725 * check against negative addresses: they are ok. The return value is weird,
726 * though, in that case (0).
728 * also note that seeking relative to the "end of file" isn't supported:
729 * it has no meaning, so it returns -EINVAL.
731 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
733 loff_t ret;
735 down(&file->f_dentry->d_inode->i_sem);
736 switch (orig) {
737 case 0:
738 file->f_pos = offset;
739 ret = file->f_pos;
740 force_successful_syscall_return();
741 break;
742 case 1:
743 file->f_pos += offset;
744 ret = file->f_pos;
745 force_successful_syscall_return();
746 break;
747 default:
748 ret = -EINVAL;
750 up(&file->f_dentry->d_inode->i_sem);
751 return ret;
754 static int open_port(struct inode * inode, struct file * filp)
756 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
759 #define zero_lseek null_lseek
760 #define full_lseek null_lseek
761 #define write_zero write_null
762 #define read_full read_zero
763 #define open_mem open_port
764 #define open_kmem open_mem
765 #define open_oldmem open_mem
767 static struct file_operations mem_fops = {
768 .llseek = memory_lseek,
769 .read = read_mem,
770 .write = write_mem,
771 .mmap = mmap_mem,
772 .open = open_mem,
775 static struct file_operations kmem_fops = {
776 .llseek = memory_lseek,
777 .read = read_kmem,
778 .write = write_kmem,
779 .mmap = mmap_kmem,
780 .open = open_kmem,
783 static struct file_operations null_fops = {
784 .llseek = null_lseek,
785 .read = read_null,
786 .write = write_null,
789 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
790 static struct file_operations port_fops = {
791 .llseek = memory_lseek,
792 .read = read_port,
793 .write = write_port,
794 .open = open_port,
796 #endif
798 static struct file_operations zero_fops = {
799 .llseek = zero_lseek,
800 .read = read_zero,
801 .write = write_zero,
802 .mmap = mmap_zero,
805 static struct backing_dev_info zero_bdi = {
806 .capabilities = BDI_CAP_MAP_COPY,
809 static struct file_operations full_fops = {
810 .llseek = full_lseek,
811 .read = read_full,
812 .write = write_full,
815 #ifdef CONFIG_CRASH_DUMP
816 static struct file_operations oldmem_fops = {
817 .read = read_oldmem,
818 .open = open_oldmem,
820 #endif
822 static ssize_t kmsg_write(struct file * file, const char __user * buf,
823 size_t count, loff_t *ppos)
825 char *tmp;
826 int ret;
828 tmp = kmalloc(count + 1, GFP_KERNEL);
829 if (tmp == NULL)
830 return -ENOMEM;
831 ret = -EFAULT;
832 if (!copy_from_user(tmp, buf, count)) {
833 tmp[count] = 0;
834 ret = printk("%s", tmp);
836 kfree(tmp);
837 return ret;
840 static struct file_operations kmsg_fops = {
841 .write = kmsg_write,
844 static int memory_open(struct inode * inode, struct file * filp)
846 switch (iminor(inode)) {
847 case 1:
848 filp->f_op = &mem_fops;
849 break;
850 case 2:
851 filp->f_op = &kmem_fops;
852 break;
853 case 3:
854 filp->f_op = &null_fops;
855 break;
856 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
857 case 4:
858 filp->f_op = &port_fops;
859 break;
860 #endif
861 case 5:
862 filp->f_mapping->backing_dev_info = &zero_bdi;
863 filp->f_op = &zero_fops;
864 break;
865 case 7:
866 filp->f_op = &full_fops;
867 break;
868 case 8:
869 filp->f_op = &random_fops;
870 break;
871 case 9:
872 filp->f_op = &urandom_fops;
873 break;
874 case 11:
875 filp->f_op = &kmsg_fops;
876 break;
877 #ifdef CONFIG_CRASH_DUMP
878 case 12:
879 filp->f_op = &oldmem_fops;
880 break;
881 #endif
882 default:
883 return -ENXIO;
885 if (filp->f_op && filp->f_op->open)
886 return filp->f_op->open(inode,filp);
887 return 0;
890 static struct file_operations memory_fops = {
891 .open = memory_open, /* just a selector for the real open */
894 static const struct {
895 unsigned int minor;
896 char *name;
897 umode_t mode;
898 struct file_operations *fops;
899 } devlist[] = { /* list of minor devices */
900 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
901 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
902 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
903 #if (defined(CONFIG_ISA) || !defined(__mc68000__)) && (!defined(CONFIG_PPC_ISERIES) || defined(CONFIG_PCI))
904 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
905 #endif
906 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
907 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
908 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
909 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
910 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
911 #ifdef CONFIG_CRASH_DUMP
912 {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
913 #endif
916 static struct class *mem_class;
918 static int __init chr_dev_init(void)
920 int i;
922 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
923 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
925 mem_class = class_create(THIS_MODULE, "mem");
926 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
927 class_device_create(mem_class, MKDEV(MEM_MAJOR, devlist[i].minor),
928 NULL, devlist[i].name);
929 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
930 S_IFCHR | devlist[i].mode, devlist[i].name);
933 return 0;
936 fs_initcall(chr_dev_init);