2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/miscdevice.h>
10 #include <linux/tpqic02.h>
11 #include <linux/ftape.h>
12 #include <linux/malloc.h>
13 #include <linux/vmalloc.h>
14 #include <linux/mman.h>
15 #include <linux/random.h>
16 #include <linux/init.h>
17 #include <linux/joystick.h>
18 #include <linux/i2c.h>
19 #include <linux/raw.h>
21 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
26 void soundcore_init(void);
27 #ifdef CONFIG_SOUND_OSS
28 void soundcard_init(void);
30 #ifdef CONFIG_DMASOUND
31 void dmasound_init(void);
34 #ifdef CONFIG_SPARCAUDIO
35 extern int sparcaudio_init(void);
40 #ifdef CONFIG_VIDEO_DEV
41 extern int videodev_init(void);
44 extern void fbmem_init(void);
46 #ifdef CONFIG_PROM_CONSOLE
47 extern void prom_con_init(void);
49 #ifdef CONFIG_MDA_CONSOLE
50 extern void mda_console_init(void);
52 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
53 extern void adbdev_init(void);
56 extern void usb_init(void);
59 static ssize_t
do_write_mem(struct file
* file
, void *p
, unsigned long realp
,
60 const char * buf
, size_t count
, loff_t
*ppos
)
65 #if defined(__sparc__) || defined(__mc68000__)
66 /* we don't have page 0 mapped on sparc and m68k.. */
67 if (realp
< PAGE_SIZE
) {
68 unsigned long sz
= PAGE_SIZE
-realp
;
69 if (sz
> count
) sz
= count
;
70 /* Hmm. Do something? */
77 if (copy_from_user(p
, buf
, count
))
86 * This funcion reads the *physical* memory. The f_pos points directly to the
89 static ssize_t
read_mem(struct file
* file
, char * buf
,
90 size_t count
, loff_t
*ppos
)
92 unsigned long p
= *ppos
;
93 unsigned long end_mem
;
96 end_mem
= __pa(high_memory
);
99 if (count
> end_mem
- p
)
102 #if defined(__sparc__) || defined(__mc68000__)
103 /* we don't have page 0 mapped on sparc and m68k.. */
105 unsigned long sz
= PAGE_SIZE
-p
;
109 if (clear_user(buf
, sz
))
118 if (copy_to_user(buf
, __va(p
), count
))
125 static ssize_t
write_mem(struct file
* file
, const char * buf
,
126 size_t count
, loff_t
*ppos
)
128 unsigned long p
= *ppos
;
129 unsigned long end_mem
;
131 end_mem
= __pa(high_memory
);
134 if (count
> end_mem
- p
)
136 return do_write_mem(file
, __va(p
), p
, buf
, count
, ppos
);
140 * This should probably be per-architecture in <asm/pgtable.h>
142 static inline unsigned long pgprot_noncached(unsigned long prot
)
144 #if defined(__i386__)
145 /* On PPro and successors, PCD alone doesn't always mean
146 uncached because of interactions with the MTRRs. PCD | PWT
147 means definitely uncached. */
148 if (boot_cpu_data
.x86
> 3)
149 prot
|= _PAGE_PCD
| _PAGE_PWT
;
150 #elif defined(__powerpc__)
151 prot
|= _PAGE_NO_CACHE
| _PAGE_GUARDED
;
152 #elif defined(__mc68000__)
153 if (CPU_IS_020_OR_030
)
154 prot
|= _PAGE_NOCACHE030
;
155 /* Use no-cache mode, serialized */
156 if (CPU_IS_040_OR_060
)
157 prot
= (prot
& _CACHEMASK040
) | _PAGE_NOCACHE_S
;
158 #elif defined(__mips__)
159 prot
= (prot
& ~_CACHE_MASK
) | _CACHE_UNCACHED
;
166 * Architectures vary in how they handle caching for addresses
167 * outside of main memory.
169 static inline int noncached_address(unsigned long addr
)
171 #if defined(__i386__)
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.
180 return !(boot_cpu_data
.x86_capability
& X86_FEATURE_MTRR
)
181 && addr
>= __pa(high_memory
);
183 return addr
>= __pa(high_memory
);
187 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
189 unsigned long offset
= vma
->vm_offset
;
191 if (offset
& ~PAGE_MASK
)
195 * Accessing memory above the top the kernel knows about or
196 * through a file pointer that was marked O_SYNC will be
199 if (noncached_address(offset
) || (file
->f_flags
& O_SYNC
))
200 pgprot_val(vma
->vm_page_prot
)
201 = pgprot_noncached(pgprot_val(vma
->vm_page_prot
));
204 * Don't dump addresses that are not real memory to a core file.
206 if (offset
>= __pa(high_memory
) || (file
->f_flags
& O_SYNC
))
207 vma
->vm_flags
|= VM_IO
;
209 if (remap_page_range(vma
->vm_start
, offset
, vma
->vm_end
-vma
->vm_start
,
216 * This function reads the *virtual* memory as seen by the kernel.
218 static ssize_t
read_kmem(struct file
*file
, char *buf
,
219 size_t count
, loff_t
*ppos
)
221 unsigned long p
= *ppos
;
225 if (p
< (unsigned long) high_memory
) {
227 if (count
> (unsigned long) high_memory
- p
)
228 read
= (unsigned long) high_memory
- p
;
230 #if defined(__sparc__) || defined(__mc68000__)
231 /* we don't have page 0 mapped on sparc and m68k.. */
232 if (p
< PAGE_SIZE
&& read
> 0) {
233 size_t tmp
= PAGE_SIZE
- p
;
234 if (tmp
> read
) tmp
= read
;
235 clear_user(buf
, tmp
);
242 copy_to_user(buf
, (char *)p
, read
);
248 virtr
= vread(buf
, (char *)p
, count
);
256 * This function writes to the *virtual* memory as seen by the kernel.
258 static ssize_t
write_kmem(struct file
* file
, const char * buf
,
259 size_t count
, loff_t
*ppos
)
261 unsigned long p
= *ppos
;
263 if (p
>= (unsigned long) high_memory
)
265 if (count
> (unsigned long) high_memory
- p
)
266 count
= (unsigned long) high_memory
- p
;
267 return do_write_mem(file
, (void*)p
, p
, buf
, count
, ppos
);
270 static ssize_t
read_port(struct file
* file
, char * buf
,
271 size_t count
, loff_t
*ppos
)
273 unsigned long i
= *ppos
;
276 if (verify_area(VERIFY_WRITE
,buf
,count
))
278 while (count
-- > 0 && i
< 65536) {
279 if (__put_user(inb(i
),tmp
) < 0)
288 static ssize_t
write_port(struct file
* file
, const char * buf
,
289 size_t count
, loff_t
*ppos
)
291 unsigned long i
= *ppos
;
292 const char * tmp
= buf
;
294 if (verify_area(VERIFY_READ
,buf
,count
))
296 while (count
-- > 0 && i
< 65536) {
298 if (__get_user(c
, tmp
))
308 static ssize_t
read_null(struct file
* file
, char * buf
,
309 size_t count
, loff_t
*ppos
)
314 static ssize_t
write_null(struct file
* file
, const char * buf
,
315 size_t count
, loff_t
*ppos
)
321 * For fun, we are using the MMU for this.
323 static inline size_t read_zero_pagealigned(char * buf
, size_t size
)
325 struct mm_struct
*mm
;
326 struct vm_area_struct
* vma
;
327 unsigned long addr
=(unsigned long)buf
;
330 /* Oops, this was forgotten before. -ben */
333 /* For private mappings, just map in zero pages. */
334 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
337 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
339 if (vma
->vm_flags
& VM_SHARED
)
341 count
= vma
->vm_end
- addr
;
345 flush_cache_range(mm
, addr
, addr
+ count
);
346 zap_page_range(mm
, addr
, count
);
347 zeromap_page_range(addr
, count
, PAGE_COPY
);
348 flush_tlb_range(mm
, addr
, addr
+ count
);
359 /* The shared case is hard. Let's do the conventional zeroing. */
361 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
363 return size
+ unwritten
- PAGE_SIZE
;
364 if (current
->need_resched
)
376 static ssize_t
read_zero(struct file
* file
, char * buf
,
377 size_t count
, loff_t
*ppos
)
379 unsigned long left
, unwritten
, written
= 0;
384 if (!access_ok(VERIFY_WRITE
, buf
, count
))
389 /* do we want to be clever? Arbitrary cut-off */
390 if (count
>= PAGE_SIZE
*4) {
391 unsigned long partial
;
393 /* How much left of the page? */
394 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
395 unwritten
= clear_user(buf
, partial
);
396 written
= partial
- unwritten
;
401 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
402 written
+= (left
& PAGE_MASK
) - unwritten
;
405 buf
+= left
& PAGE_MASK
;
408 unwritten
= clear_user(buf
, left
);
409 written
+= left
- unwritten
;
411 return written
? written
: -EFAULT
;
414 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
416 if (vma
->vm_flags
& VM_SHARED
)
418 if (zeromap_page_range(vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
423 static ssize_t
write_full(struct file
* file
, const char * buf
,
424 size_t count
, loff_t
*ppos
)
430 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
431 * can fopen() both devices with "a" now. This was previously impossible.
435 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
437 return file
->f_pos
= 0;
441 * The memory devices use the full 32/64 bits of the offset, and so we cannot
442 * check against negative addresses: they are ok. The return value is weird,
443 * though, in that case (0).
445 * also note that seeking relative to the "end of file" isn't supported:
446 * it has no meaning, so it returns -EINVAL.
448 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
452 file
->f_pos
= offset
;
455 file
->f_pos
+= offset
;
462 #define mmap_kmem mmap_mem
463 #define zero_lseek null_lseek
464 #define full_lseek null_lseek
465 #define write_zero write_null
466 #define read_full read_zero
468 static struct file_operations mem_fops
= {
472 NULL
, /* mem_readdir */
474 NULL
, /* mem_ioctl */
476 NULL
, /* no special open code */
478 NULL
, /* no special release code */
482 static struct file_operations kmem_fops
= {
486 NULL
, /* kmem_readdir */
487 NULL
, /* kmem_poll */
488 NULL
, /* kmem_ioctl */
490 NULL
, /* no special open code */
492 NULL
, /* no special release code */
496 static struct file_operations null_fops
= {
500 NULL
, /* null_readdir */
501 NULL
, /* null_poll */
502 NULL
, /* null_ioctl */
503 NULL
, /* null_mmap */
504 NULL
, /* no special open code */
506 NULL
, /* no special release code */
510 static struct file_operations port_fops
= {
514 NULL
, /* port_readdir */
515 NULL
, /* port_poll */
516 NULL
, /* port_ioctl */
517 NULL
, /* port_mmap */
518 NULL
, /* no special open code */
520 NULL
, /* no special release code */
524 static struct file_operations zero_fops
= {
528 NULL
, /* zero_readdir */
529 NULL
, /* zero_poll */
530 NULL
, /* zero_ioctl */
532 NULL
, /* no special open code */
534 NULL
/* no special release code */
537 static struct file_operations full_fops
= {
541 NULL
, /* full_readdir */
542 NULL
, /* full_poll */
543 NULL
, /* full_ioctl */
544 NULL
, /* full_mmap */
545 NULL
, /* no special open code */
547 NULL
/* no special release code */
550 static int memory_open(struct inode
* inode
, struct file
* filp
)
552 switch (MINOR(inode
->i_rdev
)) {
554 filp
->f_op
= &mem_fops
;
557 filp
->f_op
= &kmem_fops
;
560 filp
->f_op
= &null_fops
;
562 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
564 filp
->f_op
= &port_fops
;
568 filp
->f_op
= &zero_fops
;
571 filp
->f_op
= &full_fops
;
574 filp
->f_op
= &random_fops
;
577 filp
->f_op
= &urandom_fops
;
582 if (filp
->f_op
&& filp
->f_op
->open
)
583 return filp
->f_op
->open(inode
,filp
);
587 static struct file_operations memory_fops
= {
595 memory_open
, /* just a selector for the real open */
601 int __init
chr_dev_init(void)
603 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
604 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
610 #if defined (CONFIG_FB)
613 #if defined (CONFIG_PROM_CONSOLE)
616 #if defined (CONFIG_MDA_CONSOLE)
620 #ifdef CONFIG_PRINTER
623 #ifdef CONFIG_M68K_PRINTER
629 #ifdef CONFIG_SOUND_OSS
632 #ifdef CONFIG_DMASOUND
636 #ifdef CONFIG_SPARCAUDIO
639 #ifdef CONFIG_JOYSTICK
641 * Some joysticks only appear when the sound card they are
642 * connected to is configured. Keep the sound/joystick ordering.
646 #if CONFIG_QIC02_TAPE
655 #ifdef CONFIG_VIDEO_BT848
658 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
661 #ifdef CONFIG_VIDEO_DEV