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