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