Import 2.1.118
[davej-history.git] / drivers / char / mem.c
blobfac10176b75c6dbeeacac4fd2e039c64eb11c0e6
1 /*
2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/major.h>
13 #include <linux/tty.h>
14 #include <linux/miscdevice.h>
15 #include <linux/tpqic02.h>
16 #include <linux/ftape.h>
17 #include <linux/malloc.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mman.h>
20 #include <linux/mm.h>
21 #include <linux/random.h>
22 #include <linux/init.h>
23 #include <linux/joystick.h>
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include <asm/pgtable.h>
29 #ifdef CONFIG_SOUND
30 void soundcore_init(void);
31 #ifdef CONFIG_SOUND_OSS
32 void soundcard_init(void);
33 #endif
34 #ifdef CONFIG_DMASOUND
35 void dmasound_init(void);
36 #endif
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
48 static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
49 const char * buf, size_t count, loff_t *ppos)
51 ssize_t written;
53 written = 0;
54 #if defined(__sparc__) || defined(__mc68000__)
55 /* we don't have page 0 mapped on sparc and m68k.. */
56 if (realp < PAGE_SIZE) {
57 unsigned long sz = PAGE_SIZE-realp;
58 if (sz > count) sz = count;
59 /* Hmm. Do something? */
60 buf+=sz;
61 p+=sz;
62 count-=sz;
63 written+=sz;
65 #endif
66 if (copy_from_user(p, buf, count))
67 return -EFAULT;
68 written += count;
69 *ppos += written;
70 return written;
75 * This funcion reads the *physical* memory. The f_pos points directly to the
76 * memory location.
78 static ssize_t read_mem(struct file * file, char * buf,
79 size_t count, loff_t *ppos)
81 unsigned long p = *ppos;
82 unsigned long end_mem;
83 ssize_t read;
85 end_mem = __pa(high_memory);
86 if (p >= end_mem)
87 return 0;
88 if (count > end_mem - p)
89 count = end_mem - p;
90 read = 0;
91 #if defined(__sparc__) || defined(__mc68000__)
92 /* we don't have page 0 mapped on sparc and m68k.. */
93 if (p < PAGE_SIZE) {
94 unsigned long sz = PAGE_SIZE-p;
95 if (sz > count)
96 sz = count;
97 if (sz > 0) {
98 if (clear_user(buf, sz))
99 return -EFAULT;
100 buf += sz;
101 p += sz;
102 count -= sz;
103 read += sz;
106 #endif
107 if (copy_to_user(buf, __va(p), count))
108 return -EFAULT;
109 read += count;
110 *ppos += read;
111 return read;
114 static ssize_t write_mem(struct file * file, const char * buf,
115 size_t count, loff_t *ppos)
117 unsigned long p = *ppos;
118 unsigned long end_mem;
120 end_mem = __pa(high_memory);
121 if (p >= end_mem)
122 return 0;
123 if (count > end_mem - p)
124 count = end_mem - p;
125 return do_write_mem(file, __va(p), p, buf, count, ppos);
128 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
130 unsigned long offset = vma->vm_offset;
132 if (offset & ~PAGE_MASK)
133 return -ENXIO;
134 #if defined(__i386__)
136 * hmm.. This disables high-memory caching, as the XFree86 team
137 * wondered about that at one time.
138 * The surround logic should disable caching for the high device
139 * addresses anyway, but right now this seems still needed.
141 if (boot_cpu_data.x86 > 3 && offset >= __pa(high_memory))
142 pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
143 #endif
144 #ifdef __powerpc__
145 if (offset >= __pa(high_memory))
146 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
147 #endif
148 if (remap_page_range(vma->vm_start, offset, vma->vm_end-vma->vm_start,
149 vma->vm_page_prot))
150 return -EAGAIN;
151 vma->vm_file = file;
152 file->f_count++;
153 return 0;
157 * This function reads the *virtual* memory as seen by the kernel.
159 static ssize_t read_kmem(struct file *file, char *buf,
160 size_t count, loff_t *ppos)
162 unsigned long p = *ppos;
163 ssize_t read = 0;
164 ssize_t virtr;
166 if (p < (unsigned long) high_memory) {
167 read = count;
168 if (count > (unsigned long) high_memory - p)
169 read = (unsigned long) high_memory - p;
171 #if defined(__sparc__) || defined(__mc68000__)
172 /* we don't have page 0 mapped on sparc and m68k.. */
173 if (p < PAGE_SIZE && read > 0) {
174 size_t tmp = PAGE_SIZE - p;
175 if (tmp > read) tmp = read;
176 clear_user(buf, tmp);
177 buf += tmp;
178 p += tmp;
179 read -= tmp;
180 count -= tmp;
182 #endif
183 copy_to_user(buf, (char *)p, read);
184 p += read;
185 buf += read;
186 count -= read;
189 virtr = vread(buf, (char *)p, count);
190 if (virtr < 0)
191 return virtr;
192 *ppos += p + virtr;
193 return virtr + read;
197 * This function writes to the *virtual* memory as seen by the kernel.
199 static ssize_t write_kmem(struct file * file, const char * buf,
200 size_t count, loff_t *ppos)
202 unsigned long p = *ppos;
204 if (p >= (unsigned long) high_memory)
205 return 0;
206 if (count > (unsigned long) high_memory - p)
207 count = (unsigned long) high_memory - p;
208 return do_write_mem(file, (void*)p, p, buf, count, ppos);
211 static ssize_t read_port(struct file * file, char * buf,
212 size_t count, loff_t *ppos)
214 unsigned long i = *ppos;
215 char *tmp = buf;
217 if (verify_area(VERIFY_WRITE,buf,count))
218 return -EFAULT;
219 while (count-- > 0 && i < 65536) {
220 if (__put_user(inb(i),tmp) < 0)
221 return -EFAULT;
222 i++;
223 tmp++;
225 *ppos = i;
226 return tmp-buf;
229 static ssize_t write_port(struct file * file, const char * buf,
230 size_t count, loff_t *ppos)
232 unsigned long i = *ppos;
233 const char * tmp = buf;
235 if (verify_area(VERIFY_READ,buf,count))
236 return -EFAULT;
237 while (count-- > 0 && i < 65536) {
238 char c;
239 if (__get_user(c, tmp))
240 return -EFAULT;
241 outb(c,i);
242 i++;
243 tmp++;
245 *ppos = i;
246 return tmp-buf;
249 static ssize_t read_null(struct file * file, char * buf,
250 size_t count, loff_t *ppos)
252 return 0;
255 static ssize_t write_null(struct file * file, const char * buf,
256 size_t count, loff_t *ppos)
258 return count;
262 * For fun, we are using the MMU for this.
264 static inline size_t read_zero_pagealigned(char * buf, size_t size)
266 struct mm_struct *mm;
267 struct vm_area_struct * vma;
268 unsigned long addr=(unsigned long)buf;
270 mm = current->mm;
271 /* Oops, this was forgotten before. -ben */
272 down(&mm->mmap_sem);
274 /* For private mappings, just map in zero pages. */
275 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
276 unsigned long count;
278 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
279 goto out_up;
280 if (vma->vm_flags & VM_SHARED)
281 break;
282 count = vma->vm_end - addr;
283 if (count > size)
284 count = size;
286 flush_cache_range(mm, addr, addr + count);
287 zap_page_range(mm, addr, count);
288 zeromap_page_range(addr, count, PAGE_COPY);
289 flush_tlb_range(mm, addr, addr + count);
291 size -= count;
292 buf += count;
293 addr += count;
294 if (size == 0)
295 goto out_up;
298 up(&mm->mmap_sem);
300 /* The shared case is hard. Let's do the conventional zeroing. */
301 do {
302 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
303 if (unwritten)
304 return size + unwritten - PAGE_SIZE;
305 if (current->need_resched)
306 schedule();
307 buf += PAGE_SIZE;
308 size -= PAGE_SIZE;
309 } while (size);
311 return size;
312 out_up:
313 up(&mm->mmap_sem);
314 return size;
317 static ssize_t read_zero(struct file * file, char * buf,
318 size_t count, loff_t *ppos)
320 unsigned long left, unwritten, written = 0;
322 if (!count)
323 return 0;
325 if (!access_ok(VERIFY_WRITE, buf, count))
326 return -EFAULT;
328 left = count;
330 /* do we want to be clever? Arbitrary cut-off */
331 if (count >= PAGE_SIZE*4) {
332 unsigned long partial;
334 /* How much left of the page? */
335 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
336 unwritten = clear_user(buf, partial);
337 written = partial - unwritten;
338 if (unwritten)
339 goto out;
340 left -= partial;
341 buf += partial;
342 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
343 written += (left & PAGE_MASK) - unwritten;
344 if (unwritten)
345 goto out;
346 buf += left & PAGE_MASK;
347 left &= ~PAGE_MASK;
349 unwritten = clear_user(buf, left);
350 written += left - unwritten;
351 out:
352 return written ? written : -EFAULT;
355 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
357 if (vma->vm_flags & VM_SHARED)
358 return -EINVAL;
359 if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
360 return -EAGAIN;
361 return 0;
364 static ssize_t write_full(struct file * file, const char * buf,
365 size_t count, loff_t *ppos)
367 return -ENOSPC;
371 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
372 * can fopen() both devices with "a" now. This was previously impossible.
373 * -- SRB.
376 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
378 return file->f_pos = 0;
382 * The memory devices use the full 32/64 bits of the offset, and so we cannot
383 * check against negative addresses: they are ok. The return value is weird,
384 * though, in that case (0).
386 * also note that seeking relative to the "end of file" isn't supported:
387 * it has no meaning, so it returns -EINVAL.
389 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
391 switch (orig) {
392 case 0:
393 file->f_pos = offset;
394 return file->f_pos;
395 case 1:
396 file->f_pos += offset;
397 return file->f_pos;
398 default:
399 return -EINVAL;
403 #define mmap_kmem mmap_mem
404 #define zero_lseek null_lseek
405 #define full_lseek null_lseek
406 #define write_zero write_null
407 #define read_full read_zero
409 static struct file_operations mem_fops = {
410 memory_lseek,
411 read_mem,
412 write_mem,
413 NULL, /* mem_readdir */
414 NULL, /* mem_poll */
415 NULL, /* mem_ioctl */
416 mmap_mem,
417 NULL, /* no special open code */
418 NULL, /* flush */
419 NULL, /* no special release code */
420 NULL /* fsync */
423 static struct file_operations kmem_fops = {
424 memory_lseek,
425 read_kmem,
426 write_kmem,
427 NULL, /* kmem_readdir */
428 NULL, /* kmem_poll */
429 NULL, /* kmem_ioctl */
430 mmap_kmem,
431 NULL, /* no special open code */
432 NULL, /* flush */
433 NULL, /* no special release code */
434 NULL /* fsync */
437 static struct file_operations null_fops = {
438 null_lseek,
439 read_null,
440 write_null,
441 NULL, /* null_readdir */
442 NULL, /* null_poll */
443 NULL, /* null_ioctl */
444 NULL, /* null_mmap */
445 NULL, /* no special open code */
446 NULL, /* flush */
447 NULL, /* no special release code */
448 NULL /* fsync */
451 static struct file_operations port_fops = {
452 memory_lseek,
453 read_port,
454 write_port,
455 NULL, /* port_readdir */
456 NULL, /* port_poll */
457 NULL, /* port_ioctl */
458 NULL, /* port_mmap */
459 NULL, /* no special open code */
460 NULL, /* flush */
461 NULL, /* no special release code */
462 NULL /* fsync */
465 static struct file_operations zero_fops = {
466 zero_lseek,
467 read_zero,
468 write_zero,
469 NULL, /* zero_readdir */
470 NULL, /* zero_poll */
471 NULL, /* zero_ioctl */
472 mmap_zero,
473 NULL, /* no special open code */
474 NULL, /* flush */
475 NULL /* no special release code */
478 static struct file_operations full_fops = {
479 full_lseek,
480 read_full,
481 write_full,
482 NULL, /* full_readdir */
483 NULL, /* full_poll */
484 NULL, /* full_ioctl */
485 NULL, /* full_mmap */
486 NULL, /* no special open code */
487 NULL, /* flush */
488 NULL /* no special release code */
491 static int memory_open(struct inode * inode, struct file * filp)
493 switch (MINOR(inode->i_rdev)) {
494 case 1:
495 filp->f_op = &mem_fops;
496 break;
497 case 2:
498 filp->f_op = &kmem_fops;
499 break;
500 case 3:
501 filp->f_op = &null_fops;
502 break;
503 case 4:
504 filp->f_op = &port_fops;
505 break;
506 case 5:
507 filp->f_op = &zero_fops;
508 break;
509 case 7:
510 filp->f_op = &full_fops;
511 break;
512 case 8:
513 filp->f_op = &random_fops;
514 break;
515 case 9:
516 filp->f_op = &urandom_fops;
517 break;
518 default:
519 return -ENXIO;
521 if (filp->f_op && filp->f_op->open)
522 return filp->f_op->open(inode,filp);
523 return 0;
526 static struct file_operations memory_fops = {
527 NULL, /* lseek */
528 NULL, /* read */
529 NULL, /* write */
530 NULL, /* readdir */
531 NULL, /* poll */
532 NULL, /* ioctl */
533 NULL, /* mmap */
534 memory_open, /* just a selector for the real open */
535 NULL, /* flush */
536 NULL, /* release */
537 NULL /* fsync */
540 __initfunc(int chr_dev_init(void))
542 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
543 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
544 rand_initialize();
545 #if defined (CONFIG_FB)
546 fbmem_init();
547 #endif
548 tty_init();
549 #ifdef CONFIG_PRINTER
550 lp_init();
551 #endif
552 #if defined (CONFIG_BUSMOUSE) || defined(CONFIG_UMISC) || \
553 defined (CONFIG_PSMOUSE) || defined (CONFIG_MS_BUSMOUSE) || \
554 defined (CONFIG_ATIXL_BUSMOUSE) || defined(CONFIG_SOFT_WATCHDOG) || \
555 defined (CONFIG_AMIGAMOUSE) || defined (CONFIG_ATARIMOUSE) || \
556 defined (CONFIG_MACMOUSE) || defined (CONFIG_PCWATCHDOG) || \
557 defined (CONFIG_APM) || defined (CONFIG_RTC) || \
558 defined (CONFIG_SUN_MOUSE) || defined (CONFIG_NVRAM)
559 misc_init();
560 #endif
561 #ifdef CONFIG_SOUND
562 soundcore_init();
563 #ifdef CONFIG_SOUND_OSS
564 soundcard_init();
565 #endif
566 #ifdef CONFIG_DMASOUND
567 dmasound_init();
568 #endif
569 #endif
570 #ifdef CONFIG_JOYSTICK
572 * Some joysticks only appear when the sound card they are
573 * connected to is configured. Keep the sound/joystick ordering.
575 js_init();
576 #endif
577 #if CONFIG_QIC02_TAPE
578 qic02_tape_init();
579 #endif
580 #if CONFIG_ISDN
581 isdn_init();
582 #endif
583 #ifdef CONFIG_FTAPE
584 ftape_init();
585 #endif
586 #ifdef CONFIG_VIDEO_BT848
587 i2c_init();
588 #endif
589 #ifdef CONFIG_VIDEO_DEV
590 videodev_init();
591 #endif
592 return 0;