Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / mem.c
blob3b5a2c4975e7dce12ca5762e4901f1b8d319f560
1 /*
2 * linux/drivers/char/mem.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Added devfs support.
7 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
8 * Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
9 */
11 #include <linux/config.h>
12 #include <linux/mm.h>
13 #include <linux/miscdevice.h>
14 #include <linux/tpqic02.h>
15 #include <linux/ftape.h>
16 #include <linux/malloc.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mman.h>
19 #include <linux/random.h>
20 #include <linux/init.h>
21 #include <linux/raw.h>
22 #include <linux/capability.h>
24 #include <asm/uaccess.h>
25 #include <asm/io.h>
26 #include <asm/pgalloc.h>
28 #ifdef CONFIG_I2C
29 extern int i2c_init_all(void);
30 #endif
31 #ifdef CONFIG_SOUND
32 void soundcore_init(void);
33 #ifdef CONFIG_SOUND_OSS
34 void soundcard_init(void);
35 #endif
36 #endif
37 #ifdef CONFIG_SPARCAUDIO
38 extern int sparcaudio_init(void);
39 #endif
40 #ifdef CONFIG_ISDN
41 int isdn_init(void);
42 #endif
43 #ifdef CONFIG_VIDEO_DEV
44 extern int videodev_init(void);
45 #endif
46 #ifdef CONFIG_FB
47 extern void fbmem_init(void);
48 #endif
49 #ifdef CONFIG_PROM_CONSOLE
50 extern void prom_con_init(void);
51 #endif
52 #ifdef CONFIG_MDA_CONSOLE
53 extern void mda_console_init(void);
54 #endif
55 #if defined(CONFIG_ADB)
56 extern void adbdev_init(void);
57 #endif
58 #ifdef CONFIG_PHONE
59 extern void telephony_init(void);
60 #endif
62 static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
63 const char * buf, size_t count, loff_t *ppos)
65 ssize_t written;
67 written = 0;
68 #if defined(__sparc__) || defined(__mc68000__)
69 /* we don't have page 0 mapped on sparc and m68k.. */
70 if (realp < PAGE_SIZE) {
71 unsigned long sz = PAGE_SIZE-realp;
72 if (sz > count) sz = count;
73 /* Hmm. Do something? */
74 buf+=sz;
75 p+=sz;
76 count-=sz;
77 written+=sz;
79 #endif
80 if (copy_from_user(p, buf, count))
81 return -EFAULT;
82 written += count;
83 *ppos += written;
84 return written;
89 * This funcion reads the *physical* memory. The f_pos points directly to the
90 * memory location.
92 static ssize_t read_mem(struct file * file, char * buf,
93 size_t count, loff_t *ppos)
95 unsigned long p = *ppos;
96 unsigned long end_mem;
97 ssize_t read;
99 end_mem = __pa(high_memory);
100 if (p >= end_mem)
101 return 0;
102 if (count > end_mem - p)
103 count = end_mem - p;
104 read = 0;
105 #if defined(__sparc__) || defined(__mc68000__)
106 /* we don't have page 0 mapped on sparc and m68k.. */
107 if (p < PAGE_SIZE) {
108 unsigned long sz = PAGE_SIZE-p;
109 if (sz > count)
110 sz = count;
111 if (sz > 0) {
112 if (clear_user(buf, sz))
113 return -EFAULT;
114 buf += sz;
115 p += sz;
116 count -= sz;
117 read += sz;
120 #endif
121 if (copy_to_user(buf, __va(p), count))
122 return -EFAULT;
123 read += count;
124 *ppos += read;
125 return read;
128 static ssize_t write_mem(struct file * file, const char * buf,
129 size_t count, loff_t *ppos)
131 unsigned long p = *ppos;
132 unsigned long end_mem;
134 end_mem = __pa(high_memory);
135 if (p >= end_mem)
136 return 0;
137 if (count > end_mem - p)
138 count = end_mem - p;
139 return do_write_mem(file, __va(p), p, buf, count, ppos);
142 #ifndef pgprot_noncached
145 * This should probably be per-architecture in <asm/pgtable.h>
147 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
149 unsigned long prot = pgprot_val(_prot);
151 #if defined(__i386__)
152 /* On PPro and successors, PCD alone doesn't always mean
153 uncached because of interactions with the MTRRs. PCD | PWT
154 means definitely uncached. */
155 if (boot_cpu_data.x86 > 3)
156 prot |= _PAGE_PCD | _PAGE_PWT;
157 #elif defined(__powerpc__)
158 prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
159 #elif defined(__mc68000__)
160 if (MMU_IS_SUN3)
161 prot |= SUN3_PAGE_NOCACHE;
162 else if (MMU_IS_851 || MMU_IS_030)
163 prot |= _PAGE_NOCACHE030;
164 /* Use no-cache mode, serialized */
165 else if (MMU_IS_040 || MMU_IS_060)
166 prot = (prot & _CACHEMASK040) | _PAGE_NOCACHE_S;
167 #elif defined(__mips__)
168 prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
169 #elif defined(__arm__) && defined(CONFIG_CPU_32)
170 /* Turn off caching for all I/O areas */
171 prot &= ~(L_PTE_CACHEABLE | L_PTE_BUFFERABLE);
172 #endif
174 return __pgprot(prot);
177 #endif /* !pgprot_noncached */
180 * Architectures vary in how they handle caching for addresses
181 * outside of main memory.
183 static inline int noncached_address(unsigned long addr)
185 #if defined(__i386__)
187 * On the PPro and successors, the MTRRs are used to set
188 * memory types for physical addresses outside main memory,
189 * so blindly setting PCD or PWT on those pages is wrong.
190 * For Pentiums and earlier, the surround logic should disable
191 * caching for the high addresses through the KEN pin, but
192 * we maintain the tradition of paranoia in this code.
194 return !(boot_cpu_data.x86_capability & X86_FEATURE_MTRR)
195 && addr >= __pa(high_memory);
196 #else
197 return addr >= __pa(high_memory);
198 #endif
201 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
203 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
206 * Accessing memory above the top the kernel knows about or
207 * through a file pointer that was marked O_SYNC will be
208 * done non-cached.
210 if (noncached_address(offset) || (file->f_flags & O_SYNC))
211 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
214 * Don't dump addresses that are not real memory to a core file.
216 if (offset >= __pa(high_memory) || (file->f_flags & O_SYNC))
217 vma->vm_flags |= VM_IO;
219 if (remap_page_range(vma->vm_start, offset, vma->vm_end-vma->vm_start,
220 vma->vm_page_prot))
221 return -EAGAIN;
222 return 0;
226 * This function reads the *virtual* memory as seen by the kernel.
228 static ssize_t read_kmem(struct file *file, char *buf,
229 size_t count, loff_t *ppos)
231 unsigned long p = *ppos;
232 ssize_t read = 0;
233 ssize_t virtr = 0;
234 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
236 if (p < (unsigned long) high_memory) {
237 read = count;
238 if (count > (unsigned long) high_memory - p)
239 read = (unsigned long) high_memory - p;
241 #if defined(__sparc__) || defined(__mc68000__)
242 /* we don't have page 0 mapped on sparc and m68k.. */
243 if (p < PAGE_SIZE && read > 0) {
244 size_t tmp = PAGE_SIZE - p;
245 if (tmp > read) tmp = read;
246 if (clear_user(buf, tmp))
247 return -EFAULT;
248 buf += tmp;
249 p += tmp;
250 read -= tmp;
251 count -= tmp;
253 #endif
254 if (copy_to_user(buf, (char *)p, read))
255 return -EFAULT;
256 p += read;
257 buf += read;
258 count -= read;
261 kbuf = (char *)__get_free_page(GFP_KERNEL);
262 if (!kbuf)
263 return -ENOMEM;
264 while (count > 0) {
265 int len = count;
267 if (len > PAGE_SIZE)
268 len = PAGE_SIZE;
269 len = vread(kbuf, (char *)p, len);
270 if (len && copy_to_user(buf, kbuf, len)) {
271 free_page((unsigned long)kbuf);
272 return -EFAULT;
274 count -= len;
275 buf += len;
276 virtr += len;
277 p += len;
279 free_page((unsigned long)kbuf);
280 *ppos = p;
281 return virtr + read;
285 * This function writes to the *virtual* memory as seen by the kernel.
287 static ssize_t write_kmem(struct file * file, const char * buf,
288 size_t count, loff_t *ppos)
290 unsigned long p = *ppos;
292 if (p >= (unsigned long) high_memory)
293 return 0;
294 if (count > (unsigned long) high_memory - p)
295 count = (unsigned long) high_memory - p;
296 return do_write_mem(file, (void*)p, p, buf, count, ppos);
299 #ifdef CONFIG_ISA
300 static ssize_t read_port(struct file * file, char * buf,
301 size_t count, loff_t *ppos)
303 unsigned long i = *ppos;
304 char *tmp = buf;
306 if (verify_area(VERIFY_WRITE,buf,count))
307 return -EFAULT;
308 while (count-- > 0 && i < 65536) {
309 if (__put_user(inb(i),tmp) < 0)
310 return -EFAULT;
311 i++;
312 tmp++;
314 *ppos = i;
315 return tmp-buf;
318 static ssize_t write_port(struct file * file, const char * buf,
319 size_t count, loff_t *ppos)
321 unsigned long i = *ppos;
322 const char * tmp = buf;
324 if (verify_area(VERIFY_READ,buf,count))
325 return -EFAULT;
326 while (count-- > 0 && i < 65536) {
327 char c;
328 if (__get_user(c, tmp))
329 return -EFAULT;
330 outb(c,i);
331 i++;
332 tmp++;
334 *ppos = i;
335 return tmp-buf;
337 #endif
339 static ssize_t read_null(struct file * file, char * buf,
340 size_t count, loff_t *ppos)
342 return 0;
345 static ssize_t write_null(struct file * file, const char * buf,
346 size_t count, loff_t *ppos)
348 return count;
352 * For fun, we are using the MMU for this.
354 static inline size_t read_zero_pagealigned(char * buf, size_t size)
356 struct mm_struct *mm;
357 struct vm_area_struct * vma;
358 unsigned long addr=(unsigned long)buf;
360 mm = current->mm;
361 /* Oops, this was forgotten before. -ben */
362 down(&mm->mmap_sem);
364 /* For private mappings, just map in zero pages. */
365 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
366 unsigned long count;
368 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
369 goto out_up;
370 if (vma->vm_flags & VM_SHARED)
371 break;
372 count = vma->vm_end - addr;
373 if (count > size)
374 count = size;
376 flush_cache_range(mm, addr, addr + count);
377 zap_page_range(mm, addr, count);
378 zeromap_page_range(addr, count, PAGE_COPY);
379 flush_tlb_range(mm, addr, addr + count);
381 size -= count;
382 buf += count;
383 addr += count;
384 if (size == 0)
385 goto out_up;
388 up(&mm->mmap_sem);
390 /* The shared case is hard. Let's do the conventional zeroing. */
391 do {
392 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
393 if (unwritten)
394 return size + unwritten - PAGE_SIZE;
395 if (current->need_resched)
396 schedule();
397 buf += PAGE_SIZE;
398 size -= PAGE_SIZE;
399 } while (size);
401 return size;
402 out_up:
403 up(&mm->mmap_sem);
404 return size;
407 static ssize_t read_zero(struct file * file, char * buf,
408 size_t count, loff_t *ppos)
410 unsigned long left, unwritten, written = 0;
412 if (!count)
413 return 0;
415 if (!access_ok(VERIFY_WRITE, buf, count))
416 return -EFAULT;
418 left = count;
420 /* do we want to be clever? Arbitrary cut-off */
421 if (count >= PAGE_SIZE*4) {
422 unsigned long partial;
424 /* How much left of the page? */
425 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
426 unwritten = clear_user(buf, partial);
427 written = partial - unwritten;
428 if (unwritten)
429 goto out;
430 left -= partial;
431 buf += partial;
432 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
433 written += (left & PAGE_MASK) - unwritten;
434 if (unwritten)
435 goto out;
436 buf += left & PAGE_MASK;
437 left &= ~PAGE_MASK;
439 unwritten = clear_user(buf, left);
440 written += left - unwritten;
441 out:
442 return written ? written : -EFAULT;
445 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
447 if (vma->vm_flags & VM_SHARED)
448 return map_zero_setup(vma);
449 if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
450 return -EAGAIN;
451 return 0;
454 static ssize_t write_full(struct file * file, const char * buf,
455 size_t count, loff_t *ppos)
457 return -ENOSPC;
461 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
462 * can fopen() both devices with "a" now. This was previously impossible.
463 * -- SRB.
466 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
468 return file->f_pos = 0;
472 * The memory devices use the full 32/64 bits of the offset, and so we cannot
473 * check against negative addresses: they are ok. The return value is weird,
474 * though, in that case (0).
476 * also note that seeking relative to the "end of file" isn't supported:
477 * it has no meaning, so it returns -EINVAL.
479 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
481 switch (orig) {
482 case 0:
483 file->f_pos = offset;
484 return file->f_pos;
485 case 1:
486 file->f_pos += offset;
487 return file->f_pos;
488 default:
489 return -EINVAL;
493 static int open_port(struct inode * inode, struct file * filp)
495 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
498 #define mmap_kmem mmap_mem
499 #define zero_lseek null_lseek
500 #define full_lseek null_lseek
501 #define write_zero write_null
502 #define read_full read_zero
503 #define open_mem open_port
504 #define open_kmem open_mem
506 static struct file_operations mem_fops = {
507 llseek: memory_lseek,
508 read: read_mem,
509 write: write_mem,
510 mmap: mmap_mem,
511 open: open_mem,
514 static struct file_operations kmem_fops = {
515 llseek: memory_lseek,
516 read: read_kmem,
517 write: write_kmem,
518 mmap: mmap_kmem,
519 open: open_kmem,
522 static struct file_operations null_fops = {
523 llseek: null_lseek,
524 read: read_null,
525 write: write_null,
528 #ifdef CONFIG_ISA
529 static struct file_operations port_fops = {
530 llseek: memory_lseek,
531 read: read_port,
532 write: write_port,
533 open: open_port,
535 #endif
537 static struct file_operations zero_fops = {
538 llseek: zero_lseek,
539 read: read_zero,
540 write: write_zero,
541 mmap: mmap_zero,
544 static struct file_operations full_fops = {
545 llseek: full_lseek,
546 read: read_full,
547 write: write_full,
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 #ifdef CONFIG_ISA
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 void __init memory_devfs_register (void)
589 /* These are never unregistered */
590 static const struct {
591 unsigned short minor;
592 char *name;
593 umode_t mode;
594 struct file_operations *fops;
595 } list[] = { /* list of minor devices */
596 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
597 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
598 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
599 #ifdef CONFIG_ISA
600 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
601 #endif
602 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
603 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
604 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
605 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops}
607 int i;
609 for (i=0; i<(sizeof(list)/sizeof(*list)); i++)
610 devfs_register (NULL, list[i].name, DEVFS_FL_NONE,
611 MEM_MAJOR, list[i].minor,
612 list[i].mode | S_IFCHR,
613 list[i].fops, NULL);
616 static struct file_operations memory_fops = {
617 open: memory_open, /* just a selector for the real open */
620 int __init chr_dev_init(void)
622 if (devfs_register_chrdev(MEM_MAJOR,"mem",&memory_fops))
623 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
624 memory_devfs_register();
625 rand_initialize();
626 raw_init();
627 #ifdef CONFIG_I2C
628 i2c_init_all();
629 #endif
630 #if defined (CONFIG_FB)
631 fbmem_init();
632 #endif
633 #if defined (CONFIG_PROM_CONSOLE)
634 prom_con_init();
635 #endif
636 #if defined (CONFIG_MDA_CONSOLE)
637 mda_console_init();
638 #endif
639 tty_init();
640 #ifdef CONFIG_PRINTER
641 lp_init();
642 #endif
643 #ifdef CONFIG_M68K_PRINTER
644 lp_m68k_init();
645 #endif
646 misc_init();
647 #ifdef CONFIG_SOUND
648 soundcore_init();
649 #ifdef CONFIG_SOUND_OSS
650 soundcard_init();
651 #endif
652 #endif
653 #ifdef CONFIG_SPARCAUDIO
654 sparcaudio_init();
655 #endif
656 #if CONFIG_QIC02_TAPE
657 qic02_tape_init();
658 #endif
659 #if CONFIG_ISDN
660 isdn_init();
661 #endif
662 #ifdef CONFIG_FTAPE
663 ftape_init();
664 #endif
665 #if defined(CONFIG_ADB)
666 adbdev_init();
667 #endif
668 #ifdef CONFIG_VIDEO_DEV
669 videodev_init();
670 #endif
671 #ifdef CONFIG_PHONE
672 telephony_init();
673 #endif
674 return 0;