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>
20 #include <asm/uaccess.h>
22 #include <asm/pgtable.h>
25 void soundcore_init(void);
26 #ifdef CONFIG_SOUND_OSS
27 void soundcard_init(void);
29 #ifdef CONFIG_DMASOUND
30 void dmasound_init(void);
33 #ifdef CONFIG_SPARCAUDIO
34 extern int sparcaudio_init(void);
39 #ifdef CONFIG_VIDEO_DEV
40 extern int videodev_init(void);
43 extern void fbmem_init(void);
45 #ifdef CONFIG_PROM_CONSOLE
46 extern void prom_con_init(void);
48 #ifdef CONFIG_MDA_CONSOLE
49 extern void mda_console_init(void);
51 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
52 extern void adbdev_init(void);
55 static ssize_t
do_write_mem(struct file
* file
, void *p
, unsigned long realp
,
56 const char * buf
, size_t count
, loff_t
*ppos
)
61 #if defined(__sparc__) || defined(__mc68000__)
62 /* we don't have page 0 mapped on sparc and m68k.. */
63 if (realp
< PAGE_SIZE
) {
64 unsigned long sz
= PAGE_SIZE
-realp
;
65 if (sz
> count
) sz
= count
;
66 /* Hmm. Do something? */
73 if (copy_from_user(p
, buf
, count
))
82 * This funcion reads the *physical* memory. The f_pos points directly to the
85 static ssize_t
read_mem(struct file
* file
, char * buf
,
86 size_t count
, loff_t
*ppos
)
88 unsigned long p
= *ppos
;
89 unsigned long end_mem
;
92 end_mem
= __pa(high_memory
);
95 if (count
> end_mem
- p
)
98 #if defined(__sparc__) || defined(__mc68000__)
99 /* we don't have page 0 mapped on sparc and m68k.. */
101 unsigned long sz
= PAGE_SIZE
-p
;
105 if (clear_user(buf
, sz
))
114 if (copy_to_user(buf
, __va(p
), count
))
121 static ssize_t
write_mem(struct file
* file
, const char * buf
,
122 size_t count
, loff_t
*ppos
)
124 unsigned long p
= *ppos
;
125 unsigned long end_mem
;
127 end_mem
= __pa(high_memory
);
130 if (count
> end_mem
- p
)
132 return do_write_mem(file
, __va(p
), p
, buf
, count
, ppos
);
136 * This should probably be per-architecture in <asm/pgtable.h>
138 static inline unsigned long pgprot_noncached(unsigned long prot
)
140 #if defined(__i386__)
141 if (boot_cpu_data
.x86
> 3)
143 #elif defined(__powerpc__)
144 prot
|= _PAGE_NO_CACHE
| _PAGE_GUARDED
;
145 #elif defined(__mc68000__)
146 if (CPU_IS_020_OR_030
)
147 prot
|= _PAGE_NOCACHE030
;
148 /* Use no-cache mode, serialized */
149 if (CPU_IS_040_OR_060
)
150 prot
= (prot
& _CACHEMASK040
) | _PAGE_NOCACHE_S
;
151 #elif defined(__mips__)
152 prot
= (prot
& ~_CACHE_MASK
) | _CACHE_UNCACHED
;
158 static int mmap_mem(struct file
* file
, struct vm_area_struct
* vma
)
160 unsigned long offset
= vma
->vm_offset
;
162 if (offset
& ~PAGE_MASK
)
166 * Accessing memory above the top the kernel knows about or
167 * through a file pointer that was marked O_SYNC will be
170 * Set VM_IO, as this is likely a non-cached access to an
171 * I/O area, and we don't want to include that in a core
174 if (offset
>= __pa(high_memory
) || (file
->f_flags
& O_SYNC
)) {
175 pgprot_val(vma
->vm_page_prot
) = pgprot_noncached(pgprot_val(vma
->vm_page_prot
));
176 vma
->vm_flags
|= VM_IO
;
178 if (remap_page_range(vma
->vm_start
, offset
, vma
->vm_end
-vma
->vm_start
,
187 * This function reads the *virtual* memory as seen by the kernel.
189 static ssize_t
read_kmem(struct file
*file
, char *buf
,
190 size_t count
, loff_t
*ppos
)
192 unsigned long p
= *ppos
;
196 if (p
< (unsigned long) high_memory
) {
198 if (count
> (unsigned long) high_memory
- p
)
199 read
= (unsigned long) high_memory
- p
;
201 #if defined(__sparc__) || defined(__mc68000__)
202 /* we don't have page 0 mapped on sparc and m68k.. */
203 if (p
< PAGE_SIZE
&& read
> 0) {
204 size_t tmp
= PAGE_SIZE
- p
;
205 if (tmp
> read
) tmp
= read
;
206 clear_user(buf
, tmp
);
213 copy_to_user(buf
, (char *)p
, read
);
219 virtr
= vread(buf
, (char *)p
, count
);
227 * This function writes to the *virtual* memory as seen by the kernel.
229 static ssize_t
write_kmem(struct file
* file
, const char * buf
,
230 size_t count
, loff_t
*ppos
)
232 unsigned long p
= *ppos
;
234 if (p
>= (unsigned long) high_memory
)
236 if (count
> (unsigned long) high_memory
- p
)
237 count
= (unsigned long) high_memory
- p
;
238 return do_write_mem(file
, (void*)p
, p
, buf
, count
, ppos
);
241 static ssize_t
read_port(struct file
* file
, char * buf
,
242 size_t count
, loff_t
*ppos
)
244 unsigned long i
= *ppos
;
247 if (verify_area(VERIFY_WRITE
,buf
,count
))
249 while (count
-- > 0 && i
< 65536) {
250 if (__put_user(inb(i
),tmp
) < 0)
259 static ssize_t
write_port(struct file
* file
, const char * buf
,
260 size_t count
, loff_t
*ppos
)
262 unsigned long i
= *ppos
;
263 const char * tmp
= buf
;
265 if (verify_area(VERIFY_READ
,buf
,count
))
267 while (count
-- > 0 && i
< 65536) {
269 if (__get_user(c
, tmp
))
279 static ssize_t
read_null(struct file
* file
, char * buf
,
280 size_t count
, loff_t
*ppos
)
285 static ssize_t
write_null(struct file
* file
, const char * buf
,
286 size_t count
, loff_t
*ppos
)
292 * For fun, we are using the MMU for this.
294 static inline size_t read_zero_pagealigned(char * buf
, size_t size
)
296 struct mm_struct
*mm
;
297 struct vm_area_struct
* vma
;
298 unsigned long addr
=(unsigned long)buf
;
301 /* Oops, this was forgotten before. -ben */
304 /* For private mappings, just map in zero pages. */
305 for (vma
= find_vma(mm
, addr
); vma
; vma
= vma
->vm_next
) {
308 if (vma
->vm_start
> addr
|| (vma
->vm_flags
& VM_WRITE
) == 0)
310 if (vma
->vm_flags
& VM_SHARED
)
312 count
= vma
->vm_end
- addr
;
316 flush_cache_range(mm
, addr
, addr
+ count
);
317 zap_page_range(mm
, addr
, count
);
318 zeromap_page_range(addr
, count
, PAGE_COPY
);
319 flush_tlb_range(mm
, addr
, addr
+ count
);
330 /* The shared case is hard. Let's do the conventional zeroing. */
332 unsigned long unwritten
= clear_user(buf
, PAGE_SIZE
);
334 return size
+ unwritten
- PAGE_SIZE
;
335 if (current
->need_resched
)
347 static ssize_t
read_zero(struct file
* file
, char * buf
,
348 size_t count
, loff_t
*ppos
)
350 unsigned long left
, unwritten
, written
= 0;
355 if (!access_ok(VERIFY_WRITE
, buf
, count
))
360 /* do we want to be clever? Arbitrary cut-off */
361 if (count
>= PAGE_SIZE
*4) {
362 unsigned long partial
;
364 /* How much left of the page? */
365 partial
= (PAGE_SIZE
-1) & -(unsigned long) buf
;
366 unwritten
= clear_user(buf
, partial
);
367 written
= partial
- unwritten
;
372 unwritten
= read_zero_pagealigned(buf
, left
& PAGE_MASK
);
373 written
+= (left
& PAGE_MASK
) - unwritten
;
376 buf
+= left
& PAGE_MASK
;
379 unwritten
= clear_user(buf
, left
);
380 written
+= left
- unwritten
;
382 return written
? written
: -EFAULT
;
385 static int mmap_zero(struct file
* file
, struct vm_area_struct
* vma
)
387 if (vma
->vm_flags
& VM_SHARED
)
389 if (zeromap_page_range(vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
))
394 static ssize_t
write_full(struct file
* file
, const char * buf
,
395 size_t count
, loff_t
*ppos
)
401 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
402 * can fopen() both devices with "a" now. This was previously impossible.
406 static loff_t
null_lseek(struct file
* file
, loff_t offset
, int orig
)
408 return file
->f_pos
= 0;
412 * The memory devices use the full 32/64 bits of the offset, and so we cannot
413 * check against negative addresses: they are ok. The return value is weird,
414 * though, in that case (0).
416 * also note that seeking relative to the "end of file" isn't supported:
417 * it has no meaning, so it returns -EINVAL.
419 static loff_t
memory_lseek(struct file
* file
, loff_t offset
, int orig
)
423 file
->f_pos
= offset
;
426 file
->f_pos
+= offset
;
433 #define mmap_kmem mmap_mem
434 #define zero_lseek null_lseek
435 #define full_lseek null_lseek
436 #define write_zero write_null
437 #define read_full read_zero
439 static struct file_operations mem_fops
= {
443 NULL
, /* mem_readdir */
445 NULL
, /* mem_ioctl */
447 NULL
, /* no special open code */
449 NULL
, /* no special release code */
453 static struct file_operations kmem_fops
= {
457 NULL
, /* kmem_readdir */
458 NULL
, /* kmem_poll */
459 NULL
, /* kmem_ioctl */
461 NULL
, /* no special open code */
463 NULL
, /* no special release code */
467 static struct file_operations null_fops
= {
471 NULL
, /* null_readdir */
472 NULL
, /* null_poll */
473 NULL
, /* null_ioctl */
474 NULL
, /* null_mmap */
475 NULL
, /* no special open code */
477 NULL
, /* no special release code */
481 static struct file_operations port_fops
= {
485 NULL
, /* port_readdir */
486 NULL
, /* port_poll */
487 NULL
, /* port_ioctl */
488 NULL
, /* port_mmap */
489 NULL
, /* no special open code */
491 NULL
, /* no special release code */
495 static struct file_operations zero_fops
= {
499 NULL
, /* zero_readdir */
500 NULL
, /* zero_poll */
501 NULL
, /* zero_ioctl */
503 NULL
, /* no special open code */
505 NULL
/* no special release code */
508 static struct file_operations full_fops
= {
512 NULL
, /* full_readdir */
513 NULL
, /* full_poll */
514 NULL
, /* full_ioctl */
515 NULL
, /* full_mmap */
516 NULL
, /* no special open code */
518 NULL
/* no special release code */
521 static int memory_open(struct inode
* inode
, struct file
* filp
)
523 switch (MINOR(inode
->i_rdev
)) {
525 filp
->f_op
= &mem_fops
;
528 filp
->f_op
= &kmem_fops
;
531 filp
->f_op
= &null_fops
;
533 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
535 filp
->f_op
= &port_fops
;
539 filp
->f_op
= &zero_fops
;
542 filp
->f_op
= &full_fops
;
545 filp
->f_op
= &random_fops
;
548 filp
->f_op
= &urandom_fops
;
553 if (filp
->f_op
&& filp
->f_op
->open
)
554 return filp
->f_op
->open(inode
,filp
);
558 static struct file_operations memory_fops
= {
566 memory_open
, /* just a selector for the real open */
572 __initfunc(int chr_dev_init(void))
574 if (register_chrdev(MEM_MAJOR
,"mem",&memory_fops
))
575 printk("unable to get major %d for memory devs\n", MEM_MAJOR
);
577 #if defined (CONFIG_FB)
580 #if defined (CONFIG_PROM_CONSOLE)
583 #if defined (CONFIG_MDA_CONSOLE)
587 #ifdef CONFIG_PRINTER
590 #ifdef CONFIG_M68K_PRINTER
596 #ifdef CONFIG_SOUND_OSS
599 #ifdef CONFIG_DMASOUND
603 #ifdef CONFIG_SPARCAUDIO
606 #ifdef CONFIG_JOYSTICK
608 * Some joysticks only appear when the sound card they are
609 * connected to is configured. Keep the sound/joystick ordering.
613 #if CONFIG_QIC02_TAPE
622 #ifdef CONFIG_VIDEO_BT848
625 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
628 #ifdef CONFIG_VIDEO_DEV