Import 2.3.18pre1
[davej-history.git] / drivers / char / mem.c
blobff3158e96c960b46163fa108f77910dbb8ea8eab
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>
20 #include <linux/capability.h>
22 #include <asm/uaccess.h>
23 #include <asm/io.h>
24 #include <asm/pgtable.h>
26 #ifdef CONFIG_SOUND
27 void soundcore_init(void);
28 #ifdef CONFIG_SOUND_OSS
29 void soundcard_init(void);
30 #endif
31 #ifdef CONFIG_DMASOUND
32 void dmasound_init(void);
33 #endif
34 #endif
35 #ifdef CONFIG_SPARCAUDIO
36 extern int sparcaudio_init(void);
37 #endif
38 #ifdef CONFIG_ISDN
39 int isdn_init(void);
40 #endif
41 #ifdef CONFIG_VIDEO_DEV
42 extern int videodev_init(void);
43 #endif
44 #ifdef CONFIG_FB
45 extern void fbmem_init(void);
46 #endif
47 #ifdef CONFIG_PROM_CONSOLE
48 extern void prom_con_init(void);
49 #endif
50 #ifdef CONFIG_MDA_CONSOLE
51 extern void mda_console_init(void);
52 #endif
53 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
54 extern void adbdev_init(void);
55 #endif
56 #ifdef CONFIG_USB
57 extern void usb_init(void);
58 #endif
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)
63 ssize_t written;
65 written = 0;
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? */
72 buf+=sz;
73 p+=sz;
74 count-=sz;
75 written+=sz;
77 #endif
78 if (copy_from_user(p, buf, count))
79 return -EFAULT;
80 written += count;
81 *ppos += written;
82 return written;
87 * This funcion reads the *physical* memory. The f_pos points directly to the
88 * memory location.
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;
95 ssize_t read;
97 end_mem = __pa(high_memory);
98 if (p >= end_mem)
99 return 0;
100 if (count > end_mem - p)
101 count = end_mem - p;
102 read = 0;
103 #if defined(__sparc__) || defined(__mc68000__)
104 /* we don't have page 0 mapped on sparc and m68k.. */
105 if (p < PAGE_SIZE) {
106 unsigned long sz = PAGE_SIZE-p;
107 if (sz > count)
108 sz = count;
109 if (sz > 0) {
110 if (clear_user(buf, sz))
111 return -EFAULT;
112 buf += sz;
113 p += sz;
114 count -= sz;
115 read += sz;
118 #endif
119 if (copy_to_user(buf, __va(p), count))
120 return -EFAULT;
121 read += count;
122 *ppos += read;
123 return read;
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);
133 if (p >= end_mem)
134 return 0;
135 if (count > end_mem - p)
136 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;
161 #endif
163 return prot;
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);
183 #else
184 return addr >= __pa(high_memory);
185 #endif
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)
193 return -ENXIO;
196 * Accessing memory above the top the kernel knows about or
197 * through a file pointer that was marked O_SYNC will be
198 * done non-cached.
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,
211 vma->vm_page_prot))
212 return -EAGAIN;
213 return 0;
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;
223 ssize_t read = 0;
224 ssize_t virtr;
226 if (p < (unsigned long) high_memory) {
227 read = count;
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);
237 buf += tmp;
238 p += tmp;
239 read -= tmp;
240 count -= tmp;
242 #endif
243 copy_to_user(buf, (char *)p, read);
244 p += read;
245 buf += read;
246 count -= read;
249 virtr = vread(buf, (char *)p, count);
250 if (virtr < 0)
251 return virtr;
252 *ppos += p + virtr;
253 return virtr + read;
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)
265 return 0;
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;
276 char *tmp = buf;
278 if (verify_area(VERIFY_WRITE,buf,count))
279 return -EFAULT;
280 while (count-- > 0 && i < 65536) {
281 if (__put_user(inb(i),tmp) < 0)
282 return -EFAULT;
283 i++;
284 tmp++;
286 *ppos = i;
287 return tmp-buf;
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))
297 return -EFAULT;
298 while (count-- > 0 && i < 65536) {
299 char c;
300 if (__get_user(c, tmp))
301 return -EFAULT;
302 outb(c,i);
303 i++;
304 tmp++;
306 *ppos = i;
307 return tmp-buf;
309 #endif
311 static ssize_t read_null(struct file * file, char * buf,
312 size_t count, loff_t *ppos)
314 return 0;
317 static ssize_t write_null(struct file * file, const char * buf,
318 size_t count, loff_t *ppos)
320 return count;
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;
332 mm = current->mm;
333 /* Oops, this was forgotten before. -ben */
334 down(&mm->mmap_sem);
336 /* For private mappings, just map in zero pages. */
337 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
338 unsigned long count;
340 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
341 goto out_up;
342 if (vma->vm_flags & VM_SHARED)
343 break;
344 count = vma->vm_end - addr;
345 if (count > size)
346 count = size;
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);
353 size -= count;
354 buf += count;
355 addr += count;
356 if (size == 0)
357 goto out_up;
360 up(&mm->mmap_sem);
362 /* The shared case is hard. Let's do the conventional zeroing. */
363 do {
364 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
365 if (unwritten)
366 return size + unwritten - PAGE_SIZE;
367 if (current->need_resched)
368 schedule();
369 buf += PAGE_SIZE;
370 size -= PAGE_SIZE;
371 } while (size);
373 return size;
374 out_up:
375 up(&mm->mmap_sem);
376 return size;
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;
384 if (!count)
385 return 0;
387 if (!access_ok(VERIFY_WRITE, buf, count))
388 return -EFAULT;
390 left = 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;
400 if (unwritten)
401 goto out;
402 left -= partial;
403 buf += partial;
404 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
405 written += (left & PAGE_MASK) - unwritten;
406 if (unwritten)
407 goto out;
408 buf += left & PAGE_MASK;
409 left &= ~PAGE_MASK;
411 unwritten = clear_user(buf, left);
412 written += left - unwritten;
413 out:
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)
420 return -EINVAL;
421 if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
422 return -EAGAIN;
423 return 0;
426 static ssize_t write_full(struct file * file, const char * buf,
427 size_t count, loff_t *ppos)
429 return -ENOSPC;
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.
435 * -- SRB.
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)
453 switch (orig) {
454 case 0:
455 file->f_pos = offset;
456 return file->f_pos;
457 case 1:
458 file->f_pos += offset;
459 return file->f_pos;
460 default:
461 return -EINVAL;
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 = {
479 memory_lseek,
480 read_mem,
481 write_mem,
482 NULL, /* mem_readdir */
483 NULL, /* mem_poll */
484 NULL, /* mem_ioctl */
485 mmap_mem,
486 open_mem,
487 NULL, /* flush */
488 NULL, /* no special release code */
489 NULL /* fsync */
492 static struct file_operations kmem_fops = {
493 memory_lseek,
494 read_kmem,
495 write_kmem,
496 NULL, /* kmem_readdir */
497 NULL, /* kmem_poll */
498 NULL, /* kmem_ioctl */
499 mmap_kmem,
500 open_kmem,
501 NULL, /* flush */
502 NULL, /* no special release code */
503 NULL /* fsync */
506 static struct file_operations null_fops = {
507 null_lseek,
508 read_null,
509 write_null,
510 NULL, /* null_readdir */
511 NULL, /* null_poll */
512 NULL, /* null_ioctl */
513 NULL, /* null_mmap */
514 NULL, /* no special open code */
515 NULL, /* flush */
516 NULL, /* no special release code */
517 NULL /* fsync */
520 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
521 static struct file_operations port_fops = {
522 memory_lseek,
523 read_port,
524 write_port,
525 NULL, /* port_readdir */
526 NULL, /* port_poll */
527 NULL, /* port_ioctl */
528 NULL, /* port_mmap */
529 open_port,
530 NULL, /* flush */
531 NULL, /* no special release code */
532 NULL /* fsync */
534 #endif
536 static struct file_operations zero_fops = {
537 zero_lseek,
538 read_zero,
539 write_zero,
540 NULL, /* zero_readdir */
541 NULL, /* zero_poll */
542 NULL, /* zero_ioctl */
543 mmap_zero,
544 NULL, /* no special open code */
545 NULL, /* flush */
546 NULL /* no special release code */
549 static struct file_operations full_fops = {
550 full_lseek,
551 read_full,
552 write_full,
553 NULL, /* full_readdir */
554 NULL, /* full_poll */
555 NULL, /* full_ioctl */
556 NULL, /* full_mmap */
557 NULL, /* no special open code */
558 NULL, /* flush */
559 NULL /* no special release code */
562 static int memory_open(struct inode * inode, struct file * filp)
564 switch (MINOR(inode->i_rdev)) {
565 case 1:
566 filp->f_op = &mem_fops;
567 break;
568 case 2:
569 filp->f_op = &kmem_fops;
570 break;
571 case 3:
572 filp->f_op = &null_fops;
573 break;
574 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
575 case 4:
576 filp->f_op = &port_fops;
577 break;
578 #endif
579 case 5:
580 filp->f_op = &zero_fops;
581 break;
582 case 7:
583 filp->f_op = &full_fops;
584 break;
585 case 8:
586 filp->f_op = &random_fops;
587 break;
588 case 9:
589 filp->f_op = &urandom_fops;
590 break;
591 default:
592 return -ENXIO;
594 if (filp->f_op && filp->f_op->open)
595 return filp->f_op->open(inode,filp);
596 return 0;
599 static struct file_operations memory_fops = {
600 NULL, /* lseek */
601 NULL, /* read */
602 NULL, /* write */
603 NULL, /* readdir */
604 NULL, /* poll */
605 NULL, /* ioctl */
606 NULL, /* mmap */
607 memory_open, /* just a selector for the real open */
608 NULL, /* flush */
609 NULL, /* release */
610 NULL /* fsync */
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);
617 rand_initialize();
618 raw_init();
619 #ifdef CONFIG_USB
620 usb_init();
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 #ifdef CONFIG_SOUND
640 soundcore_init();
641 #ifdef CONFIG_SOUND_OSS
642 soundcard_init();
643 #endif
644 #ifdef CONFIG_DMASOUND
645 dmasound_init();
646 #endif
647 #endif
648 #ifdef CONFIG_SPARCAUDIO
649 sparcaudio_init();
650 #endif
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.
656 js_init();
657 #endif
658 #if CONFIG_QIC02_TAPE
659 qic02_tape_init();
660 #endif
661 #if CONFIG_ISDN
662 isdn_init();
663 #endif
664 #ifdef CONFIG_FTAPE
665 ftape_init();
666 #endif
667 #ifdef CONFIG_VIDEO_BT848
668 i2c_init();
669 #endif
670 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
671 adbdev_init();
672 #endif
673 #ifdef CONFIG_VIDEO_DEV
674 videodev_init();
675 #endif
676 return 0;