]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/char/mem.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[mirror_ubuntu-zesty-kernel.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 mmapping 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/ptrace.h>
22 #include <linux/device.h>
23 #include <linux/highmem.h>
24 #include <linux/backing-dev.h>
25 #include <linux/shmem_fs.h>
26 #include <linux/splice.h>
27 #include <linux/pfn.h>
28 #include <linux/export.h>
29 #include <linux/io.h>
30 #include <linux/uio.h>
31
32 #include <linux/uaccess.h>
33
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
36 #endif
37
38 #define DEVPORT_MINOR 4
39
40 static inline unsigned long size_inside_page(unsigned long start,
41 unsigned long size)
42 {
43 unsigned long sz;
44
45 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
46
47 return min(sz, size);
48 }
49
50 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
51 static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
52 {
53 return addr + count <= __pa(high_memory);
54 }
55
56 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
57 {
58 return 1;
59 }
60 #endif
61
62 #ifdef CONFIG_STRICT_DEVMEM
63 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
64 {
65 u64 from = ((u64)pfn) << PAGE_SHIFT;
66 u64 to = from + size;
67 u64 cursor = from;
68
69 while (cursor < to) {
70 if (!devmem_is_allowed(pfn))
71 return 0;
72 cursor += PAGE_SIZE;
73 pfn++;
74 }
75 return 1;
76 }
77 #else
78 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
79 {
80 return 1;
81 }
82 #endif
83
84 #ifndef unxlate_dev_mem_ptr
85 #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
86 void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
87 {
88 }
89 #endif
90
91 /*
92 * This funcion reads the *physical* memory. The f_pos points directly to the
93 * memory location.
94 */
95 static ssize_t read_mem(struct file *file, char __user *buf,
96 size_t count, loff_t *ppos)
97 {
98 phys_addr_t p = *ppos;
99 ssize_t read, sz;
100 void *ptr;
101
102 if (p != *ppos)
103 return 0;
104
105 if (!valid_phys_addr_range(p, count))
106 return -EFAULT;
107 read = 0;
108 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
109 /* we don't have page 0 mapped on sparc and m68k.. */
110 if (p < PAGE_SIZE) {
111 sz = size_inside_page(p, count);
112 if (sz > 0) {
113 if (clear_user(buf, sz))
114 return -EFAULT;
115 buf += sz;
116 p += sz;
117 count -= sz;
118 read += sz;
119 }
120 }
121 #endif
122
123 while (count > 0) {
124 unsigned long remaining;
125
126 sz = size_inside_page(p, count);
127
128 if (!range_is_allowed(p >> PAGE_SHIFT, count))
129 return -EPERM;
130
131 /*
132 * On ia64 if a page has been mapped somewhere as uncached, then
133 * it must also be accessed uncached by the kernel or data
134 * corruption may occur.
135 */
136 ptr = xlate_dev_mem_ptr(p);
137 if (!ptr)
138 return -EFAULT;
139
140 remaining = copy_to_user(buf, ptr, sz);
141 unxlate_dev_mem_ptr(p, ptr);
142 if (remaining)
143 return -EFAULT;
144
145 buf += sz;
146 p += sz;
147 count -= sz;
148 read += sz;
149 }
150
151 *ppos += read;
152 return read;
153 }
154
155 static ssize_t write_mem(struct file *file, const char __user *buf,
156 size_t count, loff_t *ppos)
157 {
158 phys_addr_t p = *ppos;
159 ssize_t written, sz;
160 unsigned long copied;
161 void *ptr;
162
163 if (p != *ppos)
164 return -EFBIG;
165
166 if (!valid_phys_addr_range(p, count))
167 return -EFAULT;
168
169 written = 0;
170
171 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
172 /* we don't have page 0 mapped on sparc and m68k.. */
173 if (p < PAGE_SIZE) {
174 sz = size_inside_page(p, count);
175 /* Hmm. Do something? */
176 buf += sz;
177 p += sz;
178 count -= sz;
179 written += sz;
180 }
181 #endif
182
183 while (count > 0) {
184 sz = size_inside_page(p, count);
185
186 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
187 return -EPERM;
188
189 /*
190 * On ia64 if a page has been mapped somewhere as uncached, then
191 * it must also be accessed uncached by the kernel or data
192 * corruption may occur.
193 */
194 ptr = xlate_dev_mem_ptr(p);
195 if (!ptr) {
196 if (written)
197 break;
198 return -EFAULT;
199 }
200
201 copied = copy_from_user(ptr, buf, sz);
202 unxlate_dev_mem_ptr(p, ptr);
203 if (copied) {
204 written += sz - copied;
205 if (written)
206 break;
207 return -EFAULT;
208 }
209
210 buf += sz;
211 p += sz;
212 count -= sz;
213 written += sz;
214 }
215
216 *ppos += written;
217 return written;
218 }
219
220 int __weak phys_mem_access_prot_allowed(struct file *file,
221 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
222 {
223 return 1;
224 }
225
226 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
227
228 /*
229 * Architectures vary in how they handle caching for addresses
230 * outside of main memory.
231 *
232 */
233 #ifdef pgprot_noncached
234 static int uncached_access(struct file *file, phys_addr_t addr)
235 {
236 #if defined(CONFIG_IA64)
237 /*
238 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
239 * attribute aliases.
240 */
241 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
242 #elif defined(CONFIG_MIPS)
243 {
244 extern int __uncached_access(struct file *file,
245 unsigned long addr);
246
247 return __uncached_access(file, addr);
248 }
249 #else
250 /*
251 * Accessing memory above the top the kernel knows about or through a
252 * file pointer
253 * that was marked O_DSYNC will be done non-cached.
254 */
255 if (file->f_flags & O_DSYNC)
256 return 1;
257 return addr >= __pa(high_memory);
258 #endif
259 }
260 #endif
261
262 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
263 unsigned long size, pgprot_t vma_prot)
264 {
265 #ifdef pgprot_noncached
266 phys_addr_t offset = pfn << PAGE_SHIFT;
267
268 if (uncached_access(file, offset))
269 return pgprot_noncached(vma_prot);
270 #endif
271 return vma_prot;
272 }
273 #endif
274
275 #ifndef CONFIG_MMU
276 static unsigned long get_unmapped_area_mem(struct file *file,
277 unsigned long addr,
278 unsigned long len,
279 unsigned long pgoff,
280 unsigned long flags)
281 {
282 if (!valid_mmap_phys_addr_range(pgoff, len))
283 return (unsigned long) -EINVAL;
284 return pgoff << PAGE_SHIFT;
285 }
286
287 /* permit direct mmap, for read, write or exec */
288 static unsigned memory_mmap_capabilities(struct file *file)
289 {
290 return NOMMU_MAP_DIRECT |
291 NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
292 }
293
294 static unsigned zero_mmap_capabilities(struct file *file)
295 {
296 return NOMMU_MAP_COPY;
297 }
298
299 /* can't do an in-place private mapping if there's no MMU */
300 static inline int private_mapping_ok(struct vm_area_struct *vma)
301 {
302 return vma->vm_flags & VM_MAYSHARE;
303 }
304 #else
305
306 static inline int private_mapping_ok(struct vm_area_struct *vma)
307 {
308 return 1;
309 }
310 #endif
311
312 static const struct vm_operations_struct mmap_mem_ops = {
313 #ifdef CONFIG_HAVE_IOREMAP_PROT
314 .access = generic_access_phys
315 #endif
316 };
317
318 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
319 {
320 size_t size = vma->vm_end - vma->vm_start;
321
322 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
323 return -EINVAL;
324
325 if (!private_mapping_ok(vma))
326 return -ENOSYS;
327
328 if (!range_is_allowed(vma->vm_pgoff, size))
329 return -EPERM;
330
331 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
332 &vma->vm_page_prot))
333 return -EINVAL;
334
335 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
336 size,
337 vma->vm_page_prot);
338
339 vma->vm_ops = &mmap_mem_ops;
340
341 /* Remap-pfn-range will mark the range VM_IO */
342 if (remap_pfn_range(vma,
343 vma->vm_start,
344 vma->vm_pgoff,
345 size,
346 vma->vm_page_prot)) {
347 return -EAGAIN;
348 }
349 return 0;
350 }
351
352 static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
353 {
354 unsigned long pfn;
355
356 /* Turn a kernel-virtual address into a physical page frame */
357 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
358
359 /*
360 * RED-PEN: on some architectures there is more mapped memory than
361 * available in mem_map which pfn_valid checks for. Perhaps should add a
362 * new macro here.
363 *
364 * RED-PEN: vmalloc is not supported right now.
365 */
366 if (!pfn_valid(pfn))
367 return -EIO;
368
369 vma->vm_pgoff = pfn;
370 return mmap_mem(file, vma);
371 }
372
373 /*
374 * This function reads the *virtual* memory as seen by the kernel.
375 */
376 static ssize_t read_kmem(struct file *file, char __user *buf,
377 size_t count, loff_t *ppos)
378 {
379 unsigned long p = *ppos;
380 ssize_t low_count, read, sz;
381 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
382 int err = 0;
383
384 if (!pfn_valid(PFN_DOWN(p)))
385 return -EIO;
386
387 read = 0;
388 if (p < (unsigned long) high_memory) {
389 low_count = count;
390 if (count > (unsigned long)high_memory - p)
391 low_count = (unsigned long)high_memory - p;
392
393 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
394 /* we don't have page 0 mapped on sparc and m68k.. */
395 if (p < PAGE_SIZE && low_count > 0) {
396 sz = size_inside_page(p, low_count);
397 if (clear_user(buf, sz))
398 return -EFAULT;
399 buf += sz;
400 p += sz;
401 read += sz;
402 low_count -= sz;
403 count -= sz;
404 }
405 #endif
406 while (low_count > 0) {
407 sz = size_inside_page(p, low_count);
408
409 /*
410 * On ia64 if a page has been mapped somewhere as
411 * uncached, then it must also be accessed uncached
412 * by the kernel or data corruption may occur
413 */
414 kbuf = xlate_dev_kmem_ptr((void *)p);
415
416 if (copy_to_user(buf, kbuf, sz))
417 return -EFAULT;
418 buf += sz;
419 p += sz;
420 read += sz;
421 low_count -= sz;
422 count -= sz;
423 }
424 }
425
426 if (count > 0) {
427 kbuf = (char *)__get_free_page(GFP_KERNEL);
428 if (!kbuf)
429 return -ENOMEM;
430 while (count > 0) {
431 sz = size_inside_page(p, count);
432 if (!is_vmalloc_or_module_addr((void *)p)) {
433 err = -ENXIO;
434 break;
435 }
436 sz = vread(kbuf, (char *)p, sz);
437 if (!sz)
438 break;
439 if (copy_to_user(buf, kbuf, sz)) {
440 err = -EFAULT;
441 break;
442 }
443 count -= sz;
444 buf += sz;
445 read += sz;
446 p += sz;
447 }
448 free_page((unsigned long)kbuf);
449 }
450 *ppos = p;
451 return read ? read : err;
452 }
453
454
455 static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
456 size_t count, loff_t *ppos)
457 {
458 ssize_t written, sz;
459 unsigned long copied;
460
461 written = 0;
462 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
463 /* we don't have page 0 mapped on sparc and m68k.. */
464 if (p < PAGE_SIZE) {
465 sz = size_inside_page(p, count);
466 /* Hmm. Do something? */
467 buf += sz;
468 p += sz;
469 count -= sz;
470 written += sz;
471 }
472 #endif
473
474 while (count > 0) {
475 void *ptr;
476
477 sz = size_inside_page(p, count);
478
479 /*
480 * On ia64 if a page has been mapped somewhere as uncached, then
481 * it must also be accessed uncached by the kernel or data
482 * corruption may occur.
483 */
484 ptr = xlate_dev_kmem_ptr((void *)p);
485
486 copied = copy_from_user(ptr, buf, sz);
487 if (copied) {
488 written += sz - copied;
489 if (written)
490 break;
491 return -EFAULT;
492 }
493 buf += sz;
494 p += sz;
495 count -= sz;
496 written += sz;
497 }
498
499 *ppos += written;
500 return written;
501 }
502
503 /*
504 * This function writes to the *virtual* memory as seen by the kernel.
505 */
506 static ssize_t write_kmem(struct file *file, const char __user *buf,
507 size_t count, loff_t *ppos)
508 {
509 unsigned long p = *ppos;
510 ssize_t wrote = 0;
511 ssize_t virtr = 0;
512 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
513 int err = 0;
514
515 if (!pfn_valid(PFN_DOWN(p)))
516 return -EIO;
517
518 if (p < (unsigned long) high_memory) {
519 unsigned long to_write = min_t(unsigned long, count,
520 (unsigned long)high_memory - p);
521 wrote = do_write_kmem(p, buf, to_write, ppos);
522 if (wrote != to_write)
523 return wrote;
524 p += wrote;
525 buf += wrote;
526 count -= wrote;
527 }
528
529 if (count > 0) {
530 kbuf = (char *)__get_free_page(GFP_KERNEL);
531 if (!kbuf)
532 return wrote ? wrote : -ENOMEM;
533 while (count > 0) {
534 unsigned long sz = size_inside_page(p, count);
535 unsigned long n;
536
537 if (!is_vmalloc_or_module_addr((void *)p)) {
538 err = -ENXIO;
539 break;
540 }
541 n = copy_from_user(kbuf, buf, sz);
542 if (n) {
543 err = -EFAULT;
544 break;
545 }
546 vwrite(kbuf, (char *)p, sz);
547 count -= sz;
548 buf += sz;
549 virtr += sz;
550 p += sz;
551 }
552 free_page((unsigned long)kbuf);
553 }
554
555 *ppos = p;
556 return virtr + wrote ? : err;
557 }
558
559 static ssize_t read_port(struct file *file, char __user *buf,
560 size_t count, loff_t *ppos)
561 {
562 unsigned long i = *ppos;
563 char __user *tmp = buf;
564
565 if (!access_ok(VERIFY_WRITE, buf, count))
566 return -EFAULT;
567 while (count-- > 0 && i < 65536) {
568 if (__put_user(inb(i), tmp) < 0)
569 return -EFAULT;
570 i++;
571 tmp++;
572 }
573 *ppos = i;
574 return tmp-buf;
575 }
576
577 static ssize_t write_port(struct file *file, const char __user *buf,
578 size_t count, loff_t *ppos)
579 {
580 unsigned long i = *ppos;
581 const char __user *tmp = buf;
582
583 if (!access_ok(VERIFY_READ, buf, count))
584 return -EFAULT;
585 while (count-- > 0 && i < 65536) {
586 char c;
587
588 if (__get_user(c, tmp)) {
589 if (tmp > buf)
590 break;
591 return -EFAULT;
592 }
593 outb(c, i);
594 i++;
595 tmp++;
596 }
597 *ppos = i;
598 return tmp-buf;
599 }
600
601 static ssize_t read_null(struct file *file, char __user *buf,
602 size_t count, loff_t *ppos)
603 {
604 return 0;
605 }
606
607 static ssize_t write_null(struct file *file, const char __user *buf,
608 size_t count, loff_t *ppos)
609 {
610 return count;
611 }
612
613 static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
614 {
615 return 0;
616 }
617
618 static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
619 {
620 size_t count = iov_iter_count(from);
621 iov_iter_advance(from, count);
622 return count;
623 }
624
625 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
626 struct splice_desc *sd)
627 {
628 return sd->len;
629 }
630
631 static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
632 loff_t *ppos, size_t len, unsigned int flags)
633 {
634 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
635 }
636
637 static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
638 {
639 size_t written = 0;
640
641 while (iov_iter_count(iter)) {
642 size_t chunk = iov_iter_count(iter), n;
643
644 if (chunk > PAGE_SIZE)
645 chunk = PAGE_SIZE; /* Just for latency reasons */
646 n = iov_iter_zero(chunk, iter);
647 if (!n && iov_iter_count(iter))
648 return written ? written : -EFAULT;
649 written += n;
650 if (signal_pending(current))
651 return written ? written : -ERESTARTSYS;
652 cond_resched();
653 }
654 return written;
655 }
656
657 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
658 {
659 #ifndef CONFIG_MMU
660 return -ENOSYS;
661 #endif
662 if (vma->vm_flags & VM_SHARED)
663 return shmem_zero_setup(vma);
664 return 0;
665 }
666
667 static unsigned long get_unmapped_area_zero(struct file *file,
668 unsigned long addr, unsigned long len,
669 unsigned long pgoff, unsigned long flags)
670 {
671 #ifdef CONFIG_MMU
672 if (flags & MAP_SHARED) {
673 /*
674 * mmap_zero() will call shmem_zero_setup() to create a file,
675 * so use shmem's get_unmapped_area in case it can be huge;
676 * and pass NULL for file as in mmap.c's get_unmapped_area(),
677 * so as not to confuse shmem with our handle on "/dev/zero".
678 */
679 return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
680 }
681
682 /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
683 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
684 #else
685 return -ENOSYS;
686 #endif
687 }
688
689 static ssize_t write_full(struct file *file, const char __user *buf,
690 size_t count, loff_t *ppos)
691 {
692 return -ENOSPC;
693 }
694
695 /*
696 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
697 * can fopen() both devices with "a" now. This was previously impossible.
698 * -- SRB.
699 */
700 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
701 {
702 return file->f_pos = 0;
703 }
704
705 /*
706 * The memory devices use the full 32/64 bits of the offset, and so we cannot
707 * check against negative addresses: they are ok. The return value is weird,
708 * though, in that case (0).
709 *
710 * also note that seeking relative to the "end of file" isn't supported:
711 * it has no meaning, so it returns -EINVAL.
712 */
713 static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
714 {
715 loff_t ret;
716
717 inode_lock(file_inode(file));
718 switch (orig) {
719 case SEEK_CUR:
720 offset += file->f_pos;
721 case SEEK_SET:
722 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
723 if ((unsigned long long)offset >= -MAX_ERRNO) {
724 ret = -EOVERFLOW;
725 break;
726 }
727 file->f_pos = offset;
728 ret = file->f_pos;
729 force_successful_syscall_return();
730 break;
731 default:
732 ret = -EINVAL;
733 }
734 inode_unlock(file_inode(file));
735 return ret;
736 }
737
738 static int open_port(struct inode *inode, struct file *filp)
739 {
740 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
741 }
742
743 #define zero_lseek null_lseek
744 #define full_lseek null_lseek
745 #define write_zero write_null
746 #define write_iter_zero write_iter_null
747 #define open_mem open_port
748 #define open_kmem open_mem
749
750 static const struct file_operations __maybe_unused mem_fops = {
751 .llseek = memory_lseek,
752 .read = read_mem,
753 .write = write_mem,
754 .mmap = mmap_mem,
755 .open = open_mem,
756 #ifndef CONFIG_MMU
757 .get_unmapped_area = get_unmapped_area_mem,
758 .mmap_capabilities = memory_mmap_capabilities,
759 #endif
760 };
761
762 static const struct file_operations __maybe_unused kmem_fops = {
763 .llseek = memory_lseek,
764 .read = read_kmem,
765 .write = write_kmem,
766 .mmap = mmap_kmem,
767 .open = open_kmem,
768 #ifndef CONFIG_MMU
769 .get_unmapped_area = get_unmapped_area_mem,
770 .mmap_capabilities = memory_mmap_capabilities,
771 #endif
772 };
773
774 static const struct file_operations null_fops = {
775 .llseek = null_lseek,
776 .read = read_null,
777 .write = write_null,
778 .read_iter = read_iter_null,
779 .write_iter = write_iter_null,
780 .splice_write = splice_write_null,
781 };
782
783 static const struct file_operations __maybe_unused port_fops = {
784 .llseek = memory_lseek,
785 .read = read_port,
786 .write = write_port,
787 .open = open_port,
788 };
789
790 static const struct file_operations zero_fops = {
791 .llseek = zero_lseek,
792 .write = write_zero,
793 .read_iter = read_iter_zero,
794 .write_iter = write_iter_zero,
795 .mmap = mmap_zero,
796 .get_unmapped_area = get_unmapped_area_zero,
797 #ifndef CONFIG_MMU
798 .mmap_capabilities = zero_mmap_capabilities,
799 #endif
800 };
801
802 static const struct file_operations full_fops = {
803 .llseek = full_lseek,
804 .read_iter = read_iter_zero,
805 .write = write_full,
806 };
807
808 static const struct memdev {
809 const char *name;
810 umode_t mode;
811 const struct file_operations *fops;
812 fmode_t fmode;
813 } devlist[] = {
814 #ifdef CONFIG_DEVMEM
815 [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
816 #endif
817 #ifdef CONFIG_DEVKMEM
818 [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
819 #endif
820 [3] = { "null", 0666, &null_fops, 0 },
821 #ifdef CONFIG_DEVPORT
822 [4] = { "port", 0, &port_fops, 0 },
823 #endif
824 [5] = { "zero", 0666, &zero_fops, 0 },
825 [7] = { "full", 0666, &full_fops, 0 },
826 [8] = { "random", 0666, &random_fops, 0 },
827 [9] = { "urandom", 0666, &urandom_fops, 0 },
828 #ifdef CONFIG_PRINTK
829 [11] = { "kmsg", 0644, &kmsg_fops, 0 },
830 #endif
831 };
832
833 static int memory_open(struct inode *inode, struct file *filp)
834 {
835 int minor;
836 const struct memdev *dev;
837
838 minor = iminor(inode);
839 if (minor >= ARRAY_SIZE(devlist))
840 return -ENXIO;
841
842 dev = &devlist[minor];
843 if (!dev->fops)
844 return -ENXIO;
845
846 filp->f_op = dev->fops;
847 filp->f_mode |= dev->fmode;
848
849 if (dev->fops->open)
850 return dev->fops->open(inode, filp);
851
852 return 0;
853 }
854
855 static const struct file_operations memory_fops = {
856 .open = memory_open,
857 .llseek = noop_llseek,
858 };
859
860 static char *mem_devnode(struct device *dev, umode_t *mode)
861 {
862 if (mode && devlist[MINOR(dev->devt)].mode)
863 *mode = devlist[MINOR(dev->devt)].mode;
864 return NULL;
865 }
866
867 static struct class *mem_class;
868
869 static int __init chr_dev_init(void)
870 {
871 int minor;
872
873 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
874 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
875
876 mem_class = class_create(THIS_MODULE, "mem");
877 if (IS_ERR(mem_class))
878 return PTR_ERR(mem_class);
879
880 mem_class->devnode = mem_devnode;
881 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
882 if (!devlist[minor].name)
883 continue;
884
885 /*
886 * Create /dev/port?
887 */
888 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
889 continue;
890
891 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
892 NULL, devlist[minor].name);
893 }
894
895 return tty_init();
896 }
897
898 fs_initcall(chr_dev_init);