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>
20 #include <linux/capability.h>
22 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
27 void soundcore_init(void);
28 #ifdef CONFIG_SOUND_OSS
29 void soundcard_init(void);
31 #ifdef CONFIG_DMASOUND
32 void dmasound_init(void);
35 #ifdef CONFIG_SPARCAUDIO
36 extern int sparcaudio_init(void);
41 #ifdef CONFIG_VIDEO_DEV
42 extern int videodev_init(void);
45 extern void fbmem_init(void);
47 #ifdef CONFIG_PROM_CONSOLE
48 extern void prom_con_init(void);
50 #ifdef CONFIG_MDA_CONSOLE
51 extern void mda_console_init(void);
53 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
54 extern void adbdev_init(void);
57 extern void usb_init(void);
60 static ssize_t
do_write_mem(struct file
* file
, void *p
, unsigned long realp
,
61 const char * buf
, size_t count
, loff_t
*ppos
)
66 #if defined(__sparc__) || defined(__mc68000__)
67 /* we don't have page 0 mapped on sparc and m68k.. */
68 if (realp
< PAGE_SIZE
) {
69 unsigned long sz
= PAGE_SIZE
-realp
;
70 if (sz
> count
) sz
= count
;
71 /* Hmm. Do something? */
78 if (copy_from_user(p
, buf
, count
))
87 * This funcion reads the *physical* memory. The f_pos points directly to the
90 static ssize_t
read_mem(struct file
* file
, char * buf
,
91 size_t count
, loff_t
*ppos
)
93 unsigned long p
= *ppos
;
94 unsigned long end_mem
;
97 end_mem
= __pa(high_memory
);
100 if (count
> end_mem
- p
)
103 #if defined(__sparc__) || defined(__mc68000__)
104 /* we don't have page 0 mapped on sparc and m68k.. */
106 unsigned long sz
= PAGE_SIZE
-p
;
110 if (clear_user(buf
, sz
))
119 if (copy_to_user(buf
, __va(p
), count
))
126 static ssize_t
write_mem(struct file
* file
, const char * buf
,
127 size_t count
, loff_t
*ppos
)
129 unsigned long p
= *ppos
;
130 unsigned long end_mem
;
132 end_mem
= __pa(high_memory
);
135 if (count
> end_mem
- p
)
137 return do_write_mem(file
, __va(p
), p
, buf
, count
, ppos
);
141 * This should probably be per-architecture in <asm/pgtable.h>
143 static inline unsigned long pgprot_noncached(unsigned long prot
)
145 #if defined(__i386__)
146 /* On PPro and successors, PCD alone doesn't always mean
147 uncached because of interactions with the MTRRs. PCD | PWT
148 means definitely uncached. */
149 if (boot_cpu_data
.x86
> 3)
150 prot
|= _PAGE_PCD
| _PAGE_PWT
;
151 #elif defined(__powerpc__)
152 prot
|= _PAGE_NO_CACHE
| _PAGE_GUARDED
;
153 #elif defined(__mc68000__)
154 if (CPU_IS_020_OR_030
)
155 prot
|= _PAGE_NOCACHE030
;
156 /* Use no-cache mode, serialized */
157 if (CPU_IS_040_OR_060
)
158 prot
= (prot
& _CACHEMASK040
) | _PAGE_NOCACHE_S
;
159 #elif defined(__mips__)
160 prot
= (prot
& ~_CACHE_MASK
) | _CACHE_UNCACHED
;
167 * Architectures vary in how they handle caching for addresses
168 * outside of main memory.
170 static inline int noncached_address(unsigned long addr
)
172 #if defined(__i386__)
174 * On the PPro and successors, the MTRRs are used to set
175 * memory types for physical addresses outside main memory,
176 * so blindly setting PCD or PWT on those pages is wrong.
177 * For Pentiums and earlier, the surround logic should disable
178 * caching for the high addresses through the KEN pin, but
179 * we maintain the tradition of paranoia in this code.
181 return !(boot_cpu_data
.x86_capability
& X86_FEATURE_MTRR
)
182 && addr
>= __pa(high_memory
);
184 return addr
>= __pa(high_memory
);
188 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
190 unsigned long offset
= vma
->vm_offset
;
192 if (offset
& ~PAGE_MASK
)
196 * Accessing memory above the top the kernel knows about or
197 * through a file pointer that was marked O_SYNC will be
200 if (noncached_address(offset
) || (file
->f_flags
& O_SYNC
))
201 pgprot_val(vma
->vm_page_prot
)
202 = pgprot_noncached(pgprot_val(vma
->vm_page_prot
));
205 * Don't dump addresses that are not real memory to a core file.
207 if (offset
>= __pa(high_memory
) || (file
->f_flags
& O_SYNC
))
208 vma
->vm_flags
|= VM_IO
;
210 if (remap_page_range(vma
->vm_start
, offset
, vma
->vm_end
-vma
->vm_start
,
217 * This function reads the *virtual* memory as seen by the kernel.
219 static ssize_t
read_kmem(struct file
*file
, char *buf
,
220 size_t count
, loff_t
*ppos
)
222 unsigned long p
= *ppos
;
226 if (p
< (unsigned long) high_memory
) {
228 if (count
> (unsigned long) high_memory
- p
)
229 read
= (unsigned long) high_memory
- p
;
231 #if defined(__sparc__) || defined(__mc68000__)
232 /* we don't have page 0 mapped on sparc and m68k.. */
233 if (p
< PAGE_SIZE
&& read
> 0) {
234 size_t tmp
= PAGE_SIZE
- p
;
235 if (tmp
> read
) tmp
= read
;
236 clear_user(buf
, tmp
);
243 copy_to_user(buf
, (char *)p
, read
);
249 virtr
= vread(buf
, (char *)p
, count
);
257 * This function writes to the *virtual* memory as seen by the kernel.
259 static ssize_t
write_kmem(struct file
* file
, const char * buf
,
260 size_t count
, loff_t
*ppos
)
262 unsigned long p
= *ppos
;
264 if (p
>= (unsigned long) high_memory
)
266 if (count
> (unsigned long) high_memory
- p
)
267 count
= (unsigned long) high_memory
- p
;
268 return do_write_mem(file
, (void*)p
, p
, buf
, count
, ppos
);
271 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
272 static ssize_t
read_port(struct file
* file
, char * buf
,
273 size_t count
, loff_t
*ppos
)
275 unsigned long i
= *ppos
;
278 if (verify_area(VERIFY_WRITE
,buf
,count
))
280 while (count
-- > 0 && i
< 65536) {
281 if (__put_user(inb(i
),tmp
) < 0)
290 static ssize_t
write_port(struct file
* file
, const char * buf
,
291 size_t count
, loff_t
*ppos
)
293 unsigned long i
= *ppos
;
294 const char * tmp
= buf
;
296 if (verify_area(VERIFY_READ
,buf
,count
))
298 while (count
-- > 0 && i
< 65536) {
300 if (__get_user(c
, tmp
))
311 static ssize_t
read_null(struct file
* file
, char * buf
,
312 size_t count
, loff_t
*ppos
)
317 static ssize_t
write_null(struct file
* file
, const char * buf
,
318 size_t count
, loff_t
*ppos
)
324 * For fun, we are using the MMU for this.
326 static inline size_t read_zero_pagealigned(char * buf
, size_t size
)
328 struct mm_struct
*mm
;
329 struct vm_area_struct
* vma
;
330 unsigned long addr
=(unsigned long)buf
;
333 /* Oops, this was forgotten before. -ben */
336 /* For private mappings, just map in zero pages. */
337 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
340 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
342 if (vma
->vm_flags
& VM_SHARED
)
344 count
= vma
->vm_end
- addr
;
348 flush_cache_range(mm
, addr
, addr
+ count
);
349 zap_page_range(mm
, addr
, count
);
350 zeromap_page_range(addr
, count
, PAGE_COPY
);
351 flush_tlb_range(mm
, addr
, addr
+ count
);
362 /* The shared case is hard. Let's do the conventional zeroing. */
364 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
366 return size
+ unwritten
- PAGE_SIZE
;
367 if (current
->need_resched
)
379 static ssize_t
read_zero(struct file
* file
, char * buf
,
380 size_t count
, loff_t
*ppos
)
382 unsigned long left
, unwritten
, written
= 0;
387 if (!access_ok(VERIFY_WRITE
, buf
, count
))
392 /* do we want to be clever? Arbitrary cut-off */
393 if (count
>= PAGE_SIZE
*4) {
394 unsigned long partial
;
396 /* How much left of the page? */
397 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
398 unwritten
= clear_user(buf
, partial
);
399 written
= partial
- unwritten
;
404 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
405 written
+= (left
& PAGE_MASK
) - unwritten
;
408 buf
+= left
& PAGE_MASK
;
411 unwritten
= clear_user(buf
, left
);
412 written
+= left
- unwritten
;
414 return written
? written
: -EFAULT
;
417 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
419 if (vma
->vm_flags
& VM_SHARED
)
421 if (zeromap_page_range(vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
426 static ssize_t
write_full(struct file
* file
, const char * buf
,
427 size_t count
, loff_t
*ppos
)
433 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
434 * can fopen() both devices with "a" now. This was previously impossible.
438 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
440 return file
->f_pos
= 0;
444 * The memory devices use the full 32/64 bits of the offset, and so we cannot
445 * check against negative addresses: they are ok. The return value is weird,
446 * though, in that case (0).
448 * also note that seeking relative to the "end of file" isn't supported:
449 * it has no meaning, so it returns -EINVAL.
451 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
455 file
->f_pos
= offset
;
458 file
->f_pos
+= offset
;
465 static int open_port(struct inode
* inode
, struct file
* filp
)
467 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
470 #define mmap_kmem mmap_mem
471 #define zero_lseek null_lseek
472 #define full_lseek null_lseek
473 #define write_zero write_null
474 #define read_full read_zero
475 #define open_mem open_port
476 #define open_kmem open_mem
478 static struct file_operations mem_fops
= {
482 NULL
, /* mem_readdir */
484 NULL
, /* mem_ioctl */
488 NULL
, /* no special release code */
492 static struct file_operations kmem_fops
= {
496 NULL
, /* kmem_readdir */
497 NULL
, /* kmem_poll */
498 NULL
, /* kmem_ioctl */
502 NULL
, /* no special release code */
506 static struct file_operations null_fops
= {
510 NULL
, /* null_readdir */
511 NULL
, /* null_poll */
512 NULL
, /* null_ioctl */
513 NULL
, /* null_mmap */
514 NULL
, /* no special open code */
516 NULL
, /* no special release code */
520 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
521 static struct file_operations port_fops
= {
525 NULL
, /* port_readdir */
526 NULL
, /* port_poll */
527 NULL
, /* port_ioctl */
528 NULL
, /* port_mmap */
531 NULL
, /* no special release code */
536 static struct file_operations zero_fops
= {
540 NULL
, /* zero_readdir */
541 NULL
, /* zero_poll */
542 NULL
, /* zero_ioctl */
544 NULL
, /* no special open code */
546 NULL
/* no special release code */
549 static struct file_operations full_fops
= {
553 NULL
, /* full_readdir */
554 NULL
, /* full_poll */
555 NULL
, /* full_ioctl */
556 NULL
, /* full_mmap */
557 NULL
, /* no special open code */
559 NULL
/* no special release code */
562 static int memory_open(struct inode
* inode
, struct file
* filp
)
564 switch (MINOR(inode
->i_rdev
)) {
566 filp
->f_op
= &mem_fops
;
569 filp
->f_op
= &kmem_fops
;
572 filp
->f_op
= &null_fops
;
574 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
576 filp
->f_op
= &port_fops
;
580 filp
->f_op
= &zero_fops
;
583 filp
->f_op
= &full_fops
;
586 filp
->f_op
= &random_fops
;
589 filp
->f_op
= &urandom_fops
;
594 if (filp
->f_op
&& filp
->f_op
->open
)
595 return filp
->f_op
->open(inode
,filp
);
599 static struct file_operations memory_fops
= {
607 memory_open
, /* just a selector for the real open */
613 int __init
chr_dev_init(void)
615 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
616 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
622 #if defined (CONFIG_FB)
625 #if defined (CONFIG_PROM_CONSOLE)
628 #if defined (CONFIG_MDA_CONSOLE)
632 #ifdef CONFIG_PRINTER
635 #ifdef CONFIG_M68K_PRINTER
641 #ifdef CONFIG_SOUND_OSS
644 #ifdef CONFIG_DMASOUND
648 #ifdef CONFIG_SPARCAUDIO
651 #ifdef CONFIG_JOYSTICK
653 * Some joysticks only appear when the sound card they are
654 * connected to is configured. Keep the sound/joystick ordering.
658 #if CONFIG_QIC02_TAPE
667 #ifdef CONFIG_VIDEO_BT848
670 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
673 #ifdef CONFIG_VIDEO_DEV