Linux 2.2.0
[davej-history.git] / drivers / char / mem.c
blob6eaa07d23e714e9daacb5a1edd929490771eccd4
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>
20 #include <asm/uaccess.h>
21 #include <asm/io.h>
22 #include <asm/pgtable.h>
24 #ifdef CONFIG_SOUND
25 void soundcore_init(void);
26 #ifdef CONFIG_SOUND_OSS
27 void soundcard_init(void);
28 #endif
29 #ifdef CONFIG_DMASOUND
30 void dmasound_init(void);
31 #endif
32 #endif
33 #ifdef CONFIG_SPARCAUDIO
34 extern int sparcaudio_init(void);
35 #endif
36 #ifdef CONFIG_ISDN
37 int isdn_init(void);
38 #endif
39 #ifdef CONFIG_VIDEO_DEV
40 extern int videodev_init(void);
41 #endif
42 #ifdef CONFIG_FB
43 extern void fbmem_init(void);
44 #endif
45 #ifdef CONFIG_PROM_CONSOLE
46 extern void prom_con_init(void);
47 #endif
48 #ifdef CONFIG_MDA_CONSOLE
49 extern void mda_console_init(void);
50 #endif
51 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
52 extern void adbdev_init(void);
53 #endif
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)
58 ssize_t written;
60 written = 0;
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? */
67 buf+=sz;
68 p+=sz;
69 count-=sz;
70 written+=sz;
72 #endif
73 if (copy_from_user(p, buf, count))
74 return -EFAULT;
75 written += count;
76 *ppos += written;
77 return written;
82 * This funcion reads the *physical* memory. The f_pos points directly to the
83 * memory location.
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;
90 ssize_t read;
92 end_mem = __pa(high_memory);
93 if (p >= end_mem)
94 return 0;
95 if (count > end_mem - p)
96 count = end_mem - p;
97 read = 0;
98 #if defined(__sparc__) || defined(__mc68000__)
99 /* we don't have page 0 mapped on sparc and m68k.. */
100 if (p < PAGE_SIZE) {
101 unsigned long sz = PAGE_SIZE-p;
102 if (sz > count)
103 sz = count;
104 if (sz > 0) {
105 if (clear_user(buf, sz))
106 return -EFAULT;
107 buf += sz;
108 p += sz;
109 count -= sz;
110 read += sz;
113 #endif
114 if (copy_to_user(buf, __va(p), count))
115 return -EFAULT;
116 read += count;
117 *ppos += read;
118 return read;
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);
128 if (p >= end_mem)
129 return 0;
130 if (count > end_mem - p)
131 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)
142 prot |= _PAGE_PCD;
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;
153 #endif
155 return prot;
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)
163 return -ENXIO;
166 * Accessing memory above the top the kernel knows about or
167 * through a file pointer that was marked O_SYNC will be
168 * done non-cached.
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
172 * file.
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,
179 vma->vm_page_prot))
180 return -EAGAIN;
181 vma->vm_file = file;
182 file->f_count++;
183 return 0;
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;
193 ssize_t read = 0;
194 ssize_t virtr;
196 if (p < (unsigned long) high_memory) {
197 read = count;
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);
207 buf += tmp;
208 p += tmp;
209 read -= tmp;
210 count -= tmp;
212 #endif
213 copy_to_user(buf, (char *)p, read);
214 p += read;
215 buf += read;
216 count -= read;
219 virtr = vread(buf, (char *)p, count);
220 if (virtr < 0)
221 return virtr;
222 *ppos += p + virtr;
223 return virtr + read;
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)
235 return 0;
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;
245 char *tmp = buf;
247 if (verify_area(VERIFY_WRITE,buf,count))
248 return -EFAULT;
249 while (count-- > 0 && i < 65536) {
250 if (__put_user(inb(i),tmp) < 0)
251 return -EFAULT;
252 i++;
253 tmp++;
255 *ppos = i;
256 return tmp-buf;
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))
266 return -EFAULT;
267 while (count-- > 0 && i < 65536) {
268 char c;
269 if (__get_user(c, tmp))
270 return -EFAULT;
271 outb(c,i);
272 i++;
273 tmp++;
275 *ppos = i;
276 return tmp-buf;
279 static ssize_t read_null(struct file * file, char * buf,
280 size_t count, loff_t *ppos)
282 return 0;
285 static ssize_t write_null(struct file * file, const char * buf,
286 size_t count, loff_t *ppos)
288 return count;
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;
300 mm = current->mm;
301 /* Oops, this was forgotten before. -ben */
302 down(&mm->mmap_sem);
304 /* For private mappings, just map in zero pages. */
305 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
306 unsigned long count;
308 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
309 goto out_up;
310 if (vma->vm_flags & VM_SHARED)
311 break;
312 count = vma->vm_end - addr;
313 if (count > size)
314 count = size;
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);
321 size -= count;
322 buf += count;
323 addr += count;
324 if (size == 0)
325 goto out_up;
328 up(&mm->mmap_sem);
330 /* The shared case is hard. Let's do the conventional zeroing. */
331 do {
332 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
333 if (unwritten)
334 return size + unwritten - PAGE_SIZE;
335 if (current->need_resched)
336 schedule();
337 buf += PAGE_SIZE;
338 size -= PAGE_SIZE;
339 } while (size);
341 return size;
342 out_up:
343 up(&mm->mmap_sem);
344 return size;
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;
352 if (!count)
353 return 0;
355 if (!access_ok(VERIFY_WRITE, buf, count))
356 return -EFAULT;
358 left = 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;
368 if (unwritten)
369 goto out;
370 left -= partial;
371 buf += partial;
372 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
373 written += (left & PAGE_MASK) - unwritten;
374 if (unwritten)
375 goto out;
376 buf += left & PAGE_MASK;
377 left &= ~PAGE_MASK;
379 unwritten = clear_user(buf, left);
380 written += left - unwritten;
381 out:
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)
388 return -EINVAL;
389 if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
390 return -EAGAIN;
391 return 0;
394 static ssize_t write_full(struct file * file, const char * buf,
395 size_t count, loff_t *ppos)
397 return -ENOSPC;
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.
403 * -- SRB.
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)
421 switch (orig) {
422 case 0:
423 file->f_pos = offset;
424 return file->f_pos;
425 case 1:
426 file->f_pos += offset;
427 return file->f_pos;
428 default:
429 return -EINVAL;
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 = {
440 memory_lseek,
441 read_mem,
442 write_mem,
443 NULL, /* mem_readdir */
444 NULL, /* mem_poll */
445 NULL, /* mem_ioctl */
446 mmap_mem,
447 NULL, /* no special open code */
448 NULL, /* flush */
449 NULL, /* no special release code */
450 NULL /* fsync */
453 static struct file_operations kmem_fops = {
454 memory_lseek,
455 read_kmem,
456 write_kmem,
457 NULL, /* kmem_readdir */
458 NULL, /* kmem_poll */
459 NULL, /* kmem_ioctl */
460 mmap_kmem,
461 NULL, /* no special open code */
462 NULL, /* flush */
463 NULL, /* no special release code */
464 NULL /* fsync */
467 static struct file_operations null_fops = {
468 null_lseek,
469 read_null,
470 write_null,
471 NULL, /* null_readdir */
472 NULL, /* null_poll */
473 NULL, /* null_ioctl */
474 NULL, /* null_mmap */
475 NULL, /* no special open code */
476 NULL, /* flush */
477 NULL, /* no special release code */
478 NULL /* fsync */
481 static struct file_operations port_fops = {
482 memory_lseek,
483 read_port,
484 write_port,
485 NULL, /* port_readdir */
486 NULL, /* port_poll */
487 NULL, /* port_ioctl */
488 NULL, /* port_mmap */
489 NULL, /* no special open code */
490 NULL, /* flush */
491 NULL, /* no special release code */
492 NULL /* fsync */
495 static struct file_operations zero_fops = {
496 zero_lseek,
497 read_zero,
498 write_zero,
499 NULL, /* zero_readdir */
500 NULL, /* zero_poll */
501 NULL, /* zero_ioctl */
502 mmap_zero,
503 NULL, /* no special open code */
504 NULL, /* flush */
505 NULL /* no special release code */
508 static struct file_operations full_fops = {
509 full_lseek,
510 read_full,
511 write_full,
512 NULL, /* full_readdir */
513 NULL, /* full_poll */
514 NULL, /* full_ioctl */
515 NULL, /* full_mmap */
516 NULL, /* no special open code */
517 NULL, /* flush */
518 NULL /* no special release code */
521 static int memory_open(struct inode * inode, struct file * filp)
523 switch (MINOR(inode->i_rdev)) {
524 case 1:
525 filp->f_op = &mem_fops;
526 break;
527 case 2:
528 filp->f_op = &kmem_fops;
529 break;
530 case 3:
531 filp->f_op = &null_fops;
532 break;
533 #if !defined(CONFIG_PPC) && !defined(__mc68000__)
534 case 4:
535 filp->f_op = &port_fops;
536 break;
537 #endif
538 case 5:
539 filp->f_op = &zero_fops;
540 break;
541 case 7:
542 filp->f_op = &full_fops;
543 break;
544 case 8:
545 filp->f_op = &random_fops;
546 break;
547 case 9:
548 filp->f_op = &urandom_fops;
549 break;
550 default:
551 return -ENXIO;
553 if (filp->f_op && filp->f_op->open)
554 return filp->f_op->open(inode,filp);
555 return 0;
558 static struct file_operations memory_fops = {
559 NULL, /* lseek */
560 NULL, /* read */
561 NULL, /* write */
562 NULL, /* readdir */
563 NULL, /* poll */
564 NULL, /* ioctl */
565 NULL, /* mmap */
566 memory_open, /* just a selector for the real open */
567 NULL, /* flush */
568 NULL, /* release */
569 NULL /* fsync */
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);
576 rand_initialize();
577 #if defined (CONFIG_FB)
578 fbmem_init();
579 #endif
580 #if defined (CONFIG_PROM_CONSOLE)
581 prom_con_init();
582 #endif
583 #if defined (CONFIG_MDA_CONSOLE)
584 mda_console_init();
585 #endif
586 tty_init();
587 #ifdef CONFIG_PRINTER
588 lp_init();
589 #endif
590 #ifdef CONFIG_M68K_PRINTER
591 lp_m68k_init();
592 #endif
593 misc_init();
594 #ifdef CONFIG_SOUND
595 soundcore_init();
596 #ifdef CONFIG_SOUND_OSS
597 soundcard_init();
598 #endif
599 #ifdef CONFIG_DMASOUND
600 dmasound_init();
601 #endif
602 #endif
603 #ifdef CONFIG_SPARCAUDIO
604 sparcaudio_init();
605 #endif
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.
611 js_init();
612 #endif
613 #if CONFIG_QIC02_TAPE
614 qic02_tape_init();
615 #endif
616 #if CONFIG_ISDN
617 isdn_init();
618 #endif
619 #ifdef CONFIG_FTAPE
620 ftape_init();
621 #endif
622 #ifdef CONFIG_VIDEO_BT848
623 i2c_init();
624 #endif
625 #if defined(CONFIG_PPC) || defined(CONFIG_MAC)
626 adbdev_init();
627 #endif
628 #ifdef CONFIG_VIDEO_DEV
629 videodev_init();
630 #endif
631 return 0;