direct-io.hg

view linux-2.6-xen-sparse/drivers/char/mem.c @ 7547:03612b7f69d5

Don't use XendRoot.get_vif_bridge to provide a default bridge -- instead, pass
no bridge parameter and let the vif-bridge script find one for itself.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Sun Oct 30 13:45:06 2005 +0100 (2005-10-30)
parents 06d84bf87159
children 4b06313b9790
line source
1 /*
2 * linux/drivers/char/mem.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
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/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mman.h>
17 #include <linux/random.h>
18 #include <linux/init.h>
19 #include <linux/raw.h>
20 #include <linux/tty.h>
21 #include <linux/capability.h>
22 #include <linux/smp_lock.h>
23 #include <linux/devfs_fs_kernel.h>
24 #include <linux/ptrace.h>
25 #include <linux/device.h>
26 #include <linux/backing-dev.h>
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
31 #ifdef CONFIG_IA64
32 # include <linux/efi.h>
33 #endif
35 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
36 extern void tapechar_init(void);
37 #endif
39 /*
40 * Architectures vary in how they handle caching for addresses
41 * outside of main memory.
42 *
43 */
44 static inline int uncached_access(struct file *file, unsigned long addr)
45 {
46 #if defined(__i386__)
47 /*
48 * On the PPro and successors, the MTRRs are used to set
49 * memory types for physical addresses outside main memory,
50 * so blindly setting PCD or PWT on those pages is wrong.
51 * For Pentiums and earlier, the surround logic should disable
52 * caching for the high addresses through the KEN pin, but
53 * we maintain the tradition of paranoia in this code.
54 */
55 if (file->f_flags & O_SYNC)
56 return 1;
57 return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
58 test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
59 test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
60 test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
61 && addr >= __pa(high_memory);
62 #elif defined(__x86_64__)
63 /*
64 * This is broken because it can generate memory type aliases,
65 * which can cause cache corruptions
66 * But it is only available for root and we have to be bug-to-bug
67 * compatible with i386.
68 */
69 if (file->f_flags & O_SYNC)
70 return 1;
71 /* same behaviour as i386. PAT always set to cached and MTRRs control the
72 caching behaviour.
73 Hopefully a full PAT implementation will fix that soon. */
74 return 0;
75 #elif defined(CONFIG_IA64)
76 /*
77 * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
78 */
79 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
80 #else
81 /*
82 * Accessing memory above the top the kernel knows about or through a file pointer
83 * that was marked O_SYNC will be done non-cached.
84 */
85 if (file->f_flags & O_SYNC)
86 return 1;
87 return addr >= __pa(high_memory);
88 #endif
89 }
91 #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
92 static inline int valid_phys_addr_range(unsigned long addr, size_t *count)
93 {
94 unsigned long end_mem;
96 end_mem = __pa(high_memory);
97 if (addr >= end_mem)
98 return 0;
100 if (*count > end_mem - addr)
101 *count = end_mem - addr;
103 return 1;
104 }
105 #endif
107 #ifndef ARCH_HAS_DEV_MEM
108 /*
109 * This funcion reads the *physical* memory. The f_pos points directly to the
110 * memory location.
111 */
112 static ssize_t read_mem(struct file * file, char __user * buf,
113 size_t count, loff_t *ppos)
114 {
115 unsigned long p = *ppos;
116 ssize_t read, sz;
117 char *ptr;
119 if (!valid_phys_addr_range(p, &count))
120 return -EFAULT;
121 read = 0;
122 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
123 /* we don't have page 0 mapped on sparc and m68k.. */
124 if (p < PAGE_SIZE) {
125 sz = PAGE_SIZE - p;
126 if (sz > count)
127 sz = count;
128 if (sz > 0) {
129 if (clear_user(buf, sz))
130 return -EFAULT;
131 buf += sz;
132 p += sz;
133 count -= sz;
134 read += sz;
135 }
136 }
137 #endif
139 while (count > 0) {
140 /*
141 * Handle first page in case it's not aligned
142 */
143 if (-p & (PAGE_SIZE - 1))
144 sz = -p & (PAGE_SIZE - 1);
145 else
146 sz = PAGE_SIZE;
148 sz = min_t(unsigned long, sz, count);
150 /*
151 * On ia64 if a page has been mapped somewhere as
152 * uncached, then it must also be accessed uncached
153 * by the kernel or data corruption may occur
154 */
155 ptr = xlate_dev_mem_ptr(p);
157 if (copy_to_user(buf, ptr, sz))
158 return -EFAULT;
159 buf += sz;
160 p += sz;
161 count -= sz;
162 read += sz;
163 }
165 *ppos += read;
166 return read;
167 }
169 static ssize_t write_mem(struct file * file, const char __user * buf,
170 size_t count, loff_t *ppos)
171 {
172 unsigned long p = *ppos;
173 ssize_t written, sz;
174 unsigned long copied;
175 void *ptr;
177 if (!valid_phys_addr_range(p, &count))
178 return -EFAULT;
180 written = 0;
182 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
183 /* we don't have page 0 mapped on sparc and m68k.. */
184 if (p < PAGE_SIZE) {
185 unsigned long sz = PAGE_SIZE - p;
186 if (sz > count)
187 sz = count;
188 /* Hmm. Do something? */
189 buf += sz;
190 p += sz;
191 count -= sz;
192 written += sz;
193 }
194 #endif
196 while (count > 0) {
197 /*
198 * Handle first page in case it's not aligned
199 */
200 if (-p & (PAGE_SIZE - 1))
201 sz = -p & (PAGE_SIZE - 1);
202 else
203 sz = PAGE_SIZE;
205 sz = min_t(unsigned long, sz, count);
207 /*
208 * On ia64 if a page has been mapped somewhere as
209 * uncached, then it must also be accessed uncached
210 * by the kernel or data corruption may occur
211 */
212 ptr = xlate_dev_mem_ptr(p);
214 copied = copy_from_user(ptr, buf, sz);
215 if (copied) {
216 ssize_t ret;
218 ret = written + (sz - copied);
219 if (ret)
220 return ret;
221 return -EFAULT;
222 }
223 buf += sz;
224 p += sz;
225 count -= sz;
226 written += sz;
227 }
229 *ppos += written;
230 return written;
231 }
232 #endif
234 static int mmap_mem(struct file * file, struct vm_area_struct * vma)
235 {
236 #if defined(__HAVE_PHYS_MEM_ACCESS_PROT)
237 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
239 vma->vm_page_prot = phys_mem_access_prot(file, offset,
240 vma->vm_end - vma->vm_start,
241 vma->vm_page_prot);
242 #elif defined(pgprot_noncached)
243 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
244 int uncached;
246 uncached = uncached_access(file, offset);
247 if (uncached)
248 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
249 #endif
251 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
252 if (remap_pfn_range(vma,
253 vma->vm_start,
254 vma->vm_pgoff,
255 vma->vm_end-vma->vm_start,
256 vma->vm_page_prot))
257 return -EAGAIN;
258 return 0;
259 }
261 static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
262 {
263 unsigned long long val;
264 /*
265 * RED-PEN: on some architectures there is more mapped memory
266 * than available in mem_map which pfn_valid checks
267 * for. Perhaps should add a new macro here.
268 *
269 * RED-PEN: vmalloc is not supported right now.
270 */
271 if (!pfn_valid(vma->vm_pgoff))
272 return -EIO;
273 val = (u64)vma->vm_pgoff << PAGE_SHIFT;
274 vma->vm_pgoff = __pa(val) >> PAGE_SHIFT;
275 return mmap_mem(file, vma);
276 }
278 extern long vread(char *buf, char *addr, unsigned long count);
279 extern long vwrite(char *buf, char *addr, unsigned long count);
281 /*
282 * This function reads the *virtual* memory as seen by the kernel.
283 */
284 static ssize_t read_kmem(struct file *file, char __user *buf,
285 size_t count, loff_t *ppos)
286 {
287 unsigned long p = *ppos;
288 ssize_t low_count, read, sz;
289 char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
291 read = 0;
292 if (p < (unsigned long) high_memory) {
293 low_count = count;
294 if (count > (unsigned long) high_memory - p)
295 low_count = (unsigned long) high_memory - p;
297 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
298 /* we don't have page 0 mapped on sparc and m68k.. */
299 if (p < PAGE_SIZE && low_count > 0) {
300 size_t tmp = PAGE_SIZE - p;
301 if (tmp > low_count) tmp = low_count;
302 if (clear_user(buf, tmp))
303 return -EFAULT;
304 buf += tmp;
305 p += tmp;
306 read += tmp;
307 low_count -= tmp;
308 count -= tmp;
309 }
310 #endif
311 while (low_count > 0) {
312 /*
313 * Handle first page in case it's not aligned
314 */
315 if (-p & (PAGE_SIZE - 1))
316 sz = -p & (PAGE_SIZE - 1);
317 else
318 sz = PAGE_SIZE;
320 sz = min_t(unsigned long, sz, low_count);
322 /*
323 * On ia64 if a page has been mapped somewhere as
324 * uncached, then it must also be accessed uncached
325 * by the kernel or data corruption may occur
326 */
327 kbuf = xlate_dev_kmem_ptr((char *)p);
329 if (copy_to_user(buf, kbuf, sz))
330 return -EFAULT;
331 buf += sz;
332 p += sz;
333 read += sz;
334 low_count -= sz;
335 count -= sz;
336 }
337 }
339 if (count > 0) {
340 kbuf = (char *)__get_free_page(GFP_KERNEL);
341 if (!kbuf)
342 return -ENOMEM;
343 while (count > 0) {
344 int len = count;
346 if (len > PAGE_SIZE)
347 len = PAGE_SIZE;
348 len = vread(kbuf, (char *)p, len);
349 if (!len)
350 break;
351 if (copy_to_user(buf, kbuf, len)) {
352 free_page((unsigned long)kbuf);
353 return -EFAULT;
354 }
355 count -= len;
356 buf += len;
357 read += len;
358 p += len;
359 }
360 free_page((unsigned long)kbuf);
361 }
362 *ppos = p;
363 return read;
364 }
367 static inline ssize_t
368 do_write_kmem(void *p, unsigned long realp, const char __user * buf,
369 size_t count, loff_t *ppos)
370 {
371 ssize_t written, sz;
372 unsigned long copied;
374 written = 0;
375 #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
376 /* we don't have page 0 mapped on sparc and m68k.. */
377 if (realp < PAGE_SIZE) {
378 unsigned long sz = PAGE_SIZE - realp;
379 if (sz > count)
380 sz = count;
381 /* Hmm. Do something? */
382 buf += sz;
383 p += sz;
384 realp += sz;
385 count -= sz;
386 written += sz;
387 }
388 #endif
390 while (count > 0) {
391 char *ptr;
392 /*
393 * Handle first page in case it's not aligned
394 */
395 if (-realp & (PAGE_SIZE - 1))
396 sz = -realp & (PAGE_SIZE - 1);
397 else
398 sz = PAGE_SIZE;
400 sz = min_t(unsigned long, sz, count);
402 /*
403 * On ia64 if a page has been mapped somewhere as
404 * uncached, then it must also be accessed uncached
405 * by the kernel or data corruption may occur
406 */
407 ptr = xlate_dev_kmem_ptr(p);
409 copied = copy_from_user(ptr, buf, sz);
410 if (copied) {
411 ssize_t ret;
413 ret = written + (sz - copied);
414 if (ret)
415 return ret;
416 return -EFAULT;
417 }
418 buf += sz;
419 p += sz;
420 realp += sz;
421 count -= sz;
422 written += sz;
423 }
425 *ppos += written;
426 return written;
427 }
430 /*
431 * This function writes to the *virtual* memory as seen by the kernel.
432 */
433 static ssize_t write_kmem(struct file * file, const char __user * buf,
434 size_t count, loff_t *ppos)
435 {
436 unsigned long p = *ppos;
437 ssize_t wrote = 0;
438 ssize_t virtr = 0;
439 ssize_t written;
440 char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
442 if (p < (unsigned long) high_memory) {
444 wrote = count;
445 if (count > (unsigned long) high_memory - p)
446 wrote = (unsigned long) high_memory - p;
448 written = do_write_kmem((void*)p, p, buf, wrote, ppos);
449 if (written != wrote)
450 return written;
451 wrote = written;
452 p += wrote;
453 buf += wrote;
454 count -= wrote;
455 }
457 if (count > 0) {
458 kbuf = (char *)__get_free_page(GFP_KERNEL);
459 if (!kbuf)
460 return wrote ? wrote : -ENOMEM;
461 while (count > 0) {
462 int len = count;
464 if (len > PAGE_SIZE)
465 len = PAGE_SIZE;
466 if (len) {
467 written = copy_from_user(kbuf, buf, len);
468 if (written) {
469 ssize_t ret;
471 free_page((unsigned long)kbuf);
472 ret = wrote + virtr + (len - written);
473 return ret ? ret : -EFAULT;
474 }
475 }
476 len = vwrite(kbuf, (char *)p, len);
477 count -= len;
478 buf += len;
479 virtr += len;
480 p += len;
481 }
482 free_page((unsigned long)kbuf);
483 }
485 *ppos = p;
486 return virtr + wrote;
487 }
489 #if defined(CONFIG_ISA) || !defined(__mc68000__)
490 static ssize_t read_port(struct file * file, char __user * buf,
491 size_t count, loff_t *ppos)
492 {
493 unsigned long i = *ppos;
494 char __user *tmp = buf;
496 if (!access_ok(VERIFY_WRITE, buf, count))
497 return -EFAULT;
498 while (count-- > 0 && i < 65536) {
499 if (__put_user(inb(i),tmp) < 0)
500 return -EFAULT;
501 i++;
502 tmp++;
503 }
504 *ppos = i;
505 return tmp-buf;
506 }
508 static ssize_t write_port(struct file * file, const char __user * buf,
509 size_t count, loff_t *ppos)
510 {
511 unsigned long i = *ppos;
512 const char __user * tmp = buf;
514 if (!access_ok(VERIFY_READ,buf,count))
515 return -EFAULT;
516 while (count-- > 0 && i < 65536) {
517 char c;
518 if (__get_user(c, tmp))
519 return -EFAULT;
520 outb(c,i);
521 i++;
522 tmp++;
523 }
524 *ppos = i;
525 return tmp-buf;
526 }
527 #endif
529 static ssize_t read_null(struct file * file, char __user * buf,
530 size_t count, loff_t *ppos)
531 {
532 return 0;
533 }
535 static ssize_t write_null(struct file * file, const char __user * buf,
536 size_t count, loff_t *ppos)
537 {
538 return count;
539 }
541 #ifdef CONFIG_MMU
542 /*
543 * For fun, we are using the MMU for this.
544 */
545 static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
546 {
547 struct mm_struct *mm;
548 struct vm_area_struct * vma;
549 unsigned long addr=(unsigned long)buf;
551 mm = current->mm;
552 /* Oops, this was forgotten before. -ben */
553 down_read(&mm->mmap_sem);
555 /* For private mappings, just map in zero pages. */
556 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
557 unsigned long count;
559 if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
560 goto out_up;
561 if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
562 break;
563 count = vma->vm_end - addr;
564 if (count > size)
565 count = size;
567 zap_page_range(vma, addr, count, NULL);
568 zeromap_page_range(vma, addr, count, PAGE_COPY);
570 size -= count;
571 buf += count;
572 addr += count;
573 if (size == 0)
574 goto out_up;
575 }
577 up_read(&mm->mmap_sem);
579 /* The shared case is hard. Let's do the conventional zeroing. */
580 do {
581 unsigned long unwritten = clear_user(buf, PAGE_SIZE);
582 if (unwritten)
583 return size + unwritten - PAGE_SIZE;
584 cond_resched();
585 buf += PAGE_SIZE;
586 size -= PAGE_SIZE;
587 } while (size);
589 return size;
590 out_up:
591 up_read(&mm->mmap_sem);
592 return size;
593 }
595 static ssize_t read_zero(struct file * file, char __user * buf,
596 size_t count, loff_t *ppos)
597 {
598 unsigned long left, unwritten, written = 0;
600 if (!count)
601 return 0;
603 if (!access_ok(VERIFY_WRITE, buf, count))
604 return -EFAULT;
606 left = count;
608 /* do we want to be clever? Arbitrary cut-off */
609 if (count >= PAGE_SIZE*4) {
610 unsigned long partial;
612 /* How much left of the page? */
613 partial = (PAGE_SIZE-1) & -(unsigned long) buf;
614 unwritten = clear_user(buf, partial);
615 written = partial - unwritten;
616 if (unwritten)
617 goto out;
618 left -= partial;
619 buf += partial;
620 unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
621 written += (left & PAGE_MASK) - unwritten;
622 if (unwritten)
623 goto out;
624 buf += left & PAGE_MASK;
625 left &= ~PAGE_MASK;
626 }
627 unwritten = clear_user(buf, left);
628 written += left - unwritten;
629 out:
630 return written ? written : -EFAULT;
631 }
633 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
634 {
635 if (vma->vm_flags & VM_SHARED)
636 return shmem_zero_setup(vma);
637 if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
638 return -EAGAIN;
639 return 0;
640 }
641 #else /* CONFIG_MMU */
642 static ssize_t read_zero(struct file * file, char * buf,
643 size_t count, loff_t *ppos)
644 {
645 size_t todo = count;
647 while (todo) {
648 size_t chunk = todo;
650 if (chunk > 4096)
651 chunk = 4096; /* Just for latency reasons */
652 if (clear_user(buf, chunk))
653 return -EFAULT;
654 buf += chunk;
655 todo -= chunk;
656 cond_resched();
657 }
658 return count;
659 }
661 static int mmap_zero(struct file * file, struct vm_area_struct * vma)
662 {
663 return -ENOSYS;
664 }
665 #endif /* CONFIG_MMU */
667 static ssize_t write_full(struct file * file, const char __user * buf,
668 size_t count, loff_t *ppos)
669 {
670 return -ENOSPC;
671 }
673 /*
674 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
675 * can fopen() both devices with "a" now. This was previously impossible.
676 * -- SRB.
677 */
679 static loff_t null_lseek(struct file * file, loff_t offset, int orig)
680 {
681 return file->f_pos = 0;
682 }
684 /*
685 * The memory devices use the full 32/64 bits of the offset, and so we cannot
686 * check against negative addresses: they are ok. The return value is weird,
687 * though, in that case (0).
688 *
689 * also note that seeking relative to the "end of file" isn't supported:
690 * it has no meaning, so it returns -EINVAL.
691 */
692 static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
693 {
694 loff_t ret;
696 down(&file->f_dentry->d_inode->i_sem);
697 switch (orig) {
698 case 0:
699 file->f_pos = offset;
700 ret = file->f_pos;
701 force_successful_syscall_return();
702 break;
703 case 1:
704 file->f_pos += offset;
705 ret = file->f_pos;
706 force_successful_syscall_return();
707 break;
708 default:
709 ret = -EINVAL;
710 }
711 up(&file->f_dentry->d_inode->i_sem);
712 return ret;
713 }
715 static int open_port(struct inode * inode, struct file * filp)
716 {
717 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
718 }
720 #define zero_lseek null_lseek
721 #define full_lseek null_lseek
722 #define write_zero write_null
723 #define read_full read_zero
724 #define open_mem open_port
725 #define open_kmem open_mem
727 #ifndef ARCH_HAS_DEV_MEM
728 static struct file_operations mem_fops = {
729 .llseek = memory_lseek,
730 .read = read_mem,
731 .write = write_mem,
732 .mmap = mmap_mem,
733 .open = open_mem,
734 };
735 #else
736 extern struct file_operations mem_fops;
737 #endif
739 static struct file_operations kmem_fops = {
740 .llseek = memory_lseek,
741 .read = read_kmem,
742 .write = write_kmem,
743 .mmap = mmap_kmem,
744 .open = open_kmem,
745 };
747 static struct file_operations null_fops = {
748 .llseek = null_lseek,
749 .read = read_null,
750 .write = write_null,
751 };
753 #if defined(CONFIG_ISA) || !defined(__mc68000__)
754 static struct file_operations port_fops = {
755 .llseek = memory_lseek,
756 .read = read_port,
757 .write = write_port,
758 .open = open_port,
759 };
760 #endif
762 static struct file_operations zero_fops = {
763 .llseek = zero_lseek,
764 .read = read_zero,
765 .write = write_zero,
766 .mmap = mmap_zero,
767 };
769 static struct backing_dev_info zero_bdi = {
770 .capabilities = BDI_CAP_MAP_COPY,
771 };
773 static struct file_operations full_fops = {
774 .llseek = full_lseek,
775 .read = read_full,
776 .write = write_full,
777 };
779 static ssize_t kmsg_write(struct file * file, const char __user * buf,
780 size_t count, loff_t *ppos)
781 {
782 char *tmp;
783 int ret;
785 tmp = kmalloc(count + 1, GFP_KERNEL);
786 if (tmp == NULL)
787 return -ENOMEM;
788 ret = -EFAULT;
789 if (!copy_from_user(tmp, buf, count)) {
790 tmp[count] = 0;
791 ret = printk("%s", tmp);
792 }
793 kfree(tmp);
794 return ret;
795 }
797 static struct file_operations kmsg_fops = {
798 .write = kmsg_write,
799 };
801 static int memory_open(struct inode * inode, struct file * filp)
802 {
803 switch (iminor(inode)) {
804 case 1:
805 filp->f_op = &mem_fops;
806 break;
807 case 2:
808 filp->f_op = &kmem_fops;
809 break;
810 case 3:
811 filp->f_op = &null_fops;
812 break;
813 #if defined(CONFIG_ISA) || !defined(__mc68000__)
814 case 4:
815 filp->f_op = &port_fops;
816 break;
817 #endif
818 case 5:
819 filp->f_mapping->backing_dev_info = &zero_bdi;
820 filp->f_op = &zero_fops;
821 break;
822 case 7:
823 filp->f_op = &full_fops;
824 break;
825 case 8:
826 filp->f_op = &random_fops;
827 break;
828 case 9:
829 filp->f_op = &urandom_fops;
830 break;
831 case 11:
832 filp->f_op = &kmsg_fops;
833 break;
834 default:
835 return -ENXIO;
836 }
837 if (filp->f_op && filp->f_op->open)
838 return filp->f_op->open(inode,filp);
839 return 0;
840 }
842 static struct file_operations memory_fops = {
843 .open = memory_open, /* just a selector for the real open */
844 };
846 static const struct {
847 unsigned int minor;
848 char *name;
849 umode_t mode;
850 struct file_operations *fops;
851 } devlist[] = { /* list of minor devices */
852 {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
853 {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
854 {3, "null", S_IRUGO | S_IWUGO, &null_fops},
855 #if defined(CONFIG_ISA) || !defined(__mc68000__)
856 {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
857 #endif
858 {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
859 {7, "full", S_IRUGO | S_IWUGO, &full_fops},
860 {8, "random", S_IRUGO | S_IWUSR, &random_fops},
861 {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
862 {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
863 };
865 static struct class_simple *mem_class;
867 static int __init chr_dev_init(void)
868 {
869 int i;
871 if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
872 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
874 mem_class = class_simple_create(THIS_MODULE, "mem");
875 for (i = 0; i < ARRAY_SIZE(devlist); i++) {
876 class_simple_device_add(mem_class,
877 MKDEV(MEM_MAJOR, devlist[i].minor),
878 NULL, devlist[i].name);
879 devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
880 S_IFCHR | devlist[i].mode, devlist[i].name);
881 }
883 return 0;
884 }
886 fs_initcall(chr_dev_init);