blob: f895a8c8a244b54c02eb8b59b0eba8d42734a011 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/mem.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
Andrew Mortond7d4d842010-03-10 15:21:52 -08006 * Added devfs support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008 * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/miscdevice.h>
13#include <linux/slab.h>
14#include <linux/vmalloc.h>
15#include <linux/mman.h>
16#include <linux/random.h>
17#include <linux/init.h>
18#include <linux/raw.h>
19#include <linux/tty.h>
20#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/ptrace.h>
22#include <linux/device.h>
Vivek Goyal50b1fdb2005-06-25 14:58:23 -070023#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/backing-dev.h>
Vivek Goyal315c2152005-06-25 14:58:24 -070025#include <linux/bootmem.h>
Jens Axboed6b29d72007-06-04 09:59:47 +020026#include <linux/splice.h>
Linus Torvaldsb8a3ad52006-10-13 08:42:10 -070027#include <linux/pfn.h>
Paul Gortmaker66300e62011-07-10 12:14:53 -040028#include <linux/export.h>
Haren Mynenie1612de2012-07-11 15:18:44 +100029#include <linux/io.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070030#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#ifdef CONFIG_IA64
35# include <linux/efi.h>
36#endif
37
Haren Mynenie1612de2012-07-11 15:18:44 +100038#define DEVPORT_MINOR 4
39
Wu Fengguangf2223182009-12-14 17:58:07 -080040static inline unsigned long size_inside_page(unsigned long start,
41 unsigned long size)
42{
43 unsigned long sz;
44
Wu Fengguang7fabadd2009-12-14 17:58:09 -080045 sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
Wu Fengguangf2223182009-12-14 17:58:07 -080046
Wu Fengguang7fabadd2009-12-14 17:58:09 -080047 return min(sz, size);
Wu Fengguangf2223182009-12-14 17:58:07 -080048}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040051static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Changli Gaocfaf346c2011-03-23 16:42:58 -070053 return addr + count <= __pa(high_memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080055
Lennert Buytenhek06c67be2006-07-10 04:45:27 -070056static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
Bjorn Helgaas80851ef2006-01-08 01:04:13 -080057{
58 return 1;
59}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
Ingo Molnard0926332008-07-18 00:26:59 +020062#ifdef CONFIG_STRICT_DEVMEM
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080063static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020064{
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080065 u64 from = ((u64)pfn) << PAGE_SHIFT;
66 u64 to = from + size;
67 u64 cursor = from;
Arjan van de Venae531c22008-04-24 23:40:47 +020068
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080069 while (cursor < to) {
70 if (!devmem_is_allowed(pfn)) {
71 printk(KERN_INFO
72 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
Arjan van de Venae531c22008-04-24 23:40:47 +020073 current->comm, from, to);
74 return 0;
75 }
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080076 cursor += PAGE_SIZE;
77 pfn++;
Arjan van de Venae531c22008-04-24 23:40:47 +020078 }
79 return 1;
80}
81#else
Venki Pallipadie2beb3e2008-03-06 23:01:47 -080082static inline int range_is_allowed(unsigned long pfn, unsigned long size)
Arjan van de Venae531c22008-04-24 23:40:47 +020083{
84 return 1;
85}
86#endif
87
Andrew Mortond7d4d842010-03-10 15:21:52 -080088void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -070089{
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
Andrew Mortond7d4d842010-03-10 15:21:52 -080093 * This funcion reads the *physical* memory. The f_pos points directly to the
94 * memory location.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Andrew Mortond7d4d842010-03-10 15:21:52 -080096static ssize_t read_mem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 size_t count, loff_t *ppos)
98{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -040099 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 ssize_t read, sz;
101 char *ptr;
102
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800103 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EFAULT;
105 read = 0;
106#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
107 /* we don't have page 0 mapped on sparc and m68k.. */
108 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800109 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (sz > 0) {
111 if (clear_user(buf, sz))
112 return -EFAULT;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800113 buf += sz;
114 p += sz;
115 count -= sz;
116 read += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 }
119#endif
120
121 while (count > 0) {
Wu Fengguangfa29e972009-12-14 17:58:08 -0800122 unsigned long remaining;
123
Wu Fengguangf2223182009-12-14 17:58:07 -0800124 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700126 if (!range_is_allowed(p >> PAGE_SHIFT, count))
127 return -EPERM;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800130 * On ia64 if a page has been mapped somewhere as uncached, then
131 * it must also be accessed uncached by the kernel or data
132 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700135 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700137
Wu Fengguangfa29e972009-12-14 17:58:08 -0800138 remaining = copy_to_user(buf, ptr, sz);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700139 unxlate_dev_mem_ptr(p, ptr);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800140 if (remaining)
141 return -EFAULT;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 buf += sz;
144 p += sz;
145 count -= sz;
146 read += sz;
147 }
148
149 *ppos += read;
150 return read;
151}
152
Andrew Mortond7d4d842010-03-10 15:21:52 -0800153static ssize_t write_mem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 size_t count, loff_t *ppos)
155{
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400156 phys_addr_t p = *ppos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ssize_t written, sz;
158 unsigned long copied;
159 void *ptr;
160
Bjorn Helgaas136939a2006-03-26 01:37:05 -0800161 if (!valid_phys_addr_range(p, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EFAULT;
163
164 written = 0;
165
166#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
167 /* we don't have page 0 mapped on sparc and m68k.. */
168 if (p < PAGE_SIZE) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800169 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Hmm. Do something? */
171 buf += sz;
172 p += sz;
173 count -= sz;
174 written += sz;
175 }
176#endif
177
178 while (count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800179 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700181 if (!range_is_allowed(p >> PAGE_SHIFT, sz))
182 return -EPERM;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800185 * On ia64 if a page has been mapped somewhere as uncached, then
186 * it must also be accessed uncached by the kernel or data
187 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
189 ptr = xlate_dev_mem_ptr(p);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700190 if (!ptr) {
Jan Beulichc654d602006-03-25 03:07:31 -0800191 if (written)
192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -EFAULT;
194 }
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700195
196 copied = copy_from_user(ptr, buf, sz);
Wu Fengguangfa29e972009-12-14 17:58:08 -0800197 unxlate_dev_mem_ptr(p, ptr);
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700198 if (copied) {
199 written += sz - copied;
venkatesh.pallipadi@intel.come045fb22008-03-18 17:00:15 -0700200 if (written)
201 break;
202 return -EFAULT;
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 buf += sz;
206 p += sz;
207 count -= sz;
208 written += sz;
209 }
210
211 *ppos += written;
212 return written;
213}
214
Andrew Mortond7d4d842010-03-10 15:21:52 -0800215int __weak phys_mem_access_prot_allowed(struct file *file,
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700216 unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
217{
218 return 1;
219}
220
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800221#ifndef __HAVE_PHYS_MEM_ACCESS_PROT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800222
223/*
224 * Architectures vary in how they handle caching for addresses
225 * outside of main memory.
226 *
227 */
David Howellsea56f412010-04-06 14:35:08 -0700228#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400229static int uncached_access(struct file *file, phys_addr_t addr)
Andrew Mortond7d4d842010-03-10 15:21:52 -0800230{
231#if defined(CONFIG_IA64)
232 /*
233 * On ia64, we ignore O_DSYNC because we cannot tolerate memory
234 * attribute aliases.
235 */
236 return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
237#elif defined(CONFIG_MIPS)
238 {
239 extern int __uncached_access(struct file *file,
240 unsigned long addr);
241
242 return __uncached_access(file, addr);
243 }
244#else
245 /*
246 * Accessing memory above the top the kernel knows about or through a
247 * file pointer
248 * that was marked O_DSYNC will be done non-cached.
249 */
250 if (file->f_flags & O_DSYNC)
251 return 1;
252 return addr >= __pa(high_memory);
253#endif
254}
David Howellsea56f412010-04-06 14:35:08 -0700255#endif
Andrew Mortond7d4d842010-03-10 15:21:52 -0800256
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800257static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
258 unsigned long size, pgprot_t vma_prot)
259{
260#ifdef pgprot_noncached
Cyril Chemparathy7e6735c2012-09-12 14:05:58 -0400261 phys_addr_t offset = pfn << PAGE_SHIFT;
Bjorn Helgaas44ac8412006-01-08 01:04:10 -0800262
263 if (uncached_access(file, offset))
264 return pgprot_noncached(vma_prot);
265#endif
266 return vma_prot;
267}
268#endif
269
David Howells5da61852006-09-27 01:50:16 -0700270#ifndef CONFIG_MMU
271static unsigned long get_unmapped_area_mem(struct file *file,
272 unsigned long addr,
273 unsigned long len,
274 unsigned long pgoff,
275 unsigned long flags)
276{
277 if (!valid_mmap_phys_addr_range(pgoff, len))
278 return (unsigned long) -EINVAL;
Benjamin Herrenschmidt8a932582007-04-16 22:53:16 -0700279 return pgoff << PAGE_SHIFT;
David Howells5da61852006-09-27 01:50:16 -0700280}
281
282/* can't do an in-place private mapping if there's no MMU */
283static inline int private_mapping_ok(struct vm_area_struct *vma)
284{
285 return vma->vm_flags & VM_MAYSHARE;
286}
287#else
288#define get_unmapped_area_mem NULL
289
290static inline int private_mapping_ok(struct vm_area_struct *vma)
291{
292 return 1;
293}
294#endif
295
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400296static const struct vm_operations_struct mmap_mem_ops = {
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700297#ifdef CONFIG_HAVE_IOREMAP_PROT
298 .access = generic_access_phys
299#endif
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700300};
301
Andrew Mortond7d4d842010-03-10 15:21:52 -0800302static int mmap_mem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800304 size_t size = vma->vm_end - vma->vm_start;
305
Lennert Buytenhek06c67be2006-07-10 04:45:27 -0700306 if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800307 return -EINVAL;
308
David Howells5da61852006-09-27 01:50:16 -0700309 if (!private_mapping_ok(vma))
310 return -ENOSYS;
311
Venki Pallipadie2beb3e2008-03-06 23:01:47 -0800312 if (!range_is_allowed(vma->vm_pgoff, size))
313 return -EPERM;
314
venkatesh.pallipadi@intel.comf0970c12008-03-18 17:00:20 -0700315 if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
316 &vma->vm_page_prot))
317 return -EINVAL;
318
Roland Dreier8b150472005-10-28 17:46:18 -0700319 vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800320 size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700323 vma->vm_ops = &mmap_mem_ops;
324
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700325 /* Remap-pfn-range will mark the range VM_IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (remap_pfn_range(vma,
327 vma->vm_start,
328 vma->vm_pgoff,
Bjorn Helgaas80851ef2006-01-08 01:04:13 -0800329 size,
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700330 vma->vm_page_prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EAGAIN;
venkatesh.pallipadi@intel.come7f260a2008-03-18 17:00:21 -0700332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700336#ifdef CONFIG_DEVKMEM
Andrew Mortond7d4d842010-03-10 15:21:52 -0800337static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Linus Torvalds4bb82552005-08-13 14:22:59 -0700339 unsigned long pfn;
340
Linus Torvalds6d3154c2007-01-22 08:53:24 -0800341 /* Turn a kernel-virtual address into a physical page frame */
342 pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800345 * RED-PEN: on some architectures there is more mapped memory than
346 * available in mem_map which pfn_valid checks for. Perhaps should add a
347 * new macro here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
349 * RED-PEN: vmalloc is not supported right now.
350 */
Linus Torvalds4bb82552005-08-13 14:22:59 -0700351 if (!pfn_valid(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -EIO;
Linus Torvalds4bb82552005-08-13 14:22:59 -0700353
354 vma->vm_pgoff = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return mmap_mem(file, vma);
356}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700357#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700359#ifdef CONFIG_DEVKMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * This function reads the *virtual* memory as seen by the kernel.
362 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800363static ssize_t read_kmem(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 size_t count, loff_t *ppos)
365{
366 unsigned long p = *ppos;
367 ssize_t low_count, read, sz;
Hans Grob890537b2013-02-06 11:37:20 +0100368 char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800369 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 read = 0;
372 if (p < (unsigned long) high_memory) {
373 low_count = count;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800374 if (count > (unsigned long)high_memory - p)
375 low_count = (unsigned long)high_memory - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
378 /* we don't have page 0 mapped on sparc and m68k.. */
379 if (p < PAGE_SIZE && low_count > 0) {
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800380 sz = size_inside_page(p, low_count);
381 if (clear_user(buf, sz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EFAULT;
Wu Fengguang7fabadd2009-12-14 17:58:09 -0800383 buf += sz;
384 p += sz;
385 read += sz;
386 low_count -= sz;
387 count -= sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389#endif
390 while (low_count > 0) {
Wu Fengguangf2223182009-12-14 17:58:07 -0800391 sz = size_inside_page(p, low_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /*
394 * On ia64 if a page has been mapped somewhere as
395 * uncached, then it must also be accessed uncached
396 * by the kernel or data corruption may occur
397 */
398 kbuf = xlate_dev_kmem_ptr((char *)p);
399
400 if (copy_to_user(buf, kbuf, sz))
401 return -EFAULT;
402 buf += sz;
403 p += sz;
404 read += sz;
405 low_count -= sz;
406 count -= sz;
407 }
408 }
409
410 if (count > 0) {
411 kbuf = (char *)__get_free_page(GFP_KERNEL);
412 if (!kbuf)
413 return -ENOMEM;
414 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800415 sz = size_inside_page(p, count);
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800416 if (!is_vmalloc_or_module_addr((void *)p)) {
417 err = -ENXIO;
418 break;
419 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800420 sz = vread(kbuf, (char *)p, sz);
421 if (!sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800423 if (copy_to_user(buf, kbuf, sz)) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800424 err = -EFAULT;
425 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800427 count -= sz;
428 buf += sz;
429 read += sz;
430 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 free_page((unsigned long)kbuf);
433 }
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800434 *ppos = p;
435 return read ? read : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438
Andrew Mortond7d4d842010-03-10 15:21:52 -0800439static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
440 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 ssize_t written, sz;
443 unsigned long copied;
444
445 written = 0;
446#ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
447 /* we don't have page 0 mapped on sparc and m68k.. */
Wu Fengguangee323982009-12-14 17:58:10 -0800448 if (p < PAGE_SIZE) {
449 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Hmm. Do something? */
451 buf += sz;
452 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 count -= sz;
454 written += sz;
455 }
456#endif
457
458 while (count > 0) {
459 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Wu Fengguangee323982009-12-14 17:58:10 -0800461 sz = size_inside_page(p, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /*
Andrew Mortond7d4d842010-03-10 15:21:52 -0800464 * On ia64 if a page has been mapped somewhere as uncached, then
465 * it must also be accessed uncached by the kernel or data
466 * corruption may occur.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
Wu Fengguangee323982009-12-14 17:58:10 -0800468 ptr = xlate_dev_kmem_ptr((char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 copied = copy_from_user(ptr, buf, sz);
471 if (copied) {
Jan Beulichc654d602006-03-25 03:07:31 -0800472 written += sz - copied;
473 if (written)
474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EFAULT;
476 }
477 buf += sz;
478 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 count -= sz;
480 written += sz;
481 }
482
483 *ppos += written;
484 return written;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * This function writes to the *virtual* memory as seen by the kernel.
489 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800490static ssize_t write_kmem(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 size_t count, loff_t *ppos)
492{
493 unsigned long p = *ppos;
494 ssize_t wrote = 0;
495 ssize_t virtr = 0;
Hans Grob890537b2013-02-06 11:37:20 +0100496 char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800497 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (p < (unsigned long) high_memory) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800500 unsigned long to_write = min_t(unsigned long, count,
501 (unsigned long)high_memory - p);
Wu Fengguangee323982009-12-14 17:58:10 -0800502 wrote = do_write_kmem(p, buf, to_write, ppos);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800503 if (wrote != to_write)
504 return wrote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 p += wrote;
506 buf += wrote;
507 count -= wrote;
508 }
509
510 if (count > 0) {
511 kbuf = (char *)__get_free_page(GFP_KERNEL);
512 if (!kbuf)
513 return wrote ? wrote : -ENOMEM;
514 while (count > 0) {
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800515 unsigned long sz = size_inside_page(p, count);
516 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800518 if (!is_vmalloc_or_module_addr((void *)p)) {
519 err = -ENXIO;
520 break;
521 }
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800522 n = copy_from_user(kbuf, buf, sz);
523 if (n) {
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800524 err = -EFAULT;
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Wu Fengguangc85e9a92010-02-02 13:44:06 -0800527 vwrite(kbuf, (char *)p, sz);
Wu Fengguang80ad89a2009-12-14 17:58:10 -0800528 count -= sz;
529 buf += sz;
530 virtr += sz;
531 p += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 free_page((unsigned long)kbuf);
534 }
535
KAMEZAWA Hiroyuki325fda72010-02-02 13:44:05 -0800536 *ppos = p;
537 return virtr + wrote ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Russell King4f911d62007-05-08 00:28:17 -0700541#ifdef CONFIG_DEVPORT
Andrew Mortond7d4d842010-03-10 15:21:52 -0800542static ssize_t read_port(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 size_t count, loff_t *ppos)
544{
545 unsigned long i = *ppos;
546 char __user *tmp = buf;
547
548 if (!access_ok(VERIFY_WRITE, buf, count))
Andrew Mortond7d4d842010-03-10 15:21:52 -0800549 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 while (count-- > 0 && i < 65536) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800551 if (__put_user(inb(i), tmp) < 0)
552 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 i++;
554 tmp++;
555 }
556 *ppos = i;
557 return tmp-buf;
558}
559
Andrew Mortond7d4d842010-03-10 15:21:52 -0800560static ssize_t write_port(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 size_t count, loff_t *ppos)
562{
563 unsigned long i = *ppos;
Hans Grob890537b2013-02-06 11:37:20 +0100564 const char __user *tmp = buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Andrew Mortond7d4d842010-03-10 15:21:52 -0800566 if (!access_ok(VERIFY_READ, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -EFAULT;
568 while (count-- > 0 && i < 65536) {
569 char c;
Jan Beulichc654d602006-03-25 03:07:31 -0800570 if (__get_user(c, tmp)) {
571 if (tmp > buf)
572 break;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800573 return -EFAULT;
Jan Beulichc654d602006-03-25 03:07:31 -0800574 }
Andrew Mortond7d4d842010-03-10 15:21:52 -0800575 outb(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 i++;
577 tmp++;
578 }
579 *ppos = i;
580 return tmp-buf;
581}
582#endif
583
Andrew Mortond7d4d842010-03-10 15:21:52 -0800584static ssize_t read_null(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 size_t count, loff_t *ppos)
586{
587 return 0;
588}
589
Andrew Mortond7d4d842010-03-10 15:21:52 -0800590static ssize_t write_null(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 size_t count, loff_t *ppos)
592{
593 return count;
594}
595
Zach Brown162934d2013-05-07 16:18:27 -0700596static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
597 unsigned long nr_segs, loff_t pos)
598{
599 return 0;
600}
601
602static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
603 unsigned long nr_segs, loff_t pos)
604{
605 return iov_length(iov, nr_segs);
606}
607
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200608static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
609 struct splice_desc *sd)
610{
611 return sd->len;
612}
613
Andrew Mortond7d4d842010-03-10 15:21:52 -0800614static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200615 loff_t *ppos, size_t len, unsigned int flags)
616{
617 return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
618}
619
Andrew Mortond7d4d842010-03-10 15:21:52 -0800620static ssize_t read_zero(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 size_t count, loff_t *ppos)
622{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700623 size_t written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (!count)
626 return 0;
627
628 if (!access_ok(VERIFY_WRITE, buf, count))
629 return -EFAULT;
630
Nick Piggin557ed1f2007-10-16 01:24:40 -0700631 written = 0;
632 while (count) {
633 unsigned long unwritten;
634 size_t chunk = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Nick Piggin557ed1f2007-10-16 01:24:40 -0700636 if (chunk > PAGE_SIZE)
637 chunk = PAGE_SIZE; /* Just for latency reasons */
Nikanth Karthikesanbb521c52009-09-23 15:57:09 -0700638 unwritten = __clear_user(buf, chunk);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700639 written += chunk - unwritten;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (unwritten)
Nick Piggin557ed1f2007-10-16 01:24:40 -0700641 break;
Linus Torvalds2b838682009-06-09 20:40:25 -0700642 if (signal_pending(current))
643 return written ? written : -ERESTARTSYS;
Nick Piggin557ed1f2007-10-16 01:24:40 -0700644 buf += chunk;
645 count -= chunk;
646 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return written ? written : -EFAULT;
649}
650
Zach Brown162934d2013-05-07 16:18:27 -0700651static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
652 unsigned long nr_segs, loff_t pos)
653{
654 size_t written = 0;
655 unsigned long i;
656 ssize_t ret;
657
658 for (i = 0; i < nr_segs; i++) {
659 ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
660 &pos);
661 if (ret < 0)
662 break;
663 written += ret;
664 }
665
666 return written ? written : -EFAULT;
667}
668
Andrew Mortond7d4d842010-03-10 15:21:52 -0800669static int mmap_zero(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Nick Piggin557ed1f2007-10-16 01:24:40 -0700671#ifndef CONFIG_MMU
672 return -ENOSYS;
673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (vma->vm_flags & VM_SHARED)
675 return shmem_zero_setup(vma);
Nick Piggin557ed1f2007-10-16 01:24:40 -0700676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Andrew Mortond7d4d842010-03-10 15:21:52 -0800679static ssize_t write_full(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 size_t count, loff_t *ppos)
681{
682 return -ENOSPC;
683}
684
685/*
686 * Special lseek() function for /dev/null and /dev/zero. Most notably, you
687 * can fopen() both devices with "a" now. This was previously impossible.
688 * -- SRB.
689 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800690static loff_t null_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 return file->f_pos = 0;
693}
694
695/*
696 * The memory devices use the full 32/64 bits of the offset, and so we cannot
697 * check against negative addresses: they are ok. The return value is weird,
698 * though, in that case (0).
699 *
700 * also note that seeking relative to the "end of file" isn't supported:
701 * it has no meaning, so it returns -EINVAL.
702 */
Andrew Mortond7d4d842010-03-10 15:21:52 -0800703static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 loff_t ret;
706
Al Viro496ad9a2013-01-23 17:07:38 -0500707 mutex_lock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 switch (orig) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800709 case SEEK_CUR:
710 offset += file->f_pos;
Andrew Mortond7d4d842010-03-10 15:21:52 -0800711 case SEEK_SET:
712 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
Rasmus Villemoes71811f32013-06-05 17:09:39 +0000713 if (IS_ERR_VALUE((unsigned long long)offset)) {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800714 ret = -EOVERFLOW;
715 break;
716 }
717 file->f_pos = offset;
718 ret = file->f_pos;
719 force_successful_syscall_return();
720 break;
721 default:
722 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Al Viro496ad9a2013-01-23 17:07:38 -0500724 mutex_unlock(&file_inode(file)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return ret;
726}
727
Hans Grob890537b2013-02-06 11:37:20 +0100728static int open_port(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
731}
732
733#define zero_lseek null_lseek
734#define full_lseek null_lseek
735#define write_zero write_null
736#define read_full read_zero
Zach Brown162934d2013-05-07 16:18:27 -0700737#define aio_write_zero aio_write_null
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#define open_mem open_port
739#define open_kmem open_mem
740
Arjan van de Ven62322d22006-07-03 00:24:21 -0700741static const struct file_operations mem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .llseek = memory_lseek,
743 .read = read_mem,
744 .write = write_mem,
745 .mmap = mmap_mem,
746 .open = open_mem,
David Howells5da61852006-09-27 01:50:16 -0700747 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748};
749
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700750#ifdef CONFIG_DEVKMEM
Arjan van de Ven62322d22006-07-03 00:24:21 -0700751static const struct file_operations kmem_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 .llseek = memory_lseek,
753 .read = read_kmem,
754 .write = write_kmem,
755 .mmap = mmap_kmem,
756 .open = open_kmem,
David Howells5da61852006-09-27 01:50:16 -0700757 .get_unmapped_area = get_unmapped_area_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};
Arjan van de Venb781ecb2008-04-29 00:58:34 -0700759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Arjan van de Ven62322d22006-07-03 00:24:21 -0700761static const struct file_operations null_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 .llseek = null_lseek,
763 .read = read_null,
764 .write = write_null,
Zach Brown162934d2013-05-07 16:18:27 -0700765 .aio_read = aio_read_null,
766 .aio_write = aio_write_null,
Jens Axboe1ebd32f2006-04-26 14:40:08 +0200767 .splice_write = splice_write_null,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768};
769
Russell King4f911d62007-05-08 00:28:17 -0700770#ifdef CONFIG_DEVPORT
Arjan van de Ven62322d22006-07-03 00:24:21 -0700771static const struct file_operations port_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 .llseek = memory_lseek,
773 .read = read_port,
774 .write = write_port,
775 .open = open_port,
776};
777#endif
778
Arjan van de Ven62322d22006-07-03 00:24:21 -0700779static const struct file_operations zero_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 .llseek = zero_lseek,
781 .read = read_zero,
782 .write = write_zero,
Zach Brown162934d2013-05-07 16:18:27 -0700783 .aio_read = aio_read_zero,
784 .aio_write = aio_write_zero,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 .mmap = mmap_zero,
786};
787
David Howells5da61852006-09-27 01:50:16 -0700788/*
789 * capabilities for /dev/zero
790 * - permits private mappings, "copies" are taken of the source of zeros
Jan Kara371d2172010-09-21 11:49:01 +0200791 * - no writeback happens
David Howells5da61852006-09-27 01:50:16 -0700792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793static struct backing_dev_info zero_bdi = {
Jens Axboed9938312009-06-12 14:45:52 +0200794 .name = "char/mem",
Jan Kara371d2172010-09-21 11:49:01 +0200795 .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796};
797
Arjan van de Ven62322d22006-07-03 00:24:21 -0700798static const struct file_operations full_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .llseek = full_lseek,
800 .read = read_full,
801 .write = write_full,
802};
803
Kay Sievers389e0cb2009-07-04 16:51:29 +0200804static const struct memdev {
805 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400806 umode_t mode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200807 const struct file_operations *fops;
808 struct backing_dev_info *dev_info;
809} devlist[] = {
Kay Sieverse454cea2009-09-18 23:01:12 +0200810 [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700811#ifdef CONFIG_DEVKMEM
Kay Sieverse454cea2009-09-18 23:01:12 +0200812 [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700813#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200814 [3] = { "null", 0666, &null_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700815#ifdef CONFIG_DEVPORT
Kay Sieverse454cea2009-09-18 23:01:12 +0200816 [4] = { "port", 0, &port_fops, NULL },
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700817#endif
Kay Sieverse454cea2009-09-18 23:01:12 +0200818 [5] = { "zero", 0666, &zero_fops, &zero_bdi },
819 [7] = { "full", 0666, &full_fops, NULL },
820 [8] = { "random", 0666, &random_fops, NULL },
821 [9] = { "urandom", 0666, &urandom_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200822#ifdef CONFIG_PRINTK
Kay Sieverse11fea92012-05-03 02:29:41 +0200823 [11] = { "kmsg", 0644, &kmsg_fops, NULL },
Kay Sievers7f3a7812012-05-09 01:37:51 +0200824#endif
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700825};
826
827static int memory_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200829 int minor;
830 const struct memdev *dev;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700831
Kay Sievers389e0cb2009-07-04 16:51:29 +0200832 minor = iminor(inode);
833 if (minor >= ARRAY_SIZE(devlist))
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200834 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700835
Kay Sievers389e0cb2009-07-04 16:51:29 +0200836 dev = &devlist[minor];
837 if (!dev->fops)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200838 return -ENXIO;
Adriano dos Santos Fernandesd6f47be2009-06-17 16:27:48 -0700839
Kay Sievers389e0cb2009-07-04 16:51:29 +0200840 filp->f_op = dev->fops;
841 if (dev->dev_info)
842 filp->f_mapping->backing_dev_info = dev->dev_info;
843
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700844 /* Is /dev/mem or /dev/kmem ? */
845 if (dev->dev_info == &directly_mappable_cdev_bdi)
846 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
847
Kay Sievers389e0cb2009-07-04 16:51:29 +0200848 if (dev->fops->open)
Frederic Weisbecker205153a2009-10-09 20:31:02 +0200849 return dev->fops->open(inode, filp);
850
851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Arjan van de Ven62322d22006-07-03 00:24:21 -0700854static const struct file_operations memory_fops = {
Andrew Mortond7d4d842010-03-10 15:21:52 -0800855 .open = memory_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200856 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
Al Viro2c9ede52011-07-23 20:24:48 -0400859static char *mem_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +0200860{
861 if (mode && devlist[MINOR(dev->devt)].mode)
862 *mode = devlist[MINOR(dev->devt)].mode;
863 return NULL;
864}
865
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800866static struct class *mem_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868static int __init chr_dev_init(void)
869{
Kay Sievers389e0cb2009-07-04 16:51:29 +0200870 int minor;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700871 int err;
872
873 err = bdi_init(&zero_bdi);
874 if (err)
875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Andrew Mortond7d4d842010-03-10 15:21:52 -0800877 if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 printk("unable to get major %d for memory devs\n", MEM_MAJOR);
879
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800880 mem_class = class_create(THIS_MODULE, "mem");
Anton Blanchard6e191f72010-04-06 14:34:55 -0700881 if (IS_ERR(mem_class))
882 return PTR_ERR(mem_class);
883
Kay Sieverse454cea2009-09-18 23:01:12 +0200884 mem_class->devnode = mem_devnode;
Kay Sievers389e0cb2009-07-04 16:51:29 +0200885 for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
886 if (!devlist[minor].name)
887 continue;
Haren Mynenie1612de2012-07-11 15:18:44 +1000888
889 /*
Hans Grob890537b2013-02-06 11:37:20 +0100890 * Create /dev/port?
Haren Mynenie1612de2012-07-11 15:18:44 +1000891 */
892 if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
893 continue;
894
Kay Sievers389e0cb2009-07-04 16:51:29 +0200895 device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
896 NULL, devlist[minor].name);
897 }
Greg Kroah-Hartmanebf644c2006-07-25 17:13:31 -0700898
David Howells31d1d482010-08-06 16:34:43 +0100899 return tty_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902fs_initcall(chr_dev_init);