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