v2.4.2.1 -> v2.4.2.2
[opensuse: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 mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9  */
10
11 #include <linux/config.h>
12 #include <linux/mm.h>
13 #include <linux/miscdevice.h>
14 #include <linux/tpqic02.h>
15 #include <linux/ftape.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mman.h>
19 #include <linux/random.h>
20 #include <linux/init.h>
21 #include <linux/raw.h>
22 #include <linux/tty.h>
23 #include <linux/capability.h>
24
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include <asm/pgalloc.h>
28
29 #ifdef CONFIG_I2C
30 extern int i2c_init_all(void);
31 #endif
32 #ifdef CONFIG_VIDEO_DEV
33 extern int videodev_init(void);
34 #endif
35 #ifdef CONFIG_FB
36 extern void fbmem_init(void);
37 #endif
38 #ifdef CONFIG_PROM_CONSOLE
39 extern void prom_con_init(void);
40 #endif
41 #ifdef CONFIG_MDA_CONSOLE
42 extern void mda_console_init(void);
43 #endif
44 #if defined(CONFIG_ADB)
45 extern void adbdev_init(void);
46 #endif
47      
48 static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
49                             const char * buf, size_t count, loff_t *ppos)
50 {
51         ssize_t written;
52
53         written = 0;
54 #if defined(__sparc__) || defined(__mc68000__)
55         /* we don't have page 0 mapped on sparc and m68k.. */
56         if (realp < PAGE_SIZE) {
57                 unsigned long sz = PAGE_SIZE-realp;
58                 if (sz > count) sz = count; 
59                 /* Hmm. Do something? */
60                 buf+=sz;
61                 p+=sz;
62                 count-=sz;
63                 written+=sz;
64         }
65 #endif
66         if (copy_from_user(p, buf, count))
67                 return -EFAULT;
68         written += count;
69         *ppos += written;
70         return written;
71 }
72
73
74 /*
75  * This funcion reads the *physical* memory. The f_pos points directly to the 
76  * memory location. 
77  */
78 static ssize_t read_mem(struct file * file, char * buf,
79                         size_t count, loff_t *ppos)
80 {
81         unsigned long p = *ppos;
82         unsigned long end_mem;
83         ssize_t read;
84         
85         end_mem = __pa(high_memory);
86         if (p >= end_mem)
87                 return 0;
88         if (count > end_mem - p)
89                 count = end_mem - p;
90         read = 0;
91 #if defined(__sparc__) || defined(__mc68000__)
92         /* we don't have page 0 mapped on sparc and m68k.. */
93         if (p < PAGE_SIZE) {
94                 unsigned long sz = PAGE_SIZE-p;
95                 if (sz > count) 
96                         sz = count; 
97                 if (sz > 0) {
98                         if (clear_user(buf, sz))
99                                 return -EFAULT;
100                         buf += sz; 
101                         p += sz; 
102                         count -= sz; 
103                         read += sz; 
104                 }
105         }
106 #endif
107         if (copy_to_user(buf, __va(p), count))
108                 return -EFAULT;
109         read += count;
110         *ppos += read;
111         return read;
112 }
113
114 static ssize_t write_mem(struct file * file, const char * buf, 
115                          size_t count, loff_t *ppos)
116 {
117         unsigned long p = *ppos;
118         unsigned long end_mem;
119
120         end_mem = __pa(high_memory);
121         if (p >= end_mem)
122                 return 0;
123         if (count > end_mem - p)
124                 count = end_mem - p;
125         return do_write_mem(file, __va(p), p, buf, count, ppos);
126 }
127
128 #ifndef pgprot_noncached
129
130 /*
131  * This should probably be per-architecture in <asm/pgtable.h>
132  */
133 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
134 {
135         unsigned long prot = pgprot_val(_prot);
136
137 #if defined(__i386__)
138         /* On PPro and successors, PCD alone doesn't always mean 
139             uncached because of interactions with the MTRRs. PCD | PWT
140             means definitely uncached. */ 
141         if (boot_cpu_data.x86 > 3)
142                 prot |= _PAGE_PCD | _PAGE_PWT;
143 #elif defined(__powerpc__)
144         prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
145 #elif defined(__mc68000__)
146 #ifdef SUN3_PAGE_NOCACHE
147         if (MMU_IS_SUN3)
148                 prot |= SUN3_PAGE_NOCACHE;
149         else
150 #endif
151         if (MMU_IS_851 || MMU_IS_030)
152                 prot |= _PAGE_NOCACHE030;
153         /* Use no-cache mode, serialized */
154         else if (MMU_IS_040 || MMU_IS_060)
155                 prot = (prot & _CACHEMASK040) | _PAGE_NOCACHE_S;
156 #elif defined(__mips__)
157         prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
158 #endif
159
160         return __pgprot(prot);
161 }
162
163 #endif /* !pgprot_noncached */
164
165 /*
166  * Architectures vary in how they handle caching for addresses 
167  * outside of main memory.
168  */
169 static inline int noncached_address(unsigned long addr)
170 {
171 #if defined(__i386__)
172         /* 
173          * On the PPro and successors, the MTRRs are used to set
174          * memory types for physical addresses outside main memory, 
175          * so blindly setting PCD or PWT on those pages is wrong.
176          * For Pentiums and earlier, the surround logic should disable 
177          * caching for the high addresses through the KEN pin, but
178          * we maintain the tradition of paranoia in this code.
179          */
180         return !( test_bit(X86_FEATURE_MTRR, &boot_cpu_data.x86_capability) ||
181                   test_bit(X86_FEATURE_K6_MTRR, &boot_cpu_data.x86_capability) ||
182                   test_bit(X86_FEATURE_CYRIX_ARR, &boot_cpu_data.x86_capability) ||
183                   test_bit(X86_FEATURE_CENTAUR_MCR, &boot_cpu_data.x86_capability) )
184           && addr >= __pa(high_memory);
185 #else
186         return addr >= __pa(high_memory);
187 #endif
188 }
189
190 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
191 {
192         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
193
194         /*
195          * Accessing memory above the top the kernel knows about or
196          * through a file pointer that was marked O_SYNC will be
197          * done non-cached.
198          */
199         if (noncached_address(offset) || (file->f_flags & O_SYNC))
200                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
201
202         /* Don't try to swap out physical pages.. */
203         vma->vm_flags |= VM_RESERVED;
204
205         /*
206          * Don't dump addresses that are not real memory to a core file.
207          */
208         if (offset >= __pa(high_memory) || (file->f_flags & O_SYNC))
209                 vma->vm_flags |= VM_IO;
210
211         if (remap_page_range(vma->vm_start, offset, vma->vm_end-vma->vm_start,
212                              vma->vm_page_prot))
213                 return -EAGAIN;
214         return 0;
215 }
216
217 /*
218  * This function reads the *virtual* memory as seen by the kernel.
219  */
220 static ssize_t read_kmem(struct file *file, char *buf, 
221                          size_t count, loff_t *ppos)
222 {
223         unsigned long p = *ppos;
224         ssize_t read = 0;
225         ssize_t virtr = 0;
226         char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
227                 
228         if (p < (unsigned long) high_memory) {
229                 read = count;
230                 if (count > (unsigned long) high_memory - p)
231                         read = (unsigned long) high_memory - p;
232
233 #if defined(__sparc__) || defined(__mc68000__)
234                 /* we don't have page 0 mapped on sparc and m68k.. */
235                 if (p < PAGE_SIZE && read > 0) {
236                         size_t tmp = PAGE_SIZE - p;
237                         if (tmp > read) tmp = read;
238                         if (clear_user(buf, tmp))
239                                 return -EFAULT;
240                         buf += tmp;
241                         p += tmp;
242                         read -= tmp;
243                         count -= tmp;
244                 }
245 #endif
246                 if (copy_to_user(buf, (char *)p, read))
247                         return -EFAULT;
248                 p += read;
249                 buf += read;
250                 count -= read;
251         }
252
253         if (count > 0) {
254                 kbuf = (char *)__get_free_page(GFP_KERNEL);
255                 if (!kbuf)
256                         return -ENOMEM;
257                 while (count > 0) {
258                         int len = count;
259
260                         if (len > PAGE_SIZE)
261                                 len = PAGE_SIZE;
262                         len = vread(kbuf, (char *)p, len);
263                         if (len && copy_to_user(buf, kbuf, len)) {
264                                 free_page((unsigned long)kbuf);
265                                 return -EFAULT;
266                         }
267                         count -= len;
268                         buf += len;
269                         virtr += len;
270                         p += len;
271                 }
272                 free_page((unsigned long)kbuf);
273         }
274         *ppos = p;
275         return virtr + read;
276 }
277
278 /*
279  * This function writes to the *virtual* memory as seen by the kernel.
280  */
281 static ssize_t write_kmem(struct file * file, const char * buf, 
282                           size_t count, loff_t *ppos)
283 {
284         unsigned long p = *ppos;
285
286         if (p >= (unsigned long) high_memory)
287                 return 0;
288         if (count > (unsigned long) high_memory - p)
289                 count = (unsigned long) high_memory - p;
290         return do_write_mem(file, (void*)p, p, buf, count, ppos);
291 }
292
293 #if !defined(__mc68000__)
294 static ssize_t read_port(struct file * file, char * buf,
295                          size_t count, loff_t *ppos)
296 {
297         unsigned long i = *ppos;
298         char *tmp = buf;
299
300         if (verify_area(VERIFY_WRITE,buf,count))
301                 return -EFAULT; 
302         while (count-- > 0 && i < 65536) {
303                 if (__put_user(inb(i),tmp) < 0) 
304                         return -EFAULT;  
305                 i++;
306                 tmp++;
307         }
308         *ppos = i;
309         return tmp-buf;
310 }
311
312 static ssize_t write_port(struct file * file, const char * buf,
313                           size_t count, loff_t *ppos)
314 {
315         unsigned long i = *ppos;
316         const char * tmp = buf;
317
318         if (verify_area(VERIFY_READ,buf,count))
319                 return -EFAULT;
320         while (count-- > 0 && i < 65536) {
321                 char c;
322                 if (__get_user(c, tmp)) 
323                         return -EFAULT; 
324                 outb(c,i);
325                 i++;
326                 tmp++;
327         }
328         *ppos = i;
329         return tmp-buf;
330 }
331 #endif
332
333 static ssize_t read_null(struct file * file, char * buf,
334                          size_t count, loff_t *ppos)
335 {
336         return 0;
337 }
338
339 static ssize_t write_null(struct file * file, const char * buf,
340                           size_t count, loff_t *ppos)
341 {
342         return count;
343 }
344
345 /*
346  * For fun, we are using the MMU for this.
347  */
348 static inline size_t read_zero_pagealigned(char * buf, size_t size)
349 {
350         struct mm_struct *mm;
351         struct vm_area_struct * vma;
352         unsigned long addr=(unsigned long)buf;
353
354         mm = current->mm;
355         /* Oops, this was forgotten before. -ben */
356         down(&mm->mmap_sem);
357
358         /* For private mappings, just map in zero pages. */
359         for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
360                 unsigned long count;
361
362                 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
363                         goto out_up;
364                 if (vma->vm_flags & VM_SHARED)
365                         break;
366                 count = vma->vm_end - addr;
367                 if (count > size)
368                         count = size;
369
370                 flush_cache_range(mm, addr, addr + count);
371                 zap_page_range(mm, addr, count);
372                 zeromap_page_range(addr, count, PAGE_COPY);
373                 flush_tlb_range(mm, addr, addr + count);
374
375                 size -= count;
376                 buf += count;
377                 addr += count;
378                 if (size == 0)
379                         goto out_up;
380         }
381
382         up(&mm->mmap_sem);
383         
384         /* The shared case is hard. Let's do the conventional zeroing. */ 
385         do {
386                 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
387                 if (unwritten)
388                         return size + unwritten - PAGE_SIZE;
389                 if (current->need_resched)
390                         schedule();
391                 buf += PAGE_SIZE;
392                 size -= PAGE_SIZE;
393         } while (size);
394
395         return size;
396 out_up:
397         up(&mm->mmap_sem);
398         return size;
399 }
400
401 static ssize_t read_zero(struct file * file, char * buf, 
402                          size_t count, loff_t *ppos)
403 {
404         unsigned long left, unwritten, written = 0;
405
406         if (!count)
407                 return 0;
408
409         if (!access_ok(VERIFY_WRITE, buf, count))
410                 return -EFAULT;
411
412         left = count;
413
414         /* do we want to be clever? Arbitrary cut-off */
415         if (count >= PAGE_SIZE*4) {
416                 unsigned long partial;
417
418                 /* How much left of the page? */
419                 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
420                 unwritten = clear_user(buf, partial);
421                 written = partial - unwritten;
422                 if (unwritten)
423                         goto out;
424                 left -= partial;
425                 buf += partial;
426                 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
427                 written += (left & PAGE_MASK) - unwritten;
428                 if (unwritten)
429                         goto out;
430                 buf += left & PAGE_MASK;
431                 left &= ~PAGE_MASK;
432         }
433         unwritten = clear_user(buf, left);
434         written += left - unwritten;
435 out:
436         return written ? written : -EFAULT;
437 }
438
439 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
440 {
441         if (vma->vm_flags & VM_SHARED)
442                 return shmem_zero_setup(vma);
443         if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
444                 return -EAGAIN;
445         return 0;
446 }
447
448 static ssize_t write_full(struct file * file, const char * buf,
449                           size_t count, loff_t *ppos)
450 {
451         return -ENOSPC;
452 }
453
454 /*
455  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
456  * can fopen() both devices with "a" now.  This was previously impossible.
457  * -- SRB.
458  */
459
460 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
461 {
462         return file->f_pos = 0;
463 }
464
465 /*
466  * The memory devices use the full 32/64 bits of the offset, and so we cannot
467  * check against negative addresses: they are ok. The return value is weird,
468  * though, in that case (0).
469  *
470  * also note that seeking relative to the "end of file" isn't supported:
471  * it has no meaning, so it returns -EINVAL.
472  */
473 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
474 {
475         switch (orig) {
476                 case 0:
477                         file->f_pos = offset;
478                         return file->f_pos;
479                 case 1:
480                         file->f_pos += offset;
481                         return file->f_pos;
482                 default:
483                         return -EINVAL;
484         }
485 }
486
487 static int open_port(struct inode * inode, struct file * filp)
488 {
489         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
490 }
491
492 #define mmap_kmem       mmap_mem
493 #define zero_lseek      null_lseek
494 #define full_lseek      null_lseek
495 #define write_zero      write_null
496 #define read_full       read_zero
497 #define open_mem        open_port
498 #define open_kmem       open_mem
499
500 static struct file_operations mem_fops = {
501         llseek:         memory_lseek,
502         read:           read_mem,
503         write:          write_mem,
504         mmap:           mmap_mem,
505         open:           open_mem,
506 };
507
508 static struct file_operations kmem_fops = {
509         llseek:         memory_lseek,
510         read:           read_kmem,
511         write:          write_kmem,
512         mmap:           mmap_kmem,
513         open:           open_kmem,
514 };
515
516 static struct file_operations null_fops = {
517         llseek:         null_lseek,
518         read:           read_null,
519         write:          write_null,
520 };
521
522 #if !defined(__mc68000__)
523 static struct file_operations port_fops = {
524         llseek:         memory_lseek,
525         read:           read_port,
526         write:          write_port,
527         open:           open_port,
528 };
529 #endif
530
531 static struct file_operations zero_fops = {
532         llseek:         zero_lseek,
533         read:           read_zero,
534         write:          write_zero,
535         mmap:           mmap_zero,
536 };
537
538 static struct file_operations full_fops = {
539         llseek:         full_lseek,
540         read:           read_full,
541         write:          write_full,
542 };
543
544 static int memory_open(struct inode * inode, struct file * filp)
545 {
546         switch (MINOR(inode->i_rdev)) {
547                 case 1:
548                         filp->f_op = &mem_fops;
549                         break;
550                 case 2:
551                         filp->f_op = &kmem_fops;
552                         break;
553                 case 3:
554                         filp->f_op = &null_fops;
555                         break;
556 #if !defined(__mc68000__)
557                 case 4:
558                         filp->f_op = &port_fops;
559                         break;
560 #endif
561                 case 5:
562                         filp->f_op = &zero_fops;
563                         break;
564                 case 7:
565                         filp->f_op = &full_fops;
566                         break;
567                 case 8:
568                         filp->f_op = &random_fops;
569                         break;
570                 case 9:
571                         filp->f_op = &urandom_fops;
572                         break;
573                 default:
574                         return -ENXIO;
575         }
576         if (filp->f_op && filp->f_op->open)
577                 return filp->f_op->open(inode,filp);
578         return 0;
579 }
580
581 void __init memory_devfs_register (void)
582 {
583     /*  These are never unregistered  */
584     static const struct {
585         unsigned short minor;
586         char *name;
587         umode_t mode;
588         struct file_operations *fops;
589     } list[] = { /* list of minor devices */
590         {1, "mem",     S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
591         {2, "kmem",    S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
592         {3, "null",    S_IRUGO | S_IWUGO,           &null_fops},
593         {4, "port",    S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
594         {5, "zero",    S_IRUGO | S_IWUGO,           &zero_fops},
595         {7, "full",    S_IRUGO | S_IWUGO,           &full_fops},
596         {8, "random",  S_IRUGO | S_IWUSR,           &random_fops},
597         {9, "urandom", S_IRUGO | S_IWUSR,           &urandom_fops}
598     };
599     int i;
600
601     for (i=0; i<(sizeof(list)/sizeof(*list)); i++)
602         devfs_register (NULL, list[i].name, DEVFS_FL_NONE,
603                         MEM_MAJOR, list[i].minor,
604                         list[i].mode | S_IFCHR,
605                         list[i].fops, NULL);
606 }
607
608 static struct file_operations memory_fops = {
609         open:           memory_open,    /* just a selector for the real open */
610 };
611
612 int __init chr_dev_init(void)
613 {
614         if (devfs_register_chrdev(MEM_MAJOR,"mem",&memory_fops))
615                 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
616         memory_devfs_register();
617         rand_initialize();
618         raw_init();
619 #ifdef CONFIG_I2C
620         i2c_init_all();
621 #endif
622 #if defined (CONFIG_FB)
623         fbmem_init();
624 #endif
625 #if defined (CONFIG_PROM_CONSOLE)
626         prom_con_init();
627 #endif
628 #if defined (CONFIG_MDA_CONSOLE)
629         mda_console_init();
630 #endif
631         tty_init();
632 #ifdef CONFIG_PRINTER
633         lp_init();
634 #endif
635 #ifdef CONFIG_M68K_PRINTER
636         lp_m68k_init();
637 #endif
638         misc_init();
639 #if CONFIG_QIC02_TAPE
640         qic02_tape_init();
641 #endif
642 #ifdef CONFIG_FTAPE
643         ftape_init();
644 #endif
645 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
646         tapechar_init();
647 #endif
648 #if defined(CONFIG_ADB)
649         adbdev_init();
650 #endif
651 #ifdef CONFIG_VIDEO_DEV
652         videodev_init();
653 #endif
654         return 0;
655 }