V4L/DVB: v4l2-ioctl: integer overflow in video_usercopy()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / mem.c
blob1270f6452e9a8217c5831900d726af090f9a338d
1 /*
2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Added devfs support.
7 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8 * Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9 */
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/smp_lock.h>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
34 #ifdef CONFIG_IA64
35 # include <linux/efi.h>
36 #endif
38 static inline unsigned long size_inside_page(unsigned long start,
39 unsigned long size)
41 unsigned long sz;
43 if (-start & (PAGE_SIZE - 1))
44 sz = -start & (PAGE_SIZE - 1);
45 else
46 sz = PAGE_SIZE;
48 return min_t(unsigned long, sz, size);
52 * Architectures vary in how they handle caching for addresses
53 * outside of main memory.
56 static inline int uncached_access(struct file *file, unsigned long addr)
58 #if defined(CONFIG_IA64)
60 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
62 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
63 #elif defined(CONFIG_MIPS)
65 extern int __uncached_access(struct file *file,
66 unsigned long addr);
68 return __uncached_access(file, addr);
70 #else
72 * Accessing memory above the top the kernel knows about or through a file pointer
73 * that was marked O_SYNC will be done non-cached.
75 if (file->f_flags & O_SYNC)
76 return 1;
77 return addr >= __pa(high_memory);
78 #endif
81 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
82 static inline int valid_phys_addr_range(unsigned long addr, size_t count)
84 if (addr + count > __pa(high_memory))
85 return 0;
87 return 1;
90 static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
92 return 1;
94 #endif
96 #ifdef CONFIG_STRICT_DEVMEM
97 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
99 u64 from = ((u64)pfn) << PAGE_SHIFT;
100 u64 to = from + size;
101 u64 cursor = from;
103 while (cursor < to) {
104 if (!devmem_is_allowed(pfn)) {
105 printk(KERN_INFO
106 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
107 current->comm, from, to);
108 return 0;
110 cursor += PAGE_SIZE;
111 pfn++;
113 return 1;
115 #else
116 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
118 return 1;
120 #endif
122 void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
127 * This funcion reads the *physical* memory. The f_pos points directly to the
128 * memory location.
130 static ssize_t read_mem(struct file * file, char __user * buf,
131 size_t count, loff_t *ppos)
133 unsigned long p = *ppos;
134 ssize_t read, sz;
135 char *ptr;
137 if (!valid_phys_addr_range(p, count))
138 return -EFAULT;
139 read = 0;
140 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
141 /* we don't have page 0 mapped on sparc and m68k.. */
142 if (p < PAGE_SIZE) {
143 sz = PAGE_SIZE - p;
144 if (sz > count)
145 sz = count;
146 if (sz > 0) {
147 if (clear_user(buf, sz))
148 return -EFAULT;
149 buf += sz;
150 p += sz;
151 count -= sz;
152 read += sz;
155 #endif
157 while (count > 0) {
159 * Handle first page in case it's not aligned
161 if (-p & (PAGE_SIZE - 1))
162 sz = -p & (PAGE_SIZE - 1);
163 else
164 sz = PAGE_SIZE;
166 sz = min_t(unsigned long, sz, count);
168 if (!range_is_allowed(p >> PAGE_SHIFT, count))
169 return -EPERM;
172 * On ia64 if a page has been mapped somewhere as
173 * uncached, then it must also be accessed uncached
174 * by the kernel or data corruption may occur
176 ptr = xlate_dev_mem_ptr(p);
177 if (!ptr)
178 return -EFAULT;
180 if (copy_to_user(buf, ptr, sz)) {
181 unxlate_dev_mem_ptr(p, ptr);
182 return -EFAULT;
185 unxlate_dev_mem_ptr(p, ptr);
187 buf += sz;
188 p += sz;
189 count -= sz;
190 read += sz;
193 *ppos += read;
194 return read;
197 static ssize_t write_mem(struct file * file, const char __user * buf,
198 size_t count, loff_t *ppos)
200 unsigned long p = *ppos;
201 ssize_t written, sz;
202 unsigned long copied;
203 void *ptr;
205 if (!valid_phys_addr_range(p, count))
206 return -EFAULT;
208 written = 0;
210 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
211 /* we don't have page 0 mapped on sparc and m68k.. */
212 if (p < PAGE_SIZE) {
213 unsigned long sz = PAGE_SIZE - p;
214 if (sz > count)
215 sz = count;
216 /* Hmm. Do something? */
217 buf += sz;
218 p += sz;
219 count -= sz;
220 written += sz;
222 #endif
224 while (count > 0) {
226 * Handle first page in case it's not aligned
228 if (-p & (PAGE_SIZE - 1))
229 sz = -p & (PAGE_SIZE - 1);
230 else
231 sz = PAGE_SIZE;
233 sz = min_t(unsigned long, sz, count);
235 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
236 return -EPERM;
239 * On ia64 if a page has been mapped somewhere as
240 * uncached, then it must also be accessed uncached
241 * by the kernel or data corruption may occur
243 ptr = xlate_dev_mem_ptr(p);
244 if (!ptr) {
245 if (written)
246 break;
247 return -EFAULT;
250 copied = copy_from_user(ptr, buf, sz);
251 if (copied) {
252 written += sz - copied;
253 unxlate_dev_mem_ptr(p, ptr);
254 if (written)
255 break;
256 return -EFAULT;
259 unxlate_dev_mem_ptr(p, ptr);
261 buf += sz;
262 p += sz;
263 count -= sz;
264 written += sz;
267 *ppos += written;
268 return written;
271 int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
272 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
274 return 1;
277 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
278 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
279 unsigned long size, pgprot_t vma_prot)
281 #ifdef pgprot_noncached
282 unsigned long offset = pfn << PAGE_SHIFT;
284 if (uncached_access(file, offset))
285 return pgprot_noncached(vma_prot);
286 #endif
287 return vma_prot;
289 #endif
291 #ifndef CONFIG_MMU
292 static unsigned long get_unmapped_area_mem(struct file *file,
293 unsigned long addr,
294 unsigned long len,
295 unsigned long pgoff,
296 unsigned long flags)
298 if (!valid_mmap_phys_addr_range(pgoff, len))
299 return (unsigned long) -EINVAL;
300 return pgoff << PAGE_SHIFT;
303 /* can't do an in-place private mapping if there's no MMU */
304 static inline int private_mapping_ok(struct vm_area_struct *vma)
306 return vma->vm_flags & VM_MAYSHARE;
308 #else
309 #define get_unmapped_area_mem NULL
311 static inline int private_mapping_ok(struct vm_area_struct *vma)
313 return 1;
315 #endif
317 static const struct vm_operations_struct mmap_mem_ops = {
318 #ifdef CONFIG_HAVE_IOREMAP_PROT
319 .access = generic_access_phys
320 #endif
323 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
325 size_t size = vma->vm_end - vma->vm_start;
327 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
328 return -EINVAL;
330 if (!private_mapping_ok(vma))
331 return -ENOSYS;
333 if (!range_is_allowed(vma->vm_pgoff, size))
334 return -EPERM;
336 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
337 &vma->vm_page_prot))
338 return -EINVAL;
340 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
341 size,
342 vma->vm_page_prot);
344 vma->vm_ops = &mmap_mem_ops;
346 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
347 if (remap_pfn_range(vma,
348 vma->vm_start,
349 vma->vm_pgoff,
350 size,
351 vma->vm_page_prot)) {
352 return -EAGAIN;
354 return 0;
357 #ifdef CONFIG_DEVKMEM
358 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
360 unsigned long pfn;
362 /* Turn a kernel-virtual address into a physical page frame */
363 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
366 * RED-PEN: on some architectures there is more mapped memory
367 * than available in mem_map which pfn_valid checks
368 * for. Perhaps should add a new macro here.
370 * RED-PEN: vmalloc is not supported right now.
372 if (!pfn_valid(pfn))
373 return -EIO;
375 vma->vm_pgoff = pfn;
376 return mmap_mem(file, vma);
378 #endif
380 #ifdef CONFIG_CRASH_DUMP
382 * Read memory corresponding to the old kernel.
384 static ssize_t read_oldmem(struct file *file, char __user *buf,
385 size_t count, loff_t *ppos)
387 unsigned long pfn, offset;
388 size_t read = 0, csize;
389 int rc = 0;
391 while (count) {
392 pfn = *ppos / PAGE_SIZE;
393 if (pfn > saved_max_pfn)
394 return read;
396 offset = (unsigned long)(*ppos % PAGE_SIZE);
397 if (count > PAGE_SIZE - offset)
398 csize = PAGE_SIZE - offset;
399 else
400 csize = count;
402 rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
403 if (rc < 0)
404 return rc;
405 buf += csize;
406 *ppos += csize;
407 read += csize;
408 count -= csize;
410 return read;
412 #endif
414 #ifdef CONFIG_DEVKMEM
416 * This function reads the *virtual* memory as seen by the kernel.
418 static ssize_t read_kmem(struct file *file, char __user *buf,
419 size_t count, loff_t *ppos)
421 unsigned long p = *ppos;
422 ssize_t low_count, read, sz;
423 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
424 int err = 0;
426 read = 0;
427 if (p < (unsigned long) high_memory) {
428 low_count = count;
429 if (count > (unsigned long) high_memory - p)
430 low_count = (unsigned long) high_memory - p;
432 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
433 /* we don't have page 0 mapped on sparc and m68k.. */
434 if (p < PAGE_SIZE && low_count > 0) {
435 size_t tmp = PAGE_SIZE - p;
436 if (tmp > low_count) tmp = low_count;
437 if (clear_user(buf, tmp))
438 return -EFAULT;
439 buf += tmp;
440 p += tmp;
441 read += tmp;
442 low_count -= tmp;
443 count -= tmp;
445 #endif
446 while (low_count > 0) {
447 sz = size_inside_page(p, low_count);
450 * On ia64 if a page has been mapped somewhere as
451 * uncached, then it must also be accessed uncached
452 * by the kernel or data corruption may occur
454 kbuf = xlate_dev_kmem_ptr((char *)p);
456 if (copy_to_user(buf, kbuf, sz))
457 return -EFAULT;
458 buf += sz;
459 p += sz;
460 read += sz;
461 low_count -= sz;
462 count -= sz;
466 if (count > 0) {
467 kbuf = (char *)__get_free_page(GFP_KERNEL);
468 if (!kbuf)
469 return -ENOMEM;
470 while (count > 0) {
471 int len = size_inside_page(p, count);
473 if (!is_vmalloc_or_module_addr((void *)p)) {
474 err = -ENXIO;
475 break;
477 len = vread(kbuf, (char *)p, len);
478 if (!len)
479 break;
480 if (copy_to_user(buf, kbuf, len)) {
481 err = -EFAULT;
482 break;
484 count -= len;
485 buf += len;
486 read += len;
487 p += len;
489 free_page((unsigned long)kbuf);
491 *ppos = p;
492 return read ? read : err;
496 static inline ssize_t
497 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
498 size_t count, loff_t *ppos)
500 ssize_t written, sz;
501 unsigned long copied;
503 written = 0;
504 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
505 /* we don't have page 0 mapped on sparc and m68k.. */
506 if (realp < PAGE_SIZE) {
507 unsigned long sz = PAGE_SIZE - realp;
508 if (sz > count)
509 sz = count;
510 /* Hmm. Do something? */
511 buf += sz;
512 p += sz;
513 realp += sz;
514 count -= sz;
515 written += sz;
517 #endif
519 while (count > 0) {
520 char *ptr;
522 sz = size_inside_page(realp, count);
525 * On ia64 if a page has been mapped somewhere as
526 * uncached, then it must also be accessed uncached
527 * by the kernel or data corruption may occur
529 ptr = xlate_dev_kmem_ptr(p);
531 copied = copy_from_user(ptr, buf, sz);
532 if (copied) {
533 written += sz - copied;
534 if (written)
535 break;
536 return -EFAULT;
538 buf += sz;
539 p += sz;
540 realp += sz;
541 count -= sz;
542 written += sz;
545 *ppos += written;
546 return written;
551 * This function writes to the *virtual* memory as seen by the kernel.
553 static ssize_t write_kmem(struct file * file, const char __user * buf,
554 size_t count, loff_t *ppos)
556 unsigned long p = *ppos;
557 ssize_t wrote = 0;
558 ssize_t virtr = 0;
559 ssize_t written;
560 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
561 int err = 0;
563 if (p < (unsigned long) high_memory) {
565 wrote = count;
566 if (count > (unsigned long) high_memory - p)
567 wrote = (unsigned long) high_memory - p;
569 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
570 if (written != wrote)
571 return written;
572 wrote = written;
573 p += wrote;
574 buf += wrote;
575 count -= wrote;
578 if (count > 0) {
579 kbuf = (char *)__get_free_page(GFP_KERNEL);
580 if (!kbuf)
581 return wrote ? wrote : -ENOMEM;
582 while (count > 0) {
583 int len = size_inside_page(p, count);
585 if (!is_vmalloc_or_module_addr((void *)p)) {
586 err = -ENXIO;
587 break;
589 if (len) {
590 written = copy_from_user(kbuf, buf, len);
591 if (written) {
592 err = -EFAULT;
593 break;
596 vwrite(kbuf, (char *)p, len);
597 count -= len;
598 buf += len;
599 virtr += len;
600 p += len;
602 free_page((unsigned long)kbuf);
605 *ppos = p;
606 return virtr + wrote ? : err;
608 #endif
610 #ifdef CONFIG_DEVPORT
611 static ssize_t read_port(struct file * file, char __user * buf,
612 size_t count, loff_t *ppos)
614 unsigned long i = *ppos;
615 char __user *tmp = buf;
617 if (!access_ok(VERIFY_WRITE, buf, count))
618 return -EFAULT;
619 while (count-- > 0 && i < 65536) {
620 if (__put_user(inb(i),tmp) < 0)
621 return -EFAULT;
622 i++;
623 tmp++;
625 *ppos = i;
626 return tmp-buf;
629 static ssize_t write_port(struct file * file, const char __user * buf,
630 size_t count, loff_t *ppos)
632 unsigned long i = *ppos;
633 const char __user * tmp = buf;
635 if (!access_ok(VERIFY_READ,buf,count))
636 return -EFAULT;
637 while (count-- > 0 && i < 65536) {
638 char c;
639 if (__get_user(c, tmp)) {
640 if (tmp > buf)
641 break;
642 return -EFAULT;
644 outb(c,i);
645 i++;
646 tmp++;
648 *ppos = i;
649 return tmp-buf;
651 #endif
653 static ssize_t read_null(struct file * file, char __user * buf,
654 size_t count, loff_t *ppos)
656 return 0;
659 static ssize_t write_null(struct file * file, const char __user * buf,
660 size_t count, loff_t *ppos)
662 return count;
665 static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
666 struct splice_desc *sd)
668 return sd->len;
671 static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
672 loff_t *ppos, size_t len, unsigned int flags)
674 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
677 static ssize_t read_zero(struct file * file, char __user * buf,
678 size_t count, loff_t *ppos)
680 size_t written;
682 if (!count)
683 return 0;
685 if (!access_ok(VERIFY_WRITE, buf, count))
686 return -EFAULT;
688 written = 0;
689 while (count) {
690 unsigned long unwritten;
691 size_t chunk = count;
693 if (chunk > PAGE_SIZE)
694 chunk = PAGE_SIZE; /* Just for latency reasons */
695 unwritten = __clear_user(buf, chunk);
696 written += chunk - unwritten;
697 if (unwritten)
698 break;
699 if (signal_pending(current))
700 return written ? written : -ERESTARTSYS;
701 buf += chunk;
702 count -= chunk;
703 cond_resched();
705 return written ? written : -EFAULT;
708 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
710 #ifndef CONFIG_MMU
711 return -ENOSYS;
712 #endif
713 if (vma->vm_flags & VM_SHARED)
714 return shmem_zero_setup(vma);
715 return 0;
718 static ssize_t write_full(struct file * file, const char __user * buf,
719 size_t count, loff_t *ppos)
721 return -ENOSPC;
725 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
726 * can fopen() both devices with "a" now. This was previously impossible.
727 * -- SRB.
730 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
732 return file->f_pos = 0;
736 * The memory devices use the full 32/64 bits of the offset, and so we cannot
737 * check against negative addresses: they are ok. The return value is weird,
738 * though, in that case (0).
740 * also note that seeking relative to the "end of file" isn't supported:
741 * it has no meaning, so it returns -EINVAL.
743 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
745 loff_t ret;
747 mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
748 switch (orig) {
749 case 0:
750 file->f_pos = offset;
751 ret = file->f_pos;
752 force_successful_syscall_return();
753 break;
754 case 1:
755 file->f_pos += offset;
756 ret = file->f_pos;
757 force_successful_syscall_return();
758 break;
759 default:
760 ret = -EINVAL;
762 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
763 return ret;
766 static int open_port(struct inode * inode, struct file * filp)
768 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
771 #define zero_lseek null_lseek
772 #define full_lseek null_lseek
773 #define write_zero write_null
774 #define read_full read_zero
775 #define open_mem open_port
776 #define open_kmem open_mem
777 #define open_oldmem open_mem
779 static const struct file_operations mem_fops = {
780 .llseek = memory_lseek,
781 .read = read_mem,
782 .write = write_mem,
783 .mmap = mmap_mem,
784 .open = open_mem,
785 .get_unmapped_area = get_unmapped_area_mem,
788 #ifdef CONFIG_DEVKMEM
789 static const struct file_operations kmem_fops = {
790 .llseek = memory_lseek,
791 .read = read_kmem,
792 .write = write_kmem,
793 .mmap = mmap_kmem,
794 .open = open_kmem,
795 .get_unmapped_area = get_unmapped_area_mem,
797 #endif
799 static const struct file_operations null_fops = {
800 .llseek = null_lseek,
801 .read = read_null,
802 .write = write_null,
803 .splice_write = splice_write_null,
806 #ifdef CONFIG_DEVPORT
807 static const struct file_operations port_fops = {
808 .llseek = memory_lseek,
809 .read = read_port,
810 .write = write_port,
811 .open = open_port,
813 #endif
815 static const struct file_operations zero_fops = {
816 .llseek = zero_lseek,
817 .read = read_zero,
818 .write = write_zero,
819 .mmap = mmap_zero,
823 * capabilities for /dev/zero
824 * - permits private mappings, "copies" are taken of the source of zeros
825 * - no writeback happens
827 static struct backing_dev_info zero_bdi = {
828 .name = "char/mem",
829 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
832 static const struct file_operations full_fops = {
833 .llseek = full_lseek,
834 .read = read_full,
835 .write = write_full,
838 #ifdef CONFIG_CRASH_DUMP
839 static const struct file_operations oldmem_fops = {
840 .read = read_oldmem,
841 .open = open_oldmem,
843 #endif
845 static ssize_t kmsg_write(struct file * file, const char __user * buf,
846 size_t count, loff_t *ppos)
848 char *tmp;
849 ssize_t ret;
851 tmp = kmalloc(count + 1, GFP_KERNEL);
852 if (tmp == NULL)
853 return -ENOMEM;
854 ret = -EFAULT;
855 if (!copy_from_user(tmp, buf, count)) {
856 tmp[count] = 0;
857 ret = printk("%s", tmp);
858 if (ret > count)
859 /* printk can add a prefix */
860 ret = count;
862 kfree(tmp);
863 return ret;
866 static const struct file_operations kmsg_fops = {
867 .write = kmsg_write,
870 static const struct memdev {
871 const char *name;
872 mode_t mode;
873 const struct file_operations *fops;
874 struct backing_dev_info *dev_info;
875 } devlist[] = {
876 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
877 #ifdef CONFIG_DEVKMEM
878 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
879 #endif
880 [3] = { "null", 0666, &null_fops, NULL },
881 #ifdef CONFIG_DEVPORT
882 [4] = { "port", 0, &port_fops, NULL },
883 #endif
884 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
885 [7] = { "full", 0666, &full_fops, NULL },
886 [8] = { "random", 0666, &random_fops, NULL },
887 [9] = { "urandom", 0666, &urandom_fops, NULL },
888 [11] = { "kmsg", 0, &kmsg_fops, NULL },
889 #ifdef CONFIG_CRASH_DUMP
890 [12] = { "oldmem", 0, &oldmem_fops, NULL },
891 #endif
894 static int memory_open(struct inode *inode, struct file *filp)
896 int minor;
897 const struct memdev *dev;
898 int ret = -ENXIO;
900 lock_kernel();
902 minor = iminor(inode);
903 if (minor >= ARRAY_SIZE(devlist))
904 goto out;
906 dev = &devlist[minor];
907 if (!dev->fops)
908 goto out;
910 filp->f_op = dev->fops;
911 if (dev->dev_info)
912 filp->f_mapping->backing_dev_info = dev->dev_info;
914 if (dev->fops->open)
915 ret = dev->fops->open(inode, filp);
916 else
917 ret = 0;
918 out:
919 unlock_kernel();
920 return ret;
923 static const struct file_operations memory_fops = {
924 .open = memory_open,
927 static char *mem_devnode(struct device *dev, mode_t *mode)
929 if (mode && devlist[MINOR(dev->devt)].mode)
930 *mode = devlist[MINOR(dev->devt)].mode;
931 return NULL;
934 static struct class *mem_class;
936 static int __init chr_dev_init(void)
938 int minor;
939 int err;
941 err = bdi_init(&zero_bdi);
942 if (err)
943 return err;
945 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
946 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
948 mem_class = class_create(THIS_MODULE, "mem");
949 mem_class->devnode = mem_devnode;
950 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
951 if (!devlist[minor].name)
952 continue;
953 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
954 NULL, devlist[minor].name);
957 return 0;
960 fs_initcall(chr_dev_init);