Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux/fpc-iii.git] / drivers / char / mem.c
blob7b4e4de778e45f7900732a243f6d53f783089d32
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/drivers/char/mem.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Added devfs support.
8 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
9 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
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/ptrace.h>
23 #include <linux/device.h>
24 #include <linux/highmem.h>
25 #include <linux/backing-dev.h>
26 #include <linux/shmem_fs.h>
27 #include <linux/splice.h>
28 #include <linux/pfn.h>
29 #include <linux/export.h>
30 #include <linux/io.h>
31 #include <linux/uio.h>
33 #include <linux/uaccess.h>
35 #ifdef CONFIG_IA64
36 # include <linux/efi.h>
37 #endif
39 #define DEVPORT_MINOR 4
41 static inline unsigned long size_inside_page(unsigned long start,
42 unsigned long size)
44 unsigned long sz;
46 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
48 return min(sz, size);
51 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
52 static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
54 return addr + count <= __pa(high_memory);
57 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
59 return 1;
61 #endif
63 #ifdef CONFIG_STRICT_DEVMEM
64 static inline int page_is_allowed(unsigned long pfn)
66 return devmem_is_allowed(pfn);
68 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
70 u64 from = ((u64)pfn) << PAGE_SHIFT;
71 u64 to = from + size;
72 u64 cursor = from;
74 while (cursor < to) {
75 if (!devmem_is_allowed(pfn))
76 return 0;
77 cursor += PAGE_SIZE;
78 pfn++;
80 return 1;
82 #else
83 static inline int page_is_allowed(unsigned long pfn)
85 return 1;
87 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
89 return 1;
91 #endif
93 #ifndef unxlate_dev_mem_ptr
94 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
95 void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
98 #endif
101 * This funcion reads the *physical* memory. The f_pos points directly to the
102 * memory location.
104 static ssize_t read_mem(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
107 phys_addr_t p = *ppos;
108 ssize_t read, sz;
109 void *ptr;
110 char *bounce;
111 int err;
113 if (p != *ppos)
114 return 0;
116 if (!valid_phys_addr_range(p, count))
117 return -EFAULT;
118 read = 0;
119 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
120 /* we don't have page 0 mapped on sparc and m68k.. */
121 if (p < PAGE_SIZE) {
122 sz = size_inside_page(p, count);
123 if (sz > 0) {
124 if (clear_user(buf, sz))
125 return -EFAULT;
126 buf += sz;
127 p += sz;
128 count -= sz;
129 read += sz;
132 #endif
134 bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
135 if (!bounce)
136 return -ENOMEM;
138 while (count > 0) {
139 unsigned long remaining;
140 int allowed, probe;
142 sz = size_inside_page(p, count);
144 err = -EPERM;
145 allowed = page_is_allowed(p >> PAGE_SHIFT);
146 if (!allowed)
147 goto failed;
149 err = -EFAULT;
150 if (allowed == 2) {
151 /* Show zeros for restricted memory. */
152 remaining = clear_user(buf, sz);
153 } else {
155 * On ia64 if a page has been mapped somewhere as
156 * uncached, then it must also be accessed uncached
157 * by the kernel or data corruption may occur.
159 ptr = xlate_dev_mem_ptr(p);
160 if (!ptr)
161 goto failed;
163 probe = probe_kernel_read(bounce, ptr, sz);
164 unxlate_dev_mem_ptr(p, ptr);
165 if (probe)
166 goto failed;
168 remaining = copy_to_user(buf, bounce, sz);
171 if (remaining)
172 goto failed;
174 buf += sz;
175 p += sz;
176 count -= sz;
177 read += sz;
179 kfree(bounce);
181 *ppos += read;
182 return read;
184 failed:
185 kfree(bounce);
186 return err;
189 static ssize_t write_mem(struct file *file, const char __user *buf,
190 size_t count, loff_t *ppos)
192 phys_addr_t p = *ppos;
193 ssize_t written, sz;
194 unsigned long copied;
195 void *ptr;
197 if (p != *ppos)
198 return -EFBIG;
200 if (!valid_phys_addr_range(p, count))
201 return -EFAULT;
203 written = 0;
205 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
206 /* we don't have page 0 mapped on sparc and m68k.. */
207 if (p < PAGE_SIZE) {
208 sz = size_inside_page(p, count);
209 /* Hmm. Do something? */
210 buf += sz;
211 p += sz;
212 count -= sz;
213 written += sz;
215 #endif
217 while (count > 0) {
218 int allowed;
220 sz = size_inside_page(p, count);
222 allowed = page_is_allowed(p >> PAGE_SHIFT);
223 if (!allowed)
224 return -EPERM;
226 /* Skip actual writing when a page is marked as restricted. */
227 if (allowed == 1) {
229 * On ia64 if a page has been mapped somewhere as
230 * uncached, then it must also be accessed uncached
231 * by the kernel or data corruption may occur.
233 ptr = xlate_dev_mem_ptr(p);
234 if (!ptr) {
235 if (written)
236 break;
237 return -EFAULT;
240 copied = copy_from_user(ptr, buf, sz);
241 unxlate_dev_mem_ptr(p, ptr);
242 if (copied) {
243 written += sz - copied;
244 if (written)
245 break;
246 return -EFAULT;
250 buf += sz;
251 p += sz;
252 count -= sz;
253 written += sz;
256 *ppos += written;
257 return written;
260 int __weak phys_mem_access_prot_allowed(struct file *file,
261 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
263 return 1;
266 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
269 * Architectures vary in how they handle caching for addresses
270 * outside of main memory.
273 #ifdef pgprot_noncached
274 static int uncached_access(struct file *file, phys_addr_t addr)
276 #if defined(CONFIG_IA64)
278 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
279 * attribute aliases.
281 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
282 #elif defined(CONFIG_MIPS)
284 extern int __uncached_access(struct file *file,
285 unsigned long addr);
287 return __uncached_access(file, addr);
289 #else
291 * Accessing memory above the top the kernel knows about or through a
292 * file pointer
293 * that was marked O_DSYNC will be done non-cached.
295 if (file->f_flags & O_DSYNC)
296 return 1;
297 return addr >= __pa(high_memory);
298 #endif
300 #endif
302 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
303 unsigned long size, pgprot_t vma_prot)
305 #ifdef pgprot_noncached
306 phys_addr_t offset = pfn << PAGE_SHIFT;
308 if (uncached_access(file, offset))
309 return pgprot_noncached(vma_prot);
310 #endif
311 return vma_prot;
313 #endif
315 #ifndef CONFIG_MMU
316 static unsigned long get_unmapped_area_mem(struct file *file,
317 unsigned long addr,
318 unsigned long len,
319 unsigned long pgoff,
320 unsigned long flags)
322 if (!valid_mmap_phys_addr_range(pgoff, len))
323 return (unsigned long) -EINVAL;
324 return pgoff << PAGE_SHIFT;
327 /* permit direct mmap, for read, write or exec */
328 static unsigned memory_mmap_capabilities(struct file *file)
330 return NOMMU_MAP_DIRECT |
331 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
334 static unsigned zero_mmap_capabilities(struct file *file)
336 return NOMMU_MAP_COPY;
339 /* can't do an in-place private mapping if there's no MMU */
340 static inline int private_mapping_ok(struct vm_area_struct *vma)
342 return vma->vm_flags & VM_MAYSHARE;
344 #else
346 static inline int private_mapping_ok(struct vm_area_struct *vma)
348 return 1;
350 #endif
352 static const struct vm_operations_struct mmap_mem_ops = {
353 #ifdef CONFIG_HAVE_IOREMAP_PROT
354 .access = generic_access_phys
355 #endif
358 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
360 size_t size = vma->vm_end - vma->vm_start;
361 phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
363 /* Does it even fit in phys_addr_t? */
364 if (offset >> PAGE_SHIFT != vma->vm_pgoff)
365 return -EINVAL;
367 /* It's illegal to wrap around the end of the physical address space. */
368 if (offset + (phys_addr_t)size - 1 < offset)
369 return -EINVAL;
371 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
372 return -EINVAL;
374 if (!private_mapping_ok(vma))
375 return -ENOSYS;
377 if (!range_is_allowed(vma->vm_pgoff, size))
378 return -EPERM;
380 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
381 &vma->vm_page_prot))
382 return -EINVAL;
384 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
385 size,
386 vma->vm_page_prot);
388 vma->vm_ops = &mmap_mem_ops;
390 /* Remap-pfn-range will mark the range VM_IO */
391 if (remap_pfn_range(vma,
392 vma->vm_start,
393 vma->vm_pgoff,
394 size,
395 vma->vm_page_prot)) {
396 return -EAGAIN;
398 return 0;
401 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
403 unsigned long pfn;
405 /* Turn a kernel-virtual address into a physical page frame */
406 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
409 * RED-PEN: on some architectures there is more mapped memory than
410 * available in mem_map which pfn_valid checks for. Perhaps should add a
411 * new macro here.
413 * RED-PEN: vmalloc is not supported right now.
415 if (!pfn_valid(pfn))
416 return -EIO;
418 vma->vm_pgoff = pfn;
419 return mmap_mem(file, vma);
423 * This function reads the *virtual* memory as seen by the kernel.
425 static ssize_t read_kmem(struct file *file, char __user *buf,
426 size_t count, loff_t *ppos)
428 unsigned long p = *ppos;
429 ssize_t low_count, read, sz;
430 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
431 int err = 0;
433 read = 0;
434 if (p < (unsigned long) high_memory) {
435 low_count = count;
436 if (count > (unsigned long)high_memory - p)
437 low_count = (unsigned long)high_memory - p;
439 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
440 /* we don't have page 0 mapped on sparc and m68k.. */
441 if (p < PAGE_SIZE && low_count > 0) {
442 sz = size_inside_page(p, low_count);
443 if (clear_user(buf, sz))
444 return -EFAULT;
445 buf += sz;
446 p += sz;
447 read += sz;
448 low_count -= sz;
449 count -= sz;
451 #endif
452 while (low_count > 0) {
453 sz = size_inside_page(p, low_count);
456 * On ia64 if a page has been mapped somewhere as
457 * uncached, then it must also be accessed uncached
458 * by the kernel or data corruption may occur
460 kbuf = xlate_dev_kmem_ptr((void *)p);
461 if (!virt_addr_valid(kbuf))
462 return -ENXIO;
464 if (copy_to_user(buf, kbuf, sz))
465 return -EFAULT;
466 buf += sz;
467 p += sz;
468 read += sz;
469 low_count -= sz;
470 count -= sz;
474 if (count > 0) {
475 kbuf = (char *)__get_free_page(GFP_KERNEL);
476 if (!kbuf)
477 return -ENOMEM;
478 while (count > 0) {
479 sz = size_inside_page(p, count);
480 if (!is_vmalloc_or_module_addr((void *)p)) {
481 err = -ENXIO;
482 break;
484 sz = vread(kbuf, (char *)p, sz);
485 if (!sz)
486 break;
487 if (copy_to_user(buf, kbuf, sz)) {
488 err = -EFAULT;
489 break;
491 count -= sz;
492 buf += sz;
493 read += sz;
494 p += sz;
496 free_page((unsigned long)kbuf);
498 *ppos = p;
499 return read ? read : err;
503 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
504 size_t count, loff_t *ppos)
506 ssize_t written, sz;
507 unsigned long copied;
509 written = 0;
510 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
511 /* we don't have page 0 mapped on sparc and m68k.. */
512 if (p < PAGE_SIZE) {
513 sz = size_inside_page(p, count);
514 /* Hmm. Do something? */
515 buf += sz;
516 p += sz;
517 count -= sz;
518 written += sz;
520 #endif
522 while (count > 0) {
523 void *ptr;
525 sz = size_inside_page(p, count);
528 * On ia64 if a page has been mapped somewhere as uncached, then
529 * it must also be accessed uncached by the kernel or data
530 * corruption may occur.
532 ptr = xlate_dev_kmem_ptr((void *)p);
533 if (!virt_addr_valid(ptr))
534 return -ENXIO;
536 copied = copy_from_user(ptr, buf, sz);
537 if (copied) {
538 written += sz - copied;
539 if (written)
540 break;
541 return -EFAULT;
543 buf += sz;
544 p += sz;
545 count -= sz;
546 written += sz;
549 *ppos += written;
550 return written;
554 * This function writes to the *virtual* memory as seen by the kernel.
556 static ssize_t write_kmem(struct file *file, const char __user *buf,
557 size_t count, loff_t *ppos)
559 unsigned long p = *ppos;
560 ssize_t wrote = 0;
561 ssize_t virtr = 0;
562 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
563 int err = 0;
565 if (p < (unsigned long) high_memory) {
566 unsigned long to_write = min_t(unsigned long, count,
567 (unsigned long)high_memory - p);
568 wrote = do_write_kmem(p, buf, to_write, ppos);
569 if (wrote != to_write)
570 return wrote;
571 p += wrote;
572 buf += wrote;
573 count -= wrote;
576 if (count > 0) {
577 kbuf = (char *)__get_free_page(GFP_KERNEL);
578 if (!kbuf)
579 return wrote ? wrote : -ENOMEM;
580 while (count > 0) {
581 unsigned long sz = size_inside_page(p, count);
582 unsigned long n;
584 if (!is_vmalloc_or_module_addr((void *)p)) {
585 err = -ENXIO;
586 break;
588 n = copy_from_user(kbuf, buf, sz);
589 if (n) {
590 err = -EFAULT;
591 break;
593 vwrite(kbuf, (char *)p, sz);
594 count -= sz;
595 buf += sz;
596 virtr += sz;
597 p += sz;
599 free_page((unsigned long)kbuf);
602 *ppos = p;
603 return virtr + wrote ? : err;
606 static ssize_t read_port(struct file *file, char __user *buf,
607 size_t count, loff_t *ppos)
609 unsigned long i = *ppos;
610 char __user *tmp = buf;
612 if (!access_ok(VERIFY_WRITE, buf, count))
613 return -EFAULT;
614 while (count-- > 0 && i < 65536) {
615 if (__put_user(inb(i), tmp) < 0)
616 return -EFAULT;
617 i++;
618 tmp++;
620 *ppos = i;
621 return tmp-buf;
624 static ssize_t write_port(struct file *file, const char __user *buf,
625 size_t count, loff_t *ppos)
627 unsigned long i = *ppos;
628 const char __user *tmp = buf;
630 if (!access_ok(VERIFY_READ, buf, count))
631 return -EFAULT;
632 while (count-- > 0 && i < 65536) {
633 char c;
635 if (__get_user(c, tmp)) {
636 if (tmp > buf)
637 break;
638 return -EFAULT;
640 outb(c, i);
641 i++;
642 tmp++;
644 *ppos = i;
645 return tmp-buf;
648 static ssize_t read_null(struct file *file, char __user *buf,
649 size_t count, loff_t *ppos)
651 return 0;
654 static ssize_t write_null(struct file *file, const char __user *buf,
655 size_t count, loff_t *ppos)
657 return count;
660 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
662 return 0;
665 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
667 size_t count = iov_iter_count(from);
668 iov_iter_advance(from, count);
669 return count;
672 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
673 struct splice_desc *sd)
675 return sd->len;
678 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
679 loff_t *ppos, size_t len, unsigned int flags)
681 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
684 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
686 size_t written = 0;
688 while (iov_iter_count(iter)) {
689 size_t chunk = iov_iter_count(iter), n;
691 if (chunk > PAGE_SIZE)
692 chunk = PAGE_SIZE; /* Just for latency reasons */
693 n = iov_iter_zero(chunk, iter);
694 if (!n && iov_iter_count(iter))
695 return written ? written : -EFAULT;
696 written += n;
697 if (signal_pending(current))
698 return written ? written : -ERESTARTSYS;
699 cond_resched();
701 return written;
704 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
706 #ifndef CONFIG_MMU
707 return -ENOSYS;
708 #endif
709 if (vma->vm_flags & VM_SHARED)
710 return shmem_zero_setup(vma);
711 vma_set_anonymous(vma);
712 return 0;
715 static unsigned long get_unmapped_area_zero(struct file *file,
716 unsigned long addr, unsigned long len,
717 unsigned long pgoff, unsigned long flags)
719 #ifdef CONFIG_MMU
720 if (flags & MAP_SHARED) {
722 * mmap_zero() will call shmem_zero_setup() to create a file,
723 * so use shmem's get_unmapped_area in case it can be huge;
724 * and pass NULL for file as in mmap.c's get_unmapped_area(),
725 * so as not to confuse shmem with our handle on "/dev/zero".
727 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
730 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
731 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
732 #else
733 return -ENOSYS;
734 #endif
737 static ssize_t write_full(struct file *file, const char __user *buf,
738 size_t count, loff_t *ppos)
740 return -ENOSPC;
744 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
745 * can fopen() both devices with "a" now. This was previously impossible.
746 * -- SRB.
748 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
750 return file->f_pos = 0;
754 * The memory devices use the full 32/64 bits of the offset, and so we cannot
755 * check against negative addresses: they are ok. The return value is weird,
756 * though, in that case (0).
758 * also note that seeking relative to the "end of file" isn't supported:
759 * it has no meaning, so it returns -EINVAL.
761 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
763 loff_t ret;
765 inode_lock(file_inode(file));
766 switch (orig) {
767 case SEEK_CUR:
768 offset += file->f_pos;
769 /* fall through */
770 case SEEK_SET:
771 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
772 if ((unsigned long long)offset >= -MAX_ERRNO) {
773 ret = -EOVERFLOW;
774 break;
776 file->f_pos = offset;
777 ret = file->f_pos;
778 force_successful_syscall_return();
779 break;
780 default:
781 ret = -EINVAL;
783 inode_unlock(file_inode(file));
784 return ret;
787 static int open_port(struct inode *inode, struct file *filp)
789 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
792 #define zero_lseek null_lseek
793 #define full_lseek null_lseek
794 #define write_zero write_null
795 #define write_iter_zero write_iter_null
796 #define open_mem open_port
797 #define open_kmem open_mem
799 static const struct file_operations __maybe_unused mem_fops = {
800 .llseek = memory_lseek,
801 .read = read_mem,
802 .write = write_mem,
803 .mmap = mmap_mem,
804 .open = open_mem,
805 #ifndef CONFIG_MMU
806 .get_unmapped_area = get_unmapped_area_mem,
807 .mmap_capabilities = memory_mmap_capabilities,
808 #endif
811 static const struct file_operations __maybe_unused kmem_fops = {
812 .llseek = memory_lseek,
813 .read = read_kmem,
814 .write = write_kmem,
815 .mmap = mmap_kmem,
816 .open = open_kmem,
817 #ifndef CONFIG_MMU
818 .get_unmapped_area = get_unmapped_area_mem,
819 .mmap_capabilities = memory_mmap_capabilities,
820 #endif
823 static const struct file_operations null_fops = {
824 .llseek = null_lseek,
825 .read = read_null,
826 .write = write_null,
827 .read_iter = read_iter_null,
828 .write_iter = write_iter_null,
829 .splice_write = splice_write_null,
832 static const struct file_operations __maybe_unused port_fops = {
833 .llseek = memory_lseek,
834 .read = read_port,
835 .write = write_port,
836 .open = open_port,
839 static const struct file_operations zero_fops = {
840 .llseek = zero_lseek,
841 .write = write_zero,
842 .read_iter = read_iter_zero,
843 .write_iter = write_iter_zero,
844 .mmap = mmap_zero,
845 .get_unmapped_area = get_unmapped_area_zero,
846 #ifndef CONFIG_MMU
847 .mmap_capabilities = zero_mmap_capabilities,
848 #endif
851 static const struct file_operations full_fops = {
852 .llseek = full_lseek,
853 .read_iter = read_iter_zero,
854 .write = write_full,
857 static const struct memdev {
858 const char *name;
859 umode_t mode;
860 const struct file_operations *fops;
861 fmode_t fmode;
862 } devlist[] = {
863 #ifdef CONFIG_DEVMEM
864 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
865 #endif
866 #ifdef CONFIG_DEVKMEM
867 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
868 #endif
869 [3] = { "null", 0666, &null_fops, 0 },
870 #ifdef CONFIG_DEVPORT
871 [4] = { "port", 0, &port_fops, 0 },
872 #endif
873 [5] = { "zero", 0666, &zero_fops, 0 },
874 [7] = { "full", 0666, &full_fops, 0 },
875 [8] = { "random", 0666, &random_fops, 0 },
876 [9] = { "urandom", 0666, &urandom_fops, 0 },
877 #ifdef CONFIG_PRINTK
878 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
879 #endif
882 static int memory_open(struct inode *inode, struct file *filp)
884 int minor;
885 const struct memdev *dev;
887 minor = iminor(inode);
888 if (minor >= ARRAY_SIZE(devlist))
889 return -ENXIO;
891 dev = &devlist[minor];
892 if (!dev->fops)
893 return -ENXIO;
895 filp->f_op = dev->fops;
896 filp->f_mode |= dev->fmode;
898 if (dev->fops->open)
899 return dev->fops->open(inode, filp);
901 return 0;
904 static const struct file_operations memory_fops = {
905 .open = memory_open,
906 .llseek = noop_llseek,
909 static char *mem_devnode(struct device *dev, umode_t *mode)
911 if (mode && devlist[MINOR(dev->devt)].mode)
912 *mode = devlist[MINOR(dev->devt)].mode;
913 return NULL;
916 static struct class *mem_class;
918 static int __init chr_dev_init(void)
920 int minor;
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 if (IS_ERR(mem_class))
927 return PTR_ERR(mem_class);
929 mem_class->devnode = mem_devnode;
930 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
931 if (!devlist[minor].name)
932 continue;
935 * Create /dev/port?
937 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
938 continue;
940 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
941 NULL, devlist[minor].name);
944 return tty_init();
947 fs_initcall(chr_dev_init);