Import 2.3.13pre7
[davej-history.git] / drivers / char / mem.c
blobfd123de44e72a05be2bbdd414c6a27f28fcdfef2
1 /*
2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/mm.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>
22 #include <asm/io.h>
23 #include <asm/pgtable.h>
25 #ifdef CONFIG_SOUND
26 void soundcore_init(void);
27 #ifdef CONFIG_SOUND_OSS
28 void soundcard_init(void);
29 #endif
30 #ifdef CONFIG_DMASOUND
31 void dmasound_init(void);
32 #endif
33 #endif
34 #ifdef CONFIG_SPARCAUDIO
35 extern int sparcaudio_init(void);
36 #endif
37 #ifdef CONFIG_ISDN
38 int isdn_init(void);
39 #endif
40 #ifdef CONFIG_VIDEO_DEV
41 extern int videodev_init(void);
42 #endif
43 #ifdef CONFIG_FB
44 extern void fbmem_init(void);
45 #endif
46 #ifdef CONFIG_PROM_CONSOLE
47 extern void prom_con_init(void);
48 #endif
49 #ifdef CONFIG_MDA_CONSOLE
50 extern void mda_console_init(void);
51 #endif
52 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
53 extern void adbdev_init(void);
54 #endif
55 #ifdef CONFIG_USB
56 extern void usb_init(void);
57 #endif
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)
62 ssize_t written;
64 written = 0;
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? */
71 buf+=sz;
72 p+=sz;
73 count-=sz;
74 written+=sz;
76 #endif
77 if (copy_from_user(p, buf, count))
78 return -EFAULT;
79 written += count;
80 *ppos += written;
81 return written;
86 * This funcion reads the *physical* memory. The f_pos points directly to the
87 * memory location.
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;
94 ssize_t read;
96 end_mem = __pa(high_memory);
97 if (p >= end_mem)
98 return 0;
99 if (count > end_mem - p)
100 count = end_mem - p;
101 read = 0;
102 #if defined(__sparc__) || defined(__mc68000__)
103 /* we don't have page 0 mapped on sparc and m68k.. */
104 if (p < PAGE_SIZE) {
105 unsigned long sz = PAGE_SIZE-p;
106 if (sz > count)
107 sz = count;
108 if (sz > 0) {
109 if (clear_user(buf, sz))
110 return -EFAULT;
111 buf += sz;
112 p += sz;
113 count -= sz;
114 read += sz;
117 #endif
118 if (copy_to_user(buf, __va(p), count))
119 return -EFAULT;
120 read += count;
121 *ppos += read;
122 return read;
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);
132 if (p >= end_mem)
133 return 0;
134 if (count > end_mem - p)
135 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;
160 #endif
162 return prot;
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);
182 #else
183 return addr >= __pa(high_memory);
184 #endif
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)
192 return -ENXIO;
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.
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,
210 vma->vm_page_prot))
211 return -EAGAIN;
212 return 0;
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;
222 ssize_t read = 0;
223 ssize_t virtr;
225 if (p < (unsigned long) high_memory) {
226 read = count;
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);
236 buf += tmp;
237 p += tmp;
238 read -= tmp;
239 count -= tmp;
241 #endif
242 copy_to_user(buf, (char *)p, read);
243 p += read;
244 buf += read;
245 count -= read;
248 virtr = vread(buf, (char *)p, count);
249 if (virtr < 0)
250 return virtr;
251 *ppos += p + virtr;
252 return virtr + read;
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)
264 return 0;
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;
274 char *tmp = buf;
276 if (verify_area(VERIFY_WRITE,buf,count))
277 return -EFAULT;
278 while (count-- > 0 && i < 65536) {
279 if (__put_user(inb(i),tmp) < 0)
280 return -EFAULT;
281 i++;
282 tmp++;
284 *ppos = i;
285 return tmp-buf;
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))
295 return -EFAULT;
296 while (count-- > 0 && i < 65536) {
297 char c;
298 if (__get_user(c, tmp))
299 return -EFAULT;
300 outb(c,i);
301 i++;
302 tmp++;
304 *ppos = i;
305 return tmp-buf;
308 static ssize_t read_null(struct file * file, char * buf,
309 size_t count, loff_t *ppos)
311 return 0;
314 static ssize_t write_null(struct file * file, const char * buf,
315 size_t count, loff_t *ppos)
317 return count;
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;
329 mm = current->mm;
330 /* Oops, this was forgotten before. -ben */
331 down(&mm->mmap_sem);
333 /* For private mappings, just map in zero pages. */
334 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
335 unsigned long count;
337 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
338 goto out_up;
339 if (vma->vm_flags & VM_SHARED)
340 break;
341 count = vma->vm_end - addr;
342 if (count > size)
343 count = size;
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);
350 size -= count;
351 buf += count;
352 addr += count;
353 if (size == 0)
354 goto out_up;
357 up(&mm->mmap_sem);
359 /* The shared case is hard. Let's do the conventional zeroing. */
360 do {
361 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
362 if (unwritten)
363 return size + unwritten - PAGE_SIZE;
364 if (current->need_resched)
365 schedule();
366 buf += PAGE_SIZE;
367 size -= PAGE_SIZE;
368 } while (size);
370 return size;
371 out_up:
372 up(&mm->mmap_sem);
373 return size;
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;
381 if (!count)
382 return 0;
384 if (!access_ok(VERIFY_WRITE, buf, count))
385 return -EFAULT;
387 left = 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;
397 if (unwritten)
398 goto out;
399 left -= partial;
400 buf += partial;
401 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
402 written += (left & PAGE_MASK) - unwritten;
403 if (unwritten)
404 goto out;
405 buf += left & PAGE_MASK;
406 left &= ~PAGE_MASK;
408 unwritten = clear_user(buf, left);
409 written += left - unwritten;
410 out:
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)
417 return -EINVAL;
418 if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
419 return -EAGAIN;
420 return 0;
423 static ssize_t write_full(struct file * file, const char * buf,
424 size_t count, loff_t *ppos)
426 return -ENOSPC;
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.
432 * -- SRB.
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)
450 switch (orig) {
451 case 0:
452 file->f_pos = offset;
453 return file->f_pos;
454 case 1:
455 file->f_pos += offset;
456 return file->f_pos;
457 default:
458 return -EINVAL;
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 = {
469 memory_lseek,
470 read_mem,
471 write_mem,
472 NULL, /* mem_readdir */
473 NULL, /* mem_poll */
474 NULL, /* mem_ioctl */
475 mmap_mem,
476 NULL, /* no special open code */
477 NULL, /* flush */
478 NULL, /* no special release code */
479 NULL /* fsync */
482 static struct file_operations kmem_fops = {
483 memory_lseek,
484 read_kmem,
485 write_kmem,
486 NULL, /* kmem_readdir */
487 NULL, /* kmem_poll */
488 NULL, /* kmem_ioctl */
489 mmap_kmem,
490 NULL, /* no special open code */
491 NULL, /* flush */
492 NULL, /* no special release code */
493 NULL /* fsync */
496 static struct file_operations null_fops = {
497 null_lseek,
498 read_null,
499 write_null,
500 NULL, /* null_readdir */
501 NULL, /* null_poll */
502 NULL, /* null_ioctl */
503 NULL, /* null_mmap */
504 NULL, /* no special open code */
505 NULL, /* flush */
506 NULL, /* no special release code */
507 NULL /* fsync */
510 static struct file_operations port_fops = {
511 memory_lseek,
512 read_port,
513 write_port,
514 NULL, /* port_readdir */
515 NULL, /* port_poll */
516 NULL, /* port_ioctl */
517 NULL, /* port_mmap */
518 NULL, /* no special open code */
519 NULL, /* flush */
520 NULL, /* no special release code */
521 NULL /* fsync */
524 static struct file_operations zero_fops = {
525 zero_lseek,
526 read_zero,
527 write_zero,
528 NULL, /* zero_readdir */
529 NULL, /* zero_poll */
530 NULL, /* zero_ioctl */
531 mmap_zero,
532 NULL, /* no special open code */
533 NULL, /* flush */
534 NULL /* no special release code */
537 static struct file_operations full_fops = {
538 full_lseek,
539 read_full,
540 write_full,
541 NULL, /* full_readdir */
542 NULL, /* full_poll */
543 NULL, /* full_ioctl */
544 NULL, /* full_mmap */
545 NULL, /* no special open code */
546 NULL, /* flush */
547 NULL /* no special release code */
550 static int memory_open(struct inode * inode, struct file * filp)
552 switch (MINOR(inode->i_rdev)) {
553 case 1:
554 filp->f_op = &mem_fops;
555 break;
556 case 2:
557 filp->f_op = &kmem_fops;
558 break;
559 case 3:
560 filp->f_op = &null_fops;
561 break;
562 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
563 case 4:
564 filp->f_op = &port_fops;
565 break;
566 #endif
567 case 5:
568 filp->f_op = &zero_fops;
569 break;
570 case 7:
571 filp->f_op = &full_fops;
572 break;
573 case 8:
574 filp->f_op = &random_fops;
575 break;
576 case 9:
577 filp->f_op = &urandom_fops;
578 break;
579 default:
580 return -ENXIO;
582 if (filp->f_op && filp->f_op->open)
583 return filp->f_op->open(inode,filp);
584 return 0;
587 static struct file_operations memory_fops = {
588 NULL, /* lseek */
589 NULL, /* read */
590 NULL, /* write */
591 NULL, /* readdir */
592 NULL, /* poll */
593 NULL, /* ioctl */
594 NULL, /* mmap */
595 memory_open, /* just a selector for the real open */
596 NULL, /* flush */
597 NULL, /* release */
598 NULL /* fsync */
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);
605 rand_initialize();
606 raw_init();
607 #ifdef CONFIG_USB
608 usb_init();
609 #endif
610 #if defined (CONFIG_FB)
611 fbmem_init();
612 #endif
613 #if defined (CONFIG_PROM_CONSOLE)
614 prom_con_init();
615 #endif
616 #if defined (CONFIG_MDA_CONSOLE)
617 mda_console_init();
618 #endif
619 tty_init();
620 #ifdef CONFIG_PRINTER
621 lp_init();
622 #endif
623 #ifdef CONFIG_M68K_PRINTER
624 lp_m68k_init();
625 #endif
626 misc_init();
627 #ifdef CONFIG_SOUND
628 soundcore_init();
629 #ifdef CONFIG_SOUND_OSS
630 soundcard_init();
631 #endif
632 #ifdef CONFIG_DMASOUND
633 dmasound_init();
634 #endif
635 #endif
636 #ifdef CONFIG_SPARCAUDIO
637 sparcaudio_init();
638 #endif
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.
644 js_init();
645 #endif
646 #if CONFIG_QIC02_TAPE
647 qic02_tape_init();
648 #endif
649 #if CONFIG_ISDN
650 isdn_init();
651 #endif
652 #ifdef CONFIG_FTAPE
653 ftape_init();
654 #endif
655 #ifdef CONFIG_VIDEO_BT848
656 i2c_init();
657 #endif
658 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
659 adbdev_init();
660 #endif
661 #ifdef CONFIG_VIDEO_DEV
662 videodev_init();
663 #endif
664 return 0;