tree-wide: fix assorted typos all over the place
[linux-2.6.git] / drivers / char / mem.c
index 964ff3b..42e65cf 100644 (file)
@@ -5,7 +5,7 @@
  *
  *  Added devfs support. 
  *    Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
- *  Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
+ *  Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
  */
 
 #include <linux/mm.h>
@@ -26,6 +26,7 @@
 #include <linux/bootmem.h>
 #include <linux/splice.h>
 #include <linux/pfn.h>
+#include <linux/smp_lock.h>
 
 #include <asm/uaccess.h>
 #include <asm/io.h>
  */
 static inline int uncached_access(struct file *file, unsigned long addr)
 {
-#if defined(__i386__) && !defined(__arch_um__)
-       /*
-        * On the PPro and successors, the MTRRs are used to set
-        * memory types for physical addresses outside main memory,
-        * so blindly setting PCD or PWT on those pages is wrong.
-        * For Pentiums and earlier, the surround logic should disable
-        * caching for the high addresses through the KEN pin, but
-        * we maintain the tradition of paranoia in this code.
-        */
-       if (file->f_flags & O_SYNC)
-               return 1;
-       return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
-                 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
-                 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
-                 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
-         && addr >= __pa(high_memory);
-#elif defined(__x86_64__) && !defined(__arch_um__)
-       /* 
-        * This is broken because it can generate memory type aliases,
-        * which can cause cache corruptions
-        * But it is only available for root and we have to be bug-to-bug
-        * compatible with i386.
-        */
-       if (file->f_flags & O_SYNC)
-               return 1;
-       /* same behaviour as i386. PAT always set to cached and MTRRs control the
-          caching behaviour. 
-          Hopefully a full PAT implementation will fix that soon. */      
-       return 0;
-#elif defined(CONFIG_IA64)
+#if defined(CONFIG_IA64)
        /*
         * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
         */
@@ -108,7 +80,7 @@ static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
 }
 #endif
 
-#ifdef CONFIG_NONPROMISC_DEVMEM
+#ifdef CONFIG_STRICT_DEVMEM
 static inline int range_is_allowed(unsigned long pfn, unsigned long size)
 {
        u64 from = ((u64)pfn) << PAGE_SHIFT;
@@ -134,6 +106,10 @@ static inline int range_is_allowed(unsigned long pfn, unsigned long size)
 }
 #endif
 
+void __attribute__((weak)) unxlate_dev_mem_ptr(unsigned long phys, void *addr)
+{
+}
+
 /*
  * This funcion reads the *physical* memory. The f_pos points directly to the 
  * memory location. 
@@ -176,17 +152,25 @@ static ssize_t read_mem(struct file * file, char __user * buf,
 
                sz = min_t(unsigned long, sz, count);
 
+               if (!range_is_allowed(p >> PAGE_SHIFT, count))
+                       return -EPERM;
+
                /*
                 * On ia64 if a page has been mapped somewhere as
                 * uncached, then it must also be accessed uncached
                 * by the kernel or data corruption may occur
                 */
                ptr = xlate_dev_mem_ptr(p);
+               if (!ptr)
+                       return -EFAULT;
 
-               if (!range_is_allowed(p >> PAGE_SHIFT, count))
-                       return -EPERM;
-               if (copy_to_user(buf, ptr, sz))
+               if (copy_to_user(buf, ptr, sz)) {
+                       unxlate_dev_mem_ptr(p, ptr);
                        return -EFAULT;
+               }
+
+               unxlate_dev_mem_ptr(p, ptr);
+
                buf += sz;
                p += sz;
                count -= sz;
@@ -235,22 +219,32 @@ static ssize_t write_mem(struct file * file, const char __user * buf,
 
                sz = min_t(unsigned long, sz, count);
 
+               if (!range_is_allowed(p >> PAGE_SHIFT, sz))
+                       return -EPERM;
+
                /*
                 * On ia64 if a page has been mapped somewhere as
                 * uncached, then it must also be accessed uncached
                 * by the kernel or data corruption may occur
                 */
                ptr = xlate_dev_mem_ptr(p);
+               if (!ptr) {
+                       if (written)
+                               break;
+                       return -EFAULT;
+               }
 
-               if (!range_is_allowed(p >> PAGE_SHIFT, sz))
-                       return -EPERM;
                copied = copy_from_user(ptr, buf, sz);
                if (copied) {
                        written += sz - copied;
+                       unxlate_dev_mem_ptr(p, ptr);
                        if (written)
                                break;
                        return -EFAULT;
                }
+
+               unxlate_dev_mem_ptr(p, ptr);
+
                buf += sz;
                p += sz;
                count -= sz;
@@ -261,6 +255,12 @@ static ssize_t write_mem(struct file * file, const char __user * buf,
        return written;
 }
 
+int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
+       unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
+{
+       return 1;
+}
+
 #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
 static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
                                     unsigned long size, pgprot_t vma_prot)
@@ -301,6 +301,12 @@ static inline int private_mapping_ok(struct vm_area_struct *vma)
 }
 #endif
 
+static const struct vm_operations_struct mmap_mem_ops = {
+#ifdef CONFIG_HAVE_IOREMAP_PROT
+       .access = generic_access_phys
+#endif
+};
+
 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
 {
        size_t size = vma->vm_end - vma->vm_start;
@@ -314,20 +320,28 @@ static int mmap_mem(struct file * file, struct vm_area_struct * vma)
        if (!range_is_allowed(vma->vm_pgoff, size))
                return -EPERM;
 
+       if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
+                                               &vma->vm_page_prot))
+               return -EINVAL;
+
        vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
                                                 size,
                                                 vma->vm_page_prot);
 
+       vma->vm_ops = &mmap_mem_ops;
+
        /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
        if (remap_pfn_range(vma,
                            vma->vm_start,
                            vma->vm_pgoff,
                            size,
-                           vma->vm_page_prot))
+                           vma->vm_page_prot)) {
                return -EAGAIN;
+       }
        return 0;
 }
 
+#ifdef CONFIG_DEVKMEM
 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
 {
        unsigned long pfn;
@@ -348,6 +362,7 @@ static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
        vma->vm_pgoff = pfn;
        return mmap_mem(file, vma);
 }
+#endif
 
 #ifdef CONFIG_CRASH_DUMP
 /*
@@ -383,9 +398,7 @@ static ssize_t read_oldmem(struct file *file, char __user *buf,
 }
 #endif
 
-extern long vread(char *buf, char *addr, unsigned long count);
-extern long vwrite(char *buf, char *addr, unsigned long count);
-
+#ifdef CONFIG_DEVKMEM
 /*
  * This function reads the *virtual* memory as seen by the kernel.
  */
@@ -590,6 +603,7 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
        *ppos = p;
        return virtr + wrote;
 }
+#endif
 
 #ifdef CONFIG_DEVPORT
 static ssize_t read_port(struct file * file, char __user * buf,
@@ -676,10 +690,12 @@ static ssize_t read_zero(struct file * file, char __user * buf,
 
                if (chunk > PAGE_SIZE)
                        chunk = PAGE_SIZE;      /* Just for latency reasons */
-               unwritten = clear_user(buf, chunk);
+               unwritten = __clear_user(buf, chunk);
                written += chunk - unwritten;
                if (unwritten)
                        break;
+               if (signal_pending(current))
+                       return written ? written : -ERESTARTSYS;
                buf += chunk;
                count -= chunk;
                cond_resched();
@@ -767,6 +783,7 @@ static const struct file_operations mem_fops = {
        .get_unmapped_area = get_unmapped_area_mem,
 };
 
+#ifdef CONFIG_DEVKMEM
 static const struct file_operations kmem_fops = {
        .llseek         = memory_lseek,
        .read           = read_kmem,
@@ -775,6 +792,7 @@ static const struct file_operations kmem_fops = {
        .open           = open_kmem,
        .get_unmapped_area = get_unmapped_area_mem,
 };
+#endif
 
 static const struct file_operations null_fops = {
        .llseek         = null_lseek,
@@ -804,6 +822,7 @@ static const struct file_operations zero_fops = {
  * - permits private mappings, "copies" are taken of the source of zeros
  */
 static struct backing_dev_info zero_bdi = {
+       .name           = "char/mem",
        .capabilities   = BDI_CAP_MAP_COPY,
 };
 
@@ -845,87 +864,75 @@ static const struct file_operations kmsg_fops = {
        .write =        kmsg_write,
 };
 
-static int memory_open(struct inode * inode, struct file * filp)
-{
-       switch (iminor(inode)) {
-               case 1:
-                       filp->f_op = &mem_fops;
-                       filp->f_mapping->backing_dev_info =
-                               &directly_mappable_cdev_bdi;
-                       break;
-               case 2:
-                       filp->f_op = &kmem_fops;
-                       filp->f_mapping->backing_dev_info =
-                               &directly_mappable_cdev_bdi;
-                       break;
-               case 3:
-                       filp->f_op = &null_fops;
-                       break;
+static const struct memdev {
+       const char *name;
+       mode_t mode;
+       const struct file_operations *fops;
+       struct backing_dev_info *dev_info;
+} devlist[] = {
+        [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
+#ifdef CONFIG_DEVKMEM
+        [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
+#endif
+        [3] = { "null", 0666, &null_fops, NULL },
 #ifdef CONFIG_DEVPORT
-               case 4:
-                       filp->f_op = &port_fops;
-                       break;
+        [4] = { "port", 0, &port_fops, NULL },
 #endif
-               case 5:
-                       filp->f_mapping->backing_dev_info = &zero_bdi;
-                       filp->f_op = &zero_fops;
-                       break;
-               case 7:
-                       filp->f_op = &full_fops;
-                       break;
-               case 8:
-                       filp->f_op = &random_fops;
-                       break;
-               case 9:
-                       filp->f_op = &urandom_fops;
-                       break;
-               case 11:
-                       filp->f_op = &kmsg_fops;
-                       break;
+        [5] = { "zero", 0666, &zero_fops, &zero_bdi },
+        [7] = { "full", 0666, &full_fops, NULL },
+        [8] = { "random", 0666, &random_fops, NULL },
+        [9] = { "urandom", 0666, &urandom_fops, NULL },
+       [11] = { "kmsg", 0, &kmsg_fops, NULL },
 #ifdef CONFIG_CRASH_DUMP
-               case 12:
-                       filp->f_op = &oldmem_fops;
-                       break;
+       [12] = { "oldmem", 0, &oldmem_fops, NULL },
 #endif
-               default:
-                       return -ENXIO;
-       }
-       if (filp->f_op && filp->f_op->open)
-               return filp->f_op->open(inode,filp);
-       return 0;
+};
+
+static int memory_open(struct inode *inode, struct file *filp)
+{
+       int minor;
+       const struct memdev *dev;
+       int ret = -ENXIO;
+
+       lock_kernel();
+
+       minor = iminor(inode);
+       if (minor >= ARRAY_SIZE(devlist))
+               goto out;
+
+       dev = &devlist[minor];
+       if (!dev->fops)
+               goto out;
+
+       filp->f_op = dev->fops;
+       if (dev->dev_info)
+               filp->f_mapping->backing_dev_info = dev->dev_info;
+
+       if (dev->fops->open)
+               ret = dev->fops->open(inode, filp);
+       else
+               ret = 0;
+out:
+       unlock_kernel();
+       return ret;
 }
 
 static const struct file_operations memory_fops = {
-       .open           = memory_open,  /* just a selector for the real open */
+       .open           = memory_open,
 };
 
-static const struct {
-       unsigned int            minor;
-       char                    *name;
-       umode_t                 mode;
-       const struct file_operations    *fops;
-} devlist[] = { /* list of minor devices */
-       {1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
-       {2, "kmem",    S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
-       {3, "null",    S_IRUGO | S_IWUGO,           &null_fops},
-#ifdef CONFIG_DEVPORT
-       {4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
-#endif
-       {5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
-       {7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
-       {8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
-       {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops},
-       {11,"kmsg",    S_IRUGO | S_IWUSR,           &kmsg_fops},
-#ifdef CONFIG_CRASH_DUMP
-       {12,"oldmem",    S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
-#endif
-};
+static char *mem_devnode(struct device *dev, mode_t *mode)
+{
+       if (mode && devlist[MINOR(dev->devt)].mode)
+               *mode = devlist[MINOR(dev->devt)].mode;
+       return NULL;
+}
 
 static struct class *mem_class;
 
 static int __init chr_dev_init(void)
 {
-       int i;
+       int minor;
        int err;
 
        err = bdi_init(&zero_bdi);
@@ -936,10 +943,13 @@ static int __init chr_dev_init(void)
                printk("unable to get major %d for memory devs\n", MEM_MAJOR);
 
        mem_class = class_create(THIS_MODULE, "mem");
-       for (i = 0; i < ARRAY_SIZE(devlist); i++)
-               device_create(mem_class, NULL,
-                             MKDEV(MEM_MAJOR, devlist[i].minor),
-                             devlist[i].name);
+       mem_class->devnode = mem_devnode;
+       for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
+               if (!devlist[minor].name)
+                       continue;
+               device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
+                             NULL, devlist[minor].name);
+       }
 
        return 0;
 }